blob: aa915fa0f55d79f5507f96e7b0801ed9d5df8d63 [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 *
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000010 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen17d49ef1999-10-06 20:25:32 +000011 */
12
Bernhard Reutner-Fischere15d7572006-06-02 20:56:16 +000013#include "busybox.h"
Matt Kraai096370d2001-02-07 03:52:38 +000014#include <fnmatch.h>
Eric Andersen17d49ef1999-10-06 20:25:32 +000015
Matt Kraai096370d2001-02-07 03:52:38 +000016static char *pattern;
Paul Foxd7384292006-05-12 14:47:20 +000017#ifdef CONFIG_FEATURE_FIND_PRINT0
18static char printsep = '\n';
19#endif
Matt Kraai096370d2001-02-07 03:52:38 +000020
Rob Landleycee605c2005-10-06 16:39:17 +000021#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +000022static int type_mask = 0;
23#endif
24
Rob Landleycee605c2005-10-06 16:39:17 +000025#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +000026static char perm_char = 0;
27static int perm_mask = 0;
28#endif
29
Rob Landleycee605c2005-10-06 16:39:17 +000030#ifdef CONFIG_FEATURE_FIND_MTIME
Matt Kraai096370d2001-02-07 03:52:38 +000031static char mtime_char;
32static int mtime_days;
33#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +000034
Paul Fox72d1a232006-01-13 21:05:41 +000035#ifdef CONFIG_FEATURE_FIND_MMIN
36static char mmin_char;
37static int mmin_mins;
38#endif
39
Rob Landleycee605c2005-10-06 16:39:17 +000040#ifdef CONFIG_FEATURE_FIND_XDEV
Robert Griebl41369af2002-07-24 00:34:48 +000041static dev_t *xdev_dev;
42static int xdev_count = 0;
43#endif
44
Rob Landleycee605c2005-10-06 16:39:17 +000045#ifdef CONFIG_FEATURE_FIND_NEWER
Eric Andersen14f5c8d2005-04-16 19:39:00 +000046static time_t newer_mtime;
Eric Andersen97d86f22003-01-23 05:27:42 +000047#endif
48
Rob Landleycee605c2005-10-06 16:39:17 +000049#ifdef CONFIG_FEATURE_FIND_INUM
Eric Andersen97d86f22003-01-23 05:27:42 +000050static ino_t inode_num;
51#endif
Robert Griebl41369af2002-07-24 00:34:48 +000052
Rob Landleycee605c2005-10-06 16:39:17 +000053#ifdef CONFIG_FEATURE_FIND_EXEC
Rob Landley5d3a0e82005-10-04 03:34:39 +000054static char **exec_str;
55static int num_matches;
56static int exec_opt;
57#endif
58
Erik Andersen3364d782000-03-28 00:58:14 +000059static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersen17d49ef1999-10-06 20:25:32 +000060{
Paul Fox8416a2d2006-03-27 16:42:33 +000061#ifdef CONFIG_FEATURE_FIND_XDEV
62 if (S_ISDIR(statbuf->st_mode) && xdev_count) {
63 int i;
64 for (i=0; i<xdev_count; i++) {
65 if (xdev_dev[i] != statbuf->st_dev)
66 return SKIP;
67 }
68 }
69#endif
Matt Kraai096370d2001-02-07 03:52:38 +000070 if (pattern != NULL) {
71 const char *tmp = strrchr(fileName, '/');
Erik Andersene49d5ec2000-02-08 19:58:47 +000072
73 if (tmp == NULL)
Matt Kraai096370d2001-02-07 03:52:38 +000074 tmp = fileName;
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 else
76 tmp++;
Matt Kraai096370d2001-02-07 03:52:38 +000077 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
78 goto no_match;
Erik Andersene49d5ec2000-02-08 19:58:47 +000079 }
Rob Landleycee605c2005-10-06 16:39:17 +000080#ifdef CONFIG_FEATURE_FIND_TYPE
81 if (type_mask != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +000082 if (!((statbuf->st_mode & S_IFMT) == type_mask))
83 goto no_match;
84 }
Rob Landleycee605c2005-10-06 16:39:17 +000085#endif
86#ifdef CONFIG_FEATURE_FIND_PERM
87 if (perm_mask != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +000088 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)))
91 goto no_match;
92 }
Rob Landleycee605c2005-10-06 16:39:17 +000093#endif
94#ifdef CONFIG_FEATURE_FIND_MTIME
95 if (mtime_char != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +000096 time_t file_age = time(NULL) - statbuf->st_mtime;
97 time_t mtime_secs = mtime_days * 24 * 60 * 60;
Glenn L McGrath49b0f862002-12-11 21:22:21 +000098 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
99 file_age < mtime_secs + 24 * 60 * 60) ||
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000100 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
Glenn L McGrath49b0f862002-12-11 21:22:21 +0000101 (mtime_char == '-' && file_age < mtime_secs)))
Matt Kraai096370d2001-02-07 03:52:38 +0000102 goto no_match;
103 }
Rob Landleycee605c2005-10-06 16:39:17 +0000104#endif
Paul Fox72d1a232006-01-13 21:05:41 +0000105#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)))
113 goto no_match;
114 }
115#endif
Rob Landleycee605c2005-10-06 16:39:17 +0000116#ifdef CONFIG_FEATURE_FIND_NEWER
117 if (newer_mtime != 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000118 time_t file_age = newer_mtime - statbuf->st_mtime;
119 if (file_age >= 0)
120 goto no_match;
121 }
Rob Landleycee605c2005-10-06 16:39:17 +0000122#endif
123#ifdef CONFIG_FEATURE_FIND_INUM
124 if (inode_num != 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000125 if (!(statbuf->st_ino == inode_num))
126 goto no_match;
127 }
Rob Landleycee605c2005-10-06 16:39:17 +0000128#endif
129#ifdef CONFIG_FEATURE_FIND_EXEC
130 if (exec_opt) {
Rob Landley5d3a0e82005-10-04 03:34:39 +0000131 int i;
132 char *cmd_string = "";
133 for (i = 0; i < num_matches; i++)
Rob Landleyd921b2e2006-08-03 15:41:12 +0000134 cmd_string = xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
135 cmd_string = xasprintf("%s%s", cmd_string, exec_str[num_matches]);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000136 system(cmd_string);
137 goto no_match;
138 }
Rob Landleycee605c2005-10-06 16:39:17 +0000139#endif
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000140
Paul Foxd7384292006-05-12 14:47:20 +0000141#ifdef CONFIG_FEATURE_FIND_PRINT0
142 printf("%s%c", fileName, printsep);
143#else
Matt Kraai096370d2001-02-07 03:52:38 +0000144 puts(fileName);
Paul Foxd7384292006-05-12 14:47:20 +0000145#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000146no_match:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000147 return (TRUE);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000148}
149
Rob Landleycee605c2005-10-06 16:39:17 +0000150#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000151static int find_type(char *type)
152{
153 int mask = 0;
154
155 switch (type[0]) {
156 case 'b':
157 mask = S_IFBLK;
158 break;
159 case 'c':
160 mask = S_IFCHR;
161 break;
162 case 'd':
163 mask = S_IFDIR;
164 break;
165 case 'p':
166 mask = S_IFIFO;
167 break;
168 case 'f':
169 mask = S_IFREG;
170 break;
171 case 'l':
172 mask = S_IFLNK;
173 break;
174 case 's':
175 mask = S_IFSOCK;
176 break;
177 }
178
179 if (mask == 0 || type[1] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000180 bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000181
182 return mask;
183}
184#endif
185
Eric Andersen17d49ef1999-10-06 20:25:32 +0000186int find_main(int argc, char **argv)
187{
Matt Kraai096370d2001-02-07 03:52:38 +0000188 int dereference = FALSE;
189 int i, firstopt, status = EXIT_SUCCESS;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000190
Matt Kraai096370d2001-02-07 03:52:38 +0000191 for (firstopt = 1; firstopt < argc; firstopt++) {
192 if (argv[firstopt][0] == '-')
193 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000194 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000195
Erik Andersene49d5ec2000-02-08 19:58:47 +0000196 /* Parse any options */
Matt Kraai096370d2001-02-07 03:52:38 +0000197 for (i = firstopt; i < argc; i++) {
198 if (strcmp(argv[i], "-follow") == 0)
199 dereference = TRUE;
Mark Whitleye0a7f912001-03-28 22:04:42 +0000200 else if (strcmp(argv[i], "-print") == 0) {
201 ;
202 }
Paul Foxd7384292006-05-12 14:47:20 +0000203#ifdef CONFIG_FEATURE_FIND_PRINT0
204 else if (strcmp(argv[i], "-print0") == 0)
205 printsep = '\0';
206#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000207 else if (strcmp(argv[i], "-name") == 0) {
208 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000209 bb_error_msg_and_die(bb_msg_requires_arg, "-name");
Matt Kraai096370d2001-02-07 03:52:38 +0000210 pattern = argv[i];
Rob Landleycee605c2005-10-06 16:39:17 +0000211#ifdef CONFIG_FEATURE_FIND_TYPE
212 } else if (strcmp(argv[i], "-type") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000213 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000214 bb_error_msg_and_die(bb_msg_requires_arg, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000215 type_mask = find_type(argv[i]);
Rob Landleycee605c2005-10-06 16:39:17 +0000216#endif
217#ifdef CONFIG_FEATURE_FIND_PERM
218 } else if (strcmp(argv[i], "-perm") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000219 char *end;
220 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000221 bb_error_msg_and_die(bb_msg_requires_arg, "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000222 perm_mask = strtol(argv[i], &end, 8);
Eric Andersen97d86f22003-01-23 05:27:42 +0000223 if ((end[0] != '\0') || (perm_mask > 07777))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000224 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000225 if ((perm_char = argv[i][0]) == '-')
226 perm_mask = -perm_mask;
Rob Landleycee605c2005-10-06 16:39:17 +0000227#endif
228#ifdef CONFIG_FEATURE_FIND_MTIME
229 } else if (strcmp(argv[i], "-mtime") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000230 char *end;
231 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000232 bb_error_msg_and_die(bb_msg_requires_arg, "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000233 mtime_days = strtol(argv[i], &end, 10);
234 if (end[0] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000235 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000236 if ((mtime_char = argv[i][0]) == '-')
237 mtime_days = -mtime_days;
Rob Landleycee605c2005-10-06 16:39:17 +0000238#endif
Paul Fox72d1a232006-01-13 21:05:41 +0000239#ifdef CONFIG_FEATURE_FIND_MMIN
240 } else if (strcmp(argv[i], "-mmin") == 0) {
241 char *end;
242 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000243 bb_error_msg_and_die(bb_msg_requires_arg, "-mmin");
Paul Fox72d1a232006-01-13 21:05:41 +0000244 mmin_mins = strtol(argv[i], &end, 10);
245 if (end[0] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000246 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-mmin");
Paul Fox72d1a232006-01-13 21:05:41 +0000247 if ((mmin_char = argv[i][0]) == '-')
248 mmin_mins = -mmin_mins;
249#endif
Rob Landleycee605c2005-10-06 16:39:17 +0000250#ifdef CONFIG_FEATURE_FIND_XDEV
251 } else if (strcmp(argv[i], "-xdev") == 0) {
Robert Griebl41369af2002-07-24 00:34:48 +0000252 struct stat stbuf;
253
254 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
255 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
256
257 if ( firstopt == 1 ) {
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000258 xstat ( ".", &stbuf );
Robert Griebl41369af2002-07-24 00:34:48 +0000259 xdev_dev [0] = stbuf. st_dev;
260 }
261 else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000262
Robert Griebl41369af2002-07-24 00:34:48 +0000263 for (i = 1; i < firstopt; i++) {
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000264 xstat ( argv [i], &stbuf );
Robert Griebl41369af2002-07-24 00:34:48 +0000265 xdev_dev [i-1] = stbuf. st_dev;
266 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000267 }
Rob Landleycee605c2005-10-06 16:39:17 +0000268#endif
269#ifdef CONFIG_FEATURE_FIND_NEWER
270 } else if (strcmp(argv[i], "-newer") == 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000271 struct stat stat_newer;
272 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000273 bb_error_msg_and_die(bb_msg_requires_arg, "-newer");
Rob Landleyc5b1d4d2006-03-13 15:45:16 +0000274 xstat (argv[i], &stat_newer);
Eric Andersen97d86f22003-01-23 05:27:42 +0000275 newer_mtime = stat_newer.st_mtime;
Rob Landleycee605c2005-10-06 16:39:17 +0000276#endif
277#ifdef CONFIG_FEATURE_FIND_INUM
278 } else if (strcmp(argv[i], "-inum") == 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000279 char *end;
280 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000281 bb_error_msg_and_die(bb_msg_requires_arg, "-inum");
Eric Andersen97d86f22003-01-23 05:27:42 +0000282 inode_num = strtol(argv[i], &end, 10);
283 if (end[0] != '\0')
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000284 bb_error_msg_and_die(bb_msg_invalid_arg, argv[i], "-inum");
Rob Landleycee605c2005-10-06 16:39:17 +0000285#endif
286#ifdef CONFIG_FEATURE_FIND_EXEC
287 } else if (strcmp(argv[i], "-exec") == 0) {
Rob Landley5d3a0e82005-10-04 03:34:39 +0000288 int b_pos;
289 char *cmd_string = "";
290
291 while (i++) {
292 if (i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000293 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Rob Landley5d3a0e82005-10-04 03:34:39 +0000294 if (*argv[i] == ';')
295 break;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000296 cmd_string = xasprintf("%s %s", cmd_string, argv[i]);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000297 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000298
Rob Landley5d3a0e82005-10-04 03:34:39 +0000299 if (*cmd_string == 0)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000300 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Rob Landley5d3a0e82005-10-04 03:34:39 +0000301 cmd_string++;
302 exec_str = xmalloc(sizeof(char *));
303
304 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
305 num_matches++;
306 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000307 exec_str[num_matches - 1] = xstrndup(cmd_string, b_pos);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000308 cmd_string += b_pos + 2;
309 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000310 exec_str[num_matches] = xstrdup(cmd_string);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000311 exec_opt = 1;
Rob Landleycee605c2005-10-06 16:39:17 +0000312#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000313 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000314 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000315 }
316
Matt Kraai096370d2001-02-07 03:52:38 +0000317 if (firstopt == 1) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000318 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
319 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000320 status = EXIT_FAILURE;
321 } else {
322 for (i = 1; i < firstopt; i++) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000323 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
324 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000325 status = EXIT_FAILURE;
326 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000327 }
328
Matt Kraai096370d2001-02-07 03:52:38 +0000329 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000330}