blob: d4b7c8ec888ba84b12b4c92abfbcf56d0e2883a4 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +020010 * Licensed under GPLv2, see file LICENSE in this source tree.
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
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020056//config:config FIND
57//config: bool "find"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020058//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020059//config: help
60//config: find is used to search your system to find specified files.
61//config:
62//config:config FEATURE_FIND_PRINT0
63//config: bool "Enable -print0: NUL-terminated output"
64//config: default y
65//config: depends on FIND
66//config: help
67//config: Causes output names to be separated by a NUL character
68//config: rather than a newline. This allows names that contain
69//config: newlines and other whitespace to be more easily
70//config: interpreted by other programs.
71//config:
72//config:config FEATURE_FIND_MTIME
73//config: bool "Enable -mtime: modified time matching"
74//config: default y
75//config: depends on FIND
76//config: help
77//config: Allow searching based on the modification time of
78//config: files, in days.
79//config:
80//config:config FEATURE_FIND_MMIN
81//config: bool "Enable -mmin: modified time matching by minutes"
82//config: default y
83//config: depends on FIND
84//config: help
85//config: Allow searching based on the modification time of
86//config: files, in minutes.
87//config:
88//config:config FEATURE_FIND_PERM
89//config: bool "Enable -perm: permissions matching"
90//config: default y
91//config: depends on FIND
92//config: help
93//config: Enable searching based on file permissions.
94//config:
95//config:config FEATURE_FIND_TYPE
96//config: bool "Enable -type: file type matching (file/dir/link/...)"
97//config: default y
98//config: depends on FIND
99//config: help
100//config: Enable searching based on file type (file,
101//config: directory, socket, device, etc.).
102//config:
103//config:config FEATURE_FIND_XDEV
104//config: bool "Enable -xdev: 'stay in filesystem'"
105//config: default y
106//config: depends on FIND
107//config: help
108//config: This option allows find to restrict searches to a single filesystem.
109//config:
110//config:config FEATURE_FIND_MAXDEPTH
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100111//config: bool "Enable -mindepth N and -maxdepth N"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200112//config: default y
113//config: depends on FIND
114//config: help
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100115//config: This option enables -mindepth N and -maxdepth N option.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200116//config:
117//config:config FEATURE_FIND_NEWER
118//config: bool "Enable -newer: compare file modification times"
119//config: default y
120//config: depends on FIND
121//config: help
122//config: Support the 'find -newer' option for finding any files which have
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100123//config: modification time that is more recent than the specified FILE.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200124//config:
125//config:config FEATURE_FIND_INUM
126//config: bool "Enable -inum: inode number matching"
127//config: default y
128//config: depends on FIND
129//config: help
130//config: Support the 'find -inum' option for searching by inode number.
131//config:
132//config:config FEATURE_FIND_EXEC
133//config: bool "Enable -exec: execute commands"
134//config: default y
135//config: depends on FIND
136//config: help
137//config: Support the 'find -exec' option for executing commands based upon
138//config: the files matched.
139//config:
140//config:config FEATURE_FIND_USER
141//config: bool "Enable -user: username/uid matching"
142//config: default y
143//config: depends on FIND
144//config: help
145//config: Support the 'find -user' option for searching by username or uid.
146//config:
147//config:config FEATURE_FIND_GROUP
148//config: bool "Enable -group: group/gid matching"
149//config: default y
150//config: depends on FIND
151//config: help
152//config: Support the 'find -group' option for searching by group name or gid.
153//config:
154//config:config FEATURE_FIND_NOT
155//config: bool "Enable the 'not' (!) operator"
156//config: default y
157//config: depends on FIND
158//config: help
159//config: Support the '!' operator to invert the test results.
160//config: If 'Enable full-blown desktop' is enabled, then will also support
161//config: the non-POSIX notation '-not'.
162//config:
163//config:config FEATURE_FIND_DEPTH
164//config: bool "Enable -depth"
165//config: default y
166//config: depends on FIND
167//config: help
168//config: Process each directory's contents before the directory itself.
169//config:
170//config:config FEATURE_FIND_PAREN
171//config: bool "Enable parens in options"
172//config: default y
173//config: depends on FIND
174//config: help
175//config: Enable usage of parens '(' to specify logical order of arguments.
176//config:
177//config:config FEATURE_FIND_SIZE
178//config: bool "Enable -size: file size matching"
179//config: default y
180//config: depends on FIND
181//config: help
182//config: Support the 'find -size' option for searching by file size.
183//config:
184//config:config FEATURE_FIND_PRUNE
185//config: bool "Enable -prune: exclude subdirectories"
186//config: default y
187//config: depends on FIND
188//config: help
189//config: If the file is a directory, dont descend into it. Useful for
190//config: exclusion .svn and CVS directories.
191//config:
192//config:config FEATURE_FIND_DELETE
193//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200194//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200195//config: depends on FIND && FEATURE_FIND_DEPTH
196//config: help
197//config: Support the 'find -delete' option for deleting files and directories.
198//config: WARNING: This option can do much harm if used wrong. Busybox will not
199//config: try to protect the user from doing stupid things. Use with care.
200//config:
201//config:config FEATURE_FIND_PATH
202//config: bool "Enable -path: match pathname with shell pattern"
203//config: default y
204//config: depends on FIND
205//config: help
206//config: The -path option matches whole pathname instead of just filename.
207//config:
208//config:config FEATURE_FIND_REGEX
209//config: bool "Enable -regex: match pathname with regex"
210//config: default y
211//config: depends on FIND
212//config: help
213//config: The -regex option matches whole pathname against regular expression.
214//config:
215//config:config FEATURE_FIND_CONTEXT
216//config: bool "Enable -context: security context matching"
217//config: default n
218//config: depends on FIND && SELINUX
219//config: help
220//config: Support the 'find -context' option for matching security context.
221//config:
222//config:config FEATURE_FIND_LINKS
223//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200224//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200225//config: depends on FIND
226//config: help
227//config: Support the 'find -links' option for matching number of links.
228
Denys Vlasenko805f2452011-02-12 20:26:14 +0100229//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
230
231//kbuild:lib-$(CONFIG_FIND) += find.o
232
233//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200234//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100235//usage:#define find_full_usage "\n\n"
236//usage: "Search for files and perform actions on them.\n"
237//usage: "First failed action stops processing of current file.\n"
238//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200239//usage: "\n -L,-follow Follow symlinks"
240//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100241//usage: IF_FEATURE_FIND_XDEV(
242//usage: "\n -xdev Don't descend directories on other filesystems"
243//usage: )
244//usage: IF_FEATURE_FIND_MAXDEPTH(
245//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
246//usage: "\n actions to command line arguments only"
247//usage: "\n -mindepth N Don't act on first N levels"
248//usage: )
249//usage: IF_FEATURE_FIND_DEPTH(
250//usage: "\n -depth Act on directory *after* traversing it"
251//usage: )
252//usage: "\n"
253//usage: "\nActions:"
254//usage: IF_FEATURE_FIND_PAREN(
255//usage: "\n ( ACTIONS ) Group actions for -o / -a"
256//usage: )
257//usage: IF_FEATURE_FIND_NOT(
258//usage: "\n ! ACT Invert ACT's success/failure"
259//usage: )
260//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
261//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
262//usage: "\n Note: -a has higher priority than -o"
263//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
264//usage: "\n -iname PATTERN Case insensitive -name"
265//usage: IF_FEATURE_FIND_PATH(
266//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200267//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100268//usage: )
269//usage: IF_FEATURE_FIND_REGEX(
270//usage: "\n -regex PATTERN Match path to regex PATTERN"
271//usage: )
272//usage: IF_FEATURE_FIND_TYPE(
273//usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
274//usage: )
275//usage: IF_FEATURE_FIND_PERM(
276//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100277//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100278//usage: )
279//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100280//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
281//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100282//usage: )
283//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100284//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
285//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100286//usage: )
287//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100288//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100289//usage: )
290//usage: IF_FEATURE_FIND_INUM(
291//usage: "\n -inum N File has inode number N"
292//usage: )
293//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100294//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100295//usage: )
296//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100297//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100298//usage: )
299//usage: IF_FEATURE_FIND_SIZE(
300//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
301//usage: "\n +/-N: file size is bigger/smaller than N"
302//usage: )
303//usage: IF_FEATURE_FIND_LINKS(
304//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
305//usage: "\n or exactly N"
306//usage: )
307//usage: IF_FEATURE_FIND_CONTEXT(
308//usage: "\n -context CTX File has specified security context"
309//usage: )
310//usage: IF_FEATURE_FIND_PRUNE(
311//usage: "\n -prune If current file is directory, don't descend into it"
312//usage: )
313//usage: "\nIf none of the following actions is specified, -print is assumed"
314//usage: "\n -print Print file name"
315//usage: IF_FEATURE_FIND_PRINT0(
316//usage: "\n -print0 Print file name, NUL terminated"
317//usage: )
318//usage: IF_FEATURE_FIND_EXEC(
319//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
320//usage: "\n file name. Fails if CMD exits with nonzero"
321//usage: )
322//usage: IF_FEATURE_FIND_DELETE(
323//usage: "\n -delete Delete current file/directory. Turns on -depth option"
324//usage: )
325//usage:
326//usage:#define find_example_usage
327//usage: "$ find / -name passwd\n"
328//usage: "/etc/passwd\n"
329
Matt Kraai096370d2001-02-07 03:52:38 +0000330#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000331#include "libbb.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000332#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200333# include "xregex.h"
334#endif
335/* GNUism: */
336#ifndef FNM_CASEFOLD
337# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000338#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000339
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200340#define dbg(...) ((void)0)
341/* #define dbg(...) bb_error_msg(__VA_ARGS__) */
342
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000343/* This is a NOEXEC applet. Be very careful! */
344
345
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200346typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000347
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000348typedef struct {
349 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000350#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000351 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000352#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000353} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200354
Denys Vlasenko606291b2009-09-23 23:15:43 +0200355#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200356#define ACTF(name) \
357 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200358 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200359 action_##name* ap UNUSED_PARAM)
360
361 ACTS(print)
362 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200363IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000364IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
365IF_FEATURE_FIND_PRINT0( ACTS(print0))
366IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
367IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
368IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
369IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
370IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
371IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
372IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
373IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
374IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
375IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
376IF_FEATURE_FIND_PRUNE( ACTS(prune))
377IF_FEATURE_FIND_DELETE( ACTS(delete))
378IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;))
379IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100380IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000381
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200382struct globals {
383 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
384 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100385#if ENABLE_FEATURE_FIND_MAXDEPTH
386 int minmaxdepth[2];
387#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200388 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100389 smallint need_print;
390 smallint xdev_on;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200391 recurse_flags_t recurse_flags;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100392} FIX_ALIASING;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200393#define G (*(struct globals*)&bb_common_bufsiz1)
394#define INIT_G() do { \
395 struct G_sizecheck { \
396 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
397 }; \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200398 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100399 memset(&G, 0, sizeof(G)); \
400 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200401 G.need_print = 1; \
402 G.recurse_flags = ACTION_RECURSE; \
403} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000404
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000405#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000406static unsigned count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000407{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000408 unsigned count = 0;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000409 while ((str = strstr(str, "{}")) != NULL) {
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000410 count++;
411 str++;
412 }
413 return count;
414}
415
416
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000417static char* subst(const char *src, unsigned count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000418{
419 char *buf, *dst, *end;
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000420 size_t flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000421 /* we replace each '{}' with filename: growth by strlen-2 */
422 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
423 while ((end = strstr(src, "{}"))) {
424 memcpy(dst, src, end - src);
425 dst += end - src;
426 src = end + 2;
427 memcpy(dst, filename, flen);
428 dst += flen;
429 }
430 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000431 return buf;
432}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000433#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000434
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000435/* Return values of ACTFs ('action functions') are a bit mask:
436 * bit 1=1: prune (use SKIP constant for setting it)
437 * bit 0=1: matched successfully (TRUE)
438 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000439
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200440static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000441{
442 int cur_group;
443 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000444 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000445 action **app, *ap;
446
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000447 /* "action group" is a set of actions ANDed together.
448 * groups are ORed together.
449 * We simply evaluate each group until we find one in which all actions
450 * succeed. */
451
452 /* -prune is special: if it is encountered, then we won't
453 * descend into current directory. It doesn't matter whether
454 * action group (in which -prune sits) will succeed or not:
455 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
456 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
457 * not starting with 'f' */
458
459 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
460 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
461 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
462 * On return, bit is restored. */
463
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000464 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100465 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000466 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000467 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000468 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000469 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000470 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000471 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000472 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000473#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000474 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000475#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200476 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000477 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000478 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000479 }
480 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200481 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000482 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000483}
484
485
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200486#if !FNM_CASEFOLD
487static char *strcpy_upcase(char *dst, const char *src)
488{
489 char *d = dst;
490 while (1) {
491 unsigned char ch = *src++;
492 if (ch >= 'a' && ch <= 'z')
493 ch -= ('a' - 'A');
494 *d++ = ch;
495 if (ch == '\0')
496 break;
497 }
498 return dst;
499}
500#endif
501
Denis Vlasenko92258542006-11-01 10:25:35 +0000502ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000503{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000504 const char *tmp = bb_basename(fileName);
Denys Vlasenko91388172010-04-26 09:54:04 +0200505 if (tmp != fileName && *tmp == '\0') {
506 /* "foo/bar/". Oh no... go back to 'b' */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000507 tmp--;
508 while (tmp != fileName && *--tmp != '/')
509 continue;
510 if (*tmp == '/')
511 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000512 }
Denys Vlasenko91388172010-04-26 09:54:04 +0200513 /* Was using FNM_PERIOD flag too,
514 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
515 * find -name '*foo' should match .foo too:
516 */
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200517#if FNM_CASEFOLD
Denys Vlasenko91388172010-04-26 09:54:04 +0200518 return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200519#else
520 if (ap->iname)
521 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
522 return fnmatch(ap->pattern, tmp, 0) == 0;
523#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000524}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000525
Denis Vlasenkob9411292007-06-15 23:43:11 +0000526#if ENABLE_FEATURE_FIND_PATH
527ACTF(path)
528{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200529# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200530 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200531# else
532 if (ap->ipath)
533 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
534 return fnmatch(ap->pattern, fileName, 0) == 0;
535# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000536}
537#endif
538#if ENABLE_FEATURE_FIND_REGEX
539ACTF(regex)
540{
541 regmatch_t match;
542 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
543 return 0; /* no match */
544 if (match.rm_so)
545 return 0; /* match doesn't start at pos 0 */
546 if (fileName[match.rm_eo])
547 return 0; /* match doesn't end exactly at end of pathname */
548 return 1;
549}
550#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000551#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000552ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000553{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000554 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000555}
Rob Landleycee605c2005-10-06 16:39:17 +0000556#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000557#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000558ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000559{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000560 /* -perm +mode: at least one of perm_mask bits are set */
561 if (ap->perm_char == '+')
562 return (statbuf->st_mode & ap->perm_mask) != 0;
563 /* -perm -mode: all of perm_mask are set */
564 if (ap->perm_char == '-')
565 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
566 /* -perm mode: file mode must match perm_mask */
567 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000568}
Rob Landleycee605c2005-10-06 16:39:17 +0000569#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000570#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000571ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000572{
573 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000574 time_t mtime_secs = ap->mtime_days * 24*60*60;
575 if (ap->mtime_char == '+')
576 return file_age >= mtime_secs + 24*60*60;
577 if (ap->mtime_char == '-')
578 return file_age < mtime_secs;
579 /* just numeric mtime */
580 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000581}
Rob Landleycee605c2005-10-06 16:39:17 +0000582#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000583#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000584ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000585{
586 time_t file_age = time(NULL) - statbuf->st_mtime;
587 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000588 if (ap->mmin_char == '+')
589 return file_age >= mmin_secs + 60;
590 if (ap->mmin_char == '-')
591 return file_age < mmin_secs;
592 /* just numeric mmin */
593 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000594}
Paul Fox72d1a232006-01-13 21:05:41 +0000595#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000596#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000597ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000598{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000599 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000600}
Rob Landleycee605c2005-10-06 16:39:17 +0000601#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000602#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000603ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000604{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000605 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000606}
Rob Landleycee605c2005-10-06 16:39:17 +0000607#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000608#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000609ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000610{
611 int i, rc;
Denys Vlasenkoaa7da732009-09-13 14:57:24 +0200612#if ENABLE_USE_PORTABLE_CODE
613 char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1));
614#else /* gcc 4.3.1 generates smaller code: */
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000615 char *argv[ap->exec_argc + 1];
Denys Vlasenkoaa7da732009-09-13 14:57:24 +0200616#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000617 for (i = 0; i < ap->exec_argc; i++)
618 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
619 argv[i] = NULL; /* terminate the list */
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000620
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000621 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000622 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000623 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000624
625 i = 0;
626 while (argv[i])
627 free(argv[i++]);
628 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000629}
Rob Landleycee605c2005-10-06 16:39:17 +0000630#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000631#if ENABLE_FEATURE_FIND_USER
632ACTF(user)
633{
634 return (statbuf->st_uid == ap->uid);
635}
636#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000637#if ENABLE_FEATURE_FIND_GROUP
638ACTF(group)
639{
640 return (statbuf->st_gid == ap->gid);
641}
642#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000643#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000644ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000645{
646 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000647 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000648}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000649#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000650ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000651{
652 puts(fileName);
653 return TRUE;
654}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000655#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000656ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000657{
658 return exec_actions(ap->subexpr, fileName, statbuf);
659}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000660#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000661#if ENABLE_FEATURE_FIND_SIZE
662ACTF(size)
663{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000664 if (ap->size_char == '+')
665 return statbuf->st_size > ap->size;
666 if (ap->size_char == '-')
667 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000668 return statbuf->st_size == ap->size;
669}
670#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000671#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000672/*
673 * -prune: if -depth is not given, return true and do not descend
674 * current dir; if -depth is given, return false with no effect.
675 * Example:
676 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
677 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000678ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000679{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000680 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000681}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000682#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000683#if ENABLE_FEATURE_FIND_DELETE
684ACTF(delete)
685{
686 int rc;
687 if (S_ISDIR(statbuf->st_mode)) {
688 rc = rmdir(fileName);
689 } else {
690 rc = unlink(fileName);
691 }
692 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000693 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000694 return TRUE;
695}
696#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000697#if ENABLE_FEATURE_FIND_CONTEXT
698ACTF(context)
699{
700 security_context_t con;
701 int rc;
702
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200703 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000704 rc = getfilecon(fileName, &con);
705 } else {
706 rc = lgetfilecon(fileName, &con);
707 }
708 if (rc < 0)
709 return FALSE;
710 rc = strcmp(ap->context, con);
711 freecon(con);
712 return rc == 0;
713}
714#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100715#if ENABLE_FEATURE_FIND_LINKS
716ACTF(links)
717{
718 switch(ap->links_char) {
719 case '-' : return (statbuf->st_nlink < ap->links_count);
720 case '+' : return (statbuf->st_nlink > ap->links_count);
721 default: return (statbuf->st_nlink == ap->links_count);
722 }
723}
724#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000725
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000726static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000727 struct stat *statbuf,
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100728 void *userData UNUSED_PARAM,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000729 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000730{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100731 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200732 int same_fs = 1;
733
734#if ENABLE_FEATURE_FIND_XDEV
735 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
736 int i;
737 for (i = 0; i < G.xdev_count; i++) {
738 if (G.xdev_dev[i] == statbuf->st_dev)
739 goto found;
740 }
741 //bb_error_msg("'%s': not same fs", fileName);
742 same_fs = 0;
743 found: ;
744 }
745#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000746
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100747#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200748 if (depth < G.minmaxdepth[0]) {
749 if (same_fs)
750 return TRUE; /* skip this, continue recursing */
751 return SKIP; /* stop recursing */
752 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100753 if (depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100754 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000755#endif
756
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100757 r = exec_actions(G.actions, fileName, statbuf);
758 /* Had no explicit -print[0] or -exec? then print */
759 if ((r & TRUE) && G.need_print)
760 puts(fileName);
761
762#if ENABLE_FEATURE_FIND_MAXDEPTH
763 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100764 if (depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100765 return SKIP;
766 }
767#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100768 /* -xdev stops on mountpoints, but AFTER mountpoit itself
769 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200770 if (!same_fs) {
771 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000772 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200773
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000774 /* Cannot return 0: our caller, recursive_action(),
775 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100776 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000777}
778
Eric Andersen17d49ef1999-10-06 20:25:32 +0000779
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000780#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000781static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000782{
783 int mask = 0;
784
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000785 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000786 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000787 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000788 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000789 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000790 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000791 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000792 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000793 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000794 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000795 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000796 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000797 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000798 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000799
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200800 if (mask == 0 || type[1] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000801 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000802
803 return mask;
804}
805#endif
806
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000807#if ENABLE_FEATURE_FIND_PERM \
808 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100809 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000810static const char* plus_minus_num(const char* str)
811{
812 if (*str == '-' || *str == '+')
813 str++;
814 return str;
815}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000816#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000817
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000818static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000819{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000820 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100821 OPT_FOLLOW ,
822 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
823 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200824 PARM_a ,
825 PARM_o ,
826 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000827#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200828 PARM_and ,
829 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000830 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000831#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200832 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000833 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000834 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
835 IF_FEATURE_FIND_DELETE( PARM_delete ,)
836 IF_FEATURE_FIND_EXEC( PARM_exec ,)
837 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100838 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200839 PARM_name ,
840 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000841 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200842#if ENABLE_DESKTOP
843 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +0200844 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200845 IF_FEATURE_FIND_PATH( PARM_wholename ,)
846#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200847 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000848 IF_FEATURE_FIND_REGEX( PARM_regex ,)
849 IF_FEATURE_FIND_TYPE( PARM_type ,)
850 IF_FEATURE_FIND_PERM( PARM_perm ,)
851 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
852 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
853 IF_FEATURE_FIND_NEWER( PARM_newer ,)
854 IF_FEATURE_FIND_INUM( PARM_inum ,)
855 IF_FEATURE_FIND_USER( PARM_user ,)
856 IF_FEATURE_FIND_GROUP( PARM_group ,)
857 IF_FEATURE_FIND_SIZE( PARM_size ,)
858 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100859 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100860 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000861 };
862
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000863 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100864 "-follow\0"
865 IF_FEATURE_FIND_XDEV( "-xdev\0" )
866 IF_FEATURE_FIND_DEPTH( "-depth\0" )
867 "-a\0"
868 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000869 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000870#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100871 "-and\0"
872 "-or\0"
873 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000874#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100875 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000876 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000877 IF_FEATURE_FIND_PRUNE( "-prune\0" )
878 IF_FEATURE_FIND_DELETE( "-delete\0" )
879 IF_FEATURE_FIND_EXEC( "-exec\0" )
880 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100881 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +0100882 "-name\0"
883 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000884 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200885#if ENABLE_DESKTOP
886 IF_FEATURE_FIND_PATH( "-wholename\0")
887#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200888 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000889 IF_FEATURE_FIND_REGEX( "-regex\0" )
890 IF_FEATURE_FIND_TYPE( "-type\0" )
891 IF_FEATURE_FIND_PERM( "-perm\0" )
892 IF_FEATURE_FIND_MTIME( "-mtime\0" )
893 IF_FEATURE_FIND_MMIN( "-mmin\0" )
894 IF_FEATURE_FIND_NEWER( "-newer\0" )
895 IF_FEATURE_FIND_INUM( "-inum\0" )
896 IF_FEATURE_FIND_USER( "-user\0" )
897 IF_FEATURE_FIND_GROUP( "-group\0" )
898 IF_FEATURE_FIND_SIZE( "-size\0" )
899 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100900 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100901 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100902 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +0000903
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000904 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000905 unsigned cur_group = 0;
906 unsigned cur_action = 0;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000907 IF_FEATURE_FIND_NOT( bool invert_flag = 0; )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000908
Denis Vlasenko0f3a5802008-03-20 13:13:09 +0000909 /* This is the only place in busybox where we use nested function.
910 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +0200911 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +0000912 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000913 action* alloc_action(int sizeof_struct, action_fp f)
914 {
915 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000916 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +0200917 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000918 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000919 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000920 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
921 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000922 return ap;
923 }
Denis Vlasenko56fce002007-04-04 23:41:35 +0000924
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000925#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000926
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000927 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000928
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000929 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000930 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000931 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000932 const char *arg1 = argv[1];
933
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200934 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
935
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000936 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100937 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000938 if (!arg1)
939 bb_error_msg_and_die(bb_msg_requires_arg, arg);
940 argv++;
941 }
942
943 /* We can use big switch() here, but on i386
944 * it doesn't give smaller code. Other arches? */
945
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100946/* Options always return true. They always take effect
947 * rather than being processed only when their place in the
948 * expression is reached.
949 */
950 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +0200951 if (parm == OPT_FOLLOW) {
952 dbg("follow enabled: %d", __LINE__);
953 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
954 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100955#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200956 else if (parm == OPT_XDEV) {
957 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100958 G.xdev_on = 1;
959 }
960#endif
961#if ENABLE_FEATURE_FIND_MAXDEPTH
962 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200963 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100964 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
965 }
966#endif
967#if ENABLE_FEATURE_FIND_DEPTH
968 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200969 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100970 G.recurse_flags |= ACTION_DEPTHFIRST;
971 }
972#endif
973/* Actions are grouped by operators
974 * ( expr ) Force precedence
975 * ! expr True if expr is false
976 * -not expr Same as ! expr
977 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
978 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
979 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
980 * We implement: (), -a, -o
981 */
982 /* Operators */
983 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200984 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000985 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000986 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000987 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200988 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000989 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000990 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000991 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000992 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000993 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000994 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000995 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000996#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000997 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000998 /* also handles "find ! ! -name 'foo*'" */
999 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001000 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001001 }
1002#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001003 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001004 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001005 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001006 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001007 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001008 }
1009#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001010 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001011 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001012 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001013 (void) ALLOC_ACTION(print0);
1014 }
Paul Foxd7384292006-05-12 14:47:20 +00001015#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001016#if ENABLE_FEATURE_FIND_PRUNE
1017 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001018 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001019 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001020 }
1021#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001022#if ENABLE_FEATURE_FIND_DELETE
1023 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001024 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001025 G.need_print = 0;
1026 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001027 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001028 }
Rob Landleycee605c2005-10-06 16:39:17 +00001029#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001030#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001031 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001032 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001033 action_exec *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001034 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001035 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001036 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001037 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001038 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001039 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001040 if (!*argv) /* did not see ';' or '+' until end */
1041 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001042 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001043 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001044 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001045 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001046 // TODO (so far we treat "+" just like ";")
1047 if ((argv[0][0] == ';' || argv[0][0] == '+')
1048 && argv[0][1] == '\0'
1049 ) {
Rob Landley5d3a0e82005-10-04 03:34:39 +00001050 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001051 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001052 argv++;
1053 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001054 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001055 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001056 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001057 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001058 i = ap->exec_argc;
1059 while (i--)
1060 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
1061 }
1062#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001063#if ENABLE_FEATURE_FIND_PAREN
1064 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001065 action_paren *ap;
1066 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001067 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001068
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001069 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001070 endarg = argv;
1071 while (1) {
1072 if (!*++endarg)
1073 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001074 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001075 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001076 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001077 *endarg = NULL;
1078 break;
1079 }
1080 }
1081 ap = ALLOC_ACTION(paren);
1082 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001083 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001084 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001085 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001086#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001087 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001088 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001089 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001090 ap = ALLOC_ACTION(name);
1091 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001092 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001093 }
1094#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001095 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001096 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001097 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001098 ap = ALLOC_ACTION(path);
1099 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001100 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001101 }
1102#endif
1103#if ENABLE_FEATURE_FIND_REGEX
1104 else if (parm == PARM_regex) {
1105 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001106 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001107 ap = ALLOC_ACTION(regex);
1108 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1109 }
1110#endif
1111#if ENABLE_FEATURE_FIND_TYPE
1112 else if (parm == PARM_type) {
1113 action_type *ap;
1114 ap = ALLOC_ACTION(type);
1115 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001116 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001117 }
1118#endif
1119#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001120/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001121 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001122 * -perm -BITS All of the BITS are set in file's mode.
1123 * -perm +BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001124 */
1125 else if (parm == PARM_perm) {
1126 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001127 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001128 ap = ALLOC_ACTION(perm);
1129 ap->perm_char = arg1[0];
1130 arg1 = plus_minus_num(arg1);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001131 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001132 if (!bb_parse_mode(arg1, &ap->perm_mask))
Denys Vlasenko651a2692010-03-23 16:25:17 +01001133 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001134 }
1135#endif
1136#if ENABLE_FEATURE_FIND_MTIME
1137 else if (parm == PARM_mtime) {
1138 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001139 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001140 ap = ALLOC_ACTION(mtime);
1141 ap->mtime_char = arg1[0];
1142 ap->mtime_days = xatoul(plus_minus_num(arg1));
1143 }
1144#endif
1145#if ENABLE_FEATURE_FIND_MMIN
1146 else if (parm == PARM_mmin) {
1147 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001148 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001149 ap = ALLOC_ACTION(mmin);
1150 ap->mmin_char = arg1[0];
1151 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1152 }
1153#endif
1154#if ENABLE_FEATURE_FIND_NEWER
1155 else if (parm == PARM_newer) {
1156 struct stat stat_newer;
1157 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001158 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001159 ap = ALLOC_ACTION(newer);
1160 xstat(arg1, &stat_newer);
1161 ap->newer_mtime = stat_newer.st_mtime;
1162 }
1163#endif
1164#if ENABLE_FEATURE_FIND_INUM
1165 else if (parm == PARM_inum) {
1166 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001167 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001168 ap = ALLOC_ACTION(inum);
1169 ap->inode_num = xatoul(arg1);
1170 }
1171#endif
1172#if ENABLE_FEATURE_FIND_USER
1173 else if (parm == PARM_user) {
1174 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001175 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001176 ap = ALLOC_ACTION(user);
1177 ap->uid = bb_strtou(arg1, NULL, 10);
1178 if (errno)
1179 ap->uid = xuname2uid(arg1);
1180 }
1181#endif
1182#if ENABLE_FEATURE_FIND_GROUP
1183 else if (parm == PARM_group) {
1184 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001185 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001186 ap = ALLOC_ACTION(group);
1187 ap->gid = bb_strtou(arg1, NULL, 10);
1188 if (errno)
1189 ap->gid = xgroup2gid(arg1);
1190 }
1191#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001192#if ENABLE_FEATURE_FIND_SIZE
1193 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001194/* -size n[bckw]: file uses n units of space
1195 * b (default): units are 512-byte blocks
1196 * c: 1 byte
1197 * k: kilobytes
1198 * w: 2-byte words
1199 */
1200#if ENABLE_LFS
1201#define XATOU_SFX xatoull_sfx
1202#else
1203#define XATOU_SFX xatoul_sfx
1204#endif
1205 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001206 { "c", 1 },
1207 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001208 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001209 { "b", 512 },
1210 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001211 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001212 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001213 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001214 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001215 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001216 ap->size_char = arg1[0];
1217 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001218 }
1219#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001220#if ENABLE_FEATURE_FIND_CONTEXT
1221 else if (parm == PARM_context) {
1222 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001223 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001224 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001225 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001226 /* SELinux headers erroneously declare non-const parameter */
1227 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001228 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001229 }
1230#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001231#if ENABLE_FEATURE_FIND_LINKS
1232 else if (parm == PARM_links) {
1233 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001234 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001235 ap = ALLOC_ACTION(links);
1236 ap->links_char = arg1[0];
1237 ap->links_count = xatoul(plus_minus_num(arg1));
1238 }
1239#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001240 else {
1241 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001242 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001243 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001244 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001245 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001246 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001247 return appp;
1248#undef ALLOC_ACTION
1249}
1250
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001251int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001252int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001253{
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001254 int i, firstopt, status = EXIT_SUCCESS;
1255
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001256 INIT_G();
1257
Denys Vlasenko6782f482013-05-14 17:20:35 +02001258 /* "+": stop on first non-option */
1259 i = getopt32(argv, "+HLP");
1260 if (i & (1<<0))
1261 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1262 if (i & (1<<1))
1263 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1264 /* -P is default and is ignored */
1265 argv += optind;
1266
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001267 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001268 if (argv[firstopt][0] == '-')
1269 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001270 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1271 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001272 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001273 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001274 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001275 if (firstopt == 0) {
1276 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001277 firstopt++;
1278 }
1279
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001280 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001281 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001282
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001283#if ENABLE_FEATURE_FIND_XDEV
1284 if (G.xdev_on) {
1285 struct stat stbuf;
1286
1287 G.xdev_count = firstopt;
1288 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1289 for (i = 0; argv[i]; i++) {
1290 /* not xstat(): shouldn't bomb out on
1291 * "find not_exist exist -xdev" */
1292 if (stat(argv[i], &stbuf) == 0)
1293 G.xdev_dev[i] = stbuf.st_dev;
1294 /* else G.xdev_dev[i] stays 0 and
1295 * won't match any real device dev_t
1296 */
1297 }
1298 }
1299#endif
1300
1301 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001302 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001303 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001304 fileAction, /* file action */
1305 fileAction, /* dir action */
1306 NULL, /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001307 0) /* depth */
1308 ) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001309 status = EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001310 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001311 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001312
Matt Kraai096370d2001-02-07 03:52:38 +00001313 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001314}