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 | */ |
| 12 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 13 | /* findutils-4.1.20: |
| 14 | * |
| 15 | * # find file.txt -exec 'echo {}' '{} {}' ';' |
| 16 | * find: echo file.txt: No such file or directory |
| 17 | * # find file.txt -exec 'echo' '{} {}' '; ' |
| 18 | * find: missing argument to `-exec' |
| 19 | * # find file.txt -exec 'echo {}' '{} {}' ';' junk |
| 20 | * find: paths must precede expression |
| 21 | * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';' |
| 22 | * find: paths must precede expression |
| 23 | * # find file.txt -exec 'echo' '{} {}' ';' |
| 24 | * file.txt file.txt |
| 25 | * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ])) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 26 | * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';' |
| 27 | * file.txt file.txt |
| 28 | * file.txt |
| 29 | * /tmp |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 30 | * # find -name '*.c' -o -name '*.h' |
| 31 | * [shows files, *.c and *.h intermixed] |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 32 | * # find file.txt -name '*f*' -o -name '*t*' |
| 33 | * file.txt |
| 34 | * # find file.txt -name '*z*' -o -name '*t*' |
| 35 | * file.txt |
| 36 | * # find file.txt -name '*f*' -o -name '*z*' |
| 37 | * file.txt |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 38 | * |
| 39 | * # find t z -name '*t*' -print -o -name '*z*' |
| 40 | * t |
| 41 | * # find t z t z -name '*t*' -o -name '*z*' -print |
| 42 | * z |
| 43 | * z |
| 44 | * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print |
| 45 | * (no output) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 48 | /* Testing script |
| 49 | * ./busybox find "$@" | tee /tmp/bb_find |
| 50 | * echo ================== |
| 51 | * /path/to/gnu/find "$@" | tee /tmp/std_find |
| 52 | * echo ================== |
| 53 | * diff -u /tmp/std_find /tmp/bb_find && echo Identical |
| 54 | */ |
| 55 | |
Denys Vlasenko | b9f2d9f | 2011-01-18 13:58:01 +0100 | [diff] [blame] | 56 | //applet:IF_FIND(APPLET_NOEXEC(find, find, BB_DIR_USR_BIN, BB_SUID_DROP, find)) |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 57 | |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 58 | //kbuild:lib-$(CONFIG_FIND) += find.o |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 59 | |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 60 | //config:config FIND |
| 61 | //config: bool "find" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 62 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 63 | //config: help |
| 64 | //config: find is used to search your system to find specified files. |
| 65 | //config: |
| 66 | //config:config FEATURE_FIND_PRINT0 |
| 67 | //config: bool "Enable -print0: NUL-terminated output" |
| 68 | //config: default y |
| 69 | //config: depends on FIND |
| 70 | //config: help |
| 71 | //config: Causes output names to be separated by a NUL character |
| 72 | //config: rather than a newline. This allows names that contain |
| 73 | //config: newlines and other whitespace to be more easily |
| 74 | //config: interpreted by other programs. |
| 75 | //config: |
| 76 | //config:config FEATURE_FIND_MTIME |
| 77 | //config: bool "Enable -mtime: modified time matching" |
| 78 | //config: default y |
| 79 | //config: depends on FIND |
| 80 | //config: help |
| 81 | //config: Allow searching based on the modification time of |
| 82 | //config: files, in days. |
| 83 | //config: |
| 84 | //config:config FEATURE_FIND_MMIN |
| 85 | //config: bool "Enable -mmin: modified time matching by minutes" |
| 86 | //config: default y |
| 87 | //config: depends on FIND |
| 88 | //config: help |
| 89 | //config: Allow searching based on the modification time of |
| 90 | //config: files, in minutes. |
| 91 | //config: |
| 92 | //config:config FEATURE_FIND_PERM |
| 93 | //config: bool "Enable -perm: permissions matching" |
| 94 | //config: default y |
| 95 | //config: depends on FIND |
| 96 | //config: help |
| 97 | //config: Enable searching based on file permissions. |
| 98 | //config: |
| 99 | //config:config FEATURE_FIND_TYPE |
| 100 | //config: bool "Enable -type: file type matching (file/dir/link/...)" |
| 101 | //config: default y |
| 102 | //config: depends on FIND |
| 103 | //config: help |
| 104 | //config: Enable searching based on file type (file, |
| 105 | //config: directory, socket, device, etc.). |
| 106 | //config: |
| 107 | //config:config FEATURE_FIND_XDEV |
| 108 | //config: bool "Enable -xdev: 'stay in filesystem'" |
| 109 | //config: default y |
| 110 | //config: depends on FIND |
| 111 | //config: help |
| 112 | //config: This option allows find to restrict searches to a single filesystem. |
| 113 | //config: |
| 114 | //config:config FEATURE_FIND_MAXDEPTH |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 115 | //config: bool "Enable -mindepth N and -maxdepth N" |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 116 | //config: default y |
| 117 | //config: depends on FIND |
| 118 | //config: help |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 119 | //config: This option enables -mindepth N and -maxdepth N option. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 120 | //config: |
| 121 | //config:config FEATURE_FIND_NEWER |
| 122 | //config: bool "Enable -newer: compare file modification times" |
| 123 | //config: default y |
| 124 | //config: depends on FIND |
| 125 | //config: help |
| 126 | //config: Support the 'find -newer' option for finding any files which have |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 127 | //config: modification time that is more recent than the specified FILE. |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 128 | //config: |
| 129 | //config:config FEATURE_FIND_INUM |
| 130 | //config: bool "Enable -inum: inode number matching" |
| 131 | //config: default y |
| 132 | //config: depends on FIND |
| 133 | //config: help |
| 134 | //config: Support the 'find -inum' option for searching by inode number. |
| 135 | //config: |
| 136 | //config:config FEATURE_FIND_EXEC |
| 137 | //config: bool "Enable -exec: execute commands" |
| 138 | //config: default y |
| 139 | //config: depends on FIND |
| 140 | //config: help |
| 141 | //config: Support the 'find -exec' option for executing commands based upon |
| 142 | //config: the files matched. |
| 143 | //config: |
| 144 | //config:config FEATURE_FIND_USER |
| 145 | //config: bool "Enable -user: username/uid matching" |
| 146 | //config: default y |
| 147 | //config: depends on FIND |
| 148 | //config: help |
| 149 | //config: Support the 'find -user' option for searching by username or uid. |
| 150 | //config: |
| 151 | //config:config FEATURE_FIND_GROUP |
| 152 | //config: bool "Enable -group: group/gid matching" |
| 153 | //config: default y |
| 154 | //config: depends on FIND |
| 155 | //config: help |
| 156 | //config: Support the 'find -group' option for searching by group name or gid. |
| 157 | //config: |
| 158 | //config:config FEATURE_FIND_NOT |
| 159 | //config: bool "Enable the 'not' (!) operator" |
| 160 | //config: default y |
| 161 | //config: depends on FIND |
| 162 | //config: help |
| 163 | //config: Support the '!' operator to invert the test results. |
| 164 | //config: If 'Enable full-blown desktop' is enabled, then will also support |
| 165 | //config: the non-POSIX notation '-not'. |
| 166 | //config: |
| 167 | //config:config FEATURE_FIND_DEPTH |
| 168 | //config: bool "Enable -depth" |
| 169 | //config: default y |
| 170 | //config: depends on FIND |
| 171 | //config: help |
| 172 | //config: Process each directory's contents before the directory itself. |
| 173 | //config: |
| 174 | //config:config FEATURE_FIND_PAREN |
| 175 | //config: bool "Enable parens in options" |
| 176 | //config: default y |
| 177 | //config: depends on FIND |
| 178 | //config: help |
| 179 | //config: Enable usage of parens '(' to specify logical order of arguments. |
| 180 | //config: |
| 181 | //config:config FEATURE_FIND_SIZE |
| 182 | //config: bool "Enable -size: file size matching" |
| 183 | //config: default y |
| 184 | //config: depends on FIND |
| 185 | //config: help |
| 186 | //config: Support the 'find -size' option for searching by file size. |
| 187 | //config: |
| 188 | //config:config FEATURE_FIND_PRUNE |
| 189 | //config: bool "Enable -prune: exclude subdirectories" |
| 190 | //config: default y |
| 191 | //config: depends on FIND |
| 192 | //config: help |
| 193 | //config: If the file is a directory, dont descend into it. Useful for |
| 194 | //config: exclusion .svn and CVS directories. |
| 195 | //config: |
| 196 | //config:config FEATURE_FIND_DELETE |
| 197 | //config: bool "Enable -delete: delete files/dirs" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 198 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 199 | //config: depends on FIND && FEATURE_FIND_DEPTH |
| 200 | //config: help |
| 201 | //config: Support the 'find -delete' option for deleting files and directories. |
| 202 | //config: WARNING: This option can do much harm if used wrong. Busybox will not |
| 203 | //config: try to protect the user from doing stupid things. Use with care. |
| 204 | //config: |
| 205 | //config:config FEATURE_FIND_PATH |
| 206 | //config: bool "Enable -path: match pathname with shell pattern" |
| 207 | //config: default y |
| 208 | //config: depends on FIND |
| 209 | //config: help |
| 210 | //config: The -path option matches whole pathname instead of just filename. |
| 211 | //config: |
| 212 | //config:config FEATURE_FIND_REGEX |
| 213 | //config: bool "Enable -regex: match pathname with regex" |
| 214 | //config: default y |
| 215 | //config: depends on FIND |
| 216 | //config: help |
| 217 | //config: The -regex option matches whole pathname against regular expression. |
| 218 | //config: |
| 219 | //config:config FEATURE_FIND_CONTEXT |
| 220 | //config: bool "Enable -context: security context matching" |
| 221 | //config: default n |
| 222 | //config: depends on FIND && SELINUX |
| 223 | //config: help |
| 224 | //config: Support the 'find -context' option for matching security context. |
| 225 | //config: |
| 226 | //config:config FEATURE_FIND_LINKS |
| 227 | //config: bool "Enable -links: link count matching" |
Denys Vlasenko | 2f32bf8 | 2010-06-06 04:14:28 +0200 | [diff] [blame] | 228 | //config: default y |
Denys Vlasenko | 7fb68f1 | 2010-05-09 04:22:48 +0200 | [diff] [blame] | 229 | //config: depends on FIND |
| 230 | //config: help |
| 231 | //config: Support the 'find -links' option for matching number of links. |
| 232 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 233 | #include <fnmatch.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 234 | #include "libbb.h" |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 235 | #if ENABLE_FEATURE_FIND_REGEX |
| 236 | #include "xregex.h" |
| 237 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 238 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 239 | /* This is a NOEXEC applet. Be very careful! */ |
| 240 | |
| 241 | |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 242 | 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] | 243 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 244 | typedef struct { |
| 245 | action_fp f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 246 | #if ENABLE_FEATURE_FIND_NOT |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 247 | bool invert; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 248 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 249 | } action; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 250 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 251 | #define ACTS(name, ...) typedef struct { action a; __VA_ARGS__ } action_##name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 252 | #define ACTF(name) \ |
| 253 | static int FAST_FUNC func_##name(const char *fileName UNUSED_PARAM, \ |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 254 | const struct stat *statbuf UNUSED_PARAM, \ |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 255 | action_##name* ap UNUSED_PARAM) |
| 256 | |
| 257 | ACTS(print) |
| 258 | ACTS(name, const char *pattern; bool iname;) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 259 | IF_FEATURE_FIND_PATH( ACTS(path, const char *pattern;)) |
| 260 | IF_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;)) |
| 261 | IF_FEATURE_FIND_PRINT0( ACTS(print0)) |
| 262 | IF_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) |
| 263 | IF_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;)) |
| 264 | IF_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;)) |
| 265 | IF_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;)) |
| 266 | IF_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) |
| 267 | IF_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) |
| 268 | IF_FEATURE_FIND_USER( ACTS(user, uid_t uid;)) |
| 269 | IF_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;)) |
| 270 | IF_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;)) |
| 271 | IF_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;)) |
| 272 | IF_FEATURE_FIND_PRUNE( ACTS(prune)) |
| 273 | IF_FEATURE_FIND_DELETE( ACTS(delete)) |
| 274 | IF_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;)) |
| 275 | IF_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;)) |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 276 | IF_FEATURE_FIND_LINKS( ACTS(links, char links_char; int links_count;)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 277 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 278 | struct globals { |
| 279 | IF_FEATURE_FIND_XDEV(dev_t *xdev_dev;) |
| 280 | IF_FEATURE_FIND_XDEV(int xdev_count;) |
| 281 | action ***actions; |
| 282 | bool need_print; |
| 283 | recurse_flags_t recurse_flags; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 284 | } FIX_ALIASING; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 285 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 286 | #define INIT_G() do { \ |
| 287 | struct G_sizecheck { \ |
| 288 | char G_sizecheck[sizeof(G) > COMMON_BUFSIZE ? -1 : 1]; \ |
| 289 | }; \ |
Denys Vlasenko | 1b34d4f | 2009-09-30 02:39:57 +0200 | [diff] [blame] | 290 | /* we have to zero it out because of NOEXEC */ \ |
| 291 | memset(&G, 0, offsetof(struct globals, need_print)); \ |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 292 | G.need_print = 1; \ |
| 293 | G.recurse_flags = ACTION_RECURSE; \ |
| 294 | } while (0) |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 295 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 296 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 297 | static unsigned count_subst(const char *str) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 298 | { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 299 | unsigned count = 0; |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 300 | while ((str = strstr(str, "{}")) != NULL) { |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 301 | count++; |
| 302 | str++; |
| 303 | } |
| 304 | return count; |
| 305 | } |
| 306 | |
| 307 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 308 | static char* subst(const char *src, unsigned count, const char* filename) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 309 | { |
| 310 | char *buf, *dst, *end; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 311 | size_t flen = strlen(filename); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 312 | /* we replace each '{}' with filename: growth by strlen-2 */ |
| 313 | buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); |
| 314 | while ((end = strstr(src, "{}"))) { |
| 315 | memcpy(dst, src, end - src); |
| 316 | dst += end - src; |
| 317 | src = end + 2; |
| 318 | memcpy(dst, filename, flen); |
| 319 | dst += flen; |
| 320 | } |
| 321 | strcpy(dst, src); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 322 | return buf; |
| 323 | } |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 324 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 325 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 326 | /* Return values of ACTFs ('action functions') are a bit mask: |
| 327 | * bit 1=1: prune (use SKIP constant for setting it) |
| 328 | * bit 0=1: matched successfully (TRUE) |
| 329 | */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 330 | |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 331 | 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] | 332 | { |
| 333 | int cur_group; |
| 334 | int cur_action; |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 335 | int rc = 0; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 336 | action **app, *ap; |
| 337 | |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 338 | /* "action group" is a set of actions ANDed together. |
| 339 | * groups are ORed together. |
| 340 | * We simply evaluate each group until we find one in which all actions |
| 341 | * succeed. */ |
| 342 | |
| 343 | /* -prune is special: if it is encountered, then we won't |
| 344 | * descend into current directory. It doesn't matter whether |
| 345 | * action group (in which -prune sits) will succeed or not: |
| 346 | * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir |
| 347 | * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs |
| 348 | * not starting with 'f' */ |
| 349 | |
| 350 | /* We invert TRUE bit (bit 0). Now 1 there means 'failure'. |
| 351 | * and bitwise OR in "rc |= TRUE ^ ap->f()" will: |
| 352 | * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'. |
| 353 | * On return, bit is restored. */ |
| 354 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 355 | cur_group = -1; |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 356 | while ((app = appp[++cur_group]) != NULL) { |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 357 | rc &= ~TRUE; /* 'success' so far, clear TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 358 | cur_action = -1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 359 | while (1) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 360 | ap = app[++cur_action]; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 361 | if (!ap) /* all actions in group were successful */ |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 362 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 363 | rc |= TRUE ^ ap->f(fileName, statbuf, ap); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 364 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 365 | if (ap->invert) rc ^= TRUE; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 366 | #endif |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 367 | if (rc & TRUE) /* current group failed, try next */ |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 368 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 369 | } |
| 370 | } |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 371 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 375 | ACTF(name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 376 | { |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 377 | const char *tmp = bb_basename(fileName); |
Denys Vlasenko | 9138817 | 2010-04-26 09:54:04 +0200 | [diff] [blame] | 378 | if (tmp != fileName && *tmp == '\0') { |
| 379 | /* "foo/bar/". Oh no... go back to 'b' */ |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 380 | tmp--; |
| 381 | while (tmp != fileName && *--tmp != '/') |
| 382 | continue; |
| 383 | if (*tmp == '/') |
| 384 | tmp++; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 385 | } |
Denys Vlasenko | 9138817 | 2010-04-26 09:54:04 +0200 | [diff] [blame] | 386 | /* Was using FNM_PERIOD flag too, |
| 387 | * but somewhere between 4.1.20 and 4.4.0 GNU find stopped using it. |
| 388 | * find -name '*foo' should match .foo too: |
| 389 | */ |
| 390 | return fnmatch(ap->pattern, tmp, (ap->iname ? FNM_CASEFOLD : 0)) == 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 391 | } |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 392 | |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 393 | #if ENABLE_FEATURE_FIND_PATH |
| 394 | ACTF(path) |
| 395 | { |
| 396 | return fnmatch(ap->pattern, fileName, 0) == 0; |
| 397 | } |
| 398 | #endif |
| 399 | #if ENABLE_FEATURE_FIND_REGEX |
| 400 | ACTF(regex) |
| 401 | { |
| 402 | regmatch_t match; |
| 403 | if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/)) |
| 404 | return 0; /* no match */ |
| 405 | if (match.rm_so) |
| 406 | return 0; /* match doesn't start at pos 0 */ |
| 407 | if (fileName[match.rm_eo]) |
| 408 | return 0; /* match doesn't end exactly at end of pathname */ |
| 409 | return 1; |
| 410 | } |
| 411 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 412 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 413 | ACTF(type) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 414 | { |
Denis Vlasenko | 31c65f2 | 2006-10-31 23:39:37 +0000 | [diff] [blame] | 415 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 416 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 417 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 418 | #if ENABLE_FEATURE_FIND_PERM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 419 | ACTF(perm) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 420 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 421 | /* -perm +mode: at least one of perm_mask bits are set */ |
| 422 | if (ap->perm_char == '+') |
| 423 | return (statbuf->st_mode & ap->perm_mask) != 0; |
| 424 | /* -perm -mode: all of perm_mask are set */ |
| 425 | if (ap->perm_char == '-') |
| 426 | return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask; |
| 427 | /* -perm mode: file mode must match perm_mask */ |
| 428 | return (statbuf->st_mode & 07777) == ap->perm_mask; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 429 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 430 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 431 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 432 | ACTF(mtime) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 433 | { |
| 434 | time_t file_age = time(NULL) - statbuf->st_mtime; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 435 | time_t mtime_secs = ap->mtime_days * 24*60*60; |
| 436 | if (ap->mtime_char == '+') |
| 437 | return file_age >= mtime_secs + 24*60*60; |
| 438 | if (ap->mtime_char == '-') |
| 439 | return file_age < mtime_secs; |
| 440 | /* just numeric mtime */ |
| 441 | return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 442 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 443 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 444 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 445 | ACTF(mmin) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 446 | { |
| 447 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 448 | time_t mmin_secs = ap->mmin_mins * 60; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 449 | if (ap->mmin_char == '+') |
| 450 | return file_age >= mmin_secs + 60; |
| 451 | if (ap->mmin_char == '-') |
| 452 | return file_age < mmin_secs; |
| 453 | /* just numeric mmin */ |
| 454 | return file_age >= mmin_secs && file_age < (mmin_secs + 60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 455 | } |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 456 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 457 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 458 | ACTF(newer) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 459 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 460 | return (ap->newer_mtime < statbuf->st_mtime); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 461 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 462 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 463 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 464 | ACTF(inum) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 465 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 466 | return (statbuf->st_ino == ap->inode_num); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 467 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 468 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 469 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 470 | ACTF(exec) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 471 | { |
| 472 | int i, rc; |
Denys Vlasenko | aa7da73 | 2009-09-13 14:57:24 +0200 | [diff] [blame] | 473 | #if ENABLE_USE_PORTABLE_CODE |
| 474 | char **argv = alloca(sizeof(char*) * (ap->exec_argc + 1)); |
| 475 | #else /* gcc 4.3.1 generates smaller code: */ |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 476 | char *argv[ap->exec_argc + 1]; |
Denys Vlasenko | aa7da73 | 2009-09-13 14:57:24 +0200 | [diff] [blame] | 477 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 478 | for (i = 0; i < ap->exec_argc; i++) |
| 479 | argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); |
| 480 | argv[i] = NULL; /* terminate the list */ |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 481 | |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 482 | rc = spawn_and_wait(argv); |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 483 | if (rc < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 484 | bb_simple_perror_msg(argv[0]); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 485 | |
| 486 | i = 0; |
| 487 | while (argv[i]) |
| 488 | free(argv[i++]); |
| 489 | return rc == 0; /* return 1 if exitcode 0 */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 490 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 491 | #endif |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 492 | #if ENABLE_FEATURE_FIND_USER |
| 493 | ACTF(user) |
| 494 | { |
| 495 | return (statbuf->st_uid == ap->uid); |
| 496 | } |
| 497 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 498 | #if ENABLE_FEATURE_FIND_GROUP |
| 499 | ACTF(group) |
| 500 | { |
| 501 | return (statbuf->st_gid == ap->gid); |
| 502 | } |
| 503 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 504 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 505 | ACTF(print0) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 506 | { |
| 507 | printf("%s%c", fileName, '\0'); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 508 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 509 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 510 | #endif |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 511 | ACTF(print) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 512 | { |
| 513 | puts(fileName); |
| 514 | return TRUE; |
| 515 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 516 | #if ENABLE_FEATURE_FIND_PAREN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 517 | ACTF(paren) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 518 | { |
| 519 | return exec_actions(ap->subexpr, fileName, statbuf); |
| 520 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 521 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 522 | #if ENABLE_FEATURE_FIND_SIZE |
| 523 | ACTF(size) |
| 524 | { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 525 | if (ap->size_char == '+') |
| 526 | return statbuf->st_size > ap->size; |
| 527 | if (ap->size_char == '-') |
| 528 | return statbuf->st_size < ap->size; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 529 | return statbuf->st_size == ap->size; |
| 530 | } |
| 531 | #endif |
Bernhard Reutner-Fischer | 6d79dd6 | 2007-04-05 09:40:12 +0000 | [diff] [blame] | 532 | #if ENABLE_FEATURE_FIND_PRUNE |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 533 | /* |
| 534 | * -prune: if -depth is not given, return true and do not descend |
| 535 | * current dir; if -depth is given, return false with no effect. |
| 536 | * Example: |
| 537 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print |
| 538 | */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 539 | ACTF(prune) |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 540 | { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 541 | return SKIP + TRUE; |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 542 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 543 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 544 | #if ENABLE_FEATURE_FIND_DELETE |
| 545 | ACTF(delete) |
| 546 | { |
| 547 | int rc; |
| 548 | if (S_ISDIR(statbuf->st_mode)) { |
| 549 | rc = rmdir(fileName); |
| 550 | } else { |
| 551 | rc = unlink(fileName); |
| 552 | } |
| 553 | if (rc < 0) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 554 | bb_simple_perror_msg(fileName); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 555 | return TRUE; |
| 556 | } |
| 557 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 558 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 559 | ACTF(context) |
| 560 | { |
| 561 | security_context_t con; |
| 562 | int rc; |
| 563 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 564 | if (G.recurse_flags & ACTION_FOLLOWLINKS) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 565 | rc = getfilecon(fileName, &con); |
| 566 | } else { |
| 567 | rc = lgetfilecon(fileName, &con); |
| 568 | } |
| 569 | if (rc < 0) |
| 570 | return FALSE; |
| 571 | rc = strcmp(ap->context, con); |
| 572 | freecon(con); |
| 573 | return rc == 0; |
| 574 | } |
| 575 | #endif |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 576 | #if ENABLE_FEATURE_FIND_LINKS |
| 577 | ACTF(links) |
| 578 | { |
| 579 | switch(ap->links_char) { |
| 580 | case '-' : return (statbuf->st_nlink < ap->links_count); |
| 581 | case '+' : return (statbuf->st_nlink > ap->links_count); |
| 582 | default: return (statbuf->st_nlink == ap->links_count); |
| 583 | } |
| 584 | } |
| 585 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 586 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 587 | static int FAST_FUNC fileAction(const char *fileName, |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 588 | struct stat *statbuf, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 589 | void *userData IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM), |
| 590 | int depth IF_NOT_FEATURE_FIND_MAXDEPTH(UNUSED_PARAM)) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 591 | { |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 592 | int r; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 593 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 594 | #define minmaxdepth ((int*)userData) |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 595 | |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 596 | if (depth < minmaxdepth[0]) |
| 597 | return TRUE; /* skip this, continue recursing */ |
| 598 | if (depth > minmaxdepth[1]) |
| 599 | return SKIP; /* stop recursing */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 600 | #endif |
| 601 | |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 602 | r = exec_actions(G.actions, fileName, statbuf); |
| 603 | /* Had no explicit -print[0] or -exec? then print */ |
| 604 | if ((r & TRUE) && G.need_print) |
| 605 | puts(fileName); |
| 606 | |
| 607 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 608 | if (S_ISDIR(statbuf->st_mode)) { |
| 609 | if (depth == minmaxdepth[1]) |
| 610 | return SKIP; |
| 611 | } |
| 612 | #endif |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 613 | #if ENABLE_FEATURE_FIND_XDEV |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 614 | /* -xdev stops on mountpoints, but AFTER mountpoit itself |
| 615 | * is processed as usual */ |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 616 | if (S_ISDIR(statbuf->st_mode)) { |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 617 | if (G.xdev_count) { |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 618 | int i; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 619 | for (i = 0; i < G.xdev_count; i++) { |
| 620 | if (G.xdev_dev[i] == statbuf->st_dev) |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 621 | goto found; |
| 622 | } |
Paul Fox | dcf9bba | 2007-09-06 17:45:16 +0000 | [diff] [blame] | 623 | return SKIP; |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 624 | found: ; |
| 625 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 626 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 627 | #endif |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 628 | |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 629 | /* Cannot return 0: our caller, recursive_action(), |
| 630 | * will perror() and skip dirs (if called on dir) */ |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 631 | return (r & SKIP) ? SKIP : TRUE; |
Denys Vlasenko | 6c750f1 | 2009-09-27 01:51:47 +0200 | [diff] [blame] | 632 | #undef minmaxdepth |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 635 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 636 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 637 | static int find_type(const char *type) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 638 | { |
| 639 | int mask = 0; |
| 640 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 641 | if (*type == 'b') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 642 | mask = S_IFBLK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 643 | else if (*type == 'c') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 644 | mask = S_IFCHR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 645 | else if (*type == 'd') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 646 | mask = S_IFDIR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 647 | else if (*type == 'p') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 648 | mask = S_IFIFO; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 649 | else if (*type == 'f') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 650 | mask = S_IFREG; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 651 | else if (*type == 'l') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 652 | mask = S_IFLNK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 653 | else if (*type == 's') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 654 | mask = S_IFSOCK; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 655 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 656 | if (mask == 0 || type[1] != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 657 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 658 | |
| 659 | return mask; |
| 660 | } |
| 661 | #endif |
| 662 | |
Denis Vlasenko | 4b9b0ce | 2007-07-01 17:35:17 +0000 | [diff] [blame] | 663 | #if ENABLE_FEATURE_FIND_PERM \ |
| 664 | || ENABLE_FEATURE_FIND_MTIME || ENABLE_FEATURE_FIND_MMIN \ |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 665 | || ENABLE_FEATURE_FIND_SIZE || ENABLE_FEATURE_FIND_LINKS |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 666 | static const char* plus_minus_num(const char* str) |
| 667 | { |
| 668 | if (*str == '-' || *str == '+') |
| 669 | str++; |
| 670 | return str; |
| 671 | } |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 672 | #endif |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 673 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 674 | static action*** parse_params(char **argv) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 675 | { |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 676 | enum { |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 677 | PARM_a , |
| 678 | PARM_o , |
| 679 | IF_FEATURE_FIND_NOT( PARM_char_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 680 | #if ENABLE_DESKTOP |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 681 | PARM_and , |
| 682 | PARM_or , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 683 | IF_FEATURE_FIND_NOT( PARM_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 684 | #endif |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 685 | PARM_print , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 686 | IF_FEATURE_FIND_PRINT0( PARM_print0 ,) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 687 | IF_FEATURE_FIND_PRUNE( PARM_prune ,) |
| 688 | IF_FEATURE_FIND_DELETE( PARM_delete ,) |
| 689 | IF_FEATURE_FIND_EXEC( PARM_exec ,) |
| 690 | IF_FEATURE_FIND_PAREN( PARM_char_brace,) |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 691 | /* All options starting from here require argument */ |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 692 | PARM_name , |
| 693 | PARM_iname , |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 694 | IF_FEATURE_FIND_PATH( PARM_path ,) |
| 695 | IF_FEATURE_FIND_REGEX( PARM_regex ,) |
| 696 | IF_FEATURE_FIND_TYPE( PARM_type ,) |
| 697 | IF_FEATURE_FIND_PERM( PARM_perm ,) |
| 698 | IF_FEATURE_FIND_MTIME( PARM_mtime ,) |
| 699 | IF_FEATURE_FIND_MMIN( PARM_mmin ,) |
| 700 | IF_FEATURE_FIND_NEWER( PARM_newer ,) |
| 701 | IF_FEATURE_FIND_INUM( PARM_inum ,) |
| 702 | IF_FEATURE_FIND_USER( PARM_user ,) |
| 703 | IF_FEATURE_FIND_GROUP( PARM_group ,) |
| 704 | IF_FEATURE_FIND_SIZE( PARM_size ,) |
| 705 | IF_FEATURE_FIND_CONTEXT(PARM_context ,) |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 706 | IF_FEATURE_FIND_LINKS( PARM_links ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 707 | }; |
| 708 | |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 709 | static const char params[] ALIGN1 = |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 710 | "-a\0" |
| 711 | "-o\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 712 | IF_FEATURE_FIND_NOT( "!\0" ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 713 | #if ENABLE_DESKTOP |
Denis Vlasenko | 319f8eb | 2007-08-13 11:09:30 +0000 | [diff] [blame] | 714 | "-and\0" |
| 715 | "-or\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 716 | IF_FEATURE_FIND_NOT( "-not\0" ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 717 | #endif |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 718 | "-print\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 719 | IF_FEATURE_FIND_PRINT0( "-print0\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 720 | IF_FEATURE_FIND_PRUNE( "-prune\0" ) |
| 721 | IF_FEATURE_FIND_DELETE( "-delete\0" ) |
| 722 | IF_FEATURE_FIND_EXEC( "-exec\0" ) |
| 723 | IF_FEATURE_FIND_PAREN( "(\0" ) |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 724 | /* All options starting from here require argument */ |
Denis Vlasenko | 319f8eb | 2007-08-13 11:09:30 +0000 | [diff] [blame] | 725 | "-name\0" |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 726 | "-iname\0" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 727 | IF_FEATURE_FIND_PATH( "-path\0" ) |
| 728 | IF_FEATURE_FIND_REGEX( "-regex\0" ) |
| 729 | IF_FEATURE_FIND_TYPE( "-type\0" ) |
| 730 | IF_FEATURE_FIND_PERM( "-perm\0" ) |
| 731 | IF_FEATURE_FIND_MTIME( "-mtime\0" ) |
| 732 | IF_FEATURE_FIND_MMIN( "-mmin\0" ) |
| 733 | IF_FEATURE_FIND_NEWER( "-newer\0" ) |
| 734 | IF_FEATURE_FIND_INUM( "-inum\0" ) |
| 735 | IF_FEATURE_FIND_USER( "-user\0" ) |
| 736 | IF_FEATURE_FIND_GROUP( "-group\0" ) |
| 737 | IF_FEATURE_FIND_SIZE( "-size\0" ) |
| 738 | IF_FEATURE_FIND_CONTEXT("-context\0") |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 739 | IF_FEATURE_FIND_LINKS( "-links\0" ) |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 740 | ; |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 741 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 742 | action*** appp; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 743 | unsigned cur_group = 0; |
| 744 | unsigned cur_action = 0; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 745 | IF_FEATURE_FIND_NOT( bool invert_flag = 0; ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 746 | |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 747 | /* This is the only place in busybox where we use nested function. |
| 748 | * So far more standard alternatives were bigger. */ |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 749 | /* Auto decl suppresses "func without a prototype" warning: */ |
Denis Vlasenko | 0f3a580 | 2008-03-20 13:13:09 +0000 | [diff] [blame] | 750 | auto action* alloc_action(int sizeof_struct, action_fp f); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 751 | action* alloc_action(int sizeof_struct, action_fp f) |
| 752 | { |
| 753 | action *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 754 | appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp)); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 755 | appp[cur_group][cur_action++] = ap = xzalloc(sizeof_struct); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 756 | appp[cur_group][cur_action] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 757 | ap->f = f; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 758 | IF_FEATURE_FIND_NOT( ap->invert = invert_flag; ) |
| 759 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 760 | return ap; |
| 761 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 762 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 763 | #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 764 | |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 765 | appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 766 | |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 767 | /* Actions have side effects and return a true or false value |
| 768 | * We implement: -print, -print0, -exec |
| 769 | * |
| 770 | * The rest are tests. |
| 771 | * |
| 772 | * Tests and actions are grouped by operators |
| 773 | * ( expr ) Force precedence |
| 774 | * ! expr True if expr is false |
| 775 | * -not expr Same as ! expr |
| 776 | * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false |
| 777 | * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true |
| 778 | * expr1 , expr2 List; both expr1 and expr2 are always evaluated |
| 779 | * We implement: (), -a, -o |
| 780 | */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 781 | while (*argv) { |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 782 | const char *arg = argv[0]; |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 783 | int parm = index_in_strings(params, arg); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 784 | const char *arg1 = argv[1]; |
| 785 | |
| 786 | if (parm >= PARM_name) { |
| 787 | /* All options starting from -name require argument */ |
| 788 | if (!arg1) |
| 789 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 790 | argv++; |
| 791 | } |
| 792 | |
| 793 | /* We can use big switch() here, but on i386 |
| 794 | * it doesn't give smaller code. Other arches? */ |
| 795 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 796 | /* --- Operators --- */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 797 | if (parm == PARM_a IF_DESKTOP(|| parm == PARM_and)) { |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 798 | /* no further special handling required */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 799 | } |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 800 | else if (parm == PARM_o IF_DESKTOP(|| parm == PARM_or)) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 801 | /* start new OR group */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 802 | cur_group++; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 803 | appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 804 | /*appp[cur_group] = NULL; - already NULL */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 805 | appp[cur_group+1] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 806 | cur_action = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 807 | } |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 808 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 809 | else if (parm == PARM_char_not IF_DESKTOP(|| parm == PARM_not)) { |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 810 | /* also handles "find ! ! -name 'foo*'" */ |
| 811 | invert_flag ^= 1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 812 | } |
| 813 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 814 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 815 | /* --- Tests and actions --- */ |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 816 | else if (parm == PARM_print) { |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 817 | G.need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 818 | /* GNU find ignores '!' here: "find ! -print" */ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 819 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 820 | (void) ALLOC_ACTION(print); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 821 | } |
| 822 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 823 | else if (parm == PARM_print0) { |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 824 | G.need_print = 0; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 825 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 826 | (void) ALLOC_ACTION(print0); |
| 827 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 828 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 829 | #if ENABLE_FEATURE_FIND_PRUNE |
| 830 | else if (parm == PARM_prune) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 831 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 832 | (void) ALLOC_ACTION(prune); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 833 | } |
| 834 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 835 | #if ENABLE_FEATURE_FIND_DELETE |
| 836 | else if (parm == PARM_delete) { |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 837 | G.need_print = 0; |
| 838 | G.recurse_flags |= ACTION_DEPTHFIRST; |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 839 | (void) ALLOC_ACTION(delete); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 840 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 841 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 842 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 843 | else if (parm == PARM_exec) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 844 | int i; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 845 | action_exec *ap; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 846 | G.need_print = 0; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 847 | IF_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 848 | ap = ALLOC_ACTION(exec); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 849 | ap->exec_argv = ++argv; /* first arg after -exec */ |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 850 | /*ap->exec_argc = 0; - ALLOC_ACTION did it */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 851 | while (1) { |
Bernhard Reutner-Fischer | 2e73d84 | 2010-02-25 13:22:37 +0100 | [diff] [blame] | 852 | if (!*argv) /* did not see ';' or '+' until end */ |
| 853 | bb_error_msg_and_die(bb_msg_requires_arg, "-exec"); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 854 | // find -exec echo Foo ">{}<" ";" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 855 | // executes "echo Foo >FILENAME<", |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 856 | // find -exec echo Foo ">{}<" "+" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 857 | // executes "echo Foo FILENAME1 FILENAME2 FILENAME3...". |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 858 | // TODO (so far we treat "+" just like ";") |
| 859 | if ((argv[0][0] == ';' || argv[0][0] == '+') |
| 860 | && argv[0][1] == '\0' |
| 861 | ) { |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 862 | break; |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 863 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 864 | argv++; |
| 865 | ap->exec_argc++; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 866 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 867 | if (ap->exec_argc == 0) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 868 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 869 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 870 | i = ap->exec_argc; |
| 871 | while (i--) |
| 872 | ap->subst_count[i] = count_subst(ap->exec_argv[i]); |
| 873 | } |
| 874 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 875 | #if ENABLE_FEATURE_FIND_PAREN |
| 876 | else if (parm == PARM_char_brace) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 877 | action_paren *ap; |
| 878 | char **endarg; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 879 | unsigned nested = 1; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 880 | |
| 881 | endarg = argv; |
| 882 | while (1) { |
| 883 | if (!*++endarg) |
| 884 | bb_error_msg_and_die("unpaired '('"); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 885 | if (LONE_CHAR(*endarg, '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 886 | nested++; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 887 | else if (LONE_CHAR(*endarg, ')') && !--nested) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 888 | *endarg = NULL; |
| 889 | break; |
| 890 | } |
| 891 | } |
| 892 | ap = ALLOC_ACTION(paren); |
| 893 | ap->subexpr = parse_params(argv + 1); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 894 | *endarg = (char*) ")"; /* restore NULLed parameter */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 895 | argv = endarg; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 896 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 897 | #endif |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 898 | else if (parm == PARM_name || parm == PARM_iname) { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 899 | action_name *ap; |
| 900 | ap = ALLOC_ACTION(name); |
| 901 | ap->pattern = arg1; |
Denis Vlasenko | 75aa615 | 2007-12-09 10:07:39 +0000 | [diff] [blame] | 902 | ap->iname = (parm == PARM_iname); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 903 | } |
| 904 | #if ENABLE_FEATURE_FIND_PATH |
| 905 | else if (parm == PARM_path) { |
| 906 | action_path *ap; |
| 907 | ap = ALLOC_ACTION(path); |
| 908 | ap->pattern = arg1; |
| 909 | } |
| 910 | #endif |
| 911 | #if ENABLE_FEATURE_FIND_REGEX |
| 912 | else if (parm == PARM_regex) { |
| 913 | action_regex *ap; |
| 914 | ap = ALLOC_ACTION(regex); |
| 915 | xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/); |
| 916 | } |
| 917 | #endif |
| 918 | #if ENABLE_FEATURE_FIND_TYPE |
| 919 | else if (parm == PARM_type) { |
| 920 | action_type *ap; |
| 921 | ap = ALLOC_ACTION(type); |
| 922 | ap->type_mask = find_type(arg1); |
| 923 | } |
| 924 | #endif |
| 925 | #if ENABLE_FEATURE_FIND_PERM |
| 926 | /* -perm mode File's permission bits are exactly mode (octal or symbolic). |
| 927 | * Symbolic modes use mode 0 as a point of departure. |
| 928 | * -perm -mode All of the permission bits mode are set for the file. |
| 929 | * -perm +mode Any of the permission bits mode are set for the file. |
| 930 | */ |
| 931 | else if (parm == PARM_perm) { |
| 932 | action_perm *ap; |
| 933 | ap = ALLOC_ACTION(perm); |
| 934 | ap->perm_char = arg1[0]; |
| 935 | arg1 = plus_minus_num(arg1); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 936 | /*ap->perm_mask = 0; - ALLOC_ACTION did it */ |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 937 | if (!bb_parse_mode(arg1, &ap->perm_mask)) |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 938 | bb_error_msg_and_die("invalid mode '%s'", arg1); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 939 | } |
| 940 | #endif |
| 941 | #if ENABLE_FEATURE_FIND_MTIME |
| 942 | else if (parm == PARM_mtime) { |
| 943 | action_mtime *ap; |
| 944 | ap = ALLOC_ACTION(mtime); |
| 945 | ap->mtime_char = arg1[0]; |
| 946 | ap->mtime_days = xatoul(plus_minus_num(arg1)); |
| 947 | } |
| 948 | #endif |
| 949 | #if ENABLE_FEATURE_FIND_MMIN |
| 950 | else if (parm == PARM_mmin) { |
| 951 | action_mmin *ap; |
| 952 | ap = ALLOC_ACTION(mmin); |
| 953 | ap->mmin_char = arg1[0]; |
| 954 | ap->mmin_mins = xatoul(plus_minus_num(arg1)); |
| 955 | } |
| 956 | #endif |
| 957 | #if ENABLE_FEATURE_FIND_NEWER |
| 958 | else if (parm == PARM_newer) { |
| 959 | struct stat stat_newer; |
| 960 | action_newer *ap; |
| 961 | ap = ALLOC_ACTION(newer); |
| 962 | xstat(arg1, &stat_newer); |
| 963 | ap->newer_mtime = stat_newer.st_mtime; |
| 964 | } |
| 965 | #endif |
| 966 | #if ENABLE_FEATURE_FIND_INUM |
| 967 | else if (parm == PARM_inum) { |
| 968 | action_inum *ap; |
| 969 | ap = ALLOC_ACTION(inum); |
| 970 | ap->inode_num = xatoul(arg1); |
| 971 | } |
| 972 | #endif |
| 973 | #if ENABLE_FEATURE_FIND_USER |
| 974 | else if (parm == PARM_user) { |
| 975 | action_user *ap; |
| 976 | ap = ALLOC_ACTION(user); |
| 977 | ap->uid = bb_strtou(arg1, NULL, 10); |
| 978 | if (errno) |
| 979 | ap->uid = xuname2uid(arg1); |
| 980 | } |
| 981 | #endif |
| 982 | #if ENABLE_FEATURE_FIND_GROUP |
| 983 | else if (parm == PARM_group) { |
| 984 | action_group *ap; |
| 985 | ap = ALLOC_ACTION(group); |
| 986 | ap->gid = bb_strtou(arg1, NULL, 10); |
| 987 | if (errno) |
| 988 | ap->gid = xgroup2gid(arg1); |
| 989 | } |
| 990 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 991 | #if ENABLE_FEATURE_FIND_SIZE |
| 992 | else if (parm == PARM_size) { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 993 | /* -size n[bckw]: file uses n units of space |
| 994 | * b (default): units are 512-byte blocks |
| 995 | * c: 1 byte |
| 996 | * k: kilobytes |
| 997 | * w: 2-byte words |
| 998 | */ |
| 999 | #if ENABLE_LFS |
| 1000 | #define XATOU_SFX xatoull_sfx |
| 1001 | #else |
| 1002 | #define XATOU_SFX xatoul_sfx |
| 1003 | #endif |
| 1004 | static const struct suffix_mult find_suffixes[] = { |
Denis Vlasenko | 99fcc62 | 2007-07-01 17:29:44 +0000 | [diff] [blame] | 1005 | { "c", 1 }, |
| 1006 | { "w", 2 }, |
Denis Vlasenko | f868963 | 2007-07-27 15:06:25 +0000 | [diff] [blame] | 1007 | { "", 512 }, |
Denis Vlasenko | 99fcc62 | 2007-07-01 17:29:44 +0000 | [diff] [blame] | 1008 | { "b", 512 }, |
| 1009 | { "k", 1024 }, |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 1010 | { "", 0 } |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1011 | }; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1012 | action_size *ap; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1013 | ap = ALLOC_ACTION(size); |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 1014 | ap->size_char = arg1[0]; |
| 1015 | ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1016 | } |
| 1017 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame] | 1018 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 1019 | else if (parm == PARM_context) { |
| 1020 | action_context *ap; |
| 1021 | ap = ALLOC_ACTION(context); |
Denys Vlasenko | a61ff51 | 2010-08-31 19:17:42 +0200 | [diff] [blame] | 1022 | /*ap->context = NULL; - ALLOC_ACTION did it */ |
Denis Vlasenko | 838daca | 2007-08-16 10:41:24 +0000 | [diff] [blame] | 1023 | /* SELinux headers erroneously declare non-const parameter */ |
| 1024 | if (selinux_raw_to_trans_context((char*)arg1, &ap->context)) |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 1025 | bb_simple_perror_msg(arg1); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 1026 | } |
| 1027 | #endif |
Matheus Izvekov | d0f601f | 2010-01-06 09:19:04 +0100 | [diff] [blame] | 1028 | #if ENABLE_FEATURE_FIND_LINKS |
| 1029 | else if (parm == PARM_links) { |
| 1030 | action_links *ap; |
| 1031 | ap = ALLOC_ACTION(links); |
| 1032 | ap->links_char = arg1[0]; |
| 1033 | ap->links_count = xatoul(plus_minus_num(arg1)); |
| 1034 | } |
| 1035 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1036 | else { |
| 1037 | bb_error_msg("unrecognized: %s", arg); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1038 | bb_show_usage(); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1039 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1040 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1041 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1042 | return appp; |
| 1043 | #undef ALLOC_ACTION |
| 1044 | } |
| 1045 | |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1046 | //usage:#define find_trivial_usage |
Denys Vlasenko | 4604dd8 | 2011-02-12 18:24:15 +0100 | [diff] [blame] | 1047 | //usage: "[PATH]... [OPTIONS] [ACTIONS]" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1048 | //usage:#define find_full_usage "\n\n" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1049 | //usage: "Search for files and perform actions on them.\n" |
| 1050 | //usage: "First failed action stops processing of current file.\n" |
| 1051 | //usage: "Defaults: PATH is current directory, action is '-print'\n" |
| 1052 | //usage: "\nOptions:" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1053 | //usage: "\n -follow Follow symlinks" |
| 1054 | //usage: IF_FEATURE_FIND_XDEV( |
| 1055 | //usage: "\n -xdev Don't descend directories on other filesystems" |
| 1056 | //usage: ) |
| 1057 | //usage: IF_FEATURE_FIND_MAXDEPTH( |
| 1058 | //usage: "\n -maxdepth N Descend at most N levels. -maxdepth 0 applies" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1059 | //usage: "\n actions to command line arguments only" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1060 | //usage: "\n -mindepth N Don't act on first N levels" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1061 | //usage: ) |
| 1062 | //usage: IF_FEATURE_FIND_DEPTH( |
| 1063 | //usage: "\n -depth Act on directory *after* traversing it" |
| 1064 | //usage: ) |
| 1065 | //usage: "\n" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1066 | //usage: "\nActions:" |
Denys Vlasenko | 56cdb2f | 2011-02-12 17:13:03 +0100 | [diff] [blame] | 1067 | //usage: IF_FEATURE_FIND_PAREN( |
| 1068 | //usage: "\n ( ACTIONS ) Group actions for -o / -a" |
| 1069 | //usage: ) |
Denys Vlasenko | 27076b8 | 2011-02-12 18:49:48 +0100 | [diff] [blame^] | 1070 | //usage: IF_FEATURE_FIND_NOT( |
| 1071 | //usage: "\n ! ACT Invert ACT's success/failure" |
| 1072 | //usage: ) |
Denys Vlasenko | 56cdb2f | 2011-02-12 17:13:03 +0100 | [diff] [blame] | 1073 | //usage: "\n ACT1 [-a] ACT2 If ACT1 fails, stop, else do ACT2" |
| 1074 | //usage: "\n ACT1 -o ACT2 If ACT1 succeeds, stop, else do ACT2" |
| 1075 | //usage: "\n Note: -a has higher priority than -o" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1076 | //usage: "\n -name PATTERN Match file name (w/o directory name) to PATTERN" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1077 | //usage: "\n -iname PATTERN Case insensitive -name" |
| 1078 | //usage: IF_FEATURE_FIND_PATH( |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1079 | //usage: "\n -path PATTERN Match path to PATTERN" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1080 | //usage: ) |
| 1081 | //usage: IF_FEATURE_FIND_REGEX( |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1082 | //usage: "\n -regex PATTERN Match path to regex PATTERN" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1083 | //usage: ) |
| 1084 | //usage: IF_FEATURE_FIND_TYPE( |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1085 | //usage: "\n -type X File type is X (one of: f,d,l,b,c,...)" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1086 | //usage: ) |
| 1087 | //usage: IF_FEATURE_FIND_PERM( |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1088 | //usage: "\n -perm MASK At least one mask bit (+MASK), all bits (-MASK)," |
| 1089 | //usage: "\n or exactly MASK bits are set in mode bits" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1090 | //usage: ) |
| 1091 | //usage: IF_FEATURE_FIND_MTIME( |
| 1092 | //usage: "\n -mtime DAYS Modified time is greater than (+N), less than (-N)," |
| 1093 | //usage: "\n or exactly N days" |
| 1094 | //usage: ) |
| 1095 | //usage: IF_FEATURE_FIND_MMIN( |
| 1096 | //usage: "\n -mmin MINS Modified time is greater than (+N), less than (-N)," |
| 1097 | //usage: "\n or exactly N minutes" |
| 1098 | //usage: ) |
| 1099 | //usage: IF_FEATURE_FIND_NEWER( |
| 1100 | //usage: "\n -newer FILE Modified time is more recent than FILE's" |
| 1101 | //usage: ) |
| 1102 | //usage: IF_FEATURE_FIND_INUM( |
| 1103 | //usage: "\n -inum N File has inode number N" |
| 1104 | //usage: ) |
| 1105 | //usage: IF_FEATURE_FIND_USER( |
| 1106 | //usage: "\n -user NAME File is owned by user NAME (numeric user ID allowed)" |
| 1107 | //usage: ) |
| 1108 | //usage: IF_FEATURE_FIND_GROUP( |
| 1109 | //usage: "\n -group NAME File belongs to group NAME (numeric group ID allowed)" |
| 1110 | //usage: ) |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1111 | //usage: IF_FEATURE_FIND_SIZE( |
| 1112 | //usage: "\n -size N[bck] File size is N (c:bytes,k:kbytes,b:512 bytes(def.))" |
| 1113 | //usage: "\n +/-N: file size is bigger/smaller than N" |
| 1114 | //usage: ) |
| 1115 | //usage: IF_FEATURE_FIND_LINKS( |
| 1116 | //usage: "\n -links N Number of links is greater than (+N), less than (-N)," |
| 1117 | //usage: "\n or exactly N" |
| 1118 | //usage: ) |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1119 | //usage: IF_FEATURE_FIND_CONTEXT( |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1120 | //usage: "\n -context CTX File has specified security context" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1121 | //usage: ) |
Denys Vlasenko | 4dea9eb | 2011-02-12 17:08:16 +0100 | [diff] [blame] | 1122 | //usage: IF_FEATURE_FIND_PRUNE( |
| 1123 | //usage: "\n -prune If current file is directory, don't descend into it" |
| 1124 | //usage: ) |
Denys Vlasenko | 56cdb2f | 2011-02-12 17:13:03 +0100 | [diff] [blame] | 1125 | //usage: "\nIf none of the following actions is specified, -print is assumed" |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1126 | //usage: "\n -print Print file name" |
| 1127 | //usage: IF_FEATURE_FIND_PRINT0( |
| 1128 | //usage: "\n -print0 Print file name, NUL terminated" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1129 | //usage: ) |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1130 | //usage: IF_FEATURE_FIND_EXEC( |
| 1131 | //usage: "\n -exec CMD ARG ; Run CMD with all instances of {} replaced by" |
| 1132 | //usage: "\n file name. Fails if CMD exits with nonzero" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1133 | //usage: ) |
| 1134 | //usage: IF_FEATURE_FIND_DELETE( |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1135 | //usage: "\n -delete Delete current file/directory. Turns on -depth option" |
Denys Vlasenko | 4f731ce | 2010-06-15 15:40:16 +0200 | [diff] [blame] | 1136 | //usage: ) |
| 1137 | //usage: |
| 1138 | //usage:#define find_example_usage |
| 1139 | //usage: "$ find / -name passwd\n" |
| 1140 | //usage: "/etc/passwd\n" |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1141 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1142 | int find_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1143 | int find_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1144 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 1145 | static const char options[] ALIGN1 = |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1146 | "-follow\0" |
| 1147 | IF_FEATURE_FIND_XDEV( "-xdev\0" ) |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1148 | IF_FEATURE_FIND_MAXDEPTH("-mindepth\0""-maxdepth\0") |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1149 | IF_FEATURE_FIND_DEPTH( "-depth\0" ) |
| 1150 | ; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1151 | enum { |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1152 | OPT_FOLLOW, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1153 | IF_FEATURE_FIND_XDEV( OPT_XDEV ,) |
| 1154 | IF_FEATURE_FIND_MAXDEPTH(OPT_MINDEPTH,) |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1155 | IF_FEATURE_FIND_DEPTH( OPT_DEPTH ,) |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1156 | }; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1157 | |
Denis Vlasenko | 7d6c929 | 2007-04-05 00:35:43 +0000 | [diff] [blame] | 1158 | char *arg; |
| 1159 | char **argp; |
| 1160 | int i, firstopt, status = EXIT_SUCCESS; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1161 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 1162 | int minmaxdepth[2] = { 0, INT_MAX }; |
| 1163 | #else |
| 1164 | #define minmaxdepth NULL |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1165 | #endif |
Denis Vlasenko | 7d6c929 | 2007-04-05 00:35:43 +0000 | [diff] [blame] | 1166 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1167 | INIT_G(); |
| 1168 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1169 | for (firstopt = 1; argv[firstopt]; firstopt++) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1170 | if (argv[firstopt][0] == '-') |
| 1171 | break; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 1172 | if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!')) |
| 1173 | break; |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 1174 | #if ENABLE_FEATURE_FIND_PAREN |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 1175 | if (LONE_CHAR(argv[firstopt], '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1176 | break; |
| 1177 | #endif |
| 1178 | } |
| 1179 | if (firstopt == 1) { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1180 | argv[0] = (char*)"."; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1181 | argv--; |
| 1182 | firstopt++; |
| 1183 | } |
| 1184 | |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 1185 | /* All options always return true. They always take effect |
| 1186 | * rather than being processed only when their place in the |
| 1187 | * expression is reached. |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1188 | * We implement: -follow, -xdev, -mindepth, -maxdepth, -depth |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 1189 | */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1190 | /* Process options, and replace then with -a */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 1191 | /* (-a will be ignored by recursive parser later) */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1192 | argp = &argv[firstopt]; |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1193 | while ((arg = argp[0]) != NULL) { |
Denis Vlasenko | 990d0f6 | 2007-07-24 15:54:42 +0000 | [diff] [blame] | 1194 | int opt = index_in_strings(options, arg); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1195 | if (opt == OPT_FOLLOW) { |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1196 | G.recurse_flags |= ACTION_FOLLOWLINKS | ACTION_DANGLING_OK; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1197 | argp[0] = (char*)"-a"; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1198 | } |
| 1199 | #if ENABLE_FEATURE_FIND_XDEV |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1200 | if (opt == OPT_XDEV) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1201 | struct stat stbuf; |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1202 | if (!G.xdev_count) { |
| 1203 | G.xdev_count = firstopt - 1; |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1204 | G.xdev_dev = xzalloc(G.xdev_count * sizeof(G.xdev_dev[0])); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1205 | for (i = 1; i < firstopt; i++) { |
| 1206 | /* not xstat(): shouldn't bomb out on |
| 1207 | * "find not_exist exist -xdev" */ |
Denys Vlasenko | ce9b97f | 2010-01-10 02:33:02 +0100 | [diff] [blame] | 1208 | if (stat(argv[i], &stbuf) == 0) |
| 1209 | G.xdev_dev[i-1] = stbuf.st_dev; |
| 1210 | /* else G.xdev_dev[i-1] stays 0 and |
| 1211 | * won't match any real device dev_t */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1212 | } |
| 1213 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1214 | argp[0] = (char*)"-a"; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1215 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1216 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1217 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 1218 | if (opt == OPT_MINDEPTH || opt == OPT_MINDEPTH + 1) { |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1219 | if (!argp[1]) |
| 1220 | bb_show_usage(); |
Denys Vlasenko | 7783248 | 2010-08-12 14:14:45 +0200 | [diff] [blame] | 1221 | minmaxdepth[opt - OPT_MINDEPTH] = xatoi_positive(argp[1]); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1222 | argp[0] = (char*)"-a"; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1223 | argp++; |
Denys Vlasenko | cef85c2 | 2011-02-12 16:35:24 +0100 | [diff] [blame] | 1224 | argp[0] = (char*)"-a"; |
| 1225 | } |
| 1226 | #endif |
| 1227 | #if ENABLE_FEATURE_FIND_DEPTH |
| 1228 | if (opt == OPT_DEPTH) { |
| 1229 | G.recurse_flags |= ACTION_DEPTHFIRST; |
| 1230 | argp[0] = (char*)"-a"; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1231 | } |
| 1232 | #endif |
Denis Vlasenko | c9d34da | 2007-01-03 03:15:58 +0000 | [diff] [blame] | 1233 | argp++; |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 1234 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 1235 | |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1236 | G.actions = parse_params(&argv[firstopt]); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 1237 | |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 1238 | for (i = 1; i < firstopt; i++) { |
| 1239 | if (!recursive_action(argv[i], |
Denys Vlasenko | 8f7a6d2 | 2009-09-29 11:07:04 +0200 | [diff] [blame] | 1240 | G.recurse_flags,/* flags */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 1241 | fileAction, /* file action */ |
| 1242 | fileAction, /* dir action */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1243 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
Denis Vlasenko | 83518d1 | 2009-03-20 22:17:13 +0000 | [diff] [blame] | 1244 | minmaxdepth, /* user data */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1245 | #else |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 1246 | NULL, /* user data */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 1247 | #endif |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 1248 | 0)) /* depth */ |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 1249 | status = EXIT_FAILURE; |
| 1250 | } |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 1251 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 1252 | } |