Update.
[glibc.git] / io / ftwtest.c
blob607926517595149cca20cc83335a3b5fe2cd3c2e
1 #include <ftw.h>
2 #include <getopt.h>
3 #include <mcheck.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <unistd.h>
7 #include <sys/stat.h>
10 int do_depth;
11 int do_chdir;
12 int do_phys;
14 struct option options[] =
16 { "depth", no_argument, &do_depth, 1 },
17 { "chdir", no_argument, &do_chdir, 1 },
18 { "phys", no_argument, &do_phys, 1 },
19 { NULL, 0, NULL, 0 }
22 const char *flag2name[] =
24 [FTW_F] = "FTW_F",
25 [FTW_D] = "FTW_D",
26 [FTW_DNR] = "FTW_DNR",
27 [FTW_NS] = "FTW_NS",
28 [FTW_SL] = "FTW_SL",
29 [FTW_DP] = "FTW_DP",
30 [FTW_SLN] = "FTW_SLN"
34 int
35 cb (const char *name, const struct stat *st, int flag, struct FTW *f)
37 printf ("base = \"%.*s\", file = \"%s\", flag = %s",
38 f->base, name, name + f->base, flag2name[flag]);
39 if (do_chdir)
41 char *cwd = getcwd (NULL, 0);
42 printf (", cwd = %s", cwd);
43 free (cwd);
45 printf (", level = %d\n", f->level);
46 return 0;
49 int
50 main (int argc, char *argv[])
52 int opt;
53 int r;
54 int flag = 0;
55 mtrace ();
57 while ((opt = getopt_long_only (argc, argv, "", options, NULL)) != -1)
60 if (do_chdir)
61 flag |= FTW_CHDIR;
62 if (do_depth)
63 flag |= FTW_DEPTH;
64 if (do_phys)
65 flag |= FTW_PHYS;
67 r = nftw (optind < argc ? argv[optind] : ".", cb, 3, flag);
68 if (r)
69 perror ("nftw");
70 return r;