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 GPLv2, see file LICENSE in this source tree.
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
56 //applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
58 //kbuild:lib-$(CONFIG_FIND) += find.o
64 //config: find is used to search your system to find specified files.
66 //config:config FEATURE_FIND_PRINT0
67 //config: bool "Enable -print0: NUL-terminated output"
69 //config: depends on FIND
71 //config: Causes output names to be separated by a NUL character
72 //config: rather than a newline. This allows names that contain
73 //config: newlines and other whitespace to be more easily
74 //config: interpreted by other programs.
76 //config:config FEATURE_FIND_MTIME
77 //config: bool "Enable -mtime: modified time matching"
79 //config: depends on FIND
81 //config: Allow searching based on the modification time of
82 //config: files, in days.
84 //config:config FEATURE_FIND_MMIN
85 //config: bool "Enable -mmin: modified time matching by minutes"
87 //config: depends on FIND
89 //config: Allow searching based on the modification time of
90 //config: files, in minutes.
92 //config:config FEATURE_FIND_PERM
93 //config: bool "Enable -perm: permissions matching"
95 //config: depends on FIND
97 //config: Enable searching based on file permissions.
99 //config:config FEATURE_FIND_TYPE
100 //config: bool "Enable -type: file type matching (file/dir/link/...)"
102 //config: depends on FIND
104 //config: Enable searching based on file type (file,
105 //config: directory, socket, device, etc.).
107 //config:config FEATURE_FIND_XDEV
108 //config: bool "Enable -xdev: 'stay in filesystem'"
110 //config: depends on FIND
112 //config: This option allows find to restrict searches to a single filesystem.
114 //config:config FEATURE_FIND_MAXDEPTH
115 //config: bool "Enable -mindepth N and -maxdepth N"
117 //config: depends on FIND
119 //config: This option enables -mindepth N and -maxdepth N option.
121 //config:config FEATURE_FIND_NEWER
122 //config: bool "Enable -newer: compare file modification times"
124 //config: depends on FIND
126 //config: Support the 'find -newer' option for finding any files which have
127 //config: modification time that is more recent than the specified FILE.
129 //config:config FEATURE_FIND_INUM
130 //config: bool "Enable -inum: inode number matching"
132 //config: depends on FIND
134 //config: Support the 'find -inum' option for searching by inode number.
136 //config:config FEATURE_FIND_EXEC
137 //config: bool "Enable -exec: execute commands"
139 //config: depends on FIND
141 //config: Support the 'find -exec' option for executing commands based upon
142 //config: the files matched.
144 //config:config FEATURE_FIND_USER
145 //config: bool "Enable -user: username/uid matching"
147 //config: depends on FIND
149 //config: Support the 'find -user' option for searching by username or uid.
151 //config:config FEATURE_FIND_GROUP
152 //config: bool "Enable -group: group/gid matching"
154 //config: depends on FIND
156 //config: Support the 'find -group' option for searching by group name or gid.
158 //config:config FEATURE_FIND_NOT
159 //config: bool "Enable the 'not' (!) operator"
161 //config: depends on FIND
163 //config: Support the '!' operator to invert the test results.
164 //config: If 'Enable full-blown desktop' is enabled, then will also support
165 //config: the non-POSIX notation '-not'.
167 //config:config FEATURE_FIND_DEPTH
168 //config: bool "Enable -depth"
170 //config: depends on FIND
172 //config: Process each directory's contents before the directory itself.
174 //config:config FEATURE_FIND_PAREN
175 //config: bool "Enable parens in options"
177 //config: depends on FIND
179 //config: Enable usage of parens '(' to specify logical order of arguments.
181 //config:config FEATURE_FIND_SIZE
182 //config: bool "Enable -size: file size matching"
184 //config: depends on FIND
186 //config: Support the 'find -size' option for searching by file size.
188 //config:config FEATURE_FIND_PRUNE
189 //config: bool "Enable -prune: exclude subdirectories"
191 //config: depends on FIND
193 //config: If the file is a directory, dont descend into it. Useful for
194 //config: exclusion .svn and CVS directories.
196 //config:config FEATURE_FIND_DELETE
197 //config: bool "Enable -delete: delete files/dirs"
199 //config: depends on FIND && FEATURE_FIND_DEPTH
201 //config: Support the 'find -delete' option for deleting files and directories.
202 //config: WARNING: This option can do much harm if used wrong. Busybox will not
203 //config: try to protect the user from doing stupid things. Use with care.
205 //config:config FEATURE_FIND_PATH
206 //config: bool "Enable -path: match pathname with shell pattern"
208 //config: depends on FIND
210 //config: The -path option matches whole pathname instead of just filename.
212 //config:config FEATURE_FIND_REGEX
213 //config: bool "Enable -regex: match pathname with regex"
215 //config: depends on FIND
217 //config: The -regex option matches whole pathname against regular expression.
219 //config:config FEATURE_FIND_CONTEXT
220 //config: bool "Enable -context: security context matching"
222 //config: depends on FIND && SELINUX
224 //config: Support the 'find -context' option for matching security context.
226 //config:config FEATURE_FIND_LINKS
227 //config: bool "Enable -links: link count matching"
229 //config: depends on FIND
231 //config: Support the 'find -links' option for matching number of links.
235 #if ENABLE_FEATURE_FIND_REGEX
239 /* This is a NOEXEC applet. Be very careful! */
242 typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
246 #if ENABLE_FEATURE_FIND_NOT
251 #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
253 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
254 const struct stat *statbuf UNUSED_PARAM, \
255 action_##name* ap UNUSED_PARAM)
258 ACTS(name, const char *pattern; bool iname;)
259 IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
260 IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
261 IF_FEATURE_FIND_PRINT0( ACTS(print0))
262 IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
263 IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
264 IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
265 IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
266 IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
267 IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
268 IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
269 IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
270 IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
271 IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
272 IF_FEATURE_FIND_PRUNE( ACTS(prune))
273 IF_FEATURE_FIND_DELETE( ACTS(delete))
274 IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
275 IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
276 IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
279 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
280 IF_FEATURE_FIND_XDEV(int xdev_count;)
283 recurse_flags_t recurse_flags;
285 #define G (*(struct globals*)&bb_common_bufsiz1)
286 #define INIT_G() do { \
287 struct G_sizecheck { \
288 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
290 /* we have to zero it out because of NOEXEC */ \
291 memset(&G, 0, offsetof(struct globals, need_print)); \
293 G.recurse_flags = ACTION_RECURSE; \
296 #if ENABLE_FEATURE_FIND_EXEC
297 static unsigned count_subst(const char *str)
300 while ((str = strstr(str, "{}")) != NULL) {
308 static char* subst(const char *src, unsigned count, const char* filename)
310 char *buf, *dst, *end;
311 size_t flen = strlen(filename);
312 /* we replace each '{}' with filename: growth by strlen-2 */
313 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
314 while ((end = strstr(src, "{}"))) {
315 memcpy(dst, src, end - src);
318 memcpy(dst, filename, flen);
326 /* Return values of ACTFs ('action functions') are a bit mask:
327 * bit 1=1: prune (use SKIP constant for setting it)
328 * bit 0=1: matched successfully (TRUE)
331 static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
338 /* "action group" is a set of actions ANDed together.
339 * groups are ORed together.
340 * We simply evaluate each group until we find one in which all actions
343 /* -prune is special: if it is encountered, then we won't
344 * descend into current directory. It doesn't matter whether
345 * action group (in which -prune sits) will succeed or not:
346 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
347 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
348 * not starting with 'f' */
350 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
351 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
352 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
353 * On return, bit is restored. */
356 while ((app = appp[++cur_group]) != NULL) {
357 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
360 ap = app[++cur_action];
361 if (!ap) /* all actions in group were successful */
362 return rc ^ TRUE; /* restore TRUE bit */
363 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
364 #if ENABLE_FEATURE_FIND_NOT
365 if (ap->invert) rc ^= TRUE;
367 if (rc & TRUE) /* current group failed, try next */
371 return rc ^ TRUE; /* restore TRUE bit */
377 const char *tmp = bb_basename(fileName);
378 if (tmp != fileName && *tmp == '\0') {
379 /* "foo/bar/". Oh no... go back to 'b' */
381 while (tmp != fileName && *--tmp != '/')
386 /* Was using FNM_PERIOD flag too,
387 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
388 * find -name '*foo' should match .foo too:
390 return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0;
393 #if ENABLE_FEATURE_FIND_PATH
396 return fnmatch(ap->pattern, fileName, 0) == 0;
399 #if ENABLE_FEATURE_FIND_REGEX
403 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
404 return 0; /* no match */
406 return 0; /* match doesn't start at pos 0 */
407 if (fileName[match.rm_eo])
408 return 0; /* match doesn't end exactly at end of pathname */
412 #if ENABLE_FEATURE_FIND_TYPE
415 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
418 #if ENABLE_FEATURE_FIND_PERM
421 /* -perm +mode: at least one of perm_mask bits are set */
422 if (ap->perm_char == '+')
423 return (statbuf->st_mode & ap->perm_mask) != 0;
424 /* -perm -mode: all of perm_mask are set */
425 if (ap->perm_char == '-')
426 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
427 /* -perm mode: file mode must match perm_mask */
428 return (statbuf->st_mode & 07777) == ap->perm_mask;
431 #if ENABLE_FEATURE_FIND_MTIME
434 time_t file_age = time(NULL) - statbuf->st_mtime;
435 time_t mtime_secs = ap->mtime_days * 24*60*60;
436 if (ap->mtime_char == '+')
437 return file_age >= mtime_secs + 24*60*60;
438 if (ap->mtime_char == '-')
439 return file_age < mtime_secs;
440 /* just numeric mtime */
441 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
444 #if ENABLE_FEATURE_FIND_MMIN
447 time_t file_age = time(NULL) - statbuf->st_mtime;
448 time_t mmin_secs = ap->mmin_mins * 60;
449 if (ap->mmin_char == '+')
450 return file_age >= mmin_secs + 60;
451 if (ap->mmin_char == '-')
452 return file_age < mmin_secs;
453 /* just numeric mmin */
454 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
457 #if ENABLE_FEATURE_FIND_NEWER
460 return (ap->newer_mtime < statbuf->st_mtime);
463 #if ENABLE_FEATURE_FIND_INUM
466 return (statbuf->st_ino == ap->inode_num);
469 #if ENABLE_FEATURE_FIND_EXEC
473 #if ENABLE_USE_PORTABLE_CODE
474 char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
475 #else /* gcc 4.3.1 generates smaller code: */
476 char *argv[ap->exec_argc + 1];
478 for (i = 0; i < ap->exec_argc; i++)
479 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
480 argv[i] = NULL; /* terminate the list */
482 rc = spawn_and_wait(argv);
484 bb_simple_perror_msg(argv[0]);
489 return rc == 0; /* return 1 if exitcode 0 */
492 #if ENABLE_FEATURE_FIND_USER
495 return (statbuf->st_uid == ap->uid);
498 #if ENABLE_FEATURE_FIND_GROUP
501 return (statbuf->st_gid == ap->gid);
504 #if ENABLE_FEATURE_FIND_PRINT0
507 printf("%s%c", fileName, '\0');
516 #if ENABLE_FEATURE_FIND_PAREN
519 return exec_actions(ap->subexpr, fileName, statbuf);
522 #if ENABLE_FEATURE_FIND_SIZE
525 if (ap->size_char == '+')
526 return statbuf->st_size > ap->size;
527 if (ap->size_char == '-')
528 return statbuf->st_size < ap->size;
529 return statbuf->st_size == ap->size;
532 #if ENABLE_FEATURE_FIND_PRUNE
534 * -prune: if -depth is not given, return true and do not descend
535 * current dir; if -depth is given, return false with no effect.
537 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
544 #if ENABLE_FEATURE_FIND_DELETE
548 if (S_ISDIR(statbuf->st_mode)) {
549 rc = rmdir(fileName);
551 rc = unlink(fileName);
554 bb_simple_perror_msg(fileName);
558 #if ENABLE_FEATURE_FIND_CONTEXT
561 security_context_t con;
564 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
565 rc = getfilecon(fileName, &con);
567 rc = lgetfilecon(fileName, &con);
571 rc = strcmp(ap->context, con);
576 #if ENABLE_FEATURE_FIND_LINKS
579 switch(ap->links_char) {
580 case '-' : return (statbuf->st_nlink < ap->links_count);
581 case '+' : return (statbuf->st_nlink > ap->links_count);
582 default: return (statbuf->st_nlink == ap->links_count);
587 static int FAST_FUNC fileAction(const char *fileName,
588 struct stat *statbuf,
589 void *userData IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
590 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
593 #if ENABLE_FEATURE_FIND_MAXDEPTH
594 #define minmaxdepth ((int*)userData)
596 if (depth < minmaxdepth[0])
597 return TRUE; /* skip this, continue recursing */
598 if (depth > minmaxdepth[1])
599 return SKIP; /* stop recursing */
602 r = exec_actions(G.actions, fileName, statbuf);
603 /* Had no explicit -print[0] or -exec? then print */
604 if ((r & TRUE) && G.need_print)
607 #if ENABLE_FEATURE_FIND_MAXDEPTH
608 if (S_ISDIR(statbuf->st_mode)) {
609 if (depth == minmaxdepth[1])
613 #if ENABLE_FEATURE_FIND_XDEV
614 /* -xdev stops on mountpoints, but AFTER mountpoit itself
615 * is processed as usual */
616 if (S_ISDIR(statbuf->st_mode)) {
619 for (i = 0; i < G.xdev_count; i++) {
620 if (G.xdev_dev[i] == statbuf->st_dev)
629 /* Cannot return 0: our caller, recursive_action(),
630 * will perror() and skip dirs (if called on dir) */
631 return (r & SKIP) ? SKIP : TRUE;
636 #if ENABLE_FEATURE_FIND_TYPE
637 static int find_type(const char *type)
643 else if (*type == 'c')
645 else if (*type == 'd')
647 else if (*type == 'p')
649 else if (*type == 'f')
651 else if (*type == 'l')
653 else if (*type == 's')
656 if (mask == 0 || type[1] != '\0')
657 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
663 #if ENABLE_FEATURE_FIND_PERM \
664 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
665 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
666 static const char* plus_minus_num(const char* str)
668 if (*str == '-' || *str == '+')
674 static action*** parse_params(char **argv)
679 IF_FEATURE_FIND_NOT( PARM_char_not ,)
683 IF_FEATURE_FIND_NOT( PARM_not ,)
686 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
687 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
688 IF_FEATURE_FIND_DELETE( PARM_delete ,)
689 IF_FEATURE_FIND_EXEC( PARM_exec ,)
690 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
691 /* All options starting from here require argument */
694 IF_FEATURE_FIND_PATH( PARM_path ,)
695 IF_FEATURE_FIND_REGEX( PARM_regex ,)
696 IF_FEATURE_FIND_TYPE( PARM_type ,)
697 IF_FEATURE_FIND_PERM( PARM_perm ,)
698 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
699 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
700 IF_FEATURE_FIND_NEWER( PARM_newer ,)
701 IF_FEATURE_FIND_INUM( PARM_inum ,)
702 IF_FEATURE_FIND_USER( PARM_user ,)
703 IF_FEATURE_FIND_GROUP( PARM_group ,)
704 IF_FEATURE_FIND_SIZE( PARM_size ,)
705 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
706 IF_FEATURE_FIND_LINKS( PARM_links ,)
709 static const char params[] ALIGN1 =
712 IF_FEATURE_FIND_NOT( "!\0" )
716 IF_FEATURE_FIND_NOT( "-not\0" )
719 IF_FEATURE_FIND_PRINT0( "-print0\0" )
720 IF_FEATURE_FIND_PRUNE( "-prune\0" )
721 IF_FEATURE_FIND_DELETE( "-delete\0" )
722 IF_FEATURE_FIND_EXEC( "-exec\0" )
723 IF_FEATURE_FIND_PAREN( "(\0" )
724 /* All options starting from here require argument */
727 IF_FEATURE_FIND_PATH( "-path\0" )
728 IF_FEATURE_FIND_REGEX( "-regex\0" )
729 IF_FEATURE_FIND_TYPE( "-type\0" )
730 IF_FEATURE_FIND_PERM( "-perm\0" )
731 IF_FEATURE_FIND_MTIME( "-mtime\0" )
732 IF_FEATURE_FIND_MMIN( "-mmin\0" )
733 IF_FEATURE_FIND_NEWER( "-newer\0" )
734 IF_FEATURE_FIND_INUM( "-inum\0" )
735 IF_FEATURE_FIND_USER( "-user\0" )
736 IF_FEATURE_FIND_GROUP( "-group\0" )
737 IF_FEATURE_FIND_SIZE( "-size\0" )
738 IF_FEATURE_FIND_CONTEXT("-context\0")
739 IF_FEATURE_FIND_LINKS( "-links\0" )
743 unsigned cur_group = 0;
744 unsigned cur_action = 0;
745 IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
747 /* This is the only place in busybox where we use nested function.
748 * So far more standard alternatives were bigger. */
749 /* Auto decl suppresses "func without a prototype" warning: */
750 auto action* alloc_action(int sizeof_struct, action_fp f);
751 action* alloc_action(int sizeof_struct, action_fp f)
754 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
755 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
756 appp[cur_group][cur_action] = NULL;
758 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
759 IF_FEATURE_FIND_NOT( invert_flag = 0; )
763 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
765 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
767 /* Actions have side effects and return a true or false value
768 * We implement: -print, -print0, -exec
770 * The rest are tests.
772 * Tests and actions are grouped by operators
773 * ( expr ) Force precedence
774 * ! expr True if expr is false
775 * -not expr Same as ! expr
776 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
777 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
778 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
779 * We implement: (), -a, -o
782 const char *arg = argv[0];
783 int parm = index_in_strings(params, arg);
784 const char *arg1 = argv[1];
786 if (parm >= PARM_name) {
787 /* All options starting from -name require argument */
789 bb_error_msg_and_die(bb_msg_requires_arg, arg);
793 /* We can use big switch() here, but on i386
794 * it doesn't give smaller code. Other arches? */
796 /* --- Operators --- */
797 if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
798 /* no further special handling required */
800 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
801 /* start new OR group */
803 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
804 /*appp[cur_group] = NULL; - already NULL */
805 appp[cur_group+1] = NULL;
808 #if ENABLE_FEATURE_FIND_NOT
809 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
810 /* also handles "find ! ! -name 'foo*'" */
815 /* --- Tests and actions --- */
816 else if (parm == PARM_print) {
818 /* GNU find ignores '!' here: "find ! -print" */
819 IF_FEATURE_FIND_NOT( invert_flag = 0; )
820 (void) ALLOC_ACTION(print);
822 #if ENABLE_FEATURE_FIND_PRINT0
823 else if (parm == PARM_print0) {
825 IF_FEATURE_FIND_NOT( invert_flag = 0; )
826 (void) ALLOC_ACTION(print0);
829 #if ENABLE_FEATURE_FIND_PRUNE
830 else if (parm == PARM_prune) {
831 IF_FEATURE_FIND_NOT( invert_flag = 0; )
832 (void) ALLOC_ACTION(prune);
835 #if ENABLE_FEATURE_FIND_DELETE
836 else if (parm == PARM_delete) {
838 G.recurse_flags |= ACTION_DEPTHFIRST;
839 (void) ALLOC_ACTION(delete);
842 #if ENABLE_FEATURE_FIND_EXEC
843 else if (parm == PARM_exec) {
847 IF_FEATURE_FIND_NOT( invert_flag = 0; )
848 ap = ALLOC_ACTION(exec);
849 ap->exec_argv = ++argv; /* first arg after -exec */
850 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
852 if (!*argv) /* did not see ';' or '+' until end */
853 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
854 // find -exec echo Foo ">{}<" ";"
855 // executes "echo Foo >FILENAME<",
856 // find -exec echo Foo ">{}<" "+"
857 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
858 // TODO (so far we treat "+" just like ";")
859 if ((argv[0][0] == ';' || argv[0][0] == '+')
860 && argv[0][1] == '\0'
867 if (ap->exec_argc == 0)
868 bb_error_msg_and_die(bb_msg_requires_arg, arg);
869 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
872 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
875 #if ENABLE_FEATURE_FIND_PAREN
876 else if (parm == PARM_char_brace) {
884 bb_error_msg_and_die("unpaired '('");
885 if (LONE_CHAR(*endarg, '('))
887 else if (LONE_CHAR(*endarg, ')') && !--nested) {
892 ap = ALLOC_ACTION(paren);
893 ap->subexpr = parse_params(argv + 1);
894 *endarg = (char*) ")"; /* restore NULLed parameter */
898 else if (parm == PARM_name || parm == PARM_iname) {
900 ap = ALLOC_ACTION(name);
902 ap->iname = (parm == PARM_iname);
904 #if ENABLE_FEATURE_FIND_PATH
905 else if (parm == PARM_path) {
907 ap = ALLOC_ACTION(path);
911 #if ENABLE_FEATURE_FIND_REGEX
912 else if (parm == PARM_regex) {
914 ap = ALLOC_ACTION(regex);
915 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
918 #if ENABLE_FEATURE_FIND_TYPE
919 else if (parm == PARM_type) {
921 ap = ALLOC_ACTION(type);
922 ap->type_mask = find_type(arg1);
925 #if ENABLE_FEATURE_FIND_PERM
926 /* -perm mode File's permission bits are exactly mode (octal or symbolic).
927 * Symbolic modes use mode 0 as a point of departure.
928 * -perm -mode All of the permission bits mode are set for the file.
929 * -perm +mode Any of the permission bits mode are set for the file.
931 else if (parm == PARM_perm) {
933 ap = ALLOC_ACTION(perm);
934 ap->perm_char = arg1[0];
935 arg1 = plus_minus_num(arg1);
936 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
937 if (!bb_parse_mode(arg1, &ap->perm_mask))
938 bb_error_msg_and_die("invalid mode '%s'", arg1);
941 #if ENABLE_FEATURE_FIND_MTIME
942 else if (parm == PARM_mtime) {
944 ap = ALLOC_ACTION(mtime);
945 ap->mtime_char = arg1[0];
946 ap->mtime_days = xatoul(plus_minus_num(arg1));
949 #if ENABLE_FEATURE_FIND_MMIN
950 else if (parm == PARM_mmin) {
952 ap = ALLOC_ACTION(mmin);
953 ap->mmin_char = arg1[0];
954 ap->mmin_mins = xatoul(plus_minus_num(arg1));
957 #if ENABLE_FEATURE_FIND_NEWER
958 else if (parm == PARM_newer) {
959 struct stat stat_newer;
961 ap = ALLOC_ACTION(newer);
962 xstat(arg1, &stat_newer);
963 ap->newer_mtime = stat_newer.st_mtime;
966 #if ENABLE_FEATURE_FIND_INUM
967 else if (parm == PARM_inum) {
969 ap = ALLOC_ACTION(inum);
970 ap->inode_num = xatoul(arg1);
973 #if ENABLE_FEATURE_FIND_USER
974 else if (parm == PARM_user) {
976 ap = ALLOC_ACTION(user);
977 ap->uid = bb_strtou(arg1, NULL, 10);
979 ap->uid = xuname2uid(arg1);
982 #if ENABLE_FEATURE_FIND_GROUP
983 else if (parm == PARM_group) {
985 ap = ALLOC_ACTION(group);
986 ap->gid = bb_strtou(arg1, NULL, 10);
988 ap->gid = xgroup2gid(arg1);
991 #if ENABLE_FEATURE_FIND_SIZE
992 else if (parm == PARM_size) {
993 /* -size n[bckw]: file uses n units of space
994 * b (default): units are 512-byte blocks
1000 #define XATOU_SFX xatoull_sfx
1002 #define XATOU_SFX xatoul_sfx
1004 static const struct suffix_mult find_suffixes[] = {
1013 ap = ALLOC_ACTION(size);
1014 ap->size_char = arg1[0];
1015 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
1018 #if ENABLE_FEATURE_FIND_CONTEXT
1019 else if (parm == PARM_context) {
1021 ap = ALLOC_ACTION(context);
1022 /*ap->context = NULL; - ALLOC_ACTION did it */
1023 /* SELinux headers erroneously declare non-const parameter */
1024 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
1025 bb_simple_perror_msg(arg1);
1028 #if ENABLE_FEATURE_FIND_LINKS
1029 else if (parm == PARM_links) {
1031 ap = ALLOC_ACTION(links);
1032 ap->links_char = arg1[0];
1033 ap->links_count = xatoul(plus_minus_num(arg1));
1037 bb_error_msg("unrecognized: %s", arg);
1046 //usage:#define find_trivial_usage
1047 //usage: "[OPTIONS] [PATH]... [ACTIONS]"
1048 //usage:#define find_full_usage "\n\n"
1049 //usage: "Search for files and perform actions on them.\n"
1050 //usage: "First failed action stops processing of current file.\n"
1051 //usage: "Defaults: PATH is current directory, action is '-print'\n"
1052 //usage: "\nOptions:"
1053 //usage: "\n -follow Follow symlinks"
1054 //usage: IF_FEATURE_FIND_XDEV(
1055 //usage: "\n -xdev Don't descend directories on other filesystems"
1057 //usage: IF_FEATURE_FIND_MAXDEPTH(
1058 //usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
1059 //usage: "\n actions to command line arguments only"
1060 //usage: "\n -mindepth N Don't act on first N levels"
1062 //usage: IF_FEATURE_FIND_DEPTH(
1063 //usage: "\n -depth Act on directory *after* traversing it"
1066 //usage: "\nActions:"
1067 //usage: IF_FEATURE_FIND_PAREN(
1068 //usage: "\n ( ACTIONS ) Group actions for -o / -a"
1070 //usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
1071 //usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
1072 //usage: "\n Note: -a has higher priority than -o"
1073 //usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
1074 //usage: "\n -iname PATTERN Case insensitive -name"
1075 //usage: IF_FEATURE_FIND_PATH(
1076 //usage: "\n -path PATTERN Match path to PATTERN"
1078 //usage: IF_FEATURE_FIND_REGEX(
1079 //usage: "\n -regex PATTERN Match path to regex PATTERN"
1081 //usage: IF_FEATURE_FIND_TYPE(
1082 //usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
1084 //usage: IF_FEATURE_FIND_PERM(
1085 //usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
1086 //usage: "\n or exactly MASK bits are set in mode bits"
1088 //usage: IF_FEATURE_FIND_MTIME(
1089 //usage: "\n -mtime DAYS Modified time is greater than (+N), less than (-N),"
1090 //usage: "\n or exactly N days"
1092 //usage: IF_FEATURE_FIND_MMIN(
1093 //usage: "\n -mmin MINS Modified time is greater than (+N), less than (-N),"
1094 //usage: "\n or exactly N minutes"
1096 //usage: IF_FEATURE_FIND_NEWER(
1097 //usage: "\n -newer FILE Modified time is more recent than FILE's"
1099 //usage: IF_FEATURE_FIND_INUM(
1100 //usage: "\n -inum N File has inode number N"
1102 //usage: IF_FEATURE_FIND_USER(
1103 //usage: "\n -user NAME File is owned by user NAME (numeric user ID allowed)"
1105 //usage: IF_FEATURE_FIND_GROUP(
1106 //usage: "\n -group NAME File belongs to group NAME (numeric group ID allowed)"
1108 //usage: IF_FEATURE_FIND_SIZE(
1109 //usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
1110 //usage: "\n +/-N: file size is bigger/smaller than N"
1112 //usage: IF_FEATURE_FIND_LINKS(
1113 //usage: "\n -links N Number of links is greater than (+N), less than (-N),"
1114 //usage: "\n or exactly N"
1116 //usage: IF_FEATURE_FIND_CONTEXT(
1117 //usage: "\n -context CTX File has specified security context"
1119 //usage: IF_FEATURE_FIND_PRUNE(
1120 //usage: "\n -prune If current file is directory, don't descend into it"
1122 //usage: "\nIf none of the following actions is specified, -print is assumed"
1123 //usage: "\n -print Print file name"
1124 //usage: IF_FEATURE_FIND_PRINT0(
1125 //usage: "\n -print0 Print file name, NUL terminated"
1127 //usage: IF_FEATURE_FIND_EXEC(
1128 //usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
1129 //usage: "\n file name. Fails if CMD exits with nonzero"
1131 //usage: IF_FEATURE_FIND_DELETE(
1132 //usage: "\n -delete Delete current file/directory. Turns on -depth option"
1135 //usage:#define find_example_usage
1136 //usage: "$ find / -name passwd\n"
1137 //usage: "/etc/passwd\n"
1139 int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1140 int find_main(int argc UNUSED_PARAM, char **argv)
1142 static const char options[] ALIGN1 =
1144 IF_FEATURE_FIND_XDEV( "-xdev\0" )
1145 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
1146 IF_FEATURE_FIND_DEPTH( "-depth\0" )
1150 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
1151 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,)
1152 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
1157 int i, firstopt, status = EXIT_SUCCESS;
1158 #if ENABLE_FEATURE_FIND_MAXDEPTH
1159 int minmaxdepth[2] = { 0, INT_MAX };
1161 #define minmaxdepth NULL
1166 for (firstopt = 1; argv[firstopt]; firstopt++) {
1167 if (argv[firstopt][0] == '-')
1169 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1171 #if ENABLE_FEATURE_FIND_PAREN
1172 if (LONE_CHAR(argv[firstopt], '('))
1176 if (firstopt == 1) {
1177 argv[0] = (char*)".";
1182 /* All options always return true. They always take effect
1183 * rather than being processed only when their place in the
1184 * expression is reached.
1185 * We implement: -follow, -xdev, -mindepth, -maxdepth, -depth
1187 /* Process options, and replace then with -a */
1188 /* (-a will be ignored by recursive parser later) */
1189 argp = &argv[firstopt];
1190 while ((arg = argp[0]) != NULL) {
1191 int opt = index_in_strings(options, arg);
1192 if (opt == OPT_FOLLOW) {
1193 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1194 argp[0] = (char*)"-a";
1196 #if ENABLE_FEATURE_FIND_XDEV
1197 if (opt == OPT_XDEV) {
1199 if (!G.xdev_count) {
1200 G.xdev_count = firstopt - 1;
1201 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1202 for (i = 1; i < firstopt; i++) {
1203 /* not xstat(): shouldn't bomb out on
1204 * "find not_exist exist -xdev" */
1205 if (stat(argv[i], &stbuf) == 0)
1206 G.xdev_dev[i-1] = stbuf.st_dev;
1207 /* else G.xdev_dev[i-1] stays 0 and
1208 * won't match any real device dev_t */
1211 argp[0] = (char*)"-a";
1214 #if ENABLE_FEATURE_FIND_MAXDEPTH
1215 if (opt == OPT_MINDEPTH || opt == OPT_MINDEPTH + 1) {
1218 minmaxdepth[opt - OPT_MINDEPTH] = xatoi_positive(argp[1]);
1219 argp[0] = (char*)"-a";
1221 argp[0] = (char*)"-a";
1224 #if ENABLE_FEATURE_FIND_DEPTH
1225 if (opt == OPT_DEPTH) {
1226 G.recurse_flags |= ACTION_DEPTHFIRST;
1227 argp[0] = (char*)"-a";
1233 G.actions = parse_params(&argv[firstopt]);
1235 for (i = 1; i < firstopt; i++) {
1236 if (!recursive_action(argv[i],
1237 G.recurse_flags,/* flags */
1238 fileAction, /* file action */
1239 fileAction, /* dir action */
1240 #if ENABLE_FEATURE_FIND_MAXDEPTH
1241 minmaxdepth, /* user data */
1243 NULL, /* user data */
1246 status = EXIT_FAILURE;