224
224
/* Exit status; nonzero if any child process exited with a
225
225
status of 1-125. */
226
volatile static int child_error = 0;
226
static volatile int child_error = 0;
228
volatile static int original_exit_value;
228
static volatile int original_exit_value;
230
230
/* If true, print each command on stderr before executing it. */
231
231
static boolean print_command = false; /* Option -t */
271
271
static void wait_for_proc PARAMS ((boolean all));
272
272
static void wait_for_proc_all PARAMS ((void));
273
273
static long parse_num PARAMS ((char *str, int option, long min, long max, int fatal));
274
static long env_size PARAMS ((char **envp));
274
static size_t env_size PARAMS ((char **envp));
275
275
static void usage PARAMS ((FILE * stream));
284
val = sysconf(_SC_LINE_MAX);
292
/* either _SC_LINE_MAX was not available or
293
* there is no particular limit.
302
return 2048L; /* a reasonable guess. */
306
280
get_char_oct_or_hex_escape(const char *s)
422
407
int show_limits = 0; /* --show-limits */
423
408
int always_run_command = 1;
424
409
char *input_file = "-"; /* "-" is stdin */
425
long posix_arg_size_max;
426
long posix_arg_size_min;
428
long size_of_environment = env_size(environ);
410
size_t size_of_environment = env_size(environ);
429
411
char *default_cmd = "/bin/echo";
430
412
int (*read_args) PARAMS ((void)) = read_line;
413
void (*act_on_init_result)(void) = noop;
431
414
int env_too_big = 0;
415
enum BC_INIT_STATUS bcstatus;
433
417
program_name = argv[0];
434
418
original_exit_value = 0;
440
424
textdomain (PACKAGE);
441
425
atexit (close_stdout);
442
426
atexit (wait_for_proc_all);
444
/* IEEE Std 1003.1, 2003 specifies that the combined argument and
445
* environment list shall not exceed {ARG_MAX}-2048 bytes. It also
446
* specifies that it shall be at least LINE_MAX.
448
posix_arg_size_min = get_line_max();
449
posix_arg_size_max = bc_get_arg_max();
450
posix_arg_size_max -= 2048; /* POSIX.2 requires subtracting 2048. */
452
bc_init_controlinfo(&bc_ctl);
453
assert(bc_ctl.arg_max == posix_arg_size_max);
455
bc_ctl.exec_callback = xargs_do_exec;
458
/* Start with a reasonable default size, though this can be
459
* adjusted via the -s option.
428
bcstatus = bc_init_controlinfo(&bc_ctl);
429
/* The bc_init_controlinfo call may have determined that the
430
* environment is too big. In that case, we will fail with
431
* an error message after processing the command-line options,
432
* as "xargs --help" should still work even if the environment is
435
* Some of the argument processing depends on the contents of
436
* bc_ctl, which will be in an undefined state if bc_init_controlinfo()
461
arg_size = (128 * 1024) + size_of_environment;
463
/* Take the size of the environment into account. */
464
if (size_of_environment > posix_arg_size_max)
439
if (BC_INIT_ENV_TOO_BIG == bcstatus)
441
act_on_init_result = fail_due_to_env_size;
471
bc_ctl.arg_max = posix_arg_size_max - size_of_environment;
445
/* IEEE Std 1003.1, 2003 specifies that the combined argument and
446
* environment list shall not exceed {ARG_MAX}-2048 bytes. It also
447
* specifies that it shall be at least LINE_MAX.
450
assert(bc_ctl.arg_max <= (ARG_MAX-2048));
452
assert(bc_ctl.arg_max >= LINE_MAX);
454
bc_ctl.exec_callback = xargs_do_exec;
456
/* Start with a reasonable default size, though this can be
457
* adjusted via the -s option.
459
bc_use_sensible_arg_max(&bc_ctl);
474
/* Check against the upper and lower limits. */
475
if (arg_size > bc_ctl.arg_max)
476
arg_size = bc_ctl.arg_max;
477
if (arg_size < posix_arg_size_min)
478
arg_size = posix_arg_size_min;
483
462
while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
484
463
longopts, (int *) 0)) != -1)
551
530
* cannot support - in that case, the relevant limit is used.
554
arg_size = parse_num (optarg, 's', 1L, posix_arg_size_max, 0);
555
if (arg_size > posix_arg_size_max)
557
error (0, 0, "warning: value %ld for -s option is too large, using %ld instead", arg_size, posix_arg_size_max);
558
arg_size = posix_arg_size_max;
535
act_on_init_result();
536
arg_size = parse_num (optarg, 's', 1L,
537
bc_ctl.posix_arg_size_max, 0);
538
if (arg_size > bc_ctl.posix_arg_size_max)
541
_("warning: value %ld for -s option is too large, "
542
"using %ld instead"),
543
arg_size, bc_ctl.posix_arg_size_max);
544
arg_size = bc_ctl.posix_arg_size_max;
546
bc_ctl.arg_max = arg_size;
603
/* We issue this error message after processing command line
604
* arguments so that it is possible to use "xargs --help" even if
605
* the environment is too large.
607
error (1, 0, _("environment is too large for exec"));
589
/* If we had deferred failing due to problems in bc_init_controlinfo(),
592
* We issue this error message after processing command line
593
* arguments so that it is possible to use "xargs --help" even if
594
* the environment is too large.
596
act_on_init_result();
597
assert(BC_INIT_OK == bcstatus);
610
599
if (0 == strcmp (input_file, "-"))
633
622
argv = &default_cmd;
636
/* Taking into account the size of the environment,
637
* figure out how large a buffer we need to
638
* hold all the arguments. We cannot use ARG_MAX
639
* directly since that may be arbitrarily large.
640
* This is from a patch by Bob Prolux, <bob@proulx.com>.
642
if (bc_ctl.arg_max > arg_size)
647
_("Reducing arg_max (%ld) to arg_size (%ld)\n"),
648
bc_ctl.arg_max, arg_size);
650
bc_ctl.arg_max = arg_size;
625
/* We want to be able to print size_t values as unsigned long, so if
626
* the cast isn't value-preserving, we have a problem. This isn't a
627
* problem in C89, because size_t was known to be no wider than
628
* unsigned long. In C99 this is no longer the case, but there are
629
* special C99 ways to print such values. Unfortunately this
630
* program tries to work on both C89 and C99 systems.
632
#if defined(SIZE_MAX)
633
# if SIZE_MAX > ULONG_MAX
634
# error "I'm not sure how to print size_t values on your system"
637
/* Without SIZE_MAX (i.e. limits.h) this is probably
638
* close to the best we can do.
640
assert(sizeof(size_t) <= sizeof(unsigned long));
656
_("Your environment variables take up %ld bytes\n"),
657
size_of_environment);
659
_("POSIX lower and upper limits on argument length: %ld, %ld\n"),
663
_("Maximum length of command we could actually use: %ld\n"),
664
(posix_arg_size_max - size_of_environment));
666
_("Size of command buffer we are actually using: %ld\n"),
646
_("Your environment variables take up %lu bytes\n"),
647
(unsigned long)bc_size_of_environment());
649
_("POSIX lower and upper limits on argument length: %lu, %lu\n"),
650
(unsigned long)bc_ctl.posix_arg_size_min,
651
(unsigned long)bc_ctl.posix_arg_size_max);
653
_("Maximum length of command we could actually use: %ld\n"),
654
(unsigned long)(bc_ctl.posix_arg_size_max -
655
bc_size_of_environment()));
657
_("Size of command buffer we are actually using: %lu\n"),
658
(unsigned long)bc_ctl.arg_max);
669
660
if (isatty(STDIN_FILENO))
1244
1234
[-I replace-str] [-i[replace-str]] [--replace[=replace-str]]\n\
1245
1235
[-n max-args] [--max-args=max-args]\n\
1246
1236
[-s max-chars] [--max-chars=max-chars]\n\
1247
[-P max-procs] [--max-procs=max-procs]\n\
1237
[-P max-procs] [--max-procs=max-procs] [[--show-limits]\n\
1248
1238
[--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]\n\
1249
1239
[--version] [--help] [command [initial-arguments]]\n"),