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