* sysdeps/m68k/Makefile (CFLAGS-.oS): Append -fPIC.
[glibc.git] / io / ftwtest.c
blob851ed1d5870ef462f7ed5645a3662a8a81a78f6d
1 #include <ftw.h>
2 #include <getopt.h>
3 #include <mcheck.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <sys/stat.h>
11 int do_depth;
12 int do_chdir;
13 int do_phys;
14 int do_exit;
16 struct option options[] =
18 { "depth", no_argument, &do_depth, 1 },
19 { "chdir", no_argument, &do_chdir, 1 },
20 { "phys", no_argument, &do_phys, 1 },
21 { "early-exit", no_argument, &do_exit, 1 },
22 { NULL, 0, NULL, 0 }
25 const char *flag2name[] =
27 [FTW_F] = "FTW_F",
28 [FTW_D] = "FTW_D",
29 [FTW_DNR] = "FTW_DNR",
30 [FTW_NS] = "FTW_NS",
31 [FTW_SL] = "FTW_SL",
32 [FTW_DP] = "FTW_DP",
33 [FTW_SLN] = "FTW_SLN"
37 static int
38 cb (const char *name, const struct stat *st, int flag, struct FTW *f)
40 if (do_exit && strcmp (name + f->base, "file@2"))
41 return 0;
43 printf ("base = \"%.*s\", file = \"%s\", flag = %s",
44 f->base, name, name + f->base, flag2name[flag]);
45 if (do_chdir)
47 char *cwd = getcwd (NULL, 0);
48 printf (", cwd = %s", cwd);
49 free (cwd);
51 printf (", level = %d\n", f->level);
52 return do_exit ? 26 : 0;
55 int
56 main (int argc, char *argv[])
58 int opt;
59 int r;
60 int flag = 0;
61 mtrace ();
63 while ((opt = getopt_long_only (argc, argv, "", options, NULL)) != -1)
66 if (do_chdir)
67 flag |= FTW_CHDIR;
68 if (do_depth)
69 flag |= FTW_DEPTH;
70 if (do_phys)
71 flag |= FTW_PHYS;
73 r = nftw (optind < argc ? argv[optind] : ".", cb, do_exit ? 1 : 3, flag);
74 if (r < 0)
75 perror ("nftw");
76 if (do_exit)
78 puts (r == 26 ? "succeeded" : "failed");
79 return r == 26 ? 0 : 1;
81 return r;