9ae84fa0d1148b7ba1c00cf5ad22698929cb8605
[platform/upstream/busybox.git] / findutils / find.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini find implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Reworked by David Douthitt <n9ubh@callsign.net> and
8  *  Matt Kraai <kraai@alumni.carnegiemellon.edu>.
9  *
10  * Licensed under GPLv2, see file LICENSE in this source tree.
11  */
12
13 /* findutils-4.1.20:
14  *
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' '{}  {}' ';'
24  * file.txt  file.txt
25  * (strace: execve("/bin/echo", ["echo", "file.txt  file.txt"], [ 30 vars ]))
26  * # find file.txt -exec 'echo' '{}  {}' ';' -print -exec pwd ';'
27  * file.txt  file.txt
28  * file.txt
29  * /tmp
30  * # find -name '*.c' -o -name '*.h'
31  * [shows files, *.c and *.h intermixed]
32  * # find file.txt -name '*f*' -o -name '*t*'
33  * file.txt
34  * # find file.txt -name '*z*' -o -name '*t*'
35  * file.txt
36  * # find file.txt -name '*f*' -o -name '*z*'
37  * file.txt
38  *
39  * # find t z -name '*t*' -print -o -name '*z*'
40  * t
41  * # find t z t z -name '*t*' -o -name '*z*' -print
42  * z
43  * z
44  * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
45  * (no output)
46  */
47
48 /* Testing script
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
54  */
55
56 //config:config FIND
57 //config:       bool "find"
58 //config:       default y
59 //config:       help
60 //config:         find is used to search your system to find specified files.
61 //config:
62 //config:config FEATURE_FIND_PRINT0
63 //config:       bool "Enable -print0: NUL-terminated output"
64 //config:       default y
65 //config:       depends on FIND
66 //config:       help
67 //config:         Causes output names to be separated by a NUL character
68 //config:         rather than a newline. This allows names that contain
69 //config:         newlines and other whitespace to be more easily
70 //config:         interpreted by other programs.
71 //config:
72 //config:config FEATURE_FIND_MTIME
73 //config:       bool "Enable -mtime: modified time matching"
74 //config:       default y
75 //config:       depends on FIND
76 //config:       help
77 //config:         Allow searching based on the modification time of
78 //config:         files, in days.
79 //config:
80 //config:config FEATURE_FIND_MMIN
81 //config:       bool "Enable -mmin: modified time matching by minutes"
82 //config:       default y
83 //config:       depends on FIND
84 //config:       help
85 //config:         Allow searching based on the modification time of
86 //config:         files, in minutes.
87 //config:
88 //config:config FEATURE_FIND_PERM
89 //config:       bool "Enable -perm: permissions matching"
90 //config:       default y
91 //config:       depends on FIND
92 //config:       help
93 //config:         Enable searching based on file permissions.
94 //config:
95 //config:config FEATURE_FIND_TYPE
96 //config:       bool "Enable -type: file type matching (file/dir/link/...)"
97 //config:       default y
98 //config:       depends on FIND
99 //config:       help
100 //config:         Enable searching based on file type (file,
101 //config:         directory, socket, device, etc.).
102 //config:
103 //config:config FEATURE_FIND_XDEV
104 //config:       bool "Enable -xdev: 'stay in filesystem'"
105 //config:       default y
106 //config:       depends on FIND
107 //config:       help
108 //config:         This option allows find to restrict searches to a single filesystem.
109 //config:
110 //config:config FEATURE_FIND_MAXDEPTH
111 //config:       bool "Enable -mindepth N and -maxdepth N"
112 //config:       default y
113 //config:       depends on FIND
114 //config:       help
115 //config:         This option enables -mindepth N and -maxdepth N option.
116 //config:
117 //config:config FEATURE_FIND_NEWER
118 //config:       bool "Enable -newer: compare file modification times"
119 //config:       default y
120 //config:       depends on FIND
121 //config:       help
122 //config:         Support the 'find -newer' option for finding any files which have
123 //config:         modification time that is more recent than the specified FILE.
124 //config:
125 //config:config FEATURE_FIND_INUM
126 //config:       bool "Enable -inum: inode number matching"
127 //config:       default y
128 //config:       depends on FIND
129 //config:       help
130 //config:         Support the 'find -inum' option for searching by inode number.
131 //config:
132 //config:config FEATURE_FIND_EXEC
133 //config:       bool "Enable -exec: execute commands"
134 //config:       default y
135 //config:       depends on FIND
136 //config:       help
137 //config:         Support the 'find -exec' option for executing commands based upon
138 //config:         the files matched.
139 //config:
140 //config:config FEATURE_FIND_USER
141 //config:       bool "Enable -user: username/uid matching"
142 //config:       default y
143 //config:       depends on FIND
144 //config:       help
145 //config:         Support the 'find -user' option for searching by username or uid.
146 //config:
147 //config:config FEATURE_FIND_GROUP
148 //config:       bool "Enable -group: group/gid matching"
149 //config:       default y
150 //config:       depends on FIND
151 //config:       help
152 //config:         Support the 'find -group' option for searching by group name or gid.
153 //config:
154 //config:config FEATURE_FIND_NOT
155 //config:       bool "Enable the 'not' (!) operator"
156 //config:       default y
157 //config:       depends on FIND
158 //config:       help
159 //config:         Support the '!' operator to invert the test results.
160 //config:         If 'Enable full-blown desktop' is enabled, then will also support
161 //config:         the non-POSIX notation '-not'.
162 //config:
163 //config:config FEATURE_FIND_DEPTH
164 //config:       bool "Enable -depth"
165 //config:       default y
166 //config:       depends on FIND
167 //config:       help
168 //config:         Process each directory's contents before the directory itself.
169 //config:
170 //config:config FEATURE_FIND_PAREN
171 //config:       bool "Enable parens in options"
172 //config:       default y
173 //config:       depends on FIND
174 //config:       help
175 //config:         Enable usage of parens '(' to specify logical order of arguments.
176 //config:
177 //config:config FEATURE_FIND_SIZE
178 //config:       bool "Enable -size: file size matching"
179 //config:       default y
180 //config:       depends on FIND
181 //config:       help
182 //config:         Support the 'find -size' option for searching by file size.
183 //config:
184 //config:config FEATURE_FIND_PRUNE
185 //config:       bool "Enable -prune: exclude subdirectories"
186 //config:       default y
187 //config:       depends on FIND
188 //config:       help
189 //config:         If the file is a directory, dont descend into it. Useful for
190 //config:         exclusion .svn and CVS directories.
191 //config:
192 //config:config FEATURE_FIND_DELETE
193 //config:       bool "Enable -delete: delete files/dirs"
194 //config:       default y
195 //config:       depends on FIND && FEATURE_FIND_DEPTH
196 //config:       help
197 //config:         Support the 'find -delete' option for deleting files and directories.
198 //config:         WARNING: This option can do much harm if used wrong. Busybox will not
199 //config:         try to protect the user from doing stupid things. Use with care.
200 //config:
201 //config:config FEATURE_FIND_PATH
202 //config:       bool "Enable -path: match pathname with shell pattern"
203 //config:       default y
204 //config:       depends on FIND
205 //config:       help
206 //config:         The -path option matches whole pathname instead of just filename.
207 //config:
208 //config:config FEATURE_FIND_REGEX
209 //config:       bool "Enable -regex: match pathname with regex"
210 //config:       default y
211 //config:       depends on FIND
212 //config:       help
213 //config:         The -regex option matches whole pathname against regular expression.
214 //config:
215 //config:config FEATURE_FIND_CONTEXT
216 //config:       bool "Enable -context: security context matching"
217 //config:       default n
218 //config:       depends on FIND && SELINUX
219 //config:       help
220 //config:         Support the 'find -context' option for matching security context.
221 //config:
222 //config:config FEATURE_FIND_LINKS
223 //config:       bool "Enable -links: link count matching"
224 //config:       default y
225 //config:       depends on FIND
226 //config:       help
227 //config:         Support the 'find -links' option for matching number of links.
228
229 //applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
230
231 //kbuild:lib-$(CONFIG_FIND) += find.o
232
233 //usage:#define find_trivial_usage
234 //usage:       "[PATH]... [OPTIONS] [ACTIONS]"
235 //usage:#define find_full_usage "\n\n"
236 //usage:       "Search for files and perform actions on them.\n"
237 //usage:       "First failed action stops processing of current file.\n"
238 //usage:       "Defaults: PATH is current directory, action is '-print'\n"
239 //usage:     "\nOptions:"
240 //usage:     "\n        -follow         Follow symlinks"
241 //usage:        IF_FEATURE_FIND_XDEV(
242 //usage:     "\n        -xdev           Don't descend directories on other filesystems"
243 //usage:        )
244 //usage:        IF_FEATURE_FIND_MAXDEPTH(
245 //usage:     "\n        -maxdepth N     Descend at most N levels. -maxdepth 0 applies"
246 //usage:     "\n                        actions to command line arguments only"
247 //usage:     "\n        -mindepth N     Don't act on first N levels"
248 //usage:        )
249 //usage:        IF_FEATURE_FIND_DEPTH(
250 //usage:     "\n        -depth          Act on directory *after* traversing it"
251 //usage:        )
252 //usage:     "\n"
253 //usage:     "\nActions:"
254 //usage:        IF_FEATURE_FIND_PAREN(
255 //usage:     "\n        ( ACTIONS )     Group actions for -o / -a"
256 //usage:        )
257 //usage:        IF_FEATURE_FIND_NOT(
258 //usage:     "\n        ! ACT           Invert ACT's success/failure"
259 //usage:        )
260 //usage:     "\n        ACT1 [-a] ACT2  If ACT1 fails, stop, else do ACT2"
261 //usage:     "\n        ACT1 -o ACT2    If ACT1 succeeds, stop, else do ACT2"
262 //usage:     "\n                        Note: -a has higher priority than -o"
263 //usage:     "\n        -name PATTERN   Match file name (w/o directory name) to PATTERN"
264 //usage:     "\n        -iname PATTERN  Case insensitive -name"
265 //usage:        IF_FEATURE_FIND_PATH(
266 //usage:     "\n        -path PATTERN   Match path to PATTERN"
267 //usage:        )
268 //usage:        IF_FEATURE_FIND_REGEX(
269 //usage:     "\n        -regex PATTERN  Match path to regex PATTERN"
270 //usage:        )
271 //usage:        IF_FEATURE_FIND_TYPE(
272 //usage:     "\n        -type X         File type is X (one of: f,d,l,b,c,...)"
273 //usage:        )
274 //usage:        IF_FEATURE_FIND_PERM(
275 //usage:     "\n        -perm MASK      At least one mask bit (+MASK), all bits (-MASK),"
276 //usage:     "\n                        or exactly MASK bits are set in file's mode"
277 //usage:        )
278 //usage:        IF_FEATURE_FIND_MTIME(
279 //usage:     "\n        -mtime DAYS     mtime is greater than (+N), less than (-N),"
280 //usage:     "\n                        or exactly N days in the past"
281 //usage:        )
282 //usage:        IF_FEATURE_FIND_MMIN(
283 //usage:     "\n        -mmin MINS      mtime is greater than (+N), less than (-N),"
284 //usage:     "\n                        or exactly N minutes in the past"
285 //usage:        )
286 //usage:        IF_FEATURE_FIND_NEWER(
287 //usage:     "\n        -newer FILE     mtime is more recent than FILE's"
288 //usage:        )
289 //usage:        IF_FEATURE_FIND_INUM(
290 //usage:     "\n        -inum N         File has inode number N"
291 //usage:        )
292 //usage:        IF_FEATURE_FIND_USER(
293 //usage:     "\n        -user NAME/ID   File is owned by given user"
294 //usage:        )
295 //usage:        IF_FEATURE_FIND_GROUP(
296 //usage:     "\n        -group NAME/ID  File is owned by given group"
297 //usage:        )
298 //usage:        IF_FEATURE_FIND_SIZE(
299 //usage:     "\n        -size N[bck]    File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
300 //usage:     "\n                        +/-N: file size is bigger/smaller than N"
301 //usage:        )
302 //usage:        IF_FEATURE_FIND_LINKS(
303 //usage:     "\n        -links N        Number of links is greater than (+N), less than (-N),"
304 //usage:     "\n                        or exactly N"
305 //usage:        )
306 //usage:        IF_FEATURE_FIND_CONTEXT(
307 //usage:     "\n        -context CTX    File has specified security context"
308 //usage:        )
309 //usage:        IF_FEATURE_FIND_PRUNE(
310 //usage:     "\n        -prune          If current file is directory, don't descend into it"
311 //usage:        )
312 //usage:     "\nIf none of the following actions is specified, -print is assumed"
313 //usage:     "\n        -print          Print file name"
314 //usage:        IF_FEATURE_FIND_PRINT0(
315 //usage:     "\n        -print0         Print file name, NUL terminated"
316 //usage:        )
317 //usage:        IF_FEATURE_FIND_EXEC(
318 //usage:     "\n        -exec CMD ARG ; Run CMD with all instances of {} replaced by"
319 //usage:     "\n                        file name. Fails if CMD exits with nonzero"
320 //usage:        )
321 //usage:        IF_FEATURE_FIND_DELETE(
322 //usage:     "\n        -delete         Delete current file/directory. Turns on -depth option"
323 //usage:        )
324 //usage:
325 //usage:#define find_example_usage
326 //usage:       "$ find / -name passwd\n"
327 //usage:       "/etc/passwd\n"
328
329 #include <fnmatch.h>
330 #include "libbb.h"
331 #if ENABLE_FEATURE_FIND_REGEX
332 #include "xregex.h"
333 #endif
334
335 /* This is a NOEXEC applet. Be very careful! */
336
337
338 typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
339
340 typedef struct {
341         action_fp f;
342 #if ENABLE_FEATURE_FIND_NOT
343         bool invert;
344 #endif
345 } action;
346
347 #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
348 #define ACTF(name) \
349         static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
350                 const struct stat *statbuf UNUSED_PARAM, \
351                 action_##name* ap UNUSED_PARAM)
352
353                         ACTS(print)
354                         ACTS(name,  const char *pattern; bool iname;)
355 IF_FEATURE_FIND_PATH(   ACTS(path,  const char *pattern;))
356 IF_FEATURE_FIND_REGEX(  ACTS(regex, regex_t compiled_pattern;))
357 IF_FEATURE_FIND_PRINT0( ACTS(print0))
358 IF_FEATURE_FIND_TYPE(   ACTS(type,  int type_mask;))
359 IF_FEATURE_FIND_PERM(   ACTS(perm,  char perm_char; mode_t perm_mask;))
360 IF_FEATURE_FIND_MTIME(  ACTS(mtime, char mtime_char; unsigned mtime_days;))
361 IF_FEATURE_FIND_MMIN(   ACTS(mmin,  char mmin_char; unsigned mmin_mins;))
362 IF_FEATURE_FIND_NEWER(  ACTS(newer, time_t newer_mtime;))
363 IF_FEATURE_FIND_INUM(   ACTS(inum,  ino_t inode_num;))
364 IF_FEATURE_FIND_USER(   ACTS(user,  uid_t uid;))
365 IF_FEATURE_FIND_SIZE(   ACTS(size,  char size_char; off_t size;))
366 IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
367 IF_FEATURE_FIND_PAREN(  ACTS(paren, action ***subexpr;))
368 IF_FEATURE_FIND_PRUNE(  ACTS(prune))
369 IF_FEATURE_FIND_DELETE( ACTS(delete))
370 IF_FEATURE_FIND_EXEC(   ACTS(exec,  char **exec_argv; unsigned *subst_count; int exec_argc;))
371 IF_FEATURE_FIND_GROUP(  ACTS(group, gid_t gid;))
372 IF_FEATURE_FIND_LINKS(  ACTS(links, char links_char; int links_count;))
373
374 struct globals {
375         IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
376         IF_FEATURE_FIND_XDEV(int xdev_count;)
377 #if ENABLE_FEATURE_FIND_MAXDEPTH
378         int minmaxdepth[2];
379 #endif
380         action ***actions;
381         smallint need_print;
382         smallint xdev_on;
383         recurse_flags_t recurse_flags;
384 } FIX_ALIASING;
385 #define G (*(struct globals*)&bb_common_bufsiz1)
386 #define INIT_G() do { \
387         struct G_sizecheck { \
388                 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
389         }; \
390         /* we have to zero it out because of NOEXEC */ \
391         memset(&G, 0, sizeof(G)); \
392         IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
393         G.need_print = 1; \
394         G.recurse_flags = ACTION_RECURSE; \
395 } while (0)
396
397 #if ENABLE_FEATURE_FIND_EXEC
398 static unsigned count_subst(const char *str)
399 {
400         unsigned count = 0;
401         while ((str = strstr(str, "{}")) != NULL) {
402                 count++;
403                 str++;
404         }
405         return count;
406 }
407
408
409 static char* subst(const char *src, unsigned count, const char* filename)
410 {
411         char *buf, *dst, *end;
412         size_t flen = strlen(filename);
413         /* we replace each '{}' with filename: growth by strlen-2 */
414         buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
415         while ((end = strstr(src, "{}"))) {
416                 memcpy(dst, src, end - src);
417                 dst += end - src;
418                 src = end + 2;
419                 memcpy(dst, filename, flen);
420                 dst += flen;
421         }
422         strcpy(dst, src);
423         return buf;
424 }
425 #endif
426
427 /* Return values of ACTFs ('action functions') are a bit mask:
428  * bit 1=1: prune (use SKIP constant for setting it)
429  * bit 0=1: matched successfully (TRUE)
430  */
431
432 static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
433 {
434         int cur_group;
435         int cur_action;
436         int rc = 0;
437         action **app, *ap;
438
439         /* "action group" is a set of actions ANDed together.
440          * groups are ORed together.
441          * We simply evaluate each group until we find one in which all actions
442          * succeed. */
443
444         /* -prune is special: if it is encountered, then we won't
445          * descend into current directory. It doesn't matter whether
446          * action group (in which -prune sits) will succeed or not:
447          * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
448          * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
449          *     not starting with 'f' */
450
451         /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
452          * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
453          * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
454          * On return, bit is restored.  */
455
456         cur_group = -1;
457         while ((app = appp[++cur_group]) != NULL) {
458                 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
459                 cur_action = -1;
460                 while (1) {
461                         ap = app[++cur_action];
462                         if (!ap) /* all actions in group were successful */
463                                 return rc ^ TRUE; /* restore TRUE bit */
464                         rc |= TRUE ^ ap->f(fileName, statbuf, ap);
465 #if ENABLE_FEATURE_FIND_NOT
466                         if (ap->invert) rc ^= TRUE;
467 #endif
468                         if (rc & TRUE) /* current group failed, try next */
469                                 break;
470                 }
471         }
472         return rc ^ TRUE; /* restore TRUE bit */
473 }
474
475
476 ACTF(name)
477 {
478         const char *tmp = bb_basename(fileName);
479         if (tmp != fileName && *tmp == '\0') {
480                 /* "foo/bar/". Oh no... go back to 'b' */
481                 tmp--;
482                 while (tmp != fileName && *--tmp != '/')
483                         continue;
484                 if (*tmp == '/')
485                         tmp++;
486         }
487         /* Was using FNM_PERIOD flag too,
488          * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
489          * find -name '*foo' should match .foo too:
490          */
491         return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0;
492 }
493
494 #if ENABLE_FEATURE_FIND_PATH
495 ACTF(path)
496 {
497         return fnmatch(ap->pattern, fileName, 0) == 0;
498 }
499 #endif
500 #if ENABLE_FEATURE_FIND_REGEX
501 ACTF(regex)
502 {
503         regmatch_t match;
504         if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
505                 return 0; /* no match */
506         if (match.rm_so)
507                 return 0; /* match doesn't start at pos 0 */
508         if (fileName[match.rm_eo])
509                 return 0; /* match doesn't end exactly at end of pathname */
510         return 1;
511 }
512 #endif
513 #if ENABLE_FEATURE_FIND_TYPE
514 ACTF(type)
515 {
516         return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
517 }
518 #endif
519 #if ENABLE_FEATURE_FIND_PERM
520 ACTF(perm)
521 {
522         /* -perm +mode: at least one of perm_mask bits are set */
523         if (ap->perm_char == '+')
524                 return (statbuf->st_mode & ap->perm_mask) != 0;
525         /* -perm -mode: all of perm_mask are set */
526         if (ap->perm_char == '-')
527                 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
528         /* -perm mode: file mode must match perm_mask */
529         return (statbuf->st_mode & 07777) == ap->perm_mask;
530 }
531 #endif
532 #if ENABLE_FEATURE_FIND_MTIME
533 ACTF(mtime)
534 {
535         time_t file_age = time(NULL) - statbuf->st_mtime;
536         time_t mtime_secs = ap->mtime_days * 24*60*60;
537         if (ap->mtime_char == '+')
538                 return file_age >= mtime_secs + 24*60*60;
539         if (ap->mtime_char == '-')
540                 return file_age < mtime_secs;
541         /* just numeric mtime */
542         return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
543 }
544 #endif
545 #if ENABLE_FEATURE_FIND_MMIN
546 ACTF(mmin)
547 {
548         time_t file_age = time(NULL) - statbuf->st_mtime;
549         time_t mmin_secs = ap->mmin_mins * 60;
550         if (ap->mmin_char == '+')
551                 return file_age >= mmin_secs + 60;
552         if (ap->mmin_char == '-')
553                 return file_age < mmin_secs;
554         /* just numeric mmin */
555         return file_age >= mmin_secs && file_age < (mmin_secs + 60);
556 }
557 #endif
558 #if ENABLE_FEATURE_FIND_NEWER
559 ACTF(newer)
560 {
561         return (ap->newer_mtime < statbuf->st_mtime);
562 }
563 #endif
564 #if ENABLE_FEATURE_FIND_INUM
565 ACTF(inum)
566 {
567         return (statbuf->st_ino == ap->inode_num);
568 }
569 #endif
570 #if ENABLE_FEATURE_FIND_EXEC
571 ACTF(exec)
572 {
573         int i, rc;
574 #if ENABLE_USE_PORTABLE_CODE
575         char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
576 #else /* gcc 4.3.1 generates smaller code: */
577         char *argv[ap->exec_argc + 1];
578 #endif
579         for (i = 0; i < ap->exec_argc; i++)
580                 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
581         argv[i] = NULL; /* terminate the list */
582
583         rc = spawn_and_wait(argv);
584         if (rc < 0)
585                 bb_simple_perror_msg(argv[0]);
586
587         i = 0;
588         while (argv[i])
589                 free(argv[i++]);
590         return rc == 0; /* return 1 if exitcode 0 */
591 }
592 #endif
593 #if ENABLE_FEATURE_FIND_USER
594 ACTF(user)
595 {
596         return (statbuf->st_uid == ap->uid);
597 }
598 #endif
599 #if ENABLE_FEATURE_FIND_GROUP
600 ACTF(group)
601 {
602         return (statbuf->st_gid == ap->gid);
603 }
604 #endif
605 #if ENABLE_FEATURE_FIND_PRINT0
606 ACTF(print0)
607 {
608         printf("%s%c", fileName, '\0');
609         return TRUE;
610 }
611 #endif
612 ACTF(print)
613 {
614         puts(fileName);
615         return TRUE;
616 }
617 #if ENABLE_FEATURE_FIND_PAREN
618 ACTF(paren)
619 {
620         return exec_actions(ap->subexpr, fileName, statbuf);
621 }
622 #endif
623 #if ENABLE_FEATURE_FIND_SIZE
624 ACTF(size)
625 {
626         if (ap->size_char == '+')
627                 return statbuf->st_size > ap->size;
628         if (ap->size_char == '-')
629                 return statbuf->st_size < ap->size;
630         return statbuf->st_size == ap->size;
631 }
632 #endif
633 #if ENABLE_FEATURE_FIND_PRUNE
634 /*
635  * -prune: if -depth is not given, return true and do not descend
636  * current dir; if -depth is given, return false with no effect.
637  * Example:
638  * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
639  */
640 ACTF(prune)
641 {
642         return SKIP + TRUE;
643 }
644 #endif
645 #if ENABLE_FEATURE_FIND_DELETE
646 ACTF(delete)
647 {
648         int rc;
649         if (S_ISDIR(statbuf->st_mode)) {
650                 rc = rmdir(fileName);
651         } else {
652                 rc = unlink(fileName);
653         }
654         if (rc < 0)
655                 bb_simple_perror_msg(fileName);
656         return TRUE;
657 }
658 #endif
659 #if ENABLE_FEATURE_FIND_CONTEXT
660 ACTF(context)
661 {
662         security_context_t con;
663         int rc;
664
665         if (G.recurse_flags & ACTION_FOLLOWLINKS) {
666                 rc = getfilecon(fileName, &con);
667         } else {
668                 rc = lgetfilecon(fileName, &con);
669         }
670         if (rc < 0)
671                 return FALSE;
672         rc = strcmp(ap->context, con);
673         freecon(con);
674         return rc == 0;
675 }
676 #endif
677 #if ENABLE_FEATURE_FIND_LINKS
678 ACTF(links)
679 {
680         switch(ap->links_char) {
681         case '-' : return (statbuf->st_nlink <  ap->links_count);
682         case '+' : return (statbuf->st_nlink >  ap->links_count);
683         default:   return (statbuf->st_nlink == ap->links_count);
684         }
685 }
686 #endif
687
688 static int FAST_FUNC fileAction(const char *fileName,
689                 struct stat *statbuf,
690                 void *userData UNUSED_PARAM,
691                 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
692 {
693         int r;
694
695 #if ENABLE_FEATURE_FIND_MAXDEPTH
696         if (depth < G.minmaxdepth[0])
697                 return TRUE; /* skip this, continue recursing */
698         if (depth > G.minmaxdepth[1])
699                 return SKIP; /* stop recursing */
700 #endif
701
702         r = exec_actions(G.actions, fileName, statbuf);
703         /* Had no explicit -print[0] or -exec? then print */
704         if ((r & TRUE) && G.need_print)
705                 puts(fileName);
706
707 #if ENABLE_FEATURE_FIND_MAXDEPTH
708         if (S_ISDIR(statbuf->st_mode)) {
709                 if (depth == G.minmaxdepth[1])
710                         return SKIP;
711         }
712 #endif
713 #if ENABLE_FEATURE_FIND_XDEV
714         /* -xdev stops on mountpoints, but AFTER mountpoit itself
715          * is processed as usual */
716         if (S_ISDIR(statbuf->st_mode)) {
717                 if (G.xdev_count) {
718                         int i;
719                         for (i = 0; i < G.xdev_count; i++) {
720                                 if (G.xdev_dev[i] == statbuf->st_dev)
721                                         goto found;
722                         }
723                         return SKIP;
724  found: ;
725                 }
726         }
727 #endif
728
729         /* Cannot return 0: our caller, recursive_action(),
730          * will perror() and skip dirs (if called on dir) */
731         return (r & SKIP) ? SKIP : TRUE;
732 }
733
734
735 #if ENABLE_FEATURE_FIND_TYPE
736 static int find_type(const char *type)
737 {
738         int mask = 0;
739
740         if (*type == 'b')
741                 mask = S_IFBLK;
742         else if (*type == 'c')
743                 mask = S_IFCHR;
744         else if (*type == 'd')
745                 mask = S_IFDIR;
746         else if (*type == 'p')
747                 mask = S_IFIFO;
748         else if (*type == 'f')
749                 mask = S_IFREG;
750         else if (*type == 'l')
751                 mask = S_IFLNK;
752         else if (*type == 's')
753                 mask = S_IFSOCK;
754
755         if (mask == 0 || type[1] != '\0')
756                 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
757
758         return mask;
759 }
760 #endif
761
762 #if ENABLE_FEATURE_FIND_PERM \
763  || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
764  || ENABLE_FEATURE_FIND_SIZE  || ENABLE_FEATURE_FIND_LINKS
765 static const char* plus_minus_num(const char* str)
766 {
767         if (*str == '-' || *str == '+')
768                 str++;
769         return str;
770 }
771 #endif
772
773 static action*** parse_params(char **argv)
774 {
775         enum {
776                                 OPT_FOLLOW     ,
777         IF_FEATURE_FIND_XDEV(   OPT_XDEV       ,)
778         IF_FEATURE_FIND_DEPTH(  OPT_DEPTH      ,)
779                                 PARM_a         ,
780                                 PARM_o         ,
781         IF_FEATURE_FIND_NOT(    PARM_char_not  ,)
782 #if ENABLE_DESKTOP
783                                 PARM_and       ,
784                                 PARM_or        ,
785         IF_FEATURE_FIND_NOT(    PARM_not       ,)
786 #endif
787                                 PARM_print     ,
788         IF_FEATURE_FIND_PRINT0( PARM_print0    ,)
789         IF_FEATURE_FIND_PRUNE(  PARM_prune     ,)
790         IF_FEATURE_FIND_DELETE( PARM_delete    ,)
791         IF_FEATURE_FIND_EXEC(   PARM_exec      ,)
792         IF_FEATURE_FIND_PAREN(  PARM_char_brace,)
793         /* All options/actions starting from here require argument */
794                                 PARM_name      ,
795                                 PARM_iname     ,
796         IF_FEATURE_FIND_PATH(   PARM_path      ,)
797         IF_FEATURE_FIND_REGEX(  PARM_regex     ,)
798         IF_FEATURE_FIND_TYPE(   PARM_type      ,)
799         IF_FEATURE_FIND_PERM(   PARM_perm      ,)
800         IF_FEATURE_FIND_MTIME(  PARM_mtime     ,)
801         IF_FEATURE_FIND_MMIN(   PARM_mmin      ,)
802         IF_FEATURE_FIND_NEWER(  PARM_newer     ,)
803         IF_FEATURE_FIND_INUM(   PARM_inum      ,)
804         IF_FEATURE_FIND_USER(   PARM_user      ,)
805         IF_FEATURE_FIND_GROUP(  PARM_group     ,)
806         IF_FEATURE_FIND_SIZE(   PARM_size      ,)
807         IF_FEATURE_FIND_CONTEXT(PARM_context   ,)
808         IF_FEATURE_FIND_LINKS(  PARM_links     ,)
809         IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
810         };
811
812         static const char params[] ALIGN1 =
813                                 "-follow\0"
814         IF_FEATURE_FIND_XDEV(   "-xdev\0"                 )
815         IF_FEATURE_FIND_DEPTH(  "-depth\0"                )
816                                 "-a\0"
817                                 "-o\0"
818         IF_FEATURE_FIND_NOT(    "!\0"       )
819 #if ENABLE_DESKTOP
820                                 "-and\0"
821                                 "-or\0"
822         IF_FEATURE_FIND_NOT(    "-not\0"    )
823 #endif
824                                 "-print\0"
825         IF_FEATURE_FIND_PRINT0( "-print0\0" )
826         IF_FEATURE_FIND_PRUNE(  "-prune\0"  )
827         IF_FEATURE_FIND_DELETE( "-delete\0" )
828         IF_FEATURE_FIND_EXEC(   "-exec\0"   )
829         IF_FEATURE_FIND_PAREN(  "(\0"       )
830         /* All options/actions starting from here require argument */
831                                  "-name\0"
832                                  "-iname\0"
833         IF_FEATURE_FIND_PATH(   "-path\0"   )
834         IF_FEATURE_FIND_REGEX(  "-regex\0"  )
835         IF_FEATURE_FIND_TYPE(   "-type\0"   )
836         IF_FEATURE_FIND_PERM(   "-perm\0"   )
837         IF_FEATURE_FIND_MTIME(  "-mtime\0"  )
838         IF_FEATURE_FIND_MMIN(   "-mmin\0"   )
839         IF_FEATURE_FIND_NEWER(  "-newer\0"  )
840         IF_FEATURE_FIND_INUM(   "-inum\0"   )
841         IF_FEATURE_FIND_USER(   "-user\0"   )
842         IF_FEATURE_FIND_GROUP(  "-group\0"  )
843         IF_FEATURE_FIND_SIZE(   "-size\0"   )
844         IF_FEATURE_FIND_CONTEXT("-context\0")
845         IF_FEATURE_FIND_LINKS(  "-links\0"  )
846         IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
847         ;
848
849         action*** appp;
850         unsigned cur_group = 0;
851         unsigned cur_action = 0;
852         IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
853
854         /* This is the only place in busybox where we use nested function.
855          * So far more standard alternatives were bigger. */
856         /* Auto decl suppresses "func without a prototype" warning: */
857         auto action* alloc_action(int sizeof_struct, action_fp f);
858         action* alloc_action(int sizeof_struct, action_fp f)
859         {
860                 action *ap;
861                 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
862                 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
863                 appp[cur_group][cur_action] = NULL;
864                 ap->f = f;
865                 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
866                 IF_FEATURE_FIND_NOT( invert_flag = 0; )
867                 return ap;
868         }
869
870 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
871
872         appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
873
874         while (*argv) {
875                 const char *arg = argv[0];
876                 int parm = index_in_strings(params, arg);
877                 const char *arg1 = argv[1];
878
879                 if (parm >= PARM_name) {
880                         /* All options/actions starting from -name require argument */
881                         if (!arg1)
882                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
883                         argv++;
884                 }
885
886                 /* We can use big switch() here, but on i386
887                  * it doesn't give smaller code. Other arches? */
888
889 /* Options always return true. They always take effect
890  * rather than being processed only when their place in the
891  * expression is reached.
892  */
893                 /* Options */
894 #if ENABLE_FEATURE_FIND_XDEV
895                 if (parm == OPT_XDEV) {
896                         G.xdev_on = 1;
897                 }
898 #endif
899 #if ENABLE_FEATURE_FIND_MAXDEPTH
900                 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
901                         G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
902                 }
903 #endif
904 #if ENABLE_FEATURE_FIND_DEPTH
905                 else if (parm == OPT_DEPTH) {
906                         G.recurse_flags |= ACTION_DEPTHFIRST;
907                 }
908 #endif
909 /* Actions are grouped by operators
910  * ( expr )              Force precedence
911  * ! expr                True if expr is false
912  * -not expr             Same as ! expr
913  * expr1 [-a[nd]] expr2  And; expr2 is not evaluated if expr1 is false
914  * expr1 -o[r] expr2     Or; expr2 is not evaluated if expr1 is true
915  * expr1 , expr2         List; both expr1 and expr2 are always evaluated
916  * We implement: (), -a, -o
917  */
918                 /* Operators */
919                 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
920                         /* no further special handling required */
921                 }
922                 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
923                         /* start new OR group */
924                         cur_group++;
925                         appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
926                         /*appp[cur_group] = NULL; - already NULL */
927                         appp[cur_group+1] = NULL;
928                         cur_action = 0;
929                 }
930 #if ENABLE_FEATURE_FIND_NOT
931                 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
932                         /* also handles "find ! ! -name 'foo*'" */
933                         invert_flag ^= 1;
934                 }
935 #endif
936                 /* Actions */
937                 else if (parm == PARM_print) {
938                         G.need_print = 0;
939                         (void) ALLOC_ACTION(print);
940                 }
941 #if ENABLE_FEATURE_FIND_PRINT0
942                 else if (parm == PARM_print0) {
943                         G.need_print = 0;
944                         (void) ALLOC_ACTION(print0);
945                 }
946 #endif
947 #if ENABLE_FEATURE_FIND_PRUNE
948                 else if (parm == PARM_prune) {
949                         (void) ALLOC_ACTION(prune);
950                 }
951 #endif
952 #if ENABLE_FEATURE_FIND_DELETE
953                 else if (parm == PARM_delete) {
954                         G.need_print = 0;
955                         G.recurse_flags |= ACTION_DEPTHFIRST;
956                         (void) ALLOC_ACTION(delete);
957                 }
958 #endif
959 #if ENABLE_FEATURE_FIND_EXEC
960                 else if (parm == PARM_exec) {
961                         int i;
962                         action_exec *ap;
963                         G.need_print = 0;
964                         ap = ALLOC_ACTION(exec);
965                         ap->exec_argv = ++argv; /* first arg after -exec */
966                         /*ap->exec_argc = 0; - ALLOC_ACTION did it */
967                         while (1) {
968                                 if (!*argv) /* did not see ';' or '+' until end */
969                                         bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
970                                 // find -exec echo Foo ">{}<" ";"
971                                 // executes "echo Foo >FILENAME<",
972                                 // find -exec echo Foo ">{}<" "+"
973                                 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
974                                 // TODO (so far we treat "+" just like ";")
975                                 if ((argv[0][0] == ';' || argv[0][0] == '+')
976                                  && argv[0][1] == '\0'
977                                 ) {
978                                         break;
979                                 }
980                                 argv++;
981                                 ap->exec_argc++;
982                         }
983                         if (ap->exec_argc == 0)
984                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
985                         ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
986                         i = ap->exec_argc;
987                         while (i--)
988                                 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
989                 }
990 #endif
991 #if ENABLE_FEATURE_FIND_PAREN
992                 else if (parm == PARM_char_brace) {
993                         action_paren *ap;
994                         char **endarg;
995                         unsigned nested = 1;
996
997                         endarg = argv;
998                         while (1) {
999                                 if (!*++endarg)
1000                                         bb_error_msg_and_die("unpaired '('");
1001                                 if (LONE_CHAR(*endarg, '('))
1002                                         nested++;
1003                                 else if (LONE_CHAR(*endarg, ')') && !--nested) {
1004                                         *endarg = NULL;
1005                                         break;
1006                                 }
1007                         }
1008                         ap = ALLOC_ACTION(paren);
1009                         ap->subexpr = parse_params(argv + 1);
1010                         *endarg = (char*) ")"; /* restore NULLed parameter */
1011                         argv = endarg;
1012                 }
1013 #endif
1014                 else if (parm == PARM_name || parm == PARM_iname) {
1015                         action_name *ap;
1016                         ap = ALLOC_ACTION(name);
1017                         ap->pattern = arg1;
1018                         ap->iname = (parm == PARM_iname);
1019                 }
1020 #if ENABLE_FEATURE_FIND_PATH
1021                 else if (parm == PARM_path) {
1022                         action_path *ap;
1023                         ap = ALLOC_ACTION(path);
1024                         ap->pattern = arg1;
1025                 }
1026 #endif
1027 #if ENABLE_FEATURE_FIND_REGEX
1028                 else if (parm == PARM_regex) {
1029                         action_regex *ap;
1030                         ap = ALLOC_ACTION(regex);
1031                         xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1032                 }
1033 #endif
1034 #if ENABLE_FEATURE_FIND_TYPE
1035                 else if (parm == PARM_type) {
1036                         action_type *ap;
1037                         ap = ALLOC_ACTION(type);
1038                         ap->type_mask = find_type(arg1);
1039                 }
1040 #endif
1041 #if ENABLE_FEATURE_FIND_PERM
1042 /* -perm BITS   File's mode bits are exactly BITS (octal or symbolic).
1043  *              Symbolic modes use mode 0 as a point of departure.
1044  * -perm -BITS  All of the BITS are set in file's mode.
1045  * -perm +BITS  At least one of the BITS is set in file's mode.
1046  */
1047                 else if (parm == PARM_perm) {
1048                         action_perm *ap;
1049                         ap = ALLOC_ACTION(perm);
1050                         ap->perm_char = arg1[0];
1051                         arg1 = plus_minus_num(arg1);
1052                         /*ap->perm_mask = 0; - ALLOC_ACTION did it */
1053                         if (!bb_parse_mode(arg1, &ap->perm_mask))
1054                                 bb_error_msg_and_die("invalid mode '%s'", arg1);
1055                 }
1056 #endif
1057 #if ENABLE_FEATURE_FIND_MTIME
1058                 else if (parm == PARM_mtime) {
1059                         action_mtime *ap;
1060                         ap = ALLOC_ACTION(mtime);
1061                         ap->mtime_char = arg1[0];
1062                         ap->mtime_days = xatoul(plus_minus_num(arg1));
1063                 }
1064 #endif
1065 #if ENABLE_FEATURE_FIND_MMIN
1066                 else if (parm == PARM_mmin) {
1067                         action_mmin *ap;
1068                         ap = ALLOC_ACTION(mmin);
1069                         ap->mmin_char = arg1[0];
1070                         ap->mmin_mins = xatoul(plus_minus_num(arg1));
1071                 }
1072 #endif
1073 #if ENABLE_FEATURE_FIND_NEWER
1074                 else if (parm == PARM_newer) {
1075                         struct stat stat_newer;
1076                         action_newer *ap;
1077                         ap = ALLOC_ACTION(newer);
1078                         xstat(arg1, &stat_newer);
1079                         ap->newer_mtime = stat_newer.st_mtime;
1080                 }
1081 #endif
1082 #if ENABLE_FEATURE_FIND_INUM
1083                 else if (parm == PARM_inum) {
1084                         action_inum *ap;
1085                         ap = ALLOC_ACTION(inum);
1086                         ap->inode_num = xatoul(arg1);
1087                 }
1088 #endif
1089 #if ENABLE_FEATURE_FIND_USER
1090                 else if (parm == PARM_user) {
1091                         action_user *ap;
1092                         ap = ALLOC_ACTION(user);
1093                         ap->uid = bb_strtou(arg1, NULL, 10);
1094                         if (errno)
1095                                 ap->uid = xuname2uid(arg1);
1096                 }
1097 #endif
1098 #if ENABLE_FEATURE_FIND_GROUP
1099                 else if (parm == PARM_group) {
1100                         action_group *ap;
1101                         ap = ALLOC_ACTION(group);
1102                         ap->gid = bb_strtou(arg1, NULL, 10);
1103                         if (errno)
1104                                 ap->gid = xgroup2gid(arg1);
1105                 }
1106 #endif
1107 #if ENABLE_FEATURE_FIND_SIZE
1108                 else if (parm == PARM_size) {
1109 /* -size n[bckw]: file uses n units of space
1110  * b (default): units are 512-byte blocks
1111  * c: 1 byte
1112  * k: kilobytes
1113  * w: 2-byte words
1114  */
1115 #if ENABLE_LFS
1116 #define XATOU_SFX xatoull_sfx
1117 #else
1118 #define XATOU_SFX xatoul_sfx
1119 #endif
1120                         static const struct suffix_mult find_suffixes[] = {
1121                                 { "c", 1 },
1122                                 { "w", 2 },
1123                                 { "", 512 },
1124                                 { "b", 512 },
1125                                 { "k", 1024 },
1126                                 { "", 0 }
1127                         };
1128                         action_size *ap;
1129                         ap = ALLOC_ACTION(size);
1130                         ap->size_char = arg1[0];
1131                         ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
1132                 }
1133 #endif
1134 #if ENABLE_FEATURE_FIND_CONTEXT
1135                 else if (parm == PARM_context) {
1136                         action_context *ap;
1137                         ap = ALLOC_ACTION(context);
1138                         /*ap->context = NULL; - ALLOC_ACTION did it */
1139                         /* SELinux headers erroneously declare non-const parameter */
1140                         if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
1141                                 bb_simple_perror_msg(arg1);
1142                 }
1143 #endif
1144 #if ENABLE_FEATURE_FIND_LINKS
1145                 else if (parm == PARM_links) {
1146                         action_links *ap;
1147                         ap = ALLOC_ACTION(links);
1148                         ap->links_char = arg1[0];
1149                         ap->links_count = xatoul(plus_minus_num(arg1));
1150                 }
1151 #endif
1152                 else {
1153                         bb_error_msg("unrecognized: %s", arg);
1154                         bb_show_usage();
1155                 }
1156                 argv++;
1157         }
1158         return appp;
1159 #undef ALLOC_ACTION
1160 }
1161
1162 int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
1163 int find_main(int argc UNUSED_PARAM, char **argv)
1164 {
1165         int i, firstopt, status = EXIT_SUCCESS;
1166
1167         INIT_G();
1168
1169         argv++;
1170         for (firstopt = 0; argv[firstopt]; firstopt++) {
1171                 if (argv[firstopt][0] == '-')
1172                         break;
1173                 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1174                         break;
1175                 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
1176                         break;
1177         }
1178         if (firstopt == 0) {
1179                 *--argv = (char*)".";
1180                 firstopt++;
1181         }
1182
1183         G.actions = parse_params(&argv[firstopt]);
1184         argv[firstopt] = NULL;
1185
1186 #if ENABLE_FEATURE_FIND_XDEV
1187         if (G.xdev_on) {
1188                 struct stat stbuf;
1189
1190                 G.xdev_count = firstopt;
1191                 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1192                 for (i = 0; argv[i]; i++) {
1193                         /* not xstat(): shouldn't bomb out on
1194                          * "find not_exist exist -xdev" */
1195                         if (stat(argv[i], &stbuf) == 0)
1196                                 G.xdev_dev[i] = stbuf.st_dev;
1197                         /* else G.xdev_dev[i] stays 0 and
1198                          * won't match any real device dev_t
1199                          */
1200                 }
1201         }
1202 #endif
1203
1204         for (i = 0; argv[i]; i++) {
1205                 if (!recursive_action(argv[i],
1206                                 G.recurse_flags,/* flags */
1207                                 fileAction,     /* file action */
1208                                 fileAction,     /* dir action */
1209                                 NULL,           /* user data */
1210                                 0)              /* depth */
1211                 ) {
1212                         status = EXIT_FAILURE;
1213                 }
1214         }
1215
1216         return status;
1217 }