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