9ca38c21afd6d1649ddcff644323e0bff78cc9e3
[oweals/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 the GPL version 2, see the file LICENSE in this tarball.
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 #include <fnmatch.h>
49 #include "busybox.h"
50
51 USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
52 USE_FEATURE_FIND_XDEV(static int xdev_count;)
53
54 typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
55
56 typedef struct {
57         action_fp f;
58 #if ENABLE_FEATURE_FIND_NOT
59         bool invert;
60 #endif
61 } action;
62 #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
63 #define ACTF(name)         static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
64                         ACTS(print)
65                         ACTS(name,  const char *pattern;)
66 USE_FEATURE_FIND_PRINT0(ACTS(print0))
67 USE_FEATURE_FIND_TYPE(  ACTS(type,  int type_mask;))
68 USE_FEATURE_FIND_PERM(  ACTS(perm,  char perm_char; mode_t perm_mask;))
69 USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
70 USE_FEATURE_FIND_MMIN(  ACTS(mmin,  char mmin_char; unsigned mmin_mins;))
71 USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
72 USE_FEATURE_FIND_INUM(  ACTS(inum,  ino_t inode_num;))
73 USE_FEATURE_FIND_EXEC(  ACTS(exec,  char **exec_argv; unsigned int *subst_count; int exec_argc;))
74 USE_FEATURE_FIND_USER(  ACTS(user,  uid_t uid;))
75 USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
76 USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
77 USE_FEATURE_FIND_SIZE(  ACTS(size,  off_t size;))
78 USE_FEATURE_FIND_PRUNE( ACTS(prune))
79
80 static action ***actions;
81 static bool need_print = 1;
82 static int recurse_flags = action_recurse;
83
84 #if ENABLE_FEATURE_FIND_EXEC
85 static unsigned int count_subst(const char *str)
86 {
87         unsigned int count = 0;
88         while ((str = strstr(str, "{}"))) {
89                 count++;
90                 str++;
91         }
92         return count;
93 }
94
95
96 static char* subst(const char *src, unsigned int count, const char* filename)
97 {
98         char *buf, *dst, *end;
99         size_t flen = strlen(filename);
100         /* we replace each '{}' with filename: growth by strlen-2 */
101         buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
102         while ((end = strstr(src, "{}"))) {
103                 memcpy(dst, src, end - src);
104                 dst += end - src;
105                 src = end + 2;
106                 memcpy(dst, filename, flen);
107                 dst += flen;
108         }
109         strcpy(dst, src);
110         return buf;
111 }
112 #endif
113
114
115 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
116 {
117         int cur_group;
118         int cur_action;
119         int rc = TRUE;
120         action **app, *ap;
121
122         cur_group = -1;
123         while ((app = appp[++cur_group])) {
124                 cur_action = -1;
125                 while (1) {
126                         ap = app[++cur_action];
127                         if (!ap) {
128                                 /* all actions in group were successful */
129                                 return rc;
130                         }
131                         rc = ap->f(fileName, statbuf, ap);
132 #if ENABLE_FEATURE_FIND_NOT
133                         if (ap->invert) rc = !rc;
134 #endif
135                         if (!rc) {
136                                 /* current group failed, try next */
137                                 break;
138                         }
139                 }
140         }
141         return rc;
142 }
143
144
145 ACTF(name)
146 {
147         const char *tmp = strrchr(fileName, '/');
148         if (tmp == NULL)
149                 tmp = fileName;
150         else
151                 tmp++;
152         return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
153 }
154 #if ENABLE_FEATURE_FIND_TYPE
155 ACTF(type)
156 {
157         return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
158 }
159 #endif
160 #if ENABLE_FEATURE_FIND_PERM
161 ACTF(perm)
162 {
163         /* -perm +mode: at least one of perm_mask bits are set */
164         if (ap->perm_char == '+')
165                 return (statbuf->st_mode & ap->perm_mask) != 0;
166         /* -perm -mode: all of perm_mask are set */
167         if (ap->perm_char == '-')
168                 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
169         /* -perm mode: file mode must match perm_mask */
170         return (statbuf->st_mode & 07777) == ap->perm_mask;
171 }
172 #endif
173 #if ENABLE_FEATURE_FIND_MTIME
174 ACTF(mtime)
175 {
176         time_t file_age = time(NULL) - statbuf->st_mtime;
177         time_t mtime_secs = ap->mtime_days * 24*60*60;
178         if (ap->mtime_char == '+')
179                 return file_age >= mtime_secs + 24*60*60;
180         if (ap->mtime_char == '-')
181                 return file_age < mtime_secs;
182         /* just numeric mtime */
183         return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
184 }
185 #endif
186 #if ENABLE_FEATURE_FIND_MMIN
187 ACTF(mmin)
188 {
189         time_t file_age = time(NULL) - statbuf->st_mtime;
190         time_t mmin_secs = ap->mmin_mins * 60;
191         if (ap->mmin_char == '+')
192                 return file_age >= mmin_secs + 60;
193         if (ap->mmin_char == '-')
194                 return file_age < mmin_secs;
195         /* just numeric mmin */
196         return file_age >= mmin_secs && file_age < (mmin_secs + 60);
197 }
198 #endif
199 #if ENABLE_FEATURE_FIND_NEWER
200 ACTF(newer)
201 {
202         return (ap->newer_mtime < statbuf->st_mtime);
203 }
204 #endif
205 #if ENABLE_FEATURE_FIND_INUM
206 ACTF(inum)
207 {
208         return (statbuf->st_ino == ap->inode_num);
209 }
210 #endif
211 #if ENABLE_FEATURE_FIND_EXEC
212 ACTF(exec)
213 {
214         int i, rc;
215         char *argv[ap->exec_argc+1];
216         for (i = 0; i < ap->exec_argc; i++)
217                 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
218         argv[i] = NULL; /* terminate the list */
219         rc = wait4pid(spawn(argv));
220         if (rc)
221                 bb_perror_msg("%s", argv[0]);
222         for (i = 0; i < ap->exec_argc; i++)
223                 free(argv[i]);
224         return rc == 0; /* return 1 if success */
225 }
226 #endif
227
228 #if ENABLE_FEATURE_FIND_USER
229 ACTF(user)
230 {
231         return (statbuf->st_uid == ap->uid);
232 }
233 #endif
234
235 #if ENABLE_FEATURE_FIND_GROUP
236 ACTF(group)
237 {
238         return (statbuf->st_gid == ap->gid);
239 }
240 #endif
241
242 #if ENABLE_FEATURE_FIND_PRINT0
243 ACTF(print0)
244 {
245         printf("%s%c", fileName, '\0');
246         return TRUE;
247 }
248 #endif
249
250 ACTF(print)
251 {
252         puts(fileName);
253         return TRUE;
254 }
255
256 #if ENABLE_FEATURE_FIND_PAREN
257 ACTF(paren)
258 {
259         return exec_actions(ap->subexpr, fileName, statbuf);
260 }
261 #endif
262
263 #if ENABLE_FEATURE_FIND_SIZE
264 /*
265  * -prune: if -depth is not given, return true and do not descend
266  * current dir; if -depth is given, return false with no effect.
267  * Example:
268  * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
269  */
270 ACTF(prune)
271 {
272         return SKIP;
273 }
274 #endif
275
276 #if ENABLE_FEATURE_FIND_PRUNE
277 ACTF(size)
278 {
279         return statbuf->st_size == ap->size;
280 }
281 #endif
282
283
284 static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
285 {
286         int i;
287 #ifdef CONFIG_FEATURE_FIND_XDEV
288         if (S_ISDIR(statbuf->st_mode) && xdev_count) {
289                 for (i = 0; i < xdev_count; i++) {
290                         if (xdev_dev[i] != statbuf->st_dev)
291                                 return SKIP;
292                 }
293         }
294 #endif
295         i = exec_actions(actions, fileName, statbuf);
296         /* Had no explicit -print[0] or -exec? then print */
297         if (i && need_print)
298                 puts(fileName);
299         /* Cannot return 0: our caller, recursive_action(),
300          * will perror() and skip dirs (if called on dir) */
301         return i == 0 ? TRUE : i;
302 }
303
304
305 #if ENABLE_FEATURE_FIND_TYPE
306 static int find_type(const char *type)
307 {
308         int mask = 0;
309
310         if (*type == 'b')
311                 mask = S_IFBLK;
312         else if (*type == 'c')
313                 mask = S_IFCHR;
314         else if (*type == 'd')
315                 mask = S_IFDIR;
316         else if (*type == 'p')
317                 mask = S_IFIFO;
318         else if (*type == 'f')
319                 mask = S_IFREG;
320         else if (*type == 'l')
321                 mask = S_IFLNK;
322         else if (*type == 's')
323                 mask = S_IFSOCK;
324
325         if (mask == 0 || *(type + 1) != '\0')
326                 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
327
328         return mask;
329 }
330 #endif
331
332 #if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
333  || ENABLE_FEATURE_FIND_MMIN
334 static const char* plus_minus_num(const char* str)
335 {
336         if (*str == '-' || *str == '+')
337                 str++;
338         return str;
339 }
340 #endif
341
342 static action*** parse_params(char **argv)
343 {
344         enum {
345                                 PARM_a         ,
346                                 PARM_o         ,
347         USE_FEATURE_FIND_NOT(   PARM_char_not  ,)
348                                 PARM_print     ,
349         USE_FEATURE_FIND_PRINT0(PARM_print0    ,)
350                                 PARM_name      ,
351         USE_FEATURE_FIND_TYPE(  PARM_type      ,)
352         USE_FEATURE_FIND_PERM(  PARM_perm      ,)
353         USE_FEATURE_FIND_MTIME( PARM_mtime     ,)
354         USE_FEATURE_FIND_MMIN(  PARM_mmin      ,)
355         USE_FEATURE_FIND_NEWER( PARM_newer     ,)
356         USE_FEATURE_FIND_INUM(  PARM_inum      ,)
357         USE_FEATURE_FIND_EXEC(  PARM_exec      ,)
358         USE_FEATURE_FIND_USER(  PARM_user      ,)
359         USE_FEATURE_FIND_GROUP( PARM_group     ,)
360         USE_FEATURE_FIND_DEPTH( PARM_depth     ,)
361         USE_FEATURE_FIND_PAREN( PARM_char_brace,)
362         USE_FEATURE_FIND_SIZE(  PARM_prune     ,)
363         USE_FEATURE_FIND_PRUNE( PARM_size      ,)
364 #if ENABLE_DESKTOP
365                                 PARM_and       ,
366                                 PARM_or        ,
367         USE_FEATURE_FIND_NOT(   PARM_not       ,)
368 #endif
369         };
370
371         static const char *const params[] = {
372                                 "-a"     ,
373                                 "-o"     ,
374         USE_FEATURE_FIND_NOT(   "!"      ,)
375                                 "-print" ,
376         USE_FEATURE_FIND_PRINT0("-print0",)
377                                 "-name"  ,
378         USE_FEATURE_FIND_TYPE(  "-type"  ,)
379         USE_FEATURE_FIND_PERM(  "-perm"  ,)
380         USE_FEATURE_FIND_MTIME( "-mtime" ,)
381         USE_FEATURE_FIND_MMIN(  "-mmin"  ,)
382         USE_FEATURE_FIND_NEWER( "-newer" ,)
383         USE_FEATURE_FIND_INUM(  "-inum"  ,)
384         USE_FEATURE_FIND_EXEC(  "-exec"  ,)
385         USE_FEATURE_FIND_USER(  "-user"  ,)
386         USE_FEATURE_FIND_GROUP( "-group" ,)
387         USE_FEATURE_FIND_DEPTH( "-depth" ,)
388         USE_FEATURE_FIND_PAREN( "("      ,)
389         USE_FEATURE_FIND_SIZE(  "-prune" ,)
390         USE_FEATURE_FIND_PRUNE( "-size"  ,)
391 #if ENABLE_DESKTOP
392                                 "-and"   ,
393                                 "-or"    ,
394         USE_FEATURE_FIND_NOT(   "-not"   ,)
395 #endif
396                                 NULL
397         };
398
399         action*** appp;
400         unsigned cur_group = 0;
401         unsigned cur_action = 0;
402         USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
403
404         /* 'static' doesn't work here! (gcc 4.1.2) */
405         action* alloc_action(int sizeof_struct, action_fp f)
406         {
407                 action *ap;
408                 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
409                 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
410                 appp[cur_group][cur_action] = NULL;
411                 ap->f = f;
412                 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
413                 USE_FEATURE_FIND_NOT( invert_flag = 0; )
414                 return ap;
415         }
416
417 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
418
419         appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
420
421 /* Actions have side effects and return a true or false value
422  * We implement: -print, -print0, -exec
423  *
424  * The rest are tests.
425  *
426  * Tests and actions are grouped by operators
427  * ( expr )              Force precedence
428  * ! expr                True if expr is false
429  * -not expr             Same as ! expr
430  * expr1 [-a[nd]] expr2  And; expr2 is not evaluated if expr1 is false
431  * expr1 -o[r] expr2     Or; expr2 is not evaluated if expr1 is true
432  * expr1 , expr2         List; both expr1 and expr2 are always evaluated
433  * We implement: (), -a, -o
434  */
435         while (*argv) {
436                 const char *arg = argv[0];
437                 const char *arg1 = argv[1];
438                 int parm = index_in_str_array(params, arg);
439         /* --- Operators --- */
440                 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
441                         /* no further special handling required */
442                 }
443                 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
444                         /* start new OR group */
445                         cur_group++;
446                         appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
447                         /*appp[cur_group] = NULL; - already NULL */
448                         appp[cur_group+1] = NULL;
449                         cur_action = 0;
450                 }
451 #if ENABLE_FEATURE_FIND_NOT
452                 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
453                         /* also handles "find ! ! -name 'foo*'" */
454                         invert_flag ^= 1;
455                 }
456 #endif
457
458         /* --- Tests and actions --- */
459                 else if (parm == PARM_print) {
460                         need_print = 0;
461                         /* GNU find ignores '!' here: "find ! -print" */
462                         USE_FEATURE_FIND_NOT( invert_flag = 0; )
463                         (void) ALLOC_ACTION(print);
464                 }
465 #if ENABLE_FEATURE_FIND_PRINT0
466                 else if (parm == PARM_print0) {
467                         need_print = 0;
468                         USE_FEATURE_FIND_NOT( invert_flag = 0; )
469                         (void) ALLOC_ACTION(print0);
470                 }
471 #endif
472                 else if (parm == PARM_name) {
473                         action_name *ap;
474                         if (!*++argv)
475                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
476                         ap = ALLOC_ACTION(name);
477                         ap->pattern = arg1;
478                 }
479 #if ENABLE_FEATURE_FIND_TYPE
480                 else if (parm == PARM_type) {
481                         action_type *ap;
482                         if (!*++argv)
483                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
484                         ap = ALLOC_ACTION(type);
485                         ap->type_mask = find_type(arg1);
486                 }
487 #endif
488 #if ENABLE_FEATURE_FIND_PERM
489 /* TODO:
490  * -perm mode   File's permission bits are exactly mode (octal or symbolic).
491  *              Symbolic modes use mode 0 as a point of departure.
492  * -perm -mode  All of the permission bits mode are set for the file.
493  * -perm +mode  Any of the permission bits mode are set for the file.
494  */
495                 else if (parm == PARM_perm) {
496                         action_perm *ap;
497                         if (!*++argv)
498                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
499                         ap = ALLOC_ACTION(perm);
500                         ap->perm_char = arg1[0];
501                         arg1 = plus_minus_num(arg1);
502                         ap->perm_mask = 0;
503                         if (!bb_parse_mode(arg1, &ap->perm_mask))
504                                 bb_error_msg_and_die("invalid mode: %s", arg1);
505                 }
506 #endif
507 #if ENABLE_FEATURE_FIND_MTIME
508                 else if (parm == PARM_mtime) {
509                         action_mtime *ap;
510                         if (!*++argv)
511                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
512                         ap = ALLOC_ACTION(mtime);
513                         ap->mtime_char = arg1[0];
514                         ap->mtime_days = xatoul(plus_minus_num(arg1));
515                 }
516 #endif
517 #if ENABLE_FEATURE_FIND_MMIN
518                 else if (parm == PARM_mmin) {
519                         action_mmin *ap;
520                         if (!*++argv)
521                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
522                         ap = ALLOC_ACTION(mmin);
523                         ap->mmin_char = arg1[0];
524                         ap->mmin_mins = xatoul(plus_minus_num(arg1));
525                 }
526 #endif
527 #if ENABLE_FEATURE_FIND_NEWER
528                 else if (parm == PARM_newer) {
529                         action_newer *ap;
530                         struct stat stat_newer;
531                         if (!*++argv)
532                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
533                         xstat(arg1, &stat_newer);
534                         ap = ALLOC_ACTION(newer);
535                         ap->newer_mtime = stat_newer.st_mtime;
536                 }
537 #endif
538 #if ENABLE_FEATURE_FIND_INUM
539                 else if (parm == PARM_inum) {
540                         action_inum *ap;
541                         if (!*++argv)
542                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
543                         ap = ALLOC_ACTION(inum);
544                         ap->inode_num = xatoul(arg1);
545                 }
546 #endif
547 #if ENABLE_FEATURE_FIND_EXEC
548                 else if (parm == PARM_exec) {
549                         int i;
550                         action_exec *ap;
551                         need_print = 0;
552                         USE_FEATURE_FIND_NOT( invert_flag = 0; )
553                         ap = ALLOC_ACTION(exec);
554                         ap->exec_argv = ++argv; /* first arg after -exec */
555                         ap->exec_argc = 0;
556                         while (1) {
557                                 if (!*argv) /* did not see ';' util end */
558                                         bb_error_msg_and_die(bb_msg_requires_arg, arg);
559                                 if (LONE_CHAR(argv[0], ';'))
560                                         break;
561                                 argv++;
562                                 ap->exec_argc++;
563                         }
564                         if (ap->exec_argc == 0)
565                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
566                         ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
567                         i = ap->exec_argc;
568                         while (i--)
569                                 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
570                 }
571 #endif
572 #if ENABLE_FEATURE_FIND_USER
573                 else if (parm == PARM_user) {
574                         action_user *ap;
575                         if (!*++argv)
576                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
577                         ap = ALLOC_ACTION(user);
578                         ap->uid = bb_strtou(arg1, NULL, 10);
579                         if (errno)
580                                 ap->uid = xuname2uid(arg1);
581                 }
582 #endif
583 #if ENABLE_FEATURE_FIND_GROUP
584                 else if (parm == PARM_group) {
585                         action_group *ap;
586                         if (!*++argv)
587                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
588                         ap = ALLOC_ACTION(group);
589                         ap->gid = bb_strtou(arg1, NULL, 10);
590                         if (errno)
591                                 ap->gid = xgroup2gid(arg1);
592                 }
593 #endif
594 #if ENABLE_FEATURE_FIND_DEPTH
595                 else if (parm == PARM_depth) {
596                         recurse_flags |= action_depthFirst;
597                 }
598 #endif
599 #if ENABLE_FEATURE_FIND_PAREN
600                 else if (parm == PARM_char_brace) {
601                         action_paren *ap;
602                         char **endarg;
603                         unsigned nested = 1;
604
605                         endarg = argv;
606                         while (1) {
607                                 if (!*++endarg)
608                                         bb_error_msg_and_die("unpaired '('");
609                                 if (LONE_CHAR(*endarg, '('))
610                                         nested++;
611                                 else if (LONE_CHAR(*endarg, ')') && !--nested) {
612                                         *endarg = NULL;
613                                         break;
614                                 }
615                         }
616                         ap = ALLOC_ACTION(paren);
617                         ap->subexpr = parse_params(argv + 1);
618                         *endarg = (char*) ")"; /* restore NULLed parameter */
619                         argv = endarg;
620                 }
621 #endif
622 #if ENABLE_FEATURE_FIND_PRUNE
623                 else if (parm == PARM_prune) {
624                         USE_FEATURE_FIND_NOT( invert_flag = 0; )
625                         (void) ALLOC_ACTION(prune);
626                 }
627 #endif
628 #if ENABLE_FEATURE_FIND_SIZE
629                 else if (parm == PARM_size) {
630                         action_size *ap;
631                         if (!*++argv)
632                                 bb_error_msg_and_die(bb_msg_requires_arg, arg);
633                         ap = ALLOC_ACTION(size);
634                         ap->size = XATOOFF(arg1);
635                 }
636 #endif
637                 else
638                         bb_show_usage();
639                 argv++;
640         }
641         return appp;
642 #undef ALLOC_ACTION
643 }
644
645
646 int find_main(int argc, char **argv);
647 int find_main(int argc, char **argv)
648 {
649         bool dereference = FALSE;
650         char *arg;
651         char **argp;
652         int i, firstopt, status = EXIT_SUCCESS;
653         const char * const options[] = {
654                 "-follow",
655 USE_FEATURE_FIND_XDEV( "-xdev", )
656                 NULL
657         };
658
659         for (firstopt = 1; firstopt < argc; firstopt++) {
660                 if (argv[firstopt][0] == '-')
661                         break;
662                 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
663                         break;
664 #if ENABLE_FEATURE_FIND_PAREN
665                 if (LONE_CHAR(argv[firstopt], '('))
666                         break;
667 #endif
668         }
669         if (firstopt == 1) {
670                 argv[0] = (char*)".";
671                 argv--;
672                 firstopt++;
673         }
674
675 /* All options always return true. They always take effect
676  * rather than being processed only when their place in the
677  * expression is reached.
678  * We implement: -follow, -xdev
679  */
680         /* Process options, and replace then with -a */
681         /* (-a will be ignored by recursive parser later) */
682         argp = &argv[firstopt];
683         while ((arg = argp[0])) {
684                 i = index_in_str_array(options, arg);
685                 if (i == 0) { /* -follow */
686                         dereference = TRUE;
687                         argp[0] = (char*)"-a";
688                 }
689 #if ENABLE_FEATURE_FIND_XDEV
690                 else if (i == 1) { /* -xdev */
691                         struct stat stbuf;
692                         if (!xdev_count) {
693                                 xdev_count = firstopt - 1;
694                                 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
695                                 for (i = 1; i < firstopt; i++) {
696                                         /* not xstat(): shouldn't bomb out on
697                                          * "find not_exist exist -xdev" */
698                                         if (stat(argv[i], &stbuf))
699                                                 stbuf.st_dev = -1L;
700                                         xdev_dev[i-1] = stbuf.st_dev;
701                                 }
702                         }
703                         argp[0] = (char*)"-a";
704                 }
705 #endif
706                 argp++;
707         }
708
709         actions = parse_params(&argv[firstopt]);
710
711         for (i = 1; i < firstopt; i++) {
712                 if (!recursive_action(argv[i],
713                                 recurse_flags|(1<<dereference), /* flags */
714                                 fileAction,     /* file action */
715                                 fileAction,     /* dir action */
716                                 NULL,           /* user data */
717                                 0))             /* depth */
718                         status = EXIT_FAILURE;
719         }
720         return status;
721 }