diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-19 21:44:01 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2010-06-19 21:44:01 +0200 |
commit | 4a96617095f40e3cfcf148a8a7d5d83f71079aa1 (patch) | |
tree | 15f72e679e30da4d36c75c18795da848bd960207 | |
parent | e15a6c82dff9f85cfb2ee5ff32b00cbd722c7d92 (diff) | |
download | busybox-w32-4a96617095f40e3cfcf148a8a7d5d83f71079aa1.tar.gz busybox-w32-4a96617095f40e3cfcf148a8a7d5d83f71079aa1.tar.bz2 busybox-w32-4a96617095f40e3cfcf148a8a7d5d83f71079aa1.zip |
xargs: bump default -sNUM up to 32k. Use sysconf() to trim it down if necessary
function old new delta
xargs_main 819 830 +11
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | findutils/xargs.c | 52 |
1 files changed, 27 insertions, 25 deletions
diff --git a/findutils/xargs.c b/findutils/xargs.c index 46a62cbf1..7b9f1fb73 100644 --- a/findutils/xargs.c +++ b/findutils/xargs.c | |||
@@ -59,14 +59,6 @@ | |||
59 | //config: are not special. | 59 | //config: are not special. |
60 | 60 | ||
61 | #include "libbb.h" | 61 | #include "libbb.h" |
62 | /* COMPAT: SYSV version defaults size (and has a max value of) to 470. | ||
63 | We try to make it as large as possible. */ | ||
64 | #if !defined(ARG_MAX) && defined(_SC_ARG_MAX) | ||
65 | # define ARG_MAX sysconf(_SC_ARG_MAX) | ||
66 | #endif | ||
67 | #if !defined(ARG_MAX) | ||
68 | # define ARG_MAX 470 | ||
69 | #endif | ||
70 | 62 | ||
71 | /* This is a NOEXEC applet. Be very careful! */ | 63 | /* This is a NOEXEC applet. Be very careful! */ |
72 | 64 | ||
@@ -440,35 +432,43 @@ int xargs_main(int argc, char **argv) | |||
440 | argc++; | 432 | argc++; |
441 | } | 433 | } |
442 | 434 | ||
443 | /* The Open Group Base Specifications Issue 6: | 435 | /* -s NUM default. fileutils-4.4.2 uses 128k, but I heasitate |
436 | * to use such a big value - first need to change code to use | ||
437 | * growable buffer instead of fixed one. | ||
438 | */ | ||
439 | n_max_chars = 32 * 1024; | ||
440 | /* Make smaller if system does not allow our default value. | ||
441 | * The Open Group Base Specifications Issue 6: | ||
444 | * "The xargs utility shall limit the command line length such that | 442 | * "The xargs utility shall limit the command line length such that |
445 | * when the command line is invoked, the combined argument | 443 | * when the command line is invoked, the combined argument |
446 | * and environment lists (see the exec family of functions | 444 | * and environment lists (see the exec family of functions |
447 | * in the System Interfaces volume of IEEE Std 1003.1-2001) | 445 | * in the System Interfaces volume of IEEE Std 1003.1-2001) |
448 | * shall not exceed {ARG_MAX}-2048 bytes". | 446 | * shall not exceed {ARG_MAX}-2048 bytes". |
449 | */ | 447 | */ |
450 | n_max_chars = ARG_MAX; /* might be calling sysconf(_SC_ARG_MAX) */ | 448 | { |
451 | if (n_max_chars < 4*1024); /* paranoia */ | 449 | long arg_max = 0; |
452 | n_max_chars = 4*1024; | 450 | #if defined _SC_ARG_MAX |
453 | n_max_chars -= 2048; | 451 | arg_max = sysconf(_SC_ARG_MAX) - 2048; |
454 | /* Sanity check for systems with huge ARG_MAX defines (e.g., Suns which | 452 | #elif defined ARG_MAX |
455 | * have it at 1 meg). Things will work fine with a large ARG_MAX | 453 | arg_max = ARG_MAX - 2048; |
456 | * but it will probably hurt the system more than it needs to; | 454 | #endif |
457 | * an array of this size is allocated. | 455 | if (arg_max > 0 && n_max_chars > arg_max) |
458 | */ | 456 | n_max_chars = arg_max; |
459 | if (n_max_chars > 20 * 1024) | 457 | } |
460 | n_max_chars = 20 * 1024; | ||
461 | |||
462 | if (opt & OPT_UPTO_SIZE) { | 458 | if (opt & OPT_UPTO_SIZE) { |
463 | size_t n_chars = 0; | ||
464 | n_max_chars = xatou_range(max_chars, 1, INT_MAX); | 459 | n_max_chars = xatou_range(max_chars, 1, INT_MAX); |
460 | } | ||
461 | /* Account for prepended fixed arguments */ | ||
462 | { | ||
463 | size_t n_chars = 0; | ||
465 | for (i = 0; argv[i]; i++) { | 464 | for (i = 0; argv[i]; i++) { |
466 | n_chars += strlen(argv[i]) + 1; | 465 | n_chars += strlen(argv[i]) + 1; |
467 | } | 466 | } |
468 | n_max_chars -= n_chars; | 467 | n_max_chars -= n_chars; |
469 | if (n_max_chars <= 0) { | 468 | } |
470 | bb_error_msg_and_die("can't fit single argument within argument list size limit"); | 469 | /* Sanity check */ |
471 | } | 470 | if (n_max_chars <= 0) { |
471 | bb_error_msg_and_die("can't fit single argument within argument list size limit"); | ||
472 | } | 472 | } |
473 | 473 | ||
474 | buf = xzalloc(n_max_chars + 1); | 474 | buf = xzalloc(n_max_chars + 1); |
@@ -476,6 +476,8 @@ int xargs_main(int argc, char **argv) | |||
476 | n_max_arg = n_max_chars; | 476 | n_max_arg = n_max_chars; |
477 | if (opt & OPT_UPTO_NUMBER) { | 477 | if (opt & OPT_UPTO_NUMBER) { |
478 | n_max_arg = xatou_range(max_args, 1, INT_MAX); | 478 | n_max_arg = xatou_range(max_args, 1, INT_MAX); |
479 | /* Not necessary, we use growable args[]: */ | ||
480 | /* if (n_max_arg > n_max_chars) n_max_arg = n_max_chars */ | ||
479 | } | 481 | } |
480 | 482 | ||
481 | /* Allocate pointers for execvp */ | 483 | /* Allocate pointers for execvp */ |