blob: ced8922e7051a5c482550fd3fbf2d0fffd787b55 [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:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200140//config:config FEATURE_FIND_EXEC_PLUS
141//config: bool "Enable -exec ... {} +"
142//config: default y
143//config: depends on FEATURE_FIND_EXEC
144//config: help
145//config: Support the 'find -exec ... {} +' option for executing commands
146//config: for all matched files at once.
147//config: Without this option, -exec + is a synonym for -exec ;
148//config: (IOW: it works correctly, but without expected speedup)
149//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200150//config:config FEATURE_FIND_USER
151//config: bool "Enable -user: username/uid matching"
152//config: default y
153//config: depends on FIND
154//config: help
155//config: Support the 'find -user' option for searching by username or uid.
156//config:
157//config:config FEATURE_FIND_GROUP
158//config: bool "Enable -group: group/gid matching"
159//config: default y
160//config: depends on FIND
161//config: help
162//config: Support the 'find -group' option for searching by group name or gid.
163//config:
164//config:config FEATURE_FIND_NOT
165//config: bool "Enable the 'not' (!) operator"
166//config: default y
167//config: depends on FIND
168//config: help
169//config: Support the '!' operator to invert the test results.
170//config: If 'Enable full-blown desktop' is enabled, then will also support
171//config: the non-POSIX notation '-not'.
172//config:
173//config:config FEATURE_FIND_DEPTH
174//config: bool "Enable -depth"
175//config: default y
176//config: depends on FIND
177//config: help
178//config: Process each directory's contents before the directory itself.
179//config:
180//config:config FEATURE_FIND_PAREN
181//config: bool "Enable parens in options"
182//config: default y
183//config: depends on FIND
184//config: help
185//config: Enable usage of parens '(' to specify logical order of arguments.
186//config:
187//config:config FEATURE_FIND_SIZE
188//config: bool "Enable -size: file size matching"
189//config: default y
190//config: depends on FIND
191//config: help
192//config: Support the 'find -size' option for searching by file size.
193//config:
194//config:config FEATURE_FIND_PRUNE
195//config: bool "Enable -prune: exclude subdirectories"
196//config: default y
197//config: depends on FIND
198//config: help
199//config: If the file is a directory, dont descend into it. Useful for
200//config: exclusion .svn and CVS directories.
201//config:
202//config:config FEATURE_FIND_DELETE
203//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200204//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200205//config: depends on FIND && FEATURE_FIND_DEPTH
206//config: help
207//config: Support the 'find -delete' option for deleting files and directories.
208//config: WARNING: This option can do much harm if used wrong. Busybox will not
209//config: try to protect the user from doing stupid things. Use with care.
210//config:
211//config:config FEATURE_FIND_PATH
212//config: bool "Enable -path: match pathname with shell pattern"
213//config: default y
214//config: depends on FIND
215//config: help
216//config: The -path option matches whole pathname instead of just filename.
217//config:
218//config:config FEATURE_FIND_REGEX
219//config: bool "Enable -regex: match pathname with regex"
220//config: default y
221//config: depends on FIND
222//config: help
223//config: The -regex option matches whole pathname against regular expression.
224//config:
225//config:config FEATURE_FIND_CONTEXT
226//config: bool "Enable -context: security context matching"
227//config: default n
228//config: depends on FIND && SELINUX
229//config: help
230//config: Support the 'find -context' option for matching security context.
231//config:
232//config:config FEATURE_FIND_LINKS
233//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200234//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200235//config: depends on FIND
236//config: help
237//config: Support the 'find -links' option for matching number of links.
238
Denys Vlasenko805f2452011-02-12 20:26:14 +0100239//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
240
241//kbuild:lib-$(CONFIG_FIND) += find.o
242
243//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200244//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100245//usage:#define find_full_usage "\n\n"
246//usage: "Search for files and perform actions on them.\n"
247//usage: "First failed action stops processing of current file.\n"
248//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200249//usage: "\n -L,-follow Follow symlinks"
250//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100251//usage: IF_FEATURE_FIND_XDEV(
252//usage: "\n -xdev Don't descend directories on other filesystems"
253//usage: )
254//usage: IF_FEATURE_FIND_MAXDEPTH(
255//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
256//usage: "\n actions to command line arguments only"
257//usage: "\n -mindepth N Don't act on first N levels"
258//usage: )
259//usage: IF_FEATURE_FIND_DEPTH(
260//usage: "\n -depth Act on directory *after* traversing it"
261//usage: )
262//usage: "\n"
263//usage: "\nActions:"
264//usage: IF_FEATURE_FIND_PAREN(
265//usage: "\n ( ACTIONS ) Group actions for -o / -a"
266//usage: )
267//usage: IF_FEATURE_FIND_NOT(
268//usage: "\n ! ACT Invert ACT's success/failure"
269//usage: )
270//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
271//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
272//usage: "\n Note: -a has higher priority than -o"
273//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
274//usage: "\n -iname PATTERN Case insensitive -name"
275//usage: IF_FEATURE_FIND_PATH(
276//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200277//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100278//usage: )
279//usage: IF_FEATURE_FIND_REGEX(
280//usage: "\n -regex PATTERN Match path to regex PATTERN"
281//usage: )
282//usage: IF_FEATURE_FIND_TYPE(
283//usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
284//usage: )
285//usage: IF_FEATURE_FIND_PERM(
286//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100287//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100288//usage: )
289//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100290//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
291//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100292//usage: )
293//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100294//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
295//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100296//usage: )
297//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100298//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100299//usage: )
300//usage: IF_FEATURE_FIND_INUM(
301//usage: "\n -inum N File has inode number N"
302//usage: )
303//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100304//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100305//usage: )
306//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100307//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100308//usage: )
309//usage: IF_FEATURE_FIND_SIZE(
310//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
311//usage: "\n +/-N: file size is bigger/smaller than N"
312//usage: )
313//usage: IF_FEATURE_FIND_LINKS(
314//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
315//usage: "\n or exactly N"
316//usage: )
317//usage: IF_FEATURE_FIND_CONTEXT(
318//usage: "\n -context CTX File has specified security context"
319//usage: )
320//usage: IF_FEATURE_FIND_PRUNE(
321//usage: "\n -prune If current file is directory, don't descend into it"
322//usage: )
323//usage: "\nIf none of the following actions is specified, -print is assumed"
324//usage: "\n -print Print file name"
325//usage: IF_FEATURE_FIND_PRINT0(
326//usage: "\n -print0 Print file name, NUL terminated"
327//usage: )
328//usage: IF_FEATURE_FIND_EXEC(
329//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
330//usage: "\n file name. Fails if CMD exits with nonzero"
331//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200332//usage: IF_FEATURE_FIND_EXEC_PLUS(
333//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
334//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100335//usage: IF_FEATURE_FIND_DELETE(
336//usage: "\n -delete Delete current file/directory. Turns on -depth option"
337//usage: )
338//usage:
339//usage:#define find_example_usage
340//usage: "$ find / -name passwd\n"
341//usage: "/etc/passwd\n"
342
Matt Kraai096370d2001-02-07 03:52:38 +0000343#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000344#include "libbb.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000345#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200346# include "xregex.h"
347#endif
348/* GNUism: */
349#ifndef FNM_CASEFOLD
350# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000351#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000352
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200353#if 1
354# define dbg(...) ((void)0)
355#else
356# define dbg(...) bb_error_msg(__VA_ARGS__)
357#endif
358
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200359
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000360/* This is a NOEXEC applet. Be very careful! */
361
362
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200363typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000364
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000365typedef struct {
366 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000367#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000368 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000369#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000370} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200371
Denys Vlasenko606291b2009-09-23 23:15:43 +0200372#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200373#define ACTF(name) \
374 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200375 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200376 action_##name* ap UNUSED_PARAM)
377
378 ACTS(print)
379 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200380IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000381IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
382IF_FEATURE_FIND_PRINT0( ACTS(print0))
383IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
384IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
385IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
386IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
387IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
388IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
389IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
390IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
391IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
392IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
393IF_FEATURE_FIND_PRUNE( ACTS(prune))
394IF_FEATURE_FIND_DELETE( ACTS(delete))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200395IF_FEATURE_FIND_EXEC( ACTS(exec,
396 char **exec_argv; /* -exec ARGS */
397 unsigned *subst_count;
398 int exec_argc; /* count of ARGS */
399 IF_FEATURE_FIND_EXEC_PLUS(
400 /*
401 * filelist is NULL if "exec ;"
402 * non-NULL if "exec +"
403 */
404 char **filelist;
405 int filelist_idx;
406 int file_len;
407 )
408 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000409IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100410IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000411
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200412struct globals {
413 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
414 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100415#if ENABLE_FEATURE_FIND_MAXDEPTH
416 int minmaxdepth[2];
417#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200418 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100419 smallint need_print;
420 smallint xdev_on;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200421 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200422 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100423} FIX_ALIASING;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200424#define G (*(struct globals*)&bb_common_bufsiz1)
425#define INIT_G() do { \
426 struct G_sizecheck { \
427 char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \
428 }; \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200429 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100430 memset(&G, 0, sizeof(G)); \
431 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200432 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200433 G.need_print = 1; \
434 G.recurse_flags = ACTION_RECURSE; \
435} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000436
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000437/* Return values of ACTFs ('action functions') are a bit mask:
438 * bit 1=1: prune (use SKIP constant for setting it)
439 * bit 0=1: matched successfully (TRUE)
440 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000441
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200442static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000443{
444 int cur_group;
445 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000446 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000447 action **app, *ap;
448
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000449 /* "action group" is a set of actions ANDed together.
450 * groups are ORed together.
451 * We simply evaluate each group until we find one in which all actions
452 * succeed. */
453
454 /* -prune is special: if it is encountered, then we won't
455 * descend into current directory. It doesn't matter whether
456 * action group (in which -prune sits) will succeed or not:
457 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
458 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
459 * not starting with 'f' */
460
461 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
462 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
463 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
464 * On return, bit is restored. */
465
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000466 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100467 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000468 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000469 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000470 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000471 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000472 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000473 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000474 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000475#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000476 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000477#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200478 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000479 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000480 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000481 }
482 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200483 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000484 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000485}
486
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200487#if !FNM_CASEFOLD
488static char *strcpy_upcase(char *dst, const char *src)
489{
490 char *d = dst;
491 while (1) {
492 unsigned char ch = *src++;
493 if (ch >= 'a' && ch <= 'z')
494 ch -= ('a' - 'A');
495 *d++ = ch;
496 if (ch == '\0')
497 break;
498 }
499 return dst;
500}
501#endif
502
Denis Vlasenko92258542006-11-01 10:25:35 +0000503ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000504{
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000505 const char *tmp = bb_basename(fileName);
Denys Vlasenko91388172010-04-26 09:54:04 +0200506 if (tmp != fileName && *tmp == '\0') {
507 /* "foo/bar/". Oh no... go back to 'b' */
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000508 tmp--;
509 while (tmp != fileName && *--tmp != '/')
510 continue;
511 if (*tmp == '/')
512 tmp++;
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000513 }
Denys Vlasenko91388172010-04-26 09:54:04 +0200514 /* Was using FNM_PERIOD flag too,
515 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
516 * find -name '*foo' should match .foo too:
517 */
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200518#if FNM_CASEFOLD
Denys Vlasenko91388172010-04-26 09:54:04 +0200519 return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200520#else
521 if (ap->iname)
522 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
523 return fnmatch(ap->pattern, tmp, 0) == 0;
524#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000525}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000526
Denis Vlasenkob9411292007-06-15 23:43:11 +0000527#if ENABLE_FEATURE_FIND_PATH
528ACTF(path)
529{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200530# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200531 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200532# else
533 if (ap->ipath)
534 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
535 return fnmatch(ap->pattern, fileName, 0) == 0;
536# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000537}
538#endif
539#if ENABLE_FEATURE_FIND_REGEX
540ACTF(regex)
541{
542 regmatch_t match;
543 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
544 return 0; /* no match */
545 if (match.rm_so)
546 return 0; /* match doesn't start at pos 0 */
547 if (fileName[match.rm_eo])
548 return 0; /* match doesn't end exactly at end of pathname */
549 return 1;
550}
551#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000552#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000553ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000554{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000555 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000556}
Rob Landleycee605c2005-10-06 16:39:17 +0000557#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000558#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000559ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000560{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200561 /* -perm [+/]mode: at least one of perm_mask bits are set */
562 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000563 return (statbuf->st_mode & ap->perm_mask) != 0;
564 /* -perm -mode: all of perm_mask are set */
565 if (ap->perm_char == '-')
566 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
567 /* -perm mode: file mode must match perm_mask */
568 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000569}
Rob Landleycee605c2005-10-06 16:39:17 +0000570#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000571#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000572ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000573{
574 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000575 time_t mtime_secs = ap->mtime_days * 24*60*60;
576 if (ap->mtime_char == '+')
577 return file_age >= mtime_secs + 24*60*60;
578 if (ap->mtime_char == '-')
579 return file_age < mtime_secs;
580 /* just numeric mtime */
581 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000582}
Rob Landleycee605c2005-10-06 16:39:17 +0000583#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000584#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000585ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000586{
587 time_t file_age = time(NULL) - statbuf->st_mtime;
588 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000589 if (ap->mmin_char == '+')
590 return file_age >= mmin_secs + 60;
591 if (ap->mmin_char == '-')
592 return file_age < mmin_secs;
593 /* just numeric mmin */
594 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000595}
Paul Fox72d1a232006-01-13 21:05:41 +0000596#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000597#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000598ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000599{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000600 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000601}
Rob Landleycee605c2005-10-06 16:39:17 +0000602#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000603#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000604ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000605{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000606 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000607}
Rob Landleycee605c2005-10-06 16:39:17 +0000608#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000609#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200610static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000611{
612 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200613# if ENABLE_FEATURE_FIND_EXEC_PLUS
614 int size = ap->exec_argc + ap->filelist_idx + 1;
615# else
616 int size = ap->exec_argc + 1;
617# endif
618# if ENABLE_USE_PORTABLE_CODE
619 char **argv = alloca(sizeof(char*) * size);
620# else /* gcc 4.3.1 generates smaller code: */
621 char *argv[size];
622# endif
623 char **pp = argv;
624
625 for (i = 0; i < ap->exec_argc; i++) {
626 const char *arg = ap->exec_argv[i];
627
628# if ENABLE_FEATURE_FIND_EXEC_PLUS
629 if (ap->filelist) {
630 /* Handling "-exec +"
631 * Only one exec_argv[i] has substitution in it.
632 * Expand that one exec_argv[i] into file list.
633 */
634 if (ap->subst_count[i] == 0) {
635 *pp++ = xstrdup(arg);
636 } else {
637 int j = 0;
638 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200639 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200640 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
641 free(ap->filelist[j]);
642 j++;
643 }
644 }
645 } else
646# endif
647 {
648 /* Handling "-exec ;" */
649 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
650 }
651 }
652 *pp = NULL; /* terminate the list */
653
654# if ENABLE_FEATURE_FIND_EXEC_PLUS
655 if (ap->filelist) {
656 ap->filelist[0] = NULL;
657 ap->filelist_idx = 0;
658 ap->file_len = 0;
659 }
660# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000661
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000662 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000663 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000664 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000665
666 i = 0;
667 while (argv[i])
668 free(argv[i++]);
669 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000670}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200671ACTF(exec)
672{
673# if ENABLE_FEATURE_FIND_EXEC_PLUS
674 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200675 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200676
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200677 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
678 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200679 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
680 /* If we have lots of files already, exec the command */
681 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200682 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200683 rc = do_exec(ap, NULL);
684 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200685 }
686# endif
687 return do_exec(ap, fileName);
688}
689# if ENABLE_FEATURE_FIND_EXEC_PLUS
690static int flush_exec_plus(void)
691{
692 action *ap;
693 action **app;
694 action ***appp = G.actions;
695 while ((app = *appp++) != NULL) {
696 while ((ap = *app++) != NULL) {
697 if (ap->f == (action_fp)func_exec) {
698 action_exec *ae = (void*)ap;
699 if (ae->filelist_idx != 0) {
700 int rc = do_exec(ae, NULL);
701# if ENABLE_FEATURE_FIND_NOT
702 if (ap->invert) rc = !rc;
703# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200704 if (rc == 0)
705 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200706 }
707 }
708 }
709 }
710 return 0;
711}
712# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000713#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000714#if ENABLE_FEATURE_FIND_USER
715ACTF(user)
716{
717 return (statbuf->st_uid == ap->uid);
718}
719#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000720#if ENABLE_FEATURE_FIND_GROUP
721ACTF(group)
722{
723 return (statbuf->st_gid == ap->gid);
724}
725#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000726#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000727ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000728{
729 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000730 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000731}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000732#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000733ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000734{
735 puts(fileName);
736 return TRUE;
737}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000738#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000739ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000740{
741 return exec_actions(ap->subexpr, fileName, statbuf);
742}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000743#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000744#if ENABLE_FEATURE_FIND_SIZE
745ACTF(size)
746{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000747 if (ap->size_char == '+')
748 return statbuf->st_size > ap->size;
749 if (ap->size_char == '-')
750 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000751 return statbuf->st_size == ap->size;
752}
753#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000754#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000755/*
756 * -prune: if -depth is not given, return true and do not descend
757 * current dir; if -depth is given, return false with no effect.
758 * Example:
759 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
760 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000761ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000762{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000763 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000764}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000765#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000766#if ENABLE_FEATURE_FIND_DELETE
767ACTF(delete)
768{
769 int rc;
770 if (S_ISDIR(statbuf->st_mode)) {
771 rc = rmdir(fileName);
772 } else {
773 rc = unlink(fileName);
774 }
775 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000776 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000777 return TRUE;
778}
779#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000780#if ENABLE_FEATURE_FIND_CONTEXT
781ACTF(context)
782{
783 security_context_t con;
784 int rc;
785
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200786 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000787 rc = getfilecon(fileName, &con);
788 } else {
789 rc = lgetfilecon(fileName, &con);
790 }
791 if (rc < 0)
792 return FALSE;
793 rc = strcmp(ap->context, con);
794 freecon(con);
795 return rc == 0;
796}
797#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100798#if ENABLE_FEATURE_FIND_LINKS
799ACTF(links)
800{
801 switch(ap->links_char) {
802 case '-' : return (statbuf->st_nlink < ap->links_count);
803 case '+' : return (statbuf->st_nlink > ap->links_count);
804 default: return (statbuf->st_nlink == ap->links_count);
805 }
806}
807#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000808
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000809static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000810 struct stat *statbuf,
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100811 void *userData UNUSED_PARAM,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000812 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000813{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100814 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200815 int same_fs = 1;
816
817#if ENABLE_FEATURE_FIND_XDEV
818 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
819 int i;
820 for (i = 0; i < G.xdev_count; i++) {
821 if (G.xdev_dev[i] == statbuf->st_dev)
822 goto found;
823 }
824 //bb_error_msg("'%s': not same fs", fileName);
825 same_fs = 0;
826 found: ;
827 }
828#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000829
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100830#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200831 if (depth < G.minmaxdepth[0]) {
832 if (same_fs)
833 return TRUE; /* skip this, continue recursing */
834 return SKIP; /* stop recursing */
835 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100836 if (depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100837 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000838#endif
839
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100840 r = exec_actions(G.actions, fileName, statbuf);
841 /* Had no explicit -print[0] or -exec? then print */
842 if ((r & TRUE) && G.need_print)
843 puts(fileName);
844
845#if ENABLE_FEATURE_FIND_MAXDEPTH
846 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100847 if (depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100848 return SKIP;
849 }
850#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100851 /* -xdev stops on mountpoints, but AFTER mountpoit itself
852 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200853 if (!same_fs) {
854 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000855 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200856
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000857 /* Cannot return 0: our caller, recursive_action(),
858 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100859 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000860}
861
Eric Andersen17d49ef1999-10-06 20:25:32 +0000862
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000863#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000864static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000865{
866 int mask = 0;
867
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000868 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000869 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000870 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000871 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000872 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000873 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000874 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000875 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000876 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000877 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000878 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000879 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000880 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000881 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000882
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200883 if (mask == 0 || type[1] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000884 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000885
886 return mask;
887}
888#endif
889
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000890#if ENABLE_FEATURE_FIND_PERM \
891 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100892 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000893static const char* plus_minus_num(const char* str)
894{
895 if (*str == '-' || *str == '+')
896 str++;
897 return str;
898}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000899#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000900
Denys Vlasenko6db5f672013-05-16 18:36:42 +0200901/* Say no to GCCism */
902#define USE_NESTED_FUNCTION 0
903
904#if !USE_NESTED_FUNCTION
905struct pp_locals {
906 action*** appp;
907 unsigned cur_group;
908 unsigned cur_action;
909 IF_FEATURE_FIND_NOT( bool invert_flag; )
910};
911static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
912{
913 action *ap = xzalloc(sizeof_struct);
914 action **app;
915 action ***group = &ppl->appp[ppl->cur_group];
916 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
917 app[ppl->cur_action++] = ap;
918 app[ppl->cur_action] = NULL;
919 ap->f = f;
920 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
921 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
922 return ap;
923}
924#endif
925
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000926static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000927{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000928 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100929 OPT_FOLLOW ,
930 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
931 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200932 PARM_a ,
933 PARM_o ,
934 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000935#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200936 PARM_and ,
937 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000938 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000939#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200940 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000941 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000942 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
943 IF_FEATURE_FIND_DELETE( PARM_delete ,)
944 IF_FEATURE_FIND_EXEC( PARM_exec ,)
945 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100946 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200947 PARM_name ,
948 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000949 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200950#if ENABLE_DESKTOP
951 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +0200952 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200953 IF_FEATURE_FIND_PATH( PARM_wholename ,)
954#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200955 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000956 IF_FEATURE_FIND_REGEX( PARM_regex ,)
957 IF_FEATURE_FIND_TYPE( PARM_type ,)
958 IF_FEATURE_FIND_PERM( PARM_perm ,)
959 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
960 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
961 IF_FEATURE_FIND_NEWER( PARM_newer ,)
962 IF_FEATURE_FIND_INUM( PARM_inum ,)
963 IF_FEATURE_FIND_USER( PARM_user ,)
964 IF_FEATURE_FIND_GROUP( PARM_group ,)
965 IF_FEATURE_FIND_SIZE( PARM_size ,)
966 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100967 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100968 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000969 };
970
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000971 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100972 "-follow\0"
973 IF_FEATURE_FIND_XDEV( "-xdev\0" )
974 IF_FEATURE_FIND_DEPTH( "-depth\0" )
975 "-a\0"
976 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000977 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000978#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100979 "-and\0"
980 "-or\0"
981 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000982#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100983 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000984 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000985 IF_FEATURE_FIND_PRUNE( "-prune\0" )
986 IF_FEATURE_FIND_DELETE( "-delete\0" )
987 IF_FEATURE_FIND_EXEC( "-exec\0" )
988 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100989 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +0100990 "-name\0"
991 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000992 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200993#if ENABLE_DESKTOP
994 IF_FEATURE_FIND_PATH( "-wholename\0")
995#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200996 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000997 IF_FEATURE_FIND_REGEX( "-regex\0" )
998 IF_FEATURE_FIND_TYPE( "-type\0" )
999 IF_FEATURE_FIND_PERM( "-perm\0" )
1000 IF_FEATURE_FIND_MTIME( "-mtime\0" )
1001 IF_FEATURE_FIND_MMIN( "-mmin\0" )
1002 IF_FEATURE_FIND_NEWER( "-newer\0" )
1003 IF_FEATURE_FIND_INUM( "-inum\0" )
1004 IF_FEATURE_FIND_USER( "-user\0" )
1005 IF_FEATURE_FIND_GROUP( "-group\0" )
1006 IF_FEATURE_FIND_SIZE( "-size\0" )
1007 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001008 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001009 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001010 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001011
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001012#if !USE_NESTED_FUNCTION
1013 struct pp_locals ppl;
1014#define appp (ppl.appp )
1015#define cur_group (ppl.cur_group )
1016#define cur_action (ppl.cur_action )
1017#define invert_flag (ppl.invert_flag)
1018#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1019#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001020 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001021 unsigned cur_group;
1022 unsigned cur_action;
1023 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001024
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001025 /* This is the only place in busybox where we use nested function.
1026 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001027 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001028 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001029 action* alloc_action(int sizeof_struct, action_fp f)
1030 {
1031 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001032 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001033 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001034 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001035 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001036 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1037 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001038 return ap;
1039 }
1040#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001041#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001042
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001043 cur_group = 0;
1044 cur_action = 0;
1045 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001046 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001047
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001048 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001049 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001050 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001051 const char *arg1 = argv[1];
1052
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001053 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1054
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001055 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001056 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001057 if (!arg1)
1058 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1059 argv++;
1060 }
1061
1062 /* We can use big switch() here, but on i386
1063 * it doesn't give smaller code. Other arches? */
1064
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001065/* Options always return true. They always take effect
1066 * rather than being processed only when their place in the
1067 * expression is reached.
1068 */
1069 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001070 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001071 dbg("follow enabled: %d", __LINE__);
1072 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001073 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001074#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001075 else if (parm == OPT_XDEV) {
1076 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001077 G.xdev_on = 1;
1078 }
1079#endif
1080#if ENABLE_FEATURE_FIND_MAXDEPTH
1081 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001082 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001083 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1084 }
1085#endif
1086#if ENABLE_FEATURE_FIND_DEPTH
1087 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001088 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001089 G.recurse_flags |= ACTION_DEPTHFIRST;
1090 }
1091#endif
1092/* Actions are grouped by operators
1093 * ( expr ) Force precedence
1094 * ! expr True if expr is false
1095 * -not expr Same as ! expr
1096 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1097 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1098 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1099 * We implement: (), -a, -o
1100 */
1101 /* Operators */
1102 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001103 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001104 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001105 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001106 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001107 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001108 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001109 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001110 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001111 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001112 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001113 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001114 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001115#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001116 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001117 /* also handles "find ! ! -name 'foo*'" */
1118 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001119 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001120 }
1121#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001122 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001123 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001124 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001125 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001126 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001127 }
1128#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001129 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001130 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001131 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001132 (void) ALLOC_ACTION(print0);
1133 }
Paul Foxd7384292006-05-12 14:47:20 +00001134#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001135#if ENABLE_FEATURE_FIND_PRUNE
1136 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001137 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001138 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001139 }
1140#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001141#if ENABLE_FEATURE_FIND_DELETE
1142 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001143 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001144 G.need_print = 0;
1145 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001146 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001147 }
Rob Landleycee605c2005-10-06 16:39:17 +00001148#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001149#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001150 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001151 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001152 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001153 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001154 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001155 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001156 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001157 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001158 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001159 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001160 if (!*argv) /* did not see ';' or '+' until end */
1161 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001162 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001163 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001164 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001165 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001166 if ((argv[0][0] == ';' || argv[0][0] == '+')
1167 && argv[0][1] == '\0'
1168 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001169# if ENABLE_FEATURE_FIND_EXEC_PLUS
1170 if (argv[0][0] == '+')
1171 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1172# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001173 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001174 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001175 argv++;
1176 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001177 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001178 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001179 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001180 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001181 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001182 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001183 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001184 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1185 }
1186# if ENABLE_FEATURE_FIND_EXEC_PLUS
1187 /*
1188 * coreutils expects {} to appear only once in "-exec +"
1189 */
1190 if (all_subst != 1 && ap->filelist)
1191 bb_error_msg_and_die("only one '{}' allowed for -exec +");
1192# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001193 }
1194#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001195#if ENABLE_FEATURE_FIND_PAREN
1196 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001197 action_paren *ap;
1198 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001199 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001200
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001201 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001202 endarg = argv;
1203 while (1) {
1204 if (!*++endarg)
1205 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001206 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001207 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001208 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001209 *endarg = NULL;
1210 break;
1211 }
1212 }
1213 ap = ALLOC_ACTION(paren);
1214 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001215 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001216 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001217 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001218#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001219 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001220 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001221 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001222 ap = ALLOC_ACTION(name);
1223 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001224 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001225 }
1226#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001227 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001228 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001229 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001230 ap = ALLOC_ACTION(path);
1231 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001232 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001233 }
1234#endif
1235#if ENABLE_FEATURE_FIND_REGEX
1236 else if (parm == PARM_regex) {
1237 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001238 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001239 ap = ALLOC_ACTION(regex);
1240 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1241 }
1242#endif
1243#if ENABLE_FEATURE_FIND_TYPE
1244 else if (parm == PARM_type) {
1245 action_type *ap;
1246 ap = ALLOC_ACTION(type);
1247 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001248 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001249 }
1250#endif
1251#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001252/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001253 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001254 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001255 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001256 */
1257 else if (parm == PARM_perm) {
1258 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001259 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001260 ap = ALLOC_ACTION(perm);
1261 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001262 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001263 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001264 if (!bb_parse_mode(arg1, &ap->perm_mask))
Denys Vlasenko651a2692010-03-23 16:25:17 +01001265 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001266 }
1267#endif
1268#if ENABLE_FEATURE_FIND_MTIME
1269 else if (parm == PARM_mtime) {
1270 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001271 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001272 ap = ALLOC_ACTION(mtime);
1273 ap->mtime_char = arg1[0];
1274 ap->mtime_days = xatoul(plus_minus_num(arg1));
1275 }
1276#endif
1277#if ENABLE_FEATURE_FIND_MMIN
1278 else if (parm == PARM_mmin) {
1279 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001280 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001281 ap = ALLOC_ACTION(mmin);
1282 ap->mmin_char = arg1[0];
1283 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1284 }
1285#endif
1286#if ENABLE_FEATURE_FIND_NEWER
1287 else if (parm == PARM_newer) {
1288 struct stat stat_newer;
1289 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001290 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001291 ap = ALLOC_ACTION(newer);
1292 xstat(arg1, &stat_newer);
1293 ap->newer_mtime = stat_newer.st_mtime;
1294 }
1295#endif
1296#if ENABLE_FEATURE_FIND_INUM
1297 else if (parm == PARM_inum) {
1298 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001299 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001300 ap = ALLOC_ACTION(inum);
1301 ap->inode_num = xatoul(arg1);
1302 }
1303#endif
1304#if ENABLE_FEATURE_FIND_USER
1305 else if (parm == PARM_user) {
1306 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001307 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001308 ap = ALLOC_ACTION(user);
1309 ap->uid = bb_strtou(arg1, NULL, 10);
1310 if (errno)
1311 ap->uid = xuname2uid(arg1);
1312 }
1313#endif
1314#if ENABLE_FEATURE_FIND_GROUP
1315 else if (parm == PARM_group) {
1316 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001317 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001318 ap = ALLOC_ACTION(group);
1319 ap->gid = bb_strtou(arg1, NULL, 10);
1320 if (errno)
1321 ap->gid = xgroup2gid(arg1);
1322 }
1323#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001324#if ENABLE_FEATURE_FIND_SIZE
1325 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001326/* -size n[bckw]: file uses n units of space
1327 * b (default): units are 512-byte blocks
1328 * c: 1 byte
1329 * k: kilobytes
1330 * w: 2-byte words
1331 */
1332#if ENABLE_LFS
1333#define XATOU_SFX xatoull_sfx
1334#else
1335#define XATOU_SFX xatoul_sfx
1336#endif
1337 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001338 { "c", 1 },
1339 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001340 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001341 { "b", 512 },
1342 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001343 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001344 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001345 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001346 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001347 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001348 ap->size_char = arg1[0];
1349 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001350 }
1351#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001352#if ENABLE_FEATURE_FIND_CONTEXT
1353 else if (parm == PARM_context) {
1354 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001355 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001356 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001357 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001358 /* SELinux headers erroneously declare non-const parameter */
1359 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001360 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001361 }
1362#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001363#if ENABLE_FEATURE_FIND_LINKS
1364 else if (parm == PARM_links) {
1365 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001366 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001367 ap = ALLOC_ACTION(links);
1368 ap->links_char = arg1[0];
1369 ap->links_count = xatoul(plus_minus_num(arg1));
1370 }
1371#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001372 else {
1373 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001374 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001375 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001376 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001377 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001378 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001379 return appp;
1380#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001381#undef appp
1382#undef cur_action
1383#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001384}
1385
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001386int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001387int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001388{
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001389 int i, firstopt, status = EXIT_SUCCESS;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001390 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001391
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001392 INIT_G();
1393
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001394 /* "find -type f" + getopt("+HLP") => disaster.
1395 * Need to avoid getopt running into a non-HLP option.
1396 * Do this by temporarily storing NULL there:
1397 */
1398 past_HLP = argv;
1399 for (;;) {
1400 saved = *++past_HLP;
1401 if (!saved)
1402 break;
1403 if (saved[0] != '-')
1404 break;
1405 if (!saved[1])
1406 break; /* it is "-" */
1407 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1408 break;
1409 }
1410 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001411 /* "+": stop on first non-option */
1412 i = getopt32(argv, "+HLP");
1413 if (i & (1<<0))
1414 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1415 if (i & (1<<1))
1416 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1417 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001418 argv = past_HLP; /* same result as "argv += optind;" */
1419 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001420
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001421 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001422 if (argv[firstopt][0] == '-')
1423 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001424 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1425 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001426 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001427 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001428 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001429 if (firstopt == 0) {
1430 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001431 firstopt++;
1432 }
1433
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001434 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001435 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001436
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001437#if ENABLE_FEATURE_FIND_XDEV
1438 if (G.xdev_on) {
1439 struct stat stbuf;
1440
1441 G.xdev_count = firstopt;
1442 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1443 for (i = 0; argv[i]; i++) {
1444 /* not xstat(): shouldn't bomb out on
1445 * "find not_exist exist -xdev" */
1446 if (stat(argv[i], &stbuf) == 0)
1447 G.xdev_dev[i] = stbuf.st_dev;
1448 /* else G.xdev_dev[i] stays 0 and
1449 * won't match any real device dev_t
1450 */
1451 }
1452 }
1453#endif
1454
1455 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001456 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001457 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001458 fileAction, /* file action */
1459 fileAction, /* dir action */
1460 NULL, /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001461 0) /* depth */
1462 ) {
Felix Fietkauf7466e42015-04-09 10:20:16 +02001463 status |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001464 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001465 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001466
Felix Fietkauf7466e42015-04-09 10:20:16 +02001467 IF_FEATURE_FIND_EXEC_PLUS(status |= flush_exec_plus();)
Matt Kraai096370d2001-02-07 03:52:38 +00001468 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001469}