blob: 913a778eb84382d08bd3a9323db57bdfcca65d77 [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 *
Rob Landleye9a7a622006-09-22 02:52:41 +000010 * Licensed under the GPL version 2, 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 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000220 bb_error_msg_and_die(bb_msg_requires_arg, "-perm");
Denis Vlasenko13858992006-10-08 12:49:22 +0000221 perm_mask = xstrtol_range(argv[i], 8, 0, 07777);
222 perm_char = argv[i][0];
223 if (perm_char == '-')
Matt Kraai096370d2001-02-07 03:52:38 +0000224 perm_mask = -perm_mask;
Rob Landleycee605c2005-10-06 16:39:17 +0000225#endif
226#ifdef CONFIG_FEATURE_FIND_MTIME
227 } else if (strcmp(argv[i], "-mtime") == 0) {
Matt Kraai096370d2001-02-07 03:52:38 +0000228 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000229 bb_error_msg_and_die(bb_msg_requires_arg, "-mtime");
Denis Vlasenko13858992006-10-08 12:49:22 +0000230 mtime_days = xatol(argv[i]);
231 mtime_char = argv[i][0];
232 if (mtime_char == '-')
Matt Kraai096370d2001-02-07 03:52:38 +0000233 mtime_days = -mtime_days;
Rob Landleycee605c2005-10-06 16:39:17 +0000234#endif
Paul Fox72d1a232006-01-13 21:05:41 +0000235#ifdef CONFIG_FEATURE_FIND_MMIN
236 } else if (strcmp(argv[i], "-mmin") == 0) {
Paul Fox72d1a232006-01-13 21:05:41 +0000237 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000238 bb_error_msg_and_die(bb_msg_requires_arg, "-mmin");
Denis Vlasenko13858992006-10-08 12:49:22 +0000239 mmin_mins = xatol(argv[i]);
240 mmin_char = argv[i][0];
241 if (mmin_char == '-')
Paul Fox72d1a232006-01-13 21:05:41 +0000242 mmin_mins = -mmin_mins;
243#endif
Rob Landleycee605c2005-10-06 16:39:17 +0000244#ifdef CONFIG_FEATURE_FIND_XDEV
245 } else if (strcmp(argv[i], "-xdev") == 0) {
Robert Griebl41369af2002-07-24 00:34:48 +0000246 struct stat stbuf;
247
Denis Vlasenko13858992006-10-08 12:49:22 +0000248 xdev_count = (firstopt - 1) ? (firstopt - 1) : 1;
249 xdev_dev = xmalloc(xdev_count * sizeof(dev_t));
Robert Griebl41369af2002-07-24 00:34:48 +0000250
Denis Vlasenko13858992006-10-08 12:49:22 +0000251 if (firstopt == 1) {
252 xstat(".", &stbuf);
253 xdev_dev[0] = stbuf.st_dev;
Robert Griebl41369af2002-07-24 00:34:48 +0000254 }
255 else {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000256
Robert Griebl41369af2002-07-24 00:34:48 +0000257 for (i = 1; i < firstopt; i++) {
Denis Vlasenko13858992006-10-08 12:49:22 +0000258 xstat(argv[i], &stbuf);
259 xdev_dev[i-1] = stbuf.st_dev;
Robert Griebl41369af2002-07-24 00:34:48 +0000260 }
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000261 }
Rob Landleycee605c2005-10-06 16:39:17 +0000262#endif
263#ifdef CONFIG_FEATURE_FIND_NEWER
264 } else if (strcmp(argv[i], "-newer") == 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000265 struct stat stat_newer;
266 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000267 bb_error_msg_and_die(bb_msg_requires_arg, "-newer");
Denis Vlasenko13858992006-10-08 12:49:22 +0000268 xstat(argv[i], &stat_newer);
Eric Andersen97d86f22003-01-23 05:27:42 +0000269 newer_mtime = stat_newer.st_mtime;
Rob Landleycee605c2005-10-06 16:39:17 +0000270#endif
271#ifdef CONFIG_FEATURE_FIND_INUM
272 } else if (strcmp(argv[i], "-inum") == 0) {
Eric Andersen97d86f22003-01-23 05:27:42 +0000273 if (++i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000274 bb_error_msg_and_die(bb_msg_requires_arg, "-inum");
Denis Vlasenko13858992006-10-08 12:49:22 +0000275 inode_num = xatoul(argv[i]);
Rob Landleycee605c2005-10-06 16:39:17 +0000276#endif
277#ifdef CONFIG_FEATURE_FIND_EXEC
278 } else if (strcmp(argv[i], "-exec") == 0) {
Rob Landley5d3a0e82005-10-04 03:34:39 +0000279 int b_pos;
280 char *cmd_string = "";
281
282 while (i++) {
283 if (i == argc)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000284 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Rob Landley5d3a0e82005-10-04 03:34:39 +0000285 if (*argv[i] == ';')
286 break;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000287 cmd_string = xasprintf("%s %s", cmd_string, argv[i]);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000288 }
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000289
Rob Landley5d3a0e82005-10-04 03:34:39 +0000290 if (*cmd_string == 0)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000291 bb_error_msg_and_die(bb_msg_requires_arg, "-exec");
Rob Landley5d3a0e82005-10-04 03:34:39 +0000292 cmd_string++;
293 exec_str = xmalloc(sizeof(char *));
294
295 while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
296 num_matches++;
297 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000298 exec_str[num_matches - 1] = xstrndup(cmd_string, b_pos);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000299 cmd_string += b_pos + 2;
300 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000301 exec_str[num_matches] = xstrdup(cmd_string);
Rob Landley5d3a0e82005-10-04 03:34:39 +0000302 exec_opt = 1;
Rob Landleycee605c2005-10-06 16:39:17 +0000303#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000304 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000305 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000306 }
307
Matt Kraai096370d2001-02-07 03:52:38 +0000308 if (firstopt == 1) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000309 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
310 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000311 status = EXIT_FAILURE;
312 } else {
313 for (i = 1; i < firstopt; i++) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000314 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
315 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000316 status = EXIT_FAILURE;
317 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000318 }
319
Matt Kraai096370d2001-02-07 03:52:38 +0000320 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000321}