Update.
[platform/upstream/glibc.git] / io / ftwtest.c
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>
8
9
10 int do_depth;
11 int do_chdir;
12 int do_phys;
13
14 struct option options[] =
15 {
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 }
20 };
21
22 const char *flag2name[] =
23 {
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"
31 };
32
33
34 int
35 cb (const char *name, const struct stat *st, int flag, struct FTW *f)
36 {
37   printf ("base = \"%.*s\", file = \"%s\", flag = %s",
38           f->base, name, name + f->base, flag2name[flag]);
39   if (do_chdir)
40     {
41       char *cwd = getcwd (NULL, 0);
42       printf (", cwd = %s", cwd);
43       free (cwd);
44     }
45   puts ("");
46   return 0;
47 }
48
49 int
50 main (int argc, char *argv[])
51 {
52   int opt;
53   int r;
54   int flag = 0;
55   mtrace ();
56
57   while ((opt = getopt_long_only (argc, argv, "", options, NULL)) != -1)
58     ;
59
60   if (do_chdir)
61     flag |= FTW_CHDIR;
62   if (do_depth)
63     flag |= FTW_DEPTH;
64   if (do_phys)
65     flag |= FTW_PHYS;
66
67   r = nftw (optind < argc ? argv[optind] : ".", cb, 3, flag);
68   if (r)
69     perror ("nftw");
70   return r;
71 }