xref: /freebsd/usr.bin/xargs/xargs.c (revision fbbd9655e5107c68e4e0146ff22b73d7350475bc)
19b50d902SRodney W. Grimes /*-
29b50d902SRodney W. Grimes  * Copyright (c) 1990, 1993
39b50d902SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
49b50d902SRodney W. Grimes  *
59b50d902SRodney W. Grimes  * This code is derived from software contributed to Berkeley by
69b50d902SRodney W. Grimes  * John B. Roll Jr.
79b50d902SRodney W. Grimes  *
89b50d902SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
99b50d902SRodney W. Grimes  * modification, are permitted provided that the following conditions
109b50d902SRodney W. Grimes  * are met:
119b50d902SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
129b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
139b50d902SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
149b50d902SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
159b50d902SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16*fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
179b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
189b50d902SRodney W. Grimes  *    without specific prior written permission.
199b50d902SRodney W. Grimes  *
209b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
219b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
229b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
239b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
249b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
259b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
269b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
279b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
289b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
299b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
309b50d902SRodney W. Grimes  * SUCH DAMAGE.
31fc17b349SJuli Mallett  *
32fc17b349SJuli Mallett  * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $
339b50d902SRodney W. Grimes  */
349b50d902SRodney W. Grimes 
358eb2a3deSDavid E. O'Brien #if 0
36d7a43b24SJuli Mallett #ifndef lint
37d7a43b24SJuli Mallett static const char copyright[] =
38d7a43b24SJuli Mallett "@(#) Copyright (c) 1990, 1993\n\
39d7a43b24SJuli Mallett 	The Regents of the University of California.  All rights reserved.\n";
40d7a43b24SJuli Mallett #endif /* not lint */
41d7a43b24SJuli Mallett 
42d7a43b24SJuli Mallett #ifndef lint
43d7a43b24SJuli Mallett static char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
44d7a43b24SJuli Mallett #endif /* not lint */
45d7a43b24SJuli Mallett #endif
4651883012SMike Barcroft #include <sys/cdefs.h>
4751883012SMike Barcroft __FBSDID("$FreeBSD$");
4851883012SMike Barcroft 
49e896b4a4SAllan Jude #include <sys/types.h>
509b50d902SRodney W. Grimes #include <sys/wait.h>
51e896b4a4SAllan Jude #include <sys/time.h>
52e896b4a4SAllan Jude #include <sys/limits.h>
53e896b4a4SAllan Jude #include <sys/resource.h>
54a51024e2SPhilippe Charnier #include <err.h>
558ad749a4SDag-Erling Smørgrav #include <errno.h>
5698186e89SMaxime Henrion #include <fcntl.h>
57305e39f4SJuli Mallett #include <langinfo.h>
58305e39f4SJuli Mallett #include <locale.h>
59305e39f4SJuli Mallett #include <paths.h>
60305e39f4SJuli Mallett #include <regex.h>
619b50d902SRodney W. Grimes #include <stdio.h>
629b50d902SRodney W. Grimes #include <stdlib.h>
639b50d902SRodney W. Grimes #include <string.h>
649b50d902SRodney W. Grimes #include <unistd.h>
6516b07a33SMark Murray 
669b50d902SRodney W. Grimes #include "pathnames.h"
679b50d902SRodney W. Grimes 
681926d4aaSJuli Mallett static void	parse_input(int, char *[]);
691926d4aaSJuli Mallett static void	prerun(int, char *[]);
70305e39f4SJuli Mallett static int	prompt(void);
71fc17b349SJuli Mallett static void	run(char **);
7273385ac6SJuli Mallett static void	usage(void);
73fc17b349SJuli Mallett void		strnsubst(char **, const char *, const char *, size_t);
7492095ab6SStephen McKay static pid_t	xwait(int block, int *status);
7570945890SJilles Tjoelker static void	xexit(const char *, const int);
76330d23f5STim J. Robbins static void	waitchildren(const char *, int);
7792095ab6SStephen McKay static void	pids_init(void);
7892095ab6SStephen McKay static int	pids_empty(void);
7992095ab6SStephen McKay static int	pids_full(void);
8092095ab6SStephen McKay static void	pids_add(pid_t pid);
8192095ab6SStephen McKay static int	pids_remove(pid_t pid);
8292095ab6SStephen McKay static int	findslot(pid_t pid);
8392095ab6SStephen McKay static int	findfreeslot(void);
8492095ab6SStephen McKay static void	clearslot(int slot);
859b50d902SRodney W. Grimes 
8616b07a33SMark Murray static char echo[] = _PATH_ECHO;
878eb2a3deSDavid E. O'Brien static char **av, **bxp, **ep, **endxp, **xp;
8891045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr;
8991045075SJuli Mallett static const char *eofstr;
9098186e89SMaxime Henrion static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag;
91ba084f6aSJuli Mallett static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag;
92330d23f5STim J. Robbins static int curprocs, maxprocs;
9392095ab6SStephen McKay static pid_t *childpids;
94330d23f5STim J. Robbins 
95330d23f5STim J. Robbins static volatile int childerr;
9616b07a33SMark Murray 
9799a84ce1STim J. Robbins extern char **environ;
9873385ac6SJuli Mallett 
99a51024e2SPhilippe Charnier int
1001926d4aaSJuli Mallett main(int argc, char *argv[])
1019b50d902SRodney W. Grimes {
102a3e5bc4fSJoseph Koshy 	long arg_max;
10391045075SJuli Mallett 	int ch, Jflag, nargs, nflag, nline;
104fc17b349SJuli Mallett 	size_t linelen;
105e896b4a4SAllan Jude 	struct rlimit rl;
1063c675167SJuli Mallett 	char *endptr;
107cffa7aa6SBaptiste Daroussin 	const char *errstr;
1089b50d902SRodney W. Grimes 
109fc17b349SJuli Mallett 	inpline = replstr = NULL;
11091045075SJuli Mallett 	ep = environ;
111fc17b349SJuli Mallett 	eofstr = "";
11291045075SJuli Mallett 	Jflag = nflag = 0;
1138d904f15SDima Dorfman 
114f58b94cbSTim J. Robbins 	(void)setlocale(LC_ALL, "");
115305e39f4SJuli Mallett 
1169b50d902SRodney W. Grimes 	/*
1179b50d902SRodney W. Grimes 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
1189b50d902SRodney W. Grimes 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
1199b50d902SRodney W. Grimes 	 * that the smallest argument is 2 bytes in length, this means that
1209b50d902SRodney W. Grimes 	 * the number of arguments is limited to:
1219b50d902SRodney W. Grimes 	 *
1229b50d902SRodney W. Grimes 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
1239b50d902SRodney W. Grimes 	 *
1249b50d902SRodney W. Grimes 	 * We arbitrarily limit the number of arguments to 5000.  This is
1259b50d902SRodney W. Grimes 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
1269b50d902SRodney W. Grimes 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
1279b50d902SRodney W. Grimes 	 * probably not worthwhile.
1289b50d902SRodney W. Grimes 	 */
1299b50d902SRodney W. Grimes 	nargs = 5000;
130a3e5bc4fSJoseph Koshy 	if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
131a3e5bc4fSJoseph Koshy 		errx(1, "sysconf(_SC_ARG_MAX) failed");
132a3e5bc4fSJoseph Koshy 	nline = arg_max - 4 * 1024;
13391045075SJuli Mallett 	while (*ep != NULL) {
134e5009da0SSatoshi Asami 		/* 1 byte for each '\0' */
135e5009da0SSatoshi Asami 		nline -= strlen(*ep++) + 1 + sizeof(*ep);
136e5009da0SSatoshi Asami 	}
137330d23f5STim J. Robbins 	maxprocs = 1;
138ba084f6aSJuli Mallett 	while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:S:s:rtx")) != -1)
1399b50d902SRodney W. Grimes 		switch (ch) {
140fc17b349SJuli Mallett 		case 'E':
141fc17b349SJuli Mallett 			eofstr = optarg;
142fc17b349SJuli Mallett 			break;
143fc17b349SJuli Mallett 		case 'I':
1446ea89183SJuli Mallett 			Jflag = 0;
145fc17b349SJuli Mallett 			Iflag = 1;
1464f49da74SJuli Mallett 			Lflag = 1;
147fc17b349SJuli Mallett 			replstr = optarg;
148fc17b349SJuli Mallett 			break;
1498d904f15SDima Dorfman 		case 'J':
1506ea89183SJuli Mallett 			Iflag = 0;
151b50a7286SJuli Mallett 			Jflag = 1;
1528d904f15SDima Dorfman 			replstr = optarg;
1538d904f15SDima Dorfman 			break;
154fc17b349SJuli Mallett 		case 'L':
155cffa7aa6SBaptiste Daroussin 			Lflag = strtonum(optarg, 0, INT_MAX, &errstr);
156cffa7aa6SBaptiste Daroussin 			if (errstr)
157cffa7aa6SBaptiste Daroussin 				errx(1, "-L %s: %s", optarg, errstr);
158fc17b349SJuli Mallett 			break;
1599b50d902SRodney W. Grimes 		case 'n':
1609b50d902SRodney W. Grimes 			nflag = 1;
161cffa7aa6SBaptiste Daroussin 			nargs = strtonum(optarg, 1, INT_MAX, &errstr);
162cffa7aa6SBaptiste Daroussin 			if (errstr)
163cffa7aa6SBaptiste Daroussin 				errx(1, "-n %s: %s", optarg, errstr);
1649b50d902SRodney W. Grimes 			break;
16598186e89SMaxime Henrion 		case 'o':
16698186e89SMaxime Henrion 			oflag = 1;
16798186e89SMaxime Henrion 			break;
168330d23f5STim J. Robbins 		case 'P':
169c782f6f0SAllan Jude 			maxprocs = strtonum(optarg, 0, INT_MAX, &errstr);
170cffa7aa6SBaptiste Daroussin 			if (errstr)
171cffa7aa6SBaptiste Daroussin 				errx(1, "-P %s: %s", optarg, errstr);
172e896b4a4SAllan Jude 			if (getrlimit(RLIMIT_NPROC, &rl) != 0)
173e896b4a4SAllan Jude 				errx(1, "getrlimit failed");
174e896b4a4SAllan Jude 			if (maxprocs == 0 || maxprocs > rl.rlim_cur)
175e896b4a4SAllan Jude 				maxprocs = rl.rlim_cur;
176330d23f5STim J. Robbins 			break;
177fc17b349SJuli Mallett 		case 'p':
178fc17b349SJuli Mallett 			pflag = 1;
179fc17b349SJuli Mallett 			break;
180b50a7286SJuli Mallett 		case 'R':
1813c675167SJuli Mallett 			Rflag = strtol(optarg, &endptr, 10);
1823c675167SJuli Mallett 			if (*endptr != '\0')
1833c675167SJuli Mallett 				errx(1, "replacements must be a number");
184b50a7286SJuli Mallett 			break;
1852d14e0e5SDag-Erling Smørgrav 		case 'r':
1862d14e0e5SDag-Erling Smørgrav 			/* GNU compatibility */
1872d14e0e5SDag-Erling Smørgrav 			break;
188ba084f6aSJuli Mallett 		case 'S':
189ba084f6aSJuli Mallett 			Sflag = strtoul(optarg, &endptr, 10);
190ba084f6aSJuli Mallett 			if (*endptr != '\0')
191ba084f6aSJuli Mallett 				errx(1, "replsize must be a number");
192ba084f6aSJuli Mallett 			break;
1939b50d902SRodney W. Grimes 		case 's':
194cffa7aa6SBaptiste Daroussin 			nline = strtonum(optarg, 0, INT_MAX, &errstr);
195cffa7aa6SBaptiste Daroussin 			if (errstr)
196cffa7aa6SBaptiste Daroussin 				errx(1, "-s %s: %s", optarg, errstr);
1979b50d902SRodney W. Grimes 			break;
1989b50d902SRodney W. Grimes 		case 't':
1999b50d902SRodney W. Grimes 			tflag = 1;
2009b50d902SRodney W. Grimes 			break;
2019b50d902SRodney W. Grimes 		case 'x':
2029b50d902SRodney W. Grimes 			xflag = 1;
2039b50d902SRodney W. Grimes 			break;
204d9198881SWarner Losh 		case '0':
205d9198881SWarner Losh 			zflag = 1;
206d9198881SWarner Losh 			break;
2079b50d902SRodney W. Grimes 		case '?':
2089b50d902SRodney W. Grimes 		default:
2099b50d902SRodney W. Grimes 			usage();
2109b50d902SRodney W. Grimes 	}
2119b50d902SRodney W. Grimes 	argc -= optind;
2129b50d902SRodney W. Grimes 	argv += optind;
2139b50d902SRodney W. Grimes 
2146ea89183SJuli Mallett 	if (!Iflag && Rflag)
2156ea89183SJuli Mallett 		usage();
216ba084f6aSJuli Mallett 	if (!Iflag && Sflag)
217ba084f6aSJuli Mallett 		usage();
2186ea89183SJuli Mallett 	if (Iflag && !Rflag)
2196ea89183SJuli Mallett 		Rflag = 5;
220ba084f6aSJuli Mallett 	if (Iflag && !Sflag)
221ba084f6aSJuli Mallett 		Sflag = 255;
2229b50d902SRodney W. Grimes 	if (xflag && !nflag)
2239b50d902SRodney W. Grimes 		usage();
2244f49da74SJuli Mallett 	if (Iflag || Lflag)
225fc17b349SJuli Mallett 		xflag = 1;
226fc17b349SJuli Mallett 	if (replstr != NULL && *replstr == '\0')
227fc17b349SJuli Mallett 		errx(1, "replstr may not be empty");
2289b50d902SRodney W. Grimes 
22992095ab6SStephen McKay 	pids_init();
23092095ab6SStephen McKay 
2319b50d902SRodney W. Grimes 	/*
2329b50d902SRodney W. Grimes 	 * Allocate pointers for the utility name, the utility arguments,
2339b50d902SRodney W. Grimes 	 * the maximum arguments to be read from stdin and the trailing
2349b50d902SRodney W. Grimes 	 * NULL.
2359b50d902SRodney W. Grimes 	 */
236fc17b349SJuli Mallett 	linelen = 1 + argc + nargs + 1;
237c9e1c304SUlrich Spörlein 	if ((av = bxp = malloc(linelen * sizeof(char *))) == NULL)
23891045075SJuli Mallett 		errx(1, "malloc failed");
2399b50d902SRodney W. Grimes 
2409b50d902SRodney W. Grimes 	/*
2419b50d902SRodney W. Grimes 	 * Use the user's name for the utility as argv[0], just like the
2429b50d902SRodney W. Grimes 	 * shell.  Echo is the default.  Set up pointers for the user's
2439b50d902SRodney W. Grimes 	 * arguments.
2449b50d902SRodney W. Grimes 	 */
2453dca1afcSJuli Mallett 	if (*argv == NULL)
2469bf450b8SJuli Mallett 		cnt = strlen(*bxp++ = echo);
2479b50d902SRodney W. Grimes 	else {
2489b50d902SRodney W. Grimes 		do {
249b50a7286SJuli Mallett 			if (Jflag && strcmp(*argv, replstr) == 0) {
25091045075SJuli Mallett 				char **avj;
2518d904f15SDima Dorfman 				jfound = 1;
2528d904f15SDima Dorfman 				argv++;
2538d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
2548d904f15SDima Dorfman 					cnt += strlen(*avj) + 1;
2558d904f15SDima Dorfman 				break;
2568d904f15SDima Dorfman 			}
2579b50d902SRodney W. Grimes 			cnt += strlen(*bxp++ = *argv) + 1;
2583dca1afcSJuli Mallett 		} while (*++argv != NULL);
2599b50d902SRodney W. Grimes 	}
2609b50d902SRodney W. Grimes 
2619b50d902SRodney W. Grimes 	/*
2629b50d902SRodney W. Grimes 	 * Set up begin/end/traversing pointers into the array.  The -n
2639b50d902SRodney W. Grimes 	 * count doesn't include the trailing NULL pointer, so the malloc
2649b50d902SRodney W. Grimes 	 * added in an extra slot.
2659b50d902SRodney W. Grimes 	 */
2668eb2a3deSDavid E. O'Brien 	endxp = (xp = bxp) + nargs;
2679b50d902SRodney W. Grimes 
2689b50d902SRodney W. Grimes 	/*
2699b50d902SRodney W. Grimes 	 * Allocate buffer space for the arguments read from stdin and the
2709b50d902SRodney W. Grimes 	 * trailing NULL.  Buffer space is defined as the default or specified
2719b50d902SRodney W. Grimes 	 * space, minus the length of the utility name and arguments.  Set up
2729b50d902SRodney W. Grimes 	 * begin/end/traversing pointers into the array.  The -s count does
2739b50d902SRodney W. Grimes 	 * include the trailing NULL, so the malloc didn't add in an extra
2749b50d902SRodney W. Grimes 	 * slot.
2759b50d902SRodney W. Grimes 	 */
2769b50d902SRodney W. Grimes 	nline -= cnt;
2779b50d902SRodney W. Grimes 	if (nline <= 0)
278a51024e2SPhilippe Charnier 		errx(1, "insufficient space for command");
2799b50d902SRodney W. Grimes 
2809bf450b8SJuli Mallett 	if ((bbp = malloc((size_t)(nline + 1))) == NULL)
28191045075SJuli Mallett 		errx(1, "malloc failed");
2829b50d902SRodney W. Grimes 	ebp = (argp = p = bbp) + nline - 1;
28391045075SJuli Mallett 	for (;;)
28491045075SJuli Mallett 		parse_input(argc, argv);
28591045075SJuli Mallett }
2869b50d902SRodney W. Grimes 
28791045075SJuli Mallett static void
2881926d4aaSJuli Mallett parse_input(int argc, char *argv[])
28991045075SJuli Mallett {
29091045075SJuli Mallett 	int ch, foundeof;
29191045075SJuli Mallett 	char **avj;
29291045075SJuli Mallett 
29391045075SJuli Mallett 	foundeof = 0;
29491045075SJuli Mallett 
2959b50d902SRodney W. Grimes 	switch (ch = getchar()) {
2969b50d902SRodney W. Grimes 	case EOF:
2979b50d902SRodney W. Grimes 		/* No arguments since last exec. */
29870945890SJilles Tjoelker 		if (p == bbp)
29970945890SJilles Tjoelker 			xexit(*av, rval);
3009b50d902SRodney W. Grimes 		goto arg1;
3019b50d902SRodney W. Grimes 	case ' ':
3029b50d902SRodney W. Grimes 	case '\t':
3039b50d902SRodney W. Grimes 		/* Quotes escape tabs and spaces. */
304d9198881SWarner Losh 		if (insingle || indouble || zflag)
3059b50d902SRodney W. Grimes 			goto addch;
3069b50d902SRodney W. Grimes 		goto arg2;
307d9198881SWarner Losh 	case '\0':
30846793db9SGarance A Drosehn 		if (zflag) {
30946793db9SGarance A Drosehn 			/*
31046793db9SGarance A Drosehn 			 * Increment 'count', so that nulls will be treated
31146793db9SGarance A Drosehn 			 * as end-of-line, as well as end-of-argument.  This
31246793db9SGarance A Drosehn 			 * is needed so -0 works properly with -I and -L.
31346793db9SGarance A Drosehn 			 */
31446793db9SGarance A Drosehn 			count++;
315d9198881SWarner Losh 			goto arg2;
31646793db9SGarance A Drosehn 		}
317d9198881SWarner Losh 		goto addch;
3189b50d902SRodney W. Grimes 	case '\n':
319d9198881SWarner Losh 		if (zflag)
320d9198881SWarner Losh 			goto addch;
32146793db9SGarance A Drosehn 		count++;	    /* Indicate end-of-line (used by -L) */
322d9198881SWarner Losh 
3239b50d902SRodney W. Grimes 		/* Quotes do not escape newlines. */
32470945890SJilles Tjoelker arg1:		if (insingle || indouble) {
32570945890SJilles Tjoelker 			warnx("unterminated quote");
32670945890SJilles Tjoelker 			xexit(*av, 1);
32770945890SJilles Tjoelker 		}
328ef9866beSJean-Marc Zucconi arg2:
329fc17b349SJuli Mallett 		foundeof = *eofstr != '\0' &&
3304aeb6382SJuli Mallett 		    strncmp(argp, eofstr, p - argp) == 0;
331fc17b349SJuli Mallett 
332ef9866beSJean-Marc Zucconi 		/* Do not make empty args unless they are quoted */
333fc17b349SJuli Mallett 		if ((argp != p || wasquoted) && !foundeof) {
334ef9866beSJean-Marc Zucconi 			*p++ = '\0';
3359b50d902SRodney W. Grimes 			*xp++ = argp;
336fc17b349SJuli Mallett 			if (Iflag) {
337fc17b349SJuli Mallett 				size_t curlen;
338b9b03ba0SJuli Mallett 
339b9b03ba0SJuli Mallett 				if (inpline == NULL)
340fc17b349SJuli Mallett 					curlen = 0;
341fc17b349SJuli Mallett 				else {
3421925cb24SJuli Mallett 					/*
3431925cb24SJuli Mallett 					 * If this string is not zero
3441925cb24SJuli Mallett 					 * length, append a space for
3452b239dd1SJens Schweikhardt 					 * separation before the next
3461925cb24SJuli Mallett 					 * argument.
3471925cb24SJuli Mallett 					 */
348b9b03ba0SJuli Mallett 					if ((curlen = strlen(inpline)))
349fc17b349SJuli Mallett 						strcat(inpline, " ");
350fc17b349SJuli Mallett 				}
351fc17b349SJuli Mallett 				curlen++;
3521925cb24SJuli Mallett 				/*
3531925cb24SJuli Mallett 				 * Allocate enough to hold what we will
3545eb40323SJuli Mallett 				 * be holding in a second, and to append
3551925cb24SJuli Mallett 				 * a space next time through, if we have
3561925cb24SJuli Mallett 				 * to.
3571925cb24SJuli Mallett 				 */
358b9b03ba0SJuli Mallett 				inpline = realloc(inpline, curlen + 2 +
359b9b03ba0SJuli Mallett 				    strlen(argp));
36070945890SJilles Tjoelker 				if (inpline == NULL) {
36170945890SJilles Tjoelker 					warnx("realloc failed");
36270945890SJilles Tjoelker 					xexit(*av, 1);
36370945890SJilles Tjoelker 				}
364fc17b349SJuli Mallett 				if (curlen == 1)
365fc17b349SJuli Mallett 					strcpy(inpline, argp);
366fc17b349SJuli Mallett 				else
367fc17b349SJuli Mallett 					strcat(inpline, argp);
368fc17b349SJuli Mallett 			}
369ef9866beSJean-Marc Zucconi 		}
3709b50d902SRodney W. Grimes 
3719b50d902SRodney W. Grimes 		/*
3729b50d902SRodney W. Grimes 		 * If max'd out on args or buffer, or reached EOF,
3739b50d902SRodney W. Grimes 		 * run the command.  If xflag and max'd out on buffer
3741925cb24SJuli Mallett 		 * but not on args, object.  Having reached the limit
3751925cb24SJuli Mallett 		 * of input lines, as specified by -L is the same as
3761925cb24SJuli Mallett 		 * maxing out on arguments.
3779b50d902SRodney W. Grimes 		 */
3788eb2a3deSDavid E. O'Brien 		if (xp == endxp || p > ebp || ch == EOF ||
3795eb40323SJuli Mallett 		    (Lflag <= count && xflag) || foundeof) {
38070945890SJilles Tjoelker 			if (xflag && xp != endxp && p > ebp) {
38170945890SJilles Tjoelker 				warnx("insufficient space for arguments");
38270945890SJilles Tjoelker 				xexit(*av, 1);
38370945890SJilles Tjoelker 			}
3848d904f15SDima Dorfman 			if (jfound) {
3858d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
3868d904f15SDima Dorfman 					*xp++ = *avj;
3878d904f15SDima Dorfman 			}
38891045075SJuli Mallett 			prerun(argc, av);
38970945890SJilles Tjoelker 			if (ch == EOF || foundeof)
39070945890SJilles Tjoelker 				xexit(*av, rval);
3919b50d902SRodney W. Grimes 			p = bbp;
3929b50d902SRodney W. Grimes 			xp = bxp;
393fc17b349SJuli Mallett 			count = 0;
394ef9866beSJean-Marc Zucconi 		}
3959b50d902SRodney W. Grimes 		argp = p;
396ef9866beSJean-Marc Zucconi 		wasquoted = 0;
3979b50d902SRodney W. Grimes 		break;
3989b50d902SRodney W. Grimes 	case '\'':
399d9198881SWarner Losh 		if (indouble || zflag)
4009b50d902SRodney W. Grimes 			goto addch;
4019b50d902SRodney W. Grimes 		insingle = !insingle;
402ef9866beSJean-Marc Zucconi 		wasquoted = 1;
4039b50d902SRodney W. Grimes 		break;
4049b50d902SRodney W. Grimes 	case '"':
405d9198881SWarner Losh 		if (insingle || zflag)
4069b50d902SRodney W. Grimes 			goto addch;
4079b50d902SRodney W. Grimes 		indouble = !indouble;
408ef9866beSJean-Marc Zucconi 		wasquoted = 1;
4099b50d902SRodney W. Grimes 		break;
4109b50d902SRodney W. Grimes 	case '\\':
411d9198881SWarner Losh 		if (zflag)
412d9198881SWarner Losh 			goto addch;
4139b50d902SRodney W. Grimes 		/* Backslash escapes anything, is escaped by quotes. */
41470945890SJilles Tjoelker 		if (!insingle && !indouble && (ch = getchar()) == EOF) {
41570945890SJilles Tjoelker 			warnx("backslash at EOF");
41670945890SJilles Tjoelker 			xexit(*av, 1);
41770945890SJilles Tjoelker 		}
4189b50d902SRodney W. Grimes 		/* FALLTHROUGH */
4199b50d902SRodney W. Grimes 	default:
4209b50d902SRodney W. Grimes addch:		if (p < ebp) {
4219b50d902SRodney W. Grimes 			*p++ = ch;
4229b50d902SRodney W. Grimes 			break;
4239b50d902SRodney W. Grimes 		}
4249b50d902SRodney W. Grimes 
4259b50d902SRodney W. Grimes 		/* If only one argument, not enough buffer space. */
42670945890SJilles Tjoelker 		if (bxp == xp) {
42770945890SJilles Tjoelker 			warnx("insufficient space for argument");
42870945890SJilles Tjoelker 			xexit(*av, 1);
42970945890SJilles Tjoelker 		}
4309b50d902SRodney W. Grimes 		/* Didn't hit argument limit, so if xflag object. */
43170945890SJilles Tjoelker 		if (xflag) {
43270945890SJilles Tjoelker 			warnx("insufficient space for arguments");
43370945890SJilles Tjoelker 			xexit(*av, 1);
43470945890SJilles Tjoelker 		}
4359b50d902SRodney W. Grimes 
4368d904f15SDima Dorfman 		if (jfound) {
4378d904f15SDima Dorfman 			for (avj = argv; *avj; avj++)
4388d904f15SDima Dorfman 				*xp++ = *avj;
4398d904f15SDima Dorfman 		}
44091045075SJuli Mallett 		prerun(argc, av);
4419b50d902SRodney W. Grimes 		xp = bxp;
4429b50d902SRodney W. Grimes 		cnt = ebp - argp;
443fc17b349SJuli Mallett 		memcpy(bbp, argp, (size_t)cnt);
4449b50d902SRodney W. Grimes 		p = (argp = bbp) + cnt;
4459b50d902SRodney W. Grimes 		*p++ = ch;
4469b50d902SRodney W. Grimes 		break;
4479b50d902SRodney W. Grimes 	}
4489b50d902SRodney W. Grimes }
4499b50d902SRodney W. Grimes 
450263dc775SJuli Mallett /*
451263dc775SJuli Mallett  * Do things necessary before run()'ing, such as -I substitution,
452263dc775SJuli Mallett  * and then call run().
453263dc775SJuli Mallett  */
454263dc775SJuli Mallett static void
4551926d4aaSJuli Mallett prerun(int argc, char *argv[])
456263dc775SJuli Mallett {
457263dc775SJuli Mallett 	char **tmp, **tmp2, **avj;
45891045075SJuli Mallett 	int repls;
45991045075SJuli Mallett 
46091045075SJuli Mallett 	repls = Rflag;
461263dc775SJuli Mallett 
462be70f7d4SJuli Mallett 	if (argc == 0 || repls == 0) {
463263dc775SJuli Mallett 		*xp = NULL;
464263dc775SJuli Mallett 		run(argv);
465263dc775SJuli Mallett 		return;
466263dc775SJuli Mallett 	}
467263dc775SJuli Mallett 
468263dc775SJuli Mallett 	avj = argv;
469263dc775SJuli Mallett 
470263dc775SJuli Mallett 	/*
471263dc775SJuli Mallett 	 * Allocate memory to hold the argument list, and
472263dc775SJuli Mallett 	 * a NULL at the tail.
473263dc775SJuli Mallett 	 */
474c9e1c304SUlrich Spörlein 	tmp = malloc((argc + 1) * sizeof(char *));
47570945890SJilles Tjoelker 	if (tmp == NULL) {
47670945890SJilles Tjoelker 		warnx("malloc failed");
47770945890SJilles Tjoelker 		xexit(*argv, 1);
47870945890SJilles Tjoelker 	}
479263dc775SJuli Mallett 	tmp2 = tmp;
480263dc775SJuli Mallett 
481263dc775SJuli Mallett 	/*
482263dc775SJuli Mallett 	 * Save the first argument and iterate over it, we
483263dc775SJuli Mallett 	 * cannot do strnsubst() to it.
484263dc775SJuli Mallett 	 */
48570945890SJilles Tjoelker 	if ((*tmp++ = strdup(*avj++)) == NULL) {
48670945890SJilles Tjoelker 		warnx("strdup failed");
48770945890SJilles Tjoelker 		xexit(*argv, 1);
48870945890SJilles Tjoelker 	}
489263dc775SJuli Mallett 
490263dc775SJuli Mallett 	/*
491263dc775SJuli Mallett 	 * For each argument to utility, if we have not used up
492263dc775SJuli Mallett 	 * the number of replacements we are allowed to do, and
4932b239dd1SJens Schweikhardt 	 * if the argument contains at least one occurrence of
494263dc775SJuli Mallett 	 * replstr, call strnsubst(), else just save the string.
495263dc775SJuli Mallett 	 * Iterations over elements of avj and tmp are done
496263dc775SJuli Mallett 	 * where appropriate.
497263dc775SJuli Mallett 	 */
498263dc775SJuli Mallett 	while (--argc) {
499263dc775SJuli Mallett 		*tmp = *avj++;
500263dc775SJuli Mallett 		if (repls && strstr(*tmp, replstr) != NULL) {
501ba084f6aSJuli Mallett 			strnsubst(tmp++, replstr, inpline, (size_t)Sflag);
5023c675167SJuli Mallett 			if (repls > 0)
503263dc775SJuli Mallett 				repls--;
504263dc775SJuli Mallett 		} else {
50570945890SJilles Tjoelker 			if ((*tmp = strdup(*tmp)) == NULL) {
50670945890SJilles Tjoelker 				warnx("strdup failed");
50770945890SJilles Tjoelker 				xexit(*argv, 1);
50870945890SJilles Tjoelker 			}
509263dc775SJuli Mallett 			tmp++;
510263dc775SJuli Mallett 		}
511263dc775SJuli Mallett 	}
512263dc775SJuli Mallett 
513263dc775SJuli Mallett 	/*
514263dc775SJuli Mallett 	 * Run it.
515263dc775SJuli Mallett 	 */
516b6594dbaSJuli Mallett 	*tmp = NULL;
517263dc775SJuli Mallett 	run(tmp2);
518263dc775SJuli Mallett 
519263dc775SJuli Mallett 	/*
520263dc775SJuli Mallett 	 * Walk from the tail to the head, free along the way.
521263dc775SJuli Mallett 	 */
522263dc775SJuli Mallett 	for (; tmp2 != tmp; tmp--)
523263dc775SJuli Mallett 		free(*tmp);
524263dc775SJuli Mallett 	/*
525263dc775SJuli Mallett 	 * Now free the list itself.
526263dc775SJuli Mallett 	 */
527263dc775SJuli Mallett 	free(tmp2);
528263dc775SJuli Mallett 
529263dc775SJuli Mallett 	/*
530986d829bSJuli Mallett 	 * Free the input line buffer, if we have one.
531263dc775SJuli Mallett 	 */
53251f7a48bSJuli Mallett 	if (inpline != NULL) {
53391045075SJuli Mallett 		free(inpline);
53451f7a48bSJuli Mallett 		inpline = NULL;
53551f7a48bSJuli Mallett 	}
536263dc775SJuli Mallett }
537263dc775SJuli Mallett 
538fc17b349SJuli Mallett static void
53973385ac6SJuli Mallett run(char **argv)
5409b50d902SRodney W. Grimes {
5419b50d902SRodney W. Grimes 	pid_t pid;
5420792992cSMaxime Henrion 	int fd;
543330d23f5STim J. Robbins 	char **avec;
5449b50d902SRodney W. Grimes 
545305e39f4SJuli Mallett 	/*
546305e39f4SJuli Mallett 	 * If the user wants to be notified of each command before it is
547305e39f4SJuli Mallett 	 * executed, notify them.  If they want the notification to be
548305e39f4SJuli Mallett 	 * followed by a prompt, then prompt them.
549305e39f4SJuli Mallett 	 */
550fc17b349SJuli Mallett 	if (tflag || pflag) {
5519b50d902SRodney W. Grimes 		(void)fprintf(stderr, "%s", *argv);
55291045075SJuli Mallett 		for (avec = argv + 1; *avec != NULL; ++avec)
55391045075SJuli Mallett 			(void)fprintf(stderr, " %s", *avec);
554305e39f4SJuli Mallett 		/*
555305e39f4SJuli Mallett 		 * If the user has asked to be prompted, do so.
556305e39f4SJuli Mallett 		 */
557305e39f4SJuli Mallett 		if (pflag)
558305e39f4SJuli Mallett 			/*
559305e39f4SJuli Mallett 			 * If they asked not to exec, return without execution
560305e39f4SJuli Mallett 			 * but if they asked to, go to the execution.  If we
561305e39f4SJuli Mallett 			 * could not open their tty, break the switch and drop
562305e39f4SJuli Mallett 			 * back to -t behaviour.
563305e39f4SJuli Mallett 			 */
564305e39f4SJuli Mallett 			switch (prompt()) {
565305e39f4SJuli Mallett 			case 0:
566fc17b349SJuli Mallett 				return;
567305e39f4SJuli Mallett 			case 1:
568305e39f4SJuli Mallett 				goto exec;
569305e39f4SJuli Mallett 			case 2:
570305e39f4SJuli Mallett 				break;
571305e39f4SJuli Mallett 			}
5729b50d902SRodney W. Grimes 		(void)fprintf(stderr, "\n");
5739b50d902SRodney W. Grimes 		(void)fflush(stderr);
5749b50d902SRodney W. Grimes 	}
575305e39f4SJuli Mallett exec:
5768ad749a4SDag-Erling Smørgrav 	childerr = 0;
5778ad749a4SDag-Erling Smørgrav 	switch (pid = vfork()) {
5789b50d902SRodney W. Grimes 	case -1:
57970945890SJilles Tjoelker 		warn("vfork");
58070945890SJilles Tjoelker 		xexit(*argv, 1);
5819b50d902SRodney W. Grimes 	case 0:
582cec1ba8cSMaxime Henrion 		if (oflag) {
5830792992cSMaxime Henrion 			if ((fd = open(_PATH_TTY, O_RDONLY)) == -1)
5840792992cSMaxime Henrion 				err(1, "can't open /dev/tty");
585cec1ba8cSMaxime Henrion 		} else {
5860792992cSMaxime Henrion 			fd = open(_PATH_DEVNULL, O_RDONLY);
5870792992cSMaxime Henrion 		}
5880792992cSMaxime Henrion 		if (fd > STDIN_FILENO) {
5890792992cSMaxime Henrion 			if (dup2(fd, STDIN_FILENO) != 0)
5900792992cSMaxime Henrion 				err(1, "can't dup2 to stdin");
5910792992cSMaxime Henrion 			close(fd);
59298186e89SMaxime Henrion 		}
5939b50d902SRodney W. Grimes 		execvp(argv[0], argv);
5948ad749a4SDag-Erling Smørgrav 		childerr = errno;
5959b50d902SRodney W. Grimes 		_exit(1);
5969b50d902SRodney W. Grimes 	}
59792095ab6SStephen McKay 	pids_add(pid);
598330d23f5STim J. Robbins 	waitchildren(*argv, 0);
599330d23f5STim J. Robbins }
600330d23f5STim J. Robbins 
60192095ab6SStephen McKay /*
60292095ab6SStephen McKay  * Wait for a tracked child to exit and return its pid and exit status.
60392095ab6SStephen McKay  *
60492095ab6SStephen McKay  * Ignores (discards) all untracked child processes.
60592095ab6SStephen McKay  * Returns -1 and sets errno to ECHILD if no tracked children exist.
60692095ab6SStephen McKay  * If block is set, waits indefinitely for a child process to exit.
60792095ab6SStephen McKay  * If block is not set and no children have exited, returns 0 immediately.
60892095ab6SStephen McKay  */
60992095ab6SStephen McKay static pid_t
61092095ab6SStephen McKay xwait(int block, int *status) {
61192095ab6SStephen McKay 	pid_t pid;
61292095ab6SStephen McKay 
61392095ab6SStephen McKay 	if (pids_empty()) {
61492095ab6SStephen McKay 		errno = ECHILD;
61529afe03fSStephen McKay 		return (-1);
61692095ab6SStephen McKay 	}
61792095ab6SStephen McKay 
61892095ab6SStephen McKay 	while ((pid = waitpid(-1, status, block ? 0 : WNOHANG)) > 0)
61992095ab6SStephen McKay 		if (pids_remove(pid))
62092095ab6SStephen McKay 			break;
62192095ab6SStephen McKay 
62229afe03fSStephen McKay 	return (pid);
62392095ab6SStephen McKay }
62492095ab6SStephen McKay 
625330d23f5STim J. Robbins static void
62670945890SJilles Tjoelker xexit(const char *name, const int exit_code) {
62770945890SJilles Tjoelker 	waitchildren(name, 1);
62870945890SJilles Tjoelker 	exit(exit_code);
62970945890SJilles Tjoelker }
63070945890SJilles Tjoelker 
63170945890SJilles Tjoelker static void
632330d23f5STim J. Robbins waitchildren(const char *name, int waitall)
633330d23f5STim J. Robbins {
634330d23f5STim J. Robbins 	pid_t pid;
635330d23f5STim J. Robbins 	int status;
63670945890SJilles Tjoelker 	int cause_exit = 0;
637330d23f5STim J. Robbins 
63892095ab6SStephen McKay 	while ((pid = xwait(waitall || pids_full(), &status)) > 0) {
63970945890SJilles Tjoelker 		/*
64070945890SJilles Tjoelker 		 * If we couldn't invoke the utility or if utility exited
64170945890SJilles Tjoelker 		 * because of a signal or with a value of 255, warn (per
64270945890SJilles Tjoelker 		 * POSIX), and then wait until all other children have
64370945890SJilles Tjoelker 		 * exited before exiting 1-125. POSIX requires us to stop
64470945890SJilles Tjoelker 		 * reading if child exits because of a signal or with 255,
64570945890SJilles Tjoelker 		 * but it does not require us to exit immediately; waiting
64670945890SJilles Tjoelker 		 * is preferable to orphaning.
64770945890SJilles Tjoelker 		 */
64870945890SJilles Tjoelker 		if (childerr != 0 && cause_exit == 0) {
649330d23f5STim J. Robbins 			errno = childerr;
65070945890SJilles Tjoelker 			waitall = 1;
65170945890SJilles Tjoelker 			cause_exit = ENOENT ? 127 : 126;
65270945890SJilles Tjoelker 			warn("%s", name);
65370945890SJilles Tjoelker 		} else if (WIFSIGNALED(status)) {
65470945890SJilles Tjoelker 			waitall = cause_exit = 1;
65570945890SJilles Tjoelker 			warnx("%s: terminated with signal %d; aborting",
65623583c4fSJilles Tjoelker 			    name, WTERMSIG(status));
65770945890SJilles Tjoelker 		} else if (WEXITSTATUS(status) == 255) {
65870945890SJilles Tjoelker 			waitall = cause_exit = 1;
65970945890SJilles Tjoelker 			warnx("%s: exited with status 255; aborting", name);
66070945890SJilles Tjoelker 		} else if (WEXITSTATUS(status))
6619b50d902SRodney W. Grimes  			rval = 1;
6629b50d902SRodney W. Grimes 	}
66392095ab6SStephen McKay 
66470945890SJilles Tjoelker  	if (cause_exit)
66570945890SJilles Tjoelker 		exit(cause_exit);
666330d23f5STim J. Robbins 	if (pid == -1 && errno != ECHILD)
66792095ab6SStephen McKay 		err(1, "waitpid");
66892095ab6SStephen McKay }
66992095ab6SStephen McKay 
67092095ab6SStephen McKay #define	NOPID	(0)
67192095ab6SStephen McKay 
67292095ab6SStephen McKay static void
67329afe03fSStephen McKay pids_init(void)
67492095ab6SStephen McKay {
67592095ab6SStephen McKay 	int i;
67692095ab6SStephen McKay 
67792095ab6SStephen McKay 	if ((childpids = malloc(maxprocs * sizeof(*childpids))) == NULL)
67892095ab6SStephen McKay 		errx(1, "malloc failed");
67992095ab6SStephen McKay 
68092095ab6SStephen McKay 	for (i = 0; i < maxprocs; i++)
68192095ab6SStephen McKay 		clearslot(i);
68292095ab6SStephen McKay }
68392095ab6SStephen McKay 
68492095ab6SStephen McKay static int
68529afe03fSStephen McKay pids_empty(void)
68692095ab6SStephen McKay {
687a274be77SEitan Adler 
68829afe03fSStephen McKay 	return (curprocs == 0);
68992095ab6SStephen McKay }
69092095ab6SStephen McKay 
69192095ab6SStephen McKay static int
69229afe03fSStephen McKay pids_full(void)
69392095ab6SStephen McKay {
694a274be77SEitan Adler 
69529afe03fSStephen McKay 	return (curprocs >= maxprocs);
69692095ab6SStephen McKay }
69792095ab6SStephen McKay 
69892095ab6SStephen McKay static void
69992095ab6SStephen McKay pids_add(pid_t pid)
70092095ab6SStephen McKay {
70192095ab6SStephen McKay 	int slot;
70292095ab6SStephen McKay 
70392095ab6SStephen McKay 	slot = findfreeslot();
70492095ab6SStephen McKay 	childpids[slot] = pid;
70592095ab6SStephen McKay 	curprocs++;
70692095ab6SStephen McKay }
70792095ab6SStephen McKay 
70892095ab6SStephen McKay static int
70992095ab6SStephen McKay pids_remove(pid_t pid)
71092095ab6SStephen McKay {
71192095ab6SStephen McKay 	int slot;
71292095ab6SStephen McKay 
71392095ab6SStephen McKay 	if ((slot = findslot(pid)) < 0)
71429afe03fSStephen McKay 		return (0);
71592095ab6SStephen McKay 
71692095ab6SStephen McKay 	clearslot(slot);
71792095ab6SStephen McKay 	curprocs--;
71829afe03fSStephen McKay 	return (1);
71992095ab6SStephen McKay }
72092095ab6SStephen McKay 
72192095ab6SStephen McKay static int
72229afe03fSStephen McKay findfreeslot(void)
72392095ab6SStephen McKay {
72492095ab6SStephen McKay 	int slot;
72592095ab6SStephen McKay 
72692095ab6SStephen McKay 	if ((slot = findslot(NOPID)) < 0)
72792095ab6SStephen McKay 		errx(1, "internal error: no free pid slot");
72829afe03fSStephen McKay 	return (slot);
72992095ab6SStephen McKay }
73092095ab6SStephen McKay 
73192095ab6SStephen McKay static int
73292095ab6SStephen McKay findslot(pid_t pid)
73392095ab6SStephen McKay {
73492095ab6SStephen McKay 	int slot;
73592095ab6SStephen McKay 
73692095ab6SStephen McKay 	for (slot = 0; slot < maxprocs; slot++)
73792095ab6SStephen McKay 		if (childpids[slot] == pid)
73829afe03fSStephen McKay 			return (slot);
73929afe03fSStephen McKay 	return (-1);
74092095ab6SStephen McKay }
74192095ab6SStephen McKay 
74292095ab6SStephen McKay static void
74392095ab6SStephen McKay clearslot(int slot)
74492095ab6SStephen McKay {
745a274be77SEitan Adler 
74692095ab6SStephen McKay 	childpids[slot] = NOPID;
747330d23f5STim J. Robbins }
7489b50d902SRodney W. Grimes 
749305e39f4SJuli Mallett /*
750305e39f4SJuli Mallett  * Prompt the user about running a command.
751305e39f4SJuli Mallett  */
752305e39f4SJuli Mallett static int
753305e39f4SJuli Mallett prompt(void)
754305e39f4SJuli Mallett {
755305e39f4SJuli Mallett 	regex_t cre;
756305e39f4SJuli Mallett 	size_t rsize;
757305e39f4SJuli Mallett 	int match;
758305e39f4SJuli Mallett 	char *response;
759305e39f4SJuli Mallett 	FILE *ttyfp;
760305e39f4SJuli Mallett 
761305e39f4SJuli Mallett 	if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
762305e39f4SJuli Mallett 		return (2);	/* Indicate that the TTY failed to open. */
763305e39f4SJuli Mallett 	(void)fprintf(stderr, "?...");
764305e39f4SJuli Mallett 	(void)fflush(stderr);
765305e39f4SJuli Mallett 	if ((response = fgetln(ttyfp, &rsize)) == NULL ||
76630aaff11SWarner Losh 	    regcomp(&cre, nl_langinfo(YESEXPR), REG_BASIC) != 0) {
767305e39f4SJuli Mallett 		(void)fclose(ttyfp);
768305e39f4SJuli Mallett 		return (0);
769305e39f4SJuli Mallett 	}
770b65bc267SJuli Mallett 	response[rsize - 1] = '\0';
771305e39f4SJuli Mallett 	match = regexec(&cre, response, 0, NULL, 0);
772305e39f4SJuli Mallett 	(void)fclose(ttyfp);
773305e39f4SJuli Mallett 	regfree(&cre);
774305e39f4SJuli Mallett 	return (match == 0);
775305e39f4SJuli Mallett }
776305e39f4SJuli Mallett 
777a51024e2SPhilippe Charnier static void
77873385ac6SJuli Mallett usage(void)
7799b50d902SRodney W. Grimes {
780a274be77SEitan Adler 
7818d904f15SDima Dorfman 	fprintf(stderr,
782ba084f6aSJuli Mallett "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]\n"
783ba084f6aSJuli Mallett "             [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]\n"
784ba084f6aSJuli Mallett "             [-s size] [utility [argument ...]]\n");
7859b50d902SRodney W. Grimes 	exit(1);
7869b50d902SRodney W. Grimes }
787