summaryrefslogtreecommitdiff
path: root/xargs/xargs.c
blob: 3ad96267ebf187e462db1b320a30a966246187f1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
/* xargs -- build and execute command lines from standard input
   Copyright (C) 1990, 91, 92, 93, 94, 2000, 2003, 2004, 2005, 2006,
   2007, 2008, 2009 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/* Written by Mike Rendell <michael@cs.mun.ca>
   and David MacKenzie <djm@gnu.org>.  
   Modifications by 
        James Youngman
	Dmitry V. Levin
*/

#include <config.h>

# ifndef PARAMS
#  if defined PROTOTYPES || (defined __STDC__ && __STDC__)
#   define PARAMS(Args) Args
#  else
#   define PARAMS(Args) ()
#  endif
# endif

#include <ctype.h>

#if !defined (isascii) || defined (STDC_HEADERS)
#ifdef isascii
#undef isascii
#endif
#define isascii(c) 1
#endif

#ifdef isblank
#define ISBLANK(c) (isascii (c) && isblank (c))
#else
#define ISBLANK(c) ((c) == ' ' || (c) == '\t')
#endif

#define ISSPACE(c) (ISBLANK (c) || (c) == '\n' || (c) == '\r' \
		    || (c) == '\f' || (c) == '\v')

#include <sys/types.h>
#include <stdio.h>
#include <errno.h>
#include <getopt.h>
#include <fcntl.h>

#if defined STDC_HEADERS
#include <assert.h>
#endif

#if defined HAVE_STRING_H || defined STDC_HEADERS
#include <string.h>
#if !defined STDC_HEADERS
#include <memory.h>
#endif
#else
#include <strings.h>
#define memcpy(dest, source, count) (bcopy((source), (dest), (count)))
#endif

#ifndef _POSIX_SOURCE
#include <sys/param.h>
#endif

#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif

#ifndef LONG_MAX
#define LONG_MAX (~(1 << (sizeof (long) * 8 - 1)))
#endif

/* The presence of unistd.h is assumed by gnulib these days, so we 
 * might as well assume it too. 
 */
#include <unistd.h>

#include <signal.h>

#if !defined(SIGCHLD) && defined(SIGCLD)
#define SIGCHLD SIGCLD
#endif

#include "verify.h"
#include "wait.h"
#include "quotearg.h"
#include "findutils-version.h"


#ifdef STDC_HEADERS
#include <stdlib.h>
#else
extern int errno;
#endif

#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
#if ENABLE_NLS
# include <libintl.h>
# define _(Text) gettext (Text)
#else
# define _(Text) Text
#define textdomain(Domain)
#define bindtextdomain(Package, Directory)
#endif
#ifdef gettext_noop
# define N_(String) gettext_noop (String)
#else
/* See locate.c for explanation as to why not use (String) */
# define N_(String) String
#endif

#include "buildcmd.h"


/* Return nonzero if S is the EOF string.  */
#define EOF_STR(s) (eof_str && *eof_str == *s && !strcmp (eof_str, s))

/* Do multibyte processing if multibyte characters are supported,
   unless multibyte sequences are search safe.  Multibyte sequences
   are search safe if searching for a substring using the byte
   comparison function 'strstr' gives no false positives.  All 8-bit
   encodings and the UTF-8 multibyte encoding are search safe, but
   the EUC encodings are not.
   BeOS uses the UTF-8 encoding exclusively, so it is search safe. */
#if defined __BEOS__
# define MULTIBYTE_IS_SEARCH_SAFE 1
#endif
#define DO_MULTIBYTE (HAVE_MBLEN && ! MULTIBYTE_IS_SEARCH_SAFE)

#if DO_MULTIBYTE
# if HAVE_MBRLEN
#  include <wchar.h>
# else
   /* Simulate mbrlen with mblen as best we can.  */
#  define mbstate_t int
#  define mbrlen(s, n, ps) mblen (s, n)
# endif
#endif

/* Not char because of type promotion; NeXT gcc can't handle it.  */
typedef int boolean;
#define		true    1
#define		false	0

#if __STDC__
#define VOID void
#else
#define VOID char
#endif

#include <xalloc.h>
#include "closein.h"
#include "gnulib-version.h"

void error PARAMS ((int status, int errnum, char *message,...));

extern char *version_string;

/* The name this program was run with.  */
char *program_name;

static FILE *input_stream;

/* Buffer for reading arguments from input.  */
static char *linebuf;

static int keep_stdin = 0;

/* Line number in stdin since the last command was executed.  */
static int lineno = 0;

static struct buildcmd_state bc_state;
static struct buildcmd_control bc_ctl;

/* Did we already complain about NUL characters in the input? */
static int nullwarning_given = 0;


/* If nonzero, when this string is read on stdin it is treated as
   end of file.
   IEEE Std 1003.1, 2004 Edition allows this to be NULL.
   In findutils releases up to and including 4.2.8, this was "_".
*/
static char *eof_str = NULL;

/* Number of chars in the initial args.  */
/* static int initial_argv_chars = 0; */

/* true when building up initial arguments in `cmd_argv'.  */
static boolean initial_args = true;

/* If nonzero, the maximum number of child processes that can be running
   at once.  */
static unsigned long int proc_max = 1uL;

/* Did we fork a child yet? */
static boolean procs_executed = false;

/* The number of elements in `pids'.  */
static unsigned long int procs_executing = 0uL;

/* List of child processes currently executing.  */
static pid_t *pids = NULL;

/* The number of allocated elements in `pids'. */
static size_t pids_alloc = 0u;

/* Exit status; nonzero if any child process exited with a
   status of 1-125.  */
static volatile int child_error = 0;

static volatile int original_exit_value;

/* If true, print each command on stderr before executing it.  */
static boolean print_command = false; /* Option -t */

/* If true, query the user before executing each command, and only
   execute the command if the user responds affirmatively.  */
static boolean query_before_executing = false;

/* The delimiter for input arguments.   This is only consulted if the 
 * -0 or -d option had been given.
 */
static char input_delimiter = '\0';


static struct option const longopts[] =
{
  {"null", no_argument, NULL, '0'},
  {"arg-file", required_argument, NULL, 'a'},
  {"delimiter", required_argument, NULL, 'd'},
  {"eof", optional_argument, NULL, 'e'},
  {"replace", optional_argument, NULL, 'I'},
  {"max-lines", optional_argument, NULL, 'l'},
  {"max-args", required_argument, NULL, 'n'},
  {"interactive", no_argument, NULL, 'p'},
  {"no-run-if-empty", no_argument, NULL, 'r'},
  {"max-chars", required_argument, NULL, 's'},
  {"verbose", no_argument, NULL, 't'},
  {"show-limits", no_argument, NULL, 'S'},
  {"exit", no_argument, NULL, 'x'},
  {"max-procs", required_argument, NULL, 'P'},
  {"version", no_argument, NULL, 'v'},
  {"help", no_argument, NULL, 'h'},
  {NULL, no_argument, NULL, 0}
};

static int read_line PARAMS ((void));
static int read_string PARAMS ((void));
static boolean print_args PARAMS ((boolean ask));
/* static void do_exec PARAMS ((void)); */
static int xargs_do_exec (const struct buildcmd_control *cl, struct buildcmd_state *state);
static void exec_if_possible PARAMS ((void));
static void add_proc PARAMS ((pid_t pid));
static void wait_for_proc PARAMS ((boolean all, unsigned int minreap));
static void wait_for_proc_all PARAMS ((void));
static long parse_num PARAMS ((char *str, int option, long min, long max, int fatal));
static void usage PARAMS ((FILE * stream));



static char 
get_char_oct_or_hex_escape(const char *s)
{
  const char * p;
  int base = 8;
  unsigned long val;
  char *endp;

  assert ('\\' == s[0]);
  
  if ('x' == s[1])
    {
      /* hex */
      p = s+2;
      base = 16;
    }
  else if (isdigit ((unsigned char) s[1]))
    {
      /* octal */
      p = s+1;
      base = 8;
    }
  else
    {
      p = NULL;			/* Silence compiler warning. */
      error(1, 0,
	    _("Invalid escape sequence %s in input delimiter specification."),
	    s);
    }
  errno = 0;
  endp = (char*)p;
  val = strtoul(p, &endp, base);
  
  /* This if condition is carefully constructed to do 
   * the right thing if UCHAR_MAX has the same 
   * value as ULONG_MAX.   IF UCHAR_MAX==ULONG_MAX,
   * then val can never be greater than UCHAR_MAX.
   */
  if ((ULONG_MAX == val && ERANGE == errno)
      || (val > UCHAR_MAX))
    {
      if (16 == base)
	{
	  error(1, 0,
		_("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lx."),
		s, (unsigned long)UCHAR_MAX);
	}
      else
	{
	  error(1, 0,
		_("Invalid escape sequence %s in input delimiter specification; character values must not exceed %lo."),
		s, (unsigned long)UCHAR_MAX);
	}
    }
  
  /* check for trailing garbage */
  if (0 != *endp)
    {
      error(1, 0,
	    _("Invalid escape sequence %s in input delimiter specification; trailing characters %s not recognised."),
	    s, endp);
    }
  
  return (char) val;
}


static char 
get_input_delimiter(const char *s)
{
  if (1 == strlen(s))
    {
      return s[0];
    }
  else
    {
      if ('\\' == s[0])
	{
	  /* an escape code */
	  switch (s[1])
	    {
	    case 'a':
	      return '\a';
	    case 'b':
	      return '\b';
	    case 'f':
	      return '\f';
	    case 'n':
	      return '\n';
	    case 'r':
	      return '\r';
	    case 't':
	      return'\t';
	    case 'v':
	      return '\v';
	    case '\\':
	      return '\\';
	    default:
	      return get_char_oct_or_hex_escape(s);
	    }
	}
      else
	{
	  error(1, 0,
		_("Invalid input delimiter specification %s: the delimiter must be either a single character or an escape sequence starting with \\."),
		s);
	  /*NOTREACHED*/
	  return 0;
	}
    }
}

static void 
noop (void)
{
  /* does nothing. */
}

static void 
fail_due_to_env_size (void)
{
  error (1, 0, _("environment is too large for exec"));
}


int
main (int argc, char **argv)
{
  int optc;
  int show_limits = 0;			/* --show-limits */
  int always_run_command = 1;
  char *input_file = "-"; /* "-" is stdin */
  char *default_cmd = "/bin/echo";
  int (*read_args) PARAMS ((void)) = read_line;
  void (*act_on_init_result)(void) = noop;
  enum BC_INIT_STATUS bcstatus;
  enum { XARGS_POSIX_HEADROOM = 2048u };
  
  program_name = argv[0];
  original_exit_value = 0;
  
#ifdef HAVE_SETLOCALE
  setlocale (LC_ALL, "");
#endif
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);
  atexit (close_stdin);
  atexit (wait_for_proc_all);

  /* xargs is required by POSIX to allow 2048 bytes of headroom 
   * for extra environment variables (that perhaps the utliity might 
   * want to set before execing something else).
   */
  bcstatus = bc_init_controlinfo(&bc_ctl, XARGS_POSIX_HEADROOM);
  
  /* The bc_init_controlinfo call may have determined that the 
   * environment is too big.  In that case, we will fail with 
   * an error message after processing the command-line options,
   * as "xargs --help" should still work even if the environment is 
   * too big.
   *
   * Some of the argument processing depends on the contents of 
   * bc_ctl, which will be in an undefined state if bc_init_controlinfo()
   * failed.
   */
  if (BC_INIT_ENV_TOO_BIG == bcstatus)
    {
      act_on_init_result = fail_due_to_env_size;
    }
  else if (BC_INIT_CANNOT_ACCOMODATE_HEADROOM == bcstatus)
    {
      /* All POSIX systems are required to support ARG_MAX of at least
       * 4096.  For everything to work the total of (command line +
       * headroom + environment) must fit into this.  POSIX requires
       * that we use a headroom of 2048 bytes.  The user is in control
       * of the size of the environment.  
       *
       * In general if bc_init_controlinfo() returns
       * BC_INIT_CANNOT_ACCOMODATE_HEADROOM, its caller can try again
       * with a smaller headroom.  However, in the case of xargs, this
       * would not be POSIX-compliant.
       */
      act_on_init_result = fail_due_to_env_size;
    }
  else
    {
      /* IEEE Std 1003.1, 2003 specifies that the combined argument and 
       * environment list shall not exceed {ARG_MAX}-2048 bytes.  It also 
       * specifies that it shall be at least LINE_MAX.
       */
#ifdef _SC_ARG_MAX  
      long val = sysconf(_SC_ARG_MAX);
      if (val > 0)
	{
	  /* Note that val can in fact be greater than ARG_MAX
	   * and bc_ctl.arg_max can also be greater than ARG_MAX.
	   */
	  assert (bc_ctl.arg_max <= (val-XARGS_POSIX_HEADROOM));
	}
      else
	{
# if defined ARG_MAX
	  assert (bc_ctl.arg_max <= (ARG_MAX-XARGS_POSIX_HEADROOM));
# endif
	}
#else
      /* No _SC_ARG_MAX */
      assert (bc_ctl.arg_max <= (ARG_MAX-XARGS_POSIX_HEADROOM));
#endif


#ifdef LINE_MAX
      /* This assertion ensures that this xargs implementation
       * conforms to the POSIX requirement that the default command
       * line length shall be at least LINE_MAX.
       */
      assert (bc_ctl.arg_max >= LINE_MAX);
#endif
      
      bc_ctl.exec_callback = xargs_do_exec;
      
      /* Start with a reasonable default size, though this can be
       * adjusted via the -s option.
       */
      bc_use_sensible_arg_max(&bc_ctl);
    }
  
  while ((optc = getopt_long (argc, argv, "+0a:E:e::i::I:l::L:n:prs:txP:d:",
			      longopts, (int *) 0)) != -1)
    {
      switch (optc)
	{
	case '0':
	  read_args = read_string;
	  input_delimiter = '\0';
	  break;

	case 'd':
	  read_args = read_string;
	  input_delimiter = get_input_delimiter(optarg);
	  break;

	case 'E':		/* POSIX */
	case 'e':		/* deprecated */
	  if (optarg && (strlen(optarg) > 0))
	    eof_str = optarg;
	  else
	    eof_str = 0;
	  break;

	case 'h':
	  usage (stdout);
	  return 0;

	case 'I':		/* POSIX */
	case 'i':		/* deprecated */
	  if (optarg)
	    bc_ctl.replace_pat = optarg;
	  else
	    bc_ctl.replace_pat = "{}";
	  /* -i excludes -n -l.  */
	  bc_ctl.args_per_exec = 0;
	  bc_ctl.lines_per_exec = 0;
	  break;

	case 'L':		/* POSIX */
	  bc_ctl.lines_per_exec = parse_num (optarg, 'L', 1L, -1L, 1);
	  /* -L excludes -i -n.  */
	  bc_ctl.args_per_exec = 0;
	  bc_ctl.replace_pat = NULL;
	  break;

	case 'l':		/* deprecated */
	  if (optarg)
	    bc_ctl.lines_per_exec = parse_num (optarg, 'l', 1L, -1L, 1);
	  else
	    bc_ctl.lines_per_exec = 1;
	  /* -l excludes -i -n.  */
	  bc_ctl.args_per_exec = 0;
	  bc_ctl.replace_pat = NULL;
	  break;

	case 'n':
	  bc_ctl.args_per_exec = parse_num (optarg, 'n', 1L, -1L, 1);
	  /* -n excludes -i -l.  */
	  bc_ctl.lines_per_exec = 0;
	  if (bc_ctl.args_per_exec == 1 && bc_ctl.replace_pat)
	    /* ignore -n1 in '-i -n1' */
	    bc_ctl.args_per_exec = 0;
	  else
	    bc_ctl.replace_pat = NULL;
	  break;

	  /* The POSIX standard specifies that it is not an error 
	   * for the -s option to specify a size that the implementation 
	   * cannot support - in that case, the relevant limit is used.
	   */
	case 's':
	  {
	    size_t arg_size;
	    act_on_init_result();
	    arg_size = parse_num (optarg, 's', 1L,
				  bc_ctl.posix_arg_size_max, 0);
	    if (arg_size > bc_ctl.posix_arg_size_max)
	      {
		error (0, 0,
		       _("Warning: value %ld for -s option is too large, "
			 "using %ld instead"),
		       arg_size, bc_ctl.posix_arg_size_max);
		arg_size = bc_ctl.posix_arg_size_max;
	      }
	    bc_ctl.arg_max = arg_size;
	  }
	  break;

	case 'S':
	  show_limits = true;
	  break;

	case 't':
	  print_command = true;
	  break;

	case 'x':
	  bc_ctl.exit_if_size_exceeded = true;
	  break;

	case 'p':
	  query_before_executing = true;
	  print_command = true;
	  break;

	case 'r':
	  always_run_command = 0;
	  break;

	case 'P':
	  /* Allow only up to LONG_MAX child processes. */
	  proc_max = parse_num (optarg, 'P', 0L, LONG_MAX, 1);
	  break;

        case 'a':
          input_file = optarg;
          break;

	case 'v':
	  display_findutils_version("xargs");
	  return 0;

	default:
	  usage (stderr);
	  return 1;
	}
    }

  /* If we had deferred failing due to problems in bc_init_controlinfo(),
   * do it now.
   *
   * We issue this error message after processing command line 
   * arguments so that it is possible to use "xargs --help" even if
   * the environment is too large. 
   */
  act_on_init_result();
  assert (BC_INIT_OK == bcstatus);

  if (0 == strcmp (input_file, "-"))
    {
      input_stream = stdin;
    }
  else
    {
      keep_stdin = 1;		/* see prep_child_for_exec() */
      input_stream = fopen (input_file, "r");
      if (NULL == input_stream)
	{
	  error (1, errno,
		 _("Cannot open input file %s"),
		 quotearg_n_style(0, locale_quoting_style, input_file));
	}
    }

  if (bc_ctl.replace_pat || bc_ctl.lines_per_exec)
    bc_ctl.exit_if_size_exceeded = true;

  if (optind == argc)
    {
      optind = 0;
      argc = 1;
      argv = &default_cmd;
    }

  /* We want to be able to print size_t values as unsigned long, so if
   * the cast isn't value-preserving, we have a problem.  This isn't a
   * problem in C89, because size_t was known to be no wider than
   * unsigned long.  In C99 this is no longer the case, but there are
   * special C99 ways to print such values.  Unfortunately this
   * program tries to work on both C89 and C99 systems.
   */
#if defined SIZE_MAX
# if SIZE_MAX > ULONG_MAX
# error "I'm not sure how to print size_t values on your system"
# endif
#else
  /* Without SIZE_MAX (i.e. limits.h) this is probably 
   * close to the best we can do.
   */
  verify_true (sizeof(size_t) <= sizeof(unsigned long));
#endif
  
  if (show_limits)
    {
      fprintf(stderr,
	      _("Your environment variables take up %lu bytes\n"),
	      (unsigned long)bc_size_of_environment());
      fprintf(stderr,
	      _("POSIX upper limit on argument length (this system): %lu\n"),
	      (unsigned long)bc_ctl.posix_arg_size_max);
      fprintf(stderr,
	      _("POSIX smallest allowable upper limit on argument length (all systems): %lu\n"),
	      (unsigned long)bc_ctl.posix_arg_size_min);
      fprintf(stderr,
	      _("Maximum length of command we could actually use: %ld\n"),
	      (unsigned long)(bc_ctl.posix_arg_size_max -
			      bc_size_of_environment()));
      fprintf(stderr,
	      _("Size of command buffer we are actually using: %lu\n"),
	      (unsigned long)bc_ctl.arg_max);
      
      if (isatty(STDIN_FILENO))
	{
	  fprintf(stderr,
		  _("\n"
		    "Execution of xargs will continue now, and it will "
		    "try to read its input and run commands; if this is "
		    "not what you wanted to happen, please type the "
		    "end-of-file keystroke.\n"));
	  if (always_run_command)
	    {
	      fprintf(stderr,
		      _("Warning: %s will be run at least once.  "
			"If you do not want that to happen, then press "
			"the interrupt keystroke.\n"),
		      argv[optind]);
	    }
	}
    }
  
  linebuf = xmalloc (bc_ctl.arg_max + 1);
  bc_state.argbuf = xmalloc (bc_ctl.arg_max + 1);

  /* Make sure to listen for the kids.  */
  signal (SIGCHLD, SIG_DFL);

  if (!bc_ctl.replace_pat)
    {
      for (; optind < argc; optind++)
	bc_push_arg (&bc_ctl, &bc_state,
		     argv[optind], strlen (argv[optind]) + 1,
		     NULL, 0,
		     initial_args);
      initial_args = false;
      bc_ctl.initial_argc = bc_state.cmd_argc;
      bc_state.cmd_initial_argv_chars = bc_state.cmd_argv_chars;

      while ((*read_args) () != -1)
	if (bc_ctl.lines_per_exec && lineno >= bc_ctl.lines_per_exec)
	  {
	    xargs_do_exec (&bc_ctl, &bc_state);
	    lineno = 0;
	  }

      /* SYSV xargs seems to do at least one exec, even if the
         input is empty.  */
      if (bc_state.cmd_argc != bc_ctl.initial_argc
	  || (always_run_command && !procs_executed))
	xargs_do_exec (&bc_ctl, &bc_state);

    }
  else
    {
      int i;
      size_t len;
      size_t *arglen = xmalloc (sizeof (size_t) * argc);

      for (i = optind; i < argc; i++)
	arglen[i] = strlen(argv[i]);
      bc_ctl.rplen = strlen (bc_ctl.replace_pat);
      while ((len = (*read_args) ()) != -1)
	{
	  /* Don't do insert on the command name.  */
	  bc_clear_args(&bc_ctl, &bc_state);
	  bc_state.cmd_argv_chars = 0; /* begin at start of buffer */
	  
	  bc_push_arg (&bc_ctl, &bc_state,
		       argv[optind], arglen[optind] + 1,
		       NULL, 0,
		       initial_args);
	  len--;
	  initial_args = false;
	  
	  for (i = optind + 1; i < argc; i++)
	    bc_do_insert (&bc_ctl, &bc_state,
			  argv[i], arglen[i],
			  NULL, 0,
			  linebuf, len,
			  initial_args);
	  xargs_do_exec (&bc_ctl, &bc_state);
	}
    }

  original_exit_value = child_error;
  return child_error;
}


/* Read a line of arguments from the input and add them to the list of
   arguments to pass to the command.  Ignore blank lines and initial blanks.
   Single and double quotes and backslashes quote metacharacters and blanks
   as they do in the shell.
   Return -1 if eof (either physical or logical) is reached,
   otherwise the length of the last string read (including the null).  */

static int
read_line (void)
{
/* States for read_line. */
  enum read_line_state 
    {
      NORM = 0,
      SPACE = 1,
      QUOTE = 2,
      BACKSLASH = 3
    };
  static boolean eof = false;
  /* Start out in mode SPACE to always strip leading spaces (even with -i).  */
  enum read_line_state state = SPACE; /* The type of character we last read.  */
  int prevc;			/* The previous value of c.  */
  int quotc = 0;		/* The last quote character read.  */
  int c = EOF;
  boolean first = true;		/* true if reading first arg on line.  */
  boolean seen_arg = false;      /* true if we have seen any arg (or part of one) yet */
  int len;
  char *p = linebuf;
  /* Including the NUL, the args must not grow past this point.  */
  char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;
  
  if (eof)
    return -1;
  while (1)
    {
      prevc = c;
      c = getc (input_stream);
      
      if (c == EOF)
	{
	  /* COMPAT: SYSV seems to ignore stuff on a line that
	     ends without a \n; we don't.  */
	  eof = true;
	  if (p == linebuf)
	    return -1;
	  *p++ = '\0';
	  len = p - linebuf;
	  if (state == QUOTE)
	    {
	      exec_if_possible ();
	      error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
		     quotc == '"' ? _("double") : _("single"));
	    }
	  if (first && EOF_STR (linebuf))
	    return -1;
	  if (!bc_ctl.replace_pat)
	    bc_push_arg (&bc_ctl, &bc_state,
			 linebuf, len,
			 NULL, 0,
			 initial_args);
	  return len;
	}
      switch (state)
	{
	case SPACE:
	  if (ISSPACE (c))
	    continue;
	  state = NORM;
	  /* aaahhhh....  */

	case NORM:
	  if (c == '\n')
	    {
	      if (!ISBLANK (prevc))
		lineno++;	/* For -l.  */
	      if (p == linebuf)
		{
		  if (seen_arg)
		    {
		      /* An empty argument, add it to the list as normal. */
		    }
		  else
		    {
		      /* Blank line.  */
		      state = SPACE;
		      continue;
		    }
		}
	      *p++ = '\0';
	      len = p - linebuf;
	      if (EOF_STR (linebuf))
		{
		  eof = true;
		  return first ? -1 : len;
		}
	      if (!bc_ctl.replace_pat)
		bc_push_arg (&bc_ctl, &bc_state,
			     linebuf, len,
			     NULL, 0,
			     initial_args);
	      return len;
	    }
	  seen_arg = true;

	  /* POSIX: In the POSIX locale, the separators are <SPC> and
	   * <TAB>, but not <FF> or <VT>. 
	   */
	  if (!bc_ctl.replace_pat && ISBLANK (c))
	    {
	      *p++ = '\0';
	      len = p - linebuf;
	      if (EOF_STR (linebuf))
		{
		  eof = true;
		  return first ? -1 : len;
		}
	      bc_push_arg (&bc_ctl, &bc_state,
			   linebuf, len,
			   NULL, 0,
			   initial_args);
	      p = linebuf;
	      state = SPACE;
	      first = false;
	      continue;
	    }
	  switch (c)
	    {
	    case '\\':
	      state = BACKSLASH;
	      continue;

	    case '\'':
	    case '"':
	      state = QUOTE;
	      quotc = c;
	      continue;
	    }
	  break;

	case QUOTE:
	  if (c == '\n')
	    {
	      exec_if_possible ();
	      error (1, 0, _("unmatched %s quote; by default quotes are special to xargs unless you use the -0 option"),
		     quotc == '"' ? _("double") : _("single"));
	    }
	  if (c == quotc)
	    {
	      state = NORM;
	      seen_arg = true; /* Makes a difference for e.g. just '' or "" as the first arg on a line */
	      continue;
	    }
	  break;

	case BACKSLASH:
	  state = NORM;
	  break;
	}
      
      if ( (0 == c) && !nullwarning_given )
	{
	  /* This is just a warning message.  We only issue it once. */
	  error (0, 0,
		 _("Warning: a NUL character occurred in the input.  "
		   "It cannot be passed through in the argument list.  "
		   "Did you mean to use the --null option?"));
	  nullwarning_given = 1;
	}

#if 1
      if (p >= endbuf)
        {
	  exec_if_possible ();
	  error (1, 0, _("argument line too long"));
	}
      *p++ = c;
#else
      append_char_to_buf(&linebuf, &endbuf, &p, c);
#endif
    }
}

/* Read a null-terminated string from the input and add it to the list of
   arguments to pass to the command.
   Return -1 if eof (either physical or logical) is reached,
   otherwise the length of the string read (including the null).  */

static int
read_string (void)
{
  static boolean eof = false;
  int len;
  char *p = linebuf;
  /* Including the NUL, the args must not grow past this point.  */
  char *endbuf = linebuf + bc_ctl.arg_max - bc_state.cmd_initial_argv_chars - 1;

  if (eof)
    return -1;
  while (1)
    {
      int c = getc (input_stream);
      if (c == EOF)
	{
	  eof = true;
	  if (p == linebuf)
	    return -1;
	  *p++ = '\0';
	  len = p - linebuf;
	  if (!bc_ctl.replace_pat)
	    bc_push_arg (&bc_ctl, &bc_state,
			 linebuf, len,
			 NULL, 0,
			 initial_args);
	  return len;
	}
      if (c == input_delimiter)
	{
	  lineno++;		/* For -l.  */
	  *p++ = '\0';
	  len = p - linebuf;
	  if (!bc_ctl.replace_pat)
	    bc_push_arg (&bc_ctl, &bc_state,
			 linebuf, len,
			 NULL, 0,
			 initial_args);
	  return len;
	}
      if (p >= endbuf)
        {
	  exec_if_possible ();
	  error (1, 0, _("argument line too long"));
	}
      *p++ = c;
    }
}

/* Print the arguments of the command to execute.
   If ASK is nonzero, prompt the user for a response, and
   if the user responds affirmatively, return true;
   otherwise, return false.  */

static boolean
print_args (boolean ask)
{
  int i;

  for (i = 0; i < bc_state.cmd_argc - 1; i++)
    fprintf (stderr, "%s ", bc_state.cmd_argv[i]);
  if (ask)
    {
      static FILE *tty_stream;
      int c, savec;

      if (!tty_stream)
	{
	  tty_stream = fopen ("/dev/tty", "r");
	  if (!tty_stream)
	    error (1, errno, "/dev/tty");
	}
      fputs ("?...", stderr);
      fflush (stderr);
      c = savec = getc (tty_stream);
      while (c != EOF && c != '\n')
	c = getc (tty_stream);
      if (savec == 'y' || savec == 'Y')
	return true;
    }
  else
    putc ('\n', stderr);

  return false;
}


/* Close stdin and attach /dev/null to it.
 * This resolves Savannah bug #3992.
 */
static void
prep_child_for_exec (void)
{
  if (!keep_stdin)
    {
      const char inputfile[] = "/dev/null";
      /* fprintf(stderr, "attaching stdin to /dev/null\n"); */
      
      close(0);
      if (open(inputfile, O_RDONLY) < 0)
	{
	  /* This is not entirely fatal, since 
	   * executing the child with a closed
	   * stdin is almost as good as executing it
	   * with its stdin attached to /dev/null.
	   */
	  error (0, errno, "%s", quotearg_n_style(0, locale_quoting_style, inputfile));
	}
    }
}


/* Execute the command that has been built in `cmd_argv'.  This may involve
   waiting for processes that were previously executed.  */

static int
xargs_do_exec (const struct buildcmd_control *ctl, struct buildcmd_state *state)
{
  pid_t child;

  (void) ctl;
  (void) state;
  
  bc_push_arg (&bc_ctl, &bc_state,
	       (char *) NULL, 0,
	       NULL, 0,
	       false); /* Null terminate the arg list.  */
  
  if (!query_before_executing || print_args (true))
    {
      if (proc_max)
	{
	  if (procs_executing >= proc_max)
	    wait_for_proc (false, proc_max - procs_executing + 1);
	  assert (procs_executing < proc_max);
	}
      if (!query_before_executing && print_command)
	print_args (false);

      /* Before forking, reap any already-exited child. We do this so
	 that we don't leave unreaped children around while we build a
	 new command line.  For example this command will spend most
	 of its time waiting for sufficient arguments to launch
	 another command line:

	 seq 1 1000 | fmt | while read x ; do echo $x; sleep 1 ; done | 
	 ./xargs -P 200 -n 20  sh -c 'echo "$@"; sleep $((1 + $RANDOM % 5))' sleeper
      */
      wait_for_proc (false, 0u);

      /* If we run out of processes, wait for a child to return and
         try again.  */
      while ((child = fork ()) < 0 && errno == EAGAIN && procs_executing)
	wait_for_proc (false, 1u);

      switch (child)
	{
	case -1:
	  error (1, errno, _("cannot fork"));

	case 0:		/* Child.  */
	  prep_child_for_exec();
	  execvp (bc_state.cmd_argv[0], bc_state.cmd_argv);
	  error (0, errno, "%s", bc_state.cmd_argv[0]);
	  _exit (errno == ENOENT ? 127 : 126);
	  /*NOTREACHED*/
	}
      add_proc (child);
    }

  bc_clear_args(&bc_ctl, &bc_state);
  return 1;			/* Success */
}

/* Execute the command if possible.  */

static void
exec_if_possible (void)
{
  if (bc_ctl.replace_pat || initial_args ||
      bc_state.cmd_argc == bc_ctl.initial_argc || bc_ctl.exit_if_size_exceeded)
    return;
  xargs_do_exec (&bc_ctl, &bc_state);
}

/* Add the process with id PID to the list of processes that have
   been executed.  */

static void
add_proc (pid_t pid)
{
  unsigned int i, j;

  /* Find an empty slot.  */
  for (i = 0; i < pids_alloc && pids[i]; i++)
    ;

  /* Extend the array if we failed. */
  if (i == pids_alloc)
    {
      pids = x2nrealloc (pids, &pids_alloc, sizeof *pids);

      /* Zero out the new slots. */
      for (j=i; j<pids_alloc; ++j)
	pids[j] = (pid_t)0;
    }
  /* Verify that we are not destroying the record of some existing child. */
  assert (0 == pids[i]);

  /* Remember the child. */
  pids[i] = pid;
  procs_executing++;
  procs_executed = true;
}


/* If ALL is true, wait for all child processes to finish;
   otherwise, wait for at least MINREAP child processes to finish.
   Remove the processes that finish from the list of executing processes.  */

static void
wait_for_proc (boolean all, unsigned int minreap)
{
  unsigned int reaped = 0;

  while (procs_executing)
    {
      unsigned int i;
      int status;
      pid_t pid;
      int wflags = 0;

      if (!all)
	{
	  if (reaped >= minreap)
	    {
	      /* We already reaped enough children.  To save system
	       * resources, reap any dead children anyway, but do not
	       * wait for any currently executing children to exit.
	       
	       */
	      wflags = WNOHANG;
	    }
	}

      do
	{
	  /* Wait for any child.   We used to use wait() here, but it's 
	   * unlikely that that offers any portability advantage over 
	   * wait these days.
	   */
	  while ((pid = waitpid (-1, &status, wflags)) == (pid_t) -1)
	    {
	      if (errno != EINTR)
		error (1, errno, _("error waiting for child process"));
	    }
	  
	  /* Find the entry in `pids' for the child process
	     that exited.  */
	  if (pid)
	    {
	      for (i = 0; i < pids_alloc && pid != pids[i]; i++)
		;
	    }
	}
      while (pid && i == pids_alloc);	/* A child died that we didn't start? */

      if (!pid)
	{
	  if (!(wflags & WNOHANG))
	    {
	      /* Nothing remained to be reaped.  This should not
	       * happen, because procs_executing should contain the
	       * number of child processes still executing, so the
	       * loop should have terminated.
	       */
	      error (0, 0, _("Warning: Lost track of %d child processes"),
		     procs_executing);
	    }
	  else
	    {
	      /* Children are (probably) executing but are not ready
	       * to be reaped at the moment.
	       */
	    }
	  break;
	}
      
      /* Remove the child from the list.  */
      pids[i] = 0;
      procs_executing--;
      reaped++;

      if (WEXITSTATUS (status) == 126 || WEXITSTATUS (status) == 127)
	exit (WEXITSTATUS (status));	/* Can't find or run the command.  */
      if (WEXITSTATUS (status) == 255)
	error (124, 0, _("%s: exited with status 255; aborting"), bc_state.cmd_argv[0]);
      if (WIFSTOPPED (status))
	error (125, 0, _("%s: stopped by signal %d"), bc_state.cmd_argv[0], WSTOPSIG (status));
      if (WIFSIGNALED (status))
	error (125, 0, _("%s: terminated by signal %d"), bc_state.cmd_argv[0], WTERMSIG (status));
      if (WEXITSTATUS (status) != 0)
	child_error = 123;
    }
}

/* Wait for all child processes to finish.  */

static void
wait_for_proc_all (void)
{
  static boolean waiting = false;
  
  if (waiting)
    return;

  waiting = true;
  wait_for_proc (true, 0u);
  waiting = false;
  
  if (original_exit_value != child_error)
    {
      /* wait_for_proc() changed the value of child_error().  This
       * function is registered via atexit(), and so may have been
       * called from exit().  We now know that the original value
       * passed to exit() is no longer the exit status we require.
       * The POSIX standard states that the behaviour if exit() is
       * called more than once is undefined.  Therefore we now have to
       * exit with _exit() instead of exit().
       */
      _exit(child_error);
    }
  
}

/* Return the value of the number represented in STR.
   OPTION is the command line option to which STR is the argument.
   If the value does not fall within the boundaries MIN and MAX,
   Print an error message mentioning OPTION.  If FATAL is true, 
   we also exit. */

static long
parse_num (char *str, int option, long int min, long int max, int fatal)
{
  char *eptr;
  long val;

  val = strtol (str, &eptr, 10);
  if (eptr == str || *eptr)
    {
      fprintf (stderr, _("%s: invalid number for -%c option\n"),
	       program_name, option);
      usage (stderr);
      exit(1);
    }
  else if (val < min)
    {
      fprintf (stderr, _("%s: value for -%c option should be >= %ld\n"),
	       program_name, option, min);
      if (fatal)
	{
	  usage (stderr);
	  exit(1);
	}
      else
	{
	  val = min;
	}
    }
  else if (max >= 0 && val > max)
    {
      fprintf (stderr, _("%s: value for -%c option should be < %ld\n"),
	       program_name, option, max);
      if (fatal)
	{
	  usage (stderr);
	  exit(1);
	}
      else
	{
	  val = max;
	}
    }
  return val;
}

static void
usage (FILE *stream)
{
  fprintf (stream, _("\
Usage: %s [-0prtx] [--interactive] [--null] [-d|--delimiter=delim]\n\
       [-E eof-str] [-e[eof-str]]  [--eof[=eof-str]]\n\
       [-L max-lines] [-l[max-lines]] [--max-lines[=max-lines]]\n\
       [-I replace-str] [-i[replace-str]] [--replace[=replace-str]]\n\
       [-n max-args] [--max-args=max-args]\n\
       [-s max-chars] [--max-chars=max-chars]\n\
       [-P max-procs]  [--max-procs=max-procs] [--show-limits]\n\
       [--verbose] [--exit] [--no-run-if-empty] [--arg-file=file]\n\
       [--version] [--help] [command [initial-arguments]]\n"),
	   program_name);
  fputs (_("\nReport bugs to <bug-findutils@gnu.org>.\n"), stream);
}