xref: /freebsd/usr.bin/xargs/xargs.c (revision 1926d4aaada34e7fd29016609f26d6bfa10f9af9)
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 
39d7a43b24SJuli Mallett #ifndef lint
40d7a43b24SJuli Mallett static const char copyright[] =
41d7a43b24SJuli Mallett "@(#) Copyright (c) 1990, 1993\n\
42d7a43b24SJuli Mallett 	The Regents of the University of California.  All rights reserved.\n";
43d7a43b24SJuli Mallett #endif /* not lint */
44d7a43b24SJuli Mallett 
45d7a43b24SJuli Mallett #if 0
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
50d7a43b24SJuli Mallett 
5151883012SMike Barcroft #include <sys/cdefs.h>
5251883012SMike Barcroft __FBSDID("$FreeBSD$");
5351883012SMike Barcroft 
549b50d902SRodney W. Grimes #include <sys/types.h>
559b50d902SRodney W. Grimes #include <sys/wait.h>
5616b07a33SMark Murray 
57a51024e2SPhilippe Charnier #include <err.h>
588ad749a4SDag-Erling Smørgrav #include <errno.h>
598e0a87c1SRuslan Ermilov #ifndef BOOTSTRAPPING
60305e39f4SJuli Mallett #include <langinfo.h>
618e0a87c1SRuslan Ermilov #endif
62305e39f4SJuli Mallett #include <locale.h>
63305e39f4SJuli Mallett #include <paths.h>
64305e39f4SJuli Mallett #include <regex.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);
77fc17b349SJuli Mallett void		strnsubst(char **, const char *, const char *, size_t);
789b50d902SRodney W. Grimes 
7916b07a33SMark Murray static char echo[] = _PATH_ECHO;
8091045075SJuli Mallett static char **av, **bxp, **ep, **exp, **xp;
8191045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr;
8291045075SJuli Mallett static const char *eofstr;
8391045075SJuli Mallett static int count, insingle, indouble, pflag, tflag, Rflag, rval, zflag;
8491045075SJuli Mallett static int cnt, Iflag, jfound, Lflag, wasquoted, xflag;
8516b07a33SMark Murray 
8673385ac6SJuli Mallett extern char *environ[];
8773385ac6SJuli Mallett 
88a51024e2SPhilippe Charnier int
891926d4aaSJuli Mallett main(int argc, char *argv[])
909b50d902SRodney W. Grimes {
91a3e5bc4fSJoseph Koshy 	long arg_max;
9291045075SJuli Mallett 	int ch, Jflag, nargs, nflag, nline;
93fc17b349SJuli Mallett 	size_t linelen;
949b50d902SRodney W. Grimes 
95fc17b349SJuli Mallett 	inpline = replstr = NULL;
9691045075SJuli Mallett 	ep = environ;
97fc17b349SJuli Mallett 	eofstr = "";
9891045075SJuli Mallett 	Jflag = nflag = 0;
998d904f15SDima Dorfman 
100305e39f4SJuli Mallett 	(void)setlocale(LC_MESSAGES, "");
101305e39f4SJuli Mallett 
1029b50d902SRodney W. Grimes 	/*
1039b50d902SRodney W. Grimes 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
1049b50d902SRodney W. Grimes 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
1059b50d902SRodney W. Grimes 	 * that the smallest argument is 2 bytes in length, this means that
1069b50d902SRodney W. Grimes 	 * the number of arguments is limited to:
1079b50d902SRodney W. Grimes 	 *
1089b50d902SRodney W. Grimes 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
1099b50d902SRodney W. Grimes 	 *
1109b50d902SRodney W. Grimes 	 * We arbitrarily limit the number of arguments to 5000.  This is
1119b50d902SRodney W. Grimes 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
1129b50d902SRodney W. Grimes 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
1139b50d902SRodney W. Grimes 	 * probably not worthwhile.
1149b50d902SRodney W. Grimes 	 */
1159b50d902SRodney W. Grimes 	nargs = 5000;
116a3e5bc4fSJoseph Koshy 	if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
117a3e5bc4fSJoseph Koshy 		errx(1, "sysconf(_SC_ARG_MAX) failed");
118a3e5bc4fSJoseph Koshy 	nline = arg_max - 4 * 1024;
11991045075SJuli Mallett 	while (*ep != NULL) {
120e5009da0SSatoshi Asami 		/* 1 byte for each '\0' */
121e5009da0SSatoshi Asami 		nline -= strlen(*ep++) + 1 + sizeof(*ep);
122e5009da0SSatoshi Asami 	}
123b50a7286SJuli Mallett 	while ((ch = getopt(argc, argv, "0E:I:J:L:n:pR:s:tx")) != -1)
1249b50d902SRodney W. Grimes 		switch(ch) {
125fc17b349SJuli Mallett 		case 'E':
126fc17b349SJuli Mallett 			eofstr = optarg;
127fc17b349SJuli Mallett 			break;
128fc17b349SJuli Mallett 		case 'I':
1296ea89183SJuli Mallett 			Jflag = 0;
130fc17b349SJuli Mallett 			Iflag = 1;
1314f49da74SJuli Mallett 			Lflag = 1;
132fc17b349SJuli Mallett 			replstr = optarg;
133fc17b349SJuli Mallett 			break;
1348d904f15SDima Dorfman 		case 'J':
1356ea89183SJuli Mallett 			Iflag = 0;
136b50a7286SJuli Mallett 			Jflag = 1;
1378d904f15SDima Dorfman 			replstr = optarg;
1388d904f15SDima Dorfman 			break;
139fc17b349SJuli Mallett 		case 'L':
1404f49da74SJuli Mallett 			Lflag = atoi(optarg);
141fc17b349SJuli Mallett 			break;
1429b50d902SRodney W. Grimes 		case 'n':
1439b50d902SRodney W. Grimes 			nflag = 1;
1449b50d902SRodney W. Grimes 			if ((nargs = atoi(optarg)) <= 0)
145a51024e2SPhilippe Charnier 				errx(1, "illegal argument count");
1469b50d902SRodney W. Grimes 			break;
147fc17b349SJuli Mallett 		case 'p':
148fc17b349SJuli Mallett 			pflag = 1;
149fc17b349SJuli Mallett 			break;
150b50a7286SJuli Mallett 		case 'R':
151b50a7286SJuli Mallett 			if ((Rflag = atoi(optarg)) <= 0)
152b50a7286SJuli Mallett 				errx(1, "illegal number of replacements");
153b50a7286SJuli Mallett 			break;
1549b50d902SRodney W. Grimes 		case 's':
1559b50d902SRodney W. Grimes 			nline = atoi(optarg);
1569b50d902SRodney W. Grimes 			break;
1579b50d902SRodney W. Grimes 		case 't':
1589b50d902SRodney W. Grimes 			tflag = 1;
1599b50d902SRodney W. Grimes 			break;
1609b50d902SRodney W. Grimes 		case 'x':
1619b50d902SRodney W. Grimes 			xflag = 1;
1629b50d902SRodney W. Grimes 			break;
163d9198881SWarner Losh 		case '0':
164d9198881SWarner Losh 			zflag = 1;
165d9198881SWarner Losh 			break;
1669b50d902SRodney W. Grimes 		case '?':
1679b50d902SRodney W. Grimes 		default:
1689b50d902SRodney W. Grimes 			usage();
1699b50d902SRodney W. Grimes 	}
1709b50d902SRodney W. Grimes 	argc -= optind;
1719b50d902SRodney W. Grimes 	argv += optind;
1729b50d902SRodney W. Grimes 
1736ea89183SJuli Mallett 	if (!Iflag && Rflag)
1746ea89183SJuli Mallett 		usage();
1756ea89183SJuli Mallett 	if (Iflag && !Rflag)
1766ea89183SJuli Mallett 		Rflag = 5;
1779b50d902SRodney W. Grimes 	if (xflag && !nflag)
1789b50d902SRodney W. Grimes 		usage();
1794f49da74SJuli Mallett 	if (Iflag || Lflag)
180fc17b349SJuli Mallett 		xflag = 1;
181fc17b349SJuli Mallett 	if (replstr != NULL && *replstr == '\0')
182fc17b349SJuli Mallett 		errx(1, "replstr may not be empty");
1839b50d902SRodney W. Grimes 
1849b50d902SRodney W. Grimes 	/*
1859b50d902SRodney W. Grimes 	 * Allocate pointers for the utility name, the utility arguments,
1869b50d902SRodney W. Grimes 	 * the maximum arguments to be read from stdin and the trailing
1879b50d902SRodney W. Grimes 	 * NULL.
1889b50d902SRodney W. Grimes 	 */
189fc17b349SJuli Mallett 	linelen = 1 + argc + nargs + 1;
1900fa5e8dcSJuli Mallett 	if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL)
19191045075SJuli Mallett 		errx(1, "malloc failed");
1929b50d902SRodney W. Grimes 
1939b50d902SRodney W. Grimes 	/*
1949b50d902SRodney W. Grimes 	 * Use the user's name for the utility as argv[0], just like the
1959b50d902SRodney W. Grimes 	 * shell.  Echo is the default.  Set up pointers for the user's
1969b50d902SRodney W. Grimes 	 * arguments.
1979b50d902SRodney W. Grimes 	 */
1983dca1afcSJuli Mallett 	if (*argv == NULL)
1999bf450b8SJuli Mallett 		cnt = strlen(*bxp++ = echo);
2009b50d902SRodney W. Grimes 	else {
2019b50d902SRodney W. Grimes 		do {
202b50a7286SJuli Mallett 			if (Jflag && strcmp(*argv, replstr) == 0) {
20391045075SJuli Mallett 				char **avj;
2048d904f15SDima Dorfman 				jfound = 1;
2058d904f15SDima Dorfman 				argv++;
2068d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
2078d904f15SDima Dorfman 					cnt += strlen(*avj) + 1;
2088d904f15SDima Dorfman 				break;
2098d904f15SDima Dorfman 			}
2109b50d902SRodney W. Grimes 			cnt += strlen(*bxp++ = *argv) + 1;
2113dca1afcSJuli Mallett 		} while (*++argv != NULL);
2129b50d902SRodney W. Grimes 	}
2139b50d902SRodney W. Grimes 
2149b50d902SRodney W. Grimes 	/*
2159b50d902SRodney W. Grimes 	 * Set up begin/end/traversing pointers into the array.  The -n
2169b50d902SRodney W. Grimes 	 * count doesn't include the trailing NULL pointer, so the malloc
2179b50d902SRodney W. Grimes 	 * added in an extra slot.
2189b50d902SRodney W. Grimes 	 */
2199b50d902SRodney W. Grimes 	exp = (xp = bxp) + nargs;
2209b50d902SRodney W. Grimes 
2219b50d902SRodney W. Grimes 	/*
2229b50d902SRodney W. Grimes 	 * Allocate buffer space for the arguments read from stdin and the
2239b50d902SRodney W. Grimes 	 * trailing NULL.  Buffer space is defined as the default or specified
2249b50d902SRodney W. Grimes 	 * space, minus the length of the utility name and arguments.  Set up
2259b50d902SRodney W. Grimes 	 * begin/end/traversing pointers into the array.  The -s count does
2269b50d902SRodney W. Grimes 	 * include the trailing NULL, so the malloc didn't add in an extra
2279b50d902SRodney W. Grimes 	 * slot.
2289b50d902SRodney W. Grimes 	 */
2299b50d902SRodney W. Grimes 	nline -= cnt;
2309b50d902SRodney W. Grimes 	if (nline <= 0)
231a51024e2SPhilippe Charnier 		errx(1, "insufficient space for command");
2329b50d902SRodney W. Grimes 
2339bf450b8SJuli Mallett 	if ((bbp = malloc((size_t)(nline + 1))) == NULL)
23491045075SJuli Mallett 		errx(1, "malloc failed");
2359b50d902SRodney W. Grimes 	ebp = (argp = p = bbp) + nline - 1;
23691045075SJuli Mallett 	for (;;)
23791045075SJuli Mallett 		parse_input(argc, argv);
23891045075SJuli Mallett }
2399b50d902SRodney W. Grimes 
24091045075SJuli Mallett static void
2411926d4aaSJuli Mallett parse_input(int argc, char *argv[])
24291045075SJuli Mallett {
24391045075SJuli Mallett 	int ch, foundeof;
24491045075SJuli Mallett 	char **avj;
24591045075SJuli Mallett 
24691045075SJuli Mallett 	foundeof = 0;
24791045075SJuli Mallett 
2489b50d902SRodney W. Grimes 	switch(ch = getchar()) {
2499b50d902SRodney W. Grimes 	case EOF:
2509b50d902SRodney W. Grimes 		/* No arguments since last exec. */
2519b50d902SRodney W. Grimes 		if (p == bbp)
2529b50d902SRodney W. Grimes 			exit(rval);
2539b50d902SRodney W. Grimes 		goto arg1;
2549b50d902SRodney W. Grimes 	case ' ':
2559b50d902SRodney W. Grimes 	case '\t':
2569b50d902SRodney W. Grimes 		/* Quotes escape tabs and spaces. */
257d9198881SWarner Losh 		if (insingle || indouble || zflag)
2589b50d902SRodney W. Grimes 			goto addch;
2599b50d902SRodney W. Grimes 		goto arg2;
260d9198881SWarner Losh 	case '\0':
261d9198881SWarner Losh 		if (zflag)
262d9198881SWarner Losh 			goto arg2;
263d9198881SWarner Losh 		goto addch;
2649b50d902SRodney W. Grimes 	case '\n':
265fc17b349SJuli Mallett 		count++;
266d9198881SWarner Losh 		if (zflag)
267d9198881SWarner Losh 			goto addch;
268d9198881SWarner Losh 
2699b50d902SRodney W. Grimes 		/* Quotes do not escape newlines. */
2709b50d902SRodney W. Grimes arg1:		if (insingle || indouble)
271a51024e2SPhilippe Charnier 			errx(1, "unterminated quote");
272ef9866beSJean-Marc Zucconi arg2:
273fc17b349SJuli Mallett 		foundeof = *eofstr != '\0' &&
274fc17b349SJuli Mallett 		    strcmp(argp, eofstr) == 0;
275fc17b349SJuli Mallett 
276ef9866beSJean-Marc Zucconi 		/* Do not make empty args unless they are quoted */
277fc17b349SJuli Mallett 		if ((argp != p || wasquoted) && !foundeof) {
278ef9866beSJean-Marc Zucconi 			*p++ = '\0';
2799b50d902SRodney W. Grimes 			*xp++ = argp;
280fc17b349SJuli Mallett 			if (Iflag) {
281fc17b349SJuli Mallett 				size_t curlen;
282b9b03ba0SJuli Mallett 
283b9b03ba0SJuli Mallett 				if (inpline == NULL)
284fc17b349SJuli Mallett 					curlen = 0;
285fc17b349SJuli Mallett 				else {
2861925cb24SJuli Mallett 					/*
2871925cb24SJuli Mallett 					 * If this string is not zero
2881925cb24SJuli Mallett 					 * length, append a space for
2891925cb24SJuli Mallett 					 * seperation before the next
2901925cb24SJuli Mallett 					 * argument.
2911925cb24SJuli Mallett 					 */
292b9b03ba0SJuli Mallett 					if ((curlen = strlen(inpline)))
293fc17b349SJuli Mallett 						strcat(inpline, " ");
294fc17b349SJuli Mallett 				}
295fc17b349SJuli Mallett 				curlen++;
2961925cb24SJuli Mallett 				/*
2971925cb24SJuli Mallett 				 * Allocate enough to hold what we will
2985eb40323SJuli Mallett 				 * be holding in a second, and to append
2991925cb24SJuli Mallett 				 * a space next time through, if we have
3001925cb24SJuli Mallett 				 * to.
3011925cb24SJuli Mallett 				 */
302b9b03ba0SJuli Mallett 				inpline = realloc(inpline, curlen + 2 +
303b9b03ba0SJuli Mallett 				    strlen(argp));
304fc17b349SJuli Mallett 				if (inpline == NULL)
30591045075SJuli Mallett 					errx(1, "realloc failed");
306fc17b349SJuli Mallett 				if (curlen == 1)
307fc17b349SJuli Mallett 					strcpy(inpline, argp);
308fc17b349SJuli Mallett 				else
309fc17b349SJuli Mallett 					strcat(inpline, argp);
310fc17b349SJuli Mallett 			}
311ef9866beSJean-Marc Zucconi 		}
3129b50d902SRodney W. Grimes 
3139b50d902SRodney W. Grimes 		/*
3149b50d902SRodney W. Grimes 		 * If max'd out on args or buffer, or reached EOF,
3159b50d902SRodney W. Grimes 		 * run the command.  If xflag and max'd out on buffer
3161925cb24SJuli Mallett 		 * but not on args, object.  Having reached the limit
3171925cb24SJuli Mallett 		 * of input lines, as specified by -L is the same as
3181925cb24SJuli Mallett 		 * maxing out on arguments.
3199b50d902SRodney W. Grimes 		 */
3205eb40323SJuli Mallett 		if (xp == exp || p > ebp || ch == EOF ||
3215eb40323SJuli Mallett 		    (Lflag <= count && xflag) || foundeof) {
322ef9866beSJean-Marc Zucconi 			if (xflag && xp != exp && p > ebp)
323a51024e2SPhilippe Charnier 				errx(1, "insufficient space for arguments");
3248d904f15SDima Dorfman 			if (jfound) {
3258d904f15SDima Dorfman 				for (avj = argv; *avj; avj++)
3268d904f15SDima Dorfman 					*xp++ = *avj;
3278d904f15SDima Dorfman 			}
32891045075SJuli Mallett 			prerun(argc, av);
329fc17b349SJuli Mallett 			if (ch == EOF || foundeof)
3309b50d902SRodney W. Grimes 				exit(rval);
3319b50d902SRodney W. Grimes 			p = bbp;
3329b50d902SRodney W. Grimes 			xp = bxp;
333fc17b349SJuli Mallett 			count = 0;
334ef9866beSJean-Marc Zucconi 		}
3359b50d902SRodney W. Grimes 		argp = p;
336ef9866beSJean-Marc Zucconi 		wasquoted = 0;
3379b50d902SRodney W. Grimes 		break;
3389b50d902SRodney W. Grimes 	case '\'':
339d9198881SWarner Losh 		if (indouble || zflag)
3409b50d902SRodney W. Grimes 			goto addch;
3419b50d902SRodney W. Grimes 		insingle = !insingle;
342ef9866beSJean-Marc Zucconi 		wasquoted = 1;
3439b50d902SRodney W. Grimes 		break;
3449b50d902SRodney W. Grimes 	case '"':
345d9198881SWarner Losh 		if (insingle || zflag)
3469b50d902SRodney W. Grimes 			goto addch;
3479b50d902SRodney W. Grimes 		indouble = !indouble;
348ef9866beSJean-Marc Zucconi 		wasquoted = 1;
3499b50d902SRodney W. Grimes 		break;
3509b50d902SRodney W. Grimes 	case '\\':
351d9198881SWarner Losh 		if (zflag)
352d9198881SWarner Losh 			goto addch;
3539b50d902SRodney W. Grimes 		/* Backslash escapes anything, is escaped by quotes. */
3549b50d902SRodney W. Grimes 		if (!insingle && !indouble && (ch = getchar()) == EOF)
355a51024e2SPhilippe Charnier 			errx(1, "backslash at EOF");
3569b50d902SRodney W. Grimes 		/* FALLTHROUGH */
3579b50d902SRodney W. Grimes 	default:
3589b50d902SRodney W. Grimes addch:		if (p < ebp) {
3599b50d902SRodney W. Grimes 			*p++ = ch;
3609b50d902SRodney W. Grimes 			break;
3619b50d902SRodney W. Grimes 		}
3629b50d902SRodney W. Grimes 
3639b50d902SRodney W. Grimes 		/* If only one argument, not enough buffer space. */
3649b50d902SRodney W. Grimes 		if (bxp == xp)
365a51024e2SPhilippe Charnier 			errx(1, "insufficient space for argument");
3669b50d902SRodney W. Grimes 		/* Didn't hit argument limit, so if xflag object. */
3679b50d902SRodney W. Grimes 		if (xflag)
368a51024e2SPhilippe Charnier 			errx(1, "insufficient space for arguments");
3699b50d902SRodney W. Grimes 
3708d904f15SDima Dorfman 		if (jfound) {
3718d904f15SDima Dorfman 			for (avj = argv; *avj; avj++)
3728d904f15SDima Dorfman 				*xp++ = *avj;
3738d904f15SDima Dorfman 		}
37491045075SJuli Mallett 		prerun(argc, av);
3759b50d902SRodney W. Grimes 		xp = bxp;
3769b50d902SRodney W. Grimes 		cnt = ebp - argp;
377fc17b349SJuli Mallett 		memcpy(bbp, argp, (size_t)cnt);
3789b50d902SRodney W. Grimes 		p = (argp = bbp) + cnt;
3799b50d902SRodney W. Grimes 		*p++ = ch;
3809b50d902SRodney W. Grimes 		break;
3819b50d902SRodney W. Grimes 	}
38291045075SJuli Mallett 	return;
3839b50d902SRodney W. Grimes }
3849b50d902SRodney W. Grimes 
385263dc775SJuli Mallett /*
386263dc775SJuli Mallett  * Do things necessary before run()'ing, such as -I substitution,
387263dc775SJuli Mallett  * and then call run().
388263dc775SJuli Mallett  */
389263dc775SJuli Mallett static void
3901926d4aaSJuli Mallett prerun(int argc, char *argv[])
391263dc775SJuli Mallett {
392263dc775SJuli Mallett 	char **tmp, **tmp2, **avj;
39391045075SJuli Mallett 	int repls;
39491045075SJuli Mallett 
39591045075SJuli Mallett 	repls = Rflag;
396263dc775SJuli Mallett 
397be70f7d4SJuli Mallett 	if (argc == 0 || repls == 0) {
398263dc775SJuli Mallett 		*xp = NULL;
399263dc775SJuli Mallett 		run(argv);
400263dc775SJuli Mallett 		return;
401263dc775SJuli Mallett 	}
402263dc775SJuli Mallett 
403263dc775SJuli Mallett 	avj = argv;
404263dc775SJuli Mallett 
405263dc775SJuli Mallett 	/*
406263dc775SJuli Mallett 	 * Allocate memory to hold the argument list, and
407263dc775SJuli Mallett 	 * a NULL at the tail.
408263dc775SJuli Mallett 	 */
409b6594dbaSJuli Mallett 	tmp = malloc((argc + 1) * sizeof(char**));
410263dc775SJuli Mallett 	if (tmp == NULL)
41191045075SJuli Mallett 		errx(1, "malloc failed");
412263dc775SJuli Mallett 	tmp2 = tmp;
413263dc775SJuli Mallett 
414263dc775SJuli Mallett 	/*
415263dc775SJuli Mallett 	 * Save the first argument and iterate over it, we
416263dc775SJuli Mallett 	 * cannot do strnsubst() to it.
417263dc775SJuli Mallett 	 */
418263dc775SJuli Mallett 	if ((*tmp++ = strdup(*avj++)) == NULL)
41991045075SJuli Mallett 		errx(1, "strdup failed");
420263dc775SJuli Mallett 
421263dc775SJuli Mallett 	/*
422263dc775SJuli Mallett 	 * For each argument to utility, if we have not used up
423263dc775SJuli Mallett 	 * the number of replacements we are allowed to do, and
424263dc775SJuli Mallett 	 * if the argument contains at least one occurance of
425263dc775SJuli Mallett 	 * replstr, call strnsubst(), else just save the string.
426263dc775SJuli Mallett 	 * Iterations over elements of avj and tmp are done
427263dc775SJuli Mallett 	 * where appropriate.
428263dc775SJuli Mallett 	 */
429263dc775SJuli Mallett 	while (--argc) {
430263dc775SJuli Mallett 		*tmp = *avj++;
431263dc775SJuli Mallett 		if (repls && strstr(*tmp, replstr) != NULL) {
43291045075SJuli Mallett 			strnsubst(tmp++, replstr, inpline, (size_t)255);
433263dc775SJuli Mallett 			repls--;
434263dc775SJuli Mallett 		} else {
435263dc775SJuli Mallett 			if ((*tmp = strdup(*tmp)) == NULL)
43691045075SJuli Mallett 				errx(1, "strdup failed");
437263dc775SJuli Mallett 			tmp++;
438263dc775SJuli Mallett 		}
439263dc775SJuli Mallett 	}
440263dc775SJuli Mallett 
441263dc775SJuli Mallett 	/*
442263dc775SJuli Mallett 	 * Run it.
443263dc775SJuli Mallett 	 */
444b6594dbaSJuli Mallett 	*tmp = NULL;
445263dc775SJuli Mallett 	run(tmp2);
446263dc775SJuli Mallett 
447263dc775SJuli Mallett 	/*
448263dc775SJuli Mallett 	 * Walk from the tail to the head, free along the way.
449263dc775SJuli Mallett 	 */
450263dc775SJuli Mallett 	for (; tmp2 != tmp; tmp--)
451263dc775SJuli Mallett 		free(*tmp);
452263dc775SJuli Mallett 	/*
453263dc775SJuli Mallett 	 * Now free the list itself.
454263dc775SJuli Mallett 	 */
455263dc775SJuli Mallett 	free(tmp2);
456263dc775SJuli Mallett 
457263dc775SJuli Mallett 	/*
458986d829bSJuli Mallett 	 * Free the input line buffer, if we have one.
459263dc775SJuli Mallett 	 */
46051f7a48bSJuli Mallett 	if (inpline != NULL) {
46191045075SJuli Mallett 		free(inpline);
46251f7a48bSJuli Mallett 		inpline = NULL;
46351f7a48bSJuli Mallett 	}
464263dc775SJuli Mallett }
465263dc775SJuli Mallett 
466fc17b349SJuli Mallett static void
46773385ac6SJuli Mallett run(char **argv)
4689b50d902SRodney W. Grimes {
4698ad749a4SDag-Erling Smørgrav 	volatile int childerr;
47091045075SJuli Mallett 	char **avec;
4719b50d902SRodney W. Grimes 	pid_t pid;
472305e39f4SJuli Mallett 	int status;
4739b50d902SRodney W. Grimes 
474305e39f4SJuli Mallett 	/*
475305e39f4SJuli Mallett 	 * If the user wants to be notified of each command before it is
476305e39f4SJuli Mallett 	 * executed, notify them.  If they want the notification to be
477305e39f4SJuli Mallett 	 * followed by a prompt, then prompt them.
478305e39f4SJuli Mallett 	 */
479fc17b349SJuli Mallett 	if (tflag || pflag) {
4809b50d902SRodney W. Grimes 		(void)fprintf(stderr, "%s", *argv);
48191045075SJuli Mallett 		for (avec = argv + 1; *avec != NULL; ++avec)
48291045075SJuli Mallett 			(void)fprintf(stderr, " %s", *avec);
483305e39f4SJuli Mallett 		/*
484305e39f4SJuli Mallett 		 * If the user has asked to be prompted, do so.
485305e39f4SJuli Mallett 		 */
486305e39f4SJuli Mallett 		if (pflag)
487305e39f4SJuli Mallett 			/*
488305e39f4SJuli Mallett 			 * If they asked not to exec, return without execution
489305e39f4SJuli Mallett 			 * but if they asked to, go to the execution.  If we
490305e39f4SJuli Mallett 			 * could not open their tty, break the switch and drop
491305e39f4SJuli Mallett 			 * back to -t behaviour.
492305e39f4SJuli Mallett 			 */
493305e39f4SJuli Mallett 			switch (prompt()) {
494305e39f4SJuli Mallett 			case 0:
495fc17b349SJuli Mallett 				return;
496305e39f4SJuli Mallett 			case 1:
497305e39f4SJuli Mallett 				goto exec;
498305e39f4SJuli Mallett 			case 2:
499305e39f4SJuli Mallett 				break;
500305e39f4SJuli Mallett 			}
5019b50d902SRodney W. Grimes 		(void)fprintf(stderr, "\n");
5029b50d902SRodney W. Grimes 		(void)fflush(stderr);
5039b50d902SRodney W. Grimes 	}
504305e39f4SJuli Mallett exec:
5058ad749a4SDag-Erling Smørgrav 	childerr = 0;
5068ad749a4SDag-Erling Smørgrav 	switch(pid = vfork()) {
5079b50d902SRodney W. Grimes 	case -1:
5088ad749a4SDag-Erling Smørgrav 		err(1, "vfork");
5099b50d902SRodney W. Grimes 	case 0:
5109b50d902SRodney W. Grimes 		execvp(argv[0], argv);
5118ad749a4SDag-Erling Smørgrav 		childerr = errno;
5129b50d902SRodney W. Grimes 		_exit(1);
5139b50d902SRodney W. Grimes 	}
5149b50d902SRodney W. Grimes 	pid = waitpid(pid, &status, 0);
5159b50d902SRodney W. Grimes 	if (pid == -1)
516a51024e2SPhilippe Charnier 		err(1, "waitpid");
517fc17b349SJuli Mallett 	/* If we couldn't invoke the utility, exit. */
518fc17b349SJuli Mallett 	if (childerr != 0)
519fc17b349SJuli Mallett 		err(childerr == ENOENT ? 127 : 126, "%s", *argv);
5209b50d902SRodney W. Grimes 	/* If utility signaled or exited with a value of 255, exit 1-125. */
5219b50d902SRodney W. Grimes 	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
5229b50d902SRodney W. Grimes 		exit(1);
5239b50d902SRodney W. Grimes 	if (WEXITSTATUS(status))
5249b50d902SRodney W. Grimes 		rval = 1;
5259b50d902SRodney W. Grimes }
5269b50d902SRodney W. Grimes 
527305e39f4SJuli Mallett /*
528305e39f4SJuli Mallett  * Prompt the user about running a command.
529305e39f4SJuli Mallett  */
530305e39f4SJuli Mallett static int
531305e39f4SJuli Mallett prompt(void)
532305e39f4SJuli Mallett {
533305e39f4SJuli Mallett 	regex_t cre;
534305e39f4SJuli Mallett 	size_t rsize;
535305e39f4SJuli Mallett 	int match;
536305e39f4SJuli Mallett 	char *response;
537305e39f4SJuli Mallett 	FILE *ttyfp;
538305e39f4SJuli Mallett 
539305e39f4SJuli Mallett 	if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
540305e39f4SJuli Mallett 		return (2);	/* Indicate that the TTY failed to open. */
541305e39f4SJuli Mallett 	(void)fprintf(stderr, "?...");
542305e39f4SJuli Mallett 	(void)fflush(stderr);
543305e39f4SJuli Mallett 	if ((response = fgetln(ttyfp, &rsize)) == NULL ||
5448e0a87c1SRuslan Ermilov 	    regcomp(&cre,
5458e0a87c1SRuslan Ermilov #ifdef BOOTSTRAPPING
5468e0a87c1SRuslan Ermilov 		"^[yY]",
5478e0a87c1SRuslan Ermilov #else
5488e0a87c1SRuslan Ermilov 		nl_langinfo(YESEXPR),
5498e0a87c1SRuslan Ermilov #endif
5508e0a87c1SRuslan Ermilov 		REG_BASIC) != 0) {
551305e39f4SJuli Mallett 		(void)fclose(ttyfp);
552305e39f4SJuli Mallett 		return (0);
553305e39f4SJuli Mallett 	}
554305e39f4SJuli Mallett 	match = regexec(&cre, response, 0, NULL, 0);
555305e39f4SJuli Mallett 	(void)fclose(ttyfp);
556305e39f4SJuli Mallett 	regfree(&cre);
557305e39f4SJuli Mallett 	return (match == 0);
558305e39f4SJuli Mallett }
559305e39f4SJuli Mallett 
560a51024e2SPhilippe Charnier static void
56173385ac6SJuli Mallett usage(void)
5629b50d902SRodney W. Grimes {
5638d904f15SDima Dorfman 	fprintf(stderr,
564b50a7286SJuli Mallett "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
565b50a7286SJuli Mallett "             [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n");
5669b50d902SRodney W. Grimes 	exit(1);
5679b50d902SRodney W. Grimes }
568