1 /* vi: set sw=4 ts=4: */
3 * Mini find implementation for busybox
5 * Copyright (C) 1999,2000 by Lineo, inc. and Erik Andersen
6 * Copyright (C) 1999,2000,2001 by Erik Andersen <andersee@debian.org>
8 * Reworked by David Douthitt <n9ubh@callsign.net> and
9 * Matt Kraai <kraai@alumni.carnegiemellon.edu>.
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.
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.
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
40 #ifdef CONFIG_FEATURE_FIND_TYPE
41 static int type_mask = 0;
44 #ifdef CONFIG_FEATURE_FIND_PERM
45 static char perm_char = 0;
46 static int perm_mask = 0;
49 #ifdef CONFIG_FEATURE_FIND_MTIME
50 static char mtime_char;
51 static int mtime_days;
54 #ifdef CONFIG_FEATURE_FIND_XDEV
55 static dev_t *xdev_dev;
56 static int xdev_count = 0;
60 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
62 if (pattern != NULL) {
63 const char *tmp = strrchr(fileName, '/');
69 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
72 #ifdef CONFIG_FEATURE_FIND_TYPE
74 if (!((statbuf->st_mode & S_IFMT) == type_mask))
78 #ifdef CONFIG_FEATURE_FIND_PERM
80 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
81 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
82 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
86 #ifdef CONFIG_FEATURE_FIND_MTIME
87 if (mtime_char != 0) {
88 time_t file_age = time(NULL) - statbuf->st_mtime;
89 time_t mtime_secs = mtime_days * 24 * 60 * 60;
90 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
91 file_age < mtime_secs + 24 * 60 * 60) ||
92 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
93 (mtime_char == '-' && file_age < mtime_secs)))
97 #ifdef CONFIG_FEATURE_FIND_XDEV
100 for (i=0; i<xdev_count; i++) {
101 if (xdev_dev[i] == statbuf-> st_dev)
104 if (i == xdev_count) {
105 if(S_ISDIR(statbuf->st_mode))
118 #ifdef CONFIG_FEATURE_FIND_TYPE
119 static int find_type(char *type)
147 if (mask == 0 || type[1] != '\0')
148 error_msg_and_die("invalid argument `%s' to `-type'", type);
154 int find_main(int argc, char **argv)
156 int dereference = FALSE;
157 int i, firstopt, status = EXIT_SUCCESS;
159 for (firstopt = 1; firstopt < argc; firstopt++) {
160 if (argv[firstopt][0] == '-')
164 /* Parse any options */
165 for (i = firstopt; i < argc; i++) {
166 if (strcmp(argv[i], "-follow") == 0)
168 else if (strcmp(argv[i], "-print") == 0) {
171 else if (strcmp(argv[i], "-name") == 0) {
173 error_msg_and_die("option `-name' requires an argument");
175 #ifdef CONFIG_FEATURE_FIND_TYPE
176 } else if (strcmp(argv[i], "-type") == 0) {
178 error_msg_and_die("option `-type' requires an argument");
179 type_mask = find_type(argv[i]);
181 #ifdef CONFIG_FEATURE_FIND_PERM
182 } else if (strcmp(argv[i], "-perm") == 0) {
185 error_msg_and_die("option `-perm' requires an argument");
186 perm_mask = strtol(argv[i], &end, 8);
188 error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]);
189 if (perm_mask > 07777)
190 error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]);
191 if ((perm_char = argv[i][0]) == '-')
192 perm_mask = -perm_mask;
194 #ifdef CONFIG_FEATURE_FIND_MTIME
195 } else if (strcmp(argv[i], "-mtime") == 0) {
198 error_msg_and_die("option `-mtime' requires an argument");
199 mtime_days = strtol(argv[i], &end, 10);
201 error_msg_and_die("invalid argument `%s' to `-mtime'", argv[i]);
202 if ((mtime_char = argv[i][0]) == '-')
203 mtime_days = -mtime_days;
205 #ifdef CONFIG_FEATURE_FIND_XDEV
206 } else if (strcmp(argv[i], "-xdev") == 0) {
209 xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
210 xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
212 if ( firstopt == 1 ) {
213 if ( stat ( ".", &stbuf ) < 0 )
214 error_msg_and_die("could not stat '.'" );
215 xdev_dev [0] = stbuf. st_dev;
219 for (i = 1; i < firstopt; i++) {
220 if ( stat ( argv [i], &stbuf ) < 0 )
221 error_msg_and_die("could not stat '%s'", argv [i] );
222 xdev_dev [i-1] = stbuf. st_dev;
231 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
233 status = EXIT_FAILURE;
235 for (i = 1; i < firstopt; i++) {
236 if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
238 status = EXIT_FAILURE;