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 v2 or later, see the file LICENSE in this tarball.
17 #ifdef CONFIG_FEATURE_FIND_PRINT0
18 static char printsep = '\n';
21 #ifdef CONFIG_FEATURE_FIND_TYPE
22 static int type_mask = 0;
25 #ifdef CONFIG_FEATURE_FIND_PERM
26 static char perm_char = 0;
27 static int perm_mask = 0;
30 #ifdef CONFIG_FEATURE_FIND_MTIME
31 static char mtime_char;
32 static int mtime_days;
35 #ifdef CONFIG_FEATURE_FIND_MMIN
36 static char mmin_char;
40 #ifdef CONFIG_FEATURE_FIND_XDEV
41 static dev_t *xdev_dev;
42 static int xdev_count = 0;
45 #ifdef CONFIG_FEATURE_FIND_NEWER
46 static time_t newer_mtime;
49 #ifdef CONFIG_FEATURE_FIND_INUM
50 static ino_t inode_num;
53 #ifdef CONFIG_FEATURE_FIND_EXEC
54 static char **exec_str;
55 static int num_matches;
59 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
61 #ifdef CONFIG_FEATURE_FIND_XDEV
62 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
64 for (i=0; i<xdev_count; i++) {
65 if (xdev_dev[i] != statbuf->st_dev)
70 if (pattern != NULL) {
71 const char *tmp = strrchr(fileName, '/');
77 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
80 #ifdef CONFIG_FEATURE_FIND_TYPE
82 if (!((statbuf->st_mode & S_IFMT) == type_mask))
86 #ifdef CONFIG_FEATURE_FIND_PERM
88 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
89 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
90 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
94 #ifdef CONFIG_FEATURE_FIND_MTIME
95 if (mtime_char != 0) {
96 time_t file_age = time(NULL) - statbuf->st_mtime;
97 time_t mtime_secs = mtime_days * 24 * 60 * 60;
98 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
99 file_age < mtime_secs + 24 * 60 * 60) ||
100 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
101 (mtime_char == '-' && file_age < mtime_secs)))
105 #ifdef CONFIG_FEATURE_FIND_MMIN
106 if (mmin_char != 0) {
107 time_t file_age = time(NULL) - statbuf->st_mtime;
108 time_t mmin_secs = mmin_mins * 60;
109 if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
110 file_age < mmin_secs + 60) ||
111 (mmin_char == '+' && file_age >= mmin_secs + 60) ||
112 (mmin_char == '-' && file_age < mmin_secs)))
116 #ifdef CONFIG_FEATURE_FIND_NEWER
117 if (newer_mtime != 0) {
118 time_t file_age = newer_mtime - statbuf->st_mtime;
123 #ifdef CONFIG_FEATURE_FIND_INUM
124 if (inode_num != 0) {
125 if (!(statbuf->st_ino == inode_num))
129 #ifdef CONFIG_FEATURE_FIND_EXEC
132 char *cmd_string = "";
133 for (i = 0; i < num_matches; i++)
134 cmd_string = xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
135 cmd_string = xasprintf("%s%s", cmd_string, exec_str[num_matches]);
141 #ifdef CONFIG_FEATURE_FIND_PRINT0
142 printf("%s%c", fileName, printsep);
150 #ifdef CONFIG_FEATURE_FIND_TYPE
151 static int find_type(char *type)
179 if (mask == 0 || type[1] != '\0')
180 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
186 int find_main(int argc, char **argv)
188 int dereference = FALSE;
189 int i, firstopt, status = EXIT_SUCCESS;
191 for (firstopt = 1; firstopt < argc; firstopt++) {
192 if (argv[firstopt][0] == '-')
196 /* Parse any options */
197 for (i = firstopt; i < argc; i++) {
198 if (strcmp(argv[i], "-follow") == 0)
200 else if (strcmp(argv[i], "-print") == 0) {
203 #ifdef CONFIG_FEATURE_FIND_PRINT0
204 else if (strcmp(argv[i], "-print0") == 0)
207 else if (strcmp(argv[i], "-name") == 0) {
209 bb_error_msg_and_die(bb_msg_requires_arg, "-name");
211 #ifdef CONFIG_FEATURE_FIND_TYPE
212 } else if (strcmp(argv[i], "-type") == 0) {
214 bb_error_msg_and_die(bb_msg_requires_arg, "-type");
215 type_mask = find_type(argv[i]);
217 #ifdef CONFIG_FEATURE_FIND_PERM
218 } else if (strcmp(argv[i], "-perm") == 0) {
221 bb_error_msg_and_die(bb_msg_requires_arg, "-perm");
222 perm_mask = strtol(argv[i], &end, 8);
223 if ((end[0] != '\0') || (perm_mask > 07777))
224 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-perm");
225 if ((perm_char = argv[i][0]) == '-')
226 perm_mask = -perm_mask;
228 #ifdef CONFIG_FEATURE_FIND_MTIME
229 } else if (strcmp(argv[i], "-mtime") == 0) {
232 bb_error_msg_and_die(bb_msg_requires_arg, "-mtime");
233 mtime_days = strtol(argv[i], &end, 10);
235 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mtime");
236 if ((mtime_char = argv[i][0]) == '-')
237 mtime_days = -mtime_days;
239 #ifdef CONFIG_FEATURE_FIND_MMIN
240 } else if (strcmp(argv[i], "-mmin") == 0) {
243 bb_error_msg_and_die(bb_msg_requires_arg, "-mmin");
244 mmin_mins = strtol(argv[i], &end, 10);
246 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mmin");
247 if ((mmin_char = argv[i][0]) == '-')
248 mmin_mins = -mmin_mins;
250 #ifdef CONFIG_FEATURE_FIND_XDEV
251 } else if (strcmp(argv[i], "-xdev") == 0) {
254 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
255 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
257 if ( firstopt == 1 ) {
258 xstat ( ".", &stbuf );
259 xdev_dev [0] = stbuf. st_dev;
263 for (i = 1; i < firstopt; i++) {
264 xstat ( argv [i], &stbuf );
265 xdev_dev [i-1] = stbuf. st_dev;
269 #ifdef CONFIG_FEATURE_FIND_NEWER
270 } else if (strcmp(argv[i], "-newer") == 0) {
271 struct stat stat_newer;
273 bb_error_msg_and_die(bb_msg_requires_arg, "-newer");
274 xstat (argv[i], &stat_newer);
275 newer_mtime = stat_newer.st_mtime;
277 #ifdef CONFIG_FEATURE_FIND_INUM
278 } else if (strcmp(argv[i], "-inum") == 0) {
281 bb_error_msg_and_die(bb_msg_requires_arg, "-inum");
282 inode_num = strtol(argv[i], &end, 10);
284 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-inum");
286 #ifdef CONFIG_FEATURE_FIND_EXEC
287 } else if (strcmp(argv[i], "-exec") == 0) {
289 char *cmd_string = "";
293 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
296 cmd_string = xasprintf("%s %s", cmd_string, argv[i]);
299 if (*cmd_string == 0)
300 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
302 exec_str = xmalloc(sizeof(char *));
304 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
306 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
307 exec_str[num_matches - 1] = xstrndup(cmd_string, b_pos);
308 cmd_string += b_pos + 2;
310 exec_str[num_matches] = xstrdup(cmd_string);
318 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
320 status = EXIT_FAILURE;
322 for (i = 1; i < firstopt; i++) {
323 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
325 status = EXIT_FAILURE;