-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathftw2.c
62 lines (57 loc) · 2.32 KB
/
ftw2.c
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
/* nftw_dir_tree.c
Demonstrate the use of nftw(3). Walk though the directory tree specified
on the command line (or the current working directory if no directory
is specified on the command line), displaying an indented hierarchy
of files in the tree. For each file, display:
* a letter indicating the file type (using the same letters as "ls -l")
* a string indicating the file type, as supplied by nftw()
* the file's i-node number.
*/
#define _XOPEN_SOURCE 600 /* Get nftw() */
#include <ftw.h>
#include <sys/types.h> /* Type definitions used by many programs */
#include <stdio.h> /* Standard I/O functions */
#include <stdlib.h> /* Prototypes of commonly used library functions,
plus EXIT_SUCCESS and EXIT_FAILURE constants */
#include <unistd.h> /* Prototypes for many system calls */
#include <errno.h> /* Declares errno and defines error constants */
#include <string.h> /* Commonly used string-handling functions */
static int /* Callback function called by ftw() */
dirTree(const char *pathname, const struct stat *sbuf, int type, struct FTW *ftwb)
{
if (type == FTW_NS) { /* Could not stat() file */
printf("?");
} else {
switch (sbuf->st_mode & S_IFMT) { /* Print file type */
case S_IFREG: printf("-"); break;
case S_IFDIR: printf("d"); break;
case S_IFCHR: printf("c"); break;
case S_IFBLK: printf("b"); break;
case S_IFLNK: printf("l"); break;
case S_IFIFO: printf("p"); break;
case S_IFSOCK: printf("s"); break;
default: printf("?"); break; /* Should never happen (on Linux) */
}
}
if (type != FTW_NS)
printf("%7ld ", (long) sbuf->st_ino);
else
printf(" ");
printf(" %*s", 4 * ftwb->level, " "); /* Indent suitably */
printf("%s\n", &pathname[ftwb->base]); /* Print basename */
return 0; /* Tell nftw() to continue */
}
int
main(int argc, char *argv[])
{
int flags = 0;
if (argc != 2) {
fprintf(stderr, "Usage: %s directory-path\n", argv[0]);
exit(EXIT_FAILURE);
}
if (nftw(argv[1], dirTree, 10, flags) == -1) {
perror("nftw");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}