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