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 | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 38 | */ |
| 39 | |
Bernhard Reutner-Fischer | e15d757 | 2006-06-02 20:56:16 +0000 | [diff] [blame] | 40 | #include "busybox.h" |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 41 | #include <fnmatch.h> |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 42 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 43 | USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;) |
| 44 | USE_FEATURE_FIND_XDEV(static int xdev_count;) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 45 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 46 | typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 47 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 48 | typedef struct { |
| 49 | action_fp f; |
| 50 | } action; |
| 51 | #define SACT(name, arg...) typedef struct { action a; arg; } action_##name; |
| 52 | #define SFUNC(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) |
| 53 | SACT(print) |
| 54 | SACT(name, char *pattern;) |
| 55 | USE_FEATURE_FIND_PRINT0(SACT(print0)) |
| 56 | USE_FEATURE_FIND_TYPE( SACT(type, int type_mask;)) |
| 57 | USE_FEATURE_FIND_PERM( SACT(perm, char perm_char; int perm_mask;)) |
| 58 | USE_FEATURE_FIND_MTIME( SACT(mtime, char mtime_char; int mtime_days;)) |
| 59 | USE_FEATURE_FIND_MMIN( SACT(mmin, char mmin_char; int mmin_mins;)) |
| 60 | USE_FEATURE_FIND_NEWER( SACT(newer, time_t newer_mtime;)) |
| 61 | USE_FEATURE_FIND_INUM( SACT(inum, ino_t inode_num;)) |
| 62 | USE_FEATURE_FIND_EXEC( SACT(exec, char **exec_argv; int *subst_count; int exec_argc;)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 63 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 64 | static action ***actions; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 65 | |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 67 | static int count_subst(const char *str) |
| 68 | { |
| 69 | int count = 0; |
| 70 | while ((str = strstr(str, "{}"))) { |
| 71 | count++; |
| 72 | str++; |
| 73 | } |
| 74 | return count; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | static char* subst(const char *src, int count, const char* filename) |
| 79 | { |
| 80 | char *buf, *dst, *end; |
| 81 | int flen = strlen(filename); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 82 | /* we replace each '{}' with filename: growth by strlen-2 */ |
| 83 | buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); |
| 84 | while ((end = strstr(src, "{}"))) { |
| 85 | memcpy(dst, src, end - src); |
| 86 | dst += end - src; |
| 87 | src = end + 2; |
| 88 | memcpy(dst, filename, flen); |
| 89 | dst += flen; |
| 90 | } |
| 91 | strcpy(dst, src); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 92 | return buf; |
| 93 | } |
| 94 | |
| 95 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 96 | SFUNC(name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 97 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 98 | const char *tmp = strrchr(fileName, '/'); |
| 99 | if (tmp == NULL) |
| 100 | tmp = fileName; |
| 101 | else |
| 102 | tmp++; |
| 103 | return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0; |
| 104 | } |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 105 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 106 | SFUNC(type) |
| 107 | { |
| 108 | return !((statbuf->st_mode & S_IFMT) == ap->type_mask); |
| 109 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 110 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 111 | #if ENABLE_FEATURE_FIND_PERM |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 112 | SFUNC(perm) |
| 113 | { |
| 114 | return !((isdigit(ap->perm_char) && (statbuf->st_mode & 07777) == ap->perm_mask) |
| 115 | || (ap->perm_char == '-' && (statbuf->st_mode & ap->perm_mask) == ap->perm_mask) |
| 116 | || (ap->perm_char == '+' && (statbuf->st_mode & ap->perm_mask) != 0)); |
| 117 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 118 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 119 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 120 | SFUNC(mtime) |
| 121 | { |
| 122 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 123 | time_t mtime_secs = ap->mtime_days * 24 * 60 * 60; |
| 124 | return !((isdigit(ap->mtime_char) && file_age >= mtime_secs |
| 125 | && file_age < mtime_secs + 24 * 60 * 60) |
| 126 | || (ap->mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) |
| 127 | || (ap->mtime_char == '-' && file_age < mtime_secs)); |
| 128 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 129 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 130 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 131 | SFUNC(mmin) |
| 132 | { |
| 133 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 134 | time_t mmin_secs = ap->mmin_mins * 60; |
| 135 | return !((isdigit(ap->mmin_char) && file_age >= mmin_secs |
| 136 | && file_age < mmin_secs + 60) |
| 137 | || (ap->mmin_char == '+' && file_age >= mmin_secs + 60) |
| 138 | || (ap->mmin_char == '-' && file_age < mmin_secs)); |
| 139 | } |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 140 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 141 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 142 | SFUNC(newer) |
| 143 | { |
| 144 | return (ap->newer_mtime >= statbuf->st_mtime); |
| 145 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 146 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 147 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 148 | SFUNC(inum) |
| 149 | { |
| 150 | return (statbuf->st_ino != ap->inode_num); |
| 151 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 152 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 153 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 154 | SFUNC(exec) |
| 155 | { |
| 156 | int i, rc; |
| 157 | char *argv[ap->exec_argc+1]; |
| 158 | for (i = 0; i < ap->exec_argc; i++) |
| 159 | argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); |
| 160 | argv[i] = NULL; /* terminate the list */ |
| 161 | errno = 0; |
| 162 | rc = wait4pid(spawn(argv)); |
| 163 | if (errno) |
| 164 | bb_perror_msg("%s", argv[0]); |
| 165 | for (i = 0; i < ap->exec_argc; i++) |
| 166 | free(argv[i]); |
| 167 | return rc == 0; /* return 1 if success */ |
| 168 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 169 | #endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 170 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 171 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 172 | SFUNC(print0) |
| 173 | { |
| 174 | printf("%s%c", fileName, '\0'); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 175 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 176 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 177 | #endif |
| 178 | SFUNC(print) |
| 179 | { |
| 180 | puts(fileName); |
| 181 | return TRUE; |
| 182 | } |
| 183 | |
| 184 | |
| 185 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth) |
| 186 | { |
| 187 | int cur_group; |
| 188 | int cur_action; |
| 189 | action **app, *ap; |
| 190 | |
| 191 | cur_group = -1; |
| 192 | while ((app = actions[++cur_group])) { |
| 193 | cur_action = -1; |
| 194 | do { |
| 195 | ap = app[++cur_action]; |
| 196 | } while (ap && ap->f(fileName, statbuf, ap)); |
| 197 | if (!ap) { |
| 198 | /* all actions in group were successful */ |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | return TRUE; |
| 203 | } |
| 204 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 205 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 206 | #if ENABLE_FEATURE_FIND_TYPE |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 207 | static int find_type(char *type) |
| 208 | { |
| 209 | int mask = 0; |
| 210 | |
| 211 | switch (type[0]) { |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 212 | case 'b': |
| 213 | mask = S_IFBLK; |
| 214 | break; |
| 215 | case 'c': |
| 216 | mask = S_IFCHR; |
| 217 | break; |
| 218 | case 'd': |
| 219 | mask = S_IFDIR; |
| 220 | break; |
| 221 | case 'p': |
| 222 | mask = S_IFIFO; |
| 223 | break; |
| 224 | case 'f': |
| 225 | mask = S_IFREG; |
| 226 | break; |
| 227 | case 'l': |
| 228 | mask = S_IFLNK; |
| 229 | break; |
| 230 | case 's': |
| 231 | mask = S_IFSOCK; |
| 232 | break; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | if (mask == 0 || type[1] != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 236 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 237 | |
| 238 | return mask; |
| 239 | } |
| 240 | #endif |
| 241 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 242 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 243 | int find_main(int argc, char **argv) |
| 244 | { |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 245 | int dereference = FALSE; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 246 | int i, j, firstopt, status = EXIT_SUCCESS; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 247 | int cur_group; |
| 248 | int cur_action; |
| 249 | int need_default = 1; |
| 250 | |
| 251 | action* alloc_action(int sizeof_struct, action_fp f) |
| 252 | { |
| 253 | action *ap; |
| 254 | actions[cur_group] = xrealloc(actions[cur_group], (cur_action+2) * sizeof(*actions)); |
| 255 | actions[cur_group][cur_action++] = ap = xmalloc(sizeof_struct); |
| 256 | actions[cur_group][cur_action] = NULL; |
| 257 | ap->f = f; |
| 258 | return ap; |
| 259 | } |
| 260 | #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] | 261 | |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 262 | for (firstopt = 1; firstopt < argc; firstopt++) { |
| 263 | if (argv[firstopt][0] == '-') |
| 264 | break; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 265 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 266 | if (firstopt == 1) { |
| 267 | argv[0] = "."; |
| 268 | argv--; |
| 269 | argc++; |
| 270 | firstopt++; |
| 271 | } |
| 272 | |
| 273 | // All options always return true. They always take effect, |
| 274 | // rather than being processed only when their place in the |
| 275 | // expression is reached |
| 276 | // We implement: -follow, -xdev |
| 277 | |
| 278 | // Actions have side effects and return a true or false value |
| 279 | // We implement: -print, -print0, -exec |
| 280 | |
| 281 | // The rest are tests. |
| 282 | |
| 283 | // Tests and actions are grouped by operators |
| 284 | // ( expr ) Force precedence |
| 285 | // ! expr True if expr is false |
| 286 | // -not expr Same as ! expr |
| 287 | // expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false |
| 288 | // expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true |
| 289 | // expr1 , expr2 List; both expr1 and expr2 are always evaluated |
| 290 | // We implement: -a, -o |
| 291 | |
| 292 | cur_group = 0; |
| 293 | cur_action = 0; |
| 294 | actions = xzalloc(sizeof(*actions)); /* actions[0] == NULL */ |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 295 | |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 296 | /* Parse any options */ |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 297 | for (i = firstopt; i < argc; i++) { |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 298 | char *arg = argv[i]; |
| 299 | char *arg1 = argv[i+1]; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 300 | |
| 301 | /* --- Operators --- */ |
| 302 | if (ENABLE_DESKTOP |
| 303 | && (strcmp(arg, "-a") == 0 || strcmp(arg, "-and") == 0) |
| 304 | ) { |
| 305 | /* no special handling required */ |
| 306 | } |
| 307 | else if (strcmp(arg, "-o") == 0 |
| 308 | USE_DESKTOP(|| strcmp(arg, "-or") == 0) |
| 309 | ) { |
| 310 | if (need_default) |
| 311 | (void) ALLOC_ACTION(print); |
| 312 | cur_group++; |
| 313 | actions = xrealloc(actions, (cur_group+1) * sizeof(*actions)); |
| 314 | actions[cur_group] = NULL; |
| 315 | actions[cur_group+1] = NULL; |
| 316 | cur_action = 0; |
| 317 | need_default = 1; |
| 318 | } |
| 319 | |
| 320 | /* --- Options --- */ |
| 321 | else if (strcmp(arg, "-follow") == 0) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 322 | dereference = TRUE; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 323 | #if ENABLE_FEATURE_FIND_XDEV |
| 324 | else if (strcmp(arg, "-xdev") == 0) { |
| 325 | struct stat stbuf; |
| 326 | |
| 327 | xdev_count = firstopt - 1; |
| 328 | xdev_dev = xmalloc(xdev_count * sizeof(dev_t)); |
| 329 | for (j = 1; j < firstopt; i++) { |
| 330 | /* not xstat(): shouldn't bomd out on |
| 331 | * "find not_exist exist -xdev" */ |
| 332 | if (stat(argv[j], &stbuf)) stbuf.st_dev = -1L; |
| 333 | xdev_dev[j-1] = stbuf.st_dev; |
| 334 | } |
| 335 | } |
| 336 | #endif |
| 337 | /* --- Tests and actions --- */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 338 | else if (strcmp(arg, "-print") == 0) { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 339 | need_default = 0; |
| 340 | (void) ALLOC_ACTION(print); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 341 | } |
| 342 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 343 | else if (strcmp(arg, "-print0") == 0) { |
| 344 | need_default = 0; |
| 345 | (void) ALLOC_ACTION(print0); |
| 346 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 347 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 348 | else if (strcmp(arg, "-name") == 0) { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 349 | action_name *ap; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 350 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 351 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 352 | ap = ALLOC_ACTION(name); |
| 353 | ap->pattern = arg1; |
| 354 | } |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 355 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 356 | else if (strcmp(arg, "-type") == 0) { |
| 357 | action_type *ap; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 358 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 359 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 360 | ap = ALLOC_ACTION(type); |
| 361 | ap->type_mask = find_type(arg1); |
| 362 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 363 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 364 | #if ENABLE_FEATURE_FIND_PERM |
| 365 | /* TODO: |
| 366 | * -perm mode File's permission bits are exactly mode (octal or symbolic). |
| 367 | * Symbolic modes use mode 0 as a point of departure. |
| 368 | * -perm -mode All of the permission bits mode are set for the file. |
| 369 | * -perm +mode Any of the permission bits mode are set for the file. |
| 370 | */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 371 | else if (strcmp(arg, "-perm") == 0) { |
| 372 | action_perm *ap; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 373 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 374 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 375 | ap = ALLOC_ACTION(perm); |
| 376 | ap->perm_mask = xstrtol_range(arg1, 8, 0, 07777); |
| 377 | ap->perm_char = arg1[0]; |
| 378 | if (ap->perm_char == '-') |
| 379 | ap->perm_mask = -ap->perm_mask; |
| 380 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 381 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 382 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 383 | else if (strcmp(arg, "-mtime") == 0) { |
| 384 | action_mtime *ap; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 385 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 386 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 387 | ap = ALLOC_ACTION(mtime); |
| 388 | ap->mtime_days = xatol(arg1); |
| 389 | ap->mtime_char = arg1[0]; |
| 390 | if (ap->mtime_char == '-') |
| 391 | ap->mtime_days = -ap->mtime_days; |
| 392 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 393 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 394 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 395 | else if (strcmp(arg, "-mmin") == 0) { |
| 396 | action_mmin *ap; |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 397 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 398 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 399 | ap = ALLOC_ACTION(mmin); |
| 400 | ap->mmin_mins = xatol(arg1); |
| 401 | ap->mmin_char = arg1[0]; |
| 402 | if (ap->mmin_char == '-') |
| 403 | ap->mmin_mins = -ap->mmin_mins; |
| 404 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 405 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 406 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 407 | else if (strcmp(arg, "-newer") == 0) { |
| 408 | action_newer *ap; |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 409 | struct stat stat_newer; |
| 410 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 411 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 412 | xstat(arg1, &stat_newer); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 413 | ap = ALLOC_ACTION(newer); |
| 414 | ap->newer_mtime = stat_newer.st_mtime; |
| 415 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 416 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 417 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 418 | else if (strcmp(arg, "-inum") == 0) { |
| 419 | action_inum *ap; |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 420 | if (++i == argc) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 421 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 422 | ap = ALLOC_ACTION(inum); |
| 423 | ap->inode_num = xatoul(arg1); |
| 424 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 425 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 426 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 427 | else if (strcmp(arg, "-exec") == 0) { |
| 428 | action_exec *ap; |
| 429 | need_default = 0; |
| 430 | ap = ALLOC_ACTION(exec); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 431 | i++; /* now: argv[i] is the first arg after -exec */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 432 | ap->exec_argv = &argv[i]; |
| 433 | ap->exec_argc = i; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 434 | while (1) { |
| 435 | if (i == argc) /* did not see ';' till end */ |
| 436 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 437 | if (argv[i][0] == ';' && argv[i][1] == '\0') |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 438 | break; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 439 | i++; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 440 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 441 | ap->exec_argc = i - ap->exec_argc; /* number of --exec arguments */ |
| 442 | if (ap->exec_argc == 0) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 443 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 444 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); |
| 445 | j = ap->exec_argc; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 446 | while (j--) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 447 | ap->subst_count[j] = count_subst(ap->exec_argv[j]); |
| 448 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 449 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 450 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 451 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 452 | } |
| 453 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame^] | 454 | if (need_default) |
| 455 | (void) ALLOC_ACTION(print); |
| 456 | |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 457 | for (i = 1; i < firstopt; i++) { |
| 458 | if (!recursive_action(argv[i], |
| 459 | TRUE, // recurse |
| 460 | dereference, // follow links |
| 461 | FALSE, // depth first |
| 462 | fileAction, // file action |
| 463 | fileAction, // dir action |
| 464 | NULL, // user data |
| 465 | 0)) // depth |
| 466 | status = EXIT_FAILURE; |
| 467 | } |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 468 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 469 | } |