]> git.ipfire.org Git - thirdparty/glibc.git/blame - io/ftwtest.c
hurd: Avoid some PLTs in libc and librt
[thirdparty/glibc.git] / io / ftwtest.c
CommitLineData
d951286f
UD
1#include <ftw.h>
2#include <getopt.h>
3#include <mcheck.h>
4#include <stdio.h>
5#include <stdlib.h>
3d73829c 6#include <string.h>
d951286f
UD
7#include <unistd.h>
8#include <sys/stat.h>
9
10
11int do_depth;
12int do_chdir;
13int do_phys;
3d73829c 14int do_exit;
ca10f338
UD
15char *skip_subtree;
16char *skip_siblings;
d951286f
UD
17
18struct option options[] =
19{
20 { "depth", no_argument, &do_depth, 1 },
21 { "chdir", no_argument, &do_chdir, 1 },
22 { "phys", no_argument, &do_phys, 1 },
ca10f338
UD
23 { "skip-subtree", required_argument, NULL, 't' },
24 { "skip-siblings", required_argument, NULL, 's' },
3d73829c 25 { "early-exit", no_argument, &do_exit, 1 },
d951286f
UD
26 { NULL, 0, NULL, 0 }
27};
28
29const char *flag2name[] =
30{
31 [FTW_F] = "FTW_F",
32 [FTW_D] = "FTW_D",
33 [FTW_DNR] = "FTW_DNR",
34 [FTW_NS] = "FTW_NS",
35 [FTW_SL] = "FTW_SL",
36 [FTW_DP] = "FTW_DP",
37 [FTW_SLN] = "FTW_SLN"
38};
39
40
3d73829c 41static int
d951286f
UD
42cb (const char *name, const struct stat *st, int flag, struct FTW *f)
43{
3d73829c 44 if (do_exit && strcmp (name + f->base, "file@2"))
ca10f338 45 return FTW_CONTINUE;
3d73829c 46
d951286f
UD
47 printf ("base = \"%.*s\", file = \"%s\", flag = %s",
48 f->base, name, name + f->base, flag2name[flag]);
49 if (do_chdir)
50 {
51 char *cwd = getcwd (NULL, 0);
52 printf (", cwd = %s", cwd);
53 free (cwd);
54 }
2604afb1 55 printf (", level = %d\n", f->level);
ca10f338
UD
56
57 if (skip_siblings && strcmp (name + f->base, skip_siblings) == 0)
58 return FTW_SKIP_SIBLINGS;
59
60 if (skip_subtree && strcmp (name + f->base, skip_subtree) == 0)
61 return FTW_SKIP_SUBTREE;
62
63 return do_exit ? 26 : FTW_CONTINUE;
d951286f
UD
64}
65
66int
67main (int argc, char *argv[])
68{
69 int opt;
70 int r;
71 int flag = 0;
72 mtrace ();
73
74 while ((opt = getopt_long_only (argc, argv, "", options, NULL)) != -1)
ca10f338
UD
75 {
76 if (opt == 't')
77 skip_subtree = optarg;
78 else if (opt == 's')
79 skip_siblings = optarg;
80 }
d951286f
UD
81
82 if (do_chdir)
83 flag |= FTW_CHDIR;
84 if (do_depth)
85 flag |= FTW_DEPTH;
86 if (do_phys)
87 flag |= FTW_PHYS;
ca10f338
UD
88 if (skip_subtree || skip_siblings)
89 {
90 flag |= FTW_ACTIONRETVAL;
91 if (do_exit)
92 {
93 printf ("--early-exit cannot be used together with --skip-{siblings,subtree}");
94 exit (1);
95 }
96 }
d951286f 97
34c86f42
UD
98 char *cw1 = getcwd (NULL, 0);
99
3d73829c
UD
100 r = nftw (optind < argc ? argv[optind] : ".", cb, do_exit ? 1 : 3, flag);
101 if (r < 0)
d951286f 102 perror ("nftw");
34c86f42
UD
103
104 char *cw2 = getcwd (NULL, 0);
105
106 if (strcmp (cw1, cw2) != 0)
107 {
108 printf ("current working directory before and after nftw call differ:\n"
109 "before: %s\n"
110 "after: %s\n", cw1, cw2);
111 exit (1);
112 }
113
3d73829c
UD
114 if (do_exit)
115 {
116 puts (r == 26 ? "succeeded" : "failed");
117 return r == 26 ? 0 : 1;
118 }
d951286f
UD
119 return r;
120}