-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnftw_usage.c
More file actions
executable file
·80 lines (72 loc) · 1.47 KB
/
nftw_usage.c
File metadata and controls
executable file
·80 lines (72 loc) · 1.47 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
Example usage of nftw, noting the difference between file
order when called with and without FTW_DEPTH
*/
#define _XOPEN_SOURCE 600
#include <ftw.h>
#include <unistd.h>
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
static int print;
void error(char *reason) {
puts(reason);
exit(EXIT_FAILURE);
}
int print_info(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
if (print) {
for (int i = 0; i < ftwbuf->level; i++)
printf("*");
printf("*\n");
} else {
puts("----------------------------------------------");
switch (typeflag) {
case FTW_F:
puts("Regular File: ");
break;
case FTW_D:
puts("Directory: ");
break;
case FTW_DNR:
puts("Directory (Can't Read): ");
break;
case FTW_DP:
puts("Directory (FTW_DEPTH): ");
break;
case FTW_NS:
puts("Can't Stat: (r-- .?): ");
break;
case FTW_SL:
puts("Sym Link (FTW_PHYS): ");
break;
case FTW_SLN:
puts("Dangling Symlink: ");
break;
}
puts(fpath);
puts("");
}
return 0;
}
int main(int argc, char **argv) {
char path[PATH_MAX], opt;
int flags;
flags = 0;
while ((opt = getopt(argc, argv, "dp")) != -1) {
switch (opt) {
case 'p':
print = 1;
break;
case 'd':
flags |= FTW_DEPTH;
break;
case '?':
error("usage: nftw_usage [-d] [-p]");
}
}
if (getcwd(path, PATH_MAX) == NULL)
error("getcwd1");
nftw(path, print_info, 20, flags);
exit(EXIT_SUCCESS);
}