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, struct stat *statbuf, action_##name* ap)
79 ACTS(name, const char *pattern; bool iname;)
80 USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
81 USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
82 USE_FEATURE_FIND_PRINT0( ACTS(print0))
83 USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
84 USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
85 USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
86 USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
87 USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
88 USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
89 USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
90 USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
91 USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
92 USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
93 USE_FEATURE_FIND_PRUNE( ACTS(prune))
94 USE_FEATURE_FIND_DELETE( ACTS(delete))
95 USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
96 USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
98 static action ***actions;
99 static bool need_print = 1;
100 static int recurse_flags = ACTION_RECURSE;
102 #if ENABLE_FEATURE_FIND_EXEC
103 static unsigned count_subst(const char *str)
106 while ((str = strstr(str, "{}")) != NULL) {
114 static char* subst(const char *src, unsigned count, const char* filename)
116 char *buf, *dst, *end;
117 size_t flen = strlen(filename);
118 /* we replace each '{}' with filename: growth by strlen-2 */
119 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
120 while ((end = strstr(src, "{}"))) {
121 memcpy(dst, src, end - src);
124 memcpy(dst, filename, flen);
132 /* Return values of ACTFs ('action functions') are a bit mask:
133 * bit 1=1: prune (use SKIP constant for setting it)
134 * bit 0=1: matched successfully (TRUE)
137 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
144 /* "action group" is a set of actions ANDed together.
145 * groups are ORed together.
146 * We simply evaluate each group until we find one in which all actions
149 /* -prune is special: if it is encountered, then we won't
150 * descend into current directory. It doesn't matter whether
151 * action group (in which -prune sits) will succeed or not:
152 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
153 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
154 * not starting with 'f' */
156 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
157 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
158 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
159 * On return, bit is restored. */
162 while ((app = appp[++cur_group])) {
163 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
166 ap = app[++cur_action];
167 if (!ap) /* all actions in group were successful */
168 return rc ^ TRUE; /* restore TRUE bit */
169 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
170 #if ENABLE_FEATURE_FIND_NOT
171 if (ap->invert) rc ^= TRUE;
173 if (rc & TRUE) /* current group failed, try next */
177 return rc ^ TRUE; /* restore TRUE bit */
183 const char *tmp = bb_basename(fileName);
184 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
186 while (tmp != fileName && *--tmp != '/')
191 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
194 #if ENABLE_FEATURE_FIND_PATH
197 return fnmatch(ap->pattern, fileName, 0) == 0;
200 #if ENABLE_FEATURE_FIND_REGEX
204 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
205 return 0; /* no match */
207 return 0; /* match doesn't start at pos 0 */
208 if (fileName[match.rm_eo])
209 return 0; /* match doesn't end exactly at end of pathname */
213 #if ENABLE_FEATURE_FIND_TYPE
216 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
219 #if ENABLE_FEATURE_FIND_PERM
222 /* -perm +mode: at least one of perm_mask bits are set */
223 if (ap->perm_char == '+')
224 return (statbuf->st_mode & ap->perm_mask) != 0;
225 /* -perm -mode: all of perm_mask are set */
226 if (ap->perm_char == '-')
227 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
228 /* -perm mode: file mode must match perm_mask */
229 return (statbuf->st_mode & 07777) == ap->perm_mask;
232 #if ENABLE_FEATURE_FIND_MTIME
235 time_t file_age = time(NULL) - statbuf->st_mtime;
236 time_t mtime_secs = ap->mtime_days * 24*60*60;
237 if (ap->mtime_char == '+')
238 return file_age >= mtime_secs + 24*60*60;
239 if (ap->mtime_char == '-')
240 return file_age < mtime_secs;
241 /* just numeric mtime */
242 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
245 #if ENABLE_FEATURE_FIND_MMIN
248 time_t file_age = time(NULL) - statbuf->st_mtime;
249 time_t mmin_secs = ap->mmin_mins * 60;
250 if (ap->mmin_char == '+')
251 return file_age >= mmin_secs + 60;
252 if (ap->mmin_char == '-')
253 return file_age < mmin_secs;
254 /* just numeric mmin */
255 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
258 #if ENABLE_FEATURE_FIND_NEWER
261 return (ap->newer_mtime < statbuf->st_mtime);
264 #if ENABLE_FEATURE_FIND_INUM
267 return (statbuf->st_ino == ap->inode_num);
270 #if ENABLE_FEATURE_FIND_EXEC
274 char *argv[ap->exec_argc + 1];
275 for (i = 0; i < ap->exec_argc; i++)
276 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
277 argv[i] = NULL; /* terminate the list */
279 rc = spawn_and_wait(argv);
281 bb_simple_perror_msg(argv[0]);
286 return rc == 0; /* return 1 if exitcode 0 */
289 #if ENABLE_FEATURE_FIND_USER
292 return (statbuf->st_uid == ap->uid);
295 #if ENABLE_FEATURE_FIND_GROUP
298 return (statbuf->st_gid == ap->gid);
301 #if ENABLE_FEATURE_FIND_PRINT0
304 printf("%s%c", fileName, '\0');
313 #if ENABLE_FEATURE_FIND_PAREN
316 return exec_actions(ap->subexpr, fileName, statbuf);
319 #if ENABLE_FEATURE_FIND_SIZE
322 if (ap->size_char == '+')
323 return statbuf->st_size > ap->size;
324 if (ap->size_char == '-')
325 return statbuf->st_size < ap->size;
326 return statbuf->st_size == ap->size;
329 #if ENABLE_FEATURE_FIND_PRUNE
331 * -prune: if -depth is not given, return true and do not descend
332 * current dir; if -depth is given, return false with no effect.
334 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
341 #if ENABLE_FEATURE_FIND_DELETE
345 if (S_ISDIR(statbuf->st_mode)) {
346 rc = rmdir(fileName);
348 rc = unlink(fileName);
351 bb_simple_perror_msg(fileName);
355 #if ENABLE_FEATURE_FIND_CONTEXT
358 security_context_t con;
361 if (recurse_flags & ACTION_FOLLOWLINKS) {
362 rc = getfilecon(fileName, &con);
364 rc = lgetfilecon(fileName, &con);
368 rc = strcmp(ap->context, con);
375 static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth)
378 #if ENABLE_FEATURE_FIND_MAXDEPTH
379 int maxdepth = (int)(ptrdiff_t)userData;
381 if (depth > maxdepth) return SKIP;
384 #if ENABLE_FEATURE_FIND_XDEV
385 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
386 for (i = 0; i < xdev_count; i++) {
387 if (xdev_dev[i] == statbuf->st_dev)
394 i = exec_actions(actions, fileName, statbuf);
395 /* Had no explicit -print[0] or -exec? then print */
396 if ((i & TRUE) && need_print)
398 /* Cannot return 0: our caller, recursive_action(),
399 * will perror() and skip dirs (if called on dir) */
400 return (i & SKIP) ? SKIP : TRUE;
404 #if ENABLE_FEATURE_FIND_TYPE
405 static int find_type(const char *type)
411 else if (*type == 'c')
413 else if (*type == 'd')
415 else if (*type == 'p')
417 else if (*type == 'f')
419 else if (*type == 'l')
421 else if (*type == 's')
424 if (mask == 0 || *(type + 1) != '\0')
425 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
431 #if ENABLE_FEATURE_FIND_PERM \
432 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
433 || ENABLE_FEATURE_FIND_SIZE
434 static const char* plus_minus_num(const char* str)
436 if (*str == '-' || *str == '+')
442 static action*** parse_params(char **argv)
447 USE_FEATURE_FIND_NOT( PARM_char_not ,)
451 USE_FEATURE_FIND_NOT( PARM_not ,)
454 USE_FEATURE_FIND_PRINT0( PARM_print0 ,)
455 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
456 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
457 USE_FEATURE_FIND_DELETE( PARM_delete ,)
458 USE_FEATURE_FIND_EXEC( PARM_exec ,)
459 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
460 /* All options starting from here require argument */
463 USE_FEATURE_FIND_PATH( PARM_path ,)
464 USE_FEATURE_FIND_REGEX( PARM_regex ,)
465 USE_FEATURE_FIND_TYPE( PARM_type ,)
466 USE_FEATURE_FIND_PERM( PARM_perm ,)
467 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
468 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
469 USE_FEATURE_FIND_NEWER( PARM_newer ,)
470 USE_FEATURE_FIND_INUM( PARM_inum ,)
471 USE_FEATURE_FIND_USER( PARM_user ,)
472 USE_FEATURE_FIND_GROUP( PARM_group ,)
473 USE_FEATURE_FIND_SIZE( PARM_size ,)
474 USE_FEATURE_FIND_CONTEXT(PARM_context ,)
477 static const char params[] ALIGN1 =
480 USE_FEATURE_FIND_NOT( "!\0" )
484 USE_FEATURE_FIND_NOT( "-not\0" )
487 USE_FEATURE_FIND_PRINT0( "-print0\0" )
488 USE_FEATURE_FIND_DEPTH( "-depth\0" )
489 USE_FEATURE_FIND_PRUNE( "-prune\0" )
490 USE_FEATURE_FIND_DELETE( "-delete\0" )
491 USE_FEATURE_FIND_EXEC( "-exec\0" )
492 USE_FEATURE_FIND_PAREN( "(\0" )
493 /* All options starting from here require argument */
496 USE_FEATURE_FIND_PATH( "-path\0" )
497 USE_FEATURE_FIND_REGEX( "-regex\0" )
498 USE_FEATURE_FIND_TYPE( "-type\0" )
499 USE_FEATURE_FIND_PERM( "-perm\0" )
500 USE_FEATURE_FIND_MTIME( "-mtime\0" )
501 USE_FEATURE_FIND_MMIN( "-mmin\0" )
502 USE_FEATURE_FIND_NEWER( "-newer\0" )
503 USE_FEATURE_FIND_INUM( "-inum\0" )
504 USE_FEATURE_FIND_USER( "-user\0" )
505 USE_FEATURE_FIND_GROUP( "-group\0" )
506 USE_FEATURE_FIND_SIZE( "-size\0" )
507 USE_FEATURE_FIND_CONTEXT("-context\0")
511 unsigned cur_group = 0;
512 unsigned cur_action = 0;
513 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
515 /* 'static' doesn't work here! (gcc 4.1.2) */
516 action* alloc_action(int sizeof_struct, action_fp f)
519 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
520 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
521 appp[cur_group][cur_action] = NULL;
523 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
524 USE_FEATURE_FIND_NOT( invert_flag = 0; )
528 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
530 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
532 /* Actions have side effects and return a true or false value
533 * We implement: -print, -print0, -exec
535 * The rest are tests.
537 * Tests and actions are grouped by operators
538 * ( expr ) Force precedence
539 * ! expr True if expr is false
540 * -not expr Same as ! expr
541 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
542 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
543 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
544 * We implement: (), -a, -o
547 const char *arg = argv[0];
548 int parm = index_in_strings(params, arg);
549 const char *arg1 = argv[1];
551 if (parm >= PARM_name) {
552 /* All options starting from -name require argument */
554 bb_error_msg_and_die(bb_msg_requires_arg, arg);
558 /* We can use big switch() here, but on i386
559 * it doesn't give smaller code. Other arches? */
561 /* --- Operators --- */
562 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
563 /* no further special handling required */
565 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
566 /* start new OR group */
568 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
569 /*appp[cur_group] = NULL; - already NULL */
570 appp[cur_group+1] = NULL;
573 #if ENABLE_FEATURE_FIND_NOT
574 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
575 /* also handles "find ! ! -name 'foo*'" */
580 /* --- Tests and actions --- */
581 else if (parm == PARM_print) {
583 /* GNU find ignores '!' here: "find ! -print" */
584 USE_FEATURE_FIND_NOT( invert_flag = 0; )
585 (void) ALLOC_ACTION(print);
587 #if ENABLE_FEATURE_FIND_PRINT0
588 else if (parm == PARM_print0) {
590 USE_FEATURE_FIND_NOT( invert_flag = 0; )
591 (void) ALLOC_ACTION(print0);
594 #if ENABLE_FEATURE_FIND_DEPTH
595 else if (parm == PARM_depth) {
596 recurse_flags |= ACTION_DEPTHFIRST;
599 #if ENABLE_FEATURE_FIND_PRUNE
600 else if (parm == PARM_prune) {
601 USE_FEATURE_FIND_NOT( invert_flag = 0; )
602 (void) ALLOC_ACTION(prune);
605 #if ENABLE_FEATURE_FIND_DELETE
606 else if (parm == PARM_delete) {
608 recurse_flags |= ACTION_DEPTHFIRST;
609 (void) ALLOC_ACTION(delete);
612 #if ENABLE_FEATURE_FIND_EXEC
613 else if (parm == PARM_exec) {
617 USE_FEATURE_FIND_NOT( invert_flag = 0; )
618 ap = ALLOC_ACTION(exec);
619 ap->exec_argv = ++argv; /* first arg after -exec */
622 if (!*argv) /* did not see ';' until end */
623 bb_error_msg_and_die("-exec CMD must end by ';'");
624 if (LONE_CHAR(argv[0], ';'))
629 if (ap->exec_argc == 0)
630 bb_error_msg_and_die(bb_msg_requires_arg, arg);
631 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
634 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
637 #if ENABLE_FEATURE_FIND_PAREN
638 else if (parm == PARM_char_brace) {
646 bb_error_msg_and_die("unpaired '('");
647 if (LONE_CHAR(*endarg, '('))
649 else if (LONE_CHAR(*endarg, ')') && !--nested) {
654 ap = ALLOC_ACTION(paren);
655 ap->subexpr = parse_params(argv + 1);
656 *endarg = (char*) ")"; /* restore NULLed parameter */
660 else if (parm == PARM_name || parm == PARM_iname) {
662 ap = ALLOC_ACTION(name);
664 ap->iname = (parm == PARM_iname);
666 #if ENABLE_FEATURE_FIND_PATH
667 else if (parm == PARM_path) {
669 ap = ALLOC_ACTION(path);
673 #if ENABLE_FEATURE_FIND_REGEX
674 else if (parm == PARM_regex) {
676 ap = ALLOC_ACTION(regex);
677 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
680 #if ENABLE_FEATURE_FIND_TYPE
681 else if (parm == PARM_type) {
683 ap = ALLOC_ACTION(type);
684 ap->type_mask = find_type(arg1);
687 #if ENABLE_FEATURE_FIND_PERM
688 /* -perm mode File's permission bits are exactly mode (octal or symbolic).
689 * Symbolic modes use mode 0 as a point of departure.
690 * -perm -mode All of the permission bits mode are set for the file.
691 * -perm +mode Any of the permission bits mode are set for the file.
693 else if (parm == PARM_perm) {
695 ap = ALLOC_ACTION(perm);
696 ap->perm_char = arg1[0];
697 arg1 = plus_minus_num(arg1);
699 if (!bb_parse_mode(arg1, &ap->perm_mask))
700 bb_error_msg_and_die("invalid mode: %s", arg1);
703 #if ENABLE_FEATURE_FIND_MTIME
704 else if (parm == PARM_mtime) {
706 ap = ALLOC_ACTION(mtime);
707 ap->mtime_char = arg1[0];
708 ap->mtime_days = xatoul(plus_minus_num(arg1));
711 #if ENABLE_FEATURE_FIND_MMIN
712 else if (parm == PARM_mmin) {
714 ap = ALLOC_ACTION(mmin);
715 ap->mmin_char = arg1[0];
716 ap->mmin_mins = xatoul(plus_minus_num(arg1));
719 #if ENABLE_FEATURE_FIND_NEWER
720 else if (parm == PARM_newer) {
721 struct stat stat_newer;
723 ap = ALLOC_ACTION(newer);
724 xstat(arg1, &stat_newer);
725 ap->newer_mtime = stat_newer.st_mtime;
728 #if ENABLE_FEATURE_FIND_INUM
729 else if (parm == PARM_inum) {
731 ap = ALLOC_ACTION(inum);
732 ap->inode_num = xatoul(arg1);
735 #if ENABLE_FEATURE_FIND_USER
736 else if (parm == PARM_user) {
738 ap = ALLOC_ACTION(user);
739 ap->uid = bb_strtou(arg1, NULL, 10);
741 ap->uid = xuname2uid(arg1);
744 #if ENABLE_FEATURE_FIND_GROUP
745 else if (parm == PARM_group) {
747 ap = ALLOC_ACTION(group);
748 ap->gid = bb_strtou(arg1, NULL, 10);
750 ap->gid = xgroup2gid(arg1);
753 #if ENABLE_FEATURE_FIND_SIZE
754 else if (parm == PARM_size) {
755 /* -size n[bckw]: file uses n units of space
756 * b (default): units are 512-byte blocks
762 #define XATOU_SFX xatoull_sfx
764 #define XATOU_SFX xatoul_sfx
766 static const struct suffix_mult find_suffixes[] = {
775 ap = ALLOC_ACTION(size);
776 ap->size_char = arg1[0];
777 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
780 #if ENABLE_FEATURE_FIND_CONTEXT
781 else if (parm == PARM_context) {
783 ap = ALLOC_ACTION(context);
785 /* SELinux headers erroneously declare non-const parameter */
786 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
787 bb_simple_perror_msg(arg1);
791 bb_error_msg("unrecognized: %s", arg);
801 int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
802 int find_main(int argc, char **argv)
804 static const char options[] ALIGN1 =
806 USE_FEATURE_FIND_XDEV( "-xdev\0" )
807 USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
811 USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
812 USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
817 int i, firstopt, status = EXIT_SUCCESS;
818 #if ENABLE_FEATURE_FIND_MAXDEPTH
819 int maxdepth = INT_MAX;
822 for (firstopt = 1; firstopt < argc; firstopt++) {
823 if (argv[firstopt][0] == '-')
825 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
827 #if ENABLE_FEATURE_FIND_PAREN
828 if (LONE_CHAR(argv[firstopt], '('))
833 argv[0] = (char*)".";
838 /* All options always return true. They always take effect
839 * rather than being processed only when their place in the
840 * expression is reached.
841 * We implement: -follow, -xdev, -maxdepth
843 /* Process options, and replace then with -a */
844 /* (-a will be ignored by recursive parser later) */
845 argp = &argv[firstopt];
846 while ((arg = argp[0])) {
847 int opt = index_in_strings(options, arg);
848 if (opt == OPT_FOLLOW) {
849 recurse_flags |= ACTION_FOLLOWLINKS;
850 argp[0] = (char*)"-a";
852 #if ENABLE_FEATURE_FIND_XDEV
853 if (opt == OPT_XDEV) {
856 xdev_count = firstopt - 1;
857 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
858 for (i = 1; i < firstopt; i++) {
859 /* not xstat(): shouldn't bomb out on
860 * "find not_exist exist -xdev" */
861 if (stat(argv[i], &stbuf))
863 xdev_dev[i-1] = stbuf.st_dev;
866 argp[0] = (char*)"-a";
869 #if ENABLE_FEATURE_FIND_MAXDEPTH
870 if (opt == OPT_MAXDEPTH) {
873 maxdepth = xatoi_u(argp[1]);
874 argp[0] = (char*)"-a";
875 argp[1] = (char*)"-a";
882 actions = parse_params(&argv[firstopt]);
884 for (i = 1; i < firstopt; i++) {
885 if (!recursive_action(argv[i],
886 recurse_flags, /* flags */
887 fileAction, /* file action */
888 fileAction, /* dir action */
889 #if ENABLE_FEATURE_FIND_MAXDEPTH
890 /* double cast suppresses
891 * "cast to ptr from int of different size" */
892 (void*)(ptrdiff_t)maxdepth,/* user data */
894 NULL, /* user data */
897 status = EXIT_FAILURE;