-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnftw_stats.c
More file actions
executable file
·70 lines (62 loc) · 1.41 KB
/
nftw_stats.c
File metadata and controls
executable file
·70 lines (62 loc) · 1.41 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
/**
Uses nftw() to walk through a directory tree and print out the counts
and percentages of the various types (dir, reg, sym, etc).
*/
#define _XOPEN_SOURCE 600
#include <ftw.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
static int syms, regs, dirs, unknowns;
void error(char *reason) {
puts(reason);
exit(EXIT_FAILURE);
}
int count_types(const char *fpath, const struct stat *sb, int typeFlag, struct FTW *ftwbuf) {
switch (typeFlag) {
case FTW_F:
regs++;
break;
case FTW_D:
dirs++;
break;
case FTW_DNR:
dirs++;
break;
case FTW_DP:
dirs++;
break;
case FTW_NS:
unknowns++;
break;
case FTW_SL:
syms++;
break;
case FTW_SLN:
syms++;
break;
}
return 0;
}
void report_stats() {
int total;
double pregs, pdirs, psyms, punknowns;
total = regs + dirs + syms + unknowns;
pregs = 100.0 * regs / total;
pdirs = 100.0 * dirs / total;
psyms = 100.0 * syms / total;
punknowns = 100.0 * syms / total;
printf("\t\tRegular\t\tDirectory\tSymbolic\tUnknown\n");
printf("Count:\t\t%d\t\t%d\t\t%d\t\t%d\n", regs, dirs, syms, unknowns);
printf("Percent:\t%2.1f\t\t%2.1f\t\t%2.1f\t\t%2.1f\n", pregs, pdirs, psyms, punknowns);
}
int main(int argc, char **argv) {
char path[PATH_MAX];
if (getcwd(path, PATH_MAX) == NULL)
error("getcwd");
if(nftw(path, count_types, 20, FTW_PHYS) == -1)
error("nftw");
report_stats();
exit(EXIT_SUCCESS);
}