1 /* vi: set sw=4 ts=4: */
3 * Mini find implementation for busybox
6 * Copyright (C) 1999,2000,2001 by Lineo, inc.
7 * Written by Erik Andersen <andersen@lineo.com>, <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 BB_FEATURE_FIND_TYPE
41 static int type_mask = 0;
44 #ifdef BB_FEATURE_FIND_PERM
45 static char perm_char = 0;
46 static int perm_mask = 0;
49 #ifdef BB_FEATURE_FIND_MTIME
50 static char mtime_char;
51 static int mtime_days;
54 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
56 if (pattern != NULL) {
57 const char *tmp = strrchr(fileName, '/');
63 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
66 #ifdef BB_FEATURE_FIND_TYPE
68 if (!((statbuf->st_mode & S_IFMT) == type_mask))
72 #ifdef BB_FEATURE_FIND_PERM
74 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
75 (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
76 (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
80 #ifdef BB_FEATURE_FIND_MTIME
81 if (mtime_days != 0) {
82 time_t file_age = time(NULL) - statbuf->st_mtime;
83 time_t mtime_secs = mtime_days * 24 * 60 * 60;
84 if (!((isdigit(mtime_char) && mtime_secs >= file_age &&
85 mtime_secs < file_age + 24 * 60 * 60) ||
86 (mtime_char == '+' && mtime_secs >= file_age) ||
87 (mtime_char == '-' && mtime_secs < file_age)))
96 #ifdef BB_FEATURE_FIND_TYPE
97 static int find_type(char *type)
125 if (mask == 0 || type[1] != '\0')
126 error_msg_and_die("invalid argument `%s' to `-type'", type);
132 int find_main(int argc, char **argv)
134 int dereference = FALSE;
135 int i, firstopt, status = EXIT_SUCCESS;
137 for (firstopt = 1; firstopt < argc; firstopt++) {
138 if (argv[firstopt][0] == '-')
142 /* Parse any options */
143 for (i = firstopt; i < argc; i++) {
144 if (strcmp(argv[i], "-follow") == 0)
146 else if (strcmp(argv[i], "-print") == 0) {
149 else if (strcmp(argv[i], "-name") == 0) {
151 error_msg_and_die("option `-name' requires an argument");
153 #ifdef BB_FEATURE_FIND_TYPE
154 } else if (strcmp(argv[i], "-type") == 0) {
156 error_msg_and_die("option `-type' requires an argument");
157 type_mask = find_type(argv[i]);
159 #ifdef BB_FEATURE_FIND_PERM
160 } else if (strcmp(argv[i], "-perm") == 0) {
163 error_msg_and_die("option `-perm' requires an argument");
164 perm_mask = strtol(argv[i], &end, 8);
166 error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]);
167 if (perm_mask > 07777)
168 error_msg_and_die("invalid argument `%s' to `-perm'", argv[i]);
169 if ((perm_char = argv[i][0]) == '-')
170 perm_mask = -perm_mask;
172 #ifdef BB_FEATURE_FIND_MTIME
173 } else if (strcmp(argv[i], "-mtime") == 0) {
176 error_msg_and_die("option `-mtime' requires an argument");
177 mtime_days = strtol(argv[i], &end, 10);
179 error_msg_and_die("invalid argument `%s' to `-mtime'", argv[i]);
180 if ((mtime_char = argv[i][0]) == '-')
181 mtime_days = -mtime_days;
188 if (recursive_action(".", TRUE, dereference, FALSE, fileAction,
189 fileAction, NULL) == FALSE)
190 status = EXIT_FAILURE;
192 for (i = 1; i < firstopt; i++) {
193 if (recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
194 fileAction, NULL) == FALSE)
195 status = EXIT_FAILURE;