blob: 8c0397798b2ee0e5857dfe605a18313bb12f9c87 [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)
Denis Vlasenko75aa6152007-12-09 10:07:39 +000079 ACTS(name, const char *pattern; bool iname;)
Denis Vlasenkoa19faf82007-06-19 22:22:57 +000080USE_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 Vlasenko75aa6152007-12-09 10:07:39 +0000191 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000192}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000193
Denis Vlasenkob9411292007-06-15 23:43:11 +0000194#if ENABLE_FEATURE_FIND_PATH
195ACTF(path)
196{
197 return fnmatch(ap->pattern, fileName, 0) == 0;
198}
199#endif
200#if ENABLE_FEATURE_FIND_REGEX
201ACTF(regex)
202{
203 regmatch_t match;
204 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
205 return 0; /* no match */
206 if (match.rm_so)
207 return 0; /* match doesn't start at pos 0 */
208 if (fileName[match.rm_eo])
209 return 0; /* match doesn't end exactly at end of pathname */
210 return 1;
211}
212#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000213#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000214ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000215{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000216 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000217}
Rob Landleycee605c2005-10-06 16:39:17 +0000218#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000219#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000220ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000221{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000222 /* -perm +mode: at least one of perm_mask bits are set */
223 if (ap->perm_char == '+')
224 return (statbuf->st_mode & ap->perm_mask) != 0;
225 /* -perm -mode: all of perm_mask are set */
226 if (ap->perm_char == '-')
227 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
228 /* -perm mode: file mode must match perm_mask */
229 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000230}
Rob Landleycee605c2005-10-06 16:39:17 +0000231#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000232#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000233ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000234{
235 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000236 time_t mtime_secs = ap->mtime_days * 24*60*60;
237 if (ap->mtime_char == '+')
238 return file_age >= mtime_secs + 24*60*60;
239 if (ap->mtime_char == '-')
240 return file_age < mtime_secs;
241 /* just numeric mtime */
242 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000243}
Rob Landleycee605c2005-10-06 16:39:17 +0000244#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000245#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000246ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000247{
248 time_t file_age = time(NULL) - statbuf->st_mtime;
249 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000250 if (ap->mmin_char == '+')
251 return file_age >= mmin_secs + 60;
252 if (ap->mmin_char == '-')
253 return file_age < mmin_secs;
254 /* just numeric mmin */
255 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000256}
Paul Fox72d1a232006-01-13 21:05:41 +0000257#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000258#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000259ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000260{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000261 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000262}
Rob Landleycee605c2005-10-06 16:39:17 +0000263#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000264#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000265ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000266{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000267 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000268}
Rob Landleycee605c2005-10-06 16:39:17 +0000269#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000270#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000271ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000272{
273 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000274 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000275 for (i = 0; i < ap->exec_argc; i++)
276 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
277 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000278
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000279 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000280 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000281 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000282
283 i = 0;
284 while (argv[i])
285 free(argv[i++]);
286 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000287}
Rob Landleycee605c2005-10-06 16:39:17 +0000288#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000289#if ENABLE_FEATURE_FIND_USER
290ACTF(user)
291{
292 return (statbuf->st_uid == ap->uid);
293}
294#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000295#if ENABLE_FEATURE_FIND_GROUP
296ACTF(group)
297{
298 return (statbuf->st_gid == ap->gid);
299}
300#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000301#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000302ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000303{
304 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000305 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000306}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000307#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000308ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000309{
310 puts(fileName);
311 return TRUE;
312}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000313#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000314ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000315{
316 return exec_actions(ap->subexpr, fileName, statbuf);
317}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000318#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000319#if ENABLE_FEATURE_FIND_SIZE
320ACTF(size)
321{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000322 if (ap->size_char == '+')
323 return statbuf->st_size > ap->size;
324 if (ap->size_char == '-')
325 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000326 return statbuf->st_size == ap->size;
327}
328#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000329#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000330/*
331 * -prune: if -depth is not given, return true and do not descend
332 * current dir; if -depth is given, return false with no effect.
333 * Example:
334 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
335 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000336ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000337{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000338 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000339}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000340#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000341#if ENABLE_FEATURE_FIND_DELETE
342ACTF(delete)
343{
344 int rc;
345 if (S_ISDIR(statbuf->st_mode)) {
346 rc = rmdir(fileName);
347 } else {
348 rc = unlink(fileName);
349 }
350 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000351 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000352 return TRUE;
353}
354#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000355#if ENABLE_FEATURE_FIND_CONTEXT
356ACTF(context)
357{
358 security_context_t con;
359 int rc;
360
361 if (recurse_flags & ACTION_FOLLOWLINKS) {
362 rc = getfilecon(fileName, &con);
363 } else {
364 rc = lgetfilecon(fileName, &con);
365 }
366 if (rc < 0)
367 return FALSE;
368 rc = strcmp(ap->context, con);
369 freecon(con);
370 return rc == 0;
371}
372#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000373
Denis Vlasenko62f04792007-04-13 10:00:12 +0000374
Denis Vlasenkob9411292007-06-15 23:43:11 +0000375static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000376{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000377 int i;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000378#if ENABLE_FEATURE_FIND_MAXDEPTH
379 int maxdepth = (int)(ptrdiff_t)userData;
380
381 if (depth > maxdepth) return SKIP;
382#endif
383
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000384#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000385 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000386 for (i = 0; i < xdev_count; i++) {
Paul Foxdcf9bba2007-09-06 17:45:16 +0000387 if (xdev_dev[i] == statbuf->st_dev)
388 break;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000389 }
Paul Foxdcf9bba2007-09-06 17:45:16 +0000390 if (i == xdev_count)
391 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000392 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000393#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000394 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000395 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000396 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000397 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000398 /* Cannot return 0: our caller, recursive_action(),
399 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000400 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000401}
402
Eric Andersen17d49ef1999-10-06 20:25:32 +0000403
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000404#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000405static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000406{
407 int mask = 0;
408
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000409 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000410 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000411 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000412 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000413 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000414 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000415 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000416 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000417 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000418 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000419 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000420 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000421 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000422 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000423
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000424 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000425 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000426
427 return mask;
428}
429#endif
430
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000431#if ENABLE_FEATURE_FIND_PERM \
432 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
433 || ENABLE_FEATURE_FIND_SIZE
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000434static const char* plus_minus_num(const char* str)
435{
436 if (*str == '-' || *str == '+')
437 str++;
438 return str;
439}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000440#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000441
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000442static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000443{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000444 enum {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000445 PARM_a ,
446 PARM_o ,
447 USE_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000448#if ENABLE_DESKTOP
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000449 PARM_and ,
450 PARM_or ,
451 USE_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000452#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000453 PARM_print ,
454 USE_FEATURE_FIND_PRINT0( PARM_print0 ,)
455 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
456 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
457 USE_FEATURE_FIND_DELETE( PARM_delete ,)
458 USE_FEATURE_FIND_EXEC( PARM_exec ,)
459 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
460 /* All options starting from here require argument */
461 PARM_name ,
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000462 PARM_iname ,
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000463 USE_FEATURE_FIND_PATH( PARM_path ,)
464 USE_FEATURE_FIND_REGEX( PARM_regex ,)
465 USE_FEATURE_FIND_TYPE( PARM_type ,)
466 USE_FEATURE_FIND_PERM( PARM_perm ,)
467 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
468 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
469 USE_FEATURE_FIND_NEWER( PARM_newer ,)
470 USE_FEATURE_FIND_INUM( PARM_inum ,)
471 USE_FEATURE_FIND_USER( PARM_user ,)
472 USE_FEATURE_FIND_GROUP( PARM_group ,)
473 USE_FEATURE_FIND_SIZE( PARM_size ,)
474 USE_FEATURE_FIND_CONTEXT(PARM_context ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000475 };
476
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000477 static const char params[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000478 "-a\0"
479 "-o\0"
480 USE_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000481#if ENABLE_DESKTOP
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000482 "-and\0"
483 "-or\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000484 USE_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000485#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000486 "-print\0"
487 USE_FEATURE_FIND_PRINT0( "-print0\0" )
488 USE_FEATURE_FIND_DEPTH( "-depth\0" )
489 USE_FEATURE_FIND_PRUNE( "-prune\0" )
490 USE_FEATURE_FIND_DELETE( "-delete\0" )
491 USE_FEATURE_FIND_EXEC( "-exec\0" )
492 USE_FEATURE_FIND_PAREN( "(\0" )
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000493 /* All options starting from here require argument */
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000494 "-name\0"
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000495 "-iname\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000496 USE_FEATURE_FIND_PATH( "-path\0" )
497 USE_FEATURE_FIND_REGEX( "-regex\0" )
498 USE_FEATURE_FIND_TYPE( "-type\0" )
499 USE_FEATURE_FIND_PERM( "-perm\0" )
500 USE_FEATURE_FIND_MTIME( "-mtime\0" )
501 USE_FEATURE_FIND_MMIN( "-mmin\0" )
502 USE_FEATURE_FIND_NEWER( "-newer\0" )
503 USE_FEATURE_FIND_INUM( "-inum\0" )
504 USE_FEATURE_FIND_USER( "-user\0" )
505 USE_FEATURE_FIND_GROUP( "-group\0" )
506 USE_FEATURE_FIND_SIZE( "-size\0" )
507 USE_FEATURE_FIND_CONTEXT("-context\0")
508 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000509
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000510 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000511 unsigned cur_group = 0;
512 unsigned cur_action = 0;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000513 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000514
515 /* 'static' doesn't work here! (gcc 4.1.2) */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000516 action* alloc_action(int sizeof_struct, action_fp f)
517 {
518 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000519 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
520 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
521 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000522 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000523 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000524 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000525 return ap;
526 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000527
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000528#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000529
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000530 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000531
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000532/* Actions have side effects and return a true or false value
533 * We implement: -print, -print0, -exec
534 *
535 * The rest are tests.
536 *
537 * Tests and actions are grouped by operators
538 * ( expr ) Force precedence
539 * ! expr True if expr is false
540 * -not expr Same as ! expr
541 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
542 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
543 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
544 * We implement: (), -a, -o
545 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000546 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000547 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000548 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000549 const char *arg1 = argv[1];
550
551 if (parm >= PARM_name) {
552 /* All options starting from -name require argument */
553 if (!arg1)
554 bb_error_msg_and_die(bb_msg_requires_arg, arg);
555 argv++;
556 }
557
558 /* We can use big switch() here, but on i386
559 * it doesn't give smaller code. Other arches? */
560
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000561 /* --- Operators --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000562 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000563 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000564 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000565 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000566 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000567 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000568 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000569 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000570 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000571 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000572 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000573#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko56fce002007-04-04 23:41:35 +0000574 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000575 /* also handles "find ! ! -name 'foo*'" */
576 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000577 }
578#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000579
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000580 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000581 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000582 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000583 /* GNU find ignores '!' here: "find ! -print" */
584 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000585 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000586 }
587#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000588 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000589 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000590 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000591 (void) ALLOC_ACTION(print0);
592 }
Paul Foxd7384292006-05-12 14:47:20 +0000593#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000594#if ENABLE_FEATURE_FIND_DEPTH
595 else if (parm == PARM_depth) {
596 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000597 }
598#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000599#if ENABLE_FEATURE_FIND_PRUNE
600 else if (parm == PARM_prune) {
601 USE_FEATURE_FIND_NOT( invert_flag = 0; )
602 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000603 }
604#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000605#if ENABLE_FEATURE_FIND_DELETE
606 else if (parm == PARM_delete) {
607 need_print = 0;
608 recurse_flags |= ACTION_DEPTHFIRST;
609 (void) ALLOC_ACTION(delete);
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_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000613 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000614 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000615 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000616 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000617 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000618 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000619 ap->exec_argv = ++argv; /* first arg after -exec */
620 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000621 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000622 if (!*argv) /* did not see ';' until end */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000623 bb_error_msg_and_die("-exec CMD must end by ';'");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000624 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000625 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000626 argv++;
627 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000628 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000629 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000630 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000631 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000632 i = ap->exec_argc;
633 while (i--)
634 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
635 }
636#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000637#if ENABLE_FEATURE_FIND_PAREN
638 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000639 action_paren *ap;
640 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000641 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000642
643 endarg = argv;
644 while (1) {
645 if (!*++endarg)
646 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000647 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000648 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000649 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000650 *endarg = NULL;
651 break;
652 }
653 }
654 ap = ALLOC_ACTION(paren);
655 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000656 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000657 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000658 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000659#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000660 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000661 action_name *ap;
662 ap = ALLOC_ACTION(name);
663 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000664 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000665 }
666#if ENABLE_FEATURE_FIND_PATH
667 else if (parm == PARM_path) {
668 action_path *ap;
669 ap = ALLOC_ACTION(path);
670 ap->pattern = arg1;
671 }
672#endif
673#if ENABLE_FEATURE_FIND_REGEX
674 else if (parm == PARM_regex) {
675 action_regex *ap;
676 ap = ALLOC_ACTION(regex);
677 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
678 }
679#endif
680#if ENABLE_FEATURE_FIND_TYPE
681 else if (parm == PARM_type) {
682 action_type *ap;
683 ap = ALLOC_ACTION(type);
684 ap->type_mask = find_type(arg1);
685 }
686#endif
687#if ENABLE_FEATURE_FIND_PERM
688/* -perm mode File's permission bits are exactly mode (octal or symbolic).
689 * Symbolic modes use mode 0 as a point of departure.
690 * -perm -mode All of the permission bits mode are set for the file.
691 * -perm +mode Any of the permission bits mode are set for the file.
692 */
693 else if (parm == PARM_perm) {
694 action_perm *ap;
695 ap = ALLOC_ACTION(perm);
696 ap->perm_char = arg1[0];
697 arg1 = plus_minus_num(arg1);
698 ap->perm_mask = 0;
699 if (!bb_parse_mode(arg1, &ap->perm_mask))
700 bb_error_msg_and_die("invalid mode: %s", arg1);
701 }
702#endif
703#if ENABLE_FEATURE_FIND_MTIME
704 else if (parm == PARM_mtime) {
705 action_mtime *ap;
706 ap = ALLOC_ACTION(mtime);
707 ap->mtime_char = arg1[0];
708 ap->mtime_days = xatoul(plus_minus_num(arg1));
709 }
710#endif
711#if ENABLE_FEATURE_FIND_MMIN
712 else if (parm == PARM_mmin) {
713 action_mmin *ap;
714 ap = ALLOC_ACTION(mmin);
715 ap->mmin_char = arg1[0];
716 ap->mmin_mins = xatoul(plus_minus_num(arg1));
717 }
718#endif
719#if ENABLE_FEATURE_FIND_NEWER
720 else if (parm == PARM_newer) {
721 struct stat stat_newer;
722 action_newer *ap;
723 ap = ALLOC_ACTION(newer);
724 xstat(arg1, &stat_newer);
725 ap->newer_mtime = stat_newer.st_mtime;
726 }
727#endif
728#if ENABLE_FEATURE_FIND_INUM
729 else if (parm == PARM_inum) {
730 action_inum *ap;
731 ap = ALLOC_ACTION(inum);
732 ap->inode_num = xatoul(arg1);
733 }
734#endif
735#if ENABLE_FEATURE_FIND_USER
736 else if (parm == PARM_user) {
737 action_user *ap;
738 ap = ALLOC_ACTION(user);
739 ap->uid = bb_strtou(arg1, NULL, 10);
740 if (errno)
741 ap->uid = xuname2uid(arg1);
742 }
743#endif
744#if ENABLE_FEATURE_FIND_GROUP
745 else if (parm == PARM_group) {
746 action_group *ap;
747 ap = ALLOC_ACTION(group);
748 ap->gid = bb_strtou(arg1, NULL, 10);
749 if (errno)
750 ap->gid = xgroup2gid(arg1);
751 }
752#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000753#if ENABLE_FEATURE_FIND_SIZE
754 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000755/* -size n[bckw]: file uses n units of space
756 * b (default): units are 512-byte blocks
757 * c: 1 byte
758 * k: kilobytes
759 * w: 2-byte words
760 */
761#if ENABLE_LFS
762#define XATOU_SFX xatoull_sfx
763#else
764#define XATOU_SFX xatoul_sfx
765#endif
766 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000767 { "c", 1 },
768 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000769 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000770 { "b", 512 },
771 { "k", 1024 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000772 { }
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000773 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000774 action_size *ap;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000775 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000776 ap->size_char = arg1[0];
777 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000778 }
779#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000780#if ENABLE_FEATURE_FIND_CONTEXT
781 else if (parm == PARM_context) {
782 action_context *ap;
783 ap = ALLOC_ACTION(context);
784 ap->context = NULL;
Denis Vlasenko838daca2007-08-16 10:41:24 +0000785 /* SELinux headers erroneously declare non-const parameter */
786 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000787 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000788 }
789#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000790 else {
791 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000792 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000793 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000794 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000795 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000796 return appp;
797#undef ALLOC_ACTION
798}
799
800
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000801int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000802int find_main(int argc, char **argv)
803{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000804 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000805 "-follow\0"
806USE_FEATURE_FIND_XDEV( "-xdev\0" )
807USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
808 ;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000809 enum {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000810 OPT_FOLLOW,
Denis Vlasenkob9411292007-06-15 23:43:11 +0000811USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
812USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
813 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000814
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000815 char *arg;
816 char **argp;
817 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000818#if ENABLE_FEATURE_FIND_MAXDEPTH
819 int maxdepth = INT_MAX;
820#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000821
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000822 for (firstopt = 1; firstopt < argc; firstopt++) {
823 if (argv[firstopt][0] == '-')
824 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000825 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
826 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000827#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000828 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000829 break;
830#endif
831 }
832 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000833 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000834 argv--;
835 firstopt++;
836 }
837
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000838/* All options always return true. They always take effect
839 * rather than being processed only when their place in the
840 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000841 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000842 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000843 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000844 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000845 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000846 while ((arg = argp[0])) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000847 int opt = index_in_strings(options, arg);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000848 if (opt == OPT_FOLLOW) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000849 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000850 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000851 }
852#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000853 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000854 struct stat stbuf;
855 if (!xdev_count) {
856 xdev_count = firstopt - 1;
857 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
858 for (i = 1; i < firstopt; i++) {
859 /* not xstat(): shouldn't bomb out on
860 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000861 if (stat(argv[i], &stbuf))
862 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000863 xdev_dev[i-1] = stbuf.st_dev;
864 }
865 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000866 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000867 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000868#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000869#if ENABLE_FEATURE_FIND_MAXDEPTH
870 if (opt == OPT_MAXDEPTH) {
871 if (!argp[1])
872 bb_show_usage();
873 maxdepth = xatoi_u(argp[1]);
874 argp[0] = (char*)"-a";
875 argp[1] = (char*)"-a";
876 argp++;
877 }
878#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000879 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000880 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000881
882 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000883
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000884 for (i = 1; i < firstopt; i++) {
885 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000886 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000887 fileAction, /* file action */
888 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000889#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000890 /* double cast suppresses
891 * "cast to ptr from int of different size" */
892 (void*)(ptrdiff_t)maxdepth,/* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000893#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000894 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000895#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000896 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000897 status = EXIT_FAILURE;
898 }
Matt Kraai096370d2001-02-07 03:52:38 +0000899 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000900}