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