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.
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) {
220 bb_error_msg_and_die(bb_msg_requires_arg, "-perm");
221 perm_mask = xstrtol_range(argv[i], 8, 0, 07777);
222 perm_char = argv[i][0];
223 if (perm_char == '-')
224 perm_mask = -perm_mask;
226 #ifdef CONFIG_FEATURE_FIND_MTIME
227 } else if (strcmp(argv[i], "-mtime") == 0) {
229 bb_error_msg_and_die(bb_msg_requires_arg, "-mtime");
230 mtime_days = xatol(argv[i]);
231 mtime_char = argv[i][0];
232 if (mtime_char == '-')
233 mtime_days = -mtime_days;
235 #ifdef CONFIG_FEATURE_FIND_MMIN
236 } else if (strcmp(argv[i], "-mmin") == 0) {
238 bb_error_msg_and_die(bb_msg_requires_arg, "-mmin");
239 mmin_mins = xatol(argv[i]);
240 mmin_char = argv[i][0];
241 if (mmin_char == '-')
242 mmin_mins = -mmin_mins;
244 #ifdef CONFIG_FEATURE_FIND_XDEV
245 } else if (strcmp(argv[i], "-xdev") == 0) {
248 xdev_count = (firstopt - 1) ? (firstopt - 1) : 1;
249 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
253 xdev_dev[0] = stbuf.st_dev;
257 for (i = 1; i < firstopt; i++) {
258 xstat(argv[i], &stbuf);
259 xdev_dev[i-1] = stbuf.st_dev;
263 #ifdef CONFIG_FEATURE_FIND_NEWER
264 } else if (strcmp(argv[i], "-newer") == 0) {
265 struct stat stat_newer;
267 bb_error_msg_and_die(bb_msg_requires_arg, "-newer");
268 xstat(argv[i], &stat_newer);
269 newer_mtime = stat_newer.st_mtime;
271 #ifdef CONFIG_FEATURE_FIND_INUM
272 } else if (strcmp(argv[i], "-inum") == 0) {
274 bb_error_msg_and_die(bb_msg_requires_arg, "-inum");
275 inode_num = xatoul(argv[i]);
277 #ifdef CONFIG_FEATURE_FIND_EXEC
278 } else if (strcmp(argv[i], "-exec") == 0) {
280 char *cmd_string = "";
284 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
287 cmd_string = xasprintf("%s %s", cmd_string, argv[i]);
290 if (*cmd_string == 0)
291 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
293 exec_str = xmalloc(sizeof(char *));
295 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
297 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
298 exec_str[num_matches - 1] = xstrndup(cmd_string, b_pos);
299 cmd_string += b_pos + 2;
301 exec_str[num_matches] = xstrdup(cmd_string);
309 if (!recursive_action(".", TRUE, dereference, FALSE, fileAction,
311 status = EXIT_FAILURE;
313 for (i = 1; i < firstopt; i++) {
314 if (!recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
316 status = EXIT_FAILURE;