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
49 * ./busybox find "$@" | tee /tmp/bb_find
50 * echo ==================
51 * /path/to/gnu/find "$@" | tee /tmp/std_find
52 * echo ==================
53 * diff -u /tmp/std_find /tmp/bb_find && echo Identical
58 #if ENABLE_FEATURE_FIND_REGEX
62 /* This is a NOEXEC applet. Be very careful! */
65 IF_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
66 IF_FEATURE_FIND_XDEV(static int xdev_count;)
68 typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *) FAST_FUNC;
72 #if ENABLE_FEATURE_FIND_NOT
77 #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
79 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
80 struct stat *statbuf UNUSED_PARAM, \
81 action_##name* ap UNUSED_PARAM)
84 ACTS(name, const char *pattern; bool iname;)
85 IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
86 IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
87 IF_FEATURE_FIND_PRINT0( ACTS(print0))
88 IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
89 IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
90 IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
91 IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
92 IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
93 IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
94 IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
95 IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
96 IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
97 IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
98 IF_FEATURE_FIND_PRUNE( ACTS(prune))
99 IF_FEATURE_FIND_DELETE( ACTS(delete))
100 IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
101 IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
103 static action ***actions;
104 static bool need_print = 1;
105 static int recurse_flags = ACTION_RECURSE;
107 #if ENABLE_FEATURE_FIND_EXEC
108 static unsigned count_subst(const char *str)
111 while ((str = strstr(str, "{}")) != NULL) {
119 static char* subst(const char *src, unsigned count, const char* filename)
121 char *buf, *dst, *end;
122 size_t flen = strlen(filename);
123 /* we replace each '{}' with filename: growth by strlen-2 */
124 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
125 while ((end = strstr(src, "{}"))) {
126 memcpy(dst, src, end - src);
129 memcpy(dst, filename, flen);
137 /* Return values of ACTFs ('action functions') are a bit mask:
138 * bit 1=1: prune (use SKIP constant for setting it)
139 * bit 0=1: matched successfully (TRUE)
142 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
149 /* "action group" is a set of actions ANDed together.
150 * groups are ORed together.
151 * We simply evaluate each group until we find one in which all actions
154 /* -prune is special: if it is encountered, then we won't
155 * descend into current directory. It doesn't matter whether
156 * action group (in which -prune sits) will succeed or not:
157 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
158 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
159 * not starting with 'f' */
161 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
162 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
163 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
164 * On return, bit is restored. */
167 while ((app = appp[++cur_group])) {
168 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
171 ap = app[++cur_action];
172 if (!ap) /* all actions in group were successful */
173 return rc ^ TRUE; /* restore TRUE bit */
174 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
175 #if ENABLE_FEATURE_FIND_NOT
176 if (ap->invert) rc ^= TRUE;
178 if (rc & TRUE) /* current group failed, try next */
182 return rc ^ TRUE; /* restore TRUE bit */
188 const char *tmp = bb_basename(fileName);
189 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
191 while (tmp != fileName && *--tmp != '/')
196 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
199 #if ENABLE_FEATURE_FIND_PATH
202 return fnmatch(ap->pattern, fileName, 0) == 0;
205 #if ENABLE_FEATURE_FIND_REGEX
209 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
210 return 0; /* no match */
212 return 0; /* match doesn't start at pos 0 */
213 if (fileName[match.rm_eo])
214 return 0; /* match doesn't end exactly at end of pathname */
218 #if ENABLE_FEATURE_FIND_TYPE
221 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
224 #if ENABLE_FEATURE_FIND_PERM
227 /* -perm +mode: at least one of perm_mask bits are set */
228 if (ap->perm_char == '+')
229 return (statbuf->st_mode & ap->perm_mask) != 0;
230 /* -perm -mode: all of perm_mask are set */
231 if (ap->perm_char == '-')
232 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
233 /* -perm mode: file mode must match perm_mask */
234 return (statbuf->st_mode & 07777) == ap->perm_mask;
237 #if ENABLE_FEATURE_FIND_MTIME
240 time_t file_age = time(NULL) - statbuf->st_mtime;
241 time_t mtime_secs = ap->mtime_days * 24*60*60;
242 if (ap->mtime_char == '+')
243 return file_age >= mtime_secs + 24*60*60;
244 if (ap->mtime_char == '-')
245 return file_age < mtime_secs;
246 /* just numeric mtime */
247 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
250 #if ENABLE_FEATURE_FIND_MMIN
253 time_t file_age = time(NULL) - statbuf->st_mtime;
254 time_t mmin_secs = ap->mmin_mins * 60;
255 if (ap->mmin_char == '+')
256 return file_age >= mmin_secs + 60;
257 if (ap->mmin_char == '-')
258 return file_age < mmin_secs;
259 /* just numeric mmin */
260 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
263 #if ENABLE_FEATURE_FIND_NEWER
266 return (ap->newer_mtime < statbuf->st_mtime);
269 #if ENABLE_FEATURE_FIND_INUM
272 return (statbuf->st_ino == ap->inode_num);
275 #if ENABLE_FEATURE_FIND_EXEC
279 char *argv[ap->exec_argc + 1];
280 for (i = 0; i < ap->exec_argc; i++)
281 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
282 argv[i] = NULL; /* terminate the list */
284 rc = spawn_and_wait(argv);
286 bb_simple_perror_msg(argv[0]);
291 return rc == 0; /* return 1 if exitcode 0 */
294 #if ENABLE_FEATURE_FIND_USER
297 return (statbuf->st_uid == ap->uid);
300 #if ENABLE_FEATURE_FIND_GROUP
303 return (statbuf->st_gid == ap->gid);
306 #if ENABLE_FEATURE_FIND_PRINT0
309 printf("%s%c", fileName, '\0');
318 #if ENABLE_FEATURE_FIND_PAREN
321 return exec_actions(ap->subexpr, fileName, statbuf);
324 #if ENABLE_FEATURE_FIND_SIZE
327 if (ap->size_char == '+')
328 return statbuf->st_size > ap->size;
329 if (ap->size_char == '-')
330 return statbuf->st_size < ap->size;
331 return statbuf->st_size == ap->size;
334 #if ENABLE_FEATURE_FIND_PRUNE
336 * -prune: if -depth is not given, return true and do not descend
337 * current dir; if -depth is given, return false with no effect.
339 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
346 #if ENABLE_FEATURE_FIND_DELETE
350 if (S_ISDIR(statbuf->st_mode)) {
351 rc = rmdir(fileName);
353 rc = unlink(fileName);
356 bb_simple_perror_msg(fileName);
360 #if ENABLE_FEATURE_FIND_CONTEXT
363 security_context_t con;
366 if (recurse_flags & ACTION_FOLLOWLINKS) {
367 rc = getfilecon(fileName, &con);
369 rc = lgetfilecon(fileName, &con);
373 rc = strcmp(ap->context, con);
380 static int FAST_FUNC fileAction(const char *fileName,
381 struct stat *statbuf,
382 void *userData IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
383 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
386 #if ENABLE_FEATURE_FIND_MAXDEPTH
387 #define minmaxdepth ((int*)userData)
389 if (depth < minmaxdepth[0]) return TRUE;
390 if (depth > minmaxdepth[1]) return SKIP;
394 #if ENABLE_FEATURE_FIND_XDEV
395 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
396 for (i = 0; i < xdev_count; i++) {
397 if (xdev_dev[i] == statbuf->st_dev)
404 i = exec_actions(actions, fileName, statbuf);
405 /* Had no explicit -print[0] or -exec? then print */
406 if ((i & TRUE) && need_print)
408 /* Cannot return 0: our caller, recursive_action(),
409 * will perror() and skip dirs (if called on dir) */
410 return (i & SKIP) ? SKIP : TRUE;
414 #if ENABLE_FEATURE_FIND_TYPE
415 static int find_type(const char *type)
421 else if (*type == 'c')
423 else if (*type == 'd')
425 else if (*type == 'p')
427 else if (*type == 'f')
429 else if (*type == 'l')
431 else if (*type == 's')
434 if (mask == 0 || *(type + 1) != '\0')
435 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
441 #if ENABLE_FEATURE_FIND_PERM \
442 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
443 || ENABLE_FEATURE_FIND_SIZE
444 static const char* plus_minus_num(const char* str)
446 if (*str == '-' || *str == '+')
452 static action*** parse_params(char **argv)
457 IF_FEATURE_FIND_NOT( PARM_char_not ,)
461 IF_FEATURE_FIND_NOT( PARM_not ,)
464 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
465 IF_FEATURE_FIND_DEPTH( PARM_depth ,)
466 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
467 IF_FEATURE_FIND_DELETE( PARM_delete ,)
468 IF_FEATURE_FIND_EXEC( PARM_exec ,)
469 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
470 /* All options starting from here require argument */
473 IF_FEATURE_FIND_PATH( PARM_path ,)
474 IF_FEATURE_FIND_REGEX( PARM_regex ,)
475 IF_FEATURE_FIND_TYPE( PARM_type ,)
476 IF_FEATURE_FIND_PERM( PARM_perm ,)
477 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
478 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
479 IF_FEATURE_FIND_NEWER( PARM_newer ,)
480 IF_FEATURE_FIND_INUM( PARM_inum ,)
481 IF_FEATURE_FIND_USER( PARM_user ,)
482 IF_FEATURE_FIND_GROUP( PARM_group ,)
483 IF_FEATURE_FIND_SIZE( PARM_size ,)
484 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
487 static const char params[] ALIGN1 =
490 IF_FEATURE_FIND_NOT( "!\0" )
494 IF_FEATURE_FIND_NOT( "-not\0" )
497 IF_FEATURE_FIND_PRINT0( "-print0\0" )
498 IF_FEATURE_FIND_DEPTH( "-depth\0" )
499 IF_FEATURE_FIND_PRUNE( "-prune\0" )
500 IF_FEATURE_FIND_DELETE( "-delete\0" )
501 IF_FEATURE_FIND_EXEC( "-exec\0" )
502 IF_FEATURE_FIND_PAREN( "(\0" )
503 /* All options starting from here require argument */
506 IF_FEATURE_FIND_PATH( "-path\0" )
507 IF_FEATURE_FIND_REGEX( "-regex\0" )
508 IF_FEATURE_FIND_TYPE( "-type\0" )
509 IF_FEATURE_FIND_PERM( "-perm\0" )
510 IF_FEATURE_FIND_MTIME( "-mtime\0" )
511 IF_FEATURE_FIND_MMIN( "-mmin\0" )
512 IF_FEATURE_FIND_NEWER( "-newer\0" )
513 IF_FEATURE_FIND_INUM( "-inum\0" )
514 IF_FEATURE_FIND_USER( "-user\0" )
515 IF_FEATURE_FIND_GROUP( "-group\0" )
516 IF_FEATURE_FIND_SIZE( "-size\0" )
517 IF_FEATURE_FIND_CONTEXT("-context\0")
521 unsigned cur_group = 0;
522 unsigned cur_action = 0;
523 IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
525 /* This is the only place in busybox where we use nested function.
526 * So far more standard alternatives were bigger. */
527 /* Suppress a warning "func without a prototype" */
528 auto action* alloc_action(int sizeof_struct, action_fp f);
529 action* alloc_action(int sizeof_struct, action_fp f)
532 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
533 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
534 appp[cur_group][cur_action] = NULL;
536 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
537 IF_FEATURE_FIND_NOT( invert_flag = 0; )
541 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
543 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
545 /* Actions have side effects and return a true or false value
546 * We implement: -print, -print0, -exec
548 * The rest are tests.
550 * Tests and actions are grouped by operators
551 * ( expr ) Force precedence
552 * ! expr True if expr is false
553 * -not expr Same as ! expr
554 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
555 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
556 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
557 * We implement: (), -a, -o
560 const char *arg = argv[0];
561 int parm = index_in_strings(params, arg);
562 const char *arg1 = argv[1];
564 if (parm >= PARM_name) {
565 /* All options starting from -name require argument */
567 bb_error_msg_and_die(bb_msg_requires_arg, arg);
571 /* We can use big switch() here, but on i386
572 * it doesn't give smaller code. Other arches? */
574 /* --- Operators --- */
575 if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
576 /* no further special handling required */
578 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
579 /* start new OR group */
581 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
582 /*appp[cur_group] = NULL; - already NULL */
583 appp[cur_group+1] = NULL;
586 #if ENABLE_FEATURE_FIND_NOT
587 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
588 /* also handles "find ! ! -name 'foo*'" */
593 /* --- Tests and actions --- */
594 else if (parm == PARM_print) {
596 /* GNU find ignores '!' here: "find ! -print" */
597 IF_FEATURE_FIND_NOT( invert_flag = 0; )
598 (void) ALLOC_ACTION(print);
600 #if ENABLE_FEATURE_FIND_PRINT0
601 else if (parm == PARM_print0) {
603 IF_FEATURE_FIND_NOT( invert_flag = 0; )
604 (void) ALLOC_ACTION(print0);
607 #if ENABLE_FEATURE_FIND_DEPTH
608 else if (parm == PARM_depth) {
609 recurse_flags |= ACTION_DEPTHFIRST;
612 #if ENABLE_FEATURE_FIND_PRUNE
613 else if (parm == PARM_prune) {
614 IF_FEATURE_FIND_NOT( invert_flag = 0; )
615 (void) ALLOC_ACTION(prune);
618 #if ENABLE_FEATURE_FIND_DELETE
619 else if (parm == PARM_delete) {
621 recurse_flags |= ACTION_DEPTHFIRST;
622 (void) ALLOC_ACTION(delete);
625 #if ENABLE_FEATURE_FIND_EXEC
626 else if (parm == PARM_exec) {
630 IF_FEATURE_FIND_NOT( invert_flag = 0; )
631 ap = ALLOC_ACTION(exec);
632 ap->exec_argv = ++argv; /* first arg after -exec */
635 if (!*argv) /* did not see ';' until end */
636 bb_error_msg_and_die("-exec CMD must end by ';'");
637 if (LONE_CHAR(argv[0], ';'))
642 if (ap->exec_argc == 0)
643 bb_error_msg_and_die(bb_msg_requires_arg, arg);
644 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
647 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
650 #if ENABLE_FEATURE_FIND_PAREN
651 else if (parm == PARM_char_brace) {
659 bb_error_msg_and_die("unpaired '('");
660 if (LONE_CHAR(*endarg, '('))
662 else if (LONE_CHAR(*endarg, ')') && !--nested) {
667 ap = ALLOC_ACTION(paren);
668 ap->subexpr = parse_params(argv + 1);
669 *endarg = (char*) ")"; /* restore NULLed parameter */
673 else if (parm == PARM_name || parm == PARM_iname) {
675 ap = ALLOC_ACTION(name);
677 ap->iname = (parm == PARM_iname);
679 #if ENABLE_FEATURE_FIND_PATH
680 else if (parm == PARM_path) {
682 ap = ALLOC_ACTION(path);
686 #if ENABLE_FEATURE_FIND_REGEX
687 else if (parm == PARM_regex) {
689 ap = ALLOC_ACTION(regex);
690 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
693 #if ENABLE_FEATURE_FIND_TYPE
694 else if (parm == PARM_type) {
696 ap = ALLOC_ACTION(type);
697 ap->type_mask = find_type(arg1);
700 #if ENABLE_FEATURE_FIND_PERM
701 /* -perm mode File's permission bits are exactly mode (octal or symbolic).
702 * Symbolic modes use mode 0 as a point of departure.
703 * -perm -mode All of the permission bits mode are set for the file.
704 * -perm +mode Any of the permission bits mode are set for the file.
706 else if (parm == PARM_perm) {
708 ap = ALLOC_ACTION(perm);
709 ap->perm_char = arg1[0];
710 arg1 = plus_minus_num(arg1);
712 if (!bb_parse_mode(arg1, &ap->perm_mask))
713 bb_error_msg_and_die("invalid mode: %s", arg1);
716 #if ENABLE_FEATURE_FIND_MTIME
717 else if (parm == PARM_mtime) {
719 ap = ALLOC_ACTION(mtime);
720 ap->mtime_char = arg1[0];
721 ap->mtime_days = xatoul(plus_minus_num(arg1));
724 #if ENABLE_FEATURE_FIND_MMIN
725 else if (parm == PARM_mmin) {
727 ap = ALLOC_ACTION(mmin);
728 ap->mmin_char = arg1[0];
729 ap->mmin_mins = xatoul(plus_minus_num(arg1));
732 #if ENABLE_FEATURE_FIND_NEWER
733 else if (parm == PARM_newer) {
734 struct stat stat_newer;
736 ap = ALLOC_ACTION(newer);
737 xstat(arg1, &stat_newer);
738 ap->newer_mtime = stat_newer.st_mtime;
741 #if ENABLE_FEATURE_FIND_INUM
742 else if (parm == PARM_inum) {
744 ap = ALLOC_ACTION(inum);
745 ap->inode_num = xatoul(arg1);
748 #if ENABLE_FEATURE_FIND_USER
749 else if (parm == PARM_user) {
751 ap = ALLOC_ACTION(user);
752 ap->uid = bb_strtou(arg1, NULL, 10);
754 ap->uid = xuname2uid(arg1);
757 #if ENABLE_FEATURE_FIND_GROUP
758 else if (parm == PARM_group) {
760 ap = ALLOC_ACTION(group);
761 ap->gid = bb_strtou(arg1, NULL, 10);
763 ap->gid = xgroup2gid(arg1);
766 #if ENABLE_FEATURE_FIND_SIZE
767 else if (parm == PARM_size) {
768 /* -size n[bckw]: file uses n units of space
769 * b (default): units are 512-byte blocks
775 #define XATOU_SFX xatoull_sfx
777 #define XATOU_SFX xatoul_sfx
779 static const struct suffix_mult find_suffixes[] = {
788 ap = ALLOC_ACTION(size);
789 ap->size_char = arg1[0];
790 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
793 #if ENABLE_FEATURE_FIND_CONTEXT
794 else if (parm == PARM_context) {
796 ap = ALLOC_ACTION(context);
798 /* SELinux headers erroneously declare non-const parameter */
799 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
800 bb_simple_perror_msg(arg1);
804 bb_error_msg("unrecognized: %s", arg);
814 int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
815 int find_main(int argc, char **argv)
817 static const char options[] ALIGN1 =
819 IF_FEATURE_FIND_XDEV( "-xdev\0" )
820 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
824 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
825 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,)
830 int i, firstopt, status = EXIT_SUCCESS;
831 #if ENABLE_FEATURE_FIND_MAXDEPTH
832 int minmaxdepth[2] = { 0, INT_MAX };
834 #define minmaxdepth NULL
837 for (firstopt = 1; firstopt < argc; firstopt++) {
838 if (argv[firstopt][0] == '-')
840 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
842 #if ENABLE_FEATURE_FIND_PAREN
843 if (LONE_CHAR(argv[firstopt], '('))
848 argv[0] = (char*)".";
853 /* All options always return true. They always take effect
854 * rather than being processed only when their place in the
855 * expression is reached.
856 * We implement: -follow, -xdev, -maxdepth
858 /* Process options, and replace then with -a */
859 /* (-a will be ignored by recursive parser later) */
860 argp = &argv[firstopt];
861 while ((arg = argp[0])) {
862 int opt = index_in_strings(options, arg);
863 if (opt == OPT_FOLLOW) {
864 recurse_flags |= ACTION_FOLLOWLINKS;
865 argp[0] = (char*)"-a";
867 #if ENABLE_FEATURE_FIND_XDEV
868 if (opt == OPT_XDEV) {
871 xdev_count = firstopt - 1;
872 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
873 for (i = 1; i < firstopt; i++) {
874 /* not xstat(): shouldn't bomb out on
875 * "find not_exist exist -xdev" */
876 if (stat(argv[i], &stbuf))
878 xdev_dev[i-1] = stbuf.st_dev;
881 argp[0] = (char*)"-a";
884 #if ENABLE_FEATURE_FIND_MAXDEPTH
885 if (opt == OPT_MINDEPTH || opt == OPT_MINDEPTH + 1) {
888 minmaxdepth[opt - OPT_MINDEPTH] = xatoi_u(argp[1]);
889 argp[0] = (char*)"-a";
890 argp[1] = (char*)"-a";
897 actions = parse_params(&argv[firstopt]);
899 for (i = 1; i < firstopt; i++) {
900 if (!recursive_action(argv[i],
901 recurse_flags, /* flags */
902 fileAction, /* file action */
903 fileAction, /* dir action */
904 #if ENABLE_FEATURE_FIND_MAXDEPTH
905 minmaxdepth, /* user data */
907 NULL, /* user data */
910 status = EXIT_FAILURE;