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 | * |
Rob Landley | e9a7a62 | 2006-09-22 02:52:41 +0000 | [diff] [blame] | 10 | * Licensed under the GPL version 2, see the file LICENSE in this tarball. |
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 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 56 | #include <fnmatch.h> |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 57 | #include "libbb.h" |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 58 | #if ENABLE_FEATURE_FIND_REGEX |
| 59 | #include "xregex.h" |
| 60 | #endif |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 99912ca | 2007-04-10 15:43:37 +0000 | [diff] [blame] | 62 | /* This is a NOEXEC applet. Be very careful! */ |
| 63 | |
| 64 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 65 | USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;) |
| 66 | USE_FEATURE_FIND_XDEV(static int xdev_count;) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 67 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 68 | typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 69 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 70 | typedef struct { |
| 71 | action_fp f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 72 | #if ENABLE_FEATURE_FIND_NOT |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 73 | bool invert; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 74 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 75 | } action; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 76 | #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name; |
| 77 | #define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 78 | ACTS(print) |
| 79 | ACTS(name, const char *pattern;) |
| 80 | USE_FEATURE_FIND_PATH( ACTS(path, const char *pattern;)) |
| 81 | USE_FEATURE_FIND_REGEX( ACTS(regex, regex_t compiled_pattern;)) |
| 82 | USE_FEATURE_FIND_PRINT0( ACTS(print0)) |
| 83 | USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) |
| 84 | USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;)) |
| 85 | USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;)) |
| 86 | USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;)) |
| 87 | USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) |
| 88 | USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) |
| 89 | USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;)) |
| 90 | USE_FEATURE_FIND_SIZE( ACTS(size, char size_char; off_t size;)) |
| 91 | USE_FEATURE_FIND_CONTEXT(ACTS(context, security_context_t context;)) |
| 92 | USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;)) |
| 93 | USE_FEATURE_FIND_PRUNE( ACTS(prune)) |
| 94 | USE_FEATURE_FIND_DELETE( ACTS(delete)) |
| 95 | USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned *subst_count; int exec_argc;)) |
| 96 | USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 97 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 98 | static action ***actions; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 99 | static bool need_print = 1; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 100 | static int recurse_flags = ACTION_RECURSE; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 101 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 102 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 103 | static unsigned count_subst(const char *str) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 104 | { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 105 | unsigned count = 0; |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 106 | while ((str = strstr(str, "{}")) != NULL) { |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 107 | count++; |
| 108 | str++; |
| 109 | } |
| 110 | return count; |
| 111 | } |
| 112 | |
| 113 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 114 | static char* subst(const char *src, unsigned count, const char* filename) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 115 | { |
| 116 | char *buf, *dst, *end; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 117 | size_t flen = strlen(filename); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 118 | /* we replace each '{}' with filename: growth by strlen-2 */ |
| 119 | buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); |
| 120 | while ((end = strstr(src, "{}"))) { |
| 121 | memcpy(dst, src, end - src); |
| 122 | dst += end - src; |
| 123 | src = end + 2; |
| 124 | memcpy(dst, filename, flen); |
| 125 | dst += flen; |
| 126 | } |
| 127 | strcpy(dst, src); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 128 | return buf; |
| 129 | } |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 130 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 131 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 132 | /* Return values of ACTFs ('action functions') are a bit mask: |
| 133 | * bit 1=1: prune (use SKIP constant for setting it) |
| 134 | * bit 0=1: matched successfully (TRUE) |
| 135 | */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 136 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 137 | static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf) |
| 138 | { |
| 139 | int cur_group; |
| 140 | int cur_action; |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 141 | int rc = 0; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 142 | action **app, *ap; |
| 143 | |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 144 | /* "action group" is a set of actions ANDed together. |
| 145 | * groups are ORed together. |
| 146 | * We simply evaluate each group until we find one in which all actions |
| 147 | * succeed. */ |
| 148 | |
| 149 | /* -prune is special: if it is encountered, then we won't |
| 150 | * descend into current directory. It doesn't matter whether |
| 151 | * action group (in which -prune sits) will succeed or not: |
| 152 | * find * -prune -name 'f*' -o -name 'm*' -- prunes every dir |
| 153 | * find * -name 'f*' -o -prune -name 'm*' -- prunes all dirs |
| 154 | * not starting with 'f' */ |
| 155 | |
| 156 | /* We invert TRUE bit (bit 0). Now 1 there means 'failure'. |
| 157 | * and bitwise OR in "rc |= TRUE ^ ap->f()" will: |
| 158 | * (1) make SKIP (-prune) bit stick; and (2) detect 'failure'. |
| 159 | * On return, bit is restored. */ |
| 160 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 161 | cur_group = -1; |
| 162 | while ((app = appp[++cur_group])) { |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 163 | rc &= ~TRUE; /* 'success' so far, clear TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 164 | cur_action = -1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 165 | while (1) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 166 | ap = app[++cur_action]; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 167 | if (!ap) /* all actions in group were successful */ |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 168 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 169 | rc |= TRUE ^ ap->f(fileName, statbuf, ap); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 170 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 171 | if (ap->invert) rc ^= TRUE; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 172 | #endif |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 173 | if (rc & TRUE) /* current group failed, try next */ |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 174 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
Denis Vlasenko | b04b435 | 2007-04-08 11:10:43 +0000 | [diff] [blame] | 177 | return rc ^ TRUE; /* restore TRUE bit */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 181 | ACTF(name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 182 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 183 | const char *tmp = strrchr(fileName, '/'); |
| 184 | if (tmp == NULL) |
| 185 | tmp = fileName; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 186 | else { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 187 | tmp++; |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 188 | if (!*tmp) { /* "foo/bar/". Oh no... go back to 'b' */ |
| 189 | tmp--; |
| 190 | while (tmp != fileName && *--tmp != '/') |
| 191 | continue; |
| 192 | if (*tmp == '/') |
| 193 | tmp++; |
| 194 | } |
| 195 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 196 | return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0; |
| 197 | } |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 198 | #if ENABLE_FEATURE_FIND_PATH |
| 199 | ACTF(path) |
| 200 | { |
| 201 | return fnmatch(ap->pattern, fileName, 0) == 0; |
| 202 | } |
| 203 | #endif |
| 204 | #if ENABLE_FEATURE_FIND_REGEX |
| 205 | ACTF(regex) |
| 206 | { |
| 207 | regmatch_t match; |
| 208 | if (regexec(&ap->compiled_pattern, fileName, 1, &match, 0 /*eflags*/)) |
| 209 | return 0; /* no match */ |
| 210 | if (match.rm_so) |
| 211 | return 0; /* match doesn't start at pos 0 */ |
| 212 | if (fileName[match.rm_eo]) |
| 213 | return 0; /* match doesn't end exactly at end of pathname */ |
| 214 | return 1; |
| 215 | } |
| 216 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 217 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 218 | ACTF(type) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 219 | { |
Denis Vlasenko | 31c65f2 | 2006-10-31 23:39:37 +0000 | [diff] [blame] | 220 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 221 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 222 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 223 | #if ENABLE_FEATURE_FIND_PERM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 224 | ACTF(perm) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 225 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 226 | /* -perm +mode: at least one of perm_mask bits are set */ |
| 227 | if (ap->perm_char == '+') |
| 228 | return (statbuf->st_mode & ap->perm_mask) != 0; |
| 229 | /* -perm -mode: all of perm_mask are set */ |
| 230 | if (ap->perm_char == '-') |
| 231 | return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask; |
| 232 | /* -perm mode: file mode must match perm_mask */ |
| 233 | return (statbuf->st_mode & 07777) == ap->perm_mask; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 234 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 235 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 236 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 237 | ACTF(mtime) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 238 | { |
| 239 | time_t file_age = time(NULL) - statbuf->st_mtime; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 240 | time_t mtime_secs = ap->mtime_days * 24*60*60; |
| 241 | if (ap->mtime_char == '+') |
| 242 | return file_age >= mtime_secs + 24*60*60; |
| 243 | if (ap->mtime_char == '-') |
| 244 | return file_age < mtime_secs; |
| 245 | /* just numeric mtime */ |
| 246 | return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 247 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 248 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 249 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 250 | ACTF(mmin) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 251 | { |
| 252 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 253 | time_t mmin_secs = ap->mmin_mins * 60; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 254 | if (ap->mmin_char == '+') |
| 255 | return file_age >= mmin_secs + 60; |
| 256 | if (ap->mmin_char == '-') |
| 257 | return file_age < mmin_secs; |
| 258 | /* just numeric mmin */ |
| 259 | return file_age >= mmin_secs && file_age < (mmin_secs + 60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 260 | } |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 261 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 262 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 263 | ACTF(newer) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 264 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 265 | return (ap->newer_mtime < statbuf->st_mtime); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 266 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 267 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 268 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 269 | ACTF(inum) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 270 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 271 | return (statbuf->st_ino == ap->inode_num); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 272 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 273 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 274 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 275 | ACTF(exec) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 276 | { |
| 277 | int i, rc; |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 278 | char *argv[ap->exec_argc + 1]; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 279 | for (i = 0; i < ap->exec_argc; i++) |
| 280 | argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); |
| 281 | argv[i] = NULL; /* terminate the list */ |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 282 | |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 283 | rc = spawn_and_wait(argv); |
Denis Vlasenko | 7e754f1 | 2007-04-09 13:04:50 +0000 | [diff] [blame] | 284 | if (rc < 0) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 285 | bb_perror_msg("%s", argv[0]); |
Denis Vlasenko | cd7001f | 2007-04-09 21:32:30 +0000 | [diff] [blame] | 286 | |
| 287 | i = 0; |
| 288 | while (argv[i]) |
| 289 | free(argv[i++]); |
| 290 | return rc == 0; /* return 1 if exitcode 0 */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 291 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 292 | #endif |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 293 | #if ENABLE_FEATURE_FIND_USER |
| 294 | ACTF(user) |
| 295 | { |
| 296 | return (statbuf->st_uid == ap->uid); |
| 297 | } |
| 298 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 299 | #if ENABLE_FEATURE_FIND_GROUP |
| 300 | ACTF(group) |
| 301 | { |
| 302 | return (statbuf->st_gid == ap->gid); |
| 303 | } |
| 304 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 305 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 306 | ACTF(print0) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 307 | { |
| 308 | printf("%s%c", fileName, '\0'); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 309 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 310 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 311 | #endif |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 312 | ACTF(print) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 313 | { |
| 314 | puts(fileName); |
| 315 | return TRUE; |
| 316 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 317 | #if ENABLE_FEATURE_FIND_PAREN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 318 | ACTF(paren) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 319 | { |
| 320 | return exec_actions(ap->subexpr, fileName, statbuf); |
| 321 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 322 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 323 | #if ENABLE_FEATURE_FIND_SIZE |
| 324 | ACTF(size) |
| 325 | { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 326 | if (ap->size_char == '+') |
| 327 | return statbuf->st_size > ap->size; |
| 328 | if (ap->size_char == '-') |
| 329 | return statbuf->st_size < ap->size; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 330 | return statbuf->st_size == ap->size; |
| 331 | } |
| 332 | #endif |
Bernhard Reutner-Fischer | 6d79dd6 | 2007-04-05 09:40:12 +0000 | [diff] [blame] | 333 | #if ENABLE_FEATURE_FIND_PRUNE |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 334 | /* |
| 335 | * -prune: if -depth is not given, return true and do not descend |
| 336 | * current dir; if -depth is given, return false with no effect. |
| 337 | * Example: |
| 338 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print |
| 339 | */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 340 | ACTF(prune) |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 341 | { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 342 | return SKIP + TRUE; |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 343 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 344 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 345 | #if ENABLE_FEATURE_FIND_DELETE |
| 346 | ACTF(delete) |
| 347 | { |
| 348 | int rc; |
| 349 | if (S_ISDIR(statbuf->st_mode)) { |
| 350 | rc = rmdir(fileName); |
| 351 | } else { |
| 352 | rc = unlink(fileName); |
| 353 | } |
| 354 | if (rc < 0) |
| 355 | bb_perror_msg("%s", fileName); |
| 356 | return TRUE; |
| 357 | } |
| 358 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 359 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 360 | ACTF(context) |
| 361 | { |
| 362 | security_context_t con; |
| 363 | int rc; |
| 364 | |
| 365 | if (recurse_flags & ACTION_FOLLOWLINKS) { |
| 366 | rc = getfilecon(fileName, &con); |
| 367 | } else { |
| 368 | rc = lgetfilecon(fileName, &con); |
| 369 | } |
| 370 | if (rc < 0) |
| 371 | return FALSE; |
| 372 | rc = strcmp(ap->context, con); |
| 373 | freecon(con); |
| 374 | return rc == 0; |
| 375 | } |
| 376 | #endif |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 377 | |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 378 | |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 379 | static int fileAction(const char *fileName, struct stat *statbuf, void *userData, int depth) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 380 | { |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 381 | int i; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 382 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 383 | int maxdepth = (int)(ptrdiff_t)userData; |
| 384 | |
| 385 | if (depth > maxdepth) return SKIP; |
| 386 | #endif |
| 387 | |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 388 | #if ENABLE_FEATURE_FIND_XDEV |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 389 | if (S_ISDIR(statbuf->st_mode) && xdev_count) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 390 | for (i = 0; i < xdev_count; i++) { |
| 391 | if (xdev_dev[i] != statbuf->st_dev) |
| 392 | return SKIP; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 393 | } |
| 394 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 395 | #endif |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 396 | i = exec_actions(actions, fileName, statbuf); |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 397 | /* Had no explicit -print[0] or -exec? then print */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 398 | if ((i & TRUE) && need_print) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 399 | puts(fileName); |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 400 | /* Cannot return 0: our caller, recursive_action(), |
| 401 | * will perror() and skip dirs (if called on dir) */ |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 402 | return (i & SKIP) ? SKIP : TRUE; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 405 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 406 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 407 | static int find_type(const char *type) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 408 | { |
| 409 | int mask = 0; |
| 410 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 411 | if (*type == 'b') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 412 | mask = S_IFBLK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 413 | else if (*type == 'c') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 414 | mask = S_IFCHR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 415 | else if (*type == 'd') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 416 | mask = S_IFDIR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 417 | else if (*type == 'p') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 418 | mask = S_IFIFO; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 419 | else if (*type == 'f') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 420 | mask = S_IFREG; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 421 | else if (*type == 'l') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 422 | mask = S_IFLNK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 423 | else if (*type == 's') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 424 | mask = S_IFSOCK; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 425 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame] | 426 | if (mask == 0 || *(type + 1) != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 427 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 428 | |
| 429 | return mask; |
| 430 | } |
| 431 | #endif |
| 432 | |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 433 | #if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \ |
| 434 | || ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 435 | static const char* plus_minus_num(const char* str) |
| 436 | { |
| 437 | if (*str == '-' || *str == '+') |
| 438 | str++; |
| 439 | return str; |
| 440 | } |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 441 | #endif |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 442 | |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 443 | static action*** parse_params(char **argv) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 444 | { |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 445 | enum { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 446 | PARM_a , |
| 447 | PARM_o , |
| 448 | USE_FEATURE_FIND_NOT( PARM_char_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 449 | #if ENABLE_DESKTOP |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 450 | PARM_and , |
| 451 | PARM_or , |
| 452 | USE_FEATURE_FIND_NOT( PARM_not ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 453 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 454 | PARM_print , |
| 455 | USE_FEATURE_FIND_PRINT0( PARM_print0 ,) |
| 456 | USE_FEATURE_FIND_DEPTH( PARM_depth ,) |
| 457 | USE_FEATURE_FIND_PRUNE( PARM_prune ,) |
| 458 | USE_FEATURE_FIND_DELETE( PARM_delete ,) |
| 459 | USE_FEATURE_FIND_EXEC( PARM_exec ,) |
| 460 | USE_FEATURE_FIND_PAREN( PARM_char_brace,) |
| 461 | /* All options starting from here require argument */ |
| 462 | PARM_name , |
| 463 | USE_FEATURE_FIND_PATH( PARM_path ,) |
| 464 | USE_FEATURE_FIND_REGEX( PARM_regex ,) |
| 465 | USE_FEATURE_FIND_TYPE( PARM_type ,) |
| 466 | USE_FEATURE_FIND_PERM( PARM_perm ,) |
| 467 | USE_FEATURE_FIND_MTIME( PARM_mtime ,) |
| 468 | USE_FEATURE_FIND_MMIN( PARM_mmin ,) |
| 469 | USE_FEATURE_FIND_NEWER( PARM_newer ,) |
| 470 | USE_FEATURE_FIND_INUM( PARM_inum ,) |
| 471 | USE_FEATURE_FIND_USER( PARM_user ,) |
| 472 | USE_FEATURE_FIND_GROUP( PARM_group ,) |
| 473 | USE_FEATURE_FIND_SIZE( PARM_size ,) |
| 474 | USE_FEATURE_FIND_CONTEXT(PARM_context ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 475 | }; |
| 476 | |
| 477 | static const char *const params[] = { |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 478 | "-a" , |
| 479 | "-o" , |
| 480 | USE_FEATURE_FIND_NOT( "!" ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 481 | #if ENABLE_DESKTOP |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 482 | "-and" , |
| 483 | "-or" , |
| 484 | USE_FEATURE_FIND_NOT( "-not" ,) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 485 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 486 | "-print" , |
| 487 | USE_FEATURE_FIND_PRINT0( "-print0" ,) |
| 488 | USE_FEATURE_FIND_DEPTH( "-depth" ,) |
| 489 | USE_FEATURE_FIND_PRUNE( "-prune" ,) |
| 490 | USE_FEATURE_FIND_DELETE( "-delete" ,) |
| 491 | USE_FEATURE_FIND_EXEC( "-exec" ,) |
| 492 | USE_FEATURE_FIND_PAREN( "(" ,) |
| 493 | /* All options starting from here require argument */ |
| 494 | "-name" , |
| 495 | USE_FEATURE_FIND_PATH( "-path" ,) |
| 496 | USE_FEATURE_FIND_REGEX( "-regex" ,) |
| 497 | USE_FEATURE_FIND_TYPE( "-type" ,) |
| 498 | USE_FEATURE_FIND_PERM( "-perm" ,) |
| 499 | USE_FEATURE_FIND_MTIME( "-mtime" ,) |
| 500 | USE_FEATURE_FIND_MMIN( "-mmin" ,) |
| 501 | USE_FEATURE_FIND_NEWER( "-newer" ,) |
| 502 | USE_FEATURE_FIND_INUM( "-inum" ,) |
| 503 | USE_FEATURE_FIND_USER( "-user" ,) |
| 504 | USE_FEATURE_FIND_GROUP( "-group" ,) |
| 505 | USE_FEATURE_FIND_SIZE( "-size" ,) |
| 506 | USE_FEATURE_FIND_CONTEXT("-context",) |
| 507 | NULL |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 508 | }; |
| 509 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 510 | action*** appp; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 511 | unsigned cur_group = 0; |
| 512 | unsigned cur_action = 0; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 513 | USE_FEATURE_FIND_NOT( bool invert_flag = 0; ) |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 514 | |
| 515 | /* 'static' doesn't work here! (gcc 4.1.2) */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 516 | action* alloc_action(int sizeof_struct, action_fp f) |
| 517 | { |
| 518 | action *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 519 | appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp)); |
| 520 | appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct); |
| 521 | appp[cur_group][cur_action] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 522 | ap->f = f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 523 | USE_FEATURE_FIND_NOT( ap->invert = invert_flag; ) |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 524 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 525 | return ap; |
| 526 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 527 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 528 | #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] | 529 | |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 530 | appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 531 | |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 532 | /* Actions have side effects and return a true or false value |
| 533 | * We implement: -print, -print0, -exec |
| 534 | * |
| 535 | * The rest are tests. |
| 536 | * |
| 537 | * Tests and actions are grouped by operators |
| 538 | * ( expr ) Force precedence |
| 539 | * ! expr True if expr is false |
| 540 | * -not expr Same as ! expr |
| 541 | * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false |
| 542 | * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true |
| 543 | * expr1 , expr2 List; both expr1 and expr2 are always evaluated |
| 544 | * We implement: (), -a, -o |
| 545 | */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 546 | while (*argv) { |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 547 | const char *arg = argv[0]; |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 548 | int parm = index_in_str_array(params, arg); |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 549 | const char *arg1 = argv[1]; |
| 550 | |
| 551 | if (parm >= PARM_name) { |
| 552 | /* All options starting from -name require argument */ |
| 553 | if (!arg1) |
| 554 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 555 | argv++; |
| 556 | } |
| 557 | |
| 558 | /* We can use big switch() here, but on i386 |
| 559 | * it doesn't give smaller code. Other arches? */ |
| 560 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 561 | /* --- Operators --- */ |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 562 | if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) { |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 563 | /* no further special handling required */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 564 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 565 | else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 566 | /* start new OR group */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 567 | cur_group++; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 568 | appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 569 | /*appp[cur_group] = NULL; - already NULL */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 570 | appp[cur_group+1] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 571 | cur_action = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 572 | } |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 573 | #if ENABLE_FEATURE_FIND_NOT |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 574 | else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) { |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 575 | /* also handles "find ! ! -name 'foo*'" */ |
| 576 | invert_flag ^= 1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 577 | } |
| 578 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 579 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 580 | /* --- Tests and actions --- */ |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 581 | else if (parm == PARM_print) { |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 582 | need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 583 | /* GNU find ignores '!' here: "find ! -print" */ |
| 584 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 585 | (void) ALLOC_ACTION(print); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 586 | } |
| 587 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 588 | else if (parm == PARM_print0) { |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 589 | need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 590 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 591 | (void) ALLOC_ACTION(print0); |
| 592 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 593 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 594 | #if ENABLE_FEATURE_FIND_DEPTH |
| 595 | else if (parm == PARM_depth) { |
| 596 | recurse_flags |= ACTION_DEPTHFIRST; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 597 | } |
| 598 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 599 | #if ENABLE_FEATURE_FIND_PRUNE |
| 600 | else if (parm == PARM_prune) { |
| 601 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
| 602 | (void) ALLOC_ACTION(prune); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 603 | } |
| 604 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 605 | #if ENABLE_FEATURE_FIND_DELETE |
| 606 | else if (parm == PARM_delete) { |
| 607 | need_print = 0; |
| 608 | recurse_flags |= ACTION_DEPTHFIRST; |
| 609 | (void) ALLOC_ACTION(delete); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 610 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 611 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 612 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 613 | else if (parm == PARM_exec) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 614 | int i; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 615 | action_exec *ap; |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 616 | need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 617 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 618 | ap = ALLOC_ACTION(exec); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 619 | ap->exec_argv = ++argv; /* first arg after -exec */ |
| 620 | ap->exec_argc = 0; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 621 | while (1) { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 622 | if (!*argv) /* did not see ';' until end */ |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 623 | bb_error_msg_and_die("-exec CMD must end by ';'"); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 624 | if (LONE_CHAR(argv[0], ';')) |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 625 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 626 | argv++; |
| 627 | ap->exec_argc++; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 628 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 629 | if (ap->exec_argc == 0) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 630 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 631 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 632 | i = ap->exec_argc; |
| 633 | while (i--) |
| 634 | ap->subst_count[i] = count_subst(ap->exec_argv[i]); |
| 635 | } |
| 636 | #endif |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 637 | #if ENABLE_FEATURE_FIND_PAREN |
| 638 | else if (parm == PARM_char_brace) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 639 | action_paren *ap; |
| 640 | char **endarg; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 641 | unsigned nested = 1; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 642 | |
| 643 | endarg = argv; |
| 644 | while (1) { |
| 645 | if (!*++endarg) |
| 646 | bb_error_msg_and_die("unpaired '('"); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 647 | if (LONE_CHAR(*endarg, '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 648 | nested++; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 649 | else if (LONE_CHAR(*endarg, ')') && !--nested) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 650 | *endarg = NULL; |
| 651 | break; |
| 652 | } |
| 653 | } |
| 654 | ap = ALLOC_ACTION(paren); |
| 655 | ap->subexpr = parse_params(argv + 1); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 656 | *endarg = (char*) ")"; /* restore NULLed parameter */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 657 | argv = endarg; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 658 | } |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 659 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 660 | else if (parm == PARM_name) { |
| 661 | action_name *ap; |
| 662 | ap = ALLOC_ACTION(name); |
| 663 | ap->pattern = arg1; |
| 664 | } |
| 665 | #if ENABLE_FEATURE_FIND_PATH |
| 666 | else if (parm == PARM_path) { |
| 667 | action_path *ap; |
| 668 | ap = ALLOC_ACTION(path); |
| 669 | ap->pattern = arg1; |
| 670 | } |
| 671 | #endif |
| 672 | #if ENABLE_FEATURE_FIND_REGEX |
| 673 | else if (parm == PARM_regex) { |
| 674 | action_regex *ap; |
| 675 | ap = ALLOC_ACTION(regex); |
| 676 | xregcomp(&ap->compiled_pattern, arg1, 0 /*cflags*/); |
| 677 | } |
| 678 | #endif |
| 679 | #if ENABLE_FEATURE_FIND_TYPE |
| 680 | else if (parm == PARM_type) { |
| 681 | action_type *ap; |
| 682 | ap = ALLOC_ACTION(type); |
| 683 | ap->type_mask = find_type(arg1); |
| 684 | } |
| 685 | #endif |
| 686 | #if ENABLE_FEATURE_FIND_PERM |
| 687 | /* -perm mode File's permission bits are exactly mode (octal or symbolic). |
| 688 | * Symbolic modes use mode 0 as a point of departure. |
| 689 | * -perm -mode All of the permission bits mode are set for the file. |
| 690 | * -perm +mode Any of the permission bits mode are set for the file. |
| 691 | */ |
| 692 | else if (parm == PARM_perm) { |
| 693 | action_perm *ap; |
| 694 | ap = ALLOC_ACTION(perm); |
| 695 | ap->perm_char = arg1[0]; |
| 696 | arg1 = plus_minus_num(arg1); |
| 697 | ap->perm_mask = 0; |
| 698 | if (!bb_parse_mode(arg1, &ap->perm_mask)) |
| 699 | bb_error_msg_and_die("invalid mode: %s", arg1); |
| 700 | } |
| 701 | #endif |
| 702 | #if ENABLE_FEATURE_FIND_MTIME |
| 703 | else if (parm == PARM_mtime) { |
| 704 | action_mtime *ap; |
| 705 | ap = ALLOC_ACTION(mtime); |
| 706 | ap->mtime_char = arg1[0]; |
| 707 | ap->mtime_days = xatoul(plus_minus_num(arg1)); |
| 708 | } |
| 709 | #endif |
| 710 | #if ENABLE_FEATURE_FIND_MMIN |
| 711 | else if (parm == PARM_mmin) { |
| 712 | action_mmin *ap; |
| 713 | ap = ALLOC_ACTION(mmin); |
| 714 | ap->mmin_char = arg1[0]; |
| 715 | ap->mmin_mins = xatoul(plus_minus_num(arg1)); |
| 716 | } |
| 717 | #endif |
| 718 | #if ENABLE_FEATURE_FIND_NEWER |
| 719 | else if (parm == PARM_newer) { |
| 720 | struct stat stat_newer; |
| 721 | action_newer *ap; |
| 722 | ap = ALLOC_ACTION(newer); |
| 723 | xstat(arg1, &stat_newer); |
| 724 | ap->newer_mtime = stat_newer.st_mtime; |
| 725 | } |
| 726 | #endif |
| 727 | #if ENABLE_FEATURE_FIND_INUM |
| 728 | else if (parm == PARM_inum) { |
| 729 | action_inum *ap; |
| 730 | ap = ALLOC_ACTION(inum); |
| 731 | ap->inode_num = xatoul(arg1); |
| 732 | } |
| 733 | #endif |
| 734 | #if ENABLE_FEATURE_FIND_USER |
| 735 | else if (parm == PARM_user) { |
| 736 | action_user *ap; |
| 737 | ap = ALLOC_ACTION(user); |
| 738 | ap->uid = bb_strtou(arg1, NULL, 10); |
| 739 | if (errno) |
| 740 | ap->uid = xuname2uid(arg1); |
| 741 | } |
| 742 | #endif |
| 743 | #if ENABLE_FEATURE_FIND_GROUP |
| 744 | else if (parm == PARM_group) { |
| 745 | action_group *ap; |
| 746 | ap = ALLOC_ACTION(group); |
| 747 | ap->gid = bb_strtou(arg1, NULL, 10); |
| 748 | if (errno) |
| 749 | ap->gid = xgroup2gid(arg1); |
| 750 | } |
| 751 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 752 | #if ENABLE_FEATURE_FIND_SIZE |
| 753 | else if (parm == PARM_size) { |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 754 | /* -size n[bckw]: file uses n units of space |
| 755 | * b (default): units are 512-byte blocks |
| 756 | * c: 1 byte |
| 757 | * k: kilobytes |
| 758 | * w: 2-byte words |
| 759 | */ |
| 760 | #if ENABLE_LFS |
| 761 | #define XATOU_SFX xatoull_sfx |
| 762 | #else |
| 763 | #define XATOU_SFX xatoul_sfx |
| 764 | #endif |
| 765 | static const struct suffix_mult find_suffixes[] = { |
| 766 | { "c", 1 }, |
| 767 | { "w", 2 }, |
| 768 | { "b"+1, 512 }, |
| 769 | { "b", 512 }, |
| 770 | { "k", 1024 }, |
| 771 | { NULL, 0 } |
| 772 | }; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 773 | action_size *ap; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 774 | ap = ALLOC_ACTION(size); |
Denis Vlasenko | 53a0e97 | 2007-06-16 00:30:52 +0000 | [diff] [blame] | 775 | ap->size_char = arg1[0]; |
| 776 | ap->size = XATOU_SFX(plus_minus_num(arg1), find_suffixes); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 777 | } |
| 778 | #endif |
Denis Vlasenko | a19faf8 | 2007-06-19 22:22:57 +0000 | [diff] [blame^] | 779 | #if ENABLE_FEATURE_FIND_CONTEXT |
| 780 | else if (parm == PARM_context) { |
| 781 | action_context *ap; |
| 782 | ap = ALLOC_ACTION(context); |
| 783 | ap->context = NULL; |
| 784 | if (selinux_raw_to_trans_context(arg1, &ap->context)) |
| 785 | bb_perror_msg("%s", arg1); |
Denis Vlasenko | 62f0479 | 2007-04-13 10:00:12 +0000 | [diff] [blame] | 786 | } |
| 787 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 788 | else { |
| 789 | bb_error_msg("unrecognized: %s", arg); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 790 | bb_show_usage(); |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 791 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 792 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 793 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 794 | return appp; |
| 795 | #undef ALLOC_ACTION |
| 796 | } |
| 797 | |
| 798 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 799 | int find_main(int argc, char **argv); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 800 | int find_main(int argc, char **argv) |
| 801 | { |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 802 | static const char *const options[] = { |
Bernhard Reutner-Fischer | a87ed2c | 2007-03-29 13:56:49 +0000 | [diff] [blame] | 803 | "-follow", |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 804 | USE_FEATURE_FIND_XDEV( "-xdev" ,) |
| 805 | USE_FEATURE_FIND_MAXDEPTH("-maxdepth",) |
Bernhard Reutner-Fischer | a87ed2c | 2007-03-29 13:56:49 +0000 | [diff] [blame] | 806 | NULL |
| 807 | }; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 808 | enum { |
| 809 | OPT_FOLLOW, |
| 810 | USE_FEATURE_FIND_XDEV( OPT_XDEV ,) |
| 811 | USE_FEATURE_FIND_MAXDEPTH(OPT_MAXDEPTH,) |
| 812 | }; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 813 | |
Denis Vlasenko | 7d6c929 | 2007-04-05 00:35:43 +0000 | [diff] [blame] | 814 | char *arg; |
| 815 | char **argp; |
| 816 | int i, firstopt, status = EXIT_SUCCESS; |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 817 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 818 | int maxdepth = INT_MAX; |
| 819 | #endif |
Denis Vlasenko | 7d6c929 | 2007-04-05 00:35:43 +0000 | [diff] [blame] | 820 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 821 | for (firstopt = 1; firstopt < argc; firstopt++) { |
| 822 | if (argv[firstopt][0] == '-') |
| 823 | break; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 824 | if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!')) |
| 825 | break; |
Denis Vlasenko | 56fce00 | 2007-04-04 23:41:35 +0000 | [diff] [blame] | 826 | #if ENABLE_FEATURE_FIND_PAREN |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 827 | if (LONE_CHAR(argv[firstopt], '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 828 | break; |
| 829 | #endif |
| 830 | } |
| 831 | if (firstopt == 1) { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 832 | argv[0] = (char*)"."; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 833 | argv--; |
| 834 | firstopt++; |
| 835 | } |
| 836 | |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 837 | /* All options always return true. They always take effect |
| 838 | * rather than being processed only when their place in the |
| 839 | * expression is reached. |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 840 | * We implement: -follow, -xdev, -maxdepth |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 841 | */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 842 | /* Process options, and replace then with -a */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 843 | /* (-a will be ignored by recursive parser later) */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 844 | argp = &argv[firstopt]; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 845 | while ((arg = argp[0])) { |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 846 | int opt = index_in_str_array(options, arg); |
| 847 | if (opt == OPT_FOLLOW) { |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 848 | recurse_flags |= ACTION_FOLLOWLINKS; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 849 | argp[0] = (char*)"-a"; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 850 | } |
| 851 | #if ENABLE_FEATURE_FIND_XDEV |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 852 | if (opt == OPT_XDEV) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 853 | struct stat stbuf; |
| 854 | if (!xdev_count) { |
| 855 | xdev_count = firstopt - 1; |
| 856 | xdev_dev = xmalloc(xdev_count * sizeof(dev_t)); |
| 857 | for (i = 1; i < firstopt; i++) { |
| 858 | /* not xstat(): shouldn't bomb out on |
| 859 | * "find not_exist exist -xdev" */ |
Denis Vlasenko | c9d34da | 2007-01-03 03:15:58 +0000 | [diff] [blame] | 860 | if (stat(argv[i], &stbuf)) |
| 861 | stbuf.st_dev = -1L; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 862 | xdev_dev[i-1] = stbuf.st_dev; |
| 863 | } |
| 864 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 865 | argp[0] = (char*)"-a"; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 866 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 867 | #endif |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 868 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 869 | if (opt == OPT_MAXDEPTH) { |
| 870 | if (!argp[1]) |
| 871 | bb_show_usage(); |
| 872 | maxdepth = xatoi_u(argp[1]); |
| 873 | argp[0] = (char*)"-a"; |
| 874 | argp[1] = (char*)"-a"; |
| 875 | argp++; |
| 876 | } |
| 877 | #endif |
Denis Vlasenko | c9d34da | 2007-01-03 03:15:58 +0000 | [diff] [blame] | 878 | argp++; |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 879 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 880 | |
| 881 | actions = parse_params(&argv[firstopt]); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 882 | |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 883 | for (i = 1; i < firstopt; i++) { |
| 884 | if (!recursive_action(argv[i], |
Denis Vlasenko | bbd695d | 2007-04-08 10:52:28 +0000 | [diff] [blame] | 885 | recurse_flags, /* flags */ |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 886 | fileAction, /* file action */ |
| 887 | fileAction, /* dir action */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 888 | #if ENABLE_FEATURE_FIND_MAXDEPTH |
| 889 | (void*)maxdepth,/* user data */ |
| 890 | #else |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 891 | NULL, /* user data */ |
Denis Vlasenko | b941129 | 2007-06-15 23:43:11 +0000 | [diff] [blame] | 892 | #endif |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 893 | 0)) /* depth */ |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 894 | status = EXIT_FAILURE; |
| 895 | } |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 896 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 897 | } |