blob: bb6ad31e5edcba2cbfc02237fcb000eabcc1de47 [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:
Aaro Koskinenaaade692021-11-28 12:11:48 +0200158//config:config FEATURE_FIND_SAMEFILE
159//config: bool "Enable -samefile: reference file matching"
160//config: default y
161//config: depends on FIND
162//config: help
163//config: Support the 'find -samefile' option for searching by a reference file.
164//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200165//config:config FEATURE_FIND_EXEC
166//config: bool "Enable -exec: execute commands"
167//config: default y
168//config: depends on FIND
169//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200170//config: Support the 'find -exec' option for executing commands based upon
171//config: the files matched.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200172//config:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200173//config:config FEATURE_FIND_EXEC_PLUS
174//config: bool "Enable -exec ... {} +"
175//config: default y
176//config: depends on FEATURE_FIND_EXEC
177//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200178//config: Support the 'find -exec ... {} +' option for executing commands
179//config: for all matched files at once.
180//config: Without this option, -exec + is a synonym for -exec ;
181//config: (IOW: it works correctly, but without expected speedup)
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200182//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200183//config:config FEATURE_FIND_USER
184//config: bool "Enable -user: username/uid matching"
185//config: default y
186//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200187//config:
188//config:config FEATURE_FIND_GROUP
189//config: bool "Enable -group: group/gid matching"
190//config: default y
191//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200192//config:
193//config:config FEATURE_FIND_NOT
194//config: bool "Enable the 'not' (!) operator"
195//config: default y
196//config: depends on FIND
197//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200198//config: Support the '!' operator to invert the test results.
199//config: If 'Enable full-blown desktop' is enabled, then will also support
200//config: the non-POSIX notation '-not'.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200201//config:
202//config:config FEATURE_FIND_DEPTH
203//config: bool "Enable -depth"
204//config: default y
205//config: depends on FIND
206//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200207//config: Process each directory's contents before the directory itself.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200208//config:
209//config:config FEATURE_FIND_PAREN
210//config: bool "Enable parens in options"
211//config: default y
212//config: depends on FIND
213//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200214//config: Enable usage of parens '(' to specify logical order of arguments.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200215//config:
216//config:config FEATURE_FIND_SIZE
217//config: bool "Enable -size: file size matching"
218//config: default y
219//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200220//config:
221//config:config FEATURE_FIND_PRUNE
222//config: bool "Enable -prune: exclude subdirectories"
223//config: default y
224//config: depends on FIND
225//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200226//config: If the file is a directory, don't descend into it. Useful for
227//config: exclusion .svn and CVS directories.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200228//config:
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200229//config:config FEATURE_FIND_QUIT
230//config: bool "Enable -quit: exit"
231//config: default y
232//config: depends on FIND
233//config: help
234//config: If this action is reached, 'find' exits.
235//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200236//config:config FEATURE_FIND_DELETE
237//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200238//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200239//config: depends on FIND && FEATURE_FIND_DEPTH
240//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200241//config: Support the 'find -delete' option for deleting files and directories.
242//config: WARNING: This option can do much harm if used wrong. Busybox will not
243//config: try to protect the user from doing stupid things. Use with care.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200244//config:
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200245//config:config FEATURE_FIND_EMPTY
246//config: bool "Enable -empty: match empty files or directories"
247//config: default y
248//config: depends on FIND
249//config: help
250//config: Support the 'find -empty' option to find empty regular files
251//config: or directories.
252//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200253//config:config FEATURE_FIND_PATH
254//config: bool "Enable -path: match pathname with shell pattern"
255//config: default y
256//config: depends on FIND
257//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200258//config: The -path option matches whole pathname instead of just filename.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200259//config:
260//config:config FEATURE_FIND_REGEX
261//config: bool "Enable -regex: match pathname with regex"
262//config: default y
263//config: depends on FIND
264//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200265//config: The -regex option matches whole pathname against regular expression.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200266//config:
267//config:config FEATURE_FIND_CONTEXT
268//config: bool "Enable -context: security context matching"
269//config: default n
270//config: depends on FIND && SELINUX
271//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200272//config: Support the 'find -context' option for matching security context.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200273//config:
274//config:config FEATURE_FIND_LINKS
275//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200276//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200277//config: depends on FIND
278//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200279//config: Support the 'find -links' option for matching number of links.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200280
Denys Vlasenko805f2452011-02-12 20:26:14 +0100281//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
282
283//kbuild:lib-$(CONFIG_FIND) += find.o
284
285//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200286//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100287//usage:#define find_full_usage "\n\n"
288//usage: "Search for files and perform actions on them.\n"
289//usage: "First failed action stops processing of current file.\n"
290//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200291//usage: "\n -L,-follow Follow symlinks"
292//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100293//usage: IF_FEATURE_FIND_XDEV(
294//usage: "\n -xdev Don't descend directories on other filesystems"
295//usage: )
296//usage: IF_FEATURE_FIND_MAXDEPTH(
297//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
298//usage: "\n actions to command line arguments only"
299//usage: "\n -mindepth N Don't act on first N levels"
300//usage: )
301//usage: IF_FEATURE_FIND_DEPTH(
302//usage: "\n -depth Act on directory *after* traversing it"
303//usage: )
304//usage: "\n"
305//usage: "\nActions:"
306//usage: IF_FEATURE_FIND_PAREN(
307//usage: "\n ( ACTIONS ) Group actions for -o / -a"
308//usage: )
309//usage: IF_FEATURE_FIND_NOT(
310//usage: "\n ! ACT Invert ACT's success/failure"
311//usage: )
312//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
313//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
314//usage: "\n Note: -a has higher priority than -o"
315//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
316//usage: "\n -iname PATTERN Case insensitive -name"
317//usage: IF_FEATURE_FIND_PATH(
318//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200319//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100320//usage: )
321//usage: IF_FEATURE_FIND_REGEX(
322//usage: "\n -regex PATTERN Match path to regex PATTERN"
323//usage: )
324//usage: IF_FEATURE_FIND_TYPE(
Denys Vlasenkof4eaccb2018-04-24 10:13:45 +0200325//usage: "\n -type X File type is X (one of: f,d,l,b,c,s,p)"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100326//usage: )
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200327//usage: IF_FEATURE_FIND_EXECUTABLE(
328//usage: "\n -executable File is executable"
329//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100330//usage: IF_FEATURE_FIND_PERM(
331//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100332//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100333//usage: )
334//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100335//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
336//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100337//usage: )
Ismael Luceno04ad6832021-10-09 21:40:53 +0200338//usage: IF_FEATURE_FIND_ATIME(
339//usage: "\n -atime DAYS atime +N/-N/N days in the past"
340//usage: )
341//usage: IF_FEATURE_FIND_CTIME(
342//usage: "\n -ctime DAYS ctime +N/-N/N days in the past"
343//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100344//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100345//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
346//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100347//usage: )
Ismael Luceno04ad6832021-10-09 21:40:53 +0200348//usage: IF_FEATURE_FIND_AMIN(
349//usage: "\n -amin MINS atime +N/-N/N minutes in the past"
350//usage: )
351//usage: IF_FEATURE_FIND_CMIN(
352//usage: "\n -cmin MINS ctime +N/-N/N minutes in the past"
353//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100354//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100355//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100356//usage: )
357//usage: IF_FEATURE_FIND_INUM(
358//usage: "\n -inum N File has inode number N"
359//usage: )
Aaro Koskinenaaade692021-11-28 12:11:48 +0200360//usage: IF_FEATURE_FIND_SAMEFILE(
361//usage: "\n -samefile FILE File is same as FILE"
362//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100363//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100364//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100365//usage: )
366//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100367//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100368//usage: )
369//usage: IF_FEATURE_FIND_SIZE(
370//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
371//usage: "\n +/-N: file size is bigger/smaller than N"
372//usage: )
373//usage: IF_FEATURE_FIND_LINKS(
374//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
375//usage: "\n or exactly N"
376//usage: )
377//usage: IF_FEATURE_FIND_CONTEXT(
378//usage: "\n -context CTX File has specified security context"
379//usage: )
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200380//usage: IF_FEATURE_FIND_EMPTY(
381//usage: "\n -empty Match empty file/directory"
382//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100383//usage: IF_FEATURE_FIND_PRUNE(
384//usage: "\n -prune If current file is directory, don't descend into it"
385//usage: )
386//usage: "\nIf none of the following actions is specified, -print is assumed"
387//usage: "\n -print Print file name"
388//usage: IF_FEATURE_FIND_PRINT0(
389//usage: "\n -print0 Print file name, NUL terminated"
390//usage: )
391//usage: IF_FEATURE_FIND_EXEC(
392//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
393//usage: "\n file name. Fails if CMD exits with nonzero"
394//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200395//usage: IF_FEATURE_FIND_EXEC_PLUS(
396//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
397//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100398//usage: IF_FEATURE_FIND_DELETE(
399//usage: "\n -delete Delete current file/directory. Turns on -depth option"
400//usage: )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200401//usage: IF_FEATURE_FIND_QUIT(
402//usage: "\n -quit Exit"
403//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100404//usage:
405//usage:#define find_example_usage
406//usage: "$ find / -name passwd\n"
407//usage: "/etc/passwd\n"
408
Matt Kraai096370d2001-02-07 03:52:38 +0000409#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000410#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200411#include "common_bufsiz.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000412#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200413# include "xregex.h"
414#endif
415/* GNUism: */
416#ifndef FNM_CASEFOLD
417# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000418#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000419
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200420#if 1
421# define dbg(...) ((void)0)
422#else
423# define dbg(...) bb_error_msg(__VA_ARGS__)
424#endif
425
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200426
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000427/* This is a NOEXEC applet. Be very careful! */
428
429
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200430typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000431
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000432typedef struct {
433 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000434#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000435 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000436#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000437} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200438
Denys Vlasenko606291b2009-09-23 23:15:43 +0200439#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200440#define ACTF(name) \
441 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200442 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200443 action_##name* ap UNUSED_PARAM)
444
445 ACTS(print)
446 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200447IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000448IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
449IF_FEATURE_FIND_PRINT0( ACTS(print0))
450IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200451IF_FEATURE_FIND_EXECUTABLE(ACTS(executable))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000452IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200453IF_FEATURE_FIND_MTIME( ACTS(mtime, unsigned char time_type; unsigned char mtime_char; unsigned mtime_days;))
454IF_FEATURE_FIND_MMIN( ACTS(mmin, unsigned char time_type; unsigned char mmin_char; unsigned mmin_mins;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000455IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
456IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
Aaro Koskinenaaade692021-11-28 12:11:48 +0200457IF_FEATURE_FIND_SAMEFILE(ACTS(samefile, ino_t inode_num; dev_t device;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000458IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
459IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
460IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
461IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
462IF_FEATURE_FIND_PRUNE( ACTS(prune))
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200463IF_FEATURE_FIND_QUIT( ACTS(quit))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000464IF_FEATURE_FIND_DELETE( ACTS(delete))
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200465IF_FEATURE_FIND_EMPTY( ACTS(empty))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200466IF_FEATURE_FIND_EXEC( ACTS(exec,
467 char **exec_argv; /* -exec ARGS */
468 unsigned *subst_count;
469 int exec_argc; /* count of ARGS */
470 IF_FEATURE_FIND_EXEC_PLUS(
471 /*
472 * filelist is NULL if "exec ;"
473 * non-NULL if "exec +"
474 */
475 char **filelist;
476 int filelist_idx;
477 int file_len;
478 )
479 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000480IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100481IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000482
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200483struct globals {
484 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
485 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100486#if ENABLE_FEATURE_FIND_MAXDEPTH
487 int minmaxdepth[2];
488#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200489 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100490 smallint need_print;
491 smallint xdev_on;
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200492 smalluint exitstatus;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200493 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200494 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100495} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200496#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200497#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200498 setup_common_bufsiz(); \
Denys Vlasenko7b85ec32015-10-13 17:17:34 +0200499 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200500 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100501 memset(&G, 0, sizeof(G)); \
502 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200503 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200504 G.need_print = 1; \
505 G.recurse_flags = ACTION_RECURSE; \
506} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000507
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000508/* Return values of ACTFs ('action functions') are a bit mask:
509 * bit 1=1: prune (use SKIP constant for setting it)
510 * bit 0=1: matched successfully (TRUE)
511 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000512
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200513static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000514{
515 int cur_group;
516 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000517 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000518 action **app, *ap;
519
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000520 /* "action group" is a set of actions ANDed together.
521 * groups are ORed together.
522 * We simply evaluate each group until we find one in which all actions
523 * succeed. */
524
525 /* -prune is special: if it is encountered, then we won't
526 * descend into current directory. It doesn't matter whether
527 * action group (in which -prune sits) will succeed or not:
528 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
529 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
530 * not starting with 'f' */
531
532 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
533 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
534 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
535 * On return, bit is restored. */
536
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000537 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100538 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000539 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000540 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000541 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000542 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000543 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000544 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000545 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000546#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000547 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000548#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200549 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000550 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000551 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000552 }
553 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200554 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000555 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000556}
557
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200558#if !FNM_CASEFOLD
559static char *strcpy_upcase(char *dst, const char *src)
560{
561 char *d = dst;
562 while (1) {
563 unsigned char ch = *src++;
564 if (ch >= 'a' && ch <= 'z')
565 ch -= ('a' - 'A');
566 *d++ = ch;
567 if (ch == '\0')
568 break;
569 }
570 return dst;
571}
572#endif
573
Denis Vlasenko92258542006-11-01 10:25:35 +0000574ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000575{
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100576 int r;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000577 const char *tmp = bb_basename(fileName);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100578 /* GNU findutils: find DIR/ -name DIR
579 * prints "DIR/" (DIR// prints "DIR//" etc).
580 * Need to strip trailing "/".
581 * Such names can come only from top-level names, but
582 * we can't do this before recursive_action() call,
583 * since then "find FILE/ -name FILE"
584 * would also work (on non-directories), which is wrong.
585 */
586 char *trunc_slash = NULL;
587
588 if (*tmp == '\0') {
589 /* "foo/bar/[//...]" */
590 while (tmp != fileName && tmp[-1] == '/')
591 tmp--;
592 if (tmp == fileName) { /* entire fileName is "//.."? */
593 /* yes, convert "//..." to "/"
594 * Testcases:
595 * find / -maxdepth 1 -name /: prints /
596 * find // -maxdepth 1 -name /: prints //
597 * find / -maxdepth 1 -name //: prints nothing
598 * find // -maxdepth 1 -name //: prints nothing
599 */
600 if (tmp[1])
601 trunc_slash = (char*)tmp + 1;
602 } else {
603 /* no, it's "foo/bar/[//...]", go back to 'b' */
604 trunc_slash = (char*)tmp;
605 while (tmp != fileName && tmp[-1] != '/')
606 tmp--;
607 }
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000608 }
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100609
Denys Vlasenko91388172010-04-26 09:54:04 +0200610 /* Was using FNM_PERIOD flag too,
611 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
612 * find -name '*foo' should match .foo too:
613 */
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100614 if (trunc_slash) *trunc_slash = '\0';
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200615#if FNM_CASEFOLD
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100616 r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0));
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200617#else
618 if (ap->iname)
619 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100620 r = fnmatch(ap->pattern, tmp, 0);
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200621#endif
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100622 if (trunc_slash) *trunc_slash = '/';
623 return r == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000624}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000625
Denis Vlasenkob9411292007-06-15 23:43:11 +0000626#if ENABLE_FEATURE_FIND_PATH
627ACTF(path)
628{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200629# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200630 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200631# else
632 if (ap->ipath)
633 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
634 return fnmatch(ap->pattern, fileName, 0) == 0;
635# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000636}
637#endif
638#if ENABLE_FEATURE_FIND_REGEX
639ACTF(regex)
640{
641 regmatch_t match;
642 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
643 return 0; /* no match */
644 if (match.rm_so)
645 return 0; /* match doesn't start at pos 0 */
646 if (fileName[match.rm_eo])
647 return 0; /* match doesn't end exactly at end of pathname */
648 return 1;
649}
650#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000651#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000652ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000653{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000654 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000655}
Rob Landleycee605c2005-10-06 16:39:17 +0000656#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200657#if ENABLE_FEATURE_FIND_EXECUTABLE
658ACTF(executable)
659{
660 return access(fileName, X_OK) == 0;
661}
662#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000663#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000664ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000665{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200666 /* -perm [+/]mode: at least one of perm_mask bits are set */
667 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000668 return (statbuf->st_mode & ap->perm_mask) != 0;
669 /* -perm -mode: all of perm_mask are set */
670 if (ap->perm_char == '-')
671 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
672 /* -perm mode: file mode must match perm_mask */
673 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000674}
Rob Landleycee605c2005-10-06 16:39:17 +0000675#endif
Ismael Luceno421c8762021-09-29 13:41:33 +0200676
Ismael Luceno04ad6832021-10-09 21:40:53 +0200677#if 0 \
678 || ENABLE_FEATURE_FIND_AMIN \
679 || ENABLE_FEATURE_FIND_ATIME \
680 || ENABLE_FEATURE_FIND_CMIN \
681 || ENABLE_FEATURE_FIND_CTIME \
682 || ENABLE_FEATURE_FIND_MMIN \
683 || ENABLE_FEATURE_FIND_MTIME
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200684static int time_cmp(const struct stat *statbuf, unsigned type_and_char, time_t N_from_user, unsigned unit)
Ismael Luceno421c8762021-09-29 13:41:33 +0200685{
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200686 time_t ftime, file_age;
687
688 ftime = statbuf->st_mtime;
689# if ENABLE_FEATURE_FIND_ATIME || ENABLE_FEATURE_FIND_CTIME
690# if ENABLE_FEATURE_FIND_ATIME
691 if ((type_and_char >> 8) == 'a')
692 ftime = statbuf->st_atime;
693# endif
694# if ENABLE_FEATURE_FIND_CTIME
695 if ((type_and_char >> 8) == 'c')
696 ftime = statbuf->st_ctime;
697# endif
698 type_and_char &= 0xff;
699# endif
700 file_age = time(NULL) - ftime;
701 N_from_user *= unit;
702 switch (type_and_char) {
703 case '+': return file_age >= N_from_user + unit;
704 case '-': return file_age < N_from_user;
Ismael Luceno421c8762021-09-29 13:41:33 +0200705 /* just numeric time */
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200706 default: return file_age >= N_from_user && file_age < N_from_user + unit;
Ismael Luceno421c8762021-09-29 13:41:33 +0200707 }
708}
709#endif
710
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000711#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000712ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000713{
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200714 return time_cmp(statbuf,
715# if ENABLE_FEATURE_FIND_ATIME || ENABLE_FEATURE_FIND_CTIME
716 (ap->time_type << 8) |
Ismael Luceno04ad6832021-10-09 21:40:53 +0200717# endif
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200718 ap->mtime_char,
719 ap->mtime_days, 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000720}
Rob Landleycee605c2005-10-06 16:39:17 +0000721#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000722#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000723ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000724{
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200725 return time_cmp(statbuf,
726# if ENABLE_FEATURE_FIND_ATIME || ENABLE_FEATURE_FIND_CTIME
727 (ap->time_type << 8) |
Ismael Luceno04ad6832021-10-09 21:40:53 +0200728# endif
Denys Vlasenkod8e39b52021-10-10 15:18:12 +0200729 ap->mmin_char,
730 ap->mmin_mins, 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000731}
Paul Fox72d1a232006-01-13 21:05:41 +0000732#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000733#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000734ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000735{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000736 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000737}
Rob Landleycee605c2005-10-06 16:39:17 +0000738#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000739#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000740ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000741{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000742 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000743}
Rob Landleycee605c2005-10-06 16:39:17 +0000744#endif
Aaro Koskinenaaade692021-11-28 12:11:48 +0200745#if ENABLE_FEATURE_FIND_SAMEFILE
746ACTF(samefile)
747{
748 return statbuf->st_ino == ap->inode_num &&
749 statbuf->st_dev == ap->device;
750}
751#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000752#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200753static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000754{
755 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200756# if ENABLE_FEATURE_FIND_EXEC_PLUS
757 int size = ap->exec_argc + ap->filelist_idx + 1;
758# else
759 int size = ap->exec_argc + 1;
760# endif
761# if ENABLE_USE_PORTABLE_CODE
762 char **argv = alloca(sizeof(char*) * size);
763# else /* gcc 4.3.1 generates smaller code: */
764 char *argv[size];
765# endif
766 char **pp = argv;
767
768 for (i = 0; i < ap->exec_argc; i++) {
769 const char *arg = ap->exec_argv[i];
770
771# if ENABLE_FEATURE_FIND_EXEC_PLUS
772 if (ap->filelist) {
773 /* Handling "-exec +"
774 * Only one exec_argv[i] has substitution in it.
775 * Expand that one exec_argv[i] into file list.
776 */
777 if (ap->subst_count[i] == 0) {
778 *pp++ = xstrdup(arg);
779 } else {
780 int j = 0;
781 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200782 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200783 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
784 free(ap->filelist[j]);
785 j++;
786 }
787 }
788 } else
789# endif
790 {
791 /* Handling "-exec ;" */
792 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
793 }
794 }
795 *pp = NULL; /* terminate the list */
796
797# if ENABLE_FEATURE_FIND_EXEC_PLUS
798 if (ap->filelist) {
799 ap->filelist[0] = NULL;
800 ap->filelist_idx = 0;
801 ap->file_len = 0;
802 }
803# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000804
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000805 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000806 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000807 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000808
809 i = 0;
810 while (argv[i])
811 free(argv[i++]);
812 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000813}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200814ACTF(exec)
815{
816# if ENABLE_FEATURE_FIND_EXEC_PLUS
817 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200818 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200819
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200820 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
821 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200822 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
823 /* If we have lots of files already, exec the command */
824 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200825 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200826 rc = do_exec(ap, NULL);
827 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200828 }
829# endif
830 return do_exec(ap, fileName);
831}
832# if ENABLE_FEATURE_FIND_EXEC_PLUS
833static int flush_exec_plus(void)
834{
835 action *ap;
836 action **app;
837 action ***appp = G.actions;
838 while ((app = *appp++) != NULL) {
839 while ((ap = *app++) != NULL) {
840 if (ap->f == (action_fp)func_exec) {
841 action_exec *ae = (void*)ap;
842 if (ae->filelist_idx != 0) {
843 int rc = do_exec(ae, NULL);
844# if ENABLE_FEATURE_FIND_NOT
845 if (ap->invert) rc = !rc;
846# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200847 if (rc == 0)
848 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200849 }
850 }
851 }
852 }
853 return 0;
854}
855# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000856#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000857#if ENABLE_FEATURE_FIND_USER
858ACTF(user)
859{
860 return (statbuf->st_uid == ap->uid);
861}
862#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000863#if ENABLE_FEATURE_FIND_GROUP
864ACTF(group)
865{
866 return (statbuf->st_gid == ap->gid);
867}
868#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000869#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000870ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000871{
872 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000873 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000874}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000875#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000876ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000877{
878 puts(fileName);
879 return TRUE;
880}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000881#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000882ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000883{
884 return exec_actions(ap->subexpr, fileName, statbuf);
885}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000886#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000887#if ENABLE_FEATURE_FIND_SIZE
888ACTF(size)
889{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000890 if (ap->size_char == '+')
891 return statbuf->st_size > ap->size;
892 if (ap->size_char == '-')
893 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000894 return statbuf->st_size == ap->size;
895}
896#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000897#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000898/*
899 * -prune: if -depth is not given, return true and do not descend
900 * current dir; if -depth is given, return false with no effect.
901 * Example:
902 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
903 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000904ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000905{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000906 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000907}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000908#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200909#if ENABLE_FEATURE_FIND_QUIT
910ACTF(quit)
911{
912 exit(G.exitstatus);
913}
914#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000915#if ENABLE_FEATURE_FIND_DELETE
916ACTF(delete)
917{
918 int rc;
919 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8c0708a2015-10-07 21:01:47 +0200920 /* "find . -delete" skips rmdir(".") */
921 rc = 0;
922 if (NOT_LONE_CHAR(fileName, '.'))
923 rc = rmdir(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000924 } else {
925 rc = unlink(fileName);
926 }
927 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000928 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000929 return TRUE;
930}
931#endif
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200932#if ENABLE_FEATURE_FIND_EMPTY
933ACTF(empty)
934{
935 if (S_ISDIR(statbuf->st_mode)) {
936 DIR *dir;
937 struct dirent *dent;
938
939 dir = opendir(fileName);
940 if (!dir) {
941 bb_simple_perror_msg(fileName);
942 return FALSE;
943 }
944
945 while ((dent = readdir(dir)) != NULL
946 && DOT_OR_DOTDOT(dent->d_name)
947 ) {
948 continue;
949 }
950 closedir(dir);
951 return dent == NULL;
952 }
953 return S_ISREG(statbuf->st_mode) && statbuf->st_size == 0;
954}
955#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000956#if ENABLE_FEATURE_FIND_CONTEXT
957ACTF(context)
958{
959 security_context_t con;
960 int rc;
961
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200962 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000963 rc = getfilecon(fileName, &con);
964 } else {
965 rc = lgetfilecon(fileName, &con);
966 }
967 if (rc < 0)
968 return FALSE;
969 rc = strcmp(ap->context, con);
970 freecon(con);
971 return rc == 0;
972}
973#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100974#if ENABLE_FEATURE_FIND_LINKS
975ACTF(links)
976{
977 switch(ap->links_char) {
978 case '-' : return (statbuf->st_nlink < ap->links_count);
979 case '+' : return (statbuf->st_nlink > ap->links_count);
980 default: return (statbuf->st_nlink == ap->links_count);
981 }
982}
983#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000984
Denys Vlasenko689d0652020-10-01 21:52:16 +0200985static int FAST_FUNC fileAction(
986 struct recursive_state *state IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
987 const char *fileName,
988 struct stat *statbuf)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000989{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100990 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200991 int same_fs = 1;
992
993#if ENABLE_FEATURE_FIND_XDEV
994 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
995 int i;
996 for (i = 0; i < G.xdev_count; i++) {
997 if (G.xdev_dev[i] == statbuf->st_dev)
998 goto found;
999 }
1000 //bb_error_msg("'%s': not same fs", fileName);
1001 same_fs = 0;
1002 found: ;
1003 }
1004#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001005
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001006#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko689d0652020-10-01 21:52:16 +02001007 if (state->depth < G.minmaxdepth[0]) {
Denys Vlasenko2dc1a972012-09-06 15:24:11 +02001008 if (same_fs)
1009 return TRUE; /* skip this, continue recursing */
1010 return SKIP; /* stop recursing */
1011 }
Denys Vlasenko689d0652020-10-01 21:52:16 +02001012 if (state->depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001013 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +00001014#endif
1015
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001016 r = exec_actions(G.actions, fileName, statbuf);
1017 /* Had no explicit -print[0] or -exec? then print */
1018 if ((r & TRUE) && G.need_print)
1019 puts(fileName);
1020
1021#if ENABLE_FEATURE_FIND_MAXDEPTH
1022 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko689d0652020-10-01 21:52:16 +02001023 if (state->depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001024 return SKIP;
1025 }
1026#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001027 /* -xdev stops on mountpoints, but AFTER mountpoit itself
1028 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +02001029 if (!same_fs) {
1030 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001031 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +02001032
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +00001033 /* Cannot return 0: our caller, recursive_action(),
1034 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +01001035 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001036}
1037
Eric Andersen17d49ef1999-10-06 20:25:32 +00001038
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001039#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +00001040static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +00001041{
1042 int mask = 0;
1043
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001044 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001045 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001046 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001047 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001048 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001049 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001050 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001051 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001052 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001053 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001054 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001055 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +00001056 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +00001057 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +00001058
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001059 if (mask == 0 || type[1] != '\0')
Denys Vlasenko0f296a32015-10-14 13:21:01 +02001060 bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +00001061
1062 return mask;
1063}
1064#endif
1065
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +00001066#if ENABLE_FEATURE_FIND_PERM \
1067 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001068 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +00001069static const char* plus_minus_num(const char* str)
1070{
1071 if (*str == '-' || *str == '+')
1072 str++;
1073 return str;
1074}
Denis Vlasenko4c978632007-02-03 03:31:13 +00001075#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +00001076
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001077/* Say no to GCCism */
1078#define USE_NESTED_FUNCTION 0
1079
1080#if !USE_NESTED_FUNCTION
1081struct pp_locals {
1082 action*** appp;
1083 unsigned cur_group;
1084 unsigned cur_action;
1085 IF_FEATURE_FIND_NOT( bool invert_flag; )
1086};
1087static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
1088{
1089 action *ap = xzalloc(sizeof_struct);
1090 action **app;
1091 action ***group = &ppl->appp[ppl->cur_group];
1092 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
1093 app[ppl->cur_action++] = ap;
1094 app[ppl->cur_action] = NULL;
1095 ap->f = f;
1096 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
1097 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
1098 return ap;
1099}
1100#endif
1101
Denis Vlasenko769d1e02007-01-22 23:04:27 +00001102static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +00001103{
Denis Vlasenko56fce002007-04-04 23:41:35 +00001104 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001105 OPT_FOLLOW ,
1106 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
1107 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001108 PARM_a ,
1109 PARM_o ,
1110 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001111#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001112 PARM_and ,
1113 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001114 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001115#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001116 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001117 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001118 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001119 IF_FEATURE_FIND_QUIT( PARM_quit ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001120 IF_FEATURE_FIND_DELETE( PARM_delete ,)
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001121 IF_FEATURE_FIND_EMPTY( PARM_empty ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001122 IF_FEATURE_FIND_EXEC( PARM_exec ,)
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001123 IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001124 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001125 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001126 PARM_name ,
1127 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001128 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001129#if ENABLE_DESKTOP
1130 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +02001131 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001132 IF_FEATURE_FIND_PATH( PARM_wholename ,)
1133#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001134 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001135 IF_FEATURE_FIND_REGEX( PARM_regex ,)
1136 IF_FEATURE_FIND_TYPE( PARM_type ,)
1137 IF_FEATURE_FIND_PERM( PARM_perm ,)
1138 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
Ismael Luceno04ad6832021-10-09 21:40:53 +02001139 IF_FEATURE_FIND_ATIME( PARM_atime ,)
1140 IF_FEATURE_FIND_CTIME( PARM_ctime ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001141 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
Ismael Luceno04ad6832021-10-09 21:40:53 +02001142 IF_FEATURE_FIND_AMIN( PARM_amin ,)
1143 IF_FEATURE_FIND_CMIN( PARM_cmin ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001144 IF_FEATURE_FIND_NEWER( PARM_newer ,)
1145 IF_FEATURE_FIND_INUM( PARM_inum ,)
Aaro Koskinenaaade692021-11-28 12:11:48 +02001146 IF_FEATURE_FIND_SAMEFILE(PARM_samefile ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001147 IF_FEATURE_FIND_USER( PARM_user ,)
1148 IF_FEATURE_FIND_GROUP( PARM_group ,)
1149 IF_FEATURE_FIND_SIZE( PARM_size ,)
1150 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001151 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001152 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001153 };
1154
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001155 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001156 "-follow\0"
1157 IF_FEATURE_FIND_XDEV( "-xdev\0" )
1158 IF_FEATURE_FIND_DEPTH( "-depth\0" )
1159 "-a\0"
1160 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001161 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001162#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001163 "-and\0"
1164 "-or\0"
1165 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001166#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001167 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001168 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001169 IF_FEATURE_FIND_PRUNE( "-prune\0" )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001170 IF_FEATURE_FIND_QUIT( "-quit\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001171 IF_FEATURE_FIND_DELETE( "-delete\0" )
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001172 IF_FEATURE_FIND_EMPTY( "-empty\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001173 IF_FEATURE_FIND_EXEC( "-exec\0" )
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001174 IF_FEATURE_FIND_EXECUTABLE("-executable\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001175 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001176 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +01001177 "-name\0"
1178 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001179 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001180#if ENABLE_DESKTOP
1181 IF_FEATURE_FIND_PATH( "-wholename\0")
1182#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001183 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001184 IF_FEATURE_FIND_REGEX( "-regex\0" )
1185 IF_FEATURE_FIND_TYPE( "-type\0" )
1186 IF_FEATURE_FIND_PERM( "-perm\0" )
1187 IF_FEATURE_FIND_MTIME( "-mtime\0" )
Ismael Luceno04ad6832021-10-09 21:40:53 +02001188 IF_FEATURE_FIND_ATIME( "-atime\0" )
1189 IF_FEATURE_FIND_CTIME( "-ctime\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001190 IF_FEATURE_FIND_MMIN( "-mmin\0" )
Ismael Luceno04ad6832021-10-09 21:40:53 +02001191 IF_FEATURE_FIND_AMIN( "-amin\0" )
1192 IF_FEATURE_FIND_CMIN( "-cmin\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001193 IF_FEATURE_FIND_NEWER( "-newer\0" )
1194 IF_FEATURE_FIND_INUM( "-inum\0" )
Aaro Koskinenaaade692021-11-28 12:11:48 +02001195 IF_FEATURE_FIND_SAMEFILE("-samefile\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001196 IF_FEATURE_FIND_USER( "-user\0" )
1197 IF_FEATURE_FIND_GROUP( "-group\0" )
1198 IF_FEATURE_FIND_SIZE( "-size\0" )
1199 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001200 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001201 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001202 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001203
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001204#if !USE_NESTED_FUNCTION
1205 struct pp_locals ppl;
1206#define appp (ppl.appp )
1207#define cur_group (ppl.cur_group )
1208#define cur_action (ppl.cur_action )
1209#define invert_flag (ppl.invert_flag)
1210#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1211#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001212 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001213 unsigned cur_group;
1214 unsigned cur_action;
1215 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001216
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001217 /* This is the only place in busybox where we use nested function.
1218 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001219 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001220 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001221 action* alloc_action(int sizeof_struct, action_fp f)
1222 {
1223 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001224 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001225 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001226 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001227 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001228 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1229 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001230 return ap;
1231 }
1232#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001233#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001234
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001235 cur_group = 0;
1236 cur_action = 0;
1237 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001238 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001239
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001240 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001241 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001242 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001243 const char *arg1 = argv[1];
1244
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001245 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1246
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001247 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001248 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001249 if (!arg1)
1250 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1251 argv++;
1252 }
1253
1254 /* We can use big switch() here, but on i386
1255 * it doesn't give smaller code. Other arches? */
1256
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001257/* Options always return true. They always take effect
1258 * rather than being processed only when their place in the
1259 * expression is reached.
1260 */
1261 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001262 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001263 dbg("follow enabled: %d", __LINE__);
1264 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001265 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001266#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001267 else if (parm == OPT_XDEV) {
1268 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001269 G.xdev_on = 1;
1270 }
1271#endif
1272#if ENABLE_FEATURE_FIND_MAXDEPTH
1273 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001274 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001275 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1276 }
1277#endif
1278#if ENABLE_FEATURE_FIND_DEPTH
1279 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001280 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001281 G.recurse_flags |= ACTION_DEPTHFIRST;
1282 }
1283#endif
1284/* Actions are grouped by operators
1285 * ( expr ) Force precedence
1286 * ! expr True if expr is false
1287 * -not expr Same as ! expr
1288 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1289 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1290 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1291 * We implement: (), -a, -o
1292 */
1293 /* Operators */
1294 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001295 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001296 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001297 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001298 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001299 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001300 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001301 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001302 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001303 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001304 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001305 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001306 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001307#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001308 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001309 /* also handles "find ! ! -name 'foo*'" */
1310 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001311 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001312 }
1313#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001314 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001315 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001316 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001317 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001318 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001319 }
1320#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001321 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001322 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001323 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001324 (void) ALLOC_ACTION(print0);
1325 }
Paul Foxd7384292006-05-12 14:47:20 +00001326#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001327#if ENABLE_FEATURE_FIND_PRUNE
1328 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001329 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001330 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001331 }
1332#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001333#if ENABLE_FEATURE_FIND_QUIT
1334 else if (parm == PARM_quit) {
1335 dbg("%d", __LINE__);
1336 (void) ALLOC_ACTION(quit);
1337 }
1338#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001339#if ENABLE_FEATURE_FIND_DELETE
1340 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001341 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001342 G.need_print = 0;
1343 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001344 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001345 }
Rob Landleycee605c2005-10-06 16:39:17 +00001346#endif
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001347#if ENABLE_FEATURE_FIND_EMPTY
1348 else if (parm == PARM_empty) {
1349 dbg("%d", __LINE__);
1350 (void) ALLOC_ACTION(empty);
1351 }
1352#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001353#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001354 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001355 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001356 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001357 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001358 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001359 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001360 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001361 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001362 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001363 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001364 if (!*argv) /* did not see ';' or '+' until end */
1365 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001366 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001367 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001368 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001369 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001370 if ((argv[0][0] == ';' || argv[0][0] == '+')
1371 && argv[0][1] == '\0'
1372 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001373# if ENABLE_FEATURE_FIND_EXEC_PLUS
1374 if (argv[0][0] == '+')
1375 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1376# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001377 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001378 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001379 argv++;
1380 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001381 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001382 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001383 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001384 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001385 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001386 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001387 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001388 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1389 }
1390# if ENABLE_FEATURE_FIND_EXEC_PLUS
1391 /*
1392 * coreutils expects {} to appear only once in "-exec +"
1393 */
1394 if (all_subst != 1 && ap->filelist)
James Byrne69374872019-07-02 11:35:03 +02001395 bb_simple_error_msg_and_die("only one '{}' allowed for -exec +");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001396# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001397 }
1398#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001399#if ENABLE_FEATURE_FIND_PAREN
1400 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001401 action_paren *ap;
1402 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001403 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001404
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001405 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001406 endarg = argv;
1407 while (1) {
1408 if (!*++endarg)
James Byrne69374872019-07-02 11:35:03 +02001409 bb_simple_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001410 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001411 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001412 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001413 *endarg = NULL;
1414 break;
1415 }
1416 }
1417 ap = ALLOC_ACTION(paren);
1418 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001419 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001420 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001421 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001422#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001423 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001424 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001425 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001426 ap = ALLOC_ACTION(name);
1427 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001428 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001429 }
1430#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001431 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001432 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001433 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001434 ap = ALLOC_ACTION(path);
1435 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001436 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001437 }
1438#endif
1439#if ENABLE_FEATURE_FIND_REGEX
1440 else if (parm == PARM_regex) {
1441 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001442 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001443 ap = ALLOC_ACTION(regex);
1444 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1445 }
1446#endif
1447#if ENABLE_FEATURE_FIND_TYPE
1448 else if (parm == PARM_type) {
1449 action_type *ap;
1450 ap = ALLOC_ACTION(type);
1451 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001452 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001453 }
1454#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001455#if ENABLE_FEATURE_FIND_EXECUTABLE
1456 else if (parm == PARM_executable) {
1457 (void) ALLOC_ACTION(executable);
1458 }
1459#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001460#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001461/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001462 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001463 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001464 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001465 */
1466 else if (parm == PARM_perm) {
1467 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001468 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001469 ap = ALLOC_ACTION(perm);
1470 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001471 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001472 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +02001473 ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask);
1474 if (ap->perm_mask == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +01001475 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001476 }
1477#endif
1478#if ENABLE_FEATURE_FIND_MTIME
Ismael Luceno04ad6832021-10-09 21:40:53 +02001479 else if (parm == PARM_mtime
1480# if ENABLE_FEATURE_FIND_ATIME
1481 || parm == PARM_atime
1482# endif
1483# if ENABLE_FEATURE_FIND_CTIME
1484 || parm == PARM_ctime
1485# endif
1486 ) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001487 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001488 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001489 ap = ALLOC_ACTION(mtime);
Ismael Luceno04ad6832021-10-09 21:40:53 +02001490# if ENABLE_FEATURE_FIND_AMIN || ENABLE_FEATURE_FIND_CMIN
1491 ap->time_type = arg[1];
1492# endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001493 ap->mtime_char = arg1[0];
1494 ap->mtime_days = xatoul(plus_minus_num(arg1));
1495 }
1496#endif
1497#if ENABLE_FEATURE_FIND_MMIN
Ismael Luceno04ad6832021-10-09 21:40:53 +02001498 else if (parm == PARM_mmin
1499# if ENABLE_FEATURE_FIND_AMIN
1500 || parm == PARM_amin
1501# endif
1502# if ENABLE_FEATURE_FIND_CMIN
1503 || parm == PARM_cmin
1504# endif
1505 ) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001506 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001507 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001508 ap = ALLOC_ACTION(mmin);
Ismael Luceno04ad6832021-10-09 21:40:53 +02001509# if ENABLE_FEATURE_FIND_AMIN || ENABLE_FEATURE_FIND_CMIN
1510 ap->time_type = arg[1];
1511# endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001512 ap->mmin_char = arg1[0];
1513 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1514 }
1515#endif
1516#if ENABLE_FEATURE_FIND_NEWER
1517 else if (parm == PARM_newer) {
1518 struct stat stat_newer;
1519 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001520 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001521 ap = ALLOC_ACTION(newer);
1522 xstat(arg1, &stat_newer);
1523 ap->newer_mtime = stat_newer.st_mtime;
1524 }
1525#endif
1526#if ENABLE_FEATURE_FIND_INUM
1527 else if (parm == PARM_inum) {
1528 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001529 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001530 ap = ALLOC_ACTION(inum);
1531 ap->inode_num = xatoul(arg1);
1532 }
1533#endif
Aaro Koskinenaaade692021-11-28 12:11:48 +02001534#if ENABLE_FEATURE_FIND_SAMEFILE
1535 else if (parm == PARM_samefile) {
1536 action_samefile *ap;
1537 struct stat stbuf;
1538 dbg("%d", __LINE__);
1539 if (G.recurse_flags & (ACTION_FOLLOWLINKS |
1540 ACTION_FOLLOWLINKS_L0))
1541 xstat(arg1, &stbuf);
1542 else if (lstat(arg1, &stbuf))
1543 bb_perror_msg_and_die("can't stat '%s'", arg1);
1544 ap = ALLOC_ACTION(samefile);
1545 ap->inode_num = stbuf.st_ino;
1546 ap->device = stbuf.st_dev;
1547 }
1548#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001549#if ENABLE_FEATURE_FIND_USER
1550 else if (parm == PARM_user) {
1551 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001552 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001553 ap = ALLOC_ACTION(user);
1554 ap->uid = bb_strtou(arg1, NULL, 10);
1555 if (errno)
1556 ap->uid = xuname2uid(arg1);
1557 }
1558#endif
1559#if ENABLE_FEATURE_FIND_GROUP
1560 else if (parm == PARM_group) {
1561 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001562 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001563 ap = ALLOC_ACTION(group);
1564 ap->gid = bb_strtou(arg1, NULL, 10);
1565 if (errno)
1566 ap->gid = xgroup2gid(arg1);
1567 }
1568#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001569#if ENABLE_FEATURE_FIND_SIZE
1570 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001571/* -size n[bckw]: file uses n units of space
1572 * b (default): units are 512-byte blocks
1573 * c: 1 byte
1574 * k: kilobytes
1575 * w: 2-byte words
1576 */
1577#if ENABLE_LFS
1578#define XATOU_SFX xatoull_sfx
1579#else
1580#define XATOU_SFX xatoul_sfx
1581#endif
Denys Vlasenko965b7952020-11-30 13:03:03 +01001582 static const struct suffix_mult find_suffixes[] ALIGN_SUFFIX = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001583 { "c", 1 },
1584 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001585 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001586 { "b", 512 },
1587 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001588 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001589 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001590 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001591 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001592 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001593 ap->size_char = arg1[0];
1594 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001595 }
1596#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001597#if ENABLE_FEATURE_FIND_CONTEXT
1598 else if (parm == PARM_context) {
1599 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001600 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001601 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001602 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001603 /* SELinux headers erroneously declare non-const parameter */
1604 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001605 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001606 }
1607#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001608#if ENABLE_FEATURE_FIND_LINKS
1609 else if (parm == PARM_links) {
1610 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001611 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001612 ap = ALLOC_ACTION(links);
1613 ap->links_char = arg1[0];
1614 ap->links_count = xatoul(plus_minus_num(arg1));
1615 }
1616#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001617 else {
1618 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001619 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001620 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001621 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001622 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001623 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001624 return appp;
1625#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001626#undef appp
1627#undef cur_action
1628#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001629}
1630
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001631int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001632int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001633{
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001634 int i, firstopt;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001635 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001636
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001637 INIT_G();
1638
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001639 /* "find -type f" + getopt("+HLP") => disaster.
1640 * Need to avoid getopt running into a non-HLP option.
1641 * Do this by temporarily storing NULL there:
1642 */
1643 past_HLP = argv;
1644 for (;;) {
1645 saved = *++past_HLP;
1646 if (!saved)
1647 break;
1648 if (saved[0] != '-')
1649 break;
1650 if (!saved[1])
1651 break; /* it is "-" */
Denys Vlasenko2d9e0972019-01-06 19:58:18 +01001652 if (saved[1] == '-' && !saved[2]) {
1653 /* it is "--" */
1654 /* Try: find -- /dev/null */
1655 saved = *++past_HLP;
1656 break;
1657 }
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001658 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1659 break;
1660 }
1661 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001662 /* "+": stop on first non-option */
Denys Vlasenkoef2366c2020-12-12 08:44:32 +01001663 i = getopt32(argv, "+""HLP");
Denys Vlasenko6782f482013-05-14 17:20:35 +02001664 if (i & (1<<0))
1665 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1666 if (i & (1<<1))
1667 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1668 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001669 argv = past_HLP; /* same result as "argv += optind;" */
1670 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001671
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001672 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001673 if (argv[firstopt][0] == '-')
1674 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001675 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1676 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001677 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001678 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001679 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001680 if (firstopt == 0) {
1681 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001682 firstopt++;
1683 }
1684
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001685 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001686 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001687
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001688#if ENABLE_FEATURE_FIND_XDEV
1689 if (G.xdev_on) {
1690 struct stat stbuf;
1691
1692 G.xdev_count = firstopt;
1693 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1694 for (i = 0; argv[i]; i++) {
1695 /* not xstat(): shouldn't bomb out on
1696 * "find not_exist exist -xdev" */
1697 if (stat(argv[i], &stbuf) == 0)
1698 G.xdev_dev[i] = stbuf.st_dev;
1699 /* else G.xdev_dev[i] stays 0 and
1700 * won't match any real device dev_t
1701 */
1702 }
1703 }
1704#endif
1705
1706 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001707 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001708 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001709 fileAction, /* file action */
1710 fileAction, /* dir action */
Denys Vlasenko689d0652020-10-01 21:52:16 +02001711 NULL) /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001712 ) {
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001713 G.exitstatus |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001714 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001715 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001716
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001717 IF_FEATURE_FIND_EXEC_PLUS(G.exitstatus |= flush_exec_plus();)
1718 return G.exitstatus;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001719}