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