blob: 7a71af9eb6f8fa0d27c6a171cff917ea89f31156 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen17d49ef1999-10-06 20:25:32 +00002/*
3 * Mini find implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersenc4996011999-10-20 22:08:37 +00006 *
Matt Kraai096370d2001-02-07 03:52:38 +00007 * Reworked by David Douthitt <n9ubh@callsign.net> and
8 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
Eric Andersen17d49ef1999-10-06 20:25:32 +00009 *
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.
14 *
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.
19 *
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
23 *
24 */
25
26#include <stdio.h>
27#include <unistd.h>
28#include <dirent.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <string.h>
30#include <stdlib.h>
Matt Kraai096370d2001-02-07 03:52:38 +000031#include <fnmatch.h>
32#include <time.h>
33#include <ctype.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000034#include "busybox.h"
Eric Andersen17d49ef1999-10-06 20:25:32 +000035
Eric Andersen97d86f22003-01-23 05:27:42 +000036//XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz
"Vladimir N. Oleynik"007a0112005-09-22 11:11:11 +000037static const char msg_req_arg[] = "option `%s' requires an argument";
38static const char msg_invalid_arg[] = "invalid argument `%s' to `%s'";
Eric Andersen17d49ef1999-10-06 20:25:32 +000039
Matt Kraai096370d2001-02-07 03:52:38 +000040static char *pattern;
41
Rob Landleycee605c2005-10-06 16:39:17 +000042#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +000043static int type_mask = 0;
44#endif
45
Rob Landleycee605c2005-10-06 16:39:17 +000046#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +000047static char perm_char = 0;
48static int perm_mask = 0;
49#endif
50
Rob Landleycee605c2005-10-06 16:39:17 +000051#ifdef CONFIG_FEATURE_FIND_MTIME
Matt Kraai096370d2001-02-07 03:52:38 +000052static char mtime_char;
53static int mtime_days;
54#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +000055
Paul Fox72d1a232006-01-13 21:05:41 +000056#ifdef CONFIG_FEATURE_FIND_MMIN
57static char mmin_char;
58static int mmin_mins;
59#endif
60
Rob Landleycee605c2005-10-06 16:39:17 +000061#ifdef CONFIG_FEATURE_FIND_XDEV
Robert Griebl41369af2002-07-24 00:34:48 +000062static dev_t *xdev_dev;
63static int xdev_count = 0;
64#endif
65
Rob Landleycee605c2005-10-06 16:39:17 +000066#ifdef CONFIG_FEATURE_FIND_NEWER
Eric Andersen14f5c8d2005-04-16 19:39:00 +000067static time_t newer_mtime;
Eric Andersen97d86f22003-01-23 05:27:42 +000068#endif
69
Rob Landleycee605c2005-10-06 16:39:17 +000070#ifdef CONFIG_FEATURE_FIND_INUM
Eric Andersen97d86f22003-01-23 05:27:42 +000071static ino_t inode_num;
72#endif
Robert Griebl41369af2002-07-24 00:34:48 +000073
Rob Landleycee605c2005-10-06 16:39:17 +000074#ifdef CONFIG_FEATURE_FIND_EXEC
Rob Landley5d3a0e82005-10-04 03:34:39 +000075static char **exec_str;
76static int num_matches;
77static int exec_opt;
78#endif
79
Erik Andersen3364d782000-03-28 00:58:14 +000080static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersen17d49ef1999-10-06 20:25:32 +000081{
Paul Fox8416a2d2006-03-27 16:42:33 +000082#ifdef CONFIG_FEATURE_FIND_XDEV
83 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
84 int i;
85 for (i=0; i<xdev_count; i++) {
86 if (xdev_dev[i] != statbuf->st_dev)
87 return SKIP;
88 }
89 }
90#endif
Matt Kraai096370d2001-02-07 03:52:38 +000091 if (pattern != NULL) {
92 const char *tmp = strrchr(fileName, '/');
Erik Andersene49d5ec2000-02-08 19:58:47 +000093
94 if (tmp == NULL)
Matt Kraai096370d2001-02-07 03:52:38 +000095 tmp = fileName;
Erik Andersene49d5ec2000-02-08 19:58:47 +000096 else
97 tmp++;
Matt Kraai096370d2001-02-07 03:52:38 +000098 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
99 goto no_match;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 }
Rob Landleycee605c2005-10-06 16:39:17 +0000101#ifdef CONFIG_FEATURE_FIND_TYPE
102 if (type_mask != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000103 if (!((statbuf->st_mode & S_IFMT) == type_mask))
104 goto no_match;
105 }
Rob Landleycee605c2005-10-06 16:39:17 +0000106#endif
107#ifdef CONFIG_FEATURE_FIND_PERM
108 if (perm_mask != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000109 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
110 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
111 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
112 goto no_match;
113 }
Rob Landleycee605c2005-10-06 16:39:17 +0000114#endif
115#ifdef CONFIG_FEATURE_FIND_MTIME
116 if (mtime_char != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000117 time_t file_age = time(NULL) - statbuf->st_mtime;
118 time_t mtime_secs = mtime_days * 24 * 60 * 60;
Glenn L McGrath49b0f862002-12-11 21:22:21 +0000119 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
120 file_age < mtime_secs + 24 * 60 * 60) ||
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000121 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
Glenn L McGrath49b0f862002-12-11 21:22:21 +0000122 (mtime_char == '-' && file_age < mtime_secs)))
Matt Kraai096370d2001-02-07 03:52:38 +0000123 goto no_match;
124 }
Rob Landleycee605c2005-10-06 16:39:17 +0000125#endif
Paul Fox72d1a232006-01-13 21:05:41 +0000126#ifdef CONFIG_FEATURE_FIND_MMIN
127 if (mmin_char != 0) {
128 time_t file_age = time(NULL) - statbuf->st_mtime;
129 time_t mmin_secs = mmin_mins * 60;
130 if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
131 file_age < mmin_secs + 60) ||
132 (mmin_char == '+' && file_age >= mmin_secs + 60) ||
133 (mmin_char == '-' && file_age < mmin_secs)))
134 goto no_match;
135 }
136#endif
Rob Landleycee605c2005-10-06 16:39:17 +0000137#ifdef CONFIG_FEATURE_FIND_NEWER
138 if (newer_mtime != 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000139 time_t file_age = newer_mtime - statbuf->st_mtime;
140 if (file_age >= 0)
141 goto no_match;
142 }
Rob Landleycee605c2005-10-06 16:39:17 +0000143#endif
144#ifdef CONFIG_FEATURE_FIND_INUM
145 if (inode_num != 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000146 if (!(statbuf->st_ino == inode_num))
147 goto no_match;
148 }
Rob Landleycee605c2005-10-06 16:39:17 +0000149#endif
150#ifdef CONFIG_FEATURE_FIND_EXEC
151 if (exec_opt) {
Rob Landley5d3a0e82005-10-04 03:34:39 +0000152 int i;
153 char *cmd_string = "";
154 for (i = 0; i < num_matches; i++)
155 cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
156 cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
157 system(cmd_string);
158 goto no_match;
159 }
Rob Landleycee605c2005-10-06 16:39:17 +0000160#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000161
Matt Kraai096370d2001-02-07 03:52:38 +0000162 puts(fileName);
163no_match:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000164 return (TRUE);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000165}
166
Rob Landleycee605c2005-10-06 16:39:17 +0000167#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000168static int find_type(char *type)
169{
170 int mask = 0;
171
172 switch (type[0]) {
173 case 'b':
174 mask = S_IFBLK;
175 break;
176 case 'c':
177 mask = S_IFCHR;
178 break;
179 case 'd':
180 mask = S_IFDIR;
181 break;
182 case 'p':
183 mask = S_IFIFO;
184 break;
185 case 'f':
186 mask = S_IFREG;
187 break;
188 case 'l':
189 mask = S_IFLNK;
190 break;
191 case 's':
192 mask = S_IFSOCK;
193 break;
194 }
195
196 if (mask == 0 || type[1] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000197 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000198
199 return mask;
200}
201#endif
202
Eric Andersen17d49ef1999-10-06 20:25:32 +0000203int find_main(int argc, char **argv)
204{
Matt Kraai096370d2001-02-07 03:52:38 +0000205 int dereference = FALSE;
206 int i, firstopt, status = EXIT_SUCCESS;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000207
Matt Kraai096370d2001-02-07 03:52:38 +0000208 for (firstopt = 1; firstopt < argc; firstopt++) {
209 if (argv[firstopt][0] == '-')
210 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000211 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000212
Erik Andersene49d5ec2000-02-08 19:58:47 +0000213 /* Parse any options */
Matt Kraai096370d2001-02-07 03:52:38 +0000214 for (i = firstopt; i < argc; i++) {
215 if (strcmp(argv[i], "-follow") == 0)
216 dereference = TRUE;
Mark Whitleye0a7f912001-03-28 22:04:42 +0000217 else if (strcmp(argv[i], "-print") == 0) {
218 ;
219 }
Matt Kraai096370d2001-02-07 03:52:38 +0000220 else if (strcmp(argv[i], "-name") == 0) {
221 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000222 bb_error_msg_and_die(msg_req_arg, "-name");
Matt Kraai096370d2001-02-07 03:52:38 +0000223 pattern = argv[i];
Rob Landleycee605c2005-10-06 16:39:17 +0000224#ifdef CONFIG_FEATURE_FIND_TYPE
225 } else if (strcmp(argv[i], "-type") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000226 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000227 bb_error_msg_and_die(msg_req_arg, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000228 type_mask = find_type(argv[i]);
Rob Landleycee605c2005-10-06 16:39:17 +0000229#endif
230#ifdef CONFIG_FEATURE_FIND_PERM
231 } else if (strcmp(argv[i], "-perm") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000232 char *end;
233 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 bb_error_msg_and_die(msg_req_arg, "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000235 perm_mask = strtol(argv[i], &end, 8);
Eric Andersen97d86f22003-01-23 05:27:42 +0000236 if ((end[0] != '\0') || (perm_mask > 07777))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000237 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000238 if ((perm_char = argv[i][0]) == '-')
239 perm_mask = -perm_mask;
Rob Landleycee605c2005-10-06 16:39:17 +0000240#endif
241#ifdef CONFIG_FEATURE_FIND_MTIME
242 } else if (strcmp(argv[i], "-mtime") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000243 char *end;
244 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000245 bb_error_msg_and_die(msg_req_arg, "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000246 mtime_days = strtol(argv[i], &end, 10);
247 if (end[0] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000248 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000249 if ((mtime_char = argv[i][0]) == '-')
250 mtime_days = -mtime_days;
Rob Landleycee605c2005-10-06 16:39:17 +0000251#endif
Paul Fox72d1a232006-01-13 21:05:41 +0000252#ifdef CONFIG_FEATURE_FIND_MMIN
253 } else if (strcmp(argv[i], "-mmin") == 0) {
254 char *end;
255 if (++i == argc)
256 bb_error_msg_and_die(msg_req_arg, "-mmin");
257 mmin_mins = strtol(argv[i], &end, 10);
258 if (end[0] != '\0')
259 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mmin");
260 if ((mmin_char = argv[i][0]) == '-')
261 mmin_mins = -mmin_mins;
262#endif
Rob Landleycee605c2005-10-06 16:39:17 +0000263#ifdef CONFIG_FEATURE_FIND_XDEV
264 } else if (strcmp(argv[i], "-xdev") == 0) {
Robert Griebl41369af2002-07-24 00:34:48 +0000265 struct stat stbuf;
266
267 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
268 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
269
270 if ( firstopt == 1 ) {
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000271 xstat ( ".", &stbuf );
Robert Griebl41369af2002-07-24 00:34:48 +0000272 xdev_dev [0] = stbuf. st_dev;
273 }
274 else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000275
Robert Griebl41369af2002-07-24 00:34:48 +0000276 for (i = 1; i < firstopt; i++) {
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000277 xstat ( argv [i], &stbuf );
Robert Griebl41369af2002-07-24 00:34:48 +0000278 xdev_dev [i-1] = stbuf. st_dev;
279 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000280 }
Rob Landleycee605c2005-10-06 16:39:17 +0000281#endif
282#ifdef CONFIG_FEATURE_FIND_NEWER
283 } else if (strcmp(argv[i], "-newer") == 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000284 struct stat stat_newer;
285 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000286 bb_error_msg_and_die(msg_req_arg, "-newer");
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000287 xstat (argv[i], &stat_newer);
Eric Andersen97d86f22003-01-23 05:27:42 +0000288 newer_mtime = stat_newer.st_mtime;
Rob Landleycee605c2005-10-06 16:39:17 +0000289#endif
290#ifdef CONFIG_FEATURE_FIND_INUM
291 } else if (strcmp(argv[i], "-inum") == 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000292 char *end;
293 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000294 bb_error_msg_and_die(msg_req_arg, "-inum");
Eric Andersen97d86f22003-01-23 05:27:42 +0000295 inode_num = strtol(argv[i], &end, 10);
296 if (end[0] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000297 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
Rob Landleycee605c2005-10-06 16:39:17 +0000298#endif
299#ifdef CONFIG_FEATURE_FIND_EXEC
300 } else if (strcmp(argv[i], "-exec") == 0) {
Rob Landley5d3a0e82005-10-04 03:34:39 +0000301 int b_pos;
302 char *cmd_string = "";
303
304 while (i++) {
305 if (i == argc)
306 bb_error_msg_and_die(msg_req_arg, "-exec");
307 if (*argv[i] == ';')
308 break;
309 cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
310 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000311
Rob Landley5d3a0e82005-10-04 03:34:39 +0000312 if (*cmd_string == 0)
313 bb_error_msg_and_die(msg_req_arg, "-exec");
314 cmd_string++;
315 exec_str = xmalloc(sizeof(char *));
316
317 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
318 num_matches++;
319 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
320 exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
321 cmd_string += b_pos + 2;
322 }
323 exec_str[num_matches] = bb_xstrdup(cmd_string);
324 exec_opt = 1;
Rob Landleycee605c2005-10-06 16:39:17 +0000325#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000326 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000327 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000328 }
329
Matt Kraai096370d2001-02-07 03:52:38 +0000330 if (firstopt == 1) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000331 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
332 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000333 status = EXIT_FAILURE;
334 } else {
335 for (i = 1; i < firstopt; i++) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000336 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
337 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000338 status = EXIT_FAILURE;
339 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000340 }
341
Matt Kraai096370d2001-02-07 03:52:38 +0000342 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000343}