1 /* vi: set sw=4 ts=4: */
3 * Mini find implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Reworked by David Douthitt <n9ubh@callsign.net> and
8 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
10 * Licensed under the GPL version 2, see the file LICENSE in this tarball.
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' '{} {}' ';'
25 * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ]))
26 * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';'
30 * # find -name '*.c' -o -name '*.h'
31 * [shows files, *.c and *.h intermixed]
32 * # find file.txt -name '*f*' -o -name '*t*'
34 * # find file.txt -name '*z*' -o -name '*t*'
36 * # find file.txt -name '*f*' -o -name '*z*'
39 * # find t z -name '*t*' -print -o -name '*z*'
41 * # find t z t z -name '*t*' -o -name '*z*' -print
44 * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print
51 USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;)
52 USE_FEATURE_FIND_XDEV(static int xdev_count;)
54 typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *);
58 #if ENABLE_FEATURE_FIND_NOT
62 #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name;
63 #define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap)
65 ACTS(name, const char *pattern;)
66 USE_FEATURE_FIND_PRINT0(ACTS(print0))
67 USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;))
68 USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; mode_t perm_mask;))
69 USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; unsigned mtime_days;))
70 USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; unsigned mmin_mins;))
71 USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;))
72 USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;))
73 USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; unsigned int *subst_count; int exec_argc;))
74 USE_FEATURE_FIND_USER( ACTS(user, uid_t uid;))
75 USE_FEATURE_FIND_GROUP( ACTS(group, gid_t gid;))
76 USE_FEATURE_FIND_PAREN( ACTS(paren, action ***subexpr;))
77 USE_FEATURE_FIND_SIZE( ACTS(size, off_t size;))
78 USE_FEATURE_FIND_PRUNE( ACTS(prune))
80 static action ***actions;
81 static bool need_print = 1;
82 static int recurse_flags = action_recurse;
84 #if ENABLE_FEATURE_FIND_EXEC
85 static unsigned int count_subst(const char *str)
87 unsigned int count = 0;
88 while ((str = strstr(str, "{}"))) {
96 static char* subst(const char *src, unsigned int count, const char* filename)
98 char *buf, *dst, *end;
99 size_t flen = strlen(filename);
100 /* we replace each '{}' with filename: growth by strlen-2 */
101 buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1);
102 while ((end = strstr(src, "{}"))) {
103 memcpy(dst, src, end - src);
106 memcpy(dst, filename, flen);
115 static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf)
123 while ((app = appp[++cur_group])) {
126 ap = app[++cur_action];
128 /* all actions in group were successful */
131 rc = ap->f(fileName, statbuf, ap);
132 #if ENABLE_FEATURE_FIND_NOT
133 if (ap->invert) rc = !rc;
136 /* current group failed, try next */
147 const char *tmp = strrchr(fileName, '/');
152 return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0;
154 #if ENABLE_FEATURE_FIND_TYPE
157 return ((statbuf->st_mode & S_IFMT) == ap->type_mask);
160 #if ENABLE_FEATURE_FIND_PERM
163 /* -perm +mode: at least one of perm_mask bits are set */
164 if (ap->perm_char == '+')
165 return (statbuf->st_mode & ap->perm_mask) != 0;
166 /* -perm -mode: all of perm_mask are set */
167 if (ap->perm_char == '-')
168 return (statbuf->st_mode & ap->perm_mask) == ap->perm_mask;
169 /* -perm mode: file mode must match perm_mask */
170 return (statbuf->st_mode & 07777) == ap->perm_mask;
173 #if ENABLE_FEATURE_FIND_MTIME
176 time_t file_age = time(NULL) - statbuf->st_mtime;
177 time_t mtime_secs = ap->mtime_days * 24*60*60;
178 if (ap->mtime_char == '+')
179 return file_age >= mtime_secs + 24*60*60;
180 if (ap->mtime_char == '-')
181 return file_age < mtime_secs;
182 /* just numeric mtime */
183 return file_age >= mtime_secs && file_age < (mtime_secs + 24*60*60);
186 #if ENABLE_FEATURE_FIND_MMIN
189 time_t file_age = time(NULL) - statbuf->st_mtime;
190 time_t mmin_secs = ap->mmin_mins * 60;
191 if (ap->mmin_char == '+')
192 return file_age >= mmin_secs + 60;
193 if (ap->mmin_char == '-')
194 return file_age < mmin_secs;
195 /* just numeric mmin */
196 return file_age >= mmin_secs && file_age < (mmin_secs + 60);
199 #if ENABLE_FEATURE_FIND_NEWER
202 return (ap->newer_mtime < statbuf->st_mtime);
205 #if ENABLE_FEATURE_FIND_INUM
208 return (statbuf->st_ino == ap->inode_num);
211 #if ENABLE_FEATURE_FIND_EXEC
215 char *argv[ap->exec_argc+1];
216 for (i = 0; i < ap->exec_argc; i++)
217 argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName);
218 argv[i] = NULL; /* terminate the list */
219 rc = wait4pid(spawn(argv));
221 bb_perror_msg("%s", argv[0]);
222 for (i = 0; i < ap->exec_argc; i++)
224 return rc == 0; /* return 1 if success */
228 #if ENABLE_FEATURE_FIND_USER
231 return (statbuf->st_uid == ap->uid);
235 #if ENABLE_FEATURE_FIND_GROUP
238 return (statbuf->st_gid == ap->gid);
242 #if ENABLE_FEATURE_FIND_PRINT0
245 printf("%s%c", fileName, '\0');
256 #if ENABLE_FEATURE_FIND_PAREN
259 return exec_actions(ap->subexpr, fileName, statbuf);
263 #if ENABLE_FEATURE_FIND_SIZE
265 * -prune: if -depth is not given, return true and do not descend
266 * current dir; if -depth is given, return false with no effect.
268 * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print
276 #if ENABLE_FEATURE_FIND_PRUNE
279 return statbuf->st_size == ap->size;
284 static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth)
287 #ifdef CONFIG_FEATURE_FIND_XDEV
288 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
289 for (i = 0; i < xdev_count; i++) {
290 if (xdev_dev[i] != statbuf->st_dev)
295 i = exec_actions(actions, fileName, statbuf);
296 /* Had no explicit -print[0] or -exec? then print */
299 /* Cannot return 0: our caller, recursive_action(),
300 * will perror() and skip dirs (if called on dir) */
301 return i == 0 ? TRUE : i;
305 #if ENABLE_FEATURE_FIND_TYPE
306 static int find_type(const char *type)
312 else if (*type == 'c')
314 else if (*type == 'd')
316 else if (*type == 'p')
318 else if (*type == 'f')
320 else if (*type == 'l')
322 else if (*type == 's')
325 if (mask == 0 || *(type + 1) != '\0')
326 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
332 #if ENABLE_FEATURE_FIND_PERM || ENABLE_FEATURE_FIND_MTIME \
333 || ENABLE_FEATURE_FIND_MMIN
334 static const char* plus_minus_num(const char* str)
336 if (*str == '-' || *str == '+')
342 static action*** parse_params(char **argv)
347 USE_FEATURE_FIND_NOT( PARM_char_not ,)
349 USE_FEATURE_FIND_PRINT0(PARM_print0 ,)
351 USE_FEATURE_FIND_TYPE( PARM_type ,)
352 USE_FEATURE_FIND_PERM( PARM_perm ,)
353 USE_FEATURE_FIND_MTIME( PARM_mtime ,)
354 USE_FEATURE_FIND_MMIN( PARM_mmin ,)
355 USE_FEATURE_FIND_NEWER( PARM_newer ,)
356 USE_FEATURE_FIND_INUM( PARM_inum ,)
357 USE_FEATURE_FIND_EXEC( PARM_exec ,)
358 USE_FEATURE_FIND_USER( PARM_user ,)
359 USE_FEATURE_FIND_GROUP( PARM_group ,)
360 USE_FEATURE_FIND_DEPTH( PARM_depth ,)
361 USE_FEATURE_FIND_PAREN( PARM_char_brace,)
362 USE_FEATURE_FIND_SIZE( PARM_prune ,)
363 USE_FEATURE_FIND_PRUNE( PARM_size ,)
367 USE_FEATURE_FIND_NOT( PARM_not ,)
371 static const char *const params[] = {
374 USE_FEATURE_FIND_NOT( "!" ,)
376 USE_FEATURE_FIND_PRINT0("-print0",)
378 USE_FEATURE_FIND_TYPE( "-type" ,)
379 USE_FEATURE_FIND_PERM( "-perm" ,)
380 USE_FEATURE_FIND_MTIME( "-mtime" ,)
381 USE_FEATURE_FIND_MMIN( "-mmin" ,)
382 USE_FEATURE_FIND_NEWER( "-newer" ,)
383 USE_FEATURE_FIND_INUM( "-inum" ,)
384 USE_FEATURE_FIND_EXEC( "-exec" ,)
385 USE_FEATURE_FIND_USER( "-user" ,)
386 USE_FEATURE_FIND_GROUP( "-group" ,)
387 USE_FEATURE_FIND_DEPTH( "-depth" ,)
388 USE_FEATURE_FIND_PAREN( "(" ,)
389 USE_FEATURE_FIND_SIZE( "-prune" ,)
390 USE_FEATURE_FIND_PRUNE( "-size" ,)
394 USE_FEATURE_FIND_NOT( "-not" ,)
400 unsigned cur_group = 0;
401 unsigned cur_action = 0;
402 USE_FEATURE_FIND_NOT( bool invert_flag = 0; )
404 /* 'static' doesn't work here! (gcc 4.1.2) */
405 action* alloc_action(int sizeof_struct, action_fp f)
408 appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp));
409 appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct);
410 appp[cur_group][cur_action] = NULL;
412 USE_FEATURE_FIND_NOT( ap->invert = invert_flag; )
413 USE_FEATURE_FIND_NOT( invert_flag = 0; )
417 #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name)
419 appp = xzalloc(2 * sizeof(appp[0])); /* appp[0],[1] == NULL */
421 /* Actions have side effects and return a true or false value
422 * We implement: -print, -print0, -exec
424 * The rest are tests.
426 * Tests and actions are grouped by operators
427 * ( expr ) Force precedence
428 * ! expr True if expr is false
429 * -not expr Same as ! expr
430 * expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false
431 * expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true
432 * expr1 , expr2 List; both expr1 and expr2 are always evaluated
433 * We implement: (), -a, -o
436 const char *arg = argv[0];
437 const char *arg1 = argv[1];
438 int parm = index_in_str_array(params, arg);
439 /* --- Operators --- */
440 if (parm == PARM_a USE_DESKTOP(|| parm == PARM_and)) {
441 /* no further special handling required */
443 else if (parm == PARM_o USE_DESKTOP(|| parm == PARM_or)) {
444 /* start new OR group */
446 appp = xrealloc(appp, (cur_group+2) * sizeof(*appp));
447 /*appp[cur_group] = NULL; - already NULL */
448 appp[cur_group+1] = NULL;
451 #if ENABLE_FEATURE_FIND_NOT
452 else if (parm == PARM_char_not USE_DESKTOP(|| parm == PARM_not)) {
453 /* also handles "find ! ! -name 'foo*'" */
458 /* --- Tests and actions --- */
459 else if (parm == PARM_print) {
461 /* GNU find ignores '!' here: "find ! -print" */
462 USE_FEATURE_FIND_NOT( invert_flag = 0; )
463 (void) ALLOC_ACTION(print);
465 #if ENABLE_FEATURE_FIND_PRINT0
466 else if (parm == PARM_print0) {
468 USE_FEATURE_FIND_NOT( invert_flag = 0; )
469 (void) ALLOC_ACTION(print0);
472 else if (parm == PARM_name) {
475 bb_error_msg_and_die(bb_msg_requires_arg, arg);
476 ap = ALLOC_ACTION(name);
479 #if ENABLE_FEATURE_FIND_TYPE
480 else if (parm == PARM_type) {
483 bb_error_msg_and_die(bb_msg_requires_arg, arg);
484 ap = ALLOC_ACTION(type);
485 ap->type_mask = find_type(arg1);
488 #if ENABLE_FEATURE_FIND_PERM
490 * -perm mode File's permission bits are exactly mode (octal or symbolic).
491 * Symbolic modes use mode 0 as a point of departure.
492 * -perm -mode All of the permission bits mode are set for the file.
493 * -perm +mode Any of the permission bits mode are set for the file.
495 else if (parm == PARM_perm) {
498 bb_error_msg_and_die(bb_msg_requires_arg, arg);
499 ap = ALLOC_ACTION(perm);
500 ap->perm_char = arg1[0];
501 arg1 = plus_minus_num(arg1);
503 if (!bb_parse_mode(arg1, &ap->perm_mask))
504 bb_error_msg_and_die("invalid mode: %s", arg1);
507 #if ENABLE_FEATURE_FIND_MTIME
508 else if (parm == PARM_mtime) {
511 bb_error_msg_and_die(bb_msg_requires_arg, arg);
512 ap = ALLOC_ACTION(mtime);
513 ap->mtime_char = arg1[0];
514 ap->mtime_days = xatoul(plus_minus_num(arg1));
517 #if ENABLE_FEATURE_FIND_MMIN
518 else if (parm == PARM_mmin) {
521 bb_error_msg_and_die(bb_msg_requires_arg, arg);
522 ap = ALLOC_ACTION(mmin);
523 ap->mmin_char = arg1[0];
524 ap->mmin_mins = xatoul(plus_minus_num(arg1));
527 #if ENABLE_FEATURE_FIND_NEWER
528 else if (parm == PARM_newer) {
530 struct stat stat_newer;
532 bb_error_msg_and_die(bb_msg_requires_arg, arg);
533 xstat(arg1, &stat_newer);
534 ap = ALLOC_ACTION(newer);
535 ap->newer_mtime = stat_newer.st_mtime;
538 #if ENABLE_FEATURE_FIND_INUM
539 else if (parm == PARM_inum) {
542 bb_error_msg_and_die(bb_msg_requires_arg, arg);
543 ap = ALLOC_ACTION(inum);
544 ap->inode_num = xatoul(arg1);
547 #if ENABLE_FEATURE_FIND_EXEC
548 else if (parm == PARM_exec) {
552 USE_FEATURE_FIND_NOT( invert_flag = 0; )
553 ap = ALLOC_ACTION(exec);
554 ap->exec_argv = ++argv; /* first arg after -exec */
557 if (!*argv) /* did not see ';' util end */
558 bb_error_msg_and_die(bb_msg_requires_arg, arg);
559 if (LONE_CHAR(argv[0], ';'))
564 if (ap->exec_argc == 0)
565 bb_error_msg_and_die(bb_msg_requires_arg, arg);
566 ap->subst_count = xmalloc(ap->exec_argc * sizeof(int));
569 ap->subst_count[i] = count_subst(ap->exec_argv[i]);
572 #if ENABLE_FEATURE_FIND_USER
573 else if (parm == PARM_user) {
576 bb_error_msg_and_die(bb_msg_requires_arg, arg);
577 ap = ALLOC_ACTION(user);
578 ap->uid = bb_strtou(arg1, NULL, 10);
580 ap->uid = xuname2uid(arg1);
583 #if ENABLE_FEATURE_FIND_GROUP
584 else if (parm == PARM_group) {
587 bb_error_msg_and_die(bb_msg_requires_arg, arg);
588 ap = ALLOC_ACTION(group);
589 ap->gid = bb_strtou(arg1, NULL, 10);
591 ap->gid = xgroup2gid(arg1);
594 #if ENABLE_FEATURE_FIND_DEPTH
595 else if (parm == PARM_depth) {
596 recurse_flags |= action_depthFirst;
599 #if ENABLE_FEATURE_FIND_PAREN
600 else if (parm == PARM_char_brace) {
608 bb_error_msg_and_die("unpaired '('");
609 if (LONE_CHAR(*endarg, '('))
611 else if (LONE_CHAR(*endarg, ')') && !--nested) {
616 ap = ALLOC_ACTION(paren);
617 ap->subexpr = parse_params(argv + 1);
618 *endarg = (char*) ")"; /* restore NULLed parameter */
622 #if ENABLE_FEATURE_FIND_PRUNE
623 else if (parm == PARM_prune) {
624 USE_FEATURE_FIND_NOT( invert_flag = 0; )
625 (void) ALLOC_ACTION(prune);
628 #if ENABLE_FEATURE_FIND_SIZE
629 else if (parm == PARM_size) {
632 bb_error_msg_and_die(bb_msg_requires_arg, arg);
633 ap = ALLOC_ACTION(size);
634 ap->size = XATOOFF(arg1);
646 int find_main(int argc, char **argv);
647 int find_main(int argc, char **argv)
649 bool dereference = FALSE;
652 int i, firstopt, status = EXIT_SUCCESS;
653 const char * const options[] = {
655 USE_FEATURE_FIND_XDEV( "-xdev", )
659 for (firstopt = 1; firstopt < argc; firstopt++) {
660 if (argv[firstopt][0] == '-')
662 if (ENABLE_FEATURE_FIND_NOT && LONE_CHAR(argv[firstopt], '!'))
664 #if ENABLE_FEATURE_FIND_PAREN
665 if (LONE_CHAR(argv[firstopt], '('))
670 argv[0] = (char*)".";
675 /* All options always return true. They always take effect
676 * rather than being processed only when their place in the
677 * expression is reached.
678 * We implement: -follow, -xdev
680 /* Process options, and replace then with -a */
681 /* (-a will be ignored by recursive parser later) */
682 argp = &argv[firstopt];
683 while ((arg = argp[0])) {
684 i = index_in_str_array(options, arg);
685 if (i == 0) { /* -follow */
687 argp[0] = (char*)"-a";
689 #if ENABLE_FEATURE_FIND_XDEV
690 else if (i == 1) { /* -xdev */
693 xdev_count = firstopt - 1;
694 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
695 for (i = 1; i < firstopt; i++) {
696 /* not xstat(): shouldn't bomb out on
697 * "find not_exist exist -xdev" */
698 if (stat(argv[i], &stbuf))
700 xdev_dev[i-1] = stbuf.st_dev;
703 argp[0] = (char*)"-a";
709 actions = parse_params(&argv[firstopt]);
711 for (i = 1; i < firstopt; i++) {
712 if (!recursive_action(argv[i],
713 recurse_flags|(1<<dereference), /* flags */
714 fileAction, /* file action */
715 fileAction, /* dir action */
716 NULL, /* user data */
718 status = EXIT_FAILURE;