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.
23 //XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz
24 static const char msg_req_arg[] = "option `%s' requires an argument";
25 static const char msg_invalid_arg[] = "invalid argument `%s' to `%s'";
28 #ifdef CONFIG_FEATURE_FIND_PRINT0
29 static char printsep = '\n';
32 #ifdef CONFIG_FEATURE_FIND_TYPE
33 static int type_mask = 0;
36 #ifdef CONFIG_FEATURE_FIND_PERM
37 static char perm_char = 0;
38 static int perm_mask = 0;
41 #ifdef CONFIG_FEATURE_FIND_MTIME
42 static char mtime_char;
43 static int mtime_days;
46 #ifdef CONFIG_FEATURE_FIND_MMIN
47 static char mmin_char;
51 #ifdef CONFIG_FEATURE_FIND_XDEV
52 static dev_t *xdev_dev;
53 static int xdev_count = 0;
56 #ifdef CONFIG_FEATURE_FIND_NEWER
57 static time_t newer_mtime;
60 #ifdef CONFIG_FEATURE_FIND_INUM
61 static ino_t inode_num;
64 #ifdef CONFIG_FEATURE_FIND_EXEC
65 static char **exec_str;
66 static int num_matches;
70 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
72 #ifdef CONFIG_FEATURE_FIND_XDEV
73 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
75 for (i=0; i<xdev_count; i++) {
76 if (xdev_dev[i] != statbuf->st_dev)
81 if (pattern != NULL) {
82 const char *tmp = strrchr(fileName, '/');
88 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
91 #ifdef CONFIG_FEATURE_FIND_TYPE
93 if (!((statbuf->st_mode & S_IFMT) == type_mask))
97 #ifdef CONFIG_FEATURE_FIND_PERM
99 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
100 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
101 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
105 #ifdef CONFIG_FEATURE_FIND_MTIME
106 if (mtime_char != 0) {
107 time_t file_age = time(NULL) - statbuf->st_mtime;
108 time_t mtime_secs = mtime_days * 24 * 60 * 60;
109 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
110 file_age < mtime_secs + 24 * 60 * 60) ||
111 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
112 (mtime_char == '-' && file_age < mtime_secs)))
116 #ifdef CONFIG_FEATURE_FIND_MMIN
117 if (mmin_char != 0) {
118 time_t file_age = time(NULL) - statbuf->st_mtime;
119 time_t mmin_secs = mmin_mins * 60;
120 if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
121 file_age < mmin_secs + 60) ||
122 (mmin_char == '+' && file_age >= mmin_secs + 60) ||
123 (mmin_char == '-' && file_age < mmin_secs)))
127 #ifdef CONFIG_FEATURE_FIND_NEWER
128 if (newer_mtime != 0) {
129 time_t file_age = newer_mtime - statbuf->st_mtime;
134 #ifdef CONFIG_FEATURE_FIND_INUM
135 if (inode_num != 0) {
136 if (!(statbuf->st_ino == inode_num))
140 #ifdef CONFIG_FEATURE_FIND_EXEC
143 char *cmd_string = "";
144 for (i = 0; i < num_matches; i++)
145 cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
146 cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
152 #ifdef CONFIG_FEATURE_FIND_PRINT0
153 printf("%s%c", fileName, printsep);
161 #ifdef CONFIG_FEATURE_FIND_TYPE
162 static int find_type(char *type)
190 if (mask == 0 || type[1] != '\0')
191 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
197 int find_main(int argc, char **argv)
199 int dereference = FALSE;
200 int i, firstopt, status = EXIT_SUCCESS;
202 for (firstopt = 1; firstopt < argc; firstopt++) {
203 if (argv[firstopt][0] == '-')
207 /* Parse any options */
208 for (i = firstopt; i < argc; i++) {
209 if (strcmp(argv[i], "-follow") == 0)
211 else if (strcmp(argv[i], "-print") == 0) {
214 #ifdef CONFIG_FEATURE_FIND_PRINT0
215 else if (strcmp(argv[i], "-print0") == 0)
218 else if (strcmp(argv[i], "-name") == 0) {
220 bb_error_msg_and_die(msg_req_arg, "-name");
222 #ifdef CONFIG_FEATURE_FIND_TYPE
223 } else if (strcmp(argv[i], "-type") == 0) {
225 bb_error_msg_and_die(msg_req_arg, "-type");
226 type_mask = find_type(argv[i]);
228 #ifdef CONFIG_FEATURE_FIND_PERM
229 } else if (strcmp(argv[i], "-perm") == 0) {
232 bb_error_msg_and_die(msg_req_arg, "-perm");
233 perm_mask = strtol(argv[i], &end, 8);
234 if ((end[0] != '\0') || (perm_mask > 07777))
235 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
236 if ((perm_char = argv[i][0]) == '-')
237 perm_mask = -perm_mask;
239 #ifdef CONFIG_FEATURE_FIND_MTIME
240 } else if (strcmp(argv[i], "-mtime") == 0) {
243 bb_error_msg_and_die(msg_req_arg, "-mtime");
244 mtime_days = strtol(argv[i], &end, 10);
246 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
247 if ((mtime_char = argv[i][0]) == '-')
248 mtime_days = -mtime_days;
250 #ifdef CONFIG_FEATURE_FIND_MMIN
251 } else if (strcmp(argv[i], "-mmin") == 0) {
254 bb_error_msg_and_die(msg_req_arg, "-mmin");
255 mmin_mins = strtol(argv[i], &end, 10);
257 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mmin");
258 if ((mmin_char = argv[i][0]) == '-')
259 mmin_mins = -mmin_mins;
261 #ifdef CONFIG_FEATURE_FIND_XDEV
262 } else if (strcmp(argv[i], "-xdev") == 0) {
265 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
266 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
268 if ( firstopt == 1 ) {
269 xstat ( ".", &stbuf );
270 xdev_dev [0] = stbuf. st_dev;
274 for (i = 1; i < firstopt; i++) {
275 xstat ( argv [i], &stbuf );
276 xdev_dev [i-1] = stbuf. st_dev;
280 #ifdef CONFIG_FEATURE_FIND_NEWER
281 } else if (strcmp(argv[i], "-newer") == 0) {
282 struct stat stat_newer;
284 bb_error_msg_and_die(msg_req_arg, "-newer");
285 xstat (argv[i], &stat_newer);
286 newer_mtime = stat_newer.st_mtime;
288 #ifdef CONFIG_FEATURE_FIND_INUM
289 } else if (strcmp(argv[i], "-inum") == 0) {
292 bb_error_msg_and_die(msg_req_arg, "-inum");
293 inode_num = strtol(argv[i], &end, 10);
295 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
297 #ifdef CONFIG_FEATURE_FIND_EXEC
298 } else if (strcmp(argv[i], "-exec") == 0) {
300 char *cmd_string = "";
304 bb_error_msg_and_die(msg_req_arg, "-exec");
307 cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
310 if (*cmd_string == 0)
311 bb_error_msg_and_die(msg_req_arg, "-exec");
313 exec_str = xmalloc(sizeof(char *));
315 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
317 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
318 exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
319 cmd_string += b_pos + 2;
321 exec_str[num_matches] = bb_xstrdup(cmd_string);
329 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
331 status = EXIT_FAILURE;
333 for (i = 1; i < firstopt; i++) {
334 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
336 status = EXIT_FAILURE;