blob: f45abbe367894eb26ad3e6f3b150346f3ce393c5 [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
Denys Vlasenko6c750f12009-09-27 01:51:47 +020065typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +000066
Denis Vlasenko5d499e12006-10-29 19:07:01 +000067typedef struct {
68 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000069#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000070 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000071#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +000072} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020073
Denys Vlasenko606291b2009-09-23 23:15:43 +020074#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020075#define ACTF(name) \
76 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +020077 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020078 action_##name* ap UNUSED_PARAM)
79
80 ACTS(print)
81 ACTS(name, const char *pattern; bool iname;)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000082IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
83IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
84IF_FEATURE_FIND_PRINT0( ACTS(print0))
85IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
86IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
87IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
88IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
89IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
90IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
91IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
92IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
93IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
94IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
95IF_FEATURE_FIND_PRUNE( ACTS(prune))
96IF_FEATURE_FIND_DELETE( ACTS(delete))
97IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
98IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +010099IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000100
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200101struct globals {
102 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
103 IF_FEATURE_FIND_XDEV(int xdev_count;)
104 action ***actions;
105 bool need_print;
106 recurse_flags_t recurse_flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100107} FIX_ALIASING;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200108#define G (*(struct globals*)&bb_common_bufsiz1)
109#define INIT_G() do { \
110 struct G_sizecheck { \
111 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
112 }; \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200113 /* we have to zero it out because of NOEXEC */ \
114 memset(&G, 0, offsetof(struct globals, need_print)); \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200115 G.need_print = 1; \
116 G.recurse_flags = ACTION_RECURSE; \
117} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000118
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000119#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000120static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000121{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000122 unsigned count = 0;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000123 while ((str = strstr(str, "{}")) != NULL) {
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000124 count++;
125 str++;
126 }
127 return count;
128}
129
130
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000131static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000132{
133 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000134 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000135 /* we replace each '{}' with filename: growth by strlen-2 */
136 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
137 while ((end = strstr(src, "{}"))) {
138 memcpy(dst, src, end - src);
139 dst += end - src;
140 src = end + 2;
141 memcpy(dst, filename, flen);
142 dst += flen;
143 }
144 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000145 return buf;
146}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000147#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000148
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000149/* Return values of ACTFs ('action functions') are a bit mask:
150 * bit 1=1: prune (use SKIP constant for setting it)
151 * bit 0=1: matched successfully (TRUE)
152 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000153
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200154static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000155{
156 int cur_group;
157 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000158 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000159 action **app, *ap;
160
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000161 /* "action group" is a set of actions ANDed together.
162 * groups are ORed together.
163 * We simply evaluate each group until we find one in which all actions
164 * succeed. */
165
166 /* -prune is special: if it is encountered, then we won't
167 * descend into current directory. It doesn't matter whether
168 * action group (in which -prune sits) will succeed or not:
169 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
170 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
171 * not starting with 'f' */
172
173 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
174 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
175 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
176 * On return, bit is restored. */
177
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000178 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100179 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000180 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000181 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000182 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000183 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000184 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000185 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000186 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000187#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000188 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000189#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000190 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000191 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000192 }
193 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000194 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000195}
196
197
Denis Vlasenko92258542006-11-01 10:25:35 +0000198ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000199{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000200 const char *tmp = bb_basename(fileName);
201 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
202 tmp--;
203 while (tmp != fileName && *--tmp != '/')
204 continue;
205 if (*tmp == '/')
206 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000207 }
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000208 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000209}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000210
Denis Vlasenkob9411292007-06-15 23:43:11 +0000211#if ENABLE_FEATURE_FIND_PATH
212ACTF(path)
213{
214 return fnmatch(ap->pattern, fileName, 0) == 0;
215}
216#endif
217#if ENABLE_FEATURE_FIND_REGEX
218ACTF(regex)
219{
220 regmatch_t match;
221 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
222 return 0; /* no match */
223 if (match.rm_so)
224 return 0; /* match doesn't start at pos 0 */
225 if (fileName[match.rm_eo])
226 return 0; /* match doesn't end exactly at end of pathname */
227 return 1;
228}
229#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000230#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000231ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000232{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000233 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
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_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000237ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000238{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000239 /* -perm +mode: at least one of perm_mask bits are set */
240 if (ap->perm_char == '+')
241 return (statbuf->st_mode & ap->perm_mask) != 0;
242 /* -perm -mode: all of perm_mask are set */
243 if (ap->perm_char == '-')
244 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
245 /* -perm mode: file mode must match perm_mask */
246 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000247}
Rob Landleycee605c2005-10-06 16:39:17 +0000248#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000249#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000250ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000251{
252 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000253 time_t mtime_secs = ap->mtime_days * 24*60*60;
254 if (ap->mtime_char == '+')
255 return file_age >= mtime_secs + 24*60*60;
256 if (ap->mtime_char == '-')
257 return file_age < mtime_secs;
258 /* just numeric mtime */
259 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000260}
Rob Landleycee605c2005-10-06 16:39:17 +0000261#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000262#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000263ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000264{
265 time_t file_age = time(NULL) - statbuf->st_mtime;
266 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000267 if (ap->mmin_char == '+')
268 return file_age >= mmin_secs + 60;
269 if (ap->mmin_char == '-')
270 return file_age < mmin_secs;
271 /* just numeric mmin */
272 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000273}
Paul Fox72d1a232006-01-13 21:05:41 +0000274#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000275#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000276ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000277{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000278 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000279}
Rob Landleycee605c2005-10-06 16:39:17 +0000280#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000281#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000282ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000283{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000284 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000285}
Rob Landleycee605c2005-10-06 16:39:17 +0000286#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000287#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000288ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000289{
290 int i, rc;
Denys Vlasenkoaa7da732009-09-13 14:57:24 +0200291#if ENABLE_USE_PORTABLE_CODE
292 char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
293#else /* gcc 4.3.1 generates smaller code: */
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000294 char *argv[ap->exec_argc + 1];
Denys Vlasenkoaa7da732009-09-13 14:57:24 +0200295#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000296 for (i = 0; i < ap->exec_argc; i++)
297 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
298 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000299
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000300 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000301 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000302 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000303
304 i = 0;
305 while (argv[i])
306 free(argv[i++]);
307 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000308}
Rob Landleycee605c2005-10-06 16:39:17 +0000309#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000310#if ENABLE_FEATURE_FIND_USER
311ACTF(user)
312{
313 return (statbuf->st_uid == ap->uid);
314}
315#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000316#if ENABLE_FEATURE_FIND_GROUP
317ACTF(group)
318{
319 return (statbuf->st_gid == ap->gid);
320}
321#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000322#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000323ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000324{
325 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000326 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000327}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000328#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000329ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000330{
331 puts(fileName);
332 return TRUE;
333}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000334#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000335ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000336{
337 return exec_actions(ap->subexpr, fileName, statbuf);
338}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000339#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000340#if ENABLE_FEATURE_FIND_SIZE
341ACTF(size)
342{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000343 if (ap->size_char == '+')
344 return statbuf->st_size > ap->size;
345 if (ap->size_char == '-')
346 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000347 return statbuf->st_size == ap->size;
348}
349#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000350#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000351/*
352 * -prune: if -depth is not given, return true and do not descend
353 * current dir; if -depth is given, return false with no effect.
354 * Example:
355 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
356 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000357ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000358{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000359 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000360}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000361#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000362#if ENABLE_FEATURE_FIND_DELETE
363ACTF(delete)
364{
365 int rc;
366 if (S_ISDIR(statbuf->st_mode)) {
367 rc = rmdir(fileName);
368 } else {
369 rc = unlink(fileName);
370 }
371 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000372 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000373 return TRUE;
374}
375#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000376#if ENABLE_FEATURE_FIND_CONTEXT
377ACTF(context)
378{
379 security_context_t con;
380 int rc;
381
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200382 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000383 rc = getfilecon(fileName, &con);
384 } else {
385 rc = lgetfilecon(fileName, &con);
386 }
387 if (rc < 0)
388 return FALSE;
389 rc = strcmp(ap->context, con);
390 freecon(con);
391 return rc == 0;
392}
393#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100394#if ENABLE_FEATURE_FIND_LINKS
395ACTF(links)
396{
397 switch(ap->links_char) {
398 case '-' : return (statbuf->st_nlink < ap->links_count);
399 case '+' : return (statbuf->st_nlink > ap->links_count);
400 default: return (statbuf->st_nlink == ap->links_count);
401 }
402}
403#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000404
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000405static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000406 struct stat *statbuf,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000407 void *userData IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
408 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000409{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100410 int r;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000411#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000412#define minmaxdepth ((int*)userData)
Denis Vlasenkob9411292007-06-15 23:43:11 +0000413
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100414 if (depth < minmaxdepth[0])
415 return TRUE; /* skip this, continue recursing */
416 if (depth > minmaxdepth[1])
417 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000418#endif
419
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100420 r = exec_actions(G.actions, fileName, statbuf);
421 /* Had no explicit -print[0] or -exec? then print */
422 if ((r & TRUE) && G.need_print)
423 puts(fileName);
424
425#if ENABLE_FEATURE_FIND_MAXDEPTH
426 if (S_ISDIR(statbuf->st_mode)) {
427 if (depth == minmaxdepth[1])
428 return SKIP;
429 }
430#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000431#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100432 /* -xdev stops on mountpoints, but AFTER mountpoit itself
433 * is processed as usual */
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200434 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200435 if (G.xdev_count) {
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100436 int i;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200437 for (i = 0; i < G.xdev_count; i++) {
438 if (G.xdev_dev[i] == statbuf->st_dev)
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200439 goto found;
440 }
Paul Foxdcf9bba2007-09-06 17:45:16 +0000441 return SKIP;
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200442 found: ;
443 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000444 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000445#endif
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200446
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000447 /* Cannot return 0: our caller, recursive_action(),
448 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100449 return (r & SKIP) ? SKIP : TRUE;
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200450#undef minmaxdepth
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000451}
452
Eric Andersen17d49ef1999-10-06 20:25:32 +0000453
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000454#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000455static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000456{
457 int mask = 0;
458
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000459 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000460 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000461 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000462 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000463 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000464 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000465 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000466 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000467 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000468 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000469 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000470 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000471 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000472 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000473
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200474 if (mask == 0 || type[1] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000475 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000476
477 return mask;
478}
479#endif
480
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000481#if ENABLE_FEATURE_FIND_PERM \
482 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100483 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000484static const char* plus_minus_num(const char* str)
485{
486 if (*str == '-' || *str == '+')
487 str++;
488 return str;
489}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000490#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000491
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000492static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000493{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000494 enum {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200495 PARM_a ,
496 PARM_o ,
497 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000498#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200499 PARM_and ,
500 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000501 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000502#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200503 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000504 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
505 IF_FEATURE_FIND_DEPTH( PARM_depth ,)
506 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
507 IF_FEATURE_FIND_DELETE( PARM_delete ,)
508 IF_FEATURE_FIND_EXEC( PARM_exec ,)
509 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000510 /* All options starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200511 PARM_name ,
512 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000513 IF_FEATURE_FIND_PATH( PARM_path ,)
514 IF_FEATURE_FIND_REGEX( PARM_regex ,)
515 IF_FEATURE_FIND_TYPE( PARM_type ,)
516 IF_FEATURE_FIND_PERM( PARM_perm ,)
517 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
518 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
519 IF_FEATURE_FIND_NEWER( PARM_newer ,)
520 IF_FEATURE_FIND_INUM( PARM_inum ,)
521 IF_FEATURE_FIND_USER( PARM_user ,)
522 IF_FEATURE_FIND_GROUP( PARM_group ,)
523 IF_FEATURE_FIND_SIZE( PARM_size ,)
524 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100525 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000526 };
527
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000528 static const char params[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000529 "-a\0"
530 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000531 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000532#if ENABLE_DESKTOP
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000533 "-and\0"
534 "-or\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000535 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000536#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000537 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000538 IF_FEATURE_FIND_PRINT0( "-print0\0" )
539 IF_FEATURE_FIND_DEPTH( "-depth\0" )
540 IF_FEATURE_FIND_PRUNE( "-prune\0" )
541 IF_FEATURE_FIND_DELETE( "-delete\0" )
542 IF_FEATURE_FIND_EXEC( "-exec\0" )
543 IF_FEATURE_FIND_PAREN( "(\0" )
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000544 /* All options starting from here require argument */
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000545 "-name\0"
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000546 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000547 IF_FEATURE_FIND_PATH( "-path\0" )
548 IF_FEATURE_FIND_REGEX( "-regex\0" )
549 IF_FEATURE_FIND_TYPE( "-type\0" )
550 IF_FEATURE_FIND_PERM( "-perm\0" )
551 IF_FEATURE_FIND_MTIME( "-mtime\0" )
552 IF_FEATURE_FIND_MMIN( "-mmin\0" )
553 IF_FEATURE_FIND_NEWER( "-newer\0" )
554 IF_FEATURE_FIND_INUM( "-inum\0" )
555 IF_FEATURE_FIND_USER( "-user\0" )
556 IF_FEATURE_FIND_GROUP( "-group\0" )
557 IF_FEATURE_FIND_SIZE( "-size\0" )
558 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100559 IF_FEATURE_FIND_LINKS( "-links\0" )
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000560 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000561
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000562 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000563 unsigned cur_group = 0;
564 unsigned cur_action = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000565 IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000566
Denis Vlasenko0f3a5802008-03-20 13:13:09 +0000567 /* This is the only place in busybox where we use nested function.
568 * So far more standard alternatives were bigger. */
569 /* Suppress a warning "func without a prototype" */
570 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000571 action* alloc_action(int sizeof_struct, action_fp f)
572 {
573 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000574 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
575 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
576 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000577 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000578 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
579 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000580 return ap;
581 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000582
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000583#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000584
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000585 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000586
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000587/* Actions have side effects and return a true or false value
588 * We implement: -print, -print0, -exec
589 *
590 * The rest are tests.
591 *
592 * Tests and actions are grouped by operators
593 * ( expr ) Force precedence
594 * ! expr True if expr is false
595 * -not expr Same as ! expr
596 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
597 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
598 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
599 * We implement: (), -a, -o
600 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000601 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000602 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000603 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000604 const char *arg1 = argv[1];
605
606 if (parm >= PARM_name) {
607 /* All options starting from -name require argument */
608 if (!arg1)
609 bb_error_msg_and_die(bb_msg_requires_arg, arg);
610 argv++;
611 }
612
613 /* We can use big switch() here, but on i386
614 * it doesn't give smaller code. Other arches? */
615
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000616 /* --- Operators --- */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000617 if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000618 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000619 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000620 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000621 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000622 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000623 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000624 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000625 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000626 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000627 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000628#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000629 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000630 /* also handles "find ! ! -name 'foo*'" */
631 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000632 }
633#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000634
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000635 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000636 else if (parm == PARM_print) {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200637 G.need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000638 /* GNU find ignores '!' here: "find ! -print" */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000639 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000640 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000641 }
642#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000643 else if (parm == PARM_print0) {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200644 G.need_print = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000645 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000646 (void) ALLOC_ACTION(print0);
647 }
Paul Foxd7384292006-05-12 14:47:20 +0000648#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000649#if ENABLE_FEATURE_FIND_DEPTH
650 else if (parm == PARM_depth) {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200651 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000652 }
653#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000654#if ENABLE_FEATURE_FIND_PRUNE
655 else if (parm == PARM_prune) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000656 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000657 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000658 }
659#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000660#if ENABLE_FEATURE_FIND_DELETE
661 else if (parm == PARM_delete) {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200662 G.need_print = 0;
663 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000664 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000665 }
Rob Landleycee605c2005-10-06 16:39:17 +0000666#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000667#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000668 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000669 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000670 action_exec *ap;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200671 G.need_print = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000672 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000673 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000674 ap->exec_argv = ++argv; /* first arg after -exec */
675 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000676 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +0100677 if (!*argv) /* did not see ';' or '+' until end */
678 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000679 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000680 break;
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +0100681 //TODO: implement {} + (like xargs)
682 // See:
683 // find findutils/ -exec echo ">"{}"<" \;
684 // find findutils/ -exec echo ">"{}"<" +
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000685 argv++;
686 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000687 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000688 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000689 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000690 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000691 i = ap->exec_argc;
692 while (i--)
693 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
694 }
695#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000696#if ENABLE_FEATURE_FIND_PAREN
697 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000698 action_paren *ap;
699 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000700 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000701
702 endarg = argv;
703 while (1) {
704 if (!*++endarg)
705 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000706 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000707 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000708 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000709 *endarg = NULL;
710 break;
711 }
712 }
713 ap = ALLOC_ACTION(paren);
714 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000715 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000716 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000717 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000718#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000719 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000720 action_name *ap;
721 ap = ALLOC_ACTION(name);
722 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000723 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000724 }
725#if ENABLE_FEATURE_FIND_PATH
726 else if (parm == PARM_path) {
727 action_path *ap;
728 ap = ALLOC_ACTION(path);
729 ap->pattern = arg1;
730 }
731#endif
732#if ENABLE_FEATURE_FIND_REGEX
733 else if (parm == PARM_regex) {
734 action_regex *ap;
735 ap = ALLOC_ACTION(regex);
736 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
737 }
738#endif
739#if ENABLE_FEATURE_FIND_TYPE
740 else if (parm == PARM_type) {
741 action_type *ap;
742 ap = ALLOC_ACTION(type);
743 ap->type_mask = find_type(arg1);
744 }
745#endif
746#if ENABLE_FEATURE_FIND_PERM
747/* -perm mode File's permission bits are exactly mode (octal or symbolic).
748 * Symbolic modes use mode 0 as a point of departure.
749 * -perm -mode All of the permission bits mode are set for the file.
750 * -perm +mode Any of the permission bits mode are set for the file.
751 */
752 else if (parm == PARM_perm) {
753 action_perm *ap;
754 ap = ALLOC_ACTION(perm);
755 ap->perm_char = arg1[0];
756 arg1 = plus_minus_num(arg1);
757 ap->perm_mask = 0;
758 if (!bb_parse_mode(arg1, &ap->perm_mask))
759 bb_error_msg_and_die("invalid mode: %s", arg1);
760 }
761#endif
762#if ENABLE_FEATURE_FIND_MTIME
763 else if (parm == PARM_mtime) {
764 action_mtime *ap;
765 ap = ALLOC_ACTION(mtime);
766 ap->mtime_char = arg1[0];
767 ap->mtime_days = xatoul(plus_minus_num(arg1));
768 }
769#endif
770#if ENABLE_FEATURE_FIND_MMIN
771 else if (parm == PARM_mmin) {
772 action_mmin *ap;
773 ap = ALLOC_ACTION(mmin);
774 ap->mmin_char = arg1[0];
775 ap->mmin_mins = xatoul(plus_minus_num(arg1));
776 }
777#endif
778#if ENABLE_FEATURE_FIND_NEWER
779 else if (parm == PARM_newer) {
780 struct stat stat_newer;
781 action_newer *ap;
782 ap = ALLOC_ACTION(newer);
783 xstat(arg1, &stat_newer);
784 ap->newer_mtime = stat_newer.st_mtime;
785 }
786#endif
787#if ENABLE_FEATURE_FIND_INUM
788 else if (parm == PARM_inum) {
789 action_inum *ap;
790 ap = ALLOC_ACTION(inum);
791 ap->inode_num = xatoul(arg1);
792 }
793#endif
794#if ENABLE_FEATURE_FIND_USER
795 else if (parm == PARM_user) {
796 action_user *ap;
797 ap = ALLOC_ACTION(user);
798 ap->uid = bb_strtou(arg1, NULL, 10);
799 if (errno)
800 ap->uid = xuname2uid(arg1);
801 }
802#endif
803#if ENABLE_FEATURE_FIND_GROUP
804 else if (parm == PARM_group) {
805 action_group *ap;
806 ap = ALLOC_ACTION(group);
807 ap->gid = bb_strtou(arg1, NULL, 10);
808 if (errno)
809 ap->gid = xgroup2gid(arg1);
810 }
811#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000812#if ENABLE_FEATURE_FIND_SIZE
813 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000814/* -size n[bckw]: file uses n units of space
815 * b (default): units are 512-byte blocks
816 * c: 1 byte
817 * k: kilobytes
818 * w: 2-byte words
819 */
820#if ENABLE_LFS
821#define XATOU_SFX xatoull_sfx
822#else
823#define XATOU_SFX xatoul_sfx
824#endif
825 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000826 { "c", 1 },
827 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000828 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000829 { "b", 512 },
830 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +0200831 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000832 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000833 action_size *ap;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000834 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000835 ap->size_char = arg1[0];
836 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000837 }
838#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000839#if ENABLE_FEATURE_FIND_CONTEXT
840 else if (parm == PARM_context) {
841 action_context *ap;
842 ap = ALLOC_ACTION(context);
843 ap->context = NULL;
Denis Vlasenko838daca2007-08-16 10:41:24 +0000844 /* SELinux headers erroneously declare non-const parameter */
845 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000846 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000847 }
848#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100849#if ENABLE_FEATURE_FIND_LINKS
850 else if (parm == PARM_links) {
851 action_links *ap;
852 ap = ALLOC_ACTION(links);
853 ap->links_char = arg1[0];
854 ap->links_count = xatoul(plus_minus_num(arg1));
855 }
856#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000857 else {
858 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000859 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000860 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000861 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000862 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000863 return appp;
864#undef ALLOC_ACTION
865}
866
867
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000868int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100869int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000870{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000871 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000872 "-follow\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000873IF_FEATURE_FIND_XDEV( "-xdev\0" )
874IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000875 ;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000876 enum {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000877 OPT_FOLLOW,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000878IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
879IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,)
Denis Vlasenkob9411292007-06-15 23:43:11 +0000880 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000881
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000882 char *arg;
883 char **argp;
884 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000885#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000886 int minmaxdepth[2] = { 0, INT_MAX };
887#else
888#define minmaxdepth NULL
Denis Vlasenkob9411292007-06-15 23:43:11 +0000889#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000890
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200891 INIT_G();
892
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +0100893 for (firstopt = 1; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000894 if (argv[firstopt][0] == '-')
895 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000896 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
897 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000898#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000899 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000900 break;
901#endif
902 }
903 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000904 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000905 argv--;
906 firstopt++;
907 }
908
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000909/* All options always return true. They always take effect
910 * rather than being processed only when their place in the
911 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000912 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000913 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000914 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000915 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000916 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000917 while ((arg = argp[0])) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000918 int opt = index_in_strings(options, arg);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000919 if (opt == OPT_FOLLOW) {
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200920 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000921 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000922 }
923#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000924 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000925 struct stat stbuf;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200926 if (!G.xdev_count) {
927 G.xdev_count = firstopt - 1;
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100928 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000929 for (i = 1; i < firstopt; i++) {
930 /* not xstat(): shouldn't bomb out on
931 * "find not_exist exist -xdev" */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100932 if (stat(argv[i], &stbuf) == 0)
933 G.xdev_dev[i-1] = stbuf.st_dev;
934 /* else G.xdev_dev[i-1] stays 0 and
935 * won't match any real device dev_t */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000936 }
937 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000938 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000939 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000940#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000941#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000942 if (opt == OPT_MINDEPTH || opt == OPT_MINDEPTH + 1) {
Denis Vlasenkob9411292007-06-15 23:43:11 +0000943 if (!argp[1])
944 bb_show_usage();
Denis Vlasenko83518d12009-03-20 22:17:13 +0000945 minmaxdepth[opt - OPT_MINDEPTH] = xatoi_u(argp[1]);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000946 argp[0] = (char*)"-a";
947 argp[1] = (char*)"-a";
948 argp++;
949 }
950#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000951 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000952 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000953
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200954 G.actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000955
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000956 for (i = 1; i < firstopt; i++) {
957 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200958 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000959 fileAction, /* file action */
960 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000961#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000962 minmaxdepth, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000963#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000964 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000965#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000966 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000967 status = EXIT_FAILURE;
968 }
Matt Kraai096370d2001-02-07 03:52:38 +0000969 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000970}