xref: /freebsd/usr.bin/xargs/xargs.c (revision 2d14e0e54781c41c49b2b09a56c8d1474d2fc3c9)
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.
169b50d902SRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
179b50d902SRodney W. Grimes  *    must display the following acknowledgement:
189b50d902SRodney W. Grimes  *	This product includes software developed by the University of
199b50d902SRodney W. Grimes  *	California, Berkeley and its contributors.
209b50d902SRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
219b50d902SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
229b50d902SRodney W. Grimes  *    without specific prior written permission.
239b50d902SRodney W. Grimes  *
249b50d902SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
259b50d902SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
269b50d902SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
279b50d902SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
289b50d902SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
299b50d902SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
309b50d902SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
319b50d902SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
329b50d902SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
339b50d902SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
349b50d902SRodney W. Grimes  * SUCH DAMAGE.
35fc17b349SJuli Mallett  *
36fc17b349SJuli Mallett  * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $
379b50d902SRodney W. Grimes  */
389b50d902SRodney W. Grimes 
398eb2a3deSDavid E. O'Brien #if 0
40d7a43b24SJuli Mallett #ifndef lint
41d7a43b24SJuli Mallett static const char copyright[] =
42d7a43b24SJuli Mallett "@(#) Copyright (c) 1990, 1993\n\
43d7a43b24SJuli Mallett 	The Regents of the University of California.  All rights reserved.\n";
44d7a43b24SJuli Mallett #endif /* not lint */
45d7a43b24SJuli Mallett 
46d7a43b24SJuli Mallett #ifndef lint
47d7a43b24SJuli Mallett static char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
48d7a43b24SJuli Mallett #endif /* not lint */
49d7a43b24SJuli Mallett #endif
5051883012SMike Barcroft #include <sys/cdefs.h>
5151883012SMike Barcroft __FBSDID("$FreeBSD$");
5251883012SMike Barcroft 
5340c6b893SRuslan Ermilov #include <sys/param.h>
549b50d902SRodney W. Grimes #include <sys/wait.h>
5516b07a33SMark Murray 
56a51024e2SPhilippe Charnier #include <err.h>
578ad749a4SDag-Erling Smørgrav #include <errno.h>
5898186e89SMaxime Henrion #include <fcntl.h>
59305e39f4SJuli Mallett #include <langinfo.h>
60305e39f4SJuli Mallett #include <locale.h>
61305e39f4SJuli Mallett #include <paths.h>
62305e39f4SJuli Mallett #include <regex.h>
639b50d902SRodney W. Grimes #include <stdio.h>
649b50d902SRodney W. Grimes #include <stdlib.h>
659b50d902SRodney W. Grimes #include <string.h>
669b50d902SRodney W. Grimes #include <unistd.h>
6716b07a33SMark Murray 
689b50d902SRodney W. Grimes #include "pathnames.h"
699b50d902SRodney W. Grimes 
701926d4aaSJuli Mallett static void	parse_input(int, char *[]);
711926d4aaSJuli Mallett static void	prerun(int, char *[]);
72305e39f4SJuli Mallett static int	prompt(void);
73fc17b349SJuli Mallett static void	run(char **);
7473385ac6SJuli Mallett static void	usage(void);
75fc17b349SJuli Mallett void		strnsubst(char **, const char *, const char *, size_t);
76330d23f5STim J. Robbins static void	waitchildren(const char *, int);
779b50d902SRodney W. Grimes 
7816b07a33SMark Murray static char echo[] = _PATH_ECHO;
798eb2a3deSDavid E. O'Brien static char **av, **bxp, **ep, **endxp, **xp;
8091045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr;
8191045075SJuli Mallett static const char *eofstr;
8298186e89SMaxime Henrion static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag;
8391045075SJuli Mallett static int cnt, Iflag, jfound, Lflag, wasquoted, xflag;
84330d23f5STim J. Robbins static int curprocs, maxprocs;
85330d23f5STim J. Robbins 
86330d23f5STim J. Robbins static volatile int childerr;
8716b07a33SMark Murray 
8899a84ce1STim J. Robbins extern char **environ;
8973385ac6SJuli Mallett 
90a51024e2SPhilippe Charnier int
911926d4aaSJuli Mallett main(int argc, char *argv[])
929b50d902SRodney W. Grimes {
93a3e5bc4fSJoseph Koshy 	long arg_max;
9491045075SJuli Mallett 	int ch, Jflag, nargs, nflag, nline;
95fc17b349SJuli Mallett 	size_t linelen;
963c675167SJuli Mallett 	char *endptr;
979b50d902SRodney W. Grimes 
98fc17b349SJuli Mallett 	inpline = replstr = NULL;
9991045075SJuli Mallett 	ep = environ;
100fc17b349SJuli Mallett 	eofstr = "";
10191045075SJuli Mallett 	Jflag = nflag = 0;
1028d904f15SDima Dorfman 
103f58b94cbSTim J. Robbins 	(void)setlocale(LC_ALL, "");
104305e39f4SJuli Mallett 
1059b50d902SRodney W. Grimes 	/*
1069b50d902SRodney W. Grimes 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
1079b50d902SRodney W. Grimes 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
1089b50d902SRodney W. Grimes 	 * that the smallest argument is 2 bytes in length, this means that
1099b50d902SRodney W. Grimes 	 * the number of arguments is limited to:
1109b50d902SRodney W. Grimes 	 *
1119b50d902SRodney W. Grimes 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
1129b50d902SRodney W. Grimes 	 *
1139b50d902SRodney W. Grimes 	 * We arbitrarily limit the number of arguments to 5000.  This is
1149b50d902SRodney W. Grimes 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
1159b50d902SRodney W. Grimes 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
1169b50d902SRodney W. Grimes 	 * probably not worthwhile.
1179b50d902SRodney W. Grimes 	 */
1189b50d902SRodney W. Grimes 	nargs = 5000;
119a3e5bc4fSJoseph Koshy 	if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
120a3e5bc4fSJoseph Koshy 		errx(1, "sysconf(_SC_ARG_MAX) failed");
121a3e5bc4fSJoseph Koshy 	nline = arg_max - 4 * 1024;
12291045075SJuli Mallett 	while (*ep != NULL) {
123e5009da0SSatoshi Asami 		/* 1 byte for each '\0' */
124e5009da0SSatoshi Asami 		nline -= strlen(*ep++) + 1 + sizeof(*ep);
125e5009da0SSatoshi Asami 	}
126330d23f5STim J. Robbins 	maxprocs = 1;
1272d14e0e5SDag-Erling Smørgrav 	while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:s:rtx")) != -1)
1289b50d902SRodney W. Grimes 		switch(ch) {
129fc17b349SJuli Mallett 		case 'E':
130fc17b349SJuli Mallett 			eofstr = optarg;
131fc17b349SJuli Mallett 			break;
132fc17b349SJuli Mallett 		case 'I':
1336ea89183SJuli Mallett 			Jflag = 0;
134fc17b349SJuli Mallett 			Iflag = 1;
1354f49da74SJuli Mallett 			Lflag = 1;
136fc17b349SJuli Mallett 			replstr = optarg;
137fc17b349SJuli Mallett 			break;
1388d904f15SDima Dorfman 		case 'J':
1396ea89183SJuli Mallett 			Iflag = 0;
140b50a7286SJuli Mallett 			Jflag = 1;
1418d904f15SDima Dorfman 			replstr = optarg;
1428d904f15SDima Dorfman 			break;
143fc17b349SJuli Mallett 		case 'L':
1444f49da74SJuli Mallett 			Lflag = atoi(optarg);
145fc17b349SJuli Mallett 			break;
1469b50d902SRodney W. Grimes 		case 'n':
1479b50d902SRodney W. Grimes 			nflag = 1;
1489b50d902SRodney W. Grimes 			if ((nargs = atoi(optarg)) <= 0)
149a51024e2SPhilippe Charnier 				errx(1, "illegal argument count");
1509b50d902SRodney W. Grimes 			break;
15198186e89SMaxime Henrion 		case 'o':
15298186e89SMaxime Henrion 			oflag = 1;
15398186e89SMaxime Henrion 			break;
154330d23f5STim J. Robbins 		case 'P':
155330d23f5STim J. Robbins 			if ((maxprocs = atoi(optarg)) <= 0)
156330d23f5STim J. Robbins 				errx(1, "max. processes must be >0");
157330d23f5STim J. Robbins 			break;
158fc17b349SJuli Mallett 		case 'p':
159fc17b349SJuli Mallett 			pflag = 1;
160fc17b349SJuli Mallett 			break;
161b50a7286SJuli Mallett 		case 'R':
1623c675167SJuli Mallett 			Rflag = strtol(optarg, &endptr, 10);
1633c675167SJuli Mallett 			if (*endptr != '\0')
1643c675167SJuli Mallett 				errx(1, "replacements must be a number");
165b50a7286SJuli Mallett 			break;
1662d14e0e5SDag-Erling Smørgrav 		case 'r':
1672d14e0e5SDag-Erling Smørgrav 			/* GNU compatibility */
1682d14e0e5SDag-Erling Smørgrav 			break;
1699b50d902SRodney W. Grimes 		case 's':
1709b50d902SRodney W. Grimes 			nline = atoi(optarg);
1719b50d902SRodney W. Grimes 			break;
1729b50d902SRodney W. Grimes 		case 't':
1739b50d902SRodney W. Grimes 			tflag = 1;
1749b50d902SRodney W. Grimes 			break;
1759b50d902SRodney W. Grimes 		case 'x':
1769b50d902SRodney W. Grimes 			xflag = 1;
1779b50d902SRodney W. Grimes 			break;
178d9198881SWarner Losh 		case '0':
179d9198881SWarner Losh 			zflag = 1;
180d9198881SWarner Losh 			break;
1819b50d902SRodney W. Grimes 		case '?':
1829b50d902SRodney W. Grimes 		default:
1839b50d902SRodney W. Grimes 			usage();
1849b50d902SRodney W. Grimes 	}
1859b50d902SRodney W. Grimes 	argc -= optind;
1869b50d902SRodney W. Grimes 	argv += optind;
1879b50d902SRodney W. Grimes 
1886ea89183SJuli Mallett 	if (!Iflag && Rflag)
1896ea89183SJuli Mallett 		usage();
1906ea89183SJuli Mallett 	if (Iflag && !Rflag)
1916ea89183SJuli Mallett 		Rflag = 5;
1929b50d902SRodney W. Grimes 	if (xflag && !nflag)
1939b50d902SRodney W. Grimes 		usage();
1944f49da74SJuli Mallett 	if (Iflag || Lflag)
195fc17b349SJuli Mallett 		xflag = 1;
196fc17b349SJuli Mallett 	if (replstr != NULL && *replstr == '\0')
197fc17b349SJuli Mallett 		errx(1, "replstr may not be empty");
1989b50d902SRodney W. Grimes 
1999b50d902SRodney W. Grimes 	/*
2009b50d902SRodney W. Grimes 	 * Allocate pointers for the utility name, the utility arguments,
2019b50d902SRodney W. Grimes 	 * the maximum arguments to be read from stdin and the trailing
2029b50d902SRodney W. Grimes 	 * NULL.
2039b50d902SRodney W. Grimes 	 */
204fc17b349SJuli Mallett 	linelen = 1 + argc + nargs + 1;
2050fa5e8dcSJuli Mallett 	if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL)
20691045075SJuli Mallett 		errx(1, "malloc failed");
2079b50d902SRodney W. Grimes 
2089b50d902SRodney W. Grimes 	/*
2099b50d902SRodney W. Grimes 	 * Use the user's name for the utility as argv[0], just like the
2109b50d902SRodney W. Grimes 	 * shell.  Echo is the default.  Set up pointers for the user's
2119b50d902SRodney W. Grimes 	 * arguments.
2129b50d902SRodney W. Grimes 	 */
2133dca1afcSJuli Mallett 	if (*argv == NULL)
2149bf450b8SJuli Mallett 		cnt = strlen(*bxp++ = echo);
2159b50d902SRodney W. Grimes 	else {
2169b50d902SRodney W. Grimes 		do {
217b50a7286SJuli Mallett 			if (Jflag && strcmp(*argv, replstr) == 0) {
21891045075SJuli Mallett 				char **avj;
2198d904f15SDima Dorfman 				jfound = 1;
2208d904f15SDima Dorfman 				argv++;
2218d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
2228d904f15SDima Dorfman 					cnt += strlen(*avj) + 1;
2238d904f15SDima Dorfman 				break;
2248d904f15SDima Dorfman 			}
2259b50d902SRodney W. Grimes 			cnt += strlen(*bxp++ = *argv) + 1;
2263dca1afcSJuli Mallett 		} while (*++argv != NULL);
2279b50d902SRodney W. Grimes 	}
2289b50d902SRodney W. Grimes 
2299b50d902SRodney W. Grimes 	/*
2309b50d902SRodney W. Grimes 	 * Set up begin/end/traversing pointers into the array.  The -n
2319b50d902SRodney W. Grimes 	 * count doesn't include the trailing NULL pointer, so the malloc
2329b50d902SRodney W. Grimes 	 * added in an extra slot.
2339b50d902SRodney W. Grimes 	 */
2348eb2a3deSDavid E. O'Brien 	endxp = (xp = bxp) + nargs;
2359b50d902SRodney W. Grimes 
2369b50d902SRodney W. Grimes 	/*
2379b50d902SRodney W. Grimes 	 * Allocate buffer space for the arguments read from stdin and the
2389b50d902SRodney W. Grimes 	 * trailing NULL.  Buffer space is defined as the default or specified
2399b50d902SRodney W. Grimes 	 * space, minus the length of the utility name and arguments.  Set up
2409b50d902SRodney W. Grimes 	 * begin/end/traversing pointers into the array.  The -s count does
2419b50d902SRodney W. Grimes 	 * include the trailing NULL, so the malloc didn't add in an extra
2429b50d902SRodney W. Grimes 	 * slot.
2439b50d902SRodney W. Grimes 	 */
2449b50d902SRodney W. Grimes 	nline -= cnt;
2459b50d902SRodney W. Grimes 	if (nline <= 0)
246a51024e2SPhilippe Charnier 		errx(1, "insufficient space for command");
2479b50d902SRodney W. Grimes 
2489bf450b8SJuli Mallett 	if ((bbp = malloc((size_t)(nline + 1))) == NULL)
24991045075SJuli Mallett 		errx(1, "malloc failed");
2509b50d902SRodney W. Grimes 	ebp = (argp = p = bbp) + nline - 1;
25191045075SJuli Mallett 	for (;;)
25291045075SJuli Mallett 		parse_input(argc, argv);
25391045075SJuli Mallett }
2549b50d902SRodney W. Grimes 
25591045075SJuli Mallett static void
2561926d4aaSJuli Mallett parse_input(int argc, char *argv[])
25791045075SJuli Mallett {
25891045075SJuli Mallett 	int ch, foundeof;
25991045075SJuli Mallett 	char **avj;
26091045075SJuli Mallett 
26191045075SJuli Mallett 	foundeof = 0;
26291045075SJuli Mallett 
2639b50d902SRodney W. Grimes 	switch(ch = getchar()) {
2649b50d902SRodney W. Grimes 	case EOF:
2659b50d902SRodney W. Grimes 		/* No arguments since last exec. */
266330d23f5STim J. Robbins 		if (p == bbp) {
267330d23f5STim J. Robbins 			waitchildren(*argv, 1);
2689b50d902SRodney W. Grimes 			exit(rval);
269330d23f5STim J. Robbins 		}
2709b50d902SRodney W. Grimes 		goto arg1;
2719b50d902SRodney W. Grimes 	case ' ':
2729b50d902SRodney W. Grimes 	case '\t':
2739b50d902SRodney W. Grimes 		/* Quotes escape tabs and spaces. */
274d9198881SWarner Losh 		if (insingle || indouble || zflag)
2759b50d902SRodney W. Grimes 			goto addch;
2769b50d902SRodney W. Grimes 		goto arg2;
277d9198881SWarner Losh 	case '\0':
27846793db9SGarance A Drosehn 		if (zflag) {
27946793db9SGarance A Drosehn 			/*
28046793db9SGarance A Drosehn 			 * Increment 'count', so that nulls will be treated
28146793db9SGarance A Drosehn 			 * as end-of-line, as well as end-of-argument.  This
28246793db9SGarance A Drosehn 			 * is needed so -0 works properly with -I and -L.
28346793db9SGarance A Drosehn 			 */
28446793db9SGarance A Drosehn 			count++;
285d9198881SWarner Losh 			goto arg2;
28646793db9SGarance A Drosehn 		}
287d9198881SWarner Losh 		goto addch;
2889b50d902SRodney W. Grimes 	case '\n':
289d9198881SWarner Losh 		if (zflag)
290d9198881SWarner Losh 			goto addch;
29146793db9SGarance A Drosehn 		count++;	    /* Indicate end-of-line (used by -L) */
292d9198881SWarner Losh 
2939b50d902SRodney W. Grimes 		/* Quotes do not escape newlines. */
2949b50d902SRodney W. Grimes arg1:		if (insingle || indouble)
295a51024e2SPhilippe Charnier 			errx(1, "unterminated quote");
296ef9866beSJean-Marc Zucconi arg2:
297fc17b349SJuli Mallett 		foundeof = *eofstr != '\0' &&
298fc17b349SJuli Mallett 		    strcmp(argp, eofstr) == 0;
299fc17b349SJuli Mallett 
300ef9866beSJean-Marc Zucconi 		/* Do not make empty args unless they are quoted */
301fc17b349SJuli Mallett 		if ((argp != p || wasquoted) && !foundeof) {
302ef9866beSJean-Marc Zucconi 			*p++ = '\0';
3039b50d902SRodney W. Grimes 			*xp++ = argp;
304fc17b349SJuli Mallett 			if (Iflag) {
305fc17b349SJuli Mallett 				size_t curlen;
306b9b03ba0SJuli Mallett 
307b9b03ba0SJuli Mallett 				if (inpline == NULL)
308fc17b349SJuli Mallett 					curlen = 0;
309fc17b349SJuli Mallett 				else {
3101925cb24SJuli Mallett 					/*
3111925cb24SJuli Mallett 					 * If this string is not zero
3121925cb24SJuli Mallett 					 * length, append a space for
3132b239dd1SJens Schweikhardt 					 * separation before the next
3141925cb24SJuli Mallett 					 * argument.
3151925cb24SJuli Mallett 					 */
316b9b03ba0SJuli Mallett 					if ((curlen = strlen(inpline)))
317fc17b349SJuli Mallett 						strcat(inpline, " ");
318fc17b349SJuli Mallett 				}
319fc17b349SJuli Mallett 				curlen++;
3201925cb24SJuli Mallett 				/*
3211925cb24SJuli Mallett 				 * Allocate enough to hold what we will
3225eb40323SJuli Mallett 				 * be holding in a second, and to append
3231925cb24SJuli Mallett 				 * a space next time through, if we have
3241925cb24SJuli Mallett 				 * to.
3251925cb24SJuli Mallett 				 */
326b9b03ba0SJuli Mallett 				inpline = realloc(inpline, curlen + 2 +
327b9b03ba0SJuli Mallett 				    strlen(argp));
328fc17b349SJuli Mallett 				if (inpline == NULL)
32991045075SJuli Mallett 					errx(1, "realloc failed");
330fc17b349SJuli Mallett 				if (curlen == 1)
331fc17b349SJuli Mallett 					strcpy(inpline, argp);
332fc17b349SJuli Mallett 				else
333fc17b349SJuli Mallett 					strcat(inpline, argp);
334fc17b349SJuli Mallett 			}
335ef9866beSJean-Marc Zucconi 		}
3369b50d902SRodney W. Grimes 
3379b50d902SRodney W. Grimes 		/*
3389b50d902SRodney W. Grimes 		 * If max'd out on args or buffer, or reached EOF,
3399b50d902SRodney W. Grimes 		 * run the command.  If xflag and max'd out on buffer
3401925cb24SJuli Mallett 		 * but not on args, object.  Having reached the limit
3411925cb24SJuli Mallett 		 * of input lines, as specified by -L is the same as
3421925cb24SJuli Mallett 		 * maxing out on arguments.
3439b50d902SRodney W. Grimes 		 */
3448eb2a3deSDavid E. O'Brien 		if (xp == endxp || p > ebp || ch == EOF ||
3455eb40323SJuli Mallett 		    (Lflag <= count && xflag) || foundeof) {
3468eb2a3deSDavid E. O'Brien 			if (xflag && xp != endxp && p > ebp)
347a51024e2SPhilippe Charnier 				errx(1, "insufficient space for arguments");
3488d904f15SDima Dorfman 			if (jfound) {
3498d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
3508d904f15SDima Dorfman 					*xp++ = *avj;
3518d904f15SDima Dorfman 			}
35291045075SJuli Mallett 			prerun(argc, av);
353330d23f5STim J. Robbins 			if (ch == EOF || foundeof) {
354330d23f5STim J. Robbins 				waitchildren(*argv, 1);
3559b50d902SRodney W. Grimes 				exit(rval);
356330d23f5STim J. Robbins 			}
3579b50d902SRodney W. Grimes 			p = bbp;
3589b50d902SRodney W. Grimes 			xp = bxp;
359fc17b349SJuli Mallett 			count = 0;
360ef9866beSJean-Marc Zucconi 		}
3619b50d902SRodney W. Grimes 		argp = p;
362ef9866beSJean-Marc Zucconi 		wasquoted = 0;
3639b50d902SRodney W. Grimes 		break;
3649b50d902SRodney W. Grimes 	case '\'':
365d9198881SWarner Losh 		if (indouble || zflag)
3669b50d902SRodney W. Grimes 			goto addch;
3679b50d902SRodney W. Grimes 		insingle = !insingle;
368ef9866beSJean-Marc Zucconi 		wasquoted = 1;
3699b50d902SRodney W. Grimes 		break;
3709b50d902SRodney W. Grimes 	case '"':
371d9198881SWarner Losh 		if (insingle || zflag)
3729b50d902SRodney W. Grimes 			goto addch;
3739b50d902SRodney W. Grimes 		indouble = !indouble;
374ef9866beSJean-Marc Zucconi 		wasquoted = 1;
3759b50d902SRodney W. Grimes 		break;
3769b50d902SRodney W. Grimes 	case '\\':
377d9198881SWarner Losh 		if (zflag)
378d9198881SWarner Losh 			goto addch;
3799b50d902SRodney W. Grimes 		/* Backslash escapes anything, is escaped by quotes. */
3809b50d902SRodney W. Grimes 		if (!insingle && !indouble && (ch = getchar()) == EOF)
381a51024e2SPhilippe Charnier 			errx(1, "backslash at EOF");
3829b50d902SRodney W. Grimes 		/* FALLTHROUGH */
3839b50d902SRodney W. Grimes 	default:
3849b50d902SRodney W. Grimes addch:		if (p < ebp) {
3859b50d902SRodney W. Grimes 			*p++ = ch;
3869b50d902SRodney W. Grimes 			break;
3879b50d902SRodney W. Grimes 		}
3889b50d902SRodney W. Grimes 
3899b50d902SRodney W. Grimes 		/* If only one argument, not enough buffer space. */
3909b50d902SRodney W. Grimes 		if (bxp == xp)
391a51024e2SPhilippe Charnier 			errx(1, "insufficient space for argument");
3929b50d902SRodney W. Grimes 		/* Didn't hit argument limit, so if xflag object. */
3939b50d902SRodney W. Grimes 		if (xflag)
394a51024e2SPhilippe Charnier 			errx(1, "insufficient space for arguments");
3959b50d902SRodney W. Grimes 
3968d904f15SDima Dorfman 		if (jfound) {
3978d904f15SDima Dorfman 			for (avj = argv; *avj; avj++)
3988d904f15SDima Dorfman 				*xp++ = *avj;
3998d904f15SDima Dorfman 		}
40091045075SJuli Mallett 		prerun(argc, av);
4019b50d902SRodney W. Grimes 		xp = bxp;
4029b50d902SRodney W. Grimes 		cnt = ebp - argp;
403fc17b349SJuli Mallett 		memcpy(bbp, argp, (size_t)cnt);
4049b50d902SRodney W. Grimes 		p = (argp = bbp) + cnt;
4059b50d902SRodney W. Grimes 		*p++ = ch;
4069b50d902SRodney W. Grimes 		break;
4079b50d902SRodney W. Grimes 	}
4089b50d902SRodney W. Grimes }
4099b50d902SRodney W. Grimes 
410263dc775SJuli Mallett /*
411263dc775SJuli Mallett  * Do things necessary before run()'ing, such as -I substitution,
412263dc775SJuli Mallett  * and then call run().
413263dc775SJuli Mallett  */
414263dc775SJuli Mallett static void
4151926d4aaSJuli Mallett prerun(int argc, char *argv[])
416263dc775SJuli Mallett {
417263dc775SJuli Mallett 	char **tmp, **tmp2, **avj;
41891045075SJuli Mallett 	int repls;
41991045075SJuli Mallett 
42091045075SJuli Mallett 	repls = Rflag;
421263dc775SJuli Mallett 
422be70f7d4SJuli Mallett 	if (argc == 0 || repls == 0) {
423263dc775SJuli Mallett 		*xp = NULL;
424263dc775SJuli Mallett 		run(argv);
425263dc775SJuli Mallett 		return;
426263dc775SJuli Mallett 	}
427263dc775SJuli Mallett 
428263dc775SJuli Mallett 	avj = argv;
429263dc775SJuli Mallett 
430263dc775SJuli Mallett 	/*
431263dc775SJuli Mallett 	 * Allocate memory to hold the argument list, and
432263dc775SJuli Mallett 	 * a NULL at the tail.
433263dc775SJuli Mallett 	 */
434b6594dbaSJuli Mallett 	tmp = malloc((argc + 1) * sizeof(char**));
435263dc775SJuli Mallett 	if (tmp == NULL)
43691045075SJuli Mallett 		errx(1, "malloc failed");
437263dc775SJuli Mallett 	tmp2 = tmp;
438263dc775SJuli Mallett 
439263dc775SJuli Mallett 	/*
440263dc775SJuli Mallett 	 * Save the first argument and iterate over it, we
441263dc775SJuli Mallett 	 * cannot do strnsubst() to it.
442263dc775SJuli Mallett 	 */
443263dc775SJuli Mallett 	if ((*tmp++ = strdup(*avj++)) == NULL)
44491045075SJuli Mallett 		errx(1, "strdup failed");
445263dc775SJuli Mallett 
446263dc775SJuli Mallett 	/*
447263dc775SJuli Mallett 	 * For each argument to utility, if we have not used up
448263dc775SJuli Mallett 	 * the number of replacements we are allowed to do, and
4492b239dd1SJens Schweikhardt 	 * if the argument contains at least one occurrence of
450263dc775SJuli Mallett 	 * replstr, call strnsubst(), else just save the string.
451263dc775SJuli Mallett 	 * Iterations over elements of avj and tmp are done
452263dc775SJuli Mallett 	 * where appropriate.
453263dc775SJuli Mallett 	 */
454263dc775SJuli Mallett 	while (--argc) {
455263dc775SJuli Mallett 		*tmp = *avj++;
456263dc775SJuli Mallett 		if (repls && strstr(*tmp, replstr) != NULL) {
45791045075SJuli Mallett 			strnsubst(tmp++, replstr, inpline, (size_t)255);
4583c675167SJuli Mallett 			if (repls > 0)
459263dc775SJuli Mallett 				repls--;
460263dc775SJuli Mallett 		} else {
461263dc775SJuli Mallett 			if ((*tmp = strdup(*tmp)) == NULL)
46291045075SJuli Mallett 				errx(1, "strdup failed");
463263dc775SJuli Mallett 			tmp++;
464263dc775SJuli Mallett 		}
465263dc775SJuli Mallett 	}
466263dc775SJuli Mallett 
467263dc775SJuli Mallett 	/*
468263dc775SJuli Mallett 	 * Run it.
469263dc775SJuli Mallett 	 */
470b6594dbaSJuli Mallett 	*tmp = NULL;
471263dc775SJuli Mallett 	run(tmp2);
472263dc775SJuli Mallett 
473263dc775SJuli Mallett 	/*
474263dc775SJuli Mallett 	 * Walk from the tail to the head, free along the way.
475263dc775SJuli Mallett 	 */
476263dc775SJuli Mallett 	for (; tmp2 != tmp; tmp--)
477263dc775SJuli Mallett 		free(*tmp);
478263dc775SJuli Mallett 	/*
479263dc775SJuli Mallett 	 * Now free the list itself.
480263dc775SJuli Mallett 	 */
481263dc775SJuli Mallett 	free(tmp2);
482263dc775SJuli Mallett 
483263dc775SJuli Mallett 	/*
484986d829bSJuli Mallett 	 * Free the input line buffer, if we have one.
485263dc775SJuli Mallett 	 */
48651f7a48bSJuli Mallett 	if (inpline != NULL) {
48791045075SJuli Mallett 		free(inpline);
48851f7a48bSJuli Mallett 		inpline = NULL;
48951f7a48bSJuli Mallett 	}
490263dc775SJuli Mallett }
491263dc775SJuli Mallett 
492fc17b349SJuli Mallett static void
49373385ac6SJuli Mallett run(char **argv)
4949b50d902SRodney W. Grimes {
4959b50d902SRodney W. Grimes 	pid_t pid;
4960792992cSMaxime Henrion 	int fd;
497330d23f5STim J. Robbins 	char **avec;
4989b50d902SRodney W. Grimes 
499305e39f4SJuli Mallett 	/*
500305e39f4SJuli Mallett 	 * If the user wants to be notified of each command before it is
501305e39f4SJuli Mallett 	 * executed, notify them.  If they want the notification to be
502305e39f4SJuli Mallett 	 * followed by a prompt, then prompt them.
503305e39f4SJuli Mallett 	 */
504fc17b349SJuli Mallett 	if (tflag || pflag) {
5059b50d902SRodney W. Grimes 		(void)fprintf(stderr, "%s", *argv);
50691045075SJuli Mallett 		for (avec = argv + 1; *avec != NULL; ++avec)
50791045075SJuli Mallett 			(void)fprintf(stderr, " %s", *avec);
508305e39f4SJuli Mallett 		/*
509305e39f4SJuli Mallett 		 * If the user has asked to be prompted, do so.
510305e39f4SJuli Mallett 		 */
511305e39f4SJuli Mallett 		if (pflag)
512305e39f4SJuli Mallett 			/*
513305e39f4SJuli Mallett 			 * If they asked not to exec, return without execution
514305e39f4SJuli Mallett 			 * but if they asked to, go to the execution.  If we
515305e39f4SJuli Mallett 			 * could not open their tty, break the switch and drop
516305e39f4SJuli Mallett 			 * back to -t behaviour.
517305e39f4SJuli Mallett 			 */
518305e39f4SJuli Mallett 			switch (prompt()) {
519305e39f4SJuli Mallett 			case 0:
520fc17b349SJuli Mallett 				return;
521305e39f4SJuli Mallett 			case 1:
522305e39f4SJuli Mallett 				goto exec;
523305e39f4SJuli Mallett 			case 2:
524305e39f4SJuli Mallett 				break;
525305e39f4SJuli Mallett 			}
5269b50d902SRodney W. Grimes 		(void)fprintf(stderr, "\n");
5279b50d902SRodney W. Grimes 		(void)fflush(stderr);
5289b50d902SRodney W. Grimes 	}
529305e39f4SJuli Mallett exec:
5308ad749a4SDag-Erling Smørgrav 	childerr = 0;
5318ad749a4SDag-Erling Smørgrav 	switch(pid = vfork()) {
5329b50d902SRodney W. Grimes 	case -1:
5338ad749a4SDag-Erling Smørgrav 		err(1, "vfork");
5349b50d902SRodney W. Grimes 	case 0:
535cec1ba8cSMaxime Henrion 		if (oflag) {
5360792992cSMaxime Henrion 			if ((fd = open(_PATH_TTY, O_RDONLY)) == -1)
5370792992cSMaxime Henrion 				err(1, "can't open /dev/tty");
538cec1ba8cSMaxime Henrion 		} else {
5390792992cSMaxime Henrion 			fd = open(_PATH_DEVNULL, O_RDONLY);
5400792992cSMaxime Henrion 		}
5410792992cSMaxime Henrion 		if (fd > STDIN_FILENO) {
5420792992cSMaxime Henrion 			if (dup2(fd, STDIN_FILENO) != 0)
5430792992cSMaxime Henrion 				err(1, "can't dup2 to stdin");
5440792992cSMaxime Henrion 			close(fd);
54598186e89SMaxime Henrion 		}
5469b50d902SRodney W. Grimes 		execvp(argv[0], argv);
5478ad749a4SDag-Erling Smørgrav 		childerr = errno;
5489b50d902SRodney W. Grimes 		_exit(1);
5499b50d902SRodney W. Grimes 	}
550330d23f5STim J. Robbins 	curprocs++;
551330d23f5STim J. Robbins 	waitchildren(*argv, 0);
552330d23f5STim J. Robbins }
553330d23f5STim J. Robbins 
554330d23f5STim J. Robbins static void
555330d23f5STim J. Robbins waitchildren(const char *name, int waitall)
556330d23f5STim J. Robbins {
557330d23f5STim J. Robbins 	pid_t pid;
558330d23f5STim J. Robbins 	int status;
559330d23f5STim J. Robbins 
560004bd28eSJuli Mallett 	while ((pid = waitpid(-1, &status, !waitall && curprocs < maxprocs ?
561004bd28eSJuli Mallett 	    WNOHANG : 0)) > 0) {
562330d23f5STim J. Robbins 		curprocs--;
563fc17b349SJuli Mallett 		/* If we couldn't invoke the utility, exit. */
564330d23f5STim J. Robbins 		if (childerr != 0) {
565330d23f5STim J. Robbins 			errno = childerr;
566330d23f5STim J. Robbins 			err(errno == ENOENT ? 127 : 126, "%s", name);
567330d23f5STim J. Robbins 		}
568330d23f5STim J. Robbins 		/*
569330d23f5STim J. Robbins 		 * If utility signaled or exited with a value of 255,
570330d23f5STim J. Robbins 		 * exit 1-125.
571330d23f5STim J. Robbins 		 */
5729b50d902SRodney W. Grimes 		if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
5739b50d902SRodney W. Grimes 			exit(1);
5749b50d902SRodney W. Grimes 		if (WEXITSTATUS(status))
5759b50d902SRodney W. Grimes 			rval = 1;
5769b50d902SRodney W. Grimes 	}
577330d23f5STim J. Robbins 	if (pid == -1 && errno != ECHILD)
578330d23f5STim J. Robbins 		err(1, "wait3");
579330d23f5STim J. Robbins }
5809b50d902SRodney W. Grimes 
581305e39f4SJuli Mallett /*
582305e39f4SJuli Mallett  * Prompt the user about running a command.
583305e39f4SJuli Mallett  */
584305e39f4SJuli Mallett static int
585305e39f4SJuli Mallett prompt(void)
586305e39f4SJuli Mallett {
587305e39f4SJuli Mallett 	regex_t cre;
588305e39f4SJuli Mallett 	size_t rsize;
589305e39f4SJuli Mallett 	int match;
590305e39f4SJuli Mallett 	char *response;
591305e39f4SJuli Mallett 	FILE *ttyfp;
592305e39f4SJuli Mallett 
593305e39f4SJuli Mallett 	if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
594305e39f4SJuli Mallett 		return (2);	/* Indicate that the TTY failed to open. */
595305e39f4SJuli Mallett 	(void)fprintf(stderr, "?...");
596305e39f4SJuli Mallett 	(void)fflush(stderr);
597305e39f4SJuli Mallett 	if ((response = fgetln(ttyfp, &rsize)) == NULL ||
59830aaff11SWarner Losh 	    regcomp(&cre, nl_langinfo(YESEXPR), REG_BASIC) != 0) {
599305e39f4SJuli Mallett 		(void)fclose(ttyfp);
600305e39f4SJuli Mallett 		return (0);
601305e39f4SJuli Mallett 	}
602305e39f4SJuli Mallett 	match = regexec(&cre, response, 0, NULL, 0);
603305e39f4SJuli Mallett 	(void)fclose(ttyfp);
604305e39f4SJuli Mallett 	regfree(&cre);
605305e39f4SJuli Mallett 	return (match == 0);
606305e39f4SJuli Mallett }
607305e39f4SJuli Mallett 
608a51024e2SPhilippe Charnier static void
60973385ac6SJuli Mallett usage(void)
6109b50d902SRodney W. Grimes {
6118d904f15SDima Dorfman 	fprintf(stderr,
61298186e89SMaxime Henrion "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
61308f16c7aSJuli Mallett "             [-L number] [-n number [-x]] [-P maxprocs] [-s size]\n"
614330d23f5STim J. Robbins "             [utility [argument ...]]\n");
6159b50d902SRodney W. Grimes 	exit(1);
6169b50d902SRodney W. Grimes }
617