aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2017-08-24 11:21:27 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2017-08-24 11:21:27 +0200
commitb270be3fb3a905258e1b8ebb30a17ac175b684f1 (patch)
tree355e11f1b8d1d80b7dad272533ca35f8dbacbdc0
parent9c47c43e07365abe1eda02d69572b9e579b49cec (diff)
downloadbusybox-w32-b270be3fb3a905258e1b8ebb30a17ac175b684f1.tar.gz
busybox-w32-b270be3fb3a905258e1b8ebb30a17ac175b684f1.tar.bz2
busybox-w32-b270be3fb3a905258e1b8ebb30a17ac175b684f1.zip
xargs: code shrink
function old new delta xargs_main 827 787 -40 Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--findutils/xargs.c25
1 files changed, 11 insertions, 14 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c
index c3d37a64d..b4e3db02c 100644
--- a/findutils/xargs.c
+++ b/findutils/xargs.c
@@ -14,7 +14,6 @@
14 * xargs is described in the Single Unix Specification v3 at 14 * xargs is described in the Single Unix Specification v3 at
15 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html 15 * http://www.opengroup.org/onlinepubs/007904975/utilities/xargs.html
16 */ 16 */
17
18//config:config XARGS 17//config:config XARGS
19//config: bool "xargs (6.7 kb)" 18//config: bool "xargs (6.7 kb)"
20//config: default y 19//config: default y
@@ -105,6 +104,7 @@ struct globals {
105#define INIT_G() do { \ 104#define INIT_G() do { \
106 setup_common_bufsiz(); \ 105 setup_common_bufsiz(); \
107 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \ 106 G.eof_str = NULL; /* need to clear by hand because we are NOEXEC applet */ \
107 G.idx = 0; \
108 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \ 108 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.repl_str = "{}";) \
109 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \ 109 IF_FEATURE_XARGS_SUPPORT_REPL_STR(G.eol_ch = '\n';) \
110} while (0) 110} while (0)
@@ -141,7 +141,7 @@ static int xargs_exec(void)
141static void store_param(char *s) 141static void store_param(char *s)
142{ 142{
143 /* Grow by 256 elements at once */ 143 /* Grow by 256 elements at once */
144 if (!(G.idx & 0xff)) { /* G.idx == N*256 */ 144 if (!(G.idx & 0xff)) { /* G.idx == N*256? */
145 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */ 145 /* Enlarge, make G.args[(N+1)*256 - 1] last valid idx */
146 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100)); 146 G.args = xrealloc(G.args, sizeof(G.args[0]) * (G.idx + 0x100));
147 } 147 }
@@ -476,8 +476,9 @@ enum {
476 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::") 476 IF_FEATURE_XARGS_SUPPORT_REPL_STR( "I:i::")
477 477
478int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 478int xargs_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
479int xargs_main(int argc, char **argv) 479int xargs_main(int argc UNUSED_PARAM, char **argv)
480{ 480{
481 int initial_idx;
481 int i; 482 int i;
482 int child_error = 0; 483 int child_error = 0;
483 char *max_args; 484 char *max_args;
@@ -513,11 +514,11 @@ int xargs_main(int argc, char **argv)
513 } 514 }
514 515
515 argv += optind; 516 argv += optind;
516 argc -= optind; 517 //argc -= optind;
517 if (!argv[0]) { 518 if (!argv[0]) {
518 /* default behavior is to echo all the filenames */ 519 /* default behavior is to echo all the filenames */
519 *--argv = (char*)"echo"; 520 *--argv = (char*)"echo";
520 argc++; 521 //argc++;
521 } 522 }
522 523
523 /* 524 /*
@@ -572,7 +573,6 @@ int xargs_main(int argc, char **argv)
572 */ 573 */
573 G.args = NULL; 574 G.args = NULL;
574 G.argv = argv; 575 G.argv = argv;
575 argc = 0;
576 read_args = process_stdin_with_replace; 576 read_args = process_stdin_with_replace;
577 /* Make -I imply -r. GNU findutils seems to do the same: */ 577 /* Make -I imply -r. GNU findutils seems to do the same: */
578 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */ 578 /* (otherwise "echo -n | xargs -I% echo %" would SEGV) */
@@ -580,30 +580,27 @@ int xargs_main(int argc, char **argv)
580 } else 580 } else
581#endif 581#endif
582 { 582 {
583 /* Allocate pointers for execvp. 583 /* Store the command to be executed, part 1.
584 * We can statically allocate (argc + n_max_arg + 1) elements 584 * We can statically allocate (argc + n_max_arg + 1) elements
585 * and do not bother with resizing args[], but on 64-bit machines 585 * and do not bother with resizing args[], but on 64-bit machines
586 * this results in args[] vector which is ~8 times bigger 586 * this results in args[] vector which is ~8 times bigger
587 * than n_max_chars! That is, with n_max_chars == 20k, 587 * than n_max_chars! That is, with n_max_chars == 20k,
588 * args[] will take 160k (!), which will most likely be 588 * args[] will take 160k (!), which will most likely be
589 * almost entirely unused. 589 * almost entirely unused.
590 *
591 * See store_param() for matching 256-step growth logic
592 */ 590 */
593 G.args = xmalloc(sizeof(G.args[0]) * ((argc + 0xff) & ~0xff));
594 /* Store the command to be executed, part 1 */
595 for (i = 0; argv[i]; i++) 591 for (i = 0; argv[i]; i++)
596 G.args[i] = argv[i]; 592 store_param(argv[i]);
597 } 593 }
598 594
595 initial_idx = G.idx;
599 while (1) { 596 while (1) {
600 char *rem; 597 char *rem;
601 598
602 G.idx = argc; 599 G.idx = initial_idx;
603 rem = read_args(n_max_chars, n_max_arg, buf); 600 rem = read_args(n_max_chars, n_max_arg, buf);
604 store_param(NULL); 601 store_param(NULL);
605 602
606 if (!G.args[argc]) { 603 if (!G.args[initial_idx]) { /* not even one ARG was added? */
607 if (*rem != '\0') 604 if (*rem != '\0')
608 bb_error_msg_and_die("argument line too long"); 605 bb_error_msg_and_die("argument line too long");
609 if (opt & OPT_NO_EMPTY) 606 if (opt & OPT_NO_EMPTY)