blob: e98d995a401244bb6ff6904081b83e2bbd529a3a [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen17d49ef1999-10-06 20:25:32 +00002/*
3 * Mini find implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Matt Kraai096370d2001-02-07 03:52:38 +00007 * Reworked by David Douthitt <n9ubh@callsign.net> and
8 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
Eric Andersen17d49ef1999-10-06 20:25:32 +00009 *
Rob Landleye9a7a622006-09-22 02:52:41 +000010 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
Eric Andersen17d49ef1999-10-06 20:25:32 +000011 */
12
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000013/* 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 Vlasenkodf0553b2006-10-29 00:21:47 +000026 * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
27 * file.txt file.txt
28 * file.txt
29 * /tmp
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +000030 * # find -name '*.c' -o -name '*.h'
31 * [shows files, *.c and *.h intermixed]
Denis Vlasenko5d499e12006-10-29 19:07:01 +000032 * # 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 Vlasenkoa3b4fed2006-10-31 03:20:13 +000038 *
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 Vlasenkodf0553b2006-10-29 00:21:47 +000046 */
47
Matt Kraai096370d2001-02-07 03:52:38 +000048#include <fnmatch.h>
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000049#include "busybox.h"
Eric Andersen17d49ef1999-10-06 20:25:32 +000050
Denis Vlasenko5d499e12006-10-29 19:07:01 +000051USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
52USE_FEATURE_FIND_XDEV(static int xdev_count;)
Matt Kraai096370d2001-02-07 03:52:38 +000053
Denis Vlasenko5d499e12006-10-29 19:07:01 +000054typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
Matt Kraai096370d2001-02-07 03:52:38 +000055
Denis Vlasenko5d499e12006-10-29 19:07:01 +000056typedef struct {
57 action_fp f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +000058#if ENABLE_FEATURE_FIND_NOT
59 smallint invert;
60#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +000061} action;
Denis Vlasenko92258542006-11-01 10:25:35 +000062#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 Vlasenko434ad542007-01-27 13:45:17 +000065 ACTS(name, const char *pattern;)
Denis Vlasenko92258542006-11-01 10:25:35 +000066USE_FEATURE_FIND_PRINT0(ACTS(print0))
67USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
Denis Vlasenkofc7f9222007-01-26 23:00:05 +000068USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
69USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
70USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
Denis Vlasenko92258542006-11-01 10:25:35 +000071USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
72USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +000073USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned int *subst_count; int exec_argc;))
Denis Vlasenkoa1007072007-02-02 01:17:52 +000074USE_FEATURE_FIND_USER( ACTS(user, int uid;))
Denis Vlasenko92258542006-11-01 10:25:35 +000075USE_DESKTOP( ACTS(paren, action ***subexpr;))
Denis Vlasenko5fa71482006-12-12 22:31:15 +000076USE_DESKTOP( ACTS(size, off_t size;))
Denis Vlasenko92258542006-11-01 10:25:35 +000077USE_DESKTOP( ACTS(prune))
Matt Kraai096370d2001-02-07 03:52:38 +000078
Denis Vlasenko5d499e12006-10-29 19:07:01 +000079static action ***actions;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +000080static smalluint need_print = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +000081
Rob Landley5d3a0e82005-10-04 03:34:39 +000082
Denis Vlasenko666da5e2006-12-26 18:17:42 +000083#if ENABLE_FEATURE_FIND_EXEC
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +000084static unsigned int count_subst(const char *str)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000085{
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +000086 unsigned int count = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000087 while ((str = strstr(str, "{}"))) {
88 count++;
89 str++;
90 }
91 return count;
92}
93
94
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +000095static char* subst(const char *src, unsigned int count, const char* filename)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000096{
97 char *buf, *dst, *end;
98 int flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000099 /* 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 Vlasenkodf0553b2006-10-29 00:21:47 +0000109 return buf;
110}
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000111#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000112
113
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000114static 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 Vlasenkocf787cf2007-02-04 17:11:25 +0000124 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000125 ap = app[++cur_action];
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000126 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 Vlasenko6191a7a2006-10-30 02:10:47 +0000138 }
139 }
140 return rc;
141}
142
143
Denis Vlasenko92258542006-11-01 10:25:35 +0000144ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000145{
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000146 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 Vlasenkodf0553b2006-10-29 00:21:47 +0000153#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000154ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000155{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000156 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000157}
Rob Landleycee605c2005-10-06 16:39:17 +0000158#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000159#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000160ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000161{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000162 /* -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 Vlasenko5d499e12006-10-29 19:07:01 +0000170}
Rob Landleycee605c2005-10-06 16:39:17 +0000171#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000172#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000173ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000174{
175 time_t file_age = time(NULL) - statbuf->st_mtime;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000176 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 Vlasenko5d499e12006-10-29 19:07:01 +0000183}
Rob Landleycee605c2005-10-06 16:39:17 +0000184#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000185#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000186ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000187{
188 time_t file_age = time(NULL) - statbuf->st_mtime;
189 time_t mmin_secs = ap->mmin_mins * 60;
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000190 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 Vlasenko5d499e12006-10-29 19:07:01 +0000196}
Paul Fox72d1a232006-01-13 21:05:41 +0000197#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000198#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000199ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000200{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000201 return (ap->newer_mtime < statbuf->st_mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000202}
Rob Landleycee605c2005-10-06 16:39:17 +0000203#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000204#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000205ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000206{
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000207 return (statbuf->st_ino == ap->inode_num);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000208}
Rob Landleycee605c2005-10-06 16:39:17 +0000209#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000210#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000211ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000212{
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 Vlasenko5d499e12006-10-29 19:07:01 +0000218 rc = wait4pid(spawn(argv));
Denis Vlasenko8f6c7922006-12-23 00:49:10 +0000219 if (rc)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000220 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 Landleycee605c2005-10-06 16:39:17 +0000225#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000226
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000227#if ENABLE_FEATURE_FIND_USER
228ACTF(user)
229{
230 return (statbuf->st_uid == ap->uid);
231}
232#endif
233
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000234#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000235ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000236{
237 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000238 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000239}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000240#endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000241
Denis Vlasenko92258542006-11-01 10:25:35 +0000242ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000243{
244 puts(fileName);
245 return TRUE;
246}
247
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000248#if ENABLE_DESKTOP
Denis Vlasenko92258542006-11-01 10:25:35 +0000249ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000250{
251 return exec_actions(ap->subexpr, fileName, statbuf);
252}
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000253
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000254/*
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 Vlasenko92258542006-11-01 10:25:35 +0000260ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000261{
262 return SKIP;
263}
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000264
265ACTF(size)
266{
267 return statbuf->st_size == ap->size;
268}
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000269#endif
270
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000271
272static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
273{
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000274 int rc;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000275#ifdef CONFIG_FEATURE_FIND_XDEV
276 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
277 int i;
278 for (i = 0; i < xdev_count; i++) {
279 if (xdev_dev[i] != statbuf->st_dev)
280 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000281 }
282 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000283#endif
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000284 rc = exec_actions(actions, fileName, statbuf);
285 /* Had no explicit -print[0] or -exec? then print */
286 if (rc && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000287 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000288 /* Cannot return 0: our caller, recursive_action(),
289 * will perror() and skip dirs (if called on dir) */
290 return rc == 0 ? TRUE : rc;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000291}
292
Eric Andersen17d49ef1999-10-06 20:25:32 +0000293
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000294#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko434ad542007-01-27 13:45:17 +0000295static int find_type(const char *type)
Matt Kraai096370d2001-02-07 03:52:38 +0000296{
297 int mask = 0;
298
299 switch (type[0]) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000300 case 'b':
301 mask = S_IFBLK;
302 break;
303 case 'c':
304 mask = S_IFCHR;
305 break;
306 case 'd':
307 mask = S_IFDIR;
308 break;
309 case 'p':
310 mask = S_IFIFO;
311 break;
312 case 'f':
313 mask = S_IFREG;
314 break;
315 case 'l':
316 mask = S_IFLNK;
317 break;
318 case 's':
319 mask = S_IFSOCK;
320 break;
Matt Kraai096370d2001-02-07 03:52:38 +0000321 }
322
323 if (mask == 0 || type[1] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000324 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000325
326 return mask;
327}
328#endif
329
Denis Vlasenko4c978632007-02-03 03:31:13 +0000330#if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
331 || ENABLE_FEATURE_FIND_MMIN
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000332static const char* plus_minus_num(const char* str)
333{
334 if (*str == '-' || *str == '+')
335 str++;
336 return str;
337}
Denis Vlasenko4c978632007-02-03 03:31:13 +0000338#endif
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000339
Denis Vlasenko769d1e02007-01-22 23:04:27 +0000340static action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000341{
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000342 action*** appp;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000343 unsigned cur_group = 0;
344 unsigned cur_action = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000345 USE_FEATURE_FIND_NOT( smallint invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000346
347 action* alloc_action(int sizeof_struct, action_fp f)
348 {
349 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000350 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
351 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
352 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000353 ap->f = f;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000354 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
Denis Vlasenkob2922642007-02-06 17:38:29 +0000355 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000356 return ap;
357 }
358#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000359
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000360 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000361
362// Actions have side effects and return a true or false value
363// We implement: -print, -print0, -exec
364
365// The rest are tests.
366
367// Tests and actions are grouped by operators
368// ( expr ) Force precedence
369// ! expr True if expr is false
370// -not expr Same as ! expr
371// expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
372// expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
373// expr1 , expr2 List; both expr1 and expr2 are always evaluated
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000374// We implement: (), -a, -o
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000375
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000376//XXX: TODO: Use index_in_str_array here
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000377 while (*argv) {
Denis Vlasenko434ad542007-01-27 13:45:17 +0000378 const char *arg = argv[0];
379 const char *arg1 = argv[1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000380 /* --- Operators --- */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000381 if (strcmp(arg, "-a") == 0
382 USE_DESKTOP(|| strcmp(arg, "-and") == 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000383 ) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000384 /* no further special handling required */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000385 }
386 else if (strcmp(arg, "-o") == 0
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000387 USE_DESKTOP(|| strcmp(arg, "-or") == 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000388 ) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000389 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000390 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000391 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000392 /*appp[cur_group] = NULL; - already NULL */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000393 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000394 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000395 }
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000396#if ENABLE_FEATURE_FIND_NOT
397 else if (LONE_CHAR(arg, '!')
398 USE_DESKTOP(|| strcmp(arg, "-not") == 0)
399 ) {
Denis Vlasenkob2922642007-02-06 17:38:29 +0000400 /* also handles "find ! ! -name 'foo*'" */
401 invert_flag ^= 1;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000402 }
403#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000404
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000405 /* --- Tests and actions --- */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000406 else if (strcmp(arg, "-print") == 0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000407 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000408 /* GNU find ignores '!' here: "find ! -print" */
409 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000410 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000411 }
412#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000413 else if (strcmp(arg, "-print0") == 0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000414 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000415 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000416 (void) ALLOC_ACTION(print0);
417 }
Paul Foxd7384292006-05-12 14:47:20 +0000418#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000419 else if (strcmp(arg, "-name") == 0) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000420 action_name *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000421 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000422 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000423 ap = ALLOC_ACTION(name);
424 ap->pattern = arg1;
425 }
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000426#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000427 else if (strcmp(arg, "-type") == 0) {
428 action_type *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000429 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000430 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000431 ap = ALLOC_ACTION(type);
432 ap->type_mask = find_type(arg1);
433 }
Rob Landleycee605c2005-10-06 16:39:17 +0000434#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000435#if ENABLE_FEATURE_FIND_PERM
436/* TODO:
437 * -perm mode File's permission bits are exactly mode (octal or symbolic).
438 * Symbolic modes use mode 0 as a point of departure.
439 * -perm -mode All of the permission bits mode are set for the file.
440 * -perm +mode Any of the permission bits mode are set for the file.
441 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000442 else if (strcmp(arg, "-perm") == 0) {
443 action_perm *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000444 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000445 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000446 ap = ALLOC_ACTION(perm);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000447 ap->perm_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000448 arg1 = plus_minus_num(arg1);
449 ap->perm_mask = 0;
450 if (!bb_parse_mode(arg1, &ap->perm_mask))
451 bb_error_msg_and_die("invalid mode: %s", arg1);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000452 }
Rob Landleycee605c2005-10-06 16:39:17 +0000453#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000454#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000455 else if (strcmp(arg, "-mtime") == 0) {
456 action_mtime *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000457 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000458 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000459 ap = ALLOC_ACTION(mtime);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000460 ap->mtime_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000461 ap->mtime_days = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000462 }
Rob Landleycee605c2005-10-06 16:39:17 +0000463#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000464#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000465 else if (strcmp(arg, "-mmin") == 0) {
466 action_mmin *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000467 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000468 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000469 ap = ALLOC_ACTION(mmin);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000470 ap->mmin_char = arg1[0];
Denis Vlasenkofc7f9222007-01-26 23:00:05 +0000471 ap->mmin_mins = xatoul(plus_minus_num(arg1));
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000472 }
Rob Landleycee605c2005-10-06 16:39:17 +0000473#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000474#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000475 else if (strcmp(arg, "-newer") == 0) {
476 action_newer *ap;
Eric Andersen97d86f22003-01-23 05:27:42 +0000477 struct stat stat_newer;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000478 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000479 bb_error_msg_and_die(bb_msg_requires_arg, arg);
480 xstat(arg1, &stat_newer);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000481 ap = ALLOC_ACTION(newer);
482 ap->newer_mtime = stat_newer.st_mtime;
483 }
Rob Landleycee605c2005-10-06 16:39:17 +0000484#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000485#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000486 else if (strcmp(arg, "-inum") == 0) {
487 action_inum *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000488 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000489 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000490 ap = ALLOC_ACTION(inum);
491 ap->inode_num = xatoul(arg1);
492 }
Rob Landleycee605c2005-10-06 16:39:17 +0000493#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000494#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000495 else if (strcmp(arg, "-exec") == 0) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000496 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000497 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000498 need_print = 0;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000499 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000500 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000501 ap->exec_argv = ++argv; /* first arg after -exec */
502 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000503 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000504 if (!*argv) /* did not see ';' till end */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000505 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000506 if (LONE_CHAR(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000507 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000508 argv++;
509 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000510 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000511 if (ap->exec_argc == 0)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000512 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000513 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000514 i = ap->exec_argc;
515 while (i--)
516 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
517 }
518#endif
Denis Vlasenko4c978632007-02-03 03:31:13 +0000519#if ENABLE_FEATURE_FIND_USER
Denis Vlasenkoa1007072007-02-02 01:17:52 +0000520 else if (strcmp(arg, "-user") == 0) {
521 action_user *ap;
522 if (!*++argv)
523 bb_error_msg_and_die(bb_msg_requires_arg, arg);
524 ap = ALLOC_ACTION(user);
525 ap->uid = bb_strtou(arg1, NULL, 10);
526 if (errno)
527 ap->uid = xuname2uid(arg1);
528 }
529#endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000530#if ENABLE_DESKTOP
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000531 else if (LONE_CHAR(arg, '(')) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000532 action_paren *ap;
533 char **endarg;
Bernhard Reutner-Fischer10b79962007-02-04 16:09:04 +0000534 unsigned nested = 1;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000535
536 endarg = argv;
537 while (1) {
538 if (!*++endarg)
539 bb_error_msg_and_die("unpaired '('");
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000540 if (LONE_CHAR(*endarg, '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000541 nested++;
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000542 else if (LONE_CHAR(*endarg, ')') && !--nested) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000543 *endarg = NULL;
544 break;
545 }
546 }
547 ap = ALLOC_ACTION(paren);
548 ap->subexpr = parse_params(argv + 1);
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000549 *endarg = (char*) ")"; /* restore NULLed parameter */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000550 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000551 }
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000552 else if (strcmp(arg, "-prune") == 0) {
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000553 USE_FEATURE_FIND_NOT( invert_flag = 0; )
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000554 (void) ALLOC_ACTION(prune);
555 }
Denis Vlasenko5fa71482006-12-12 22:31:15 +0000556 else if (strcmp(arg, "-size") == 0) {
557 action_size *ap;
558 if (!*++argv)
559 bb_error_msg_and_die(bb_msg_requires_arg, arg);
560 ap = ALLOC_ACTION(size);
561 ap->size = XATOOFF(arg1);
562 }
Rob Landleycee605c2005-10-06 16:39:17 +0000563#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000564 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000565 bb_show_usage();
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000566 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000567 }
568
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000569 return appp;
570#undef ALLOC_ACTION
571}
572
573
Denis Vlasenko06af2162007-02-03 17:28:39 +0000574int find_main(int argc, char **argv);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000575int find_main(int argc, char **argv)
576{
577 int dereference = FALSE;
Denis Vlasenko92258542006-11-01 10:25:35 +0000578 char *arg;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000579 char **argp;
580 int i, firstopt, status = EXIT_SUCCESS;
581
582 for (firstopt = 1; firstopt < argc; firstopt++) {
583 if (argv[firstopt][0] == '-')
584 break;
Denis Vlasenkocf787cf2007-02-04 17:11:25 +0000585 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
586 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000587#if ENABLE_DESKTOP
Denis Vlasenko9ac9e552006-12-23 15:58:11 +0000588 if (LONE_CHAR(argv[firstopt], '('))
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000589 break;
590#endif
591 }
592 if (firstopt == 1) {
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000593 argv[0] = (char*)".";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000594 argv--;
595 firstopt++;
596 }
597
598// All options always return true. They always take effect,
599// rather than being processed only when their place in the
600// expression is reached
601// We implement: -follow, -xdev
602
603 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000604 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000605 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000606 while ((arg = argp[0])) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000607 if (strcmp(arg, "-follow") == 0) {
608 dereference = TRUE;
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000609 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000610 }
611#if ENABLE_FEATURE_FIND_XDEV
612 else if (strcmp(arg, "-xdev") == 0) {
613 struct stat stbuf;
614 if (!xdev_count) {
615 xdev_count = firstopt - 1;
616 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
617 for (i = 1; i < firstopt; i++) {
618 /* not xstat(): shouldn't bomb out on
619 * "find not_exist exist -xdev" */
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000620 if (stat(argv[i], &stbuf))
621 stbuf.st_dev = -1L;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000622 xdev_dev[i-1] = stbuf.st_dev;
623 }
624 }
Denis Vlasenkoab2aea42007-01-29 22:51:58 +0000625 argp[0] = (char*)"-a";
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000626 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000627#endif
Denis Vlasenkoc9d34da2007-01-03 03:15:58 +0000628 argp++;
Denis Vlasenko666da5e2006-12-26 18:17:42 +0000629 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000630
631 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000632
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000633 for (i = 1; i < firstopt; i++) {
634 if (!recursive_action(argv[i],
635 TRUE, // recurse
636 dereference, // follow links
637 FALSE, // depth first
638 fileAction, // file action
639 fileAction, // dir action
640 NULL, // user data
641 0)) // depth
642 status = EXIT_FAILURE;
643 }
Matt Kraai096370d2001-02-07 03:52:38 +0000644 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000645}