blob: 0596c005929cda2e5bfc85a2bf01e355a37c1fb1 [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
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020092//config:
93//config:config FEATURE_FIND_TYPE
94//config: bool "Enable -type: file type matching (file/dir/link/...)"
95//config: default y
96//config: depends on FIND
97//config: help
98//config: Enable searching based on file type (file,
99//config: directory, socket, device, etc.).
100//config:
101//config:config FEATURE_FIND_XDEV
102//config: bool "Enable -xdev: 'stay in filesystem'"
103//config: default y
104//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200105//config:
106//config:config FEATURE_FIND_MAXDEPTH
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100107//config: bool "Enable -mindepth N and -maxdepth N"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200108//config: default y
109//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200110//config:
111//config:config FEATURE_FIND_NEWER
112//config: bool "Enable -newer: compare file modification times"
113//config: default y
114//config: depends on FIND
115//config: help
116//config: Support the 'find -newer' option for finding any files which have
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100117//config: modification time that is more recent than the specified FILE.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200118//config:
119//config:config FEATURE_FIND_INUM
120//config: bool "Enable -inum: inode number matching"
121//config: default y
122//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200123//config:
124//config:config FEATURE_FIND_EXEC
125//config: bool "Enable -exec: execute commands"
126//config: default y
127//config: depends on FIND
128//config: help
129//config: Support the 'find -exec' option for executing commands based upon
130//config: the files matched.
131//config:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200132//config:config FEATURE_FIND_EXEC_PLUS
133//config: bool "Enable -exec ... {} +"
134//config: default y
135//config: depends on FEATURE_FIND_EXEC
136//config: help
137//config: Support the 'find -exec ... {} +' option for executing commands
138//config: for all matched files at once.
139//config: Without this option, -exec + is a synonym for -exec ;
140//config: (IOW: it works correctly, but without expected speedup)
141//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200142//config:config FEATURE_FIND_USER
143//config: bool "Enable -user: username/uid matching"
144//config: default y
145//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200146//config:
147//config:config FEATURE_FIND_GROUP
148//config: bool "Enable -group: group/gid matching"
149//config: default y
150//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200151//config:
152//config:config FEATURE_FIND_NOT
153//config: bool "Enable the 'not' (!) operator"
154//config: default y
155//config: depends on FIND
156//config: help
157//config: Support the '!' operator to invert the test results.
158//config: If 'Enable full-blown desktop' is enabled, then will also support
159//config: the non-POSIX notation '-not'.
160//config:
161//config:config FEATURE_FIND_DEPTH
162//config: bool "Enable -depth"
163//config: default y
164//config: depends on FIND
165//config: help
166//config: Process each directory's contents before the directory itself.
167//config:
168//config:config FEATURE_FIND_PAREN
169//config: bool "Enable parens in options"
170//config: default y
171//config: depends on FIND
172//config: help
173//config: Enable usage of parens '(' to specify logical order of arguments.
174//config:
175//config:config FEATURE_FIND_SIZE
176//config: bool "Enable -size: file size matching"
177//config: default y
178//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200179//config:
180//config:config FEATURE_FIND_PRUNE
181//config: bool "Enable -prune: exclude subdirectories"
182//config: default y
183//config: depends on FIND
184//config: help
Denys Vlasenko10ad6222017-04-17 16:13:32 +0200185//config: If the file is a directory, don't descend into it. Useful for
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200186//config: exclusion .svn and CVS directories.
187//config:
188//config:config FEATURE_FIND_DELETE
189//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200190//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200191//config: depends on FIND && FEATURE_FIND_DEPTH
192//config: help
193//config: Support the 'find -delete' option for deleting files and directories.
194//config: WARNING: This option can do much harm if used wrong. Busybox will not
195//config: try to protect the user from doing stupid things. Use with care.
196//config:
197//config:config FEATURE_FIND_PATH
198//config: bool "Enable -path: match pathname with shell pattern"
199//config: default y
200//config: depends on FIND
201//config: help
202//config: The -path option matches whole pathname instead of just filename.
203//config:
204//config:config FEATURE_FIND_REGEX
205//config: bool "Enable -regex: match pathname with regex"
206//config: default y
207//config: depends on FIND
208//config: help
209//config: The -regex option matches whole pathname against regular expression.
210//config:
211//config:config FEATURE_FIND_CONTEXT
212//config: bool "Enable -context: security context matching"
213//config: default n
214//config: depends on FIND && SELINUX
215//config: help
216//config: Support the 'find -context' option for matching security context.
217//config:
218//config:config FEATURE_FIND_LINKS
219//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200220//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200221//config: depends on FIND
222//config: help
223//config: Support the 'find -links' option for matching number of links.
224
Denys Vlasenko805f2452011-02-12 20:26:14 +0100225//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
226
227//kbuild:lib-$(CONFIG_FIND) += find.o
228
229//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200230//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100231//usage:#define find_full_usage "\n\n"
232//usage: "Search for files and perform actions on them.\n"
233//usage: "First failed action stops processing of current file.\n"
234//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200235//usage: "\n -L,-follow Follow symlinks"
236//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100237//usage: IF_FEATURE_FIND_XDEV(
238//usage: "\n -xdev Don't descend directories on other filesystems"
239//usage: )
240//usage: IF_FEATURE_FIND_MAXDEPTH(
241//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
242//usage: "\n actions to command line arguments only"
243//usage: "\n -mindepth N Don't act on first N levels"
244//usage: )
245//usage: IF_FEATURE_FIND_DEPTH(
246//usage: "\n -depth Act on directory *after* traversing it"
247//usage: )
248//usage: "\n"
249//usage: "\nActions:"
250//usage: IF_FEATURE_FIND_PAREN(
251//usage: "\n ( ACTIONS ) Group actions for -o / -a"
252//usage: )
253//usage: IF_FEATURE_FIND_NOT(
254//usage: "\n ! ACT Invert ACT's success/failure"
255//usage: )
256//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
257//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
258//usage: "\n Note: -a has higher priority than -o"
259//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
260//usage: "\n -iname PATTERN Case insensitive -name"
261//usage: IF_FEATURE_FIND_PATH(
262//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200263//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100264//usage: )
265//usage: IF_FEATURE_FIND_REGEX(
266//usage: "\n -regex PATTERN Match path to regex PATTERN"
267//usage: )
268//usage: IF_FEATURE_FIND_TYPE(
269//usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
270//usage: )
271//usage: IF_FEATURE_FIND_PERM(
272//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100273//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100274//usage: )
275//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100276//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
277//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100278//usage: )
279//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100280//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
281//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100282//usage: )
283//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100284//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100285//usage: )
286//usage: IF_FEATURE_FIND_INUM(
287//usage: "\n -inum N File has inode number N"
288//usage: )
289//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100290//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100291//usage: )
292//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100293//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100294//usage: )
295//usage: IF_FEATURE_FIND_SIZE(
296//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
297//usage: "\n +/-N: file size is bigger/smaller than N"
298//usage: )
299//usage: IF_FEATURE_FIND_LINKS(
300//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
301//usage: "\n or exactly N"
302//usage: )
303//usage: IF_FEATURE_FIND_CONTEXT(
304//usage: "\n -context CTX File has specified security context"
305//usage: )
306//usage: IF_FEATURE_FIND_PRUNE(
307//usage: "\n -prune If current file is directory, don't descend into it"
308//usage: )
309//usage: "\nIf none of the following actions is specified, -print is assumed"
310//usage: "\n -print Print file name"
311//usage: IF_FEATURE_FIND_PRINT0(
312//usage: "\n -print0 Print file name, NUL terminated"
313//usage: )
314//usage: IF_FEATURE_FIND_EXEC(
315//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
316//usage: "\n file name. Fails if CMD exits with nonzero"
317//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200318//usage: IF_FEATURE_FIND_EXEC_PLUS(
319//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
320//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100321//usage: IF_FEATURE_FIND_DELETE(
322//usage: "\n -delete Delete current file/directory. Turns on -depth option"
323//usage: )
324//usage:
325//usage:#define find_example_usage
326//usage: "$ find / -name passwd\n"
327//usage: "/etc/passwd\n"
328
Matt Kraai096370d2001-02-07 03:52:38 +0000329#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000330#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200331#include "common_bufsiz.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
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200340#if 1
341# define dbg(...) ((void)0)
342#else
343# define dbg(...) bb_error_msg(__VA_ARGS__)
344#endif
345
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200346
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000347/* This is a NOEXEC applet. Be very careful! */
348
349
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200350typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000351
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000352typedef struct {
353 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000354#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000355 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000356#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000357} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200358
Denys Vlasenko606291b2009-09-23 23:15:43 +0200359#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200360#define ACTF(name) \
361 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200362 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200363 action_##name* ap UNUSED_PARAM)
364
365 ACTS(print)
366 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200367IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000368IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
369IF_FEATURE_FIND_PRINT0( ACTS(print0))
370IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
371IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
372IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
373IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
374IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
375IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
376IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
377IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
378IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
379IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
380IF_FEATURE_FIND_PRUNE( ACTS(prune))
381IF_FEATURE_FIND_DELETE( ACTS(delete))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200382IF_FEATURE_FIND_EXEC( ACTS(exec,
383 char **exec_argv; /* -exec ARGS */
384 unsigned *subst_count;
385 int exec_argc; /* count of ARGS */
386 IF_FEATURE_FIND_EXEC_PLUS(
387 /*
388 * filelist is NULL if "exec ;"
389 * non-NULL if "exec +"
390 */
391 char **filelist;
392 int filelist_idx;
393 int file_len;
394 )
395 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000396IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100397IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000398
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200399struct globals {
400 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
401 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100402#if ENABLE_FEATURE_FIND_MAXDEPTH
403 int minmaxdepth[2];
404#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200405 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100406 smallint need_print;
407 smallint xdev_on;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200408 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200409 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100410} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200411#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200412#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200413 setup_common_bufsiz(); \
Denys Vlasenko7b85ec32015-10-13 17:17:34 +0200414 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200415 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100416 memset(&G, 0, sizeof(G)); \
417 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200418 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200419 G.need_print = 1; \
420 G.recurse_flags = ACTION_RECURSE; \
421} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000422
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000423/* Return values of ACTFs ('action functions') are a bit mask:
424 * bit 1=1: prune (use SKIP constant for setting it)
425 * bit 0=1: matched successfully (TRUE)
426 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000427
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200428static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000429{
430 int cur_group;
431 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000432 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000433 action **app, *ap;
434
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000435 /* "action group" is a set of actions ANDed together.
436 * groups are ORed together.
437 * We simply evaluate each group until we find one in which all actions
438 * succeed. */
439
440 /* -prune is special: if it is encountered, then we won't
441 * descend into current directory. It doesn't matter whether
442 * action group (in which -prune sits) will succeed or not:
443 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
444 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
445 * not starting with 'f' */
446
447 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
448 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
449 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
450 * On return, bit is restored. */
451
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000452 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100453 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000454 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000455 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000456 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000457 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000458 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000459 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000460 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000461#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000462 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000463#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200464 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000465 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000466 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000467 }
468 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200469 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000470 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000471}
472
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200473#if !FNM_CASEFOLD
474static char *strcpy_upcase(char *dst, const char *src)
475{
476 char *d = dst;
477 while (1) {
478 unsigned char ch = *src++;
479 if (ch >= 'a' && ch <= 'z')
480 ch -= ('a' - 'A');
481 *d++ = ch;
482 if (ch == '\0')
483 break;
484 }
485 return dst;
486}
487#endif
488
Denis Vlasenko92258542006-11-01 10:25:35 +0000489ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000490{
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100491 int r;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000492 const char *tmp = bb_basename(fileName);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100493 /* GNU findutils: find DIR/ -name DIR
494 * prints "DIR/" (DIR// prints "DIR//" etc).
495 * Need to strip trailing "/".
496 * Such names can come only from top-level names, but
497 * we can't do this before recursive_action() call,
498 * since then "find FILE/ -name FILE"
499 * would also work (on non-directories), which is wrong.
500 */
501 char *trunc_slash = NULL;
502
503 if (*tmp == '\0') {
504 /* "foo/bar/[//...]" */
505 while (tmp != fileName && tmp[-1] == '/')
506 tmp--;
507 if (tmp == fileName) { /* entire fileName is "//.."? */
508 /* yes, convert "//..." to "/"
509 * Testcases:
510 * find / -maxdepth 1 -name /: prints /
511 * find // -maxdepth 1 -name /: prints //
512 * find / -maxdepth 1 -name //: prints nothing
513 * find // -maxdepth 1 -name //: prints nothing
514 */
515 if (tmp[1])
516 trunc_slash = (char*)tmp + 1;
517 } else {
518 /* no, it's "foo/bar/[//...]", go back to 'b' */
519 trunc_slash = (char*)tmp;
520 while (tmp != fileName && tmp[-1] != '/')
521 tmp--;
522 }
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000523 }
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100524
Denys Vlasenko91388172010-04-26 09:54:04 +0200525 /* Was using FNM_PERIOD flag too,
526 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
527 * find -name '*foo' should match .foo too:
528 */
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100529 if (trunc_slash) *trunc_slash = '\0';
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200530#if FNM_CASEFOLD
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100531 r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0));
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200532#else
533 if (ap->iname)
534 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100535 r = fnmatch(ap->pattern, tmp, 0);
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200536#endif
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100537 if (trunc_slash) *trunc_slash = '/';
538 return r == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000539}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000540
Denis Vlasenkob9411292007-06-15 23:43:11 +0000541#if ENABLE_FEATURE_FIND_PATH
542ACTF(path)
543{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200544# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200545 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200546# else
547 if (ap->ipath)
548 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
549 return fnmatch(ap->pattern, fileName, 0) == 0;
550# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000551}
552#endif
553#if ENABLE_FEATURE_FIND_REGEX
554ACTF(regex)
555{
556 regmatch_t match;
557 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
558 return 0; /* no match */
559 if (match.rm_so)
560 return 0; /* match doesn't start at pos 0 */
561 if (fileName[match.rm_eo])
562 return 0; /* match doesn't end exactly at end of pathname */
563 return 1;
564}
565#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000566#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000567ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000568{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000569 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000570}
Rob Landleycee605c2005-10-06 16:39:17 +0000571#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000572#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000573ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000574{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200575 /* -perm [+/]mode: at least one of perm_mask bits are set */
576 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000577 return (statbuf->st_mode & ap->perm_mask) != 0;
578 /* -perm -mode: all of perm_mask are set */
579 if (ap->perm_char == '-')
580 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
581 /* -perm mode: file mode must match perm_mask */
582 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000583}
Rob Landleycee605c2005-10-06 16:39:17 +0000584#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000585#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000586ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000587{
588 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000589 time_t mtime_secs = ap->mtime_days * 24*60*60;
590 if (ap->mtime_char == '+')
591 return file_age >= mtime_secs + 24*60*60;
592 if (ap->mtime_char == '-')
593 return file_age < mtime_secs;
594 /* just numeric mtime */
595 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000596}
Rob Landleycee605c2005-10-06 16:39:17 +0000597#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000598#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000599ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000600{
601 time_t file_age = time(NULL) - statbuf->st_mtime;
602 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000603 if (ap->mmin_char == '+')
604 return file_age >= mmin_secs + 60;
605 if (ap->mmin_char == '-')
606 return file_age < mmin_secs;
607 /* just numeric mmin */
608 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000609}
Paul Fox72d1a232006-01-13 21:05:41 +0000610#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000611#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000612ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000613{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000614 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000615}
Rob Landleycee605c2005-10-06 16:39:17 +0000616#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000617#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000618ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000619{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000620 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000621}
Rob Landleycee605c2005-10-06 16:39:17 +0000622#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000623#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200624static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000625{
626 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200627# if ENABLE_FEATURE_FIND_EXEC_PLUS
628 int size = ap->exec_argc + ap->filelist_idx + 1;
629# else
630 int size = ap->exec_argc + 1;
631# endif
632# if ENABLE_USE_PORTABLE_CODE
633 char **argv = alloca(sizeof(char*) * size);
634# else /* gcc 4.3.1 generates smaller code: */
635 char *argv[size];
636# endif
637 char **pp = argv;
638
639 for (i = 0; i < ap->exec_argc; i++) {
640 const char *arg = ap->exec_argv[i];
641
642# if ENABLE_FEATURE_FIND_EXEC_PLUS
643 if (ap->filelist) {
644 /* Handling "-exec +"
645 * Only one exec_argv[i] has substitution in it.
646 * Expand that one exec_argv[i] into file list.
647 */
648 if (ap->subst_count[i] == 0) {
649 *pp++ = xstrdup(arg);
650 } else {
651 int j = 0;
652 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200653 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200654 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
655 free(ap->filelist[j]);
656 j++;
657 }
658 }
659 } else
660# endif
661 {
662 /* Handling "-exec ;" */
663 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
664 }
665 }
666 *pp = NULL; /* terminate the list */
667
668# if ENABLE_FEATURE_FIND_EXEC_PLUS
669 if (ap->filelist) {
670 ap->filelist[0] = NULL;
671 ap->filelist_idx = 0;
672 ap->file_len = 0;
673 }
674# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000675
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000676 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000677 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000678 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000679
680 i = 0;
681 while (argv[i])
682 free(argv[i++]);
683 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000684}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200685ACTF(exec)
686{
687# if ENABLE_FEATURE_FIND_EXEC_PLUS
688 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200689 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200690
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200691 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
692 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200693 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
694 /* If we have lots of files already, exec the command */
695 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200696 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200697 rc = do_exec(ap, NULL);
698 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200699 }
700# endif
701 return do_exec(ap, fileName);
702}
703# if ENABLE_FEATURE_FIND_EXEC_PLUS
704static int flush_exec_plus(void)
705{
706 action *ap;
707 action **app;
708 action ***appp = G.actions;
709 while ((app = *appp++) != NULL) {
710 while ((ap = *app++) != NULL) {
711 if (ap->f == (action_fp)func_exec) {
712 action_exec *ae = (void*)ap;
713 if (ae->filelist_idx != 0) {
714 int rc = do_exec(ae, NULL);
715# if ENABLE_FEATURE_FIND_NOT
716 if (ap->invert) rc = !rc;
717# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200718 if (rc == 0)
719 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200720 }
721 }
722 }
723 }
724 return 0;
725}
726# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000727#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000728#if ENABLE_FEATURE_FIND_USER
729ACTF(user)
730{
731 return (statbuf->st_uid == ap->uid);
732}
733#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000734#if ENABLE_FEATURE_FIND_GROUP
735ACTF(group)
736{
737 return (statbuf->st_gid == ap->gid);
738}
739#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000740#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000741ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000742{
743 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000744 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000745}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000746#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000747ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000748{
749 puts(fileName);
750 return TRUE;
751}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000752#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000753ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000754{
755 return exec_actions(ap->subexpr, fileName, statbuf);
756}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000757#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000758#if ENABLE_FEATURE_FIND_SIZE
759ACTF(size)
760{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000761 if (ap->size_char == '+')
762 return statbuf->st_size > ap->size;
763 if (ap->size_char == '-')
764 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000765 return statbuf->st_size == ap->size;
766}
767#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000768#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000769/*
770 * -prune: if -depth is not given, return true and do not descend
771 * current dir; if -depth is given, return false with no effect.
772 * Example:
773 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
774 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000775ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000776{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000777 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000778}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000779#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000780#if ENABLE_FEATURE_FIND_DELETE
781ACTF(delete)
782{
783 int rc;
784 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8c0708a2015-10-07 21:01:47 +0200785 /* "find . -delete" skips rmdir(".") */
786 rc = 0;
787 if (NOT_LONE_CHAR(fileName, '.'))
788 rc = rmdir(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000789 } else {
790 rc = unlink(fileName);
791 }
792 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000793 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000794 return TRUE;
795}
796#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000797#if ENABLE_FEATURE_FIND_CONTEXT
798ACTF(context)
799{
800 security_context_t con;
801 int rc;
802
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200803 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000804 rc = getfilecon(fileName, &con);
805 } else {
806 rc = lgetfilecon(fileName, &con);
807 }
808 if (rc < 0)
809 return FALSE;
810 rc = strcmp(ap->context, con);
811 freecon(con);
812 return rc == 0;
813}
814#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100815#if ENABLE_FEATURE_FIND_LINKS
816ACTF(links)
817{
818 switch(ap->links_char) {
819 case '-' : return (statbuf->st_nlink < ap->links_count);
820 case '+' : return (statbuf->st_nlink > ap->links_count);
821 default: return (statbuf->st_nlink == ap->links_count);
822 }
823}
824#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000825
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000826static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000827 struct stat *statbuf,
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100828 void *userData UNUSED_PARAM,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000829 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000830{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100831 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200832 int same_fs = 1;
833
834#if ENABLE_FEATURE_FIND_XDEV
835 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
836 int i;
837 for (i = 0; i < G.xdev_count; i++) {
838 if (G.xdev_dev[i] == statbuf->st_dev)
839 goto found;
840 }
841 //bb_error_msg("'%s': not same fs", fileName);
842 same_fs = 0;
843 found: ;
844 }
845#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000846
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100847#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200848 if (depth < G.minmaxdepth[0]) {
849 if (same_fs)
850 return TRUE; /* skip this, continue recursing */
851 return SKIP; /* stop recursing */
852 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100853 if (depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100854 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000855#endif
856
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100857 r = exec_actions(G.actions, fileName, statbuf);
858 /* Had no explicit -print[0] or -exec? then print */
859 if ((r & TRUE) && G.need_print)
860 puts(fileName);
861
862#if ENABLE_FEATURE_FIND_MAXDEPTH
863 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100864 if (depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100865 return SKIP;
866 }
867#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100868 /* -xdev stops on mountpoints, but AFTER mountpoit itself
869 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200870 if (!same_fs) {
871 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000872 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200873
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000874 /* Cannot return 0: our caller, recursive_action(),
875 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100876 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000877}
878
Eric Andersen17d49ef1999-10-06 20:25:32 +0000879
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000880#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000881static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000882{
883 int mask = 0;
884
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000885 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000886 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000887 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000888 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000889 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000890 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000891 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000892 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000893 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000894 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000895 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000896 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000897 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000898 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000899
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200900 if (mask == 0 || type[1] != '\0')
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200901 bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000902
903 return mask;
904}
905#endif
906
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000907#if ENABLE_FEATURE_FIND_PERM \
908 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100909 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000910static const char* plus_minus_num(const char* str)
911{
912 if (*str == '-' || *str == '+')
913 str++;
914 return str;
915}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000916#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000917
Denys Vlasenko6db5f672013-05-16 18:36:42 +0200918/* Say no to GCCism */
919#define USE_NESTED_FUNCTION 0
920
921#if !USE_NESTED_FUNCTION
922struct pp_locals {
923 action*** appp;
924 unsigned cur_group;
925 unsigned cur_action;
926 IF_FEATURE_FIND_NOT( bool invert_flag; )
927};
928static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
929{
930 action *ap = xzalloc(sizeof_struct);
931 action **app;
932 action ***group = &ppl->appp[ppl->cur_group];
933 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
934 app[ppl->cur_action++] = ap;
935 app[ppl->cur_action] = NULL;
936 ap->f = f;
937 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
938 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
939 return ap;
940}
941#endif
942
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000943static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000944{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000945 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100946 OPT_FOLLOW ,
947 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
948 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200949 PARM_a ,
950 PARM_o ,
951 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000952#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200953 PARM_and ,
954 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000955 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000956#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200957 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000958 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000959 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
960 IF_FEATURE_FIND_DELETE( PARM_delete ,)
961 IF_FEATURE_FIND_EXEC( PARM_exec ,)
962 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100963 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200964 PARM_name ,
965 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000966 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200967#if ENABLE_DESKTOP
968 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +0200969 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200970 IF_FEATURE_FIND_PATH( PARM_wholename ,)
971#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200972 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000973 IF_FEATURE_FIND_REGEX( PARM_regex ,)
974 IF_FEATURE_FIND_TYPE( PARM_type ,)
975 IF_FEATURE_FIND_PERM( PARM_perm ,)
976 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
977 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
978 IF_FEATURE_FIND_NEWER( PARM_newer ,)
979 IF_FEATURE_FIND_INUM( PARM_inum ,)
980 IF_FEATURE_FIND_USER( PARM_user ,)
981 IF_FEATURE_FIND_GROUP( PARM_group ,)
982 IF_FEATURE_FIND_SIZE( PARM_size ,)
983 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100984 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100985 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000986 };
987
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000988 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100989 "-follow\0"
990 IF_FEATURE_FIND_XDEV( "-xdev\0" )
991 IF_FEATURE_FIND_DEPTH( "-depth\0" )
992 "-a\0"
993 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000994 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000995#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100996 "-and\0"
997 "-or\0"
998 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000999#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001000 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001001 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001002 IF_FEATURE_FIND_PRUNE( "-prune\0" )
1003 IF_FEATURE_FIND_DELETE( "-delete\0" )
1004 IF_FEATURE_FIND_EXEC( "-exec\0" )
1005 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001006 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +01001007 "-name\0"
1008 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001009 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001010#if ENABLE_DESKTOP
1011 IF_FEATURE_FIND_PATH( "-wholename\0")
1012#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001013 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001014 IF_FEATURE_FIND_REGEX( "-regex\0" )
1015 IF_FEATURE_FIND_TYPE( "-type\0" )
1016 IF_FEATURE_FIND_PERM( "-perm\0" )
1017 IF_FEATURE_FIND_MTIME( "-mtime\0" )
1018 IF_FEATURE_FIND_MMIN( "-mmin\0" )
1019 IF_FEATURE_FIND_NEWER( "-newer\0" )
1020 IF_FEATURE_FIND_INUM( "-inum\0" )
1021 IF_FEATURE_FIND_USER( "-user\0" )
1022 IF_FEATURE_FIND_GROUP( "-group\0" )
1023 IF_FEATURE_FIND_SIZE( "-size\0" )
1024 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001025 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001026 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001027 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001028
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001029#if !USE_NESTED_FUNCTION
1030 struct pp_locals ppl;
1031#define appp (ppl.appp )
1032#define cur_group (ppl.cur_group )
1033#define cur_action (ppl.cur_action )
1034#define invert_flag (ppl.invert_flag)
1035#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1036#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001037 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001038 unsigned cur_group;
1039 unsigned cur_action;
1040 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001041
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001042 /* This is the only place in busybox where we use nested function.
1043 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001044 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001045 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001046 action* alloc_action(int sizeof_struct, action_fp f)
1047 {
1048 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001049 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001050 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001051 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001052 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001053 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1054 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001055 return ap;
1056 }
1057#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001058#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001059
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001060 cur_group = 0;
1061 cur_action = 0;
1062 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001063 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001064
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001065 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001066 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001067 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001068 const char *arg1 = argv[1];
1069
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001070 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1071
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001072 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001073 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001074 if (!arg1)
1075 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1076 argv++;
1077 }
1078
1079 /* We can use big switch() here, but on i386
1080 * it doesn't give smaller code. Other arches? */
1081
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001082/* Options always return true. They always take effect
1083 * rather than being processed only when their place in the
1084 * expression is reached.
1085 */
1086 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001087 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001088 dbg("follow enabled: %d", __LINE__);
1089 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001090 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001091#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001092 else if (parm == OPT_XDEV) {
1093 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001094 G.xdev_on = 1;
1095 }
1096#endif
1097#if ENABLE_FEATURE_FIND_MAXDEPTH
1098 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001099 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001100 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1101 }
1102#endif
1103#if ENABLE_FEATURE_FIND_DEPTH
1104 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001105 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001106 G.recurse_flags |= ACTION_DEPTHFIRST;
1107 }
1108#endif
1109/* Actions are grouped by operators
1110 * ( expr ) Force precedence
1111 * ! expr True if expr is false
1112 * -not expr Same as ! expr
1113 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1114 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1115 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1116 * We implement: (), -a, -o
1117 */
1118 /* Operators */
1119 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001120 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001121 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001122 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001123 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001124 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001125 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001126 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001127 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001128 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001129 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001130 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001131 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001132#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001133 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001134 /* also handles "find ! ! -name 'foo*'" */
1135 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001136 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001137 }
1138#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001139 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001140 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001141 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001142 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001143 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001144 }
1145#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001146 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001147 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001148 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001149 (void) ALLOC_ACTION(print0);
1150 }
Paul Foxd7384292006-05-12 14:47:20 +00001151#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001152#if ENABLE_FEATURE_FIND_PRUNE
1153 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001154 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001155 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001156 }
1157#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001158#if ENABLE_FEATURE_FIND_DELETE
1159 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001160 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001161 G.need_print = 0;
1162 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001163 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001164 }
Rob Landleycee605c2005-10-06 16:39:17 +00001165#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001166#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001167 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001168 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001169 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001170 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001171 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001172 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001173 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001174 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001175 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001176 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001177 if (!*argv) /* did not see ';' or '+' until end */
1178 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001179 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001180 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001181 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001182 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001183 if ((argv[0][0] == ';' || argv[0][0] == '+')
1184 && argv[0][1] == '\0'
1185 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001186# if ENABLE_FEATURE_FIND_EXEC_PLUS
1187 if (argv[0][0] == '+')
1188 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1189# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001190 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001191 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001192 argv++;
1193 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001194 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001195 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001196 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001197 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001198 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001199 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001200 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001201 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1202 }
1203# if ENABLE_FEATURE_FIND_EXEC_PLUS
1204 /*
1205 * coreutils expects {} to appear only once in "-exec +"
1206 */
1207 if (all_subst != 1 && ap->filelist)
1208 bb_error_msg_and_die("only one '{}' allowed for -exec +");
1209# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001210 }
1211#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001212#if ENABLE_FEATURE_FIND_PAREN
1213 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001214 action_paren *ap;
1215 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001216 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001217
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001218 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001219 endarg = argv;
1220 while (1) {
1221 if (!*++endarg)
1222 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001223 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001224 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001225 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001226 *endarg = NULL;
1227 break;
1228 }
1229 }
1230 ap = ALLOC_ACTION(paren);
1231 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001232 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001233 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001234 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001235#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001236 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001237 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001238 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001239 ap = ALLOC_ACTION(name);
1240 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001241 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001242 }
1243#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001244 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001245 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001246 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001247 ap = ALLOC_ACTION(path);
1248 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001249 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001250 }
1251#endif
1252#if ENABLE_FEATURE_FIND_REGEX
1253 else if (parm == PARM_regex) {
1254 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001255 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001256 ap = ALLOC_ACTION(regex);
1257 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1258 }
1259#endif
1260#if ENABLE_FEATURE_FIND_TYPE
1261 else if (parm == PARM_type) {
1262 action_type *ap;
1263 ap = ALLOC_ACTION(type);
1264 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001265 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001266 }
1267#endif
1268#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001269/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001270 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001271 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001272 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001273 */
1274 else if (parm == PARM_perm) {
1275 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001276 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001277 ap = ALLOC_ACTION(perm);
1278 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001279 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001280 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +02001281 ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask);
1282 if (ap->perm_mask == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +01001283 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001284 }
1285#endif
1286#if ENABLE_FEATURE_FIND_MTIME
1287 else if (parm == PARM_mtime) {
1288 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001289 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001290 ap = ALLOC_ACTION(mtime);
1291 ap->mtime_char = arg1[0];
1292 ap->mtime_days = xatoul(plus_minus_num(arg1));
1293 }
1294#endif
1295#if ENABLE_FEATURE_FIND_MMIN
1296 else if (parm == PARM_mmin) {
1297 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001298 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001299 ap = ALLOC_ACTION(mmin);
1300 ap->mmin_char = arg1[0];
1301 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1302 }
1303#endif
1304#if ENABLE_FEATURE_FIND_NEWER
1305 else if (parm == PARM_newer) {
1306 struct stat stat_newer;
1307 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001308 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001309 ap = ALLOC_ACTION(newer);
1310 xstat(arg1, &stat_newer);
1311 ap->newer_mtime = stat_newer.st_mtime;
1312 }
1313#endif
1314#if ENABLE_FEATURE_FIND_INUM
1315 else if (parm == PARM_inum) {
1316 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001317 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001318 ap = ALLOC_ACTION(inum);
1319 ap->inode_num = xatoul(arg1);
1320 }
1321#endif
1322#if ENABLE_FEATURE_FIND_USER
1323 else if (parm == PARM_user) {
1324 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001325 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001326 ap = ALLOC_ACTION(user);
1327 ap->uid = bb_strtou(arg1, NULL, 10);
1328 if (errno)
1329 ap->uid = xuname2uid(arg1);
1330 }
1331#endif
1332#if ENABLE_FEATURE_FIND_GROUP
1333 else if (parm == PARM_group) {
1334 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001335 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001336 ap = ALLOC_ACTION(group);
1337 ap->gid = bb_strtou(arg1, NULL, 10);
1338 if (errno)
1339 ap->gid = xgroup2gid(arg1);
1340 }
1341#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001342#if ENABLE_FEATURE_FIND_SIZE
1343 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001344/* -size n[bckw]: file uses n units of space
1345 * b (default): units are 512-byte blocks
1346 * c: 1 byte
1347 * k: kilobytes
1348 * w: 2-byte words
1349 */
1350#if ENABLE_LFS
1351#define XATOU_SFX xatoull_sfx
1352#else
1353#define XATOU_SFX xatoul_sfx
1354#endif
1355 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001356 { "c", 1 },
1357 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001358 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001359 { "b", 512 },
1360 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001361 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001362 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001363 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001364 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001365 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001366 ap->size_char = arg1[0];
1367 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001368 }
1369#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001370#if ENABLE_FEATURE_FIND_CONTEXT
1371 else if (parm == PARM_context) {
1372 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001373 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001374 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001375 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001376 /* SELinux headers erroneously declare non-const parameter */
1377 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001378 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001379 }
1380#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001381#if ENABLE_FEATURE_FIND_LINKS
1382 else if (parm == PARM_links) {
1383 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001384 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001385 ap = ALLOC_ACTION(links);
1386 ap->links_char = arg1[0];
1387 ap->links_count = xatoul(plus_minus_num(arg1));
1388 }
1389#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001390 else {
1391 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001392 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001393 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001394 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001395 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001396 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001397 return appp;
1398#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001399#undef appp
1400#undef cur_action
1401#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001402}
1403
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001404int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001405int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001406{
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001407 int i, firstopt, status = EXIT_SUCCESS;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001408 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001409
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001410 INIT_G();
1411
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001412 /* "find -type f" + getopt("+HLP") => disaster.
1413 * Need to avoid getopt running into a non-HLP option.
1414 * Do this by temporarily storing NULL there:
1415 */
1416 past_HLP = argv;
1417 for (;;) {
1418 saved = *++past_HLP;
1419 if (!saved)
1420 break;
1421 if (saved[0] != '-')
1422 break;
1423 if (!saved[1])
1424 break; /* it is "-" */
1425 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1426 break;
1427 }
1428 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001429 /* "+": stop on first non-option */
1430 i = getopt32(argv, "+HLP");
1431 if (i & (1<<0))
1432 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1433 if (i & (1<<1))
1434 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1435 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001436 argv = past_HLP; /* same result as "argv += optind;" */
1437 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001438
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001439 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001440 if (argv[firstopt][0] == '-')
1441 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001442 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1443 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001444 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001445 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001446 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001447 if (firstopt == 0) {
1448 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001449 firstopt++;
1450 }
1451
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001452 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001453 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001454
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001455#if ENABLE_FEATURE_FIND_XDEV
1456 if (G.xdev_on) {
1457 struct stat stbuf;
1458
1459 G.xdev_count = firstopt;
1460 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1461 for (i = 0; argv[i]; i++) {
1462 /* not xstat(): shouldn't bomb out on
1463 * "find not_exist exist -xdev" */
1464 if (stat(argv[i], &stbuf) == 0)
1465 G.xdev_dev[i] = stbuf.st_dev;
1466 /* else G.xdev_dev[i] stays 0 and
1467 * won't match any real device dev_t
1468 */
1469 }
1470 }
1471#endif
1472
1473 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001474 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001475 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001476 fileAction, /* file action */
1477 fileAction, /* dir action */
1478 NULL, /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001479 0) /* depth */
1480 ) {
Felix Fietkauf7466e42015-04-09 10:20:16 +02001481 status |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001482 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001483 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001484
Felix Fietkauf7466e42015-04-09 10:20:16 +02001485 IF_FEATURE_FIND_EXEC_PLUS(status |= flush_exec_plus();)
Matt Kraai096370d2001-02-07 03:52:38 +00001486 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001487}