xref: /freebsd/usr.bin/xargs/xargs.c (revision 21ef48af5c0f4ed85a5f42855b5a8d58b5431103)
19b50d902SRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
49b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993
59b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
69b50d902SRodney W. Grimes  *
79b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
89b50d902SRodney W. Grimes  * John B. Roll Jr.
99b50d902SRodney W. Grimes  *
109b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
119b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
129b50d902SRodney W. Grimes  * are met:
139b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
159b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
169b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
179b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
199b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
209b50d902SRodney W. Grimes  *    without specific prior written permission.
219b50d902SRodney W. Grimes  *
229b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
239b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
249b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
259b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
269b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
279b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
289b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
299b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
309b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
319b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
329b50d902SRodney W. Grimes  * SUCH DAMAGE.
33fc17b349SJuli Mallett  *
34fc17b349SJuli Mallett  * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
378eb2a3deSDavid E. O'Brien #if 0
38d7a43b24SJuli Mallett #ifndef lint
39d7a43b24SJuli Mallett static const char copyright[] =
40d7a43b24SJuli Mallett "@(#) Copyright (c) 1990, 1993\n\
41d7a43b24SJuli Mallett 	The Regents of the University of California.  All rights reserved.\n";
42d7a43b24SJuli Mallett #endif /* not lint */
43d7a43b24SJuli Mallett 
44d7a43b24SJuli Mallett #ifndef lint
45d7a43b24SJuli Mallett static char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
46d7a43b24SJuli Mallett #endif /* not lint */
47d7a43b24SJuli Mallett #endif
4851883012SMike Barcroft #include <sys/cdefs.h>
4951883012SMike Barcroft __FBSDID("$FreeBSD$");
5051883012SMike Barcroft 
51e896b4a4SAllan Jude #include <sys/types.h>
529b50d902SRodney W. Grimes #include <sys/wait.h>
53e896b4a4SAllan Jude #include <sys/time.h>
54e896b4a4SAllan Jude #include <sys/limits.h>
55e896b4a4SAllan Jude #include <sys/resource.h>
56a51024e2SPhilippe Charnier #include <err.h>
578ad749a4SDag-Erling Smørgrav #include <errno.h>
5898186e89SMaxime Henrion #include <fcntl.h>
59dc940832SKyle Evans #include <getopt.h>
60305e39f4SJuli Mallett #include <langinfo.h>
61305e39f4SJuli Mallett #include <locale.h>
62305e39f4SJuli Mallett #include <paths.h>
63305e39f4SJuli Mallett #include <regex.h>
64f058359bSTom Jones #include <stdbool.h>
659b50d902SRodney W. Grimes #include <stdio.h>
669b50d902SRodney W. Grimes #include <stdlib.h>
679b50d902SRodney W. Grimes #include <string.h>
689b50d902SRodney W. Grimes #include <unistd.h>
6916b07a33SMark Murray 
709b50d902SRodney W. Grimes #include "pathnames.h"
719b50d902SRodney W. Grimes 
721926d4aaSJuli Mallett static void	parse_input(int, char *[]);
731926d4aaSJuli Mallett static void	prerun(int, char *[]);
74305e39f4SJuli Mallett static int	prompt(void);
75fc17b349SJuli Mallett static void	run(char **);
7673385ac6SJuli Mallett static void	usage(void);
77f058359bSTom Jones bool		strnsubst(char **, const char *, const char *, size_t);
7892095ab6SStephen McKay static pid_t	xwait(int block, int *status);
7970945890SJilles Tjoelker static void	xexit(const char *, const int);
80330d23f5STim J. Robbins static void	waitchildren(const char *, int);
8192095ab6SStephen McKay static void	pids_init(void);
8292095ab6SStephen McKay static int	pids_empty(void);
8392095ab6SStephen McKay static int	pids_full(void);
8492095ab6SStephen McKay static void	pids_add(pid_t pid);
8592095ab6SStephen McKay static int	pids_remove(pid_t pid);
8692095ab6SStephen McKay static int	findslot(pid_t pid);
8792095ab6SStephen McKay static int	findfreeslot(void);
8892095ab6SStephen McKay static void	clearslot(int slot);
899b50d902SRodney W. Grimes 
9016b07a33SMark Murray static char echo[] = _PATH_ECHO;
918eb2a3deSDavid E. O'Brien static char **av, **bxp, **ep, **endxp, **xp;
9291045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr;
9391045075SJuli Mallett static const char *eofstr;
94*21ef48afSYuri Pankov static long eoflen;
9598186e89SMaxime Henrion static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag;
96ba084f6aSJuli Mallett static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag;
97330d23f5STim J. Robbins static int curprocs, maxprocs;
9892095ab6SStephen McKay static pid_t *childpids;
99330d23f5STim J. Robbins 
100330d23f5STim J. Robbins static volatile int childerr;
10116b07a33SMark Murray 
10299a84ce1STim J. Robbins extern char **environ;
10373385ac6SJuli Mallett 
104dc940832SKyle Evans static const char *optstr = "+0E:I:J:L:n:oP:pR:S:s:rtx";
105dc940832SKyle Evans 
106dc940832SKyle Evans static const struct option long_options[] =
107dc940832SKyle Evans {
108dc940832SKyle Evans 	{"exit",		no_argument,		NULL,	'x'},
109dc940832SKyle Evans 	{"interactive",		no_argument,		NULL,	'p'},
110dc940832SKyle Evans 	{"max-args",		required_argument,	NULL,	'n'},
111dc940832SKyle Evans 	{"max-chars",		required_argument,	NULL,	's'},
112dc940832SKyle Evans 	{"max-procs",		required_argument,	NULL,	'P'},
113dc940832SKyle Evans 	{"no-run-if-empty",	no_argument,		NULL,	'r'},
114dc940832SKyle Evans 	{"null",		no_argument,		NULL,	'0'},
115dc940832SKyle Evans 	{"verbose",		no_argument,		NULL,	't'},
116dc940832SKyle Evans 
117dc940832SKyle Evans 	{NULL,			no_argument,		NULL,	0},
118dc940832SKyle Evans };
119dc940832SKyle Evans 
120a51024e2SPhilippe Charnier int
1211926d4aaSJuli Mallett main(int argc, char *argv[])
1229b50d902SRodney W. Grimes {
123a3e5bc4fSJoseph Koshy 	long arg_max;
12491045075SJuli Mallett 	int ch, Jflag, nargs, nflag, nline;
125fc17b349SJuli Mallett 	size_t linelen;
126e896b4a4SAllan Jude 	struct rlimit rl;
1273c675167SJuli Mallett 	char *endptr;
128cffa7aa6SBaptiste Daroussin 	const char *errstr;
1299b50d902SRodney W. Grimes 
130fc17b349SJuli Mallett 	inpline = replstr = NULL;
13191045075SJuli Mallett 	ep = environ;
132fc17b349SJuli Mallett 	eofstr = "";
133*21ef48afSYuri Pankov 	eoflen = 0;
13491045075SJuli Mallett 	Jflag = nflag = 0;
1358d904f15SDima Dorfman 
136f58b94cbSTim J. Robbins 	(void)setlocale(LC_ALL, "");
137305e39f4SJuli Mallett 
1389b50d902SRodney W. Grimes 	/*
1399b50d902SRodney W. Grimes 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
1409b50d902SRodney W. Grimes 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
1419b50d902SRodney W. Grimes 	 * that the smallest argument is 2 bytes in length, this means that
1429b50d902SRodney W. Grimes 	 * the number of arguments is limited to:
1439b50d902SRodney W. Grimes 	 *
1449b50d902SRodney W. Grimes 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
1459b50d902SRodney W. Grimes 	 *
1469b50d902SRodney W. Grimes 	 * We arbitrarily limit the number of arguments to 5000.  This is
1479b50d902SRodney W. Grimes 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
1489b50d902SRodney W. Grimes 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
1499b50d902SRodney W. Grimes 	 * probably not worthwhile.
1509b50d902SRodney W. Grimes 	 */
1519b50d902SRodney W. Grimes 	nargs = 5000;
152a3e5bc4fSJoseph Koshy 	if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
153a3e5bc4fSJoseph Koshy 		errx(1, "sysconf(_SC_ARG_MAX) failed");
154a3e5bc4fSJoseph Koshy 	nline = arg_max - 4 * 1024;
15591045075SJuli Mallett 	while (*ep != NULL) {
156e5009da0SSatoshi Asami 		/* 1 byte for each '\0' */
157e5009da0SSatoshi Asami 		nline -= strlen(*ep++) + 1 + sizeof(*ep);
158e5009da0SSatoshi Asami 	}
159330d23f5STim J. Robbins 	maxprocs = 1;
160dc940832SKyle Evans 	while ((ch = getopt_long(argc, argv, optstr, long_options, NULL)) != -1)
1619b50d902SRodney W. Grimes 		switch (ch) {
162fc17b349SJuli Mallett 		case 'E':
163fc17b349SJuli Mallett 			eofstr = optarg;
164*21ef48afSYuri Pankov 			eoflen = strlen(eofstr);
165fc17b349SJuli Mallett 			break;
166fc17b349SJuli Mallett 		case 'I':
1676ea89183SJuli Mallett 			Jflag = 0;
168fc17b349SJuli Mallett 			Iflag = 1;
1694f49da74SJuli Mallett 			Lflag = 1;
170fc17b349SJuli Mallett 			replstr = optarg;
171fc17b349SJuli Mallett 			break;
1728d904f15SDima Dorfman 		case 'J':
1736ea89183SJuli Mallett 			Iflag = 0;
174b50a7286SJuli Mallett 			Jflag = 1;
1758d904f15SDima Dorfman 			replstr = optarg;
1768d904f15SDima Dorfman 			break;
177fc17b349SJuli Mallett 		case 'L':
178cffa7aa6SBaptiste Daroussin 			Lflag = strtonum(optarg, 0, INT_MAX, &errstr);
179cffa7aa6SBaptiste Daroussin 			if (errstr)
180cffa7aa6SBaptiste Daroussin 				errx(1, "-L %s: %s", optarg, errstr);
181fc17b349SJuli Mallett 			break;
1829b50d902SRodney W. Grimes 		case 'n':
1839b50d902SRodney W. Grimes 			nflag = 1;
184cffa7aa6SBaptiste Daroussin 			nargs = strtonum(optarg, 1, INT_MAX, &errstr);
185cffa7aa6SBaptiste Daroussin 			if (errstr)
186cffa7aa6SBaptiste Daroussin 				errx(1, "-n %s: %s", optarg, errstr);
1879b50d902SRodney W. Grimes 			break;
18898186e89SMaxime Henrion 		case 'o':
18998186e89SMaxime Henrion 			oflag = 1;
19098186e89SMaxime Henrion 			break;
191330d23f5STim J. Robbins 		case 'P':
192c782f6f0SAllan Jude 			maxprocs = strtonum(optarg, 0, INT_MAX, &errstr);
193cffa7aa6SBaptiste Daroussin 			if (errstr)
194cffa7aa6SBaptiste Daroussin 				errx(1, "-P %s: %s", optarg, errstr);
195e896b4a4SAllan Jude 			if (getrlimit(RLIMIT_NPROC, &rl) != 0)
196e896b4a4SAllan Jude 				errx(1, "getrlimit failed");
197e896b4a4SAllan Jude 			if (maxprocs == 0 || maxprocs > rl.rlim_cur)
198e896b4a4SAllan Jude 				maxprocs = rl.rlim_cur;
199330d23f5STim J. Robbins 			break;
200fc17b349SJuli Mallett 		case 'p':
201fc17b349SJuli Mallett 			pflag = 1;
202fc17b349SJuli Mallett 			break;
203b50a7286SJuli Mallett 		case 'R':
2043c675167SJuli Mallett 			Rflag = strtol(optarg, &endptr, 10);
2053c675167SJuli Mallett 			if (*endptr != '\0')
2063c675167SJuli Mallett 				errx(1, "replacements must be a number");
207b50a7286SJuli Mallett 			break;
2082d14e0e5SDag-Erling Smørgrav 		case 'r':
2092d14e0e5SDag-Erling Smørgrav 			/* GNU compatibility */
2102d14e0e5SDag-Erling Smørgrav 			break;
211ba084f6aSJuli Mallett 		case 'S':
212ba084f6aSJuli Mallett 			Sflag = strtoul(optarg, &endptr, 10);
213ba084f6aSJuli Mallett 			if (*endptr != '\0')
214ba084f6aSJuli Mallett 				errx(1, "replsize must be a number");
215ba084f6aSJuli Mallett 			break;
2169b50d902SRodney W. Grimes 		case 's':
217cffa7aa6SBaptiste Daroussin 			nline = strtonum(optarg, 0, INT_MAX, &errstr);
218cffa7aa6SBaptiste Daroussin 			if (errstr)
219cffa7aa6SBaptiste Daroussin 				errx(1, "-s %s: %s", optarg, errstr);
2209b50d902SRodney W. Grimes 			break;
2219b50d902SRodney W. Grimes 		case 't':
2229b50d902SRodney W. Grimes 			tflag = 1;
2239b50d902SRodney W. Grimes 			break;
2249b50d902SRodney W. Grimes 		case 'x':
2259b50d902SRodney W. Grimes 			xflag = 1;
2269b50d902SRodney W. Grimes 			break;
227d9198881SWarner Losh 		case '0':
228d9198881SWarner Losh 			zflag = 1;
229d9198881SWarner Losh 			break;
2309b50d902SRodney W. Grimes 		case '?':
2319b50d902SRodney W. Grimes 		default:
2329b50d902SRodney W. Grimes 			usage();
2339b50d902SRodney W. Grimes 	}
2349b50d902SRodney W. Grimes 	argc -= optind;
2359b50d902SRodney W. Grimes 	argv += optind;
2369b50d902SRodney W. Grimes 
2376ea89183SJuli Mallett 	if (!Iflag && Rflag)
2386ea89183SJuli Mallett 		usage();
239ba084f6aSJuli Mallett 	if (!Iflag && Sflag)
240ba084f6aSJuli Mallett 		usage();
2416ea89183SJuli Mallett 	if (Iflag && !Rflag)
2426ea89183SJuli Mallett 		Rflag = 5;
243ba084f6aSJuli Mallett 	if (Iflag && !Sflag)
244ba084f6aSJuli Mallett 		Sflag = 255;
2459b50d902SRodney W. Grimes 	if (xflag && !nflag)
2469b50d902SRodney W. Grimes 		usage();
2474f49da74SJuli Mallett 	if (Iflag || Lflag)
248fc17b349SJuli Mallett 		xflag = 1;
249fc17b349SJuli Mallett 	if (replstr != NULL && *replstr == '\0')
250fc17b349SJuli Mallett 		errx(1, "replstr may not be empty");
2519b50d902SRodney W. Grimes 
25292095ab6SStephen McKay 	pids_init();
25392095ab6SStephen McKay 
2549b50d902SRodney W. Grimes 	/*
2559b50d902SRodney W. Grimes 	 * Allocate pointers for the utility name, the utility arguments,
2569b50d902SRodney W. Grimes 	 * the maximum arguments to be read from stdin and the trailing
2579b50d902SRodney W. Grimes 	 * NULL.
2589b50d902SRodney W. Grimes 	 */
259fc17b349SJuli Mallett 	linelen = 1 + argc + nargs + 1;
260c9e1c304SUlrich Spörlein 	if ((av = bxp = malloc(linelen * sizeof(char *))) == NULL)
26191045075SJuli Mallett 		errx(1, "malloc failed");
2629b50d902SRodney W. Grimes 
2639b50d902SRodney W. Grimes 	/*
2649b50d902SRodney W. Grimes 	 * Use the user's name for the utility as argv[0], just like the
2659b50d902SRodney W. Grimes 	 * shell.  Echo is the default.  Set up pointers for the user's
2669b50d902SRodney W. Grimes 	 * arguments.
2679b50d902SRodney W. Grimes 	 */
2683dca1afcSJuli Mallett 	if (*argv == NULL)
2699bf450b8SJuli Mallett 		cnt = strlen(*bxp++ = echo);
2709b50d902SRodney W. Grimes 	else {
2719b50d902SRodney W. Grimes 		do {
272b50a7286SJuli Mallett 			if (Jflag && strcmp(*argv, replstr) == 0) {
27391045075SJuli Mallett 				char **avj;
2748d904f15SDima Dorfman 				jfound = 1;
2758d904f15SDima Dorfman 				argv++;
2768d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
2778d904f15SDima Dorfman 					cnt += strlen(*avj) + 1;
2788d904f15SDima Dorfman 				break;
2798d904f15SDima Dorfman 			}
2809b50d902SRodney W. Grimes 			cnt += strlen(*bxp++ = *argv) + 1;
2813dca1afcSJuli Mallett 		} while (*++argv != NULL);
2829b50d902SRodney W. Grimes 	}
2839b50d902SRodney W. Grimes 
2849b50d902SRodney W. Grimes 	/*
2859b50d902SRodney W. Grimes 	 * Set up begin/end/traversing pointers into the array.  The -n
2869b50d902SRodney W. Grimes 	 * count doesn't include the trailing NULL pointer, so the malloc
2879b50d902SRodney W. Grimes 	 * added in an extra slot.
2889b50d902SRodney W. Grimes 	 */
2898eb2a3deSDavid E. O'Brien 	endxp = (xp = bxp) + nargs;
2909b50d902SRodney W. Grimes 
2919b50d902SRodney W. Grimes 	/*
2929b50d902SRodney W. Grimes 	 * Allocate buffer space for the arguments read from stdin and the
2939b50d902SRodney W. Grimes 	 * trailing NULL.  Buffer space is defined as the default or specified
2949b50d902SRodney W. Grimes 	 * space, minus the length of the utility name and arguments.  Set up
2959b50d902SRodney W. Grimes 	 * begin/end/traversing pointers into the array.  The -s count does
2969b50d902SRodney W. Grimes 	 * include the trailing NULL, so the malloc didn't add in an extra
2979b50d902SRodney W. Grimes 	 * slot.
2989b50d902SRodney W. Grimes 	 */
2999b50d902SRodney W. Grimes 	nline -= cnt;
3009b50d902SRodney W. Grimes 	if (nline <= 0)
301a51024e2SPhilippe Charnier 		errx(1, "insufficient space for command");
3029b50d902SRodney W. Grimes 
3039bf450b8SJuli Mallett 	if ((bbp = malloc((size_t)(nline + 1))) == NULL)
30491045075SJuli Mallett 		errx(1, "malloc failed");
3059b50d902SRodney W. Grimes 	ebp = (argp = p = bbp) + nline - 1;
30691045075SJuli Mallett 	for (;;)
30791045075SJuli Mallett 		parse_input(argc, argv);
30891045075SJuli Mallett }
3099b50d902SRodney W. Grimes 
31091045075SJuli Mallett static void
3111926d4aaSJuli Mallett parse_input(int argc, char *argv[])
31291045075SJuli Mallett {
31391045075SJuli Mallett 	int ch, foundeof;
31491045075SJuli Mallett 	char **avj;
31591045075SJuli Mallett 
31691045075SJuli Mallett 	foundeof = 0;
31791045075SJuli Mallett 
3189b50d902SRodney W. Grimes 	switch (ch = getchar()) {
3199b50d902SRodney W. Grimes 	case EOF:
3209b50d902SRodney W. Grimes 		/* No arguments since last exec. */
3210ca740d9Sliu-du 		if (p == bbp) {
3220ca740d9Sliu-du 			waitchildren(*av, 1);
3230ca740d9Sliu-du 			exit(rval);
3240ca740d9Sliu-du 		}
3259b50d902SRodney W. Grimes 		goto arg1;
3269b50d902SRodney W. Grimes 	case ' ':
3279b50d902SRodney W. Grimes 	case '\t':
3289b50d902SRodney W. Grimes 		/* Quotes escape tabs and spaces. */
329d9198881SWarner Losh 		if (insingle || indouble || zflag)
3309b50d902SRodney W. Grimes 			goto addch;
3319b50d902SRodney W. Grimes 		goto arg2;
332d9198881SWarner Losh 	case '\0':
33346793db9SGarance A Drosehn 		if (zflag) {
33446793db9SGarance A Drosehn 			/*
33546793db9SGarance A Drosehn 			 * Increment 'count', so that nulls will be treated
33646793db9SGarance A Drosehn 			 * as end-of-line, as well as end-of-argument.  This
33746793db9SGarance A Drosehn 			 * is needed so -0 works properly with -I and -L.
33846793db9SGarance A Drosehn 			 */
33946793db9SGarance A Drosehn 			count++;
340d9198881SWarner Losh 			goto arg2;
34146793db9SGarance A Drosehn 		}
342d9198881SWarner Losh 		goto addch;
3439b50d902SRodney W. Grimes 	case '\n':
344d9198881SWarner Losh 		if (zflag)
345d9198881SWarner Losh 			goto addch;
34646793db9SGarance A Drosehn 		count++;	    /* Indicate end-of-line (used by -L) */
347d9198881SWarner Losh 
3489b50d902SRodney W. Grimes 		/* Quotes do not escape newlines. */
34970945890SJilles Tjoelker arg1:		if (insingle || indouble) {
35070945890SJilles Tjoelker 			warnx("unterminated quote");
35170945890SJilles Tjoelker 			xexit(*av, 1);
35270945890SJilles Tjoelker 		}
353ef9866beSJean-Marc Zucconi arg2:
354*21ef48afSYuri Pankov 		foundeof = eoflen != 0 && p - argp == eoflen &&
355*21ef48afSYuri Pankov 		    strncmp(argp, eofstr, eoflen) == 0;
356fc17b349SJuli Mallett 
357ef9866beSJean-Marc Zucconi 		/* Do not make empty args unless they are quoted */
358fc17b349SJuli Mallett 		if ((argp != p || wasquoted) && !foundeof) {
359ef9866beSJean-Marc Zucconi 			*p++ = '\0';
3609b50d902SRodney W. Grimes 			*xp++ = argp;
361fc17b349SJuli Mallett 			if (Iflag) {
362fc17b349SJuli Mallett 				size_t curlen;
363b9b03ba0SJuli Mallett 
364b9b03ba0SJuli Mallett 				if (inpline == NULL)
365fc17b349SJuli Mallett 					curlen = 0;
366fc17b349SJuli Mallett 				else {
3671925cb24SJuli Mallett 					/*
3681925cb24SJuli Mallett 					 * If this string is not zero
3691925cb24SJuli Mallett 					 * length, append a space for
3702b239dd1SJens Schweikhardt 					 * separation before the next
3711925cb24SJuli Mallett 					 * argument.
3721925cb24SJuli Mallett 					 */
373b9b03ba0SJuli Mallett 					if ((curlen = strlen(inpline)))
374fc17b349SJuli Mallett 						strcat(inpline, " ");
375fc17b349SJuli Mallett 				}
376fc17b349SJuli Mallett 				curlen++;
3771925cb24SJuli Mallett 				/*
3781925cb24SJuli Mallett 				 * Allocate enough to hold what we will
3795eb40323SJuli Mallett 				 * be holding in a second, and to append
3801925cb24SJuli Mallett 				 * a space next time through, if we have
3811925cb24SJuli Mallett 				 * to.
3821925cb24SJuli Mallett 				 */
383b9b03ba0SJuli Mallett 				inpline = realloc(inpline, curlen + 2 +
384b9b03ba0SJuli Mallett 				    strlen(argp));
38570945890SJilles Tjoelker 				if (inpline == NULL) {
38670945890SJilles Tjoelker 					warnx("realloc failed");
38770945890SJilles Tjoelker 					xexit(*av, 1);
38870945890SJilles Tjoelker 				}
389fc17b349SJuli Mallett 				if (curlen == 1)
390fc17b349SJuli Mallett 					strcpy(inpline, argp);
391fc17b349SJuli Mallett 				else
392fc17b349SJuli Mallett 					strcat(inpline, argp);
393fc17b349SJuli Mallett 			}
394ef9866beSJean-Marc Zucconi 		}
3959b50d902SRodney W. Grimes 
3969b50d902SRodney W. Grimes 		/*
3979b50d902SRodney W. Grimes 		 * If max'd out on args or buffer, or reached EOF,
3989b50d902SRodney W. Grimes 		 * run the command.  If xflag and max'd out on buffer
3991925cb24SJuli Mallett 		 * but not on args, object.  Having reached the limit
4001925cb24SJuli Mallett 		 * of input lines, as specified by -L is the same as
4011925cb24SJuli Mallett 		 * maxing out on arguments.
4029b50d902SRodney W. Grimes 		 */
4038eb2a3deSDavid E. O'Brien 		if (xp == endxp || p > ebp || ch == EOF ||
4045eb40323SJuli Mallett 		    (Lflag <= count && xflag) || foundeof) {
40570945890SJilles Tjoelker 			if (xflag && xp != endxp && p > ebp) {
40670945890SJilles Tjoelker 				warnx("insufficient space for arguments");
40770945890SJilles Tjoelker 				xexit(*av, 1);
40870945890SJilles Tjoelker 			}
4098d904f15SDima Dorfman 			if (jfound) {
4108d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
4118d904f15SDima Dorfman 					*xp++ = *avj;
4128d904f15SDima Dorfman 			}
41391045075SJuli Mallett 			prerun(argc, av);
4140ca740d9Sliu-du 			if (ch == EOF || foundeof) {
4150ca740d9Sliu-du 				waitchildren(*av, 1);
4160ca740d9Sliu-du 				exit(rval);
4170ca740d9Sliu-du 			}
4189b50d902SRodney W. Grimes 			p = bbp;
4199b50d902SRodney W. Grimes 			xp = bxp;
420fc17b349SJuli Mallett 			count = 0;
421ef9866beSJean-Marc Zucconi 		}
4229b50d902SRodney W. Grimes 		argp = p;
423ef9866beSJean-Marc Zucconi 		wasquoted = 0;
4249b50d902SRodney W. Grimes 		break;
4259b50d902SRodney W. Grimes 	case '\'':
426d9198881SWarner Losh 		if (indouble || zflag)
4279b50d902SRodney W. Grimes 			goto addch;
4289b50d902SRodney W. Grimes 		insingle = !insingle;
429ef9866beSJean-Marc Zucconi 		wasquoted = 1;
4309b50d902SRodney W. Grimes 		break;
4319b50d902SRodney W. Grimes 	case '"':
432d9198881SWarner Losh 		if (insingle || zflag)
4339b50d902SRodney W. Grimes 			goto addch;
4349b50d902SRodney W. Grimes 		indouble = !indouble;
435ef9866beSJean-Marc Zucconi 		wasquoted = 1;
4369b50d902SRodney W. Grimes 		break;
4379b50d902SRodney W. Grimes 	case '\\':
438d9198881SWarner Losh 		if (zflag)
439d9198881SWarner Losh 			goto addch;
4409b50d902SRodney W. Grimes 		/* Backslash escapes anything, is escaped by quotes. */
44170945890SJilles Tjoelker 		if (!insingle && !indouble && (ch = getchar()) == EOF) {
44270945890SJilles Tjoelker 			warnx("backslash at EOF");
44370945890SJilles Tjoelker 			xexit(*av, 1);
44470945890SJilles Tjoelker 		}
4459b50d902SRodney W. Grimes 		/* FALLTHROUGH */
4469b50d902SRodney W. Grimes 	default:
4479b50d902SRodney W. Grimes addch:		if (p < ebp) {
4489b50d902SRodney W. Grimes 			*p++ = ch;
4499b50d902SRodney W. Grimes 			break;
4509b50d902SRodney W. Grimes 		}
4519b50d902SRodney W. Grimes 
4529b50d902SRodney W. Grimes 		/* If only one argument, not enough buffer space. */
45370945890SJilles Tjoelker 		if (bxp == xp) {
45470945890SJilles Tjoelker 			warnx("insufficient space for argument");
45570945890SJilles Tjoelker 			xexit(*av, 1);
45670945890SJilles Tjoelker 		}
4579b50d902SRodney W. Grimes 		/* Didn't hit argument limit, so if xflag object. */
45870945890SJilles Tjoelker 		if (xflag) {
45970945890SJilles Tjoelker 			warnx("insufficient space for arguments");
46070945890SJilles Tjoelker 			xexit(*av, 1);
46170945890SJilles Tjoelker 		}
4629b50d902SRodney W. Grimes 
4638d904f15SDima Dorfman 		if (jfound) {
4648d904f15SDima Dorfman 			for (avj = argv; *avj; avj++)
4658d904f15SDima Dorfman 				*xp++ = *avj;
4668d904f15SDima Dorfman 		}
46791045075SJuli Mallett 		prerun(argc, av);
4689b50d902SRodney W. Grimes 		xp = bxp;
4699b50d902SRodney W. Grimes 		cnt = ebp - argp;
470fc17b349SJuli Mallett 		memcpy(bbp, argp, (size_t)cnt);
4719b50d902SRodney W. Grimes 		p = (argp = bbp) + cnt;
4729b50d902SRodney W. Grimes 		*p++ = ch;
4739b50d902SRodney W. Grimes 		break;
4749b50d902SRodney W. Grimes 	}
4759b50d902SRodney W. Grimes }
4769b50d902SRodney W. Grimes 
477263dc775SJuli Mallett /*
478263dc775SJuli Mallett  * Do things necessary before run()'ing, such as -I substitution,
479263dc775SJuli Mallett  * and then call run().
480263dc775SJuli Mallett  */
481263dc775SJuli Mallett static void
4821926d4aaSJuli Mallett prerun(int argc, char *argv[])
483263dc775SJuli Mallett {
484263dc775SJuli Mallett 	char **tmp, **tmp2, **avj;
48591045075SJuli Mallett 	int repls;
48691045075SJuli Mallett 
48791045075SJuli Mallett 	repls = Rflag;
488263dc775SJuli Mallett 
489be70f7d4SJuli Mallett 	if (argc == 0 || repls == 0) {
490263dc775SJuli Mallett 		*xp = NULL;
491263dc775SJuli Mallett 		run(argv);
492263dc775SJuli Mallett 		return;
493263dc775SJuli Mallett 	}
494263dc775SJuli Mallett 
495263dc775SJuli Mallett 	avj = argv;
496263dc775SJuli Mallett 
497263dc775SJuli Mallett 	/*
498263dc775SJuli Mallett 	 * Allocate memory to hold the argument list, and
499263dc775SJuli Mallett 	 * a NULL at the tail.
500263dc775SJuli Mallett 	 */
501c9e1c304SUlrich Spörlein 	tmp = malloc((argc + 1) * sizeof(char *));
50270945890SJilles Tjoelker 	if (tmp == NULL) {
50370945890SJilles Tjoelker 		warnx("malloc failed");
50470945890SJilles Tjoelker 		xexit(*argv, 1);
50570945890SJilles Tjoelker 	}
506263dc775SJuli Mallett 	tmp2 = tmp;
507263dc775SJuli Mallett 
508263dc775SJuli Mallett 	/*
509263dc775SJuli Mallett 	 * Save the first argument and iterate over it, we
510263dc775SJuli Mallett 	 * cannot do strnsubst() to it.
511263dc775SJuli Mallett 	 */
51270945890SJilles Tjoelker 	if ((*tmp++ = strdup(*avj++)) == NULL) {
51370945890SJilles Tjoelker 		warnx("strdup failed");
51470945890SJilles Tjoelker 		xexit(*argv, 1);
51570945890SJilles Tjoelker 	}
516263dc775SJuli Mallett 
517263dc775SJuli Mallett 	/*
518263dc775SJuli Mallett 	 * For each argument to utility, if we have not used up
519263dc775SJuli Mallett 	 * the number of replacements we are allowed to do, and
5202b239dd1SJens Schweikhardt 	 * if the argument contains at least one occurrence of
521263dc775SJuli Mallett 	 * replstr, call strnsubst(), else just save the string.
522263dc775SJuli Mallett 	 * Iterations over elements of avj and tmp are done
523263dc775SJuli Mallett 	 * where appropriate.
524263dc775SJuli Mallett 	 */
525263dc775SJuli Mallett 	while (--argc) {
526263dc775SJuli Mallett 		*tmp = *avj++;
527263dc775SJuli Mallett 		if (repls && strstr(*tmp, replstr) != NULL) {
528f058359bSTom Jones 			if (strnsubst(tmp++, replstr, inpline, (size_t)Sflag)) {
529f058359bSTom Jones 				warnx("comamnd line cannot be assembled, too long");
530f058359bSTom Jones 				xexit(*argv, 1);
531f058359bSTom Jones 			}
5323c675167SJuli Mallett 			if (repls > 0)
533263dc775SJuli Mallett 				repls--;
534263dc775SJuli Mallett 		} else {
53570945890SJilles Tjoelker 			if ((*tmp = strdup(*tmp)) == NULL) {
53670945890SJilles Tjoelker 				warnx("strdup failed");
53770945890SJilles Tjoelker 				xexit(*argv, 1);
53870945890SJilles Tjoelker 			}
539263dc775SJuli Mallett 			tmp++;
540263dc775SJuli Mallett 		}
541263dc775SJuli Mallett 	}
542263dc775SJuli Mallett 
543263dc775SJuli Mallett 	/*
544263dc775SJuli Mallett 	 * Run it.
545263dc775SJuli Mallett 	 */
546b6594dbaSJuli Mallett 	*tmp = NULL;
547263dc775SJuli Mallett 	run(tmp2);
548263dc775SJuli Mallett 
549263dc775SJuli Mallett 	/*
550263dc775SJuli Mallett 	 * Walk from the tail to the head, free along the way.
551263dc775SJuli Mallett 	 */
552263dc775SJuli Mallett 	for (; tmp2 != tmp; tmp--)
553263dc775SJuli Mallett 		free(*tmp);
554263dc775SJuli Mallett 	/*
555263dc775SJuli Mallett 	 * Now free the list itself.
556263dc775SJuli Mallett 	 */
557263dc775SJuli Mallett 	free(tmp2);
558263dc775SJuli Mallett 
559263dc775SJuli Mallett 	/*
560986d829bSJuli Mallett 	 * Free the input line buffer, if we have one.
561263dc775SJuli Mallett 	 */
56251f7a48bSJuli Mallett 	if (inpline != NULL) {
56391045075SJuli Mallett 		free(inpline);
56451f7a48bSJuli Mallett 		inpline = NULL;
56551f7a48bSJuli Mallett 	}
566263dc775SJuli Mallett }
567263dc775SJuli Mallett 
568fc17b349SJuli Mallett static void
56973385ac6SJuli Mallett run(char **argv)
5709b50d902SRodney W. Grimes {
5719b50d902SRodney W. Grimes 	pid_t pid;
5720792992cSMaxime Henrion 	int fd;
573330d23f5STim J. Robbins 	char **avec;
5749b50d902SRodney W. Grimes 
575305e39f4SJuli Mallett 	/*
576305e39f4SJuli Mallett 	 * If the user wants to be notified of each command before it is
577305e39f4SJuli Mallett 	 * executed, notify them.  If they want the notification to be
578305e39f4SJuli Mallett 	 * followed by a prompt, then prompt them.
579305e39f4SJuli Mallett 	 */
580fc17b349SJuli Mallett 	if (tflag || pflag) {
5819b50d902SRodney W. Grimes 		(void)fprintf(stderr, "%s", *argv);
58291045075SJuli Mallett 		for (avec = argv + 1; *avec != NULL; ++avec)
58391045075SJuli Mallett 			(void)fprintf(stderr, " %s", *avec);
584305e39f4SJuli Mallett 		/*
585305e39f4SJuli Mallett 		 * If the user has asked to be prompted, do so.
586305e39f4SJuli Mallett 		 */
587305e39f4SJuli Mallett 		if (pflag)
588305e39f4SJuli Mallett 			/*
589305e39f4SJuli Mallett 			 * If they asked not to exec, return without execution
590305e39f4SJuli Mallett 			 * but if they asked to, go to the execution.  If we
591305e39f4SJuli Mallett 			 * could not open their tty, break the switch and drop
592305e39f4SJuli Mallett 			 * back to -t behaviour.
593305e39f4SJuli Mallett 			 */
594305e39f4SJuli Mallett 			switch (prompt()) {
595305e39f4SJuli Mallett 			case 0:
596fc17b349SJuli Mallett 				return;
597305e39f4SJuli Mallett 			case 1:
598305e39f4SJuli Mallett 				goto exec;
599305e39f4SJuli Mallett 			case 2:
600305e39f4SJuli Mallett 				break;
601305e39f4SJuli Mallett 			}
6029b50d902SRodney W. Grimes 		(void)fprintf(stderr, "\n");
6039b50d902SRodney W. Grimes 		(void)fflush(stderr);
6049b50d902SRodney W. Grimes 	}
605305e39f4SJuli Mallett exec:
6068ad749a4SDag-Erling Smørgrav 	childerr = 0;
6078ad749a4SDag-Erling Smørgrav 	switch (pid = vfork()) {
6089b50d902SRodney W. Grimes 	case -1:
60970945890SJilles Tjoelker 		warn("vfork");
61070945890SJilles Tjoelker 		xexit(*argv, 1);
6119b50d902SRodney W. Grimes 	case 0:
612cec1ba8cSMaxime Henrion 		if (oflag) {
6130792992cSMaxime Henrion 			if ((fd = open(_PATH_TTY, O_RDONLY)) == -1)
6140792992cSMaxime Henrion 				err(1, "can't open /dev/tty");
615cec1ba8cSMaxime Henrion 		} else {
6160792992cSMaxime Henrion 			fd = open(_PATH_DEVNULL, O_RDONLY);
6170792992cSMaxime Henrion 		}
6180792992cSMaxime Henrion 		if (fd > STDIN_FILENO) {
6190792992cSMaxime Henrion 			if (dup2(fd, STDIN_FILENO) != 0)
6200792992cSMaxime Henrion 				err(1, "can't dup2 to stdin");
6210792992cSMaxime Henrion 			close(fd);
62298186e89SMaxime Henrion 		}
6239b50d902SRodney W. Grimes 		execvp(argv[0], argv);
6248ad749a4SDag-Erling Smørgrav 		childerr = errno;
6259b50d902SRodney W. Grimes 		_exit(1);
6269b50d902SRodney W. Grimes 	}
62792095ab6SStephen McKay 	pids_add(pid);
628330d23f5STim J. Robbins 	waitchildren(*argv, 0);
629330d23f5STim J. Robbins }
630330d23f5STim J. Robbins 
63192095ab6SStephen McKay /*
63292095ab6SStephen McKay  * Wait for a tracked child to exit and return its pid and exit status.
63392095ab6SStephen McKay  *
63492095ab6SStephen McKay  * Ignores (discards) all untracked child processes.
63592095ab6SStephen McKay  * Returns -1 and sets errno to ECHILD if no tracked children exist.
63692095ab6SStephen McKay  * If block is set, waits indefinitely for a child process to exit.
63792095ab6SStephen McKay  * If block is not set and no children have exited, returns 0 immediately.
63892095ab6SStephen McKay  */
63992095ab6SStephen McKay static pid_t
64092095ab6SStephen McKay xwait(int block, int *status) {
64192095ab6SStephen McKay 	pid_t pid;
64292095ab6SStephen McKay 
64392095ab6SStephen McKay 	if (pids_empty()) {
64492095ab6SStephen McKay 		errno = ECHILD;
64529afe03fSStephen McKay 		return (-1);
64692095ab6SStephen McKay 	}
64792095ab6SStephen McKay 
64892095ab6SStephen McKay 	while ((pid = waitpid(-1, status, block ? 0 : WNOHANG)) > 0)
64992095ab6SStephen McKay 		if (pids_remove(pid))
65092095ab6SStephen McKay 			break;
65192095ab6SStephen McKay 
65229afe03fSStephen McKay 	return (pid);
65392095ab6SStephen McKay }
65492095ab6SStephen McKay 
655330d23f5STim J. Robbins static void
65670945890SJilles Tjoelker xexit(const char *name, const int exit_code) {
65770945890SJilles Tjoelker 	waitchildren(name, 1);
65870945890SJilles Tjoelker 	exit(exit_code);
65970945890SJilles Tjoelker }
66070945890SJilles Tjoelker 
66170945890SJilles Tjoelker static void
662330d23f5STim J. Robbins waitchildren(const char *name, int waitall)
663330d23f5STim J. Robbins {
664330d23f5STim J. Robbins 	pid_t pid;
665330d23f5STim J. Robbins 	int status;
66670945890SJilles Tjoelker 	int cause_exit = 0;
667330d23f5STim J. Robbins 
66892095ab6SStephen McKay 	while ((pid = xwait(waitall || pids_full(), &status)) > 0) {
66970945890SJilles Tjoelker 		/*
67070945890SJilles Tjoelker 		 * If we couldn't invoke the utility or if utility exited
67170945890SJilles Tjoelker 		 * because of a signal or with a value of 255, warn (per
67270945890SJilles Tjoelker 		 * POSIX), and then wait until all other children have
67370945890SJilles Tjoelker 		 * exited before exiting 1-125. POSIX requires us to stop
67470945890SJilles Tjoelker 		 * reading if child exits because of a signal or with 255,
67570945890SJilles Tjoelker 		 * but it does not require us to exit immediately; waiting
67670945890SJilles Tjoelker 		 * is preferable to orphaning.
67770945890SJilles Tjoelker 		 */
67870945890SJilles Tjoelker 		if (childerr != 0 && cause_exit == 0) {
679330d23f5STim J. Robbins 			errno = childerr;
68070945890SJilles Tjoelker 			waitall = 1;
681ab825606SMark Johnston 			cause_exit = errno == ENOENT ? 127 : 126;
68270945890SJilles Tjoelker 			warn("%s", name);
68370945890SJilles Tjoelker 		} else if (WIFSIGNALED(status)) {
68470945890SJilles Tjoelker 			waitall = cause_exit = 1;
68570945890SJilles Tjoelker 			warnx("%s: terminated with signal %d; aborting",
68623583c4fSJilles Tjoelker 			    name, WTERMSIG(status));
68770945890SJilles Tjoelker 		} else if (WEXITSTATUS(status) == 255) {
68870945890SJilles Tjoelker 			waitall = cause_exit = 1;
68970945890SJilles Tjoelker 			warnx("%s: exited with status 255; aborting", name);
69070945890SJilles Tjoelker 		} else if (WEXITSTATUS(status))
6919b50d902SRodney W. Grimes  			rval = 1;
6929b50d902SRodney W. Grimes 	}
69392095ab6SStephen McKay 
69470945890SJilles Tjoelker  	if (cause_exit)
69570945890SJilles Tjoelker 		exit(cause_exit);
696330d23f5STim J. Robbins 	if (pid == -1 && errno != ECHILD)
69792095ab6SStephen McKay 		err(1, "waitpid");
69892095ab6SStephen McKay }
69992095ab6SStephen McKay 
70092095ab6SStephen McKay #define	NOPID	(0)
70192095ab6SStephen McKay 
70292095ab6SStephen McKay static void
70329afe03fSStephen McKay pids_init(void)
70492095ab6SStephen McKay {
70592095ab6SStephen McKay 	int i;
70692095ab6SStephen McKay 
70792095ab6SStephen McKay 	if ((childpids = malloc(maxprocs * sizeof(*childpids))) == NULL)
70892095ab6SStephen McKay 		errx(1, "malloc failed");
70992095ab6SStephen McKay 
71092095ab6SStephen McKay 	for (i = 0; i < maxprocs; i++)
71192095ab6SStephen McKay 		clearslot(i);
71292095ab6SStephen McKay }
71392095ab6SStephen McKay 
71492095ab6SStephen McKay static int
71529afe03fSStephen McKay pids_empty(void)
71692095ab6SStephen McKay {
717a274be77SEitan Adler 
71829afe03fSStephen McKay 	return (curprocs == 0);
71992095ab6SStephen McKay }
72092095ab6SStephen McKay 
72192095ab6SStephen McKay static int
72229afe03fSStephen McKay pids_full(void)
72392095ab6SStephen McKay {
724a274be77SEitan Adler 
72529afe03fSStephen McKay 	return (curprocs >= maxprocs);
72692095ab6SStephen McKay }
72792095ab6SStephen McKay 
72892095ab6SStephen McKay static void
72992095ab6SStephen McKay pids_add(pid_t pid)
73092095ab6SStephen McKay {
73192095ab6SStephen McKay 	int slot;
73292095ab6SStephen McKay 
73392095ab6SStephen McKay 	slot = findfreeslot();
73492095ab6SStephen McKay 	childpids[slot] = pid;
73592095ab6SStephen McKay 	curprocs++;
73692095ab6SStephen McKay }
73792095ab6SStephen McKay 
73892095ab6SStephen McKay static int
73992095ab6SStephen McKay pids_remove(pid_t pid)
74092095ab6SStephen McKay {
74192095ab6SStephen McKay 	int slot;
74292095ab6SStephen McKay 
74392095ab6SStephen McKay 	if ((slot = findslot(pid)) < 0)
74429afe03fSStephen McKay 		return (0);
74592095ab6SStephen McKay 
74692095ab6SStephen McKay 	clearslot(slot);
74792095ab6SStephen McKay 	curprocs--;
74829afe03fSStephen McKay 	return (1);
74992095ab6SStephen McKay }
75092095ab6SStephen McKay 
75192095ab6SStephen McKay static int
75229afe03fSStephen McKay findfreeslot(void)
75392095ab6SStephen McKay {
75492095ab6SStephen McKay 	int slot;
75592095ab6SStephen McKay 
75692095ab6SStephen McKay 	if ((slot = findslot(NOPID)) < 0)
75792095ab6SStephen McKay 		errx(1, "internal error: no free pid slot");
75829afe03fSStephen McKay 	return (slot);
75992095ab6SStephen McKay }
76092095ab6SStephen McKay 
76192095ab6SStephen McKay static int
76292095ab6SStephen McKay findslot(pid_t pid)
76392095ab6SStephen McKay {
76492095ab6SStephen McKay 	int slot;
76592095ab6SStephen McKay 
76692095ab6SStephen McKay 	for (slot = 0; slot < maxprocs; slot++)
76792095ab6SStephen McKay 		if (childpids[slot] == pid)
76829afe03fSStephen McKay 			return (slot);
76929afe03fSStephen McKay 	return (-1);
77092095ab6SStephen McKay }
77192095ab6SStephen McKay 
77292095ab6SStephen McKay static void
77392095ab6SStephen McKay clearslot(int slot)
77492095ab6SStephen McKay {
775a274be77SEitan Adler 
77692095ab6SStephen McKay 	childpids[slot] = NOPID;
777330d23f5STim J. Robbins }
7789b50d902SRodney W. Grimes 
779305e39f4SJuli Mallett /*
780305e39f4SJuli Mallett  * Prompt the user about running a command.
781305e39f4SJuli Mallett  */
782305e39f4SJuli Mallett static int
783305e39f4SJuli Mallett prompt(void)
784305e39f4SJuli Mallett {
785305e39f4SJuli Mallett 	regex_t cre;
786305e39f4SJuli Mallett 	size_t rsize;
787305e39f4SJuli Mallett 	int match;
788305e39f4SJuli Mallett 	char *response;
789305e39f4SJuli Mallett 	FILE *ttyfp;
790305e39f4SJuli Mallett 
791305e39f4SJuli Mallett 	if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
792305e39f4SJuli Mallett 		return (2);	/* Indicate that the TTY failed to open. */
793305e39f4SJuli Mallett 	(void)fprintf(stderr, "?...");
794305e39f4SJuli Mallett 	(void)fflush(stderr);
795305e39f4SJuli Mallett 	if ((response = fgetln(ttyfp, &rsize)) == NULL ||
7960d2dcf21SYuri Pankov 	    regcomp(&cre, nl_langinfo(YESEXPR), REG_EXTENDED) != 0) {
797305e39f4SJuli Mallett 		(void)fclose(ttyfp);
798305e39f4SJuli Mallett 		return (0);
799305e39f4SJuli Mallett 	}
800b65bc267SJuli Mallett 	response[rsize - 1] = '\0';
801305e39f4SJuli Mallett 	match = regexec(&cre, response, 0, NULL, 0);
802305e39f4SJuli Mallett 	(void)fclose(ttyfp);
803305e39f4SJuli Mallett 	regfree(&cre);
804305e39f4SJuli Mallett 	return (match == 0);
805305e39f4SJuli Mallett }
806305e39f4SJuli Mallett 
807a51024e2SPhilippe Charnier static void
80873385ac6SJuli Mallett usage(void)
8099b50d902SRodney W. Grimes {
810a274be77SEitan Adler 
8118d904f15SDima Dorfman 	fprintf(stderr,
812ba084f6aSJuli Mallett "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]\n"
813ba084f6aSJuli Mallett "             [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]\n"
814ba084f6aSJuli Mallett "             [-s size] [utility [argument ...]]\n");
8159b50d902SRodney W. Grimes 	exit(1);
8169b50d902SRodney W. Grimes }
817