~ubuntu-branches/ubuntu/feisty/findutils/feisty

« back to all changes in this revision

Viewing changes to xargs/xargs.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Metzler
  • Date: 2006-08-06 09:53:05 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20060806095305-265uqmwsguy8vfjq
Tags: 4.2.28-1
* New upstream version.
 - includes 01_updatedb-withnew-su.dpatch, drop it.

Show diffs side-by-side

added added

removed removed

Lines of Context:
223
223
 
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;
227
227
 
228
 
volatile static int original_exit_value;
 
228
static volatile int original_exit_value;
229
229
 
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));
276
276
 
277
277
 
278
278
 
279
 
static long
280
 
get_line_max(void)
281
 
{
282
 
  long val;
283
 
#ifdef _SC_LINE_MAX  
284
 
  val = sysconf(_SC_LINE_MAX);
285
 
#else
286
 
  val = -1;
287
 
#endif
288
 
  
289
 
  if (val > 0)
290
 
    return val;
291
 
 
292
 
  /* either _SC_LINE_MAX was not available or 
293
 
   * there is no particular limit.
294
 
   */
295
 
#ifdef LINE_MAX
296
 
  val = LINE_MAX;
297
 
#endif
298
 
 
299
 
  if (val > 0)
300
 
    return val;
301
 
 
302
 
  return 2048L;                 /* a reasonable guess. */
303
 
}
304
 
 
305
279
static char 
306
280
get_char_oct_or_hex_escape(const char *s)
307
281
{
413
387
    }
414
388
}
415
389
 
 
390
static void 
 
391
noop (void)
 
392
{
 
393
  /* does nothing. */
 
394
}
 
395
 
 
396
static void 
 
397
fail_due_to_env_size (void)
 
398
{
 
399
  error (1, 0, _("environment is too large for exec"));
 
400
}
416
401
 
417
402
 
418
403
int
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;
427
 
  long arg_size;
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;
432
416
 
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);
443
 
 
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.
447
 
   */
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.  */
451
 
 
452
 
  bc_init_controlinfo(&bc_ctl);
453
 
  assert(bc_ctl.arg_max == posix_arg_size_max);
454
 
 
455
 
  bc_ctl.exec_callback = xargs_do_exec;
456
 
 
457
427
  
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 
 
433
   * too big.
 
434
   *
 
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()
 
437
   * failed.
460
438
   */
461
 
  arg_size = (128 * 1024) + size_of_environment;
462
 
 
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)
465
440
    {
466
 
      bc_ctl.arg_max = 0;
467
 
      env_too_big = 1;
 
441
      act_on_init_result = fail_due_to_env_size;
468
442
    }
469
443
  else
470
444
    {
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.
 
448
       */
 
449
#if defined(ARG_MAX)
 
450
      assert(bc_ctl.arg_max <= (ARG_MAX-2048));
 
451
#endif
 
452
      assert(bc_ctl.arg_max >= LINE_MAX);
 
453
      
 
454
      bc_ctl.exec_callback = xargs_do_exec;
 
455
      
 
456
      /* Start with a reasonable default size, though this can be
 
457
       * adjusted via the -s option.
 
458
       */
 
459
      bc_use_sensible_arg_max(&bc_ctl);
472
460
    }
473
 
 
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;
479
 
  
480
 
  
481
 
 
482
461
  
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.
552
531
           */
553
532
        case 's':
554
 
          arg_size = parse_num (optarg, 's', 1L, posix_arg_size_max, 0);
555
 
          if (arg_size > posix_arg_size_max)
556
 
            {
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;
559
 
            }
 
533
          {
 
534
            size_t arg_size;
 
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)
 
539
              {
 
540
                error (0, 0,
 
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;
 
545
              }
 
546
            bc_ctl.arg_max = arg_size;
 
547
          }
560
548
          break;
561
549
 
562
550
        case 'S':
598
586
        }
599
587
    }
600
588
 
601
 
  if (env_too_big)
602
 
  {
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. 
606
 
     */
607
 
    error (1, 0, _("environment is too large for exec"));
608
 
  }
 
589
  /* If we had deferred failing due to problems in bc_init_controlinfo(),
 
590
   * do it now.
 
591
   *
 
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. 
 
595
   */
 
596
  act_on_init_result();
 
597
  assert(BC_INIT_OK == bcstatus);
609
598
 
610
599
  if (0 == strcmp (input_file, "-"))
611
600
    {
633
622
      argv = &default_cmd;
634
623
    }
635
624
 
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>.
641
 
   */
642
 
  if (bc_ctl.arg_max > arg_size)
643
 
    {
644
 
      if (show_limits)
645
 
        {
646
 
          fprintf(stderr,
647
 
                  _("Reducing arg_max (%ld) to arg_size (%ld)\n"),
648
 
                  bc_ctl.arg_max, arg_size);
649
 
        }
650
 
      bc_ctl.arg_max = arg_size;
651
 
    }
652
 
 
 
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.
 
631
   */
 
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"
 
635
# endif
 
636
#else
 
637
  /* Without SIZE_MAX (i.e. limits.h) this is probably 
 
638
   * close to the best we can do.
 
639
   */
 
640
  assert(sizeof(size_t) <= sizeof(unsigned long));
 
641
#endif
 
642
  
653
643
  if (show_limits)
654
644
    {
655
645
      fprintf(stderr,
656
 
              _("Your environment variables take up %ld bytes\n"),
657
 
              size_of_environment);
658
 
      fprintf(stderr,
659
 
              _("POSIX lower and upper limits on argument length: %ld, %ld\n"),
660
 
              posix_arg_size_min,
661
 
              posix_arg_size_max);
662
 
      fprintf(stderr,
663
 
              _("Maximum length of command we could actually use: %ld\n"),
664
 
              (posix_arg_size_max - size_of_environment));
665
 
      fprintf(stderr,
666
 
              _("Size of command buffer we are actually using: %ld\n"),
667
 
              bc_ctl.arg_max);
 
646
              _("Your environment variables take up %lu bytes\n"),
 
647
              (unsigned long)bc_size_of_environment());
 
648
      fprintf(stderr,
 
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);
 
652
      fprintf(stderr,
 
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()));
 
656
      fprintf(stderr,
 
657
              _("Size of command buffer we are actually using: %lu\n"),
 
658
              (unsigned long)bc_ctl.arg_max);
668
659
      
669
660
      if (isatty(STDIN_FILENO))
670
661
        {
675
666
                  "not what you wanted to happen, please type the "
676
667
                  "end-of-file keystroke.\n");
677
668
        }
678
 
      
679
669
    }
680
670
  
681
671
  linebuf = (char *) xmalloc (bc_ctl.arg_max + 1);
1223
1213
 
1224
1214
/* Return how much of ARG_MAX is used by the environment.  */
1225
1215
 
1226
 
static long
 
1216
static size_t
1227
1217
env_size (char **envp)
1228
1218
{
1229
 
  long len = 0;
 
1219
  size_t len = 0u;
1230
1220
 
1231
1221
  while (*envp)
1232
1222
    len += strlen (*envp++) + 1;
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"),
1250
1240
           program_name);