blob: edb8482d80d7dccf8550d0ae5f5a4f826784feb8 [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
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000048#include "busybox.h"
Matt Kraai096370d2001-02-07 03:52:38 +000049#include <fnmatch.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;
58} action;
Denis Vlasenko92258542006-11-01 10:25:35 +000059#define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
60#define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
61 ACTS(print)
62 ACTS(name, char *pattern;)
63USE_FEATURE_FIND_PRINT0(ACTS(print0))
64USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
65USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; int perm_mask;))
66USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; int mtime_days;))
67USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; int mmin_mins;))
68USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
69USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
70USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; int *subst_count; int exec_argc;))
71USE_DESKTOP( ACTS(paren, action ***subexpr;))
72USE_DESKTOP( ACTS(prune))
Matt Kraai096370d2001-02-07 03:52:38 +000073
Denis Vlasenko5d499e12006-10-29 19:07:01 +000074static action ***actions;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +000075static int need_print = 1;
76
77static inline int one_char(const char* str, char c)
78{
79 return (str[0] == c && str[1] == '\0');
80}
Eric Andersen17d49ef1999-10-06 20:25:32 +000081
Rob Landley5d3a0e82005-10-04 03:34:39 +000082
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000083static int count_subst(const char *str)
84{
85 int count = 0;
86 while ((str = strstr(str, "{}"))) {
87 count++;
88 str++;
89 }
90 return count;
91}
92
93
94static char* subst(const char *src, int count, const char* filename)
95{
96 char *buf, *dst, *end;
97 int flen = strlen(filename);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +000098 /* we replace each '{}' with filename: growth by strlen-2 */
99 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
100 while ((end = strstr(src, "{}"))) {
101 memcpy(dst, src, end - src);
102 dst += end - src;
103 src = end + 2;
104 memcpy(dst, filename, flen);
105 dst += flen;
106 }
107 strcpy(dst, src);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000108 return buf;
109}
110
111
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000112static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
113{
114 int cur_group;
115 int cur_action;
116 int rc = TRUE;
117 action **app, *ap;
118
119 cur_group = -1;
120 while ((app = appp[++cur_group])) {
121 cur_action = -1;
122 do {
123 ap = app[++cur_action];
124 } while (ap && (rc = ap->f(fileName, statbuf, ap)));
125 if (!ap) {
126 /* all actions in group were successful */
127 break;
128 }
129 }
130 return rc;
131}
132
133
Denis Vlasenko92258542006-11-01 10:25:35 +0000134ACTF(name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000135{
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000136 const char *tmp = strrchr(fileName, '/');
137 if (tmp == NULL)
138 tmp = fileName;
139 else
140 tmp++;
141 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
142}
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000143#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko92258542006-11-01 10:25:35 +0000144ACTF(type)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000145{
Denis Vlasenko31c65f22006-10-31 23:39:37 +0000146 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000147}
Rob Landleycee605c2005-10-06 16:39:17 +0000148#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000149#if ENABLE_FEATURE_FIND_PERM
Denis Vlasenko92258542006-11-01 10:25:35 +0000150ACTF(perm)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000151{
152 return !((isdigit(ap->perm_char) && (statbuf->st_mode & 07777) == ap->perm_mask)
153 || (ap->perm_char == '-' && (statbuf->st_mode & ap->perm_mask) == ap->perm_mask)
154 || (ap->perm_char == '+' && (statbuf->st_mode & ap->perm_mask) != 0));
155}
Rob Landleycee605c2005-10-06 16:39:17 +0000156#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000157#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko92258542006-11-01 10:25:35 +0000158ACTF(mtime)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000159{
160 time_t file_age = time(NULL) - statbuf->st_mtime;
161 time_t mtime_secs = ap->mtime_days * 24 * 60 * 60;
162 return !((isdigit(ap->mtime_char) && file_age >= mtime_secs
163 && file_age < mtime_secs + 24 * 60 * 60)
164 || (ap->mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60)
165 || (ap->mtime_char == '-' && file_age < mtime_secs));
166}
Rob Landleycee605c2005-10-06 16:39:17 +0000167#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000168#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko92258542006-11-01 10:25:35 +0000169ACTF(mmin)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000170{
171 time_t file_age = time(NULL) - statbuf->st_mtime;
172 time_t mmin_secs = ap->mmin_mins * 60;
173 return !((isdigit(ap->mmin_char) && file_age >= mmin_secs
174 && file_age < mmin_secs + 60)
175 || (ap->mmin_char == '+' && file_age >= mmin_secs + 60)
176 || (ap->mmin_char == '-' && file_age < mmin_secs));
177}
Paul Fox72d1a232006-01-13 21:05:41 +0000178#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000179#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko92258542006-11-01 10:25:35 +0000180ACTF(newer)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000181{
182 return (ap->newer_mtime >= statbuf->st_mtime);
183}
Rob Landleycee605c2005-10-06 16:39:17 +0000184#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000185#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko92258542006-11-01 10:25:35 +0000186ACTF(inum)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000187{
188 return (statbuf->st_ino != ap->inode_num);
189}
Rob Landleycee605c2005-10-06 16:39:17 +0000190#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000191#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko92258542006-11-01 10:25:35 +0000192ACTF(exec)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000193{
194 int i, rc;
195 char *argv[ap->exec_argc+1];
196 for (i = 0; i < ap->exec_argc; i++)
197 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
198 argv[i] = NULL; /* terminate the list */
199 errno = 0;
200 rc = wait4pid(spawn(argv));
201 if (errno)
202 bb_perror_msg("%s", argv[0]);
203 for (i = 0; i < ap->exec_argc; i++)
204 free(argv[i]);
205 return rc == 0; /* return 1 if success */
206}
Rob Landleycee605c2005-10-06 16:39:17 +0000207#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000208
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000209#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko92258542006-11-01 10:25:35 +0000210ACTF(print0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000211{
212 printf("%s%c", fileName, '\0');
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000213 return TRUE;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000214}
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000215#endif
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000216
Denis Vlasenko92258542006-11-01 10:25:35 +0000217ACTF(print)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000218{
219 puts(fileName);
220 return TRUE;
221}
222
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000223#if ENABLE_DESKTOP
Denis Vlasenko92258542006-11-01 10:25:35 +0000224ACTF(paren)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000225{
226 return exec_actions(ap->subexpr, fileName, statbuf);
227}
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000228/*
229 * -prune: if -depth is not given, return true and do not descend
230 * current dir; if -depth is given, return false with no effect.
231 * Example:
232 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
233 */
Denis Vlasenko92258542006-11-01 10:25:35 +0000234ACTF(prune)
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000235{
236 return SKIP;
237}
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000238#endif
239
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000240
241static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
242{
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000243 int rc;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000244#ifdef CONFIG_FEATURE_FIND_XDEV
245 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
246 int i;
247 for (i = 0; i < xdev_count; i++) {
248 if (xdev_dev[i] != statbuf->st_dev)
249 return SKIP;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000250 }
251 }
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000252#endif
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000253 rc = exec_actions(actions, fileName, statbuf);
254 /* Had no explicit -print[0] or -exec? then print */
255 if (rc && need_print)
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000256 puts(fileName);
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000257 /* Cannot return 0: our caller, recursive_action(),
258 * will perror() and skip dirs (if called on dir) */
259 return rc == 0 ? TRUE : rc;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000260}
261
Eric Andersen17d49ef1999-10-06 20:25:32 +0000262
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000263#if ENABLE_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000264static int find_type(char *type)
265{
266 int mask = 0;
267
268 switch (type[0]) {
Denis Vlasenko3a6755f2006-10-14 14:24:30 +0000269 case 'b':
270 mask = S_IFBLK;
271 break;
272 case 'c':
273 mask = S_IFCHR;
274 break;
275 case 'd':
276 mask = S_IFDIR;
277 break;
278 case 'p':
279 mask = S_IFIFO;
280 break;
281 case 'f':
282 mask = S_IFREG;
283 break;
284 case 'l':
285 mask = S_IFLNK;
286 break;
287 case 's':
288 mask = S_IFSOCK;
289 break;
Matt Kraai096370d2001-02-07 03:52:38 +0000290 }
291
292 if (mask == 0 || type[1] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000293 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000294
295 return mask;
296}
297#endif
298
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000299action*** parse_params(char **argv)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000300{
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000301 action*** appp;
302 int cur_group = 0;
303 int cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000304
305 action* alloc_action(int sizeof_struct, action_fp f)
306 {
307 action *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000308 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
309 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
310 appp[cur_group][cur_action] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000311 ap->f = f;
312 return ap;
313 }
314#define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
Eric Andersen17d49ef1999-10-06 20:25:32 +0000315
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000316 appp = xzalloc(2 * sizeof(*appp)); /* appp[0],[1] == NULL */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000317
318// Actions have side effects and return a true or false value
319// We implement: -print, -print0, -exec
320
321// The rest are tests.
322
323// Tests and actions are grouped by operators
324// ( expr ) Force precedence
325// ! expr True if expr is false
326// -not expr Same as ! expr
327// expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
328// expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
329// expr1 , expr2 List; both expr1 and expr2 are always evaluated
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000330// We implement: (), -a, -o
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000331
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000332 while (*argv) {
333 char *arg = argv[0];
334 char *arg1 = argv[1];
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000335 /* --- Operators --- */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000336 if (strcmp(arg, "-a") == 0
337 USE_DESKTOP(|| strcmp(arg, "-and") == 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000338 ) {
339 /* no special handling required */
340 }
341 else if (strcmp(arg, "-o") == 0
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000342 USE_DESKTOP(|| strcmp(arg, "-or") == 0)
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000343 ) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000344 /* start new OR group */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000345 cur_group++;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000346 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
347 appp[cur_group] = NULL;
348 appp[cur_group+1] = NULL;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000349 cur_action = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000350 }
351
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000352 /* --- Tests and actions --- */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000353 else if (strcmp(arg, "-print") == 0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000354 need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000355 (void) ALLOC_ACTION(print);
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000356 }
357#if ENABLE_FEATURE_FIND_PRINT0
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000358 else if (strcmp(arg, "-print0") == 0) {
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000359 need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000360 (void) ALLOC_ACTION(print0);
361 }
Paul Foxd7384292006-05-12 14:47:20 +0000362#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000363 else if (strcmp(arg, "-name") == 0) {
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000364 action_name *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000365 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000366 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000367 ap = ALLOC_ACTION(name);
368 ap->pattern = arg1;
369 }
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000370#if ENABLE_FEATURE_FIND_TYPE
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000371 else if (strcmp(arg, "-type") == 0) {
372 action_type *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000373 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000374 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000375 ap = ALLOC_ACTION(type);
376 ap->type_mask = find_type(arg1);
377 }
Rob Landleycee605c2005-10-06 16:39:17 +0000378#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000379#if ENABLE_FEATURE_FIND_PERM
380/* TODO:
381 * -perm mode File's permission bits are exactly mode (octal or symbolic).
382 * Symbolic modes use mode 0 as a point of departure.
383 * -perm -mode All of the permission bits mode are set for the file.
384 * -perm +mode Any of the permission bits mode are set for the file.
385 */
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000386 else if (strcmp(arg, "-perm") == 0) {
387 action_perm *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000388 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000389 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000390 ap = ALLOC_ACTION(perm);
391 ap->perm_mask = xstrtol_range(arg1, 8, 0, 07777);
392 ap->perm_char = arg1[0];
393 if (ap->perm_char == '-')
394 ap->perm_mask = -ap->perm_mask;
395 }
Rob Landleycee605c2005-10-06 16:39:17 +0000396#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000397#if ENABLE_FEATURE_FIND_MTIME
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000398 else if (strcmp(arg, "-mtime") == 0) {
399 action_mtime *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000400 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000401 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000402 ap = ALLOC_ACTION(mtime);
403 ap->mtime_days = xatol(arg1);
404 ap->mtime_char = arg1[0];
405 if (ap->mtime_char == '-')
406 ap->mtime_days = -ap->mtime_days;
407 }
Rob Landleycee605c2005-10-06 16:39:17 +0000408#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000409#if ENABLE_FEATURE_FIND_MMIN
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000410 else if (strcmp(arg, "-mmin") == 0) {
411 action_mmin *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000412 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000413 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000414 ap = ALLOC_ACTION(mmin);
415 ap->mmin_mins = xatol(arg1);
416 ap->mmin_char = arg1[0];
417 if (ap->mmin_char == '-')
418 ap->mmin_mins = -ap->mmin_mins;
419 }
Rob Landleycee605c2005-10-06 16:39:17 +0000420#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000421#if ENABLE_FEATURE_FIND_NEWER
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000422 else if (strcmp(arg, "-newer") == 0) {
423 action_newer *ap;
Eric Andersen97d86f22003-01-23 05:27:42 +0000424 struct stat stat_newer;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000425 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000426 bb_error_msg_and_die(bb_msg_requires_arg, arg);
427 xstat(arg1, &stat_newer);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000428 ap = ALLOC_ACTION(newer);
429 ap->newer_mtime = stat_newer.st_mtime;
430 }
Rob Landleycee605c2005-10-06 16:39:17 +0000431#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000432#if ENABLE_FEATURE_FIND_INUM
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000433 else if (strcmp(arg, "-inum") == 0) {
434 action_inum *ap;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000435 if (!*++argv)
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000436 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000437 ap = ALLOC_ACTION(inum);
438 ap->inode_num = xatoul(arg1);
439 }
Rob Landleycee605c2005-10-06 16:39:17 +0000440#endif
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000441#if ENABLE_FEATURE_FIND_EXEC
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000442 else if (strcmp(arg, "-exec") == 0) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000443 int i;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000444 action_exec *ap;
Denis Vlasenkoa3b4fed2006-10-31 03:20:13 +0000445 need_print = 0;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000446 ap = ALLOC_ACTION(exec);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000447 ap->exec_argv = ++argv; /* first arg after -exec */
448 ap->exec_argc = 0;
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000449 while (1) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000450 if (!*argv) /* did not see ';' till end */
Denis Vlasenkodf0553b2006-10-29 00:21:47 +0000451 bb_error_msg_and_die(bb_msg_requires_arg, arg);
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000452 if (one_char(argv[0], ';'))
Rob Landley5d3a0e82005-10-04 03:34:39 +0000453 break;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000454 argv++;
455 ap->exec_argc++;
Rob Landley5d3a0e82005-10-04 03:34:39 +0000456 }
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000457 if (ap->exec_argc == 0)
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->subst_count = xmalloc(ap->exec_argc * sizeof(int));
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000460 i = ap->exec_argc;
461 while (i--)
462 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
463 }
464#endif
465#if ENABLE_DESKTOP
466 else if (one_char(arg, '(')) {
467 action_paren *ap;
468 char **endarg;
469 int nested = 1;
470
471 endarg = argv;
472 while (1) {
473 if (!*++endarg)
474 bb_error_msg_and_die("unpaired '('");
475 if (one_char(*endarg, '('))
476 nested++;
477 else if (one_char(*endarg, ')') && !--nested) {
478 *endarg = NULL;
479 break;
480 }
481 }
482 ap = ALLOC_ACTION(paren);
483 ap->subexpr = parse_params(argv + 1);
484 *endarg = ")"; /* restore NULLed parameter */
485 argv = endarg;
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000486 }
Denis Vlasenko5f18e7c2006-10-31 03:21:02 +0000487 else if (strcmp(arg, "-prune") == 0) {
488 (void) ALLOC_ACTION(prune);
489 }
Rob Landleycee605c2005-10-06 16:39:17 +0000490#endif
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000491 else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000492 bb_show_usage();
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000493 argv++;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000494 }
495
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000496 return appp;
497#undef ALLOC_ACTION
498}
499
500
501int find_main(int argc, char **argv)
502{
503 int dereference = FALSE;
Denis Vlasenko92258542006-11-01 10:25:35 +0000504 char *arg;
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000505 char **argp;
506 int i, firstopt, status = EXIT_SUCCESS;
507
508 for (firstopt = 1; firstopt < argc; firstopt++) {
509 if (argv[firstopt][0] == '-')
510 break;
511#if ENABLE_DESKTOP
512 if (one_char(argv[firstopt], '('))
513 break;
514#endif
515 }
516 if (firstopt == 1) {
517 argv[0] = ".";
518 argv--;
519 firstopt++;
520 }
521
522// All options always return true. They always take effect,
523// rather than being processed only when their place in the
524// expression is reached
525// We implement: -follow, -xdev
526
527 /* Process options, and replace then with -a */
Denis Vlasenko92258542006-11-01 10:25:35 +0000528 /* (-a will be ignored by recursive parser later) */
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000529 argp = &argv[firstopt];
Denis Vlasenko92258542006-11-01 10:25:35 +0000530 while ((arg = argp[0])) {
Denis Vlasenko6191a7a2006-10-30 02:10:47 +0000531 if (strcmp(arg, "-follow") == 0) {
532 dereference = TRUE;
533 argp[0] = "-a";
534 }
535#if ENABLE_FEATURE_FIND_XDEV
536 else if (strcmp(arg, "-xdev") == 0) {
537 struct stat stbuf;
538 if (!xdev_count) {
539 xdev_count = firstopt - 1;
540 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
541 for (i = 1; i < firstopt; i++) {
542 /* not xstat(): shouldn't bomb out on
543 * "find not_exist exist -xdev" */
544 if (stat(argv[i], &stbuf)) stbuf.st_dev = -1L;
545 xdev_dev[i-1] = stbuf.st_dev;
546 }
547 }
548 argp[0] = "-a";
549 }
550 argp++;
551 }
552#endif
553
554 actions = parse_params(&argv[firstopt]);
Denis Vlasenko5d499e12006-10-29 19:07:01 +0000555
Denis Vlasenkoe2fb7192006-10-29 19:03:56 +0000556 for (i = 1; i < firstopt; i++) {
557 if (!recursive_action(argv[i],
558 TRUE, // recurse
559 dereference, // follow links
560 FALSE, // depth first
561 fileAction, // file action
562 fileAction, // dir action
563 NULL, // user data
564 0)) // depth
565 status = EXIT_FAILURE;
566 }
Matt Kraai096370d2001-02-07 03:52:38 +0000567 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000568}