-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbasic_listdir.c
More file actions
executable file
·46 lines (38 loc) · 897 Bytes
/
Copy pathbasic_listdir.c
File metadata and controls
executable file
·46 lines (38 loc) · 897 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h>
int main(int argc, char **argv)
{
DIR *dir;
struct dirent *entry;
char *dirname = NULL;
if (argc == 1)
dirname = ".";
else
dirname = argv[1];
dir = opendir(dirname);
if (!dir) {
fprintf(stderr, "failed to open %s\n", dirname);
return -1;
}
while (entry = readdir(dir)) {
switch (entry->d_type) {
case DT_DIR:
fprintf(stderr, "dir ");
break;
case DT_REG:
fprintf(stderr, "reg ");
break;
default:
fprintf(stderr, "unknown ");
break;
}
fprintf(stderr, "%s\n", entry->d_name);
}
closedir(dir);
return 0;
}