blob: 9feb44b642ad79d1d2bb5b5e03e7a1b9b7db1424 [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
Ismael Luceno0084c442021-09-29 13:41:32 +020070//config: bool "Enable -mtime: modification time matching"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020071//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:
Ismael Luceno04ad6832021-10-09 21:40:53 +020077//config:config FEATURE_FIND_ATIME
78//config: bool "Enable -atime: access time matching"
79//config: default y
80//config: depends on FEATURE_FIND_MTIME
81//config: help
82//config: Allow searching based on the access time of
83//config: files, in days.
84//config:
85//config:config FEATURE_FIND_CTIME
86//config: bool "Enable -ctime: status change timestamp matching"
87//config: default y
88//config: depends on FEATURE_FIND_MTIME
89//config: help
90//config: Allow searching based on the status change timestamp of
91//config: files, in days.
92//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020093//config:config FEATURE_FIND_MMIN
Ismael Luceno0084c442021-09-29 13:41:32 +020094//config: bool "Enable -mmin: modification time matching by minutes"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020095//config: default y
96//config: depends on FIND
97//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020098//config: Allow searching based on the modification time of
99//config: files, in minutes.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200100//config:
Ismael Luceno04ad6832021-10-09 21:40:53 +0200101//config:config FEATURE_FIND_AMIN
102//config: bool "Enable -amin: access time matching by minutes"
103//config: default y
104//config: depends on FEATURE_FIND_MMIN
105//config: help
106//config: Allow searching based on the access time of
107//config: files, in minutes.
108//config:
109//config:config FEATURE_FIND_CMIN
110//config: bool "Enable -cmin: status change timestamp matching by minutes"
111//config: default y
112//config: depends on FEATURE_FIND_MMIN
113//config: help
114//config: Allow searching based on the status change timestamp of
115//config: files, in minutes.
116//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200117//config:config FEATURE_FIND_PERM
118//config: bool "Enable -perm: permissions matching"
119//config: default y
120//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200121//config:
122//config:config FEATURE_FIND_TYPE
123//config: bool "Enable -type: file type matching (file/dir/link/...)"
124//config: default y
125//config: depends on FIND
126//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200127//config: Enable searching based on file type (file,
128//config: directory, socket, device, etc.).
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200129//config:
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200130//config:config FEATURE_FIND_EXECUTABLE
131//config: bool "Enable -executable: file is executable"
132//config: default y
133//config: depends on FIND
134//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200135//config:config FEATURE_FIND_XDEV
136//config: bool "Enable -xdev: 'stay in filesystem'"
137//config: default y
138//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200139//config:
140//config:config FEATURE_FIND_MAXDEPTH
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100141//config: bool "Enable -mindepth N and -maxdepth N"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200142//config: default y
143//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200144//config:
145//config:config FEATURE_FIND_NEWER
146//config: bool "Enable -newer: compare file modification times"
147//config: default y
148//config: depends on FIND
149//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200150//config: Support the 'find -newer' option for finding any files which have
151//config: modification time that is more recent than the specified FILE.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200152//config:
153//config:config FEATURE_FIND_INUM
154//config: bool "Enable -inum: inode number matching"
155//config: default y
156//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200157//config:
158//config:config FEATURE_FIND_EXEC
159//config: bool "Enable -exec: execute commands"
160//config: default y
161//config: depends on FIND
162//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200163//config: Support the 'find -exec' option for executing commands based upon
164//config: the files matched.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200165//config:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200166//config:config FEATURE_FIND_EXEC_PLUS
167//config: bool "Enable -exec ... {} +"
168//config: default y
169//config: depends on FEATURE_FIND_EXEC
170//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200171//config: Support the 'find -exec ... {} +' option for executing commands
172//config: for all matched files at once.
173//config: Without this option, -exec + is a synonym for -exec ;
174//config: (IOW: it works correctly, but without expected speedup)
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200175//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200176//config:config FEATURE_FIND_USER
177//config: bool "Enable -user: username/uid matching"
178//config: default y
179//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200180//config:
181//config:config FEATURE_FIND_GROUP
182//config: bool "Enable -group: group/gid matching"
183//config: default y
184//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200185//config:
186//config:config FEATURE_FIND_NOT
187//config: bool "Enable the 'not' (!) operator"
188//config: default y
189//config: depends on FIND
190//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200191//config: Support the '!' operator to invert the test results.
192//config: If 'Enable full-blown desktop' is enabled, then will also support
193//config: the non-POSIX notation '-not'.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200194//config:
195//config:config FEATURE_FIND_DEPTH
196//config: bool "Enable -depth"
197//config: default y
198//config: depends on FIND
199//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200200//config: Process each directory's contents before the directory itself.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200201//config:
202//config:config FEATURE_FIND_PAREN
203//config: bool "Enable parens in options"
204//config: default y
205//config: depends on FIND
206//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200207//config: Enable usage of parens '(' to specify logical order of arguments.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200208//config:
209//config:config FEATURE_FIND_SIZE
210//config: bool "Enable -size: file size matching"
211//config: default y
212//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200213//config:
214//config:config FEATURE_FIND_PRUNE
215//config: bool "Enable -prune: exclude subdirectories"
216//config: default y
217//config: depends on FIND
218//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200219//config: If the file is a directory, don't descend into it. Useful for
220//config: exclusion .svn and CVS directories.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200221//config:
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200222//config:config FEATURE_FIND_QUIT
223//config: bool "Enable -quit: exit"
224//config: default y
225//config: depends on FIND
226//config: help
227//config: If this action is reached, 'find' exits.
228//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200229//config:config FEATURE_FIND_DELETE
230//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200231//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200232//config: depends on FIND && FEATURE_FIND_DEPTH
233//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200234//config: Support the 'find -delete' option for deleting files and directories.
235//config: WARNING: This option can do much harm if used wrong. Busybox will not
236//config: try to protect the user from doing stupid things. Use with care.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200237//config:
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200238//config:config FEATURE_FIND_EMPTY
239//config: bool "Enable -empty: match empty files or directories"
240//config: default y
241//config: depends on FIND
242//config: help
243//config: Support the 'find -empty' option to find empty regular files
244//config: or directories.
245//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200246//config:config FEATURE_FIND_PATH
247//config: bool "Enable -path: match pathname with shell pattern"
248//config: default y
249//config: depends on FIND
250//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200251//config: The -path option matches whole pathname instead of just filename.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200252//config:
253//config:config FEATURE_FIND_REGEX
254//config: bool "Enable -regex: match pathname with regex"
255//config: default y
256//config: depends on FIND
257//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200258//config: The -regex option matches whole pathname against regular expression.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200259//config:
260//config:config FEATURE_FIND_CONTEXT
261//config: bool "Enable -context: security context matching"
262//config: default n
263//config: depends on FIND && SELINUX
264//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200265//config: Support the 'find -context' option for matching security context.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200266//config:
267//config:config FEATURE_FIND_LINKS
268//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200269//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200270//config: depends on FIND
271//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200272//config: Support the 'find -links' option for matching number of links.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200273
Denys Vlasenko805f2452011-02-12 20:26:14 +0100274//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
275
276//kbuild:lib-$(CONFIG_FIND) += find.o
277
278//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200279//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100280//usage:#define find_full_usage "\n\n"
281//usage: "Search for files and perform actions on them.\n"
282//usage: "First failed action stops processing of current file.\n"
283//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200284//usage: "\n -L,-follow Follow symlinks"
285//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100286//usage: IF_FEATURE_FIND_XDEV(
287//usage: "\n -xdev Don't descend directories on other filesystems"
288//usage: )
289//usage: IF_FEATURE_FIND_MAXDEPTH(
290//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
291//usage: "\n actions to command line arguments only"
292//usage: "\n -mindepth N Don't act on first N levels"
293//usage: )
294//usage: IF_FEATURE_FIND_DEPTH(
295//usage: "\n -depth Act on directory *after* traversing it"
296//usage: )
297//usage: "\n"
298//usage: "\nActions:"
299//usage: IF_FEATURE_FIND_PAREN(
300//usage: "\n ( ACTIONS ) Group actions for -o / -a"
301//usage: )
302//usage: IF_FEATURE_FIND_NOT(
303//usage: "\n ! ACT Invert ACT's success/failure"
304//usage: )
305//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
306//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
307//usage: "\n Note: -a has higher priority than -o"
308//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
309//usage: "\n -iname PATTERN Case insensitive -name"
310//usage: IF_FEATURE_FIND_PATH(
311//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200312//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100313//usage: )
314//usage: IF_FEATURE_FIND_REGEX(
315//usage: "\n -regex PATTERN Match path to regex PATTERN"
316//usage: )
317//usage: IF_FEATURE_FIND_TYPE(
Denys Vlasenkof4eaccb2018-04-24 10:13:45 +0200318//usage: "\n -type X File type is X (one of: f,d,l,b,c,s,p)"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100319//usage: )
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200320//usage: IF_FEATURE_FIND_EXECUTABLE(
321//usage: "\n -executable File is executable"
322//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100323//usage: IF_FEATURE_FIND_PERM(
324//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100325//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100326//usage: )
327//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100328//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
329//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100330//usage: )
Ismael Luceno04ad6832021-10-09 21:40:53 +0200331//usage: IF_FEATURE_FIND_ATIME(
332//usage: "\n -atime DAYS atime +N/-N/N days in the past"
333//usage: )
334//usage: IF_FEATURE_FIND_CTIME(
335//usage: "\n -ctime DAYS ctime +N/-N/N days in the past"
336//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100337//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100338//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
339//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100340//usage: )
Ismael Luceno04ad6832021-10-09 21:40:53 +0200341//usage: IF_FEATURE_FIND_AMIN(
342//usage: "\n -amin MINS atime +N/-N/N minutes in the past"
343//usage: )
344//usage: IF_FEATURE_FIND_CMIN(
345//usage: "\n -cmin MINS ctime +N/-N/N minutes in the past"
346//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100347//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100348//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100349//usage: )
350//usage: IF_FEATURE_FIND_INUM(
351//usage: "\n -inum N File has inode number N"
352//usage: )
353//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100354//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100355//usage: )
356//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100357//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100358//usage: )
359//usage: IF_FEATURE_FIND_SIZE(
360//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
361//usage: "\n +/-N: file size is bigger/smaller than N"
362//usage: )
363//usage: IF_FEATURE_FIND_LINKS(
364//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
365//usage: "\n or exactly N"
366//usage: )
367//usage: IF_FEATURE_FIND_CONTEXT(
368//usage: "\n -context CTX File has specified security context"
369//usage: )
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200370//usage: IF_FEATURE_FIND_EMPTY(
371//usage: "\n -empty Match empty file/directory"
372//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100373//usage: IF_FEATURE_FIND_PRUNE(
374//usage: "\n -prune If current file is directory, don't descend into it"
375//usage: )
376//usage: "\nIf none of the following actions is specified, -print is assumed"
377//usage: "\n -print Print file name"
378//usage: IF_FEATURE_FIND_PRINT0(
379//usage: "\n -print0 Print file name, NUL terminated"
380//usage: )
381//usage: IF_FEATURE_FIND_EXEC(
382//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
383//usage: "\n file name. Fails if CMD exits with nonzero"
384//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200385//usage: IF_FEATURE_FIND_EXEC_PLUS(
386//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
387//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100388//usage: IF_FEATURE_FIND_DELETE(
389//usage: "\n -delete Delete current file/directory. Turns on -depth option"
390//usage: )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200391//usage: IF_FEATURE_FIND_QUIT(
392//usage: "\n -quit Exit"
393//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100394//usage:
395//usage:#define find_example_usage
396//usage: "$ find / -name passwd\n"
397//usage: "/etc/passwd\n"
398
Matt Kraai096370d2001-02-07 03:52:38 +0000399#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000400#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200401#include "common_bufsiz.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000402#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200403# include "xregex.h"
404#endif
405/* GNUism: */
406#ifndef FNM_CASEFOLD
407# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000408#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000409
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200410#if 1
411# define dbg(...) ((void)0)
412#else
413# define dbg(...) bb_error_msg(__VA_ARGS__)
414#endif
415
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200416
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000417/* This is a NOEXEC applet. Be very careful! */
418
419
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200420typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000421
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000422typedef struct {
423 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000424#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000425 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000426#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000427} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200428
Denys Vlasenko606291b2009-09-23 23:15:43 +0200429#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200430#define ACTF(name) \
431 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200432 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200433 action_##name* ap UNUSED_PARAM)
434
435 ACTS(print)
436 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200437IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000438IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
439IF_FEATURE_FIND_PRINT0( ACTS(print0))
440IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200441IF_FEATURE_FIND_EXECUTABLE(ACTS(executable))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000442IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
Ismael Luceno04ad6832021-10-09 21:40:53 +0200443IF_FEATURE_FIND_MTIME( ACTS(mtime, char time_type; char mtime_char; unsigned mtime_days;))
444IF_FEATURE_FIND_MMIN( ACTS(mmin, char time_type; char mmin_char; unsigned mmin_mins;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000445IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
446IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
447IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
448IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
449IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
450IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
451IF_FEATURE_FIND_PRUNE( ACTS(prune))
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200452IF_FEATURE_FIND_QUIT( ACTS(quit))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000453IF_FEATURE_FIND_DELETE( ACTS(delete))
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200454IF_FEATURE_FIND_EMPTY( ACTS(empty))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200455IF_FEATURE_FIND_EXEC( ACTS(exec,
456 char **exec_argv; /* -exec ARGS */
457 unsigned *subst_count;
458 int exec_argc; /* count of ARGS */
459 IF_FEATURE_FIND_EXEC_PLUS(
460 /*
461 * filelist is NULL if "exec ;"
462 * non-NULL if "exec +"
463 */
464 char **filelist;
465 int filelist_idx;
466 int file_len;
467 )
468 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000469IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100470IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000471
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200472struct globals {
473 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
474 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100475#if ENABLE_FEATURE_FIND_MAXDEPTH
476 int minmaxdepth[2];
477#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200478 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100479 smallint need_print;
480 smallint xdev_on;
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200481 smalluint exitstatus;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200482 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200483 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100484} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200485#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200486#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200487 setup_common_bufsiz(); \
Denys Vlasenko7b85ec32015-10-13 17:17:34 +0200488 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200489 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100490 memset(&G, 0, sizeof(G)); \
491 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200492 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200493 G.need_print = 1; \
494 G.recurse_flags = ACTION_RECURSE; \
495} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000496
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000497/* Return values of ACTFs ('action functions') are a bit mask:
498 * bit 1=1: prune (use SKIP constant for setting it)
499 * bit 0=1: matched successfully (TRUE)
500 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000501
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200502static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000503{
504 int cur_group;
505 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000506 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000507 action **app, *ap;
508
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000509 /* "action group" is a set of actions ANDed together.
510 * groups are ORed together.
511 * We simply evaluate each group until we find one in which all actions
512 * succeed. */
513
514 /* -prune is special: if it is encountered, then we won't
515 * descend into current directory. It doesn't matter whether
516 * action group (in which -prune sits) will succeed or not:
517 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
518 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
519 * not starting with 'f' */
520
521 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
522 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
523 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
524 * On return, bit is restored. */
525
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000526 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100527 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000528 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000529 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000530 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000531 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000532 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000533 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000534 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000535#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000536 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000537#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200538 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000539 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000540 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000541 }
542 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200543 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000544 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000545}
546
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200547#if !FNM_CASEFOLD
548static char *strcpy_upcase(char *dst, const char *src)
549{
550 char *d = dst;
551 while (1) {
552 unsigned char ch = *src++;
553 if (ch >= 'a' && ch <= 'z')
554 ch -= ('a' - 'A');
555 *d++ = ch;
556 if (ch == '\0')
557 break;
558 }
559 return dst;
560}
561#endif
562
Denis Vlasenko92258542006-11-01 10:25:35 +0000563ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000564{
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100565 int r;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000566 const char *tmp = bb_basename(fileName);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100567 /* GNU findutils: find DIR/ -name DIR
568 * prints "DIR/" (DIR// prints "DIR//" etc).
569 * Need to strip trailing "/".
570 * Such names can come only from top-level names, but
571 * we can't do this before recursive_action() call,
572 * since then "find FILE/ -name FILE"
573 * would also work (on non-directories), which is wrong.
574 */
575 char *trunc_slash = NULL;
576
577 if (*tmp == '\0') {
578 /* "foo/bar/[//...]" */
579 while (tmp != fileName && tmp[-1] == '/')
580 tmp--;
581 if (tmp == fileName) { /* entire fileName is "//.."? */
582 /* yes, convert "//..." to "/"
583 * Testcases:
584 * find / -maxdepth 1 -name /: prints /
585 * find // -maxdepth 1 -name /: prints //
586 * find / -maxdepth 1 -name //: prints nothing
587 * find // -maxdepth 1 -name //: prints nothing
588 */
589 if (tmp[1])
590 trunc_slash = (char*)tmp + 1;
591 } else {
592 /* no, it's "foo/bar/[//...]", go back to 'b' */
593 trunc_slash = (char*)tmp;
594 while (tmp != fileName && tmp[-1] != '/')
595 tmp--;
596 }
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000597 }
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100598
Denys Vlasenko91388172010-04-26 09:54:04 +0200599 /* Was using FNM_PERIOD flag too,
600 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
601 * find -name '*foo' should match .foo too:
602 */
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100603 if (trunc_slash) *trunc_slash = '\0';
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200604#if FNM_CASEFOLD
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100605 r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0));
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200606#else
607 if (ap->iname)
608 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100609 r = fnmatch(ap->pattern, tmp, 0);
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200610#endif
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100611 if (trunc_slash) *trunc_slash = '/';
612 return r == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000613}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000614
Denis Vlasenkob9411292007-06-15 23:43:11 +0000615#if ENABLE_FEATURE_FIND_PATH
616ACTF(path)
617{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200618# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200619 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200620# else
621 if (ap->ipath)
622 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
623 return fnmatch(ap->pattern, fileName, 0) == 0;
624# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000625}
626#endif
627#if ENABLE_FEATURE_FIND_REGEX
628ACTF(regex)
629{
630 regmatch_t match;
631 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
632 return 0; /* no match */
633 if (match.rm_so)
634 return 0; /* match doesn't start at pos 0 */
635 if (fileName[match.rm_eo])
636 return 0; /* match doesn't end exactly at end of pathname */
637 return 1;
638}
639#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000640#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000641ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000642{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000643 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000644}
Rob Landleycee605c2005-10-06 16:39:17 +0000645#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200646#if ENABLE_FEATURE_FIND_EXECUTABLE
647ACTF(executable)
648{
649 return access(fileName, X_OK) == 0;
650}
651#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000652#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000653ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000654{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200655 /* -perm [+/]mode: at least one of perm_mask bits are set */
656 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000657 return (statbuf->st_mode & ap->perm_mask) != 0;
658 /* -perm -mode: all of perm_mask are set */
659 if (ap->perm_char == '-')
660 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
661 /* -perm mode: file mode must match perm_mask */
662 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000663}
Rob Landleycee605c2005-10-06 16:39:17 +0000664#endif
Ismael Luceno421c8762021-09-29 13:41:33 +0200665
Ismael Luceno04ad6832021-10-09 21:40:53 +0200666#if 0 \
667 || ENABLE_FEATURE_FIND_AMIN \
668 || ENABLE_FEATURE_FIND_ATIME \
669 || ENABLE_FEATURE_FIND_CMIN \
670 || ENABLE_FEATURE_FIND_CTIME \
671 || ENABLE_FEATURE_FIND_MMIN \
672 || ENABLE_FEATURE_FIND_MTIME
Ismael Luceno421c8762021-09-29 13:41:33 +0200673static int time_cmp(time_t ftime, char time_char, time_t secs, time_t delta)
674{
675 time_t file_age = time(NULL) - ftime;
676 switch (time_char) {
677 case '+': return file_age >= secs + delta;
678 case '-': return file_age < secs;
679 /* just numeric time */
680 default: return file_age >= secs && file_age < secs + delta;
681 }
682}
683#endif
684
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000685#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000686ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000687{
Ismael Luceno04ad6832021-10-09 21:40:53 +0200688 time_t t = statbuf->st_mtime;
689# if ENABLE_FEATURE_FIND_ATIME
690 if (ap->time_type == 'a')
691 t = statbuf->st_atime;
692# endif
693# if ENABLE_FEATURE_FIND_CTIME
694 if (ap->time_type == 'c')
695 t = statbuf->st_ctime;
696# endif
697 return time_cmp(t, ap->mtime_char,
Ismael Luceno421c8762021-09-29 13:41:33 +0200698 ap->mtime_days * 24*60*60, 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000699}
Rob Landleycee605c2005-10-06 16:39:17 +0000700#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000701#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000702ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000703{
Ismael Luceno04ad6832021-10-09 21:40:53 +0200704 time_t t = statbuf->st_mtime;
705# if ENABLE_FEATURE_FIND_AMIN
706 if (ap->time_type == 'a')
707 t = statbuf->st_atime;
708# endif
709# if ENABLE_FEATURE_FIND_CMIN
710 if (ap->time_type == 'c')
711 t = statbuf->st_ctime;
712# endif
713 return time_cmp(t, ap->mmin_char,
Ismael Luceno421c8762021-09-29 13:41:33 +0200714 ap->mmin_mins * 60, 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000715}
Paul Fox72d1a232006-01-13 21:05:41 +0000716#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000717#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000718ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000719{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000720 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000721}
Rob Landleycee605c2005-10-06 16:39:17 +0000722#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000723#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000724ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000725{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000726 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000727}
Rob Landleycee605c2005-10-06 16:39:17 +0000728#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000729#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200730static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000731{
732 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200733# if ENABLE_FEATURE_FIND_EXEC_PLUS
734 int size = ap->exec_argc + ap->filelist_idx + 1;
735# else
736 int size = ap->exec_argc + 1;
737# endif
738# if ENABLE_USE_PORTABLE_CODE
739 char **argv = alloca(sizeof(char*) * size);
740# else /* gcc 4.3.1 generates smaller code: */
741 char *argv[size];
742# endif
743 char **pp = argv;
744
745 for (i = 0; i < ap->exec_argc; i++) {
746 const char *arg = ap->exec_argv[i];
747
748# if ENABLE_FEATURE_FIND_EXEC_PLUS
749 if (ap->filelist) {
750 /* Handling "-exec +"
751 * Only one exec_argv[i] has substitution in it.
752 * Expand that one exec_argv[i] into file list.
753 */
754 if (ap->subst_count[i] == 0) {
755 *pp++ = xstrdup(arg);
756 } else {
757 int j = 0;
758 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200759 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200760 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
761 free(ap->filelist[j]);
762 j++;
763 }
764 }
765 } else
766# endif
767 {
768 /* Handling "-exec ;" */
769 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
770 }
771 }
772 *pp = NULL; /* terminate the list */
773
774# if ENABLE_FEATURE_FIND_EXEC_PLUS
775 if (ap->filelist) {
776 ap->filelist[0] = NULL;
777 ap->filelist_idx = 0;
778 ap->file_len = 0;
779 }
780# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000781
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000782 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000783 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000784 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000785
786 i = 0;
787 while (argv[i])
788 free(argv[i++]);
789 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000790}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200791ACTF(exec)
792{
793# if ENABLE_FEATURE_FIND_EXEC_PLUS
794 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200795 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200796
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200797 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
798 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200799 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
800 /* If we have lots of files already, exec the command */
801 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200802 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200803 rc = do_exec(ap, NULL);
804 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200805 }
806# endif
807 return do_exec(ap, fileName);
808}
809# if ENABLE_FEATURE_FIND_EXEC_PLUS
810static int flush_exec_plus(void)
811{
812 action *ap;
813 action **app;
814 action ***appp = G.actions;
815 while ((app = *appp++) != NULL) {
816 while ((ap = *app++) != NULL) {
817 if (ap->f == (action_fp)func_exec) {
818 action_exec *ae = (void*)ap;
819 if (ae->filelist_idx != 0) {
820 int rc = do_exec(ae, NULL);
821# if ENABLE_FEATURE_FIND_NOT
822 if (ap->invert) rc = !rc;
823# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200824 if (rc == 0)
825 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200826 }
827 }
828 }
829 }
830 return 0;
831}
832# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000833#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000834#if ENABLE_FEATURE_FIND_USER
835ACTF(user)
836{
837 return (statbuf->st_uid == ap->uid);
838}
839#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000840#if ENABLE_FEATURE_FIND_GROUP
841ACTF(group)
842{
843 return (statbuf->st_gid == ap->gid);
844}
845#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000846#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000847ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000848{
849 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000850 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000851}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000852#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000853ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000854{
855 puts(fileName);
856 return TRUE;
857}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000858#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000859ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000860{
861 return exec_actions(ap->subexpr, fileName, statbuf);
862}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000863#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000864#if ENABLE_FEATURE_FIND_SIZE
865ACTF(size)
866{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000867 if (ap->size_char == '+')
868 return statbuf->st_size > ap->size;
869 if (ap->size_char == '-')
870 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000871 return statbuf->st_size == ap->size;
872}
873#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000874#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000875/*
876 * -prune: if -depth is not given, return true and do not descend
877 * current dir; if -depth is given, return false with no effect.
878 * Example:
879 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
880 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000881ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000882{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000883 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000884}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000885#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200886#if ENABLE_FEATURE_FIND_QUIT
887ACTF(quit)
888{
889 exit(G.exitstatus);
890}
891#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000892#if ENABLE_FEATURE_FIND_DELETE
893ACTF(delete)
894{
895 int rc;
896 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8c0708a2015-10-07 21:01:47 +0200897 /* "find . -delete" skips rmdir(".") */
898 rc = 0;
899 if (NOT_LONE_CHAR(fileName, '.'))
900 rc = rmdir(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000901 } else {
902 rc = unlink(fileName);
903 }
904 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000905 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000906 return TRUE;
907}
908#endif
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200909#if ENABLE_FEATURE_FIND_EMPTY
910ACTF(empty)
911{
912 if (S_ISDIR(statbuf->st_mode)) {
913 DIR *dir;
914 struct dirent *dent;
915
916 dir = opendir(fileName);
917 if (!dir) {
918 bb_simple_perror_msg(fileName);
919 return FALSE;
920 }
921
922 while ((dent = readdir(dir)) != NULL
923 && DOT_OR_DOTDOT(dent->d_name)
924 ) {
925 continue;
926 }
927 closedir(dir);
928 return dent == NULL;
929 }
930 return S_ISREG(statbuf->st_mode) && statbuf->st_size == 0;
931}
932#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000933#if ENABLE_FEATURE_FIND_CONTEXT
934ACTF(context)
935{
936 security_context_t con;
937 int rc;
938
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200939 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000940 rc = getfilecon(fileName, &con);
941 } else {
942 rc = lgetfilecon(fileName, &con);
943 }
944 if (rc < 0)
945 return FALSE;
946 rc = strcmp(ap->context, con);
947 freecon(con);
948 return rc == 0;
949}
950#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100951#if ENABLE_FEATURE_FIND_LINKS
952ACTF(links)
953{
954 switch(ap->links_char) {
955 case '-' : return (statbuf->st_nlink < ap->links_count);
956 case '+' : return (statbuf->st_nlink > ap->links_count);
957 default: return (statbuf->st_nlink == ap->links_count);
958 }
959}
960#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000961
Denys Vlasenko689d0652020-10-01 21:52:16 +0200962static int FAST_FUNC fileAction(
963 struct recursive_state *state IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
964 const char *fileName,
965 struct stat *statbuf)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000966{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100967 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200968 int same_fs = 1;
969
970#if ENABLE_FEATURE_FIND_XDEV
971 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
972 int i;
973 for (i = 0; i < G.xdev_count; i++) {
974 if (G.xdev_dev[i] == statbuf->st_dev)
975 goto found;
976 }
977 //bb_error_msg("'%s': not same fs", fileName);
978 same_fs = 0;
979 found: ;
980 }
981#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000982
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100983#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko689d0652020-10-01 21:52:16 +0200984 if (state->depth < G.minmaxdepth[0]) {
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200985 if (same_fs)
986 return TRUE; /* skip this, continue recursing */
987 return SKIP; /* stop recursing */
988 }
Denys Vlasenko689d0652020-10-01 21:52:16 +0200989 if (state->depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100990 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000991#endif
992
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100993 r = exec_actions(G.actions, fileName, statbuf);
994 /* Had no explicit -print[0] or -exec? then print */
995 if ((r & TRUE) && G.need_print)
996 puts(fileName);
997
998#if ENABLE_FEATURE_FIND_MAXDEPTH
999 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko689d0652020-10-01 21:52:16 +02001000 if (state->depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001001 return SKIP;
1002 }
1003#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001004 /* -xdev stops on mountpoints, but AFTER mountpoit itself
1005 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +02001006 if (!same_fs) {
1007 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001008 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +02001009
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +00001010 /* Cannot return 0: our caller, recursive_action(),
1011 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001012 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001013}
1014
Eric Andersen17d49ef1999-10-06 20:25:32 +00001015
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001016#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +00001017static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +00001018{
1019 int mask = 0;
1020
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001021 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001022 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001023 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001024 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001025 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001026 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001027 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001028 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001029 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001030 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001031 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001032 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001033 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001034 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +00001035
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001036 if (mask == 0 || type[1] != '\0')
Denys Vlasenko0f296a32015-10-14 13:21:01 +02001037 bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +00001038
1039 return mask;
1040}
1041#endif
1042
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +00001043#if ENABLE_FEATURE_FIND_PERM \
1044 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001045 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +00001046static const char* plus_minus_num(const char* str)
1047{
1048 if (*str == '-' || *str == '+')
1049 str++;
1050 return str;
1051}
Denis Vlasenko4c978632007-02-03 03:31:13 +00001052#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +00001053
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001054/* Say no to GCCism */
1055#define USE_NESTED_FUNCTION 0
1056
1057#if !USE_NESTED_FUNCTION
1058struct pp_locals {
1059 action*** appp;
1060 unsigned cur_group;
1061 unsigned cur_action;
1062 IF_FEATURE_FIND_NOT( bool invert_flag; )
1063};
1064static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
1065{
1066 action *ap = xzalloc(sizeof_struct);
1067 action **app;
1068 action ***group = &ppl->appp[ppl->cur_group];
1069 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
1070 app[ppl->cur_action++] = ap;
1071 app[ppl->cur_action] = NULL;
1072 ap->f = f;
1073 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
1074 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
1075 return ap;
1076}
1077#endif
1078
Denis Vlasenko769d1e02007-01-22 23:04:27 +00001079static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +00001080{
Denis Vlasenko56fce002007-04-04 23:41:35 +00001081 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001082 OPT_FOLLOW ,
1083 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
1084 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001085 PARM_a ,
1086 PARM_o ,
1087 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001088#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001089 PARM_and ,
1090 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001091 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001092#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001093 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001094 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001095 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001096 IF_FEATURE_FIND_QUIT( PARM_quit ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001097 IF_FEATURE_FIND_DELETE( PARM_delete ,)
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001098 IF_FEATURE_FIND_EMPTY( PARM_empty ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001099 IF_FEATURE_FIND_EXEC( PARM_exec ,)
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001100 IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001101 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001102 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001103 PARM_name ,
1104 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001105 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001106#if ENABLE_DESKTOP
1107 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +02001108 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001109 IF_FEATURE_FIND_PATH( PARM_wholename ,)
1110#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001111 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001112 IF_FEATURE_FIND_REGEX( PARM_regex ,)
1113 IF_FEATURE_FIND_TYPE( PARM_type ,)
1114 IF_FEATURE_FIND_PERM( PARM_perm ,)
1115 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
Ismael Luceno04ad6832021-10-09 21:40:53 +02001116 IF_FEATURE_FIND_ATIME( PARM_atime ,)
1117 IF_FEATURE_FIND_CTIME( PARM_ctime ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001118 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
Ismael Luceno04ad6832021-10-09 21:40:53 +02001119 IF_FEATURE_FIND_AMIN( PARM_amin ,)
1120 IF_FEATURE_FIND_CMIN( PARM_cmin ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001121 IF_FEATURE_FIND_NEWER( PARM_newer ,)
1122 IF_FEATURE_FIND_INUM( PARM_inum ,)
1123 IF_FEATURE_FIND_USER( PARM_user ,)
1124 IF_FEATURE_FIND_GROUP( PARM_group ,)
1125 IF_FEATURE_FIND_SIZE( PARM_size ,)
1126 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001127 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001128 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001129 };
1130
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001131 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001132 "-follow\0"
1133 IF_FEATURE_FIND_XDEV( "-xdev\0" )
1134 IF_FEATURE_FIND_DEPTH( "-depth\0" )
1135 "-a\0"
1136 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001137 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001138#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001139 "-and\0"
1140 "-or\0"
1141 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001142#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001143 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001144 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001145 IF_FEATURE_FIND_PRUNE( "-prune\0" )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001146 IF_FEATURE_FIND_QUIT( "-quit\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001147 IF_FEATURE_FIND_DELETE( "-delete\0" )
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001148 IF_FEATURE_FIND_EMPTY( "-empty\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001149 IF_FEATURE_FIND_EXEC( "-exec\0" )
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001150 IF_FEATURE_FIND_EXECUTABLE("-executable\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001151 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001152 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +01001153 "-name\0"
1154 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001155 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001156#if ENABLE_DESKTOP
1157 IF_FEATURE_FIND_PATH( "-wholename\0")
1158#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001159 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001160 IF_FEATURE_FIND_REGEX( "-regex\0" )
1161 IF_FEATURE_FIND_TYPE( "-type\0" )
1162 IF_FEATURE_FIND_PERM( "-perm\0" )
1163 IF_FEATURE_FIND_MTIME( "-mtime\0" )
Ismael Luceno04ad6832021-10-09 21:40:53 +02001164 IF_FEATURE_FIND_ATIME( "-atime\0" )
1165 IF_FEATURE_FIND_CTIME( "-ctime\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001166 IF_FEATURE_FIND_MMIN( "-mmin\0" )
Ismael Luceno04ad6832021-10-09 21:40:53 +02001167 IF_FEATURE_FIND_AMIN( "-amin\0" )
1168 IF_FEATURE_FIND_CMIN( "-cmin\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001169 IF_FEATURE_FIND_NEWER( "-newer\0" )
1170 IF_FEATURE_FIND_INUM( "-inum\0" )
1171 IF_FEATURE_FIND_USER( "-user\0" )
1172 IF_FEATURE_FIND_GROUP( "-group\0" )
1173 IF_FEATURE_FIND_SIZE( "-size\0" )
1174 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001175 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001176 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001177 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001178
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001179#if !USE_NESTED_FUNCTION
1180 struct pp_locals ppl;
1181#define appp (ppl.appp )
1182#define cur_group (ppl.cur_group )
1183#define cur_action (ppl.cur_action )
1184#define invert_flag (ppl.invert_flag)
1185#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1186#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001187 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001188 unsigned cur_group;
1189 unsigned cur_action;
1190 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001191
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001192 /* This is the only place in busybox where we use nested function.
1193 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001194 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001195 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001196 action* alloc_action(int sizeof_struct, action_fp f)
1197 {
1198 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001199 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001200 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001201 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001202 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001203 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1204 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001205 return ap;
1206 }
1207#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001208#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001209
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001210 cur_group = 0;
1211 cur_action = 0;
1212 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001213 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001214
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001215 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001216 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001217 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001218 const char *arg1 = argv[1];
1219
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001220 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1221
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001222 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001223 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001224 if (!arg1)
1225 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1226 argv++;
1227 }
1228
1229 /* We can use big switch() here, but on i386
1230 * it doesn't give smaller code. Other arches? */
1231
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001232/* Options always return true. They always take effect
1233 * rather than being processed only when their place in the
1234 * expression is reached.
1235 */
1236 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001237 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001238 dbg("follow enabled: %d", __LINE__);
1239 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001240 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001241#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001242 else if (parm == OPT_XDEV) {
1243 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001244 G.xdev_on = 1;
1245 }
1246#endif
1247#if ENABLE_FEATURE_FIND_MAXDEPTH
1248 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001249 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001250 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1251 }
1252#endif
1253#if ENABLE_FEATURE_FIND_DEPTH
1254 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001255 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001256 G.recurse_flags |= ACTION_DEPTHFIRST;
1257 }
1258#endif
1259/* Actions are grouped by operators
1260 * ( expr ) Force precedence
1261 * ! expr True if expr is false
1262 * -not expr Same as ! expr
1263 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1264 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1265 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1266 * We implement: (), -a, -o
1267 */
1268 /* Operators */
1269 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001270 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001271 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001272 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001273 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001274 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001275 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001276 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001277 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001278 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001279 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001280 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001281 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001282#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001283 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001284 /* also handles "find ! ! -name 'foo*'" */
1285 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001286 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001287 }
1288#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001289 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001290 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001291 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001292 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001293 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001294 }
1295#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001296 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001297 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001298 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001299 (void) ALLOC_ACTION(print0);
1300 }
Paul Foxd7384292006-05-12 14:47:20 +00001301#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001302#if ENABLE_FEATURE_FIND_PRUNE
1303 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001304 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001305 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001306 }
1307#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001308#if ENABLE_FEATURE_FIND_QUIT
1309 else if (parm == PARM_quit) {
1310 dbg("%d", __LINE__);
1311 (void) ALLOC_ACTION(quit);
1312 }
1313#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001314#if ENABLE_FEATURE_FIND_DELETE
1315 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001316 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001317 G.need_print = 0;
1318 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001319 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001320 }
Rob Landleycee605c2005-10-06 16:39:17 +00001321#endif
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001322#if ENABLE_FEATURE_FIND_EMPTY
1323 else if (parm == PARM_empty) {
1324 dbg("%d", __LINE__);
1325 (void) ALLOC_ACTION(empty);
1326 }
1327#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001328#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001329 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001330 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001331 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001332 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001333 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001334 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001335 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001336 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001337 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001338 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001339 if (!*argv) /* did not see ';' or '+' until end */
1340 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001341 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001342 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001343 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001344 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001345 if ((argv[0][0] == ';' || argv[0][0] == '+')
1346 && argv[0][1] == '\0'
1347 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001348# if ENABLE_FEATURE_FIND_EXEC_PLUS
1349 if (argv[0][0] == '+')
1350 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1351# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001352 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001353 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001354 argv++;
1355 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001356 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001357 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001358 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001359 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001360 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001361 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001362 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001363 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1364 }
1365# if ENABLE_FEATURE_FIND_EXEC_PLUS
1366 /*
1367 * coreutils expects {} to appear only once in "-exec +"
1368 */
1369 if (all_subst != 1 && ap->filelist)
James Byrne69374872019-07-02 11:35:03 +02001370 bb_simple_error_msg_and_die("only one '{}' allowed for -exec +");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001371# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001372 }
1373#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001374#if ENABLE_FEATURE_FIND_PAREN
1375 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001376 action_paren *ap;
1377 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001378 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001379
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001380 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001381 endarg = argv;
1382 while (1) {
1383 if (!*++endarg)
James Byrne69374872019-07-02 11:35:03 +02001384 bb_simple_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001385 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001386 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001387 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001388 *endarg = NULL;
1389 break;
1390 }
1391 }
1392 ap = ALLOC_ACTION(paren);
1393 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001394 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001395 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001396 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001397#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001398 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001399 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001400 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001401 ap = ALLOC_ACTION(name);
1402 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001403 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001404 }
1405#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001406 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001407 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001408 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001409 ap = ALLOC_ACTION(path);
1410 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001411 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001412 }
1413#endif
1414#if ENABLE_FEATURE_FIND_REGEX
1415 else if (parm == PARM_regex) {
1416 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001417 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001418 ap = ALLOC_ACTION(regex);
1419 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1420 }
1421#endif
1422#if ENABLE_FEATURE_FIND_TYPE
1423 else if (parm == PARM_type) {
1424 action_type *ap;
1425 ap = ALLOC_ACTION(type);
1426 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001427 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001428 }
1429#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001430#if ENABLE_FEATURE_FIND_EXECUTABLE
1431 else if (parm == PARM_executable) {
1432 (void) ALLOC_ACTION(executable);
1433 }
1434#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001435#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001436/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001437 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001438 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001439 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001440 */
1441 else if (parm == PARM_perm) {
1442 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001443 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001444 ap = ALLOC_ACTION(perm);
1445 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001446 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001447 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +02001448 ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask);
1449 if (ap->perm_mask == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +01001450 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001451 }
1452#endif
1453#if ENABLE_FEATURE_FIND_MTIME
Ismael Luceno04ad6832021-10-09 21:40:53 +02001454 else if (parm == PARM_mtime
1455# if ENABLE_FEATURE_FIND_ATIME
1456 || parm == PARM_atime
1457# endif
1458# if ENABLE_FEATURE_FIND_CTIME
1459 || parm == PARM_ctime
1460# endif
1461 ) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001462 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001463 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001464 ap = ALLOC_ACTION(mtime);
Ismael Luceno04ad6832021-10-09 21:40:53 +02001465# if ENABLE_FEATURE_FIND_AMIN || ENABLE_FEATURE_FIND_CMIN
1466 ap->time_type = arg[1];
1467# endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001468 ap->mtime_char = arg1[0];
1469 ap->mtime_days = xatoul(plus_minus_num(arg1));
1470 }
1471#endif
1472#if ENABLE_FEATURE_FIND_MMIN
Ismael Luceno04ad6832021-10-09 21:40:53 +02001473 else if (parm == PARM_mmin
1474# if ENABLE_FEATURE_FIND_AMIN
1475 || parm == PARM_amin
1476# endif
1477# if ENABLE_FEATURE_FIND_CMIN
1478 || parm == PARM_cmin
1479# endif
1480 ) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001481 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001482 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001483 ap = ALLOC_ACTION(mmin);
Ismael Luceno04ad6832021-10-09 21:40:53 +02001484# if ENABLE_FEATURE_FIND_AMIN || ENABLE_FEATURE_FIND_CMIN
1485 ap->time_type = arg[1];
1486# endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001487 ap->mmin_char = arg1[0];
1488 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1489 }
1490#endif
1491#if ENABLE_FEATURE_FIND_NEWER
1492 else if (parm == PARM_newer) {
1493 struct stat stat_newer;
1494 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001495 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001496 ap = ALLOC_ACTION(newer);
1497 xstat(arg1, &stat_newer);
1498 ap->newer_mtime = stat_newer.st_mtime;
1499 }
1500#endif
1501#if ENABLE_FEATURE_FIND_INUM
1502 else if (parm == PARM_inum) {
1503 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001504 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001505 ap = ALLOC_ACTION(inum);
1506 ap->inode_num = xatoul(arg1);
1507 }
1508#endif
1509#if ENABLE_FEATURE_FIND_USER
1510 else if (parm == PARM_user) {
1511 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001512 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001513 ap = ALLOC_ACTION(user);
1514 ap->uid = bb_strtou(arg1, NULL, 10);
1515 if (errno)
1516 ap->uid = xuname2uid(arg1);
1517 }
1518#endif
1519#if ENABLE_FEATURE_FIND_GROUP
1520 else if (parm == PARM_group) {
1521 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001522 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001523 ap = ALLOC_ACTION(group);
1524 ap->gid = bb_strtou(arg1, NULL, 10);
1525 if (errno)
1526 ap->gid = xgroup2gid(arg1);
1527 }
1528#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001529#if ENABLE_FEATURE_FIND_SIZE
1530 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001531/* -size n[bckw]: file uses n units of space
1532 * b (default): units are 512-byte blocks
1533 * c: 1 byte
1534 * k: kilobytes
1535 * w: 2-byte words
1536 */
1537#if ENABLE_LFS
1538#define XATOU_SFX xatoull_sfx
1539#else
1540#define XATOU_SFX xatoul_sfx
1541#endif
Denys Vlasenko965b7952020-11-30 13:03:03 +01001542 static const struct suffix_mult find_suffixes[] ALIGN_SUFFIX = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001543 { "c", 1 },
1544 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001545 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001546 { "b", 512 },
1547 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001548 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001549 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001550 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001551 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001552 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001553 ap->size_char = arg1[0];
1554 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001555 }
1556#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001557#if ENABLE_FEATURE_FIND_CONTEXT
1558 else if (parm == PARM_context) {
1559 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001560 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001561 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001562 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001563 /* SELinux headers erroneously declare non-const parameter */
1564 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001565 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001566 }
1567#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001568#if ENABLE_FEATURE_FIND_LINKS
1569 else if (parm == PARM_links) {
1570 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001571 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001572 ap = ALLOC_ACTION(links);
1573 ap->links_char = arg1[0];
1574 ap->links_count = xatoul(plus_minus_num(arg1));
1575 }
1576#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001577 else {
1578 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001579 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001580 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001581 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001582 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001583 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001584 return appp;
1585#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001586#undef appp
1587#undef cur_action
1588#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001589}
1590
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001591int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001592int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001593{
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001594 int i, firstopt;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001595 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001596
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001597 INIT_G();
1598
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001599 /* "find -type f" + getopt("+HLP") => disaster.
1600 * Need to avoid getopt running into a non-HLP option.
1601 * Do this by temporarily storing NULL there:
1602 */
1603 past_HLP = argv;
1604 for (;;) {
1605 saved = *++past_HLP;
1606 if (!saved)
1607 break;
1608 if (saved[0] != '-')
1609 break;
1610 if (!saved[1])
1611 break; /* it is "-" */
Denys Vlasenko2d9e0972019-01-06 19:58:18 +01001612 if (saved[1] == '-' && !saved[2]) {
1613 /* it is "--" */
1614 /* Try: find -- /dev/null */
1615 saved = *++past_HLP;
1616 break;
1617 }
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001618 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1619 break;
1620 }
1621 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001622 /* "+": stop on first non-option */
Denys Vlasenkoef2366c2020-12-12 08:44:32 +01001623 i = getopt32(argv, "+""HLP");
Denys Vlasenko6782f482013-05-14 17:20:35 +02001624 if (i & (1<<0))
1625 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1626 if (i & (1<<1))
1627 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1628 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001629 argv = past_HLP; /* same result as "argv += optind;" */
1630 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001631
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001632 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001633 if (argv[firstopt][0] == '-')
1634 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001635 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1636 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001637 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001638 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001639 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001640 if (firstopt == 0) {
1641 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001642 firstopt++;
1643 }
1644
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001645 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001646 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001647
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001648#if ENABLE_FEATURE_FIND_XDEV
1649 if (G.xdev_on) {
1650 struct stat stbuf;
1651
1652 G.xdev_count = firstopt;
1653 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1654 for (i = 0; argv[i]; i++) {
1655 /* not xstat(): shouldn't bomb out on
1656 * "find not_exist exist -xdev" */
1657 if (stat(argv[i], &stbuf) == 0)
1658 G.xdev_dev[i] = stbuf.st_dev;
1659 /* else G.xdev_dev[i] stays 0 and
1660 * won't match any real device dev_t
1661 */
1662 }
1663 }
1664#endif
1665
1666 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001667 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001668 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001669 fileAction, /* file action */
1670 fileAction, /* dir action */
Denys Vlasenko689d0652020-10-01 21:52:16 +02001671 NULL) /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001672 ) {
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001673 G.exitstatus |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001674 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001675 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001676
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001677 IF_FEATURE_FIND_EXEC_PLUS(G.exitstatus |= flush_exec_plus();)
1678 return G.exitstatus;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001679}