xref: /illumos-gate/usr/src/cmd/xargs/xargs.c (revision 7c71d718)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ef69670dScf  * Common Development and Distribution License (the "License").
6ef69670dScf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2103fc8686SGarrett D'Amore /*
22826ac02aSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23826ac02aSGarrett D'Amore  * Copyright 2012 DEY Storage Systems, Inc.
24*7c71d718SRobert Mustacchi  * Copyright (c) 2018, Joyent, Inc.
2503fc8686SGarrett D'Amore  *
2603fc8686SGarrett D'Amore  * Portions of this file developed by DEY Storage Systems, Inc. are licensed
2703fc8686SGarrett D'Amore  * under the terms of the Common Development and Distribution License (CDDL)
2803fc8686SGarrett D'Amore  * version 1.0 only.  The use of subsequent versions of the License are
2903fc8686SGarrett D'Amore  * is specifically prohibited unless those terms are not in conflict with
3003fc8686SGarrett D'Amore  * version 1.0 of the License.  You can find this license on-line at
3103fc8686SGarrett D'Amore  * http://www.illumos.org/license/CDDL
3203fc8686SGarrett D'Amore  */
337c478bd9Sstevel@tonic-gate /*
34d2afb7a9Scf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
357c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
367c478bd9Sstevel@tonic-gate  */
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
397c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include <stdio.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
447c478bd9Sstevel@tonic-gate #include <sys/wait.h>
457c478bd9Sstevel@tonic-gate #include <unistd.h>
467c478bd9Sstevel@tonic-gate #include <fcntl.h>
477c478bd9Sstevel@tonic-gate #include <string.h>
487c478bd9Sstevel@tonic-gate #include <stdarg.h>
497c478bd9Sstevel@tonic-gate #include <stdlib.h>
507c478bd9Sstevel@tonic-gate #include <limits.h>
517c478bd9Sstevel@tonic-gate #include <wchar.h>
527c478bd9Sstevel@tonic-gate #include <locale.h>
537c478bd9Sstevel@tonic-gate #include <langinfo.h>
547c478bd9Sstevel@tonic-gate #include <stropts.h>
557c478bd9Sstevel@tonic-gate #include <poll.h>
567c478bd9Sstevel@tonic-gate #include <errno.h>
577c478bd9Sstevel@tonic-gate #include <stdarg.h>
5804427e3bSAndrew Bennett #include <sys/fork.h>
593d63ea05Sas #include "getresponse.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	HEAD	0
627c478bd9Sstevel@tonic-gate #define	TAIL	1
637c478bd9Sstevel@tonic-gate #define	FALSE 0
647c478bd9Sstevel@tonic-gate #define	TRUE 1
657c478bd9Sstevel@tonic-gate #define	MAXSBUF 255
667c478bd9Sstevel@tonic-gate #define	MAXIBUF 512
677c478bd9Sstevel@tonic-gate #define	MAXINSERTS 5
687c478bd9Sstevel@tonic-gate #define	BUFSIZE LINE_MAX
697c478bd9Sstevel@tonic-gate #define	MAXARGS 255
707c478bd9Sstevel@tonic-gate #define	INSPAT_STR	"{}"	/* default replstr string for -[Ii]	*/
717c478bd9Sstevel@tonic-gate #define	FORK_RETRY	5
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	QBUF_STARTLEN 255  /* start size of growable string buffer */
747c478bd9Sstevel@tonic-gate #define	QBUF_INC 100	   /* how much to grow a growable string by */
757c478bd9Sstevel@tonic-gate 
7603fc8686SGarrett D'Amore /* We use these macros to help make formatting look "consistent" */
7703fc8686SGarrett D'Amore #define	EMSG(s)		ermsg(gettext(s "\n"))
7803fc8686SGarrett D'Amore #define	EMSG2(s, a)	ermsg(gettext(s "\n"), a)
7903fc8686SGarrett D'Amore #define	PERR(s)		perror(gettext("xargs: " s))
8003fc8686SGarrett D'Amore 
8103fc8686SGarrett D'Amore /* Some common error messages */
8203fc8686SGarrett D'Amore 
8303fc8686SGarrett D'Amore #define	LIST2LONG	"Argument list too long"
8403fc8686SGarrett D'Amore #define	ARG2LONG	"A single argument was greater than %d bytes"
8503fc8686SGarrett D'Amore #define	MALLOCFAIL	"Memory allocation failure"
8603fc8686SGarrett D'Amore #define	CORRUPTFILE	"Corrupt input file"
8703fc8686SGarrett D'Amore #define	WAITFAIL	"Wait failure"
8803fc8686SGarrett D'Amore #define	CHILDSIG	"Child killed with signal %d"
8903fc8686SGarrett D'Amore #define	CHILDFAIL	"Command could not continue processing data"
9003fc8686SGarrett D'Amore #define	FORKFAIL	"Could not fork child"
9103fc8686SGarrett D'Amore #define	EXECFAIL	"Could not exec command"
9203fc8686SGarrett D'Amore #define	MISSQUOTE	"Missing quote"
9303fc8686SGarrett D'Amore #define	BADESCAPE	"Incomplete escape"
9403fc8686SGarrett D'Amore #define	IBUFOVERFLOW	"Insert buffer overflow"
9504427e3bSAndrew Bennett #define	NOCHILDSLOT	"No free child slot available"
9603fc8686SGarrett D'Amore 
9703fc8686SGarrett D'Amore #define	_(x)	gettext(x)
9803fc8686SGarrett D'Amore 
997c478bd9Sstevel@tonic-gate static wctype_t	blank;
1007c478bd9Sstevel@tonic-gate static char	*arglist[MAXARGS+1];
10103fc8686SGarrett D'Amore static char	argbuf[BUFSIZE * 2 + 1];
10203fc8686SGarrett D'Amore static char	lastarg[BUFSIZE + 1];
1037c478bd9Sstevel@tonic-gate static char	**ARGV = arglist;
1047c478bd9Sstevel@tonic-gate static char	*LEOF = "_";
1057c478bd9Sstevel@tonic-gate static char	*INSPAT = INSPAT_STR;
1067c478bd9Sstevel@tonic-gate static char	ins_buf[MAXIBUF];
1077c478bd9Sstevel@tonic-gate static char	*p_ibuf;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate static struct inserts {
1107c478bd9Sstevel@tonic-gate 	char	**p_ARGV;	/* where to put newarg ptr in arg list */
1117c478bd9Sstevel@tonic-gate 	char	*p_skel;	/* ptr to arg template */
1127c478bd9Sstevel@tonic-gate } saveargv[MAXINSERTS];
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate static int	PROMPT = -1;
1157c478bd9Sstevel@tonic-gate static int	BUFLIM = BUFSIZE;
11604427e3bSAndrew Bennett static int	MAXPROCS = 1;
1177c478bd9Sstevel@tonic-gate static int	N_ARGS = 0;
1187c478bd9Sstevel@tonic-gate static int	N_args = 0;
1197c478bd9Sstevel@tonic-gate static int	N_lines = 0;
1207c478bd9Sstevel@tonic-gate static int	DASHX = FALSE;
1217c478bd9Sstevel@tonic-gate static int	MORE = TRUE;
1227c478bd9Sstevel@tonic-gate static int	PER_LINE = FALSE;
123826ac02aSGarrett D'Amore static int	LINE_CONT = FALSE;
124826ac02aSGarrett D'Amore static int	EAT_LEAD = FALSE;
1257c478bd9Sstevel@tonic-gate static int	ERR = FALSE;
1267c478bd9Sstevel@tonic-gate static int	OK = TRUE;
1277c478bd9Sstevel@tonic-gate static int	LEGAL = FALSE;
1287c478bd9Sstevel@tonic-gate static int	TRACE = FALSE;
1297c478bd9Sstevel@tonic-gate static int	INSERT = FALSE;
13003fc8686SGarrett D'Amore static int	ZERO = FALSE;
1317c478bd9Sstevel@tonic-gate static int	linesize = 0;
1327c478bd9Sstevel@tonic-gate static int	ibufsize = 0;
1337c478bd9Sstevel@tonic-gate static int	exitstat = 0;	/* our exit status			*/
1347c478bd9Sstevel@tonic-gate static int	mac;		/* modified argc, after parsing		*/
1357c478bd9Sstevel@tonic-gate static char	**mav;		/* modified argv, after parsing		*/
1367c478bd9Sstevel@tonic-gate static int	n_inserts;	/* # of insertions.			*/
13704427e3bSAndrew Bennett static pid_t	*procs;		/* pids of children			*/
13804427e3bSAndrew Bennett static int	n_procs;	/* # of child processes.		*/
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate /* our usage message:							*/
1411daace1dSGarrett D'Amore #define	USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\
14204427e3bSAndrew Bennett 	"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-P maxprocs] "\
14304427e3bSAndrew Bennett 	"[-s size] [cmd [args ...]]\n"
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate static int	echoargs();
14603fc8686SGarrett D'Amore static wint_t	getwchr(char *, size_t *);
14704427e3bSAndrew Bennett static void	lcall(char *sub, char **subargs);
1487c478bd9Sstevel@tonic-gate static void	addibuf(struct inserts *p);
1497c478bd9Sstevel@tonic-gate static void	ermsg(char *messages, ...);
1507c478bd9Sstevel@tonic-gate static char	*addarg(char *arg);
15103fc8686SGarrett D'Amore static void	store_str(char **, char *, size_t);
15203fc8686SGarrett D'Amore static char	*getarg(char *);
1537c478bd9Sstevel@tonic-gate static char	*insert(char *pattern, char *subst);
1547c478bd9Sstevel@tonic-gate static void	usage();
1557c478bd9Sstevel@tonic-gate static void	parseargs();
15604427e3bSAndrew Bennett static int	procs_find(pid_t child);
15704427e3bSAndrew Bennett static void	procs_store(pid_t child);
15804427e3bSAndrew Bennett static boolean_t procs_delete(pid_t child);
15904427e3bSAndrew Bennett static pid_t	procs_waitpid(boolean_t blocking, int *stat_loc);
16004427e3bSAndrew Bennett static void	procs_wait(boolean_t blocking);
1617c478bd9Sstevel@tonic-gate 
16243a29105Srobbin int
1637c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1647c478bd9Sstevel@tonic-gate {
1657c478bd9Sstevel@tonic-gate 	int	j;
166*7c71d718SRobert Mustacchi 	long	l;
1677c478bd9Sstevel@tonic-gate 	struct inserts *psave;
1687c478bd9Sstevel@tonic-gate 	int c;
1697c478bd9Sstevel@tonic-gate 	int	initsize;
17003fc8686SGarrett D'Amore 	char	*cmdname, **initlist;
17103fc8686SGarrett D'Amore 	char	*arg;
17203fc8686SGarrett D'Amore 	char	*next;
17304427e3bSAndrew Bennett 	char	*eptr;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* initialization */
1767c478bd9Sstevel@tonic-gate 	blank = wctype("blank");
1777c478bd9Sstevel@tonic-gate 	n_inserts = 0;
1787c478bd9Sstevel@tonic-gate 	psave = saveargv;
1797c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1807c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D 		*/
1817c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't 		*/
1827c478bd9Sstevel@tonic-gate #endif
1837c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1843d63ea05Sas 	if (init_yes() < 0) {
18503fc8686SGarrett D'Amore 		ermsg(_(ERR_MSG_INIT_YES), strerror(errno));
1867c478bd9Sstevel@tonic-gate 		exit(1);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	parseargs(argc, argv);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	/* handling all of xargs arguments:				*/
19204427e3bSAndrew Bennett 	while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:P:s:x")) != EOF) {
1937c478bd9Sstevel@tonic-gate 		switch (c) {
19403fc8686SGarrett D'Amore 		case '0':
19503fc8686SGarrett D'Amore 			ZERO = TRUE;
19603fc8686SGarrett D'Amore 			break;
19703fc8686SGarrett D'Amore 
1987c478bd9Sstevel@tonic-gate 		case 't':	/* -t: turn trace mode on		*/
1997c478bd9Sstevel@tonic-gate 			TRACE = TRUE;
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 		case 'p':	/* -p: turn on prompt mode.		*/
2037c478bd9Sstevel@tonic-gate 			if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) {
20403fc8686SGarrett D'Amore 				PERR("can't read from tty for -p");
2057c478bd9Sstevel@tonic-gate 			} else {
2067c478bd9Sstevel@tonic-gate 				TRACE = TRUE;
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			break;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		case 'e':
2117c478bd9Sstevel@tonic-gate 			/*
2127c478bd9Sstevel@tonic-gate 			 * -e[eofstr]: set/disable end-of-file.
2137c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; but
2147c478bd9Sstevel@tonic-gate 			 * parseargs forced an argument if not was given.  The
2157c478bd9Sstevel@tonic-gate 			 * forced argument is the default...
2167c478bd9Sstevel@tonic-gate 			 */
2177c478bd9Sstevel@tonic-gate 			LEOF = optarg; /* can be empty */
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case 'E':
2217c478bd9Sstevel@tonic-gate 			/*
2227c478bd9Sstevel@tonic-gate 			 * -E eofstr: change end-of-file string.
223952d685eScf 			 * eofstr *is* required here, but can be empty:
2247c478bd9Sstevel@tonic-gate 			 */
2257c478bd9Sstevel@tonic-gate 			LEOF = optarg;
2267c478bd9Sstevel@tonic-gate 			break;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 		case 'I':
2297c478bd9Sstevel@tonic-gate 			/* -I replstr: Insert mode. replstr *is* required. */
230826ac02aSGarrett D'Amore 			INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
231826ac02aSGarrett D'Amore 			LINE_CONT = FALSE;
2327c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
233952d685eScf 			INSPAT = optarg;
234952d685eScf 			if (*optarg == '\0') {
23503fc8686SGarrett D'Amore 				ermsg(_("Option requires an argument: -%c\n"),
23603fc8686SGarrett D'Amore 				    c);
2377c478bd9Sstevel@tonic-gate 			}
2387c478bd9Sstevel@tonic-gate 			break;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 		case 'i':
2417c478bd9Sstevel@tonic-gate 			/*
2427c478bd9Sstevel@tonic-gate 			 * -i [replstr]: insert mode, with *optional* replstr.
2437c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2447c478bd9Sstevel@tonic-gate 			 * it's not given, then the string INSPAT_STR will
2457c478bd9Sstevel@tonic-gate 			 * be assumed.
2467c478bd9Sstevel@tonic-gate 			 *
2477c478bd9Sstevel@tonic-gate 			 * Since getopts(3C) doesn't handle the case of an
2487c478bd9Sstevel@tonic-gate 			 * optional variable argument at all, we have to
2497c478bd9Sstevel@tonic-gate 			 * parse this by hand:
2507c478bd9Sstevel@tonic-gate 			 */
2517c478bd9Sstevel@tonic-gate 
252826ac02aSGarrett D'Amore 			INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
253826ac02aSGarrett D'Amore 			LINE_CONT = FALSE;
2547c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
255788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2567c478bd9Sstevel@tonic-gate 				INSPAT = optarg;
2577c478bd9Sstevel@tonic-gate 			} else {
2587c478bd9Sstevel@tonic-gate 				/*
2597c478bd9Sstevel@tonic-gate 				 * here, there is no next argument. so
2607c478bd9Sstevel@tonic-gate 				 * we reset INSPAT to the INSPAT_STR.
2617c478bd9Sstevel@tonic-gate 				 * we *have* to do this, as -i/I may have
2627c478bd9Sstevel@tonic-gate 				 * been given previously, and XCU4 requires
2637c478bd9Sstevel@tonic-gate 				 * that only "the last one specified takes
2647c478bd9Sstevel@tonic-gate 				 * effect".
2657c478bd9Sstevel@tonic-gate 				 */
2667c478bd9Sstevel@tonic-gate 				INSPAT = INSPAT_STR;
2677c478bd9Sstevel@tonic-gate 			}
2687c478bd9Sstevel@tonic-gate 			break;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 		case 'L':
2717c478bd9Sstevel@tonic-gate 			/*
2727c478bd9Sstevel@tonic-gate 			 * -L number: # of times cmd is executed
2737c478bd9Sstevel@tonic-gate 			 * number *is* required here:
2747c478bd9Sstevel@tonic-gate 			 */
275826ac02aSGarrett D'Amore 			PER_LINE = LINE_CONT = TRUE;
2767c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
277826ac02aSGarrett D'Amore 			INSERT = EAT_LEAD = FALSE;
278952d685eScf 			if ((PER_LINE = atoi(optarg)) <= 0) {
27903fc8686SGarrett D'Amore 				ermsg(_("#lines must be positive int: %s\n"),
28003fc8686SGarrett D'Amore 				    optarg);
2817c478bd9Sstevel@tonic-gate 			}
2827c478bd9Sstevel@tonic-gate 			break;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 		case 'l':
2857c478bd9Sstevel@tonic-gate 			/*
2867c478bd9Sstevel@tonic-gate 			 * -l [number]: # of times cmd is executed
2877c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2887c478bd9Sstevel@tonic-gate 			 * it's not given, then 1 is assumed.
2897c478bd9Sstevel@tonic-gate 			 *
2907c478bd9Sstevel@tonic-gate 			 * parseargs handles the optional arg processing.
2917c478bd9Sstevel@tonic-gate 			 */
2927c478bd9Sstevel@tonic-gate 
293826ac02aSGarrett D'Amore 			PER_LINE = LINE_CONT = LEGAL = TRUE;
2947c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
295826ac02aSGarrett D'Amore 			INSERT = EAT_LEAD = FALSE;
2967c478bd9Sstevel@tonic-gate 
297788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2987c478bd9Sstevel@tonic-gate 				if ((PER_LINE = atoi(optarg)) <= 0)
2997c478bd9Sstevel@tonic-gate 					PER_LINE = 1;
3007c478bd9Sstevel@tonic-gate 			}
3017c478bd9Sstevel@tonic-gate 			break;
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 		case 'n':	/* -n number: # stdin args		*/
3047c478bd9Sstevel@tonic-gate 			/*
3057c478bd9Sstevel@tonic-gate 			 * -n number: # stdin args.
3067c478bd9Sstevel@tonic-gate 			 * number *is* required here:
3077c478bd9Sstevel@tonic-gate 			 */
308952d685eScf 			if ((N_ARGS = atoi(optarg)) <= 0) {
30903fc8686SGarrett D'Amore 				ermsg(_("#args must be positive int: %s\n"),
31003fc8686SGarrett D'Amore 				    optarg);
3117c478bd9Sstevel@tonic-gate 			} else {
3127c478bd9Sstevel@tonic-gate 				LEGAL = DASHX || N_ARGS == 1;
313826ac02aSGarrett D'Amore 				INSERT = PER_LINE = LINE_CONT = FALSE;
3147c478bd9Sstevel@tonic-gate 			}
3157c478bd9Sstevel@tonic-gate 			break;
3167c478bd9Sstevel@tonic-gate 
31704427e3bSAndrew Bennett 		case 'P':	/* -P maxprocs: # of child processses	*/
31804427e3bSAndrew Bennett 			errno = 0;
319*7c71d718SRobert Mustacchi 			l = strtol(optarg, &eptr, 10);
32004427e3bSAndrew Bennett 			if (*eptr != '\0' || errno != 0) {
32104427e3bSAndrew Bennett 				ermsg(_("failed to parse maxprocs (-P): %s\n"),
32204427e3bSAndrew Bennett 				    optarg);
32304427e3bSAndrew Bennett 				break;
32404427e3bSAndrew Bennett 			}
32504427e3bSAndrew Bennett 
326*7c71d718SRobert Mustacchi 			if (l < 0) {
327*7c71d718SRobert Mustacchi 				ermsg(_("maximum number of processes (-P) "
328*7c71d718SRobert Mustacchi 				    "cannot be negative\n"));
329*7c71d718SRobert Mustacchi 				break;
330*7c71d718SRobert Mustacchi 			}
331*7c71d718SRobert Mustacchi 
33204427e3bSAndrew Bennett 			/*
33304427e3bSAndrew Bennett 			 * Come up with an upper bound that'll probably fit in
33404427e3bSAndrew Bennett 			 * memory.
33504427e3bSAndrew Bennett 			 */
33604427e3bSAndrew Bennett 			if (l == 0 || l > ((INT_MAX / sizeof (pid_t) >> 1))) {
33704427e3bSAndrew Bennett 				l = INT_MAX / sizeof (pid_t) >> 1;
33804427e3bSAndrew Bennett 			}
33904427e3bSAndrew Bennett 			MAXPROCS = (int)l;
34004427e3bSAndrew Bennett 			break;
34104427e3bSAndrew Bennett 
3427c478bd9Sstevel@tonic-gate 		case 's':	/* -s size: set max size of each arg list */
343952d685eScf 			BUFLIM = atoi(optarg);
344952d685eScf 			if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
34503fc8686SGarrett D'Amore 				ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
34603fc8686SGarrett D'Amore 				    BUFSIZE, optarg);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 			break;
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 		case 'x':	/* -x: terminate if args > size limit	*/
3517c478bd9Sstevel@tonic-gate 			DASHX = LEGAL = TRUE;
3527c478bd9Sstevel@tonic-gate 			break;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		default:
3557c478bd9Sstevel@tonic-gate 			/*
3567c478bd9Sstevel@tonic-gate 			 * bad argument. complain and get ready to die.
3577c478bd9Sstevel@tonic-gate 			 */
3587c478bd9Sstevel@tonic-gate 			usage();
3597c478bd9Sstevel@tonic-gate 			exit(2);
3607c478bd9Sstevel@tonic-gate 			break;
3617c478bd9Sstevel@tonic-gate 		}
3627c478bd9Sstevel@tonic-gate 	}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 	/*
3657c478bd9Sstevel@tonic-gate 	 * if anything called ermsg(), something screwed up, so
3667c478bd9Sstevel@tonic-gate 	 * we exit early.
3677c478bd9Sstevel@tonic-gate 	 */
3687c478bd9Sstevel@tonic-gate 	if (OK == FALSE) {
3697c478bd9Sstevel@tonic-gate 		usage();
3707c478bd9Sstevel@tonic-gate 		exit(2);
3717c478bd9Sstevel@tonic-gate 	}
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	/*
3747c478bd9Sstevel@tonic-gate 	 * we're finished handling xargs's options, so now pick up
3757c478bd9Sstevel@tonic-gate 	 * the command name (if any), and it's options.
3767c478bd9Sstevel@tonic-gate 	 */
3777c478bd9Sstevel@tonic-gate 
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	mac -= optind;	/* dec arg count by what we've processed 	*/
3807c478bd9Sstevel@tonic-gate 	mav += optind;	/* inc to current mav				*/
3817c478bd9Sstevel@tonic-gate 
38204427e3bSAndrew Bennett 	procs = calloc(MAXPROCS, sizeof (pid_t));
38304427e3bSAndrew Bennett 	if (procs == NULL) {
38404427e3bSAndrew Bennett 		PERR(MALLOCFAIL);
38504427e3bSAndrew Bennett 		exit(1);
38604427e3bSAndrew Bennett 	}
38704427e3bSAndrew Bennett 
3887c478bd9Sstevel@tonic-gate 	if (mac <= 0) {	/* if there're no more args to process,	*/
3897c478bd9Sstevel@tonic-gate 		cmdname = "/usr/bin/echo";	/* our default command	*/
3907c478bd9Sstevel@tonic-gate 		*ARGV++ = addarg(cmdname);	/* use the default cmd.	*/
3917c478bd9Sstevel@tonic-gate 	} else {	/* otherwise keep parsing rest of the string.	*/
3927c478bd9Sstevel@tonic-gate 		/*
3937c478bd9Sstevel@tonic-gate 		 * note that we can't use getopts(3C), and *must* parse
3947c478bd9Sstevel@tonic-gate 		 * this by hand, as we don't know apriori what options the
3957c478bd9Sstevel@tonic-gate 		 * command will take.
3967c478bd9Sstevel@tonic-gate 		 */
3977c478bd9Sstevel@tonic-gate 		cmdname = *mav;	/* get the command name	*/
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 		/* pick up the remaining args from the command line:	*/
4017c478bd9Sstevel@tonic-gate 		while ((OK == TRUE) && (mac-- > 0)) {
4027c478bd9Sstevel@tonic-gate 			/*
4037c478bd9Sstevel@tonic-gate 			 * while we haven't crapped out, and there's
4047c478bd9Sstevel@tonic-gate 			 * work to do:
4057c478bd9Sstevel@tonic-gate 			 */
4067c478bd9Sstevel@tonic-gate 			if (INSERT && ! ERR) {
40703fc8686SGarrett D'Amore 				if (strstr(*mav, INSPAT) != NULL) {
4087c478bd9Sstevel@tonic-gate 					if (++n_inserts > MAXINSERTS) {
40903fc8686SGarrett D'Amore 						ermsg(_("too many args "
4107c478bd9Sstevel@tonic-gate 						    "with %s\n"), INSPAT);
4117c478bd9Sstevel@tonic-gate 						ERR = TRUE;
4127c478bd9Sstevel@tonic-gate 					}
4137c478bd9Sstevel@tonic-gate 					psave->p_ARGV = ARGV;
4147c478bd9Sstevel@tonic-gate 					(psave++)->p_skel = *mav;
4157c478bd9Sstevel@tonic-gate 				}
4167c478bd9Sstevel@tonic-gate 			}
4177c478bd9Sstevel@tonic-gate 			*ARGV++ = addarg(*mav++);
4187c478bd9Sstevel@tonic-gate 		}
4197c478bd9Sstevel@tonic-gate 	}
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	/* pick up args from standard input */
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	initlist = ARGV;
4247c478bd9Sstevel@tonic-gate 	initsize = linesize;
42503fc8686SGarrett D'Amore 	lastarg[0] = '\0';
4267c478bd9Sstevel@tonic-gate 
42703fc8686SGarrett D'Amore 	while (OK) {
4287c478bd9Sstevel@tonic-gate 		N_args = 0;
4297c478bd9Sstevel@tonic-gate 		N_lines = 0;
4307c478bd9Sstevel@tonic-gate 		ARGV = initlist;
4317c478bd9Sstevel@tonic-gate 		linesize = initsize;
43203fc8686SGarrett D'Amore 		next = argbuf;
43303fc8686SGarrett D'Amore 
43403fc8686SGarrett D'Amore 		while (MORE || (lastarg[0] != '\0')) {
43503fc8686SGarrett D'Amore 			int l;
43603fc8686SGarrett D'Amore 
43703fc8686SGarrett D'Amore 			if (*lastarg != '\0') {
43803fc8686SGarrett D'Amore 				arg = strcpy(next, lastarg);
43903fc8686SGarrett D'Amore 				*lastarg = '\0';
44003fc8686SGarrett D'Amore 			} else if ((arg = getarg(next)) == NULL) {
44103fc8686SGarrett D'Amore 				break;
44203fc8686SGarrett D'Amore 			}
44303fc8686SGarrett D'Amore 
44403fc8686SGarrett D'Amore 			l = strlen(arg) + 1;
44503fc8686SGarrett D'Amore 			linesize += l;
44603fc8686SGarrett D'Amore 			next += l;
44703fc8686SGarrett D'Amore 
44803fc8686SGarrett D'Amore 			/* Inserts are handled specially later. */
44903fc8686SGarrett D'Amore 			if ((n_inserts == 0) && (linesize >= BUFLIM)) {
45003fc8686SGarrett D'Amore 				/*
45103fc8686SGarrett D'Amore 				 * Legal indicates hard fail if the list is
45203fc8686SGarrett D'Amore 				 * truncated due to size.  So fail, or if we
45303fc8686SGarrett D'Amore 				 * cannot create any list because it would be
45403fc8686SGarrett D'Amore 				 * too big.
45503fc8686SGarrett D'Amore 				 */
45603fc8686SGarrett D'Amore 				if (LEGAL || N_args == 0) {
45703fc8686SGarrett D'Amore 					EMSG(LIST2LONG);
45804427e3bSAndrew Bennett 					procs_wait(B_TRUE);
45903fc8686SGarrett D'Amore 					exit(2);
46003fc8686SGarrett D'Amore 					/* NOTREACHED */
46103fc8686SGarrett D'Amore 				}
46203fc8686SGarrett D'Amore 
46303fc8686SGarrett D'Amore 				/*
46403fc8686SGarrett D'Amore 				 * Otherwise just save argument for later.
46503fc8686SGarrett D'Amore 				 */
46603fc8686SGarrett D'Amore 				(void) strcpy(lastarg, arg);
46703fc8686SGarrett D'Amore 				break;
46803fc8686SGarrett D'Amore 			}
46903fc8686SGarrett D'Amore 
47003fc8686SGarrett D'Amore 			*ARGV++ = arg;
47103fc8686SGarrett D'Amore 
47203fc8686SGarrett D'Amore 			N_args++;
47303fc8686SGarrett D'Amore 
474826ac02aSGarrett D'Amore 			if ((PER_LINE && (N_lines >= PER_LINE)) ||
475826ac02aSGarrett D'Amore 			    (N_ARGS && (N_args >= N_ARGS))) {
47603fc8686SGarrett D'Amore 				break;
47703fc8686SGarrett D'Amore 			}
47803fc8686SGarrett D'Amore 
4797c478bd9Sstevel@tonic-gate 
480a035dc19Scf 			if ((ARGV - arglist) == MAXARGS) {
481a035dc19Scf 				break;
482a035dc19Scf 			}
483a035dc19Scf 		}
48403fc8686SGarrett D'Amore 
48503fc8686SGarrett D'Amore 		*ARGV = NULL;
48603fc8686SGarrett D'Amore 		if (N_args == 0) {
48703fc8686SGarrett D'Amore 			/* Reached the end with no more work. */
48804427e3bSAndrew Bennett 			break;
489a035dc19Scf 		}
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 		/* insert arg if requested */
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		if (!ERR && INSERT) {
49403fc8686SGarrett D'Amore 
4957c478bd9Sstevel@tonic-gate 			p_ibuf = ins_buf;
4967c478bd9Sstevel@tonic-gate 			ARGV--;
4977c478bd9Sstevel@tonic-gate 			j = ibufsize = 0;
4987c478bd9Sstevel@tonic-gate 			for (psave = saveargv; ++j <= n_inserts; ++psave) {
4997c478bd9Sstevel@tonic-gate 				addibuf(psave);
5007c478bd9Sstevel@tonic-gate 				if (ERR)
5017c478bd9Sstevel@tonic-gate 					break;
5027c478bd9Sstevel@tonic-gate 			}
5037c478bd9Sstevel@tonic-gate 		}
50403fc8686SGarrett D'Amore 		*ARGV = NULL;
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 		if (n_inserts > 0) {
5077c478bd9Sstevel@tonic-gate 			/*
5087c478bd9Sstevel@tonic-gate 			 * if we've done any insertions, re-calculate the
5097c478bd9Sstevel@tonic-gate 			 * linesize. bomb out if we've exceeded our length.
5107c478bd9Sstevel@tonic-gate 			 */
51103fc8686SGarrett D'Amore 			linesize = 0;
5127c478bd9Sstevel@tonic-gate 			for (ARGV = arglist; *ARGV != NULL; ARGV++) {
51303fc8686SGarrett D'Amore 				linesize += strlen(*ARGV) + 1;
51403fc8686SGarrett D'Amore 			}
51503fc8686SGarrett D'Amore 			if (linesize >= BUFLIM) {
51603fc8686SGarrett D'Amore 				EMSG(LIST2LONG);
51704427e3bSAndrew Bennett 				procs_wait(B_TRUE);
51803fc8686SGarrett D'Amore 				exit(2);
51903fc8686SGarrett D'Amore 				/* NOTREACHED */
5207c478bd9Sstevel@tonic-gate 			}
5217c478bd9Sstevel@tonic-gate 		}
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 		/* exec command */
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if (!ERR) {
5267c478bd9Sstevel@tonic-gate 			if (!MORE &&
5277c478bd9Sstevel@tonic-gate 			    (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
5287c478bd9Sstevel@tonic-gate 				exit(exitstat);
5297c478bd9Sstevel@tonic-gate 			OK = TRUE;
5307c478bd9Sstevel@tonic-gate 			j = TRACE ? echoargs() : TRUE;
5317c478bd9Sstevel@tonic-gate 			if (j) {
5327c478bd9Sstevel@tonic-gate 				/*
5337c478bd9Sstevel@tonic-gate 				 * for xcu4, all invocations of cmdname must
5347c478bd9Sstevel@tonic-gate 				 * return 0, in order for us to return 0.
5357c478bd9Sstevel@tonic-gate 				 * so if we have a non-zero status here,
5367c478bd9Sstevel@tonic-gate 				 * quit immediately.
5377c478bd9Sstevel@tonic-gate 				 */
53804427e3bSAndrew Bennett 				(void) lcall(cmdname, arglist);
5397c478bd9Sstevel@tonic-gate 			}
5407c478bd9Sstevel@tonic-gate 		}
5417c478bd9Sstevel@tonic-gate 	}
5427c478bd9Sstevel@tonic-gate 
54304427e3bSAndrew Bennett 	procs_wait(B_TRUE);
54404427e3bSAndrew Bennett 
54503fc8686SGarrett D'Amore 	if (OK)
54643a29105Srobbin 		return (exitstat);
5477c478bd9Sstevel@tonic-gate 
54803fc8686SGarrett D'Amore 	/*
54903fc8686SGarrett D'Amore 	 * if exitstat was set, to match XCU4 complience,
55003fc8686SGarrett D'Amore 	 * return that value, otherwise, return 1.
55103fc8686SGarrett D'Amore 	 */
55203fc8686SGarrett D'Amore 	return (exitstat ? exitstat : 1);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate static char *
5567c478bd9Sstevel@tonic-gate addarg(char *arg)
5577c478bd9Sstevel@tonic-gate {
55803fc8686SGarrett D'Amore 	linesize += (strlen(arg) + 1);
55903fc8686SGarrett D'Amore 	return (arg);
5607c478bd9Sstevel@tonic-gate }
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 
56303fc8686SGarrett D'Amore static void
56403fc8686SGarrett D'Amore store_str(char **buffer, char *str, size_t len)
56503fc8686SGarrett D'Amore {
56603fc8686SGarrett D'Amore 	(void) memcpy(*buffer, str, len);
56703fc8686SGarrett D'Amore 	(*buffer)[len] = '\0';
56803fc8686SGarrett D'Amore 	*buffer += len;
5697c478bd9Sstevel@tonic-gate }
5707c478bd9Sstevel@tonic-gate 
57103fc8686SGarrett D'Amore 
5727c478bd9Sstevel@tonic-gate static char *
57303fc8686SGarrett D'Amore getarg(char *arg)
5747c478bd9Sstevel@tonic-gate {
57503fc8686SGarrett D'Amore 	char	*xarg = arg;
576826ac02aSGarrett D'Amore 	wchar_t	c = 0;
5777c478bd9Sstevel@tonic-gate 	char	mbc[MB_LEN_MAX];
57803fc8686SGarrett D'Amore 	size_t	len;
57903fc8686SGarrett D'Amore 	int	escape = 0;
58003fc8686SGarrett D'Amore 	int	inquote = 0;
581826ac02aSGarrett D'Amore 	int	last = 0;
5827c478bd9Sstevel@tonic-gate 
58303fc8686SGarrett D'Amore 	arg[0] = '\0';
5847c478bd9Sstevel@tonic-gate 
58503fc8686SGarrett D'Amore 	while (MORE) {
5867c478bd9Sstevel@tonic-gate 
58703fc8686SGarrett D'Amore 		len = 0;
588826ac02aSGarrett D'Amore 		last = c;
58903fc8686SGarrett D'Amore 		c = getwchr(mbc, &len);
5907c478bd9Sstevel@tonic-gate 
59103fc8686SGarrett D'Amore 		if (((arg - xarg) + len) > BUFLIM) {
59203fc8686SGarrett D'Amore 			EMSG2(ARG2LONG, BUFLIM);
59303fc8686SGarrett D'Amore 			exit(2);
59403fc8686SGarrett D'Amore 			ERR = TRUE;
5957c478bd9Sstevel@tonic-gate 			return (NULL);
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 
59803fc8686SGarrett D'Amore 		switch (c) {
59903fc8686SGarrett D'Amore 		case '\n':
60003fc8686SGarrett D'Amore 			if (ZERO) {
60103fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
6027c478bd9Sstevel@tonic-gate 				continue;
6037c478bd9Sstevel@tonic-gate 			}
604826ac02aSGarrett D'Amore 			/*
605826ac02aSGarrett D'Amore 			 * NB: Some other versions rip off all of the trailing
606826ac02aSGarrett D'Amore 			 * blanks.  The spec only claims that this should
607826ac02aSGarrett D'Amore 			 * be done for a single blank.  We follow the spec.
608826ac02aSGarrett D'Amore 			 */
609826ac02aSGarrett D'Amore 			if (LINE_CONT && iswctype(last, blank)) {
610826ac02aSGarrett D'Amore 				len = 0;
611826ac02aSGarrett D'Amore 				*arg = 0;
612826ac02aSGarrett D'Amore 				continue;
613826ac02aSGarrett D'Amore 			}
61403fc8686SGarrett D'Amore 			/* FALLTHRU */
6157c478bd9Sstevel@tonic-gate 
61603fc8686SGarrett D'Amore 		case '\0':
61703fc8686SGarrett D'Amore 		case WEOF:	/* Note WEOF == EOF */
6187c478bd9Sstevel@tonic-gate 
61903fc8686SGarrett D'Amore 			if (escape) {
62003fc8686SGarrett D'Amore 				EMSG(BADESCAPE);
62103fc8686SGarrett D'Amore 				ERR = TRUE;
62203fc8686SGarrett D'Amore 				return (NULL);
6237c478bd9Sstevel@tonic-gate 			}
6247c478bd9Sstevel@tonic-gate 			if (inquote) {
62503fc8686SGarrett D'Amore 				EMSG(MISSQUOTE);
6267c478bd9Sstevel@tonic-gate 				ERR = TRUE;
62703fc8686SGarrett D'Amore 				return (NULL);
6287c478bd9Sstevel@tonic-gate 			}
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 			N_lines++;
63103fc8686SGarrett D'Amore 			break;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 		case '"':
63403fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 1)) {
63503fc8686SGarrett D'Amore 				/* treat it literally */
63603fc8686SGarrett D'Amore 				escape = 0;
63703fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
63803fc8686SGarrett D'Amore 
63903fc8686SGarrett D'Amore 			} else if (inquote == 2) {
64003fc8686SGarrett D'Amore 				/* terminating double quote */
6417c478bd9Sstevel@tonic-gate 				inquote = 0;
64203fc8686SGarrett D'Amore 
64303fc8686SGarrett D'Amore 			} else {
64403fc8686SGarrett D'Amore 				/* starting quoted string */
6457c478bd9Sstevel@tonic-gate 				inquote = 2;
64603fc8686SGarrett D'Amore 			}
64703fc8686SGarrett D'Amore 			continue;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		case '\'':
65003fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 2)) {
65103fc8686SGarrett D'Amore 				/* treat it literally */
65203fc8686SGarrett D'Amore 				escape = 0;
65303fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
65403fc8686SGarrett D'Amore 
65503fc8686SGarrett D'Amore 			} else if (inquote == 1) {
65603fc8686SGarrett D'Amore 				/* terminating single quote */
6577c478bd9Sstevel@tonic-gate 				inquote = 0;
65803fc8686SGarrett D'Amore 
65903fc8686SGarrett D'Amore 			} else {
66003fc8686SGarrett D'Amore 				/* starting quoted string */
6617c478bd9Sstevel@tonic-gate 				inquote = 1;
66203fc8686SGarrett D'Amore 			}
66303fc8686SGarrett D'Amore 			continue;
6647c478bd9Sstevel@tonic-gate 
66503fc8686SGarrett D'Amore 		case '\\':
666d2afb7a9Scf 			/*
667d2afb7a9Scf 			 * Any unquoted character can be escaped by
668d2afb7a9Scf 			 * preceding it with a backslash.
669d2afb7a9Scf 			 */
67003fc8686SGarrett D'Amore 			if (ZERO || inquote || escape) {
67103fc8686SGarrett D'Amore 				escape = 0;
67203fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
67303fc8686SGarrett D'Amore 			} else {
67403fc8686SGarrett D'Amore 				escape = 1;
675d2afb7a9Scf 			}
67603fc8686SGarrett D'Amore 			continue;
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate 		default:
67903fc8686SGarrett D'Amore 			/* most times we will just want to store it */
68003fc8686SGarrett D'Amore 			if (inquote || escape || ZERO || !iswctype(c, blank)) {
68103fc8686SGarrett D'Amore 				escape = 0;
68203fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
68303fc8686SGarrett D'Amore 				continue;
6847c478bd9Sstevel@tonic-gate 			}
685826ac02aSGarrett D'Amore 			if (EAT_LEAD && last == 0) {
686826ac02aSGarrett D'Amore 				c = 0;		/* Roll it back */
687826ac02aSGarrett D'Amore 				continue;
688826ac02aSGarrett D'Amore 			}
689826ac02aSGarrett D'Amore 			if (PER_LINE) {
690826ac02aSGarrett D'Amore 				store_str(&arg, mbc, len);
691826ac02aSGarrett D'Amore 				continue;
692826ac02aSGarrett D'Amore 			}
693826ac02aSGarrett D'Amore 
694826ac02aSGarrett D'Amore 			/* unquoted blank without special handling */
6957c478bd9Sstevel@tonic-gate 			break;
6967c478bd9Sstevel@tonic-gate 		}
69703fc8686SGarrett D'Amore 
69803fc8686SGarrett D'Amore 		/*
69903fc8686SGarrett D'Amore 		 * At this point we are processing a complete argument.
70003fc8686SGarrett D'Amore 		 */
70103fc8686SGarrett D'Amore 		if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') {
70203fc8686SGarrett D'Amore 			MORE = FALSE;
70303fc8686SGarrett D'Amore 			return (NULL);
70403fc8686SGarrett D'Amore 		}
70503fc8686SGarrett D'Amore 		if (c == WEOF) {
70603fc8686SGarrett D'Amore 			MORE = FALSE;
70703fc8686SGarrett D'Amore 		}
70803fc8686SGarrett D'Amore 		if (xarg[0] == '\0')
70903fc8686SGarrett D'Amore 			continue;
71003fc8686SGarrett D'Amore 		break;
7117c478bd9Sstevel@tonic-gate 	}
7127c478bd9Sstevel@tonic-gate 
71303fc8686SGarrett D'Amore 	return (xarg[0] == '\0' ? NULL : xarg);
71403fc8686SGarrett D'Amore }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * ermsg():	print out an error message, and indicate failure globally.
7187c478bd9Sstevel@tonic-gate  *
7197c478bd9Sstevel@tonic-gate  *	Assumes that message has already been gettext()'d. It would be
7207c478bd9Sstevel@tonic-gate  *	nice if we could just do the gettext() here, but we can't, since
7217c478bd9Sstevel@tonic-gate  *	since xgettext(1M) wouldn't be able to pick up our error message.
7227c478bd9Sstevel@tonic-gate  */
7237c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
7247c478bd9Sstevel@tonic-gate static void
7257c478bd9Sstevel@tonic-gate ermsg(char *messages, ...)
7267c478bd9Sstevel@tonic-gate {
7277c478bd9Sstevel@tonic-gate 	va_list	ap;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	va_start(ap, messages);
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "xargs: ");
7327c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, messages, ap);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	va_end(ap);
7357c478bd9Sstevel@tonic-gate 	OK = FALSE;
7367c478bd9Sstevel@tonic-gate }
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate static int
7397c478bd9Sstevel@tonic-gate echoargs()
7407c478bd9Sstevel@tonic-gate {
7417c478bd9Sstevel@tonic-gate 	char	**anarg;
7427c478bd9Sstevel@tonic-gate 	char	**tanarg;	/* tmp ptr			*/
7437c478bd9Sstevel@tonic-gate 	int		i;
7447c478bd9Sstevel@tonic-gate 	char		reply[LINE_MAX];
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate 	tanarg = anarg = arglist-1;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * write out each argument, separated by a space. the tanarg
7507c478bd9Sstevel@tonic-gate 	 * nonsense is for xcu4 testsuite compliance - so that an
7517c478bd9Sstevel@tonic-gate 	 * extra space isn't echoed after the last argument.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	while (*++anarg) {		/* while there's an argument	*/
7547c478bd9Sstevel@tonic-gate 		++tanarg;		/* follow anarg			*/
7557c478bd9Sstevel@tonic-gate 		(void) write(2, *anarg, strlen(*anarg));
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 		if (*++tanarg) {	/* if there's another argument:	*/
7587c478bd9Sstevel@tonic-gate 			(void) write(2, " ", 1); /* add a space		*/
7597c478bd9Sstevel@tonic-gate 			--tanarg;	/* reset back to anarg		*/
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 	if (PROMPT == -1) {
7637c478bd9Sstevel@tonic-gate 		(void) write(2, "\n", 1);
7647c478bd9Sstevel@tonic-gate 		return (TRUE);
7657c478bd9Sstevel@tonic-gate 	}
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	(void) write(2, "?...", 4);	/* ask the user for input	*/
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
7707c478bd9Sstevel@tonic-gate 		if (reply[i] == '\n') {
7717c478bd9Sstevel@tonic-gate 			if (i == 0)
7727c478bd9Sstevel@tonic-gate 				return (FALSE);
7737c478bd9Sstevel@tonic-gate 			break;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	reply[i] = 0;
7777c478bd9Sstevel@tonic-gate 
7787c478bd9Sstevel@tonic-gate 	/* flush remainder of line if necessary */
7797c478bd9Sstevel@tonic-gate 	if (i == LINE_MAX) {
7807c478bd9Sstevel@tonic-gate 		char	bitbucket;
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 		while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
7837c478bd9Sstevel@tonic-gate 			;
7847c478bd9Sstevel@tonic-gate 	}
7857c478bd9Sstevel@tonic-gate 
7863d63ea05Sas 	return (yes_check(reply));
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate static char *
7917c478bd9Sstevel@tonic-gate insert(char *pattern, char *subst)
7927c478bd9Sstevel@tonic-gate {
7937c478bd9Sstevel@tonic-gate 	static char	buffer[MAXSBUF+1];
7947c478bd9Sstevel@tonic-gate 	int		len, ipatlen;
7957c478bd9Sstevel@tonic-gate 	char	*pat;
7967c478bd9Sstevel@tonic-gate 	char	*bufend;
7977c478bd9Sstevel@tonic-gate 	char	*pbuf;
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	len = strlen(subst);
8007c478bd9Sstevel@tonic-gate 	ipatlen = strlen(INSPAT) - 1;
8017c478bd9Sstevel@tonic-gate 	pat = pattern - 1;
8027c478bd9Sstevel@tonic-gate 	pbuf = buffer;
8037c478bd9Sstevel@tonic-gate 	bufend = &buffer[MAXSBUF];
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	while (*++pat) {
806826ac02aSGarrett D'Amore 		if (strncmp(pat, INSPAT, ipatlen + 1) == 0) {
8077c478bd9Sstevel@tonic-gate 			if (pbuf + len >= bufend) {
8087c478bd9Sstevel@tonic-gate 				break;
8097c478bd9Sstevel@tonic-gate 			} else {
8107c478bd9Sstevel@tonic-gate 				(void) strcpy(pbuf, subst);
8117c478bd9Sstevel@tonic-gate 				pat += ipatlen;
8127c478bd9Sstevel@tonic-gate 				pbuf += len;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		} else {
8157c478bd9Sstevel@tonic-gate 			*pbuf++ = *pat;
8167c478bd9Sstevel@tonic-gate 			if (pbuf >= bufend)
8177c478bd9Sstevel@tonic-gate 				break;
8187c478bd9Sstevel@tonic-gate 		}
8197c478bd9Sstevel@tonic-gate 	}
8207c478bd9Sstevel@tonic-gate 
8217c478bd9Sstevel@tonic-gate 	if (!*pat) {
8227c478bd9Sstevel@tonic-gate 		*pbuf = '\0';
8237c478bd9Sstevel@tonic-gate 		return (buffer);
8247c478bd9Sstevel@tonic-gate 	} else {
8257c478bd9Sstevel@tonic-gate 		ermsg(gettext("Maximum argument size with insertion via %s's "
8267c478bd9Sstevel@tonic-gate 		    "exceeded\n"), INSPAT);
8277c478bd9Sstevel@tonic-gate 		ERR = TRUE;
82803fc8686SGarrett D'Amore 		return (NULL);
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate }
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate static void
8347c478bd9Sstevel@tonic-gate addibuf(struct inserts	*p)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	char	*newarg, *skel, *sub;
8377c478bd9Sstevel@tonic-gate 	int		l;
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 	skel = p->p_skel;
8407c478bd9Sstevel@tonic-gate 	sub = *ARGV;
8417c478bd9Sstevel@tonic-gate 	newarg = insert(skel, sub);
8427c478bd9Sstevel@tonic-gate 	if (ERR)
843788f581bSceastha 		return;
8447c478bd9Sstevel@tonic-gate 
84503fc8686SGarrett D'Amore 	l = strlen(newarg) + 1;
84603fc8686SGarrett D'Amore 	if ((ibufsize += l) > MAXIBUF) {
84703fc8686SGarrett D'Amore 		EMSG(IBUFOVERFLOW);
84803fc8686SGarrett D'Amore 		ERR = TRUE;
8497c478bd9Sstevel@tonic-gate 	}
85003fc8686SGarrett D'Amore 	(void) strcpy(p_ibuf, newarg);
85103fc8686SGarrett D'Amore 	*(p->p_ARGV) = p_ibuf;
85203fc8686SGarrett D'Amore 	p_ibuf += l;
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /*
85703fc8686SGarrett D'Amore  * getwchr():	get the next wide character.
8587c478bd9Sstevel@tonic-gate  * description:
85903fc8686SGarrett D'Amore  *	we get the next character from stdin.  This returns WEOF if no
86003fc8686SGarrett D'Amore  *	character is present.  If ZERO is set, it gets a single byte instead
86103fc8686SGarrett D'Amore  *	a wide character.
8627c478bd9Sstevel@tonic-gate  */
86303fc8686SGarrett D'Amore static wint_t
86403fc8686SGarrett D'Amore getwchr(char *mbc, size_t *sz)
8657c478bd9Sstevel@tonic-gate {
86603fc8686SGarrett D'Amore 	size_t		i;
86703fc8686SGarrett D'Amore 	int		c;
86803fc8686SGarrett D'Amore 	wchar_t		wch;
8697c478bd9Sstevel@tonic-gate 
87003fc8686SGarrett D'Amore 	i = 0;
87103fc8686SGarrett D'Amore 	while (i < MB_CUR_MAX) {
8727c478bd9Sstevel@tonic-gate 
87303fc8686SGarrett D'Amore 		if ((c = fgetc(stdin)) == EOF) {
8747c478bd9Sstevel@tonic-gate 
87503fc8686SGarrett D'Amore 			if (i == 0) {
8767c478bd9Sstevel@tonic-gate 				/* TRUE EOF has been reached */
87703fc8686SGarrett D'Amore 				return (WEOF);
8787c478bd9Sstevel@tonic-gate 			}
87903fc8686SGarrett D'Amore 
8807c478bd9Sstevel@tonic-gate 			/*
8817c478bd9Sstevel@tonic-gate 			 * We have some characters in our buffer still so it
8827c478bd9Sstevel@tonic-gate 			 * must be an invalid character right before EOF.
8837c478bd9Sstevel@tonic-gate 			 */
8847c478bd9Sstevel@tonic-gate 			break;
8857c478bd9Sstevel@tonic-gate 		}
88603fc8686SGarrett D'Amore 		mbc[i++] = (char)c;
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate 		/* If this succeeds then we are done */
88903fc8686SGarrett D'Amore 		if (ZERO) {
89003fc8686SGarrett D'Amore 			*sz = i;
89103fc8686SGarrett D'Amore 			return ((char)c);
89203fc8686SGarrett D'Amore 		}
89303fc8686SGarrett D'Amore 		if (mbtowc(&wch, mbc, i) != -1) {
89403fc8686SGarrett D'Amore 			*sz = i;
89503fc8686SGarrett D'Amore 			return ((wint_t)wch);
89603fc8686SGarrett D'Amore 		}
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * We have now encountered an illegal character sequence.
9017c478bd9Sstevel@tonic-gate 	 * There is nothing much we can do at this point but
9027c478bd9Sstevel@tonic-gate 	 * return an error.  If we attempt to recover we may in fact
9037c478bd9Sstevel@tonic-gate 	 * return garbage as arguments, from the customer's point
9047c478bd9Sstevel@tonic-gate 	 * of view.  After all what if they are feeding us a file
9057c478bd9Sstevel@tonic-gate 	 * generated in another locale?
9067c478bd9Sstevel@tonic-gate 	 */
9077c478bd9Sstevel@tonic-gate 	errno = EILSEQ;
90803fc8686SGarrett D'Amore 	PERR(CORRUPTFILE);
9097c478bd9Sstevel@tonic-gate 	exit(1);
9107c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 
91404427e3bSAndrew Bennett static void
9157c478bd9Sstevel@tonic-gate lcall(char *sub, char **subargs)
9167c478bd9Sstevel@tonic-gate {
91704427e3bSAndrew Bennett 	int	retry = 0;
91804427e3bSAndrew Bennett 	pid_t	child;
9197c478bd9Sstevel@tonic-gate 
92003fc8686SGarrett D'Amore 	for (;;) {
92104427e3bSAndrew Bennett 		switch (child = forkx(FORK_NOSIGCHLD)) {
9227c478bd9Sstevel@tonic-gate 		default:
92304427e3bSAndrew Bennett 			procs_store(child);
92404427e3bSAndrew Bennett 			/*
92504427e3bSAndrew Bennett 			 * Note, if we have used up all of our slots, then this
92604427e3bSAndrew Bennett 			 * call may end up blocking.
92704427e3bSAndrew Bennett 			 */
92804427e3bSAndrew Bennett 			procs_wait(B_FALSE);
92904427e3bSAndrew Bennett 			return;
9307c478bd9Sstevel@tonic-gate 		case 0:
9317c478bd9Sstevel@tonic-gate 			(void) execvp(sub, subargs);
93203fc8686SGarrett D'Amore 			PERR(EXECFAIL);
9337c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
9347c478bd9Sstevel@tonic-gate 				exit(126);
9357c478bd9Sstevel@tonic-gate 			exit(127);
9367c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
9377c478bd9Sstevel@tonic-gate 		case -1:
9387c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN && retry++ < FORK_RETRY) {
93903fc8686SGarrett D'Amore 				PERR(FORKFAIL);
9407c478bd9Sstevel@tonic-gate 				exit(123);
9417c478bd9Sstevel@tonic-gate 			}
9427c478bd9Sstevel@tonic-gate 			(void) sleep(1);
9437c478bd9Sstevel@tonic-gate 		}
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate }
9467c478bd9Sstevel@tonic-gate 
94704427e3bSAndrew Bennett /*
94804427e3bSAndrew Bennett  * Return the index of child in the procs array.
94904427e3bSAndrew Bennett  */
95004427e3bSAndrew Bennett static int
95104427e3bSAndrew Bennett procs_find(pid_t child)
95204427e3bSAndrew Bennett {
95304427e3bSAndrew Bennett 	int	i;
95404427e3bSAndrew Bennett 
95504427e3bSAndrew Bennett 	for (i = 0; i < MAXPROCS; i++) {
95604427e3bSAndrew Bennett 		if (procs[i] == child) {
95704427e3bSAndrew Bennett 			return (i);
95804427e3bSAndrew Bennett 		}
95904427e3bSAndrew Bennett 	}
96004427e3bSAndrew Bennett 
96104427e3bSAndrew Bennett 	return (-1);
96204427e3bSAndrew Bennett }
96304427e3bSAndrew Bennett 
96404427e3bSAndrew Bennett static void
96504427e3bSAndrew Bennett procs_store(pid_t child)
96604427e3bSAndrew Bennett {
96704427e3bSAndrew Bennett 	int	i;
96804427e3bSAndrew Bennett 
96904427e3bSAndrew Bennett 	i = procs_find(0);
97004427e3bSAndrew Bennett 	if (i < 0) {
97104427e3bSAndrew Bennett 		EMSG(NOCHILDSLOT);
97204427e3bSAndrew Bennett 		exit(1);
97304427e3bSAndrew Bennett 	}
97404427e3bSAndrew Bennett 	procs[i] = child;
97504427e3bSAndrew Bennett 	n_procs++;
97604427e3bSAndrew Bennett }
97704427e3bSAndrew Bennett 
97804427e3bSAndrew Bennett static boolean_t
97904427e3bSAndrew Bennett procs_delete(pid_t child)
98004427e3bSAndrew Bennett {
98104427e3bSAndrew Bennett 	int	i;
98204427e3bSAndrew Bennett 
98304427e3bSAndrew Bennett 	i = procs_find(child);
98404427e3bSAndrew Bennett 	if (i < 0) {
98504427e3bSAndrew Bennett 		return (B_FALSE);
98604427e3bSAndrew Bennett 	}
98704427e3bSAndrew Bennett 
98804427e3bSAndrew Bennett 	procs[i] = (pid_t)0;
98904427e3bSAndrew Bennett 	n_procs--;
99004427e3bSAndrew Bennett 
99104427e3bSAndrew Bennett 	return (B_TRUE);
99204427e3bSAndrew Bennett }
99304427e3bSAndrew Bennett 
99404427e3bSAndrew Bennett static pid_t
99504427e3bSAndrew Bennett procs_waitpid(boolean_t blocking, int *stat_loc)
99604427e3bSAndrew Bennett {
99704427e3bSAndrew Bennett 	pid_t	child;
99804427e3bSAndrew Bennett 	int	options;
99904427e3bSAndrew Bennett 
100004427e3bSAndrew Bennett 	if (n_procs == 0) {
100104427e3bSAndrew Bennett 		errno = ECHILD;
100204427e3bSAndrew Bennett 		return (-1);
100304427e3bSAndrew Bennett 	}
100404427e3bSAndrew Bennett 
100504427e3bSAndrew Bennett 	options = 0;
100604427e3bSAndrew Bennett 	if (!blocking) {
100704427e3bSAndrew Bennett 		options |= WNOHANG;
100804427e3bSAndrew Bennett 	}
100904427e3bSAndrew Bennett 
101004427e3bSAndrew Bennett 	while ((child = waitpid((pid_t)-1, stat_loc, options)) > 0) {
101104427e3bSAndrew Bennett 		if (procs_delete(child)) {
101204427e3bSAndrew Bennett 			break;
101304427e3bSAndrew Bennett 		}
101404427e3bSAndrew Bennett 	}
101504427e3bSAndrew Bennett 
101604427e3bSAndrew Bennett 	return (child);
101704427e3bSAndrew Bennett }
101804427e3bSAndrew Bennett 
101904427e3bSAndrew Bennett static void
102004427e3bSAndrew Bennett procs_wait(boolean_t blocking)
102104427e3bSAndrew Bennett {
102204427e3bSAndrew Bennett 	pid_t	child;
102304427e3bSAndrew Bennett 	int	stat_loc;
102404427e3bSAndrew Bennett 
102504427e3bSAndrew Bennett 	/*
102604427e3bSAndrew Bennett 	 * If we currently have filled all of our slots, then we need to block
102704427e3bSAndrew Bennett 	 * further execution.
102804427e3bSAndrew Bennett 	 */
102904427e3bSAndrew Bennett 	if (n_procs >= MAXPROCS)
103004427e3bSAndrew Bennett 		blocking = B_TRUE;
103104427e3bSAndrew Bennett 	while ((child = procs_waitpid(blocking, &stat_loc)) > 0) {
103204427e3bSAndrew Bennett 		if (WIFSIGNALED(stat_loc)) {
103304427e3bSAndrew Bennett 			EMSG2(CHILDSIG, WTERMSIG(stat_loc));
103404427e3bSAndrew Bennett 			exit(125);
103504427e3bSAndrew Bennett 			/* NOTREACHED */
103604427e3bSAndrew Bennett 		} else if ((WEXITSTATUS(stat_loc) & 0377) == 0377) {
103704427e3bSAndrew Bennett 			EMSG(CHILDFAIL);
103804427e3bSAndrew Bennett 			exit(124);
103904427e3bSAndrew Bennett 			/* NOTREACHED */
104004427e3bSAndrew Bennett 		} else {
104104427e3bSAndrew Bennett 			exitstat |= WEXITSTATUS(stat_loc);
104204427e3bSAndrew Bennett 		}
104304427e3bSAndrew Bennett 	}
104404427e3bSAndrew Bennett 
104504427e3bSAndrew Bennett 	if (child == (pid_t)(-1) && errno != ECHILD) {
104604427e3bSAndrew Bennett 		EMSG(WAITFAIL);
104704427e3bSAndrew Bennett 		exit(122);
104804427e3bSAndrew Bennett 		/* NOTREACHED */
104904427e3bSAndrew Bennett 	}
105004427e3bSAndrew Bennett }
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate static void
10537c478bd9Sstevel@tonic-gate usage()
10547c478bd9Sstevel@tonic-gate {
105503fc8686SGarrett D'Amore 	ermsg(_(USAGEMSG));
10567c478bd9Sstevel@tonic-gate 	OK = FALSE;
10577c478bd9Sstevel@tonic-gate }
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate /*
10627c478bd9Sstevel@tonic-gate  * parseargs():		modify the args
10637c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
10647c478bd9Sstevel@tonic-gate  *	and getopts(3C) is clueless about this nonsense, we change the
10657c478bd9Sstevel@tonic-gate  *	our local argument count and strings to separate this out,
10667c478bd9Sstevel@tonic-gate  *	and make it easier to handle via getopts(3c).
10677c478bd9Sstevel@tonic-gate  *
10687c478bd9Sstevel@tonic-gate  *	-e	-> "-e ""
10697c478bd9Sstevel@tonic-gate  *	-e3	-> "-e "3"
10707c478bd9Sstevel@tonic-gate  *	-Estr	-> "-E "str"
10717c478bd9Sstevel@tonic-gate  *	-i	-> "-i "{}"
10727c478bd9Sstevel@tonic-gate  *	-irep	-> "-i "rep"
1073826ac02aSGarrett D'Amore  *	-l	-> "-l "1"
1074826ac02aSGarrett D'Amore  *	-l10	-> "-l "10"
10757c478bd9Sstevel@tonic-gate  *
10767c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
10777c478bd9Sstevel@tonic-gate  */
10787c478bd9Sstevel@tonic-gate static void
10797c478bd9Sstevel@tonic-gate parseargs(int ac, char **av)
10807c478bd9Sstevel@tonic-gate {
10817c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
10827c478bd9Sstevel@tonic-gate 	int cflag;		/* 0 = not processing cmd arg		*/
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
108503fc8686SGarrett D'Amore 		PERR(MALLOCFAIL);
10867c478bd9Sstevel@tonic-gate 		exit(1);
10877c478bd9Sstevel@tonic-gate 	}
10887c478bd9Sstevel@tonic-gate 
10897c478bd9Sstevel@tonic-gate 	/* for each argument, see if we need to change things:		*/
10907c478bd9Sstevel@tonic-gate 	for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
10917c478bd9Sstevel@tonic-gate 		if ((mav[mac] = strdup(av[i])) == NULL) {
109203fc8686SGarrett D'Amore 			PERR(MALLOCFAIL);
10937c478bd9Sstevel@tonic-gate 			exit(1);
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 
10967c478bd9Sstevel@tonic-gate 		/* -- has been found or argument list is fully processes */
10977c478bd9Sstevel@tonic-gate 		if (cflag)
10987c478bd9Sstevel@tonic-gate 			continue;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 		/*
11017c478bd9Sstevel@tonic-gate 		 * if we're doing special processing, and we've got a flag
11027c478bd9Sstevel@tonic-gate 		 */
11037c478bd9Sstevel@tonic-gate 		else if ((av[i][0] == '-') && (av[i][1] != NULL)) {
11047c478bd9Sstevel@tonic-gate 			char	*def;
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate 			switch (av[i][1]) {
11077c478bd9Sstevel@tonic-gate 			case	'e':
11087c478bd9Sstevel@tonic-gate 				def = ""; /* -e with no arg turns off eof */
11097c478bd9Sstevel@tonic-gate 				goto process_special;
11107c478bd9Sstevel@tonic-gate 			case	'i':
11117c478bd9Sstevel@tonic-gate 				def = INSPAT_STR;
11127c478bd9Sstevel@tonic-gate 				goto process_special;
11137c478bd9Sstevel@tonic-gate 			case	'l':
11147c478bd9Sstevel@tonic-gate 				def = "1";
11157c478bd9Sstevel@tonic-gate process_special:
11167c478bd9Sstevel@tonic-gate 				/*
11177c478bd9Sstevel@tonic-gate 				 * if there's no sub-option, we *must* add
11187c478bd9Sstevel@tonic-gate 				 * a default one. this is because xargs must
11197c478bd9Sstevel@tonic-gate 				 * be able to distinguish between a valid
11207c478bd9Sstevel@tonic-gate 				 * suboption, and a command name.
11217c478bd9Sstevel@tonic-gate 				 */
11227c478bd9Sstevel@tonic-gate 				if (av[i][2] == NULL) {
11237c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(def);
11247c478bd9Sstevel@tonic-gate 				} else {
11257c478bd9Sstevel@tonic-gate 					/* clear out our version: */
11267c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
11277c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(&av[i][2]);
11287c478bd9Sstevel@tonic-gate 				}
11297c478bd9Sstevel@tonic-gate 				if (mav[mac] == NULL) {
113003fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
11317c478bd9Sstevel@tonic-gate 					exit(1);
11327c478bd9Sstevel@tonic-gate 				}
11337c478bd9Sstevel@tonic-gate 				break;
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 			/* flags with required subarguments:		*/
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate 			/*
11387c478bd9Sstevel@tonic-gate 			 * there are two separate cases here. either the
11397c478bd9Sstevel@tonic-gate 			 * flag can have the normal XCU4 handling
11407c478bd9Sstevel@tonic-gate 			 * (of the form: -X subargument); or it can have
11417c478bd9Sstevel@tonic-gate 			 * the old solaris 2.[0-4] handling (of the
11427c478bd9Sstevel@tonic-gate 			 * form: -Xsubargument). in order to maintain
11437c478bd9Sstevel@tonic-gate 			 * backwards compatibility, we must support the
11447c478bd9Sstevel@tonic-gate 			 * latter case. we handle the latter possibility
11457c478bd9Sstevel@tonic-gate 			 * first so both the old solaris way of handling
11467c478bd9Sstevel@tonic-gate 			 * and the new XCU4 way of handling things are allowed.
11477c478bd9Sstevel@tonic-gate 			 */
11487c478bd9Sstevel@tonic-gate 			case	'n':	/* FALLTHROUGH			*/
114904427e3bSAndrew Bennett 			case	'P':	/* FALLTHROUGH			*/
11507c478bd9Sstevel@tonic-gate 			case	's':	/* FALLTHROUGH			*/
11517c478bd9Sstevel@tonic-gate 			case	'E':	/* FALLTHROUGH			*/
11527c478bd9Sstevel@tonic-gate 			case	'I':	/* FALLTHROUGH			*/
11537c478bd9Sstevel@tonic-gate 			case	'L':
11547c478bd9Sstevel@tonic-gate 				/*
11557c478bd9Sstevel@tonic-gate 				 * if the second character isn't null, then
11567c478bd9Sstevel@tonic-gate 				 * the user has specified the old syntax.
11577c478bd9Sstevel@tonic-gate 				 * we move the subargument into our
11587c478bd9Sstevel@tonic-gate 				 * mod'd argument list.
11597c478bd9Sstevel@tonic-gate 				 */
11607c478bd9Sstevel@tonic-gate 				if (av[i][2] != NULL) {
11617c478bd9Sstevel@tonic-gate 					/* first clean things up:	*/
11627c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate 					/* now add the separation:	*/
11657c478bd9Sstevel@tonic-gate 					++mac;	/* inc to next mod'd arg */
11667c478bd9Sstevel@tonic-gate 					if ((mav[mac] = strdup(&av[i][2])) ==
11677c478bd9Sstevel@tonic-gate 					    NULL) {
116803fc8686SGarrett D'Amore 						PERR(MALLOCFAIL);
11697c478bd9Sstevel@tonic-gate 						exit(1);
11707c478bd9Sstevel@tonic-gate 					}
11717c478bd9Sstevel@tonic-gate 					break;
11727c478bd9Sstevel@tonic-gate 				}
11737c478bd9Sstevel@tonic-gate 				i++;
11747c478bd9Sstevel@tonic-gate 				mac++;
1175952d685eScf 
11767c478bd9Sstevel@tonic-gate 				if (av[i] == NULL) {
11777c478bd9Sstevel@tonic-gate 					mav[mac] = NULL;
11787c478bd9Sstevel@tonic-gate 					return;
11797c478bd9Sstevel@tonic-gate 				}
11807c478bd9Sstevel@tonic-gate 				if ((mav[mac] = strdup(av[i])) == NULL) {
118103fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
11827c478bd9Sstevel@tonic-gate 					exit(1);
11837c478bd9Sstevel@tonic-gate 				}
11847c478bd9Sstevel@tonic-gate 				break;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 			/* flags */
11877c478bd9Sstevel@tonic-gate 			case 'p' :
11887c478bd9Sstevel@tonic-gate 			case 't' :
11897c478bd9Sstevel@tonic-gate 			case 'x' :
11901daace1dSGarrett D'Amore 			case '0' :
11917c478bd9Sstevel@tonic-gate 				break;
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 			case '-' :
11947c478bd9Sstevel@tonic-gate 			default:
11957c478bd9Sstevel@tonic-gate 				/*
11967c478bd9Sstevel@tonic-gate 				 * here we've hit the cmd argument. so
11977c478bd9Sstevel@tonic-gate 				 * we'll stop special processing, as the
11987c478bd9Sstevel@tonic-gate 				 * cmd may have a "-i" etc., argument,
11997c478bd9Sstevel@tonic-gate 				 * and we don't want to add a "" to it.
12007c478bd9Sstevel@tonic-gate 				 */
12017c478bd9Sstevel@tonic-gate 				cflag = 1;
12027c478bd9Sstevel@tonic-gate 				break;
12037c478bd9Sstevel@tonic-gate 			}
12047c478bd9Sstevel@tonic-gate 		} else if (i > 0) {	/* if we're not the 1st arg	*/
12057c478bd9Sstevel@tonic-gate 			/*
12067c478bd9Sstevel@tonic-gate 			 * if it's not a flag, then it *must* be the cmd.
12077c478bd9Sstevel@tonic-gate 			 * set cflag, so we don't mishandle the -[eil] flags.
12087c478bd9Sstevel@tonic-gate 			 */
12097c478bd9Sstevel@tonic-gate 			cflag = 1;
12107c478bd9Sstevel@tonic-gate 		}
12117c478bd9Sstevel@tonic-gate 	}
12127c478bd9Sstevel@tonic-gate 
12137c478bd9Sstevel@tonic-gate 	mav[mac] = NULL;
12147c478bd9Sstevel@tonic-gate }
1215