18 struct option options
[] =
20 { "depth", no_argument
, &do_depth
, 1 },
21 { "chdir", no_argument
, &do_chdir
, 1 },
22 { "phys", no_argument
, &do_phys
, 1 },
23 { "skip-subtree", required_argument
, NULL
, 't' },
24 { "skip-siblings", required_argument
, NULL
, 's' },
25 { "early-exit", no_argument
, &do_exit
, 1 },
29 const char *flag2name
[] =
33 [FTW_DNR
] = "FTW_DNR",
42 cb (const char *name
, const struct stat
*st
, int flag
, struct FTW
*f
)
44 if (do_exit
&& strcmp (name
+ f
->base
, "file@2"))
47 printf ("base = \"%.*s\", file = \"%s\", flag = %s",
48 f
->base
, name
, name
+ f
->base
, flag2name
[flag
]);
51 char *cwd
= getcwd (NULL
, 0);
52 printf (", cwd = %s", cwd
);
55 printf (", level = %d\n", f
->level
);
57 if (skip_siblings
&& strcmp (name
+ f
->base
, skip_siblings
) == 0)
58 return FTW_SKIP_SIBLINGS
;
60 if (skip_subtree
&& strcmp (name
+ f
->base
, skip_subtree
) == 0)
61 return FTW_SKIP_SUBTREE
;
63 return do_exit
? 26 : FTW_CONTINUE
;
67 main (int argc
, char *argv
[])
74 while ((opt
= getopt_long_only (argc
, argv
, "", options
, NULL
)) != -1)
77 skip_subtree
= optarg
;
79 skip_siblings
= optarg
;
88 if (skip_subtree
|| skip_siblings
)
90 flag
|= FTW_ACTIONRETVAL
;
93 printf ("--early-exit cannot be used together with --skip-{siblings,subtree}");
98 char *cw1
= getcwd (NULL
, 0);
100 r
= nftw (optind
< argc
? argv
[optind
] : ".", cb
, do_exit
? 1 : 3, flag
);
104 char *cw2
= getcwd (NULL
, 0);
106 if (strcmp (cw1
, cw2
) != 0)
108 printf ("current working directory before and after nftw call differ:\n"
110 "after: %s\n", cw1
, cw2
);
116 puts (r
== 26 ? "succeeded" : "failed");
117 return r
== 26 ? 0 : 1;