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