blob: d9a42b3c654b84e87d43f6f84db21a3c71d1d8f2 [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 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000012/* findutils-4.1.20:
13 *
14 * # find file.txt -exec 'echo {}' '{} {}' ';'
15 * find: echo file.txt: No such file or directory
16 * # find file.txt -exec 'echo' '{} {}' '; '
Denys Vlasenko95f79532017-08-02 14:26:33 +020017 * find: missing argument to '-exec'
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000018 * # find file.txt -exec 'echo {}' '{} {}' ';' junk
19 * find: paths must precede expression
20 * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';'
21 * find: paths must precede expression
22 * # find file.txt -exec 'echo' '{} {}' ';'
23 * file.txt file.txt
24 * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ]))
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000025 * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
26 * file.txt file.txt
27 * file.txt
28 * /tmp
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +000029 * # find -name '*.c' -o -name '*.h'
30 * [shows files, *.c and *.h intermixed]
Denis Vlasenko5d499e12006-10-29 19:07:01 +000031 * # find file.txt -name '*f*' -o -name '*t*'
32 * file.txt
33 * # find file.txt -name '*z*' -o -name '*t*'
34 * file.txt
35 * # find file.txt -name '*f*' -o -name '*z*'
36 * file.txt
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +000037 *
38 * # find t z -name '*t*' -print -o -name '*z*'
39 * t
40 * # find t z t z -name '*t*' -o -name '*z*' -print
41 * z
42 * z
43 * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
44 * (no output)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000045 */
Denis Vlasenko53a0e972007-06-16 00:30:52 +000046/* Testing script
47 * ./busybox find "$@" | tee /tmp/bb_find
48 * echo ==================
49 * /path/to/gnu/find "$@" | tee /tmp/std_find
50 * echo ==================
51 * diff -u /tmp/std_find /tmp/bb_find && echo Identical
52 */
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020053//config:config FIND
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020054//config: bool "find (14 kb)"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +020055//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020056//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020057//config: find is used to search your system to find specified files.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020058//config:
59//config:config FEATURE_FIND_PRINT0
60//config: bool "Enable -print0: NUL-terminated output"
61//config: default y
62//config: depends on FIND
63//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020064//config: Causes output names to be separated by a NUL character
65//config: rather than a newline. This allows names that contain
66//config: newlines and other whitespace to be more easily
67//config: interpreted by other programs.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020068//config:
69//config:config FEATURE_FIND_MTIME
70//config: bool "Enable -mtime: modified time matching"
71//config: default y
72//config: depends on FIND
73//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020074//config: Allow searching based on the modification time of
75//config: files, in days.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020076//config:
77//config:config FEATURE_FIND_MMIN
78//config: bool "Enable -mmin: modified time matching by minutes"
79//config: default y
80//config: depends on FIND
81//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020082//config: Allow searching based on the modification time of
83//config: files, in minutes.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020084//config:
85//config:config FEATURE_FIND_PERM
86//config: bool "Enable -perm: permissions matching"
87//config: default y
88//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020089//config:
90//config:config FEATURE_FIND_TYPE
91//config: bool "Enable -type: file type matching (file/dir/link/...)"
92//config: default y
93//config: depends on FIND
94//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020095//config: Enable searching based on file type (file,
96//config: directory, socket, device, etc.).
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020097//config:
98//config:config FEATURE_FIND_XDEV
99//config: bool "Enable -xdev: 'stay in filesystem'"
100//config: default y
101//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200102//config:
103//config:config FEATURE_FIND_MAXDEPTH
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100104//config: bool "Enable -mindepth N and -maxdepth N"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200105//config: default y
106//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200107//config:
108//config:config FEATURE_FIND_NEWER
109//config: bool "Enable -newer: compare file modification times"
110//config: default y
111//config: depends on FIND
112//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200113//config: Support the 'find -newer' option for finding any files which have
114//config: modification time that is more recent than the specified FILE.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200115//config:
116//config:config FEATURE_FIND_INUM
117//config: bool "Enable -inum: inode number matching"
118//config: default y
119//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200120//config:
121//config:config FEATURE_FIND_EXEC
122//config: bool "Enable -exec: execute commands"
123//config: default y
124//config: depends on FIND
125//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200126//config: Support the 'find -exec' option for executing commands based upon
127//config: the files matched.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200128//config:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200129//config:config FEATURE_FIND_EXEC_PLUS
130//config: bool "Enable -exec ... {} +"
131//config: default y
132//config: depends on FEATURE_FIND_EXEC
133//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200134//config: Support the 'find -exec ... {} +' option for executing commands
135//config: for all matched files at once.
136//config: Without this option, -exec + is a synonym for -exec ;
137//config: (IOW: it works correctly, but without expected speedup)
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200138//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200139//config:config FEATURE_FIND_USER
140//config: bool "Enable -user: username/uid matching"
141//config: default y
142//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200143//config:
144//config:config FEATURE_FIND_GROUP
145//config: bool "Enable -group: group/gid matching"
146//config: default y
147//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200148//config:
149//config:config FEATURE_FIND_NOT
150//config: bool "Enable the 'not' (!) operator"
151//config: default y
152//config: depends on FIND
153//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200154//config: Support the '!' operator to invert the test results.
155//config: If 'Enable full-blown desktop' is enabled, then will also support
156//config: the non-POSIX notation '-not'.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200157//config:
158//config:config FEATURE_FIND_DEPTH
159//config: bool "Enable -depth"
160//config: default y
161//config: depends on FIND
162//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200163//config: Process each directory's contents before the directory itself.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200164//config:
165//config:config FEATURE_FIND_PAREN
166//config: bool "Enable parens in options"
167//config: default y
168//config: depends on FIND
169//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200170//config: Enable usage of parens '(' to specify logical order of arguments.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200171//config:
172//config:config FEATURE_FIND_SIZE
173//config: bool "Enable -size: file size matching"
174//config: default y
175//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200176//config:
177//config:config FEATURE_FIND_PRUNE
178//config: bool "Enable -prune: exclude subdirectories"
179//config: default y
180//config: depends on FIND
181//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200182//config: If the file is a directory, don't descend into it. Useful for
183//config: exclusion .svn and CVS directories.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200184//config:
185//config:config FEATURE_FIND_DELETE
186//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200187//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200188//config: depends on FIND && FEATURE_FIND_DEPTH
189//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200190//config: Support the 'find -delete' option for deleting files and directories.
191//config: WARNING: This option can do much harm if used wrong. Busybox will not
192//config: try to protect the user from doing stupid things. Use with care.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200193//config:
194//config:config FEATURE_FIND_PATH
195//config: bool "Enable -path: match pathname with shell pattern"
196//config: default y
197//config: depends on FIND
198//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200199//config: The -path option matches whole pathname instead of just filename.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200200//config:
201//config:config FEATURE_FIND_REGEX
202//config: bool "Enable -regex: match pathname with regex"
203//config: default y
204//config: depends on FIND
205//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200206//config: The -regex option matches whole pathname against regular expression.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200207//config:
208//config:config FEATURE_FIND_CONTEXT
209//config: bool "Enable -context: security context matching"
210//config: default n
211//config: depends on FIND && SELINUX
212//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200213//config: Support the 'find -context' option for matching security context.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200214//config:
215//config:config FEATURE_FIND_LINKS
216//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200217//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200218//config: depends on FIND
219//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200220//config: Support the 'find -links' option for matching number of links.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200221
Denys Vlasenko805f2452011-02-12 20:26:14 +0100222//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
223
224//kbuild:lib-$(CONFIG_FIND) += find.o
225
226//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200227//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100228//usage:#define find_full_usage "\n\n"
229//usage: "Search for files and perform actions on them.\n"
230//usage: "First failed action stops processing of current file.\n"
231//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200232//usage: "\n -L,-follow Follow symlinks"
233//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100234//usage: IF_FEATURE_FIND_XDEV(
235//usage: "\n -xdev Don't descend directories on other filesystems"
236//usage: )
237//usage: IF_FEATURE_FIND_MAXDEPTH(
238//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
239//usage: "\n actions to command line arguments only"
240//usage: "\n -mindepth N Don't act on first N levels"
241//usage: )
242//usage: IF_FEATURE_FIND_DEPTH(
243//usage: "\n -depth Act on directory *after* traversing it"
244//usage: )
245//usage: "\n"
246//usage: "\nActions:"
247//usage: IF_FEATURE_FIND_PAREN(
248//usage: "\n ( ACTIONS ) Group actions for -o / -a"
249//usage: )
250//usage: IF_FEATURE_FIND_NOT(
251//usage: "\n ! ACT Invert ACT's success/failure"
252//usage: )
253//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
254//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
255//usage: "\n Note: -a has higher priority than -o"
256//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
257//usage: "\n -iname PATTERN Case insensitive -name"
258//usage: IF_FEATURE_FIND_PATH(
259//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200260//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100261//usage: )
262//usage: IF_FEATURE_FIND_REGEX(
263//usage: "\n -regex PATTERN Match path to regex PATTERN"
264//usage: )
265//usage: IF_FEATURE_FIND_TYPE(
266//usage: "\n -type X File type is X (one of: f,d,l,b,c,...)"
267//usage: )
268//usage: IF_FEATURE_FIND_PERM(
269//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100270//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100271//usage: )
272//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100273//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
274//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100275//usage: )
276//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100277//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
278//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100279//usage: )
280//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100281//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100282//usage: )
283//usage: IF_FEATURE_FIND_INUM(
284//usage: "\n -inum N File has inode number N"
285//usage: )
286//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100287//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100288//usage: )
289//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100290//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100291//usage: )
292//usage: IF_FEATURE_FIND_SIZE(
293//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
294//usage: "\n +/-N: file size is bigger/smaller than N"
295//usage: )
296//usage: IF_FEATURE_FIND_LINKS(
297//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
298//usage: "\n or exactly N"
299//usage: )
300//usage: IF_FEATURE_FIND_CONTEXT(
301//usage: "\n -context CTX File has specified security context"
302//usage: )
303//usage: IF_FEATURE_FIND_PRUNE(
304//usage: "\n -prune If current file is directory, don't descend into it"
305//usage: )
306//usage: "\nIf none of the following actions is specified, -print is assumed"
307//usage: "\n -print Print file name"
308//usage: IF_FEATURE_FIND_PRINT0(
309//usage: "\n -print0 Print file name, NUL terminated"
310//usage: )
311//usage: IF_FEATURE_FIND_EXEC(
312//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
313//usage: "\n file name. Fails if CMD exits with nonzero"
314//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200315//usage: IF_FEATURE_FIND_EXEC_PLUS(
316//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
317//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100318//usage: IF_FEATURE_FIND_DELETE(
319//usage: "\n -delete Delete current file/directory. Turns on -depth option"
320//usage: )
321//usage:
322//usage:#define find_example_usage
323//usage: "$ find / -name passwd\n"
324//usage: "/etc/passwd\n"
325
Matt Kraai096370d2001-02-07 03:52:38 +0000326#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000327#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200328#include "common_bufsiz.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000329#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200330# include "xregex.h"
331#endif
332/* GNUism: */
333#ifndef FNM_CASEFOLD
334# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000335#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000336
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200337#if 1
338# define dbg(...) ((void)0)
339#else
340# define dbg(...) bb_error_msg(__VA_ARGS__)
341#endif
342
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200343
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000344/* This is a NOEXEC applet. Be very careful! */
345
346
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200347typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000348
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000349typedef struct {
350 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000351#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000352 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000353#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000354} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200355
Denys Vlasenko606291b2009-09-23 23:15:43 +0200356#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200357#define ACTF(name) \
358 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200359 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200360 action_##name* ap UNUSED_PARAM)
361
362 ACTS(print)
363 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200364IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000365IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
366IF_FEATURE_FIND_PRINT0( ACTS(print0))
367IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
368IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
369IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
370IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
371IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
372IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
373IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
374IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
375IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
376IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
377IF_FEATURE_FIND_PRUNE( ACTS(prune))
378IF_FEATURE_FIND_DELETE( ACTS(delete))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200379IF_FEATURE_FIND_EXEC( ACTS(exec,
380 char **exec_argv; /* -exec ARGS */
381 unsigned *subst_count;
382 int exec_argc; /* count of ARGS */
383 IF_FEATURE_FIND_EXEC_PLUS(
384 /*
385 * filelist is NULL if "exec ;"
386 * non-NULL if "exec +"
387 */
388 char **filelist;
389 int filelist_idx;
390 int file_len;
391 )
392 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000393IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100394IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000395
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200396struct globals {
397 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
398 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100399#if ENABLE_FEATURE_FIND_MAXDEPTH
400 int minmaxdepth[2];
401#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200402 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100403 smallint need_print;
404 smallint xdev_on;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200405 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200406 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100407} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200408#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200409#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200410 setup_common_bufsiz(); \
Denys Vlasenko7b85ec32015-10-13 17:17:34 +0200411 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200412 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100413 memset(&G, 0, sizeof(G)); \
414 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200415 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200416 G.need_print = 1; \
417 G.recurse_flags = ACTION_RECURSE; \
418} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000419
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000420/* Return values of ACTFs ('action functions') are a bit mask:
421 * bit 1=1: prune (use SKIP constant for setting it)
422 * bit 0=1: matched successfully (TRUE)
423 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000424
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200425static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000426{
427 int cur_group;
428 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000429 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000430 action **app, *ap;
431
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000432 /* "action group" is a set of actions ANDed together.
433 * groups are ORed together.
434 * We simply evaluate each group until we find one in which all actions
435 * succeed. */
436
437 /* -prune is special: if it is encountered, then we won't
438 * descend into current directory. It doesn't matter whether
439 * action group (in which -prune sits) will succeed or not:
440 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
441 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
442 * not starting with 'f' */
443
444 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
445 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
446 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
447 * On return, bit is restored. */
448
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000449 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100450 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000451 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000452 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000453 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000454 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000455 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000456 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000457 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000458#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000459 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000460#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200461 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000462 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000463 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000464 }
465 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200466 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000467 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000468}
469
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200470#if !FNM_CASEFOLD
471static char *strcpy_upcase(char *dst, const char *src)
472{
473 char *d = dst;
474 while (1) {
475 unsigned char ch = *src++;
476 if (ch >= 'a' && ch <= 'z')
477 ch -= ('a' - 'A');
478 *d++ = ch;
479 if (ch == '\0')
480 break;
481 }
482 return dst;
483}
484#endif
485
Denis Vlasenko92258542006-11-01 10:25:35 +0000486ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000487{
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100488 int r;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000489 const char *tmp = bb_basename(fileName);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100490 /* GNU findutils: find DIR/ -name DIR
491 * prints "DIR/" (DIR// prints "DIR//" etc).
492 * Need to strip trailing "/".
493 * Such names can come only from top-level names, but
494 * we can't do this before recursive_action() call,
495 * since then "find FILE/ -name FILE"
496 * would also work (on non-directories), which is wrong.
497 */
498 char *trunc_slash = NULL;
499
500 if (*tmp == '\0') {
501 /* "foo/bar/[//...]" */
502 while (tmp != fileName && tmp[-1] == '/')
503 tmp--;
504 if (tmp == fileName) { /* entire fileName is "//.."? */
505 /* yes, convert "//..." to "/"
506 * Testcases:
507 * find / -maxdepth 1 -name /: prints /
508 * find // -maxdepth 1 -name /: prints //
509 * find / -maxdepth 1 -name //: prints nothing
510 * find // -maxdepth 1 -name //: prints nothing
511 */
512 if (tmp[1])
513 trunc_slash = (char*)tmp + 1;
514 } else {
515 /* no, it's "foo/bar/[//...]", go back to 'b' */
516 trunc_slash = (char*)tmp;
517 while (tmp != fileName && tmp[-1] != '/')
518 tmp--;
519 }
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000520 }
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100521
Denys Vlasenko91388172010-04-26 09:54:04 +0200522 /* Was using FNM_PERIOD flag too,
523 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
524 * find -name '*foo' should match .foo too:
525 */
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100526 if (trunc_slash) *trunc_slash = '\0';
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200527#if FNM_CASEFOLD
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100528 r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0));
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200529#else
530 if (ap->iname)
531 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100532 r = fnmatch(ap->pattern, tmp, 0);
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200533#endif
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100534 if (trunc_slash) *trunc_slash = '/';
535 return r == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000536}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000537
Denis Vlasenkob9411292007-06-15 23:43:11 +0000538#if ENABLE_FEATURE_FIND_PATH
539ACTF(path)
540{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200541# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200542 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200543# else
544 if (ap->ipath)
545 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
546 return fnmatch(ap->pattern, fileName, 0) == 0;
547# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000548}
549#endif
550#if ENABLE_FEATURE_FIND_REGEX
551ACTF(regex)
552{
553 regmatch_t match;
554 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
555 return 0; /* no match */
556 if (match.rm_so)
557 return 0; /* match doesn't start at pos 0 */
558 if (fileName[match.rm_eo])
559 return 0; /* match doesn't end exactly at end of pathname */
560 return 1;
561}
562#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000563#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000564ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000565{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000566 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000567}
Rob Landleycee605c2005-10-06 16:39:17 +0000568#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000569#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000570ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000571{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200572 /* -perm [+/]mode: at least one of perm_mask bits are set */
573 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000574 return (statbuf->st_mode & ap->perm_mask) != 0;
575 /* -perm -mode: all of perm_mask are set */
576 if (ap->perm_char == '-')
577 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
578 /* -perm mode: file mode must match perm_mask */
579 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000580}
Rob Landleycee605c2005-10-06 16:39:17 +0000581#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000582#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000583ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000584{
585 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000586 time_t mtime_secs = ap->mtime_days * 24*60*60;
587 if (ap->mtime_char == '+')
588 return file_age >= mtime_secs + 24*60*60;
589 if (ap->mtime_char == '-')
590 return file_age < mtime_secs;
591 /* just numeric mtime */
592 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000593}
Rob Landleycee605c2005-10-06 16:39:17 +0000594#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000595#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000596ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000597{
598 time_t file_age = time(NULL) - statbuf->st_mtime;
599 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000600 if (ap->mmin_char == '+')
601 return file_age >= mmin_secs + 60;
602 if (ap->mmin_char == '-')
603 return file_age < mmin_secs;
604 /* just numeric mmin */
605 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000606}
Paul Fox72d1a232006-01-13 21:05:41 +0000607#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000608#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000609ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000610{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000611 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000612}
Rob Landleycee605c2005-10-06 16:39:17 +0000613#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000614#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000615ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000616{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000617 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000618}
Rob Landleycee605c2005-10-06 16:39:17 +0000619#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000620#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200621static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000622{
623 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200624# if ENABLE_FEATURE_FIND_EXEC_PLUS
625 int size = ap->exec_argc + ap->filelist_idx + 1;
626# else
627 int size = ap->exec_argc + 1;
628# endif
629# if ENABLE_USE_PORTABLE_CODE
630 char **argv = alloca(sizeof(char*) * size);
631# else /* gcc 4.3.1 generates smaller code: */
632 char *argv[size];
633# endif
634 char **pp = argv;
635
636 for (i = 0; i < ap->exec_argc; i++) {
637 const char *arg = ap->exec_argv[i];
638
639# if ENABLE_FEATURE_FIND_EXEC_PLUS
640 if (ap->filelist) {
641 /* Handling "-exec +"
642 * Only one exec_argv[i] has substitution in it.
643 * Expand that one exec_argv[i] into file list.
644 */
645 if (ap->subst_count[i] == 0) {
646 *pp++ = xstrdup(arg);
647 } else {
648 int j = 0;
649 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200650 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200651 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
652 free(ap->filelist[j]);
653 j++;
654 }
655 }
656 } else
657# endif
658 {
659 /* Handling "-exec ;" */
660 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
661 }
662 }
663 *pp = NULL; /* terminate the list */
664
665# if ENABLE_FEATURE_FIND_EXEC_PLUS
666 if (ap->filelist) {
667 ap->filelist[0] = NULL;
668 ap->filelist_idx = 0;
669 ap->file_len = 0;
670 }
671# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000672
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000673 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000674 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000675 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000676
677 i = 0;
678 while (argv[i])
679 free(argv[i++]);
680 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000681}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200682ACTF(exec)
683{
684# if ENABLE_FEATURE_FIND_EXEC_PLUS
685 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200686 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200687
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200688 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
689 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200690 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
691 /* If we have lots of files already, exec the command */
692 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200693 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200694 rc = do_exec(ap, NULL);
695 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200696 }
697# endif
698 return do_exec(ap, fileName);
699}
700# if ENABLE_FEATURE_FIND_EXEC_PLUS
701static int flush_exec_plus(void)
702{
703 action *ap;
704 action **app;
705 action ***appp = G.actions;
706 while ((app = *appp++) != NULL) {
707 while ((ap = *app++) != NULL) {
708 if (ap->f == (action_fp)func_exec) {
709 action_exec *ae = (void*)ap;
710 if (ae->filelist_idx != 0) {
711 int rc = do_exec(ae, NULL);
712# if ENABLE_FEATURE_FIND_NOT
713 if (ap->invert) rc = !rc;
714# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200715 if (rc == 0)
716 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200717 }
718 }
719 }
720 }
721 return 0;
722}
723# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000724#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000725#if ENABLE_FEATURE_FIND_USER
726ACTF(user)
727{
728 return (statbuf->st_uid == ap->uid);
729}
730#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000731#if ENABLE_FEATURE_FIND_GROUP
732ACTF(group)
733{
734 return (statbuf->st_gid == ap->gid);
735}
736#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000737#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000738ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000739{
740 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000741 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000742}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000743#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000744ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000745{
746 puts(fileName);
747 return TRUE;
748}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000749#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000750ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000751{
752 return exec_actions(ap->subexpr, fileName, statbuf);
753}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000754#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000755#if ENABLE_FEATURE_FIND_SIZE
756ACTF(size)
757{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000758 if (ap->size_char == '+')
759 return statbuf->st_size > ap->size;
760 if (ap->size_char == '-')
761 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000762 return statbuf->st_size == ap->size;
763}
764#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000765#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000766/*
767 * -prune: if -depth is not given, return true and do not descend
768 * current dir; if -depth is given, return false with no effect.
769 * Example:
770 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
771 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000772ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000773{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000774 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000775}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000776#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000777#if ENABLE_FEATURE_FIND_DELETE
778ACTF(delete)
779{
780 int rc;
781 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8c0708a2015-10-07 21:01:47 +0200782 /* "find . -delete" skips rmdir(".") */
783 rc = 0;
784 if (NOT_LONE_CHAR(fileName, '.'))
785 rc = rmdir(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000786 } else {
787 rc = unlink(fileName);
788 }
789 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000790 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000791 return TRUE;
792}
793#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000794#if ENABLE_FEATURE_FIND_CONTEXT
795ACTF(context)
796{
797 security_context_t con;
798 int rc;
799
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200800 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000801 rc = getfilecon(fileName, &con);
802 } else {
803 rc = lgetfilecon(fileName, &con);
804 }
805 if (rc < 0)
806 return FALSE;
807 rc = strcmp(ap->context, con);
808 freecon(con);
809 return rc == 0;
810}
811#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100812#if ENABLE_FEATURE_FIND_LINKS
813ACTF(links)
814{
815 switch(ap->links_char) {
816 case '-' : return (statbuf->st_nlink < ap->links_count);
817 case '+' : return (statbuf->st_nlink > ap->links_count);
818 default: return (statbuf->st_nlink == ap->links_count);
819 }
820}
821#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000822
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000823static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000824 struct stat *statbuf,
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100825 void *userData UNUSED_PARAM,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000826 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000827{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100828 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200829 int same_fs = 1;
830
831#if ENABLE_FEATURE_FIND_XDEV
832 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
833 int i;
834 for (i = 0; i < G.xdev_count; i++) {
835 if (G.xdev_dev[i] == statbuf->st_dev)
836 goto found;
837 }
838 //bb_error_msg("'%s': not same fs", fileName);
839 same_fs = 0;
840 found: ;
841 }
842#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000843
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100844#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200845 if (depth < G.minmaxdepth[0]) {
846 if (same_fs)
847 return TRUE; /* skip this, continue recursing */
848 return SKIP; /* stop recursing */
849 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100850 if (depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100851 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000852#endif
853
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100854 r = exec_actions(G.actions, fileName, statbuf);
855 /* Had no explicit -print[0] or -exec? then print */
856 if ((r & TRUE) && G.need_print)
857 puts(fileName);
858
859#if ENABLE_FEATURE_FIND_MAXDEPTH
860 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100861 if (depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100862 return SKIP;
863 }
864#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100865 /* -xdev stops on mountpoints, but AFTER mountpoit itself
866 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200867 if (!same_fs) {
868 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000869 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200870
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000871 /* Cannot return 0: our caller, recursive_action(),
872 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100873 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000874}
875
Eric Andersen17d49ef1999-10-06 20:25:32 +0000876
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000877#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000878static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000879{
880 int mask = 0;
881
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000882 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000883 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000884 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000885 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000886 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000887 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000888 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000889 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000890 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000891 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000892 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000893 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000894 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000895 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000896
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200897 if (mask == 0 || type[1] != '\0')
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200898 bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000899
900 return mask;
901}
902#endif
903
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000904#if ENABLE_FEATURE_FIND_PERM \
905 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100906 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000907static const char* plus_minus_num(const char* str)
908{
909 if (*str == '-' || *str == '+')
910 str++;
911 return str;
912}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000913#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000914
Denys Vlasenko6db5f672013-05-16 18:36:42 +0200915/* Say no to GCCism */
916#define USE_NESTED_FUNCTION 0
917
918#if !USE_NESTED_FUNCTION
919struct pp_locals {
920 action*** appp;
921 unsigned cur_group;
922 unsigned cur_action;
923 IF_FEATURE_FIND_NOT( bool invert_flag; )
924};
925static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
926{
927 action *ap = xzalloc(sizeof_struct);
928 action **app;
929 action ***group = &ppl->appp[ppl->cur_group];
930 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
931 app[ppl->cur_action++] = ap;
932 app[ppl->cur_action] = NULL;
933 ap->f = f;
934 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
935 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
936 return ap;
937}
938#endif
939
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000940static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000941{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000942 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100943 OPT_FOLLOW ,
944 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
945 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200946 PARM_a ,
947 PARM_o ,
948 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000949#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200950 PARM_and ,
951 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000952 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000953#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200954 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000955 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000956 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
957 IF_FEATURE_FIND_DELETE( PARM_delete ,)
958 IF_FEATURE_FIND_EXEC( PARM_exec ,)
959 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100960 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200961 PARM_name ,
962 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000963 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200964#if ENABLE_DESKTOP
965 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +0200966 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200967 IF_FEATURE_FIND_PATH( PARM_wholename ,)
968#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200969 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000970 IF_FEATURE_FIND_REGEX( PARM_regex ,)
971 IF_FEATURE_FIND_TYPE( PARM_type ,)
972 IF_FEATURE_FIND_PERM( PARM_perm ,)
973 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
974 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
975 IF_FEATURE_FIND_NEWER( PARM_newer ,)
976 IF_FEATURE_FIND_INUM( PARM_inum ,)
977 IF_FEATURE_FIND_USER( PARM_user ,)
978 IF_FEATURE_FIND_GROUP( PARM_group ,)
979 IF_FEATURE_FIND_SIZE( PARM_size ,)
980 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100981 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100982 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000983 };
984
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000985 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100986 "-follow\0"
987 IF_FEATURE_FIND_XDEV( "-xdev\0" )
988 IF_FEATURE_FIND_DEPTH( "-depth\0" )
989 "-a\0"
990 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000991 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000992#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100993 "-and\0"
994 "-or\0"
995 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +0000996#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100997 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000998 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000999 IF_FEATURE_FIND_PRUNE( "-prune\0" )
1000 IF_FEATURE_FIND_DELETE( "-delete\0" )
1001 IF_FEATURE_FIND_EXEC( "-exec\0" )
1002 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001003 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +01001004 "-name\0"
1005 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001006 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001007#if ENABLE_DESKTOP
1008 IF_FEATURE_FIND_PATH( "-wholename\0")
1009#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001010 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001011 IF_FEATURE_FIND_REGEX( "-regex\0" )
1012 IF_FEATURE_FIND_TYPE( "-type\0" )
1013 IF_FEATURE_FIND_PERM( "-perm\0" )
1014 IF_FEATURE_FIND_MTIME( "-mtime\0" )
1015 IF_FEATURE_FIND_MMIN( "-mmin\0" )
1016 IF_FEATURE_FIND_NEWER( "-newer\0" )
1017 IF_FEATURE_FIND_INUM( "-inum\0" )
1018 IF_FEATURE_FIND_USER( "-user\0" )
1019 IF_FEATURE_FIND_GROUP( "-group\0" )
1020 IF_FEATURE_FIND_SIZE( "-size\0" )
1021 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001022 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001023 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001024 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001025
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001026#if !USE_NESTED_FUNCTION
1027 struct pp_locals ppl;
1028#define appp (ppl.appp )
1029#define cur_group (ppl.cur_group )
1030#define cur_action (ppl.cur_action )
1031#define invert_flag (ppl.invert_flag)
1032#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1033#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001034 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001035 unsigned cur_group;
1036 unsigned cur_action;
1037 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001038
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001039 /* This is the only place in busybox where we use nested function.
1040 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001041 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001042 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001043 action* alloc_action(int sizeof_struct, action_fp f)
1044 {
1045 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001046 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001047 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001048 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001049 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001050 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1051 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001052 return ap;
1053 }
1054#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001055#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001056
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001057 cur_group = 0;
1058 cur_action = 0;
1059 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001060 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001061
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001062 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001063 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001064 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001065 const char *arg1 = argv[1];
1066
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001067 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1068
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001069 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001070 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001071 if (!arg1)
1072 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1073 argv++;
1074 }
1075
1076 /* We can use big switch() here, but on i386
1077 * it doesn't give smaller code. Other arches? */
1078
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001079/* Options always return true. They always take effect
1080 * rather than being processed only when their place in the
1081 * expression is reached.
1082 */
1083 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001084 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001085 dbg("follow enabled: %d", __LINE__);
1086 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001087 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001088#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001089 else if (parm == OPT_XDEV) {
1090 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001091 G.xdev_on = 1;
1092 }
1093#endif
1094#if ENABLE_FEATURE_FIND_MAXDEPTH
1095 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001096 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001097 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1098 }
1099#endif
1100#if ENABLE_FEATURE_FIND_DEPTH
1101 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001102 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001103 G.recurse_flags |= ACTION_DEPTHFIRST;
1104 }
1105#endif
1106/* Actions are grouped by operators
1107 * ( expr ) Force precedence
1108 * ! expr True if expr is false
1109 * -not expr Same as ! expr
1110 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1111 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1112 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1113 * We implement: (), -a, -o
1114 */
1115 /* Operators */
1116 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001117 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001118 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001119 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001120 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001121 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001122 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001123 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001124 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001125 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001126 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001127 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001128 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001129#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001130 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001131 /* also handles "find ! ! -name 'foo*'" */
1132 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001133 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001134 }
1135#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001136 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001137 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001138 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001139 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001140 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001141 }
1142#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001143 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001144 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001145 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001146 (void) ALLOC_ACTION(print0);
1147 }
Paul Foxd7384292006-05-12 14:47:20 +00001148#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001149#if ENABLE_FEATURE_FIND_PRUNE
1150 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001151 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001152 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001153 }
1154#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001155#if ENABLE_FEATURE_FIND_DELETE
1156 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001157 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001158 G.need_print = 0;
1159 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001160 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001161 }
Rob Landleycee605c2005-10-06 16:39:17 +00001162#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001163#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001164 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001165 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001166 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001167 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001168 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001169 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001170 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001171 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001172 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001173 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001174 if (!*argv) /* did not see ';' or '+' until end */
1175 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001176 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001177 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001178 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001179 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001180 if ((argv[0][0] == ';' || argv[0][0] == '+')
1181 && argv[0][1] == '\0'
1182 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001183# if ENABLE_FEATURE_FIND_EXEC_PLUS
1184 if (argv[0][0] == '+')
1185 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1186# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001187 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001188 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001189 argv++;
1190 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001191 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001192 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001193 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001194 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001195 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001196 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001197 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001198 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1199 }
1200# if ENABLE_FEATURE_FIND_EXEC_PLUS
1201 /*
1202 * coreutils expects {} to appear only once in "-exec +"
1203 */
1204 if (all_subst != 1 && ap->filelist)
1205 bb_error_msg_and_die("only one '{}' allowed for -exec +");
1206# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001207 }
1208#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001209#if ENABLE_FEATURE_FIND_PAREN
1210 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001211 action_paren *ap;
1212 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001213 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001214
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001215 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001216 endarg = argv;
1217 while (1) {
1218 if (!*++endarg)
1219 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001220 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001221 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001222 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001223 *endarg = NULL;
1224 break;
1225 }
1226 }
1227 ap = ALLOC_ACTION(paren);
1228 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001229 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001230 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001231 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001232#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001233 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001234 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001235 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001236 ap = ALLOC_ACTION(name);
1237 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001238 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001239 }
1240#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001241 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001242 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001243 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001244 ap = ALLOC_ACTION(path);
1245 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001246 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001247 }
1248#endif
1249#if ENABLE_FEATURE_FIND_REGEX
1250 else if (parm == PARM_regex) {
1251 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001252 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001253 ap = ALLOC_ACTION(regex);
1254 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1255 }
1256#endif
1257#if ENABLE_FEATURE_FIND_TYPE
1258 else if (parm == PARM_type) {
1259 action_type *ap;
1260 ap = ALLOC_ACTION(type);
1261 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001262 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001263 }
1264#endif
1265#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001266/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001267 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001268 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001269 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001270 */
1271 else if (parm == PARM_perm) {
1272 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001273 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001274 ap = ALLOC_ACTION(perm);
1275 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001276 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001277 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +02001278 ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask);
1279 if (ap->perm_mask == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +01001280 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001281 }
1282#endif
1283#if ENABLE_FEATURE_FIND_MTIME
1284 else if (parm == PARM_mtime) {
1285 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001286 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001287 ap = ALLOC_ACTION(mtime);
1288 ap->mtime_char = arg1[0];
1289 ap->mtime_days = xatoul(plus_minus_num(arg1));
1290 }
1291#endif
1292#if ENABLE_FEATURE_FIND_MMIN
1293 else if (parm == PARM_mmin) {
1294 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001295 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001296 ap = ALLOC_ACTION(mmin);
1297 ap->mmin_char = arg1[0];
1298 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1299 }
1300#endif
1301#if ENABLE_FEATURE_FIND_NEWER
1302 else if (parm == PARM_newer) {
1303 struct stat stat_newer;
1304 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001305 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001306 ap = ALLOC_ACTION(newer);
1307 xstat(arg1, &stat_newer);
1308 ap->newer_mtime = stat_newer.st_mtime;
1309 }
1310#endif
1311#if ENABLE_FEATURE_FIND_INUM
1312 else if (parm == PARM_inum) {
1313 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001314 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001315 ap = ALLOC_ACTION(inum);
1316 ap->inode_num = xatoul(arg1);
1317 }
1318#endif
1319#if ENABLE_FEATURE_FIND_USER
1320 else if (parm == PARM_user) {
1321 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001322 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001323 ap = ALLOC_ACTION(user);
1324 ap->uid = bb_strtou(arg1, NULL, 10);
1325 if (errno)
1326 ap->uid = xuname2uid(arg1);
1327 }
1328#endif
1329#if ENABLE_FEATURE_FIND_GROUP
1330 else if (parm == PARM_group) {
1331 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001332 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001333 ap = ALLOC_ACTION(group);
1334 ap->gid = bb_strtou(arg1, NULL, 10);
1335 if (errno)
1336 ap->gid = xgroup2gid(arg1);
1337 }
1338#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001339#if ENABLE_FEATURE_FIND_SIZE
1340 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001341/* -size n[bckw]: file uses n units of space
1342 * b (default): units are 512-byte blocks
1343 * c: 1 byte
1344 * k: kilobytes
1345 * w: 2-byte words
1346 */
1347#if ENABLE_LFS
1348#define XATOU_SFX xatoull_sfx
1349#else
1350#define XATOU_SFX xatoul_sfx
1351#endif
1352 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001353 { "c", 1 },
1354 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001355 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001356 { "b", 512 },
1357 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001358 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001359 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001360 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001361 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001362 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001363 ap->size_char = arg1[0];
1364 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001365 }
1366#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001367#if ENABLE_FEATURE_FIND_CONTEXT
1368 else if (parm == PARM_context) {
1369 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001370 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001371 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001372 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001373 /* SELinux headers erroneously declare non-const parameter */
1374 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001375 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001376 }
1377#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001378#if ENABLE_FEATURE_FIND_LINKS
1379 else if (parm == PARM_links) {
1380 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001381 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001382 ap = ALLOC_ACTION(links);
1383 ap->links_char = arg1[0];
1384 ap->links_count = xatoul(plus_minus_num(arg1));
1385 }
1386#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001387 else {
1388 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001389 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001390 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001391 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001392 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001393 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001394 return appp;
1395#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001396#undef appp
1397#undef cur_action
1398#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001399}
1400
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001401int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001402int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001403{
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001404 int i, firstopt, status = EXIT_SUCCESS;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001405 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001406
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001407 INIT_G();
1408
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001409 /* "find -type f" + getopt("+HLP") => disaster.
1410 * Need to avoid getopt running into a non-HLP option.
1411 * Do this by temporarily storing NULL there:
1412 */
1413 past_HLP = argv;
1414 for (;;) {
1415 saved = *++past_HLP;
1416 if (!saved)
1417 break;
1418 if (saved[0] != '-')
1419 break;
1420 if (!saved[1])
1421 break; /* it is "-" */
1422 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1423 break;
1424 }
1425 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001426 /* "+": stop on first non-option */
1427 i = getopt32(argv, "+HLP");
1428 if (i & (1<<0))
1429 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1430 if (i & (1<<1))
1431 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1432 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001433 argv = past_HLP; /* same result as "argv += optind;" */
1434 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001435
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001436 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001437 if (argv[firstopt][0] == '-')
1438 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001439 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1440 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001441 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001442 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001443 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001444 if (firstopt == 0) {
1445 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001446 firstopt++;
1447 }
1448
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001449 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001450 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001451
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001452#if ENABLE_FEATURE_FIND_XDEV
1453 if (G.xdev_on) {
1454 struct stat stbuf;
1455
1456 G.xdev_count = firstopt;
1457 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1458 for (i = 0; argv[i]; i++) {
1459 /* not xstat(): shouldn't bomb out on
1460 * "find not_exist exist -xdev" */
1461 if (stat(argv[i], &stbuf) == 0)
1462 G.xdev_dev[i] = stbuf.st_dev;
1463 /* else G.xdev_dev[i] stays 0 and
1464 * won't match any real device dev_t
1465 */
1466 }
1467 }
1468#endif
1469
1470 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001471 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001472 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001473 fileAction, /* file action */
1474 fileAction, /* dir action */
1475 NULL, /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001476 0) /* depth */
1477 ) {
Felix Fietkauf7466e42015-04-09 10:20:16 +02001478 status |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001479 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001480 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001481
Felix Fietkauf7466e42015-04-09 10:20:16 +02001482 IF_FEATURE_FIND_EXEC_PLUS(status |= flush_exec_plus();)
Matt Kraai096370d2001-02-07 03:52:38 +00001483 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001484}