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