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 | case 'w':
|
---|
142 | fFtsFlags |= FTS_NO_ANSI;
|
---|
143 | break;
|
---|
144 | case 'L':
|
---|
145 | fFollowLinks = 1;
|
---|
146 | break;
|
---|
147 | case 'P':
|
---|
148 | fFollowLinks = 0;
|
---|
149 | break;
|
---|
150 |
|
---|
151 | case 'q':
|
---|
152 | cVerbosity = 0;
|
---|
153 | break;
|
---|
154 | case 'v':
|
---|
155 | cVerbosity++;
|
---|
156 | break;
|
---|
157 |
|
---|
158 | default:
|
---|
159 | fprintf(stderr, "syntax error: Unknown option: -%c (%s)\n", chOpt, argv[i]);
|
---|
160 | return 2;
|
---|
161 | }
|
---|
162 | chOpt = *pszArg++;
|
---|
163 | } while (chOpt != '\0');
|
---|
164 | }
|
---|
165 | else
|
---|
166 | papszFtsArgs[cFtsArgs++] = pszArg;
|
---|
167 | }
|
---|
168 |
|
---|
169 | #ifdef USE_OLD_FTS
|
---|
170 | if (papszFtsArgs[0] == NULL)
|
---|
171 | {
|
---|
172 | fprintf(stderr, "Nothing to do\n");
|
---|
173 | return 1;
|
---|
174 | }
|
---|
175 | #endif
|
---|
176 |
|
---|
177 | /*
|
---|
178 | * Do the traversal.
|
---|
179 | */
|
---|
180 | errno = 0;
|
---|
181 | pFts = fts_open((char **)papszFtsArgs, fFtsFlags, NULL /*pfnCompare*/);
|
---|
182 | if (pFts)
|
---|
183 | {
|
---|
184 | for (;;)
|
---|
185 | {
|
---|
186 | FTSENT *pFtsEnt = fts_read(pFts);
|
---|
187 | if (pFtsEnt)
|
---|
188 | {
|
---|
189 | const char *pszState;
|
---|
190 | switch (pFtsEnt->fts_info)
|
---|
191 | {
|
---|
192 | case FTS_D: pszState = "D"; break;
|
---|
193 | case FTS_DC: pszState = "DC"; break;
|
---|
194 | case FTS_DEFAULT: pszState = "DEFAULT"; break;
|
---|
195 | case FTS_DNR: pszState = "DNR"; break;
|
---|
196 | case FTS_DOT: pszState = "DOT"; break;
|
---|
197 | case FTS_DP: pszState = "DP"; break;
|
---|
198 | case FTS_ERR: pszState = "ERR"; break;
|
---|
199 | case FTS_F: pszState = "F"; break;
|
---|
200 | case FTS_INIT: pszState = "INIT"; break;
|
---|
201 | case FTS_NS: pszState = "NS"; break;
|
---|
202 | case FTS_NSOK: pszState = "NSOK"; break;
|
---|
203 | case FTS_SL: pszState = "SL"; break;
|
---|
204 | case FTS_SLNONE: pszState = "SLNONE"; break;
|
---|
205 | default:
|
---|
206 | pszState = "Invalid";
|
---|
207 | rcExit = 1;
|
---|
208 | break;
|
---|
209 | }
|
---|
210 |
|
---|
211 | if (cVerbosity > 0)
|
---|
212 | {
|
---|
213 | if (fFtsFlags & FTS_NO_ANSI)
|
---|
214 | printf("%8s %ls\n", pszState, pFtsEnt->fts_wcsaccpath);
|
---|
215 | else
|
---|
216 | printf("%8s %s\n", pszState, pFtsEnt->fts_accpath);
|
---|
217 | }
|
---|
218 | if ( pFtsEnt->fts_info == FTS_SL
|
---|
219 | && pFtsEnt->fts_number == 0
|
---|
220 | && fFollowLinks
|
---|
221 | && ( (fFtsFlags & FTS_COMFOLLOW)
|
---|
222 | || pFtsEnt->fts_level > FTS_ROOTLEVEL) ) {
|
---|
223 | pFtsEnt->fts_number++;
|
---|
224 | fts_set(pFts, pFtsEnt, FTS_FOLLOW);
|
---|
225 | }
|
---|
226 | }
|
---|
227 | else
|
---|
228 | {
|
---|
229 | if (errno != 0)
|
---|
230 | {
|
---|
231 | fprintf(stderr, "fts_read failed: errno=%d\n", errno);
|
---|
232 | rcExit = 1;
|
---|
233 | }
|
---|
234 | break;
|
---|
235 | }
|
---|
236 | } /* enum loop */
|
---|
237 |
|
---|
238 | errno = 0;
|
---|
239 | i = fts_close(pFts);
|
---|
240 | if (i != 0)
|
---|
241 | {
|
---|
242 | fprintf(stderr, "fts_close failed: errno=%d\n", errno);
|
---|
243 | rcExit = 1;
|
---|
244 | }
|
---|
245 | }
|
---|
246 | else
|
---|
247 | {
|
---|
248 | fprintf(stderr, "fts_open failed: errno=%d (cFtsArgs=%u)\n", errno, cFtsArgs);
|
---|
249 | rcExit = 1;
|
---|
250 | }
|
---|
251 |
|
---|
252 | return rcExit;
|
---|
253 | }
|
---|