17a1a565661bc3c5cc76aa503f81175f30554588
[oweals/busybox.git] / findutils / find.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini find implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Reworked by David Douthitt <n9ubh@callsign.net> and
8  *  Matt Kraai <kraai@alumni.carnegiemellon.edu>.
9  *
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>
29 #include <string.h>
30 #include <stdlib.h>
31 #include <fnmatch.h>
32 #include <time.h>
33 #include <ctype.h>
34 #include "busybox.h"
35
36 //XXX just found out about libbb/messages.c . maybe move stuff there ? - ghoz
37 static const char msg_req_arg[] = "option `%s' requires an argument";
38 static const char msg_invalid_arg[] = "invalid argument `%s' to `%s'";
39
40 static char *pattern;
41 #ifdef CONFIG_FEATURE_FIND_PRINT0
42 static char printsep = '\n';
43 #endif
44
45 #ifdef CONFIG_FEATURE_FIND_TYPE
46 static int type_mask = 0;
47 #endif
48
49 #ifdef CONFIG_FEATURE_FIND_PERM
50 static char perm_char = 0;
51 static int perm_mask = 0;
52 #endif
53
54 #ifdef CONFIG_FEATURE_FIND_MTIME
55 static char mtime_char;
56 static int mtime_days;
57 #endif
58
59 #ifdef CONFIG_FEATURE_FIND_MMIN
60 static char mmin_char;
61 static int mmin_mins;
62 #endif
63
64 #ifdef CONFIG_FEATURE_FIND_XDEV
65 static dev_t *xdev_dev;
66 static int xdev_count = 0;
67 #endif
68
69 #ifdef CONFIG_FEATURE_FIND_NEWER
70 static time_t newer_mtime;
71 #endif
72
73 #ifdef CONFIG_FEATURE_FIND_INUM
74 static ino_t inode_num;
75 #endif
76
77 #ifdef CONFIG_FEATURE_FIND_EXEC
78 static char **exec_str;
79 static int num_matches;
80 static int exec_opt;
81 #endif
82
83 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
84 {
85 #ifdef CONFIG_FEATURE_FIND_XDEV
86         if (S_ISDIR(statbuf->st_mode) && xdev_count) {
87                 int i;
88                 for (i=0; i<xdev_count; i++) {
89                         if (xdev_dev[i] != statbuf->st_dev)
90                                 return SKIP;
91                 }
92         }
93 #endif
94         if (pattern != NULL) {
95                 const char *tmp = strrchr(fileName, '/');
96
97                 if (tmp == NULL)
98                         tmp = fileName;
99                 else
100                         tmp++;
101                 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
102                         goto no_match;
103         }
104 #ifdef CONFIG_FEATURE_FIND_TYPE
105         if (type_mask != 0) {
106                 if (!((statbuf->st_mode & S_IFMT) == type_mask))
107                         goto no_match;
108         }
109 #endif
110 #ifdef CONFIG_FEATURE_FIND_PERM
111         if (perm_mask != 0) {
112                 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
113                          (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
114                          (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
115                         goto no_match;
116         }
117 #endif
118 #ifdef CONFIG_FEATURE_FIND_MTIME
119         if (mtime_char != 0) {
120                 time_t file_age = time(NULL) - statbuf->st_mtime;
121                 time_t mtime_secs = mtime_days * 24 * 60 * 60;
122                 if (!((isdigit(mtime_char) && file_age >= mtime_secs &&
123                                                 file_age < mtime_secs + 24 * 60 * 60) ||
124                                 (mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) ||
125                                 (mtime_char == '-' && file_age < mtime_secs)))
126                         goto no_match;
127         }
128 #endif
129 #ifdef CONFIG_FEATURE_FIND_MMIN
130         if (mmin_char != 0) {
131                 time_t file_age = time(NULL) - statbuf->st_mtime;
132                 time_t mmin_secs = mmin_mins * 60;
133                 if (!((isdigit(mmin_char) && file_age >= mmin_secs &&
134                                                 file_age < mmin_secs + 60) ||
135                                 (mmin_char == '+' && file_age >= mmin_secs + 60) ||
136                                 (mmin_char == '-' && file_age < mmin_secs)))
137                         goto no_match;
138         }
139 #endif
140 #ifdef CONFIG_FEATURE_FIND_NEWER
141         if (newer_mtime != 0) {
142                 time_t file_age = newer_mtime - statbuf->st_mtime;
143                 if (file_age >= 0)
144                         goto no_match;
145         }
146 #endif
147 #ifdef CONFIG_FEATURE_FIND_INUM
148         if (inode_num != 0) {
149                 if (!(statbuf->st_ino == inode_num))
150                         goto no_match;
151         }
152 #endif
153 #ifdef CONFIG_FEATURE_FIND_EXEC
154         if (exec_opt) {
155                 int i;
156                 char *cmd_string = "";
157                 for (i = 0; i < num_matches; i++)
158                         cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
159                 cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
160                 system(cmd_string);
161                 goto no_match;
162         }
163 #endif
164
165 #ifdef CONFIG_FEATURE_FIND_PRINT0
166         printf("%s%c", fileName, printsep);
167 #else
168         puts(fileName);
169 #endif
170 no_match:
171         return (TRUE);
172 }
173
174 #ifdef CONFIG_FEATURE_FIND_TYPE
175 static int find_type(char *type)
176 {
177         int mask = 0;
178
179         switch (type[0]) {
180                 case 'b':
181                         mask = S_IFBLK;
182                         break;
183                 case 'c':
184                         mask = S_IFCHR;
185                         break;
186                 case 'd':
187                         mask = S_IFDIR;
188                         break;
189                 case 'p':
190                         mask = S_IFIFO;
191                         break;
192                 case 'f':
193                         mask = S_IFREG;
194                         break;
195                 case 'l':
196                         mask = S_IFLNK;
197                         break;
198                 case 's':
199                         mask = S_IFSOCK;
200                         break;
201         }
202
203         if (mask == 0 || type[1] != '\0')
204                 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
205
206         return mask;
207 }
208 #endif
209
210 int find_main(int argc, char **argv)
211 {
212         int dereference = FALSE;
213         int i, firstopt, status = EXIT_SUCCESS;
214
215         for (firstopt = 1; firstopt < argc; firstopt++) {
216                 if (argv[firstopt][0] == '-')
217                         break;
218         }
219
220         /* Parse any options */
221         for (i = firstopt; i < argc; i++) {
222                 if (strcmp(argv[i], "-follow") == 0)
223                         dereference = TRUE;
224                 else if (strcmp(argv[i], "-print") == 0) {
225                         ;
226                         }
227 #ifdef CONFIG_FEATURE_FIND_PRINT0
228                 else if (strcmp(argv[i], "-print0") == 0)
229                         printsep = '\0';
230 #endif
231                 else if (strcmp(argv[i], "-name") == 0) {
232                         if (++i == argc)
233                                 bb_error_msg_and_die(msg_req_arg, "-name");
234                         pattern = argv[i];
235 #ifdef CONFIG_FEATURE_FIND_TYPE
236                 } else if (strcmp(argv[i], "-type") == 0) {
237                         if (++i == argc)
238                                 bb_error_msg_and_die(msg_req_arg, "-type");
239                         type_mask = find_type(argv[i]);
240 #endif
241 #ifdef CONFIG_FEATURE_FIND_PERM
242                 } else if (strcmp(argv[i], "-perm") == 0) {
243                         char *end;
244                         if (++i == argc)
245                                 bb_error_msg_and_die(msg_req_arg, "-perm");
246                         perm_mask = strtol(argv[i], &end, 8);
247                         if ((end[0] != '\0') || (perm_mask > 07777))
248                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
249                         if ((perm_char = argv[i][0]) == '-')
250                                 perm_mask = -perm_mask;
251 #endif
252 #ifdef CONFIG_FEATURE_FIND_MTIME
253                 } else if (strcmp(argv[i], "-mtime") == 0) {
254                         char *end;
255                         if (++i == argc)
256                                 bb_error_msg_and_die(msg_req_arg, "-mtime");
257                         mtime_days = strtol(argv[i], &end, 10);
258                         if (end[0] != '\0')
259                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
260                         if ((mtime_char = argv[i][0]) == '-')
261                                 mtime_days = -mtime_days;
262 #endif
263 #ifdef CONFIG_FEATURE_FIND_MMIN
264                 } else if (strcmp(argv[i], "-mmin") == 0) {
265                         char *end;
266                         if (++i == argc)
267                                 bb_error_msg_and_die(msg_req_arg, "-mmin");
268                         mmin_mins = strtol(argv[i], &end, 10);
269                         if (end[0] != '\0')
270                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mmin");
271                         if ((mmin_char = argv[i][0]) == '-')
272                                 mmin_mins = -mmin_mins;
273 #endif
274 #ifdef CONFIG_FEATURE_FIND_XDEV
275                 } else if (strcmp(argv[i], "-xdev") == 0) {
276                         struct stat stbuf;
277
278                         xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
279                         xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
280
281                         if ( firstopt == 1 ) {
282                                 xstat ( ".", &stbuf );
283                                 xdev_dev [0] = stbuf. st_dev;
284                         }
285                         else {
286
287                                 for (i = 1; i < firstopt; i++) {
288                                         xstat ( argv [i], &stbuf );
289                                         xdev_dev [i-1] = stbuf. st_dev;
290                                 }
291                         }
292 #endif
293 #ifdef CONFIG_FEATURE_FIND_NEWER
294                 } else if (strcmp(argv[i], "-newer") == 0) {
295                         struct stat stat_newer;
296                         if (++i == argc)
297                                 bb_error_msg_and_die(msg_req_arg, "-newer");
298                         xstat (argv[i], &stat_newer);
299                         newer_mtime = stat_newer.st_mtime;
300 #endif
301 #ifdef CONFIG_FEATURE_FIND_INUM
302                 } else if (strcmp(argv[i], "-inum") == 0) {
303                         char *end;
304                         if (++i == argc)
305                                 bb_error_msg_and_die(msg_req_arg, "-inum");
306                         inode_num = strtol(argv[i], &end, 10);
307                         if (end[0] != '\0')
308                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
309 #endif
310 #ifdef CONFIG_FEATURE_FIND_EXEC
311                 } else if (strcmp(argv[i], "-exec") == 0) {
312                         int b_pos;
313                         char *cmd_string = "";
314
315                         while (i++) {
316                                 if (i == argc)
317                                         bb_error_msg_and_die(msg_req_arg, "-exec");
318                                 if (*argv[i] == ';')
319                                         break;
320                                 cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
321                         }
322
323                         if (*cmd_string == 0)
324                                 bb_error_msg_and_die(msg_req_arg, "-exec");
325                         cmd_string++;
326                         exec_str = xmalloc(sizeof(char *));
327
328                         while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
329                                 num_matches++;
330                                 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
331                                 exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
332                                 cmd_string += b_pos + 2;
333                         }
334                         exec_str[num_matches] = bb_xstrdup(cmd_string);
335                         exec_opt = 1;
336 #endif
337                 } else
338                         bb_show_usage();
339         }
340
341         if (firstopt == 1) {
342                 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
343                                         fileAction, NULL))
344                         status = EXIT_FAILURE;
345         } else {
346                 for (i = 1; i < firstopt; i++) {
347                         if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
348                                                 fileAction, NULL))
349                                 status = EXIT_FAILURE;
350                 }
351         }
352
353         return status;
354 }