source: branches/libc-0.6/src/libctests/glibc/io/ftwtest.c

Last change on this file was 2176, checked in by bird, 20 years ago

..

  • Property cvs2svn:cvs-rev set to 1.2
  • Property svn:eol-style set to native
  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.6 KB
Line 
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>
9#ifdef FTW_CONTINUE
10#define FTW_CONTINUE 0
11#endif
12#ifdef FTW_STOP
13#define FTW_STOP 1
14#endif
15
16
17
18int do_depth;
19int do_chdir;
20int do_phys;
21int do_exit;
22char *skip_subtree;
23char *skip_siblings;
24
25struct option options[] =
26{
27 { "depth", no_argument, &do_depth, 1 },
28 { "chdir", no_argument, &do_chdir, 1 },
29 { "phys", no_argument, &do_phys, 1 },
30 { "skip-subtree", required_argument, NULL, 't' },
31 { "skip-siblings", required_argument, NULL, 's' },
32 { "early-exit", no_argument, &do_exit, 1 },
33 { NULL, 0, NULL, 0 }
34};
35
36const char *flag2name[] =
37{
38 [FTW_F] = "FTW_F",
39 [FTW_D] = "FTW_D",
40 [FTW_DNR] = "FTW_DNR",
41 [FTW_NS] = "FTW_NS",
42 [FTW_SL] = "FTW_SL",
43 [FTW_DP] = "FTW_DP",
44 [FTW_SLN] = "FTW_SLN"
45};
46
47
48static int
49cb (const char *name, const struct stat *st, int flag, struct FTW *f)
50{
51 if (do_exit && strcmp (name + f->base, "file@2"))
52 return FTW_CONTINUE;
53
54 printf ("base = \"%.*s\", file = \"%s\", flag = %s",
55 f->base, name, name + f->base, flag2name[flag]);
56 if (do_chdir)
57 {
58 char *cwd = getcwd (NULL, 0);
59 printf (", cwd = %s", cwd);
60 free (cwd);
61 }
62 printf (", level = %d\n", f->level);
63
64#ifdef FTW_ACTIONRETVAL
65 if (skip_siblings && strcmp (name + f->base, skip_siblings) == 0)
66 return FTW_SKIP_SIBLINGS;
67
68 if (skip_subtree && strcmp (name + f->base, skip_subtree) == 0)
69 return FTW_SKIP_SUBTREE;
70#endif
71
72 return do_exit ? 26 : FTW_CONTINUE;
73}
74
75int
76main (int argc, char *argv[])
77{
78 int opt;
79 int r;
80 int flag = 0;
81 mtrace ();
82
83 while ((opt = getopt_long_only (argc, argv, "", options, NULL)) != -1)
84 {
85 if (opt == 't')
86 skip_subtree = optarg;
87 else if (opt == 's')
88 skip_siblings = optarg;
89 }
90
91 if (do_chdir)
92 flag |= FTW_CHDIR;
93 if (do_depth)
94 flag |= FTW_DEPTH;
95 if (do_phys)
96 flag |= FTW_PHYS;
97 if (skip_subtree || skip_siblings)
98 {
99#ifdef FTW_ACTIONRETVAL
100 flag |= FTW_ACTIONRETVAL;
101#endif
102 if (do_exit)
103 {
104 printf ("--early-exit cannot be used together with --skip-{siblings,subtree}");
105 exit (1);
106 }
107 }
108
109 char *cw1 = getcwd (NULL, 0);
110
111 r = nftw (optind < argc ? argv[optind] : ".", cb, do_exit ? 1 : 3, flag);
112 if (r < 0)
113 perror ("nftw");
114
115 char *cw2 = getcwd (NULL, 0);
116
117 if (strcmp (cw1, cw2) != 0)
118 {
119 printf ("current working directory before and after nftw call differ:\n"
120 "before: %s\n"
121 "after: %s\n", cw1, cw2);
122 exit (1);
123 }
124
125 if (do_exit)
126 {
127 puts (r == 26 ? "succeeded" : "failed");
128 return r == 26 ? 0 : 1;
129 }
130 return r;
131}
Note: See TracBrowser for help on using the repository browser.