blob: 6407c6c5afd2c623ba532975932e17db23de6c14 [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:
Denys Vlasenko008e73b2018-07-06 21:04:41 +020098//config:config FEATURE_FIND_EXECUTABLE
99//config: bool "Enable -executable: file is executable"
100//config: default y
101//config: depends on FIND
102//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200103//config:config FEATURE_FIND_XDEV
104//config: bool "Enable -xdev: 'stay in filesystem'"
105//config: default y
106//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200107//config:
108//config:config FEATURE_FIND_MAXDEPTH
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100109//config: bool "Enable -mindepth N and -maxdepth N"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200110//config: default y
111//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200112//config:
113//config:config FEATURE_FIND_NEWER
114//config: bool "Enable -newer: compare file modification times"
115//config: default y
116//config: depends on FIND
117//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200118//config: Support the 'find -newer' option for finding any files which have
119//config: modification time that is more recent than the specified FILE.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200120//config:
121//config:config FEATURE_FIND_INUM
122//config: bool "Enable -inum: inode number matching"
123//config: default y
124//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200125//config:
126//config:config FEATURE_FIND_EXEC
127//config: bool "Enable -exec: execute commands"
128//config: default y
129//config: depends on FIND
130//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200131//config: Support the 'find -exec' option for executing commands based upon
132//config: the files matched.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200133//config:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200134//config:config FEATURE_FIND_EXEC_PLUS
135//config: bool "Enable -exec ... {} +"
136//config: default y
137//config: depends on FEATURE_FIND_EXEC
138//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200139//config: Support the 'find -exec ... {} +' option for executing commands
140//config: for all matched files at once.
141//config: Without this option, -exec + is a synonym for -exec ;
142//config: (IOW: it works correctly, but without expected speedup)
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200143//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200144//config:config FEATURE_FIND_USER
145//config: bool "Enable -user: username/uid matching"
146//config: default y
147//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200148//config:
149//config:config FEATURE_FIND_GROUP
150//config: bool "Enable -group: group/gid matching"
151//config: default y
152//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200153//config:
154//config:config FEATURE_FIND_NOT
155//config: bool "Enable the 'not' (!) operator"
156//config: default y
157//config: depends on FIND
158//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200159//config: Support the '!' operator to invert the test results.
160//config: If 'Enable full-blown desktop' is enabled, then will also support
161//config: the non-POSIX notation '-not'.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200162//config:
163//config:config FEATURE_FIND_DEPTH
164//config: bool "Enable -depth"
165//config: default y
166//config: depends on FIND
167//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200168//config: Process each directory's contents before the directory itself.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200169//config:
170//config:config FEATURE_FIND_PAREN
171//config: bool "Enable parens in options"
172//config: default y
173//config: depends on FIND
174//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200175//config: Enable usage of parens '(' to specify logical order of arguments.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200176//config:
177//config:config FEATURE_FIND_SIZE
178//config: bool "Enable -size: file size matching"
179//config: default y
180//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200181//config:
182//config:config FEATURE_FIND_PRUNE
183//config: bool "Enable -prune: exclude subdirectories"
184//config: default y
185//config: depends on FIND
186//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200187//config: If the file is a directory, don't descend into it. Useful for
188//config: exclusion .svn and CVS directories.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200189//config:
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200190//config:config FEATURE_FIND_QUIT
191//config: bool "Enable -quit: exit"
192//config: default y
193//config: depends on FIND
194//config: help
195//config: If this action is reached, 'find' exits.
196//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200197//config:config FEATURE_FIND_DELETE
198//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200199//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200200//config: depends on FIND && FEATURE_FIND_DEPTH
201//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200202//config: Support the 'find -delete' option for deleting files and directories.
203//config: WARNING: This option can do much harm if used wrong. Busybox will not
204//config: try to protect the user from doing stupid things. Use with care.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200205//config:
206//config:config FEATURE_FIND_PATH
207//config: bool "Enable -path: match pathname with shell pattern"
208//config: default y
209//config: depends on FIND
210//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200211//config: The -path option matches whole pathname instead of just filename.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200212//config:
213//config:config FEATURE_FIND_REGEX
214//config: bool "Enable -regex: match pathname with regex"
215//config: default y
216//config: depends on FIND
217//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200218//config: The -regex option matches whole pathname against regular expression.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200219//config:
220//config:config FEATURE_FIND_CONTEXT
221//config: bool "Enable -context: security context matching"
222//config: default n
223//config: depends on FIND && SELINUX
224//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200225//config: Support the 'find -context' option for matching security context.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200226//config:
227//config:config FEATURE_FIND_LINKS
228//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200229//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200230//config: depends on FIND
231//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200232//config: Support the 'find -links' option for matching number of links.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200233
Denys Vlasenko805f2452011-02-12 20:26:14 +0100234//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
235
236//kbuild:lib-$(CONFIG_FIND) += find.o
237
238//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200239//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100240//usage:#define find_full_usage "\n\n"
241//usage: "Search for files and perform actions on them.\n"
242//usage: "First failed action stops processing of current file.\n"
243//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200244//usage: "\n -L,-follow Follow symlinks"
245//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100246//usage: IF_FEATURE_FIND_XDEV(
247//usage: "\n -xdev Don't descend directories on other filesystems"
248//usage: )
249//usage: IF_FEATURE_FIND_MAXDEPTH(
250//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
251//usage: "\n actions to command line arguments only"
252//usage: "\n -mindepth N Don't act on first N levels"
253//usage: )
254//usage: IF_FEATURE_FIND_DEPTH(
255//usage: "\n -depth Act on directory *after* traversing it"
256//usage: )
257//usage: "\n"
258//usage: "\nActions:"
259//usage: IF_FEATURE_FIND_PAREN(
260//usage: "\n ( ACTIONS ) Group actions for -o / -a"
261//usage: )
262//usage: IF_FEATURE_FIND_NOT(
263//usage: "\n ! ACT Invert ACT's success/failure"
264//usage: )
265//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
266//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
267//usage: "\n Note: -a has higher priority than -o"
268//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
269//usage: "\n -iname PATTERN Case insensitive -name"
270//usage: IF_FEATURE_FIND_PATH(
271//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200272//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100273//usage: )
274//usage: IF_FEATURE_FIND_REGEX(
275//usage: "\n -regex PATTERN Match path to regex PATTERN"
276//usage: )
277//usage: IF_FEATURE_FIND_TYPE(
Denys Vlasenkof4eaccb2018-04-24 10:13:45 +0200278//usage: "\n -type X File type is X (one of: f,d,l,b,c,s,p)"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100279//usage: )
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200280//usage: IF_FEATURE_FIND_EXECUTABLE(
281//usage: "\n -executable File is executable"
282//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100283//usage: IF_FEATURE_FIND_PERM(
284//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100285//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100286//usage: )
287//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100288//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
289//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100290//usage: )
291//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100292//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
293//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100294//usage: )
295//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100296//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100297//usage: )
298//usage: IF_FEATURE_FIND_INUM(
299//usage: "\n -inum N File has inode number N"
300//usage: )
301//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100302//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100303//usage: )
304//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100305//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100306//usage: )
307//usage: IF_FEATURE_FIND_SIZE(
308//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
309//usage: "\n +/-N: file size is bigger/smaller than N"
310//usage: )
311//usage: IF_FEATURE_FIND_LINKS(
312//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
313//usage: "\n or exactly N"
314//usage: )
315//usage: IF_FEATURE_FIND_CONTEXT(
316//usage: "\n -context CTX File has specified security context"
317//usage: )
318//usage: IF_FEATURE_FIND_PRUNE(
319//usage: "\n -prune If current file is directory, don't descend into it"
320//usage: )
321//usage: "\nIf none of the following actions is specified, -print is assumed"
322//usage: "\n -print Print file name"
323//usage: IF_FEATURE_FIND_PRINT0(
324//usage: "\n -print0 Print file name, NUL terminated"
325//usage: )
326//usage: IF_FEATURE_FIND_EXEC(
327//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
328//usage: "\n file name. Fails if CMD exits with nonzero"
329//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200330//usage: IF_FEATURE_FIND_EXEC_PLUS(
331//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
332//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100333//usage: IF_FEATURE_FIND_DELETE(
334//usage: "\n -delete Delete current file/directory. Turns on -depth option"
335//usage: )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200336//usage: IF_FEATURE_FIND_QUIT(
337//usage: "\n -quit Exit"
338//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100339//usage:
340//usage:#define find_example_usage
341//usage: "$ find / -name passwd\n"
342//usage: "/etc/passwd\n"
343
Matt Kraai096370d2001-02-07 03:52:38 +0000344#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000345#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200346#include "common_bufsiz.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000347#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200348# include "xregex.h"
349#endif
350/* GNUism: */
351#ifndef FNM_CASEFOLD
352# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000353#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000354
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200355#if 1
356# define dbg(...) ((void)0)
357#else
358# define dbg(...) bb_error_msg(__VA_ARGS__)
359#endif
360
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200361
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000362/* This is a NOEXEC applet. Be very careful! */
363
364
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200365typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000366
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000367typedef struct {
368 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000369#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000370 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000371#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000372} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200373
Denys Vlasenko606291b2009-09-23 23:15:43 +0200374#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200375#define ACTF(name) \
376 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200377 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200378 action_##name* ap UNUSED_PARAM)
379
380 ACTS(print)
381 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200382IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000383IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
384IF_FEATURE_FIND_PRINT0( ACTS(print0))
385IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200386IF_FEATURE_FIND_EXECUTABLE(ACTS(executable))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000387IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
388IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
389IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
390IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
391IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
392IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
393IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
394IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
395IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
396IF_FEATURE_FIND_PRUNE( ACTS(prune))
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200397IF_FEATURE_FIND_QUIT( ACTS(quit))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000398IF_FEATURE_FIND_DELETE( ACTS(delete))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200399IF_FEATURE_FIND_EXEC( ACTS(exec,
400 char **exec_argv; /* -exec ARGS */
401 unsigned *subst_count;
402 int exec_argc; /* count of ARGS */
403 IF_FEATURE_FIND_EXEC_PLUS(
404 /*
405 * filelist is NULL if "exec ;"
406 * non-NULL if "exec +"
407 */
408 char **filelist;
409 int filelist_idx;
410 int file_len;
411 )
412 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000413IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100414IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000415
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200416struct globals {
417 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
418 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100419#if ENABLE_FEATURE_FIND_MAXDEPTH
420 int minmaxdepth[2];
421#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200422 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100423 smallint need_print;
424 smallint xdev_on;
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200425 smalluint exitstatus;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200426 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200427 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100428} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200429#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200430#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200431 setup_common_bufsiz(); \
Denys Vlasenko7b85ec32015-10-13 17:17:34 +0200432 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200433 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100434 memset(&G, 0, sizeof(G)); \
435 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200436 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200437 G.need_print = 1; \
438 G.recurse_flags = ACTION_RECURSE; \
439} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000440
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000441/* Return values of ACTFs ('action functions') are a bit mask:
442 * bit 1=1: prune (use SKIP constant for setting it)
443 * bit 0=1: matched successfully (TRUE)
444 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000445
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200446static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000447{
448 int cur_group;
449 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000450 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000451 action **app, *ap;
452
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000453 /* "action group" is a set of actions ANDed together.
454 * groups are ORed together.
455 * We simply evaluate each group until we find one in which all actions
456 * succeed. */
457
458 /* -prune is special: if it is encountered, then we won't
459 * descend into current directory. It doesn't matter whether
460 * action group (in which -prune sits) will succeed or not:
461 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
462 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
463 * not starting with 'f' */
464
465 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
466 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
467 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
468 * On return, bit is restored. */
469
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000470 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100471 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000472 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000473 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000474 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000475 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000476 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000477 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000478 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000479#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000480 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000481#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200482 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000483 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000484 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000485 }
486 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200487 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000488 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000489}
490
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200491#if !FNM_CASEFOLD
492static char *strcpy_upcase(char *dst, const char *src)
493{
494 char *d = dst;
495 while (1) {
496 unsigned char ch = *src++;
497 if (ch >= 'a' && ch <= 'z')
498 ch -= ('a' - 'A');
499 *d++ = ch;
500 if (ch == '\0')
501 break;
502 }
503 return dst;
504}
505#endif
506
Denis Vlasenko92258542006-11-01 10:25:35 +0000507ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000508{
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100509 int r;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000510 const char *tmp = bb_basename(fileName);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100511 /* GNU findutils: find DIR/ -name DIR
512 * prints "DIR/" (DIR// prints "DIR//" etc).
513 * Need to strip trailing "/".
514 * Such names can come only from top-level names, but
515 * we can't do this before recursive_action() call,
516 * since then "find FILE/ -name FILE"
517 * would also work (on non-directories), which is wrong.
518 */
519 char *trunc_slash = NULL;
520
521 if (*tmp == '\0') {
522 /* "foo/bar/[//...]" */
523 while (tmp != fileName && tmp[-1] == '/')
524 tmp--;
525 if (tmp == fileName) { /* entire fileName is "//.."? */
526 /* yes, convert "//..." to "/"
527 * Testcases:
528 * find / -maxdepth 1 -name /: prints /
529 * find // -maxdepth 1 -name /: prints //
530 * find / -maxdepth 1 -name //: prints nothing
531 * find // -maxdepth 1 -name //: prints nothing
532 */
533 if (tmp[1])
534 trunc_slash = (char*)tmp + 1;
535 } else {
536 /* no, it's "foo/bar/[//...]", go back to 'b' */
537 trunc_slash = (char*)tmp;
538 while (tmp != fileName && tmp[-1] != '/')
539 tmp--;
540 }
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000541 }
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100542
Denys Vlasenko91388172010-04-26 09:54:04 +0200543 /* Was using FNM_PERIOD flag too,
544 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
545 * find -name '*foo' should match .foo too:
546 */
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100547 if (trunc_slash) *trunc_slash = '\0';
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200548#if FNM_CASEFOLD
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100549 r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0));
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200550#else
551 if (ap->iname)
552 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100553 r = fnmatch(ap->pattern, tmp, 0);
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200554#endif
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100555 if (trunc_slash) *trunc_slash = '/';
556 return r == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000557}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000558
Denis Vlasenkob9411292007-06-15 23:43:11 +0000559#if ENABLE_FEATURE_FIND_PATH
560ACTF(path)
561{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200562# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200563 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200564# else
565 if (ap->ipath)
566 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
567 return fnmatch(ap->pattern, fileName, 0) == 0;
568# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000569}
570#endif
571#if ENABLE_FEATURE_FIND_REGEX
572ACTF(regex)
573{
574 regmatch_t match;
575 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
576 return 0; /* no match */
577 if (match.rm_so)
578 return 0; /* match doesn't start at pos 0 */
579 if (fileName[match.rm_eo])
580 return 0; /* match doesn't end exactly at end of pathname */
581 return 1;
582}
583#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000584#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000585ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000586{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000587 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000588}
Rob Landleycee605c2005-10-06 16:39:17 +0000589#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200590#if ENABLE_FEATURE_FIND_EXECUTABLE
591ACTF(executable)
592{
593 return access(fileName, X_OK) == 0;
594}
595#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000596#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000597ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000598{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200599 /* -perm [+/]mode: at least one of perm_mask bits are set */
600 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000601 return (statbuf->st_mode & ap->perm_mask) != 0;
602 /* -perm -mode: all of perm_mask are set */
603 if (ap->perm_char == '-')
604 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
605 /* -perm mode: file mode must match perm_mask */
606 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000607}
Rob Landleycee605c2005-10-06 16:39:17 +0000608#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000609#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000610ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000611{
612 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000613 time_t mtime_secs = ap->mtime_days * 24*60*60;
614 if (ap->mtime_char == '+')
615 return file_age >= mtime_secs + 24*60*60;
616 if (ap->mtime_char == '-')
617 return file_age < mtime_secs;
618 /* just numeric mtime */
619 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000620}
Rob Landleycee605c2005-10-06 16:39:17 +0000621#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000622#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000623ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000624{
625 time_t file_age = time(NULL) - statbuf->st_mtime;
626 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000627 if (ap->mmin_char == '+')
628 return file_age >= mmin_secs + 60;
629 if (ap->mmin_char == '-')
630 return file_age < mmin_secs;
631 /* just numeric mmin */
632 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000633}
Paul Fox72d1a232006-01-13 21:05:41 +0000634#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000635#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000636ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000637{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000638 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000639}
Rob Landleycee605c2005-10-06 16:39:17 +0000640#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000641#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000642ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000643{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000644 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000645}
Rob Landleycee605c2005-10-06 16:39:17 +0000646#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000647#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200648static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000649{
650 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200651# if ENABLE_FEATURE_FIND_EXEC_PLUS
652 int size = ap->exec_argc + ap->filelist_idx + 1;
653# else
654 int size = ap->exec_argc + 1;
655# endif
656# if ENABLE_USE_PORTABLE_CODE
657 char **argv = alloca(sizeof(char*) * size);
658# else /* gcc 4.3.1 generates smaller code: */
659 char *argv[size];
660# endif
661 char **pp = argv;
662
663 for (i = 0; i < ap->exec_argc; i++) {
664 const char *arg = ap->exec_argv[i];
665
666# if ENABLE_FEATURE_FIND_EXEC_PLUS
667 if (ap->filelist) {
668 /* Handling "-exec +"
669 * Only one exec_argv[i] has substitution in it.
670 * Expand that one exec_argv[i] into file list.
671 */
672 if (ap->subst_count[i] == 0) {
673 *pp++ = xstrdup(arg);
674 } else {
675 int j = 0;
676 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200677 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200678 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
679 free(ap->filelist[j]);
680 j++;
681 }
682 }
683 } else
684# endif
685 {
686 /* Handling "-exec ;" */
687 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
688 }
689 }
690 *pp = NULL; /* terminate the list */
691
692# if ENABLE_FEATURE_FIND_EXEC_PLUS
693 if (ap->filelist) {
694 ap->filelist[0] = NULL;
695 ap->filelist_idx = 0;
696 ap->file_len = 0;
697 }
698# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000699
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000700 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000701 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000702 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000703
704 i = 0;
705 while (argv[i])
706 free(argv[i++]);
707 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000708}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200709ACTF(exec)
710{
711# if ENABLE_FEATURE_FIND_EXEC_PLUS
712 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200713 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200714
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200715 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
716 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200717 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
718 /* If we have lots of files already, exec the command */
719 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200720 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200721 rc = do_exec(ap, NULL);
722 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200723 }
724# endif
725 return do_exec(ap, fileName);
726}
727# if ENABLE_FEATURE_FIND_EXEC_PLUS
728static int flush_exec_plus(void)
729{
730 action *ap;
731 action **app;
732 action ***appp = G.actions;
733 while ((app = *appp++) != NULL) {
734 while ((ap = *app++) != NULL) {
735 if (ap->f == (action_fp)func_exec) {
736 action_exec *ae = (void*)ap;
737 if (ae->filelist_idx != 0) {
738 int rc = do_exec(ae, NULL);
739# if ENABLE_FEATURE_FIND_NOT
740 if (ap->invert) rc = !rc;
741# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200742 if (rc == 0)
743 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200744 }
745 }
746 }
747 }
748 return 0;
749}
750# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000751#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000752#if ENABLE_FEATURE_FIND_USER
753ACTF(user)
754{
755 return (statbuf->st_uid == ap->uid);
756}
757#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000758#if ENABLE_FEATURE_FIND_GROUP
759ACTF(group)
760{
761 return (statbuf->st_gid == ap->gid);
762}
763#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000764#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000765ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000766{
767 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000768 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000769}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000770#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000771ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000772{
773 puts(fileName);
774 return TRUE;
775}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000776#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000777ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000778{
779 return exec_actions(ap->subexpr, fileName, statbuf);
780}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000781#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000782#if ENABLE_FEATURE_FIND_SIZE
783ACTF(size)
784{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000785 if (ap->size_char == '+')
786 return statbuf->st_size > ap->size;
787 if (ap->size_char == '-')
788 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000789 return statbuf->st_size == ap->size;
790}
791#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000792#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000793/*
794 * -prune: if -depth is not given, return true and do not descend
795 * current dir; if -depth is given, return false with no effect.
796 * Example:
797 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
798 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000799ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000800{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000801 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000802}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000803#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200804#if ENABLE_FEATURE_FIND_QUIT
805ACTF(quit)
806{
807 exit(G.exitstatus);
808}
809#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000810#if ENABLE_FEATURE_FIND_DELETE
811ACTF(delete)
812{
813 int rc;
814 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8c0708a2015-10-07 21:01:47 +0200815 /* "find . -delete" skips rmdir(".") */
816 rc = 0;
817 if (NOT_LONE_CHAR(fileName, '.'))
818 rc = rmdir(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000819 } else {
820 rc = unlink(fileName);
821 }
822 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000823 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000824 return TRUE;
825}
826#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000827#if ENABLE_FEATURE_FIND_CONTEXT
828ACTF(context)
829{
830 security_context_t con;
831 int rc;
832
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200833 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000834 rc = getfilecon(fileName, &con);
835 } else {
836 rc = lgetfilecon(fileName, &con);
837 }
838 if (rc < 0)
839 return FALSE;
840 rc = strcmp(ap->context, con);
841 freecon(con);
842 return rc == 0;
843}
844#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100845#if ENABLE_FEATURE_FIND_LINKS
846ACTF(links)
847{
848 switch(ap->links_char) {
849 case '-' : return (statbuf->st_nlink < ap->links_count);
850 case '+' : return (statbuf->st_nlink > ap->links_count);
851 default: return (statbuf->st_nlink == ap->links_count);
852 }
853}
854#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000855
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +0000856static int FAST_FUNC fileAction(const char *fileName,
Denis Vlasenko85c24712008-03-17 09:04:04 +0000857 struct stat *statbuf,
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100858 void *userData UNUSED_PARAM,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000859 int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM))
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000860{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100861 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200862 int same_fs = 1;
863
864#if ENABLE_FEATURE_FIND_XDEV
865 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
866 int i;
867 for (i = 0; i < G.xdev_count; i++) {
868 if (G.xdev_dev[i] == statbuf->st_dev)
869 goto found;
870 }
871 //bb_error_msg("'%s': not same fs", fileName);
872 same_fs = 0;
873 found: ;
874 }
875#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000876
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100877#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200878 if (depth < G.minmaxdepth[0]) {
879 if (same_fs)
880 return TRUE; /* skip this, continue recursing */
881 return SKIP; /* stop recursing */
882 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100883 if (depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100884 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000885#endif
886
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100887 r = exec_actions(G.actions, fileName, statbuf);
888 /* Had no explicit -print[0] or -exec? then print */
889 if ((r & TRUE) && G.need_print)
890 puts(fileName);
891
892#if ENABLE_FEATURE_FIND_MAXDEPTH
893 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100894 if (depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100895 return SKIP;
896 }
897#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100898 /* -xdev stops on mountpoints, but AFTER mountpoit itself
899 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200900 if (!same_fs) {
901 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000902 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200903
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000904 /* Cannot return 0: our caller, recursive_action(),
905 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100906 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000907}
908
Eric Andersen17d49ef1999-10-06 20:25:32 +0000909
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000910#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000911static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000912{
913 int mask = 0;
914
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000915 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000916 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000917 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000918 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000919 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000920 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000921 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000922 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000923 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000924 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000925 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000926 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000927 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000928 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000929
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200930 if (mask == 0 || type[1] != '\0')
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200931 bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000932
933 return mask;
934}
935#endif
936
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000937#if ENABLE_FEATURE_FIND_PERM \
938 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100939 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000940static const char* plus_minus_num(const char* str)
941{
942 if (*str == '-' || *str == '+')
943 str++;
944 return str;
945}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000946#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000947
Denys Vlasenko6db5f672013-05-16 18:36:42 +0200948/* Say no to GCCism */
949#define USE_NESTED_FUNCTION 0
950
951#if !USE_NESTED_FUNCTION
952struct pp_locals {
953 action*** appp;
954 unsigned cur_group;
955 unsigned cur_action;
956 IF_FEATURE_FIND_NOT( bool invert_flag; )
957};
958static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
959{
960 action *ap = xzalloc(sizeof_struct);
961 action **app;
962 action ***group = &ppl->appp[ppl->cur_group];
963 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
964 app[ppl->cur_action++] = ap;
965 app[ppl->cur_action] = NULL;
966 ap->f = f;
967 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
968 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
969 return ap;
970}
971#endif
972
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000973static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000974{
Denis Vlasenko56fce002007-04-04 23:41:35 +0000975 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100976 OPT_FOLLOW ,
977 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
978 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200979 PARM_a ,
980 PARM_o ,
981 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000982#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200983 PARM_and ,
984 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000985 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +0000986#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200987 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000988 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000989 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200990 IF_FEATURE_FIND_QUIT( PARM_quit ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000991 IF_FEATURE_FIND_DELETE( PARM_delete ,)
992 IF_FEATURE_FIND_EXEC( PARM_exec ,)
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200993 IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000994 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100995 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200996 PARM_name ,
997 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000998 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +0200999#if ENABLE_DESKTOP
1000 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +02001001 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001002 IF_FEATURE_FIND_PATH( PARM_wholename ,)
1003#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001004 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001005 IF_FEATURE_FIND_REGEX( PARM_regex ,)
1006 IF_FEATURE_FIND_TYPE( PARM_type ,)
1007 IF_FEATURE_FIND_PERM( PARM_perm ,)
1008 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
1009 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
1010 IF_FEATURE_FIND_NEWER( PARM_newer ,)
1011 IF_FEATURE_FIND_INUM( PARM_inum ,)
1012 IF_FEATURE_FIND_USER( PARM_user ,)
1013 IF_FEATURE_FIND_GROUP( PARM_group ,)
1014 IF_FEATURE_FIND_SIZE( PARM_size ,)
1015 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001016 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001017 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001018 };
1019
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001020 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001021 "-follow\0"
1022 IF_FEATURE_FIND_XDEV( "-xdev\0" )
1023 IF_FEATURE_FIND_DEPTH( "-depth\0" )
1024 "-a\0"
1025 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001026 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001027#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001028 "-and\0"
1029 "-or\0"
1030 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001031#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001032 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001033 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001034 IF_FEATURE_FIND_PRUNE( "-prune\0" )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001035 IF_FEATURE_FIND_QUIT( "-quit\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001036 IF_FEATURE_FIND_DELETE( "-delete\0" )
1037 IF_FEATURE_FIND_EXEC( "-exec\0" )
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001038 IF_FEATURE_FIND_EXECUTABLE("-executable\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001039 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001040 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +01001041 "-name\0"
1042 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001043 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001044#if ENABLE_DESKTOP
1045 IF_FEATURE_FIND_PATH( "-wholename\0")
1046#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001047 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001048 IF_FEATURE_FIND_REGEX( "-regex\0" )
1049 IF_FEATURE_FIND_TYPE( "-type\0" )
1050 IF_FEATURE_FIND_PERM( "-perm\0" )
1051 IF_FEATURE_FIND_MTIME( "-mtime\0" )
1052 IF_FEATURE_FIND_MMIN( "-mmin\0" )
1053 IF_FEATURE_FIND_NEWER( "-newer\0" )
1054 IF_FEATURE_FIND_INUM( "-inum\0" )
1055 IF_FEATURE_FIND_USER( "-user\0" )
1056 IF_FEATURE_FIND_GROUP( "-group\0" )
1057 IF_FEATURE_FIND_SIZE( "-size\0" )
1058 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001059 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001060 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001061 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001062
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001063#if !USE_NESTED_FUNCTION
1064 struct pp_locals ppl;
1065#define appp (ppl.appp )
1066#define cur_group (ppl.cur_group )
1067#define cur_action (ppl.cur_action )
1068#define invert_flag (ppl.invert_flag)
1069#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1070#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001071 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001072 unsigned cur_group;
1073 unsigned cur_action;
1074 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001075
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001076 /* This is the only place in busybox where we use nested function.
1077 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001078 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001079 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001080 action* alloc_action(int sizeof_struct, action_fp f)
1081 {
1082 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001083 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001084 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001085 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001086 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001087 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1088 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001089 return ap;
1090 }
1091#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001092#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001093
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001094 cur_group = 0;
1095 cur_action = 0;
1096 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001097 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001098
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001099 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001100 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001101 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001102 const char *arg1 = argv[1];
1103
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001104 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1105
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001106 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001107 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001108 if (!arg1)
1109 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1110 argv++;
1111 }
1112
1113 /* We can use big switch() here, but on i386
1114 * it doesn't give smaller code. Other arches? */
1115
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001116/* Options always return true. They always take effect
1117 * rather than being processed only when their place in the
1118 * expression is reached.
1119 */
1120 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001121 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001122 dbg("follow enabled: %d", __LINE__);
1123 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001124 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001125#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001126 else if (parm == OPT_XDEV) {
1127 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001128 G.xdev_on = 1;
1129 }
1130#endif
1131#if ENABLE_FEATURE_FIND_MAXDEPTH
1132 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001133 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001134 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1135 }
1136#endif
1137#if ENABLE_FEATURE_FIND_DEPTH
1138 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001139 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001140 G.recurse_flags |= ACTION_DEPTHFIRST;
1141 }
1142#endif
1143/* Actions are grouped by operators
1144 * ( expr ) Force precedence
1145 * ! expr True if expr is false
1146 * -not expr Same as ! expr
1147 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1148 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1149 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1150 * We implement: (), -a, -o
1151 */
1152 /* Operators */
1153 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001154 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001155 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001156 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001157 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001158 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001159 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001160 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001161 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001162 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001163 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001164 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001165 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001166#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001167 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001168 /* also handles "find ! ! -name 'foo*'" */
1169 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001170 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001171 }
1172#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001173 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001174 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001175 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001176 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001177 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001178 }
1179#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001180 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001181 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001182 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001183 (void) ALLOC_ACTION(print0);
1184 }
Paul Foxd7384292006-05-12 14:47:20 +00001185#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001186#if ENABLE_FEATURE_FIND_PRUNE
1187 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001188 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001189 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001190 }
1191#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001192#if ENABLE_FEATURE_FIND_QUIT
1193 else if (parm == PARM_quit) {
1194 dbg("%d", __LINE__);
1195 (void) ALLOC_ACTION(quit);
1196 }
1197#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001198#if ENABLE_FEATURE_FIND_DELETE
1199 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001200 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001201 G.need_print = 0;
1202 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001203 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001204 }
Rob Landleycee605c2005-10-06 16:39:17 +00001205#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001206#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001207 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001208 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001209 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001210 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001211 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001212 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001213 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001214 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001215 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001216 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001217 if (!*argv) /* did not see ';' or '+' until end */
1218 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001219 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001220 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001221 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001222 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001223 if ((argv[0][0] == ';' || argv[0][0] == '+')
1224 && argv[0][1] == '\0'
1225 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001226# if ENABLE_FEATURE_FIND_EXEC_PLUS
1227 if (argv[0][0] == '+')
1228 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1229# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001230 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001231 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001232 argv++;
1233 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001234 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001235 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001236 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001237 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001238 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001239 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001240 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001241 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1242 }
1243# if ENABLE_FEATURE_FIND_EXEC_PLUS
1244 /*
1245 * coreutils expects {} to appear only once in "-exec +"
1246 */
1247 if (all_subst != 1 && ap->filelist)
1248 bb_error_msg_and_die("only one '{}' allowed for -exec +");
1249# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001250 }
1251#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001252#if ENABLE_FEATURE_FIND_PAREN
1253 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001254 action_paren *ap;
1255 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001256 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001257
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001258 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001259 endarg = argv;
1260 while (1) {
1261 if (!*++endarg)
1262 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001263 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001264 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001265 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001266 *endarg = NULL;
1267 break;
1268 }
1269 }
1270 ap = ALLOC_ACTION(paren);
1271 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001272 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001273 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001274 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001275#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001276 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001277 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001278 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001279 ap = ALLOC_ACTION(name);
1280 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001281 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001282 }
1283#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001284 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001285 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001286 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001287 ap = ALLOC_ACTION(path);
1288 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001289 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001290 }
1291#endif
1292#if ENABLE_FEATURE_FIND_REGEX
1293 else if (parm == PARM_regex) {
1294 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001295 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001296 ap = ALLOC_ACTION(regex);
1297 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1298 }
1299#endif
1300#if ENABLE_FEATURE_FIND_TYPE
1301 else if (parm == PARM_type) {
1302 action_type *ap;
1303 ap = ALLOC_ACTION(type);
1304 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001305 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001306 }
1307#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001308#if ENABLE_FEATURE_FIND_EXECUTABLE
1309 else if (parm == PARM_executable) {
1310 (void) ALLOC_ACTION(executable);
1311 }
1312#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001313#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001314/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001315 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001316 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001317 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001318 */
1319 else if (parm == PARM_perm) {
1320 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001321 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001322 ap = ALLOC_ACTION(perm);
1323 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001324 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001325 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +02001326 ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask);
1327 if (ap->perm_mask == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +01001328 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001329 }
1330#endif
1331#if ENABLE_FEATURE_FIND_MTIME
1332 else if (parm == PARM_mtime) {
1333 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001334 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001335 ap = ALLOC_ACTION(mtime);
1336 ap->mtime_char = arg1[0];
1337 ap->mtime_days = xatoul(plus_minus_num(arg1));
1338 }
1339#endif
1340#if ENABLE_FEATURE_FIND_MMIN
1341 else if (parm == PARM_mmin) {
1342 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001343 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001344 ap = ALLOC_ACTION(mmin);
1345 ap->mmin_char = arg1[0];
1346 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1347 }
1348#endif
1349#if ENABLE_FEATURE_FIND_NEWER
1350 else if (parm == PARM_newer) {
1351 struct stat stat_newer;
1352 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001353 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001354 ap = ALLOC_ACTION(newer);
1355 xstat(arg1, &stat_newer);
1356 ap->newer_mtime = stat_newer.st_mtime;
1357 }
1358#endif
1359#if ENABLE_FEATURE_FIND_INUM
1360 else if (parm == PARM_inum) {
1361 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001362 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001363 ap = ALLOC_ACTION(inum);
1364 ap->inode_num = xatoul(arg1);
1365 }
1366#endif
1367#if ENABLE_FEATURE_FIND_USER
1368 else if (parm == PARM_user) {
1369 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001370 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001371 ap = ALLOC_ACTION(user);
1372 ap->uid = bb_strtou(arg1, NULL, 10);
1373 if (errno)
1374 ap->uid = xuname2uid(arg1);
1375 }
1376#endif
1377#if ENABLE_FEATURE_FIND_GROUP
1378 else if (parm == PARM_group) {
1379 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001380 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001381 ap = ALLOC_ACTION(group);
1382 ap->gid = bb_strtou(arg1, NULL, 10);
1383 if (errno)
1384 ap->gid = xgroup2gid(arg1);
1385 }
1386#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001387#if ENABLE_FEATURE_FIND_SIZE
1388 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001389/* -size n[bckw]: file uses n units of space
1390 * b (default): units are 512-byte blocks
1391 * c: 1 byte
1392 * k: kilobytes
1393 * w: 2-byte words
1394 */
1395#if ENABLE_LFS
1396#define XATOU_SFX xatoull_sfx
1397#else
1398#define XATOU_SFX xatoul_sfx
1399#endif
1400 static const struct suffix_mult find_suffixes[] = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001401 { "c", 1 },
1402 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001403 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001404 { "b", 512 },
1405 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001406 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001407 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001408 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001409 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001410 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001411 ap->size_char = arg1[0];
1412 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001413 }
1414#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001415#if ENABLE_FEATURE_FIND_CONTEXT
1416 else if (parm == PARM_context) {
1417 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001418 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001419 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001420 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001421 /* SELinux headers erroneously declare non-const parameter */
1422 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001423 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001424 }
1425#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001426#if ENABLE_FEATURE_FIND_LINKS
1427 else if (parm == PARM_links) {
1428 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001429 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001430 ap = ALLOC_ACTION(links);
1431 ap->links_char = arg1[0];
1432 ap->links_count = xatoul(plus_minus_num(arg1));
1433 }
1434#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001435 else {
1436 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001437 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001438 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001439 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001440 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001441 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001442 return appp;
1443#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001444#undef appp
1445#undef cur_action
1446#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001447}
1448
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001449int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001450int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001451{
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001452 int i, firstopt;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001453 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001454
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001455 INIT_G();
1456
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001457 /* "find -type f" + getopt("+HLP") => disaster.
1458 * Need to avoid getopt running into a non-HLP option.
1459 * Do this by temporarily storing NULL there:
1460 */
1461 past_HLP = argv;
1462 for (;;) {
1463 saved = *++past_HLP;
1464 if (!saved)
1465 break;
1466 if (saved[0] != '-')
1467 break;
1468 if (!saved[1])
1469 break; /* it is "-" */
1470 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1471 break;
1472 }
1473 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001474 /* "+": stop on first non-option */
1475 i = getopt32(argv, "+HLP");
1476 if (i & (1<<0))
1477 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1478 if (i & (1<<1))
1479 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1480 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001481 argv = past_HLP; /* same result as "argv += optind;" */
1482 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001483
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001484 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001485 if (argv[firstopt][0] == '-')
1486 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001487 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1488 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001489 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001490 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001491 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001492 if (firstopt == 0) {
1493 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001494 firstopt++;
1495 }
1496
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001497 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001498 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001499
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001500#if ENABLE_FEATURE_FIND_XDEV
1501 if (G.xdev_on) {
1502 struct stat stbuf;
1503
1504 G.xdev_count = firstopt;
1505 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1506 for (i = 0; argv[i]; i++) {
1507 /* not xstat(): shouldn't bomb out on
1508 * "find not_exist exist -xdev" */
1509 if (stat(argv[i], &stbuf) == 0)
1510 G.xdev_dev[i] = stbuf.st_dev;
1511 /* else G.xdev_dev[i] stays 0 and
1512 * won't match any real device dev_t
1513 */
1514 }
1515 }
1516#endif
1517
1518 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001519 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001520 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001521 fileAction, /* file action */
1522 fileAction, /* dir action */
1523 NULL, /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001524 0) /* depth */
1525 ) {
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001526 G.exitstatus |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001527 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001528 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001529
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001530 IF_FEATURE_FIND_EXEC_PLUS(G.exitstatus |= flush_exec_plus();)
1531 return G.exitstatus;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001532}