Define _POSIX_THREAD_PRIORITY_SCHEDULING.
[glibc/pb-stable.git] / io / ftwtest.c
blob4f527997df1db2bdbe8e9d04b9a0e8468fe2fb6a
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 char *cw1 = getcwd (NULL, 0);
75 r = nftw (optind < argc ? argv[optind] : ".", cb, do_exit ? 1 : 3, flag);
76 if (r < 0)
77 perror ("nftw");
79 char *cw2 = getcwd (NULL, 0);
81 if (strcmp (cw1, cw2) != 0)
83 printf ("current working directory before and after nftw call differ:\n"
84 "before: %s\n"
85 "after: %s\n", cw1, cw2);
86 exit (1);
89 if (do_exit)
91 puts (r == 26 ? "succeeded" : "failed");
92 return r == 26 ? 0 : 1;
94 return r;