2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-07 01:55:32 +05:30
|
|
|
/*
|
|
|
|
* Mini find implementation for busybox
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-10-21 03:38:37 +05:30
|
|
|
*
|
2001-02-07 09:22:38 +05:30
|
|
|
* Reworked by David Douthitt <n9ubh@callsign.net> and
|
|
|
|
* Matt Kraai <kraai@alumni.carnegiemellon.edu>.
|
1999-10-07 01:55:32 +05:30
|
|
|
*
|
2006-06-03 02:26:16 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
1999-10-07 01:55:32 +05:30
|
|
|
*/
|
|
|
|
|
2006-06-03 02:26:16 +05:30
|
|
|
#include "busybox.h"
|
1999-10-07 01:55:32 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <dirent.h>
|
2001-01-27 13:54:39 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
2001-02-07 09:22:38 +05:30
|
|
|
#include <fnmatch.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <ctype.h>
|
1999-10-07 01:55:32 +05:30
|
|
|
|
2001-02-07 09:22:38 +05:30
|
|
|
static char *pattern;
|
2006-05-12 20:17:20 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_PRINT0
|
|
|
|
static char printsep = '\n';
|
|
|
|
#endif
|
2001-02-07 09:22:38 +05:30
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_TYPE
|
2001-02-07 09:22:38 +05:30
|
|
|
static int type_mask = 0;
|
|
|
|
#endif
|
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_PERM
|
2001-02-07 09:22:38 +05:30
|
|
|
static char perm_char = 0;
|
|
|
|
static int perm_mask = 0;
|
|
|
|
#endif
|
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_MTIME
|
2001-02-07 09:22:38 +05:30
|
|
|
static char mtime_char;
|
|
|
|
static int mtime_days;
|
|
|
|
#endif
|
1999-10-07 01:55:32 +05:30
|
|
|
|
2006-01-14 02:35:41 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_MMIN
|
|
|
|
static char mmin_char;
|
|
|
|
static int mmin_mins;
|
|
|
|
#endif
|
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_XDEV
|
2002-07-24 06:04:48 +05:30
|
|
|
static dev_t *xdev_dev;
|
|
|
|
static int xdev_count = 0;
|
|
|
|
#endif
|
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_NEWER
|
2005-04-17 01:09:00 +05:30
|
|
|
static time_t newer_mtime;
|
2003-01-23 10:57:42 +05:30
|
|
|
#endif
|
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_INUM
|
2003-01-23 10:57:42 +05:30
|
|
|
static ino_t inode_num;
|
|
|
|
#endif
|
2002-07-24 06:04:48 +05:30
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_EXEC
|
2005-10-04 09:04:39 +05:30
|
|
|
static char **exec_str;
|
|
|
|
static int num_matches;
|
|
|
|
static int exec_opt;
|
|
|
|
#endif
|
|
|
|
|
2000-03-28 06:28:14 +05:30
|
|
|
static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
|
1999-10-07 01:55:32 +05:30
|
|
|
{
|
2006-03-27 22:12:33 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_XDEV
|
|
|
|
if (S_ISDIR(statbuf->st_mode) && xdev_count) {
|
|
|
|
int i;
|
|
|
|
for (i=0; i<xdev_count; i++) {
|
|
|
|
if (xdev_dev[i] != statbuf->st_dev)
|
|
|
|
return SKIP;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2001-02-07 09:22:38 +05:30
|
|
|
if (pattern != NULL) {
|
|
|
|
const char *tmp = strrchr(fileName, '/');
|
2000-02-09 01:28:47 +05:30
|
|
|
|
|
|
|
if (tmp == NULL)
|
2001-02-07 09:22:38 +05:30
|
|
|
tmp = fileName;
|
2000-02-09 01:28:47 +05:30
|
|
|
else
|
|
|
|
tmp++;
|
2001-02-07 09:22:38 +05:30
|
|
|
if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
|
|
|
|
goto no_match;
|
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_TYPE
|
|
|
|
if (type_mask != 0) {
|
2001-02-07 09:22:38 +05:30
|
|
|
if (!((statbuf->st_mode & S_IFMT) == type_mask))
|
|
|
|
goto no_match;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_PERM
|
|
|
|
if (perm_mask != 0) {
|
2001-02-07 09:22:38 +05:30
|
|
|
if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
|
|
|
|
(perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
|
|
|
|
(perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
|
|
|
|
goto no_match;
|
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_MTIME
|
|
|
|
if (mtime_char != 0) {
|
2001-02-07 09:22:38 +05:30
|
|
|
time_t file_age = time(NULL) - statbuf->st_mtime;
|
|
|
|
time_t mtime_secs = mtime_days * 24 * 60 * 60;
|
2002-12-12 02:52:21 +05:30
|
|
|
if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
|
|
|
|
file_age < mtime_secs + 24 * 60 * 60) ||
|
2004-03-15 13:59:22 +05:30
|
|
|
(mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
|
2002-12-12 02:52:21 +05:30
|
|
|
(mtime_char == '-' && file_age < mtime_secs)))
|
2001-02-07 09:22:38 +05:30
|
|
|
goto no_match;
|
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
2006-01-14 02:35:41 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_MMIN
|
|
|
|
if (mmin_char != 0) {
|
|
|
|
time_t file_age = time(NULL) - statbuf->st_mtime;
|
|
|
|
time_t mmin_secs = mmin_mins * 60;
|
|
|
|
if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
|
|
|
|
file_age < mmin_secs + 60) ||
|
|
|
|
(mmin_char == '+' && file_age >= mmin_secs + 60) ||
|
|
|
|
(mmin_char == '-' && file_age < mmin_secs)))
|
|
|
|
goto no_match;
|
2002-07-24 06:04:48 +05:30
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_NEWER
|
|
|
|
if (newer_mtime != 0) {
|
2003-01-23 10:57:42 +05:30
|
|
|
time_t file_age = newer_mtime - statbuf->st_mtime;
|
|
|
|
if (file_age >= 0)
|
|
|
|
goto no_match;
|
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_INUM
|
|
|
|
if (inode_num != 0) {
|
2003-01-23 10:57:42 +05:30
|
|
|
if (!(statbuf->st_ino == inode_num))
|
|
|
|
goto no_match;
|
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_EXEC
|
|
|
|
if (exec_opt) {
|
2005-10-04 09:04:39 +05:30
|
|
|
int i;
|
|
|
|
char *cmd_string = "";
|
|
|
|
for (i = 0; i < num_matches; i++)
|
|
|
|
cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
|
|
|
|
cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
|
|
|
|
system(cmd_string);
|
|
|
|
goto no_match;
|
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
2006-01-25 05:38:53 +05:30
|
|
|
|
2006-05-12 20:17:20 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_PRINT0
|
|
|
|
printf("%s%c", fileName, printsep);
|
|
|
|
#else
|
2001-02-07 09:22:38 +05:30
|
|
|
puts(fileName);
|
2006-05-12 20:17:20 +05:30
|
|
|
#endif
|
2001-02-07 09:22:38 +05:30
|
|
|
no_match:
|
2000-02-09 01:28:47 +05:30
|
|
|
return (TRUE);
|
1999-10-07 01:55:32 +05:30
|
|
|
}
|
|
|
|
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_TYPE
|
2001-02-07 09:22:38 +05:30
|
|
|
static int find_type(char *type)
|
|
|
|
{
|
|
|
|
int mask = 0;
|
|
|
|
|
|
|
|
switch (type[0]) {
|
|
|
|
case 'b':
|
|
|
|
mask = S_IFBLK;
|
|
|
|
break;
|
|
|
|
case 'c':
|
|
|
|
mask = S_IFCHR;
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
mask = S_IFDIR;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
mask = S_IFIFO;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
mask = S_IFREG;
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
mask = S_IFLNK;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
mask = S_IFSOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mask == 0 || type[1] != '\0')
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
|
2001-02-07 09:22:38 +05:30
|
|
|
|
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
1999-10-07 01:55:32 +05:30
|
|
|
int find_main(int argc, char **argv)
|
|
|
|
{
|
2001-02-07 09:22:38 +05:30
|
|
|
int dereference = FALSE;
|
|
|
|
int i, firstopt, status = EXIT_SUCCESS;
|
|
|
|
|
|
|
|
for (firstopt = 1; firstopt < argc; firstopt++) {
|
|
|
|
if (argv[firstopt][0] == '-')
|
|
|
|
break;
|
1999-10-07 01:55:32 +05:30
|
|
|
}
|
|
|
|
|
2000-02-09 01:28:47 +05:30
|
|
|
/* Parse any options */
|
2001-02-07 09:22:38 +05:30
|
|
|
for (i = firstopt; i < argc; i++) {
|
|
|
|
if (strcmp(argv[i], "-follow") == 0)
|
|
|
|
dereference = TRUE;
|
2001-03-29 03:34:42 +05:30
|
|
|
else if (strcmp(argv[i], "-print") == 0) {
|
|
|
|
;
|
|
|
|
}
|
2006-05-12 20:17:20 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_PRINT0
|
|
|
|
else if (strcmp(argv[i], "-print0") == 0)
|
|
|
|
printsep = '\0';
|
|
|
|
#endif
|
2001-02-07 09:22:38 +05:30
|
|
|
else if (strcmp(argv[i], "-name") == 0) {
|
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-name");
|
2001-02-07 09:22:38 +05:30
|
|
|
pattern = argv[i];
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_TYPE
|
|
|
|
} else if (strcmp(argv[i], "-type") == 0) {
|
2001-02-07 09:22:38 +05:30
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-type");
|
2001-02-07 09:22:38 +05:30
|
|
|
type_mask = find_type(argv[i]);
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_PERM
|
|
|
|
} else if (strcmp(argv[i], "-perm") == 0) {
|
2001-02-07 09:22:38 +05:30
|
|
|
char *end;
|
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-perm");
|
2001-02-07 09:22:38 +05:30
|
|
|
perm_mask = strtol(argv[i], &end, 8);
|
2003-01-23 10:57:42 +05:30
|
|
|
if ((end[0] != '\0') || (perm_mask > 07777))
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-perm");
|
2001-02-07 09:22:38 +05:30
|
|
|
if ((perm_char = argv[i][0]) == '-')
|
|
|
|
perm_mask = -perm_mask;
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_MTIME
|
|
|
|
} else if (strcmp(argv[i], "-mtime") == 0) {
|
2001-02-07 09:22:38 +05:30
|
|
|
char *end;
|
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-mtime");
|
2001-02-07 09:22:38 +05:30
|
|
|
mtime_days = strtol(argv[i], &end, 10);
|
|
|
|
if (end[0] != '\0')
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mtime");
|
2001-02-07 09:22:38 +05:30
|
|
|
if ((mtime_char = argv[i][0]) == '-')
|
|
|
|
mtime_days = -mtime_days;
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
2006-01-14 02:35:41 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_MMIN
|
|
|
|
} else if (strcmp(argv[i], "-mmin") == 0) {
|
|
|
|
char *end;
|
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-mmin");
|
2006-01-14 02:35:41 +05:30
|
|
|
mmin_mins = strtol(argv[i], &end, 10);
|
|
|
|
if (end[0] != '\0')
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mmin");
|
2006-01-14 02:35:41 +05:30
|
|
|
if ((mmin_char = argv[i][0]) == '-')
|
|
|
|
mmin_mins = -mmin_mins;
|
|
|
|
#endif
|
2005-10-06 22:09:17 +05:30
|
|
|
#ifdef CONFIG_FEATURE_FIND_XDEV
|
|
|
|
} else if (strcmp(argv[i], "-xdev") == 0) {
|
2002-07-24 06:04:48 +05:30
|
|
|
struct stat stbuf;
|
|
|
|
|
|
|
|
xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
|
|
|
|
xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
|
|
|
|
|
|
|
|
if ( firstopt == 1 ) {
|
2006-03-13 21:15:16 +05:30
|
|
|
xstat ( ".", &stbuf );
|
2002-07-24 06:04:48 +05:30
|
|
|
xdev_dev [0] = stbuf. st_dev;
|
|
|
|
}
|
|
|
|
else {
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-07-24 06:04:48 +05:30
|
|
|
for (i = 1; i < firstopt; i++) {
|
2006-03-13 21:15:16 +05:30
|
|
|
xstat ( argv [i], &stbuf );
|
2002-07-24 06:04:48 +05:30
|
|
|
xdev_dev [i-1] = stbuf. st_dev;
|
|
|
|
}
|
2004-03-15 13:59:22 +05:30
|
|
|
}
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_NEWER
|
|
|
|
} else if (strcmp(argv[i], "-newer") == 0) {
|
2003-01-23 10:57:42 +05:30
|
|
|
struct stat stat_newer;
|
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-newer");
|
2006-03-13 21:15:16 +05:30
|
|
|
xstat (argv[i], &stat_newer);
|
2003-01-23 10:57:42 +05:30
|
|
|
newer_mtime = stat_newer.st_mtime;
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_INUM
|
|
|
|
} else if (strcmp(argv[i], "-inum") == 0) {
|
2003-01-23 10:57:42 +05:30
|
|
|
char *end;
|
|
|
|
if (++i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-inum");
|
2003-01-23 10:57:42 +05:30
|
|
|
inode_num = strtol(argv[i], &end, 10);
|
|
|
|
if (end[0] != '\0')
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-inum");
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_FEATURE_FIND_EXEC
|
|
|
|
} else if (strcmp(argv[i], "-exec") == 0) {
|
2005-10-04 09:04:39 +05:30
|
|
|
int b_pos;
|
|
|
|
char *cmd_string = "";
|
|
|
|
|
|
|
|
while (i++) {
|
|
|
|
if (i == argc)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
|
2005-10-04 09:04:39 +05:30
|
|
|
if (*argv[i] == ';')
|
|
|
|
break;
|
|
|
|
cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
|
|
|
|
}
|
2006-01-25 05:38:53 +05:30
|
|
|
|
2005-10-04 09:04:39 +05:30
|
|
|
if (*cmd_string == 0)
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
|
2005-10-04 09:04:39 +05:30
|
|
|
cmd_string++;
|
|
|
|
exec_str = xmalloc(sizeof(char *));
|
|
|
|
|
|
|
|
while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
|
|
|
|
num_matches++;
|
|
|
|
exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
|
|
|
|
exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
|
|
|
|
cmd_string += b_pos + 2;
|
|
|
|
}
|
|
|
|
exec_str[num_matches] = bb_xstrdup(cmd_string);
|
|
|
|
exec_opt = 1;
|
2005-10-06 22:09:17 +05:30
|
|
|
#endif
|
2001-02-07 09:22:38 +05:30
|
|
|
} else
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_show_usage();
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
|
|
|
|
2001-02-07 09:22:38 +05:30
|
|
|
if (firstopt == 1) {
|
2001-12-21 04:43:26 +05:30
|
|
|
if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
|
|
|
|
fileAction, NULL))
|
2001-02-07 09:22:38 +05:30
|
|
|
status = EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
for (i = 1; i < firstopt; i++) {
|
2001-12-21 04:43:26 +05:30
|
|
|
if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
|
|
|
|
fileAction, NULL))
|
2001-02-07 09:22:38 +05:30
|
|
|
status = EXIT_FAILURE;
|
|
|
|
}
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
1999-10-22 10:00:20 +05:30
|
|
|
|
2001-02-07 09:22:38 +05:30
|
|
|
return status;
|
1999-10-07 01:55:32 +05:30
|
|
|
}
|