blob: 386bc54addd034af7f363a7d34af9387f63cf9a7 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen17d49ef1999-10-06 20:25:32 +00002/*
3 * Mini find implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Matt Kraai096370d2001-02-07 03:52:38 +00007 * Reworked by David Douthitt <n9ubh@callsign.net> and
8 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
Eric Andersen17d49ef1999-10-06 20:25:32 +00009 *
Rob Landleye9a7a622006-09-22 02:52:41 +000010 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersen17d49ef1999-10-06 20:25:32 +000011 */
12
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000013/* 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 ]))
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000026 * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
27 * file.txt file.txt
28 * file.txt
29 * /tmp
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +000030 * # find -name '*.c' -o -name '*.h'
31 * [shows files, *.c and *.h intermixed]
Denis Vlasenko5d499e12006-10-29 19:07:01 +000032 * # 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
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +000038 *
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)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000046 */
47
Matt Kraai096370d2001-02-07 03:52:38 +000048#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000049#include "libbb.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +000050#if ENABLE_FEATURE_FIND_REGEX
51#include "xregex.h"
52#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +000053
Denis Vlasenko99912ca2007-04-10 15:43:37 +000054/* This is a NOEXEC applet. Be very careful! */
55
56
Denis Vlasenko5d499e12006-10-29 19:07:01 +000057USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
58USE_FEATURE_FIND_XDEV(static int xdev_count;)
Matt Kraai096370d2001-02-07 03:52:38 +000059
Denis Vlasenko5d499e12006-10-29 19:07:01 +000060typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
Matt Kraai096370d2001-02-07 03:52:38 +000061
Denis Vlasenko5d499e12006-10-29 19:07:01 +000062typedef struct {
63 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000064#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000065 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000066#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +000067} action;
Denis Vlasenko92258542006-11-01 10:25:35 +000068#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
69#define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
70 ACTS(print)
Denis Vlasenko434ad542007-01-27 13:45:17 +000071 ACTS(name, const char *pattern;)
Denis Vlasenkob9411292007-06-15 23:43:11 +000072USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
73USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
Denis Vlasenko92258542006-11-01 10:25:35 +000074USE_FEATURE_FIND_PRINT0(ACTS(print0))
75USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000076USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
77USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
78USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
Denis Vlasenko92258542006-11-01 10:25:35 +000079USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
80USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000081USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
Denis Vlasenko56fce002007-04-04 23:41:35 +000082USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
83USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
84USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
85USE_FEATURE_FIND_SIZE( ACTS(size, off_t size;))
86USE_FEATURE_FIND_PRUNE( ACTS(prune))
Denis Vlasenko62f04792007-04-13 10:00:12 +000087USE_FEATURE_FIND_DELETE(ACTS(delete))
Matt Kraai096370d2001-02-07 03:52:38 +000088
Denis Vlasenko5d499e12006-10-29 19:07:01 +000089static action ***actions;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000090static bool need_print = 1;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000091static int recurse_flags = ACTION_RECURSE;
Rob Landley5d3a0e82005-10-04 03:34:39 +000092
Denis Vlasenko666da5e2006-12-26 18:17:42 +000093#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000094static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000095{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000096 unsigned count = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000097 while ((str = strstr(str, "{}"))) {
98 count++;
99 str++;
100 }
101 return count;
102}
103
104
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000105static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000106{
107 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000108 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000109 /* we replace each '{}' with filename: growth by strlen-2 */
110 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
111 while ((end = strstr(src, "{}"))) {
112 memcpy(dst, src, end - src);
113 dst += end - src;
114 src = end + 2;
115 memcpy(dst, filename, flen);
116 dst += flen;
117 }
118 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000119 return buf;
120}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000121#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000122
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000123/* Return values of ACTFs ('action functions') are a bit mask:
124 * bit 1=1: prune (use SKIP constant for setting it)
125 * bit 0=1: matched successfully (TRUE)
126 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000127
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000128static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
129{
130 int cur_group;
131 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000132 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000133 action **app, *ap;
134
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000135 /* "action group" is a set of actions ANDed together.
136 * groups are ORed together.
137 * We simply evaluate each group until we find one in which all actions
138 * succeed. */
139
140 /* -prune is special: if it is encountered, then we won't
141 * descend into current directory. It doesn't matter whether
142 * action group (in which -prune sits) will succeed or not:
143 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
144 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
145 * not starting with 'f' */
146
147 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
148 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
149 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
150 * On return, bit is restored. */
151
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000152 cur_group = -1;
153 while ((app = appp[++cur_group])) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000154 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000155 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000156 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000157 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000158 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000159 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000160 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000161#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000162 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000163#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000164 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000165 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000166 }
167 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000168 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000169}
170
171
Denis Vlasenko92258542006-11-01 10:25:35 +0000172ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000173{
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000174 const char *tmp = strrchr(fileName, '/');
175 if (tmp == NULL)
176 tmp = fileName;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000177 else {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000178 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000179 if (!*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
180 tmp--;
181 while (tmp != fileName && *--tmp != '/')
182 continue;
183 if (*tmp == '/')
184 tmp++;
185 }
186 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000187 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
188}
Denis Vlasenkob9411292007-06-15 23:43:11 +0000189#if ENABLE_FEATURE_FIND_PATH
190ACTF(path)
191{
192 return fnmatch(ap->pattern, fileName, 0) == 0;
193}
194#endif
195#if ENABLE_FEATURE_FIND_REGEX
196ACTF(regex)
197{
198 regmatch_t match;
199 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
200 return 0; /* no match */
201 if (match.rm_so)
202 return 0; /* match doesn't start at pos 0 */
203 if (fileName[match.rm_eo])
204 return 0; /* match doesn't end exactly at end of pathname */
205 return 1;
206}
207#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000208#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000209ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000210{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000211 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000212}
Rob Landleycee605c2005-10-06 16:39:17 +0000213#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000214#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000215ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000216{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000217 /* -perm +mode: at least one of perm_mask bits are set */
218 if (ap->perm_char == '+')
219 return (statbuf->st_mode & ap->perm_mask) != 0;
220 /* -perm -mode: all of perm_mask are set */
221 if (ap->perm_char == '-')
222 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
223 /* -perm mode: file mode must match perm_mask */
224 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000225}
Rob Landleycee605c2005-10-06 16:39:17 +0000226#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000227#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000228ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000229{
230 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000231 time_t mtime_secs = ap->mtime_days * 24*60*60;
232 if (ap->mtime_char == '+')
233 return file_age >= mtime_secs + 24*60*60;
234 if (ap->mtime_char == '-')
235 return file_age < mtime_secs;
236 /* just numeric mtime */
237 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000238}
Rob Landleycee605c2005-10-06 16:39:17 +0000239#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000240#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000241ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000242{
243 time_t file_age = time(NULL) - statbuf->st_mtime;
244 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000245 if (ap->mmin_char == '+')
246 return file_age >= mmin_secs + 60;
247 if (ap->mmin_char == '-')
248 return file_age < mmin_secs;
249 /* just numeric mmin */
250 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000251}
Paul Fox72d1a232006-01-13 21:05:41 +0000252#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000253#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000254ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000255{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000256 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000257}
Rob Landleycee605c2005-10-06 16:39:17 +0000258#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000259#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000260ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000261{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000262 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000263}
Rob Landleycee605c2005-10-06 16:39:17 +0000264#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000265#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000266ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000267{
268 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000269 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000270 for (i = 0; i < ap->exec_argc; i++)
271 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
272 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000273
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000274 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000275 if (rc < 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000276 bb_perror_msg("%s", argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000277
278 i = 0;
279 while (argv[i])
280 free(argv[i++]);
281 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000282}
Rob Landleycee605c2005-10-06 16:39:17 +0000283#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000284#if ENABLE_FEATURE_FIND_USER
285ACTF(user)
286{
287 return (statbuf->st_uid == ap->uid);
288}
289#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000290#if ENABLE_FEATURE_FIND_GROUP
291ACTF(group)
292{
293 return (statbuf->st_gid == ap->gid);
294}
295#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000296#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000297ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000298{
299 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000300 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000301}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000302#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000303ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000304{
305 puts(fileName);
306 return TRUE;
307}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000308#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000309ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000310{
311 return exec_actions(ap->subexpr, fileName, statbuf);
312}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000313#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000314#if ENABLE_FEATURE_FIND_SIZE
315ACTF(size)
316{
317 return statbuf->st_size == ap->size;
318}
319#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000320#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000321/*
322 * -prune: if -depth is not given, return true and do not descend
323 * current dir; if -depth is given, return false with no effect.
324 * Example:
325 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
326 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000327ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000328{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000329 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000330}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000331#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000332#if ENABLE_FEATURE_FIND_DELETE
333ACTF(delete)
334{
335 int rc;
336 if (S_ISDIR(statbuf->st_mode)) {
337 rc = rmdir(fileName);
338 } else {
339 rc = unlink(fileName);
340 }
341 if (rc < 0)
342 bb_perror_msg("%s", fileName);
343 return TRUE;
344}
345#endif
346
Denis Vlasenko62f04792007-04-13 10:00:12 +0000347
Denis Vlasenkob9411292007-06-15 23:43:11 +0000348static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000349{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000350 int i;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000351#if ENABLE_FEATURE_FIND_MAXDEPTH
352 int maxdepth = (int)(ptrdiff_t)userData;
353
354 if (depth > maxdepth) return SKIP;
355#endif
356
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000357#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000358 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000359 for (i = 0; i < xdev_count; i++) {
360 if (xdev_dev[i] != statbuf->st_dev)
361 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000362 }
363 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000364#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000365 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000366 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000367 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000368 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000369 /* Cannot return 0: our caller, recursive_action(),
370 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000371 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000372}
373
Eric Andersen17d49ef1999-10-06 20:25:32 +0000374
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000375#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000376static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000377{
378 int mask = 0;
379
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000380 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000381 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000382 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000383 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000384 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000385 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000386 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000387 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000388 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000389 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000390 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000391 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000392 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000393 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000394
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000395 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000396 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000397
398 return mask;
399}
400#endif
401
Denis Vlasenko4c978632007-02-03 03:31:13 +0000402#if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
403 || ENABLE_FEATURE_FIND_MMIN
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000404static const char* plus_minus_num(const char* str)
405{
406 if (*str == '-' || *str == '+')
407 str++;
408 return str;
409}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000410#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000411
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000412static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000413{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000414 enum {
415 PARM_a ,
416 PARM_o ,
417 USE_FEATURE_FIND_NOT( PARM_char_not ,)
418 PARM_print ,
419 USE_FEATURE_FIND_PRINT0(PARM_print0 ,)
420 PARM_name ,
Denis Vlasenkob9411292007-06-15 23:43:11 +0000421 USE_FEATURE_FIND_PATH( PARM_path ,)
422 USE_FEATURE_FIND_REGEX( PARM_regex ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000423 USE_FEATURE_FIND_TYPE( PARM_type ,)
424 USE_FEATURE_FIND_PERM( PARM_perm ,)
425 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
426 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
427 USE_FEATURE_FIND_NEWER( PARM_newer ,)
428 USE_FEATURE_FIND_INUM( PARM_inum ,)
429 USE_FEATURE_FIND_EXEC( PARM_exec ,)
430 USE_FEATURE_FIND_USER( PARM_user ,)
431 USE_FEATURE_FIND_GROUP( PARM_group ,)
432 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
433 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000434 USE_FEATURE_FIND_SIZE( PARM_size ,)
435 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
Denis Vlasenko62f04792007-04-13 10:00:12 +0000436 USE_FEATURE_FIND_DELETE(PARM_delete ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000437#if ENABLE_DESKTOP
438 PARM_and ,
439 PARM_or ,
440 USE_FEATURE_FIND_NOT( PARM_not ,)
441#endif
442 };
443
444 static const char *const params[] = {
445 "-a" ,
446 "-o" ,
447 USE_FEATURE_FIND_NOT( "!" ,)
448 "-print" ,
449 USE_FEATURE_FIND_PRINT0("-print0",)
450 "-name" ,
Denis Vlasenkob9411292007-06-15 23:43:11 +0000451 USE_FEATURE_FIND_PATH( "-path" ,)
452 USE_FEATURE_FIND_REGEX( "-regex" ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000453 USE_FEATURE_FIND_TYPE( "-type" ,)
454 USE_FEATURE_FIND_PERM( "-perm" ,)
455 USE_FEATURE_FIND_MTIME( "-mtime" ,)
456 USE_FEATURE_FIND_MMIN( "-mmin" ,)
457 USE_FEATURE_FIND_NEWER( "-newer" ,)
458 USE_FEATURE_FIND_INUM( "-inum" ,)
459 USE_FEATURE_FIND_EXEC( "-exec" ,)
460 USE_FEATURE_FIND_USER( "-user" ,)
461 USE_FEATURE_FIND_GROUP( "-group" ,)
462 USE_FEATURE_FIND_DEPTH( "-depth" ,)
463 USE_FEATURE_FIND_PAREN( "(" ,)
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000464 USE_FEATURE_FIND_SIZE( "-size" ,)
465 USE_FEATURE_FIND_PRUNE( "-prune" ,)
Denis Vlasenko62f04792007-04-13 10:00:12 +0000466 USE_FEATURE_FIND_DELETE("-delete",)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000467#if ENABLE_DESKTOP
468 "-and" ,
469 "-or" ,
470 USE_FEATURE_FIND_NOT( "-not" ,)
471#endif
472 NULL
473 };
474
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000475 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000476 unsigned cur_group = 0;
477 unsigned cur_action = 0;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000478 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000479
480 /* 'static' doesn't work here! (gcc 4.1.2) */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000481 action* alloc_action(int sizeof_struct, action_fp f)
482 {
483 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000484 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
485 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
486 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000487 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000488 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000489 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000490 return ap;
491 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000492
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000493#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000494
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000495 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000496
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000497/* Actions have side effects and return a true or false value
498 * We implement: -print, -print0, -exec
499 *
500 * The rest are tests.
501 *
502 * Tests and actions are grouped by operators
503 * ( expr ) Force precedence
504 * ! expr True if expr is false
505 * -not expr Same as ! expr
506 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
507 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
508 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
509 * We implement: (), -a, -o
510 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000511 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000512 const char *arg = argv[0];
513 const char *arg1 = argv[1];
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000514 int parm = index_in_str_array(params, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000515 /* --- Operators --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000516 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000517 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000518 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000519 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000520 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000521 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000522 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000523 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000524 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000525 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000526 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000527#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko56fce002007-04-04 23:41:35 +0000528 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000529 /* also handles "find ! ! -name 'foo*'" */
530 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000531 }
532#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000533
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000534 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000535 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000536 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000537 /* GNU find ignores '!' here: "find ! -print" */
538 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000539 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000540 }
541#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000542 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000543 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000544 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000545 (void) ALLOC_ACTION(print0);
546 }
Paul Foxd7384292006-05-12 14:47:20 +0000547#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000548 else if (parm == PARM_name) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000549 action_name *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000550 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000551 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000552 ap = ALLOC_ACTION(name);
553 ap->pattern = arg1;
554 }
Denis Vlasenkob9411292007-06-15 23:43:11 +0000555#if ENABLE_FEATURE_FIND_PATH
556 else if (parm == PARM_path) {
557 action_path *ap;
558 if (!*++argv)
559 bb_error_msg_and_die(bb_msg_requires_arg, arg);
560 ap = ALLOC_ACTION(path);
561 ap->pattern = arg1;
562 }
563#endif
564#if ENABLE_FEATURE_FIND_REGEX
565 else if (parm == PARM_regex) {
566 action_regex *ap;
567 if (!*++argv)
568 bb_error_msg_and_die(bb_msg_requires_arg, arg);
569 ap = ALLOC_ACTION(regex);
570 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
571 }
572#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000573#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko56fce002007-04-04 23:41:35 +0000574 else if (parm == PARM_type) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000575 action_type *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000576 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000577 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000578 ap = ALLOC_ACTION(type);
579 ap->type_mask = find_type(arg1);
580 }
Rob Landleycee605c2005-10-06 16:39:17 +0000581#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000582#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000583/* -perm mode File's permission bits are exactly mode (octal or symbolic).
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000584 * Symbolic modes use mode 0 as a point of departure.
585 * -perm -mode All of the permission bits mode are set for the file.
586 * -perm +mode Any of the permission bits mode are set for the file.
587 */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000588 else if (parm == PARM_perm) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000589 action_perm *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000590 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000591 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000592 ap = ALLOC_ACTION(perm);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000593 ap->perm_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000594 arg1 = plus_minus_num(arg1);
595 ap->perm_mask = 0;
596 if (!bb_parse_mode(arg1, &ap->perm_mask))
597 bb_error_msg_and_die("invalid mode: %s", arg1);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000598 }
Rob Landleycee605c2005-10-06 16:39:17 +0000599#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000600#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko56fce002007-04-04 23:41:35 +0000601 else if (parm == PARM_mtime) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000602 action_mtime *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000603 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000604 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000605 ap = ALLOC_ACTION(mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000606 ap->mtime_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000607 ap->mtime_days = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000608 }
Rob Landleycee605c2005-10-06 16:39:17 +0000609#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000610#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko56fce002007-04-04 23:41:35 +0000611 else if (parm == PARM_mmin) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000612 action_mmin *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000613 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000614 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000615 ap = ALLOC_ACTION(mmin);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000616 ap->mmin_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000617 ap->mmin_mins = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000618 }
Rob Landleycee605c2005-10-06 16:39:17 +0000619#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000620#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko56fce002007-04-04 23:41:35 +0000621 else if (parm == PARM_newer) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000622 action_newer *ap;
Eric Andersen97d86f22003-01-23 05:27:42 +0000623 struct stat stat_newer;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000624 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000625 bb_error_msg_and_die(bb_msg_requires_arg, arg);
626 xstat(arg1, &stat_newer);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000627 ap = ALLOC_ACTION(newer);
628 ap->newer_mtime = stat_newer.st_mtime;
629 }
Rob Landleycee605c2005-10-06 16:39:17 +0000630#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000631#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko56fce002007-04-04 23:41:35 +0000632 else if (parm == PARM_inum) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000633 action_inum *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000634 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000635 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000636 ap = ALLOC_ACTION(inum);
637 ap->inode_num = xatoul(arg1);
638 }
Rob Landleycee605c2005-10-06 16:39:17 +0000639#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000640#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000641 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000642 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000643 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000644 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000645 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000646 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000647 ap->exec_argv = ++argv; /* first arg after -exec */
648 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000649 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000650 if (!*argv) /* did not see ';' until end */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000651 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000652 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000653 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000654 argv++;
655 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000656 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000657 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000658 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000659 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000660 i = ap->exec_argc;
661 while (i--)
662 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
663 }
664#endif
Denis Vlasenko4c978632007-02-03 03:31:13 +0000665#if ENABLE_FEATURE_FIND_USER
Denis Vlasenko56fce002007-04-04 23:41:35 +0000666 else if (parm == PARM_user) {
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000667 action_user *ap;
668 if (!*++argv)
669 bb_error_msg_and_die(bb_msg_requires_arg, arg);
670 ap = ALLOC_ACTION(user);
671 ap->uid = bb_strtou(arg1, NULL, 10);
672 if (errno)
673 ap->uid = xuname2uid(arg1);
674 }
675#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000676#if ENABLE_FEATURE_FIND_GROUP
677 else if (parm == PARM_group) {
678 action_group *ap;
679 if (!*++argv)
680 bb_error_msg_and_die(bb_msg_requires_arg, arg);
681 ap = ALLOC_ACTION(group);
682 ap->gid = bb_strtou(arg1, NULL, 10);
683 if (errno)
684 ap->gid = xgroup2gid(arg1);
685 }
686#endif
687#if ENABLE_FEATURE_FIND_DEPTH
688 else if (parm == PARM_depth) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000689 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000690 }
691#endif
692#if ENABLE_FEATURE_FIND_PAREN
693 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000694 action_paren *ap;
695 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000696 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000697
698 endarg = argv;
699 while (1) {
700 if (!*++endarg)
701 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000702 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000703 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000704 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000705 *endarg = NULL;
706 break;
707 }
708 }
709 ap = ALLOC_ACTION(paren);
710 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000711 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000712 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000713 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000714#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000715#if ENABLE_FEATURE_FIND_SIZE
716 else if (parm == PARM_size) {
717 action_size *ap;
718 if (!*++argv)
719 bb_error_msg_and_die(bb_msg_requires_arg, arg);
720 ap = ALLOC_ACTION(size);
721 ap->size = XATOOFF(arg1);
722 }
723#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000724#if ENABLE_FEATURE_FIND_PRUNE
725 else if (parm == PARM_prune) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000726 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000727 (void) ALLOC_ACTION(prune);
728 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000729#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000730#if ENABLE_FEATURE_FIND_DELETE
731 else if (parm == PARM_delete) {
732 need_print = 0;
733 recurse_flags |= ACTION_DEPTHFIRST;
734 (void) ALLOC_ACTION(delete);
735 }
736#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000737 else {
738 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000739 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000740 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000741 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000742 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000743 return appp;
744#undef ALLOC_ACTION
745}
746
747
Denis Vlasenko06af2162007-02-03 17:28:39 +0000748int find_main(int argc, char **argv);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000749int find_main(int argc, char **argv)
750{
Denis Vlasenkob9411292007-06-15 23:43:11 +0000751 static const char *const options[] = {
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000752 "-follow",
Denis Vlasenkob9411292007-06-15 23:43:11 +0000753USE_FEATURE_FIND_XDEV( "-xdev" ,)
754USE_FEATURE_FIND_MAXDEPTH("-maxdepth",)
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000755 NULL
756 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000757 enum {
758 OPT_FOLLOW,
759USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
760USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
761 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000762
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000763 char *arg;
764 char **argp;
765 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000766#if ENABLE_FEATURE_FIND_MAXDEPTH
767 int maxdepth = INT_MAX;
768#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000769
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000770 for (firstopt = 1; firstopt < argc; firstopt++) {
771 if (argv[firstopt][0] == '-')
772 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000773 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
774 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000775#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000776 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000777 break;
778#endif
779 }
780 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000781 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000782 argv--;
783 firstopt++;
784 }
785
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000786/* All options always return true. They always take effect
787 * rather than being processed only when their place in the
788 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000789 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000790 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000791 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000792 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000793 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000794 while ((arg = argp[0])) {
Denis Vlasenkob9411292007-06-15 23:43:11 +0000795 int opt = index_in_str_array(options, arg);
796 if (opt == OPT_FOLLOW) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000797 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000798 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000799 }
800#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000801 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000802 struct stat stbuf;
803 if (!xdev_count) {
804 xdev_count = firstopt - 1;
805 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
806 for (i = 1; i < firstopt; i++) {
807 /* not xstat(): shouldn't bomb out on
808 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000809 if (stat(argv[i], &stbuf))
810 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000811 xdev_dev[i-1] = stbuf.st_dev;
812 }
813 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000814 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000815 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000816#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000817#if ENABLE_FEATURE_FIND_MAXDEPTH
818 if (opt == OPT_MAXDEPTH) {
819 if (!argp[1])
820 bb_show_usage();
821 maxdepth = xatoi_u(argp[1]);
822 argp[0] = (char*)"-a";
823 argp[1] = (char*)"-a";
824 argp++;
825 }
826#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000827 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000828 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000829
830 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000831
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000832 for (i = 1; i < firstopt; i++) {
833 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000834 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000835 fileAction, /* file action */
836 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000837#if ENABLE_FEATURE_FIND_MAXDEPTH
838 (void*)maxdepth,/* user data */
839#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000840 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000841#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000842 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000843 status = EXIT_FAILURE;
844 }
Matt Kraai096370d2001-02-07 03:52:38 +0000845 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000846}