blob: 5e8193ffa487030672c255f4495a65be788aaf52 [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 Vlasenko5e34ff22009-04-21 11:09:40 +000065IF_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
66IF_FEATURE_FIND_XDEV(static int xdev_count;)
Matt Kraai096370d2001-02-07 03:52:38 +000067
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020068typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *) FAST_FUNC;
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;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020076
Denis Vlasenko92258542006-11-01 10:25:35 +000077#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020078#define ACTF(name) \
79 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
80 struct stat *statbuf UNUSED_PARAM, \
81 action_##name* ap UNUSED_PARAM)
82
83 ACTS(print)
84 ACTS(name, const char *pattern; bool iname;)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000085IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
86IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
87IF_FEATURE_FIND_PRINT0( ACTS(print0))
88IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
89IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
90IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
91IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
92IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
93IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
94IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
95IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
96IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
97IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
98IF_FEATURE_FIND_PRUNE( ACTS(prune))
99IF_FEATURE_FIND_DELETE( ACTS(delete))
100IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
101IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matt Kraai096370d2001-02-07 03:52:38 +0000102
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000103static action ***actions;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000104static bool need_print = 1;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000105static int recurse_flags = ACTION_RECURSE;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000106
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000107#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000108static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000109{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000110 unsigned count = 0;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000111 while ((str = strstr(str, "{}")) != NULL) {
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000112 count++;
113 str++;
114 }
115 return count;
116}
117
118
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000119static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000120{
121 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000122 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000123 /* we replace each '{}' with filename: growth by strlen-2 */
124 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
125 while ((end = strstr(src, "{}"))) {
126 memcpy(dst, src, end - src);
127 dst += end - src;
128 src = end + 2;
129 memcpy(dst, filename, flen);
130 dst += flen;
131 }
132 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000133 return buf;
134}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000135#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000136
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000137/* Return values of ACTFs ('action functions') are a bit mask:
138 * bit 1=1: prune (use SKIP constant for setting it)
139 * bit 0=1: matched successfully (TRUE)
140 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000141
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000142static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
143{
144 int cur_group;
145 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000146 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000147 action **app, *ap;
148
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000149 /* "action group" is a set of actions ANDed together.
150 * groups are ORed together.
151 * We simply evaluate each group until we find one in which all actions
152 * succeed. */
153
154 /* -prune is special: if it is encountered, then we won't
155 * descend into current directory. It doesn't matter whether
156 * action group (in which -prune sits) will succeed or not:
157 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
158 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
159 * not starting with 'f' */
160
161 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
162 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
163 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
164 * On return, bit is restored. */
165
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000166 cur_group = -1;
167 while ((app = appp[++cur_group])) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000168 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000169 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000170 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000171 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000172 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000173 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000174 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000175#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000176 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000177#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000178 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000179 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000180 }
181 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000182 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000183}
184
185
Denis Vlasenko92258542006-11-01 10:25:35 +0000186ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000187{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000188 const char *tmp = bb_basename(fileName);
189 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
190 tmp--;
191 while (tmp != fileName && *--tmp != '/')
192 continue;
193 if (*tmp == '/')
194 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000195 }
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000196 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000197}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000198
Denis Vlasenkob9411292007-06-15 23:43:11 +0000199#if ENABLE_FEATURE_FIND_PATH
200ACTF(path)
201{
202 return fnmatch(ap->pattern, fileName, 0) == 0;
203}
204#endif
205#if ENABLE_FEATURE_FIND_REGEX
206ACTF(regex)
207{
208 regmatch_t match;
209 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
210 return 0; /* no match */
211 if (match.rm_so)
212 return 0; /* match doesn't start at pos 0 */
213 if (fileName[match.rm_eo])
214 return 0; /* match doesn't end exactly at end of pathname */
215 return 1;
216}
217#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000218#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000219ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000220{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000221 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000222}
Rob Landleycee605c2005-10-06 16:39:17 +0000223#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000224#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000225ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000226{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000227 /* -perm +mode: at least one of perm_mask bits are set */
228 if (ap->perm_char == '+')
229 return (statbuf->st_mode & ap->perm_mask) != 0;
230 /* -perm -mode: all of perm_mask are set */
231 if (ap->perm_char == '-')
232 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
233 /* -perm mode: file mode must match perm_mask */
234 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000235}
Rob Landleycee605c2005-10-06 16:39:17 +0000236#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000237#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000238ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000239{
240 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000241 time_t mtime_secs = ap->mtime_days * 24*60*60;
242 if (ap->mtime_char == '+')
243 return file_age >= mtime_secs + 24*60*60;
244 if (ap->mtime_char == '-')
245 return file_age < mtime_secs;
246 /* just numeric mtime */
247 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000248}
Rob Landleycee605c2005-10-06 16:39:17 +0000249#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000250#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000251ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000252{
253 time_t file_age = time(NULL) - statbuf->st_mtime;
254 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000255 if (ap->mmin_char == '+')
256 return file_age >= mmin_secs + 60;
257 if (ap->mmin_char == '-')
258 return file_age < mmin_secs;
259 /* just numeric mmin */
260 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000261}
Paul Fox72d1a232006-01-13 21:05:41 +0000262#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000263#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000264ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000265{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000266 return (ap->newer_mtime < statbuf->st_mtime);
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_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000270ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000271{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000272 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000273}
Rob Landleycee605c2005-10-06 16:39:17 +0000274#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000275#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000276ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000277{
278 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000279 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000280 for (i = 0; i < ap->exec_argc; i++)
281 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
282 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000283
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000284 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000285 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000286 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000287
288 i = 0;
289 while (argv[i])
290 free(argv[i++]);
291 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000292}
Rob Landleycee605c2005-10-06 16:39:17 +0000293#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000294#if ENABLE_FEATURE_FIND_USER
295ACTF(user)
296{
297 return (statbuf->st_uid == ap->uid);
298}
299#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000300#if ENABLE_FEATURE_FIND_GROUP
301ACTF(group)
302{
303 return (statbuf->st_gid == ap->gid);
304}
305#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000306#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000307ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000308{
309 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000310 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000311}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000312#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000313ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000314{
315 puts(fileName);
316 return TRUE;
317}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000318#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000319ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000320{
321 return exec_actions(ap->subexpr, fileName, statbuf);
322}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000323#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000324#if ENABLE_FEATURE_FIND_SIZE
325ACTF(size)
326{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000327 if (ap->size_char == '+')
328 return statbuf->st_size > ap->size;
329 if (ap->size_char == '-')
330 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000331 return statbuf->st_size == ap->size;
332}
333#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000334#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000335/*
336 * -prune: if -depth is not given, return true and do not descend
337 * current dir; if -depth is given, return false with no effect.
338 * Example:
339 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
340 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000341ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000342{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000343 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000344}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000345#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000346#if ENABLE_FEATURE_FIND_DELETE
347ACTF(delete)
348{
349 int rc;
350 if (S_ISDIR(statbuf->st_mode)) {
351 rc = rmdir(fileName);
352 } else {
353 rc = unlink(fileName);
354 }
355 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000356 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000357 return TRUE;
358}
359#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000360#if ENABLE_FEATURE_FIND_CONTEXT
361ACTF(context)
362{
363 security_context_t con;
364 int rc;
365
366 if (recurse_flags & ACTION_FOLLOWLINKS) {
367 rc = getfilecon(fileName, &con);
368 } else {
369 rc = lgetfilecon(fileName, &con);
370 }
371 if (rc < 0)
372 return FALSE;
373 rc = strcmp(ap->context, con);
374 freecon(con);
375 return rc == 0;
376}
377#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000378
Denis Vlasenko62f04792007-04-13 10:00:12 +0000379
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000380static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000381 struct stat *statbuf,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000382 void *userData IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
383 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000384{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000385 int i;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000386#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000387#define minmaxdepth ((int*)userData)
Denis Vlasenkob9411292007-06-15 23:43:11 +0000388
Denis Vlasenko83518d12009-03-20 22:17:13 +0000389 if (depth < minmaxdepth[0]) return TRUE;
390 if (depth > minmaxdepth[1]) return SKIP;
391#undef minmaxdepth
Denis Vlasenkob9411292007-06-15 23:43:11 +0000392#endif
393
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000394#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000395 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000396 for (i = 0; i < xdev_count; i++) {
Paul Foxdcf9bba2007-09-06 17:45:16 +0000397 if (xdev_dev[i] == statbuf->st_dev)
398 break;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000399 }
Paul Foxdcf9bba2007-09-06 17:45:16 +0000400 if (i == xdev_count)
401 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000402 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000403#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000404 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000405 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000406 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000407 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000408 /* Cannot return 0: our caller, recursive_action(),
409 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000410 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000411}
412
Eric Andersen17d49ef1999-10-06 20:25:32 +0000413
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000414#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000415static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000416{
417 int mask = 0;
418
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000419 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000420 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000421 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000422 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000423 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000424 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000425 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000426 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000427 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000428 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000429 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000430 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000431 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000432 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000433
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000434 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000435 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000436
437 return mask;
438}
439#endif
440
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000441#if ENABLE_FEATURE_FIND_PERM \
442 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
443 || ENABLE_FEATURE_FIND_SIZE
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000444static const char* plus_minus_num(const char* str)
445{
446 if (*str == '-' || *str == '+')
447 str++;
448 return str;
449}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000450#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000451
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000452static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000453{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000454 enum {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000455 PARM_a ,
456 PARM_o ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000457 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000458#if ENABLE_DESKTOP
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000459 PARM_and ,
460 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000461 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000462#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000463 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000464 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
465 IF_FEATURE_FIND_DEPTH( PARM_depth ,)
466 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
467 IF_FEATURE_FIND_DELETE( PARM_delete ,)
468 IF_FEATURE_FIND_EXEC( PARM_exec ,)
469 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000470 /* All options starting from here require argument */
471 PARM_name ,
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000472 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000473 IF_FEATURE_FIND_PATH( PARM_path ,)
474 IF_FEATURE_FIND_REGEX( PARM_regex ,)
475 IF_FEATURE_FIND_TYPE( PARM_type ,)
476 IF_FEATURE_FIND_PERM( PARM_perm ,)
477 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
478 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
479 IF_FEATURE_FIND_NEWER( PARM_newer ,)
480 IF_FEATURE_FIND_INUM( PARM_inum ,)
481 IF_FEATURE_FIND_USER( PARM_user ,)
482 IF_FEATURE_FIND_GROUP( PARM_group ,)
483 IF_FEATURE_FIND_SIZE( PARM_size ,)
484 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000485 };
486
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000487 static const char params[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000488 "-a\0"
489 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000490 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000491#if ENABLE_DESKTOP
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000492 "-and\0"
493 "-or\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000494 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000495#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000496 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000497 IF_FEATURE_FIND_PRINT0( "-print0\0" )
498 IF_FEATURE_FIND_DEPTH( "-depth\0" )
499 IF_FEATURE_FIND_PRUNE( "-prune\0" )
500 IF_FEATURE_FIND_DELETE( "-delete\0" )
501 IF_FEATURE_FIND_EXEC( "-exec\0" )
502 IF_FEATURE_FIND_PAREN( "(\0" )
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000503 /* All options starting from here require argument */
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000504 "-name\0"
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000505 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000506 IF_FEATURE_FIND_PATH( "-path\0" )
507 IF_FEATURE_FIND_REGEX( "-regex\0" )
508 IF_FEATURE_FIND_TYPE( "-type\0" )
509 IF_FEATURE_FIND_PERM( "-perm\0" )
510 IF_FEATURE_FIND_MTIME( "-mtime\0" )
511 IF_FEATURE_FIND_MMIN( "-mmin\0" )
512 IF_FEATURE_FIND_NEWER( "-newer\0" )
513 IF_FEATURE_FIND_INUM( "-inum\0" )
514 IF_FEATURE_FIND_USER( "-user\0" )
515 IF_FEATURE_FIND_GROUP( "-group\0" )
516 IF_FEATURE_FIND_SIZE( "-size\0" )
517 IF_FEATURE_FIND_CONTEXT("-context\0")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000518 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000519
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000520 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000521 unsigned cur_group = 0;
522 unsigned cur_action = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000523 IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000524
Denis Vlasenko0f3a5802008-03-20 13:13:09 +0000525 /* This is the only place in busybox where we use nested function.
526 * So far more standard alternatives were bigger. */
527 /* Suppress a warning "func without a prototype" */
528 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000529 action* alloc_action(int sizeof_struct, action_fp f)
530 {
531 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000532 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
533 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
534 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000535 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000536 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
537 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000538 return ap;
539 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000540
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000541#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000542
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000543 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000544
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000545/* Actions have side effects and return a true or false value
546 * We implement: -print, -print0, -exec
547 *
548 * The rest are tests.
549 *
550 * Tests and actions are grouped by operators
551 * ( expr ) Force precedence
552 * ! expr True if expr is false
553 * -not expr Same as ! expr
554 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
555 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
556 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
557 * We implement: (), -a, -o
558 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000559 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000560 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000561 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000562 const char *arg1 = argv[1];
563
564 if (parm >= PARM_name) {
565 /* All options starting from -name require argument */
566 if (!arg1)
567 bb_error_msg_and_die(bb_msg_requires_arg, arg);
568 argv++;
569 }
570
571 /* We can use big switch() here, but on i386
572 * it doesn't give smaller code. Other arches? */
573
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000574 /* --- Operators --- */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000575 if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000576 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000577 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000578 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000579 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000580 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000581 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000582 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000583 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000584 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000585 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000586#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000587 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000588 /* also handles "find ! ! -name 'foo*'" */
589 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000590 }
591#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000592
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000593 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000594 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000595 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000596 /* GNU find ignores '!' here: "find ! -print" */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000597 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000598 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000599 }
600#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000601 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000602 need_print = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000603 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000604 (void) ALLOC_ACTION(print0);
605 }
Paul Foxd7384292006-05-12 14:47:20 +0000606#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000607#if ENABLE_FEATURE_FIND_DEPTH
608 else if (parm == PARM_depth) {
609 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000610 }
611#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000612#if ENABLE_FEATURE_FIND_PRUNE
613 else if (parm == PARM_prune) {
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000614 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000615 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000616 }
617#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000618#if ENABLE_FEATURE_FIND_DELETE
619 else if (parm == PARM_delete) {
620 need_print = 0;
621 recurse_flags |= ACTION_DEPTHFIRST;
622 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000623 }
Rob Landleycee605c2005-10-06 16:39:17 +0000624#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000625#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000626 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000627 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000628 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000629 need_print = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000630 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000631 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000632 ap->exec_argv = ++argv; /* first arg after -exec */
633 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000634 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000635 if (!*argv) /* did not see ';' until end */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000636 bb_error_msg_and_die("-exec CMD must end by ';'");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000637 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000638 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000639 argv++;
640 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000641 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000642 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000643 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000644 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000645 i = ap->exec_argc;
646 while (i--)
647 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
648 }
649#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000650#if ENABLE_FEATURE_FIND_PAREN
651 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000652 action_paren *ap;
653 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000654 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000655
656 endarg = argv;
657 while (1) {
658 if (!*++endarg)
659 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000660 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000661 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000662 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000663 *endarg = NULL;
664 break;
665 }
666 }
667 ap = ALLOC_ACTION(paren);
668 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000669 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000670 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000671 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000672#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000673 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000674 action_name *ap;
675 ap = ALLOC_ACTION(name);
676 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000677 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000678 }
679#if ENABLE_FEATURE_FIND_PATH
680 else if (parm == PARM_path) {
681 action_path *ap;
682 ap = ALLOC_ACTION(path);
683 ap->pattern = arg1;
684 }
685#endif
686#if ENABLE_FEATURE_FIND_REGEX
687 else if (parm == PARM_regex) {
688 action_regex *ap;
689 ap = ALLOC_ACTION(regex);
690 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
691 }
692#endif
693#if ENABLE_FEATURE_FIND_TYPE
694 else if (parm == PARM_type) {
695 action_type *ap;
696 ap = ALLOC_ACTION(type);
697 ap->type_mask = find_type(arg1);
698 }
699#endif
700#if ENABLE_FEATURE_FIND_PERM
701/* -perm mode File's permission bits are exactly mode (octal or symbolic).
702 * Symbolic modes use mode 0 as a point of departure.
703 * -perm -mode All of the permission bits mode are set for the file.
704 * -perm +mode Any of the permission bits mode are set for the file.
705 */
706 else if (parm == PARM_perm) {
707 action_perm *ap;
708 ap = ALLOC_ACTION(perm);
709 ap->perm_char = arg1[0];
710 arg1 = plus_minus_num(arg1);
711 ap->perm_mask = 0;
712 if (!bb_parse_mode(arg1, &ap->perm_mask))
713 bb_error_msg_and_die("invalid mode: %s", arg1);
714 }
715#endif
716#if ENABLE_FEATURE_FIND_MTIME
717 else if (parm == PARM_mtime) {
718 action_mtime *ap;
719 ap = ALLOC_ACTION(mtime);
720 ap->mtime_char = arg1[0];
721 ap->mtime_days = xatoul(plus_minus_num(arg1));
722 }
723#endif
724#if ENABLE_FEATURE_FIND_MMIN
725 else if (parm == PARM_mmin) {
726 action_mmin *ap;
727 ap = ALLOC_ACTION(mmin);
728 ap->mmin_char = arg1[0];
729 ap->mmin_mins = xatoul(plus_minus_num(arg1));
730 }
731#endif
732#if ENABLE_FEATURE_FIND_NEWER
733 else if (parm == PARM_newer) {
734 struct stat stat_newer;
735 action_newer *ap;
736 ap = ALLOC_ACTION(newer);
737 xstat(arg1, &stat_newer);
738 ap->newer_mtime = stat_newer.st_mtime;
739 }
740#endif
741#if ENABLE_FEATURE_FIND_INUM
742 else if (parm == PARM_inum) {
743 action_inum *ap;
744 ap = ALLOC_ACTION(inum);
745 ap->inode_num = xatoul(arg1);
746 }
747#endif
748#if ENABLE_FEATURE_FIND_USER
749 else if (parm == PARM_user) {
750 action_user *ap;
751 ap = ALLOC_ACTION(user);
752 ap->uid = bb_strtou(arg1, NULL, 10);
753 if (errno)
754 ap->uid = xuname2uid(arg1);
755 }
756#endif
757#if ENABLE_FEATURE_FIND_GROUP
758 else if (parm == PARM_group) {
759 action_group *ap;
760 ap = ALLOC_ACTION(group);
761 ap->gid = bb_strtou(arg1, NULL, 10);
762 if (errno)
763 ap->gid = xgroup2gid(arg1);
764 }
765#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000766#if ENABLE_FEATURE_FIND_SIZE
767 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000768/* -size n[bckw]: file uses n units of space
769 * b (default): units are 512-byte blocks
770 * c: 1 byte
771 * k: kilobytes
772 * w: 2-byte words
773 */
774#if ENABLE_LFS
775#define XATOU_SFX xatoull_sfx
776#else
777#define XATOU_SFX xatoul_sfx
778#endif
779 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000780 { "c", 1 },
781 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000782 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000783 { "b", 512 },
784 { "k", 1024 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000785 { }
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000786 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000787 action_size *ap;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000788 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000789 ap->size_char = arg1[0];
790 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000791 }
792#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000793#if ENABLE_FEATURE_FIND_CONTEXT
794 else if (parm == PARM_context) {
795 action_context *ap;
796 ap = ALLOC_ACTION(context);
797 ap->context = NULL;
Denis Vlasenko838daca2007-08-16 10:41:24 +0000798 /* SELinux headers erroneously declare non-const parameter */
799 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000800 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000801 }
802#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000803 else {
804 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000805 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000806 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000807 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000808 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000809 return appp;
810#undef ALLOC_ACTION
811}
812
813
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000814int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000815int find_main(int argc, char **argv)
816{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000817 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000818 "-follow\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000819IF_FEATURE_FIND_XDEV( "-xdev\0" )
820IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000821 ;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000822 enum {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000823 OPT_FOLLOW,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000824IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
825IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,)
Denis Vlasenkob9411292007-06-15 23:43:11 +0000826 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000827
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000828 char *arg;
829 char **argp;
830 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000831#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000832 int minmaxdepth[2] = { 0, INT_MAX };
833#else
834#define minmaxdepth NULL
Denis Vlasenkob9411292007-06-15 23:43:11 +0000835#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000836
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000837 for (firstopt = 1; firstopt < argc; firstopt++) {
838 if (argv[firstopt][0] == '-')
839 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000840 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
841 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000842#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000843 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000844 break;
845#endif
846 }
847 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000848 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000849 argv--;
850 firstopt++;
851 }
852
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000853/* All options always return true. They always take effect
854 * rather than being processed only when their place in the
855 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000856 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000857 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000858 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000859 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000860 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000861 while ((arg = argp[0])) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000862 int opt = index_in_strings(options, arg);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000863 if (opt == OPT_FOLLOW) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000864 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000865 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000866 }
867#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000868 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000869 struct stat stbuf;
870 if (!xdev_count) {
871 xdev_count = firstopt - 1;
872 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
873 for (i = 1; i < firstopt; i++) {
874 /* not xstat(): shouldn't bomb out on
875 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000876 if (stat(argv[i], &stbuf))
877 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000878 xdev_dev[i-1] = stbuf.st_dev;
879 }
880 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000881 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000882 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000883#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000884#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000885 if (opt == OPT_MINDEPTH || opt == OPT_MINDEPTH + 1) {
Denis Vlasenkob9411292007-06-15 23:43:11 +0000886 if (!argp[1])
887 bb_show_usage();
Denis Vlasenko83518d12009-03-20 22:17:13 +0000888 minmaxdepth[opt - OPT_MINDEPTH] = xatoi_u(argp[1]);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000889 argp[0] = (char*)"-a";
890 argp[1] = (char*)"-a";
891 argp++;
892 }
893#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000894 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000895 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000896
897 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000898
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000899 for (i = 1; i < firstopt; i++) {
900 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000901 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000902 fileAction, /* file action */
903 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000904#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko83518d12009-03-20 22:17:13 +0000905 minmaxdepth, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000906#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000907 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000908#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000909 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000910 status = EXIT_FAILURE;
911 }
Matt Kraai096370d2001-02-07 03:52:38 +0000912 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000913}