blob: 036d13f4a911243e017426ce32ac64e1422c0b2a [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
Denis Vlasenko53a0e972007-06-16 00:30:52 +000048/* Testing script
49 * ./busybox find "$@" | tee /tmp/bb_find
50 * echo ==================
51 * /path/to/gnu/find "$@" | tee /tmp/std_find
52 * echo ==================
53 * diff -u /tmp/std_find /tmp/bb_find && echo Identical
54 */
55
Matt Kraai096370d2001-02-07 03:52:38 +000056#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000057#include "libbb.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +000058#if ENABLE_FEATURE_FIND_REGEX
59#include "xregex.h"
60#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +000061
Denis Vlasenko99912ca2007-04-10 15:43:37 +000062/* This is a NOEXEC applet. Be very careful! */
63
64
Denis Vlasenko5d499e12006-10-29 19:07:01 +000065USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
66USE_FEATURE_FIND_XDEV(static int xdev_count;)
Matt Kraai096370d2001-02-07 03:52:38 +000067
Denis Vlasenko5d499e12006-10-29 19:07:01 +000068typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
Matt Kraai096370d2001-02-07 03:52:38 +000069
Denis Vlasenko5d499e12006-10-29 19:07:01 +000070typedef struct {
71 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000072#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000073 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000074#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +000075} action;
Denis Vlasenko92258542006-11-01 10:25:35 +000076#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
77#define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
78 ACTS(print)
Denis Vlasenko434ad542007-01-27 13:45:17 +000079 ACTS(name, const char *pattern;)
Denis Vlasenkob9411292007-06-15 23:43:11 +000080USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
81USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
Denis Vlasenko92258542006-11-01 10:25:35 +000082USE_FEATURE_FIND_PRINT0(ACTS(print0))
83USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000084USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
85USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
86USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
Denis Vlasenko92258542006-11-01 10:25:35 +000087USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
88USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000089USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
Denis Vlasenko56fce002007-04-04 23:41:35 +000090USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
91USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
92USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
Denis Vlasenko53a0e972007-06-16 00:30:52 +000093USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
Denis Vlasenko56fce002007-04-04 23:41:35 +000094USE_FEATURE_FIND_PRUNE( ACTS(prune))
Denis Vlasenko62f04792007-04-13 10:00:12 +000095USE_FEATURE_FIND_DELETE(ACTS(delete))
Matt Kraai096370d2001-02-07 03:52:38 +000096
Denis Vlasenko5d499e12006-10-29 19:07:01 +000097static action ***actions;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000098static bool need_print = 1;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +000099static int recurse_flags = ACTION_RECURSE;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000100
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000101#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000102static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000103{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000104 unsigned count = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000105 while ((str = strstr(str, "{}"))) {
106 count++;
107 str++;
108 }
109 return count;
110}
111
112
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000113static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000114{
115 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000116 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000117 /* we replace each '{}' with filename: growth by strlen-2 */
118 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
119 while ((end = strstr(src, "{}"))) {
120 memcpy(dst, src, end - src);
121 dst += end - src;
122 src = end + 2;
123 memcpy(dst, filename, flen);
124 dst += flen;
125 }
126 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000127 return buf;
128}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000129#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000130
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000131/* Return values of ACTFs ('action functions') are a bit mask:
132 * bit 1=1: prune (use SKIP constant for setting it)
133 * bit 0=1: matched successfully (TRUE)
134 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000135
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000136static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
137{
138 int cur_group;
139 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000140 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000141 action **app, *ap;
142
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000143 /* "action group" is a set of actions ANDed together.
144 * groups are ORed together.
145 * We simply evaluate each group until we find one in which all actions
146 * succeed. */
147
148 /* -prune is special: if it is encountered, then we won't
149 * descend into current directory. It doesn't matter whether
150 * action group (in which -prune sits) will succeed or not:
151 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
152 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
153 * not starting with 'f' */
154
155 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
156 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
157 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
158 * On return, bit is restored. */
159
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000160 cur_group = -1;
161 while ((app = appp[++cur_group])) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000162 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000163 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000164 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000165 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000166 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000167 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000168 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000169#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000170 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000171#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000172 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000173 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000174 }
175 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000176 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000177}
178
179
Denis Vlasenko92258542006-11-01 10:25:35 +0000180ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000181{
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000182 const char *tmp = strrchr(fileName, '/');
183 if (tmp == NULL)
184 tmp = fileName;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000185 else {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000186 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000187 if (!*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
188 tmp--;
189 while (tmp != fileName && *--tmp != '/')
190 continue;
191 if (*tmp == '/')
192 tmp++;
193 }
194 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000195 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
196}
Denis Vlasenkob9411292007-06-15 23:43:11 +0000197#if ENABLE_FEATURE_FIND_PATH
198ACTF(path)
199{
200 return fnmatch(ap->pattern, fileName, 0) == 0;
201}
202#endif
203#if ENABLE_FEATURE_FIND_REGEX
204ACTF(regex)
205{
206 regmatch_t match;
207 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
208 return 0; /* no match */
209 if (match.rm_so)
210 return 0; /* match doesn't start at pos 0 */
211 if (fileName[match.rm_eo])
212 return 0; /* match doesn't end exactly at end of pathname */
213 return 1;
214}
215#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000216#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000217ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000218{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000219 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000220}
Rob Landleycee605c2005-10-06 16:39:17 +0000221#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000222#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000223ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000224{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000225 /* -perm +mode: at least one of perm_mask bits are set */
226 if (ap->perm_char == '+')
227 return (statbuf->st_mode & ap->perm_mask) != 0;
228 /* -perm -mode: all of perm_mask are set */
229 if (ap->perm_char == '-')
230 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
231 /* -perm mode: file mode must match perm_mask */
232 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000233}
Rob Landleycee605c2005-10-06 16:39:17 +0000234#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000235#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000236ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000237{
238 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000239 time_t mtime_secs = ap->mtime_days * 24*60*60;
240 if (ap->mtime_char == '+')
241 return file_age >= mtime_secs + 24*60*60;
242 if (ap->mtime_char == '-')
243 return file_age < mtime_secs;
244 /* just numeric mtime */
245 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000246}
Rob Landleycee605c2005-10-06 16:39:17 +0000247#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000248#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000249ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000250{
251 time_t file_age = time(NULL) - statbuf->st_mtime;
252 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000253 if (ap->mmin_char == '+')
254 return file_age >= mmin_secs + 60;
255 if (ap->mmin_char == '-')
256 return file_age < mmin_secs;
257 /* just numeric mmin */
258 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000259}
Paul Fox72d1a232006-01-13 21:05:41 +0000260#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000261#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000262ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000263{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000264 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000265}
Rob Landleycee605c2005-10-06 16:39:17 +0000266#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000267#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000268ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000269{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000270 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000271}
Rob Landleycee605c2005-10-06 16:39:17 +0000272#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000273#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000274ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000275{
276 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000277 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000278 for (i = 0; i < ap->exec_argc; i++)
279 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
280 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000281
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000282 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000283 if (rc < 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000284 bb_perror_msg("%s", argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000285
286 i = 0;
287 while (argv[i])
288 free(argv[i++]);
289 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000290}
Rob Landleycee605c2005-10-06 16:39:17 +0000291#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000292#if ENABLE_FEATURE_FIND_USER
293ACTF(user)
294{
295 return (statbuf->st_uid == ap->uid);
296}
297#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000298#if ENABLE_FEATURE_FIND_GROUP
299ACTF(group)
300{
301 return (statbuf->st_gid == ap->gid);
302}
303#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000304#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000305ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000306{
307 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000308 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000309}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000310#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000311ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000312{
313 puts(fileName);
314 return TRUE;
315}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000316#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000317ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000318{
319 return exec_actions(ap->subexpr, fileName, statbuf);
320}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000321#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000322#if ENABLE_FEATURE_FIND_SIZE
323ACTF(size)
324{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000325 if (ap->size_char == '+')
326 return statbuf->st_size > ap->size;
327 if (ap->size_char == '-')
328 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000329 return statbuf->st_size == ap->size;
330}
331#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000332#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000333/*
334 * -prune: if -depth is not given, return true and do not descend
335 * current dir; if -depth is given, return false with no effect.
336 * Example:
337 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
338 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000339ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000340{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000341 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000342}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000343#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000344#if ENABLE_FEATURE_FIND_DELETE
345ACTF(delete)
346{
347 int rc;
348 if (S_ISDIR(statbuf->st_mode)) {
349 rc = rmdir(fileName);
350 } else {
351 rc = unlink(fileName);
352 }
353 if (rc < 0)
354 bb_perror_msg("%s", fileName);
355 return TRUE;
356}
357#endif
358
Denis Vlasenko62f04792007-04-13 10:00:12 +0000359
Denis Vlasenkob9411292007-06-15 23:43:11 +0000360static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000361{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000362 int i;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000363#if ENABLE_FEATURE_FIND_MAXDEPTH
364 int maxdepth = (int)(ptrdiff_t)userData;
365
366 if (depth > maxdepth) return SKIP;
367#endif
368
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000369#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000370 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000371 for (i = 0; i < xdev_count; i++) {
372 if (xdev_dev[i] != statbuf->st_dev)
373 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000374 }
375 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000376#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000377 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000378 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000379 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000380 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000381 /* Cannot return 0: our caller, recursive_action(),
382 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000383 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000384}
385
Eric Andersen17d49ef1999-10-06 20:25:32 +0000386
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000387#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000388static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000389{
390 int mask = 0;
391
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000392 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000393 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000394 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000395 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000396 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000397 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000398 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000399 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000400 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000401 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000402 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000403 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000404 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000405 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000406
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000407 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000408 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000409
410 return mask;
411}
412#endif
413
Denis Vlasenko4c978632007-02-03 03:31:13 +0000414#if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
415 || ENABLE_FEATURE_FIND_MMIN
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000416static const char* plus_minus_num(const char* str)
417{
418 if (*str == '-' || *str == '+')
419 str++;
420 return str;
421}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000422#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000423
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000424static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000425{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000426 enum {
427 PARM_a ,
428 PARM_o ,
429 USE_FEATURE_FIND_NOT( PARM_char_not ,)
430 PARM_print ,
431 USE_FEATURE_FIND_PRINT0(PARM_print0 ,)
432 PARM_name ,
Denis Vlasenkob9411292007-06-15 23:43:11 +0000433 USE_FEATURE_FIND_PATH( PARM_path ,)
434 USE_FEATURE_FIND_REGEX( PARM_regex ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000435 USE_FEATURE_FIND_TYPE( PARM_type ,)
436 USE_FEATURE_FIND_PERM( PARM_perm ,)
437 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
438 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
439 USE_FEATURE_FIND_NEWER( PARM_newer ,)
440 USE_FEATURE_FIND_INUM( PARM_inum ,)
441 USE_FEATURE_FIND_EXEC( PARM_exec ,)
442 USE_FEATURE_FIND_USER( PARM_user ,)
443 USE_FEATURE_FIND_GROUP( PARM_group ,)
444 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
445 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000446 USE_FEATURE_FIND_SIZE( PARM_size ,)
447 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
Denis Vlasenko62f04792007-04-13 10:00:12 +0000448 USE_FEATURE_FIND_DELETE(PARM_delete ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000449#if ENABLE_DESKTOP
450 PARM_and ,
451 PARM_or ,
452 USE_FEATURE_FIND_NOT( PARM_not ,)
453#endif
454 };
455
456 static const char *const params[] = {
457 "-a" ,
458 "-o" ,
459 USE_FEATURE_FIND_NOT( "!" ,)
460 "-print" ,
461 USE_FEATURE_FIND_PRINT0("-print0",)
462 "-name" ,
Denis Vlasenkob9411292007-06-15 23:43:11 +0000463 USE_FEATURE_FIND_PATH( "-path" ,)
464 USE_FEATURE_FIND_REGEX( "-regex" ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000465 USE_FEATURE_FIND_TYPE( "-type" ,)
466 USE_FEATURE_FIND_PERM( "-perm" ,)
467 USE_FEATURE_FIND_MTIME( "-mtime" ,)
468 USE_FEATURE_FIND_MMIN( "-mmin" ,)
469 USE_FEATURE_FIND_NEWER( "-newer" ,)
470 USE_FEATURE_FIND_INUM( "-inum" ,)
471 USE_FEATURE_FIND_EXEC( "-exec" ,)
472 USE_FEATURE_FIND_USER( "-user" ,)
473 USE_FEATURE_FIND_GROUP( "-group" ,)
474 USE_FEATURE_FIND_DEPTH( "-depth" ,)
475 USE_FEATURE_FIND_PAREN( "(" ,)
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000476 USE_FEATURE_FIND_SIZE( "-size" ,)
477 USE_FEATURE_FIND_PRUNE( "-prune" ,)
Denis Vlasenko62f04792007-04-13 10:00:12 +0000478 USE_FEATURE_FIND_DELETE("-delete",)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000479#if ENABLE_DESKTOP
480 "-and" ,
481 "-or" ,
482 USE_FEATURE_FIND_NOT( "-not" ,)
483#endif
484 NULL
485 };
486
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000487 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000488 unsigned cur_group = 0;
489 unsigned cur_action = 0;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000490 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000491
492 /* 'static' doesn't work here! (gcc 4.1.2) */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000493 action* alloc_action(int sizeof_struct, action_fp f)
494 {
495 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000496 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
497 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
498 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000499 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000500 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000501 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000502 return ap;
503 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000504
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000505#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000506
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000507 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000508
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000509/* Actions have side effects and return a true or false value
510 * We implement: -print, -print0, -exec
511 *
512 * The rest are tests.
513 *
514 * Tests and actions are grouped by operators
515 * ( expr ) Force precedence
516 * ! expr True if expr is false
517 * -not expr Same as ! expr
518 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
519 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
520 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
521 * We implement: (), -a, -o
522 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000523 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000524 const char *arg = argv[0];
525 const char *arg1 = argv[1];
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000526 int parm = index_in_str_array(params, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000527 /* --- Operators --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000528 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000529 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000530 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000531 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000532 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000533 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000534 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000535 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000536 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000537 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000538 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000539#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko56fce002007-04-04 23:41:35 +0000540 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000541 /* also handles "find ! ! -name 'foo*'" */
542 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000543 }
544#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000545
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000546 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000547 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000548 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000549 /* GNU find ignores '!' here: "find ! -print" */
550 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000551 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000552 }
553#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000554 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000555 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000556 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000557 (void) ALLOC_ACTION(print0);
558 }
Paul Foxd7384292006-05-12 14:47:20 +0000559#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000560 else if (parm == PARM_name) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000561 action_name *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000562 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000563 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000564 ap = ALLOC_ACTION(name);
565 ap->pattern = arg1;
566 }
Denis Vlasenkob9411292007-06-15 23:43:11 +0000567#if ENABLE_FEATURE_FIND_PATH
568 else if (parm == PARM_path) {
569 action_path *ap;
570 if (!*++argv)
571 bb_error_msg_and_die(bb_msg_requires_arg, arg);
572 ap = ALLOC_ACTION(path);
573 ap->pattern = arg1;
574 }
575#endif
576#if ENABLE_FEATURE_FIND_REGEX
577 else if (parm == PARM_regex) {
578 action_regex *ap;
579 if (!*++argv)
580 bb_error_msg_and_die(bb_msg_requires_arg, arg);
581 ap = ALLOC_ACTION(regex);
582 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
583 }
584#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000585#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko56fce002007-04-04 23:41:35 +0000586 else if (parm == PARM_type) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000587 action_type *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000588 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000589 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000590 ap = ALLOC_ACTION(type);
591 ap->type_mask = find_type(arg1);
592 }
Rob Landleycee605c2005-10-06 16:39:17 +0000593#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000594#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000595/* -perm mode File's permission bits are exactly mode (octal or symbolic).
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000596 * Symbolic modes use mode 0 as a point of departure.
597 * -perm -mode All of the permission bits mode are set for the file.
598 * -perm +mode Any of the permission bits mode are set for the file.
599 */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000600 else if (parm == PARM_perm) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000601 action_perm *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000602 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000603 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000604 ap = ALLOC_ACTION(perm);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000605 ap->perm_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000606 arg1 = plus_minus_num(arg1);
607 ap->perm_mask = 0;
608 if (!bb_parse_mode(arg1, &ap->perm_mask))
609 bb_error_msg_and_die("invalid mode: %s", arg1);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000610 }
Rob Landleycee605c2005-10-06 16:39:17 +0000611#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000612#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko56fce002007-04-04 23:41:35 +0000613 else if (parm == PARM_mtime) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000614 action_mtime *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000615 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000616 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000617 ap = ALLOC_ACTION(mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000618 ap->mtime_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000619 ap->mtime_days = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000620 }
Rob Landleycee605c2005-10-06 16:39:17 +0000621#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000622#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko56fce002007-04-04 23:41:35 +0000623 else if (parm == PARM_mmin) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000624 action_mmin *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000625 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000626 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000627 ap = ALLOC_ACTION(mmin);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000628 ap->mmin_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000629 ap->mmin_mins = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000630 }
Rob Landleycee605c2005-10-06 16:39:17 +0000631#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000632#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko56fce002007-04-04 23:41:35 +0000633 else if (parm == PARM_newer) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000634 action_newer *ap;
Eric Andersen97d86f22003-01-23 05:27:42 +0000635 struct stat stat_newer;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000636 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000637 bb_error_msg_and_die(bb_msg_requires_arg, arg);
638 xstat(arg1, &stat_newer);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000639 ap = ALLOC_ACTION(newer);
640 ap->newer_mtime = stat_newer.st_mtime;
641 }
Rob Landleycee605c2005-10-06 16:39:17 +0000642#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000643#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko56fce002007-04-04 23:41:35 +0000644 else if (parm == PARM_inum) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000645 action_inum *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000646 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000647 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000648 ap = ALLOC_ACTION(inum);
649 ap->inode_num = xatoul(arg1);
650 }
Rob Landleycee605c2005-10-06 16:39:17 +0000651#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000652#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000653 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000654 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000655 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000656 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000657 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000658 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000659 ap->exec_argv = ++argv; /* first arg after -exec */
660 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000661 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000662 if (!*argv) /* did not see ';' until end */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000663 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000664 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000665 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000666 argv++;
667 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000668 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000669 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000670 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000671 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000672 i = ap->exec_argc;
673 while (i--)
674 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
675 }
676#endif
Denis Vlasenko4c978632007-02-03 03:31:13 +0000677#if ENABLE_FEATURE_FIND_USER
Denis Vlasenko56fce002007-04-04 23:41:35 +0000678 else if (parm == PARM_user) {
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000679 action_user *ap;
680 if (!*++argv)
681 bb_error_msg_and_die(bb_msg_requires_arg, arg);
682 ap = ALLOC_ACTION(user);
683 ap->uid = bb_strtou(arg1, NULL, 10);
684 if (errno)
685 ap->uid = xuname2uid(arg1);
686 }
687#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000688#if ENABLE_FEATURE_FIND_GROUP
689 else if (parm == PARM_group) {
690 action_group *ap;
691 if (!*++argv)
692 bb_error_msg_and_die(bb_msg_requires_arg, arg);
693 ap = ALLOC_ACTION(group);
694 ap->gid = bb_strtou(arg1, NULL, 10);
695 if (errno)
696 ap->gid = xgroup2gid(arg1);
697 }
698#endif
699#if ENABLE_FEATURE_FIND_DEPTH
700 else if (parm == PARM_depth) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000701 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000702 }
703#endif
704#if ENABLE_FEATURE_FIND_PAREN
705 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000706 action_paren *ap;
707 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000708 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000709
710 endarg = argv;
711 while (1) {
712 if (!*++endarg)
713 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000714 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000715 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000716 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000717 *endarg = NULL;
718 break;
719 }
720 }
721 ap = ALLOC_ACTION(paren);
722 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000723 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000724 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000725 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000726#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000727#if ENABLE_FEATURE_FIND_SIZE
728 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000729/* -size n[bckw]: file uses n units of space
730 * b (default): units are 512-byte blocks
731 * c: 1 byte
732 * k: kilobytes
733 * w: 2-byte words
734 */
735#if ENABLE_LFS
736#define XATOU_SFX xatoull_sfx
737#else
738#define XATOU_SFX xatoul_sfx
739#endif
740 static const struct suffix_mult find_suffixes[] = {
741 { "c", 1 },
742 { "w", 2 },
743 { "b"+1, 512 },
744 { "b", 512 },
745 { "k", 1024 },
746 { NULL, 0 }
747 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000748 action_size *ap;
749 if (!*++argv)
750 bb_error_msg_and_die(bb_msg_requires_arg, arg);
751 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000752 ap->size_char = arg1[0];
753 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000754 }
755#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000756#if ENABLE_FEATURE_FIND_PRUNE
757 else if (parm == PARM_prune) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000758 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000759 (void) ALLOC_ACTION(prune);
760 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000761#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000762#if ENABLE_FEATURE_FIND_DELETE
763 else if (parm == PARM_delete) {
764 need_print = 0;
765 recurse_flags |= ACTION_DEPTHFIRST;
766 (void) ALLOC_ACTION(delete);
767 }
768#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000769 else {
770 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000771 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000772 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000773 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000774 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000775 return appp;
776#undef ALLOC_ACTION
777}
778
779
Denis Vlasenko06af2162007-02-03 17:28:39 +0000780int find_main(int argc, char **argv);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000781int find_main(int argc, char **argv)
782{
Denis Vlasenkob9411292007-06-15 23:43:11 +0000783 static const char *const options[] = {
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000784 "-follow",
Denis Vlasenkob9411292007-06-15 23:43:11 +0000785USE_FEATURE_FIND_XDEV( "-xdev" ,)
786USE_FEATURE_FIND_MAXDEPTH("-maxdepth",)
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000787 NULL
788 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000789 enum {
790 OPT_FOLLOW,
791USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
792USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
793 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000794
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000795 char *arg;
796 char **argp;
797 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000798#if ENABLE_FEATURE_FIND_MAXDEPTH
799 int maxdepth = INT_MAX;
800#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000801
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000802 for (firstopt = 1; firstopt < argc; firstopt++) {
803 if (argv[firstopt][0] == '-')
804 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000805 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
806 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000807#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000808 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000809 break;
810#endif
811 }
812 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000813 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000814 argv--;
815 firstopt++;
816 }
817
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000818/* All options always return true. They always take effect
819 * rather than being processed only when their place in the
820 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000821 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000822 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000823 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000824 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000825 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000826 while ((arg = argp[0])) {
Denis Vlasenkob9411292007-06-15 23:43:11 +0000827 int opt = index_in_str_array(options, arg);
828 if (opt == OPT_FOLLOW) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000829 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000830 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000831 }
832#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000833 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000834 struct stat stbuf;
835 if (!xdev_count) {
836 xdev_count = firstopt - 1;
837 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
838 for (i = 1; i < firstopt; i++) {
839 /* not xstat(): shouldn't bomb out on
840 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000841 if (stat(argv[i], &stbuf))
842 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000843 xdev_dev[i-1] = stbuf.st_dev;
844 }
845 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000846 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000847 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000848#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000849#if ENABLE_FEATURE_FIND_MAXDEPTH
850 if (opt == OPT_MAXDEPTH) {
851 if (!argp[1])
852 bb_show_usage();
853 maxdepth = xatoi_u(argp[1]);
854 argp[0] = (char*)"-a";
855 argp[1] = (char*)"-a";
856 argp++;
857 }
858#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000859 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000860 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000861
862 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000863
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000864 for (i = 1; i < firstopt; i++) {
865 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000866 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000867 fileAction, /* file action */
868 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000869#if ENABLE_FEATURE_FIND_MAXDEPTH
870 (void*)maxdepth,/* user data */
871#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000872 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000873#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000874 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000875 status = EXIT_FAILURE;
876 }
Matt Kraai096370d2001-02-07 03:52:38 +0000877 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000878}