blob: 6f2cbbc78cf2d615646434fce044dfea6b761356 [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)
Denis Vlasenkoa19faf82007-06-19 22:22:57 +000078 ACTS(print)
79 ACTS(name, const char *pattern;)
80USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
81USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
82USE_FEATURE_FIND_PRINT0( ACTS(print0))
83USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
84USE_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;))
87USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
88USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
89USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
90USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
91USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
92USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
93USE_FEATURE_FIND_PRUNE( ACTS(prune))
94USE_FEATURE_FIND_DELETE( ACTS(delete))
95USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
96USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matt Kraai096370d2001-02-07 03:52:38 +000097
Denis Vlasenko5d499e12006-10-29 19:07:01 +000098static action ***actions;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +000099static bool need_print = 1;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000100static int recurse_flags = ACTION_RECURSE;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000101
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000102#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000103static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000104{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000105 unsigned count = 0;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000106 while ((str = strstr(str, "{}")) != NULL) {
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000107 count++;
108 str++;
109 }
110 return count;
111}
112
113
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000114static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000115{
116 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000117 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000118 /* we replace each '{}' with filename: growth by strlen-2 */
119 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
120 while ((end = strstr(src, "{}"))) {
121 memcpy(dst, src, end - src);
122 dst += end - src;
123 src = end + 2;
124 memcpy(dst, filename, flen);
125 dst += flen;
126 }
127 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000128 return buf;
129}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000130#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000131
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000132/* Return values of ACTFs ('action functions') are a bit mask:
133 * bit 1=1: prune (use SKIP constant for setting it)
134 * bit 0=1: matched successfully (TRUE)
135 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000136
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000137static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
138{
139 int cur_group;
140 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000141 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000142 action **app, *ap;
143
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000144 /* "action group" is a set of actions ANDed together.
145 * groups are ORed together.
146 * We simply evaluate each group until we find one in which all actions
147 * succeed. */
148
149 /* -prune is special: if it is encountered, then we won't
150 * descend into current directory. It doesn't matter whether
151 * action group (in which -prune sits) will succeed or not:
152 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
153 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
154 * not starting with 'f' */
155
156 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
157 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
158 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
159 * On return, bit is restored. */
160
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000161 cur_group = -1;
162 while ((app = appp[++cur_group])) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000163 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000164 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000165 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000166 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000167 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000168 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000169 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000170#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000171 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000172#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000173 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000174 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000175 }
176 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000177 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000178}
179
180
Denis Vlasenko92258542006-11-01 10:25:35 +0000181ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000182{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000183 const char *tmp = bb_basename(fileName);
184 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
185 tmp--;
186 while (tmp != fileName && *--tmp != '/')
187 continue;
188 if (*tmp == '/')
189 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000190 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000191 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
192}
Denis Vlasenkob9411292007-06-15 23:43:11 +0000193#if ENABLE_FEATURE_FIND_PATH
194ACTF(path)
195{
196 return fnmatch(ap->pattern, fileName, 0) == 0;
197}
198#endif
199#if ENABLE_FEATURE_FIND_REGEX
200ACTF(regex)
201{
202 regmatch_t match;
203 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
204 return 0; /* no match */
205 if (match.rm_so)
206 return 0; /* match doesn't start at pos 0 */
207 if (fileName[match.rm_eo])
208 return 0; /* match doesn't end exactly at end of pathname */
209 return 1;
210}
211#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000212#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000213ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000214{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000215 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000216}
Rob Landleycee605c2005-10-06 16:39:17 +0000217#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000218#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000219ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000220{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000221 /* -perm +mode: at least one of perm_mask bits are set */
222 if (ap->perm_char == '+')
223 return (statbuf->st_mode & ap->perm_mask) != 0;
224 /* -perm -mode: all of perm_mask are set */
225 if (ap->perm_char == '-')
226 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
227 /* -perm mode: file mode must match perm_mask */
228 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000229}
Rob Landleycee605c2005-10-06 16:39:17 +0000230#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000231#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000232ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000233{
234 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000235 time_t mtime_secs = ap->mtime_days * 24*60*60;
236 if (ap->mtime_char == '+')
237 return file_age >= mtime_secs + 24*60*60;
238 if (ap->mtime_char == '-')
239 return file_age < mtime_secs;
240 /* just numeric mtime */
241 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000242}
Rob Landleycee605c2005-10-06 16:39:17 +0000243#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000244#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000245ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000246{
247 time_t file_age = time(NULL) - statbuf->st_mtime;
248 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000249 if (ap->mmin_char == '+')
250 return file_age >= mmin_secs + 60;
251 if (ap->mmin_char == '-')
252 return file_age < mmin_secs;
253 /* just numeric mmin */
254 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000255}
Paul Fox72d1a232006-01-13 21:05:41 +0000256#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000257#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000258ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000259{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000260 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000261}
Rob Landleycee605c2005-10-06 16:39:17 +0000262#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000263#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000264ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000265{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000266 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000267}
Rob Landleycee605c2005-10-06 16:39:17 +0000268#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000269#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000270ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000271{
272 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000273 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000274 for (i = 0; i < ap->exec_argc; i++)
275 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
276 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000277
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000278 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000279 if (rc < 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000280 bb_perror_msg("%s", argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000281
282 i = 0;
283 while (argv[i])
284 free(argv[i++]);
285 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000286}
Rob Landleycee605c2005-10-06 16:39:17 +0000287#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000288#if ENABLE_FEATURE_FIND_USER
289ACTF(user)
290{
291 return (statbuf->st_uid == ap->uid);
292}
293#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000294#if ENABLE_FEATURE_FIND_GROUP
295ACTF(group)
296{
297 return (statbuf->st_gid == ap->gid);
298}
299#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000300#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000301ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000302{
303 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000304 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000305}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000306#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000307ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000308{
309 puts(fileName);
310 return TRUE;
311}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000312#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000313ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000314{
315 return exec_actions(ap->subexpr, fileName, statbuf);
316}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000317#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000318#if ENABLE_FEATURE_FIND_SIZE
319ACTF(size)
320{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000321 if (ap->size_char == '+')
322 return statbuf->st_size > ap->size;
323 if (ap->size_char == '-')
324 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000325 return statbuf->st_size == ap->size;
326}
327#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000328#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000329/*
330 * -prune: if -depth is not given, return true and do not descend
331 * current dir; if -depth is given, return false with no effect.
332 * Example:
333 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
334 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000335ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000336{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000337 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000338}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000339#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000340#if ENABLE_FEATURE_FIND_DELETE
341ACTF(delete)
342{
343 int rc;
344 if (S_ISDIR(statbuf->st_mode)) {
345 rc = rmdir(fileName);
346 } else {
347 rc = unlink(fileName);
348 }
349 if (rc < 0)
350 bb_perror_msg("%s", fileName);
351 return TRUE;
352}
353#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000354#if ENABLE_FEATURE_FIND_CONTEXT
355ACTF(context)
356{
357 security_context_t con;
358 int rc;
359
360 if (recurse_flags & ACTION_FOLLOWLINKS) {
361 rc = getfilecon(fileName, &con);
362 } else {
363 rc = lgetfilecon(fileName, &con);
364 }
365 if (rc < 0)
366 return FALSE;
367 rc = strcmp(ap->context, con);
368 freecon(con);
369 return rc == 0;
370}
371#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000372
Denis Vlasenko62f04792007-04-13 10:00:12 +0000373
Denis Vlasenkob9411292007-06-15 23:43:11 +0000374static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000375{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000376 int i;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000377#if ENABLE_FEATURE_FIND_MAXDEPTH
378 int maxdepth = (int)(ptrdiff_t)userData;
379
380 if (depth > maxdepth) return SKIP;
381#endif
382
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000383#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000384 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000385 for (i = 0; i < xdev_count; i++) {
386 if (xdev_dev[i] != statbuf->st_dev)
387 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000388 }
389 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000390#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000391 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000392 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000393 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000394 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000395 /* Cannot return 0: our caller, recursive_action(),
396 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000397 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000398}
399
Eric Andersen17d49ef1999-10-06 20:25:32 +0000400
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000401#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000402static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000403{
404 int mask = 0;
405
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000406 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000407 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000408 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000409 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000410 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000411 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000412 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000413 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000414 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000415 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000416 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000417 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000418 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000419 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000420
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000421 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000422 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000423
424 return mask;
425}
426#endif
427
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000428#if ENABLE_FEATURE_FIND_PERM \
429 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
430 || ENABLE_FEATURE_FIND_SIZE
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000431static const char* plus_minus_num(const char* str)
432{
433 if (*str == '-' || *str == '+')
434 str++;
435 return str;
436}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000437#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000438
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000439static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000440{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000441 enum {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000442 PARM_a ,
443 PARM_o ,
444 USE_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000445#if ENABLE_DESKTOP
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000446 PARM_and ,
447 PARM_or ,
448 USE_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000449#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000450 PARM_print ,
451 USE_FEATURE_FIND_PRINT0( PARM_print0 ,)
452 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
453 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
454 USE_FEATURE_FIND_DELETE( PARM_delete ,)
455 USE_FEATURE_FIND_EXEC( PARM_exec ,)
456 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
457 /* All options starting from here require argument */
458 PARM_name ,
459 USE_FEATURE_FIND_PATH( PARM_path ,)
460 USE_FEATURE_FIND_REGEX( PARM_regex ,)
461 USE_FEATURE_FIND_TYPE( PARM_type ,)
462 USE_FEATURE_FIND_PERM( PARM_perm ,)
463 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
464 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
465 USE_FEATURE_FIND_NEWER( PARM_newer ,)
466 USE_FEATURE_FIND_INUM( PARM_inum ,)
467 USE_FEATURE_FIND_USER( PARM_user ,)
468 USE_FEATURE_FIND_GROUP( PARM_group ,)
469 USE_FEATURE_FIND_SIZE( PARM_size ,)
470 USE_FEATURE_FIND_CONTEXT(PARM_context ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000471 };
472
473 static const char *const params[] = {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000474 "-a" ,
475 "-o" ,
476 USE_FEATURE_FIND_NOT( "!" ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000477#if ENABLE_DESKTOP
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000478 "-and" ,
479 "-or" ,
480 USE_FEATURE_FIND_NOT( "-not" ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000481#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000482 "-print" ,
483 USE_FEATURE_FIND_PRINT0( "-print0" ,)
484 USE_FEATURE_FIND_DEPTH( "-depth" ,)
485 USE_FEATURE_FIND_PRUNE( "-prune" ,)
486 USE_FEATURE_FIND_DELETE( "-delete" ,)
487 USE_FEATURE_FIND_EXEC( "-exec" ,)
488 USE_FEATURE_FIND_PAREN( "(" ,)
489 /* All options starting from here require argument */
490 "-name" ,
491 USE_FEATURE_FIND_PATH( "-path" ,)
492 USE_FEATURE_FIND_REGEX( "-regex" ,)
493 USE_FEATURE_FIND_TYPE( "-type" ,)
494 USE_FEATURE_FIND_PERM( "-perm" ,)
495 USE_FEATURE_FIND_MTIME( "-mtime" ,)
496 USE_FEATURE_FIND_MMIN( "-mmin" ,)
497 USE_FEATURE_FIND_NEWER( "-newer" ,)
498 USE_FEATURE_FIND_INUM( "-inum" ,)
499 USE_FEATURE_FIND_USER( "-user" ,)
500 USE_FEATURE_FIND_GROUP( "-group" ,)
501 USE_FEATURE_FIND_SIZE( "-size" ,)
502 USE_FEATURE_FIND_CONTEXT("-context",)
503 NULL
Denis Vlasenko56fce002007-04-04 23:41:35 +0000504 };
505
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000506 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000507 unsigned cur_group = 0;
508 unsigned cur_action = 0;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000509 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000510
511 /* 'static' doesn't work here! (gcc 4.1.2) */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000512 action* alloc_action(int sizeof_struct, action_fp f)
513 {
514 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000515 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
516 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
517 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000518 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000519 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000520 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000521 return ap;
522 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000523
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000524#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000525
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000526 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000527
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000528/* Actions have side effects and return a true or false value
529 * We implement: -print, -print0, -exec
530 *
531 * The rest are tests.
532 *
533 * Tests and actions are grouped by operators
534 * ( expr ) Force precedence
535 * ! expr True if expr is false
536 * -not expr Same as ! expr
537 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
538 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
539 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
540 * We implement: (), -a, -o
541 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000542 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000543 const char *arg = argv[0];
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000544 int parm = index_in_str_array(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000545 const char *arg1 = argv[1];
546
547 if (parm >= PARM_name) {
548 /* All options starting from -name require argument */
549 if (!arg1)
550 bb_error_msg_and_die(bb_msg_requires_arg, arg);
551 argv++;
552 }
553
554 /* We can use big switch() here, but on i386
555 * it doesn't give smaller code. Other arches? */
556
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000557 /* --- Operators --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000558 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000559 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000560 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000561 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000562 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000563 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000564 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000565 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000566 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000567 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000568 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000569#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko56fce002007-04-04 23:41:35 +0000570 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000571 /* also handles "find ! ! -name 'foo*'" */
572 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000573 }
574#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000575
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000576 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000577 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000578 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000579 /* GNU find ignores '!' here: "find ! -print" */
580 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000581 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000582 }
583#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000584 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000585 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000586 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000587 (void) ALLOC_ACTION(print0);
588 }
Paul Foxd7384292006-05-12 14:47:20 +0000589#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000590#if ENABLE_FEATURE_FIND_DEPTH
591 else if (parm == PARM_depth) {
592 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000593 }
594#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000595#if ENABLE_FEATURE_FIND_PRUNE
596 else if (parm == PARM_prune) {
597 USE_FEATURE_FIND_NOT( invert_flag = 0; )
598 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000599 }
600#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000601#if ENABLE_FEATURE_FIND_DELETE
602 else if (parm == PARM_delete) {
603 need_print = 0;
604 recurse_flags |= ACTION_DEPTHFIRST;
605 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000606 }
Rob Landleycee605c2005-10-06 16:39:17 +0000607#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000608#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000609 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000610 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000611 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000612 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000613 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000614 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000615 ap->exec_argv = ++argv; /* first arg after -exec */
616 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000617 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000618 if (!*argv) /* did not see ';' until end */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000619 bb_error_msg_and_die("-exec CMD must end by ';'");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000620 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000621 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000622 argv++;
623 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000624 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000625 if (ap->exec_argc == 0)
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->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000628 i = ap->exec_argc;
629 while (i--)
630 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
631 }
632#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000633#if ENABLE_FEATURE_FIND_PAREN
634 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000635 action_paren *ap;
636 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000637 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000638
639 endarg = argv;
640 while (1) {
641 if (!*++endarg)
642 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000643 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000644 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000645 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000646 *endarg = NULL;
647 break;
648 }
649 }
650 ap = ALLOC_ACTION(paren);
651 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000652 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000653 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000654 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000655#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000656 else if (parm == PARM_name) {
657 action_name *ap;
658 ap = ALLOC_ACTION(name);
659 ap->pattern = arg1;
660 }
661#if ENABLE_FEATURE_FIND_PATH
662 else if (parm == PARM_path) {
663 action_path *ap;
664 ap = ALLOC_ACTION(path);
665 ap->pattern = arg1;
666 }
667#endif
668#if ENABLE_FEATURE_FIND_REGEX
669 else if (parm == PARM_regex) {
670 action_regex *ap;
671 ap = ALLOC_ACTION(regex);
672 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
673 }
674#endif
675#if ENABLE_FEATURE_FIND_TYPE
676 else if (parm == PARM_type) {
677 action_type *ap;
678 ap = ALLOC_ACTION(type);
679 ap->type_mask = find_type(arg1);
680 }
681#endif
682#if ENABLE_FEATURE_FIND_PERM
683/* -perm mode File's permission bits are exactly mode (octal or symbolic).
684 * Symbolic modes use mode 0 as a point of departure.
685 * -perm -mode All of the permission bits mode are set for the file.
686 * -perm +mode Any of the permission bits mode are set for the file.
687 */
688 else if (parm == PARM_perm) {
689 action_perm *ap;
690 ap = ALLOC_ACTION(perm);
691 ap->perm_char = arg1[0];
692 arg1 = plus_minus_num(arg1);
693 ap->perm_mask = 0;
694 if (!bb_parse_mode(arg1, &ap->perm_mask))
695 bb_error_msg_and_die("invalid mode: %s", arg1);
696 }
697#endif
698#if ENABLE_FEATURE_FIND_MTIME
699 else if (parm == PARM_mtime) {
700 action_mtime *ap;
701 ap = ALLOC_ACTION(mtime);
702 ap->mtime_char = arg1[0];
703 ap->mtime_days = xatoul(plus_minus_num(arg1));
704 }
705#endif
706#if ENABLE_FEATURE_FIND_MMIN
707 else if (parm == PARM_mmin) {
708 action_mmin *ap;
709 ap = ALLOC_ACTION(mmin);
710 ap->mmin_char = arg1[0];
711 ap->mmin_mins = xatoul(plus_minus_num(arg1));
712 }
713#endif
714#if ENABLE_FEATURE_FIND_NEWER
715 else if (parm == PARM_newer) {
716 struct stat stat_newer;
717 action_newer *ap;
718 ap = ALLOC_ACTION(newer);
719 xstat(arg1, &stat_newer);
720 ap->newer_mtime = stat_newer.st_mtime;
721 }
722#endif
723#if ENABLE_FEATURE_FIND_INUM
724 else if (parm == PARM_inum) {
725 action_inum *ap;
726 ap = ALLOC_ACTION(inum);
727 ap->inode_num = xatoul(arg1);
728 }
729#endif
730#if ENABLE_FEATURE_FIND_USER
731 else if (parm == PARM_user) {
732 action_user *ap;
733 ap = ALLOC_ACTION(user);
734 ap->uid = bb_strtou(arg1, NULL, 10);
735 if (errno)
736 ap->uid = xuname2uid(arg1);
737 }
738#endif
739#if ENABLE_FEATURE_FIND_GROUP
740 else if (parm == PARM_group) {
741 action_group *ap;
742 ap = ALLOC_ACTION(group);
743 ap->gid = bb_strtou(arg1, NULL, 10);
744 if (errno)
745 ap->gid = xgroup2gid(arg1);
746 }
747#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000748#if ENABLE_FEATURE_FIND_SIZE
749 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000750/* -size n[bckw]: file uses n units of space
751 * b (default): units are 512-byte blocks
752 * c: 1 byte
753 * k: kilobytes
754 * w: 2-byte words
755 */
756#if ENABLE_LFS
757#define XATOU_SFX xatoull_sfx
758#else
759#define XATOU_SFX xatoul_sfx
760#endif
761 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000762 { "c", 1 },
763 { "w", 2 },
764 { "b"+1, 512 },
765 { "b", 512 },
766 { "k", 1024 },
767 { NULL, 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000768 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000769 action_size *ap;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000770 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000771 ap->size_char = arg1[0];
772 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000773 }
774#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000775#if ENABLE_FEATURE_FIND_CONTEXT
776 else if (parm == PARM_context) {
777 action_context *ap;
778 ap = ALLOC_ACTION(context);
779 ap->context = NULL;
780 if (selinux_raw_to_trans_context(arg1, &ap->context))
781 bb_perror_msg("%s", arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000782 }
783#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000784 else {
785 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000786 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000787 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000788 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000789 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000790 return appp;
791#undef ALLOC_ACTION
792}
793
794
Denis Vlasenko06af2162007-02-03 17:28:39 +0000795int find_main(int argc, char **argv);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000796int find_main(int argc, char **argv)
797{
Denis Vlasenkob9411292007-06-15 23:43:11 +0000798 static const char *const options[] = {
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000799 "-follow",
Denis Vlasenkob9411292007-06-15 23:43:11 +0000800USE_FEATURE_FIND_XDEV( "-xdev" ,)
801USE_FEATURE_FIND_MAXDEPTH("-maxdepth",)
Bernhard Reutner-Fischera87ed2c2007-03-29 13:56:49 +0000802 NULL
803 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000804 enum {
805 OPT_FOLLOW,
806USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
807USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
808 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000809
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000810 char *arg;
811 char **argp;
812 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000813#if ENABLE_FEATURE_FIND_MAXDEPTH
814 int maxdepth = INT_MAX;
815#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000816
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000817 for (firstopt = 1; firstopt < argc; firstopt++) {
818 if (argv[firstopt][0] == '-')
819 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000820 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
821 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000822#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000823 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000824 break;
825#endif
826 }
827 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000828 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000829 argv--;
830 firstopt++;
831 }
832
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000833/* All options always return true. They always take effect
834 * rather than being processed only when their place in the
835 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000836 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000837 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000838 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000839 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000840 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000841 while ((arg = argp[0])) {
Denis Vlasenkob9411292007-06-15 23:43:11 +0000842 int opt = index_in_str_array(options, arg);
843 if (opt == OPT_FOLLOW) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000844 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000845 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000846 }
847#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000848 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000849 struct stat stbuf;
850 if (!xdev_count) {
851 xdev_count = firstopt - 1;
852 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
853 for (i = 1; i < firstopt; i++) {
854 /* not xstat(): shouldn't bomb out on
855 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000856 if (stat(argv[i], &stbuf))
857 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000858 xdev_dev[i-1] = stbuf.st_dev;
859 }
860 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000861 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000862 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000863#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000864#if ENABLE_FEATURE_FIND_MAXDEPTH
865 if (opt == OPT_MAXDEPTH) {
866 if (!argp[1])
867 bb_show_usage();
868 maxdepth = xatoi_u(argp[1]);
869 argp[0] = (char*)"-a";
870 argp[1] = (char*)"-a";
871 argp++;
872 }
873#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000874 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000875 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000876
877 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000878
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000879 for (i = 1; i < firstopt; i++) {
880 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000881 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000882 fileAction, /* file action */
883 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000884#if ENABLE_FEATURE_FIND_MAXDEPTH
885 (void*)maxdepth,/* user data */
886#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000887 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000888#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000889 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000890 status = EXIT_FAILURE;
891 }
Matt Kraai096370d2001-02-07 03:52:38 +0000892 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000893}