1 /* vi: set sw=4 ts=4: */
3 * Mini find implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Reworked by David Douthitt <n9ubh@callsign.net> and
8 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
10 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
15 * # find file.txt -exec 'echo {}' '{} {}' ';'
16 * find: echo file.txt: No such file or directory
17 * # find file.txt -exec 'echo' '{} {}' '; '
18 * find: missing argument to `-exec'
19 * # find file.txt -exec 'echo {}' '{} {}' ';' junk
20 * find: paths must precede expression
21 * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';'
22 * find: paths must precede expression
23 * # find file.txt -exec 'echo' '{} {}' ';'
25 * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ]))
26 * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
30 * # find -name '*.c' -o -name '*.h'
31 * [shows files, *.c and *.h intermixed]
32 * # find file.txt -name '*f*' -o -name '*t*'
34 * # find file.txt -name '*z*' -o -name '*t*'
36 * # find file.txt -name '*f*' -o -name '*z*'
39 * # find t z -name '*t*' -print -o -name '*z*'
41 * # find t z t z -name '*t*' -o -name '*z*' -print
44 * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
51 USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
52 USE_FEATURE_FIND_XDEV(static int xdev_count;)
54 typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
58 #if ENABLE_FEATURE_FIND_NOT
62 #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
63 #define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
65 ACTS(name, const char *pattern;)
66 USE_FEATURE_FIND_PRINT0(ACTS(print0))
67 USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
68 USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
69 USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
70 USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
71 USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
72 USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
73 USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
74 USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
75 USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
76 USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
77 USE_FEATURE_FIND_SIZE( ACTS(size, off_t size;))
78 USE_FEATURE_FIND_PRUNE( ACTS(prune))
80 static action ***actions;
81 static bool need_print = 1;
82 static int recurse_flags = ACTION_RECURSE;
84 #if ENABLE_FEATURE_FIND_EXEC
85 static unsigned count_subst(const char *str)
88 while ((str = strstr(str, "{}"))) {
96 static char* subst(const char *src, unsigned count, const char* filename)
98 char *buf, *dst, *end;
99 size_t flen = strlen(filename);
100 /* we replace each '{}' with filename: growth by strlen-2 */
101 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
102 while ((end = strstr(src, "{}"))) {
103 memcpy(dst, src, end - src);
106 memcpy(dst, filename, flen);
114 /* Return values of ACTFs ('action functions') are a bit mask:
115 * bit 1=1: prune (use SKIP constant for setting it)
116 * bit 0=1: matched successfully (TRUE)
119 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
126 /* "action group" is a set of actions ANDed together.
127 * groups are ORed together.
128 * We simply evaluate each group until we find one in which all actions
131 /* -prune is special: if it is encountered, then we won't
132 * descend into current directory. It doesn't matter whether
133 * action group (in which -prune sits) will succeed or not:
134 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
135 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
136 * not starting with 'f' */
138 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
139 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
140 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
141 * On return, bit is restored. */
144 while ((app = appp[++cur_group])) {
145 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
148 ap = app[++cur_action];
149 if (!ap) /* all actions in group were successful */
150 return rc ^ TRUE; /* restore TRUE bit */
151 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
152 #if ENABLE_FEATURE_FIND_NOT
153 if (ap->invert) rc ^= TRUE;
155 if (rc & TRUE) /* current group failed, try next */
159 return rc ^ TRUE; /* restore TRUE bit */
165 const char *tmp = strrchr(fileName, '/');
170 if (!*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
172 while (tmp != fileName && *--tmp != '/')
178 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
180 #if ENABLE_FEATURE_FIND_TYPE
183 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
186 #if ENABLE_FEATURE_FIND_PERM
189 /* -perm +mode: at least one of perm_mask bits are set */
190 if (ap->perm_char == '+')
191 return (statbuf->st_mode & ap->perm_mask) != 0;
192 /* -perm -mode: all of perm_mask are set */
193 if (ap->perm_char == '-')
194 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
195 /* -perm mode: file mode must match perm_mask */
196 return (statbuf->st_mode & 07777) == ap->perm_mask;
199 #if ENABLE_FEATURE_FIND_MTIME
202 time_t file_age = time(NULL) - statbuf->st_mtime;
203 time_t mtime_secs = ap->mtime_days * 24*60*60;
204 if (ap->mtime_char == '+')
205 return file_age >= mtime_secs + 24*60*60;
206 if (ap->mtime_char == '-')
207 return file_age < mtime_secs;
208 /* just numeric mtime */
209 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
212 #if ENABLE_FEATURE_FIND_MMIN
215 time_t file_age = time(NULL) - statbuf->st_mtime;
216 time_t mmin_secs = ap->mmin_mins * 60;
217 if (ap->mmin_char == '+')
218 return file_age >= mmin_secs + 60;
219 if (ap->mmin_char == '-')
220 return file_age < mmin_secs;
221 /* just numeric mmin */
222 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
225 #if ENABLE_FEATURE_FIND_NEWER
228 return (ap->newer_mtime < statbuf->st_mtime);
231 #if ENABLE_FEATURE_FIND_INUM
234 return (statbuf->st_ino == ap->inode_num);
237 #if ENABLE_FEATURE_FIND_EXEC
241 char *argv[ap->exec_argc+1];
242 for (i = 0; i < ap->exec_argc; i++)
243 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
244 argv[i] = NULL; /* terminate the list */
245 rc = wait4pid(spawn(argv));
247 bb_perror_msg("%s", argv[0]);
248 for (i = 0; i < ap->exec_argc; i++)
250 return rc == 0; /* return 1 if success */
254 #if ENABLE_FEATURE_FIND_USER
257 return (statbuf->st_uid == ap->uid);
261 #if ENABLE_FEATURE_FIND_GROUP
264 return (statbuf->st_gid == ap->gid);
268 #if ENABLE_FEATURE_FIND_PRINT0
271 printf("%s%c", fileName, '\0');
282 #if ENABLE_FEATURE_FIND_PAREN
285 return exec_actions(ap->subexpr, fileName, statbuf);
289 #if ENABLE_FEATURE_FIND_PRUNE
291 * -prune: if -depth is not given, return true and do not descend
292 * current dir; if -depth is given, return false with no effect.
294 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
302 #if ENABLE_FEATURE_FIND_SIZE
305 return statbuf->st_size == ap->size;
310 static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
313 #if ENABLE_FEATURE_FIND_XDEV
314 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
315 for (i = 0; i < xdev_count; i++) {
316 if (xdev_dev[i] != statbuf->st_dev)
321 i = exec_actions(actions, fileName, statbuf);
322 /* Had no explicit -print[0] or -exec? then print */
323 if ((i & TRUE) && need_print)
325 /* Cannot return 0: our caller, recursive_action(),
326 * will perror() and skip dirs (if called on dir) */
327 return (i & SKIP) ? SKIP : TRUE;
331 #if ENABLE_FEATURE_FIND_TYPE
332 static int find_type(const char *type)
338 else if (*type == 'c')
340 else if (*type == 'd')
342 else if (*type == 'p')
344 else if (*type == 'f')
346 else if (*type == 'l')
348 else if (*type == 's')
351 if (mask == 0 || *(type + 1) != '\0')
352 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
358 #if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
359 || ENABLE_FEATURE_FIND_MMIN
360 static const char* plus_minus_num(const char* str)
362 if (*str == '-' || *str == '+')
368 static action*** parse_params(char **argv)
373 USE_FEATURE_FIND_NOT( PARM_char_not ,)
375 USE_FEATURE_FIND_PRINT0(PARM_print0 ,)
377 USE_FEATURE_FIND_TYPE( PARM_type ,)
378 USE_FEATURE_FIND_PERM( PARM_perm ,)
379 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
380 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
381 USE_FEATURE_FIND_NEWER( PARM_newer ,)
382 USE_FEATURE_FIND_INUM( PARM_inum ,)
383 USE_FEATURE_FIND_EXEC( PARM_exec ,)
384 USE_FEATURE_FIND_USER( PARM_user ,)
385 USE_FEATURE_FIND_GROUP( PARM_group ,)
386 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
387 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
388 USE_FEATURE_FIND_SIZE( PARM_size ,)
389 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
393 USE_FEATURE_FIND_NOT( PARM_not ,)
397 static const char *const params[] = {
400 USE_FEATURE_FIND_NOT( "!" ,)
402 USE_FEATURE_FIND_PRINT0("-print0",)
404 USE_FEATURE_FIND_TYPE( "-type" ,)
405 USE_FEATURE_FIND_PERM( "-perm" ,)
406 USE_FEATURE_FIND_MTIME( "-mtime" ,)
407 USE_FEATURE_FIND_MMIN( "-mmin" ,)
408 USE_FEATURE_FIND_NEWER( "-newer" ,)
409 USE_FEATURE_FIND_INUM( "-inum" ,)
410 USE_FEATURE_FIND_EXEC( "-exec" ,)
411 USE_FEATURE_FIND_USER( "-user" ,)
412 USE_FEATURE_FIND_GROUP( "-group" ,)
413 USE_FEATURE_FIND_DEPTH( "-depth" ,)
414 USE_FEATURE_FIND_PAREN( "(" ,)
415 USE_FEATURE_FIND_SIZE( "-size" ,)
416 USE_FEATURE_FIND_PRUNE( "-prune" ,)
420 USE_FEATURE_FIND_NOT( "-not" ,)
426 unsigned cur_group = 0;
427 unsigned cur_action = 0;
428 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
430 /* 'static' doesn't work here! (gcc 4.1.2) */
431 action* alloc_action(int sizeof_struct, action_fp f)
434 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
435 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
436 appp[cur_group][cur_action] = NULL;
438 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
439 USE_FEATURE_FIND_NOT( invert_flag = 0; )
443 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
445 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
447 /* Actions have side effects and return a true or false value
448 * We implement: -print, -print0, -exec
450 * The rest are tests.
452 * Tests and actions are grouped by operators
453 * ( expr ) Force precedence
454 * ! expr True if expr is false
455 * -not expr Same as ! expr
456 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
457 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
458 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
459 * We implement: (), -a, -o
462 const char *arg = argv[0];
463 const char *arg1 = argv[1];
464 int parm = index_in_str_array(params, arg);
465 /* --- Operators --- */
466 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
467 /* no further special handling required */
469 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
470 /* start new OR group */
472 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
473 /*appp[cur_group] = NULL; - already NULL */
474 appp[cur_group+1] = NULL;
477 #if ENABLE_FEATURE_FIND_NOT
478 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
479 /* also handles "find ! ! -name 'foo*'" */
484 /* --- Tests and actions --- */
485 else if (parm == PARM_print) {
487 /* GNU find ignores '!' here: "find ! -print" */
488 USE_FEATURE_FIND_NOT( invert_flag = 0; )
489 (void) ALLOC_ACTION(print);
491 #if ENABLE_FEATURE_FIND_PRINT0
492 else if (parm == PARM_print0) {
494 USE_FEATURE_FIND_NOT( invert_flag = 0; )
495 (void) ALLOC_ACTION(print0);
498 else if (parm == PARM_name) {
501 bb_error_msg_and_die(bb_msg_requires_arg, arg);
502 ap = ALLOC_ACTION(name);
505 #if ENABLE_FEATURE_FIND_TYPE
506 else if (parm == PARM_type) {
509 bb_error_msg_and_die(bb_msg_requires_arg, arg);
510 ap = ALLOC_ACTION(type);
511 ap->type_mask = find_type(arg1);
514 #if ENABLE_FEATURE_FIND_PERM
515 /* -perm mode File's permission bits are exactly mode (octal or symbolic).
516 * Symbolic modes use mode 0 as a point of departure.
517 * -perm -mode All of the permission bits mode are set for the file.
518 * -perm +mode Any of the permission bits mode are set for the file.
520 else if (parm == PARM_perm) {
523 bb_error_msg_and_die(bb_msg_requires_arg, arg);
524 ap = ALLOC_ACTION(perm);
525 ap->perm_char = arg1[0];
526 arg1 = plus_minus_num(arg1);
528 if (!bb_parse_mode(arg1, &ap->perm_mask))
529 bb_error_msg_and_die("invalid mode: %s", arg1);
532 #if ENABLE_FEATURE_FIND_MTIME
533 else if (parm == PARM_mtime) {
536 bb_error_msg_and_die(bb_msg_requires_arg, arg);
537 ap = ALLOC_ACTION(mtime);
538 ap->mtime_char = arg1[0];
539 ap->mtime_days = xatoul(plus_minus_num(arg1));
542 #if ENABLE_FEATURE_FIND_MMIN
543 else if (parm == PARM_mmin) {
546 bb_error_msg_and_die(bb_msg_requires_arg, arg);
547 ap = ALLOC_ACTION(mmin);
548 ap->mmin_char = arg1[0];
549 ap->mmin_mins = xatoul(plus_minus_num(arg1));
552 #if ENABLE_FEATURE_FIND_NEWER
553 else if (parm == PARM_newer) {
555 struct stat stat_newer;
557 bb_error_msg_and_die(bb_msg_requires_arg, arg);
558 xstat(arg1, &stat_newer);
559 ap = ALLOC_ACTION(newer);
560 ap->newer_mtime = stat_newer.st_mtime;
563 #if ENABLE_FEATURE_FIND_INUM
564 else if (parm == PARM_inum) {
567 bb_error_msg_and_die(bb_msg_requires_arg, arg);
568 ap = ALLOC_ACTION(inum);
569 ap->inode_num = xatoul(arg1);
572 #if ENABLE_FEATURE_FIND_EXEC
573 else if (parm == PARM_exec) {
577 USE_FEATURE_FIND_NOT( invert_flag = 0; )
578 ap = ALLOC_ACTION(exec);
579 ap->exec_argv = ++argv; /* first arg after -exec */
582 if (!*argv) /* did not see ';' until end */
583 bb_error_msg_and_die(bb_msg_requires_arg, arg);
584 if (LONE_CHAR(argv[0], ';'))
589 if (ap->exec_argc == 0)
590 bb_error_msg_and_die(bb_msg_requires_arg, arg);
591 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
594 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
597 #if ENABLE_FEATURE_FIND_USER
598 else if (parm == PARM_user) {
601 bb_error_msg_and_die(bb_msg_requires_arg, arg);
602 ap = ALLOC_ACTION(user);
603 ap->uid = bb_strtou(arg1, NULL, 10);
605 ap->uid = xuname2uid(arg1);
608 #if ENABLE_FEATURE_FIND_GROUP
609 else if (parm == PARM_group) {
612 bb_error_msg_and_die(bb_msg_requires_arg, arg);
613 ap = ALLOC_ACTION(group);
614 ap->gid = bb_strtou(arg1, NULL, 10);
616 ap->gid = xgroup2gid(arg1);
619 #if ENABLE_FEATURE_FIND_DEPTH
620 else if (parm == PARM_depth) {
621 recurse_flags |= ACTION_DEPTHFIRST;
624 #if ENABLE_FEATURE_FIND_PAREN
625 else if (parm == PARM_char_brace) {
633 bb_error_msg_and_die("unpaired '('");
634 if (LONE_CHAR(*endarg, '('))
636 else if (LONE_CHAR(*endarg, ')') && !--nested) {
641 ap = ALLOC_ACTION(paren);
642 ap->subexpr = parse_params(argv + 1);
643 *endarg = (char*) ")"; /* restore NULLed parameter */
647 #if ENABLE_FEATURE_FIND_PRUNE
648 else if (parm == PARM_prune) {
649 USE_FEATURE_FIND_NOT( invert_flag = 0; )
650 (void) ALLOC_ACTION(prune);
653 #if ENABLE_FEATURE_FIND_SIZE
654 else if (parm == PARM_size) {
657 bb_error_msg_and_die(bb_msg_requires_arg, arg);
658 ap = ALLOC_ACTION(size);
659 ap->size = XATOOFF(arg1);
671 int find_main(int argc, char **argv);
672 int find_main(int argc, char **argv)
674 static const char * const options[] = {
676 USE_FEATURE_FIND_XDEV( "-xdev", )
682 int i, firstopt, status = EXIT_SUCCESS;
684 for (firstopt = 1; firstopt < argc; firstopt++) {
685 if (argv[firstopt][0] == '-')
687 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
689 #if ENABLE_FEATURE_FIND_PAREN
690 if (LONE_CHAR(argv[firstopt], '('))
695 argv[0] = (char*)".";
700 /* All options always return true. They always take effect
701 * rather than being processed only when their place in the
702 * expression is reached.
703 * We implement: -follow, -xdev
705 /* Process options, and replace then with -a */
706 /* (-a will be ignored by recursive parser later) */
707 argp = &argv[firstopt];
708 while ((arg = argp[0])) {
709 i = index_in_str_array(options, arg);
710 if (i == 0) { /* -follow */
711 recurse_flags |= ACTION_FOLLOWLINKS;
712 argp[0] = (char*)"-a";
714 #if ENABLE_FEATURE_FIND_XDEV
715 else if (i == 1) { /* -xdev */
718 xdev_count = firstopt - 1;
719 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
720 for (i = 1; i < firstopt; i++) {
721 /* not xstat(): shouldn't bomb out on
722 * "find not_exist exist -xdev" */
723 if (stat(argv[i], &stbuf))
725 xdev_dev[i-1] = stbuf.st_dev;
728 argp[0] = (char*)"-a";
734 actions = parse_params(&argv[firstopt]);
736 for (i = 1; i < firstopt; i++) {
737 if (!recursive_action(argv[i],
738 recurse_flags, /* flags */
739 fileAction, /* file action */
740 fileAction, /* dir action */
741 NULL, /* user data */
743 status = EXIT_FAILURE;