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 },
25 const char *flag2name
[] =
29 [FTW_DNR
] = "FTW_DNR",
38 cb (const char *name
, const struct stat
*st
, int flag
, struct FTW
*f
)
40 if (do_exit
&& strcmp (name
+ f
->base
, "file@2"))
43 printf ("base = \"%.*s\", file = \"%s\", flag = %s",
44 f
->base
, name
, name
+ f
->base
, flag2name
[flag
]);
47 char *cwd
= getcwd (NULL
, 0);
48 printf (", cwd = %s", cwd
);
51 printf (", level = %d\n", f
->level
);
52 return do_exit
? 26 : 0;
56 main (int argc
, char *argv
[])
63 while ((opt
= getopt_long_only (argc
, argv
, "", options
, NULL
)) != -1)
73 char *cw1
= getcwd (NULL
, 0);
75 r
= nftw (optind
< argc
? argv
[optind
] : ".", cb
, do_exit
? 1 : 3, flag
);
79 char *cw2
= getcwd (NULL
, 0);
81 if (strcmp (cw1
, cw2
) != 0)
83 printf ("current working directory before and after nftw call differ:\n"
85 "after: %s\n", cw1
, cw2
);
91 puts (r
== 26 ? "succeeded" : "failed");
92 return r
== 26 ? 0 : 1;