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 | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 49 | #include "busybox.h" |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 50 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 51 | USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;) |
| 52 | USE_FEATURE_FIND_XDEV(static int xdev_count;) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 53 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 54 | typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 56 | typedef struct { |
| 57 | action_fp f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 58 | #if ENABLE_FEATURE_FIND_NOT |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 59 | bool invert; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 60 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 61 | } action; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 62 | #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name; |
| 63 | #define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) |
| 64 | ACTS(print) |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 65 | ACTS(name, const char *pattern;) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 66 | USE_FEATURE_FIND_PRINT0(ACTS(print0)) |
| 67 | USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 68 | USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;)) |
| 69 | USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;)) |
| 70 | USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;)) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 71 | USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) |
| 72 | USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 73 | USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned int *subst_count; int exec_argc;)) |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 74 | USE_FEATURE_FIND_USER( ACTS(user, int uid;)) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 75 | USE_DESKTOP( ACTS(paren, action ***subexpr;)) |
Denis Vlasenko | 5fa7148 | 2006-12-12 22:31:15 +0000 | [diff] [blame] | 76 | USE_DESKTOP( ACTS(size, off_t size;)) |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 77 | USE_DESKTOP( ACTS(prune)) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 78 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 79 | static action ***actions; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 80 | static bool need_print = 1; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 81 | |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 82 | |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 83 | #if ENABLE_FEATURE_FIND_EXEC |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 84 | static unsigned int count_subst(const char *str) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 85 | { |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 86 | unsigned int count = 0; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 87 | while ((str = strstr(str, "{}"))) { |
| 88 | count++; |
| 89 | str++; |
| 90 | } |
| 91 | return count; |
| 92 | } |
| 93 | |
| 94 | |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 95 | static char* subst(const char *src, unsigned int count, const char* filename) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 96 | { |
| 97 | char *buf, *dst, *end; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 98 | size_t flen = strlen(filename); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 99 | /* we replace each '{}' with filename: growth by strlen-2 */ |
| 100 | buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); |
| 101 | while ((end = strstr(src, "{}"))) { |
| 102 | memcpy(dst, src, end - src); |
| 103 | dst += end - src; |
| 104 | src = end + 2; |
| 105 | memcpy(dst, filename, flen); |
| 106 | dst += flen; |
| 107 | } |
| 108 | strcpy(dst, src); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 109 | return buf; |
| 110 | } |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 111 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 112 | |
| 113 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 114 | static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf) |
| 115 | { |
| 116 | int cur_group; |
| 117 | int cur_action; |
| 118 | int rc = TRUE; |
| 119 | action **app, *ap; |
| 120 | |
| 121 | cur_group = -1; |
| 122 | while ((app = appp[++cur_group])) { |
| 123 | cur_action = -1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 124 | while (1) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 125 | ap = app[++cur_action]; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 126 | if (!ap) { |
| 127 | /* all actions in group were successful */ |
| 128 | return rc; |
| 129 | } |
| 130 | rc = ap->f(fileName, statbuf, ap); |
| 131 | #if ENABLE_FEATURE_FIND_NOT |
| 132 | if (ap->invert) rc = !rc; |
| 133 | #endif |
| 134 | if (!rc) { |
| 135 | /* current group failed, try next */ |
| 136 | break; |
| 137 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | return rc; |
| 141 | } |
| 142 | |
| 143 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 144 | ACTF(name) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 145 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 146 | const char *tmp = strrchr(fileName, '/'); |
| 147 | if (tmp == NULL) |
| 148 | tmp = fileName; |
| 149 | else |
| 150 | tmp++; |
| 151 | return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0; |
| 152 | } |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 153 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 154 | ACTF(type) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 155 | { |
Denis Vlasenko | 31c65f2 | 2006-10-31 23:39:37 +0000 | [diff] [blame] | 156 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 157 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 158 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 159 | #if ENABLE_FEATURE_FIND_PERM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 160 | ACTF(perm) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 161 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 162 | /* -perm +mode: at least one of perm_mask bits are set */ |
| 163 | if (ap->perm_char == '+') |
| 164 | return (statbuf->st_mode & ap->perm_mask) != 0; |
| 165 | /* -perm -mode: all of perm_mask are set */ |
| 166 | if (ap->perm_char == '-') |
| 167 | return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask; |
| 168 | /* -perm mode: file mode must match perm_mask */ |
| 169 | return (statbuf->st_mode & 07777) == ap->perm_mask; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 170 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 171 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 172 | #if ENABLE_FEATURE_FIND_MTIME |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 173 | ACTF(mtime) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 174 | { |
| 175 | time_t file_age = time(NULL) - statbuf->st_mtime; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 176 | time_t mtime_secs = ap->mtime_days * 24*60*60; |
| 177 | if (ap->mtime_char == '+') |
| 178 | return file_age >= mtime_secs + 24*60*60; |
| 179 | if (ap->mtime_char == '-') |
| 180 | return file_age < mtime_secs; |
| 181 | /* just numeric mtime */ |
| 182 | return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 183 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 184 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 185 | #if ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 186 | ACTF(mmin) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 187 | { |
| 188 | time_t file_age = time(NULL) - statbuf->st_mtime; |
| 189 | time_t mmin_secs = ap->mmin_mins * 60; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 190 | if (ap->mmin_char == '+') |
| 191 | return file_age >= mmin_secs + 60; |
| 192 | if (ap->mmin_char == '-') |
| 193 | return file_age < mmin_secs; |
| 194 | /* just numeric mmin */ |
| 195 | return file_age >= mmin_secs && file_age < (mmin_secs + 60); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 196 | } |
Paul Fox | 72d1a23 | 2006-01-13 21:05:41 +0000 | [diff] [blame] | 197 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 198 | #if ENABLE_FEATURE_FIND_NEWER |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 199 | ACTF(newer) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 200 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 201 | return (ap->newer_mtime < statbuf->st_mtime); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 202 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 203 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 204 | #if ENABLE_FEATURE_FIND_INUM |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 205 | ACTF(inum) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 206 | { |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 207 | return (statbuf->st_ino == ap->inode_num); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 208 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 209 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 210 | #if ENABLE_FEATURE_FIND_EXEC |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 211 | ACTF(exec) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 212 | { |
| 213 | int i, rc; |
| 214 | char *argv[ap->exec_argc+1]; |
| 215 | for (i = 0; i < ap->exec_argc; i++) |
| 216 | argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); |
| 217 | argv[i] = NULL; /* terminate the list */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 218 | rc = wait4pid(spawn(argv)); |
Denis Vlasenko | 8f6c792 | 2006-12-23 00:49:10 +0000 | [diff] [blame] | 219 | if (rc) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 220 | bb_perror_msg("%s", argv[0]); |
| 221 | for (i = 0; i < ap->exec_argc; i++) |
| 222 | free(argv[i]); |
| 223 | return rc == 0; /* return 1 if success */ |
| 224 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 225 | #endif |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 226 | |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 227 | #if ENABLE_FEATURE_FIND_USER |
| 228 | ACTF(user) |
| 229 | { |
| 230 | return (statbuf->st_uid == ap->uid); |
| 231 | } |
| 232 | #endif |
| 233 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 234 | #if ENABLE_FEATURE_FIND_PRINT0 |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 235 | ACTF(print0) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 236 | { |
| 237 | printf("%s%c", fileName, '\0'); |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 238 | return TRUE; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 239 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 240 | #endif |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 241 | |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 242 | ACTF(print) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 243 | { |
| 244 | puts(fileName); |
| 245 | return TRUE; |
| 246 | } |
| 247 | |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 248 | #if ENABLE_DESKTOP |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 249 | ACTF(paren) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 250 | { |
| 251 | return exec_actions(ap->subexpr, fileName, statbuf); |
| 252 | } |
Denis Vlasenko | 5fa7148 | 2006-12-12 22:31:15 +0000 | [diff] [blame] | 253 | |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 254 | /* |
| 255 | * -prune: if -depth is not given, return true and do not descend |
| 256 | * current dir; if -depth is given, return false with no effect. |
| 257 | * Example: |
| 258 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print |
| 259 | */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 260 | ACTF(prune) |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 261 | { |
| 262 | return SKIP; |
| 263 | } |
Denis Vlasenko | 5fa7148 | 2006-12-12 22:31:15 +0000 | [diff] [blame] | 264 | |
| 265 | ACTF(size) |
| 266 | { |
| 267 | return statbuf->st_size == ap->size; |
| 268 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 269 | #endif |
| 270 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 271 | |
| 272 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth) |
| 273 | { |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 274 | int i; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 275 | #ifdef CONFIG_FEATURE_FIND_XDEV |
| 276 | if (S_ISDIR(statbuf->st_mode) && xdev_count) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 277 | for (i = 0; i < xdev_count; i++) { |
| 278 | if (xdev_dev[i] != statbuf->st_dev) |
| 279 | return SKIP; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 282 | #endif |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 283 | i = exec_actions(actions, fileName, statbuf); |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 284 | /* Had no explicit -print[0] or -exec? then print */ |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 285 | if (i && need_print) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 286 | puts(fileName); |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 287 | /* Cannot return 0: our caller, recursive_action(), |
| 288 | * will perror() and skip dirs (if called on dir) */ |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 289 | return i == 0 ? TRUE : i; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 292 | |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 293 | #if ENABLE_FEATURE_FIND_TYPE |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 294 | static int find_type(const char *type) |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 295 | { |
| 296 | int mask = 0; |
| 297 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 298 | if (*type == 'b') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 299 | mask = S_IFBLK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 300 | else if (*type == 'c') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 301 | mask = S_IFCHR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 302 | else if (*type == 'd') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 303 | mask = S_IFDIR; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 304 | else if (*type == 'p') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 305 | mask = S_IFIFO; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 306 | else if (*type == 'f') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 307 | mask = S_IFREG; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 308 | else if (*type == 'l') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 309 | mask = S_IFLNK; |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 310 | else if (*type == 's') |
Denis Vlasenko | 3a6755f | 2006-10-14 14:24:30 +0000 | [diff] [blame] | 311 | mask = S_IFSOCK; |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 312 | |
Bernhard Reutner-Fischer | 307d27d | 2007-03-29 13:56:02 +0000 | [diff] [blame^] | 313 | if (mask == 0 || *(type + 1) != '\0') |
Bernhard Reutner-Fischer | 19008b8 | 2006-06-07 20:17:41 +0000 | [diff] [blame] | 314 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 315 | |
| 316 | return mask; |
| 317 | } |
| 318 | #endif |
| 319 | |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 320 | #if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \ |
| 321 | || ENABLE_FEATURE_FIND_MMIN |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 322 | static const char* plus_minus_num(const char* str) |
| 323 | { |
| 324 | if (*str == '-' || *str == '+') |
| 325 | str++; |
| 326 | return str; |
| 327 | } |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 328 | #endif |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 329 | |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 330 | #define PARM_a 0 |
| 331 | #define PARM_o 1 |
| 332 | #define PARM_char_not (PARM_o + ENABLE_FEATURE_FIND_NOT) |
| 333 | #define PARM_print (PARM_char_not + 1) |
| 334 | #define PARM_print0 (PARM_print + ENABLE_FEATURE_FIND_PRINT0) |
| 335 | #define PARM_name (PARM_print0 + 1) |
| 336 | #define PARM_type (PARM_name + ENABLE_FEATURE_FIND_TYPE) |
| 337 | #define PARM_perm (PARM_type + ENABLE_FEATURE_FIND_PERM) |
| 338 | #define PARM_mtime (PARM_perm + ENABLE_FEATURE_FIND_MTIME) |
| 339 | #define PARM_mmin (PARM_mtime + ENABLE_FEATURE_FIND_MMIN) |
| 340 | #define PARM_newer (PARM_mmin + ENABLE_FEATURE_FIND_NEWER) |
| 341 | #define PARM_inum (PARM_newer + ENABLE_FEATURE_FIND_INUM) |
| 342 | #define PARM_exec (PARM_inum + ENABLE_FEATURE_FIND_EXEC) |
| 343 | #define PARM_user (PARM_exec + ENABLE_FEATURE_FIND_USER) |
| 344 | #if ENABLE_DESKTOP |
| 345 | #define PARM_and (PARM_user + 1) |
| 346 | #define PARM_or (PARM_and + 1) |
| 347 | #define PARM_not (PARM_or + ENABLE_FEATURE_FIND_NOT) |
| 348 | #define PARM_char_brace (PARM_not + 1) |
| 349 | #define PARM_prune (PARM_char_brace + 1) |
| 350 | #define PARM_size (PARM_prune + 1) |
| 351 | #endif |
Denis Vlasenko | 769d1e0 | 2007-01-22 23:04:27 +0000 | [diff] [blame] | 352 | static action*** parse_params(char **argv) |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 353 | { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 354 | action*** appp; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 355 | unsigned cur_group = 0; |
| 356 | unsigned cur_action = 0; |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 357 | USE_FEATURE_FIND_NOT( bool invert_flag = 0; ) |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 358 | const char * const params[] = { |
| 359 | "-a", |
| 360 | "-o", |
| 361 | #if ENABLE_FEATURE_FIND_NOT |
| 362 | "!", |
| 363 | #endif |
| 364 | "-print", |
| 365 | #if ENABLE_FEATURE_FIND_PRINT0 |
| 366 | "-print0", |
| 367 | #endif |
| 368 | "-name", |
| 369 | #if ENABLE_FEATURE_FIND_TYPE |
| 370 | "-type", |
| 371 | #endif |
| 372 | #if ENABLE_FEATURE_FIND_PERM |
| 373 | "-perm", |
| 374 | #endif |
| 375 | #if ENABLE_FEATURE_FIND_MTIME |
| 376 | "-mtime", |
| 377 | #endif |
| 378 | #if ENABLE_FEATURE_FIND_MMIN |
| 379 | "-mmin", |
| 380 | #endif |
| 381 | #if ENABLE_FEATURE_FIND_NEWER |
| 382 | "-newer", |
| 383 | #endif |
| 384 | #if ENABLE_FEATURE_FIND_INUM |
| 385 | "-inum", |
| 386 | #endif |
| 387 | #if ENABLE_FEATURE_FIND_EXEC |
| 388 | "-exec", |
| 389 | #endif |
| 390 | #if ENABLE_FEATURE_FIND_USER |
| 391 | "-user", |
| 392 | #endif |
| 393 | #if ENABLE_DESKTOP |
| 394 | "-and", |
| 395 | "-or", |
| 396 | # if ENABLE_FEATURE_FIND_NOT |
| 397 | "-not", |
| 398 | # endif |
| 399 | "(", |
| 400 | "-prune", |
| 401 | "-size", |
| 402 | #endif |
| 403 | NULL |
| 404 | }; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 405 | action* alloc_action(int sizeof_struct, action_fp f) |
| 406 | { |
| 407 | action *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 408 | appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp)); |
| 409 | appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct); |
| 410 | appp[cur_group][cur_action] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 411 | ap->f = f; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 412 | USE_FEATURE_FIND_NOT( ap->invert = invert_flag; ) |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 413 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 414 | return ap; |
| 415 | } |
| 416 | #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] | 417 | |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 418 | appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 419 | |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 420 | /* Actions have side effects and return a true or false value |
| 421 | * We implement: -print, -print0, -exec |
| 422 | * |
| 423 | * The rest are tests. |
| 424 | * |
| 425 | * Tests and actions are grouped by operators |
| 426 | * ( expr ) Force precedence |
| 427 | * ! expr True if expr is false |
| 428 | * -not expr Same as ! expr |
| 429 | * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false |
| 430 | * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true |
| 431 | * expr1 , expr2 List; both expr1 and expr2 are always evaluated |
| 432 | * We implement: (), -a, -o |
| 433 | */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 434 | while (*argv) { |
Denis Vlasenko | 434ad54 | 2007-01-27 13:45:17 +0000 | [diff] [blame] | 435 | const char *arg = argv[0]; |
| 436 | const char *arg1 = argv[1]; |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 437 | int parm = index_in_str_array(params, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 438 | /* --- Operators --- */ |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 439 | if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) |
| 440 | { |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 441 | /* no further special handling required */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 442 | } |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 443 | else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) |
| 444 | { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 445 | /* start new OR group */ |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 446 | cur_group++; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 447 | appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 448 | /*appp[cur_group] = NULL; - already NULL */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 449 | appp[cur_group+1] = NULL; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 450 | cur_action = 0; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 451 | } |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 452 | #if ENABLE_FEATURE_FIND_NOT |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 453 | else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) |
| 454 | { |
Denis Vlasenko | b292264 | 2007-02-06 17:38:29 +0000 | [diff] [blame] | 455 | /* also handles "find ! ! -name 'foo*'" */ |
| 456 | invert_flag ^= 1; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 457 | } |
| 458 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 459 | |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 460 | /* --- Tests and actions --- */ |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 461 | else if (parm == PARM_print) |
| 462 | { |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 463 | need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 464 | /* GNU find ignores '!' here: "find ! -print" */ |
| 465 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 466 | (void) ALLOC_ACTION(print); |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 467 | } |
| 468 | #if ENABLE_FEATURE_FIND_PRINT0 |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 469 | else if (parm == PARM_print0) |
| 470 | { |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 471 | need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 472 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 473 | (void) ALLOC_ACTION(print0); |
| 474 | } |
Paul Fox | d738429 | 2006-05-12 14:47:20 +0000 | [diff] [blame] | 475 | #endif |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 476 | else if (parm == PARM_name) |
| 477 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 478 | action_name *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 479 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 480 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 481 | ap = ALLOC_ACTION(name); |
| 482 | ap->pattern = arg1; |
| 483 | } |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 484 | #if ENABLE_FEATURE_FIND_TYPE |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 485 | else if (parm == PARM_type) |
| 486 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 487 | action_type *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 488 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 489 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 490 | ap = ALLOC_ACTION(type); |
| 491 | ap->type_mask = find_type(arg1); |
| 492 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 493 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 494 | #if ENABLE_FEATURE_FIND_PERM |
| 495 | /* TODO: |
| 496 | * -perm mode File's permission bits are exactly mode (octal or symbolic). |
| 497 | * Symbolic modes use mode 0 as a point of departure. |
| 498 | * -perm -mode All of the permission bits mode are set for the file. |
| 499 | * -perm +mode Any of the permission bits mode are set for the file. |
| 500 | */ |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 501 | else if (parm == PARM_perm) |
| 502 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 503 | action_perm *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 504 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 505 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 506 | ap = ALLOC_ACTION(perm); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 507 | ap->perm_char = arg1[0]; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 508 | arg1 = plus_minus_num(arg1); |
| 509 | ap->perm_mask = 0; |
| 510 | if (!bb_parse_mode(arg1, &ap->perm_mask)) |
| 511 | bb_error_msg_and_die("invalid mode: %s", arg1); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 512 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 513 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 514 | #if ENABLE_FEATURE_FIND_MTIME |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 515 | else if (parm == PARM_mtime) |
| 516 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 517 | action_mtime *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 518 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 519 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 520 | ap = ALLOC_ACTION(mtime); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 521 | ap->mtime_char = arg1[0]; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 522 | ap->mtime_days = xatoul(plus_minus_num(arg1)); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 523 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 524 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 525 | #if ENABLE_FEATURE_FIND_MMIN |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 526 | else if (parm == PARM_mmin) |
| 527 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 528 | action_mmin *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 529 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 530 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 531 | ap = ALLOC_ACTION(mmin); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 532 | ap->mmin_char = arg1[0]; |
Denis Vlasenko | fc7f922 | 2007-01-26 23:00:05 +0000 | [diff] [blame] | 533 | ap->mmin_mins = xatoul(plus_minus_num(arg1)); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 534 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 535 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 536 | #if ENABLE_FEATURE_FIND_NEWER |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 537 | else if (parm == PARM_newer) |
| 538 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 539 | action_newer *ap; |
Eric Andersen | 97d86f2 | 2003-01-23 05:27:42 +0000 | [diff] [blame] | 540 | struct stat stat_newer; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 541 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 542 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 543 | xstat(arg1, &stat_newer); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 544 | ap = ALLOC_ACTION(newer); |
| 545 | ap->newer_mtime = stat_newer.st_mtime; |
| 546 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 547 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 548 | #if ENABLE_FEATURE_FIND_INUM |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 549 | else if (parm == PARM_inum) |
| 550 | { |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 551 | action_inum *ap; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 552 | if (!*++argv) |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 553 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 554 | ap = ALLOC_ACTION(inum); |
| 555 | ap->inode_num = xatoul(arg1); |
| 556 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 557 | #endif |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 558 | #if ENABLE_FEATURE_FIND_EXEC |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 559 | else if (parm == PARM_exec) |
| 560 | { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 561 | int i; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 562 | action_exec *ap; |
Denis Vlasenko | a3b4fed | 2006-10-31 03:20:13 +0000 | [diff] [blame] | 563 | need_print = 0; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 564 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 565 | ap = ALLOC_ACTION(exec); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 566 | ap->exec_argv = ++argv; /* first arg after -exec */ |
| 567 | ap->exec_argc = 0; |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 568 | while (1) { |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 569 | if (!*argv) /* did not see ';' util end */ |
Denis Vlasenko | df0553b | 2006-10-29 00:21:47 +0000 | [diff] [blame] | 570 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 571 | if (LONE_CHAR(argv[0], ';')) |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 572 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 573 | argv++; |
| 574 | ap->exec_argc++; |
Rob Landley | 5d3a0e8 | 2005-10-04 03:34:39 +0000 | [diff] [blame] | 575 | } |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 576 | if (ap->exec_argc == 0) |
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->subst_count = xmalloc(ap->exec_argc * sizeof(int)); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 579 | i = ap->exec_argc; |
| 580 | while (i--) |
| 581 | ap->subst_count[i] = count_subst(ap->exec_argv[i]); |
| 582 | } |
| 583 | #endif |
Denis Vlasenko | 4c97863 | 2007-02-03 03:31:13 +0000 | [diff] [blame] | 584 | #if ENABLE_FEATURE_FIND_USER |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 585 | else if (parm == PARM_user) |
| 586 | { |
Denis Vlasenko | a100707 | 2007-02-02 01:17:52 +0000 | [diff] [blame] | 587 | action_user *ap; |
| 588 | if (!*++argv) |
| 589 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 590 | ap = ALLOC_ACTION(user); |
| 591 | ap->uid = bb_strtou(arg1, NULL, 10); |
| 592 | if (errno) |
| 593 | ap->uid = xuname2uid(arg1); |
| 594 | } |
| 595 | #endif |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 596 | #if ENABLE_DESKTOP |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 597 | else if (parm == PARM_char_brace) |
| 598 | { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 599 | action_paren *ap; |
| 600 | char **endarg; |
Bernhard Reutner-Fischer | 10b7996 | 2007-02-04 16:09:04 +0000 | [diff] [blame] | 601 | unsigned nested = 1; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 602 | |
| 603 | endarg = argv; |
| 604 | while (1) { |
| 605 | if (!*++endarg) |
| 606 | bb_error_msg_and_die("unpaired '('"); |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 607 | if (LONE_CHAR(*endarg, '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 608 | nested++; |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 609 | else if (LONE_CHAR(*endarg, ')') && !--nested) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 610 | *endarg = NULL; |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | ap = ALLOC_ACTION(paren); |
| 615 | ap->subexpr = parse_params(argv + 1); |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 616 | *endarg = (char*) ")"; /* restore NULLed parameter */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 617 | argv = endarg; |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 618 | } |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 619 | else if (parm == PARM_prune) |
| 620 | { |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 621 | USE_FEATURE_FIND_NOT( invert_flag = 0; ) |
Denis Vlasenko | 5f18e7c | 2006-10-31 03:21:02 +0000 | [diff] [blame] | 622 | (void) ALLOC_ACTION(prune); |
| 623 | } |
Bernhard Reutner-Fischer | 557b458 | 2007-03-29 13:55:17 +0000 | [diff] [blame] | 624 | else if (parm == PARM_size) |
| 625 | { |
Denis Vlasenko | 5fa7148 | 2006-12-12 22:31:15 +0000 | [diff] [blame] | 626 | action_size *ap; |
| 627 | if (!*++argv) |
| 628 | bb_error_msg_and_die(bb_msg_requires_arg, arg); |
| 629 | ap = ALLOC_ACTION(size); |
| 630 | ap->size = XATOOFF(arg1); |
| 631 | } |
Rob Landley | cee605c | 2005-10-06 16:39:17 +0000 | [diff] [blame] | 632 | #endif |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 633 | else |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 634 | bb_show_usage(); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 635 | argv++; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 636 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 637 | return appp; |
| 638 | #undef ALLOC_ACTION |
| 639 | } |
| 640 | |
| 641 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 642 | int find_main(int argc, char **argv); |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 643 | int find_main(int argc, char **argv) |
| 644 | { |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 645 | bool dereference = FALSE; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 646 | char *arg; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 647 | char **argp; |
| 648 | int i, firstopt, status = EXIT_SUCCESS; |
| 649 | |
| 650 | for (firstopt = 1; firstopt < argc; firstopt++) { |
| 651 | if (argv[firstopt][0] == '-') |
| 652 | break; |
Denis Vlasenko | cf787cf | 2007-02-04 17:11:25 +0000 | [diff] [blame] | 653 | if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!')) |
| 654 | break; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 655 | #if ENABLE_DESKTOP |
Denis Vlasenko | 9ac9e55 | 2006-12-23 15:58:11 +0000 | [diff] [blame] | 656 | if (LONE_CHAR(argv[firstopt], '(')) |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 657 | break; |
| 658 | #endif |
| 659 | } |
| 660 | if (firstopt == 1) { |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 661 | argv[0] = (char*)"."; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 662 | argv--; |
| 663 | firstopt++; |
| 664 | } |
| 665 | |
Bernhard Reutner-Fischer | 32773ac | 2007-03-29 13:54:17 +0000 | [diff] [blame] | 666 | /* All options always return true. They always take effect |
| 667 | * rather than being processed only when their place in the |
| 668 | * expression is reached. |
| 669 | * We implement: -follow, -xdev |
| 670 | */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 671 | /* Process options, and replace then with -a */ |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 672 | /* (-a will be ignored by recursive parser later) */ |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 673 | argp = &argv[firstopt]; |
Denis Vlasenko | 9225854 | 2006-11-01 10:25:35 +0000 | [diff] [blame] | 674 | while ((arg = argp[0])) { |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 675 | if (strcmp(arg, "-follow") == 0) { |
| 676 | dereference = TRUE; |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 677 | argp[0] = (char*)"-a"; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 678 | } |
| 679 | #if ENABLE_FEATURE_FIND_XDEV |
| 680 | else if (strcmp(arg, "-xdev") == 0) { |
| 681 | struct stat stbuf; |
| 682 | if (!xdev_count) { |
| 683 | xdev_count = firstopt - 1; |
| 684 | xdev_dev = xmalloc(xdev_count * sizeof(dev_t)); |
| 685 | for (i = 1; i < firstopt; i++) { |
| 686 | /* not xstat(): shouldn't bomb out on |
| 687 | * "find not_exist exist -xdev" */ |
Denis Vlasenko | c9d34da | 2007-01-03 03:15:58 +0000 | [diff] [blame] | 688 | if (stat(argv[i], &stbuf)) |
| 689 | stbuf.st_dev = -1L; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 690 | xdev_dev[i-1] = stbuf.st_dev; |
| 691 | } |
| 692 | } |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 693 | argp[0] = (char*)"-a"; |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 694 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 695 | #endif |
Denis Vlasenko | c9d34da | 2007-01-03 03:15:58 +0000 | [diff] [blame] | 696 | argp++; |
Denis Vlasenko | 666da5e | 2006-12-26 18:17:42 +0000 | [diff] [blame] | 697 | } |
Denis Vlasenko | 6191a7a | 2006-10-30 02:10:47 +0000 | [diff] [blame] | 698 | |
| 699 | actions = parse_params(&argv[firstopt]); |
Denis Vlasenko | 5d499e1 | 2006-10-29 19:07:01 +0000 | [diff] [blame] | 700 | |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 701 | for (i = 1; i < firstopt; i++) { |
| 702 | if (!recursive_action(argv[i], |
Bernhard Reutner-Fischer | 3e816c1 | 2007-03-29 10:30:50 +0000 | [diff] [blame] | 703 | action_recurse|(1<<dereference), /* flags */ |
| 704 | fileAction, /* file action */ |
| 705 | fileAction, /* dir action */ |
| 706 | NULL, /* user data */ |
| 707 | 0)) /* depth */ |
Denis Vlasenko | e2fb719 | 2006-10-29 19:03:56 +0000 | [diff] [blame] | 708 | status = EXIT_FAILURE; |
| 709 | } |
Matt Kraai | 096370d | 2001-02-07 03:52:38 +0000 | [diff] [blame] | 710 | return status; |
Eric Andersen | 17d49ef | 1999-10-06 20:25:32 +0000 | [diff] [blame] | 711 | } |