1 |
|
---|
2 |
|
---|
3 | /*********************************************************************************************************************************
|
---|
4 | * Header Files *
|
---|
5 | *********************************************************************************************************************************/
|
---|
6 | #ifndef USE_OLD_FTS
|
---|
7 | # include "fts-nt.h"
|
---|
8 | #else
|
---|
9 | # include "kmkbuiltin/ftsfake.h"
|
---|
10 | #endif
|
---|
11 | #include <stdlib.h>
|
---|
12 | #include <stdio.h>
|
---|
13 | #include <string.h>
|
---|
14 | #include <errno.h>
|
---|
15 |
|
---|
16 |
|
---|
17 | static int usage(const char *argv0)
|
---|
18 | {
|
---|
19 | printf("usage: %s [options] <dirs & files>\n", argv0);
|
---|
20 | printf("\n"
|
---|
21 | "options:\n"
|
---|
22 | " -d, --see-dot\n"
|
---|
23 | " FTS_SEEDOT\n"
|
---|
24 | " -p, --physical\n"
|
---|
25 | " FTS_PHYSICAL\n"
|
---|
26 | " -l, --logical\n"
|
---|
27 | " FTS_LOGICAL\n"
|
---|
28 | " -H, --dereference-command-line\n"
|
---|
29 | " FTS_COMFOLLOW\n"
|
---|
30 | " -L, --dereference\n"
|
---|
31 | " Follow symbolic links while scanning directories.\n"
|
---|
32 | " -P, --no-dereference\n"
|
---|
33 | " Do not follow symbolic links while scanning directories.\n"
|
---|
34 | " -c, --no-chdir\n"
|
---|
35 | " FTS_NOCHDIR\n"
|
---|
36 | " -s, --no-stat\n"
|
---|
37 | " FTS_NOSTAT\n"
|
---|
38 | " -x, --one-file-system\n"
|
---|
39 | " FTS_XDEV\n"
|
---|
40 | " -q, --quiet\n"
|
---|
41 | " Quiet operation, no output.\n"
|
---|
42 | " -v, --verbose\n"
|
---|
43 | " Verbose operation (default).\n"
|
---|
44 | );
|
---|
45 | return 0;
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 | int main(int argc, char **argv)
|
---|
50 | {
|
---|
51 | FTS *pFts;
|
---|
52 | int i;
|
---|
53 | int rcExit = 0;
|
---|
54 | int cVerbosity = 1;
|
---|
55 | int fFollowLinks = 0;
|
---|
56 | int fFtsFlags = 0;
|
---|
57 | unsigned fDoneOptions = 0;
|
---|
58 | unsigned cFtsArgs = 0;
|
---|
59 | char const **papszFtsArgs = calloc(argc + 1, sizeof(char *));
|
---|
60 |
|
---|
61 | /*
|
---|
62 | * Parse options and heap up non-options.
|
---|
63 | */
|
---|
64 | for (i = 1; i < argc; i++)
|
---|
65 | {
|
---|
66 | const char *pszArg = argv[i];
|
---|
67 | if (*pszArg == '-' && !fDoneOptions)
|
---|
68 | {
|
---|
69 | char chOpt = *++pszArg;
|
---|
70 | pszArg++;
|
---|
71 | if (chOpt == '-')
|
---|
72 | {
|
---|
73 | if (!chOpt)
|
---|
74 | {
|
---|
75 | fDoneOptions = 1;
|
---|
76 | continue;
|
---|
77 | }
|
---|
78 | if (strcmp(pszArg, "help") == 0)
|
---|
79 | chOpt = 'h';
|
---|
80 | else if (strcmp(pszArg, "version") == 0)
|
---|
81 | chOpt = 'V';
|
---|
82 | else if (strcmp(pszArg, "see-dot") == 0)
|
---|
83 | chOpt = 'd';
|
---|
84 | else if (strcmp(pszArg, "physical") == 0)
|
---|
85 | chOpt = 'p';
|
---|
86 | else if (strcmp(pszArg, "logical") == 0)
|
---|
87 | chOpt = 'l';
|
---|
88 | else if (strcmp(pszArg, "dereference-command-line") == 0)
|
---|
89 | chOpt = 'H';
|
---|
90 | else if (strcmp(pszArg, "no-chdir") == 0)
|
---|
91 | chOpt = 'c';
|
---|
92 | else if (strcmp(pszArg, "no-stat") == 0)
|
---|
93 | chOpt = 's';
|
---|
94 | else if (strcmp(pszArg, "one-file-system") == 0)
|
---|
95 | chOpt = 'x';
|
---|
96 | else if (strcmp(pszArg, "quiet") == 0)
|
---|
97 | chOpt = 'q';
|
---|
98 | else if (strcmp(pszArg, "verbose") == 0)
|
---|
99 | chOpt = 'v';
|
---|
100 | else if (strcmp(pszArg, "no-ansi") == 0)
|
---|
101 | chOpt = 'w';
|
---|
102 | else
|
---|
103 | {
|
---|
104 | fprintf(stderr, "syntax error: Unknown option: %s (%s)\n", argv[i], pszArg);
|
---|
105 | return 2;
|
---|
106 | }
|
---|
107 | pszArg = "";
|
---|
108 | }
|
---|
109 | do
|
---|
110 | {
|
---|
111 | switch (chOpt)
|
---|
112 | {
|
---|
113 | case '?':
|
---|
114 | case 'h':
|
---|
115 | return usage(argv[0]);
|
---|
116 | case 'V':
|
---|
117 | printf("v0.0.0\n");
|
---|
118 | return 0;
|
---|
119 |
|
---|
120 | case 'd':
|
---|
121 | fFtsFlags |= FTS_SEEDOT;
|
---|
122 | break;
|
---|
123 | case 'l':
|
---|
124 | fFtsFlags |= FTS_LOGICAL;
|
---|
125 | break;
|
---|
126 | case 'p':
|
---|
127 | fFtsFlags |= FTS_PHYSICAL;
|
---|
128 | break;
|
---|
129 | case 'H':
|
---|
130 | fFtsFlags |= FTS_COMFOLLOW;
|
---|
131 | break;
|
---|
132 | case 'c':
|
---|
133 | fFtsFlags |= FTS_NOCHDIR;
|
---|
134 | break;
|
---|
135 | case 's':
|
---|
136 | fFtsFlags |= FTS_NOSTAT;
|
---|
137 | break;
|
---|
138 | case 'x':
|
---|
139 | fFtsFlags |= FTS_XDEV;
|
---|
140 | break;
|
---|
141 | #ifdef FTS_NO_ANSI
|
---|
142 | case 'w':
|
---|
143 | fFtsFlags |= FTS_NO_ANSI;
|
---|
144 | break;
|
---|
145 | #endif
|
---|
146 | case 'L':
|
---|
147 | fFollowLinks = 1;
|
---|
148 | break;
|
---|
149 | case 'P':
|
---|
150 | fFollowLinks = 0;
|
---|
151 | break;
|
---|
152 |
|
---|
153 | case 'q':
|
---|
154 | cVerbosity = 0;
|
---|
155 | break;
|
---|
156 | case 'v':
|
---|
157 | cVerbosity++;
|
---|
158 | break;
|
---|
159 |
|
---|
160 | default:
|
---|
161 | fprintf(stderr, "syntax error: Unknown option: -%c (%s)\n", chOpt, argv[i]);
|
---|
162 | return 2;
|
---|
163 | }
|
---|
164 | chOpt = *pszArg++;
|
---|
165 | } while (chOpt != '\0');
|
---|
166 | }
|
---|
167 | else
|
---|
168 | papszFtsArgs[cFtsArgs++] = pszArg;
|
---|
169 | }
|
---|
170 |
|
---|
171 | #ifdef USE_OLD_FTS
|
---|
172 | if (papszFtsArgs[0] == NULL)
|
---|
173 | {
|
---|
174 | fprintf(stderr, "Nothing to do\n");
|
---|
175 | return 1;
|
---|
176 | }
|
---|
177 | #endif
|
---|
178 |
|
---|
179 | /*
|
---|
180 | * Do the traversal.
|
---|
181 | */
|
---|
182 | errno = 0;
|
---|
183 | pFts = fts_open((char **)papszFtsArgs, fFtsFlags, NULL /*pfnCompare*/);
|
---|
184 | if (pFts)
|
---|
185 | {
|
---|
186 | for (;;)
|
---|
187 | {
|
---|
188 | FTSENT *pFtsEnt = fts_read(pFts);
|
---|
189 | if (pFtsEnt)
|
---|
190 | {
|
---|
191 | const char *pszState;
|
---|
192 | switch (pFtsEnt->fts_info)
|
---|
193 | {
|
---|
194 | case FTS_D: pszState = "D"; break;
|
---|
195 | case FTS_DC: pszState = "DC"; break;
|
---|
196 | case FTS_DEFAULT: pszState = "DEFAULT"; break;
|
---|
197 | case FTS_DNR: pszState = "DNR"; break;
|
---|
198 | case FTS_DOT: pszState = "DOT"; break;
|
---|
199 | case FTS_DP: pszState = "DP"; break;
|
---|
200 | case FTS_ERR: pszState = "ERR"; break;
|
---|
201 | case FTS_F: pszState = "F"; break;
|
---|
202 | case FTS_INIT: pszState = "INIT"; break;
|
---|
203 | case FTS_NS: pszState = "NS"; break;
|
---|
204 | case FTS_NSOK: pszState = "NSOK"; break;
|
---|
205 | case FTS_SL: pszState = "SL"; break;
|
---|
206 | case FTS_SLNONE: pszState = "SLNONE"; break;
|
---|
207 | default:
|
---|
208 | pszState = "Invalid";
|
---|
209 | rcExit = 1;
|
---|
210 | break;
|
---|
211 | }
|
---|
212 |
|
---|
213 | if (cVerbosity > 0)
|
---|
214 | {
|
---|
215 | #ifdef FTS_NO_ANSI
|
---|
216 | if (fFtsFlags & FTS_NO_ANSI)
|
---|
217 | printf("%8s %ls\n", pszState, pFtsEnt->fts_wcsaccpath);
|
---|
218 | else
|
---|
219 | #endif
|
---|
220 | printf("%8s %s\n", pszState, pFtsEnt->fts_accpath);
|
---|
221 | }
|
---|
222 | if ( pFtsEnt->fts_info == FTS_SL
|
---|
223 | && pFtsEnt->fts_number == 0
|
---|
224 | && fFollowLinks
|
---|
225 | && ( (fFtsFlags & FTS_COMFOLLOW)
|
---|
226 | || pFtsEnt->fts_level > FTS_ROOTLEVEL) ) {
|
---|
227 | pFtsEnt->fts_number++;
|
---|
228 | fts_set(pFts, pFtsEnt, FTS_FOLLOW);
|
---|
229 | }
|
---|
230 | }
|
---|
231 | else
|
---|
232 | {
|
---|
233 | if (errno != 0)
|
---|
234 | {
|
---|
235 | fprintf(stderr, "fts_read failed: errno=%d\n", errno);
|
---|
236 | rcExit = 1;
|
---|
237 | }
|
---|
238 | break;
|
---|
239 | }
|
---|
240 | } /* enum loop */
|
---|
241 |
|
---|
242 | errno = 0;
|
---|
243 | i = fts_close(pFts);
|
---|
244 | if (i != 0)
|
---|
245 | {
|
---|
246 | fprintf(stderr, "fts_close failed: errno=%d\n", errno);
|
---|
247 | rcExit = 1;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | else
|
---|
251 | {
|
---|
252 | fprintf(stderr, "fts_open failed: errno=%d (cFtsArgs=%u)\n", errno, cFtsArgs);
|
---|
253 | rcExit = 1;
|
---|
254 | }
|
---|
255 |
|
---|
256 | return rcExit;
|
---|
257 | }
|
---|