blob: 58bc8bf9c824ede36156c4cc5e14a126de01d112 [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 Andersenbdfd0d72001-10-24 05:00:29 +00005 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
Eric Andersenc4996011999-10-20 22:08:37 +00007 *
Matt Kraai096370d2001-02-07 03:52:38 +00008 * Reworked by David Douthitt <n9ubh@callsign.net> and
9 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
Eric Andersen17d49ef1999-10-06 20:25:32 +000010 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 */
26
27#include <stdio.h>
28#include <unistd.h>
29#include <dirent.h>
Eric Andersened3ef502001-01-27 08:24:39 +000030#include <string.h>
31#include <stdlib.h>
Matt Kraai096370d2001-02-07 03:52:38 +000032#include <fnmatch.h>
33#include <time.h>
34#include <ctype.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000035#include "busybox.h"
Eric Andersen17d49ef1999-10-06 20:25:32 +000036
Eric Andersen97d86f22003-01-23 05:27:42 +000037//XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz
38const char msg_req_arg[] = "option `%s' requires an argument";
39const char msg_invalid_arg[] = "invalid argument `%s' to `%s'";
Eric Andersen17d49ef1999-10-06 20:25:32 +000040
Matt Kraai096370d2001-02-07 03:52:38 +000041static char *pattern;
42
Eric Andersenbdfd0d72001-10-24 05:00:29 +000043#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +000044static int type_mask = 0;
45#endif
46
Eric Andersenbdfd0d72001-10-24 05:00:29 +000047#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +000048static char perm_char = 0;
49static int perm_mask = 0;
50#endif
51
Eric Andersenbdfd0d72001-10-24 05:00:29 +000052#ifdef CONFIG_FEATURE_FIND_MTIME
Matt Kraai096370d2001-02-07 03:52:38 +000053static char mtime_char;
54static int mtime_days;
55#endif
Eric Andersen17d49ef1999-10-06 20:25:32 +000056
Robert Griebl41369af2002-07-24 00:34:48 +000057#ifdef CONFIG_FEATURE_FIND_XDEV
58static dev_t *xdev_dev;
59static int xdev_count = 0;
60#endif
61
Eric Andersen97d86f22003-01-23 05:27:42 +000062#ifdef CONFIG_FEATURE_FIND_NEWER
63time_t newer_mtime;
64#endif
65
66#ifdef CONFIG_FEATURE_FIND_INUM
67static ino_t inode_num;
68#endif
Robert Griebl41369af2002-07-24 00:34:48 +000069
Erik Andersen3364d782000-03-28 00:58:14 +000070static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
Eric Andersen17d49ef1999-10-06 20:25:32 +000071{
Matt Kraai096370d2001-02-07 03:52:38 +000072 if (pattern != NULL) {
73 const char *tmp = strrchr(fileName, '/');
Erik Andersene49d5ec2000-02-08 19:58:47 +000074
75 if (tmp == NULL)
Matt Kraai096370d2001-02-07 03:52:38 +000076 tmp = fileName;
Erik Andersene49d5ec2000-02-08 19:58:47 +000077 else
78 tmp++;
Matt Kraai096370d2001-02-07 03:52:38 +000079 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
80 goto no_match;
Erik Andersene49d5ec2000-02-08 19:58:47 +000081 }
Eric Andersenbdfd0d72001-10-24 05:00:29 +000082#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +000083 if (type_mask != 0) {
84 if (!((statbuf->st_mode & S_IFMT) == type_mask))
85 goto no_match;
86 }
87#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000088#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +000089 if (perm_mask != 0) {
90 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
91 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
92 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
93 goto no_match;
94 }
95#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +000096#ifdef CONFIG_FEATURE_FIND_MTIME
Glenn L McGrath49b0f862002-12-11 21:22:21 +000097 if (mtime_char != 0) {
Matt Kraai096370d2001-02-07 03:52:38 +000098 time_t file_age = time(NULL) - statbuf->st_mtime;
99 time_t mtime_secs = mtime_days * 24 * 60 * 60;
Glenn L McGrath49b0f862002-12-11 21:22:21 +0000100 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
101 file_age < mtime_secs + 24 * 60 * 60) ||
102 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
103 (mtime_char == '-' && file_age < mtime_secs)))
Matt Kraai096370d2001-02-07 03:52:38 +0000104 goto no_match;
105 }
106#endif
Robert Griebl41369af2002-07-24 00:34:48 +0000107#ifdef CONFIG_FEATURE_FIND_XDEV
108 if (xdev_count) {
109 int i;
110 for (i=0; i<xdev_count; i++) {
111 if (xdev_dev[i] == statbuf-> st_dev)
112 break;
113 }
114 if (i == xdev_count) {
115 if(S_ISDIR(statbuf->st_mode))
116 return SKIP;
117 else
118 goto no_match;
119 }
120 }
121#endif
Eric Andersen97d86f22003-01-23 05:27:42 +0000122#ifdef CONFIG_FEATURE_FIND_NEWER
123 if (newer_mtime != 0) {
124 time_t file_age = newer_mtime - statbuf->st_mtime;
125 if (file_age >= 0)
126 goto no_match;
127 }
128#endif
129#ifdef CONFIG_FEATURE_FIND_INUM
130 if (inode_num != 0) {
131 if (!(statbuf->st_ino == inode_num))
132 goto no_match;
133 }
134#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000135 puts(fileName);
136no_match:
Erik Andersene49d5ec2000-02-08 19:58:47 +0000137 return (TRUE);
Eric Andersen17d49ef1999-10-06 20:25:32 +0000138}
139
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000140#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000141static int find_type(char *type)
142{
143 int mask = 0;
144
145 switch (type[0]) {
146 case 'b':
147 mask = S_IFBLK;
148 break;
149 case 'c':
150 mask = S_IFCHR;
151 break;
152 case 'd':
153 mask = S_IFDIR;
154 break;
155 case 'p':
156 mask = S_IFIFO;
157 break;
158 case 'f':
159 mask = S_IFREG;
160 break;
161 case 'l':
162 mask = S_IFLNK;
163 break;
164 case 's':
165 mask = S_IFSOCK;
166 break;
167 }
168
169 if (mask == 0 || type[1] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000171
172 return mask;
173}
174#endif
175
Eric Andersen17d49ef1999-10-06 20:25:32 +0000176int find_main(int argc, char **argv)
177{
Matt Kraai096370d2001-02-07 03:52:38 +0000178 int dereference = FALSE;
179 int i, firstopt, status = EXIT_SUCCESS;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000180
Matt Kraai096370d2001-02-07 03:52:38 +0000181 for (firstopt = 1; firstopt < argc; firstopt++) {
182 if (argv[firstopt][0] == '-')
183 break;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000184 }
Eric Andersen17d49ef1999-10-06 20:25:32 +0000185
Erik Andersene49d5ec2000-02-08 19:58:47 +0000186 /* Parse any options */
Matt Kraai096370d2001-02-07 03:52:38 +0000187 for (i = firstopt; i < argc; i++) {
188 if (strcmp(argv[i], "-follow") == 0)
189 dereference = TRUE;
Mark Whitleye0a7f912001-03-28 22:04:42 +0000190 else if (strcmp(argv[i], "-print") == 0) {
191 ;
192 }
Matt Kraai096370d2001-02-07 03:52:38 +0000193 else if (strcmp(argv[i], "-name") == 0) {
194 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000195 bb_error_msg_and_die(msg_req_arg, "-name");
Matt Kraai096370d2001-02-07 03:52:38 +0000196 pattern = argv[i];
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000197#ifdef CONFIG_FEATURE_FIND_TYPE
Matt Kraai096370d2001-02-07 03:52:38 +0000198 } else if (strcmp(argv[i], "-type") == 0) {
199 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000200 bb_error_msg_and_die(msg_req_arg, "-type");
Matt Kraai096370d2001-02-07 03:52:38 +0000201 type_mask = find_type(argv[i]);
202#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000203#ifdef CONFIG_FEATURE_FIND_PERM
Matt Kraai096370d2001-02-07 03:52:38 +0000204 } else if (strcmp(argv[i], "-perm") == 0) {
205 char *end;
206 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000207 bb_error_msg_and_die(msg_req_arg, "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000208 perm_mask = strtol(argv[i], &end, 8);
Eric Andersen97d86f22003-01-23 05:27:42 +0000209 if ((end[0] != '\0') || (perm_mask > 07777))
Manuel Novoa III cad53642003-03-19 09:13:01 +0000210 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
Matt Kraai096370d2001-02-07 03:52:38 +0000211 if ((perm_char = argv[i][0]) == '-')
212 perm_mask = -perm_mask;
213#endif
Eric Andersenbdfd0d72001-10-24 05:00:29 +0000214#ifdef CONFIG_FEATURE_FIND_MTIME
Matt Kraai096370d2001-02-07 03:52:38 +0000215 } else if (strcmp(argv[i], "-mtime") == 0) {
216 char *end;
217 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000218 bb_error_msg_and_die(msg_req_arg, "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000219 mtime_days = strtol(argv[i], &end, 10);
220 if (end[0] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000221 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
Matt Kraai096370d2001-02-07 03:52:38 +0000222 if ((mtime_char = argv[i][0]) == '-')
223 mtime_days = -mtime_days;
224#endif
Robert Griebl41369af2002-07-24 00:34:48 +0000225#ifdef CONFIG_FEATURE_FIND_XDEV
226 } else if (strcmp(argv[i], "-xdev") == 0) {
227 struct stat stbuf;
228
229 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
230 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
231
232 if ( firstopt == 1 ) {
233 if ( stat ( ".", &stbuf ) < 0 )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000234 bb_error_msg_and_die("could not stat '.'" );
Robert Griebl41369af2002-07-24 00:34:48 +0000235 xdev_dev [0] = stbuf. st_dev;
236 }
237 else {
238
239 for (i = 1; i < firstopt; i++) {
240 if ( stat ( argv [i], &stbuf ) < 0 )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000241 bb_error_msg_and_die("could not stat '%s'", argv [i] );
Robert Griebl41369af2002-07-24 00:34:48 +0000242 xdev_dev [i-1] = stbuf. st_dev;
243 }
244 }
245#endif
Eric Andersen97d86f22003-01-23 05:27:42 +0000246#ifdef CONFIG_FEATURE_FIND_NEWER
247 } else if (strcmp(argv[i], "-newer") == 0) {
248 struct stat stat_newer;
249 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000250 bb_error_msg_and_die(msg_req_arg, "-newer");
Eric Andersen97d86f22003-01-23 05:27:42 +0000251 if (stat (argv[i], &stat_newer) != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000252 bb_error_msg_and_die("file %s not found", argv[i]);
Eric Andersen97d86f22003-01-23 05:27:42 +0000253 newer_mtime = stat_newer.st_mtime;
254#endif
255#ifdef CONFIG_FEATURE_FIND_INUM
256 } else if (strcmp(argv[i], "-inum") == 0) {
257 char *end;
258 if (++i == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000259 bb_error_msg_and_die(msg_req_arg, "-inum");
Eric Andersen97d86f22003-01-23 05:27:42 +0000260 inode_num = strtol(argv[i], &end, 10);
261 if (end[0] != '\0')
Manuel Novoa III cad53642003-03-19 09:13:01 +0000262 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
Eric Andersen97d86f22003-01-23 05:27:42 +0000263#endif
Matt Kraai096370d2001-02-07 03:52:38 +0000264 } else
Manuel Novoa III cad53642003-03-19 09:13:01 +0000265 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000266 }
267
Matt Kraai096370d2001-02-07 03:52:38 +0000268 if (firstopt == 1) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000269 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
270 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000271 status = EXIT_FAILURE;
272 } else {
273 for (i = 1; i < firstopt; i++) {
Matt Kraai1f0c4362001-12-20 23:13:26 +0000274 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
275 fileAction, NULL))
Matt Kraai096370d2001-02-07 03:52:38 +0000276 status = EXIT_FAILURE;
277 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000278 }
279
Matt Kraai096370d2001-02-07 03:52:38 +0000280 return status;
Eric Andersen17d49ef1999-10-06 20:25:32 +0000281}