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.
24 #ifdef CONFIG_FEATURE_FIND_PRINT0
25 static char printsep = '\n';
28 #ifdef CONFIG_FEATURE_FIND_TYPE
29 static int type_mask = 0;
32 #ifdef CONFIG_FEATURE_FIND_PERM
33 static char perm_char = 0;
34 static int perm_mask = 0;
37 #ifdef CONFIG_FEATURE_FIND_MTIME
38 static char mtime_char;
39 static int mtime_days;
42 #ifdef CONFIG_FEATURE_FIND_MMIN
43 static char mmin_char;
47 #ifdef CONFIG_FEATURE_FIND_XDEV
48 static dev_t *xdev_dev;
49 static int xdev_count = 0;
52 #ifdef CONFIG_FEATURE_FIND_NEWER
53 static time_t newer_mtime;
56 #ifdef CONFIG_FEATURE_FIND_INUM
57 static ino_t inode_num;
60 #ifdef CONFIG_FEATURE_FIND_EXEC
61 static char **exec_str;
62 static int num_matches;
66 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
68 #ifdef CONFIG_FEATURE_FIND_XDEV
69 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
71 for (i=0; i<xdev_count; i++) {
72 if (xdev_dev[i] != statbuf->st_dev)
77 if (pattern != NULL) {
78 const char *tmp = strrchr(fileName, '/');
84 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
87 #ifdef CONFIG_FEATURE_FIND_TYPE
89 if (!((statbuf->st_mode & S_IFMT) == type_mask))
93 #ifdef CONFIG_FEATURE_FIND_PERM
95 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
96 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
97 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
101 #ifdef CONFIG_FEATURE_FIND_MTIME
102 if (mtime_char != 0) {
103 time_t file_age = time(NULL) - statbuf->st_mtime;
104 time_t mtime_secs = mtime_days * 24 * 60 * 60;
105 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
106 file_age < mtime_secs + 24 * 60 * 60) ||
107 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
108 (mtime_char == '-' && file_age < mtime_secs)))
112 #ifdef CONFIG_FEATURE_FIND_MMIN
113 if (mmin_char != 0) {
114 time_t file_age = time(NULL) - statbuf->st_mtime;
115 time_t mmin_secs = mmin_mins * 60;
116 if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
117 file_age < mmin_secs + 60) ||
118 (mmin_char == '+' && file_age >= mmin_secs + 60) ||
119 (mmin_char == '-' && file_age < mmin_secs)))
123 #ifdef CONFIG_FEATURE_FIND_NEWER
124 if (newer_mtime != 0) {
125 time_t file_age = newer_mtime - statbuf->st_mtime;
130 #ifdef CONFIG_FEATURE_FIND_INUM
131 if (inode_num != 0) {
132 if (!(statbuf->st_ino == inode_num))
136 #ifdef CONFIG_FEATURE_FIND_EXEC
139 char *cmd_string = "";
140 for (i = 0; i < num_matches; i++)
141 cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
142 cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
148 #ifdef CONFIG_FEATURE_FIND_PRINT0
149 printf("%s%c", fileName, printsep);
157 #ifdef CONFIG_FEATURE_FIND_TYPE
158 static int find_type(char *type)
186 if (mask == 0 || type[1] != '\0')
187 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
193 int find_main(int argc, char **argv)
195 int dereference = FALSE;
196 int i, firstopt, status = EXIT_SUCCESS;
198 for (firstopt = 1; firstopt < argc; firstopt++) {
199 if (argv[firstopt][0] == '-')
203 /* Parse any options */
204 for (i = firstopt; i < argc; i++) {
205 if (strcmp(argv[i], "-follow") == 0)
207 else if (strcmp(argv[i], "-print") == 0) {
210 #ifdef CONFIG_FEATURE_FIND_PRINT0
211 else if (strcmp(argv[i], "-print0") == 0)
214 else if (strcmp(argv[i], "-name") == 0) {
216 bb_error_msg_and_die(bb_msg_requires_arg, "-name");
218 #ifdef CONFIG_FEATURE_FIND_TYPE
219 } else if (strcmp(argv[i], "-type") == 0) {
221 bb_error_msg_and_die(bb_msg_requires_arg, "-type");
222 type_mask = find_type(argv[i]);
224 #ifdef CONFIG_FEATURE_FIND_PERM
225 } else if (strcmp(argv[i], "-perm") == 0) {
228 bb_error_msg_and_die(bb_msg_requires_arg, "-perm");
229 perm_mask = strtol(argv[i], &end, 8);
230 if ((end[0] != '\0') || (perm_mask > 07777))
231 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-perm");
232 if ((perm_char = argv[i][0]) == '-')
233 perm_mask = -perm_mask;
235 #ifdef CONFIG_FEATURE_FIND_MTIME
236 } else if (strcmp(argv[i], "-mtime") == 0) {
239 bb_error_msg_and_die(bb_msg_requires_arg, "-mtime");
240 mtime_days = strtol(argv[i], &end, 10);
242 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mtime");
243 if ((mtime_char = argv[i][0]) == '-')
244 mtime_days = -mtime_days;
246 #ifdef CONFIG_FEATURE_FIND_MMIN
247 } else if (strcmp(argv[i], "-mmin") == 0) {
250 bb_error_msg_and_die(bb_msg_requires_arg, "-mmin");
251 mmin_mins = strtol(argv[i], &end, 10);
253 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mmin");
254 if ((mmin_char = argv[i][0]) == '-')
255 mmin_mins = -mmin_mins;
257 #ifdef CONFIG_FEATURE_FIND_XDEV
258 } else if (strcmp(argv[i], "-xdev") == 0) {
261 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
262 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
264 if ( firstopt == 1 ) {
265 xstat ( ".", &stbuf );
266 xdev_dev [0] = stbuf. st_dev;
270 for (i = 1; i < firstopt; i++) {
271 xstat ( argv [i], &stbuf );
272 xdev_dev [i-1] = stbuf. st_dev;
276 #ifdef CONFIG_FEATURE_FIND_NEWER
277 } else if (strcmp(argv[i], "-newer") == 0) {
278 struct stat stat_newer;
280 bb_error_msg_and_die(bb_msg_requires_arg, "-newer");
281 xstat (argv[i], &stat_newer);
282 newer_mtime = stat_newer.st_mtime;
284 #ifdef CONFIG_FEATURE_FIND_INUM
285 } else if (strcmp(argv[i], "-inum") == 0) {
288 bb_error_msg_and_die(bb_msg_requires_arg, "-inum");
289 inode_num = strtol(argv[i], &end, 10);
291 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-inum");
293 #ifdef CONFIG_FEATURE_FIND_EXEC
294 } else if (strcmp(argv[i], "-exec") == 0) {
296 char *cmd_string = "";
300 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
303 cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
306 if (*cmd_string == 0)
307 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
309 exec_str = xmalloc(sizeof(char *));
311 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
313 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
314 exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
315 cmd_string += b_pos + 2;
317 exec_str[num_matches] = bb_xstrdup(cmd_string);
325 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
327 status = EXIT_FAILURE;
329 for (i = 1; i < firstopt; i++) {
330 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
332 status = EXIT_FAILURE;