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 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 //XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz
37 static const char msg_req_arg[] = "option `%s' requires an argument";
38 static const char msg_invalid_arg[] = "invalid argument `%s' to `%s'";
42 #ifdef CONFIG_FEATURE_FIND_TYPE
43 static int type_mask = 0;
46 #ifdef CONFIG_FEATURE_FIND_PERM
47 static char perm_char = 0;
48 static int perm_mask = 0;
51 #ifdef CONFIG_FEATURE_FIND_MTIME
52 static char mtime_char;
53 static int mtime_days;
56 #ifdef CONFIG_FEATURE_FIND_MMIN
57 static char mmin_char;
61 #ifdef CONFIG_FEATURE_FIND_XDEV
62 static dev_t *xdev_dev;
63 static int xdev_count = 0;
66 #ifdef CONFIG_FEATURE_FIND_NEWER
67 static time_t newer_mtime;
70 #ifdef CONFIG_FEATURE_FIND_INUM
71 static ino_t inode_num;
74 #ifdef CONFIG_FEATURE_FIND_EXEC
75 static char **exec_str;
76 static int num_matches;
80 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
82 if (pattern != NULL) {
83 const char *tmp = strrchr(fileName, '/');
89 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
92 #ifdef CONFIG_FEATURE_FIND_TYPE
94 if (!((statbuf->st_mode & S_IFMT) == type_mask))
98 #ifdef CONFIG_FEATURE_FIND_PERM
100 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
101 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
102 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
106 #ifdef CONFIG_FEATURE_FIND_MTIME
107 if (mtime_char != 0) {
108 time_t file_age = time(NULL) - statbuf->st_mtime;
109 time_t mtime_secs = mtime_days * 24 * 60 * 60;
110 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
111 file_age < mtime_secs + 24 * 60 * 60) ||
112 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
113 (mtime_char == '-' && file_age < mtime_secs)))
117 #ifdef CONFIG_FEATURE_FIND_MMIN
118 if (mmin_char != 0) {
119 time_t file_age = time(NULL) - statbuf->st_mtime;
120 time_t mmin_secs = mmin_mins * 60;
121 if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
122 file_age < mmin_secs + 60) ||
123 (mmin_char == '+' && file_age >= mmin_secs + 60) ||
124 (mmin_char == '-' && file_age < mmin_secs)))
128 #ifdef CONFIG_FEATURE_FIND_XDEV
131 for (i=0; i<xdev_count; i++) {
132 if (xdev_dev[i] == statbuf-> st_dev)
135 if (i == xdev_count) {
136 if(S_ISDIR(statbuf->st_mode))
143 #ifdef CONFIG_FEATURE_FIND_NEWER
144 if (newer_mtime != 0) {
145 time_t file_age = newer_mtime - statbuf->st_mtime;
150 #ifdef CONFIG_FEATURE_FIND_INUM
151 if (inode_num != 0) {
152 if (!(statbuf->st_ino == inode_num))
156 #ifdef CONFIG_FEATURE_FIND_EXEC
159 char *cmd_string = "";
160 for (i = 0; i < num_matches; i++)
161 cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
162 cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
173 #ifdef CONFIG_FEATURE_FIND_TYPE
174 static int find_type(char *type)
202 if (mask == 0 || type[1] != '\0')
203 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
209 int find_main(int argc, char **argv)
211 int dereference = FALSE;
212 int i, firstopt, status = EXIT_SUCCESS;
214 for (firstopt = 1; firstopt < argc; firstopt++) {
215 if (argv[firstopt][0] == '-')
219 /* Parse any options */
220 for (i = firstopt; i < argc; i++) {
221 if (strcmp(argv[i], "-follow") == 0)
223 else if (strcmp(argv[i], "-print") == 0) {
226 else if (strcmp(argv[i], "-name") == 0) {
228 bb_error_msg_and_die(msg_req_arg, "-name");
230 #ifdef CONFIG_FEATURE_FIND_TYPE
231 } else if (strcmp(argv[i], "-type") == 0) {
233 bb_error_msg_and_die(msg_req_arg, "-type");
234 type_mask = find_type(argv[i]);
236 #ifdef CONFIG_FEATURE_FIND_PERM
237 } else if (strcmp(argv[i], "-perm") == 0) {
240 bb_error_msg_and_die(msg_req_arg, "-perm");
241 perm_mask = strtol(argv[i], &end, 8);
242 if ((end[0] != '\0') || (perm_mask > 07777))
243 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
244 if ((perm_char = argv[i][0]) == '-')
245 perm_mask = -perm_mask;
247 #ifdef CONFIG_FEATURE_FIND_MTIME
248 } else if (strcmp(argv[i], "-mtime") == 0) {
251 bb_error_msg_and_die(msg_req_arg, "-mtime");
252 mtime_days = strtol(argv[i], &end, 10);
254 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
255 if ((mtime_char = argv[i][0]) == '-')
256 mtime_days = -mtime_days;
258 #ifdef CONFIG_FEATURE_FIND_MMIN
259 } else if (strcmp(argv[i], "-mmin") == 0) {
262 bb_error_msg_and_die(msg_req_arg, "-mmin");
263 mmin_mins = strtol(argv[i], &end, 10);
265 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mmin");
266 if ((mmin_char = argv[i][0]) == '-')
267 mmin_mins = -mmin_mins;
269 #ifdef CONFIG_FEATURE_FIND_XDEV
270 } else if (strcmp(argv[i], "-xdev") == 0) {
273 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
274 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
276 if ( firstopt == 1 ) {
277 xstat ( ".", &stbuf );
278 xdev_dev [0] = stbuf. st_dev;
282 for (i = 1; i < firstopt; i++) {
283 xstat ( argv [i], &stbuf );
284 xdev_dev [i-1] = stbuf. st_dev;
288 #ifdef CONFIG_FEATURE_FIND_NEWER
289 } else if (strcmp(argv[i], "-newer") == 0) {
290 struct stat stat_newer;
292 bb_error_msg_and_die(msg_req_arg, "-newer");
293 xstat (argv[i], &stat_newer);
294 newer_mtime = stat_newer.st_mtime;
296 #ifdef CONFIG_FEATURE_FIND_INUM
297 } else if (strcmp(argv[i], "-inum") == 0) {
300 bb_error_msg_and_die(msg_req_arg, "-inum");
301 inode_num = strtol(argv[i], &end, 10);
303 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
305 #ifdef CONFIG_FEATURE_FIND_EXEC
306 } else if (strcmp(argv[i], "-exec") == 0) {
308 char *cmd_string = "";
312 bb_error_msg_and_die(msg_req_arg, "-exec");
315 cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
318 if (*cmd_string == 0)
319 bb_error_msg_and_die(msg_req_arg, "-exec");
321 exec_str = xmalloc(sizeof(char *));
323 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
325 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
326 exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
327 cmd_string += b_pos + 2;
329 exec_str[num_matches] = bb_xstrdup(cmd_string);
337 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
339 status = EXIT_FAILURE;
341 for (i = 1; i < firstopt; i++) {
342 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
344 status = EXIT_FAILURE;