xref: /illumos-gate/usr/src/cmd/xargs/xargs.c (revision 04427e3bf236c18cc532680b957267ee70b1037d)
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*04427e3bSAndrew Bennett  * Copyright (c) 2017, 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>
58*04427e3bSAndrew 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"
95*04427e3bSAndrew 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;
116*04427e3bSAndrew 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.			*/
137*04427e3bSAndrew Bennett static pid_t	*procs;		/* pids of children			*/
138*04427e3bSAndrew 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] "\
142*04427e3bSAndrew Bennett 	"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-P maxprocs] "\
143*04427e3bSAndrew 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 *);
147*04427e3bSAndrew 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();
156*04427e3bSAndrew Bennett static int	procs_find(pid_t child);
157*04427e3bSAndrew Bennett static void	procs_store(pid_t child);
158*04427e3bSAndrew Bennett static boolean_t procs_delete(pid_t child);
159*04427e3bSAndrew Bennett static pid_t	procs_waitpid(boolean_t blocking, int *stat_loc);
160*04427e3bSAndrew 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*04427e3bSAndrew Bennett 	unsigned 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;
173*04427e3bSAndrew 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:				*/
192*04427e3bSAndrew 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 
317*04427e3bSAndrew Bennett 		case 'P':	/* -P maxprocs: # of child processses	*/
318*04427e3bSAndrew Bennett 			errno = 0;
319*04427e3bSAndrew Bennett 			l = strtoul(optarg, &eptr, 10);
320*04427e3bSAndrew Bennett 			if (*eptr != '\0' || errno != 0) {
321*04427e3bSAndrew Bennett 				ermsg(_("failed to parse maxprocs (-P): %s\n"),
322*04427e3bSAndrew Bennett 				    optarg);
323*04427e3bSAndrew Bennett 				break;
324*04427e3bSAndrew Bennett 			}
325*04427e3bSAndrew Bennett 
326*04427e3bSAndrew Bennett 			/*
327*04427e3bSAndrew Bennett 			 * Come up with an upper bound that'll probably fit in
328*04427e3bSAndrew Bennett 			 * memory.
329*04427e3bSAndrew Bennett 			 */
330*04427e3bSAndrew Bennett 			if (l == 0 || l > ((INT_MAX / sizeof (pid_t) >> 1))) {
331*04427e3bSAndrew Bennett 				l = INT_MAX / sizeof (pid_t) >> 1;
332*04427e3bSAndrew Bennett 			}
333*04427e3bSAndrew Bennett 			MAXPROCS = (int)l;
334*04427e3bSAndrew Bennett 			break;
335*04427e3bSAndrew Bennett 
3367c478bd9Sstevel@tonic-gate 		case 's':	/* -s size: set max size of each arg list */
337952d685eScf 			BUFLIM = atoi(optarg);
338952d685eScf 			if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
33903fc8686SGarrett D'Amore 				ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
34003fc8686SGarrett D'Amore 				    BUFSIZE, optarg);
3417c478bd9Sstevel@tonic-gate 			}
3427c478bd9Sstevel@tonic-gate 			break;
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 		case 'x':	/* -x: terminate if args > size limit	*/
3457c478bd9Sstevel@tonic-gate 			DASHX = LEGAL = TRUE;
3467c478bd9Sstevel@tonic-gate 			break;
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate 		default:
3497c478bd9Sstevel@tonic-gate 			/*
3507c478bd9Sstevel@tonic-gate 			 * bad argument. complain and get ready to die.
3517c478bd9Sstevel@tonic-gate 			 */
3527c478bd9Sstevel@tonic-gate 			usage();
3537c478bd9Sstevel@tonic-gate 			exit(2);
3547c478bd9Sstevel@tonic-gate 			break;
3557c478bd9Sstevel@tonic-gate 		}
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	/*
3597c478bd9Sstevel@tonic-gate 	 * if anything called ermsg(), something screwed up, so
3607c478bd9Sstevel@tonic-gate 	 * we exit early.
3617c478bd9Sstevel@tonic-gate 	 */
3627c478bd9Sstevel@tonic-gate 	if (OK == FALSE) {
3637c478bd9Sstevel@tonic-gate 		usage();
3647c478bd9Sstevel@tonic-gate 		exit(2);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	/*
3687c478bd9Sstevel@tonic-gate 	 * we're finished handling xargs's options, so now pick up
3697c478bd9Sstevel@tonic-gate 	 * the command name (if any), and it's options.
3707c478bd9Sstevel@tonic-gate 	 */
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 
3737c478bd9Sstevel@tonic-gate 	mac -= optind;	/* dec arg count by what we've processed 	*/
3747c478bd9Sstevel@tonic-gate 	mav += optind;	/* inc to current mav				*/
3757c478bd9Sstevel@tonic-gate 
376*04427e3bSAndrew Bennett 	procs = calloc(MAXPROCS, sizeof (pid_t));
377*04427e3bSAndrew Bennett 	if (procs == NULL) {
378*04427e3bSAndrew Bennett 		PERR(MALLOCFAIL);
379*04427e3bSAndrew Bennett 		exit(1);
380*04427e3bSAndrew Bennett 	}
381*04427e3bSAndrew Bennett 
3827c478bd9Sstevel@tonic-gate 	if (mac <= 0) {	/* if there're no more args to process,	*/
3837c478bd9Sstevel@tonic-gate 		cmdname = "/usr/bin/echo";	/* our default command	*/
3847c478bd9Sstevel@tonic-gate 		*ARGV++ = addarg(cmdname);	/* use the default cmd.	*/
3857c478bd9Sstevel@tonic-gate 	} else {	/* otherwise keep parsing rest of the string.	*/
3867c478bd9Sstevel@tonic-gate 		/*
3877c478bd9Sstevel@tonic-gate 		 * note that we can't use getopts(3C), and *must* parse
3887c478bd9Sstevel@tonic-gate 		 * this by hand, as we don't know apriori what options the
3897c478bd9Sstevel@tonic-gate 		 * command will take.
3907c478bd9Sstevel@tonic-gate 		 */
3917c478bd9Sstevel@tonic-gate 		cmdname = *mav;	/* get the command name	*/
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 		/* pick up the remaining args from the command line:	*/
3957c478bd9Sstevel@tonic-gate 		while ((OK == TRUE) && (mac-- > 0)) {
3967c478bd9Sstevel@tonic-gate 			/*
3977c478bd9Sstevel@tonic-gate 			 * while we haven't crapped out, and there's
3987c478bd9Sstevel@tonic-gate 			 * work to do:
3997c478bd9Sstevel@tonic-gate 			 */
4007c478bd9Sstevel@tonic-gate 			if (INSERT && ! ERR) {
40103fc8686SGarrett D'Amore 				if (strstr(*mav, INSPAT) != NULL) {
4027c478bd9Sstevel@tonic-gate 					if (++n_inserts > MAXINSERTS) {
40303fc8686SGarrett D'Amore 						ermsg(_("too many args "
4047c478bd9Sstevel@tonic-gate 						    "with %s\n"), INSPAT);
4057c478bd9Sstevel@tonic-gate 						ERR = TRUE;
4067c478bd9Sstevel@tonic-gate 					}
4077c478bd9Sstevel@tonic-gate 					psave->p_ARGV = ARGV;
4087c478bd9Sstevel@tonic-gate 					(psave++)->p_skel = *mav;
4097c478bd9Sstevel@tonic-gate 				}
4107c478bd9Sstevel@tonic-gate 			}
4117c478bd9Sstevel@tonic-gate 			*ARGV++ = addarg(*mav++);
4127c478bd9Sstevel@tonic-gate 		}
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	/* pick up args from standard input */
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	initlist = ARGV;
4187c478bd9Sstevel@tonic-gate 	initsize = linesize;
41903fc8686SGarrett D'Amore 	lastarg[0] = '\0';
4207c478bd9Sstevel@tonic-gate 
42103fc8686SGarrett D'Amore 	while (OK) {
4227c478bd9Sstevel@tonic-gate 		N_args = 0;
4237c478bd9Sstevel@tonic-gate 		N_lines = 0;
4247c478bd9Sstevel@tonic-gate 		ARGV = initlist;
4257c478bd9Sstevel@tonic-gate 		linesize = initsize;
42603fc8686SGarrett D'Amore 		next = argbuf;
42703fc8686SGarrett D'Amore 
42803fc8686SGarrett D'Amore 		while (MORE || (lastarg[0] != '\0')) {
42903fc8686SGarrett D'Amore 			int l;
43003fc8686SGarrett D'Amore 
43103fc8686SGarrett D'Amore 			if (*lastarg != '\0') {
43203fc8686SGarrett D'Amore 				arg = strcpy(next, lastarg);
43303fc8686SGarrett D'Amore 				*lastarg = '\0';
43403fc8686SGarrett D'Amore 			} else if ((arg = getarg(next)) == NULL) {
43503fc8686SGarrett D'Amore 				break;
43603fc8686SGarrett D'Amore 			}
43703fc8686SGarrett D'Amore 
43803fc8686SGarrett D'Amore 			l = strlen(arg) + 1;
43903fc8686SGarrett D'Amore 			linesize += l;
44003fc8686SGarrett D'Amore 			next += l;
44103fc8686SGarrett D'Amore 
44203fc8686SGarrett D'Amore 			/* Inserts are handled specially later. */
44303fc8686SGarrett D'Amore 			if ((n_inserts == 0) && (linesize >= BUFLIM)) {
44403fc8686SGarrett D'Amore 				/*
44503fc8686SGarrett D'Amore 				 * Legal indicates hard fail if the list is
44603fc8686SGarrett D'Amore 				 * truncated due to size.  So fail, or if we
44703fc8686SGarrett D'Amore 				 * cannot create any list because it would be
44803fc8686SGarrett D'Amore 				 * too big.
44903fc8686SGarrett D'Amore 				 */
45003fc8686SGarrett D'Amore 				if (LEGAL || N_args == 0) {
45103fc8686SGarrett D'Amore 					EMSG(LIST2LONG);
452*04427e3bSAndrew Bennett 					procs_wait(B_TRUE);
45303fc8686SGarrett D'Amore 					exit(2);
45403fc8686SGarrett D'Amore 					/* NOTREACHED */
45503fc8686SGarrett D'Amore 				}
45603fc8686SGarrett D'Amore 
45703fc8686SGarrett D'Amore 				/*
45803fc8686SGarrett D'Amore 				 * Otherwise just save argument for later.
45903fc8686SGarrett D'Amore 				 */
46003fc8686SGarrett D'Amore 				(void) strcpy(lastarg, arg);
46103fc8686SGarrett D'Amore 				break;
46203fc8686SGarrett D'Amore 			}
46303fc8686SGarrett D'Amore 
46403fc8686SGarrett D'Amore 			*ARGV++ = arg;
46503fc8686SGarrett D'Amore 
46603fc8686SGarrett D'Amore 			N_args++;
46703fc8686SGarrett D'Amore 
468826ac02aSGarrett D'Amore 			if ((PER_LINE && (N_lines >= PER_LINE)) ||
469826ac02aSGarrett D'Amore 			    (N_ARGS && (N_args >= N_ARGS))) {
47003fc8686SGarrett D'Amore 				break;
47103fc8686SGarrett D'Amore 			}
47203fc8686SGarrett D'Amore 
4737c478bd9Sstevel@tonic-gate 
474a035dc19Scf 			if ((ARGV - arglist) == MAXARGS) {
475a035dc19Scf 				break;
476a035dc19Scf 			}
477a035dc19Scf 		}
47803fc8686SGarrett D'Amore 
47903fc8686SGarrett D'Amore 		*ARGV = NULL;
48003fc8686SGarrett D'Amore 		if (N_args == 0) {
48103fc8686SGarrett D'Amore 			/* Reached the end with no more work. */
482*04427e3bSAndrew Bennett 			break;
483a035dc19Scf 		}
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		/* insert arg if requested */
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 		if (!ERR && INSERT) {
48803fc8686SGarrett D'Amore 
4897c478bd9Sstevel@tonic-gate 			p_ibuf = ins_buf;
4907c478bd9Sstevel@tonic-gate 			ARGV--;
4917c478bd9Sstevel@tonic-gate 			j = ibufsize = 0;
4927c478bd9Sstevel@tonic-gate 			for (psave = saveargv; ++j <= n_inserts; ++psave) {
4937c478bd9Sstevel@tonic-gate 				addibuf(psave);
4947c478bd9Sstevel@tonic-gate 				if (ERR)
4957c478bd9Sstevel@tonic-gate 					break;
4967c478bd9Sstevel@tonic-gate 			}
4977c478bd9Sstevel@tonic-gate 		}
49803fc8686SGarrett D'Amore 		*ARGV = NULL;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 		if (n_inserts > 0) {
5017c478bd9Sstevel@tonic-gate 			/*
5027c478bd9Sstevel@tonic-gate 			 * if we've done any insertions, re-calculate the
5037c478bd9Sstevel@tonic-gate 			 * linesize. bomb out if we've exceeded our length.
5047c478bd9Sstevel@tonic-gate 			 */
50503fc8686SGarrett D'Amore 			linesize = 0;
5067c478bd9Sstevel@tonic-gate 			for (ARGV = arglist; *ARGV != NULL; ARGV++) {
50703fc8686SGarrett D'Amore 				linesize += strlen(*ARGV) + 1;
50803fc8686SGarrett D'Amore 			}
50903fc8686SGarrett D'Amore 			if (linesize >= BUFLIM) {
51003fc8686SGarrett D'Amore 				EMSG(LIST2LONG);
511*04427e3bSAndrew Bennett 				procs_wait(B_TRUE);
51203fc8686SGarrett D'Amore 				exit(2);
51303fc8686SGarrett D'Amore 				/* NOTREACHED */
5147c478bd9Sstevel@tonic-gate 			}
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 		/* exec command */
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate 		if (!ERR) {
5207c478bd9Sstevel@tonic-gate 			if (!MORE &&
5217c478bd9Sstevel@tonic-gate 			    (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
5227c478bd9Sstevel@tonic-gate 				exit(exitstat);
5237c478bd9Sstevel@tonic-gate 			OK = TRUE;
5247c478bd9Sstevel@tonic-gate 			j = TRACE ? echoargs() : TRUE;
5257c478bd9Sstevel@tonic-gate 			if (j) {
5267c478bd9Sstevel@tonic-gate 				/*
5277c478bd9Sstevel@tonic-gate 				 * for xcu4, all invocations of cmdname must
5287c478bd9Sstevel@tonic-gate 				 * return 0, in order for us to return 0.
5297c478bd9Sstevel@tonic-gate 				 * so if we have a non-zero status here,
5307c478bd9Sstevel@tonic-gate 				 * quit immediately.
5317c478bd9Sstevel@tonic-gate 				 */
532*04427e3bSAndrew Bennett 				(void) lcall(cmdname, arglist);
5337c478bd9Sstevel@tonic-gate 			}
5347c478bd9Sstevel@tonic-gate 		}
5357c478bd9Sstevel@tonic-gate 	}
5367c478bd9Sstevel@tonic-gate 
537*04427e3bSAndrew Bennett 	procs_wait(B_TRUE);
538*04427e3bSAndrew Bennett 
53903fc8686SGarrett D'Amore 	if (OK)
54043a29105Srobbin 		return (exitstat);
5417c478bd9Sstevel@tonic-gate 
54203fc8686SGarrett D'Amore 	/*
54303fc8686SGarrett D'Amore 	 * if exitstat was set, to match XCU4 complience,
54403fc8686SGarrett D'Amore 	 * return that value, otherwise, return 1.
54503fc8686SGarrett D'Amore 	 */
54603fc8686SGarrett D'Amore 	return (exitstat ? exitstat : 1);
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate static char *
5507c478bd9Sstevel@tonic-gate addarg(char *arg)
5517c478bd9Sstevel@tonic-gate {
55203fc8686SGarrett D'Amore 	linesize += (strlen(arg) + 1);
55303fc8686SGarrett D'Amore 	return (arg);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 
55703fc8686SGarrett D'Amore static void
55803fc8686SGarrett D'Amore store_str(char **buffer, char *str, size_t len)
55903fc8686SGarrett D'Amore {
56003fc8686SGarrett D'Amore 	(void) memcpy(*buffer, str, len);
56103fc8686SGarrett D'Amore 	(*buffer)[len] = '\0';
56203fc8686SGarrett D'Amore 	*buffer += len;
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
56503fc8686SGarrett D'Amore 
5667c478bd9Sstevel@tonic-gate static char *
56703fc8686SGarrett D'Amore getarg(char *arg)
5687c478bd9Sstevel@tonic-gate {
56903fc8686SGarrett D'Amore 	char	*xarg = arg;
570826ac02aSGarrett D'Amore 	wchar_t	c = 0;
5717c478bd9Sstevel@tonic-gate 	char	mbc[MB_LEN_MAX];
57203fc8686SGarrett D'Amore 	size_t	len;
57303fc8686SGarrett D'Amore 	int	escape = 0;
57403fc8686SGarrett D'Amore 	int	inquote = 0;
575826ac02aSGarrett D'Amore 	int	last = 0;
5767c478bd9Sstevel@tonic-gate 
57703fc8686SGarrett D'Amore 	arg[0] = '\0';
5787c478bd9Sstevel@tonic-gate 
57903fc8686SGarrett D'Amore 	while (MORE) {
5807c478bd9Sstevel@tonic-gate 
58103fc8686SGarrett D'Amore 		len = 0;
582826ac02aSGarrett D'Amore 		last = c;
58303fc8686SGarrett D'Amore 		c = getwchr(mbc, &len);
5847c478bd9Sstevel@tonic-gate 
58503fc8686SGarrett D'Amore 		if (((arg - xarg) + len) > BUFLIM) {
58603fc8686SGarrett D'Amore 			EMSG2(ARG2LONG, BUFLIM);
58703fc8686SGarrett D'Amore 			exit(2);
58803fc8686SGarrett D'Amore 			ERR = TRUE;
5897c478bd9Sstevel@tonic-gate 			return (NULL);
5907c478bd9Sstevel@tonic-gate 		}
5917c478bd9Sstevel@tonic-gate 
59203fc8686SGarrett D'Amore 		switch (c) {
59303fc8686SGarrett D'Amore 		case '\n':
59403fc8686SGarrett D'Amore 			if (ZERO) {
59503fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
5967c478bd9Sstevel@tonic-gate 				continue;
5977c478bd9Sstevel@tonic-gate 			}
598826ac02aSGarrett D'Amore 			/*
599826ac02aSGarrett D'Amore 			 * NB: Some other versions rip off all of the trailing
600826ac02aSGarrett D'Amore 			 * blanks.  The spec only claims that this should
601826ac02aSGarrett D'Amore 			 * be done for a single blank.  We follow the spec.
602826ac02aSGarrett D'Amore 			 */
603826ac02aSGarrett D'Amore 			if (LINE_CONT && iswctype(last, blank)) {
604826ac02aSGarrett D'Amore 				len = 0;
605826ac02aSGarrett D'Amore 				*arg = 0;
606826ac02aSGarrett D'Amore 				continue;
607826ac02aSGarrett D'Amore 			}
60803fc8686SGarrett D'Amore 			/* FALLTHRU */
6097c478bd9Sstevel@tonic-gate 
61003fc8686SGarrett D'Amore 		case '\0':
61103fc8686SGarrett D'Amore 		case WEOF:	/* Note WEOF == EOF */
6127c478bd9Sstevel@tonic-gate 
61303fc8686SGarrett D'Amore 			if (escape) {
61403fc8686SGarrett D'Amore 				EMSG(BADESCAPE);
61503fc8686SGarrett D'Amore 				ERR = TRUE;
61603fc8686SGarrett D'Amore 				return (NULL);
6177c478bd9Sstevel@tonic-gate 			}
6187c478bd9Sstevel@tonic-gate 			if (inquote) {
61903fc8686SGarrett D'Amore 				EMSG(MISSQUOTE);
6207c478bd9Sstevel@tonic-gate 				ERR = TRUE;
62103fc8686SGarrett D'Amore 				return (NULL);
6227c478bd9Sstevel@tonic-gate 			}
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 			N_lines++;
62503fc8686SGarrett D'Amore 			break;
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		case '"':
62803fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 1)) {
62903fc8686SGarrett D'Amore 				/* treat it literally */
63003fc8686SGarrett D'Amore 				escape = 0;
63103fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
63203fc8686SGarrett D'Amore 
63303fc8686SGarrett D'Amore 			} else if (inquote == 2) {
63403fc8686SGarrett D'Amore 				/* terminating double quote */
6357c478bd9Sstevel@tonic-gate 				inquote = 0;
63603fc8686SGarrett D'Amore 
63703fc8686SGarrett D'Amore 			} else {
63803fc8686SGarrett D'Amore 				/* starting quoted string */
6397c478bd9Sstevel@tonic-gate 				inquote = 2;
64003fc8686SGarrett D'Amore 			}
64103fc8686SGarrett D'Amore 			continue;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		case '\'':
64403fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 2)) {
64503fc8686SGarrett D'Amore 				/* treat it literally */
64603fc8686SGarrett D'Amore 				escape = 0;
64703fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
64803fc8686SGarrett D'Amore 
64903fc8686SGarrett D'Amore 			} else if (inquote == 1) {
65003fc8686SGarrett D'Amore 				/* terminating single quote */
6517c478bd9Sstevel@tonic-gate 				inquote = 0;
65203fc8686SGarrett D'Amore 
65303fc8686SGarrett D'Amore 			} else {
65403fc8686SGarrett D'Amore 				/* starting quoted string */
6557c478bd9Sstevel@tonic-gate 				inquote = 1;
65603fc8686SGarrett D'Amore 			}
65703fc8686SGarrett D'Amore 			continue;
6587c478bd9Sstevel@tonic-gate 
65903fc8686SGarrett D'Amore 		case '\\':
660d2afb7a9Scf 			/*
661d2afb7a9Scf 			 * Any unquoted character can be escaped by
662d2afb7a9Scf 			 * preceding it with a backslash.
663d2afb7a9Scf 			 */
66403fc8686SGarrett D'Amore 			if (ZERO || inquote || escape) {
66503fc8686SGarrett D'Amore 				escape = 0;
66603fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
66703fc8686SGarrett D'Amore 			} else {
66803fc8686SGarrett D'Amore 				escape = 1;
669d2afb7a9Scf 			}
67003fc8686SGarrett D'Amore 			continue;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 		default:
67303fc8686SGarrett D'Amore 			/* most times we will just want to store it */
67403fc8686SGarrett D'Amore 			if (inquote || escape || ZERO || !iswctype(c, blank)) {
67503fc8686SGarrett D'Amore 				escape = 0;
67603fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
67703fc8686SGarrett D'Amore 				continue;
6787c478bd9Sstevel@tonic-gate 			}
679826ac02aSGarrett D'Amore 			if (EAT_LEAD && last == 0) {
680826ac02aSGarrett D'Amore 				c = 0;		/* Roll it back */
681826ac02aSGarrett D'Amore 				continue;
682826ac02aSGarrett D'Amore 			}
683826ac02aSGarrett D'Amore 			if (PER_LINE) {
684826ac02aSGarrett D'Amore 				store_str(&arg, mbc, len);
685826ac02aSGarrett D'Amore 				continue;
686826ac02aSGarrett D'Amore 			}
687826ac02aSGarrett D'Amore 
688826ac02aSGarrett D'Amore 			/* unquoted blank without special handling */
6897c478bd9Sstevel@tonic-gate 			break;
6907c478bd9Sstevel@tonic-gate 		}
69103fc8686SGarrett D'Amore 
69203fc8686SGarrett D'Amore 		/*
69303fc8686SGarrett D'Amore 		 * At this point we are processing a complete argument.
69403fc8686SGarrett D'Amore 		 */
69503fc8686SGarrett D'Amore 		if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') {
69603fc8686SGarrett D'Amore 			MORE = FALSE;
69703fc8686SGarrett D'Amore 			return (NULL);
69803fc8686SGarrett D'Amore 		}
69903fc8686SGarrett D'Amore 		if (c == WEOF) {
70003fc8686SGarrett D'Amore 			MORE = FALSE;
70103fc8686SGarrett D'Amore 		}
70203fc8686SGarrett D'Amore 		if (xarg[0] == '\0')
70303fc8686SGarrett D'Amore 			continue;
70403fc8686SGarrett D'Amore 		break;
7057c478bd9Sstevel@tonic-gate 	}
7067c478bd9Sstevel@tonic-gate 
70703fc8686SGarrett D'Amore 	return (xarg[0] == '\0' ? NULL : xarg);
70803fc8686SGarrett D'Amore }
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate /*
7117c478bd9Sstevel@tonic-gate  * ermsg():	print out an error message, and indicate failure globally.
7127c478bd9Sstevel@tonic-gate  *
7137c478bd9Sstevel@tonic-gate  *	Assumes that message has already been gettext()'d. It would be
7147c478bd9Sstevel@tonic-gate  *	nice if we could just do the gettext() here, but we can't, since
7157c478bd9Sstevel@tonic-gate  *	since xgettext(1M) wouldn't be able to pick up our error message.
7167c478bd9Sstevel@tonic-gate  */
7177c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
7187c478bd9Sstevel@tonic-gate static void
7197c478bd9Sstevel@tonic-gate ermsg(char *messages, ...)
7207c478bd9Sstevel@tonic-gate {
7217c478bd9Sstevel@tonic-gate 	va_list	ap;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	va_start(ap, messages);
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "xargs: ");
7267c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, messages, ap);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	va_end(ap);
7297c478bd9Sstevel@tonic-gate 	OK = FALSE;
7307c478bd9Sstevel@tonic-gate }
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate static int
7337c478bd9Sstevel@tonic-gate echoargs()
7347c478bd9Sstevel@tonic-gate {
7357c478bd9Sstevel@tonic-gate 	char	**anarg;
7367c478bd9Sstevel@tonic-gate 	char	**tanarg;	/* tmp ptr			*/
7377c478bd9Sstevel@tonic-gate 	int		i;
7387c478bd9Sstevel@tonic-gate 	char		reply[LINE_MAX];
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	tanarg = anarg = arglist-1;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	/*
7437c478bd9Sstevel@tonic-gate 	 * write out each argument, separated by a space. the tanarg
7447c478bd9Sstevel@tonic-gate 	 * nonsense is for xcu4 testsuite compliance - so that an
7457c478bd9Sstevel@tonic-gate 	 * extra space isn't echoed after the last argument.
7467c478bd9Sstevel@tonic-gate 	 */
7477c478bd9Sstevel@tonic-gate 	while (*++anarg) {		/* while there's an argument	*/
7487c478bd9Sstevel@tonic-gate 		++tanarg;		/* follow anarg			*/
7497c478bd9Sstevel@tonic-gate 		(void) write(2, *anarg, strlen(*anarg));
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		if (*++tanarg) {	/* if there's another argument:	*/
7527c478bd9Sstevel@tonic-gate 			(void) write(2, " ", 1); /* add a space		*/
7537c478bd9Sstevel@tonic-gate 			--tanarg;	/* reset back to anarg		*/
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 	if (PROMPT == -1) {
7577c478bd9Sstevel@tonic-gate 		(void) write(2, "\n", 1);
7587c478bd9Sstevel@tonic-gate 		return (TRUE);
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate 	(void) write(2, "?...", 4);	/* ask the user for input	*/
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 	for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
7647c478bd9Sstevel@tonic-gate 		if (reply[i] == '\n') {
7657c478bd9Sstevel@tonic-gate 			if (i == 0)
7667c478bd9Sstevel@tonic-gate 				return (FALSE);
7677c478bd9Sstevel@tonic-gate 			break;
7687c478bd9Sstevel@tonic-gate 		}
7697c478bd9Sstevel@tonic-gate 	}
7707c478bd9Sstevel@tonic-gate 	reply[i] = 0;
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	/* flush remainder of line if necessary */
7737c478bd9Sstevel@tonic-gate 	if (i == LINE_MAX) {
7747c478bd9Sstevel@tonic-gate 		char	bitbucket;
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 		while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
7777c478bd9Sstevel@tonic-gate 			;
7787c478bd9Sstevel@tonic-gate 	}
7797c478bd9Sstevel@tonic-gate 
7803d63ea05Sas 	return (yes_check(reply));
7817c478bd9Sstevel@tonic-gate }
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate static char *
7857c478bd9Sstevel@tonic-gate insert(char *pattern, char *subst)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate 	static char	buffer[MAXSBUF+1];
7887c478bd9Sstevel@tonic-gate 	int		len, ipatlen;
7897c478bd9Sstevel@tonic-gate 	char	*pat;
7907c478bd9Sstevel@tonic-gate 	char	*bufend;
7917c478bd9Sstevel@tonic-gate 	char	*pbuf;
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	len = strlen(subst);
7947c478bd9Sstevel@tonic-gate 	ipatlen = strlen(INSPAT) - 1;
7957c478bd9Sstevel@tonic-gate 	pat = pattern - 1;
7967c478bd9Sstevel@tonic-gate 	pbuf = buffer;
7977c478bd9Sstevel@tonic-gate 	bufend = &buffer[MAXSBUF];
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	while (*++pat) {
800826ac02aSGarrett D'Amore 		if (strncmp(pat, INSPAT, ipatlen + 1) == 0) {
8017c478bd9Sstevel@tonic-gate 			if (pbuf + len >= bufend) {
8027c478bd9Sstevel@tonic-gate 				break;
8037c478bd9Sstevel@tonic-gate 			} else {
8047c478bd9Sstevel@tonic-gate 				(void) strcpy(pbuf, subst);
8057c478bd9Sstevel@tonic-gate 				pat += ipatlen;
8067c478bd9Sstevel@tonic-gate 				pbuf += len;
8077c478bd9Sstevel@tonic-gate 			}
8087c478bd9Sstevel@tonic-gate 		} else {
8097c478bd9Sstevel@tonic-gate 			*pbuf++ = *pat;
8107c478bd9Sstevel@tonic-gate 			if (pbuf >= bufend)
8117c478bd9Sstevel@tonic-gate 				break;
8127c478bd9Sstevel@tonic-gate 		}
8137c478bd9Sstevel@tonic-gate 	}
8147c478bd9Sstevel@tonic-gate 
8157c478bd9Sstevel@tonic-gate 	if (!*pat) {
8167c478bd9Sstevel@tonic-gate 		*pbuf = '\0';
8177c478bd9Sstevel@tonic-gate 		return (buffer);
8187c478bd9Sstevel@tonic-gate 	} else {
8197c478bd9Sstevel@tonic-gate 		ermsg(gettext("Maximum argument size with insertion via %s's "
8207c478bd9Sstevel@tonic-gate 		    "exceeded\n"), INSPAT);
8217c478bd9Sstevel@tonic-gate 		ERR = TRUE;
82203fc8686SGarrett D'Amore 		return (NULL);
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate static void
8287c478bd9Sstevel@tonic-gate addibuf(struct inserts	*p)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	char	*newarg, *skel, *sub;
8317c478bd9Sstevel@tonic-gate 	int		l;
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	skel = p->p_skel;
8347c478bd9Sstevel@tonic-gate 	sub = *ARGV;
8357c478bd9Sstevel@tonic-gate 	newarg = insert(skel, sub);
8367c478bd9Sstevel@tonic-gate 	if (ERR)
837788f581bSceastha 		return;
8387c478bd9Sstevel@tonic-gate 
83903fc8686SGarrett D'Amore 	l = strlen(newarg) + 1;
84003fc8686SGarrett D'Amore 	if ((ibufsize += l) > MAXIBUF) {
84103fc8686SGarrett D'Amore 		EMSG(IBUFOVERFLOW);
84203fc8686SGarrett D'Amore 		ERR = TRUE;
8437c478bd9Sstevel@tonic-gate 	}
84403fc8686SGarrett D'Amore 	(void) strcpy(p_ibuf, newarg);
84503fc8686SGarrett D'Amore 	*(p->p_ARGV) = p_ibuf;
84603fc8686SGarrett D'Amore 	p_ibuf += l;
8477c478bd9Sstevel@tonic-gate }
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /*
85103fc8686SGarrett D'Amore  * getwchr():	get the next wide character.
8527c478bd9Sstevel@tonic-gate  * description:
85303fc8686SGarrett D'Amore  *	we get the next character from stdin.  This returns WEOF if no
85403fc8686SGarrett D'Amore  *	character is present.  If ZERO is set, it gets a single byte instead
85503fc8686SGarrett D'Amore  *	a wide character.
8567c478bd9Sstevel@tonic-gate  */
85703fc8686SGarrett D'Amore static wint_t
85803fc8686SGarrett D'Amore getwchr(char *mbc, size_t *sz)
8597c478bd9Sstevel@tonic-gate {
86003fc8686SGarrett D'Amore 	size_t		i;
86103fc8686SGarrett D'Amore 	int		c;
86203fc8686SGarrett D'Amore 	wchar_t		wch;
8637c478bd9Sstevel@tonic-gate 
86403fc8686SGarrett D'Amore 	i = 0;
86503fc8686SGarrett D'Amore 	while (i < MB_CUR_MAX) {
8667c478bd9Sstevel@tonic-gate 
86703fc8686SGarrett D'Amore 		if ((c = fgetc(stdin)) == EOF) {
8687c478bd9Sstevel@tonic-gate 
86903fc8686SGarrett D'Amore 			if (i == 0) {
8707c478bd9Sstevel@tonic-gate 				/* TRUE EOF has been reached */
87103fc8686SGarrett D'Amore 				return (WEOF);
8727c478bd9Sstevel@tonic-gate 			}
87303fc8686SGarrett D'Amore 
8747c478bd9Sstevel@tonic-gate 			/*
8757c478bd9Sstevel@tonic-gate 			 * We have some characters in our buffer still so it
8767c478bd9Sstevel@tonic-gate 			 * must be an invalid character right before EOF.
8777c478bd9Sstevel@tonic-gate 			 */
8787c478bd9Sstevel@tonic-gate 			break;
8797c478bd9Sstevel@tonic-gate 		}
88003fc8686SGarrett D'Amore 		mbc[i++] = (char)c;
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 		/* If this succeeds then we are done */
88303fc8686SGarrett D'Amore 		if (ZERO) {
88403fc8686SGarrett D'Amore 			*sz = i;
88503fc8686SGarrett D'Amore 			return ((char)c);
88603fc8686SGarrett D'Amore 		}
88703fc8686SGarrett D'Amore 		if (mbtowc(&wch, mbc, i) != -1) {
88803fc8686SGarrett D'Amore 			*sz = i;
88903fc8686SGarrett D'Amore 			return ((wint_t)wch);
89003fc8686SGarrett D'Amore 		}
8917c478bd9Sstevel@tonic-gate 	}
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 	/*
8947c478bd9Sstevel@tonic-gate 	 * We have now encountered an illegal character sequence.
8957c478bd9Sstevel@tonic-gate 	 * There is nothing much we can do at this point but
8967c478bd9Sstevel@tonic-gate 	 * return an error.  If we attempt to recover we may in fact
8977c478bd9Sstevel@tonic-gate 	 * return garbage as arguments, from the customer's point
8987c478bd9Sstevel@tonic-gate 	 * of view.  After all what if they are feeding us a file
8997c478bd9Sstevel@tonic-gate 	 * generated in another locale?
9007c478bd9Sstevel@tonic-gate 	 */
9017c478bd9Sstevel@tonic-gate 	errno = EILSEQ;
90203fc8686SGarrett D'Amore 	PERR(CORRUPTFILE);
9037c478bd9Sstevel@tonic-gate 	exit(1);
9047c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
9057c478bd9Sstevel@tonic-gate }
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 
908*04427e3bSAndrew Bennett static void
9097c478bd9Sstevel@tonic-gate lcall(char *sub, char **subargs)
9107c478bd9Sstevel@tonic-gate {
911*04427e3bSAndrew Bennett 	int	retry = 0;
912*04427e3bSAndrew Bennett 	pid_t	child;
9137c478bd9Sstevel@tonic-gate 
91403fc8686SGarrett D'Amore 	for (;;) {
915*04427e3bSAndrew Bennett 		switch (child = forkx(FORK_NOSIGCHLD)) {
9167c478bd9Sstevel@tonic-gate 		default:
917*04427e3bSAndrew Bennett 			procs_store(child);
918*04427e3bSAndrew Bennett 			/*
919*04427e3bSAndrew Bennett 			 * Note, if we have used up all of our slots, then this
920*04427e3bSAndrew Bennett 			 * call may end up blocking.
921*04427e3bSAndrew Bennett 			 */
922*04427e3bSAndrew Bennett 			procs_wait(B_FALSE);
923*04427e3bSAndrew Bennett 			return;
9247c478bd9Sstevel@tonic-gate 		case 0:
9257c478bd9Sstevel@tonic-gate 			(void) execvp(sub, subargs);
92603fc8686SGarrett D'Amore 			PERR(EXECFAIL);
9277c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
9287c478bd9Sstevel@tonic-gate 				exit(126);
9297c478bd9Sstevel@tonic-gate 			exit(127);
9307c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
9317c478bd9Sstevel@tonic-gate 		case -1:
9327c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN && retry++ < FORK_RETRY) {
93303fc8686SGarrett D'Amore 				PERR(FORKFAIL);
9347c478bd9Sstevel@tonic-gate 				exit(123);
9357c478bd9Sstevel@tonic-gate 			}
9367c478bd9Sstevel@tonic-gate 			(void) sleep(1);
9377c478bd9Sstevel@tonic-gate 		}
9387c478bd9Sstevel@tonic-gate 	}
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
941*04427e3bSAndrew Bennett /*
942*04427e3bSAndrew Bennett  * Return the index of child in the procs array.
943*04427e3bSAndrew Bennett  */
944*04427e3bSAndrew Bennett static int
945*04427e3bSAndrew Bennett procs_find(pid_t child)
946*04427e3bSAndrew Bennett {
947*04427e3bSAndrew Bennett 	int	i;
948*04427e3bSAndrew Bennett 
949*04427e3bSAndrew Bennett 	for (i = 0; i < MAXPROCS; i++) {
950*04427e3bSAndrew Bennett 		if (procs[i] == child) {
951*04427e3bSAndrew Bennett 			return (i);
952*04427e3bSAndrew Bennett 		}
953*04427e3bSAndrew Bennett 	}
954*04427e3bSAndrew Bennett 
955*04427e3bSAndrew Bennett 	return (-1);
956*04427e3bSAndrew Bennett }
957*04427e3bSAndrew Bennett 
958*04427e3bSAndrew Bennett static void
959*04427e3bSAndrew Bennett procs_store(pid_t child)
960*04427e3bSAndrew Bennett {
961*04427e3bSAndrew Bennett 	int	i;
962*04427e3bSAndrew Bennett 
963*04427e3bSAndrew Bennett 	i = procs_find(0);
964*04427e3bSAndrew Bennett 	if (i < 0) {
965*04427e3bSAndrew Bennett 		EMSG(NOCHILDSLOT);
966*04427e3bSAndrew Bennett 		exit(1);
967*04427e3bSAndrew Bennett 	}
968*04427e3bSAndrew Bennett 	procs[i] = child;
969*04427e3bSAndrew Bennett 	n_procs++;
970*04427e3bSAndrew Bennett }
971*04427e3bSAndrew Bennett 
972*04427e3bSAndrew Bennett static boolean_t
973*04427e3bSAndrew Bennett procs_delete(pid_t child)
974*04427e3bSAndrew Bennett {
975*04427e3bSAndrew Bennett 	int	i;
976*04427e3bSAndrew Bennett 
977*04427e3bSAndrew Bennett 	i = procs_find(child);
978*04427e3bSAndrew Bennett 	if (i < 0) {
979*04427e3bSAndrew Bennett 		return (B_FALSE);
980*04427e3bSAndrew Bennett 	}
981*04427e3bSAndrew Bennett 
982*04427e3bSAndrew Bennett 	procs[i] = (pid_t)0;
983*04427e3bSAndrew Bennett 	n_procs--;
984*04427e3bSAndrew Bennett 
985*04427e3bSAndrew Bennett 	return (B_TRUE);
986*04427e3bSAndrew Bennett }
987*04427e3bSAndrew Bennett 
988*04427e3bSAndrew Bennett static pid_t
989*04427e3bSAndrew Bennett procs_waitpid(boolean_t blocking, int *stat_loc)
990*04427e3bSAndrew Bennett {
991*04427e3bSAndrew Bennett 	pid_t	child;
992*04427e3bSAndrew Bennett 	int	options;
993*04427e3bSAndrew Bennett 
994*04427e3bSAndrew Bennett 	if (n_procs == 0) {
995*04427e3bSAndrew Bennett 		errno = ECHILD;
996*04427e3bSAndrew Bennett 		return (-1);
997*04427e3bSAndrew Bennett 	}
998*04427e3bSAndrew Bennett 
999*04427e3bSAndrew Bennett 	options = 0;
1000*04427e3bSAndrew Bennett 	if (!blocking) {
1001*04427e3bSAndrew Bennett 		options |= WNOHANG;
1002*04427e3bSAndrew Bennett 	}
1003*04427e3bSAndrew Bennett 
1004*04427e3bSAndrew Bennett 	while ((child = waitpid((pid_t)-1, stat_loc, options)) > 0) {
1005*04427e3bSAndrew Bennett 		if (procs_delete(child)) {
1006*04427e3bSAndrew Bennett 			break;
1007*04427e3bSAndrew Bennett 		}
1008*04427e3bSAndrew Bennett 	}
1009*04427e3bSAndrew Bennett 
1010*04427e3bSAndrew Bennett 	return (child);
1011*04427e3bSAndrew Bennett }
1012*04427e3bSAndrew Bennett 
1013*04427e3bSAndrew Bennett static void
1014*04427e3bSAndrew Bennett procs_wait(boolean_t blocking)
1015*04427e3bSAndrew Bennett {
1016*04427e3bSAndrew Bennett 	pid_t	child;
1017*04427e3bSAndrew Bennett 	int	stat_loc;
1018*04427e3bSAndrew Bennett 
1019*04427e3bSAndrew Bennett 	/*
1020*04427e3bSAndrew Bennett 	 * If we currently have filled all of our slots, then we need to block
1021*04427e3bSAndrew Bennett 	 * further execution.
1022*04427e3bSAndrew Bennett 	 */
1023*04427e3bSAndrew Bennett 	if (n_procs >= MAXPROCS)
1024*04427e3bSAndrew Bennett 		blocking = B_TRUE;
1025*04427e3bSAndrew Bennett 	while ((child = procs_waitpid(blocking, &stat_loc)) > 0) {
1026*04427e3bSAndrew Bennett 		if (WIFSIGNALED(stat_loc)) {
1027*04427e3bSAndrew Bennett 			EMSG2(CHILDSIG, WTERMSIG(stat_loc));
1028*04427e3bSAndrew Bennett 			exit(125);
1029*04427e3bSAndrew Bennett 			/* NOTREACHED */
1030*04427e3bSAndrew Bennett 		} else if ((WEXITSTATUS(stat_loc) & 0377) == 0377) {
1031*04427e3bSAndrew Bennett 			EMSG(CHILDFAIL);
1032*04427e3bSAndrew Bennett 			exit(124);
1033*04427e3bSAndrew Bennett 			/* NOTREACHED */
1034*04427e3bSAndrew Bennett 		} else {
1035*04427e3bSAndrew Bennett 			exitstat |= WEXITSTATUS(stat_loc);
1036*04427e3bSAndrew Bennett 		}
1037*04427e3bSAndrew Bennett 	}
1038*04427e3bSAndrew Bennett 
1039*04427e3bSAndrew Bennett 	if (child == (pid_t)(-1) && errno != ECHILD) {
1040*04427e3bSAndrew Bennett 		EMSG(WAITFAIL);
1041*04427e3bSAndrew Bennett 		exit(122);
1042*04427e3bSAndrew Bennett 		/* NOTREACHED */
1043*04427e3bSAndrew Bennett 	}
1044*04427e3bSAndrew Bennett }
10457c478bd9Sstevel@tonic-gate 
10467c478bd9Sstevel@tonic-gate static void
10477c478bd9Sstevel@tonic-gate usage()
10487c478bd9Sstevel@tonic-gate {
104903fc8686SGarrett D'Amore 	ermsg(_(USAGEMSG));
10507c478bd9Sstevel@tonic-gate 	OK = FALSE;
10517c478bd9Sstevel@tonic-gate }
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate /*
10567c478bd9Sstevel@tonic-gate  * parseargs():		modify the args
10577c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
10587c478bd9Sstevel@tonic-gate  *	and getopts(3C) is clueless about this nonsense, we change the
10597c478bd9Sstevel@tonic-gate  *	our local argument count and strings to separate this out,
10607c478bd9Sstevel@tonic-gate  *	and make it easier to handle via getopts(3c).
10617c478bd9Sstevel@tonic-gate  *
10627c478bd9Sstevel@tonic-gate  *	-e	-> "-e ""
10637c478bd9Sstevel@tonic-gate  *	-e3	-> "-e "3"
10647c478bd9Sstevel@tonic-gate  *	-Estr	-> "-E "str"
10657c478bd9Sstevel@tonic-gate  *	-i	-> "-i "{}"
10667c478bd9Sstevel@tonic-gate  *	-irep	-> "-i "rep"
1067826ac02aSGarrett D'Amore  *	-l	-> "-l "1"
1068826ac02aSGarrett D'Amore  *	-l10	-> "-l "10"
10697c478bd9Sstevel@tonic-gate  *
10707c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
10717c478bd9Sstevel@tonic-gate  */
10727c478bd9Sstevel@tonic-gate static void
10737c478bd9Sstevel@tonic-gate parseargs(int ac, char **av)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
10767c478bd9Sstevel@tonic-gate 	int cflag;		/* 0 = not processing cmd arg		*/
10777c478bd9Sstevel@tonic-gate 
10787c478bd9Sstevel@tonic-gate 	if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
107903fc8686SGarrett D'Amore 		PERR(MALLOCFAIL);
10807c478bd9Sstevel@tonic-gate 		exit(1);
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	/* for each argument, see if we need to change things:		*/
10847c478bd9Sstevel@tonic-gate 	for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
10857c478bd9Sstevel@tonic-gate 		if ((mav[mac] = strdup(av[i])) == NULL) {
108603fc8686SGarrett D'Amore 			PERR(MALLOCFAIL);
10877c478bd9Sstevel@tonic-gate 			exit(1);
10887c478bd9Sstevel@tonic-gate 		}
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 		/* -- has been found or argument list is fully processes */
10917c478bd9Sstevel@tonic-gate 		if (cflag)
10927c478bd9Sstevel@tonic-gate 			continue;
10937c478bd9Sstevel@tonic-gate 
10947c478bd9Sstevel@tonic-gate 		/*
10957c478bd9Sstevel@tonic-gate 		 * if we're doing special processing, and we've got a flag
10967c478bd9Sstevel@tonic-gate 		 */
10977c478bd9Sstevel@tonic-gate 		else if ((av[i][0] == '-') && (av[i][1] != NULL)) {
10987c478bd9Sstevel@tonic-gate 			char	*def;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 			switch (av[i][1]) {
11017c478bd9Sstevel@tonic-gate 			case	'e':
11027c478bd9Sstevel@tonic-gate 				def = ""; /* -e with no arg turns off eof */
11037c478bd9Sstevel@tonic-gate 				goto process_special;
11047c478bd9Sstevel@tonic-gate 			case	'i':
11057c478bd9Sstevel@tonic-gate 				def = INSPAT_STR;
11067c478bd9Sstevel@tonic-gate 				goto process_special;
11077c478bd9Sstevel@tonic-gate 			case	'l':
11087c478bd9Sstevel@tonic-gate 				def = "1";
11097c478bd9Sstevel@tonic-gate process_special:
11107c478bd9Sstevel@tonic-gate 				/*
11117c478bd9Sstevel@tonic-gate 				 * if there's no sub-option, we *must* add
11127c478bd9Sstevel@tonic-gate 				 * a default one. this is because xargs must
11137c478bd9Sstevel@tonic-gate 				 * be able to distinguish between a valid
11147c478bd9Sstevel@tonic-gate 				 * suboption, and a command name.
11157c478bd9Sstevel@tonic-gate 				 */
11167c478bd9Sstevel@tonic-gate 				if (av[i][2] == NULL) {
11177c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(def);
11187c478bd9Sstevel@tonic-gate 				} else {
11197c478bd9Sstevel@tonic-gate 					/* clear out our version: */
11207c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
11217c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(&av[i][2]);
11227c478bd9Sstevel@tonic-gate 				}
11237c478bd9Sstevel@tonic-gate 				if (mav[mac] == NULL) {
112403fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
11257c478bd9Sstevel@tonic-gate 					exit(1);
11267c478bd9Sstevel@tonic-gate 				}
11277c478bd9Sstevel@tonic-gate 				break;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 			/* flags with required subarguments:		*/
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 			/*
11327c478bd9Sstevel@tonic-gate 			 * there are two separate cases here. either the
11337c478bd9Sstevel@tonic-gate 			 * flag can have the normal XCU4 handling
11347c478bd9Sstevel@tonic-gate 			 * (of the form: -X subargument); or it can have
11357c478bd9Sstevel@tonic-gate 			 * the old solaris 2.[0-4] handling (of the
11367c478bd9Sstevel@tonic-gate 			 * form: -Xsubargument). in order to maintain
11377c478bd9Sstevel@tonic-gate 			 * backwards compatibility, we must support the
11387c478bd9Sstevel@tonic-gate 			 * latter case. we handle the latter possibility
11397c478bd9Sstevel@tonic-gate 			 * first so both the old solaris way of handling
11407c478bd9Sstevel@tonic-gate 			 * and the new XCU4 way of handling things are allowed.
11417c478bd9Sstevel@tonic-gate 			 */
11427c478bd9Sstevel@tonic-gate 			case	'n':	/* FALLTHROUGH			*/
1143*04427e3bSAndrew Bennett 			case	'P':	/* FALLTHROUGH			*/
11447c478bd9Sstevel@tonic-gate 			case	's':	/* FALLTHROUGH			*/
11457c478bd9Sstevel@tonic-gate 			case	'E':	/* FALLTHROUGH			*/
11467c478bd9Sstevel@tonic-gate 			case	'I':	/* FALLTHROUGH			*/
11477c478bd9Sstevel@tonic-gate 			case	'L':
11487c478bd9Sstevel@tonic-gate 				/*
11497c478bd9Sstevel@tonic-gate 				 * if the second character isn't null, then
11507c478bd9Sstevel@tonic-gate 				 * the user has specified the old syntax.
11517c478bd9Sstevel@tonic-gate 				 * we move the subargument into our
11527c478bd9Sstevel@tonic-gate 				 * mod'd argument list.
11537c478bd9Sstevel@tonic-gate 				 */
11547c478bd9Sstevel@tonic-gate 				if (av[i][2] != NULL) {
11557c478bd9Sstevel@tonic-gate 					/* first clean things up:	*/
11567c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate 					/* now add the separation:	*/
11597c478bd9Sstevel@tonic-gate 					++mac;	/* inc to next mod'd arg */
11607c478bd9Sstevel@tonic-gate 					if ((mav[mac] = strdup(&av[i][2])) ==
11617c478bd9Sstevel@tonic-gate 					    NULL) {
116203fc8686SGarrett D'Amore 						PERR(MALLOCFAIL);
11637c478bd9Sstevel@tonic-gate 						exit(1);
11647c478bd9Sstevel@tonic-gate 					}
11657c478bd9Sstevel@tonic-gate 					break;
11667c478bd9Sstevel@tonic-gate 				}
11677c478bd9Sstevel@tonic-gate 				i++;
11687c478bd9Sstevel@tonic-gate 				mac++;
1169952d685eScf 
11707c478bd9Sstevel@tonic-gate 				if (av[i] == NULL) {
11717c478bd9Sstevel@tonic-gate 					mav[mac] = NULL;
11727c478bd9Sstevel@tonic-gate 					return;
11737c478bd9Sstevel@tonic-gate 				}
11747c478bd9Sstevel@tonic-gate 				if ((mav[mac] = strdup(av[i])) == NULL) {
117503fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
11767c478bd9Sstevel@tonic-gate 					exit(1);
11777c478bd9Sstevel@tonic-gate 				}
11787c478bd9Sstevel@tonic-gate 				break;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 			/* flags */
11817c478bd9Sstevel@tonic-gate 			case 'p' :
11827c478bd9Sstevel@tonic-gate 			case 't' :
11837c478bd9Sstevel@tonic-gate 			case 'x' :
11841daace1dSGarrett D'Amore 			case '0' :
11857c478bd9Sstevel@tonic-gate 				break;
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate 			case '-' :
11887c478bd9Sstevel@tonic-gate 			default:
11897c478bd9Sstevel@tonic-gate 				/*
11907c478bd9Sstevel@tonic-gate 				 * here we've hit the cmd argument. so
11917c478bd9Sstevel@tonic-gate 				 * we'll stop special processing, as the
11927c478bd9Sstevel@tonic-gate 				 * cmd may have a "-i" etc., argument,
11937c478bd9Sstevel@tonic-gate 				 * and we don't want to add a "" to it.
11947c478bd9Sstevel@tonic-gate 				 */
11957c478bd9Sstevel@tonic-gate 				cflag = 1;
11967c478bd9Sstevel@tonic-gate 				break;
11977c478bd9Sstevel@tonic-gate 			}
11987c478bd9Sstevel@tonic-gate 		} else if (i > 0) {	/* if we're not the 1st arg	*/
11997c478bd9Sstevel@tonic-gate 			/*
12007c478bd9Sstevel@tonic-gate 			 * if it's not a flag, then it *must* be the cmd.
12017c478bd9Sstevel@tonic-gate 			 * set cflag, so we don't mishandle the -[eil] flags.
12027c478bd9Sstevel@tonic-gate 			 */
12037c478bd9Sstevel@tonic-gate 			cflag = 1;
12047c478bd9Sstevel@tonic-gate 		}
12057c478bd9Sstevel@tonic-gate 	}
12067c478bd9Sstevel@tonic-gate 
12077c478bd9Sstevel@tonic-gate 	mav[mac] = NULL;
12087c478bd9Sstevel@tonic-gate }
1209