diff options
author | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-27 04:35:04 +0000 |
---|---|---|
committer | Denis Vlasenko <vda.linux@googlemail.com> | 2006-12-27 04:35:04 +0000 |
commit | 8d42f86b146871ae4c4cafd3801a85f381249a14 (patch) | |
tree | b963999fc54eddb65f1929b894f868e24851fc9c /findutils | |
download | busybox-w32-8d42f86b146871ae4c4cafd3801a85f381249a14.tar.gz busybox-w32-8d42f86b146871ae4c4cafd3801a85f381249a14.tar.bz2 busybox-w32-8d42f86b146871ae4c4cafd3801a85f381249a14.zip |
Correcting branch name to be like previous ones
Diffstat (limited to 'findutils')
-rw-r--r-- | findutils/Config.in | 159 | ||||
-rw-r--r-- | findutils/Kbuild | 10 | ||||
-rw-r--r-- | findutils/find.c | 582 | ||||
-rw-r--r-- | findutils/grep.c | 483 | ||||
-rw-r--r-- | findutils/xargs.c | 539 |
5 files changed, 1773 insertions, 0 deletions
diff --git a/findutils/Config.in b/findutils/Config.in new file mode 100644 index 000000000..5a4476a98 --- /dev/null +++ b/findutils/Config.in | |||
@@ -0,0 +1,159 @@ | |||
1 | # | ||
2 | # For a description of the syntax of this configuration file, | ||
3 | # see scripts/kbuild/config-language.txt. | ||
4 | # | ||
5 | |||
6 | menu "Finding Utilities" | ||
7 | |||
8 | config FIND | ||
9 | bool "find" | ||
10 | default n | ||
11 | help | ||
12 | find is used to search your system to find specified files. | ||
13 | |||
14 | config FEATURE_FIND_PRINT0 | ||
15 | bool "Enable -print0 option" | ||
16 | default y | ||
17 | depends on FIND | ||
18 | help | ||
19 | Causes output names to be separated by a null character | ||
20 | rather than a newline. This allows names that contain | ||
21 | newlines and other whitespace to be more easily | ||
22 | interpreted by other programs. | ||
23 | |||
24 | config FEATURE_FIND_MTIME | ||
25 | bool "Enable modified time matching (-mtime) option" | ||
26 | default y | ||
27 | depends on FIND | ||
28 | help | ||
29 | Allow searching based on the modification time of | ||
30 | files, in days. | ||
31 | |||
32 | config FEATURE_FIND_MMIN | ||
33 | bool "Enable modified time matching (-min) option" | ||
34 | default y | ||
35 | depends on FIND | ||
36 | help | ||
37 | Allow searching based on the modification time of | ||
38 | files, in minutes. | ||
39 | |||
40 | config FEATURE_FIND_PERM | ||
41 | bool "Enable permissions matching (-perm) option" | ||
42 | default y | ||
43 | depends on FIND | ||
44 | help | ||
45 | Enable searching based on file permissions. | ||
46 | |||
47 | config FEATURE_FIND_TYPE | ||
48 | bool "Enable filetype matching (-type) option" | ||
49 | default y | ||
50 | depends on FIND | ||
51 | help | ||
52 | Enable searching based on file type (file, | ||
53 | directory, socket, device, etc.). | ||
54 | |||
55 | config FEATURE_FIND_XDEV | ||
56 | bool "Enable stay in filesystem (-xdev) option" | ||
57 | default y | ||
58 | depends on FIND | ||
59 | help | ||
60 | This option will allow find to restrict searches to a single | ||
61 | filesystem. | ||
62 | |||
63 | config FEATURE_FIND_NEWER | ||
64 | bool "Enable -newer option for comparing file mtimes" | ||
65 | default y | ||
66 | depends on FIND | ||
67 | help | ||
68 | Support the 'find -newer' option for finding any files which have | ||
69 | a modified time that is more recent than the specified FILE. | ||
70 | |||
71 | config FEATURE_FIND_INUM | ||
72 | bool "Enable inode number matching (-inum) option" | ||
73 | default y | ||
74 | depends on FIND | ||
75 | help | ||
76 | Support the 'find -inum' option for searching by inode number. | ||
77 | |||
78 | config FEATURE_FIND_EXEC | ||
79 | bool "Enable (-exec) option allowing execution of commands" | ||
80 | default y | ||
81 | depends on FIND | ||
82 | help | ||
83 | Support the 'find -exec' option for executing commands based upon | ||
84 | the files matched. | ||
85 | |||
86 | config GREP | ||
87 | bool "grep" | ||
88 | default n | ||
89 | help | ||
90 | grep is used to search files for a specified pattern. | ||
91 | |||
92 | config FEATURE_GREP_EGREP_ALIAS | ||
93 | bool "Support extended regular expressions (egrep & grep -E)" | ||
94 | default y | ||
95 | depends on GREP | ||
96 | help | ||
97 | Enabled support for extended regular expressions. Extended | ||
98 | regular expressions allow for alternation (foo|bar), grouping, | ||
99 | and various repetition operators. | ||
100 | |||
101 | config FEATURE_GREP_FGREP_ALIAS | ||
102 | bool "Alias fgrep to grep -F" | ||
103 | default y | ||
104 | depends on GREP | ||
105 | help | ||
106 | fgrep sees the search pattern as a normal string rather than | ||
107 | regular expressions. | ||
108 | grep -F is always builtin, this just creates the fgrep alias. | ||
109 | |||
110 | config FEATURE_GREP_CONTEXT | ||
111 | bool "Enable before and after context flags (-A, -B and -C)" | ||
112 | default y | ||
113 | depends on GREP | ||
114 | help | ||
115 | Print the specified number of leading (-B) and/or trailing (-A) | ||
116 | context surrounding our matching lines. | ||
117 | Print the specified number of context lines (-C). | ||
118 | |||
119 | config XARGS | ||
120 | bool "xargs" | ||
121 | default n | ||
122 | help | ||
123 | xargs is used to execute a specified command on | ||
124 | every item from standard input. | ||
125 | |||
126 | config FEATURE_XARGS_SUPPORT_CONFIRMATION | ||
127 | bool "Enable prompt and confirmation option -p" | ||
128 | default n | ||
129 | depends on XARGS | ||
130 | help | ||
131 | Support prompt the user about whether to run each command | ||
132 | line and read a line from the terminal. | ||
133 | |||
134 | config FEATURE_XARGS_SUPPORT_QUOTES | ||
135 | bool "Enable support single and double quotes and backslash" | ||
136 | default n | ||
137 | depends on XARGS | ||
138 | help | ||
139 | Default xargs unsupport single and double quotes | ||
140 | and backslash for can use aruments with spaces. | ||
141 | |||
142 | config FEATURE_XARGS_SUPPORT_TERMOPT | ||
143 | bool "Enable support options -x" | ||
144 | default n | ||
145 | depends on XARGS | ||
146 | help | ||
147 | Enable support exit if the size (see the -s or -n option) | ||
148 | is exceeded. | ||
149 | |||
150 | config FEATURE_XARGS_SUPPORT_ZERO_TERM | ||
151 | bool "Enable null terminated option -0" | ||
152 | default n | ||
153 | depends on XARGS | ||
154 | help | ||
155 | Enable input filenames are terminated by a null character | ||
156 | instead of by whitespace, and the quotes and backslash | ||
157 | are not special. | ||
158 | |||
159 | endmenu | ||
diff --git a/findutils/Kbuild b/findutils/Kbuild new file mode 100644 index 000000000..7b504bacf --- /dev/null +++ b/findutils/Kbuild | |||
@@ -0,0 +1,10 @@ | |||
1 | # Makefile for busybox | ||
2 | # | ||
3 | # Copyright (C) 1999-2005 by Erik Andersen <andersen@codepoet.org> | ||
4 | # | ||
5 | # Licensed under the GPL v2, see the file LICENSE in this tarball. | ||
6 | |||
7 | lib-y:= | ||
8 | lib-$(CONFIG_FIND) += find.o | ||
9 | lib-$(CONFIG_GREP) += grep.o | ||
10 | lib-$(CONFIG_XARGS) += xargs.o | ||
diff --git a/findutils/find.c b/findutils/find.c new file mode 100644 index 000000000..bf6b71a83 --- /dev/null +++ b/findutils/find.c | |||
@@ -0,0 +1,582 @@ | |||
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 version 2, see the file LICENSE in this tarball. | ||
11 | */ | ||
12 | |||
13 | /* findutils-4.1.20: | ||
14 | * | ||
15 | * # find file.txt -exec 'echo {}' '{} {}' ';' | ||
16 | * find: echo file.txt: No such file or directory | ||
17 | * # find file.txt -exec 'echo' '{} {}' '; ' | ||
18 | * find: missing argument to `-exec' | ||
19 | * # find file.txt -exec 'echo {}' '{} {}' ';' junk | ||
20 | * find: paths must precede expression | ||
21 | * # find file.txt -exec 'echo {}' '{} {}' ';' junk ';' | ||
22 | * find: paths must precede expression | ||
23 | * # find file.txt -exec 'echo' '{} {}' ';' | ||
24 | * file.txt file.txt | ||
25 | * (strace: execve("/bin/echo", ["echo", "file.txt file.txt"], [ 30 vars ])) | ||
26 | * # find file.txt -exec 'echo' '{} {}' ';' -print -exec pwd ';' | ||
27 | * file.txt file.txt | ||
28 | * file.txt | ||
29 | * /tmp | ||
30 | * # find -name '*.c' -o -name '*.h' | ||
31 | * [shows files, *.c and *.h intermixed] | ||
32 | * # find file.txt -name '*f*' -o -name '*t*' | ||
33 | * file.txt | ||
34 | * # find file.txt -name '*z*' -o -name '*t*' | ||
35 | * file.txt | ||
36 | * # find file.txt -name '*f*' -o -name '*z*' | ||
37 | * file.txt | ||
38 | * | ||
39 | * # find t z -name '*t*' -print -o -name '*z*' | ||
40 | * t | ||
41 | * # find t z t z -name '*t*' -o -name '*z*' -print | ||
42 | * z | ||
43 | * z | ||
44 | * # find t z t z '(' -name '*t*' -o -name '*z*' ')' -o -print | ||
45 | * (no output) | ||
46 | */ | ||
47 | |||
48 | #include "busybox.h" | ||
49 | #include <fnmatch.h> | ||
50 | |||
51 | USE_FEATURE_FIND_XDEV(static dev_t *xdev_dev;) | ||
52 | USE_FEATURE_FIND_XDEV(static int xdev_count;) | ||
53 | |||
54 | typedef int (*action_fp)(const char *fileName, struct stat *statbuf, void *); | ||
55 | |||
56 | typedef struct { | ||
57 | action_fp f; | ||
58 | } action; | ||
59 | #define ACTS(name, arg...) typedef struct { action a; arg; } action_##name; | ||
60 | #define ACTF(name) static int func_##name(const char *fileName, struct stat *statbuf, action_##name* ap) | ||
61 | ACTS(print) | ||
62 | ACTS(name, char *pattern;) | ||
63 | USE_FEATURE_FIND_PRINT0(ACTS(print0)) | ||
64 | USE_FEATURE_FIND_TYPE( ACTS(type, int type_mask;)) | ||
65 | USE_FEATURE_FIND_PERM( ACTS(perm, char perm_char; int perm_mask;)) | ||
66 | USE_FEATURE_FIND_MTIME( ACTS(mtime, char mtime_char; int mtime_days;)) | ||
67 | USE_FEATURE_FIND_MMIN( ACTS(mmin, char mmin_char; int mmin_mins;)) | ||
68 | USE_FEATURE_FIND_NEWER( ACTS(newer, time_t newer_mtime;)) | ||
69 | USE_FEATURE_FIND_INUM( ACTS(inum, ino_t inode_num;)) | ||
70 | USE_FEATURE_FIND_EXEC( ACTS(exec, char **exec_argv; int *subst_count; int exec_argc;)) | ||
71 | USE_DESKTOP( ACTS(paren, action ***subexpr;)) | ||
72 | USE_DESKTOP( ACTS(size, off_t size;)) | ||
73 | USE_DESKTOP( ACTS(prune)) | ||
74 | |||
75 | static action ***actions; | ||
76 | static int need_print = 1; | ||
77 | |||
78 | static inline int one_char(const char* str, char c) | ||
79 | { | ||
80 | return (str[0] == c && str[1] == '\0'); | ||
81 | } | ||
82 | |||
83 | |||
84 | static int count_subst(const char *str) | ||
85 | { | ||
86 | int count = 0; | ||
87 | while ((str = strstr(str, "{}"))) { | ||
88 | count++; | ||
89 | str++; | ||
90 | } | ||
91 | return count; | ||
92 | } | ||
93 | |||
94 | |||
95 | static char* subst(const char *src, int count, const char* filename) | ||
96 | { | ||
97 | char *buf, *dst, *end; | ||
98 | int flen = strlen(filename); | ||
99 | /* we replace each '{}' with filename: growth by strlen-2 */ | ||
100 | buf = dst = xmalloc(strlen(src) + count*(flen-2) + 1); | ||
101 | while ((end = strstr(src, "{}"))) { | ||
102 | memcpy(dst, src, end - src); | ||
103 | dst += end - src; | ||
104 | src = end + 2; | ||
105 | memcpy(dst, filename, flen); | ||
106 | dst += flen; | ||
107 | } | ||
108 | strcpy(dst, src); | ||
109 | return buf; | ||
110 | } | ||
111 | |||
112 | |||
113 | static int exec_actions(action ***appp, const char *fileName, struct stat *statbuf) | ||
114 | { | ||
115 | int cur_group; | ||
116 | int cur_action; | ||
117 | int rc = TRUE; | ||
118 | action **app, *ap; | ||
119 | |||
120 | cur_group = -1; | ||
121 | while ((app = appp[++cur_group])) { | ||
122 | cur_action = -1; | ||
123 | do { | ||
124 | ap = app[++cur_action]; | ||
125 | } while (ap && (rc = ap->f(fileName, statbuf, ap))); | ||
126 | if (!ap) { | ||
127 | /* all actions in group were successful */ | ||
128 | break; | ||
129 | } | ||
130 | } | ||
131 | return rc; | ||
132 | } | ||
133 | |||
134 | |||
135 | ACTF(name) | ||
136 | { | ||
137 | const char *tmp = strrchr(fileName, '/'); | ||
138 | if (tmp == NULL) | ||
139 | tmp = fileName; | ||
140 | else | ||
141 | tmp++; | ||
142 | return fnmatch(ap->pattern, tmp, FNM_PERIOD) == 0; | ||
143 | } | ||
144 | #if ENABLE_FEATURE_FIND_TYPE | ||
145 | ACTF(type) | ||
146 | { | ||
147 | return ((statbuf->st_mode & S_IFMT) == ap->type_mask); | ||
148 | } | ||
149 | #endif | ||
150 | #if ENABLE_FEATURE_FIND_PERM | ||
151 | ACTF(perm) | ||
152 | { | ||
153 | return !((isdigit(ap->perm_char) && (statbuf->st_mode & 07777) == ap->perm_mask) | ||
154 | || (ap->perm_char == '-' && (statbuf->st_mode & ap->perm_mask) == ap->perm_mask) | ||
155 | || (ap->perm_char == '+' && (statbuf->st_mode & ap->perm_mask) != 0)); | ||
156 | } | ||
157 | #endif | ||
158 | #if ENABLE_FEATURE_FIND_MTIME | ||
159 | ACTF(mtime) | ||
160 | { | ||
161 | time_t file_age = time(NULL) - statbuf->st_mtime; | ||
162 | time_t mtime_secs = ap->mtime_days * 24 * 60 * 60; | ||
163 | return !((isdigit(ap->mtime_char) && file_age >= mtime_secs | ||
164 | && file_age < mtime_secs + 24 * 60 * 60) | ||
165 | || (ap->mtime_char == '+' && file_age >= mtime_secs + 24 * 60 * 60) | ||
166 | || (ap->mtime_char == '-' && file_age < mtime_secs)); | ||
167 | } | ||
168 | #endif | ||
169 | #if ENABLE_FEATURE_FIND_MMIN | ||
170 | ACTF(mmin) | ||
171 | { | ||
172 | time_t file_age = time(NULL) - statbuf->st_mtime; | ||
173 | time_t mmin_secs = ap->mmin_mins * 60; | ||
174 | return !((isdigit(ap->mmin_char) && file_age >= mmin_secs | ||
175 | && file_age < mmin_secs + 60) | ||
176 | || (ap->mmin_char == '+' && file_age >= mmin_secs + 60) | ||
177 | || (ap->mmin_char == '-' && file_age < mmin_secs)); | ||
178 | } | ||
179 | #endif | ||
180 | #if ENABLE_FEATURE_FIND_NEWER | ||
181 | ACTF(newer) | ||
182 | { | ||
183 | return (ap->newer_mtime >= statbuf->st_mtime); | ||
184 | } | ||
185 | #endif | ||
186 | #if ENABLE_FEATURE_FIND_INUM | ||
187 | ACTF(inum) | ||
188 | { | ||
189 | return (statbuf->st_ino != ap->inode_num); | ||
190 | } | ||
191 | #endif | ||
192 | #if ENABLE_FEATURE_FIND_EXEC | ||
193 | ACTF(exec) | ||
194 | { | ||
195 | int i, rc; | ||
196 | char *argv[ap->exec_argc+1]; | ||
197 | for (i = 0; i < ap->exec_argc; i++) | ||
198 | argv[i] = subst(ap->exec_argv[i], ap->subst_count[i], fileName); | ||
199 | argv[i] = NULL; /* terminate the list */ | ||
200 | errno = 0; | ||
201 | rc = wait4pid(spawn(argv)); | ||
202 | if (errno) | ||
203 | bb_perror_msg("%s", argv[0]); | ||
204 | for (i = 0; i < ap->exec_argc; i++) | ||
205 | free(argv[i]); | ||
206 | return rc == 0; /* return 1 if success */ | ||
207 | } | ||
208 | #endif | ||
209 | |||
210 | #if ENABLE_FEATURE_FIND_PRINT0 | ||
211 | ACTF(print0) | ||
212 | { | ||
213 | printf("%s%c", fileName, '\0'); | ||
214 | return TRUE; | ||
215 | } | ||
216 | #endif | ||
217 | |||
218 | ACTF(print) | ||
219 | { | ||
220 | puts(fileName); | ||
221 | return TRUE; | ||
222 | } | ||
223 | |||
224 | #if ENABLE_DESKTOP | ||
225 | ACTF(paren) | ||
226 | { | ||
227 | return exec_actions(ap->subexpr, fileName, statbuf); | ||
228 | } | ||
229 | |||
230 | /* | ||
231 | * -prune: if -depth is not given, return true and do not descend | ||
232 | * current dir; if -depth is given, return false with no effect. | ||
233 | * Example: | ||
234 | * find dir -name 'asm-*' -prune -o -name '*.[chS]' -print | ||
235 | */ | ||
236 | ACTF(prune) | ||
237 | { | ||
238 | return SKIP; | ||
239 | } | ||
240 | |||
241 | ACTF(size) | ||
242 | { | ||
243 | return statbuf->st_size == ap->size; | ||
244 | } | ||
245 | #endif | ||
246 | |||
247 | |||
248 | static int fileAction(const char *fileName, struct stat *statbuf, void* junk, int depth) | ||
249 | { | ||
250 | int rc; | ||
251 | #ifdef CONFIG_FEATURE_FIND_XDEV | ||
252 | if (S_ISDIR(statbuf->st_mode) && xdev_count) { | ||
253 | int i; | ||
254 | for (i = 0; i < xdev_count; i++) { | ||
255 | if (xdev_dev[i] != statbuf->st_dev) | ||
256 | return SKIP; | ||
257 | } | ||
258 | } | ||
259 | #endif | ||
260 | rc = exec_actions(actions, fileName, statbuf); | ||
261 | /* Had no explicit -print[0] or -exec? then print */ | ||
262 | if (rc && need_print) | ||
263 | puts(fileName); | ||
264 | /* Cannot return 0: our caller, recursive_action(), | ||
265 | * will perror() and skip dirs (if called on dir) */ | ||
266 | return rc == 0 ? TRUE : rc; | ||
267 | } | ||
268 | |||
269 | |||
270 | #if ENABLE_FEATURE_FIND_TYPE | ||
271 | static int find_type(char *type) | ||
272 | { | ||
273 | int mask = 0; | ||
274 | |||
275 | switch (type[0]) { | ||
276 | case 'b': | ||
277 | mask = S_IFBLK; | ||
278 | break; | ||
279 | case 'c': | ||
280 | mask = S_IFCHR; | ||
281 | break; | ||
282 | case 'd': | ||
283 | mask = S_IFDIR; | ||
284 | break; | ||
285 | case 'p': | ||
286 | mask = S_IFIFO; | ||
287 | break; | ||
288 | case 'f': | ||
289 | mask = S_IFREG; | ||
290 | break; | ||
291 | case 'l': | ||
292 | mask = S_IFLNK; | ||
293 | break; | ||
294 | case 's': | ||
295 | mask = S_IFSOCK; | ||
296 | break; | ||
297 | } | ||
298 | |||
299 | if (mask == 0 || type[1] != '\0') | ||
300 | bb_error_msg_and_die(bb_msg_invalid_arg, type, "-type"); | ||
301 | |||
302 | return mask; | ||
303 | } | ||
304 | #endif | ||
305 | |||
306 | action*** parse_params(char **argv) | ||
307 | { | ||
308 | action*** appp; | ||
309 | int cur_group = 0; | ||
310 | int cur_action = 0; | ||
311 | |||
312 | action* alloc_action(int sizeof_struct, action_fp f) | ||
313 | { | ||
314 | action *ap; | ||
315 | appp[cur_group] = xrealloc(appp[cur_group], (cur_action+2) * sizeof(*appp)); | ||
316 | appp[cur_group][cur_action++] = ap = xmalloc(sizeof_struct); | ||
317 | appp[cur_group][cur_action] = NULL; | ||
318 | ap->f = f; | ||
319 | return ap; | ||
320 | } | ||
321 | #define ALLOC_ACTION(name) (action_##name*)alloc_action(sizeof(action_##name), (action_fp) func_##name) | ||
322 | |||
323 | appp = xzalloc(2 * sizeof(*appp)); /* appp[0],[1] == NULL */ | ||
324 | |||
325 | // Actions have side effects and return a true or false value | ||
326 | // We implement: -print, -print0, -exec | ||
327 | |||
328 | // The rest are tests. | ||
329 | |||
330 | // Tests and actions are grouped by operators | ||
331 | // ( expr ) Force precedence | ||
332 | // ! expr True if expr is false | ||
333 | // -not expr Same as ! expr | ||
334 | // expr1 [-a[nd]] expr2 And; expr2 is not evaluated if expr1 is false | ||
335 | // expr1 -o[r] expr2 Or; expr2 is not evaluated if expr1 is true | ||
336 | // expr1 , expr2 List; both expr1 and expr2 are always evaluated | ||
337 | // We implement: (), -a, -o | ||
338 | |||
339 | while (*argv) { | ||
340 | char *arg = argv[0]; | ||
341 | char *arg1 = argv[1]; | ||
342 | /* --- Operators --- */ | ||
343 | if (strcmp(arg, "-a") == 0 | ||
344 | USE_DESKTOP(|| strcmp(arg, "-and") == 0) | ||
345 | ) { | ||
346 | /* no special handling required */ | ||
347 | } | ||
348 | else if (strcmp(arg, "-o") == 0 | ||
349 | USE_DESKTOP(|| strcmp(arg, "-or") == 0) | ||
350 | ) { | ||
351 | /* start new OR group */ | ||
352 | cur_group++; | ||
353 | appp = xrealloc(appp, (cur_group+2) * sizeof(*appp)); | ||
354 | appp[cur_group] = NULL; | ||
355 | appp[cur_group+1] = NULL; | ||
356 | cur_action = 0; | ||
357 | } | ||
358 | |||
359 | /* --- Tests and actions --- */ | ||
360 | else if (strcmp(arg, "-print") == 0) { | ||
361 | need_print = 0; | ||
362 | (void) ALLOC_ACTION(print); | ||
363 | } | ||
364 | #if ENABLE_FEATURE_FIND_PRINT0 | ||
365 | else if (strcmp(arg, "-print0") == 0) { | ||
366 | need_print = 0; | ||
367 | (void) ALLOC_ACTION(print0); | ||
368 | } | ||
369 | #endif | ||
370 | else if (strcmp(arg, "-name") == 0) { | ||
371 | action_name *ap; | ||
372 | if (!*++argv) | ||
373 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
374 | ap = ALLOC_ACTION(name); | ||
375 | ap->pattern = arg1; | ||
376 | } | ||
377 | #if ENABLE_FEATURE_FIND_TYPE | ||
378 | else if (strcmp(arg, "-type") == 0) { | ||
379 | action_type *ap; | ||
380 | if (!*++argv) | ||
381 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
382 | ap = ALLOC_ACTION(type); | ||
383 | ap->type_mask = find_type(arg1); | ||
384 | } | ||
385 | #endif | ||
386 | #if ENABLE_FEATURE_FIND_PERM | ||
387 | /* TODO: | ||
388 | * -perm mode File's permission bits are exactly mode (octal or symbolic). | ||
389 | * Symbolic modes use mode 0 as a point of departure. | ||
390 | * -perm -mode All of the permission bits mode are set for the file. | ||
391 | * -perm +mode Any of the permission bits mode are set for the file. | ||
392 | */ | ||
393 | else if (strcmp(arg, "-perm") == 0) { | ||
394 | action_perm *ap; | ||
395 | if (!*++argv) | ||
396 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
397 | ap = ALLOC_ACTION(perm); | ||
398 | ap->perm_mask = xstrtol_range(arg1, 8, 0, 07777); | ||
399 | ap->perm_char = arg1[0]; | ||
400 | if (ap->perm_char == '-') | ||
401 | ap->perm_mask = -ap->perm_mask; | ||
402 | } | ||
403 | #endif | ||
404 | #if ENABLE_FEATURE_FIND_MTIME | ||
405 | else if (strcmp(arg, "-mtime") == 0) { | ||
406 | action_mtime *ap; | ||
407 | if (!*++argv) | ||
408 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
409 | ap = ALLOC_ACTION(mtime); | ||
410 | ap->mtime_days = xatol(arg1); | ||
411 | ap->mtime_char = arg1[0]; | ||
412 | if (ap->mtime_char == '-') | ||
413 | ap->mtime_days = -ap->mtime_days; | ||
414 | } | ||
415 | #endif | ||
416 | #if ENABLE_FEATURE_FIND_MMIN | ||
417 | else if (strcmp(arg, "-mmin") == 0) { | ||
418 | action_mmin *ap; | ||
419 | if (!*++argv) | ||
420 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
421 | ap = ALLOC_ACTION(mmin); | ||
422 | ap->mmin_mins = xatol(arg1); | ||
423 | ap->mmin_char = arg1[0]; | ||
424 | if (ap->mmin_char == '-') | ||
425 | ap->mmin_mins = -ap->mmin_mins; | ||
426 | } | ||
427 | #endif | ||
428 | #if ENABLE_FEATURE_FIND_NEWER | ||
429 | else if (strcmp(arg, "-newer") == 0) { | ||
430 | action_newer *ap; | ||
431 | struct stat stat_newer; | ||
432 | if (!*++argv) | ||
433 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
434 | xstat(arg1, &stat_newer); | ||
435 | ap = ALLOC_ACTION(newer); | ||
436 | ap->newer_mtime = stat_newer.st_mtime; | ||
437 | } | ||
438 | #endif | ||
439 | #if ENABLE_FEATURE_FIND_INUM | ||
440 | else if (strcmp(arg, "-inum") == 0) { | ||
441 | action_inum *ap; | ||
442 | if (!*++argv) | ||
443 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
444 | ap = ALLOC_ACTION(inum); | ||
445 | ap->inode_num = xatoul(arg1); | ||
446 | } | ||
447 | #endif | ||
448 | #if ENABLE_FEATURE_FIND_EXEC | ||
449 | else if (strcmp(arg, "-exec") == 0) { | ||
450 | int i; | ||
451 | action_exec *ap; | ||
452 | need_print = 0; | ||
453 | ap = ALLOC_ACTION(exec); | ||
454 | ap->exec_argv = ++argv; /* first arg after -exec */ | ||
455 | ap->exec_argc = 0; | ||
456 | while (1) { | ||
457 | if (!*argv) /* did not see ';' till end */ | ||
458 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
459 | if (one_char(argv[0], ';')) | ||
460 | break; | ||
461 | argv++; | ||
462 | ap->exec_argc++; | ||
463 | } | ||
464 | if (ap->exec_argc == 0) | ||
465 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
466 | ap->subst_count = xmalloc(ap->exec_argc * sizeof(int)); | ||
467 | i = ap->exec_argc; | ||
468 | while (i--) | ||
469 | ap->subst_count[i] = count_subst(ap->exec_argv[i]); | ||
470 | } | ||
471 | #endif | ||
472 | #if ENABLE_DESKTOP | ||
473 | else if (one_char(arg, '(')) { | ||
474 | action_paren *ap; | ||
475 | char **endarg; | ||
476 | int nested = 1; | ||
477 | |||
478 | endarg = argv; | ||
479 | while (1) { | ||
480 | if (!*++endarg) | ||
481 | bb_error_msg_and_die("unpaired '('"); | ||
482 | if (one_char(*endarg, '(')) | ||
483 | nested++; | ||
484 | else if (one_char(*endarg, ')') && !--nested) { | ||
485 | *endarg = NULL; | ||
486 | break; | ||
487 | } | ||
488 | } | ||
489 | ap = ALLOC_ACTION(paren); | ||
490 | ap->subexpr = parse_params(argv + 1); | ||
491 | *endarg = ")"; /* restore NULLed parameter */ | ||
492 | argv = endarg; | ||
493 | } | ||
494 | else if (strcmp(arg, "-prune") == 0) { | ||
495 | (void) ALLOC_ACTION(prune); | ||
496 | } | ||
497 | else if (strcmp(arg, "-size") == 0) { | ||
498 | action_size *ap; | ||
499 | if (!*++argv) | ||
500 | bb_error_msg_and_die(bb_msg_requires_arg, arg); | ||
501 | ap = ALLOC_ACTION(size); | ||
502 | ap->size = XATOOFF(arg1); | ||
503 | } | ||
504 | #endif | ||
505 | else | ||
506 | bb_show_usage(); | ||
507 | argv++; | ||
508 | } | ||
509 | |||
510 | return appp; | ||
511 | #undef ALLOC_ACTION | ||
512 | } | ||
513 | |||
514 | |||
515 | int find_main(int argc, char **argv) | ||
516 | { | ||
517 | int dereference = FALSE; | ||
518 | char *arg; | ||
519 | char **argp; | ||
520 | int i, firstopt, status = EXIT_SUCCESS; | ||
521 | |||
522 | for (firstopt = 1; firstopt < argc; firstopt++) { | ||
523 | if (argv[firstopt][0] == '-') | ||
524 | break; | ||
525 | #if ENABLE_DESKTOP | ||
526 | if (one_char(argv[firstopt], '(')) | ||
527 | break; | ||
528 | #endif | ||
529 | } | ||
530 | if (firstopt == 1) { | ||
531 | argv[0] = "."; | ||
532 | argv--; | ||
533 | firstopt++; | ||
534 | } | ||
535 | |||
536 | // All options always return true. They always take effect, | ||
537 | // rather than being processed only when their place in the | ||
538 | // expression is reached | ||
539 | // We implement: -follow, -xdev | ||
540 | |||
541 | /* Process options, and replace then with -a */ | ||
542 | /* (-a will be ignored by recursive parser later) */ | ||
543 | argp = &argv[firstopt]; | ||
544 | while ((arg = argp[0])) { | ||
545 | if (strcmp(arg, "-follow") == 0) { | ||
546 | dereference = TRUE; | ||
547 | argp[0] = "-a"; | ||
548 | } | ||
549 | #if ENABLE_FEATURE_FIND_XDEV | ||
550 | else if (strcmp(arg, "-xdev") == 0) { | ||
551 | struct stat stbuf; | ||
552 | if (!xdev_count) { | ||
553 | xdev_count = firstopt - 1; | ||
554 | xdev_dev = xmalloc(xdev_count * sizeof(dev_t)); | ||
555 | for (i = 1; i < firstopt; i++) { | ||
556 | /* not xstat(): shouldn't bomb out on | ||
557 | * "find not_exist exist -xdev" */ | ||
558 | if (stat(argv[i], &stbuf)) stbuf.st_dev = -1L; | ||
559 | xdev_dev[i-1] = stbuf.st_dev; | ||
560 | } | ||
561 | } | ||
562 | argp[0] = "-a"; | ||
563 | } | ||
564 | argp++; | ||
565 | } | ||
566 | #endif | ||
567 | |||
568 | actions = parse_params(&argv[firstopt]); | ||
569 | |||
570 | for (i = 1; i < firstopt; i++) { | ||
571 | if (!recursive_action(argv[i], | ||
572 | TRUE, // recurse | ||
573 | dereference, // follow links | ||
574 | FALSE, // depth first | ||
575 | fileAction, // file action | ||
576 | fileAction, // dir action | ||
577 | NULL, // user data | ||
578 | 0)) // depth | ||
579 | status = EXIT_FAILURE; | ||
580 | } | ||
581 | return status; | ||
582 | } | ||
diff --git a/findutils/grep.c b/findutils/grep.c new file mode 100644 index 000000000..8bb38f95c --- /dev/null +++ b/findutils/grep.c | |||
@@ -0,0 +1,483 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini grep implementation for busybox using libc regex. | ||
4 | * | ||
5 | * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley | ||
6 | * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> | ||
7 | * | ||
8 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
9 | */ | ||
10 | /* BB_AUDIT SUSv3 defects - unsupported option -x. */ | ||
11 | /* BB_AUDIT GNU defects - always acts as -a. */ | ||
12 | /* http://www.opengroup.org/onlinepubs/007904975/utilities/grep.html */ | ||
13 | /* | ||
14 | * 2004,2006 (C) Vladimir Oleynik <dzo@simtreas.ru> - | ||
15 | * correction "-e pattern1 -e pattern2" logic and more optimizations. | ||
16 | * precompiled regex | ||
17 | */ | ||
18 | /* | ||
19 | * (C) 2006 Jac Goudsmit added -o option | ||
20 | */ | ||
21 | |||
22 | #include "busybox.h" | ||
23 | #include "xregex.h" | ||
24 | |||
25 | |||
26 | /* options */ | ||
27 | #define GREP_OPTS "lnqvscFiHhe:f:Lor" | ||
28 | #define GREP_OPT_l (1<<0) | ||
29 | #define PRINT_FILES_WITH_MATCHES (option_mask32 & GREP_OPT_l) | ||
30 | #define GREP_OPT_n (1<<1) | ||
31 | #define PRINT_LINE_NUM (option_mask32 & GREP_OPT_n) | ||
32 | #define GREP_OPT_q (1<<2) | ||
33 | #define BE_QUIET (option_mask32 & GREP_OPT_q) | ||
34 | #define GREP_OPT_v (1<<3) | ||
35 | #define GREP_OPT_s (1<<4) | ||
36 | #define SUPPRESS_ERR_MSGS (option_mask32 & GREP_OPT_s) | ||
37 | #define GREP_OPT_c (1<<5) | ||
38 | #define PRINT_MATCH_COUNTS (option_mask32 & GREP_OPT_c) | ||
39 | #define GREP_OPT_F (1<<6) | ||
40 | #define FGREP_FLAG (option_mask32 & GREP_OPT_F) | ||
41 | #define GREP_OPT_i (1<<7) | ||
42 | #define GREP_OPT_H (1<<8) | ||
43 | #define GREP_OPT_h (1<<9) | ||
44 | #define GREP_OPT_e (1<<10) | ||
45 | #define GREP_OPT_f (1<<11) | ||
46 | #define GREP_OPT_L (1<<12) | ||
47 | #define PRINT_FILES_WITHOUT_MATCHES (option_mask32 & GREP_OPT_L) | ||
48 | #define GREP_OPT_o (1<<13) | ||
49 | #define GREP_OPT_r (1<<14) | ||
50 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
51 | # define GREP_OPT_CONTEXT "A:B:C:" | ||
52 | # define GREP_OPT_A (1<<15) | ||
53 | # define GREP_OPT_B (1<<16) | ||
54 | # define GREP_OPT_C (1<<17) | ||
55 | # define GREP_OPT_E (1<<18) | ||
56 | #else | ||
57 | # define GREP_OPT_CONTEXT "" | ||
58 | # define GREP_OPT_A 0 | ||
59 | # define GREP_OPT_B 0 | ||
60 | # define GREP_OPT_C 0 | ||
61 | # define GREP_OPT_E (1<<15) | ||
62 | #endif | ||
63 | #if ENABLE_FEATURE_GREP_EGREP_ALIAS | ||
64 | # define OPT_EGREP "E" | ||
65 | #else | ||
66 | # define OPT_EGREP "" | ||
67 | #endif | ||
68 | |||
69 | typedef unsigned char byte_t; | ||
70 | |||
71 | static int reflags; | ||
72 | static byte_t invert_search; | ||
73 | static byte_t print_filename; | ||
74 | static byte_t open_errors; | ||
75 | |||
76 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
77 | static int lines_before; | ||
78 | static int lines_after; | ||
79 | static char **before_buf; | ||
80 | static int last_line_printed; | ||
81 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ | ||
82 | |||
83 | /* globals used internally */ | ||
84 | static llist_t *pattern_head; /* growable list of patterns to match */ | ||
85 | static const char *cur_file; /* the current file we are reading */ | ||
86 | |||
87 | typedef struct GREP_LIST_DATA { | ||
88 | char *pattern; | ||
89 | regex_t preg; | ||
90 | #define PATTERN_MEM_A 1 | ||
91 | #define COMPILED 2 | ||
92 | int flg_mem_alocated_compiled; | ||
93 | } grep_list_data_t; | ||
94 | |||
95 | static void print_line(const char *line, int linenum, char decoration) | ||
96 | { | ||
97 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
98 | /* possibly print the little '--' separator */ | ||
99 | if ((lines_before || lines_after) && last_line_printed && | ||
100 | last_line_printed < linenum - 1) { | ||
101 | puts("--"); | ||
102 | } | ||
103 | last_line_printed = linenum; | ||
104 | #endif | ||
105 | if (print_filename) | ||
106 | printf("%s%c", cur_file, decoration); | ||
107 | if (PRINT_LINE_NUM) | ||
108 | printf("%i%c", linenum, decoration); | ||
109 | /* Emulate weird GNU grep behavior with -ov */ | ||
110 | if ((option_mask32 & (GREP_OPT_v+GREP_OPT_o)) != (GREP_OPT_v+GREP_OPT_o)) | ||
111 | puts(line); | ||
112 | } | ||
113 | |||
114 | |||
115 | static int grep_file(FILE *file) | ||
116 | { | ||
117 | char *line; | ||
118 | byte_t ret; | ||
119 | int linenum = 0; | ||
120 | int nmatches = 0; | ||
121 | regmatch_t regmatch; | ||
122 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
123 | int print_n_lines_after = 0; | ||
124 | int curpos = 0; /* track where we are in the circular 'before' buffer */ | ||
125 | int idx = 0; /* used for iteration through the circular buffer */ | ||
126 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ | ||
127 | |||
128 | while ((line = xmalloc_getline(file)) != NULL) { | ||
129 | llist_t *pattern_ptr = pattern_head; | ||
130 | grep_list_data_t * gl; | ||
131 | |||
132 | linenum++; | ||
133 | ret = 0; | ||
134 | while (pattern_ptr) { | ||
135 | gl = (grep_list_data_t *)pattern_ptr->data; | ||
136 | if (FGREP_FLAG) { | ||
137 | ret = strstr(line, gl->pattern) != NULL; | ||
138 | } else { | ||
139 | /* | ||
140 | * test for a postitive-assertion match (regexec returns success (0) | ||
141 | * and the user did not specify invert search), or a negative-assertion | ||
142 | * match (regexec returns failure (REG_NOMATCH) and the user specified | ||
143 | * invert search) | ||
144 | */ | ||
145 | if (!(gl->flg_mem_alocated_compiled & COMPILED)) { | ||
146 | gl->flg_mem_alocated_compiled |= COMPILED; | ||
147 | xregcomp(&(gl->preg), gl->pattern, reflags); | ||
148 | } | ||
149 | regmatch.rm_so = 0; | ||
150 | regmatch.rm_eo = 0; | ||
151 | ret |= regexec(&(gl->preg), line, 1, ®match, 0) == 0; | ||
152 | } | ||
153 | pattern_ptr = pattern_ptr->link; | ||
154 | } /* while (pattern_ptr) */ | ||
155 | |||
156 | if (ret ^ invert_search) { | ||
157 | |||
158 | if (PRINT_FILES_WITH_MATCHES || BE_QUIET) | ||
159 | free(line); | ||
160 | |||
161 | /* if we found a match but were told to be quiet, stop here */ | ||
162 | if (BE_QUIET || PRINT_FILES_WITHOUT_MATCHES) | ||
163 | return -1; | ||
164 | |||
165 | /* keep track of matches */ | ||
166 | nmatches++; | ||
167 | |||
168 | /* if we're just printing filenames, we stop after the first match */ | ||
169 | if (PRINT_FILES_WITH_MATCHES) | ||
170 | break; | ||
171 | |||
172 | /* print the matched line */ | ||
173 | if (PRINT_MATCH_COUNTS == 0) { | ||
174 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
175 | int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1; | ||
176 | |||
177 | /* if we were told to print 'before' lines and there is at least | ||
178 | * one line in the circular buffer, print them */ | ||
179 | if (lines_before && before_buf[prevpos] != NULL) { | ||
180 | int first_buf_entry_line_num = linenum - lines_before; | ||
181 | |||
182 | /* advance to the first entry in the circular buffer, and | ||
183 | * figure out the line number is of the first line in the | ||
184 | * buffer */ | ||
185 | idx = curpos; | ||
186 | while (before_buf[idx] == NULL) { | ||
187 | idx = (idx + 1) % lines_before; | ||
188 | first_buf_entry_line_num++; | ||
189 | } | ||
190 | |||
191 | /* now print each line in the buffer, clearing them as we go */ | ||
192 | while (before_buf[idx] != NULL) { | ||
193 | print_line(before_buf[idx], first_buf_entry_line_num, '-'); | ||
194 | free(before_buf[idx]); | ||
195 | before_buf[idx] = NULL; | ||
196 | idx = (idx + 1) % lines_before; | ||
197 | first_buf_entry_line_num++; | ||
198 | } | ||
199 | } | ||
200 | |||
201 | /* make a note that we need to print 'after' lines */ | ||
202 | print_n_lines_after = lines_after; | ||
203 | #endif | ||
204 | if (option_mask32 & GREP_OPT_o) { | ||
205 | line[regmatch.rm_eo] = '\0'; | ||
206 | print_line(line + regmatch.rm_so, linenum, ':'); | ||
207 | } else { | ||
208 | print_line(line, linenum, ':'); | ||
209 | } | ||
210 | } | ||
211 | } | ||
212 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
213 | else { /* no match */ | ||
214 | /* Add the line to the circular 'before' buffer */ | ||
215 | if (lines_before) { | ||
216 | free(before_buf[curpos]); | ||
217 | before_buf[curpos] = xstrdup(line); | ||
218 | curpos = (curpos + 1) % lines_before; | ||
219 | } | ||
220 | } | ||
221 | |||
222 | /* if we need to print some context lines after the last match, do so */ | ||
223 | if (print_n_lines_after && (last_line_printed != linenum)) { | ||
224 | print_line(line, linenum, '-'); | ||
225 | print_n_lines_after--; | ||
226 | } | ||
227 | #endif /* ENABLE_FEATURE_GREP_CONTEXT */ | ||
228 | free(line); | ||
229 | } | ||
230 | |||
231 | /* special-case file post-processing for options where we don't print line | ||
232 | * matches, just filenames and possibly match counts */ | ||
233 | |||
234 | /* grep -c: print [filename:]count, even if count is zero */ | ||
235 | if (PRINT_MATCH_COUNTS) { | ||
236 | if (print_filename) | ||
237 | printf("%s:", cur_file); | ||
238 | printf("%d\n", nmatches); | ||
239 | } | ||
240 | |||
241 | /* grep -l: print just the filename, but only if we grepped the line in the file */ | ||
242 | if (PRINT_FILES_WITH_MATCHES && nmatches > 0) { | ||
243 | puts(cur_file); | ||
244 | } | ||
245 | |||
246 | /* grep -L: print just the filename, but only if we didn't grep the line in the file */ | ||
247 | if (PRINT_FILES_WITHOUT_MATCHES && nmatches == 0) { | ||
248 | puts(cur_file); | ||
249 | } | ||
250 | |||
251 | return nmatches; | ||
252 | } | ||
253 | |||
254 | #if ENABLE_FEATURE_CLEAN_UP | ||
255 | #define new_grep_list_data(p, m) add_grep_list_data(p, m) | ||
256 | static char * add_grep_list_data(char *pattern, int flg_used_mem) | ||
257 | #else | ||
258 | #define new_grep_list_data(p, m) add_grep_list_data(p) | ||
259 | static char * add_grep_list_data(char *pattern) | ||
260 | #endif | ||
261 | { | ||
262 | grep_list_data_t *gl = xmalloc(sizeof(grep_list_data_t)); | ||
263 | gl->pattern = pattern; | ||
264 | #if ENABLE_FEATURE_CLEAN_UP | ||
265 | gl->flg_mem_alocated_compiled = flg_used_mem; | ||
266 | #else | ||
267 | gl->flg_mem_alocated_compiled = 0; | ||
268 | #endif | ||
269 | return (char *)gl; | ||
270 | } | ||
271 | |||
272 | |||
273 | static void load_regexes_from_file(llist_t *fopt) | ||
274 | { | ||
275 | char *line; | ||
276 | FILE *f; | ||
277 | |||
278 | while (fopt) { | ||
279 | llist_t *cur = fopt; | ||
280 | char *ffile = cur->data; | ||
281 | |||
282 | fopt = cur->link; | ||
283 | free(cur); | ||
284 | f = xfopen(ffile, "r"); | ||
285 | while ((line = xmalloc_getline(f)) != NULL) { | ||
286 | llist_add_to(&pattern_head, | ||
287 | new_grep_list_data(line, PATTERN_MEM_A)); | ||
288 | } | ||
289 | } | ||
290 | } | ||
291 | |||
292 | |||
293 | static int file_action_grep(const char *filename, struct stat *statbuf, void* matched, int depth) | ||
294 | { | ||
295 | FILE *file = fopen(filename, "r"); | ||
296 | if (file == NULL) { | ||
297 | if (!SUPPRESS_ERR_MSGS) | ||
298 | bb_perror_msg("%s", cur_file); | ||
299 | open_errors = 1; | ||
300 | return 0; | ||
301 | } | ||
302 | cur_file = filename; | ||
303 | *(int*)matched += grep_file(file); | ||
304 | fclose(file); | ||
305 | return 1; | ||
306 | } | ||
307 | |||
308 | |||
309 | static int grep_dir(const char *dir) | ||
310 | { | ||
311 | int matched = 0; | ||
312 | recursive_action(dir, | ||
313 | /* recurse= */ 1, | ||
314 | /* followLinks= */ 0, | ||
315 | /* depthFirst= */ 1, | ||
316 | /* fileAction= */ file_action_grep, | ||
317 | /* dirAction= */ NULL, | ||
318 | /* userData= */ &matched, | ||
319 | /* depth= */ 0); | ||
320 | return matched; | ||
321 | } | ||
322 | |||
323 | |||
324 | int grep_main(int argc, char **argv) | ||
325 | { | ||
326 | FILE *file; | ||
327 | int matched; | ||
328 | llist_t *fopt = NULL; | ||
329 | |||
330 | /* do normal option parsing */ | ||
331 | #if ENABLE_FEATURE_GREP_CONTEXT | ||
332 | char *slines_after; | ||
333 | char *slines_before; | ||
334 | char *Copt; | ||
335 | |||
336 | opt_complementary = "H-h:e::f::C-AB"; | ||
337 | getopt32(argc, argv, | ||
338 | GREP_OPTS GREP_OPT_CONTEXT OPT_EGREP, | ||
339 | &pattern_head, &fopt, | ||
340 | &slines_after, &slines_before, &Copt); | ||
341 | |||
342 | if (option_mask32 & GREP_OPT_C) { | ||
343 | /* -C unsets prev -A and -B, but following -A or -B | ||
344 | may override it */ | ||
345 | if (!(option_mask32 & GREP_OPT_A)) /* not overridden */ | ||
346 | slines_after = Copt; | ||
347 | if (!(option_mask32 & GREP_OPT_B)) /* not overridden */ | ||
348 | slines_before = Copt; | ||
349 | option_mask32 |= GREP_OPT_A|GREP_OPT_B; /* for parser */ | ||
350 | } | ||
351 | if (option_mask32 & GREP_OPT_A) { | ||
352 | lines_after = xatoi_u(slines_after); | ||
353 | } | ||
354 | if (option_mask32 & GREP_OPT_B) { | ||
355 | lines_before = xatoi_u(slines_before); | ||
356 | } | ||
357 | /* sanity checks */ | ||
358 | if (option_mask32 & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L)) { | ||
359 | option_mask32 &= ~GREP_OPT_n; | ||
360 | lines_before = 0; | ||
361 | lines_after = 0; | ||
362 | } else if (lines_before > 0) | ||
363 | before_buf = (char **)xzalloc(lines_before * sizeof(char *)); | ||
364 | #else | ||
365 | /* with auto sanity checks */ | ||
366 | opt_complementary = "H-h:e::f::c-n:q-n:l-n"; | ||
367 | getopt32(argc, argv, GREP_OPTS OPT_EGREP, | ||
368 | &pattern_head, &fopt); | ||
369 | #endif | ||
370 | invert_search = ((option_mask32 & GREP_OPT_v) != 0); /* 0 | 1 */ | ||
371 | |||
372 | if (pattern_head != NULL) { | ||
373 | /* convert char *argv[] to grep_list_data_t */ | ||
374 | llist_t *cur; | ||
375 | |||
376 | for (cur = pattern_head; cur; cur = cur->link) | ||
377 | cur->data = new_grep_list_data(cur->data, 0); | ||
378 | } | ||
379 | if (option_mask32 & GREP_OPT_f) | ||
380 | load_regexes_from_file(fopt); | ||
381 | |||
382 | if (ENABLE_FEATURE_GREP_FGREP_ALIAS && applet_name[0] == 'f') | ||
383 | option_mask32 |= GREP_OPT_F; | ||
384 | |||
385 | if (!(option_mask32 & GREP_OPT_o)) | ||
386 | reflags = REG_NOSUB; | ||
387 | |||
388 | if (ENABLE_FEATURE_GREP_EGREP_ALIAS && | ||
389 | (applet_name[0] == 'e' || (option_mask32 & GREP_OPT_E))) | ||
390 | reflags |= REG_EXTENDED; | ||
391 | |||
392 | if (option_mask32 & GREP_OPT_i) | ||
393 | reflags |= REG_ICASE; | ||
394 | |||
395 | argv += optind; | ||
396 | argc -= optind; | ||
397 | |||
398 | /* if we didn't get a pattern from a -e and no command file was specified, | ||
399 | * argv[optind] should be the pattern. no pattern, no worky */ | ||
400 | if (pattern_head == NULL) { | ||
401 | if (*argv == NULL) | ||
402 | bb_show_usage(); | ||
403 | else { | ||
404 | char *pattern = new_grep_list_data(*argv++, 0); | ||
405 | |||
406 | llist_add_to(&pattern_head, pattern); | ||
407 | argc--; | ||
408 | } | ||
409 | } | ||
410 | |||
411 | /* argv[(optind)..(argc-1)] should be names of file to grep through. If | ||
412 | * there is more than one file to grep, we will print the filenames. */ | ||
413 | if (argc > 1) | ||
414 | print_filename = 1; | ||
415 | /* -H / -h of course override */ | ||
416 | if (option_mask32 & GREP_OPT_H) | ||
417 | print_filename = 1; | ||
418 | if (option_mask32 & GREP_OPT_h) | ||
419 | print_filename = 0; | ||
420 | |||
421 | /* If no files were specified, or '-' was specified, take input from | ||
422 | * stdin. Otherwise, we grep through all the files specified. */ | ||
423 | if (argc == 0) | ||
424 | argc++; | ||
425 | matched = 0; | ||
426 | while (argc--) { | ||
427 | cur_file = *argv++; | ||
428 | file = stdin; | ||
429 | if (!cur_file || (*cur_file == '-' && !cur_file[1])) { | ||
430 | cur_file = "(standard input)"; | ||
431 | } else { | ||
432 | if (option_mask32 & GREP_OPT_r) { | ||
433 | struct stat st; | ||
434 | if (stat(cur_file, &st) == 0 && S_ISDIR(st.st_mode)) { | ||
435 | if (!(option_mask32 & GREP_OPT_h)) | ||
436 | print_filename = 1; | ||
437 | matched += grep_dir(cur_file); | ||
438 | goto grep_done; | ||
439 | } | ||
440 | } | ||
441 | /* else: fopen(dir) will succeed, but reading won't */ | ||
442 | file = fopen(cur_file, "r"); | ||
443 | if (file == NULL) { | ||
444 | if (!SUPPRESS_ERR_MSGS) | ||
445 | bb_perror_msg("%s", cur_file); | ||
446 | open_errors = 1; | ||
447 | continue; | ||
448 | } | ||
449 | } | ||
450 | matched += grep_file(file); | ||
451 | fclose_if_not_stdin(file); | ||
452 | grep_done: | ||
453 | if (matched < 0) { | ||
454 | /* we found a match but were told to be quiet, stop here and | ||
455 | * return success */ | ||
456 | break; | ||
457 | } | ||
458 | } | ||
459 | |||
460 | /* destroy all the elments in the pattern list */ | ||
461 | if (ENABLE_FEATURE_CLEAN_UP) { | ||
462 | while (pattern_head) { | ||
463 | llist_t *pattern_head_ptr = pattern_head; | ||
464 | grep_list_data_t *gl = | ||
465 | (grep_list_data_t *)pattern_head_ptr->data; | ||
466 | |||
467 | pattern_head = pattern_head->link; | ||
468 | if ((gl->flg_mem_alocated_compiled & PATTERN_MEM_A)) | ||
469 | free(gl->pattern); | ||
470 | if ((gl->flg_mem_alocated_compiled & COMPILED)) | ||
471 | regfree(&(gl->preg)); | ||
472 | free(pattern_head_ptr); | ||
473 | } | ||
474 | } | ||
475 | /* 0 = success, 1 = failed, 2 = error */ | ||
476 | /* If the -q option is specified, the exit status shall be zero | ||
477 | * if an input line is selected, even if an error was detected. */ | ||
478 | if (BE_QUIET && matched) | ||
479 | return 0; | ||
480 | if (open_errors) | ||
481 | return 2; | ||
482 | return !matched; /* invert return value 0 = success, 1 = failed */ | ||
483 | } | ||
diff --git a/findutils/xargs.c b/findutils/xargs.c new file mode 100644 index 000000000..029d4077d --- /dev/null +++ b/findutils/xargs.c | |||
@@ -0,0 +1,539 @@ | |||
1 | /* vi: set sw=4 ts=4: */ | ||
2 | /* | ||
3 | * Mini xargs implementation for busybox | ||
4 | * Options are supported: "-prtx -n max_arg -s max_chars -e[ouf_str]" | ||
5 | * | ||
6 | * (C) 2002,2003 by Vladimir Oleynik <dzo@simtreas.ru> | ||
7 | * | ||
8 | * Special thanks | ||
9 | * - Mark Whitley and Glenn McGrath for stimulus to rewrite :) | ||
10 | * - Mike Rendell <michael@cs.mun.ca> | ||
11 | * and David MacKenzie <djm@gnu.ai.mit.edu>. | ||
12 | * | ||
13 | * Licensed under the GPL v2 or later, see the file LICENSE in this tarball. | ||
14 | * | ||
15 | * xargs is described in the Single Unix Specification v3 at | ||
16 | * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html | ||
17 | * | ||
18 | */ | ||
19 | |||
20 | #include "busybox.h" | ||
21 | |||
22 | /* COMPAT: SYSV version defaults size (and has a max value of) to 470. | ||
23 | We try to make it as large as possible. */ | ||
24 | #if !defined(ARG_MAX) && defined(_SC_ARG_MAX) | ||
25 | #define ARG_MAX sysconf (_SC_ARG_MAX) | ||
26 | #endif | ||
27 | #ifndef ARG_MAX | ||
28 | #define ARG_MAX 470 | ||
29 | #endif | ||
30 | |||
31 | |||
32 | #ifdef TEST | ||
33 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION | ||
34 | # define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION | ||
35 | # endif | ||
36 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES | ||
37 | # define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES | ||
38 | # endif | ||
39 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT | ||
40 | # define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT | ||
41 | # endif | ||
42 | # ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM | ||
43 | # define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM | ||
44 | # endif | ||
45 | #endif | ||
46 | |||
47 | /* | ||
48 | This function has special algorithm. | ||
49 | Don't use fork and include to main! | ||
50 | */ | ||
51 | static int xargs_exec(char *const *args) | ||
52 | { | ||
53 | pid_t p; | ||
54 | volatile int exec_errno = 0; /* shared vfork stack */ | ||
55 | int status; | ||
56 | |||
57 | p = vfork(); | ||
58 | if (p < 0) | ||
59 | bb_perror_msg_and_die("vfork"); | ||
60 | |||
61 | if (p == 0) { | ||
62 | /* vfork -- child */ | ||
63 | execvp(args[0], args); | ||
64 | exec_errno = errno; /* set error to shared stack */ | ||
65 | _exit(1); | ||
66 | } | ||
67 | |||
68 | /* vfork -- parent */ | ||
69 | while (wait(&status) == (pid_t) -1) | ||
70 | if (errno != EINTR) | ||
71 | break; | ||
72 | if (exec_errno) { | ||
73 | errno = exec_errno; | ||
74 | bb_perror_msg("%s", args[0]); | ||
75 | return exec_errno == ENOENT ? 127 : 126; | ||
76 | } | ||
77 | if (WEXITSTATUS(status) == 255) { | ||
78 | bb_error_msg("%s: exited with status 255; aborting", args[0]); | ||
79 | return 124; | ||
80 | } | ||
81 | if (WIFSTOPPED(status)) { | ||
82 | bb_error_msg("%s: stopped by signal %d", | ||
83 | args[0], WSTOPSIG(status)); | ||
84 | return 125; | ||
85 | } | ||
86 | if (WIFSIGNALED(status)) { | ||
87 | bb_error_msg("%s: terminated by signal %d", | ||
88 | args[0], WTERMSIG(status)); | ||
89 | return 125; | ||
90 | } | ||
91 | if (WEXITSTATUS(status)) | ||
92 | return 123; | ||
93 | return 0; | ||
94 | } | ||
95 | |||
96 | |||
97 | typedef struct xlist_s { | ||
98 | char *data; | ||
99 | size_t lenght; | ||
100 | struct xlist_s *link; | ||
101 | } xlist_t; | ||
102 | |||
103 | static int eof_stdin_detected; | ||
104 | |||
105 | #define ISBLANK(c) ((c) == ' ' || (c) == '\t') | ||
106 | #define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \ | ||
107 | || (c) == '\f' || (c) == '\v') | ||
108 | |||
109 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES | ||
110 | static xlist_t *process_stdin(xlist_t * list_arg, | ||
111 | const char *eof_str, size_t mc, char *buf) | ||
112 | { | ||
113 | #define NORM 0 | ||
114 | #define QUOTE 1 | ||
115 | #define BACKSLASH 2 | ||
116 | #define SPACE 4 | ||
117 | |||
118 | char *s = NULL; /* start word */ | ||
119 | char *p = NULL; /* pointer to end word */ | ||
120 | char q = 0; /* quote char */ | ||
121 | char state = NORM; | ||
122 | char eof_str_detected = 0; | ||
123 | size_t line_l = 0; /* size loaded args line */ | ||
124 | int c; /* current char */ | ||
125 | xlist_t *cur; | ||
126 | xlist_t *prev; | ||
127 | |||
128 | for (prev = cur = list_arg; cur; cur = cur->link) { | ||
129 | line_l += cur->lenght; /* previous allocated */ | ||
130 | if (prev != cur) | ||
131 | prev = prev->link; | ||
132 | } | ||
133 | |||
134 | while (!eof_stdin_detected) { | ||
135 | c = getchar(); | ||
136 | if (c == EOF) { | ||
137 | eof_stdin_detected++; | ||
138 | if (s) | ||
139 | goto unexpected_eof; | ||
140 | break; | ||
141 | } | ||
142 | if (eof_str_detected) | ||
143 | continue; | ||
144 | if (state == BACKSLASH) { | ||
145 | state = NORM; | ||
146 | goto set; | ||
147 | } else if (state == QUOTE) { | ||
148 | if (c == q) { | ||
149 | q = 0; | ||
150 | state = NORM; | ||
151 | } else { | ||
152 | goto set; | ||
153 | } | ||
154 | } else { /* if(state == NORM) */ | ||
155 | |||
156 | if (ISSPACE(c)) { | ||
157 | if (s) { | ||
158 | unexpected_eof: | ||
159 | state = SPACE; | ||
160 | c = 0; | ||
161 | goto set; | ||
162 | } | ||
163 | } else { | ||
164 | if (s == NULL) | ||
165 | s = p = buf; | ||
166 | if (c == '\\') { | ||
167 | state = BACKSLASH; | ||
168 | } else if (c == '\'' || c == '"') { | ||
169 | q = c; | ||
170 | state = QUOTE; | ||
171 | } else { | ||
172 | set: | ||
173 | if ((size_t)(p - buf) >= mc) | ||
174 | bb_error_msg_and_die("argument line too long"); | ||
175 | *p++ = c; | ||
176 | } | ||
177 | } | ||
178 | } | ||
179 | if (state == SPACE) { /* word's delimiter or EOF detected */ | ||
180 | if (q) { | ||
181 | bb_error_msg_and_die("unmatched %s quote", | ||
182 | q == '\'' ? "single" : "double"); | ||
183 | } | ||
184 | /* word loaded */ | ||
185 | if (eof_str) { | ||
186 | eof_str_detected = strcmp(s, eof_str) == 0; | ||
187 | } | ||
188 | if (!eof_str_detected) { | ||
189 | size_t lenght = (p - buf); | ||
190 | |||
191 | cur = xmalloc(sizeof(xlist_t) + lenght); | ||
192 | cur->data = memcpy(cur + 1, s, lenght); | ||
193 | cur->lenght = lenght; | ||
194 | cur->link = NULL; | ||
195 | if (prev == NULL) { | ||
196 | list_arg = cur; | ||
197 | } else { | ||
198 | prev->link = cur; | ||
199 | } | ||
200 | prev = cur; | ||
201 | line_l += lenght; | ||
202 | if (line_l > mc) { | ||
203 | /* stop memory usage :-) */ | ||
204 | break; | ||
205 | } | ||
206 | } | ||
207 | s = NULL; | ||
208 | state = NORM; | ||
209 | } | ||
210 | } | ||
211 | return list_arg; | ||
212 | } | ||
213 | #else | ||
214 | /* The variant does not support single quotes, double quotes or backslash */ | ||
215 | static xlist_t *process_stdin(xlist_t * list_arg, | ||
216 | const char *eof_str, size_t mc, char *buf) | ||
217 | { | ||
218 | |||
219 | int c; /* current char */ | ||
220 | int eof_str_detected = 0; | ||
221 | char *s = NULL; /* start word */ | ||
222 | char *p = NULL; /* pointer to end word */ | ||
223 | size_t line_l = 0; /* size loaded args line */ | ||
224 | xlist_t *cur; | ||
225 | xlist_t *prev; | ||
226 | |||
227 | for (prev = cur = list_arg; cur; cur = cur->link) { | ||
228 | line_l += cur->lenght; /* previous allocated */ | ||
229 | if (prev != cur) | ||
230 | prev = prev->link; | ||
231 | } | ||
232 | |||
233 | while (!eof_stdin_detected) { | ||
234 | c = getchar(); | ||
235 | if (c == EOF) { | ||
236 | eof_stdin_detected++; | ||
237 | } | ||
238 | if (eof_str_detected) | ||
239 | continue; | ||
240 | if (c == EOF || ISSPACE(c)) { | ||
241 | if (s == NULL) | ||
242 | continue; | ||
243 | c = EOF; | ||
244 | } | ||
245 | if (s == NULL) | ||
246 | s = p = buf; | ||
247 | if ((p - buf) >= mc) | ||
248 | bb_error_msg_and_die("argument line too long"); | ||
249 | *p++ = c == EOF ? 0 : c; | ||
250 | if (c == EOF) { /* word's delimiter or EOF detected */ | ||
251 | /* word loaded */ | ||
252 | if (eof_str) { | ||
253 | eof_str_detected = strcmp(s, eof_str) == 0; | ||
254 | } | ||
255 | if (!eof_str_detected) { | ||
256 | size_t lenght = (p - buf); | ||
257 | |||
258 | cur = xmalloc(sizeof(xlist_t) + lenght); | ||
259 | cur->data = memcpy(cur + 1, s, lenght); | ||
260 | cur->lenght = lenght; | ||
261 | cur->link = NULL; | ||
262 | if (prev == NULL) { | ||
263 | list_arg = cur; | ||
264 | } else { | ||
265 | prev->link = cur; | ||
266 | } | ||
267 | prev = cur; | ||
268 | line_l += lenght; | ||
269 | if (line_l > mc) { | ||
270 | /* stop memory usage :-) */ | ||
271 | break; | ||
272 | } | ||
273 | s = NULL; | ||
274 | } | ||
275 | } | ||
276 | } | ||
277 | return list_arg; | ||
278 | } | ||
279 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */ | ||
280 | |||
281 | |||
282 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION | ||
283 | /* Prompt the user for a response, and | ||
284 | if the user responds affirmatively, return true; | ||
285 | otherwise, return false. Used "/dev/tty", not stdin. */ | ||
286 | static int xargs_ask_confirmation(void) | ||
287 | { | ||
288 | static FILE *tty_stream; | ||
289 | int c, savec; | ||
290 | |||
291 | if (!tty_stream) { | ||
292 | tty_stream = xfopen(CURRENT_TTY, "r"); | ||
293 | /* pranoidal security by vodz */ | ||
294 | fcntl(fileno(tty_stream), F_SETFD, FD_CLOEXEC); | ||
295 | } | ||
296 | fputs(" ?...", stderr); | ||
297 | fflush(stderr); | ||
298 | c = savec = getc(tty_stream); | ||
299 | while (c != EOF && c != '\n') | ||
300 | c = getc(tty_stream); | ||
301 | if (savec == 'y' || savec == 'Y') | ||
302 | return 1; | ||
303 | return 0; | ||
304 | } | ||
305 | #else | ||
306 | # define xargs_ask_confirmation() 1 | ||
307 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */ | ||
308 | |||
309 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM | ||
310 | static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED, | ||
311 | size_t mc, char *buf) | ||
312 | { | ||
313 | int c; /* current char */ | ||
314 | char *s = NULL; /* start word */ | ||
315 | char *p = NULL; /* pointer to end word */ | ||
316 | size_t line_l = 0; /* size loaded args line */ | ||
317 | xlist_t *cur; | ||
318 | xlist_t *prev; | ||
319 | |||
320 | for (prev = cur = list_arg; cur; cur = cur->link) { | ||
321 | line_l += cur->lenght; /* previous allocated */ | ||
322 | if (prev != cur) | ||
323 | prev = prev->link; | ||
324 | } | ||
325 | |||
326 | while (!eof_stdin_detected) { | ||
327 | c = getchar(); | ||
328 | if (c == EOF) { | ||
329 | eof_stdin_detected++; | ||
330 | if (s == NULL) | ||
331 | break; | ||
332 | c = 0; | ||
333 | } | ||
334 | if (s == NULL) | ||
335 | s = p = buf; | ||
336 | if ((size_t)(p - buf) >= mc) | ||
337 | bb_error_msg_and_die("argument line too long"); | ||
338 | *p++ = c; | ||
339 | if (c == 0) { /* word's delimiter or EOF detected */ | ||
340 | /* word loaded */ | ||
341 | size_t lenght = (p - buf); | ||
342 | |||
343 | cur = xmalloc(sizeof(xlist_t) + lenght); | ||
344 | cur->data = memcpy(cur + 1, s, lenght); | ||
345 | cur->lenght = lenght; | ||
346 | cur->link = NULL; | ||
347 | if (prev == NULL) { | ||
348 | list_arg = cur; | ||
349 | } else { | ||
350 | prev->link = cur; | ||
351 | } | ||
352 | prev = cur; | ||
353 | line_l += lenght; | ||
354 | if (line_l > mc) { | ||
355 | /* stop memory usage :-) */ | ||
356 | break; | ||
357 | } | ||
358 | s = NULL; | ||
359 | } | ||
360 | } | ||
361 | return list_arg; | ||
362 | } | ||
363 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */ | ||
364 | |||
365 | /* Correct regardless of combination of CONFIG_xxx */ | ||
366 | enum { | ||
367 | OPTBIT_VERBOSE = 0, | ||
368 | OPTBIT_NO_EMPTY, | ||
369 | OPTBIT_UPTO_NUMBER, | ||
370 | OPTBIT_UPTO_SIZE, | ||
371 | OPTBIT_EOF_STRING, | ||
372 | USE_FEATURE_XARGS_SUPPORT_CONFIRMATION(OPTBIT_INTERACTIVE,) | ||
373 | USE_FEATURE_XARGS_SUPPORT_TERMOPT( OPTBIT_TERMINATE ,) | ||
374 | USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( OPTBIT_ZEROTERM ,) | ||
375 | |||
376 | OPT_VERBOSE = 1<<OPTBIT_VERBOSE , | ||
377 | OPT_NO_EMPTY = 1<<OPTBIT_NO_EMPTY , | ||
378 | OPT_UPTO_NUMBER = 1<<OPTBIT_UPTO_NUMBER, | ||
379 | OPT_UPTO_SIZE = 1<<OPTBIT_UPTO_SIZE , | ||
380 | OPT_EOF_STRING = 1<<OPTBIT_EOF_STRING , | ||
381 | OPT_INTERACTIVE = USE_FEATURE_XARGS_SUPPORT_CONFIRMATION((1<<OPTBIT_INTERACTIVE)) + 0, | ||
382 | OPT_TERMINATE = USE_FEATURE_XARGS_SUPPORT_TERMOPT( (1<<OPTBIT_TERMINATE )) + 0, | ||
383 | OPT_ZEROTERM = USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( (1<<OPTBIT_ZEROTERM )) + 0, | ||
384 | }; | ||
385 | #define OPTION_STR "+trn:s:e::" \ | ||
386 | USE_FEATURE_XARGS_SUPPORT_CONFIRMATION("p") \ | ||
387 | USE_FEATURE_XARGS_SUPPORT_TERMOPT( "x") \ | ||
388 | USE_FEATURE_XARGS_SUPPORT_ZERO_TERM( "0") | ||
389 | |||
390 | int xargs_main(int argc, char **argv) | ||
391 | { | ||
392 | char **args; | ||
393 | int i, n; | ||
394 | xlist_t *list = NULL; | ||
395 | xlist_t *cur; | ||
396 | int child_error = 0; | ||
397 | char *max_args, *max_chars; | ||
398 | int n_max_arg; | ||
399 | size_t n_chars = 0; | ||
400 | long orig_arg_max; | ||
401 | const char *eof_str = "_"; | ||
402 | unsigned opt; | ||
403 | size_t n_max_chars; | ||
404 | #if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM | ||
405 | xlist_t* (*read_args)(xlist_t*, const char*, size_t, char*) = process_stdin; | ||
406 | #else | ||
407 | #define read_args process_stdin | ||
408 | #endif | ||
409 | |||
410 | opt = getopt32(argc, argv, OPTION_STR, &max_args, &max_chars, &eof_str); | ||
411 | |||
412 | if (opt & OPT_ZEROTERM) | ||
413 | USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); | ||
414 | |||
415 | argc -= optind; | ||
416 | argv += optind; | ||
417 | if (!argc) { | ||
418 | /* default behavior is to echo all the filenames */ | ||
419 | *argv = "echo"; | ||
420 | argc++; | ||
421 | } | ||
422 | |||
423 | orig_arg_max = ARG_MAX; | ||
424 | if (orig_arg_max == -1) | ||
425 | orig_arg_max = LONG_MAX; | ||
426 | orig_arg_max -= 2048; /* POSIX.2 requires subtracting 2048 */ | ||
427 | |||
428 | if (opt & OPT_UPTO_SIZE) { | ||
429 | n_max_chars = xatoul_range(max_chars, 1, orig_arg_max); | ||
430 | for (i = 0; i < argc; i++) { | ||
431 | n_chars += strlen(*argv) + 1; | ||
432 | } | ||
433 | if (n_max_chars < n_chars) { | ||
434 | bb_error_msg_and_die("cannot fit single argument within argument list size limit"); | ||
435 | } | ||
436 | n_max_chars -= n_chars; | ||
437 | } else { | ||
438 | /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which | ||
439 | have it at 1 meg). Things will work fine with a large ARG_MAX but it | ||
440 | will probably hurt the system more than it needs to; an array of this | ||
441 | size is allocated. */ | ||
442 | if (orig_arg_max > 20 * 1024) | ||
443 | orig_arg_max = 20 * 1024; | ||
444 | n_max_chars = orig_arg_max; | ||
445 | } | ||
446 | max_chars = xmalloc(n_max_chars); | ||
447 | |||
448 | if (opt & OPT_UPTO_NUMBER) { | ||
449 | n_max_arg = xatoul_range(max_args, 1, INT_MAX); | ||
450 | } else { | ||
451 | n_max_arg = n_max_chars; | ||
452 | } | ||
453 | |||
454 | while ((list = read_args(list, eof_str, n_max_chars, max_chars)) != NULL || | ||
455 | !(opt & OPT_NO_EMPTY)) | ||
456 | { | ||
457 | opt |= OPT_NO_EMPTY; | ||
458 | n = 0; | ||
459 | n_chars = 0; | ||
460 | #ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT | ||
461 | for (cur = list; cur;) { | ||
462 | n_chars += cur->lenght; | ||
463 | n++; | ||
464 | cur = cur->link; | ||
465 | if (n_chars > n_max_chars || (n == n_max_arg && cur)) { | ||
466 | if (opt & OPT_TERMINATE) | ||
467 | bb_error_msg_and_die("argument list too long"); | ||
468 | break; | ||
469 | } | ||
470 | } | ||
471 | #else | ||
472 | for (cur = list; cur; cur = cur->link) { | ||
473 | n_chars += cur->lenght; | ||
474 | n++; | ||
475 | if (n_chars > n_max_chars || n == n_max_arg) { | ||
476 | break; | ||
477 | } | ||
478 | } | ||
479 | #endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */ | ||
480 | |||
481 | /* allocate pointers for execvp: | ||
482 | argc*arg, n*arg from stdin, NULL */ | ||
483 | args = xzalloc((n + argc + 1) * sizeof(char *)); | ||
484 | |||
485 | /* store the command to be executed | ||
486 | (taken from the command line) */ | ||
487 | for (i = 0; i < argc; i++) | ||
488 | args[i] = argv[i]; | ||
489 | /* (taken from stdin) */ | ||
490 | for (cur = list; n; cur = cur->link) { | ||
491 | args[i++] = cur->data; | ||
492 | n--; | ||
493 | } | ||
494 | |||
495 | if (opt & (OPT_INTERACTIVE | OPT_VERBOSE)) { | ||
496 | for (i = 0; args[i]; i++) { | ||
497 | if (i) | ||
498 | fputc(' ', stderr); | ||
499 | fputs(args[i], stderr); | ||
500 | } | ||
501 | if (!(opt & OPT_INTERACTIVE)) | ||
502 | fputc('\n', stderr); | ||
503 | } | ||
504 | if (!(opt & OPT_INTERACTIVE) || xargs_ask_confirmation()) { | ||
505 | child_error = xargs_exec(args); | ||
506 | } | ||
507 | |||
508 | /* clean up */ | ||
509 | for (i = argc; args[i]; i++) { | ||
510 | cur = list; | ||
511 | list = list->link; | ||
512 | free(cur); | ||
513 | } | ||
514 | free(args); | ||
515 | if (child_error > 0 && child_error != 123) { | ||
516 | break; | ||
517 | } | ||
518 | } | ||
519 | if (ENABLE_FEATURE_CLEAN_UP) free(max_chars); | ||
520 | return child_error; | ||
521 | } | ||
522 | |||
523 | |||
524 | #ifdef TEST | ||
525 | |||
526 | const char *applet_name = "debug stuff usage"; | ||
527 | |||
528 | void bb_show_usage(void) | ||
529 | { | ||
530 | fprintf(stderr, "Usage: %s [-p] [-r] [-t] -[x] [-n max_arg] [-s max_chars]\n", | ||
531 | applet_name); | ||
532 | exit(1); | ||
533 | } | ||
534 | |||
535 | int main(int argc, char **argv) | ||
536 | { | ||
537 | return xargs_main(argc, argv); | ||
538 | } | ||
539 | #endif /* TEST */ | ||