blob: 66103046008754ec0b0bdff8b1c75762b9bbb871 [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 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
37const char msg_req_arg[] = "option `%s' requires an argument";
38const 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
Eric Andersenbdfd0d72001-10-24 05:00:29 +000042#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +000043static int type_mask = 0;
44#endif
45
Eric Andersenbdfd0d72001-10-24 05:00:29 +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
Eric Andersenbdfd0d72001-10-24 05:00:29 +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
Robert Griebl41369af2002-07-24 00:34:48 +000056#ifdef CONFIG_FEATURE_FIND_XDEV
57static dev_t *xdev_dev;
58static int xdev_count = 0;
59#endif
60
Eric Andersen97d86f22003-01-23 05:27:42 +000061#ifdef CONFIG_FEATURE_FIND_NEWER
62time_t newer_mtime;
63#endif
64
65#ifdef CONFIG_FEATURE_FIND_INUM
66static ino_t inode_num;
67#endif
Robert Griebl41369af2002-07-24 00:34:48 +000068
Erik Andersen3364d782000-03-28 00:58:14 +000069static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersen17d49ef1999-10-06 20:25:32 +000070{
Matt Kraai096370d2001-02-07 03:52:38 +000071 if (pattern != NULL) {
72 const char *tmp = strrchr(fileName, '/');
Erik Andersene49d5ec2000-02-08 19:58:47 +000073
74 if (tmp == NULL)
Matt Kraai096370d2001-02-07 03:52:38 +000075 tmp = fileName;
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 else
77 tmp++;
Matt Kraai096370d2001-02-07 03:52:38 +000078 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
79 goto no_match;
Erik Andersene49d5ec2000-02-08 19:58:47 +000080 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +000081#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +000082 if (type_mask != 0) {
83 if (!((statbuf->st_mode & S_IFMT) == type_mask))
84 goto no_match;
85 }
86#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000087#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +000088 if (perm_mask != 0) {
89 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
90 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
91 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
92 goto no_match;
93 }
94#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000095#ifdef CONFIG_FEATURE_FIND_MTIME
Glenn L McGrath49b0f862002-12-11 21:22:21 +000096 if (mtime_char != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +000097 time_t file_age = time(NULL) - statbuf->st_mtime;
98 time_t mtime_secs = mtime_days * 24 * 60 * 60;
Glenn L McGrath49b0f862002-12-11 21:22:21 +000099 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
100 file_age < mtime_secs + 24 * 60 * 60) ||
101 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
102 (mtime_char == '-' && file_age < mtime_secs)))
Matt Kraai096370d2001-02-07 03:52:38 +0000103 goto no_match;
104 }
105#endif
Robert Griebl41369af2002-07-24 00:34:48 +0000106#ifdef CONFIG_FEATURE_FIND_XDEV
107 if (xdev_count) {
108 int i;
109 for (i=0; i<xdev_count; i++) {
110 if (xdev_dev[i] == statbuf-> st_dev)
111 break;
112 }
113 if (i == xdev_count) {
114 if(S_ISDIR(statbuf->st_mode))
115 return SKIP;
116 else
117 goto no_match;
118 }
119 }
120#endif
Eric Andersen97d86f22003-01-23 05:27:42 +0000121#ifdef CONFIG_FEATURE_FIND_NEWER
122 if (newer_mtime != 0) {
123 time_t file_age = newer_mtime - statbuf->st_mtime;
124 if (file_age >= 0)
125 goto no_match;
126 }
127#endif
128#ifdef CONFIG_FEATURE_FIND_INUM
129 if (inode_num != 0) {
130 if (!(statbuf->st_ino == inode_num))
131 goto no_match;
132 }
133#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000134 puts(fileName);
135no_match:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000136 return (TRUE);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000137}
138
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000139#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000140static int find_type(char *type)
141{
142 int mask = 0;
143
144 switch (type[0]) {
145 case 'b':
146 mask = S_IFBLK;
147 break;
148 case 'c':
149 mask = S_IFCHR;
150 break;
151 case 'd':
152 mask = S_IFDIR;
153 break;
154 case 'p':
155 mask = S_IFIFO;
156 break;
157 case 'f':
158 mask = S_IFREG;
159 break;
160 case 'l':
161 mask = S_IFLNK;
162 break;
163 case 's':
164 mask = S_IFSOCK;
165 break;
166 }
167
168 if (mask == 0 || type[1] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000169 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000170
171 return mask;
172}
173#endif
174
Eric Andersen17d49ef1999-10-06 20:25:32 +0000175int find_main(int argc, char **argv)
176{
Matt Kraai096370d2001-02-07 03:52:38 +0000177 int dereference = FALSE;
178 int i, firstopt, status = EXIT_SUCCESS;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000179
Matt Kraai096370d2001-02-07 03:52:38 +0000180 for (firstopt = 1; firstopt < argc; firstopt++) {
181 if (argv[firstopt][0] == '-')
182 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000183 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000184
Erik Andersene49d5ec2000-02-08 19:58:47 +0000185 /* Parse any options */
Matt Kraai096370d2001-02-07 03:52:38 +0000186 for (i = firstopt; i < argc; i++) {
187 if (strcmp(argv[i], "-follow") == 0)
188 dereference = TRUE;
Mark Whitleye0a7f912001-03-28 22:04:42 +0000189 else if (strcmp(argv[i], "-print") == 0) {
190 ;
191 }
Matt Kraai096370d2001-02-07 03:52:38 +0000192 else if (strcmp(argv[i], "-name") == 0) {
193 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000194 bb_error_msg_and_die(msg_req_arg, "-name");
Matt Kraai096370d2001-02-07 03:52:38 +0000195 pattern = argv[i];
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000196#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000197 } else if (strcmp(argv[i], "-type") == 0) {
198 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000199 bb_error_msg_and_die(msg_req_arg, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000200 type_mask = find_type(argv[i]);
201#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000202#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +0000203 } else if (strcmp(argv[i], "-perm") == 0) {
204 char *end;
205 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000206 bb_error_msg_and_die(msg_req_arg, "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000207 perm_mask = strtol(argv[i], &end, 8);
Eric Andersen97d86f22003-01-23 05:27:42 +0000208 if ((end[0] != '\0') || (perm_mask > 07777))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000209 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000210 if ((perm_char = argv[i][0]) == '-')
211 perm_mask = -perm_mask;
212#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000213#ifdef CONFIG_FEATURE_FIND_MTIME
Matt Kraai096370d2001-02-07 03:52:38 +0000214 } else if (strcmp(argv[i], "-mtime") == 0) {
215 char *end;
216 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000217 bb_error_msg_and_die(msg_req_arg, "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000218 mtime_days = strtol(argv[i], &end, 10);
219 if (end[0] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000220 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000221 if ((mtime_char = argv[i][0]) == '-')
222 mtime_days = -mtime_days;
223#endif
Robert Griebl41369af2002-07-24 00:34:48 +0000224#ifdef CONFIG_FEATURE_FIND_XDEV
225 } else if (strcmp(argv[i], "-xdev") == 0) {
226 struct stat stbuf;
227
228 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
229 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
230
231 if ( firstopt == 1 ) {
232 if ( stat ( ".", &stbuf ) < 0 )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000233 bb_error_msg_and_die("could not stat '.'" );
Robert Griebl41369af2002-07-24 00:34:48 +0000234 xdev_dev [0] = stbuf. st_dev;
235 }
236 else {
237
238 for (i = 1; i < firstopt; i++) {
239 if ( stat ( argv [i], &stbuf ) < 0 )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000240 bb_error_msg_and_die("could not stat '%s'", argv [i] );
Robert Griebl41369af2002-07-24 00:34:48 +0000241 xdev_dev [i-1] = stbuf. st_dev;
242 }
243 }
244#endif
Eric Andersen97d86f22003-01-23 05:27:42 +0000245#ifdef CONFIG_FEATURE_FIND_NEWER
246 } else if (strcmp(argv[i], "-newer") == 0) {
247 struct stat stat_newer;
248 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000249 bb_error_msg_and_die(msg_req_arg, "-newer");
Eric Andersen97d86f22003-01-23 05:27:42 +0000250 if (stat (argv[i], &stat_newer) != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000251 bb_error_msg_and_die("file %s not found", argv[i]);
Eric Andersen97d86f22003-01-23 05:27:42 +0000252 newer_mtime = stat_newer.st_mtime;
253#endif
254#ifdef CONFIG_FEATURE_FIND_INUM
255 } else if (strcmp(argv[i], "-inum") == 0) {
256 char *end;
257 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000258 bb_error_msg_and_die(msg_req_arg, "-inum");
Eric Andersen97d86f22003-01-23 05:27:42 +0000259 inode_num = strtol(argv[i], &end, 10);
260 if (end[0] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000261 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
Eric Andersen97d86f22003-01-23 05:27:42 +0000262#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000263 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000264 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000265 }
266
Matt Kraai096370d2001-02-07 03:52:38 +0000267 if (firstopt == 1) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000268 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
269 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000270 status = EXIT_FAILURE;
271 } else {
272 for (i = 1; i < firstopt; i++) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000273 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
274 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000275 status = EXIT_FAILURE;
276 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000277 }
278
Matt Kraai096370d2001-02-07 03:52:38 +0000279 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000280}