blob: aec22a5bc7f215e8d79ee959e317707681d226ba [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 Vlasenkofc7f9222007-01-26 23:00:05 +000049#include "busybox.h"
Eric Andersen17d49ef1999-10-06 20:25:32 +000050
Denis Vlasenko99912ca2007-04-10 15:43:37 +000051/* This is a NOEXEC applet. Be very careful! */
52
53
Denis Vlasenko5d499e12006-10-29 19:07:01 +000054USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
55USE_FEATURE_FIND_XDEV(static int xdev_count;)
Matt Kraai096370d2001-02-07 03:52:38 +000056
Denis Vlasenko5d499e12006-10-29 19:07:01 +000057typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
Matt Kraai096370d2001-02-07 03:52:38 +000058
Denis Vlasenko5d499e12006-10-29 19:07:01 +000059typedef struct {
60 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000061#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000062 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000063#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +000064} action;
Denis Vlasenko92258542006-11-01 10:25:35 +000065#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)
Denis Vlasenko434ad542007-01-27 13:45:17 +000068 ACTS(name, const char *pattern;)
Denis Vlasenko92258542006-11-01 10:25:35 +000069USE_FEATURE_FIND_PRINT0(ACTS(print0))
70USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000071USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
72USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
73USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
Denis Vlasenko92258542006-11-01 10:25:35 +000074USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
75USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000076USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
Denis Vlasenko56fce002007-04-04 23:41:35 +000077USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
78USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
79USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
80USE_FEATURE_FIND_SIZE( ACTS(size, off_t size;))
81USE_FEATURE_FIND_PRUNE( ACTS(prune))
Denis Vlasenko62f04792007-04-13 10:00:12 +000082USE_FEATURE_FIND_DELETE(ACTS(delete))
83USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
Matt Kraai096370d2001-02-07 03:52:38 +000084
Denis Vlasenko5d499e12006-10-29 19:07:01 +000085static action ***actions;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000086static bool need_print = 1;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000087static int recurse_flags = ACTION_RECURSE;
Rob Landley5d3a0e82005-10-04 03:34:39 +000088
Denis Vlasenko666da5e2006-12-26 18:17:42 +000089#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000090static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000091{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000092 unsigned count = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000093 while ((str = strstr(str, "{}"))) {
94 count++;
95 str++;
96 }
97 return count;
98}
99
100
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000101static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000102{
103 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000104 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000105 /* we replace each '{}' with filename: growth by strlen-2 */
106 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
107 while ((end = strstr(src, "{}"))) {
108 memcpy(dst, src, end - src);
109 dst += end - src;
110 src = end + 2;
111 memcpy(dst, filename, flen);
112 dst += flen;
113 }
114 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000115 return buf;
116}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000117#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000118
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000119/* Return values of ACTFs ('action functions') are a bit mask:
120 * bit 1=1: prune (use SKIP constant for setting it)
121 * bit 0=1: matched successfully (TRUE)
122 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000123
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000124static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
125{
126 int cur_group;
127 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000128 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000129 action **app, *ap;
130
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000131 /* "action group" is a set of actions ANDed together.
132 * groups are ORed together.
133 * We simply evaluate each group until we find one in which all actions
134 * succeed. */
135
136 /* -prune is special: if it is encountered, then we won't
137 * descend into current directory. It doesn't matter whether
138 * action group (in which -prune sits) will succeed or not:
139 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
140 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
141 * not starting with 'f' */
142
143 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
144 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
145 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
146 * On return, bit is restored. */
147
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000148 cur_group = -1;
149 while ((app = appp[++cur_group])) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000150 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000151 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000152 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000153 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000154 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000155 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000156 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000157#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000158 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000159#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000160 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000161 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000162 }
163 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000164 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000165}
166
167
Denis Vlasenko92258542006-11-01 10:25:35 +0000168ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000169{
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000170 const char *tmp = strrchr(fileName, '/');
171 if (tmp == NULL)
172 tmp = fileName;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000173 else {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000174 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000175 if (!*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
176 tmp--;
177 while (tmp != fileName && *--tmp != '/')
178 continue;
179 if (*tmp == '/')
180 tmp++;
181 }
182 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000183 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
184}
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000185#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000186ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000187{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000188 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000189}
Rob Landleycee605c2005-10-06 16:39:17 +0000190#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000191#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000192ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000193{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000194 /* -perm +mode: at least one of perm_mask bits are set */
195 if (ap->perm_char == '+')
196 return (statbuf->st_mode & ap->perm_mask) != 0;
197 /* -perm -mode: all of perm_mask are set */
198 if (ap->perm_char == '-')
199 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
200 /* -perm mode: file mode must match perm_mask */
201 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000202}
Rob Landleycee605c2005-10-06 16:39:17 +0000203#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000204#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000205ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000206{
207 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000208 time_t mtime_secs = ap->mtime_days * 24*60*60;
209 if (ap->mtime_char == '+')
210 return file_age >= mtime_secs + 24*60*60;
211 if (ap->mtime_char == '-')
212 return file_age < mtime_secs;
213 /* just numeric mtime */
214 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000215}
Rob Landleycee605c2005-10-06 16:39:17 +0000216#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000217#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000218ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000219{
220 time_t file_age = time(NULL) - statbuf->st_mtime;
221 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000222 if (ap->mmin_char == '+')
223 return file_age >= mmin_secs + 60;
224 if (ap->mmin_char == '-')
225 return file_age < mmin_secs;
226 /* just numeric mmin */
227 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000228}
Paul Fox72d1a232006-01-13 21:05:41 +0000229#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000230#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000231ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000232{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000233 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000234}
Rob Landleycee605c2005-10-06 16:39:17 +0000235#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000236#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000237ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000238{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000239 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000240}
Rob Landleycee605c2005-10-06 16:39:17 +0000241#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000242#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000243ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000244{
245 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000246 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000247 for (i = 0; i < ap->exec_argc; i++)
248 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
249 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000250
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000251 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000252 if (rc < 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000253 bb_perror_msg("%s", argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000254
255 i = 0;
256 while (argv[i])
257 free(argv[i++]);
258 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000259}
Rob Landleycee605c2005-10-06 16:39:17 +0000260#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000261
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000262#if ENABLE_FEATURE_FIND_USER
263ACTF(user)
264{
265 return (statbuf->st_uid == ap->uid);
266}
267#endif
268
Denis Vlasenko56fce002007-04-04 23:41:35 +0000269#if ENABLE_FEATURE_FIND_GROUP
270ACTF(group)
271{
272 return (statbuf->st_gid == ap->gid);
273}
274#endif
275
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000276#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000277ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000278{
279 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000280 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000281}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000282#endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000283
Denis Vlasenko92258542006-11-01 10:25:35 +0000284ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000285{
286 puts(fileName);
287 return TRUE;
288}
289
Denis Vlasenko56fce002007-04-04 23:41:35 +0000290#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000291ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000292{
293 return exec_actions(ap->subexpr, fileName, statbuf);
294}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000295#endif
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000296
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000297#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000298/*
299 * -prune: if -depth is not given, return true and do not descend
300 * current dir; if -depth is given, return false with no effect.
301 * Example:
302 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
303 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000304ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000305{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000306 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000307}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000308#endif
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000309
Denis Vlasenko62f04792007-04-13 10:00:12 +0000310#if ENABLE_FEATURE_FIND_DELETE
311ACTF(delete)
312{
313 int rc;
314 if (S_ISDIR(statbuf->st_mode)) {
315 rc = rmdir(fileName);
316 } else {
317 rc = unlink(fileName);
318 }
319 if (rc < 0)
320 bb_perror_msg("%s", fileName);
321 return TRUE;
322}
323#endif
324
325#if ENABLE_FEATURE_FIND_PATH
326ACTF(path)
327{
328 return fnmatch(ap->pattern, fileName, 0) == 0;
329}
330#endif
331
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000332#if ENABLE_FEATURE_FIND_SIZE
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000333ACTF(size)
334{
335 return statbuf->st_size == ap->size;
336}
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000337#endif
338
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000339
340static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
341{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000342 int i;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000343#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000344 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000345 for (i = 0; i < xdev_count; i++) {
346 if (xdev_dev[i] != statbuf->st_dev)
347 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000348 }
349 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000350#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000351 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000352 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000353 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000354 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000355 /* Cannot return 0: our caller, recursive_action(),
356 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000357 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000358}
359
Eric Andersen17d49ef1999-10-06 20:25:32 +0000360
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000361#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000362static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000363{
364 int mask = 0;
365
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000366 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000367 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000368 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000369 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000370 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000371 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000372 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000373 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000374 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000375 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000376 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000377 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000378 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000379 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000380
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000381 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000382 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000383
384 return mask;
385}
386#endif
387
Denis Vlasenko4c978632007-02-03 03:31:13 +0000388#if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
389 || ENABLE_FEATURE_FIND_MMIN
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000390static const char* plus_minus_num(const char* str)
391{
392 if (*str == '-' || *str == '+')
393 str++;
394 return str;
395}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000396#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000397
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000398static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000399{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000400 enum {
401 PARM_a ,
402 PARM_o ,
403 USE_FEATURE_FIND_NOT( PARM_char_not ,)
404 PARM_print ,
405 USE_FEATURE_FIND_PRINT0(PARM_print0 ,)
406 PARM_name ,
407 USE_FEATURE_FIND_TYPE( PARM_type ,)
408 USE_FEATURE_FIND_PERM( PARM_perm ,)
409 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
410 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
411 USE_FEATURE_FIND_NEWER( PARM_newer ,)
412 USE_FEATURE_FIND_INUM( PARM_inum ,)
413 USE_FEATURE_FIND_EXEC( PARM_exec ,)
414 USE_FEATURE_FIND_USER( PARM_user ,)
415 USE_FEATURE_FIND_GROUP( PARM_group ,)
416 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
417 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000418 USE_FEATURE_FIND_SIZE( PARM_size ,)
419 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
Denis Vlasenko62f04792007-04-13 10:00:12 +0000420 USE_FEATURE_FIND_DELETE(PARM_delete ,)
421 USE_FEATURE_FIND_PATH( PARM_path ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000422#if ENABLE_DESKTOP
423 PARM_and ,
424 PARM_or ,
425 USE_FEATURE_FIND_NOT( PARM_not ,)
426#endif
427 };
428
429 static const char *const params[] = {
430 "-a" ,
431 "-o" ,
432 USE_FEATURE_FIND_NOT( "!" ,)
433 "-print" ,
434 USE_FEATURE_FIND_PRINT0("-print0",)
435 "-name" ,
436 USE_FEATURE_FIND_TYPE( "-type" ,)
437 USE_FEATURE_FIND_PERM( "-perm" ,)
438 USE_FEATURE_FIND_MTIME( "-mtime" ,)
439 USE_FEATURE_FIND_MMIN( "-mmin" ,)
440 USE_FEATURE_FIND_NEWER( "-newer" ,)
441 USE_FEATURE_FIND_INUM( "-inum" ,)
442 USE_FEATURE_FIND_EXEC( "-exec" ,)
443 USE_FEATURE_FIND_USER( "-user" ,)
444 USE_FEATURE_FIND_GROUP( "-group" ,)
445 USE_FEATURE_FIND_DEPTH( "-depth" ,)
446 USE_FEATURE_FIND_PAREN( "(" ,)
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000447 USE_FEATURE_FIND_SIZE( "-size" ,)
448 USE_FEATURE_FIND_PRUNE( "-prune" ,)
Denis Vlasenko62f04792007-04-13 10:00:12 +0000449 USE_FEATURE_FIND_DELETE("-delete",)
450 USE_FEATURE_FIND_PATH( "-path" ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000451#if ENABLE_DESKTOP
452 "-and" ,
453 "-or" ,
454 USE_FEATURE_FIND_NOT( "-not" ,)
455#endif
456 NULL
457 };
458
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000459 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000460 unsigned cur_group = 0;
461 unsigned cur_action = 0;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000462 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000463
464 /* 'static' doesn't work here! (gcc 4.1.2) */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000465 action* alloc_action(int sizeof_struct, action_fp f)
466 {
467 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000468 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
469 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
470 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000471 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000472 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000473 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000474 return ap;
475 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000476
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000477#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000478
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000479 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000480
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000481/* Actions have side effects and return a true or false value
482 * We implement: -print, -print0, -exec
483 *
484 * The rest are tests.
485 *
486 * Tests and actions are grouped by operators
487 * ( expr ) Force precedence
488 * ! expr True if expr is false
489 * -not expr Same as ! expr
490 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
491 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
492 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
493 * We implement: (), -a, -o
494 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000495 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000496 const char *arg = argv[0];
497 const char *arg1 = argv[1];
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000498 int parm = index_in_str_array(params, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000499 /* --- Operators --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000500 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000501 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000502 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000503 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000504 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000505 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000506 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000507 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000508 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000509 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000510 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000511#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko56fce002007-04-04 23:41:35 +0000512 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000513 /* also handles "find ! ! -name 'foo*'" */
514 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000515 }
516#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000517
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000518 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000519 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000520 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000521 /* GNU find ignores '!' here: "find ! -print" */
522 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000523 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000524 }
525#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000526 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000527 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000528 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000529 (void) ALLOC_ACTION(print0);
530 }
Paul Foxd7384292006-05-12 14:47:20 +0000531#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000532 else if (parm == PARM_name) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000533 action_name *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000534 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000535 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000536 ap = ALLOC_ACTION(name);
537 ap->pattern = arg1;
538 }
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000539#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko56fce002007-04-04 23:41:35 +0000540 else if (parm == PARM_type) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000541 action_type *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000542 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000543 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000544 ap = ALLOC_ACTION(type);
545 ap->type_mask = find_type(arg1);
546 }
Rob Landleycee605c2005-10-06 16:39:17 +0000547#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000548#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000549/* -perm mode File's permission bits are exactly mode (octal or symbolic).
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000550 * Symbolic modes use mode 0 as a point of departure.
551 * -perm -mode All of the permission bits mode are set for the file.
552 * -perm +mode Any of the permission bits mode are set for the file.
553 */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000554 else if (parm == PARM_perm) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000555 action_perm *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000556 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000557 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000558 ap = ALLOC_ACTION(perm);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000559 ap->perm_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000560 arg1 = plus_minus_num(arg1);
561 ap->perm_mask = 0;
562 if (!bb_parse_mode(arg1, &ap->perm_mask))
563 bb_error_msg_and_die("invalid mode: %s", arg1);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000564 }
Rob Landleycee605c2005-10-06 16:39:17 +0000565#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000566#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko56fce002007-04-04 23:41:35 +0000567 else if (parm == PARM_mtime) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000568 action_mtime *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000569 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000570 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000571 ap = ALLOC_ACTION(mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000572 ap->mtime_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000573 ap->mtime_days = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000574 }
Rob Landleycee605c2005-10-06 16:39:17 +0000575#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000576#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko56fce002007-04-04 23:41:35 +0000577 else if (parm == PARM_mmin) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000578 action_mmin *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000579 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000580 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000581 ap = ALLOC_ACTION(mmin);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000582 ap->mmin_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000583 ap->mmin_mins = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000584 }
Rob Landleycee605c2005-10-06 16:39:17 +0000585#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000586#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko56fce002007-04-04 23:41:35 +0000587 else if (parm == PARM_newer) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000588 action_newer *ap;
Eric Andersen97d86f22003-01-23 05:27:42 +0000589 struct stat stat_newer;
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);
592 xstat(arg1, &stat_newer);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000593 ap = ALLOC_ACTION(newer);
594 ap->newer_mtime = stat_newer.st_mtime;
595 }
Rob Landleycee605c2005-10-06 16:39:17 +0000596#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000597#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko56fce002007-04-04 23:41:35 +0000598 else if (parm == PARM_inum) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000599 action_inum *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000600 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000601 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000602 ap = ALLOC_ACTION(inum);
603 ap->inode_num = xatoul(arg1);
604 }
Rob Landleycee605c2005-10-06 16:39:17 +0000605#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000606#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000607 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000608 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000609 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000610 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000611 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000612 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000613 ap->exec_argv = ++argv; /* first arg after -exec */
614 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000615 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000616 if (!*argv) /* did not see ';' until end */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000617 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000618 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000619 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000620 argv++;
621 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000622 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000623 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000624 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000625 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000626 i = ap->exec_argc;
627 while (i--)
628 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
629 }
630#endif
Denis Vlasenko4c978632007-02-03 03:31:13 +0000631#if ENABLE_FEATURE_FIND_USER
Denis Vlasenko56fce002007-04-04 23:41:35 +0000632 else if (parm == PARM_user) {
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000633 action_user *ap;
634 if (!*++argv)
635 bb_error_msg_and_die(bb_msg_requires_arg, arg);
636 ap = ALLOC_ACTION(user);
637 ap->uid = bb_strtou(arg1, NULL, 10);
638 if (errno)
639 ap->uid = xuname2uid(arg1);
640 }
641#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000642#if ENABLE_FEATURE_FIND_GROUP
643 else if (parm == PARM_group) {
644 action_group *ap;
645 if (!*++argv)
646 bb_error_msg_and_die(bb_msg_requires_arg, arg);
647 ap = ALLOC_ACTION(group);
648 ap->gid = bb_strtou(arg1, NULL, 10);
649 if (errno)
650 ap->gid = xgroup2gid(arg1);
651 }
652#endif
653#if ENABLE_FEATURE_FIND_DEPTH
654 else if (parm == PARM_depth) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000655 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000656 }
657#endif
658#if ENABLE_FEATURE_FIND_PAREN
659 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000660 action_paren *ap;
661 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000662 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000663
664 endarg = argv;
665 while (1) {
666 if (!*++endarg)
667 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000668 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000669 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000670 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000671 *endarg = NULL;
672 break;
673 }
674 }
675 ap = ALLOC_ACTION(paren);
676 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000677 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000678 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000679 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000680#endif
681#if ENABLE_FEATURE_FIND_PRUNE
682 else if (parm == PARM_prune) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000683 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000684 (void) ALLOC_ACTION(prune);
685 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000686#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000687#if ENABLE_FEATURE_FIND_DELETE
688 else if (parm == PARM_delete) {
689 need_print = 0;
690 recurse_flags |= ACTION_DEPTHFIRST;
691 (void) ALLOC_ACTION(delete);
692 }
693#endif
694#if ENABLE_FEATURE_FIND_PATH
695 else if (parm == PARM_path) {
696 action_path *ap;
697 if (!*++argv)
698 bb_error_msg_and_die(bb_msg_requires_arg, arg);
699 ap = ALLOC_ACTION(path);
700 ap->pattern = arg1;
701 }
702#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000703#if ENABLE_FEATURE_FIND_SIZE
704 else if (parm == PARM_size) {
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000705 action_size *ap;
706 if (!*++argv)
707 bb_error_msg_and_die(bb_msg_requires_arg, arg);
708 ap = ALLOC_ACTION(size);
709 ap->size = XATOOFF(arg1);
710 }
Rob Landleycee605c2005-10-06 16:39:17 +0000711#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000712 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000713 bb_show_usage();
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000714 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000715 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000716 return appp;
717#undef ALLOC_ACTION
718}
719
720
Denis Vlasenko06af2162007-02-03 17:28:39 +0000721int find_main(int argc, char **argv);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000722int find_main(int argc, char **argv)
723{
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000724 static const char * const options[] = {
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000725 "-follow",
726USE_FEATURE_FIND_XDEV( "-xdev", )
727 NULL
728 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000729
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000730 char *arg;
731 char **argp;
732 int i, firstopt, status = EXIT_SUCCESS;
733
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000734 for (firstopt = 1; firstopt < argc; firstopt++) {
735 if (argv[firstopt][0] == '-')
736 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000737 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
738 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000739#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000740 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000741 break;
742#endif
743 }
744 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000745 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000746 argv--;
747 firstopt++;
748 }
749
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000750/* All options always return true. They always take effect
751 * rather than being processed only when their place in the
752 * expression is reached.
753 * We implement: -follow, -xdev
754 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000755 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000756 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000757 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000758 while ((arg = argp[0])) {
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000759 i = index_in_str_array(options, arg);
760 if (i == 0) { /* -follow */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000761 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000762 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000763 }
764#if ENABLE_FEATURE_FIND_XDEV
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000765 else if (i == 1) { /* -xdev */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000766 struct stat stbuf;
767 if (!xdev_count) {
768 xdev_count = firstopt - 1;
769 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
770 for (i = 1; i < firstopt; i++) {
771 /* not xstat(): shouldn't bomb out on
772 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000773 if (stat(argv[i], &stbuf))
774 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000775 xdev_dev[i-1] = stbuf.st_dev;
776 }
777 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000778 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000779 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000780#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000781 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000782 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000783
784 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000785
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000786 for (i = 1; i < firstopt; i++) {
787 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000788 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000789 fileAction, /* file action */
790 fileAction, /* dir action */
791 NULL, /* user data */
792 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000793 status = EXIT_FAILURE;
794 }
Matt Kraai096370d2001-02-07 03:52:38 +0000795 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000796}