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 |
Ismael Luceno | 0084c44 | 2021-09-29 13:41:32 +0200 | [diff] [blame] | 70 | //config: bool "Enable -mtime: modification time matching" |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 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: |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 77 | //config:config FEATURE_FIND_ATIME |
| 78 | //config: bool "Enable -atime: access time matching" |
| 79 | //config: default y |
| 80 | //config: depends on FEATURE_FIND_MTIME |
| 81 | //config: help |
| 82 | //config: Allow searching based on the access time of |
| 83 | //config: files, in days. |
| 84 | //config: |
| 85 | //config:config FEATURE_FIND_CTIME |
| 86 | //config: bool "Enable -ctime: status change timestamp matching" |
| 87 | //config: default y |
| 88 | //config: depends on FEATURE_FIND_MTIME |
| 89 | //config: help |
| 90 | //config: Allow searching based on the status change timestamp of |
| 91 | //config: files, in days. |
| 92 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 93 | //config:config FEATURE_FIND_MMIN |
Ismael Luceno | 0084c44 | 2021-09-29 13:41:32 +0200 | [diff] [blame] | 94 | //config: bool "Enable -mmin: modification time matching by minutes" |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 95 | //config: default y |
| 96 | //config: depends on FIND |
| 97 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 98 | //config: Allow searching based on the modification time of |
| 99 | //config: files, in minutes. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 100 | //config: |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 101 | //config:config FEATURE_FIND_AMIN |
| 102 | //config: bool "Enable -amin: access time matching by minutes" |
| 103 | //config: default y |
| 104 | //config: depends on FEATURE_FIND_MMIN |
| 105 | //config: help |
| 106 | //config: Allow searching based on the access time of |
| 107 | //config: files, in minutes. |
| 108 | //config: |
| 109 | //config:config FEATURE_FIND_CMIN |
| 110 | //config: bool "Enable -cmin: status change timestamp matching by minutes" |
| 111 | //config: default y |
| 112 | //config: depends on FEATURE_FIND_MMIN |
| 113 | //config: help |
| 114 | //config: Allow searching based on the status change timestamp of |
| 115 | //config: files, in minutes. |
| 116 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 117 | //config:config FEATURE_FIND_PERM |
| 118 | //config: bool "Enable -perm: permissions matching" |
| 119 | //config: default y |
| 120 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 121 | //config: |
| 122 | //config:config FEATURE_FIND_TYPE |
| 123 | //config: bool "Enable -type: file type matching (file/dir/link/...)" |
| 124 | //config: default y |
| 125 | //config: depends on FIND |
| 126 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 127 | //config: Enable searching based on file type (file, |
| 128 | //config: directory, socket, device, etc.). |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 129 | //config: |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 130 | //config:config FEATURE_FIND_EXECUTABLE |
| 131 | //config: bool "Enable -executable: file is executable" |
| 132 | //config: default y |
| 133 | //config: depends on FIND |
| 134 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 135 | //config:config FEATURE_FIND_XDEV |
| 136 | //config: bool "Enable -xdev: 'stay in filesystem'" |
| 137 | //config: default y |
| 138 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 139 | //config: |
| 140 | //config:config FEATURE_FIND_MAXDEPTH |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 141 | //config: bool "Enable -mindepth N and -maxdepth N" |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 142 | //config: default y |
| 143 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 144 | //config: |
| 145 | //config:config FEATURE_FIND_NEWER |
| 146 | //config: bool "Enable -newer: compare file modification times" |
| 147 | //config: default y |
| 148 | //config: depends on FIND |
| 149 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 150 | //config: Support the 'find -newer' option for finding any files which have |
| 151 | //config: modification time that is more recent than the specified FILE. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 152 | //config: |
| 153 | //config:config FEATURE_FIND_INUM |
| 154 | //config: bool "Enable -inum: inode number matching" |
| 155 | //config: default y |
| 156 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 157 | //config: |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 158 | //config:config FEATURE_FIND_SAMEFILE |
| 159 | //config: bool "Enable -samefile: reference file matching" |
| 160 | //config: default y |
| 161 | //config: depends on FIND |
| 162 | //config: help |
| 163 | //config: Support the 'find -samefile' option for searching by a reference file. |
| 164 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 165 | //config:config FEATURE_FIND_EXEC |
| 166 | //config: bool "Enable -exec: execute commands" |
| 167 | //config: default y |
| 168 | //config: depends on FIND |
| 169 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 170 | //config: Support the 'find -exec' option for executing commands based upon |
| 171 | //config: the files matched. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 172 | //config: |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 173 | //config:config FEATURE_FIND_EXEC_PLUS |
| 174 | //config: bool "Enable -exec ... {} +" |
| 175 | //config: default y |
| 176 | //config: depends on FEATURE_FIND_EXEC |
| 177 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 178 | //config: Support the 'find -exec ... {} +' option for executing commands |
| 179 | //config: for all matched files at once. |
| 180 | //config: Without this option, -exec + is a synonym for -exec ; |
| 181 | //config: (IOW: it works correctly, but without expected speedup) |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 182 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 183 | //config:config FEATURE_FIND_USER |
| 184 | //config: bool "Enable -user: username/uid matching" |
| 185 | //config: default y |
| 186 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 187 | //config: |
| 188 | //config:config FEATURE_FIND_GROUP |
| 189 | //config: bool "Enable -group: group/gid matching" |
| 190 | //config: default y |
| 191 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 192 | //config: |
| 193 | //config:config FEATURE_FIND_NOT |
| 194 | //config: bool "Enable the 'not' (!) operator" |
| 195 | //config: default y |
| 196 | //config: depends on FIND |
| 197 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 198 | //config: Support the '!' operator to invert the test results. |
| 199 | //config: If 'Enable full-blown desktop' is enabled, then will also support |
| 200 | //config: the non-POSIX notation '-not'. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 201 | //config: |
| 202 | //config:config FEATURE_FIND_DEPTH |
| 203 | //config: bool "Enable -depth" |
| 204 | //config: default y |
| 205 | //config: depends on FIND |
| 206 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 207 | //config: Process each directory's contents before the directory itself. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 208 | //config: |
| 209 | //config:config FEATURE_FIND_PAREN |
| 210 | //config: bool "Enable parens in options" |
| 211 | //config: default y |
| 212 | //config: depends on FIND |
| 213 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 214 | //config: Enable usage of parens '(' to specify logical order of arguments. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 215 | //config: |
| 216 | //config:config FEATURE_FIND_SIZE |
| 217 | //config: bool "Enable -size: file size matching" |
| 218 | //config: default y |
| 219 | //config: depends on FIND |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 220 | //config: |
| 221 | //config:config FEATURE_FIND_PRUNE |
| 222 | //config: bool "Enable -prune: exclude subdirectories" |
| 223 | //config: default y |
| 224 | //config: depends on FIND |
| 225 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 226 | //config: If the file is a directory, don't descend into it. Useful for |
| 227 | //config: exclusion .svn and CVS directories. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 228 | //config: |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 229 | //config:config FEATURE_FIND_QUIT |
| 230 | //config: bool "Enable -quit: exit" |
| 231 | //config: default y |
| 232 | //config: depends on FIND |
| 233 | //config: help |
| 234 | //config: If this action is reached, 'find' exits. |
| 235 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 236 | //config:config FEATURE_FIND_DELETE |
| 237 | //config: bool "Enable -delete: delete files/dirs" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 238 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 239 | //config: depends on FIND && FEATURE_FIND_DEPTH |
| 240 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 241 | //config: Support the 'find -delete' option for deleting files and directories. |
| 242 | //config: WARNING: This option can do much harm if used wrong. Busybox will not |
| 243 | //config: try to protect the user from doing stupid things. Use with care. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 244 | //config: |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 245 | //config:config FEATURE_FIND_EMPTY |
| 246 | //config: bool "Enable -empty: match empty files or directories" |
| 247 | //config: default y |
| 248 | //config: depends on FIND |
| 249 | //config: help |
| 250 | //config: Support the 'find -empty' option to find empty regular files |
| 251 | //config: or directories. |
| 252 | //config: |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 253 | //config:config FEATURE_FIND_PATH |
| 254 | //config: bool "Enable -path: match pathname with shell pattern" |
| 255 | //config: default y |
| 256 | //config: depends on FIND |
| 257 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 258 | //config: The -path option matches whole pathname instead of just filename. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 259 | //config: |
| 260 | //config:config FEATURE_FIND_REGEX |
| 261 | //config: bool "Enable -regex: match pathname with regex" |
| 262 | //config: default y |
| 263 | //config: depends on FIND |
| 264 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 265 | //config: The -regex option matches whole pathname against regular expression. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 266 | //config: |
| 267 | //config:config FEATURE_FIND_CONTEXT |
| 268 | //config: bool "Enable -context: security context matching" |
| 269 | //config: default n |
| 270 | //config: depends on FIND && SELINUX |
| 271 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 272 | //config: Support the 'find -context' option for matching security context. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 273 | //config: |
| 274 | //config:config FEATURE_FIND_LINKS |
| 275 | //config: bool "Enable -links: link count matching" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 276 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 277 | //config: depends on FIND |
| 278 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 279 | //config: Support the 'find -links' option for matching number of links. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 280 | |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 281 | //applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find)) |
| 282 | |
| 283 | //kbuild:lib-$(CONFIG_FIND) += find.o |
| 284 | |
| 285 | //usage:#define find_trivial_usage |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 286 | //usage: "[-HL] [PATH]... [OPTIONS] [ACTIONS]" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 287 | //usage:#define find_full_usage "\n\n" |
| 288 | //usage: "Search for files and perform actions on them.\n" |
| 289 | //usage: "First failed action stops processing of current file.\n" |
| 290 | //usage: "Defaults: PATH is current directory, action is '-print'\n" |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 291 | //usage: "\n -L,-follow Follow symlinks" |
| 292 | //usage: "\n -H ...on command line only" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 293 | //usage: IF_FEATURE_FIND_XDEV( |
| 294 | //usage: "\n -xdev Don't descend directories on other filesystems" |
| 295 | //usage: ) |
| 296 | //usage: IF_FEATURE_FIND_MAXDEPTH( |
| 297 | //usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies" |
| 298 | //usage: "\n actions to command line arguments only" |
| 299 | //usage: "\n -mindepth N Don't act on first N levels" |
| 300 | //usage: ) |
| 301 | //usage: IF_FEATURE_FIND_DEPTH( |
| 302 | //usage: "\n -depth Act on directory *after* traversing it" |
| 303 | //usage: ) |
| 304 | //usage: "\n" |
| 305 | //usage: "\nActions:" |
| 306 | //usage: IF_FEATURE_FIND_PAREN( |
| 307 | //usage: "\n ( ACTIONS ) Group actions for -o / -a" |
| 308 | //usage: ) |
| 309 | //usage: IF_FEATURE_FIND_NOT( |
| 310 | //usage: "\n ! ACT Invert ACT's success/failure" |
| 311 | //usage: ) |
| 312 | //usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2" |
| 313 | //usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2" |
| 314 | //usage: "\n Note: -a has higher priority than -o" |
| 315 | //usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN" |
| 316 | //usage: "\n -iname PATTERN Case insensitive -name" |
| 317 | //usage: IF_FEATURE_FIND_PATH( |
| 318 | //usage: "\n -path PATTERN Match path to PATTERN" |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 319 | //usage: "\n -ipath PATTERN Case insensitive -path" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 320 | //usage: ) |
| 321 | //usage: IF_FEATURE_FIND_REGEX( |
| 322 | //usage: "\n -regex PATTERN Match path to regex PATTERN" |
| 323 | //usage: ) |
| 324 | //usage: IF_FEATURE_FIND_TYPE( |
Denys Vlasenko | f4eaccb | 2018-04-24 10:13:45 +0200 | [diff] [blame] | 325 | //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] | 326 | //usage: ) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 327 | //usage: IF_FEATURE_FIND_EXECUTABLE( |
| 328 | //usage: "\n -executable File is executable" |
| 329 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 330 | //usage: IF_FEATURE_FIND_PERM( |
| 331 | //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] | 332 | //usage: "\n or exactly MASK bits are set in file's mode" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 333 | //usage: ) |
| 334 | //usage: IF_FEATURE_FIND_MTIME( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 335 | //usage: "\n -mtime DAYS mtime is greater than (+N), less than (-N)," |
| 336 | //usage: "\n or exactly N days in the past" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 337 | //usage: ) |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 338 | //usage: IF_FEATURE_FIND_ATIME( |
| 339 | //usage: "\n -atime DAYS atime +N/-N/N days in the past" |
| 340 | //usage: ) |
| 341 | //usage: IF_FEATURE_FIND_CTIME( |
| 342 | //usage: "\n -ctime DAYS ctime +N/-N/N days in the past" |
| 343 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 344 | //usage: IF_FEATURE_FIND_MMIN( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 345 | //usage: "\n -mmin MINS mtime is greater than (+N), less than (-N)," |
| 346 | //usage: "\n or exactly N minutes in the past" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 347 | //usage: ) |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 348 | //usage: IF_FEATURE_FIND_AMIN( |
| 349 | //usage: "\n -amin MINS atime +N/-N/N minutes in the past" |
| 350 | //usage: ) |
| 351 | //usage: IF_FEATURE_FIND_CMIN( |
| 352 | //usage: "\n -cmin MINS ctime +N/-N/N minutes in the past" |
| 353 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 354 | //usage: IF_FEATURE_FIND_NEWER( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 355 | //usage: "\n -newer FILE mtime is more recent than FILE's" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 356 | //usage: ) |
| 357 | //usage: IF_FEATURE_FIND_INUM( |
| 358 | //usage: "\n -inum N File has inode number N" |
| 359 | //usage: ) |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 360 | //usage: IF_FEATURE_FIND_SAMEFILE( |
| 361 | //usage: "\n -samefile FILE File is same as FILE" |
| 362 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 363 | //usage: IF_FEATURE_FIND_USER( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 364 | //usage: "\n -user NAME/ID File is owned by given user" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 365 | //usage: ) |
| 366 | //usage: IF_FEATURE_FIND_GROUP( |
Denys Vlasenko | e1a4209 | 2011-02-12 20:37:41 +0100 | [diff] [blame] | 367 | //usage: "\n -group NAME/ID File is owned by given group" |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 368 | //usage: ) |
| 369 | //usage: IF_FEATURE_FIND_SIZE( |
| 370 | //usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))" |
| 371 | //usage: "\n +/-N: file size is bigger/smaller than N" |
| 372 | //usage: ) |
| 373 | //usage: IF_FEATURE_FIND_LINKS( |
| 374 | //usage: "\n -links N Number of links is greater than (+N), less than (-N)," |
| 375 | //usage: "\n or exactly N" |
| 376 | //usage: ) |
| 377 | //usage: IF_FEATURE_FIND_CONTEXT( |
| 378 | //usage: "\n -context CTX File has specified security context" |
| 379 | //usage: ) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 380 | //usage: IF_FEATURE_FIND_EMPTY( |
| 381 | //usage: "\n -empty Match empty file/directory" |
| 382 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 383 | //usage: IF_FEATURE_FIND_PRUNE( |
| 384 | //usage: "\n -prune If current file is directory, don't descend into it" |
| 385 | //usage: ) |
| 386 | //usage: "\nIf none of the following actions is specified, -print is assumed" |
| 387 | //usage: "\n -print Print file name" |
| 388 | //usage: IF_FEATURE_FIND_PRINT0( |
| 389 | //usage: "\n -print0 Print file name, NUL terminated" |
| 390 | //usage: ) |
| 391 | //usage: IF_FEATURE_FIND_EXEC( |
| 392 | //usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by" |
| 393 | //usage: "\n file name. Fails if CMD exits with nonzero" |
| 394 | //usage: ) |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 395 | //usage: IF_FEATURE_FIND_EXEC_PLUS( |
| 396 | //usage: "\n -exec CMD ARG + Run CMD with {} replaced by list of file names" |
| 397 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 398 | //usage: IF_FEATURE_FIND_DELETE( |
| 399 | //usage: "\n -delete Delete current file/directory. Turns on -depth option" |
| 400 | //usage: ) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 401 | //usage: IF_FEATURE_FIND_QUIT( |
| 402 | //usage: "\n -quit Exit" |
| 403 | //usage: ) |
Denys Vlasenko | 805f245 | 2011-02-12 20:26:14 +0100 | [diff] [blame] | 404 | //usage: |
| 405 | //usage:#define find_example_usage |
| 406 | //usage: "$ find / -name passwd\n" |
| 407 | //usage: "/etc/passwd\n" |
| 408 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 409 | #include <fnmatch.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 410 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 411 | #include "common_bufsiz.h" |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 412 | #if ENABLE_FEATURE_FIND_REGEX |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 413 | # include "xregex.h" |
| 414 | #endif |
| 415 | /* GNUism: */ |
| 416 | #ifndef FNM_CASEFOLD |
| 417 | # define FNM_CASEFOLD 0 |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 418 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 419 | |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 420 | #if 1 |
| 421 | # define dbg(...) ((void)0) |
| 422 | #else |
| 423 | # define dbg(...) bb_error_msg(__VA_ARGS__) |
| 424 | #endif |
| 425 | |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 426 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 427 | /* This is a NOEXEC applet. Be very careful! */ |
| 428 | |
| 429 | |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 430 | 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] | 431 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 432 | typedef struct { |
| 433 | action_fp f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 434 | #if ENABLE_FEATURE_FIND_NOT |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 435 | bool invert; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 436 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 437 | } action; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 438 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 439 | #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 440 | #define ACTF(name) \ |
| 441 | static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \ |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 442 | const struct stat *statbuf UNUSED_PARAM, \ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 443 | action_##name* ap UNUSED_PARAM) |
| 444 | |
| 445 | ACTS(print) |
| 446 | ACTS(name, const char *pattern; bool iname;) |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 447 | IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern; bool ipath;)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 448 | IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;)) |
| 449 | IF_FEATURE_FIND_PRINT0( ACTS(print0)) |
| 450 | IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 451 | IF_FEATURE_FIND_EXECUTABLE(ACTS(executable)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 452 | IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;)) |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 453 | IF_FEATURE_FIND_MTIME( ACTS(mtime, unsigned char time_type; unsigned char mtime_char; unsigned mtime_days;)) |
| 454 | IF_FEATURE_FIND_MMIN( ACTS(mmin, unsigned char time_type; unsigned char mmin_char; unsigned mmin_mins;)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 455 | IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) |
| 456 | IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 457 | IF_FEATURE_FIND_SAMEFILE(ACTS(samefile, ino_t inode_num; dev_t device;)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 458 | IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;)) |
| 459 | IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;)) |
| 460 | IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;)) |
| 461 | IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;)) |
| 462 | IF_FEATURE_FIND_PRUNE( ACTS(prune)) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 463 | IF_FEATURE_FIND_QUIT( ACTS(quit)) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 464 | IF_FEATURE_FIND_DELETE( ACTS(delete)) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 465 | IF_FEATURE_FIND_EMPTY( ACTS(empty)) |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 466 | IF_FEATURE_FIND_EXEC( ACTS(exec, |
| 467 | char **exec_argv; /* -exec ARGS */ |
| 468 | unsigned *subst_count; |
| 469 | int exec_argc; /* count of ARGS */ |
| 470 | IF_FEATURE_FIND_EXEC_PLUS( |
| 471 | /* |
| 472 | * filelist is NULL if "exec ;" |
| 473 | * non-NULL if "exec +" |
| 474 | */ |
| 475 | char **filelist; |
| 476 | int filelist_idx; |
| 477 | int file_len; |
| 478 | ) |
| 479 | )) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 480 | IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;)) |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 481 | IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 482 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 483 | struct globals { |
| 484 | IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;) |
| 485 | IF_FEATURE_FIND_XDEV(int xdev_count;) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 486 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 487 | int minmaxdepth[2]; |
| 488 | #endif |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 489 | action ***actions; |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 490 | smallint need_print; |
| 491 | smallint xdev_on; |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 492 | smalluint exitstatus; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 493 | recurse_flags_t recurse_flags; |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 494 | IF_FEATURE_FIND_EXEC_PLUS(unsigned max_argv_len;) |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 495 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 496 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 497 | #define INIT_G() do { \ |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 498 | setup_common_bufsiz(); \ |
Denys Vlasenko | 7b85ec3 | 2015-10-13 17:17:34 +0200 | [diff] [blame] | 499 | BUILD_BUG_ON(sizeof(G) > COMMON_BUFSIZE); \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 500 | /* we have to zero it out because of NOEXEC */ \ |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 501 | memset(&G, 0, sizeof(G)); \ |
| 502 | IF_FEATURE_FIND_MAXDEPTH(G.minmaxdepth[1] = INT_MAX;) \ |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 503 | 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] | 504 | G.need_print = 1; \ |
| 505 | G.recurse_flags = ACTION_RECURSE; \ |
| 506 | } while (0) |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 507 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 508 | /* Return values of ACTFs ('action functions') are a bit mask: |
| 509 | * bit 1=1: prune (use SKIP constant for setting it) |
| 510 | * bit 0=1: matched successfully (TRUE) |
| 511 | */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 512 | |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 513 | 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] | 514 | { |
| 515 | int cur_group; |
| 516 | int cur_action; |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 517 | int rc = 0; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 518 | action **app, *ap; |
| 519 | |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 520 | /* "action group" is a set of actions ANDed together. |
| 521 | * groups are ORed together. |
| 522 | * We simply evaluate each group until we find one in which all actions |
| 523 | * succeed. */ |
| 524 | |
| 525 | /* -prune is special: if it is encountered, then we won't |
| 526 | * descend into current directory. It doesn't matter whether |
| 527 | * action group (in which -prune sits) will succeed or not: |
| 528 | * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir |
| 529 | * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs |
| 530 | * not starting with 'f' */ |
| 531 | |
| 532 | /* We invert TRUE bit (bit 0). Now 1 there means 'failure'. |
| 533 | * and bitwise OR in "rc |= TRUE ^ ap->f()" will: |
| 534 | * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'. |
| 535 | * On return, bit is restored. */ |
| 536 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 537 | cur_group = -1; |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 538 | while ((app = appp[++cur_group]) != NULL) { |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 539 | rc &= ~TRUE; /* 'success' so far, clear TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 540 | cur_action = -1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 541 | while (1) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 542 | ap = app[++cur_action]; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 543 | if (!ap) /* all actions in group were successful */ |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 544 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 545 | rc |= TRUE ^ ap->f(fileName, statbuf, ap); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 546 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 547 | if (ap->invert) rc ^= TRUE; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 548 | #endif |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 549 | 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] | 550 | if (rc & TRUE) /* current group failed, try next */ |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 551 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 552 | } |
| 553 | } |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 554 | dbg("returning:0x%x", rc ^ TRUE); |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 555 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 558 | #if !FNM_CASEFOLD |
| 559 | static char *strcpy_upcase(char *dst, const char *src) |
| 560 | { |
| 561 | char *d = dst; |
| 562 | while (1) { |
| 563 | unsigned char ch = *src++; |
| 564 | if (ch >= 'a' && ch <= 'z') |
| 565 | ch -= ('a' - 'A'); |
| 566 | *d++ = ch; |
| 567 | if (ch == '\0') |
| 568 | break; |
| 569 | } |
| 570 | return dst; |
| 571 | } |
| 572 | #endif |
| 573 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 574 | ACTF(name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 575 | { |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 576 | int r; |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 577 | const char *tmp = bb_basename(fileName); |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 578 | /* GNU findutils: find DIR/ -name DIR |
| 579 | * prints "DIR/" (DIR// prints "DIR//" etc). |
| 580 | * Need to strip trailing "/". |
| 581 | * Such names can come only from top-level names, but |
| 582 | * we can't do this before recursive_action() call, |
| 583 | * since then "find FILE/ -name FILE" |
| 584 | * would also work (on non-directories), which is wrong. |
| 585 | */ |
| 586 | char *trunc_slash = NULL; |
| 587 | |
| 588 | if (*tmp == '\0') { |
| 589 | /* "foo/bar/[//...]" */ |
| 590 | while (tmp != fileName && tmp[-1] == '/') |
| 591 | tmp--; |
| 592 | if (tmp == fileName) { /* entire fileName is "//.."? */ |
| 593 | /* yes, convert "//..." to "/" |
| 594 | * Testcases: |
| 595 | * find / -maxdepth 1 -name /: prints / |
| 596 | * find // -maxdepth 1 -name /: prints // |
| 597 | * find / -maxdepth 1 -name //: prints nothing |
| 598 | * find // -maxdepth 1 -name //: prints nothing |
| 599 | */ |
| 600 | if (tmp[1]) |
| 601 | trunc_slash = (char*)tmp + 1; |
| 602 | } else { |
| 603 | /* no, it's "foo/bar/[//...]", go back to 'b' */ |
| 604 | trunc_slash = (char*)tmp; |
| 605 | while (tmp != fileName && tmp[-1] != '/') |
| 606 | tmp--; |
| 607 | } |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 608 | } |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 609 | |
Denys Vlasenko | 9138817 | 2010-04-26 09:54:04 +0200 | [diff] [blame] | 610 | /* Was using FNM_PERIOD flag too, |
| 611 | * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it. |
| 612 | * find -name '*foo' should match .foo too: |
| 613 | */ |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 614 | if (trunc_slash) *trunc_slash = '\0'; |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 615 | #if FNM_CASEFOLD |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 616 | r = fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)); |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 617 | #else |
| 618 | if (ap->iname) |
| 619 | tmp = strcpy_upcase(alloca(strlen(tmp) + 1), tmp); |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 620 | r = fnmatch(ap->pattern, tmp, 0); |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 621 | #endif |
Denys Vlasenko | ccc9985 | 2016-11-25 20:14:33 +0100 | [diff] [blame] | 622 | if (trunc_slash) *trunc_slash = '/'; |
| 623 | return r == 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 624 | } |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 625 | |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 626 | #if ENABLE_FEATURE_FIND_PATH |
| 627 | ACTF(path) |
| 628 | { |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 629 | # if FNM_CASEFOLD |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 630 | return fnmatch(ap->pattern, fileName, (ap->ipath ? FNM_CASEFOLD : 0)) == 0; |
Denys Vlasenko | b24ef03 | 2011-05-23 00:40:54 +0200 | [diff] [blame] | 631 | # else |
| 632 | if (ap->ipath) |
| 633 | fileName = strcpy_upcase(alloca(strlen(fileName) + 1), fileName); |
| 634 | return fnmatch(ap->pattern, fileName, 0) == 0; |
| 635 | # endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 636 | } |
| 637 | #endif |
| 638 | #if ENABLE_FEATURE_FIND_REGEX |
| 639 | ACTF(regex) |
| 640 | { |
| 641 | regmatch_t match; |
| 642 | if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/)) |
| 643 | return 0; /* no match */ |
| 644 | if (match.rm_so) |
| 645 | return 0; /* match doesn't start at pos 0 */ |
| 646 | if (fileName[match.rm_eo]) |
| 647 | return 0; /* match doesn't end exactly at end of pathname */ |
| 648 | return 1; |
| 649 | } |
| 650 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 651 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 652 | ACTF(type) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 653 | { |
Denis Vlasenko | 31c65f2 | 2006-10-31 23:39:37 +0000 | [diff] [blame] | 654 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 655 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 656 | #endif |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 657 | #if ENABLE_FEATURE_FIND_EXECUTABLE |
| 658 | ACTF(executable) |
| 659 | { |
| 660 | return access(fileName, X_OK) == 0; |
| 661 | } |
| 662 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 663 | #if ENABLE_FEATURE_FIND_PERM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 664 | ACTF(perm) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 665 | { |
Denys Vlasenko | 3e9b13e | 2014-09-05 12:16:15 +0200 | [diff] [blame] | 666 | /* -perm [+/]mode: at least one of perm_mask bits are set */ |
| 667 | if (ap->perm_char == '+' || ap->perm_char == '/') |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 668 | return (statbuf->st_mode & ap->perm_mask) != 0; |
| 669 | /* -perm -mode: all of perm_mask are set */ |
| 670 | if (ap->perm_char == '-') |
| 671 | return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask; |
| 672 | /* -perm mode: file mode must match perm_mask */ |
| 673 | return (statbuf->st_mode & 07777) == ap->perm_mask; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 674 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 675 | #endif |
Ismael Luceno | 421c876 | 2021-09-29 13:41:33 +0200 | [diff] [blame] | 676 | |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 677 | #if 0 \ |
| 678 | || ENABLE_FEATURE_FIND_AMIN \ |
| 679 | || ENABLE_FEATURE_FIND_ATIME \ |
| 680 | || ENABLE_FEATURE_FIND_CMIN \ |
| 681 | || ENABLE_FEATURE_FIND_CTIME \ |
| 682 | || ENABLE_FEATURE_FIND_MMIN \ |
| 683 | || ENABLE_FEATURE_FIND_MTIME |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 684 | static int time_cmp(const struct stat *statbuf, unsigned type_and_char, time_t N_from_user, unsigned unit) |
Ismael Luceno | 421c876 | 2021-09-29 13:41:33 +0200 | [diff] [blame] | 685 | { |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 686 | time_t ftime, file_age; |
| 687 | |
| 688 | ftime = statbuf->st_mtime; |
| 689 | # if ENABLE_FEATURE_FIND_ATIME || ENABLE_FEATURE_FIND_CTIME |
| 690 | # if ENABLE_FEATURE_FIND_ATIME |
| 691 | if ((type_and_char >> 8) == 'a') |
| 692 | ftime = statbuf->st_atime; |
| 693 | # endif |
| 694 | # if ENABLE_FEATURE_FIND_CTIME |
| 695 | if ((type_and_char >> 8) == 'c') |
| 696 | ftime = statbuf->st_ctime; |
| 697 | # endif |
| 698 | type_and_char &= 0xff; |
| 699 | # endif |
| 700 | file_age = time(NULL) - ftime; |
| 701 | N_from_user *= unit; |
| 702 | switch (type_and_char) { |
| 703 | case '+': return file_age >= N_from_user + unit; |
| 704 | case '-': return file_age < N_from_user; |
Ismael Luceno | 421c876 | 2021-09-29 13:41:33 +0200 | [diff] [blame] | 705 | /* just numeric time */ |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 706 | default: return file_age >= N_from_user && file_age < N_from_user + unit; |
Ismael Luceno | 421c876 | 2021-09-29 13:41:33 +0200 | [diff] [blame] | 707 | } |
| 708 | } |
| 709 | #endif |
| 710 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 711 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 712 | ACTF(mtime) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 713 | { |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 714 | return time_cmp(statbuf, |
| 715 | # if ENABLE_FEATURE_FIND_ATIME || ENABLE_FEATURE_FIND_CTIME |
| 716 | (ap->time_type << 8) | |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 717 | # endif |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 718 | ap->mtime_char, |
| 719 | ap->mtime_days, 24*60*60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 720 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 721 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 722 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 723 | ACTF(mmin) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 724 | { |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 725 | return time_cmp(statbuf, |
| 726 | # if ENABLE_FEATURE_FIND_ATIME || ENABLE_FEATURE_FIND_CTIME |
| 727 | (ap->time_type << 8) | |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 728 | # endif |
Denys Vlasenko | d8e39b5 | 2021-10-10 15:18:12 +0200 | [diff] [blame] | 729 | ap->mmin_char, |
| 730 | ap->mmin_mins, 60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 731 | } |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 732 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 733 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 734 | ACTF(newer) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 735 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 736 | return (ap->newer_mtime < statbuf->st_mtime); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 737 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 738 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 739 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 740 | ACTF(inum) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 741 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 742 | return (statbuf->st_ino == ap->inode_num); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 743 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 744 | #endif |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 745 | #if ENABLE_FEATURE_FIND_SAMEFILE |
| 746 | ACTF(samefile) |
| 747 | { |
| 748 | return statbuf->st_ino == ap->inode_num && |
| 749 | statbuf->st_dev == ap->device; |
| 750 | } |
| 751 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 752 | #if ENABLE_FEATURE_FIND_EXEC |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 753 | static int do_exec(action_exec *ap, const char *fileName) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 754 | { |
| 755 | int i, rc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 756 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 757 | int size = ap->exec_argc + ap->filelist_idx + 1; |
| 758 | # else |
| 759 | int size = ap->exec_argc + 1; |
| 760 | # endif |
| 761 | # if ENABLE_USE_PORTABLE_CODE |
| 762 | char **argv = alloca(sizeof(char*) * size); |
| 763 | # else /* gcc 4.3.1 generates smaller code: */ |
| 764 | char *argv[size]; |
| 765 | # endif |
| 766 | char **pp = argv; |
| 767 | |
| 768 | for (i = 0; i < ap->exec_argc; i++) { |
| 769 | const char *arg = ap->exec_argv[i]; |
| 770 | |
| 771 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 772 | if (ap->filelist) { |
| 773 | /* Handling "-exec +" |
| 774 | * Only one exec_argv[i] has substitution in it. |
| 775 | * Expand that one exec_argv[i] into file list. |
| 776 | */ |
| 777 | if (ap->subst_count[i] == 0) { |
| 778 | *pp++ = xstrdup(arg); |
| 779 | } else { |
| 780 | int j = 0; |
| 781 | while (ap->filelist[j]) { |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 782 | /* 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] | 783 | *pp++ = xmalloc_substitute_string(arg, 1, "{}", ap->filelist[j]); |
| 784 | free(ap->filelist[j]); |
| 785 | j++; |
| 786 | } |
| 787 | } |
| 788 | } else |
| 789 | # endif |
| 790 | { |
| 791 | /* Handling "-exec ;" */ |
| 792 | *pp++ = xmalloc_substitute_string(arg, ap->subst_count[i], "{}", fileName); |
| 793 | } |
| 794 | } |
| 795 | *pp = NULL; /* terminate the list */ |
| 796 | |
| 797 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 798 | if (ap->filelist) { |
| 799 | ap->filelist[0] = NULL; |
| 800 | ap->filelist_idx = 0; |
| 801 | ap->file_len = 0; |
| 802 | } |
| 803 | # endif |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 804 | |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 805 | rc = spawn_and_wait(argv); |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 806 | if (rc < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 807 | bb_simple_perror_msg(argv[0]); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 808 | |
| 809 | i = 0; |
| 810 | while (argv[i]) |
| 811 | free(argv[i++]); |
| 812 | return rc == 0; /* return 1 if exitcode 0 */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 813 | } |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 814 | ACTF(exec) |
| 815 | { |
| 816 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 817 | if (ap->filelist) { |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 818 | int rc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 819 | |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 820 | ap->filelist = xrealloc_vector(ap->filelist, 8, ap->filelist_idx); |
| 821 | ap->filelist[ap->filelist_idx++] = xstrdup(fileName); |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 822 | ap->file_len += strlen(fileName) + sizeof(char*) + 1; |
| 823 | /* If we have lots of files already, exec the command */ |
| 824 | rc = 1; |
Denys Vlasenko | f92f1d0 | 2014-06-22 13:54:40 +0200 | [diff] [blame] | 825 | if (ap->file_len >= G.max_argv_len) |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 826 | rc = do_exec(ap, NULL); |
| 827 | return rc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 828 | } |
| 829 | # endif |
| 830 | return do_exec(ap, fileName); |
| 831 | } |
| 832 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 833 | static int flush_exec_plus(void) |
| 834 | { |
| 835 | action *ap; |
| 836 | action **app; |
| 837 | action ***appp = G.actions; |
| 838 | while ((app = *appp++) != NULL) { |
| 839 | while ((ap = *app++) != NULL) { |
| 840 | if (ap->f == (action_fp)func_exec) { |
| 841 | action_exec *ae = (void*)ap; |
| 842 | if (ae->filelist_idx != 0) { |
| 843 | int rc = do_exec(ae, NULL); |
| 844 | # if ENABLE_FEATURE_FIND_NOT |
| 845 | if (ap->invert) rc = !rc; |
| 846 | # endif |
Denys Vlasenko | 6be3a52 | 2014-06-19 11:32:11 +0200 | [diff] [blame] | 847 | if (rc == 0) |
| 848 | return 1; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 849 | } |
| 850 | } |
| 851 | } |
| 852 | } |
| 853 | return 0; |
| 854 | } |
| 855 | # endif |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 856 | #endif |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 857 | #if ENABLE_FEATURE_FIND_USER |
| 858 | ACTF(user) |
| 859 | { |
| 860 | return (statbuf->st_uid == ap->uid); |
| 861 | } |
| 862 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 863 | #if ENABLE_FEATURE_FIND_GROUP |
| 864 | ACTF(group) |
| 865 | { |
| 866 | return (statbuf->st_gid == ap->gid); |
| 867 | } |
| 868 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 869 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 870 | ACTF(print0) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 871 | { |
| 872 | printf("%s%c", fileName, '\0'); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 873 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 874 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 875 | #endif |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 876 | ACTF(print) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 877 | { |
| 878 | puts(fileName); |
| 879 | return TRUE; |
| 880 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 881 | #if ENABLE_FEATURE_FIND_PAREN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 882 | ACTF(paren) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 883 | { |
| 884 | return exec_actions(ap->subexpr, fileName, statbuf); |
| 885 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 886 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 887 | #if ENABLE_FEATURE_FIND_SIZE |
| 888 | ACTF(size) |
| 889 | { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 890 | if (ap->size_char == '+') |
| 891 | return statbuf->st_size > ap->size; |
| 892 | if (ap->size_char == '-') |
| 893 | return statbuf->st_size < ap->size; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 894 | return statbuf->st_size == ap->size; |
| 895 | } |
| 896 | #endif |
Bernhard Reutner-Fischer | 6d79dd6 | 2007-04-05 09:40:12 +0000 | [diff] [blame] | 897 | #if ENABLE_FEATURE_FIND_PRUNE |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 898 | /* |
| 899 | * -prune: if -depth is not given, return true and do not descend |
| 900 | * current dir; if -depth is given, return false with no effect. |
| 901 | * Example: |
| 902 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print |
| 903 | */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 904 | ACTF(prune) |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 905 | { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 906 | return SKIP + TRUE; |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 907 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 908 | #endif |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 909 | #if ENABLE_FEATURE_FIND_QUIT |
| 910 | ACTF(quit) |
| 911 | { |
| 912 | exit(G.exitstatus); |
| 913 | } |
| 914 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 915 | #if ENABLE_FEATURE_FIND_DELETE |
| 916 | ACTF(delete) |
| 917 | { |
| 918 | int rc; |
| 919 | if (S_ISDIR(statbuf->st_mode)) { |
Denys Vlasenko | 8c0708a | 2015-10-07 21:01:47 +0200 | [diff] [blame] | 920 | /* "find . -delete" skips rmdir(".") */ |
| 921 | rc = 0; |
| 922 | if (NOT_LONE_CHAR(fileName, '.')) |
| 923 | rc = rmdir(fileName); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 924 | } else { |
| 925 | rc = unlink(fileName); |
| 926 | } |
| 927 | if (rc < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 928 | bb_simple_perror_msg(fileName); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 929 | return TRUE; |
| 930 | } |
| 931 | #endif |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 932 | #if ENABLE_FEATURE_FIND_EMPTY |
| 933 | ACTF(empty) |
| 934 | { |
| 935 | if (S_ISDIR(statbuf->st_mode)) { |
| 936 | DIR *dir; |
| 937 | struct dirent *dent; |
| 938 | |
| 939 | dir = opendir(fileName); |
| 940 | if (!dir) { |
| 941 | bb_simple_perror_msg(fileName); |
| 942 | return FALSE; |
| 943 | } |
| 944 | |
| 945 | while ((dent = readdir(dir)) != NULL |
| 946 | && DOT_OR_DOTDOT(dent->d_name) |
| 947 | ) { |
| 948 | continue; |
| 949 | } |
| 950 | closedir(dir); |
| 951 | return dent == NULL; |
| 952 | } |
| 953 | return S_ISREG(statbuf->st_mode) && statbuf->st_size == 0; |
| 954 | } |
| 955 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 956 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 957 | ACTF(context) |
| 958 | { |
| 959 | security_context_t con; |
| 960 | int rc; |
| 961 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 962 | if (G.recurse_flags & ACTION_FOLLOWLINKS) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 963 | rc = getfilecon(fileName, &con); |
| 964 | } else { |
| 965 | rc = lgetfilecon(fileName, &con); |
| 966 | } |
| 967 | if (rc < 0) |
| 968 | return FALSE; |
| 969 | rc = strcmp(ap->context, con); |
| 970 | freecon(con); |
| 971 | return rc == 0; |
| 972 | } |
| 973 | #endif |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 974 | #if ENABLE_FEATURE_FIND_LINKS |
| 975 | ACTF(links) |
| 976 | { |
| 977 | switch(ap->links_char) { |
| 978 | case '-' : return (statbuf->st_nlink < ap->links_count); |
| 979 | case '+' : return (statbuf->st_nlink > ap->links_count); |
| 980 | default: return (statbuf->st_nlink == ap->links_count); |
| 981 | } |
| 982 | } |
| 983 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 984 | |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 985 | static int FAST_FUNC fileAction( |
| 986 | struct recursive_state *state IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM), |
| 987 | const char *fileName, |
| 988 | struct stat *statbuf) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 989 | { |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 990 | int r; |
Denys Vlasenko | 2dc1a97 | 2012-09-06 15:24:11 +0200 | [diff] [blame] | 991 | int same_fs = 1; |
| 992 | |
| 993 | #if ENABLE_FEATURE_FIND_XDEV |
| 994 | if (S_ISDIR(statbuf->st_mode) && G.xdev_count) { |
| 995 | int i; |
| 996 | for (i = 0; i < G.xdev_count; i++) { |
| 997 | if (G.xdev_dev[i] == statbuf->st_dev) |
| 998 | goto found; |
| 999 | } |
| 1000 | //bb_error_msg("'%s': not same fs", fileName); |
| 1001 | same_fs = 0; |
| 1002 | found: ; |
| 1003 | } |
| 1004 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1005 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1006 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 1007 | if (state->depth < G.minmaxdepth[0]) { |
Denys Vlasenko | 2dc1a97 | 2012-09-06 15:24:11 +0200 | [diff] [blame] | 1008 | if (same_fs) |
| 1009 | return TRUE; /* skip this, continue recursing */ |
| 1010 | return SKIP; /* stop recursing */ |
| 1011 | } |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 1012 | if (state->depth > G.minmaxdepth[1]) |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1013 | return SKIP; /* stop recursing */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1014 | #endif |
| 1015 | |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1016 | r = exec_actions(G.actions, fileName, statbuf); |
| 1017 | /* Had no explicit -print[0] or -exec? then print */ |
| 1018 | if ((r & TRUE) && G.need_print) |
| 1019 | puts(fileName); |
| 1020 | |
| 1021 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 1022 | if (S_ISDIR(statbuf->st_mode)) { |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 1023 | if (state->depth == G.minmaxdepth[1]) |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1024 | return SKIP; |
| 1025 | } |
| 1026 | #endif |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1027 | /* -xdev stops on mountpoints, but AFTER mountpoit itself |
| 1028 | * is processed as usual */ |
Denys Vlasenko | 2dc1a97 | 2012-09-06 15:24:11 +0200 | [diff] [blame] | 1029 | if (!same_fs) { |
| 1030 | return SKIP; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1031 | } |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 1032 | |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 1033 | /* Cannot return 0: our caller, recursive_action(), |
| 1034 | * will perror() and skip dirs (if called on dir) */ |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1035 | return (r & SKIP) ? SKIP : TRUE; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1038 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1039 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 1040 | static int find_type(const char *type) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 1041 | { |
| 1042 | int mask = 0; |
| 1043 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1044 | if (*type == 'b') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1045 | mask = S_IFBLK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1046 | else if (*type == 'c') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1047 | mask = S_IFCHR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1048 | else if (*type == 'd') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1049 | mask = S_IFDIR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1050 | else if (*type == 'p') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1051 | mask = S_IFIFO; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1052 | else if (*type == 'f') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1053 | mask = S_IFREG; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1054 | else if (*type == 'l') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1055 | mask = S_IFLNK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 1056 | else if (*type == 's') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 1057 | mask = S_IFSOCK; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 1058 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1059 | if (mask == 0 || type[1] != '\0') |
Denys Vlasenko | 0f296a3 | 2015-10-14 13:21:01 +0200 | [diff] [blame] | 1060 | bb_error_msg_and_die(bb_msg_invalid_arg_to, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 1061 | |
| 1062 | return mask; |
| 1063 | } |
| 1064 | #endif |
| 1065 | |
Denis Vlasenko | 4b9b0ce | 2007-07-01 17:35:17 +0000 | [diff] [blame] | 1066 | #if ENABLE_FEATURE_FIND_PERM \ |
| 1067 | || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \ |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1068 | || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 1069 | static const char* plus_minus_num(const char* str) |
| 1070 | { |
| 1071 | if (*str == '-' || *str == '+') |
| 1072 | str++; |
| 1073 | return str; |
| 1074 | } |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 1075 | #endif |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 1076 | |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1077 | /* Say no to GCCism */ |
| 1078 | #define USE_NESTED_FUNCTION 0 |
| 1079 | |
| 1080 | #if !USE_NESTED_FUNCTION |
| 1081 | struct pp_locals { |
| 1082 | action*** appp; |
| 1083 | unsigned cur_group; |
| 1084 | unsigned cur_action; |
| 1085 | IF_FEATURE_FIND_NOT( bool invert_flag; ) |
| 1086 | }; |
| 1087 | static action* alloc_action(struct pp_locals *ppl, int sizeof_struct, action_fp f) |
| 1088 | { |
| 1089 | action *ap = xzalloc(sizeof_struct); |
| 1090 | action **app; |
| 1091 | action ***group = &ppl->appp[ppl->cur_group]; |
| 1092 | *group = app = xrealloc(*group, (ppl->cur_action+2) * sizeof(ppl->appp[0][0])); |
| 1093 | app[ppl->cur_action++] = ap; |
| 1094 | app[ppl->cur_action] = NULL; |
| 1095 | ap->f = f; |
| 1096 | IF_FEATURE_FIND_NOT( ap->invert = ppl->invert_flag; ) |
| 1097 | IF_FEATURE_FIND_NOT( ppl->invert_flag = 0; ) |
| 1098 | return ap; |
| 1099 | } |
| 1100 | #endif |
| 1101 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 1102 | static action*** parse_params(char **argv) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1103 | { |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1104 | enum { |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1105 | OPT_FOLLOW , |
| 1106 | IF_FEATURE_FIND_XDEV( OPT_XDEV ,) |
| 1107 | IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,) |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1108 | PARM_a , |
| 1109 | PARM_o , |
| 1110 | IF_FEATURE_FIND_NOT( PARM_char_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1111 | #if ENABLE_DESKTOP |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1112 | PARM_and , |
| 1113 | PARM_or , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1114 | IF_FEATURE_FIND_NOT( PARM_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1115 | #endif |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1116 | PARM_print , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1117 | IF_FEATURE_FIND_PRINT0( PARM_print0 ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1118 | IF_FEATURE_FIND_PRUNE( PARM_prune ,) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1119 | IF_FEATURE_FIND_QUIT( PARM_quit ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1120 | IF_FEATURE_FIND_DELETE( PARM_delete ,) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 1121 | IF_FEATURE_FIND_EMPTY( PARM_empty ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1122 | IF_FEATURE_FIND_EXEC( PARM_exec ,) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 1123 | IF_FEATURE_FIND_EXECUTABLE(PARM_executable,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1124 | IF_FEATURE_FIND_PAREN( PARM_char_brace,) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1125 | /* All options/actions starting from here require argument */ |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1126 | PARM_name , |
| 1127 | PARM_iname , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1128 | IF_FEATURE_FIND_PATH( PARM_path ,) |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1129 | #if ENABLE_DESKTOP |
| 1130 | /* -wholename is a synonym for -path */ |
Denys Vlasenko | 7c8aa2b | 2012-05-18 02:53:11 +0200 | [diff] [blame] | 1131 | /* We support it because Linux kernel's "make tags" uses it */ |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1132 | IF_FEATURE_FIND_PATH( PARM_wholename ,) |
| 1133 | #endif |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 1134 | IF_FEATURE_FIND_PATH( PARM_ipath ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1135 | IF_FEATURE_FIND_REGEX( PARM_regex ,) |
| 1136 | IF_FEATURE_FIND_TYPE( PARM_type ,) |
| 1137 | IF_FEATURE_FIND_PERM( PARM_perm ,) |
| 1138 | IF_FEATURE_FIND_MTIME( PARM_mtime ,) |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1139 | IF_FEATURE_FIND_ATIME( PARM_atime ,) |
| 1140 | IF_FEATURE_FIND_CTIME( PARM_ctime ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1141 | IF_FEATURE_FIND_MMIN( PARM_mmin ,) |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1142 | IF_FEATURE_FIND_AMIN( PARM_amin ,) |
| 1143 | IF_FEATURE_FIND_CMIN( PARM_cmin ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1144 | IF_FEATURE_FIND_NEWER( PARM_newer ,) |
| 1145 | IF_FEATURE_FIND_INUM( PARM_inum ,) |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 1146 | IF_FEATURE_FIND_SAMEFILE(PARM_samefile ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1147 | IF_FEATURE_FIND_USER( PARM_user ,) |
| 1148 | IF_FEATURE_FIND_GROUP( PARM_group ,) |
| 1149 | IF_FEATURE_FIND_SIZE( PARM_size ,) |
| 1150 | IF_FEATURE_FIND_CONTEXT(PARM_context ,) |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1151 | IF_FEATURE_FIND_LINKS( PARM_links ,) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1152 | IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,OPT_MAXDEPTH,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1153 | }; |
| 1154 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1155 | static const char params[] ALIGN1 = |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1156 | "-follow\0" |
| 1157 | IF_FEATURE_FIND_XDEV( "-xdev\0" ) |
| 1158 | IF_FEATURE_FIND_DEPTH( "-depth\0" ) |
| 1159 | "-a\0" |
| 1160 | "-o\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1161 | IF_FEATURE_FIND_NOT( "!\0" ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1162 | #if ENABLE_DESKTOP |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1163 | "-and\0" |
| 1164 | "-or\0" |
| 1165 | IF_FEATURE_FIND_NOT( "-not\0" ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1166 | #endif |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1167 | "-print\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1168 | IF_FEATURE_FIND_PRINT0( "-print0\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1169 | IF_FEATURE_FIND_PRUNE( "-prune\0" ) |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1170 | IF_FEATURE_FIND_QUIT( "-quit\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1171 | IF_FEATURE_FIND_DELETE( "-delete\0" ) |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 1172 | IF_FEATURE_FIND_EMPTY( "-empty\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1173 | IF_FEATURE_FIND_EXEC( "-exec\0" ) |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 1174 | IF_FEATURE_FIND_EXECUTABLE("-executable\0") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1175 | IF_FEATURE_FIND_PAREN( "(\0" ) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1176 | /* All options/actions starting from here require argument */ |
Denys Vlasenko | 6967578 | 2013-01-14 01:34:48 +0100 | [diff] [blame] | 1177 | "-name\0" |
| 1178 | "-iname\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1179 | IF_FEATURE_FIND_PATH( "-path\0" ) |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1180 | #if ENABLE_DESKTOP |
| 1181 | IF_FEATURE_FIND_PATH( "-wholename\0") |
| 1182 | #endif |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 1183 | IF_FEATURE_FIND_PATH( "-ipath\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1184 | IF_FEATURE_FIND_REGEX( "-regex\0" ) |
| 1185 | IF_FEATURE_FIND_TYPE( "-type\0" ) |
| 1186 | IF_FEATURE_FIND_PERM( "-perm\0" ) |
| 1187 | IF_FEATURE_FIND_MTIME( "-mtime\0" ) |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1188 | IF_FEATURE_FIND_ATIME( "-atime\0" ) |
| 1189 | IF_FEATURE_FIND_CTIME( "-ctime\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1190 | IF_FEATURE_FIND_MMIN( "-mmin\0" ) |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1191 | IF_FEATURE_FIND_AMIN( "-amin\0" ) |
| 1192 | IF_FEATURE_FIND_CMIN( "-cmin\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1193 | IF_FEATURE_FIND_NEWER( "-newer\0" ) |
| 1194 | IF_FEATURE_FIND_INUM( "-inum\0" ) |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 1195 | IF_FEATURE_FIND_SAMEFILE("-samefile\0") |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1196 | IF_FEATURE_FIND_USER( "-user\0" ) |
| 1197 | IF_FEATURE_FIND_GROUP( "-group\0" ) |
| 1198 | IF_FEATURE_FIND_SIZE( "-size\0" ) |
| 1199 | IF_FEATURE_FIND_CONTEXT("-context\0") |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1200 | IF_FEATURE_FIND_LINKS( "-links\0" ) |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1201 | IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0") |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1202 | ; |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1203 | |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1204 | #if !USE_NESTED_FUNCTION |
| 1205 | struct pp_locals ppl; |
| 1206 | #define appp (ppl.appp ) |
| 1207 | #define cur_group (ppl.cur_group ) |
| 1208 | #define cur_action (ppl.cur_action ) |
| 1209 | #define invert_flag (ppl.invert_flag) |
| 1210 | #define ALLOC_ACTION(name) (action_##name*)alloc_action(&ppl, sizeof(action_##name), (action_fp) func_##name) |
| 1211 | #else |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1212 | action*** appp; |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1213 | unsigned cur_group; |
| 1214 | unsigned cur_action; |
| 1215 | IF_FEATURE_FIND_NOT( bool invert_flag; ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1216 | |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 1217 | /* This is the only place in busybox where we use nested function. |
| 1218 | * So far more standard alternatives were bigger. */ |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1219 | /* Auto decl suppresses "func without a prototype" warning: */ |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 1220 | auto action* alloc_action(int sizeof_struct, action_fp f); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1221 | action* alloc_action(int sizeof_struct, action_fp f) |
| 1222 | { |
| 1223 | action *ap; |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1224 | 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] | 1225 | appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1226 | appp[cur_group][cur_action] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1227 | ap->f = f; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1228 | IF_FEATURE_FIND_NOT( ap->invert = invert_flag; ) |
| 1229 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1230 | return ap; |
| 1231 | } |
| 1232 | #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] | 1233 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1234 | |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1235 | cur_group = 0; |
| 1236 | cur_action = 0; |
| 1237 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1238 | appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1239 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1240 | while (*argv) { |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 1241 | const char *arg = argv[0]; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1242 | int parm = index_in_strings(params, arg); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1243 | const char *arg1 = argv[1]; |
| 1244 | |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1245 | dbg("arg:'%s' arg1:'%s' parm:%d PARM_type:%d", arg, arg1, parm, PARM_type); |
| 1246 | |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1247 | if (parm >= PARM_name) { |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1248 | /* All options/actions starting from -name require argument */ |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1249 | if (!arg1) |
| 1250 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 1251 | argv++; |
| 1252 | } |
| 1253 | |
| 1254 | /* We can use big switch() here, but on i386 |
| 1255 | * it doesn't give smaller code. Other arches? */ |
| 1256 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1257 | /* Options always return true. They always take effect |
| 1258 | * rather than being processed only when their place in the |
| 1259 | * expression is reached. |
| 1260 | */ |
| 1261 | /* Options */ |
Ian Wienand | abc33ac | 2011-08-28 06:53:20 +0200 | [diff] [blame] | 1262 | if (parm == OPT_FOLLOW) { |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 1263 | dbg("follow enabled: %d", __LINE__); |
| 1264 | G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK; |
Ian Wienand | abc33ac | 2011-08-28 06:53:20 +0200 | [diff] [blame] | 1265 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1266 | #if ENABLE_FEATURE_FIND_XDEV |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1267 | else if (parm == OPT_XDEV) { |
| 1268 | dbg("%d", __LINE__); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1269 | G.xdev_on = 1; |
| 1270 | } |
| 1271 | #endif |
| 1272 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 1273 | else if (parm == OPT_MINDEPTH || parm == OPT_MINDEPTH + 1) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1274 | dbg("%d", __LINE__); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1275 | G.minmaxdepth[parm - OPT_MINDEPTH] = xatoi_positive(arg1); |
| 1276 | } |
| 1277 | #endif |
| 1278 | #if ENABLE_FEATURE_FIND_DEPTH |
| 1279 | else if (parm == OPT_DEPTH) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1280 | dbg("%d", __LINE__); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1281 | G.recurse_flags |= ACTION_DEPTHFIRST; |
| 1282 | } |
| 1283 | #endif |
| 1284 | /* Actions are grouped by operators |
| 1285 | * ( expr ) Force precedence |
| 1286 | * ! expr True if expr is false |
| 1287 | * -not expr Same as ! expr |
| 1288 | * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false |
| 1289 | * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true |
| 1290 | * expr1 , expr2 List; both expr1 and expr2 are always evaluated |
| 1291 | * We implement: (), -a, -o |
| 1292 | */ |
| 1293 | /* Operators */ |
| 1294 | else if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1295 | dbg("%d", __LINE__); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1296 | /* no further special handling required */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1297 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1298 | else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1299 | dbg("%d", __LINE__); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1300 | /* start new OR group */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1301 | cur_group++; |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1302 | appp = xrealloc(appp, (cur_group+2) * sizeof(appp[0])); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1303 | /*appp[cur_group] = NULL; - already NULL */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1304 | appp[cur_group+1] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1305 | cur_action = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1306 | } |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1307 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1308 | else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) { |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 1309 | /* also handles "find ! ! -name 'foo*'" */ |
| 1310 | invert_flag ^= 1; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1311 | dbg("invert_flag:%d", invert_flag); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1312 | } |
| 1313 | #endif |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1314 | /* Actions */ |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1315 | else if (parm == PARM_print) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1316 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1317 | G.need_print = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1318 | (void) ALLOC_ACTION(print); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1319 | } |
| 1320 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1321 | else if (parm == PARM_print0) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1322 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1323 | G.need_print = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1324 | (void) ALLOC_ACTION(print0); |
| 1325 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 1326 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1327 | #if ENABLE_FEATURE_FIND_PRUNE |
| 1328 | else if (parm == PARM_prune) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1329 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1330 | (void) ALLOC_ACTION(prune); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1331 | } |
| 1332 | #endif |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1333 | #if ENABLE_FEATURE_FIND_QUIT |
| 1334 | else if (parm == PARM_quit) { |
| 1335 | dbg("%d", __LINE__); |
| 1336 | (void) ALLOC_ACTION(quit); |
| 1337 | } |
| 1338 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1339 | #if ENABLE_FEATURE_FIND_DELETE |
| 1340 | else if (parm == PARM_delete) { |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1341 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1342 | G.need_print = 0; |
| 1343 | G.recurse_flags |= ACTION_DEPTHFIRST; |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1344 | (void) ALLOC_ACTION(delete); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1345 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 1346 | #endif |
Aaro Koskinen | af18b30 | 2019-09-12 12:04:13 +0200 | [diff] [blame] | 1347 | #if ENABLE_FEATURE_FIND_EMPTY |
| 1348 | else if (parm == PARM_empty) { |
| 1349 | dbg("%d", __LINE__); |
| 1350 | (void) ALLOC_ACTION(empty); |
| 1351 | } |
| 1352 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1353 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1354 | else if (parm == PARM_exec) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1355 | int i; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1356 | action_exec *ap; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1357 | IF_FEATURE_FIND_EXEC_PLUS(int all_subst = 0;) |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1358 | dbg("%d", __LINE__); |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1359 | G.need_print = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1360 | ap = ALLOC_ACTION(exec); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1361 | ap->exec_argv = ++argv; /* first arg after -exec */ |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1362 | /*ap->exec_argc = 0; - ALLOC_ACTION did it */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1363 | while (1) { |
Bernhard Reutner-Fischer | 2e73d84 | 2010-02-25 13:22:37 +0100 | [diff] [blame] | 1364 | if (!*argv) /* did not see ';' or '+' until end */ |
| 1365 | bb_error_msg_and_die(bb_msg_requires_arg, "-exec"); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1366 | // find -exec echo Foo ">{}<" ";" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1367 | // executes "echo Foo >FILENAME<", |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1368 | // find -exec echo Foo ">{}<" "+" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1369 | // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...". |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1370 | if ((argv[0][0] == ';' || argv[0][0] == '+') |
| 1371 | && argv[0][1] == '\0' |
| 1372 | ) { |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1373 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 1374 | if (argv[0][0] == '+') |
| 1375 | ap->filelist = xzalloc(sizeof(ap->filelist[0])); |
| 1376 | # endif |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 1377 | break; |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1378 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1379 | argv++; |
| 1380 | ap->exec_argc++; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 1381 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1382 | if (ap->exec_argc == 0) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 1383 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1384 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1385 | i = ap->exec_argc; |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1386 | while (i--) { |
Denys Vlasenko | 6f06890 | 2014-02-27 11:17:06 +0100 | [diff] [blame] | 1387 | ap->subst_count[i] = count_strstr(ap->exec_argv[i], "{}"); |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1388 | IF_FEATURE_FIND_EXEC_PLUS(all_subst += ap->subst_count[i];) |
| 1389 | } |
| 1390 | # if ENABLE_FEATURE_FIND_EXEC_PLUS |
| 1391 | /* |
| 1392 | * coreutils expects {} to appear only once in "-exec +" |
| 1393 | */ |
| 1394 | if (all_subst != 1 && ap->filelist) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1395 | bb_simple_error_msg_and_die("only one '{}' allowed for -exec +"); |
Bartosz Golaszewski | 14158b4 | 2014-06-17 17:09:17 +0200 | [diff] [blame] | 1396 | # endif |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1397 | } |
| 1398 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1399 | #if ENABLE_FEATURE_FIND_PAREN |
| 1400 | else if (parm == PARM_char_brace) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1401 | action_paren *ap; |
| 1402 | char **endarg; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 1403 | unsigned nested = 1; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1404 | |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1405 | dbg("%d", __LINE__); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1406 | endarg = argv; |
| 1407 | while (1) { |
| 1408 | if (!*++endarg) |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 1409 | bb_simple_error_msg_and_die("unpaired '('"); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 1410 | if (LONE_CHAR(*endarg, '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1411 | nested++; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 1412 | else if (LONE_CHAR(*endarg, ')') && !--nested) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1413 | *endarg = NULL; |
| 1414 | break; |
| 1415 | } |
| 1416 | } |
| 1417 | ap = ALLOC_ACTION(paren); |
| 1418 | ap->subexpr = parse_params(argv + 1); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1419 | *endarg = (char*) ")"; /* restore NULLed parameter */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1420 | argv = endarg; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1421 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1422 | #endif |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 1423 | else if (parm == PARM_name || parm == PARM_iname) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1424 | action_name *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1425 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1426 | ap = ALLOC_ACTION(name); |
| 1427 | ap->pattern = arg1; |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 1428 | ap->iname = (parm == PARM_iname); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1429 | } |
| 1430 | #if ENABLE_FEATURE_FIND_PATH |
Denys Vlasenko | be1cfee | 2012-05-17 12:25:25 +0200 | [diff] [blame] | 1431 | 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] | 1432 | action_path *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1433 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1434 | ap = ALLOC_ACTION(path); |
| 1435 | ap->pattern = arg1; |
Bogdan Harjoc | 7948ecb | 2011-05-22 03:50:21 +0200 | [diff] [blame] | 1436 | ap->ipath = (parm == PARM_ipath); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1437 | } |
| 1438 | #endif |
| 1439 | #if ENABLE_FEATURE_FIND_REGEX |
| 1440 | else if (parm == PARM_regex) { |
| 1441 | action_regex *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1442 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1443 | ap = ALLOC_ACTION(regex); |
| 1444 | xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/); |
| 1445 | } |
| 1446 | #endif |
| 1447 | #if ENABLE_FEATURE_FIND_TYPE |
| 1448 | else if (parm == PARM_type) { |
| 1449 | action_type *ap; |
| 1450 | ap = ALLOC_ACTION(type); |
| 1451 | ap->type_mask = find_type(arg1); |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1452 | dbg("created:type mask:%x", ap->type_mask); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1453 | } |
| 1454 | #endif |
Denys Vlasenko | 008e73b | 2018-07-06 21:04:41 +0200 | [diff] [blame] | 1455 | #if ENABLE_FEATURE_FIND_EXECUTABLE |
| 1456 | else if (parm == PARM_executable) { |
| 1457 | (void) ALLOC_ACTION(executable); |
| 1458 | } |
| 1459 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1460 | #if ENABLE_FEATURE_FIND_PERM |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1461 | /* -perm BITS File's mode bits are exactly BITS (octal or symbolic). |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1462 | * Symbolic modes use mode 0 as a point of departure. |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1463 | * -perm -BITS All of the BITS are set in file's mode. |
Denys Vlasenko | 3e9b13e | 2014-09-05 12:16:15 +0200 | [diff] [blame] | 1464 | * -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] | 1465 | */ |
| 1466 | else if (parm == PARM_perm) { |
| 1467 | action_perm *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1468 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1469 | ap = ALLOC_ACTION(perm); |
| 1470 | ap->perm_char = arg1[0]; |
Denys Vlasenko | 3e9b13e | 2014-09-05 12:16:15 +0200 | [diff] [blame] | 1471 | arg1 = (arg1[0] == '/' ? arg1+1 : plus_minus_num(arg1)); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1472 | /*ap->perm_mask = 0; - ALLOC_ACTION did it */ |
Denys Vlasenko | 5711a2a | 2015-10-07 17:55:33 +0200 | [diff] [blame] | 1473 | ap->perm_mask = bb_parse_mode(arg1, ap->perm_mask); |
| 1474 | if (ap->perm_mask == (mode_t)-1) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 1475 | bb_error_msg_and_die("invalid mode '%s'", arg1); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1476 | } |
| 1477 | #endif |
| 1478 | #if ENABLE_FEATURE_FIND_MTIME |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1479 | else if (parm == PARM_mtime |
| 1480 | # if ENABLE_FEATURE_FIND_ATIME |
| 1481 | || parm == PARM_atime |
| 1482 | # endif |
| 1483 | # if ENABLE_FEATURE_FIND_CTIME |
| 1484 | || parm == PARM_ctime |
| 1485 | # endif |
| 1486 | ) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1487 | action_mtime *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1488 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1489 | ap = ALLOC_ACTION(mtime); |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1490 | # if ENABLE_FEATURE_FIND_AMIN || ENABLE_FEATURE_FIND_CMIN |
| 1491 | ap->time_type = arg[1]; |
| 1492 | # endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1493 | ap->mtime_char = arg1[0]; |
| 1494 | ap->mtime_days = xatoul(plus_minus_num(arg1)); |
| 1495 | } |
| 1496 | #endif |
| 1497 | #if ENABLE_FEATURE_FIND_MMIN |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1498 | else if (parm == PARM_mmin |
| 1499 | # if ENABLE_FEATURE_FIND_AMIN |
| 1500 | || parm == PARM_amin |
| 1501 | # endif |
| 1502 | # if ENABLE_FEATURE_FIND_CMIN |
| 1503 | || parm == PARM_cmin |
| 1504 | # endif |
| 1505 | ) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1506 | action_mmin *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1507 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1508 | ap = ALLOC_ACTION(mmin); |
Ismael Luceno | 04ad683 | 2021-10-09 21:40:53 +0200 | [diff] [blame] | 1509 | # if ENABLE_FEATURE_FIND_AMIN || ENABLE_FEATURE_FIND_CMIN |
| 1510 | ap->time_type = arg[1]; |
| 1511 | # endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1512 | ap->mmin_char = arg1[0]; |
| 1513 | ap->mmin_mins = xatoul(plus_minus_num(arg1)); |
| 1514 | } |
| 1515 | #endif |
| 1516 | #if ENABLE_FEATURE_FIND_NEWER |
| 1517 | else if (parm == PARM_newer) { |
| 1518 | struct stat stat_newer; |
| 1519 | action_newer *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1520 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1521 | ap = ALLOC_ACTION(newer); |
| 1522 | xstat(arg1, &stat_newer); |
| 1523 | ap->newer_mtime = stat_newer.st_mtime; |
| 1524 | } |
| 1525 | #endif |
| 1526 | #if ENABLE_FEATURE_FIND_INUM |
| 1527 | else if (parm == PARM_inum) { |
| 1528 | action_inum *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1529 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1530 | ap = ALLOC_ACTION(inum); |
| 1531 | ap->inode_num = xatoul(arg1); |
| 1532 | } |
| 1533 | #endif |
Aaro Koskinen | aaade69 | 2021-11-28 12:11:48 +0200 | [diff] [blame] | 1534 | #if ENABLE_FEATURE_FIND_SAMEFILE |
| 1535 | else if (parm == PARM_samefile) { |
| 1536 | action_samefile *ap; |
| 1537 | struct stat stbuf; |
| 1538 | dbg("%d", __LINE__); |
| 1539 | if (G.recurse_flags & (ACTION_FOLLOWLINKS | |
| 1540 | ACTION_FOLLOWLINKS_L0)) |
| 1541 | xstat(arg1, &stbuf); |
| 1542 | else if (lstat(arg1, &stbuf)) |
| 1543 | bb_perror_msg_and_die("can't stat '%s'", arg1); |
| 1544 | ap = ALLOC_ACTION(samefile); |
| 1545 | ap->inode_num = stbuf.st_ino; |
| 1546 | ap->device = stbuf.st_dev; |
| 1547 | } |
| 1548 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1549 | #if ENABLE_FEATURE_FIND_USER |
| 1550 | else if (parm == PARM_user) { |
| 1551 | action_user *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1552 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1553 | ap = ALLOC_ACTION(user); |
| 1554 | ap->uid = bb_strtou(arg1, NULL, 10); |
| 1555 | if (errno) |
| 1556 | ap->uid = xuname2uid(arg1); |
| 1557 | } |
| 1558 | #endif |
| 1559 | #if ENABLE_FEATURE_FIND_GROUP |
| 1560 | else if (parm == PARM_group) { |
| 1561 | action_group *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1562 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1563 | ap = ALLOC_ACTION(group); |
| 1564 | ap->gid = bb_strtou(arg1, NULL, 10); |
| 1565 | if (errno) |
| 1566 | ap->gid = xgroup2gid(arg1); |
| 1567 | } |
| 1568 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1569 | #if ENABLE_FEATURE_FIND_SIZE |
| 1570 | else if (parm == PARM_size) { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1571 | /* -size n[bckw]: file uses n units of space |
| 1572 | * b (default): units are 512-byte blocks |
| 1573 | * c: 1 byte |
| 1574 | * k: kilobytes |
| 1575 | * w: 2-byte words |
| 1576 | */ |
| 1577 | #if ENABLE_LFS |
| 1578 | #define XATOU_SFX xatoull_sfx |
| 1579 | #else |
| 1580 | #define XATOU_SFX xatoul_sfx |
| 1581 | #endif |
Denys Vlasenko | 965b795 | 2020-11-30 13:03:03 +0100 | [diff] [blame] | 1582 | static const struct suffix_mult find_suffixes[] ALIGN_SUFFIX = { |
Denis Vlasenko | 99fcc62 | 2007-07-01 17:29:44 +0000 | [diff] [blame] | 1583 | { "c", 1 }, |
| 1584 | { "w", 2 }, |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 1585 | { "", 512 }, |
Denis Vlasenko | 99fcc62 | 2007-07-01 17:29:44 +0000 | [diff] [blame] | 1586 | { "b", 512 }, |
| 1587 | { "k", 1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 1588 | { "", 0 } |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1589 | }; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1590 | action_size *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1591 | dbg("%d", __LINE__); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1592 | ap = ALLOC_ACTION(size); |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1593 | ap->size_char = arg1[0]; |
| 1594 | ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1595 | } |
| 1596 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1597 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 1598 | else if (parm == PARM_context) { |
| 1599 | action_context *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1600 | dbg("%d", __LINE__); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1601 | ap = ALLOC_ACTION(context); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1602 | /*ap->context = NULL; - ALLOC_ACTION did it */ |
Denis Vlasenko | 838daca | 2007-08-16 10:41:24 +0000 | [diff] [blame] | 1603 | /* SELinux headers erroneously declare non-const parameter */ |
| 1604 | if (selinux_raw_to_trans_context((char*)arg1, &ap->context)) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 1605 | bb_simple_perror_msg(arg1); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 1606 | } |
| 1607 | #endif |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1608 | #if ENABLE_FEATURE_FIND_LINKS |
| 1609 | else if (parm == PARM_links) { |
| 1610 | action_links *ap; |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1611 | dbg("%d", __LINE__); |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1612 | ap = ALLOC_ACTION(links); |
| 1613 | ap->links_char = arg1[0]; |
| 1614 | ap->links_count = xatoul(plus_minus_num(arg1)); |
| 1615 | } |
| 1616 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1617 | else { |
| 1618 | bb_error_msg("unrecognized: %s", arg); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1619 | bb_show_usage(); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1620 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1621 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1622 | } |
Denys Vlasenko | 87249fb | 2011-08-11 02:27:13 +0200 | [diff] [blame] | 1623 | dbg("exiting %s", __func__); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1624 | return appp; |
| 1625 | #undef ALLOC_ACTION |
Denys Vlasenko | 6db5f67 | 2013-05-16 18:36:42 +0200 | [diff] [blame] | 1626 | #undef appp |
| 1627 | #undef cur_action |
| 1628 | #undef invert_flag |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1631 | int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1632 | int find_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1633 | { |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1634 | int i, firstopt; |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1635 | char **past_HLP, *saved; |
Denis Vlasenko | 7d6c929 | 2007-04-05 00:35:43 +0000 | [diff] [blame] | 1636 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1637 | INIT_G(); |
| 1638 | |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1639 | /* "find -type f" + getopt("+HLP") => disaster. |
| 1640 | * Need to avoid getopt running into a non-HLP option. |
| 1641 | * Do this by temporarily storing NULL there: |
| 1642 | */ |
| 1643 | past_HLP = argv; |
| 1644 | for (;;) { |
| 1645 | saved = *++past_HLP; |
| 1646 | if (!saved) |
| 1647 | break; |
| 1648 | if (saved[0] != '-') |
| 1649 | break; |
| 1650 | if (!saved[1]) |
| 1651 | break; /* it is "-" */ |
Denys Vlasenko | 2d9e097 | 2019-01-06 19:58:18 +0100 | [diff] [blame] | 1652 | if (saved[1] == '-' && !saved[2]) { |
| 1653 | /* it is "--" */ |
| 1654 | /* Try: find -- /dev/null */ |
| 1655 | saved = *++past_HLP; |
| 1656 | break; |
| 1657 | } |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1658 | if ((saved+1)[strspn(saved+1, "HLP")] != '\0') |
| 1659 | break; |
| 1660 | } |
| 1661 | *past_HLP = NULL; |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 1662 | /* "+": stop on first non-option */ |
Denys Vlasenko | ef2366c | 2020-12-12 08:44:32 +0100 | [diff] [blame] | 1663 | i = getopt32(argv, "+""HLP"); |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 1664 | if (i & (1<<0)) |
| 1665 | G.recurse_flags |= ACTION_FOLLOWLINKS_L0 | ACTION_DANGLING_OK; |
| 1666 | if (i & (1<<1)) |
| 1667 | G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK; |
| 1668 | /* -P is default and is ignored */ |
Denys Vlasenko | 6eb0cbe | 2014-01-09 16:07:11 +0100 | [diff] [blame] | 1669 | argv = past_HLP; /* same result as "argv += optind;" */ |
| 1670 | *past_HLP = saved; |
Denys Vlasenko | 6782f48 | 2013-05-14 17:20:35 +0200 | [diff] [blame] | 1671 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1672 | for (firstopt = 0; argv[firstopt]; firstopt++) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1673 | if (argv[firstopt][0] == '-') |
| 1674 | break; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1675 | if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!')) |
| 1676 | break; |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1677 | if (ENABLE_FEATURE_FIND_PAREN && LONE_CHAR(argv[firstopt], '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1678 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1679 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1680 | if (firstopt == 0) { |
| 1681 | *--argv = (char*)"."; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1682 | firstopt++; |
| 1683 | } |
| 1684 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1685 | G.actions = parse_params(&argv[firstopt]); |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1686 | argv[firstopt] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1687 | |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1688 | #if ENABLE_FEATURE_FIND_XDEV |
| 1689 | if (G.xdev_on) { |
| 1690 | struct stat stbuf; |
| 1691 | |
| 1692 | G.xdev_count = firstopt; |
| 1693 | G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0])); |
| 1694 | for (i = 0; argv[i]; i++) { |
| 1695 | /* not xstat(): shouldn't bomb out on |
| 1696 | * "find not_exist exist -xdev" */ |
| 1697 | if (stat(argv[i], &stbuf) == 0) |
| 1698 | G.xdev_dev[i] = stbuf.st_dev; |
| 1699 | /* else G.xdev_dev[i] stays 0 and |
| 1700 | * won't match any real device dev_t |
| 1701 | */ |
| 1702 | } |
| 1703 | } |
| 1704 | #endif |
| 1705 | |
| 1706 | for (i = 0; argv[i]; i++) { |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 1707 | if (!recursive_action(argv[i], |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1708 | G.recurse_flags,/* flags */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 1709 | fileAction, /* file action */ |
| 1710 | fileAction, /* dir action */ |
Denys Vlasenko | 689d065 | 2020-10-01 21:52:16 +0200 | [diff] [blame] | 1711 | NULL) /* user data */ |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1712 | ) { |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1713 | G.exitstatus |= EXIT_FAILURE; |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1714 | } |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 1715 | } |
Denys Vlasenko | 053c12e | 2011-02-12 20:27:13 +0100 | [diff] [blame] | 1716 | |
Denys Vlasenko | abe22cd | 2018-07-06 20:35:35 +0200 | [diff] [blame] | 1717 | IF_FEATURE_FIND_EXEC_PLUS(G.exitstatus |= flush_exec_plus();) |
| 1718 | return G.exitstatus; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1719 | } |