blob: d473b82321a7bf7dbf202f525133bc2c231cdc00 (
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
46
47
48
49
50
51
52
53
54
55
|
#include <camellia/path.h>
#include <dirent.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void recurse(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] == '/') {
// TODO no overflow check
char *pend = strchr(path, '\0');
strcpy(pend, dent->d_name);
recurse(path);
*pend = '\0';
}
}
closedir(d);
}
void find(const char *path) {
// TODO bound checking
// TODO or just implement asprintf()
char *buf = malloc(PATH_MAX);
memcpy(buf, path, strlen(path)+1);
recurse(buf);
free(buf);
}
int main(int argc, char **argv) {
if (argc < 2) {
find("/");
} else {
for (int i = 1; i < argc; i++)
find(argv[i]);
}
return 0;
}
|