aboutsummaryrefslogtreecommitdiff
path: root/findutils/xargs.c
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2007-04-09 21:30:53 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2007-04-09 21:30:53 +0000
commit1b4b2cb20e5291c319ce0c7721e64445e2749b10 (patch)
tree23a0ccfb8d766037d3dd425b6813d74c27c2efe9 /findutils/xargs.c
parent53d445aa7571c780b8f2410afb4f326e45f851e4 (diff)
downloadbusybox-w32-1b4b2cb20e5291c319ce0c7721e64445e2749b10.tar.gz
busybox-w32-1b4b2cb20e5291c319ce0c7721e64445e2749b10.tar.bz2
busybox-w32-1b4b2cb20e5291c319ce0c7721e64445e2749b10.zip
xargs: shrink code, ~80 bytes
applets.h: +#undef APPLET_NOEXEC
Diffstat (limited to 'findutils/xargs.c')
-rw-r--r--findutils/xargs.c152
1 files changed, 77 insertions, 75 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c
index 44dbcdad0..ea7c22060 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -30,17 +30,17 @@
30 30
31 31
32#ifdef TEST 32#ifdef TEST
33# ifndef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION 33# ifndef ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
34# define CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION 34# define ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION 1
35# endif 35# endif
36# ifndef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES 36# ifndef ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
37# define CONFIG_FEATURE_XARGS_SUPPORT_QUOTES 37# define ENABLE_FEATURE_XARGS_SUPPORT_QUOTES 1
38# endif 38# endif
39# ifndef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT 39# ifndef ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
40# define CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT 40# define ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT 1
41# endif 41# endif
42# ifndef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM 42# ifndef ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
43# define CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM 43# define ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM 1
44# endif 44# endif
45#endif 45#endif
46 46
@@ -94,20 +94,20 @@ static int xargs_exec(char *const *args)
94} 94}
95 95
96 96
97typedef struct xlist_s { 97typedef struct xlist_t {
98 char *data; 98 char *data;
99 size_t lenght; 99 size_t length;
100 struct xlist_s *link; 100 struct xlist_t *link;
101} xlist_t; 101} xlist_t;
102 102
103static int eof_stdin_detected; 103static smallint eof_stdin_detected;
104 104
105#define ISBLANK(c) ((c) == ' ' || (c) == '\t') 105#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
106#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \ 106#define ISSPACE(c) (ISBLANK(c) || (c) == '\n' || (c) == '\r' \
107 || (c) == '\f' || (c) == '\v') 107 || (c) == '\f' || (c) == '\v')
108 108
109#ifdef CONFIG_FEATURE_XARGS_SUPPORT_QUOTES 109#if ENABLE_FEATURE_XARGS_SUPPORT_QUOTES
110static xlist_t *process_stdin(xlist_t * list_arg, 110static xlist_t *process_stdin(xlist_t *list_arg,
111 const char *eof_str, size_t mc, char *buf) 111 const char *eof_str, size_t mc, char *buf)
112{ 112{
113#define NORM 0 113#define NORM 0
@@ -125,16 +125,18 @@ static xlist_t *process_stdin(xlist_t * list_arg,
125 xlist_t *cur; 125 xlist_t *cur;
126 xlist_t *prev; 126 xlist_t *prev;
127 127
128 for (prev = cur = list_arg; cur; cur = cur->link) { 128 cur = list_arg;
129 line_l += cur->lenght; /* previous allocated */ 129 while (1) {
130 if (prev != cur) 130 prev = cur;
131 prev = prev->link; 131 if (!cur) break;
132 line_l += cur->length;
133 cur = cur->link;
132 } 134 }
133 135
134 while (!eof_stdin_detected) { 136 while (!eof_stdin_detected) {
135 c = getchar(); 137 c = getchar();
136 if (c == EOF) { 138 if (c == EOF) {
137 eof_stdin_detected++; 139 eof_stdin_detected = 1;
138 if (s) 140 if (s)
139 goto unexpected_eof; 141 goto unexpected_eof;
140 break; 142 break;
@@ -183,22 +185,22 @@ set:
183 } 185 }
184 /* word loaded */ 186 /* word loaded */
185 if (eof_str) { 187 if (eof_str) {
186 eof_str_detected = strcmp(s, eof_str) == 0; 188 eof_str_detected = (strcmp(s, eof_str) == 0);
187 } 189 }
188 if (!eof_str_detected) { 190 if (!eof_str_detected) {
189 size_t lenght = (p - buf); 191 size_t length = (p - buf);
190 192
191 cur = xmalloc(sizeof(xlist_t) + lenght); 193 cur = xzalloc(sizeof(xlist_t) + length);
192 cur->data = memcpy(cur + 1, s, lenght); 194 cur->data = memcpy(cur + 1, s, length);
193 cur->lenght = lenght; 195 cur->length = length;
194 cur->link = NULL; 196 /*cur->link = NULL;*/
195 if (prev == NULL) { 197 if (prev == NULL) {
196 list_arg = cur; 198 list_arg = cur;
197 } else { 199 } else {
198 prev->link = cur; 200 prev->link = cur;
199 } 201 }
200 prev = cur; 202 prev = cur;
201 line_l += lenght; 203 line_l += length;
202 if (line_l > mc) { 204 if (line_l > mc) {
203 /* stop memory usage :-) */ 205 /* stop memory usage :-) */
204 break; 206 break;
@@ -212,28 +214,30 @@ set:
212} 214}
213#else 215#else
214/* The variant does not support single quotes, double quotes or backslash */ 216/* The variant does not support single quotes, double quotes or backslash */
215static xlist_t *process_stdin(xlist_t * list_arg, 217static xlist_t *process_stdin(xlist_t *list_arg,
216 const char *eof_str, size_t mc, char *buf) 218 const char *eof_str, size_t mc, char *buf)
217{ 219{
218 220
219 int c; /* current char */ 221 int c; /* current char */
220 int eof_str_detected = 0; 222 char eof_str_detected = 0;
221 char *s = NULL; /* start word */ 223 char *s = NULL; /* start word */
222 char *p = NULL; /* pointer to end word */ 224 char *p = NULL; /* pointer to end word */
223 size_t line_l = 0; /* size loaded args line */ 225 size_t line_l = 0; /* size loaded args line */
224 xlist_t *cur; 226 xlist_t *cur;
225 xlist_t *prev; 227 xlist_t *prev;
226 228
227 for (prev = cur = list_arg; cur; cur = cur->link) { 229 cur = list_arg;
228 line_l += cur->lenght; /* previous allocated */ 230 while (1) {
229 if (prev != cur) 231 prev = cur;
230 prev = prev->link; 232 if (!cur) break;
233 line_l += cur->length;
234 cur = cur->link;
231 } 235 }
232 236
233 while (!eof_stdin_detected) { 237 while (!eof_stdin_detected) {
234 c = getchar(); 238 c = getchar();
235 if (c == EOF) { 239 if (c == EOF) {
236 eof_stdin_detected++; 240 eof_stdin_detected = 1;
237 } 241 }
238 if (eof_str_detected) 242 if (eof_str_detected)
239 continue; 243 continue;
@@ -250,22 +254,22 @@ static xlist_t *process_stdin(xlist_t * list_arg,
250 if (c == EOF) { /* word's delimiter or EOF detected */ 254 if (c == EOF) { /* word's delimiter or EOF detected */
251 /* word loaded */ 255 /* word loaded */
252 if (eof_str) { 256 if (eof_str) {
253 eof_str_detected = strcmp(s, eof_str) == 0; 257 eof_str_detected = (strcmp(s, eof_str) == 0);
254 } 258 }
255 if (!eof_str_detected) { 259 if (!eof_str_detected) {
256 size_t lenght = (p - buf); 260 size_t length = (p - buf);
257 261
258 cur = xmalloc(sizeof(xlist_t) + lenght); 262 cur = xzalloc(sizeof(xlist_t) + length);
259 cur->data = memcpy(cur + 1, s, lenght); 263 cur->data = memcpy(cur + 1, s, length);
260 cur->lenght = lenght; 264 cur->length = length;
261 cur->link = NULL; 265 /*cur->link = NULL;*/
262 if (prev == NULL) { 266 if (prev == NULL) {
263 list_arg = cur; 267 list_arg = cur;
264 } else { 268 } else {
265 prev->link = cur; 269 prev->link = cur;
266 } 270 }
267 prev = cur; 271 prev = cur;
268 line_l += lenght; 272 line_l += length;
269 if (line_l > mc) { 273 if (line_l > mc) {
270 /* stop memory usage :-) */ 274 /* stop memory usage :-) */
271 break; 275 break;
@@ -276,39 +280,34 @@ static xlist_t *process_stdin(xlist_t * list_arg,
276 } 280 }
277 return list_arg; 281 return list_arg;
278} 282}
279#endif /* CONFIG_FEATURE_XARGS_SUPPORT_QUOTES */ 283#endif /* FEATURE_XARGS_SUPPORT_QUOTES */
280 284
281 285
282#ifdef CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION 286#if ENABLE_FEATURE_XARGS_SUPPORT_CONFIRMATION
283/* Prompt the user for a response, and 287/* Prompt the user for a response, and
284 if the user responds affirmatively, return true; 288 if the user responds affirmatively, return true;
285 otherwise, return false. Used "/dev/tty", not stdin. */ 289 otherwise, return false. Uses "/dev/tty", not stdin. */
286static int xargs_ask_confirmation(void) 290static int xargs_ask_confirmation(void)
287{ 291{
288 static FILE *tty_stream; 292 FILE *tty_stream;
289 int c, savec; 293 int c, savec;
290 294
291 if (!tty_stream) { 295 tty_stream = xfopen(CURRENT_TTY, "r");
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); 296 fputs(" ?...", stderr);
297 fflush(stderr); 297 fflush(stderr);
298 c = savec = getc(tty_stream); 298 c = savec = getc(tty_stream);
299 while (c != EOF && c != '\n') 299 while (c != EOF && c != '\n')
300 c = getc(tty_stream); 300 c = getc(tty_stream);
301 if (savec == 'y' || savec == 'Y') 301 fclose(tty_stream);
302 return 1; 302 return (savec == 'y' || savec == 'Y');
303 return 0;
304} 303}
305#else 304#else
306# define xargs_ask_confirmation() 1 305# define xargs_ask_confirmation() 1
307#endif /* CONFIG_FEATURE_XARGS_SUPPORT_CONFIRMATION */ 306#endif /* FEATURE_XARGS_SUPPORT_CONFIRMATION */
308 307
309#ifdef CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM 308#if ENABLE_FEATURE_XARGS_SUPPORT_ZERO_TERM
310static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE_UNUSED, 309static xlist_t *process0_stdin(xlist_t *list_arg,
311 size_t mc, char *buf) 310 const char *eof_str ATTRIBUTE_UNUSED, size_t mc, char *buf)
312{ 311{
313 int c; /* current char */ 312 int c; /* current char */
314 char *s = NULL; /* start word */ 313 char *s = NULL; /* start word */
@@ -317,16 +316,18 @@ static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE
317 xlist_t *cur; 316 xlist_t *cur;
318 xlist_t *prev; 317 xlist_t *prev;
319 318
320 for (prev = cur = list_arg; cur; cur = cur->link) { 319 cur = list_arg;
321 line_l += cur->lenght; /* previous allocated */ 320 while (1) {
322 if (prev != cur) 321 prev = cur;
323 prev = prev->link; 322 if (!cur) break;
323 line_l += cur->length;
324 cur = cur->link;
324 } 325 }
325 326
326 while (!eof_stdin_detected) { 327 while (!eof_stdin_detected) {
327 c = getchar(); 328 c = getchar();
328 if (c == EOF) { 329 if (c == EOF) {
329 eof_stdin_detected++; 330 eof_stdin_detected = 1;
330 if (s == NULL) 331 if (s == NULL)
331 break; 332 break;
332 c = 0; 333 c = 0;
@@ -338,19 +339,19 @@ static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE
338 *p++ = c; 339 *p++ = c;
339 if (c == 0) { /* word's delimiter or EOF detected */ 340 if (c == 0) { /* word's delimiter or EOF detected */
340 /* word loaded */ 341 /* word loaded */
341 size_t lenght = (p - buf); 342 size_t length = (p - buf);
342 343
343 cur = xmalloc(sizeof(xlist_t) + lenght); 344 cur = xzalloc(sizeof(xlist_t) + length);
344 cur->data = memcpy(cur + 1, s, lenght); 345 cur->data = memcpy(cur + 1, s, length);
345 cur->lenght = lenght; 346 cur->length = length;
346 cur->link = NULL; 347 /*cur->link = NULL;*/
347 if (prev == NULL) { 348 if (prev == NULL) {
348 list_arg = cur; 349 list_arg = cur;
349 } else { 350 } else {
350 prev->link = cur; 351 prev->link = cur;
351 } 352 }
352 prev = cur; 353 prev = cur;
353 line_l += lenght; 354 line_l += length;
354 if (line_l > mc) { 355 if (line_l > mc) {
355 /* stop memory usage :-) */ 356 /* stop memory usage :-) */
356 break; 357 break;
@@ -360,7 +361,7 @@ static xlist_t *process0_stdin(xlist_t * list_arg, const char *eof_str ATTRIBUTE
360 } 361 }
361 return list_arg; 362 return list_arg;
362} 363}
363#endif /* CONFIG_FEATURE_XARGS_SUPPORT_ZERO_TERM */ 364#endif /* FEATURE_XARGS_SUPPORT_ZERO_TERM */
364 365
365/* Correct regardless of combination of CONFIG_xxx */ 366/* Correct regardless of combination of CONFIG_xxx */
366enum { 367enum {
@@ -413,8 +414,8 @@ int xargs_main(int argc, char **argv)
413 if (opt & OPT_ZEROTERM) 414 if (opt & OPT_ZEROTERM)
414 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin); 415 USE_FEATURE_XARGS_SUPPORT_ZERO_TERM(read_args = process0_stdin);
415 416
416 argc -= optind;
417 argv += optind; 417 argv += optind;
418 argc -= optind;
418 if (!argc) { 419 if (!argc) {
419 /* default behavior is to echo all the filenames */ 420 /* default behavior is to echo all the filenames */
420 *argv = (char*)"echo"; 421 *argv = (char*)"echo";
@@ -458,9 +459,9 @@ int xargs_main(int argc, char **argv)
458 opt |= OPT_NO_EMPTY; 459 opt |= OPT_NO_EMPTY;
459 n = 0; 460 n = 0;
460 n_chars = 0; 461 n_chars = 0;
461#ifdef CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT 462#if ENABLE_FEATURE_XARGS_SUPPORT_TERMOPT
462 for (cur = list; cur;) { 463 for (cur = list; cur;) {
463 n_chars += cur->lenght; 464 n_chars += cur->length;
464 n++; 465 n++;
465 cur = cur->link; 466 cur = cur->link;
466 if (n_chars > n_max_chars || (n == n_max_arg && cur)) { 467 if (n_chars > n_max_chars || (n == n_max_arg && cur)) {
@@ -471,13 +472,13 @@ int xargs_main(int argc, char **argv)
471 } 472 }
472#else 473#else
473 for (cur = list; cur; cur = cur->link) { 474 for (cur = list; cur; cur = cur->link) {
474 n_chars += cur->lenght; 475 n_chars += cur->length;
475 n++; 476 n++;
476 if (n_chars > n_max_chars || n == n_max_arg) { 477 if (n_chars > n_max_chars || n == n_max_arg) {
477 break; 478 break;
478 } 479 }
479 } 480 }
480#endif /* CONFIG_FEATURE_XARGS_SUPPORT_TERMOPT */ 481#endif /* FEATURE_XARGS_SUPPORT_TERMOPT */
481 482
482 /* allocate pointers for execvp: 483 /* allocate pointers for execvp:
483 argc*arg, n*arg from stdin, NULL */ 484 argc*arg, n*arg from stdin, NULL */
@@ -517,7 +518,8 @@ int xargs_main(int argc, char **argv)
517 break; 518 break;
518 } 519 }
519 } 520 }
520 if (ENABLE_FEATURE_CLEAN_UP) free(max_chars); 521 if (ENABLE_FEATURE_CLEAN_UP)
522 free(max_chars);
521 return child_error; 523 return child_error;
522} 524}
523 525