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 USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
66 USE_FEATURE_FIND_XDEV(static int xdev_count;)
68 typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
72 #if ENABLE_FEATURE_FIND_NOT
76 #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
77 #define ACTF(name) static int func_##name(const char *fileName ATTRIBUTE_UNUSED, \
78 struct stat *statbuf ATTRIBUTE_UNUSED, \
79 action_##name* ap ATTRIBUTE_UNUSED)
81 ACTS(name, const char *pattern; bool iname;)
82 USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
83 USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
84 USE_FEATURE_FIND_PRINT0( ACTS(print0))
85 USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
86 USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
87 USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
88 USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
89 USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
90 USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
91 USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
92 USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
93 USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
94 USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
95 USE_FEATURE_FIND_PRUNE( ACTS(prune))
96 USE_FEATURE_FIND_DELETE( ACTS(delete))
97 USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
98 USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
100 static action ***actions;
101 static bool need_print = 1;
102 static int recurse_flags = ACTION_RECURSE;
104 #if ENABLE_FEATURE_FIND_EXEC
105 static unsigned count_subst(const char *str)
108 while ((str = strstr(str, "{}")) != NULL) {
116 static char* subst(const char *src, unsigned count, const char* filename)
118 char *buf, *dst, *end;
119 size_t flen = strlen(filename);
120 /* we replace each '{}' with filename: growth by strlen-2 */
121 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
122 while ((end = strstr(src, "{}"))) {
123 memcpy(dst, src, end - src);
126 memcpy(dst, filename, flen);
134 /* Return values of ACTFs ('action functions') are a bit mask:
135 * bit 1=1: prune (use SKIP constant for setting it)
136 * bit 0=1: matched successfully (TRUE)
139 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
146 /* "action group" is a set of actions ANDed together.
147 * groups are ORed together.
148 * We simply evaluate each group until we find one in which all actions
151 /* -prune is special: if it is encountered, then we won't
152 * descend into current directory. It doesn't matter whether
153 * action group (in which -prune sits) will succeed or not:
154 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
155 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
156 * not starting with 'f' */
158 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
159 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
160 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
161 * On return, bit is restored. */
164 while ((app = appp[++cur_group])) {
165 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
168 ap = app[++cur_action];
169 if (!ap) /* all actions in group were successful */
170 return rc ^ TRUE; /* restore TRUE bit */
171 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
172 #if ENABLE_FEATURE_FIND_NOT
173 if (ap->invert) rc ^= TRUE;
175 if (rc & TRUE) /* current group failed, try next */
179 return rc ^ TRUE; /* restore TRUE bit */
185 const char *tmp = bb_basename(fileName);
186 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
188 while (tmp != fileName && *--tmp != '/')
193 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
196 #if ENABLE_FEATURE_FIND_PATH
199 return fnmatch(ap->pattern, fileName, 0) == 0;
202 #if ENABLE_FEATURE_FIND_REGEX
206 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
207 return 0; /* no match */
209 return 0; /* match doesn't start at pos 0 */
210 if (fileName[match.rm_eo])
211 return 0; /* match doesn't end exactly at end of pathname */
215 #if ENABLE_FEATURE_FIND_TYPE
218 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
221 #if ENABLE_FEATURE_FIND_PERM
224 /* -perm +mode: at least one of perm_mask bits are set */
225 if (ap->perm_char == '+')
226 return (statbuf->st_mode & ap->perm_mask) != 0;
227 /* -perm -mode: all of perm_mask are set */
228 if (ap->perm_char == '-')
229 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
230 /* -perm mode: file mode must match perm_mask */
231 return (statbuf->st_mode & 07777) == ap->perm_mask;
234 #if ENABLE_FEATURE_FIND_MTIME
237 time_t file_age = time(NULL) - statbuf->st_mtime;
238 time_t mtime_secs = ap->mtime_days * 24*60*60;
239 if (ap->mtime_char == '+')
240 return file_age >= mtime_secs + 24*60*60;
241 if (ap->mtime_char == '-')
242 return file_age < mtime_secs;
243 /* just numeric mtime */
244 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
247 #if ENABLE_FEATURE_FIND_MMIN
250 time_t file_age = time(NULL) - statbuf->st_mtime;
251 time_t mmin_secs = ap->mmin_mins * 60;
252 if (ap->mmin_char == '+')
253 return file_age >= mmin_secs + 60;
254 if (ap->mmin_char == '-')
255 return file_age < mmin_secs;
256 /* just numeric mmin */
257 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
260 #if ENABLE_FEATURE_FIND_NEWER
263 return (ap->newer_mtime < statbuf->st_mtime);
266 #if ENABLE_FEATURE_FIND_INUM
269 return (statbuf->st_ino == ap->inode_num);
272 #if ENABLE_FEATURE_FIND_EXEC
276 char *argv[ap->exec_argc + 1];
277 for (i = 0; i < ap->exec_argc; i++)
278 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
279 argv[i] = NULL; /* terminate the list */
281 rc = spawn_and_wait(argv);
283 bb_simple_perror_msg(argv[0]);
288 return rc == 0; /* return 1 if exitcode 0 */
291 #if ENABLE_FEATURE_FIND_USER
294 return (statbuf->st_uid == ap->uid);
297 #if ENABLE_FEATURE_FIND_GROUP
300 return (statbuf->st_gid == ap->gid);
303 #if ENABLE_FEATURE_FIND_PRINT0
306 printf("%s%c", fileName, '\0');
315 #if ENABLE_FEATURE_FIND_PAREN
318 return exec_actions(ap->subexpr, fileName, statbuf);
321 #if ENABLE_FEATURE_FIND_SIZE
324 if (ap->size_char == '+')
325 return statbuf->st_size > ap->size;
326 if (ap->size_char == '-')
327 return statbuf->st_size < ap->size;
328 return statbuf->st_size == ap->size;
331 #if ENABLE_FEATURE_FIND_PRUNE
333 * -prune: if -depth is not given, return true and do not descend
334 * current dir; if -depth is given, return false with no effect.
336 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
343 #if ENABLE_FEATURE_FIND_DELETE
347 if (S_ISDIR(statbuf->st_mode)) {
348 rc = rmdir(fileName);
350 rc = unlink(fileName);
353 bb_simple_perror_msg(fileName);
357 #if ENABLE_FEATURE_FIND_CONTEXT
360 security_context_t con;
363 if (recurse_flags & ACTION_FOLLOWLINKS) {
364 rc = getfilecon(fileName, &con);
366 rc = lgetfilecon(fileName, &con);
370 rc = strcmp(ap->context, con);
377 static int fileAction(const char *fileName,
378 struct stat *statbuf,
379 void *userData SKIP_FEATURE_FIND_MAXDEPTH(ATTRIBUTE_UNUSED),
380 int depth SKIP_FEATURE_FIND_MAXDEPTH(ATTRIBUTE_UNUSED))
383 #if ENABLE_FEATURE_FIND_MAXDEPTH
384 int maxdepth = (int)(ptrdiff_t)userData;
386 if (depth > maxdepth) return SKIP;
389 #if ENABLE_FEATURE_FIND_XDEV
390 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
391 for (i = 0; i < xdev_count; i++) {
392 if (xdev_dev[i] == statbuf->st_dev)
399 i = exec_actions(actions, fileName, statbuf);
400 /* Had no explicit -print[0] or -exec? then print */
401 if ((i & TRUE) && need_print)
403 /* Cannot return 0: our caller, recursive_action(),
404 * will perror() and skip dirs (if called on dir) */
405 return (i & SKIP) ? SKIP : TRUE;
409 #if ENABLE_FEATURE_FIND_TYPE
410 static int find_type(const char *type)
416 else if (*type == 'c')
418 else if (*type == 'd')
420 else if (*type == 'p')
422 else if (*type == 'f')
424 else if (*type == 'l')
426 else if (*type == 's')
429 if (mask == 0 || *(type + 1) != '\0')
430 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
436 #if ENABLE_FEATURE_FIND_PERM \
437 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
438 || ENABLE_FEATURE_FIND_SIZE
439 static const char* plus_minus_num(const char* str)
441 if (*str == '-' || *str == '+')
447 static action*** parse_params(char **argv)
452 USE_FEATURE_FIND_NOT( PARM_char_not ,)
456 USE_FEATURE_FIND_NOT( PARM_not ,)
459 USE_FEATURE_FIND_PRINT0( PARM_print0 ,)
460 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
461 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
462 USE_FEATURE_FIND_DELETE( PARM_delete ,)
463 USE_FEATURE_FIND_EXEC( PARM_exec ,)
464 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
465 /* All options starting from here require argument */
468 USE_FEATURE_FIND_PATH( PARM_path ,)
469 USE_FEATURE_FIND_REGEX( PARM_regex ,)
470 USE_FEATURE_FIND_TYPE( PARM_type ,)
471 USE_FEATURE_FIND_PERM( PARM_perm ,)
472 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
473 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
474 USE_FEATURE_FIND_NEWER( PARM_newer ,)
475 USE_FEATURE_FIND_INUM( PARM_inum ,)
476 USE_FEATURE_FIND_USER( PARM_user ,)
477 USE_FEATURE_FIND_GROUP( PARM_group ,)
478 USE_FEATURE_FIND_SIZE( PARM_size ,)
479 USE_FEATURE_FIND_CONTEXT(PARM_context ,)
482 static const char params[] ALIGN1 =
485 USE_FEATURE_FIND_NOT( "!\0" )
489 USE_FEATURE_FIND_NOT( "-not\0" )
492 USE_FEATURE_FIND_PRINT0( "-print0\0" )
493 USE_FEATURE_FIND_DEPTH( "-depth\0" )
494 USE_FEATURE_FIND_PRUNE( "-prune\0" )
495 USE_FEATURE_FIND_DELETE( "-delete\0" )
496 USE_FEATURE_FIND_EXEC( "-exec\0" )
497 USE_FEATURE_FIND_PAREN( "(\0" )
498 /* All options starting from here require argument */
501 USE_FEATURE_FIND_PATH( "-path\0" )
502 USE_FEATURE_FIND_REGEX( "-regex\0" )
503 USE_FEATURE_FIND_TYPE( "-type\0" )
504 USE_FEATURE_FIND_PERM( "-perm\0" )
505 USE_FEATURE_FIND_MTIME( "-mtime\0" )
506 USE_FEATURE_FIND_MMIN( "-mmin\0" )
507 USE_FEATURE_FIND_NEWER( "-newer\0" )
508 USE_FEATURE_FIND_INUM( "-inum\0" )
509 USE_FEATURE_FIND_USER( "-user\0" )
510 USE_FEATURE_FIND_GROUP( "-group\0" )
511 USE_FEATURE_FIND_SIZE( "-size\0" )
512 USE_FEATURE_FIND_CONTEXT("-context\0")
516 unsigned cur_group = 0;
517 unsigned cur_action = 0;
518 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
520 /* 'static' doesn't work here! (gcc 4.1.2) */
521 action* alloc_action(int sizeof_struct, action_fp f)
524 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
525 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
526 appp[cur_group][cur_action] = NULL;
528 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
529 USE_FEATURE_FIND_NOT( invert_flag = 0; )
533 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
535 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
537 /* Actions have side effects and return a true or false value
538 * We implement: -print, -print0, -exec
540 * The rest are tests.
542 * Tests and actions are grouped by operators
543 * ( expr ) Force precedence
544 * ! expr True if expr is false
545 * -not expr Same as ! expr
546 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
547 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
548 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
549 * We implement: (), -a, -o
552 const char *arg = argv[0];
553 int parm = index_in_strings(params, arg);
554 const char *arg1 = argv[1];
556 if (parm >= PARM_name) {
557 /* All options starting from -name require argument */
559 bb_error_msg_and_die(bb_msg_requires_arg, arg);
563 /* We can use big switch() here, but on i386
564 * it doesn't give smaller code. Other arches? */
566 /* --- Operators --- */
567 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
568 /* no further special handling required */
570 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
571 /* start new OR group */
573 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
574 /*appp[cur_group] = NULL; - already NULL */
575 appp[cur_group+1] = NULL;
578 #if ENABLE_FEATURE_FIND_NOT
579 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
580 /* also handles "find ! ! -name 'foo*'" */
585 /* --- Tests and actions --- */
586 else if (parm == PARM_print) {
588 /* GNU find ignores '!' here: "find ! -print" */
589 USE_FEATURE_FIND_NOT( invert_flag = 0; )
590 (void) ALLOC_ACTION(print);
592 #if ENABLE_FEATURE_FIND_PRINT0
593 else if (parm == PARM_print0) {
595 USE_FEATURE_FIND_NOT( invert_flag = 0; )
596 (void) ALLOC_ACTION(print0);
599 #if ENABLE_FEATURE_FIND_DEPTH
600 else if (parm == PARM_depth) {
601 recurse_flags |= ACTION_DEPTHFIRST;
604 #if ENABLE_FEATURE_FIND_PRUNE
605 else if (parm == PARM_prune) {
606 USE_FEATURE_FIND_NOT( invert_flag = 0; )
607 (void) ALLOC_ACTION(prune);
610 #if ENABLE_FEATURE_FIND_DELETE
611 else if (parm == PARM_delete) {
613 recurse_flags |= ACTION_DEPTHFIRST;
614 (void) ALLOC_ACTION(delete);
617 #if ENABLE_FEATURE_FIND_EXEC
618 else if (parm == PARM_exec) {
622 USE_FEATURE_FIND_NOT( invert_flag = 0; )
623 ap = ALLOC_ACTION(exec);
624 ap->exec_argv = ++argv; /* first arg after -exec */
627 if (!*argv) /* did not see ';' until end */
628 bb_error_msg_and_die("-exec CMD must end by ';'");
629 if (LONE_CHAR(argv[0], ';'))
634 if (ap->exec_argc == 0)
635 bb_error_msg_and_die(bb_msg_requires_arg, arg);
636 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
639 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
642 #if ENABLE_FEATURE_FIND_PAREN
643 else if (parm == PARM_char_brace) {
651 bb_error_msg_and_die("unpaired '('");
652 if (LONE_CHAR(*endarg, '('))
654 else if (LONE_CHAR(*endarg, ')') && !--nested) {
659 ap = ALLOC_ACTION(paren);
660 ap->subexpr = parse_params(argv + 1);
661 *endarg = (char*) ")"; /* restore NULLed parameter */
665 else if (parm == PARM_name || parm == PARM_iname) {
667 ap = ALLOC_ACTION(name);
669 ap->iname = (parm == PARM_iname);
671 #if ENABLE_FEATURE_FIND_PATH
672 else if (parm == PARM_path) {
674 ap = ALLOC_ACTION(path);
678 #if ENABLE_FEATURE_FIND_REGEX
679 else if (parm == PARM_regex) {
681 ap = ALLOC_ACTION(regex);
682 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
685 #if ENABLE_FEATURE_FIND_TYPE
686 else if (parm == PARM_type) {
688 ap = ALLOC_ACTION(type);
689 ap->type_mask = find_type(arg1);
692 #if ENABLE_FEATURE_FIND_PERM
693 /* -perm mode File's permission bits are exactly mode (octal or symbolic).
694 * Symbolic modes use mode 0 as a point of departure.
695 * -perm -mode All of the permission bits mode are set for the file.
696 * -perm +mode Any of the permission bits mode are set for the file.
698 else if (parm == PARM_perm) {
700 ap = ALLOC_ACTION(perm);
701 ap->perm_char = arg1[0];
702 arg1 = plus_minus_num(arg1);
704 if (!bb_parse_mode(arg1, &ap->perm_mask))
705 bb_error_msg_and_die("invalid mode: %s", arg1);
708 #if ENABLE_FEATURE_FIND_MTIME
709 else if (parm == PARM_mtime) {
711 ap = ALLOC_ACTION(mtime);
712 ap->mtime_char = arg1[0];
713 ap->mtime_days = xatoul(plus_minus_num(arg1));
716 #if ENABLE_FEATURE_FIND_MMIN
717 else if (parm == PARM_mmin) {
719 ap = ALLOC_ACTION(mmin);
720 ap->mmin_char = arg1[0];
721 ap->mmin_mins = xatoul(plus_minus_num(arg1));
724 #if ENABLE_FEATURE_FIND_NEWER
725 else if (parm == PARM_newer) {
726 struct stat stat_newer;
728 ap = ALLOC_ACTION(newer);
729 xstat(arg1, &stat_newer);
730 ap->newer_mtime = stat_newer.st_mtime;
733 #if ENABLE_FEATURE_FIND_INUM
734 else if (parm == PARM_inum) {
736 ap = ALLOC_ACTION(inum);
737 ap->inode_num = xatoul(arg1);
740 #if ENABLE_FEATURE_FIND_USER
741 else if (parm == PARM_user) {
743 ap = ALLOC_ACTION(user);
744 ap->uid = bb_strtou(arg1, NULL, 10);
746 ap->uid = xuname2uid(arg1);
749 #if ENABLE_FEATURE_FIND_GROUP
750 else if (parm == PARM_group) {
752 ap = ALLOC_ACTION(group);
753 ap->gid = bb_strtou(arg1, NULL, 10);
755 ap->gid = xgroup2gid(arg1);
758 #if ENABLE_FEATURE_FIND_SIZE
759 else if (parm == PARM_size) {
760 /* -size n[bckw]: file uses n units of space
761 * b (default): units are 512-byte blocks
767 #define XATOU_SFX xatoull_sfx
769 #define XATOU_SFX xatoul_sfx
771 static const struct suffix_mult find_suffixes[] = {
780 ap = ALLOC_ACTION(size);
781 ap->size_char = arg1[0];
782 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
785 #if ENABLE_FEATURE_FIND_CONTEXT
786 else if (parm == PARM_context) {
788 ap = ALLOC_ACTION(context);
790 /* SELinux headers erroneously declare non-const parameter */
791 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
792 bb_simple_perror_msg(arg1);
796 bb_error_msg("unrecognized: %s", arg);
806 int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
807 int find_main(int argc, char **argv)
809 static const char options[] ALIGN1 =
811 USE_FEATURE_FIND_XDEV( "-xdev\0" )
812 USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
816 USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
817 USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
822 int i, firstopt, status = EXIT_SUCCESS;
823 #if ENABLE_FEATURE_FIND_MAXDEPTH
824 int maxdepth = INT_MAX;
827 for (firstopt = 1; firstopt < argc; firstopt++) {
828 if (argv[firstopt][0] == '-')
830 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
832 #if ENABLE_FEATURE_FIND_PAREN
833 if (LONE_CHAR(argv[firstopt], '('))
838 argv[0] = (char*)".";
843 /* All options always return true. They always take effect
844 * rather than being processed only when their place in the
845 * expression is reached.
846 * We implement: -follow, -xdev, -maxdepth
848 /* Process options, and replace then with -a */
849 /* (-a will be ignored by recursive parser later) */
850 argp = &argv[firstopt];
851 while ((arg = argp[0])) {
852 int opt = index_in_strings(options, arg);
853 if (opt == OPT_FOLLOW) {
854 recurse_flags |= ACTION_FOLLOWLINKS;
855 argp[0] = (char*)"-a";
857 #if ENABLE_FEATURE_FIND_XDEV
858 if (opt == OPT_XDEV) {
861 xdev_count = firstopt - 1;
862 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
863 for (i = 1; i < firstopt; i++) {
864 /* not xstat(): shouldn't bomb out on
865 * "find not_exist exist -xdev" */
866 if (stat(argv[i], &stbuf))
868 xdev_dev[i-1] = stbuf.st_dev;
871 argp[0] = (char*)"-a";
874 #if ENABLE_FEATURE_FIND_MAXDEPTH
875 if (opt == OPT_MAXDEPTH) {
878 maxdepth = xatoi_u(argp[1]);
879 argp[0] = (char*)"-a";
880 argp[1] = (char*)"-a";
887 actions = parse_params(&argv[firstopt]);
889 for (i = 1; i < firstopt; i++) {
890 if (!recursive_action(argv[i],
891 recurse_flags, /* flags */
892 fileAction, /* file action */
893 fileAction, /* dir action */
894 #if ENABLE_FEATURE_FIND_MAXDEPTH
895 /* double cast suppresses
896 * "cast to ptr from int of different size" */
897 (void*)(ptrdiff_t)maxdepth,/* user data */
899 NULL, /* user data */
902 status = EXIT_FAILURE;