more correction for getopt_ulflags() documentation by author of this fuck logic
[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
42 #if ENABLE_FEATURE_FIND_TYPE
43 static int type_mask = 0;
44 #endif
45
46 #if ENABLE_FEATURE_FIND_PERM
47 static char perm_char = 0;
48 static int perm_mask = 0;
49 #endif
50
51 #if ENABLE_FEATURE_FIND_MTIME
52 static char mtime_char;
53 static int mtime_days;
54 #endif
55
56 #if ENABLE_FEATURE_FIND_XDEV
57 static dev_t *xdev_dev;
58 static int xdev_count = 0;
59 #endif
60
61 #if ENABLE_FEATURE_FIND_NEWER
62 static time_t newer_mtime;
63 #endif
64
65 #if ENABLE_FEATURE_FIND_INUM
66 static ino_t inode_num;
67 #endif
68
69 #if ENABLE_FEATURE_FIND_EXEC
70 static char **exec_str;
71 static int num_matches;
72 static int exec_opt;
73 #endif
74
75 static int fileAction(const char *fileName, struct stat *statbuf, void* junk)
76 {
77         if (pattern != NULL) {
78                 const char *tmp = strrchr(fileName, '/');
79
80                 if (tmp == NULL)
81                         tmp = fileName;
82                 else
83                         tmp++;
84                 if (!(fnmatch(pattern, tmp, FNM_PERIOD) == 0))
85                         goto no_match;
86         }
87         if (ENABLE_FEATURE_FIND_TYPE && type_mask != 0) {
88                 if (!((statbuf->st_mode & S_IFMT) == type_mask))
89                         goto no_match;
90         }
91         if (ENABLE_FEATURE_FIND_PERM && perm_mask != 0) {
92                 if (!((isdigit(perm_char) && (statbuf->st_mode & 07777) == perm_mask) ||
93                          (perm_char == '-' && (statbuf->st_mode & perm_mask) == perm_mask) ||
94                          (perm_char == '+' && (statbuf->st_mode & perm_mask) != 0)))
95                         goto no_match;
96         }
97         if (ENABLE_FEATURE_FIND_MTIME && mtime_char != 0) {
98                 time_t file_age = time(NULL) - statbuf->st_mtime;
99                 time_t mtime_secs = mtime_days * 24 * 60 * 60;
100                 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)))
104                         goto no_match;
105         }
106         if (ENABLE_FEATURE_FIND_XDEV && xdev_count) {
107                 int i;
108                 for (i=0; i<xdev_count; i++) {
109                         if (xdev_dev[i] == statbuf-> st_dev)
110                                 break;
111                 }
112                 if (i == xdev_count) {
113                         if(S_ISDIR(statbuf->st_mode))
114                                 return SKIP;
115                         else
116                                 goto no_match;
117                 }
118         }
119         if (ENABLE_FEATURE_FIND_NEWER && newer_mtime != 0) {
120                 time_t file_age = newer_mtime - statbuf->st_mtime;
121                 if (file_age >= 0)
122                         goto no_match;
123         }
124         if (ENABLE_FEATURE_FIND_INUM && inode_num != 0) {
125                 if (!(statbuf->st_ino == inode_num))
126                         goto no_match;
127         }
128         if (ENABLE_FEATURE_FIND_EXEC && exec_opt) {
129                 int i;
130                 char *cmd_string = "";
131                 for (i = 0; i < num_matches; i++)
132                         cmd_string = bb_xasprintf("%s%s%s", cmd_string, exec_str[i], fileName);
133                 cmd_string = bb_xasprintf("%s%s", cmd_string, exec_str[num_matches]);
134                 system(cmd_string);
135                 goto no_match;
136         }
137         
138         puts(fileName);
139 no_match:
140         return (TRUE);
141 }
142
143 #if ENABLE_FEATURE_FIND_TYPE
144 static int find_type(char *type)
145 {
146         int mask = 0;
147
148         switch (type[0]) {
149                 case 'b':
150                         mask = S_IFBLK;
151                         break;
152                 case 'c':
153                         mask = S_IFCHR;
154                         break;
155                 case 'd':
156                         mask = S_IFDIR;
157                         break;
158                 case 'p':
159                         mask = S_IFIFO;
160                         break;
161                 case 'f':
162                         mask = S_IFREG;
163                         break;
164                 case 'l':
165                         mask = S_IFLNK;
166                         break;
167                 case 's':
168                         mask = S_IFSOCK;
169                         break;
170         }
171
172         if (mask == 0 || type[1] != '\0')
173                 bb_error_msg_and_die(msg_invalid_arg, type, "-type");
174
175         return mask;
176 }
177 #endif
178
179 int find_main(int argc, char **argv)
180 {
181         int dereference = FALSE;
182         int i, firstopt, status = EXIT_SUCCESS;
183
184         for (firstopt = 1; firstopt < argc; firstopt++) {
185                 if (argv[firstopt][0] == '-')
186                         break;
187         }
188
189         /* Parse any options */
190         for (i = firstopt; i < argc; i++) {
191                 if (strcmp(argv[i], "-follow") == 0)
192                         dereference = TRUE;
193                 else if (strcmp(argv[i], "-print") == 0) {
194                         ;
195                         }
196                 else if (strcmp(argv[i], "-name") == 0) {
197                         if (++i == argc)
198                                 bb_error_msg_and_die(msg_req_arg, "-name");
199                         pattern = argv[i];
200                 } else if (ENABLE_FEATURE_FIND_TYPE && strcmp(argv[i], "-type") == 0) {
201                         if (++i == argc)
202                                 bb_error_msg_and_die(msg_req_arg, "-type");
203                         type_mask = find_type(argv[i]);
204                 } else if (ENABLE_FEATURE_FIND_PERM && strcmp(argv[i], "-perm") == 0) {
205                         char *end;
206                         if (++i == argc)
207                                 bb_error_msg_and_die(msg_req_arg, "-perm");
208                         perm_mask = strtol(argv[i], &end, 8);
209                         if ((end[0] != '\0') || (perm_mask > 07777))
210                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-perm");
211                         if ((perm_char = argv[i][0]) == '-')
212                                 perm_mask = -perm_mask;
213                 } else if (ENABLE_FEATURE_FIND_MTIME && strcmp(argv[i], "-mtime") == 0) {
214                         char *end;
215                         if (++i == argc)
216                                 bb_error_msg_and_die(msg_req_arg, "-mtime");
217                         mtime_days = strtol(argv[i], &end, 10);
218                         if (end[0] != '\0')
219                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-mtime");
220                         if ((mtime_char = argv[i][0]) == '-')
221                                 mtime_days = -mtime_days;
222                 } else if (ENABLE_FEATURE_FIND_XDEV && strcmp(argv[i], "-xdev") == 0) {
223                         struct stat stbuf;
224
225                         xdev_count = ( firstopt - 1 ) ? ( firstopt - 1 ) : 1;
226                         xdev_dev = xmalloc ( xdev_count * sizeof( dev_t ));
227
228                         if ( firstopt == 1 ) {
229                                 if ( stat ( ".", &stbuf ) < 0 )
230                                         bb_error_msg_and_die("could not stat '.'" );
231                                 xdev_dev [0] = stbuf. st_dev;
232                         }
233                         else {
234
235                                 for (i = 1; i < firstopt; i++) {
236                                         if ( stat ( argv [i], &stbuf ) < 0 )
237                                                 bb_error_msg_and_die("could not stat '%s'", argv [i] );
238                                         xdev_dev [i-1] = stbuf. st_dev;
239                                 }
240                         }
241                 } else if (ENABLE_FEATURE_FIND_NEWER && strcmp(argv[i], "-newer") == 0) {
242                         struct stat stat_newer;
243                         if (++i == argc)
244                                 bb_error_msg_and_die(msg_req_arg, "-newer");
245                     if (stat (argv[i], &stat_newer) != 0)
246                                 bb_error_msg_and_die("file %s not found", argv[i]);
247                         newer_mtime = stat_newer.st_mtime;
248                 } else if (ENABLE_FEATURE_FIND_INUM && strcmp(argv[i], "-inum") == 0) {
249                         char *end;
250                         if (++i == argc)
251                                 bb_error_msg_and_die(msg_req_arg, "-inum");
252                         inode_num = strtol(argv[i], &end, 10);
253                         if (end[0] != '\0')
254                                 bb_error_msg_and_die(msg_invalid_arg, argv[i], "-inum");
255                 } else if (ENABLE_FEATURE_FIND_EXEC && strcmp(argv[i], "-exec") == 0) {
256                         int b_pos;
257                         char *cmd_string = "";
258
259                         while (i++) {
260                                 if (i == argc)
261                                         bb_error_msg_and_die(msg_req_arg, "-exec");
262                                 if (*argv[i] == ';')
263                                         break;
264                                 cmd_string = bb_xasprintf("%s %s", cmd_string, argv[i]);
265                         }
266                         
267                         if (*cmd_string == 0)
268                                 bb_error_msg_and_die(msg_req_arg, "-exec");
269                         cmd_string++;
270                         exec_str = xmalloc(sizeof(char *));
271
272                         while ((b_pos = strstr(cmd_string, "{}") - cmd_string), (b_pos >= 0)) {
273                                 num_matches++;
274                                 exec_str = xrealloc(exec_str, (num_matches + 1) * sizeof(char *));
275                                 exec_str[num_matches - 1] = bb_xstrndup(cmd_string, b_pos);
276                                 cmd_string += b_pos + 2;
277                         }
278                         exec_str[num_matches] = bb_xstrdup(cmd_string);
279                         exec_opt = 1;
280                 } else
281                         bb_show_usage();
282         }
283
284         if (firstopt == 1) {
285                 if (! recursive_action(".", TRUE, dereference, FALSE, fileAction,
286                                         fileAction, NULL))
287                         status = EXIT_FAILURE;
288         } else {
289                 for (i = 1; i < firstopt; i++) {
290                         if (! recursive_action(argv[i], TRUE, dereference, FALSE, fileAction,
291                                                 fileAction, NULL))
292                                 status = EXIT_FAILURE;
293                 }
294         }
295
296         return status;
297 }