Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini find implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | c499601 | 1999-10-20 22:08:37 +0000 | [diff] [blame] | 6 | * |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 7 | * Reworked by David Douthitt <n9ubh@callsign.net> and |
| 8 | * Matt Kraai <kraai@alumni.carnegiemellon.edu>. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 9 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 10 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 11 | */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 12 | /* 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 Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 17 | * find: missing argument to '-exec' |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 18 | * # 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 Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 25 | * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';' |
| 26 | * file.txt file.txt |
| 27 | * file.txt |
| 28 | * /tmp |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 29 | * # find -name '*.c' -o -name '*.h' |
| 30 | * [shows files, *.c and *.h intermixed] |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 31 | * # 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 Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 37 | * |
| 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 Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 45 | */ |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 46 | /* 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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 53 | //config:config FIND |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 54 | //config: bool "find (14 kb)" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 55 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 56 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 57 | //config: find is used to search your system to find specified files. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 58 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 64 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 68 | //config: |
| 69 | //config:config FEATURE_FIND_MTIME |
| 70 | //config: bool "Enable -mtime: modified time matching" |
| 71 | //config: default y |
| 72 | //config: depends on FIND |
| 73 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 74 | //config: Allow searching based on the modification time of |
| 75 | //config: files, in days. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 76 | //config: |
| 77 | //config:config FEATURE_FIND_MMIN |
| 78 | //config: bool "Enable -mmin: modified time matching by minutes" |
| 79 | //config: default y |
| 80 | //config: depends on FIND |
| 81 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 82 | //config: Allow searching based on the modification time of |
| 83 | //config: files, in minutes. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 84 | //config: |
| 85 | //config:config FEATURE_FIND_PERM |
| 86 | //config: bool "Enable -perm: permissions matching" |
| 87 | //config: default y |
| 88 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 89 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 95 | //config: Enable searching based on file type (file, |
| 96 | //config: directory, socket, device, etc.). |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 97 | //config: |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 98 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 103 | //config:config FEATURE_FIND_XDEV |
| 104 | //config: bool "Enable -xdev: 'stay in filesystem'" |
| 105 | //config: default y |
| 106 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 107 | //config: |
| 108 | //config:config FEATURE_FIND_MAXDEPTH |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 109 | //config: bool "Enable -mindepth N and -maxdepth N" |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 110 | //config: default y |
| 111 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 112 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 118 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 120 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 125 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 131 | //config: Support the 'find -exec' option for executing commands based upon |
| 132 | //config: the files matched. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 133 | //config: |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 134 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 139 | //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 Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 143 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 144 | //config:config FEATURE_FIND_USER |
| 145 | //config: bool "Enable -user: username/uid matching" |
| 146 | //config: default y |
| 147 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 148 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 153 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 159 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 162 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 168 | //config: Process each directory's contents before the directory itself. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 169 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 175 | //config: Enable usage of parens '(' to specify logical order of arguments. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 176 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 181 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 187 | //config: If the file is a directory, don't descend into it. Useful for |
| 188 | //config: exclusion .svn and CVS directories. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 189 | //config: |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 190 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 197 | //config:config FEATURE_FIND_DELETE |
| 198 | //config: bool "Enable -delete: delete files/dirs" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 199 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 200 | //config: depends on FIND && FEATURE_FIND_DEPTH |
| 201 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 202 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 205 | //config: |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 206 | //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 Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 214 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 219 | //config: The -path option matches whole pathname instead of just filename. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 220 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 226 | //config: The -regex option matches whole pathname against regular expression. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 227 | //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 Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 233 | //config: Support the 'find -context' option for matching security context. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 234 | //config: |
| 235 | //config:config FEATURE_FIND_LINKS |
| 236 | //config: bool "Enable -links: link count matching" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 237 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 238 | //config: depends on FIND |
| 239 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 240 | //config: Support the 'find -links' option for matching number of links. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 241 | |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 242 | //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 Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 247 | //usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 248 | //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 Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 252 | //usage: "\n -L,-follow Follow symlinks" |
| 253 | //usage: "\n -H ...on command line only" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 254 | //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 Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 280 | //usage: "\n -ipath PATTERN Case insensitive -path" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 281 | //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 Vlasenko | f4eaccb | 2018-04-24 10:13:45 +0200 | [diff] [blame] | 286 | //usage: "\n -type X File type is X (one of: f,d,l,b,c,s,p)" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 287 | //usage: ) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 288 | //usage: IF_FEATURE_FIND_EXECUTABLE( |
| 289 | //usage: "\n -executable File is executable" |
| 290 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 291 | //usage: IF_FEATURE_FIND_PERM( |
| 292 | //usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK)," |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 293 | //usage: "\n or exactly MASK bits are set in file's mode" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 294 | //usage: ) |
| 295 | //usage: IF_FEATURE_FIND_MTIME( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 296 | //usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N)," |
| 297 | //usage: "\n or exactly N days in the past" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 298 | //usage: ) |
| 299 | //usage: IF_FEATURE_FIND_MMIN( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 300 | //usage: "\n -mmin MINS mtime is greater than (+N), less than (-N)," |
| 301 | //usage: "\n or exactly N minutes in the past" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 302 | //usage: ) |
| 303 | //usage: IF_FEATURE_FIND_NEWER( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 304 | //usage: "\n -newer FILE mtime is more recent than FILE's" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 305 | //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 Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 310 | //usage: "\n -user NAME/ID File is owned by given user" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 311 | //usage: ) |
| 312 | //usage: IF_FEATURE_FIND_GROUP( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 313 | //usage: "\n -group NAME/ID File is owned by given group" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 314 | //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 Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 326 | //usage: IF_FEATURE_FIND_EMPTY( |
| 327 | //usage: "\n -empty Match empty file/directory" |
| 328 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 329 | //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 Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 341 | //usage: IF_FEATURE_FIND_EXEC_PLUS( |
| 342 | //usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names" |
| 343 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 344 | //usage: IF_FEATURE_FIND_DELETE( |
| 345 | //usage: "\n -delete Delete current file/directory. Turns on -depth option" |
| 346 | //usage: ) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 347 | //usage: IF_FEATURE_FIND_QUIT( |
| 348 | //usage: "\n -quit Exit" |
| 349 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 350 | //usage: |
| 351 | //usage:#define find_example_usage |
| 352 | //usage: "$ find / -name passwd\n" |
| 353 | //usage: "/etc/passwd\n" |
| 354 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 355 | #include <fnmatch.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 356 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 357 | #include "common_bufsiz.h" |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 358 | #if ENABLE_FEATURE_FIND_REGEX |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 359 | # include "xregex.h" |
| 360 | #endif |
| 361 | /* GNUism: */ |
| 362 | #ifndef FNM_CASEFOLD |
| 363 | # define FNM_CASEFOLD 0 |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 364 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 365 | |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 366 | #if 1 |
| 367 | # define dbg(...) ((void)0) |
| 368 | #else |
| 369 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 370 | #endif |
| 371 | |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 372 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 373 | /* This is a NOEXEC applet. Be very careful! */ |
| 374 | |
| 375 | |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 376 | typedef int (*action_fp)(const char *fileName, const struct stat *statbuf, void *) FAST_FUNC; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 377 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 378 | typedef struct { |
| 379 | action_fp f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 380 | #if ENABLE_FEATURE_FIND_NOT |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 381 | bool invert; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 382 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 383 | } action; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 384 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 385 | #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 386 | #define ACTF(name) \ |
| 387 | static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \ |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 388 | const struct stat *statbuf UNUSED_PARAM, \ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 389 | action_##name* ap UNUSED_PARAM) |
| 390 | |
| 391 | ACTS(print) |
| 392 | ACTS(name, const char *pattern; bool iname;) |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 393 | IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 394 | IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;)) |
| 395 | IF_FEATURE_FIND_PRINT0( ACTS(print0)) |
| 396 | IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 397 | IF_FEATURE_FIND_EXECUTABLE(ACTS(executable)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 398 | IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;)) |
| 399 | IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;)) |
| 400 | IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;)) |
| 401 | IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) |
| 402 | IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) |
| 403 | IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;)) |
| 404 | IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;)) |
| 405 | IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;)) |
| 406 | IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;)) |
| 407 | IF_FEATURE_FIND_PRUNE( ACTS(prune)) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 408 | IF_FEATURE_FIND_QUIT( ACTS(quit)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 409 | IF_FEATURE_FIND_DELETE( ACTS(delete)) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 410 | IF_FEATURE_FIND_EMPTY( ACTS(empty)) |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 411 | IF_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 Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 425 | IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;)) |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 426 | IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 427 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 428 | struct globals { |
| 429 | IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;) |
| 430 | IF_FEATURE_FIND_XDEV(int xdev_count;) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 431 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 432 | int minmaxdepth[2]; |
| 433 | #endif |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 434 | action ***actions; |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 435 | smallint need_print; |
| 436 | smallint xdev_on; |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 437 | smalluint exitstatus; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 438 | recurse_flags_t recurse_flags; |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 439 | IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;) |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 440 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 441 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 442 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 443 | setup_common_bufsiz(); \ |
Denys Vlasenko | 7b85ec3 | 2015-10-13 17:17:34 +0200 | [diff] [blame] | 444 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 445 | /* we have to zero it out because of NOEXEC */ \ |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 446 | memset(&G, 0, sizeof(G)); \ |
| 447 | IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \ |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 448 | IF_FEATURE_FIND_EXEC_PLUS(G.max_argv_len = bb_arg_max() - 2048;) \ |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 449 | G.need_print = 1; \ |
| 450 | G.recurse_flags = ACTION_RECURSE; \ |
| 451 | } while (0) |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 452 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 453 | /* 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 Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 457 | |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 458 | static int exec_actions(action ***appp, const char *fileName, const struct stat *statbuf) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 459 | { |
| 460 | int cur_group; |
| 461 | int cur_action; |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 462 | int rc = 0; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 463 | action **app, *ap; |
| 464 | |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 465 | /* "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 Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 482 | cur_group = -1; |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 483 | while ((app = appp[++cur_group]) != NULL) { |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 484 | rc &= ~TRUE; /* 'success' so far, clear TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 485 | cur_action = -1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 486 | while (1) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 487 | ap = app[++cur_action]; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 488 | if (!ap) /* all actions in group were successful */ |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 489 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 490 | rc |= TRUE ^ ap->f(fileName, statbuf, ap); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 491 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 492 | if (ap->invert) rc ^= TRUE; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 493 | #endif |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 494 | dbg("grp %d action %d rc:0x%x", cur_group, cur_action, rc); |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 495 | if (rc & TRUE) /* current group failed, try next */ |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 496 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 499 | dbg("returning:0x%x", rc ^ TRUE); |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 500 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 501 | } |
| 502 | |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 503 | #if !FNM_CASEFOLD |
| 504 | static 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 Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 519 | ACTF(name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 520 | { |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 521 | int r; |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 522 | const char *tmp = bb_basename(fileName); |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 523 | /* 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 Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 553 | } |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 554 | |
Denys Vlasenko | 9138817 | 2010-04-26 09:54:04 +0200 | [diff] [blame] | 555 | /* 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 Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 559 | if (trunc_slash) *trunc_slash = '\0'; |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 560 | #if FNM_CASEFOLD |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 561 | r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)); |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 562 | #else |
| 563 | if (ap->iname) |
| 564 | tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp); |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 565 | r = fnmatch(ap->pattern, tmp, 0); |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 566 | #endif |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 567 | if (trunc_slash) *trunc_slash = '/'; |
| 568 | return r == 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 569 | } |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 570 | |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 571 | #if ENABLE_FEATURE_FIND_PATH |
| 572 | ACTF(path) |
| 573 | { |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 574 | # if FNM_CASEFOLD |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 575 | return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0; |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 576 | # 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 Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 581 | } |
| 582 | #endif |
| 583 | #if ENABLE_FEATURE_FIND_REGEX |
| 584 | ACTF(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 Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 596 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 597 | ACTF(type) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 598 | { |
Denis Vlasenko | 31c65f2 | 2006-10-31 23:39:37 +0000 | [diff] [blame] | 599 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 600 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 601 | #endif |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 602 | #if ENABLE_FEATURE_FIND_EXECUTABLE |
| 603 | ACTF(executable) |
| 604 | { |
| 605 | return access(fileName, X_OK) == 0; |
| 606 | } |
| 607 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 608 | #if ENABLE_FEATURE_FIND_PERM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 609 | ACTF(perm) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 610 | { |
Denys Vlasenko | 3e9b13e | 2014-09-05 12:16:15 +0200 | [diff] [blame] | 611 | /* -perm [+/]mode: at least one of perm_mask bits are set */ |
| 612 | if (ap->perm_char == '+' || ap->perm_char == '/') |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 613 | 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 Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 619 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 620 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 621 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 622 | ACTF(mtime) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 623 | { |
| 624 | time_t file_age = time(NULL) - statbuf->st_mtime; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 625 | time_t mtime_secs = ap->mtime_days * 24*60*60; |
| 626 | if (ap->mtime_char == '+') |
| 627 | return file_age >= mtime_secs + 24*60*60; |
| 628 | if (ap->mtime_char == '-') |
| 629 | return file_age < mtime_secs; |
| 630 | /* just numeric mtime */ |
| 631 | return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 632 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 633 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 634 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 635 | ACTF(mmin) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 636 | { |
| 637 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 638 | time_t mmin_secs = ap->mmin_mins * 60; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 639 | if (ap->mmin_char == '+') |
| 640 | return file_age >= mmin_secs + 60; |
| 641 | if (ap->mmin_char == '-') |
| 642 | return file_age < mmin_secs; |
| 643 | /* just numeric mmin */ |
| 644 | return file_age >= mmin_secs && file_age < (mmin_secs + 60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 645 | } |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 646 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 647 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 648 | ACTF(newer) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 649 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 650 | return (ap->newer_mtime < statbuf->st_mtime); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 651 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 652 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 653 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 654 | ACTF(inum) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 655 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 656 | return (statbuf->st_ino == ap->inode_num); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 657 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 658 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 659 | #if ENABLE_FEATURE_FIND_EXEC |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 660 | static int do_exec(action_exec *ap, const char *fileName) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 661 | { |
| 662 | int i, rc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 663 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 664 | int size = ap->exec_argc + ap->filelist_idx + 1; |
| 665 | # else |
| 666 | int size = ap->exec_argc + 1; |
| 667 | # endif |
| 668 | # if ENABLE_USE_PORTABLE_CODE |
| 669 | char **argv = alloca(sizeof(char*) * size); |
| 670 | # else /* gcc 4.3.1 generates smaller code: */ |
| 671 | char *argv[size]; |
| 672 | # endif |
| 673 | char **pp = argv; |
| 674 | |
| 675 | for (i = 0; i < ap->exec_argc; i++) { |
| 676 | const char *arg = ap->exec_argv[i]; |
| 677 | |
| 678 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 679 | if (ap->filelist) { |
| 680 | /* Handling "-exec +" |
| 681 | * Only one exec_argv[i] has substitution in it. |
| 682 | * Expand that one exec_argv[i] into file list. |
| 683 | */ |
| 684 | if (ap->subst_count[i] == 0) { |
| 685 | *pp++ = xstrdup(arg); |
| 686 | } else { |
| 687 | int j = 0; |
| 688 | while (ap->filelist[j]) { |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 689 | /* 2nd arg here should be ap->subst_count[i], but it is always 1: */ |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 690 | *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]); |
| 691 | free(ap->filelist[j]); |
| 692 | j++; |
| 693 | } |
| 694 | } |
| 695 | } else |
| 696 | # endif |
| 697 | { |
| 698 | /* Handling "-exec ;" */ |
| 699 | *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName); |
| 700 | } |
| 701 | } |
| 702 | *pp = NULL; /* terminate the list */ |
| 703 | |
| 704 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 705 | if (ap->filelist) { |
| 706 | ap->filelist[0] = NULL; |
| 707 | ap->filelist_idx = 0; |
| 708 | ap->file_len = 0; |
| 709 | } |
| 710 | # endif |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 711 | |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 712 | rc = spawn_and_wait(argv); |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 713 | if (rc < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 714 | bb_simple_perror_msg(argv[0]); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 715 | |
| 716 | i = 0; |
| 717 | while (argv[i]) |
| 718 | free(argv[i++]); |
| 719 | return rc == 0; /* return 1 if exitcode 0 */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 720 | } |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 721 | ACTF(exec) |
| 722 | { |
| 723 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 724 | if (ap->filelist) { |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 725 | int rc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 726 | |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 727 | ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx); |
| 728 | ap->filelist[ap->filelist_idx++] = xstrdup(fileName); |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 729 | ap->file_len += strlen(fileName) + sizeof(char*) + 1; |
| 730 | /* If we have lots of files already, exec the command */ |
| 731 | rc = 1; |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 732 | if (ap->file_len >= G.max_argv_len) |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 733 | rc = do_exec(ap, NULL); |
| 734 | return rc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 735 | } |
| 736 | # endif |
| 737 | return do_exec(ap, fileName); |
| 738 | } |
| 739 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 740 | static int flush_exec_plus(void) |
| 741 | { |
| 742 | action *ap; |
| 743 | action **app; |
| 744 | action ***appp = G.actions; |
| 745 | while ((app = *appp++) != NULL) { |
| 746 | while ((ap = *app++) != NULL) { |
| 747 | if (ap->f == (action_fp)func_exec) { |
| 748 | action_exec *ae = (void*)ap; |
| 749 | if (ae->filelist_idx != 0) { |
| 750 | int rc = do_exec(ae, NULL); |
| 751 | # if ENABLE_FEATURE_FIND_NOT |
| 752 | if (ap->invert) rc = !rc; |
| 753 | # endif |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 754 | if (rc == 0) |
| 755 | return 1; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | } |
| 759 | } |
| 760 | return 0; |
| 761 | } |
| 762 | # endif |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 763 | #endif |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 764 | #if ENABLE_FEATURE_FIND_USER |
| 765 | ACTF(user) |
| 766 | { |
| 767 | return (statbuf->st_uid == ap->uid); |
| 768 | } |
| 769 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 770 | #if ENABLE_FEATURE_FIND_GROUP |
| 771 | ACTF(group) |
| 772 | { |
| 773 | return (statbuf->st_gid == ap->gid); |
| 774 | } |
| 775 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 776 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 777 | ACTF(print0) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 778 | { |
| 779 | printf("%s%c", fileName, '\0'); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 780 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 781 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 782 | #endif |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 783 | ACTF(print) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 784 | { |
| 785 | puts(fileName); |
| 786 | return TRUE; |
| 787 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 788 | #if ENABLE_FEATURE_FIND_PAREN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 789 | ACTF(paren) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 790 | { |
| 791 | return exec_actions(ap->subexpr, fileName, statbuf); |
| 792 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 793 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 794 | #if ENABLE_FEATURE_FIND_SIZE |
| 795 | ACTF(size) |
| 796 | { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 797 | if (ap->size_char == '+') |
| 798 | return statbuf->st_size > ap->size; |
| 799 | if (ap->size_char == '-') |
| 800 | return statbuf->st_size < ap->size; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 801 | return statbuf->st_size == ap->size; |
| 802 | } |
| 803 | #endif |
Bernhard Reutner-Fischer | 6d79dd6 | 2007-04-05 09:40:12 +0000 | [diff] [blame] | 804 | #if ENABLE_FEATURE_FIND_PRUNE |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 805 | /* |
| 806 | * -prune: if -depth is not given, return true and do not descend |
| 807 | * current dir; if -depth is given, return false with no effect. |
| 808 | * Example: |
| 809 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print |
| 810 | */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 811 | ACTF(prune) |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 812 | { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 813 | return SKIP + TRUE; |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 814 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 815 | #endif |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 816 | #if ENABLE_FEATURE_FIND_QUIT |
| 817 | ACTF(quit) |
| 818 | { |
| 819 | exit(G.exitstatus); |
| 820 | } |
| 821 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 822 | #if ENABLE_FEATURE_FIND_DELETE |
| 823 | ACTF(delete) |
| 824 | { |
| 825 | int rc; |
| 826 | if (S_ISDIR(statbuf->st_mode)) { |
Denys Vlasenko | 8c0708a | 2015-10-07 21:01:47 +0200 | [diff] [blame] | 827 | /* "find . -delete" skips rmdir(".") */ |
| 828 | rc = 0; |
| 829 | if (NOT_LONE_CHAR(fileName, '.')) |
| 830 | rc = rmdir(fileName); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 831 | } else { |
| 832 | rc = unlink(fileName); |
| 833 | } |
| 834 | if (rc < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 835 | bb_simple_perror_msg(fileName); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 836 | return TRUE; |
| 837 | } |
| 838 | #endif |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 839 | #if ENABLE_FEATURE_FIND_EMPTY |
| 840 | ACTF(empty) |
| 841 | { |
| 842 | if (S_ISDIR(statbuf->st_mode)) { |
| 843 | DIR *dir; |
| 844 | struct dirent *dent; |
| 845 | |
| 846 | dir = opendir(fileName); |
| 847 | if (!dir) { |
| 848 | bb_simple_perror_msg(fileName); |
| 849 | return FALSE; |
| 850 | } |
| 851 | |
| 852 | while ((dent = readdir(dir)) != NULL |
| 853 | && DOT_OR_DOTDOT(dent->d_name) |
| 854 | ) { |
| 855 | continue; |
| 856 | } |
| 857 | closedir(dir); |
| 858 | return dent == NULL; |
| 859 | } |
| 860 | return S_ISREG(statbuf->st_mode) && statbuf->st_size == 0; |
| 861 | } |
| 862 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 863 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 864 | ACTF(context) |
| 865 | { |
| 866 | security_context_t con; |
| 867 | int rc; |
| 868 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 869 | if (G.recurse_flags & ACTION_FOLLOWLINKS) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 870 | rc = getfilecon(fileName, &con); |
| 871 | } else { |
| 872 | rc = lgetfilecon(fileName, &con); |
| 873 | } |
| 874 | if (rc < 0) |
| 875 | return FALSE; |
| 876 | rc = strcmp(ap->context, con); |
| 877 | freecon(con); |
| 878 | return rc == 0; |
| 879 | } |
| 880 | #endif |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 881 | #if ENABLE_FEATURE_FIND_LINKS |
| 882 | ACTF(links) |
| 883 | { |
| 884 | switch(ap->links_char) { |
| 885 | case '-' : return (statbuf->st_nlink < ap->links_count); |
| 886 | case '+' : return (statbuf->st_nlink > ap->links_count); |
| 887 | default: return (statbuf->st_nlink == ap->links_count); |
| 888 | } |
| 889 | } |
| 890 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 891 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 892 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 893 | struct stat *statbuf, |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 894 | void *userData UNUSED_PARAM, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 895 | int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM)) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 896 | { |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 897 | int r; |
Denys Vlasenko | 2dc1a97 | 2012-09-06 15:24:11 +0200 | [diff] [blame] | 898 | int same_fs = 1; |
| 899 | |
| 900 | #if ENABLE_FEATURE_FIND_XDEV |
| 901 | if (S_ISDIR(statbuf->st_mode) && G.xdev_count) { |
| 902 | int i; |
| 903 | for (i = 0; i < G.xdev_count; i++) { |
| 904 | if (G.xdev_dev[i] == statbuf->st_dev) |
| 905 | goto found; |
| 906 | } |
| 907 | //bb_error_msg("'%s': not same fs", fileName); |
| 908 | same_fs = 0; |
| 909 | found: ; |
| 910 | } |
| 911 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 912 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 913 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
Denys Vlasenko | 2dc1a97 | 2012-09-06 15:24:11 +0200 | [diff] [blame] | 914 | if (depth < G.minmaxdepth[0]) { |
| 915 | if (same_fs) |
| 916 | return TRUE; /* skip this, continue recursing */ |
| 917 | return SKIP; /* stop recursing */ |
| 918 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 919 | if (depth > G.minmaxdepth[1]) |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 920 | return SKIP; /* stop recursing */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 921 | #endif |
| 922 | |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 923 | r = exec_actions(G.actions, fileName, statbuf); |
| 924 | /* Had no explicit -print[0] or -exec? then print */ |
| 925 | if ((r & TRUE) && G.need_print) |
| 926 | puts(fileName); |
| 927 | |
| 928 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 929 | if (S_ISDIR(statbuf->st_mode)) { |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 930 | if (depth == G.minmaxdepth[1]) |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 931 | return SKIP; |
| 932 | } |
| 933 | #endif |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 934 | /* -xdev stops on mountpoints, but AFTER mountpoit itself |
| 935 | * is processed as usual */ |
Denys Vlasenko | 2dc1a97 | 2012-09-06 15:24:11 +0200 | [diff] [blame] | 936 | if (!same_fs) { |
| 937 | return SKIP; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 938 | } |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 939 | |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 940 | /* Cannot return 0: our caller, recursive_action(), |
| 941 | * will perror() and skip dirs (if called on dir) */ |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 942 | return (r & SKIP) ? SKIP : TRUE; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 943 | } |
| 944 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 945 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 946 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 947 | static int find_type(const char *type) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 948 | { |
| 949 | int mask = 0; |
| 950 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 951 | if (*type == 'b') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 952 | mask = S_IFBLK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 953 | else if (*type == 'c') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 954 | mask = S_IFCHR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 955 | else if (*type == 'd') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 956 | mask = S_IFDIR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 957 | else if (*type == 'p') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 958 | mask = S_IFIFO; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 959 | else if (*type == 'f') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 960 | mask = S_IFREG; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 961 | else if (*type == 'l') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 962 | mask = S_IFLNK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 963 | else if (*type == 's') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 964 | mask = S_IFSOCK; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 965 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 966 | if (mask == 0 || type[1] != '\0') |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 967 | bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 968 | |
| 969 | return mask; |
| 970 | } |
| 971 | #endif |
| 972 | |
Denis Vlasenko | 4b9b0ce | 2007-07-01 17:35:17 +0000 | [diff] [blame] | 973 | #if ENABLE_FEATURE_FIND_PERM \ |
| 974 | || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \ |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 975 | || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 976 | static const char* plus_minus_num(const char* str) |
| 977 | { |
| 978 | if (*str == '-' || *str == '+') |
| 979 | str++; |
| 980 | return str; |
| 981 | } |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 982 | #endif |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 983 | |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 984 | /* Say no to GCCism */ |
| 985 | #define USE_NESTED_FUNCTION 0 |
| 986 | |
| 987 | #if !USE_NESTED_FUNCTION |
| 988 | struct pp_locals { |
| 989 | action*** appp; |
| 990 | unsigned cur_group; |
| 991 | unsigned cur_action; |
| 992 | IF_FEATURE_FIND_NOT( bool invert_flag; ) |
| 993 | }; |
| 994 | static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f) |
| 995 | { |
| 996 | action *ap = xzalloc(sizeof_struct); |
| 997 | action **app; |
| 998 | action ***group = &ppl->appp[ppl->cur_group]; |
| 999 | *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0])); |
| 1000 | app[ppl->cur_action++] = ap; |
| 1001 | app[ppl->cur_action] = NULL; |
| 1002 | ap->f = f; |
| 1003 | IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; ) |
| 1004 | IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; ) |
| 1005 | return ap; |
| 1006 | } |
| 1007 | #endif |
| 1008 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 1009 | static action*** parse_params(char **argv) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1010 | { |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1011 | enum { |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1012 | OPT_FOLLOW , |
| 1013 | IF_FEATURE_FIND_XDEV( OPT_XDEV ,) |
| 1014 | IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,) |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1015 | PARM_a , |
| 1016 | PARM_o , |
| 1017 | IF_FEATURE_FIND_NOT( PARM_char_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1018 | #if ENABLE_DESKTOP |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1019 | PARM_and , |
| 1020 | PARM_or , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1021 | IF_FEATURE_FIND_NOT( PARM_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1022 | #endif |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1023 | PARM_print , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1024 | IF_FEATURE_FIND_PRINT0( PARM_print0 ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1025 | IF_FEATURE_FIND_PRUNE( PARM_prune ,) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1026 | IF_FEATURE_FIND_QUIT( PARM_quit ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1027 | IF_FEATURE_FIND_DELETE( PARM_delete ,) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 1028 | IF_FEATURE_FIND_EMPTY( PARM_empty ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1029 | IF_FEATURE_FIND_EXEC( PARM_exec ,) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 1030 | IF_FEATURE_FIND_EXECUTABLE(PARM_executable,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1031 | IF_FEATURE_FIND_PAREN( PARM_char_brace,) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1032 | /* All options/actions starting from here require argument */ |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1033 | PARM_name , |
| 1034 | PARM_iname , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1035 | IF_FEATURE_FIND_PATH( PARM_path ,) |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1036 | #if ENABLE_DESKTOP |
| 1037 | /* -wholename is a synonym for -path */ |
Denys Vlasenko | 7c8aa2b | 2012-05-18 02:53:11 +0200 | [diff] [blame] | 1038 | /* We support it because Linux kernel's "make tags" uses it */ |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1039 | IF_FEATURE_FIND_PATH( PARM_wholename ,) |
| 1040 | #endif |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 1041 | IF_FEATURE_FIND_PATH( PARM_ipath ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1042 | IF_FEATURE_FIND_REGEX( PARM_regex ,) |
| 1043 | IF_FEATURE_FIND_TYPE( PARM_type ,) |
| 1044 | IF_FEATURE_FIND_PERM( PARM_perm ,) |
| 1045 | IF_FEATURE_FIND_MTIME( PARM_mtime ,) |
| 1046 | IF_FEATURE_FIND_MMIN( PARM_mmin ,) |
| 1047 | IF_FEATURE_FIND_NEWER( PARM_newer ,) |
| 1048 | IF_FEATURE_FIND_INUM( PARM_inum ,) |
| 1049 | IF_FEATURE_FIND_USER( PARM_user ,) |
| 1050 | IF_FEATURE_FIND_GROUP( PARM_group ,) |
| 1051 | IF_FEATURE_FIND_SIZE( PARM_size ,) |
| 1052 | IF_FEATURE_FIND_CONTEXT(PARM_context ,) |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1053 | IF_FEATURE_FIND_LINKS( PARM_links ,) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1054 | IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1055 | }; |
| 1056 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1057 | static const char params[] ALIGN1 = |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1058 | "-follow\0" |
| 1059 | IF_FEATURE_FIND_XDEV( "-xdev\0" ) |
| 1060 | IF_FEATURE_FIND_DEPTH( "-depth\0" ) |
| 1061 | "-a\0" |
| 1062 | "-o\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1063 | IF_FEATURE_FIND_NOT( "!\0" ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1064 | #if ENABLE_DESKTOP |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1065 | "-and\0" |
| 1066 | "-or\0" |
| 1067 | IF_FEATURE_FIND_NOT( "-not\0" ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1068 | #endif |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1069 | "-print\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1070 | IF_FEATURE_FIND_PRINT0( "-print0\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1071 | IF_FEATURE_FIND_PRUNE( "-prune\0" ) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1072 | IF_FEATURE_FIND_QUIT( "-quit\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1073 | IF_FEATURE_FIND_DELETE( "-delete\0" ) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 1074 | IF_FEATURE_FIND_EMPTY( "-empty\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1075 | IF_FEATURE_FIND_EXEC( "-exec\0" ) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 1076 | IF_FEATURE_FIND_EXECUTABLE("-executable\0") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1077 | IF_FEATURE_FIND_PAREN( "(\0" ) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1078 | /* All options/actions starting from here require argument */ |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1079 | "-name\0" |
| 1080 | "-iname\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1081 | IF_FEATURE_FIND_PATH( "-path\0" ) |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1082 | #if ENABLE_DESKTOP |
| 1083 | IF_FEATURE_FIND_PATH( "-wholename\0") |
| 1084 | #endif |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 1085 | IF_FEATURE_FIND_PATH( "-ipath\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1086 | IF_FEATURE_FIND_REGEX( "-regex\0" ) |
| 1087 | IF_FEATURE_FIND_TYPE( "-type\0" ) |
| 1088 | IF_FEATURE_FIND_PERM( "-perm\0" ) |
| 1089 | IF_FEATURE_FIND_MTIME( "-mtime\0" ) |
| 1090 | IF_FEATURE_FIND_MMIN( "-mmin\0" ) |
| 1091 | IF_FEATURE_FIND_NEWER( "-newer\0" ) |
| 1092 | IF_FEATURE_FIND_INUM( "-inum\0" ) |
| 1093 | IF_FEATURE_FIND_USER( "-user\0" ) |
| 1094 | IF_FEATURE_FIND_GROUP( "-group\0" ) |
| 1095 | IF_FEATURE_FIND_SIZE( "-size\0" ) |
| 1096 | IF_FEATURE_FIND_CONTEXT("-context\0") |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1097 | IF_FEATURE_FIND_LINKS( "-links\0" ) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1098 | IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0") |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1099 | ; |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1100 | |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1101 | #if !USE_NESTED_FUNCTION |
| 1102 | struct pp_locals ppl; |
| 1103 | #define appp (ppl.appp ) |
| 1104 | #define cur_group (ppl.cur_group ) |
| 1105 | #define cur_action (ppl.cur_action ) |
| 1106 | #define invert_flag (ppl.invert_flag) |
| 1107 | #define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name) |
| 1108 | #else |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1109 | action*** appp; |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1110 | unsigned cur_group; |
| 1111 | unsigned cur_action; |
| 1112 | IF_FEATURE_FIND_NOT( bool invert_flag; ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1113 | |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 1114 | /* This is the only place in busybox where we use nested function. |
| 1115 | * So far more standard alternatives were bigger. */ |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1116 | /* Auto decl suppresses "func without a prototype" warning: */ |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 1117 | auto action* alloc_action(int sizeof_struct, action_fp f); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1118 | action* alloc_action(int sizeof_struct, action_fp f) |
| 1119 | { |
| 1120 | action *ap; |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1121 | appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(appp[0][0])); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1122 | appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1123 | appp[cur_group][cur_action] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1124 | ap->f = f; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1125 | IF_FEATURE_FIND_NOT( ap->invert = invert_flag; ) |
| 1126 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1127 | return ap; |
| 1128 | } |
| 1129 | #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name) |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1130 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1131 | |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1132 | cur_group = 0; |
| 1133 | cur_action = 0; |
| 1134 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1135 | appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1136 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1137 | while (*argv) { |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 1138 | const char *arg = argv[0]; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1139 | int parm = index_in_strings(params, arg); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1140 | const char *arg1 = argv[1]; |
| 1141 | |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1142 | dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type); |
| 1143 | |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1144 | if (parm >= PARM_name) { |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1145 | /* All options/actions starting from -name require argument */ |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1146 | if (!arg1) |
| 1147 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 1148 | argv++; |
| 1149 | } |
| 1150 | |
| 1151 | /* We can use big switch() here, but on i386 |
| 1152 | * it doesn't give smaller code. Other arches? */ |
| 1153 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1154 | /* Options always return true. They always take effect |
| 1155 | * rather than being processed only when their place in the |
| 1156 | * expression is reached. |
| 1157 | */ |
| 1158 | /* Options */ |
Ian Wienand | abc33ac | 2011-08-28 06:53:20 +0200 | [diff] [blame] | 1159 | if (parm == OPT_FOLLOW) { |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 1160 | dbg("follow enabled: %d", __LINE__); |
| 1161 | G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK; |
Ian Wienand | abc33ac | 2011-08-28 06:53:20 +0200 | [diff] [blame] | 1162 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1163 | #if ENABLE_FEATURE_FIND_XDEV |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1164 | else if (parm == OPT_XDEV) { |
| 1165 | dbg("%d", __LINE__); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1166 | G.xdev_on = 1; |
| 1167 | } |
| 1168 | #endif |
| 1169 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 1170 | else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1171 | dbg("%d", __LINE__); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1172 | G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1); |
| 1173 | } |
| 1174 | #endif |
| 1175 | #if ENABLE_FEATURE_FIND_DEPTH |
| 1176 | else if (parm == OPT_DEPTH) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1177 | dbg("%d", __LINE__); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1178 | G.recurse_flags |= ACTION_DEPTHFIRST; |
| 1179 | } |
| 1180 | #endif |
| 1181 | /* Actions are grouped by operators |
| 1182 | * ( expr ) Force precedence |
| 1183 | * ! expr True if expr is false |
| 1184 | * -not expr Same as ! expr |
| 1185 | * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false |
| 1186 | * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true |
| 1187 | * expr1 , expr2 List; both expr1 and expr2 are always evaluated |
| 1188 | * We implement: (), -a, -o |
| 1189 | */ |
| 1190 | /* Operators */ |
| 1191 | else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1192 | dbg("%d", __LINE__); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1193 | /* no further special handling required */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1194 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1195 | else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1196 | dbg("%d", __LINE__); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1197 | /* start new OR group */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1198 | cur_group++; |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1199 | appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0])); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1200 | /*appp[cur_group] = NULL; - already NULL */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1201 | appp[cur_group+1] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1202 | cur_action = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1203 | } |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1204 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1205 | else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) { |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 1206 | /* also handles "find ! ! -name 'foo*'" */ |
| 1207 | invert_flag ^= 1; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1208 | dbg("invert_flag:%d", invert_flag); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1209 | } |
| 1210 | #endif |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1211 | /* Actions */ |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1212 | else if (parm == PARM_print) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1213 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1214 | G.need_print = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1215 | (void) ALLOC_ACTION(print); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1216 | } |
| 1217 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1218 | else if (parm == PARM_print0) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1219 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1220 | G.need_print = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1221 | (void) ALLOC_ACTION(print0); |
| 1222 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 1223 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1224 | #if ENABLE_FEATURE_FIND_PRUNE |
| 1225 | else if (parm == PARM_prune) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1226 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1227 | (void) ALLOC_ACTION(prune); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1228 | } |
| 1229 | #endif |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1230 | #if ENABLE_FEATURE_FIND_QUIT |
| 1231 | else if (parm == PARM_quit) { |
| 1232 | dbg("%d", __LINE__); |
| 1233 | (void) ALLOC_ACTION(quit); |
| 1234 | } |
| 1235 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1236 | #if ENABLE_FEATURE_FIND_DELETE |
| 1237 | else if (parm == PARM_delete) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1238 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1239 | G.need_print = 0; |
| 1240 | G.recurse_flags |= ACTION_DEPTHFIRST; |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1241 | (void) ALLOC_ACTION(delete); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1242 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 1243 | #endif |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 1244 | #if ENABLE_FEATURE_FIND_EMPTY |
| 1245 | else if (parm == PARM_empty) { |
| 1246 | dbg("%d", __LINE__); |
| 1247 | (void) ALLOC_ACTION(empty); |
| 1248 | } |
| 1249 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1250 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1251 | else if (parm == PARM_exec) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1252 | int i; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1253 | action_exec *ap; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1254 | IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;) |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1255 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1256 | G.need_print = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1257 | ap = ALLOC_ACTION(exec); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1258 | ap->exec_argv = ++argv; /* first arg after -exec */ |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1259 | /*ap->exec_argc = 0; - ALLOC_ACTION did it */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1260 | while (1) { |
Bernhard Reutner-Fischer | 2e73d84 | 2010-02-25 13:22:37 +0100 | [diff] [blame] | 1261 | if (!*argv) /* did not see ';' or '+' until end */ |
| 1262 | bb_error_msg_and_die(bb_msg_requires_arg, "-exec"); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1263 | // find -exec echo Foo ">{}<" ";" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1264 | // executes "echo Foo >FILENAME<", |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1265 | // find -exec echo Foo ">{}<" "+" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1266 | // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...". |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1267 | if ((argv[0][0] == ';' || argv[0][0] == '+') |
| 1268 | && argv[0][1] == '\0' |
| 1269 | ) { |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1270 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 1271 | if (argv[0][0] == '+') |
| 1272 | ap->filelist = xzalloc(sizeof(ap->filelist[0])); |
| 1273 | # endif |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 1274 | break; |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1275 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1276 | argv++; |
| 1277 | ap->exec_argc++; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 1278 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1279 | if (ap->exec_argc == 0) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1280 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1281 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1282 | i = ap->exec_argc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1283 | while (i--) { |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 1284 | ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}"); |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1285 | IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];) |
| 1286 | } |
| 1287 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 1288 | /* |
| 1289 | * coreutils expects {} to appear only once in "-exec +" |
| 1290 | */ |
| 1291 | if (all_subst != 1 && ap->filelist) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1292 | bb_simple_error_msg_and_die("only one '{}' allowed for -exec +"); |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1293 | # endif |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1294 | } |
| 1295 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1296 | #if ENABLE_FEATURE_FIND_PAREN |
| 1297 | else if (parm == PARM_char_brace) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1298 | action_paren *ap; |
| 1299 | char **endarg; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 1300 | unsigned nested = 1; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1301 | |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1302 | dbg("%d", __LINE__); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1303 | endarg = argv; |
| 1304 | while (1) { |
| 1305 | if (!*++endarg) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1306 | bb_simple_error_msg_and_die("unpaired '('"); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 1307 | if (LONE_CHAR(*endarg, '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1308 | nested++; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 1309 | else if (LONE_CHAR(*endarg, ')') && !--nested) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1310 | *endarg = NULL; |
| 1311 | break; |
| 1312 | } |
| 1313 | } |
| 1314 | ap = ALLOC_ACTION(paren); |
| 1315 | ap->subexpr = parse_params(argv + 1); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1316 | *endarg = (char*) ")"; /* restore NULLed parameter */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1317 | argv = endarg; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1318 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1319 | #endif |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 1320 | else if (parm == PARM_name || parm == PARM_iname) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1321 | action_name *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1322 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1323 | ap = ALLOC_ACTION(name); |
| 1324 | ap->pattern = arg1; |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 1325 | ap->iname = (parm == PARM_iname); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1326 | } |
| 1327 | #if ENABLE_FEATURE_FIND_PATH |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1328 | else if (parm == PARM_path IF_DESKTOP(|| parm == PARM_wholename) || parm == PARM_ipath) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1329 | action_path *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1330 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1331 | ap = ALLOC_ACTION(path); |
| 1332 | ap->pattern = arg1; |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 1333 | ap->ipath = (parm == PARM_ipath); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1334 | } |
| 1335 | #endif |
| 1336 | #if ENABLE_FEATURE_FIND_REGEX |
| 1337 | else if (parm == PARM_regex) { |
| 1338 | action_regex *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1339 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1340 | ap = ALLOC_ACTION(regex); |
| 1341 | xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/); |
| 1342 | } |
| 1343 | #endif |
| 1344 | #if ENABLE_FEATURE_FIND_TYPE |
| 1345 | else if (parm == PARM_type) { |
| 1346 | action_type *ap; |
| 1347 | ap = ALLOC_ACTION(type); |
| 1348 | ap->type_mask = find_type(arg1); |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1349 | dbg("created:type mask:%x", ap->type_mask); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1350 | } |
| 1351 | #endif |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 1352 | #if ENABLE_FEATURE_FIND_EXECUTABLE |
| 1353 | else if (parm == PARM_executable) { |
| 1354 | (void) ALLOC_ACTION(executable); |
| 1355 | } |
| 1356 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1357 | #if ENABLE_FEATURE_FIND_PERM |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1358 | /* -perm BITS File's mode bits are exactly BITS (octal or symbolic). |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1359 | * Symbolic modes use mode 0 as a point of departure. |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1360 | * -perm -BITS All of the BITS are set in file's mode. |
Denys Vlasenko | 3e9b13e | 2014-09-05 12:16:15 +0200 | [diff] [blame] | 1361 | * -perm [+/]BITS At least one of the BITS is set in file's mode. |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1362 | */ |
| 1363 | else if (parm == PARM_perm) { |
| 1364 | action_perm *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1365 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1366 | ap = ALLOC_ACTION(perm); |
| 1367 | ap->perm_char = arg1[0]; |
Denys Vlasenko | 3e9b13e | 2014-09-05 12:16:15 +0200 | [diff] [blame] | 1368 | arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1)); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1369 | /*ap->perm_mask = 0; - ALLOC_ACTION did it */ |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 1370 | ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask); |
| 1371 | if (ap->perm_mask == (mode_t)-1) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1372 | bb_error_msg_and_die("invalid mode '%s'", arg1); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1373 | } |
| 1374 | #endif |
| 1375 | #if ENABLE_FEATURE_FIND_MTIME |
| 1376 | else if (parm == PARM_mtime) { |
| 1377 | action_mtime *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1378 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1379 | ap = ALLOC_ACTION(mtime); |
| 1380 | ap->mtime_char = arg1[0]; |
| 1381 | ap->mtime_days = xatoul(plus_minus_num(arg1)); |
| 1382 | } |
| 1383 | #endif |
| 1384 | #if ENABLE_FEATURE_FIND_MMIN |
| 1385 | else if (parm == PARM_mmin) { |
| 1386 | action_mmin *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1387 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1388 | ap = ALLOC_ACTION(mmin); |
| 1389 | ap->mmin_char = arg1[0]; |
| 1390 | ap->mmin_mins = xatoul(plus_minus_num(arg1)); |
| 1391 | } |
| 1392 | #endif |
| 1393 | #if ENABLE_FEATURE_FIND_NEWER |
| 1394 | else if (parm == PARM_newer) { |
| 1395 | struct stat stat_newer; |
| 1396 | action_newer *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1397 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1398 | ap = ALLOC_ACTION(newer); |
| 1399 | xstat(arg1, &stat_newer); |
| 1400 | ap->newer_mtime = stat_newer.st_mtime; |
| 1401 | } |
| 1402 | #endif |
| 1403 | #if ENABLE_FEATURE_FIND_INUM |
| 1404 | else if (parm == PARM_inum) { |
| 1405 | action_inum *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1406 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1407 | ap = ALLOC_ACTION(inum); |
| 1408 | ap->inode_num = xatoul(arg1); |
| 1409 | } |
| 1410 | #endif |
| 1411 | #if ENABLE_FEATURE_FIND_USER |
| 1412 | else if (parm == PARM_user) { |
| 1413 | action_user *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1414 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1415 | ap = ALLOC_ACTION(user); |
| 1416 | ap->uid = bb_strtou(arg1, NULL, 10); |
| 1417 | if (errno) |
| 1418 | ap->uid = xuname2uid(arg1); |
| 1419 | } |
| 1420 | #endif |
| 1421 | #if ENABLE_FEATURE_FIND_GROUP |
| 1422 | else if (parm == PARM_group) { |
| 1423 | action_group *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1424 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1425 | ap = ALLOC_ACTION(group); |
| 1426 | ap->gid = bb_strtou(arg1, NULL, 10); |
| 1427 | if (errno) |
| 1428 | ap->gid = xgroup2gid(arg1); |
| 1429 | } |
| 1430 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1431 | #if ENABLE_FEATURE_FIND_SIZE |
| 1432 | else if (parm == PARM_size) { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1433 | /* -size n[bckw]: file uses n units of space |
| 1434 | * b (default): units are 512-byte blocks |
| 1435 | * c: 1 byte |
| 1436 | * k: kilobytes |
| 1437 | * w: 2-byte words |
| 1438 | */ |
| 1439 | #if ENABLE_LFS |
| 1440 | #define XATOU_SFX xatoull_sfx |
| 1441 | #else |
| 1442 | #define XATOU_SFX xatoul_sfx |
| 1443 | #endif |
| 1444 | static const struct suffix_mult find_suffixes[] = { |
Denis Vlasenko | 99fcc62 | 2007-07-01 17:29:44 +0000 | [diff] [blame] | 1445 | { "c", 1 }, |
| 1446 | { "w", 2 }, |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 1447 | { "", 512 }, |
Denis Vlasenko | 99fcc62 | 2007-07-01 17:29:44 +0000 | [diff] [blame] | 1448 | { "b", 512 }, |
| 1449 | { "k", 1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 1450 | { "", 0 } |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1451 | }; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1452 | action_size *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1453 | dbg("%d", __LINE__); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1454 | ap = ALLOC_ACTION(size); |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1455 | ap->size_char = arg1[0]; |
| 1456 | ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1457 | } |
| 1458 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1459 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 1460 | else if (parm == PARM_context) { |
| 1461 | action_context *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1462 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1463 | ap = ALLOC_ACTION(context); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1464 | /*ap->context = NULL; - ALLOC_ACTION did it */ |
Denis Vlasenko | 838daca | 2007-08-16 10:41:24 +0000 | [diff] [blame] | 1465 | /* SELinux headers erroneously declare non-const parameter */ |
| 1466 | if (selinux_raw_to_trans_context((char*)arg1, &ap->context)) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 1467 | bb_simple_perror_msg(arg1); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 1468 | } |
| 1469 | #endif |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1470 | #if ENABLE_FEATURE_FIND_LINKS |
| 1471 | else if (parm == PARM_links) { |
| 1472 | action_links *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1473 | dbg("%d", __LINE__); |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1474 | ap = ALLOC_ACTION(links); |
| 1475 | ap->links_char = arg1[0]; |
| 1476 | ap->links_count = xatoul(plus_minus_num(arg1)); |
| 1477 | } |
| 1478 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1479 | else { |
| 1480 | bb_error_msg("unrecognized: %s", arg); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1481 | bb_show_usage(); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1482 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1483 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1484 | } |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1485 | dbg("exiting %s", __func__); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1486 | return appp; |
| 1487 | #undef ALLOC_ACTION |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1488 | #undef appp |
| 1489 | #undef cur_action |
| 1490 | #undef invert_flag |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1493 | int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1494 | int find_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1495 | { |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1496 | int i, firstopt; |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1497 | char **past_HLP, *saved; |
Denis Vlasenko | 7d6c929 | 2007-04-05 00:35:43 +0000 | [diff] [blame] | 1498 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1499 | INIT_G(); |
| 1500 | |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1501 | /* "find -type f" + getopt("+HLP") => disaster. |
| 1502 | * Need to avoid getopt running into a non-HLP option. |
| 1503 | * Do this by temporarily storing NULL there: |
| 1504 | */ |
| 1505 | past_HLP = argv; |
| 1506 | for (;;) { |
| 1507 | saved = *++past_HLP; |
| 1508 | if (!saved) |
| 1509 | break; |
| 1510 | if (saved[0] != '-') |
| 1511 | break; |
| 1512 | if (!saved[1]) |
| 1513 | break; /* it is "-" */ |
Denys Vlasenko | 2d9e097 | 2019-01-06 19:58:18 +0100 | [diff] [blame] | 1514 | if (saved[1] == '-' && !saved[2]) { |
| 1515 | /* it is "--" */ |
| 1516 | /* Try: find -- /dev/null */ |
| 1517 | saved = *++past_HLP; |
| 1518 | break; |
| 1519 | } |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1520 | if ((saved+1)[strspn(saved+1, "HLP")] != '\0') |
| 1521 | break; |
| 1522 | } |
| 1523 | *past_HLP = NULL; |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 1524 | /* "+": stop on first non-option */ |
| 1525 | i = getopt32(argv, "+HLP"); |
| 1526 | if (i & (1<<0)) |
| 1527 | G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK; |
| 1528 | if (i & (1<<1)) |
| 1529 | G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK; |
| 1530 | /* -P is default and is ignored */ |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1531 | argv = past_HLP; /* same result as "argv += optind;" */ |
| 1532 | *past_HLP = saved; |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 1533 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1534 | for (firstopt = 0; argv[firstopt]; firstopt++) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1535 | if (argv[firstopt][0] == '-') |
| 1536 | break; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1537 | if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!')) |
| 1538 | break; |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1539 | if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1540 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1541 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1542 | if (firstopt == 0) { |
| 1543 | *--argv = (char*)"."; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1544 | firstopt++; |
| 1545 | } |
| 1546 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1547 | G.actions = parse_params(&argv[firstopt]); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1548 | argv[firstopt] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1549 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1550 | #if ENABLE_FEATURE_FIND_XDEV |
| 1551 | if (G.xdev_on) { |
| 1552 | struct stat stbuf; |
| 1553 | |
| 1554 | G.xdev_count = firstopt; |
| 1555 | G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0])); |
| 1556 | for (i = 0; argv[i]; i++) { |
| 1557 | /* not xstat(): shouldn't bomb out on |
| 1558 | * "find not_exist exist -xdev" */ |
| 1559 | if (stat(argv[i], &stbuf) == 0) |
| 1560 | G.xdev_dev[i] = stbuf.st_dev; |
| 1561 | /* else G.xdev_dev[i] stays 0 and |
| 1562 | * won't match any real device dev_t |
| 1563 | */ |
| 1564 | } |
| 1565 | } |
| 1566 | #endif |
| 1567 | |
| 1568 | for (i = 0; argv[i]; i++) { |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 1569 | if (!recursive_action(argv[i], |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1570 | G.recurse_flags,/* flags */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 1571 | fileAction, /* file action */ |
| 1572 | fileAction, /* dir action */ |
| 1573 | NULL, /* user data */ |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1574 | 0) /* depth */ |
| 1575 | ) { |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1576 | G.exitstatus |= EXIT_FAILURE; |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1577 | } |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 1578 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1579 | |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1580 | IF_FEATURE_FIND_EXEC_PLUS(G.exitstatus |= flush_exec_plus();) |
| 1581 | return G.exitstatus; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1582 | } |