blob: f557bb7625db0b175dcf6f83c3b035a161cb9da7 [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:
77//config:config FEATURE_FIND_MMIN
Ismael Luceno0084c442021-09-29 13:41:32 +020078//config: bool "Enable -mmin: modification time matching by minutes"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020079//config: default y
80//config: depends on FIND
81//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020082//config: Allow searching based on the modification time of
83//config: files, in minutes.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020084//config:
85//config:config FEATURE_FIND_PERM
86//config: bool "Enable -perm: permissions matching"
87//config: default y
88//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020089//config:
90//config:config FEATURE_FIND_TYPE
91//config: bool "Enable -type: file type matching (file/dir/link/...)"
92//config: default y
93//config: depends on FIND
94//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020095//config: Enable searching based on file type (file,
96//config: directory, socket, device, etc.).
Denys Vlasenko7fb68f12010-05-09 04:22:48 +020097//config:
Denys Vlasenko008e73b2018-07-06 21:04:41 +020098//config:config FEATURE_FIND_EXECUTABLE
99//config: bool "Enable -executable: file is executable"
100//config: default y
101//config: depends on FIND
102//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200103//config:config FEATURE_FIND_XDEV
104//config: bool "Enable -xdev: 'stay in filesystem'"
105//config: default y
106//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200107//config:
108//config:config FEATURE_FIND_MAXDEPTH
Denys Vlasenkocef85c22011-02-12 16:35:24 +0100109//config: bool "Enable -mindepth N and -maxdepth N"
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200110//config: default y
111//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200112//config:
113//config:config FEATURE_FIND_NEWER
114//config: bool "Enable -newer: compare file modification times"
115//config: default y
116//config: depends on FIND
117//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200118//config: Support the 'find -newer' option for finding any files which have
119//config: modification time that is more recent than the specified FILE.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200120//config:
121//config:config FEATURE_FIND_INUM
122//config: bool "Enable -inum: inode number matching"
123//config: default y
124//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200125//config:
126//config:config FEATURE_FIND_EXEC
127//config: bool "Enable -exec: execute commands"
128//config: default y
129//config: depends on FIND
130//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200131//config: Support the 'find -exec' option for executing commands based upon
132//config: the files matched.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200133//config:
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200134//config:config FEATURE_FIND_EXEC_PLUS
135//config: bool "Enable -exec ... {} +"
136//config: default y
137//config: depends on FEATURE_FIND_EXEC
138//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200139//config: Support the 'find -exec ... {} +' option for executing commands
140//config: for all matched files at once.
141//config: Without this option, -exec + is a synonym for -exec ;
142//config: (IOW: it works correctly, but without expected speedup)
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200143//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200144//config:config FEATURE_FIND_USER
145//config: bool "Enable -user: username/uid matching"
146//config: default y
147//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200148//config:
149//config:config FEATURE_FIND_GROUP
150//config: bool "Enable -group: group/gid matching"
151//config: default y
152//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200153//config:
154//config:config FEATURE_FIND_NOT
155//config: bool "Enable the 'not' (!) operator"
156//config: default y
157//config: depends on FIND
158//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200159//config: Support the '!' operator to invert the test results.
160//config: If 'Enable full-blown desktop' is enabled, then will also support
161//config: the non-POSIX notation '-not'.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200162//config:
163//config:config FEATURE_FIND_DEPTH
164//config: bool "Enable -depth"
165//config: default y
166//config: depends on FIND
167//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200168//config: Process each directory's contents before the directory itself.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200169//config:
170//config:config FEATURE_FIND_PAREN
171//config: bool "Enable parens in options"
172//config: default y
173//config: depends on FIND
174//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200175//config: Enable usage of parens '(' to specify logical order of arguments.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200176//config:
177//config:config FEATURE_FIND_SIZE
178//config: bool "Enable -size: file size matching"
179//config: default y
180//config: depends on FIND
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200181//config:
182//config:config FEATURE_FIND_PRUNE
183//config: bool "Enable -prune: exclude subdirectories"
184//config: default y
185//config: depends on FIND
186//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200187//config: If the file is a directory, don't descend into it. Useful for
188//config: exclusion .svn and CVS directories.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200189//config:
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200190//config:config FEATURE_FIND_QUIT
191//config: bool "Enable -quit: exit"
192//config: default y
193//config: depends on FIND
194//config: help
195//config: If this action is reached, 'find' exits.
196//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200197//config:config FEATURE_FIND_DELETE
198//config: bool "Enable -delete: delete files/dirs"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200199//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200200//config: depends on FIND && FEATURE_FIND_DEPTH
201//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200202//config: Support the 'find -delete' option for deleting files and directories.
203//config: WARNING: This option can do much harm if used wrong. Busybox will not
204//config: try to protect the user from doing stupid things. Use with care.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200205//config:
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200206//config:config FEATURE_FIND_EMPTY
207//config: bool "Enable -empty: match empty files or directories"
208//config: default y
209//config: depends on FIND
210//config: help
211//config: Support the 'find -empty' option to find empty regular files
212//config: or directories.
213//config:
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200214//config:config FEATURE_FIND_PATH
215//config: bool "Enable -path: match pathname with shell pattern"
216//config: default y
217//config: depends on FIND
218//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200219//config: The -path option matches whole pathname instead of just filename.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200220//config:
221//config:config FEATURE_FIND_REGEX
222//config: bool "Enable -regex: match pathname with regex"
223//config: default y
224//config: depends on FIND
225//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200226//config: The -regex option matches whole pathname against regular expression.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200227//config:
228//config:config FEATURE_FIND_CONTEXT
229//config: bool "Enable -context: security context matching"
230//config: default n
231//config: depends on FIND && SELINUX
232//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200233//config: Support the 'find -context' option for matching security context.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200234//config:
235//config:config FEATURE_FIND_LINKS
236//config: bool "Enable -links: link count matching"
Denys Vlasenko2f32bf82010-06-06 04:14:28 +0200237//config: default y
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200238//config: depends on FIND
239//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +0200240//config: Support the 'find -links' option for matching number of links.
Denys Vlasenko7fb68f12010-05-09 04:22:48 +0200241
Denys Vlasenko805f2452011-02-12 20:26:14 +0100242//applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find))
243
244//kbuild:lib-$(CONFIG_FIND) += find.o
245
246//usage:#define find_trivial_usage
Denys Vlasenko6782f482013-05-14 17:20:35 +0200247//usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100248//usage:#define find_full_usage "\n\n"
249//usage: "Search for files and perform actions on them.\n"
250//usage: "First failed action stops processing of current file.\n"
251//usage: "Defaults: PATH is current directory, action is '-print'\n"
Denys Vlasenko6782f482013-05-14 17:20:35 +0200252//usage: "\n -L,-follow Follow symlinks"
253//usage: "\n -H ...on command line only"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100254//usage: IF_FEATURE_FIND_XDEV(
255//usage: "\n -xdev Don't descend directories on other filesystems"
256//usage: )
257//usage: IF_FEATURE_FIND_MAXDEPTH(
258//usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies"
259//usage: "\n actions to command line arguments only"
260//usage: "\n -mindepth N Don't act on first N levels"
261//usage: )
262//usage: IF_FEATURE_FIND_DEPTH(
263//usage: "\n -depth Act on directory *after* traversing it"
264//usage: )
265//usage: "\n"
266//usage: "\nActions:"
267//usage: IF_FEATURE_FIND_PAREN(
268//usage: "\n ( ACTIONS ) Group actions for -o / -a"
269//usage: )
270//usage: IF_FEATURE_FIND_NOT(
271//usage: "\n ! ACT Invert ACT's success/failure"
272//usage: )
273//usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2"
274//usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2"
275//usage: "\n Note: -a has higher priority than -o"
276//usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN"
277//usage: "\n -iname PATTERN Case insensitive -name"
278//usage: IF_FEATURE_FIND_PATH(
279//usage: "\n -path PATTERN Match path to PATTERN"
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200280//usage: "\n -ipath PATTERN Case insensitive -path"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100281//usage: )
282//usage: IF_FEATURE_FIND_REGEX(
283//usage: "\n -regex PATTERN Match path to regex PATTERN"
284//usage: )
285//usage: IF_FEATURE_FIND_TYPE(
Denys Vlasenkof4eaccb2018-04-24 10:13:45 +0200286//usage: "\n -type X File type is X (one of: f,d,l,b,c,s,p)"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100287//usage: )
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200288//usage: IF_FEATURE_FIND_EXECUTABLE(
289//usage: "\n -executable File is executable"
290//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100291//usage: IF_FEATURE_FIND_PERM(
292//usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK),"
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100293//usage: "\n or exactly MASK bits are set in file's mode"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100294//usage: )
295//usage: IF_FEATURE_FIND_MTIME(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100296//usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N),"
297//usage: "\n or exactly N days in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100298//usage: )
299//usage: IF_FEATURE_FIND_MMIN(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100300//usage: "\n -mmin MINS mtime is greater than (+N), less than (-N),"
301//usage: "\n or exactly N minutes in the past"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100302//usage: )
303//usage: IF_FEATURE_FIND_NEWER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100304//usage: "\n -newer FILE mtime is more recent than FILE's"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100305//usage: )
306//usage: IF_FEATURE_FIND_INUM(
307//usage: "\n -inum N File has inode number N"
308//usage: )
309//usage: IF_FEATURE_FIND_USER(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100310//usage: "\n -user NAME/ID File is owned by given user"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100311//usage: )
312//usage: IF_FEATURE_FIND_GROUP(
Denys Vlasenkoe1a42092011-02-12 20:37:41 +0100313//usage: "\n -group NAME/ID File is owned by given group"
Denys Vlasenko805f2452011-02-12 20:26:14 +0100314//usage: )
315//usage: IF_FEATURE_FIND_SIZE(
316//usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))"
317//usage: "\n +/-N: file size is bigger/smaller than N"
318//usage: )
319//usage: IF_FEATURE_FIND_LINKS(
320//usage: "\n -links N Number of links is greater than (+N), less than (-N),"
321//usage: "\n or exactly N"
322//usage: )
323//usage: IF_FEATURE_FIND_CONTEXT(
324//usage: "\n -context CTX File has specified security context"
325//usage: )
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200326//usage: IF_FEATURE_FIND_EMPTY(
327//usage: "\n -empty Match empty file/directory"
328//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100329//usage: IF_FEATURE_FIND_PRUNE(
330//usage: "\n -prune If current file is directory, don't descend into it"
331//usage: )
332//usage: "\nIf none of the following actions is specified, -print is assumed"
333//usage: "\n -print Print file name"
334//usage: IF_FEATURE_FIND_PRINT0(
335//usage: "\n -print0 Print file name, NUL terminated"
336//usage: )
337//usage: IF_FEATURE_FIND_EXEC(
338//usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by"
339//usage: "\n file name. Fails if CMD exits with nonzero"
340//usage: )
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200341//usage: IF_FEATURE_FIND_EXEC_PLUS(
342//usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names"
343//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100344//usage: IF_FEATURE_FIND_DELETE(
345//usage: "\n -delete Delete current file/directory. Turns on -depth option"
346//usage: )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200347//usage: IF_FEATURE_FIND_QUIT(
348//usage: "\n -quit Exit"
349//usage: )
Denys Vlasenko805f2452011-02-12 20:26:14 +0100350//usage:
351//usage:#define find_example_usage
352//usage: "$ find / -name passwd\n"
353//usage: "/etc/passwd\n"
354
Matt Kraai096370d2001-02-07 03:52:38 +0000355#include <fnmatch.h>
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000356#include "libbb.h"
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200357#include "common_bufsiz.h"
Denis Vlasenkob9411292007-06-15 23:43:11 +0000358#if ENABLE_FEATURE_FIND_REGEX
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200359# include "xregex.h"
360#endif
361/* GNUism: */
362#ifndef FNM_CASEFOLD
363# define FNM_CASEFOLD 0
Denis Vlasenkob9411292007-06-15 23:43:11 +0000364#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +0000365
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200366#if 1
367# define dbg(...) ((void)0)
368#else
369# define dbg(...) bb_error_msg(__VA_ARGS__)
370#endif
371
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200372
Denis Vlasenko99912ca2007-04-10 15:43:37 +0000373/* This is a NOEXEC applet. Be very careful! */
374
375
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200376typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC;
Matt Kraai096370d2001-02-07 03:52:38 +0000377
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000378typedef struct {
379 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000380#if ENABLE_FEATURE_FIND_NOT
Bernhard Reutner-Fischer32773ac2007-03-29 13:54:17 +0000381 bool invert;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000382#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000383} action;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200384
Denys Vlasenko606291b2009-09-23 23:15:43 +0200385#define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name;
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200386#define ACTF(name) \
387 static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200388 const struct stat *statbuf UNUSED_PARAM, \
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +0200389 action_##name* ap UNUSED_PARAM)
390
391 ACTS(print)
392 ACTS(name, const char *pattern; bool iname;)
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200393IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000394IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;))
395IF_FEATURE_FIND_PRINT0( ACTS(print0))
396IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200397IF_FEATURE_FIND_EXECUTABLE(ACTS(executable))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000398IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
399IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
400IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
401IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
402IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
403IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
404IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;))
405IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;))
406IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
407IF_FEATURE_FIND_PRUNE( ACTS(prune))
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200408IF_FEATURE_FIND_QUIT( ACTS(quit))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000409IF_FEATURE_FIND_DELETE( ACTS(delete))
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200410IF_FEATURE_FIND_EMPTY( ACTS(empty))
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200411IF_FEATURE_FIND_EXEC( ACTS(exec,
412 char **exec_argv; /* -exec ARGS */
413 unsigned *subst_count;
414 int exec_argc; /* count of ARGS */
415 IF_FEATURE_FIND_EXEC_PLUS(
416 /*
417 * filelist is NULL if "exec ;"
418 * non-NULL if "exec +"
419 */
420 char **filelist;
421 int filelist_idx;
422 int file_len;
423 )
424 ))
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000425IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100426IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;))
Matt Kraai096370d2001-02-07 03:52:38 +0000427
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200428struct globals {
429 IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;)
430 IF_FEATURE_FIND_XDEV(int xdev_count;)
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100431#if ENABLE_FEATURE_FIND_MAXDEPTH
432 int minmaxdepth[2];
433#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200434 action ***actions;
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100435 smallint need_print;
436 smallint xdev_on;
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200437 smalluint exitstatus;
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200438 recurse_flags_t recurse_flags;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200439 IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;)
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100440} FIX_ALIASING;
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +0200441#define G (*(struct globals*)bb_common_bufsiz1)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200442#define INIT_G() do { \
Denys Vlasenko47cfbf32016-04-21 18:18:48 +0200443 setup_common_bufsiz(); \
Denys Vlasenko7b85ec32015-10-13 17:17:34 +0200444 BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \
Denys Vlasenko1b34d4f2009-09-30 02:39:57 +0200445 /* we have to zero it out because of NOEXEC */ \
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100446 memset(&G, 0, sizeof(G)); \
447 IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200448 IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200449 G.need_print = 1; \
450 G.recurse_flags = ACTION_RECURSE; \
451} while (0)
Rob Landley5d3a0e82005-10-04 03:34:39 +0000452
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000453/* Return values of ACTFs ('action functions') are a bit mask:
454 * bit 1=1: prune (use SKIP constant for setting it)
455 * bit 0=1: matched successfully (TRUE)
456 */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000457
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200458static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000459{
460 int cur_group;
461 int cur_action;
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000462 int rc = 0;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000463 action **app, *ap;
464
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000465 /* "action group" is a set of actions ANDed together.
466 * groups are ORed together.
467 * We simply evaluate each group until we find one in which all actions
468 * succeed. */
469
470 /* -prune is special: if it is encountered, then we won't
471 * descend into current directory. It doesn't matter whether
472 * action group (in which -prune sits) will succeed or not:
473 * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir
474 * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs
475 * not starting with 'f' */
476
477 /* We invert TRUE bit (bit 0). Now 1 there means 'failure'.
478 * and bitwise OR in "rc |= TRUE ^ ap->f()" will:
479 * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'.
480 * On return, bit is restored. */
481
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000482 cur_group = -1;
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100483 while ((app = appp[++cur_group]) != NULL) {
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000484 rc &= ~TRUE; /* 'success' so far, clear TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000485 cur_action = -1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000486 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000487 ap = app[++cur_action];
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000488 if (!ap) /* all actions in group were successful */
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000489 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000490 rc |= TRUE ^ ap->f(fileName, statbuf, ap);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000491#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000492 if (ap->invert) rc ^= TRUE;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000493#endif
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200494 dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc);
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000495 if (rc & TRUE) /* current group failed, try next */
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000496 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000497 }
498 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +0200499 dbg("returning:0x%x", rc ^ TRUE);
Denis Vlasenkob04b4352007-04-08 11:10:43 +0000500 return rc ^ TRUE; /* restore TRUE bit */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000501}
502
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200503#if !FNM_CASEFOLD
504static char *strcpy_upcase(char *dst, const char *src)
505{
506 char *d = dst;
507 while (1) {
508 unsigned char ch = *src++;
509 if (ch >= 'a' && ch <= 'z')
510 ch -= ('a' - 'A');
511 *d++ = ch;
512 if (ch == '\0')
513 break;
514 }
515 return dst;
516}
517#endif
518
Denis Vlasenko92258542006-11-01 10:25:35 +0000519ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000520{
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100521 int r;
Denis Vlasenkodc757aa2007-06-30 08:04:05 +0000522 const char *tmp = bb_basename(fileName);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100523 /* GNU findutils: find DIR/ -name DIR
524 * prints "DIR/" (DIR// prints "DIR//" etc).
525 * Need to strip trailing "/".
526 * Such names can come only from top-level names, but
527 * we can't do this before recursive_action() call,
528 * since then "find FILE/ -name FILE"
529 * would also work (on non-directories), which is wrong.
530 */
531 char *trunc_slash = NULL;
532
533 if (*tmp == '\0') {
534 /* "foo/bar/[//...]" */
535 while (tmp != fileName && tmp[-1] == '/')
536 tmp--;
537 if (tmp == fileName) { /* entire fileName is "//.."? */
538 /* yes, convert "//..." to "/"
539 * Testcases:
540 * find / -maxdepth 1 -name /: prints /
541 * find // -maxdepth 1 -name /: prints //
542 * find / -maxdepth 1 -name //: prints nothing
543 * find // -maxdepth 1 -name //: prints nothing
544 */
545 if (tmp[1])
546 trunc_slash = (char*)tmp + 1;
547 } else {
548 /* no, it's "foo/bar/[//...]", go back to 'b' */
549 trunc_slash = (char*)tmp;
550 while (tmp != fileName && tmp[-1] != '/')
551 tmp--;
552 }
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000553 }
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100554
Denys Vlasenko91388172010-04-26 09:54:04 +0200555 /* Was using FNM_PERIOD flag too,
556 * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it.
557 * find -name '*foo' should match .foo too:
558 */
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100559 if (trunc_slash) *trunc_slash = '\0';
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200560#if FNM_CASEFOLD
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100561 r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0));
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200562#else
563 if (ap->iname)
564 tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp);
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100565 r = fnmatch(ap->pattern, tmp, 0);
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200566#endif
Denys Vlasenkoccc99852016-11-25 20:14:33 +0100567 if (trunc_slash) *trunc_slash = '/';
568 return r == 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000569}
Denis Vlasenko75aa6152007-12-09 10:07:39 +0000570
Denis Vlasenkob9411292007-06-15 23:43:11 +0000571#if ENABLE_FEATURE_FIND_PATH
572ACTF(path)
573{
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200574# if FNM_CASEFOLD
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +0200575 return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0;
Denys Vlasenkob24ef032011-05-23 00:40:54 +0200576# else
577 if (ap->ipath)
578 fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName);
579 return fnmatch(ap->pattern, fileName, 0) == 0;
580# endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000581}
582#endif
583#if ENABLE_FEATURE_FIND_REGEX
584ACTF(regex)
585{
586 regmatch_t match;
587 if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/))
588 return 0; /* no match */
589 if (match.rm_so)
590 return 0; /* match doesn't start at pos 0 */
591 if (fileName[match.rm_eo])
592 return 0; /* match doesn't end exactly at end of pathname */
593 return 1;
594}
595#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000596#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000597ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000598{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000599 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000600}
Rob Landleycee605c2005-10-06 16:39:17 +0000601#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +0200602#if ENABLE_FEATURE_FIND_EXECUTABLE
603ACTF(executable)
604{
605 return access(fileName, X_OK) == 0;
606}
607#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000608#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000609ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000610{
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +0200611 /* -perm [+/]mode: at least one of perm_mask bits are set */
612 if (ap->perm_char == '+' || ap->perm_char == '/')
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000613 return (statbuf->st_mode & ap->perm_mask) != 0;
614 /* -perm -mode: all of perm_mask are set */
615 if (ap->perm_char == '-')
616 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
617 /* -perm mode: file mode must match perm_mask */
618 return (statbuf->st_mode & 07777) == ap->perm_mask;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000619}
Rob Landleycee605c2005-10-06 16:39:17 +0000620#endif
Ismael Luceno421c8762021-09-29 13:41:33 +0200621
622#if \
623 ENABLE_FEATURE_FIND_MMIN || \
624 ENABLE_FEATURE_FIND_MTIME
625static int time_cmp(time_t ftime, char time_char, time_t secs, time_t delta)
626{
627 time_t file_age = time(NULL) - ftime;
628 switch (time_char) {
629 case '+': return file_age >= secs + delta;
630 case '-': return file_age < secs;
631 /* just numeric time */
632 default: return file_age >= secs && file_age < secs + delta;
633 }
634}
635#endif
636
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000637#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000638ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000639{
Ismael Luceno421c8762021-09-29 13:41:33 +0200640 return time_cmp(statbuf->st_mtime, ap->mtime_char,
641 ap->mtime_days * 24*60*60, 24*60*60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000642}
Rob Landleycee605c2005-10-06 16:39:17 +0000643#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000644#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000645ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000646{
Ismael Luceno421c8762021-09-29 13:41:33 +0200647 return time_cmp(statbuf->st_mtime, ap->mmin_char,
648 ap->mmin_mins * 60, 60);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000649}
Paul Fox72d1a232006-01-13 21:05:41 +0000650#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000651#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000652ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000653{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000654 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000655}
Rob Landleycee605c2005-10-06 16:39:17 +0000656#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000657#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000658ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000659{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000660 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000661}
Rob Landleycee605c2005-10-06 16:39:17 +0000662#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000663#if ENABLE_FEATURE_FIND_EXEC
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200664static int do_exec(action_exec *ap, const char *fileName)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000665{
666 int i, rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200667# if ENABLE_FEATURE_FIND_EXEC_PLUS
668 int size = ap->exec_argc + ap->filelist_idx + 1;
669# else
670 int size = ap->exec_argc + 1;
671# endif
672# if ENABLE_USE_PORTABLE_CODE
673 char **argv = alloca(sizeof(char*) * size);
674# else /* gcc 4.3.1 generates smaller code: */
675 char *argv[size];
676# endif
677 char **pp = argv;
678
679 for (i = 0; i < ap->exec_argc; i++) {
680 const char *arg = ap->exec_argv[i];
681
682# if ENABLE_FEATURE_FIND_EXEC_PLUS
683 if (ap->filelist) {
684 /* Handling "-exec +"
685 * Only one exec_argv[i] has substitution in it.
686 * Expand that one exec_argv[i] into file list.
687 */
688 if (ap->subst_count[i] == 0) {
689 *pp++ = xstrdup(arg);
690 } else {
691 int j = 0;
692 while (ap->filelist[j]) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200693 /* 2nd arg here should be ap->subst_count[i], but it is always 1: */
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200694 *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]);
695 free(ap->filelist[j]);
696 j++;
697 }
698 }
699 } else
700# endif
701 {
702 /* Handling "-exec ;" */
703 *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName);
704 }
705 }
706 *pp = NULL; /* terminate the list */
707
708# if ENABLE_FEATURE_FIND_EXEC_PLUS
709 if (ap->filelist) {
710 ap->filelist[0] = NULL;
711 ap->filelist_idx = 0;
712 ap->file_len = 0;
713 }
714# endif
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000715
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000716 rc = spawn_and_wait(argv);
Denis Vlasenko7e754f12007-04-09 13:04:50 +0000717 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000718 bb_simple_perror_msg(argv[0]);
Denis Vlasenkocd7001f2007-04-09 21:32:30 +0000719
720 i = 0;
721 while (argv[i])
722 free(argv[i++]);
723 return rc == 0; /* return 1 if exitcode 0 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000724}
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200725ACTF(exec)
726{
727# if ENABLE_FEATURE_FIND_EXEC_PLUS
728 if (ap->filelist) {
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200729 int rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200730
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200731 ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx);
732 ap->filelist[ap->filelist_idx++] = xstrdup(fileName);
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200733 ap->file_len += strlen(fileName) + sizeof(char*) + 1;
734 /* If we have lots of files already, exec the command */
735 rc = 1;
Denys Vlasenkof92f1d02014-06-22 13:54:40 +0200736 if (ap->file_len >= G.max_argv_len)
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200737 rc = do_exec(ap, NULL);
738 return rc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200739 }
740# endif
741 return do_exec(ap, fileName);
742}
743# if ENABLE_FEATURE_FIND_EXEC_PLUS
744static int flush_exec_plus(void)
745{
746 action *ap;
747 action **app;
748 action ***appp = G.actions;
749 while ((app = *appp++) != NULL) {
750 while ((ap = *app++) != NULL) {
751 if (ap->f == (action_fp)func_exec) {
752 action_exec *ae = (void*)ap;
753 if (ae->filelist_idx != 0) {
754 int rc = do_exec(ae, NULL);
755# if ENABLE_FEATURE_FIND_NOT
756 if (ap->invert) rc = !rc;
757# endif
Denys Vlasenko6be3a522014-06-19 11:32:11 +0200758 if (rc == 0)
759 return 1;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +0200760 }
761 }
762 }
763 }
764 return 0;
765}
766# endif
Rob Landleycee605c2005-10-06 16:39:17 +0000767#endif
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000768#if ENABLE_FEATURE_FIND_USER
769ACTF(user)
770{
771 return (statbuf->st_uid == ap->uid);
772}
773#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +0000774#if ENABLE_FEATURE_FIND_GROUP
775ACTF(group)
776{
777 return (statbuf->st_gid == ap->gid);
778}
779#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000780#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000781ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000782{
783 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000784 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000785}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000786#endif
Denis Vlasenko92258542006-11-01 10:25:35 +0000787ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000788{
789 puts(fileName);
790 return TRUE;
791}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000792#if ENABLE_FEATURE_FIND_PAREN
Denis Vlasenko92258542006-11-01 10:25:35 +0000793ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000794{
795 return exec_actions(ap->subexpr, fileName, statbuf);
796}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000797#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000798#if ENABLE_FEATURE_FIND_SIZE
799ACTF(size)
800{
Denis Vlasenko53a0e972007-06-16 00:30:52 +0000801 if (ap->size_char == '+')
802 return statbuf->st_size > ap->size;
803 if (ap->size_char == '-')
804 return statbuf->st_size < ap->size;
Denis Vlasenkob9411292007-06-15 23:43:11 +0000805 return statbuf->st_size == ap->size;
806}
807#endif
Bernhard Reutner-Fischer6d79dd62007-04-05 09:40:12 +0000808#if ENABLE_FEATURE_FIND_PRUNE
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000809/*
810 * -prune: if -depth is not given, return true and do not descend
811 * current dir; if -depth is given, return false with no effect.
812 * Example:
813 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
814 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000815ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000816{
Denis Vlasenkobbd695d2007-04-08 10:52:28 +0000817 return SKIP + TRUE;
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000818}
Denis Vlasenko56fce002007-04-04 23:41:35 +0000819#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +0200820#if ENABLE_FEATURE_FIND_QUIT
821ACTF(quit)
822{
823 exit(G.exitstatus);
824}
825#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000826#if ENABLE_FEATURE_FIND_DELETE
827ACTF(delete)
828{
829 int rc;
830 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko8c0708a2015-10-07 21:01:47 +0200831 /* "find . -delete" skips rmdir(".") */
832 rc = 0;
833 if (NOT_LONE_CHAR(fileName, '.'))
834 rc = rmdir(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000835 } else {
836 rc = unlink(fileName);
837 }
838 if (rc < 0)
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000839 bb_simple_perror_msg(fileName);
Denis Vlasenko62f04792007-04-13 10:00:12 +0000840 return TRUE;
841}
842#endif
Aaro Koskinenaf18b302019-09-12 12:04:13 +0200843#if ENABLE_FEATURE_FIND_EMPTY
844ACTF(empty)
845{
846 if (S_ISDIR(statbuf->st_mode)) {
847 DIR *dir;
848 struct dirent *dent;
849
850 dir = opendir(fileName);
851 if (!dir) {
852 bb_simple_perror_msg(fileName);
853 return FALSE;
854 }
855
856 while ((dent = readdir(dir)) != NULL
857 && DOT_OR_DOTDOT(dent->d_name)
858 ) {
859 continue;
860 }
861 closedir(dir);
862 return dent == NULL;
863 }
864 return S_ISREG(statbuf->st_mode) && statbuf->st_size == 0;
865}
866#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000867#if ENABLE_FEATURE_FIND_CONTEXT
868ACTF(context)
869{
870 security_context_t con;
871 int rc;
872
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200873 if (G.recurse_flags & ACTION_FOLLOWLINKS) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +0000874 rc = getfilecon(fileName, &con);
875 } else {
876 rc = lgetfilecon(fileName, &con);
877 }
878 if (rc < 0)
879 return FALSE;
880 rc = strcmp(ap->context, con);
881 freecon(con);
882 return rc == 0;
883}
884#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100885#if ENABLE_FEATURE_FIND_LINKS
886ACTF(links)
887{
888 switch(ap->links_char) {
889 case '-' : return (statbuf->st_nlink < ap->links_count);
890 case '+' : return (statbuf->st_nlink > ap->links_count);
891 default: return (statbuf->st_nlink == ap->links_count);
892 }
893}
894#endif
Denis Vlasenko62f04792007-04-13 10:00:12 +0000895
Denys Vlasenko689d0652020-10-01 21:52:16 +0200896static int FAST_FUNC fileAction(
897 struct recursive_state *state IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM),
898 const char *fileName,
899 struct stat *statbuf)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000900{
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100901 int r;
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200902 int same_fs = 1;
903
904#if ENABLE_FEATURE_FIND_XDEV
905 if (S_ISDIR(statbuf->st_mode) && G.xdev_count) {
906 int i;
907 for (i = 0; i < G.xdev_count; i++) {
908 if (G.xdev_dev[i] == statbuf->st_dev)
909 goto found;
910 }
911 //bb_error_msg("'%s': not same fs", fileName);
912 same_fs = 0;
913 found: ;
914 }
915#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +0000916
Denys Vlasenko053c12e2011-02-12 20:27:13 +0100917#if ENABLE_FEATURE_FIND_MAXDEPTH
Denys Vlasenko689d0652020-10-01 21:52:16 +0200918 if (state->depth < G.minmaxdepth[0]) {
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200919 if (same_fs)
920 return TRUE; /* skip this, continue recursing */
921 return SKIP; /* stop recursing */
922 }
Denys Vlasenko689d0652020-10-01 21:52:16 +0200923 if (state->depth > G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100924 return SKIP; /* stop recursing */
Denis Vlasenkob9411292007-06-15 23:43:11 +0000925#endif
926
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100927 r = exec_actions(G.actions, fileName, statbuf);
928 /* Had no explicit -print[0] or -exec? then print */
929 if ((r & TRUE) && G.need_print)
930 puts(fileName);
931
932#if ENABLE_FEATURE_FIND_MAXDEPTH
933 if (S_ISDIR(statbuf->st_mode)) {
Denys Vlasenko689d0652020-10-01 21:52:16 +0200934 if (state->depth == G.minmaxdepth[1])
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100935 return SKIP;
936 }
937#endif
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100938 /* -xdev stops on mountpoints, but AFTER mountpoit itself
939 * is processed as usual */
Denys Vlasenko2dc1a972012-09-06 15:24:11 +0200940 if (!same_fs) {
941 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000942 }
Denys Vlasenko6c750f12009-09-27 01:51:47 +0200943
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000944 /* Cannot return 0: our caller, recursive_action(),
945 * will perror() and skip dirs (if called on dir) */
Denys Vlasenkoce9b97f2010-01-10 02:33:02 +0100946 return (r & SKIP) ? SKIP : TRUE;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000947}
948
Eric Andersen17d49ef1999-10-06 20:25:32 +0000949
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000950#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000951static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000952{
953 int mask = 0;
954
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000955 if (*type == 'b')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000956 mask = S_IFBLK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000957 else if (*type == 'c')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000958 mask = S_IFCHR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000959 else if (*type == 'd')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000960 mask = S_IFDIR;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000961 else if (*type == 'p')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000962 mask = S_IFIFO;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000963 else if (*type == 'f')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000964 mask = S_IFREG;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000965 else if (*type == 'l')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000966 mask = S_IFLNK;
Bernhard Reutner-Fischer307d27d2007-03-29 13:56:02 +0000967 else if (*type == 's')
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000968 mask = S_IFSOCK;
Matt Kraai096370d2001-02-07 03:52:38 +0000969
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +0200970 if (mask == 0 || type[1] != '\0')
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200971 bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000972
973 return mask;
974}
975#endif
976
Denis Vlasenko4b9b0ce2007-07-01 17:35:17 +0000977#if ENABLE_FEATURE_FIND_PERM \
978 || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \
Matheus Izvekovd0f601f2010-01-06 09:19:04 +0100979 || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000980static const char* plus_minus_num(const char* str)
981{
982 if (*str == '-' || *str == '+')
983 str++;
984 return str;
985}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000986#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000987
Denys Vlasenko6db5f672013-05-16 18:36:42 +0200988/* Say no to GCCism */
989#define USE_NESTED_FUNCTION 0
990
991#if !USE_NESTED_FUNCTION
992struct pp_locals {
993 action*** appp;
994 unsigned cur_group;
995 unsigned cur_action;
996 IF_FEATURE_FIND_NOT( bool invert_flag; )
997};
998static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f)
999{
1000 action *ap = xzalloc(sizeof_struct);
1001 action **app;
1002 action ***group = &ppl->appp[ppl->cur_group];
1003 *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0]));
1004 app[ppl->cur_action++] = ap;
1005 app[ppl->cur_action] = NULL;
1006 ap->f = f;
1007 IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; )
1008 IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; )
1009 return ap;
1010}
1011#endif
1012
Denis Vlasenko769d1e02007-01-22 23:04:27 +00001013static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +00001014{
Denis Vlasenko56fce002007-04-04 23:41:35 +00001015 enum {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001016 OPT_FOLLOW ,
1017 IF_FEATURE_FIND_XDEV( OPT_XDEV ,)
1018 IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,)
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001019 PARM_a ,
1020 PARM_o ,
1021 IF_FEATURE_FIND_NOT( PARM_char_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001022#if ENABLE_DESKTOP
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001023 PARM_and ,
1024 PARM_or ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001025 IF_FEATURE_FIND_NOT( PARM_not ,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001026#endif
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001027 PARM_print ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001028 IF_FEATURE_FIND_PRINT0( PARM_print0 ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001029 IF_FEATURE_FIND_PRUNE( PARM_prune ,)
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001030 IF_FEATURE_FIND_QUIT( PARM_quit ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001031 IF_FEATURE_FIND_DELETE( PARM_delete ,)
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001032 IF_FEATURE_FIND_EMPTY( PARM_empty ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001033 IF_FEATURE_FIND_EXEC( PARM_exec ,)
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001034 IF_FEATURE_FIND_EXECUTABLE(PARM_executable,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001035 IF_FEATURE_FIND_PAREN( PARM_char_brace,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001036 /* All options/actions starting from here require argument */
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001037 PARM_name ,
1038 PARM_iname ,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001039 IF_FEATURE_FIND_PATH( PARM_path ,)
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001040#if ENABLE_DESKTOP
1041 /* -wholename is a synonym for -path */
Denys Vlasenko7c8aa2b2012-05-18 02:53:11 +02001042 /* We support it because Linux kernel's "make tags" uses it */
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001043 IF_FEATURE_FIND_PATH( PARM_wholename ,)
1044#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001045 IF_FEATURE_FIND_PATH( PARM_ipath ,)
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001046 IF_FEATURE_FIND_REGEX( PARM_regex ,)
1047 IF_FEATURE_FIND_TYPE( PARM_type ,)
1048 IF_FEATURE_FIND_PERM( PARM_perm ,)
1049 IF_FEATURE_FIND_MTIME( PARM_mtime ,)
1050 IF_FEATURE_FIND_MMIN( PARM_mmin ,)
1051 IF_FEATURE_FIND_NEWER( PARM_newer ,)
1052 IF_FEATURE_FIND_INUM( PARM_inum ,)
1053 IF_FEATURE_FIND_USER( PARM_user ,)
1054 IF_FEATURE_FIND_GROUP( PARM_group ,)
1055 IF_FEATURE_FIND_SIZE( PARM_size ,)
1056 IF_FEATURE_FIND_CONTEXT(PARM_context ,)
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001057 IF_FEATURE_FIND_LINKS( PARM_links ,)
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001058 IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,)
Denis Vlasenko56fce002007-04-04 23:41:35 +00001059 };
1060
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00001061 static const char params[] ALIGN1 =
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001062 "-follow\0"
1063 IF_FEATURE_FIND_XDEV( "-xdev\0" )
1064 IF_FEATURE_FIND_DEPTH( "-depth\0" )
1065 "-a\0"
1066 "-o\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001067 IF_FEATURE_FIND_NOT( "!\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001068#if ENABLE_DESKTOP
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001069 "-and\0"
1070 "-or\0"
1071 IF_FEATURE_FIND_NOT( "-not\0" )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001072#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001073 "-print\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001074 IF_FEATURE_FIND_PRINT0( "-print0\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001075 IF_FEATURE_FIND_PRUNE( "-prune\0" )
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001076 IF_FEATURE_FIND_QUIT( "-quit\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001077 IF_FEATURE_FIND_DELETE( "-delete\0" )
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001078 IF_FEATURE_FIND_EMPTY( "-empty\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001079 IF_FEATURE_FIND_EXEC( "-exec\0" )
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001080 IF_FEATURE_FIND_EXECUTABLE("-executable\0")
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001081 IF_FEATURE_FIND_PAREN( "(\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001082 /* All options/actions starting from here require argument */
Denys Vlasenko69675782013-01-14 01:34:48 +01001083 "-name\0"
1084 "-iname\0"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001085 IF_FEATURE_FIND_PATH( "-path\0" )
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001086#if ENABLE_DESKTOP
1087 IF_FEATURE_FIND_PATH( "-wholename\0")
1088#endif
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001089 IF_FEATURE_FIND_PATH( "-ipath\0" )
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001090 IF_FEATURE_FIND_REGEX( "-regex\0" )
1091 IF_FEATURE_FIND_TYPE( "-type\0" )
1092 IF_FEATURE_FIND_PERM( "-perm\0" )
1093 IF_FEATURE_FIND_MTIME( "-mtime\0" )
1094 IF_FEATURE_FIND_MMIN( "-mmin\0" )
1095 IF_FEATURE_FIND_NEWER( "-newer\0" )
1096 IF_FEATURE_FIND_INUM( "-inum\0" )
1097 IF_FEATURE_FIND_USER( "-user\0" )
1098 IF_FEATURE_FIND_GROUP( "-group\0" )
1099 IF_FEATURE_FIND_SIZE( "-size\0" )
1100 IF_FEATURE_FIND_CONTEXT("-context\0")
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001101 IF_FEATURE_FIND_LINKS( "-links\0" )
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001102 IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0")
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001103 ;
Denis Vlasenko56fce002007-04-04 23:41:35 +00001104
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001105#if !USE_NESTED_FUNCTION
1106 struct pp_locals ppl;
1107#define appp (ppl.appp )
1108#define cur_group (ppl.cur_group )
1109#define cur_action (ppl.cur_action )
1110#define invert_flag (ppl.invert_flag)
1111#define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name)
1112#else
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001113 action*** appp;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001114 unsigned cur_group;
1115 unsigned cur_action;
1116 IF_FEATURE_FIND_NOT( bool invert_flag; )
Denis Vlasenko56fce002007-04-04 23:41:35 +00001117
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001118 /* This is the only place in busybox where we use nested function.
1119 * So far more standard alternatives were bigger. */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001120 /* Auto decl suppresses "func without a prototype" warning: */
Denis Vlasenko0f3a5802008-03-20 13:13:09 +00001121 auto action* alloc_action(int sizeof_struct, action_fp f);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001122 action* alloc_action(int sizeof_struct, action_fp f)
1123 {
1124 action *ap;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001125 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0]));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001126 appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001127 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001128 ap->f = f;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001129 IF_FEATURE_FIND_NOT( ap->invert = invert_flag; )
1130 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001131 return ap;
1132 }
1133#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001134#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +00001135
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001136 cur_group = 0;
1137 cur_action = 0;
1138 IF_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001139 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001140
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001141 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +00001142 const char *arg = argv[0];
Denis Vlasenko990d0f62007-07-24 15:54:42 +00001143 int parm = index_in_strings(params, arg);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001144 const char *arg1 = argv[1];
1145
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001146 dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type);
1147
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001148 if (parm >= PARM_name) {
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001149 /* All options/actions starting from -name require argument */
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001150 if (!arg1)
1151 bb_error_msg_and_die(bb_msg_requires_arg, arg);
1152 argv++;
1153 }
1154
1155 /* We can use big switch() here, but on i386
1156 * it doesn't give smaller code. Other arches? */
1157
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001158/* Options always return true. They always take effect
1159 * rather than being processed only when their place in the
1160 * expression is reached.
1161 */
1162 /* Options */
Ian Wienandabc33ac2011-08-28 06:53:20 +02001163 if (parm == OPT_FOLLOW) {
Denys Vlasenko982e87f2013-07-30 11:52:58 +02001164 dbg("follow enabled: %d", __LINE__);
1165 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
Ian Wienandabc33ac2011-08-28 06:53:20 +02001166 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001167#if ENABLE_FEATURE_FIND_XDEV
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001168 else if (parm == OPT_XDEV) {
1169 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001170 G.xdev_on = 1;
1171 }
1172#endif
1173#if ENABLE_FEATURE_FIND_MAXDEPTH
1174 else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001175 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001176 G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1);
1177 }
1178#endif
1179#if ENABLE_FEATURE_FIND_DEPTH
1180 else if (parm == OPT_DEPTH) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001181 dbg("%d", __LINE__);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001182 G.recurse_flags |= ACTION_DEPTHFIRST;
1183 }
1184#endif
1185/* Actions are grouped by operators
1186 * ( expr ) Force precedence
1187 * ! expr True if expr is false
1188 * -not expr Same as ! expr
1189 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
1190 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
1191 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
1192 * We implement: (), -a, -o
1193 */
1194 /* Operators */
1195 else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001196 dbg("%d", __LINE__);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001197 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001198 }
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001199 else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001200 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001201 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001202 cur_group++;
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001203 appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0]));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001204 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001205 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001206 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001207 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001208#if ENABLE_FEATURE_FIND_NOT
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001209 else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) {
Denis Vlasenkob2922642007-02-06 17:38:29 +00001210 /* also handles "find ! ! -name 'foo*'" */
1211 invert_flag ^= 1;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001212 dbg("invert_flag:%d", invert_flag);
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001213 }
1214#endif
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001215 /* Actions */
Denis Vlasenko56fce002007-04-04 23:41:35 +00001216 else if (parm == PARM_print) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001217 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001218 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001219 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001220 }
1221#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko56fce002007-04-04 23:41:35 +00001222 else if (parm == PARM_print0) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001223 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001224 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001225 (void) ALLOC_ACTION(print0);
1226 }
Paul Foxd7384292006-05-12 14:47:20 +00001227#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001228#if ENABLE_FEATURE_FIND_PRUNE
1229 else if (parm == PARM_prune) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001230 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001231 (void) ALLOC_ACTION(prune);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001232 }
1233#endif
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001234#if ENABLE_FEATURE_FIND_QUIT
1235 else if (parm == PARM_quit) {
1236 dbg("%d", __LINE__);
1237 (void) ALLOC_ACTION(quit);
1238 }
1239#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001240#if ENABLE_FEATURE_FIND_DELETE
1241 else if (parm == PARM_delete) {
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001242 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001243 G.need_print = 0;
1244 G.recurse_flags |= ACTION_DEPTHFIRST;
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001245 (void) ALLOC_ACTION(delete);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001246 }
Rob Landleycee605c2005-10-06 16:39:17 +00001247#endif
Aaro Koskinenaf18b302019-09-12 12:04:13 +02001248#if ENABLE_FEATURE_FIND_EMPTY
1249 else if (parm == PARM_empty) {
1250 dbg("%d", __LINE__);
1251 (void) ALLOC_ACTION(empty);
1252 }
1253#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001254#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko56fce002007-04-04 23:41:35 +00001255 else if (parm == PARM_exec) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001256 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001257 action_exec *ap;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001258 IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;)
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001259 dbg("%d", __LINE__);
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001260 G.need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001261 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001262 ap->exec_argv = ++argv; /* first arg after -exec */
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001263 /*ap->exec_argc = 0; - ALLOC_ACTION did it */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001264 while (1) {
Bernhard Reutner-Fischer2e73d842010-02-25 13:22:37 +01001265 if (!*argv) /* did not see ';' or '+' until end */
1266 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001267 // find -exec echo Foo ">{}<" ";"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001268 // executes "echo Foo >FILENAME<",
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001269 // find -exec echo Foo ">{}<" "+"
Denys Vlasenkocef85c22011-02-12 16:35:24 +01001270 // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...".
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001271 if ((argv[0][0] == ';' || argv[0][0] == '+')
1272 && argv[0][1] == '\0'
1273 ) {
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001274# if ENABLE_FEATURE_FIND_EXEC_PLUS
1275 if (argv[0][0] == '+')
1276 ap->filelist = xzalloc(sizeof(ap->filelist[0]));
1277# endif
Rob Landley5d3a0e82005-10-04 03:34:39 +00001278 break;
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001279 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001280 argv++;
1281 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +00001282 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001283 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +00001284 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001285 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001286 i = ap->exec_argc;
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001287 while (i--) {
Denys Vlasenko6f068902014-02-27 11:17:06 +01001288 ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001289 IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];)
1290 }
1291# if ENABLE_FEATURE_FIND_EXEC_PLUS
1292 /*
1293 * coreutils expects {} to appear only once in "-exec +"
1294 */
1295 if (all_subst != 1 && ap->filelist)
James Byrne69374872019-07-02 11:35:03 +02001296 bb_simple_error_msg_and_die("only one '{}' allowed for -exec +");
Bartosz Golaszewski14158b42014-06-17 17:09:17 +02001297# endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001298 }
1299#endif
Denis Vlasenko56fce002007-04-04 23:41:35 +00001300#if ENABLE_FEATURE_FIND_PAREN
1301 else if (parm == PARM_char_brace) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001302 action_paren *ap;
1303 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +00001304 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001305
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001306 dbg("%d", __LINE__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001307 endarg = argv;
1308 while (1) {
1309 if (!*++endarg)
James Byrne69374872019-07-02 11:35:03 +02001310 bb_simple_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001311 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001312 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +00001313 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001314 *endarg = NULL;
1315 break;
1316 }
1317 }
1318 ap = ALLOC_ACTION(paren);
1319 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +00001320 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001321 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001322 }
Denis Vlasenko56fce002007-04-04 23:41:35 +00001323#endif
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001324 else if (parm == PARM_name || parm == PARM_iname) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001325 action_name *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001326 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001327 ap = ALLOC_ACTION(name);
1328 ap->pattern = arg1;
Denis Vlasenko75aa6152007-12-09 10:07:39 +00001329 ap->iname = (parm == PARM_iname);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001330 }
1331#if ENABLE_FEATURE_FIND_PATH
Denys Vlasenkobe1cfee2012-05-17 12:25:25 +02001332 else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) {
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001333 action_path *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001334 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001335 ap = ALLOC_ACTION(path);
1336 ap->pattern = arg1;
Bogdan Harjoc7948ecb2011-05-22 03:50:21 +02001337 ap->ipath = (parm == PARM_ipath);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001338 }
1339#endif
1340#if ENABLE_FEATURE_FIND_REGEX
1341 else if (parm == PARM_regex) {
1342 action_regex *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001343 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001344 ap = ALLOC_ACTION(regex);
1345 xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/);
1346 }
1347#endif
1348#if ENABLE_FEATURE_FIND_TYPE
1349 else if (parm == PARM_type) {
1350 action_type *ap;
1351 ap = ALLOC_ACTION(type);
1352 ap->type_mask = find_type(arg1);
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001353 dbg("created:type mask:%x", ap->type_mask);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001354 }
1355#endif
Denys Vlasenko008e73b2018-07-06 21:04:41 +02001356#if ENABLE_FEATURE_FIND_EXECUTABLE
1357 else if (parm == PARM_executable) {
1358 (void) ALLOC_ACTION(executable);
1359 }
1360#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001361#if ENABLE_FEATURE_FIND_PERM
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001362/* -perm BITS File's mode bits are exactly BITS (octal or symbolic).
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001363 * Symbolic modes use mode 0 as a point of departure.
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001364 * -perm -BITS All of the BITS are set in file's mode.
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001365 * -perm [+/]BITS At least one of the BITS is set in file's mode.
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001366 */
1367 else if (parm == PARM_perm) {
1368 action_perm *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001369 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001370 ap = ALLOC_ACTION(perm);
1371 ap->perm_char = arg1[0];
Denys Vlasenko3e9b13e2014-09-05 12:16:15 +02001372 arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1));
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001373 /*ap->perm_mask = 0; - ALLOC_ACTION did it */
Denys Vlasenko5711a2a2015-10-07 17:55:33 +02001374 ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask);
1375 if (ap->perm_mask == (mode_t)-1)
Denys Vlasenko651a2692010-03-23 16:25:17 +01001376 bb_error_msg_and_die("invalid mode '%s'", arg1);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001377 }
1378#endif
1379#if ENABLE_FEATURE_FIND_MTIME
1380 else if (parm == PARM_mtime) {
1381 action_mtime *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001382 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001383 ap = ALLOC_ACTION(mtime);
1384 ap->mtime_char = arg1[0];
1385 ap->mtime_days = xatoul(plus_minus_num(arg1));
1386 }
1387#endif
1388#if ENABLE_FEATURE_FIND_MMIN
1389 else if (parm == PARM_mmin) {
1390 action_mmin *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001391 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001392 ap = ALLOC_ACTION(mmin);
1393 ap->mmin_char = arg1[0];
1394 ap->mmin_mins = xatoul(plus_minus_num(arg1));
1395 }
1396#endif
1397#if ENABLE_FEATURE_FIND_NEWER
1398 else if (parm == PARM_newer) {
1399 struct stat stat_newer;
1400 action_newer *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001401 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001402 ap = ALLOC_ACTION(newer);
1403 xstat(arg1, &stat_newer);
1404 ap->newer_mtime = stat_newer.st_mtime;
1405 }
1406#endif
1407#if ENABLE_FEATURE_FIND_INUM
1408 else if (parm == PARM_inum) {
1409 action_inum *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001410 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001411 ap = ALLOC_ACTION(inum);
1412 ap->inode_num = xatoul(arg1);
1413 }
1414#endif
1415#if ENABLE_FEATURE_FIND_USER
1416 else if (parm == PARM_user) {
1417 action_user *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001418 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001419 ap = ALLOC_ACTION(user);
1420 ap->uid = bb_strtou(arg1, NULL, 10);
1421 if (errno)
1422 ap->uid = xuname2uid(arg1);
1423 }
1424#endif
1425#if ENABLE_FEATURE_FIND_GROUP
1426 else if (parm == PARM_group) {
1427 action_group *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001428 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001429 ap = ALLOC_ACTION(group);
1430 ap->gid = bb_strtou(arg1, NULL, 10);
1431 if (errno)
1432 ap->gid = xgroup2gid(arg1);
1433 }
1434#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001435#if ENABLE_FEATURE_FIND_SIZE
1436 else if (parm == PARM_size) {
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001437/* -size n[bckw]: file uses n units of space
1438 * b (default): units are 512-byte blocks
1439 * c: 1 byte
1440 * k: kilobytes
1441 * w: 2-byte words
1442 */
1443#if ENABLE_LFS
1444#define XATOU_SFX xatoull_sfx
1445#else
1446#define XATOU_SFX xatoul_sfx
1447#endif
Denys Vlasenko965b7952020-11-30 13:03:03 +01001448 static const struct suffix_mult find_suffixes[] ALIGN_SUFFIX = {
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001449 { "c", 1 },
1450 { "w", 2 },
Denis Vlasenkof8689632007-07-27 15:06:25 +00001451 { "", 512 },
Denis Vlasenko99fcc622007-07-01 17:29:44 +00001452 { "b", 512 },
1453 { "k", 1024 },
Denys Vlasenko043b1e52009-09-06 12:47:55 +02001454 { "", 0 }
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001455 };
Denis Vlasenkob9411292007-06-15 23:43:11 +00001456 action_size *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001457 dbg("%d", __LINE__);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001458 ap = ALLOC_ACTION(size);
Denis Vlasenko53a0e972007-06-16 00:30:52 +00001459 ap->size_char = arg1[0];
1460 ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes);
Denis Vlasenkob9411292007-06-15 23:43:11 +00001461 }
1462#endif
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001463#if ENABLE_FEATURE_FIND_CONTEXT
1464 else if (parm == PARM_context) {
1465 action_context *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001466 dbg("%d", __LINE__);
Denis Vlasenkoa19faf82007-06-19 22:22:57 +00001467 ap = ALLOC_ACTION(context);
Denys Vlasenkoa61ff512010-08-31 19:17:42 +02001468 /*ap->context = NULL; - ALLOC_ACTION did it */
Denis Vlasenko838daca2007-08-16 10:41:24 +00001469 /* SELinux headers erroneously declare non-const parameter */
1470 if (selinux_raw_to_trans_context((char*)arg1, &ap->context))
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +00001471 bb_simple_perror_msg(arg1);
Denis Vlasenko62f04792007-04-13 10:00:12 +00001472 }
1473#endif
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001474#if ENABLE_FEATURE_FIND_LINKS
1475 else if (parm == PARM_links) {
1476 action_links *ap;
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001477 dbg("%d", __LINE__);
Matheus Izvekovd0f601f2010-01-06 09:19:04 +01001478 ap = ALLOC_ACTION(links);
1479 ap->links_char = arg1[0];
1480 ap->links_count = xatoul(plus_minus_num(arg1));
1481 }
1482#endif
Denis Vlasenkob9411292007-06-15 23:43:11 +00001483 else {
1484 bb_error_msg("unrecognized: %s", arg);
Manuel Novoa III cad53642003-03-19 09:13:01 +00001485 bb_show_usage();
Denis Vlasenkob9411292007-06-15 23:43:11 +00001486 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001487 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +00001488 }
Denys Vlasenko87249fb2011-08-11 02:27:13 +02001489 dbg("exiting %s", __func__);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001490 return appp;
1491#undef ALLOC_ACTION
Denys Vlasenko6db5f672013-05-16 18:36:42 +02001492#undef appp
1493#undef cur_action
1494#undef invert_flag
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001495}
1496
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +00001497int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko2ec91ae2010-01-04 14:15:38 +01001498int find_main(int argc UNUSED_PARAM, char **argv)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001499{
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001500 int i, firstopt;
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001501 char **past_HLP, *saved;
Denis Vlasenko7d6c9292007-04-05 00:35:43 +00001502
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001503 INIT_G();
1504
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001505 /* "find -type f" + getopt("+HLP") => disaster.
1506 * Need to avoid getopt running into a non-HLP option.
1507 * Do this by temporarily storing NULL there:
1508 */
1509 past_HLP = argv;
1510 for (;;) {
1511 saved = *++past_HLP;
1512 if (!saved)
1513 break;
1514 if (saved[0] != '-')
1515 break;
1516 if (!saved[1])
1517 break; /* it is "-" */
Denys Vlasenko2d9e0972019-01-06 19:58:18 +01001518 if (saved[1] == '-' && !saved[2]) {
1519 /* it is "--" */
1520 /* Try: find -- /dev/null */
1521 saved = *++past_HLP;
1522 break;
1523 }
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001524 if ((saved+1)[strspn(saved+1, "HLP")] != '\0')
1525 break;
1526 }
1527 *past_HLP = NULL;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001528 /* "+": stop on first non-option */
Denys Vlasenkoef2366c2020-12-12 08:44:32 +01001529 i = getopt32(argv, "+""HLP");
Denys Vlasenko6782f482013-05-14 17:20:35 +02001530 if (i & (1<<0))
1531 G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK;
1532 if (i & (1<<1))
1533 G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK;
1534 /* -P is default and is ignored */
Denys Vlasenko6eb0cbe2014-01-09 16:07:11 +01001535 argv = past_HLP; /* same result as "argv += optind;" */
1536 *past_HLP = saved;
Denys Vlasenko6782f482013-05-14 17:20:35 +02001537
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001538 for (firstopt = 0; argv[firstopt]; firstopt++) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001539 if (argv[firstopt][0] == '-')
1540 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +00001541 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
1542 break;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001543 if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001544 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001545 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001546 if (firstopt == 0) {
1547 *--argv = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +00001548 firstopt++;
1549 }
1550
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001551 G.actions = parse_params(&argv[firstopt]);
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001552 argv[firstopt] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +00001553
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001554#if ENABLE_FEATURE_FIND_XDEV
1555 if (G.xdev_on) {
1556 struct stat stbuf;
1557
1558 G.xdev_count = firstopt;
1559 G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0]));
1560 for (i = 0; argv[i]; i++) {
1561 /* not xstat(): shouldn't bomb out on
1562 * "find not_exist exist -xdev" */
1563 if (stat(argv[i], &stbuf) == 0)
1564 G.xdev_dev[i] = stbuf.st_dev;
1565 /* else G.xdev_dev[i] stays 0 and
1566 * won't match any real device dev_t
1567 */
1568 }
1569 }
1570#endif
1571
1572 for (i = 0; argv[i]; i++) {
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001573 if (!recursive_action(argv[i],
Denys Vlasenko8f7a6d22009-09-29 11:07:04 +02001574 G.recurse_flags,/* flags */
Bernhard Reutner-Fischer3e816c12007-03-29 10:30:50 +00001575 fileAction, /* file action */
1576 fileAction, /* dir action */
Denys Vlasenko689d0652020-10-01 21:52:16 +02001577 NULL) /* user data */
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001578 ) {
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001579 G.exitstatus |= EXIT_FAILURE;
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001580 }
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +00001581 }
Denys Vlasenko053c12e2011-02-12 20:27:13 +01001582
Denys Vlasenkoabe22cd2018-07-06 20:35:35 +02001583 IF_FEATURE_FIND_EXEC_PLUS(G.exitstatus |= flush_exec_plus();)
1584 return G.exitstatus;
Eric Andersen17d49ef1999-10-06 20:25:32 +00001585}