blob: 8f0352cfdd5c7fba71d505f40de60f990da437ba (
plain)
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 <dirent.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void recurse(const char *path) {
DIR *d = opendir(path);
if (!d) {
warn("couldn't open %s", path);
return;
}
for (;;) {
struct dirent *dent;
errno = 0;
dent = readdir(d);
if (!dent) {
if (errno) {
warn("when reading %s", path);
}
break;
}
printf("%s%s\n", path, dent->d_name);
/* if the string ends with '/' */
if (strchr(dent->d_name, '\0')[-1] == '/') {
char *next;
if (asprintf(&next, "%s%s", path, dent->d_name) >= 0) {
recurse(next);
free(next);
}
}
}
closedir(d);
}
int main(int argc, char **argv) {
if (argc < 2) {
recurse("/");
} else {
for (int i = 1; i < argc; i++)
recurse(argv[i]);
}
return 0;
}
|