blob: 51d26c00a8f0f467b00ff9fbd292fb3c848eb117 [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;
Denis Vlasenko68404f12008-03-17 09:00:54 +000077#define ACTF(name) static int func_##name(const char *fileName ATTRIBUTE_UNUSED, \
78 struct stat *statbuf ATTRIBUTE_UNUSED, \
79 action_##name* ap ATTRIBUTE_UNUSED)
Denis Vlasenkoa19faf82007-06-19 22:22:57 +000080 ACTS(print)
Denis Vlasenko75aa6152007-12-09 10:07:39 +000081 ACTS(name, const char *pattern; bool iname;)
Denis Vlasenkoa19faf82007-06-19 22:22:57 +000082USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;))
83USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
84USE_FEATURE_FIND_PRINT0( ACTS(print0))
85USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
86USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
87USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
88USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
89USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
90USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
91USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
92USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
93USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
94USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
95USE_FEATURE_FIND_PRUNE( ACTS(prune))
96USE_FEATURE_FIND_DELETE( ACTS(delete))
97USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
98USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matt Kraai096370d2001-02-07 03:52:38 +000099
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000100static action ***actions;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000101static bool need_print = 1;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000102static int recurse_flags = ACTION_RECURSE;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000103
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000104#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000105static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000106{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000107 unsigned count = 0;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000108 while ((str = strstr(str, "{}")) != NULL) {
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000109 count++;
110 str++;
111 }
112 return count;
113}
114
115
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000116static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000117{
118 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000119 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000120 /* we replace each '{}' with filename: growth by strlen-2 */
121 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
122 while ((end = strstr(src, "{}"))) {
123 memcpy(dst, src, end - src);
124 dst += end - src;
125 src = end + 2;
126 memcpy(dst, filename, flen);
127 dst += flen;
128 }
129 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000130 return buf;
131}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000132#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000133
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000134/* Return values of ACTFs ('action functions') are a bit mask:
135 * bit 1=1: prune (use SKIP constant for setting it)
136 * bit 0=1: matched successfully (TRUE)
137 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000138
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000139static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
140{
141 int cur_group;
142 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000143 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000144 action **app, *ap;
145
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000146 /* "action group" is a set of actions ANDed together.
147 * groups are ORed together.
148 * We simply evaluate each group until we find one in which all actions
149 * succeed. */
150
151 /* -prune is special: if it is encountered, then we won't
152 * descend into current directory. It doesn't matter whether
153 * action group (in which -prune sits) will succeed or not:
154 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
155 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
156 * not starting with 'f' */
157
158 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
159 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
160 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
161 * On return, bit is restored. */
162
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000163 cur_group = -1;
164 while ((app = appp[++cur_group])) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000165 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000166 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000167 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000168 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000169 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000170 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000171 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000172#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000173 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000174#endif
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000175 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000176 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000177 }
178 }
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000179 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000180}
181
182
Denis Vlasenko92258542006-11-01 10:25:35 +0000183ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000184{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000185 const char *tmp = bb_basename(fileName);
186 if (tmp != fileName && !*tmp) { /* "foo/bar/". Oh no... go back to 'b' */
187 tmp--;
188 while (tmp != fileName && *--tmp != '/')
189 continue;
190 if (*tmp == '/')
191 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000192 }
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000193 return fnmatch(ap->pattern, tmp, FNM_PERIOD | (ap->iname ? FNM_CASEFOLD : 0)) == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000194}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000195
Denis Vlasenkob9411292007-06-15 23:43:11 +0000196#if ENABLE_FEATURE_FIND_PATH
197ACTF(path)
198{
199 return fnmatch(ap->pattern, fileName, 0) == 0;
200}
201#endif
202#if ENABLE_FEATURE_FIND_REGEX
203ACTF(regex)
204{
205 regmatch_t match;
206 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
207 return 0; /* no match */
208 if (match.rm_so)
209 return 0; /* match doesn't start at pos 0 */
210 if (fileName[match.rm_eo])
211 return 0; /* match doesn't end exactly at end of pathname */
212 return 1;
213}
214#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000215#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000216ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000217{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000218 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000219}
Rob Landleycee605c2005-10-06 16:39:17 +0000220#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000221#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000222ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000223{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000224 /* -perm +mode: at least one of perm_mask bits are set */
225 if (ap->perm_char == '+')
226 return (statbuf->st_mode & ap->perm_mask) != 0;
227 /* -perm -mode: all of perm_mask are set */
228 if (ap->perm_char == '-')
229 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
230 /* -perm mode: file mode must match perm_mask */
231 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000232}
Rob Landleycee605c2005-10-06 16:39:17 +0000233#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000234#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000235ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000236{
237 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000238 time_t mtime_secs = ap->mtime_days * 24*60*60;
239 if (ap->mtime_char == '+')
240 return file_age >= mtime_secs + 24*60*60;
241 if (ap->mtime_char == '-')
242 return file_age < mtime_secs;
243 /* just numeric mtime */
244 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000245}
Rob Landleycee605c2005-10-06 16:39:17 +0000246#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000247#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000248ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000249{
250 time_t file_age = time(NULL) - statbuf->st_mtime;
251 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000252 if (ap->mmin_char == '+')
253 return file_age >= mmin_secs + 60;
254 if (ap->mmin_char == '-')
255 return file_age < mmin_secs;
256 /* just numeric mmin */
257 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000258}
Paul Fox72d1a232006-01-13 21:05:41 +0000259#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000260#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000261ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000262{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000263 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000264}
Rob Landleycee605c2005-10-06 16:39:17 +0000265#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000266#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000267ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000268{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000269 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000270}
Rob Landleycee605c2005-10-06 16:39:17 +0000271#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000272#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000273ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000274{
275 int i, rc;
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000276 char *argv[ap->exec_argc + 1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000277 for (i = 0; i < ap->exec_argc; i++)
278 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
279 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000280
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000281 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000282 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000283 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000284
285 i = 0;
286 while (argv[i])
287 free(argv[i++]);
288 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000289}
Rob Landleycee605c2005-10-06 16:39:17 +0000290#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000291#if ENABLE_FEATURE_FIND_USER
292ACTF(user)
293{
294 return (statbuf->st_uid == ap->uid);
295}
296#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000297#if ENABLE_FEATURE_FIND_GROUP
298ACTF(group)
299{
300 return (statbuf->st_gid == ap->gid);
301}
302#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000303#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000304ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000305{
306 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000307 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000308}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000309#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000310ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000311{
312 puts(fileName);
313 return TRUE;
314}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000315#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000316ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000317{
318 return exec_actions(ap->subexpr, fileName, statbuf);
319}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000320#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000321#if ENABLE_FEATURE_FIND_SIZE
322ACTF(size)
323{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000324 if (ap->size_char == '+')
325 return statbuf->st_size > ap->size;
326 if (ap->size_char == '-')
327 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000328 return statbuf->st_size == ap->size;
329}
330#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000331#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000332/*
333 * -prune: if -depth is not given, return true and do not descend
334 * current dir; if -depth is given, return false with no effect.
335 * Example:
336 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
337 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000338ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000339{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000340 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000341}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000342#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000343#if ENABLE_FEATURE_FIND_DELETE
344ACTF(delete)
345{
346 int rc;
347 if (S_ISDIR(statbuf->st_mode)) {
348 rc = rmdir(fileName);
349 } else {
350 rc = unlink(fileName);
351 }
352 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000353 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000354 return TRUE;
355}
356#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000357#if ENABLE_FEATURE_FIND_CONTEXT
358ACTF(context)
359{
360 security_context_t con;
361 int rc;
362
363 if (recurse_flags & ACTION_FOLLOWLINKS) {
364 rc = getfilecon(fileName, &con);
365 } else {
366 rc = lgetfilecon(fileName, &con);
367 }
368 if (rc < 0)
369 return FALSE;
370 rc = strcmp(ap->context, con);
371 freecon(con);
372 return rc == 0;
373}
374#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000375
Denis Vlasenko62f04792007-04-13 10:00:12 +0000376
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000377static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000378 struct stat *statbuf,
379 void *userData SKIP_FEATURE_FIND_MAXDEPTH(ATTRIBUTE_UNUSED),
380 int depth SKIP_FEATURE_FIND_MAXDEPTH(ATTRIBUTE_UNUSED))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000381{
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000382 int i;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000383#if ENABLE_FEATURE_FIND_MAXDEPTH
384 int maxdepth = (int)(ptrdiff_t)userData;
385
386 if (depth > maxdepth) return SKIP;
387#endif
388
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000389#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000390 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000391 for (i = 0; i < xdev_count; i++) {
Paul Foxdcf9bba2007-09-06 17:45:16 +0000392 if (xdev_dev[i] == statbuf->st_dev)
393 break;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000394 }
Paul Foxdcf9bba2007-09-06 17:45:16 +0000395 if (i == xdev_count)
396 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000397 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000398#endif
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000399 i = exec_actions(actions, fileName, statbuf);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000400 /* Had no explicit -print[0] or -exec? then print */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000401 if ((i & TRUE) && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000402 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000403 /* Cannot return 0: our caller, recursive_action(),
404 * will perror() and skip dirs (if called on dir) */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000405 return (i & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000406}
407
Eric Andersen17d49ef1999-10-06 20:25:32 +0000408
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000409#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000410static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000411{
412 int mask = 0;
413
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000414 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000415 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000416 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000417 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000418 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000419 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000420 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000421 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000422 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000423 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000424 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000425 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000426 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000427 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000428
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000429 if (mask == 0 || *(type + 1) != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000430 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000431
432 return mask;
433}
434#endif
435
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000436#if ENABLE_FEATURE_FIND_PERM \
437 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
438 || ENABLE_FEATURE_FIND_SIZE
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000439static const char* plus_minus_num(const char* str)
440{
441 if (*str == '-' || *str == '+')
442 str++;
443 return str;
444}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000445#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000446
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000447static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000448{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000449 enum {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000450 PARM_a ,
451 PARM_o ,
452 USE_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000453#if ENABLE_DESKTOP
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000454 PARM_and ,
455 PARM_or ,
456 USE_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000457#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000458 PARM_print ,
459 USE_FEATURE_FIND_PRINT0( PARM_print0 ,)
460 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
461 USE_FEATURE_FIND_PRUNE( PARM_prune ,)
462 USE_FEATURE_FIND_DELETE( PARM_delete ,)
463 USE_FEATURE_FIND_EXEC( PARM_exec ,)
464 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
465 /* All options starting from here require argument */
466 PARM_name ,
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000467 PARM_iname ,
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000468 USE_FEATURE_FIND_PATH( PARM_path ,)
469 USE_FEATURE_FIND_REGEX( PARM_regex ,)
470 USE_FEATURE_FIND_TYPE( PARM_type ,)
471 USE_FEATURE_FIND_PERM( PARM_perm ,)
472 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
473 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
474 USE_FEATURE_FIND_NEWER( PARM_newer ,)
475 USE_FEATURE_FIND_INUM( PARM_inum ,)
476 USE_FEATURE_FIND_USER( PARM_user ,)
477 USE_FEATURE_FIND_GROUP( PARM_group ,)
478 USE_FEATURE_FIND_SIZE( PARM_size ,)
479 USE_FEATURE_FIND_CONTEXT(PARM_context ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000480 };
481
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000482 static const char params[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000483 "-a\0"
484 "-o\0"
485 USE_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000486#if ENABLE_DESKTOP
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000487 "-and\0"
488 "-or\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000489 USE_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000490#endif
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000491 "-print\0"
492 USE_FEATURE_FIND_PRINT0( "-print0\0" )
493 USE_FEATURE_FIND_DEPTH( "-depth\0" )
494 USE_FEATURE_FIND_PRUNE( "-prune\0" )
495 USE_FEATURE_FIND_DELETE( "-delete\0" )
496 USE_FEATURE_FIND_EXEC( "-exec\0" )
497 USE_FEATURE_FIND_PAREN( "(\0" )
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000498 /* All options starting from here require argument */
Denis Vlasenko319f8eb2007-08-13 11:09:30 +0000499 "-name\0"
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000500 "-iname\0"
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000501 USE_FEATURE_FIND_PATH( "-path\0" )
502 USE_FEATURE_FIND_REGEX( "-regex\0" )
503 USE_FEATURE_FIND_TYPE( "-type\0" )
504 USE_FEATURE_FIND_PERM( "-perm\0" )
505 USE_FEATURE_FIND_MTIME( "-mtime\0" )
506 USE_FEATURE_FIND_MMIN( "-mmin\0" )
507 USE_FEATURE_FIND_NEWER( "-newer\0" )
508 USE_FEATURE_FIND_INUM( "-inum\0" )
509 USE_FEATURE_FIND_USER( "-user\0" )
510 USE_FEATURE_FIND_GROUP( "-group\0" )
511 USE_FEATURE_FIND_SIZE( "-size\0" )
512 USE_FEATURE_FIND_CONTEXT("-context\0")
513 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000514
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000515 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000516 unsigned cur_group = 0;
517 unsigned cur_action = 0;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000518 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000519
Denis Vlasenko0f3a5802008-03-20 13:13:09 +0000520 /* This is the only place in busybox where we use nested function.
521 * So far more standard alternatives were bigger. */
522 /* Suppress a warning "func without a prototype" */
523 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000524 action* alloc_action(int sizeof_struct, action_fp f)
525 {
526 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000527 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
528 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
529 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000530 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000531 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000532 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000533 return ap;
534 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000535
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000536#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000537
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000538 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000539
Bernhard Reutner-Fischer557b4582007-03-29 13:55:17 +0000540/* Actions have side effects and return a true or false value
541 * We implement: -print, -print0, -exec
542 *
543 * The rest are tests.
544 *
545 * Tests and actions are grouped by operators
546 * ( expr ) Force precedence
547 * ! expr True if expr is false
548 * -not expr Same as ! expr
549 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
550 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
551 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
552 * We implement: (), -a, -o
553 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000554 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000555 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000556 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000557 const char *arg1 = argv[1];
558
559 if (parm >= PARM_name) {
560 /* All options starting from -name require argument */
561 if (!arg1)
562 bb_error_msg_and_die(bb_msg_requires_arg, arg);
563 argv++;
564 }
565
566 /* We can use big switch() here, but on i386
567 * it doesn't give smaller code. Other arches? */
568
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000569 /* --- Operators --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000570 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000571 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000572 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000573 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000574 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000575 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000576 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000577 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000578 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000579 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000580 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000581#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko56fce002007-04-04 23:41:35 +0000582 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000583 /* also handles "find ! ! -name 'foo*'" */
584 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000585 }
586#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000587
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000588 /* --- Tests and actions --- */
Denis Vlasenko56fce002007-04-04 23:41:35 +0000589 else if (parm == PARM_print) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000590 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000591 /* GNU find ignores '!' here: "find ! -print" */
592 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000593 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000594 }
595#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +0000596 else if (parm == PARM_print0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000597 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000598 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000599 (void) ALLOC_ACTION(print0);
600 }
Paul Foxd7384292006-05-12 14:47:20 +0000601#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000602#if ENABLE_FEATURE_FIND_DEPTH
603 else if (parm == PARM_depth) {
604 recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000605 }
606#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000607#if ENABLE_FEATURE_FIND_PRUNE
608 else if (parm == PARM_prune) {
609 USE_FEATURE_FIND_NOT( invert_flag = 0; )
610 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000611 }
612#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000613#if ENABLE_FEATURE_FIND_DELETE
614 else if (parm == PARM_delete) {
615 need_print = 0;
616 recurse_flags |= ACTION_DEPTHFIRST;
617 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000618 }
Rob Landleycee605c2005-10-06 16:39:17 +0000619#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000620#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +0000621 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000622 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000623 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000624 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000625 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000626 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000627 ap->exec_argv = ++argv; /* first arg after -exec */
628 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000629 while (1) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000630 if (!*argv) /* did not see ';' until end */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000631 bb_error_msg_and_die("-exec CMD must end by ';'");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000632 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000633 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000634 argv++;
635 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000636 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000637 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000638 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000639 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000640 i = ap->exec_argc;
641 while (i--)
642 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
643 }
644#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000645#if ENABLE_FEATURE_FIND_PAREN
646 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000647 action_paren *ap;
648 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000649 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000650
651 endarg = argv;
652 while (1) {
653 if (!*++endarg)
654 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000655 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000656 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000657 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000658 *endarg = NULL;
659 break;
660 }
661 }
662 ap = ALLOC_ACTION(paren);
663 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000664 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000665 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000666 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000667#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000668 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000669 action_name *ap;
670 ap = ALLOC_ACTION(name);
671 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000672 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000673 }
674#if ENABLE_FEATURE_FIND_PATH
675 else if (parm == PARM_path) {
676 action_path *ap;
677 ap = ALLOC_ACTION(path);
678 ap->pattern = arg1;
679 }
680#endif
681#if ENABLE_FEATURE_FIND_REGEX
682 else if (parm == PARM_regex) {
683 action_regex *ap;
684 ap = ALLOC_ACTION(regex);
685 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
686 }
687#endif
688#if ENABLE_FEATURE_FIND_TYPE
689 else if (parm == PARM_type) {
690 action_type *ap;
691 ap = ALLOC_ACTION(type);
692 ap->type_mask = find_type(arg1);
693 }
694#endif
695#if ENABLE_FEATURE_FIND_PERM
696/* -perm mode File's permission bits are exactly mode (octal or symbolic).
697 * Symbolic modes use mode 0 as a point of departure.
698 * -perm -mode All of the permission bits mode are set for the file.
699 * -perm +mode Any of the permission bits mode are set for the file.
700 */
701 else if (parm == PARM_perm) {
702 action_perm *ap;
703 ap = ALLOC_ACTION(perm);
704 ap->perm_char = arg1[0];
705 arg1 = plus_minus_num(arg1);
706 ap->perm_mask = 0;
707 if (!bb_parse_mode(arg1, &ap->perm_mask))
708 bb_error_msg_and_die("invalid mode: %s", arg1);
709 }
710#endif
711#if ENABLE_FEATURE_FIND_MTIME
712 else if (parm == PARM_mtime) {
713 action_mtime *ap;
714 ap = ALLOC_ACTION(mtime);
715 ap->mtime_char = arg1[0];
716 ap->mtime_days = xatoul(plus_minus_num(arg1));
717 }
718#endif
719#if ENABLE_FEATURE_FIND_MMIN
720 else if (parm == PARM_mmin) {
721 action_mmin *ap;
722 ap = ALLOC_ACTION(mmin);
723 ap->mmin_char = arg1[0];
724 ap->mmin_mins = xatoul(plus_minus_num(arg1));
725 }
726#endif
727#if ENABLE_FEATURE_FIND_NEWER
728 else if (parm == PARM_newer) {
729 struct stat stat_newer;
730 action_newer *ap;
731 ap = ALLOC_ACTION(newer);
732 xstat(arg1, &stat_newer);
733 ap->newer_mtime = stat_newer.st_mtime;
734 }
735#endif
736#if ENABLE_FEATURE_FIND_INUM
737 else if (parm == PARM_inum) {
738 action_inum *ap;
739 ap = ALLOC_ACTION(inum);
740 ap->inode_num = xatoul(arg1);
741 }
742#endif
743#if ENABLE_FEATURE_FIND_USER
744 else if (parm == PARM_user) {
745 action_user *ap;
746 ap = ALLOC_ACTION(user);
747 ap->uid = bb_strtou(arg1, NULL, 10);
748 if (errno)
749 ap->uid = xuname2uid(arg1);
750 }
751#endif
752#if ENABLE_FEATURE_FIND_GROUP
753 else if (parm == PARM_group) {
754 action_group *ap;
755 ap = ALLOC_ACTION(group);
756 ap->gid = bb_strtou(arg1, NULL, 10);
757 if (errno)
758 ap->gid = xgroup2gid(arg1);
759 }
760#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000761#if ENABLE_FEATURE_FIND_SIZE
762 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000763/* -size n[bckw]: file uses n units of space
764 * b (default): units are 512-byte blocks
765 * c: 1 byte
766 * k: kilobytes
767 * w: 2-byte words
768 */
769#if ENABLE_LFS
770#define XATOU_SFX xatoull_sfx
771#else
772#define XATOU_SFX xatoul_sfx
773#endif
774 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000775 { "c", 1 },
776 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000777 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +0000778 { "b", 512 },
779 { "k", 1024 },
Denis Vlasenkof8689632007-07-27 15:06:25 +0000780 { }
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000781 };
Denis Vlasenkob9411292007-06-15 23:43:11 +0000782 action_size *ap;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000783 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000784 ap->size_char = arg1[0];
785 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000786 }
787#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000788#if ENABLE_FEATURE_FIND_CONTEXT
789 else if (parm == PARM_context) {
790 action_context *ap;
791 ap = ALLOC_ACTION(context);
792 ap->context = NULL;
Denis Vlasenko838daca2007-08-16 10:41:24 +0000793 /* SELinux headers erroneously declare non-const parameter */
794 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000795 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000796 }
797#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000798 else {
799 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000800 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +0000801 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000802 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000803 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000804 return appp;
805#undef ALLOC_ACTION
806}
807
808
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000809int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000810int find_main(int argc, char **argv)
811{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000812 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000813 "-follow\0"
814USE_FEATURE_FIND_XDEV( "-xdev\0" )
815USE_FEATURE_FIND_MAXDEPTH("-maxdepth\0")
816 ;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000817 enum {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000818 OPT_FOLLOW,
Denis Vlasenkob9411292007-06-15 23:43:11 +0000819USE_FEATURE_FIND_XDEV( OPT_XDEV ,)
820USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,)
821 };
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000822
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000823 char *arg;
824 char **argp;
825 int i, firstopt, status = EXIT_SUCCESS;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000826#if ENABLE_FEATURE_FIND_MAXDEPTH
827 int maxdepth = INT_MAX;
828#endif
Denis Vlasenko7d6c9292007-04-05 00:35:43 +0000829
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000830 for (firstopt = 1; firstopt < argc; firstopt++) {
831 if (argv[firstopt][0] == '-')
832 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000833 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
834 break;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000835#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000836 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000837 break;
838#endif
839 }
840 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000841 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000842 argv--;
843 firstopt++;
844 }
845
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000846/* All options always return true. They always take effect
847 * rather than being processed only when their place in the
848 * expression is reached.
Denis Vlasenkob9411292007-06-15 23:43:11 +0000849 * We implement: -follow, -xdev, -maxdepth
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000850 */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000851 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000852 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000853 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000854 while ((arg = argp[0])) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000855 int opt = index_in_strings(options, arg);
Denis Vlasenkob9411292007-06-15 23:43:11 +0000856 if (opt == OPT_FOLLOW) {
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000857 recurse_flags |= ACTION_FOLLOWLINKS;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000858 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000859 }
860#if ENABLE_FEATURE_FIND_XDEV
Denis Vlasenkob9411292007-06-15 23:43:11 +0000861 if (opt == OPT_XDEV) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000862 struct stat stbuf;
863 if (!xdev_count) {
864 xdev_count = firstopt - 1;
865 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
866 for (i = 1; i < firstopt; i++) {
867 /* not xstat(): shouldn't bomb out on
868 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000869 if (stat(argv[i], &stbuf))
870 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000871 xdev_dev[i-1] = stbuf.st_dev;
872 }
873 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000874 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000875 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000876#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000877#if ENABLE_FEATURE_FIND_MAXDEPTH
878 if (opt == OPT_MAXDEPTH) {
879 if (!argp[1])
880 bb_show_usage();
881 maxdepth = xatoi_u(argp[1]);
882 argp[0] = (char*)"-a";
883 argp[1] = (char*)"-a";
884 argp++;
885 }
886#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000887 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000888 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000889
890 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000891
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000892 for (i = 1; i < firstopt; i++) {
893 if (!recursive_action(argv[i],
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000894 recurse_flags, /* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000895 fileAction, /* file action */
896 fileAction, /* dir action */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000897#if ENABLE_FEATURE_FIND_MAXDEPTH
Denis Vlasenko6ee023c2007-08-23 10:52:52 +0000898 /* double cast suppresses
899 * "cast to ptr from int of different size" */
900 (void*)(ptrdiff_t)maxdepth,/* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000901#else
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000902 NULL, /* user data */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000903#endif
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +0000904 0)) /* depth */
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000905 status = EXIT_FAILURE;
906 }
Matt Kraai096370d2001-02-07 03:52:38 +0000907 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000908}