aboutsummaryrefslogtreecommitdiff
path: root/findutils/find.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-10-30 02:10:47 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-10-30 02:10:47 +0000
commit6191a7a007ff2e22cbf48f323d4e11d9b959fe85 (patch)
treeb8b79389dc73cc34ed68f895ade3956e461f46b2 /findutils/find.c
parentafea46b4e443f70b8173fc86c36ff7fcec57a474 (diff)
downloadbusybox-w32-6191a7a007ff2e22cbf48f323d4e11d9b959fe85.tar.gz
busybox-w32-6191a7a007ff2e22cbf48f323d4e11d9b959fe85.tar.bz2
busybox-w32-6191a7a007ff2e22cbf48f323d4e11d9b959fe85.zip
find: implement ( )
Diffstat (limited to 'findutils/find.c')
-rw-r--r--findutils/find.c265
1 files changed, 169 insertions, 96 deletions
diff --git a/findutils/find.c b/findutils/find.c
index abd757662..3c7e66a56 100644
--- a/findutils/find.c
+++ b/findutils/find.c
@@ -60,8 +60,15 @@ USE_FEATURE_FIND_MMIN( SACT(mmin, char mmin_char; int mmin_mins;))
60USE_FEATURE_FIND_NEWER( SACT(newer, time_t newer_mtime;)) 60USE_FEATURE_FIND_NEWER( SACT(newer, time_t newer_mtime;))
61USE_FEATURE_FIND_INUM( SACT(inum, ino_t inode_num;)) 61USE_FEATURE_FIND_INUM( SACT(inum, ino_t inode_num;))
62USE_FEATURE_FIND_EXEC( SACT(exec, char **exec_argv; int *subst_count; int exec_argc;)) 62USE_FEATURE_FIND_EXEC( SACT(exec, char **exec_argv; int *subst_count; int exec_argc;))
63USE_DESKTOP( SACT(paren, action ***subexpr;))
63 64
64static action ***actions; 65static action ***actions;
66static int need_print = 1;
67
68static inline int one_char(const char* str, char c)
69{
70 return (str[0] == c && str[1] == '\0');
71}
65 72
66 73
67static int count_subst(const char *str) 74static int count_subst(const char *str)
@@ -93,6 +100,28 @@ static char* subst(const char *src, int count, const char* filename)
93} 100}
94 101
95 102
103static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
104{
105 int cur_group;
106 int cur_action;
107 int rc = TRUE;
108 action **app, *ap;
109
110 cur_group = -1;
111 while ((app = appp[++cur_group])) {
112 cur_action = -1;
113 do {
114 ap = app[++cur_action];
115 } while (ap && (rc = ap->f(fileName, statbuf, ap)));
116 if (!ap) {
117 /* all actions in group were successful */
118 break;
119 }
120 }
121 return rc;
122}
123
124
96SFUNC(name) 125SFUNC(name)
97{ 126{
98 const char *tmp = strrchr(fileName, '/'); 127 const char *tmp = strrchr(fileName, '/');
@@ -164,6 +193,7 @@ SFUNC(exec)
164 bb_perror_msg("%s", argv[0]); 193 bb_perror_msg("%s", argv[0]);
165 for (i = 0; i < ap->exec_argc; i++) 194 for (i = 0; i < ap->exec_argc; i++)
166 free(argv[i]); 195 free(argv[i]);
196 need_print = 0;
167 return rc == 0; /* return 1 if success */ 197 return rc == 0; /* return 1 if success */
168} 198}
169#endif 199#endif
@@ -172,33 +202,40 @@ SFUNC(exec)
172SFUNC(print0) 202SFUNC(print0)
173{ 203{
174 printf("%s%c", fileName, '\0'); 204 printf("%s%c", fileName, '\0');
205 need_print = 0;
175 return TRUE; 206 return TRUE;
176} 207}
177#endif 208#endif
209
178SFUNC(print) 210SFUNC(print)
179{ 211{
180 puts(fileName); 212 puts(fileName);
213 need_print = 0;
181 return TRUE; 214 return TRUE;
182} 215}
183 216
217#if ENABLE_DESKTOP
218SFUNC(paren)
219{
220 return exec_actions(ap->subexpr, fileName, statbuf);
221}
222#endif
223
184 224
185static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth) 225static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
186{ 226{
187 int cur_group; 227#ifdef CONFIG_FEATURE_FIND_XDEV
188 int cur_action; 228 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
189 action **app, *ap; 229 int i;
190 230 for (i = 0; i < xdev_count; i++) {
191 cur_group = -1; 231 if (xdev_dev[i] != statbuf->st_dev)
192 while ((app = actions[++cur_group])) { 232 return SKIP;
193 cur_action = -1;
194 do {
195 ap = app[++cur_action];
196 } while (ap && ap->f(fileName, statbuf, ap));
197 if (!ap) {
198 /* all actions in group were successful */
199 break;
200 } 233 }
201 } 234 }
235#endif
236 /* had no explicit -print[0] or -exec? then print */
237 if (exec_actions(actions, fileName, statbuf) && need_print)
238 puts(fileName);
202 return TRUE; 239 return TRUE;
203} 240}
204 241
@@ -239,41 +276,24 @@ static int find_type(char *type)
239} 276}
240#endif 277#endif
241 278
242 279action*** parse_params(char **argv)
243int find_main(int argc, char **argv)
244{ 280{
245 int dereference = FALSE; 281 action*** appp;
246 int i, j, firstopt, status = EXIT_SUCCESS; 282 int cur_group = 0;
247 int cur_group; 283 int cur_action = 0;
248 int cur_action;
249 int need_default = 1;
250 284
251 action* alloc_action(int sizeof_struct, action_fp f) 285 action* alloc_action(int sizeof_struct, action_fp f)
252 { 286 {
253 action *ap; 287 action *ap;
254 actions[cur_group] = xrealloc(actions[cur_group], (cur_action+2) * sizeof(*actions)); 288 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
255 actions[cur_group][cur_action++] = ap = xmalloc(sizeof_struct); 289 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
256 actions[cur_group][cur_action] = NULL; 290 appp[cur_group][cur_action] = NULL;
257 ap->f = f; 291 ap->f = f;
258 return ap; 292 return ap;
259 } 293 }
260#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name) 294#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
261 295
262 for (firstopt = 1; firstopt < argc; firstopt++) { 296 appp = xzalloc(2 * sizeof(*appp)); /* appp[0],[1] == NULL */
263 if (argv[firstopt][0] == '-')
264 break;
265 }
266 if (firstopt == 1) {
267 argv[0] = ".";
268 argv--;
269 argc++;
270 firstopt++;
271 }
272
273// All options always return true. They always take effect,
274// rather than being processed only when their place in the
275// expression is reached
276// We implement: -follow, -xdev
277 297
278// Actions have side effects and return a true or false value 298// Actions have side effects and return a true or false value
279// We implement: -print, -print0, -exec 299// We implement: -print, -print0, -exec
@@ -287,67 +307,40 @@ int find_main(int argc, char **argv)
287// expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false 307// expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
288// expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true 308// expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
289// expr1 , expr2 List; both expr1 and expr2 are always evaluated 309// expr1 , expr2 List; both expr1 and expr2 are always evaluated
290// We implement: -a, -o 310// We implement: (), -a, -o
291
292 cur_group = 0;
293 cur_action = 0;
294 actions = xzalloc(sizeof(*actions)); /* actions[0] == NULL */
295
296 /* Parse any options */
297 for (i = firstopt; i < argc; i++) {
298 char *arg = argv[i];
299 char *arg1 = argv[i+1];
300 311
312 while (*argv) {
313 char *arg = argv[0];
314 char *arg1 = argv[1];
301 /* --- Operators --- */ 315 /* --- Operators --- */
302 if (ENABLE_DESKTOP 316 if (strcmp(arg, "-a") == 0
303 && (strcmp(arg, "-a") == 0 || strcmp(arg, "-and") == 0) 317 USE_DESKTOP(|| strcmp(arg, "-and") == 0)
304 ) { 318 ) {
305 /* no special handling required */ 319 /* no special handling required */
306 } 320 }
307 else if (strcmp(arg, "-o") == 0 321 else if (strcmp(arg, "-o") == 0
308 USE_DESKTOP(|| strcmp(arg, "-or") == 0) 322 USE_DESKTOP(|| strcmp(arg, "-or") == 0)
309 ) { 323 ) {
310 if (need_default) 324 /* start new OR group */
311 (void) ALLOC_ACTION(print);
312 cur_group++; 325 cur_group++;
313 actions = xrealloc(actions, (cur_group+1) * sizeof(*actions)); 326 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
314 actions[cur_group] = NULL; 327 appp[cur_group] = NULL;
315 actions[cur_group+1] = NULL; 328 appp[cur_group+1] = NULL;
316 cur_action = 0; 329 cur_action = 0;
317 need_default = 1;
318 } 330 }
319 331
320 /* --- Options --- */
321 else if (strcmp(arg, "-follow") == 0)
322 dereference = TRUE;
323#if ENABLE_FEATURE_FIND_XDEV
324 else if (strcmp(arg, "-xdev") == 0) {
325 struct stat stbuf;
326
327 xdev_count = firstopt - 1;
328 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
329 for (j = 1; j < firstopt; i++) {
330 /* not xstat(): shouldn't bomd out on
331 * "find not_exist exist -xdev" */
332 if (stat(argv[j], &stbuf)) stbuf.st_dev = -1L;
333 xdev_dev[j-1] = stbuf.st_dev;
334 }
335 }
336#endif
337 /* --- Tests and actions --- */ 332 /* --- Tests and actions --- */
338 else if (strcmp(arg, "-print") == 0) { 333 else if (strcmp(arg, "-print") == 0) {
339 need_default = 0;
340 (void) ALLOC_ACTION(print); 334 (void) ALLOC_ACTION(print);
341 } 335 }
342#if ENABLE_FEATURE_FIND_PRINT0 336#if ENABLE_FEATURE_FIND_PRINT0
343 else if (strcmp(arg, "-print0") == 0) { 337 else if (strcmp(arg, "-print0") == 0) {
344 need_default = 0;
345 (void) ALLOC_ACTION(print0); 338 (void) ALLOC_ACTION(print0);
346 } 339 }
347#endif 340#endif
348 else if (strcmp(arg, "-name") == 0) { 341 else if (strcmp(arg, "-name") == 0) {
349 action_name *ap; 342 action_name *ap;
350 if (++i == argc) 343 if (!*++argv)
351 bb_error_msg_and_die(bb_msg_requires_arg, arg); 344 bb_error_msg_and_die(bb_msg_requires_arg, arg);
352 ap = ALLOC_ACTION(name); 345 ap = ALLOC_ACTION(name);
353 ap->pattern = arg1; 346 ap->pattern = arg1;
@@ -355,7 +348,7 @@ int find_main(int argc, char **argv)
355#if ENABLE_FEATURE_FIND_TYPE 348#if ENABLE_FEATURE_FIND_TYPE
356 else if (strcmp(arg, "-type") == 0) { 349 else if (strcmp(arg, "-type") == 0) {
357 action_type *ap; 350 action_type *ap;
358 if (++i == argc) 351 if (!*++argv)
359 bb_error_msg_and_die(bb_msg_requires_arg, arg); 352 bb_error_msg_and_die(bb_msg_requires_arg, arg);
360 ap = ALLOC_ACTION(type); 353 ap = ALLOC_ACTION(type);
361 ap->type_mask = find_type(arg1); 354 ap->type_mask = find_type(arg1);
@@ -370,7 +363,7 @@ int find_main(int argc, char **argv)
370 */ 363 */
371 else if (strcmp(arg, "-perm") == 0) { 364 else if (strcmp(arg, "-perm") == 0) {
372 action_perm *ap; 365 action_perm *ap;
373 if (++i == argc) 366 if (!*++argv)
374 bb_error_msg_and_die(bb_msg_requires_arg, arg); 367 bb_error_msg_and_die(bb_msg_requires_arg, arg);
375 ap = ALLOC_ACTION(perm); 368 ap = ALLOC_ACTION(perm);
376 ap->perm_mask = xstrtol_range(arg1, 8, 0, 07777); 369 ap->perm_mask = xstrtol_range(arg1, 8, 0, 07777);
@@ -382,7 +375,7 @@ int find_main(int argc, char **argv)
382#if ENABLE_FEATURE_FIND_MTIME 375#if ENABLE_FEATURE_FIND_MTIME
383 else if (strcmp(arg, "-mtime") == 0) { 376 else if (strcmp(arg, "-mtime") == 0) {
384 action_mtime *ap; 377 action_mtime *ap;
385 if (++i == argc) 378 if (!*++argv)
386 bb_error_msg_and_die(bb_msg_requires_arg, arg); 379 bb_error_msg_and_die(bb_msg_requires_arg, arg);
387 ap = ALLOC_ACTION(mtime); 380 ap = ALLOC_ACTION(mtime);
388 ap->mtime_days = xatol(arg1); 381 ap->mtime_days = xatol(arg1);
@@ -394,7 +387,7 @@ int find_main(int argc, char **argv)
394#if ENABLE_FEATURE_FIND_MMIN 387#if ENABLE_FEATURE_FIND_MMIN
395 else if (strcmp(arg, "-mmin") == 0) { 388 else if (strcmp(arg, "-mmin") == 0) {
396 action_mmin *ap; 389 action_mmin *ap;
397 if (++i == argc) 390 if (!*++argv)
398 bb_error_msg_and_die(bb_msg_requires_arg, arg); 391 bb_error_msg_and_die(bb_msg_requires_arg, arg);
399 ap = ALLOC_ACTION(mmin); 392 ap = ALLOC_ACTION(mmin);
400 ap->mmin_mins = xatol(arg1); 393 ap->mmin_mins = xatol(arg1);
@@ -407,7 +400,7 @@ int find_main(int argc, char **argv)
407 else if (strcmp(arg, "-newer") == 0) { 400 else if (strcmp(arg, "-newer") == 0) {
408 action_newer *ap; 401 action_newer *ap;
409 struct stat stat_newer; 402 struct stat stat_newer;
410 if (++i == argc) 403 if (!*++argv)
411 bb_error_msg_and_die(bb_msg_requires_arg, arg); 404 bb_error_msg_and_die(bb_msg_requires_arg, arg);
412 xstat(arg1, &stat_newer); 405 xstat(arg1, &stat_newer);
413 ap = ALLOC_ACTION(newer); 406 ap = ALLOC_ACTION(newer);
@@ -417,7 +410,7 @@ int find_main(int argc, char **argv)
417#if ENABLE_FEATURE_FIND_INUM 410#if ENABLE_FEATURE_FIND_INUM
418 else if (strcmp(arg, "-inum") == 0) { 411 else if (strcmp(arg, "-inum") == 0) {
419 action_inum *ap; 412 action_inum *ap;
420 if (++i == argc) 413 if (!*++argv)
421 bb_error_msg_and_die(bb_msg_requires_arg, arg); 414 bb_error_msg_and_die(bb_msg_requires_arg, arg);
422 ap = ALLOC_ACTION(inum); 415 ap = ALLOC_ACTION(inum);
423 ap->inode_num = xatoul(arg1); 416 ap->inode_num = xatoul(arg1);
@@ -425,34 +418,114 @@ int find_main(int argc, char **argv)
425#endif 418#endif
426#if ENABLE_FEATURE_FIND_EXEC 419#if ENABLE_FEATURE_FIND_EXEC
427 else if (strcmp(arg, "-exec") == 0) { 420 else if (strcmp(arg, "-exec") == 0) {
421 int i;
428 action_exec *ap; 422 action_exec *ap;
429 need_default = 0;
430 ap = ALLOC_ACTION(exec); 423 ap = ALLOC_ACTION(exec);
431 i++; /* now: argv[i] is the first arg after -exec */ 424 ap->exec_argv = ++argv; /* first arg after -exec */
432 ap->exec_argv = &argv[i]; 425 ap->exec_argc = 0;
433 ap->exec_argc = i;
434 while (1) { 426 while (1) {
435 if (i == argc) /* did not see ';' till end */ 427 if (!*argv) /* did not see ';' till end */
436 bb_error_msg_and_die(bb_msg_requires_arg, arg); 428 bb_error_msg_and_die(bb_msg_requires_arg, arg);
437 if (argv[i][0] == ';' && argv[i][1] == '\0') 429 if (one_char(argv[0], ';'))
438 break; 430 break;
439 i++; 431 argv++;
432 ap->exec_argc++;
440 } 433 }
441 ap->exec_argc = i - ap->exec_argc; /* number of --exec arguments */
442 if (ap->exec_argc == 0) 434 if (ap->exec_argc == 0)
443 bb_error_msg_and_die(bb_msg_requires_arg, arg); 435 bb_error_msg_and_die(bb_msg_requires_arg, arg);
444 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); 436 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
445 j = ap->exec_argc; 437 i = ap->exec_argc;
446 while (j--) 438 while (i--)
447 ap->subst_count[j] = count_subst(ap->exec_argv[j]); 439 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
440 }
441#endif
442#if ENABLE_DESKTOP
443 else if (one_char(arg, '(')) {
444 action_paren *ap;
445 char **endarg;
446 int nested = 1;
447
448 endarg = argv;
449 while (1) {
450 if (!*++endarg)
451 bb_error_msg_and_die("unpaired '('");
452 if (one_char(*endarg, '('))
453 nested++;
454 else if (one_char(*endarg, ')') && !--nested) {
455 *endarg = NULL;
456 break;
457 }
458 }
459 ap = ALLOC_ACTION(paren);
460 ap->subexpr = parse_params(argv + 1);
461 *endarg = ")"; /* restore NULLed parameter */
462 argv = endarg;
448 } 463 }
449#endif 464#endif
450 else 465 else
451 bb_show_usage(); 466 bb_show_usage();
467 argv++;
452 } 468 }
453 469
454 if (need_default) 470 return appp;
455 (void) ALLOC_ACTION(print); 471#undef ALLOC_ACTION
472}
473
474
475int find_main(int argc, char **argv)
476{
477 int dereference = FALSE;
478 char **argp;
479 int i, firstopt, status = EXIT_SUCCESS;
480
481 for (firstopt = 1; firstopt < argc; firstopt++) {
482 if (argv[firstopt][0] == '-')
483 break;
484#if ENABLE_DESKTOP
485 if (one_char(argv[firstopt], '('))
486 break;
487#endif
488 }
489 if (firstopt == 1) {
490 argv[0] = ".";
491 argv--;
492 firstopt++;
493 }
494
495// All options always return true. They always take effect,
496// rather than being processed only when their place in the
497// expression is reached
498// We implement: -follow, -xdev
499
500 /* Process options, and replace then with -a */
501 /* (that will be ignored by recursive parser later) */
502 argp = &argv[firstopt];
503 while (*argp) {
504 char *arg = argp[0];
505 if (strcmp(arg, "-follow") == 0) {
506 dereference = TRUE;
507 argp[0] = "-a";
508 }
509#if ENABLE_FEATURE_FIND_XDEV
510 else if (strcmp(arg, "-xdev") == 0) {
511 struct stat stbuf;
512 if (!xdev_count) {
513 xdev_count = firstopt - 1;
514 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
515 for (i = 1; i < firstopt; i++) {
516 /* not xstat(): shouldn't bomb out on
517 * "find not_exist exist -xdev" */
518 if (stat(argv[i], &stbuf)) stbuf.st_dev = -1L;
519 xdev_dev[i-1] = stbuf.st_dev;
520 }
521 }
522 argp[0] = "-a";
523 }
524 argp++;
525 }
526#endif
527
528 actions = parse_params(&argv[firstopt]);
456 529
457 for (i = 1; i < firstopt; i++) { 530 for (i = 1; i < firstopt; i++) {
458 if (!recursive_action(argv[i], 531 if (!recursive_action(argv[i],