xref: /illumos-gate/usr/src/cmd/xargs/xargs.c (revision 826ac02a0def83e0a41b29321470d299c7389aab)
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 /*
22*826ac02aSGarrett D'Amore  * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23*826ac02aSGarrett D'Amore  * Copyright 2012 DEY Storage Systems, Inc.
2403fc8686SGarrett D'Amore  *
2503fc8686SGarrett D'Amore  * Portions of this file developed by DEY Storage Systems, Inc. are licensed
2603fc8686SGarrett D'Amore  * under the terms of the Common Development and Distribution License (CDDL)
2703fc8686SGarrett D'Amore  * version 1.0 only.  The use of subsequent versions of the License are
2803fc8686SGarrett D'Amore  * is specifically prohibited unless those terms are not in conflict with
2903fc8686SGarrett D'Amore  * version 1.0 of the License.  You can find this license on-line at
3003fc8686SGarrett D'Amore  * http://www.illumos.org/license/CDDL
3103fc8686SGarrett D'Amore  */
327c478bd9Sstevel@tonic-gate /*
33d2afb7a9Scf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
347c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
357c478bd9Sstevel@tonic-gate  */
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
387c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate #include <stdio.h>
427c478bd9Sstevel@tonic-gate #include <sys/types.h>
437c478bd9Sstevel@tonic-gate #include <sys/wait.h>
447c478bd9Sstevel@tonic-gate #include <unistd.h>
457c478bd9Sstevel@tonic-gate #include <fcntl.h>
467c478bd9Sstevel@tonic-gate #include <string.h>
477c478bd9Sstevel@tonic-gate #include <stdarg.h>
487c478bd9Sstevel@tonic-gate #include <stdlib.h>
497c478bd9Sstevel@tonic-gate #include <limits.h>
507c478bd9Sstevel@tonic-gate #include <wchar.h>
517c478bd9Sstevel@tonic-gate #include <locale.h>
527c478bd9Sstevel@tonic-gate #include <langinfo.h>
537c478bd9Sstevel@tonic-gate #include <stropts.h>
547c478bd9Sstevel@tonic-gate #include <poll.h>
557c478bd9Sstevel@tonic-gate #include <errno.h>
567c478bd9Sstevel@tonic-gate #include <stdarg.h>
573d63ea05Sas #include "getresponse.h"
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define	HEAD	0
607c478bd9Sstevel@tonic-gate #define	TAIL	1
617c478bd9Sstevel@tonic-gate #define	FALSE 0
627c478bd9Sstevel@tonic-gate #define	TRUE 1
637c478bd9Sstevel@tonic-gate #define	MAXSBUF 255
647c478bd9Sstevel@tonic-gate #define	MAXIBUF 512
657c478bd9Sstevel@tonic-gate #define	MAXINSERTS 5
667c478bd9Sstevel@tonic-gate #define	BUFSIZE LINE_MAX
677c478bd9Sstevel@tonic-gate #define	MAXARGS 255
687c478bd9Sstevel@tonic-gate #define	INSPAT_STR	"{}"	/* default replstr string for -[Ii]	*/
697c478bd9Sstevel@tonic-gate #define	FORK_RETRY	5
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #define	QBUF_STARTLEN 255  /* start size of growable string buffer */
727c478bd9Sstevel@tonic-gate #define	QBUF_INC 100	   /* how much to grow a growable string by */
737c478bd9Sstevel@tonic-gate 
7403fc8686SGarrett D'Amore /* We use these macros to help make formatting look "consistent" */
7503fc8686SGarrett D'Amore #define	EMSG(s)		ermsg(gettext(s "\n"))
7603fc8686SGarrett D'Amore #define	EMSG2(s, a)	ermsg(gettext(s "\n"), a)
7703fc8686SGarrett D'Amore #define	PERR(s)		perror(gettext("xargs: " s))
7803fc8686SGarrett D'Amore 
7903fc8686SGarrett D'Amore /* Some common error messages */
8003fc8686SGarrett D'Amore 
8103fc8686SGarrett D'Amore #define	LIST2LONG	"Argument list too long"
8203fc8686SGarrett D'Amore #define	ARG2LONG	"A single argument was greater than %d bytes"
8303fc8686SGarrett D'Amore #define	MALLOCFAIL	"Memory allocation failure"
8403fc8686SGarrett D'Amore #define	CORRUPTFILE	"Corrupt input file"
8503fc8686SGarrett D'Amore #define	WAITFAIL	"Wait failure"
8603fc8686SGarrett D'Amore #define	CHILDSIG	"Child killed with signal %d"
8703fc8686SGarrett D'Amore #define	CHILDFAIL	"Command could not continue processing data"
8803fc8686SGarrett D'Amore #define	FORKFAIL	"Could not fork child"
8903fc8686SGarrett D'Amore #define	EXECFAIL	"Could not exec command"
9003fc8686SGarrett D'Amore #define	MISSQUOTE	"Missing quote"
9103fc8686SGarrett D'Amore #define	BADESCAPE	"Incomplete escape"
9203fc8686SGarrett D'Amore #define	IBUFOVERFLOW	"Insert buffer overflow"
9303fc8686SGarrett D'Amore 
9403fc8686SGarrett D'Amore #define	_(x)	gettext(x)
9503fc8686SGarrett D'Amore 
967c478bd9Sstevel@tonic-gate static wctype_t	blank;
977c478bd9Sstevel@tonic-gate static char	*arglist[MAXARGS+1];
9803fc8686SGarrett D'Amore static char	argbuf[BUFSIZE * 2 + 1];
9903fc8686SGarrett D'Amore static char	lastarg[BUFSIZE + 1];
1007c478bd9Sstevel@tonic-gate static char	**ARGV = arglist;
1017c478bd9Sstevel@tonic-gate static char	*LEOF = "_";
1027c478bd9Sstevel@tonic-gate static char	*INSPAT = INSPAT_STR;
1037c478bd9Sstevel@tonic-gate static char	ins_buf[MAXIBUF];
1047c478bd9Sstevel@tonic-gate static char	*p_ibuf;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate static struct inserts {
1077c478bd9Sstevel@tonic-gate 	char	**p_ARGV;	/* where to put newarg ptr in arg list */
1087c478bd9Sstevel@tonic-gate 	char	*p_skel;	/* ptr to arg template */
1097c478bd9Sstevel@tonic-gate } saveargv[MAXINSERTS];
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate static int	PROMPT = -1;
1127c478bd9Sstevel@tonic-gate static int	BUFLIM = BUFSIZE;
1137c478bd9Sstevel@tonic-gate static int	N_ARGS = 0;
1147c478bd9Sstevel@tonic-gate static int	N_args = 0;
1157c478bd9Sstevel@tonic-gate static int	N_lines = 0;
1167c478bd9Sstevel@tonic-gate static int	DASHX = FALSE;
1177c478bd9Sstevel@tonic-gate static int	MORE = TRUE;
1187c478bd9Sstevel@tonic-gate static int	PER_LINE = FALSE;
119*826ac02aSGarrett D'Amore static int	LINE_CONT = FALSE;
120*826ac02aSGarrett D'Amore static int	EAT_LEAD = FALSE;
1217c478bd9Sstevel@tonic-gate static int	ERR = FALSE;
1227c478bd9Sstevel@tonic-gate static int	OK = TRUE;
1237c478bd9Sstevel@tonic-gate static int	LEGAL = FALSE;
1247c478bd9Sstevel@tonic-gate static int	TRACE = FALSE;
1257c478bd9Sstevel@tonic-gate static int	INSERT = FALSE;
12603fc8686SGarrett D'Amore static int	ZERO = FALSE;
1277c478bd9Sstevel@tonic-gate static int	linesize = 0;
1287c478bd9Sstevel@tonic-gate static int	ibufsize = 0;
1297c478bd9Sstevel@tonic-gate static int	exitstat = 0;	/* our exit status			*/
1307c478bd9Sstevel@tonic-gate static int	mac;		/* modified argc, after parsing		*/
1317c478bd9Sstevel@tonic-gate static char	**mav;		/* modified argv, after parsing		*/
1327c478bd9Sstevel@tonic-gate static int	n_inserts;	/* # of insertions.			*/
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate /* our usage message:							*/
1351daace1dSGarrett D'Amore #define	USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\
1367c478bd9Sstevel@tonic-gate 	"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\
1377c478bd9Sstevel@tonic-gate 	"[cmd [args ...]]\n"
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static int	echoargs();
14003fc8686SGarrett D'Amore static wint_t	getwchr(char *, size_t *);
1417c478bd9Sstevel@tonic-gate static int	lcall(char *sub, char **subargs);
1427c478bd9Sstevel@tonic-gate static void	addibuf(struct inserts *p);
1437c478bd9Sstevel@tonic-gate static void	ermsg(char *messages, ...);
1447c478bd9Sstevel@tonic-gate static char	*addarg(char *arg);
14503fc8686SGarrett D'Amore static void	store_str(char **, char *, size_t);
14603fc8686SGarrett D'Amore static char	*getarg(char *);
1477c478bd9Sstevel@tonic-gate static char	*insert(char *pattern, char *subst);
1487c478bd9Sstevel@tonic-gate static void	usage();
1497c478bd9Sstevel@tonic-gate static void	parseargs();
1507c478bd9Sstevel@tonic-gate 
15143a29105Srobbin int
1527c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	int	j;
1557c478bd9Sstevel@tonic-gate 	struct inserts *psave;
1567c478bd9Sstevel@tonic-gate 	int c;
1577c478bd9Sstevel@tonic-gate 	int	initsize;
15803fc8686SGarrett D'Amore 	char	*cmdname, **initlist;
15903fc8686SGarrett D'Amore 	char	*arg;
16003fc8686SGarrett D'Amore 	char	*next;
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	/* initialization */
1637c478bd9Sstevel@tonic-gate 	blank = wctype("blank");
1647c478bd9Sstevel@tonic-gate 	n_inserts = 0;
1657c478bd9Sstevel@tonic-gate 	psave = saveargv;
1667c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1677c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D 		*/
1687c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't 		*/
1697c478bd9Sstevel@tonic-gate #endif
1707c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1713d63ea05Sas 	if (init_yes() < 0) {
17203fc8686SGarrett D'Amore 		ermsg(_(ERR_MSG_INIT_YES), strerror(errno));
1737c478bd9Sstevel@tonic-gate 		exit(1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	parseargs(argc, argv);
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	/* handling all of xargs arguments:				*/
17903fc8686SGarrett D'Amore 	while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:s:x")) != EOF) {
1807c478bd9Sstevel@tonic-gate 		switch (c) {
18103fc8686SGarrett D'Amore 		case '0':
18203fc8686SGarrett D'Amore 			ZERO = TRUE;
18303fc8686SGarrett D'Amore 			break;
18403fc8686SGarrett D'Amore 
1857c478bd9Sstevel@tonic-gate 		case 't':	/* -t: turn trace mode on		*/
1867c478bd9Sstevel@tonic-gate 			TRACE = TRUE;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 		case 'p':	/* -p: turn on prompt mode.		*/
1907c478bd9Sstevel@tonic-gate 			if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) {
19103fc8686SGarrett D'Amore 				PERR("can't read from tty for -p");
1927c478bd9Sstevel@tonic-gate 			} else {
1937c478bd9Sstevel@tonic-gate 				TRACE = TRUE;
1947c478bd9Sstevel@tonic-gate 			}
1957c478bd9Sstevel@tonic-gate 			break;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 		case 'e':
1987c478bd9Sstevel@tonic-gate 			/*
1997c478bd9Sstevel@tonic-gate 			 * -e[eofstr]: set/disable end-of-file.
2007c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; but
2017c478bd9Sstevel@tonic-gate 			 * parseargs forced an argument if not was given.  The
2027c478bd9Sstevel@tonic-gate 			 * forced argument is the default...
2037c478bd9Sstevel@tonic-gate 			 */
2047c478bd9Sstevel@tonic-gate 			LEOF = optarg; /* can be empty */
2057c478bd9Sstevel@tonic-gate 			break;
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 		case 'E':
2087c478bd9Sstevel@tonic-gate 			/*
2097c478bd9Sstevel@tonic-gate 			 * -E eofstr: change end-of-file string.
210952d685eScf 			 * eofstr *is* required here, but can be empty:
2117c478bd9Sstevel@tonic-gate 			 */
2127c478bd9Sstevel@tonic-gate 			LEOF = optarg;
2137c478bd9Sstevel@tonic-gate 			break;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		case 'I':
2167c478bd9Sstevel@tonic-gate 			/* -I replstr: Insert mode. replstr *is* required. */
217*826ac02aSGarrett D'Amore 			INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
218*826ac02aSGarrett D'Amore 			LINE_CONT = FALSE;
2197c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
220952d685eScf 			INSPAT = optarg;
221952d685eScf 			if (*optarg == '\0') {
22203fc8686SGarrett D'Amore 				ermsg(_("Option requires an argument: -%c\n"),
22303fc8686SGarrett D'Amore 				    c);
2247c478bd9Sstevel@tonic-gate 			}
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 		case 'i':
2287c478bd9Sstevel@tonic-gate 			/*
2297c478bd9Sstevel@tonic-gate 			 * -i [replstr]: insert mode, with *optional* replstr.
2307c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2317c478bd9Sstevel@tonic-gate 			 * it's not given, then the string INSPAT_STR will
2327c478bd9Sstevel@tonic-gate 			 * be assumed.
2337c478bd9Sstevel@tonic-gate 			 *
2347c478bd9Sstevel@tonic-gate 			 * Since getopts(3C) doesn't handle the case of an
2357c478bd9Sstevel@tonic-gate 			 * optional variable argument at all, we have to
2367c478bd9Sstevel@tonic-gate 			 * parse this by hand:
2377c478bd9Sstevel@tonic-gate 			 */
2387c478bd9Sstevel@tonic-gate 
239*826ac02aSGarrett D'Amore 			INSERT = PER_LINE = LEGAL = EAT_LEAD = TRUE;
240*826ac02aSGarrett D'Amore 			LINE_CONT = FALSE;
2417c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
242788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2437c478bd9Sstevel@tonic-gate 				INSPAT = optarg;
2447c478bd9Sstevel@tonic-gate 			} else {
2457c478bd9Sstevel@tonic-gate 				/*
2467c478bd9Sstevel@tonic-gate 				 * here, there is no next argument. so
2477c478bd9Sstevel@tonic-gate 				 * we reset INSPAT to the INSPAT_STR.
2487c478bd9Sstevel@tonic-gate 				 * we *have* to do this, as -i/I may have
2497c478bd9Sstevel@tonic-gate 				 * been given previously, and XCU4 requires
2507c478bd9Sstevel@tonic-gate 				 * that only "the last one specified takes
2517c478bd9Sstevel@tonic-gate 				 * effect".
2527c478bd9Sstevel@tonic-gate 				 */
2537c478bd9Sstevel@tonic-gate 				INSPAT = INSPAT_STR;
2547c478bd9Sstevel@tonic-gate 			}
2557c478bd9Sstevel@tonic-gate 			break;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 		case 'L':
2587c478bd9Sstevel@tonic-gate 			/*
2597c478bd9Sstevel@tonic-gate 			 * -L number: # of times cmd is executed
2607c478bd9Sstevel@tonic-gate 			 * number *is* required here:
2617c478bd9Sstevel@tonic-gate 			 */
262*826ac02aSGarrett D'Amore 			PER_LINE = LINE_CONT = TRUE;
2637c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
264*826ac02aSGarrett D'Amore 			INSERT = EAT_LEAD = FALSE;
265952d685eScf 			if ((PER_LINE = atoi(optarg)) <= 0) {
26603fc8686SGarrett D'Amore 				ermsg(_("#lines must be positive int: %s\n"),
26703fc8686SGarrett D'Amore 				    optarg);
2687c478bd9Sstevel@tonic-gate 			}
2697c478bd9Sstevel@tonic-gate 			break;
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 		case 'l':
2727c478bd9Sstevel@tonic-gate 			/*
2737c478bd9Sstevel@tonic-gate 			 * -l [number]: # of times cmd is executed
2747c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2757c478bd9Sstevel@tonic-gate 			 * it's not given, then 1 is assumed.
2767c478bd9Sstevel@tonic-gate 			 *
2777c478bd9Sstevel@tonic-gate 			 * parseargs handles the optional arg processing.
2787c478bd9Sstevel@tonic-gate 			 */
2797c478bd9Sstevel@tonic-gate 
280*826ac02aSGarrett D'Amore 			PER_LINE = LINE_CONT = LEGAL = TRUE;
2817c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
282*826ac02aSGarrett D'Amore 			INSERT = EAT_LEAD = FALSE;
2837c478bd9Sstevel@tonic-gate 
284788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2857c478bd9Sstevel@tonic-gate 				if ((PER_LINE = atoi(optarg)) <= 0)
2867c478bd9Sstevel@tonic-gate 					PER_LINE = 1;
2877c478bd9Sstevel@tonic-gate 			}
2887c478bd9Sstevel@tonic-gate 			break;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		case 'n':	/* -n number: # stdin args		*/
2917c478bd9Sstevel@tonic-gate 			/*
2927c478bd9Sstevel@tonic-gate 			 * -n number: # stdin args.
2937c478bd9Sstevel@tonic-gate 			 * number *is* required here:
2947c478bd9Sstevel@tonic-gate 			 */
295952d685eScf 			if ((N_ARGS = atoi(optarg)) <= 0) {
29603fc8686SGarrett D'Amore 				ermsg(_("#args must be positive int: %s\n"),
29703fc8686SGarrett D'Amore 				    optarg);
2987c478bd9Sstevel@tonic-gate 			} else {
2997c478bd9Sstevel@tonic-gate 				LEGAL = DASHX || N_ARGS == 1;
300*826ac02aSGarrett D'Amore 				INSERT = PER_LINE = LINE_CONT = FALSE;
3017c478bd9Sstevel@tonic-gate 			}
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate 		case 's':	/* -s size: set max size of each arg list */
305952d685eScf 			BUFLIM = atoi(optarg);
306952d685eScf 			if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
30703fc8686SGarrett D'Amore 				ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
30803fc8686SGarrett D'Amore 				    BUFSIZE, optarg);
3097c478bd9Sstevel@tonic-gate 			}
3107c478bd9Sstevel@tonic-gate 			break;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		case 'x':	/* -x: terminate if args > size limit	*/
3137c478bd9Sstevel@tonic-gate 			DASHX = LEGAL = TRUE;
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 		default:
3177c478bd9Sstevel@tonic-gate 			/*
3187c478bd9Sstevel@tonic-gate 			 * bad argument. complain and get ready to die.
3197c478bd9Sstevel@tonic-gate 			 */
3207c478bd9Sstevel@tonic-gate 			usage();
3217c478bd9Sstevel@tonic-gate 			exit(2);
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/*
3277c478bd9Sstevel@tonic-gate 	 * if anything called ermsg(), something screwed up, so
3287c478bd9Sstevel@tonic-gate 	 * we exit early.
3297c478bd9Sstevel@tonic-gate 	 */
3307c478bd9Sstevel@tonic-gate 	if (OK == FALSE) {
3317c478bd9Sstevel@tonic-gate 		usage();
3327c478bd9Sstevel@tonic-gate 		exit(2);
3337c478bd9Sstevel@tonic-gate 	}
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	/*
3367c478bd9Sstevel@tonic-gate 	 * we're finished handling xargs's options, so now pick up
3377c478bd9Sstevel@tonic-gate 	 * the command name (if any), and it's options.
3387c478bd9Sstevel@tonic-gate 	 */
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	mac -= optind;	/* dec arg count by what we've processed 	*/
3427c478bd9Sstevel@tonic-gate 	mav += optind;	/* inc to current mav				*/
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	if (mac <= 0) {	/* if there're no more args to process,	*/
3457c478bd9Sstevel@tonic-gate 		cmdname = "/usr/bin/echo";	/* our default command	*/
3467c478bd9Sstevel@tonic-gate 		*ARGV++ = addarg(cmdname);	/* use the default cmd.	*/
3477c478bd9Sstevel@tonic-gate 	} else {	/* otherwise keep parsing rest of the string.	*/
3487c478bd9Sstevel@tonic-gate 		/*
3497c478bd9Sstevel@tonic-gate 		 * note that we can't use getopts(3C), and *must* parse
3507c478bd9Sstevel@tonic-gate 		 * this by hand, as we don't know apriori what options the
3517c478bd9Sstevel@tonic-gate 		 * command will take.
3527c478bd9Sstevel@tonic-gate 		 */
3537c478bd9Sstevel@tonic-gate 		cmdname = *mav;	/* get the command name	*/
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		/* pick up the remaining args from the command line:	*/
3577c478bd9Sstevel@tonic-gate 		while ((OK == TRUE) && (mac-- > 0)) {
3587c478bd9Sstevel@tonic-gate 			/*
3597c478bd9Sstevel@tonic-gate 			 * while we haven't crapped out, and there's
3607c478bd9Sstevel@tonic-gate 			 * work to do:
3617c478bd9Sstevel@tonic-gate 			 */
3627c478bd9Sstevel@tonic-gate 			if (INSERT && ! ERR) {
36303fc8686SGarrett D'Amore 				if (strstr(*mav, INSPAT) != NULL) {
3647c478bd9Sstevel@tonic-gate 					if (++n_inserts > MAXINSERTS) {
36503fc8686SGarrett D'Amore 						ermsg(_("too many args "
3667c478bd9Sstevel@tonic-gate 						    "with %s\n"), INSPAT);
3677c478bd9Sstevel@tonic-gate 						ERR = TRUE;
3687c478bd9Sstevel@tonic-gate 					}
3697c478bd9Sstevel@tonic-gate 					psave->p_ARGV = ARGV;
3707c478bd9Sstevel@tonic-gate 					(psave++)->p_skel = *mav;
3717c478bd9Sstevel@tonic-gate 				}
3727c478bd9Sstevel@tonic-gate 			}
3737c478bd9Sstevel@tonic-gate 			*ARGV++ = addarg(*mav++);
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 	}
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate 	/* pick up args from standard input */
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	initlist = ARGV;
3807c478bd9Sstevel@tonic-gate 	initsize = linesize;
38103fc8686SGarrett D'Amore 	lastarg[0] = '\0';
3827c478bd9Sstevel@tonic-gate 
38303fc8686SGarrett D'Amore 	while (OK) {
3847c478bd9Sstevel@tonic-gate 		N_args = 0;
3857c478bd9Sstevel@tonic-gate 		N_lines = 0;
3867c478bd9Sstevel@tonic-gate 		ARGV = initlist;
3877c478bd9Sstevel@tonic-gate 		linesize = initsize;
38803fc8686SGarrett D'Amore 		next = argbuf;
38903fc8686SGarrett D'Amore 
39003fc8686SGarrett D'Amore 		while (MORE || (lastarg[0] != '\0')) {
39103fc8686SGarrett D'Amore 			int l;
39203fc8686SGarrett D'Amore 
39303fc8686SGarrett D'Amore 			if (*lastarg != '\0') {
39403fc8686SGarrett D'Amore 				arg = strcpy(next, lastarg);
39503fc8686SGarrett D'Amore 				*lastarg = '\0';
39603fc8686SGarrett D'Amore 			} else if ((arg = getarg(next)) == NULL) {
39703fc8686SGarrett D'Amore 				break;
39803fc8686SGarrett D'Amore 			}
39903fc8686SGarrett D'Amore 
40003fc8686SGarrett D'Amore 			l = strlen(arg) + 1;
40103fc8686SGarrett D'Amore 			linesize += l;
40203fc8686SGarrett D'Amore 			next += l;
40303fc8686SGarrett D'Amore 
40403fc8686SGarrett D'Amore 			/* Inserts are handled specially later. */
40503fc8686SGarrett D'Amore 			if ((n_inserts == 0) && (linesize >= BUFLIM)) {
40603fc8686SGarrett D'Amore 				/*
40703fc8686SGarrett D'Amore 				 * Legal indicates hard fail if the list is
40803fc8686SGarrett D'Amore 				 * truncated due to size.  So fail, or if we
40903fc8686SGarrett D'Amore 				 * cannot create any list because it would be
41003fc8686SGarrett D'Amore 				 * too big.
41103fc8686SGarrett D'Amore 				 */
41203fc8686SGarrett D'Amore 				if (LEGAL || N_args == 0) {
41303fc8686SGarrett D'Amore 					EMSG(LIST2LONG);
41403fc8686SGarrett D'Amore 					exit(2);
41503fc8686SGarrett D'Amore 					/* NOTREACHED */
41603fc8686SGarrett D'Amore 				}
41703fc8686SGarrett D'Amore 
41803fc8686SGarrett D'Amore 				/*
41903fc8686SGarrett D'Amore 				 * Otherwise just save argument for later.
42003fc8686SGarrett D'Amore 				 */
42103fc8686SGarrett D'Amore 				(void) strcpy(lastarg, arg);
42203fc8686SGarrett D'Amore 				break;
42303fc8686SGarrett D'Amore 			}
42403fc8686SGarrett D'Amore 
42503fc8686SGarrett D'Amore 			*ARGV++ = arg;
42603fc8686SGarrett D'Amore 
42703fc8686SGarrett D'Amore 			N_args++;
42803fc8686SGarrett D'Amore 
429*826ac02aSGarrett D'Amore 			if ((PER_LINE && (N_lines >= PER_LINE)) ||
430*826ac02aSGarrett D'Amore 			    (N_ARGS && (N_args >= N_ARGS))) {
43103fc8686SGarrett D'Amore 				break;
43203fc8686SGarrett D'Amore 			}
43303fc8686SGarrett D'Amore 
4347c478bd9Sstevel@tonic-gate 
435a035dc19Scf 			if ((ARGV - arglist) == MAXARGS) {
436a035dc19Scf 				break;
437a035dc19Scf 			}
438a035dc19Scf 		}
43903fc8686SGarrett D'Amore 
44003fc8686SGarrett D'Amore 		*ARGV = NULL;
44103fc8686SGarrett D'Amore 		if (N_args == 0) {
44203fc8686SGarrett D'Amore 			/* Reached the end with no more work. */
443a035dc19Scf 			exit(exitstat);
444a035dc19Scf 		}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 		/* insert arg if requested */
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 		if (!ERR && INSERT) {
44903fc8686SGarrett D'Amore 
4507c478bd9Sstevel@tonic-gate 			p_ibuf = ins_buf;
4517c478bd9Sstevel@tonic-gate 			ARGV--;
4527c478bd9Sstevel@tonic-gate 			j = ibufsize = 0;
4537c478bd9Sstevel@tonic-gate 			for (psave = saveargv; ++j <= n_inserts; ++psave) {
4547c478bd9Sstevel@tonic-gate 				addibuf(psave);
4557c478bd9Sstevel@tonic-gate 				if (ERR)
4567c478bd9Sstevel@tonic-gate 					break;
4577c478bd9Sstevel@tonic-gate 			}
4587c478bd9Sstevel@tonic-gate 		}
45903fc8686SGarrett D'Amore 		*ARGV = NULL;
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 		if (n_inserts > 0) {
4627c478bd9Sstevel@tonic-gate 			/*
4637c478bd9Sstevel@tonic-gate 			 * if we've done any insertions, re-calculate the
4647c478bd9Sstevel@tonic-gate 			 * linesize. bomb out if we've exceeded our length.
4657c478bd9Sstevel@tonic-gate 			 */
46603fc8686SGarrett D'Amore 			linesize = 0;
4677c478bd9Sstevel@tonic-gate 			for (ARGV = arglist; *ARGV != NULL; ARGV++) {
46803fc8686SGarrett D'Amore 				linesize += strlen(*ARGV) + 1;
46903fc8686SGarrett D'Amore 			}
47003fc8686SGarrett D'Amore 			if (linesize >= BUFLIM) {
47103fc8686SGarrett D'Amore 				EMSG(LIST2LONG);
47203fc8686SGarrett D'Amore 				exit(2);
47303fc8686SGarrett D'Amore 				/* NOTREACHED */
4747c478bd9Sstevel@tonic-gate 			}
4757c478bd9Sstevel@tonic-gate 		}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 		/* exec command */
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 		if (!ERR) {
4807c478bd9Sstevel@tonic-gate 			if (!MORE &&
4817c478bd9Sstevel@tonic-gate 			    (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
4827c478bd9Sstevel@tonic-gate 				exit(exitstat);
4837c478bd9Sstevel@tonic-gate 			OK = TRUE;
4847c478bd9Sstevel@tonic-gate 			j = TRACE ? echoargs() : TRUE;
4857c478bd9Sstevel@tonic-gate 			if (j) {
4867c478bd9Sstevel@tonic-gate 				/*
4877c478bd9Sstevel@tonic-gate 				 * for xcu4, all invocations of cmdname must
4887c478bd9Sstevel@tonic-gate 				 * return 0, in order for us to return 0.
4897c478bd9Sstevel@tonic-gate 				 * so if we have a non-zero status here,
4907c478bd9Sstevel@tonic-gate 				 * quit immediately.
4917c478bd9Sstevel@tonic-gate 				 */
49203fc8686SGarrett D'Amore 				exitstat |= lcall(cmdname, arglist);
4937c478bd9Sstevel@tonic-gate 			}
4947c478bd9Sstevel@tonic-gate 		}
4957c478bd9Sstevel@tonic-gate 	}
4967c478bd9Sstevel@tonic-gate 
49703fc8686SGarrett D'Amore 	if (OK)
49843a29105Srobbin 		return (exitstat);
4997c478bd9Sstevel@tonic-gate 
50003fc8686SGarrett D'Amore 	/*
50103fc8686SGarrett D'Amore 	 * if exitstat was set, to match XCU4 complience,
50203fc8686SGarrett D'Amore 	 * return that value, otherwise, return 1.
50303fc8686SGarrett D'Amore 	 */
50403fc8686SGarrett D'Amore 	return (exitstat ? exitstat : 1);
5057c478bd9Sstevel@tonic-gate }
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate static char *
5087c478bd9Sstevel@tonic-gate addarg(char *arg)
5097c478bd9Sstevel@tonic-gate {
51003fc8686SGarrett D'Amore 	linesize += (strlen(arg) + 1);
51103fc8686SGarrett D'Amore 	return (arg);
5127c478bd9Sstevel@tonic-gate }
5137c478bd9Sstevel@tonic-gate 
5147c478bd9Sstevel@tonic-gate 
51503fc8686SGarrett D'Amore static void
51603fc8686SGarrett D'Amore store_str(char **buffer, char *str, size_t len)
51703fc8686SGarrett D'Amore {
51803fc8686SGarrett D'Amore 	(void) memcpy(*buffer, str, len);
51903fc8686SGarrett D'Amore 	(*buffer)[len] = '\0';
52003fc8686SGarrett D'Amore 	*buffer += len;
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
52303fc8686SGarrett D'Amore 
5247c478bd9Sstevel@tonic-gate static char *
52503fc8686SGarrett D'Amore getarg(char *arg)
5267c478bd9Sstevel@tonic-gate {
52703fc8686SGarrett D'Amore 	char	*xarg = arg;
528*826ac02aSGarrett D'Amore 	wchar_t	c = 0;
5297c478bd9Sstevel@tonic-gate 	char	mbc[MB_LEN_MAX];
53003fc8686SGarrett D'Amore 	size_t	len;
53103fc8686SGarrett D'Amore 	int	escape = 0;
53203fc8686SGarrett D'Amore 	int	inquote = 0;
533*826ac02aSGarrett D'Amore 	int	last = 0;
5347c478bd9Sstevel@tonic-gate 
53503fc8686SGarrett D'Amore 	arg[0] = '\0';
5367c478bd9Sstevel@tonic-gate 
53703fc8686SGarrett D'Amore 	while (MORE) {
5387c478bd9Sstevel@tonic-gate 
53903fc8686SGarrett D'Amore 		len = 0;
540*826ac02aSGarrett D'Amore 		last = c;
54103fc8686SGarrett D'Amore 		c = getwchr(mbc, &len);
5427c478bd9Sstevel@tonic-gate 
54303fc8686SGarrett D'Amore 		if (((arg - xarg) + len) > BUFLIM) {
54403fc8686SGarrett D'Amore 			EMSG2(ARG2LONG, BUFLIM);
54503fc8686SGarrett D'Amore 			exit(2);
54603fc8686SGarrett D'Amore 			ERR = TRUE;
5477c478bd9Sstevel@tonic-gate 			return (NULL);
5487c478bd9Sstevel@tonic-gate 		}
5497c478bd9Sstevel@tonic-gate 
55003fc8686SGarrett D'Amore 		switch (c) {
55103fc8686SGarrett D'Amore 		case '\n':
55203fc8686SGarrett D'Amore 			if (ZERO) {
55303fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
5547c478bd9Sstevel@tonic-gate 				continue;
5557c478bd9Sstevel@tonic-gate 			}
556*826ac02aSGarrett D'Amore 			/*
557*826ac02aSGarrett D'Amore 			 * NB: Some other versions rip off all of the trailing
558*826ac02aSGarrett D'Amore 			 * blanks.  The spec only claims that this should
559*826ac02aSGarrett D'Amore 			 * be done for a single blank.  We follow the spec.
560*826ac02aSGarrett D'Amore 			 */
561*826ac02aSGarrett D'Amore 			if (LINE_CONT && iswctype(last, blank)) {
562*826ac02aSGarrett D'Amore 				len = 0;
563*826ac02aSGarrett D'Amore 				*arg = 0;
564*826ac02aSGarrett D'Amore 				continue;
565*826ac02aSGarrett D'Amore 			}
56603fc8686SGarrett D'Amore 			/* FALLTHRU */
5677c478bd9Sstevel@tonic-gate 
56803fc8686SGarrett D'Amore 		case '\0':
56903fc8686SGarrett D'Amore 		case WEOF:	/* Note WEOF == EOF */
5707c478bd9Sstevel@tonic-gate 
57103fc8686SGarrett D'Amore 			if (escape) {
57203fc8686SGarrett D'Amore 				EMSG(BADESCAPE);
57303fc8686SGarrett D'Amore 				ERR = TRUE;
57403fc8686SGarrett D'Amore 				return (NULL);
5757c478bd9Sstevel@tonic-gate 			}
5767c478bd9Sstevel@tonic-gate 			if (inquote) {
57703fc8686SGarrett D'Amore 				EMSG(MISSQUOTE);
5787c478bd9Sstevel@tonic-gate 				ERR = TRUE;
57903fc8686SGarrett D'Amore 				return (NULL);
5807c478bd9Sstevel@tonic-gate 			}
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate 			N_lines++;
58303fc8686SGarrett D'Amore 			break;
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 		case '"':
58603fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 1)) {
58703fc8686SGarrett D'Amore 				/* treat it literally */
58803fc8686SGarrett D'Amore 				escape = 0;
58903fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
59003fc8686SGarrett D'Amore 
59103fc8686SGarrett D'Amore 			} else if (inquote == 2) {
59203fc8686SGarrett D'Amore 				/* terminating double quote */
5937c478bd9Sstevel@tonic-gate 				inquote = 0;
59403fc8686SGarrett D'Amore 
59503fc8686SGarrett D'Amore 			} else {
59603fc8686SGarrett D'Amore 				/* starting quoted string */
5977c478bd9Sstevel@tonic-gate 				inquote = 2;
59803fc8686SGarrett D'Amore 			}
59903fc8686SGarrett D'Amore 			continue;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		case '\'':
60203fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 2)) {
60303fc8686SGarrett D'Amore 				/* treat it literally */
60403fc8686SGarrett D'Amore 				escape = 0;
60503fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
60603fc8686SGarrett D'Amore 
60703fc8686SGarrett D'Amore 			} else if (inquote == 1) {
60803fc8686SGarrett D'Amore 				/* terminating single quote */
6097c478bd9Sstevel@tonic-gate 				inquote = 0;
61003fc8686SGarrett D'Amore 
61103fc8686SGarrett D'Amore 			} else {
61203fc8686SGarrett D'Amore 				/* starting quoted string */
6137c478bd9Sstevel@tonic-gate 				inquote = 1;
61403fc8686SGarrett D'Amore 			}
61503fc8686SGarrett D'Amore 			continue;
6167c478bd9Sstevel@tonic-gate 
61703fc8686SGarrett D'Amore 		case '\\':
618d2afb7a9Scf 			/*
619d2afb7a9Scf 			 * Any unquoted character can be escaped by
620d2afb7a9Scf 			 * preceding it with a backslash.
621d2afb7a9Scf 			 */
62203fc8686SGarrett D'Amore 			if (ZERO || inquote || escape) {
62303fc8686SGarrett D'Amore 				escape = 0;
62403fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
62503fc8686SGarrett D'Amore 			} else {
62603fc8686SGarrett D'Amore 				escape = 1;
627d2afb7a9Scf 			}
62803fc8686SGarrett D'Amore 			continue;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 		default:
63103fc8686SGarrett D'Amore 			/* most times we will just want to store it */
63203fc8686SGarrett D'Amore 			if (inquote || escape || ZERO || !iswctype(c, blank)) {
63303fc8686SGarrett D'Amore 				escape = 0;
63403fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
63503fc8686SGarrett D'Amore 				continue;
6367c478bd9Sstevel@tonic-gate 			}
637*826ac02aSGarrett D'Amore 			if (EAT_LEAD && last == 0) {
638*826ac02aSGarrett D'Amore 				c = 0;		/* Roll it back */
639*826ac02aSGarrett D'Amore 				continue;
640*826ac02aSGarrett D'Amore 			}
641*826ac02aSGarrett D'Amore 			if (PER_LINE) {
642*826ac02aSGarrett D'Amore 				store_str(&arg, mbc, len);
643*826ac02aSGarrett D'Amore 				continue;
644*826ac02aSGarrett D'Amore 			}
645*826ac02aSGarrett D'Amore 
646*826ac02aSGarrett D'Amore 			/* unquoted blank without special handling */
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 		}
64903fc8686SGarrett D'Amore 
65003fc8686SGarrett D'Amore 		/*
65103fc8686SGarrett D'Amore 		 * At this point we are processing a complete argument.
65203fc8686SGarrett D'Amore 		 */
65303fc8686SGarrett D'Amore 		if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') {
65403fc8686SGarrett D'Amore 			MORE = FALSE;
65503fc8686SGarrett D'Amore 			return (NULL);
65603fc8686SGarrett D'Amore 		}
65703fc8686SGarrett D'Amore 		if (c == WEOF) {
65803fc8686SGarrett D'Amore 			MORE = FALSE;
65903fc8686SGarrett D'Amore 		}
66003fc8686SGarrett D'Amore 		if (xarg[0] == '\0')
66103fc8686SGarrett D'Amore 			continue;
66203fc8686SGarrett D'Amore 		break;
6637c478bd9Sstevel@tonic-gate 	}
6647c478bd9Sstevel@tonic-gate 
66503fc8686SGarrett D'Amore 	return (xarg[0] == '\0' ? NULL : xarg);
66603fc8686SGarrett D'Amore }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate /*
6697c478bd9Sstevel@tonic-gate  * ermsg():	print out an error message, and indicate failure globally.
6707c478bd9Sstevel@tonic-gate  *
6717c478bd9Sstevel@tonic-gate  *	Assumes that message has already been gettext()'d. It would be
6727c478bd9Sstevel@tonic-gate  *	nice if we could just do the gettext() here, but we can't, since
6737c478bd9Sstevel@tonic-gate  *	since xgettext(1M) wouldn't be able to pick up our error message.
6747c478bd9Sstevel@tonic-gate  */
6757c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
6767c478bd9Sstevel@tonic-gate static void
6777c478bd9Sstevel@tonic-gate ermsg(char *messages, ...)
6787c478bd9Sstevel@tonic-gate {
6797c478bd9Sstevel@tonic-gate 	va_list	ap;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate 	va_start(ap, messages);
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "xargs: ");
6847c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, messages, ap);
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	va_end(ap);
6877c478bd9Sstevel@tonic-gate 	OK = FALSE;
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate static int
6917c478bd9Sstevel@tonic-gate echoargs()
6927c478bd9Sstevel@tonic-gate {
6937c478bd9Sstevel@tonic-gate 	char	**anarg;
6947c478bd9Sstevel@tonic-gate 	char	**tanarg;	/* tmp ptr			*/
6957c478bd9Sstevel@tonic-gate 	int		i;
6967c478bd9Sstevel@tonic-gate 	char		reply[LINE_MAX];
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	tanarg = anarg = arglist-1;
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate 	/*
7017c478bd9Sstevel@tonic-gate 	 * write out each argument, separated by a space. the tanarg
7027c478bd9Sstevel@tonic-gate 	 * nonsense is for xcu4 testsuite compliance - so that an
7037c478bd9Sstevel@tonic-gate 	 * extra space isn't echoed after the last argument.
7047c478bd9Sstevel@tonic-gate 	 */
7057c478bd9Sstevel@tonic-gate 	while (*++anarg) {		/* while there's an argument	*/
7067c478bd9Sstevel@tonic-gate 		++tanarg;		/* follow anarg			*/
7077c478bd9Sstevel@tonic-gate 		(void) write(2, *anarg, strlen(*anarg));
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		if (*++tanarg) {	/* if there's another argument:	*/
7107c478bd9Sstevel@tonic-gate 			(void) write(2, " ", 1); /* add a space		*/
7117c478bd9Sstevel@tonic-gate 			--tanarg;	/* reset back to anarg		*/
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 	if (PROMPT == -1) {
7157c478bd9Sstevel@tonic-gate 		(void) write(2, "\n", 1);
7167c478bd9Sstevel@tonic-gate 		return (TRUE);
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	(void) write(2, "?...", 4);	/* ask the user for input	*/
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
7227c478bd9Sstevel@tonic-gate 		if (reply[i] == '\n') {
7237c478bd9Sstevel@tonic-gate 			if (i == 0)
7247c478bd9Sstevel@tonic-gate 				return (FALSE);
7257c478bd9Sstevel@tonic-gate 			break;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 	reply[i] = 0;
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	/* flush remainder of line if necessary */
7317c478bd9Sstevel@tonic-gate 	if (i == LINE_MAX) {
7327c478bd9Sstevel@tonic-gate 		char	bitbucket;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 		while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
7357c478bd9Sstevel@tonic-gate 			;
7367c478bd9Sstevel@tonic-gate 	}
7377c478bd9Sstevel@tonic-gate 
7383d63ea05Sas 	return (yes_check(reply));
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate static char *
7437c478bd9Sstevel@tonic-gate insert(char *pattern, char *subst)
7447c478bd9Sstevel@tonic-gate {
7457c478bd9Sstevel@tonic-gate 	static char	buffer[MAXSBUF+1];
7467c478bd9Sstevel@tonic-gate 	int		len, ipatlen;
7477c478bd9Sstevel@tonic-gate 	char	*pat;
7487c478bd9Sstevel@tonic-gate 	char	*bufend;
7497c478bd9Sstevel@tonic-gate 	char	*pbuf;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	len = strlen(subst);
7527c478bd9Sstevel@tonic-gate 	ipatlen = strlen(INSPAT) - 1;
7537c478bd9Sstevel@tonic-gate 	pat = pattern - 1;
7547c478bd9Sstevel@tonic-gate 	pbuf = buffer;
7557c478bd9Sstevel@tonic-gate 	bufend = &buffer[MAXSBUF];
7567c478bd9Sstevel@tonic-gate 
7577c478bd9Sstevel@tonic-gate 	while (*++pat) {
758*826ac02aSGarrett D'Amore 		if (strncmp(pat, INSPAT, ipatlen + 1) == 0) {
7597c478bd9Sstevel@tonic-gate 			if (pbuf + len >= bufend) {
7607c478bd9Sstevel@tonic-gate 				break;
7617c478bd9Sstevel@tonic-gate 			} else {
7627c478bd9Sstevel@tonic-gate 				(void) strcpy(pbuf, subst);
7637c478bd9Sstevel@tonic-gate 				pat += ipatlen;
7647c478bd9Sstevel@tonic-gate 				pbuf += len;
7657c478bd9Sstevel@tonic-gate 			}
7667c478bd9Sstevel@tonic-gate 		} else {
7677c478bd9Sstevel@tonic-gate 			*pbuf++ = *pat;
7687c478bd9Sstevel@tonic-gate 			if (pbuf >= bufend)
7697c478bd9Sstevel@tonic-gate 				break;
7707c478bd9Sstevel@tonic-gate 		}
7717c478bd9Sstevel@tonic-gate 	}
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	if (!*pat) {
7747c478bd9Sstevel@tonic-gate 		*pbuf = '\0';
7757c478bd9Sstevel@tonic-gate 		return (buffer);
7767c478bd9Sstevel@tonic-gate 	} else {
7777c478bd9Sstevel@tonic-gate 		ermsg(gettext("Maximum argument size with insertion via %s's "
7787c478bd9Sstevel@tonic-gate 		    "exceeded\n"), INSPAT);
7797c478bd9Sstevel@tonic-gate 		ERR = TRUE;
78003fc8686SGarrett D'Amore 		return (NULL);
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate }
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 
7857c478bd9Sstevel@tonic-gate static void
7867c478bd9Sstevel@tonic-gate addibuf(struct inserts	*p)
7877c478bd9Sstevel@tonic-gate {
7887c478bd9Sstevel@tonic-gate 	char	*newarg, *skel, *sub;
7897c478bd9Sstevel@tonic-gate 	int		l;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	skel = p->p_skel;
7927c478bd9Sstevel@tonic-gate 	sub = *ARGV;
7937c478bd9Sstevel@tonic-gate 	newarg = insert(skel, sub);
7947c478bd9Sstevel@tonic-gate 	if (ERR)
795788f581bSceastha 		return;
7967c478bd9Sstevel@tonic-gate 
79703fc8686SGarrett D'Amore 	l = strlen(newarg) + 1;
79803fc8686SGarrett D'Amore 	if ((ibufsize += l) > MAXIBUF) {
79903fc8686SGarrett D'Amore 		EMSG(IBUFOVERFLOW);
80003fc8686SGarrett D'Amore 		ERR = TRUE;
8017c478bd9Sstevel@tonic-gate 	}
80203fc8686SGarrett D'Amore 	(void) strcpy(p_ibuf, newarg);
80303fc8686SGarrett D'Amore 	*(p->p_ARGV) = p_ibuf;
80403fc8686SGarrett D'Amore 	p_ibuf += l;
8057c478bd9Sstevel@tonic-gate }
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate /*
80903fc8686SGarrett D'Amore  * getwchr():	get the next wide character.
8107c478bd9Sstevel@tonic-gate  * description:
81103fc8686SGarrett D'Amore  *	we get the next character from stdin.  This returns WEOF if no
81203fc8686SGarrett D'Amore  *	character is present.  If ZERO is set, it gets a single byte instead
81303fc8686SGarrett D'Amore  *	a wide character.
8147c478bd9Sstevel@tonic-gate  */
81503fc8686SGarrett D'Amore static wint_t
81603fc8686SGarrett D'Amore getwchr(char *mbc, size_t *sz)
8177c478bd9Sstevel@tonic-gate {
81803fc8686SGarrett D'Amore 	size_t		i;
81903fc8686SGarrett D'Amore 	int		c;
82003fc8686SGarrett D'Amore 	wchar_t		wch;
8217c478bd9Sstevel@tonic-gate 
82203fc8686SGarrett D'Amore 	i = 0;
82303fc8686SGarrett D'Amore 	while (i < MB_CUR_MAX) {
8247c478bd9Sstevel@tonic-gate 
82503fc8686SGarrett D'Amore 		if ((c = fgetc(stdin)) == EOF) {
8267c478bd9Sstevel@tonic-gate 
82703fc8686SGarrett D'Amore 			if (i == 0) {
8287c478bd9Sstevel@tonic-gate 				/* TRUE EOF has been reached */
82903fc8686SGarrett D'Amore 				return (WEOF);
8307c478bd9Sstevel@tonic-gate 			}
83103fc8686SGarrett D'Amore 
8327c478bd9Sstevel@tonic-gate 			/*
8337c478bd9Sstevel@tonic-gate 			 * We have some characters in our buffer still so it
8347c478bd9Sstevel@tonic-gate 			 * must be an invalid character right before EOF.
8357c478bd9Sstevel@tonic-gate 			 */
8367c478bd9Sstevel@tonic-gate 			break;
8377c478bd9Sstevel@tonic-gate 		}
83803fc8686SGarrett D'Amore 		mbc[i++] = (char)c;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		/* If this succeeds then we are done */
84103fc8686SGarrett D'Amore 		if (ZERO) {
84203fc8686SGarrett D'Amore 			*sz = i;
84303fc8686SGarrett D'Amore 			return ((char)c);
84403fc8686SGarrett D'Amore 		}
84503fc8686SGarrett D'Amore 		if (mbtowc(&wch, mbc, i) != -1) {
84603fc8686SGarrett D'Amore 			*sz = i;
84703fc8686SGarrett D'Amore 			return ((wint_t)wch);
84803fc8686SGarrett D'Amore 		}
8497c478bd9Sstevel@tonic-gate 	}
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 	/*
8527c478bd9Sstevel@tonic-gate 	 * We have now encountered an illegal character sequence.
8537c478bd9Sstevel@tonic-gate 	 * There is nothing much we can do at this point but
8547c478bd9Sstevel@tonic-gate 	 * return an error.  If we attempt to recover we may in fact
8557c478bd9Sstevel@tonic-gate 	 * return garbage as arguments, from the customer's point
8567c478bd9Sstevel@tonic-gate 	 * of view.  After all what if they are feeding us a file
8577c478bd9Sstevel@tonic-gate 	 * generated in another locale?
8587c478bd9Sstevel@tonic-gate 	 */
8597c478bd9Sstevel@tonic-gate 	errno = EILSEQ;
86003fc8686SGarrett D'Amore 	PERR(CORRUPTFILE);
8617c478bd9Sstevel@tonic-gate 	exit(1);
8627c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8637c478bd9Sstevel@tonic-gate }
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate static int
8677c478bd9Sstevel@tonic-gate lcall(char *sub, char **subargs)
8687c478bd9Sstevel@tonic-gate {
8697c478bd9Sstevel@tonic-gate 	int retcode, retry = 0;
8707c478bd9Sstevel@tonic-gate 	pid_t iwait, child;
8717c478bd9Sstevel@tonic-gate 
87203fc8686SGarrett D'Amore 	for (;;) {
8737c478bd9Sstevel@tonic-gate 		switch (child = fork()) {
8747c478bd9Sstevel@tonic-gate 		default:
8757c478bd9Sstevel@tonic-gate 			while ((iwait = wait(&retcode)) != child &&
8767c478bd9Sstevel@tonic-gate 			    iwait != (pid_t)-1)
8777c478bd9Sstevel@tonic-gate 				;
8787c478bd9Sstevel@tonic-gate 			if (iwait == (pid_t)-1) {
87903fc8686SGarrett D'Amore 				PERR(WAITFAIL);
8807c478bd9Sstevel@tonic-gate 				exit(122);
8817c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
8827c478bd9Sstevel@tonic-gate 			}
8837c478bd9Sstevel@tonic-gate 			if (WIFSIGNALED(retcode)) {
88403fc8686SGarrett D'Amore 				EMSG2(CHILDSIG, WTERMSIG(retcode));
8857c478bd9Sstevel@tonic-gate 				exit(125);
8867c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
8877c478bd9Sstevel@tonic-gate 			}
8887c478bd9Sstevel@tonic-gate 			if ((WEXITSTATUS(retcode) & 0377) == 0377) {
88903fc8686SGarrett D'Amore 				EMSG(CHILDFAIL);
8907c478bd9Sstevel@tonic-gate 				exit(124);
8917c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
8927c478bd9Sstevel@tonic-gate 			}
8937c478bd9Sstevel@tonic-gate 			return (WEXITSTATUS(retcode));
8947c478bd9Sstevel@tonic-gate 		case 0:
8957c478bd9Sstevel@tonic-gate 			(void) execvp(sub, subargs);
89603fc8686SGarrett D'Amore 			PERR(EXECFAIL);
8977c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
8987c478bd9Sstevel@tonic-gate 				exit(126);
8997c478bd9Sstevel@tonic-gate 			exit(127);
9007c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
9017c478bd9Sstevel@tonic-gate 		case -1:
9027c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN && retry++ < FORK_RETRY) {
90303fc8686SGarrett D'Amore 				PERR(FORKFAIL);
9047c478bd9Sstevel@tonic-gate 				exit(123);
9057c478bd9Sstevel@tonic-gate 			}
9067c478bd9Sstevel@tonic-gate 			(void) sleep(1);
9077c478bd9Sstevel@tonic-gate 		}
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate }
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate static void
9137c478bd9Sstevel@tonic-gate usage()
9147c478bd9Sstevel@tonic-gate {
91503fc8686SGarrett D'Amore 	ermsg(_(USAGEMSG));
9167c478bd9Sstevel@tonic-gate 	OK = FALSE;
9177c478bd9Sstevel@tonic-gate }
9187c478bd9Sstevel@tonic-gate 
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate /*
9227c478bd9Sstevel@tonic-gate  * parseargs():		modify the args
9237c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
9247c478bd9Sstevel@tonic-gate  *	and getopts(3C) is clueless about this nonsense, we change the
9257c478bd9Sstevel@tonic-gate  *	our local argument count and strings to separate this out,
9267c478bd9Sstevel@tonic-gate  *	and make it easier to handle via getopts(3c).
9277c478bd9Sstevel@tonic-gate  *
9287c478bd9Sstevel@tonic-gate  *	-e	-> "-e ""
9297c478bd9Sstevel@tonic-gate  *	-e3	-> "-e "3"
9307c478bd9Sstevel@tonic-gate  *	-Estr	-> "-E "str"
9317c478bd9Sstevel@tonic-gate  *	-i	-> "-i "{}"
9327c478bd9Sstevel@tonic-gate  *	-irep	-> "-i "rep"
933*826ac02aSGarrett D'Amore  *	-l	-> "-l "1"
934*826ac02aSGarrett D'Amore  *	-l10	-> "-l "10"
9357c478bd9Sstevel@tonic-gate  *
9367c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
9377c478bd9Sstevel@tonic-gate  */
9387c478bd9Sstevel@tonic-gate static void
9397c478bd9Sstevel@tonic-gate parseargs(int ac, char **av)
9407c478bd9Sstevel@tonic-gate {
9417c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
9427c478bd9Sstevel@tonic-gate 	int cflag;		/* 0 = not processing cmd arg		*/
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 	if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
94503fc8686SGarrett D'Amore 		PERR(MALLOCFAIL);
9467c478bd9Sstevel@tonic-gate 		exit(1);
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate 	/* for each argument, see if we need to change things:		*/
9507c478bd9Sstevel@tonic-gate 	for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
9517c478bd9Sstevel@tonic-gate 		if ((mav[mac] = strdup(av[i])) == NULL) {
95203fc8686SGarrett D'Amore 			PERR(MALLOCFAIL);
9537c478bd9Sstevel@tonic-gate 			exit(1);
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 		/* -- has been found or argument list is fully processes */
9577c478bd9Sstevel@tonic-gate 		if (cflag)
9587c478bd9Sstevel@tonic-gate 			continue;
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 		/*
9617c478bd9Sstevel@tonic-gate 		 * if we're doing special processing, and we've got a flag
9627c478bd9Sstevel@tonic-gate 		 */
9637c478bd9Sstevel@tonic-gate 		else if ((av[i][0] == '-') && (av[i][1] != NULL)) {
9647c478bd9Sstevel@tonic-gate 			char	*def;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 			switch (av[i][1]) {
9677c478bd9Sstevel@tonic-gate 			case	'e':
9687c478bd9Sstevel@tonic-gate 				def = ""; /* -e with no arg turns off eof */
9697c478bd9Sstevel@tonic-gate 				goto process_special;
9707c478bd9Sstevel@tonic-gate 			case	'i':
9717c478bd9Sstevel@tonic-gate 				def = INSPAT_STR;
9727c478bd9Sstevel@tonic-gate 				goto process_special;
9737c478bd9Sstevel@tonic-gate 			case	'l':
9747c478bd9Sstevel@tonic-gate 				def = "1";
9757c478bd9Sstevel@tonic-gate process_special:
9767c478bd9Sstevel@tonic-gate 				/*
9777c478bd9Sstevel@tonic-gate 				 * if there's no sub-option, we *must* add
9787c478bd9Sstevel@tonic-gate 				 * a default one. this is because xargs must
9797c478bd9Sstevel@tonic-gate 				 * be able to distinguish between a valid
9807c478bd9Sstevel@tonic-gate 				 * suboption, and a command name.
9817c478bd9Sstevel@tonic-gate 				 */
9827c478bd9Sstevel@tonic-gate 				if (av[i][2] == NULL) {
9837c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(def);
9847c478bd9Sstevel@tonic-gate 				} else {
9857c478bd9Sstevel@tonic-gate 					/* clear out our version: */
9867c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
9877c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(&av[i][2]);
9887c478bd9Sstevel@tonic-gate 				}
9897c478bd9Sstevel@tonic-gate 				if (mav[mac] == NULL) {
99003fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
9917c478bd9Sstevel@tonic-gate 					exit(1);
9927c478bd9Sstevel@tonic-gate 				}
9937c478bd9Sstevel@tonic-gate 				break;
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 			/* flags with required subarguments:		*/
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 			/*
9987c478bd9Sstevel@tonic-gate 			 * there are two separate cases here. either the
9997c478bd9Sstevel@tonic-gate 			 * flag can have the normal XCU4 handling
10007c478bd9Sstevel@tonic-gate 			 * (of the form: -X subargument); or it can have
10017c478bd9Sstevel@tonic-gate 			 * the old solaris 2.[0-4] handling (of the
10027c478bd9Sstevel@tonic-gate 			 * form: -Xsubargument). in order to maintain
10037c478bd9Sstevel@tonic-gate 			 * backwards compatibility, we must support the
10047c478bd9Sstevel@tonic-gate 			 * latter case. we handle the latter possibility
10057c478bd9Sstevel@tonic-gate 			 * first so both the old solaris way of handling
10067c478bd9Sstevel@tonic-gate 			 * and the new XCU4 way of handling things are allowed.
10077c478bd9Sstevel@tonic-gate 			 */
10087c478bd9Sstevel@tonic-gate 			case	'n':	/* FALLTHROUGH			*/
10097c478bd9Sstevel@tonic-gate 			case	's':	/* FALLTHROUGH			*/
10107c478bd9Sstevel@tonic-gate 			case	'E':	/* FALLTHROUGH			*/
10117c478bd9Sstevel@tonic-gate 			case	'I':	/* FALLTHROUGH			*/
10127c478bd9Sstevel@tonic-gate 			case	'L':
10137c478bd9Sstevel@tonic-gate 				/*
10147c478bd9Sstevel@tonic-gate 				 * if the second character isn't null, then
10157c478bd9Sstevel@tonic-gate 				 * the user has specified the old syntax.
10167c478bd9Sstevel@tonic-gate 				 * we move the subargument into our
10177c478bd9Sstevel@tonic-gate 				 * mod'd argument list.
10187c478bd9Sstevel@tonic-gate 				 */
10197c478bd9Sstevel@tonic-gate 				if (av[i][2] != NULL) {
10207c478bd9Sstevel@tonic-gate 					/* first clean things up:	*/
10217c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
10227c478bd9Sstevel@tonic-gate 
10237c478bd9Sstevel@tonic-gate 					/* now add the separation:	*/
10247c478bd9Sstevel@tonic-gate 					++mac;	/* inc to next mod'd arg */
10257c478bd9Sstevel@tonic-gate 					if ((mav[mac] = strdup(&av[i][2])) ==
10267c478bd9Sstevel@tonic-gate 					    NULL) {
102703fc8686SGarrett D'Amore 						PERR(MALLOCFAIL);
10287c478bd9Sstevel@tonic-gate 						exit(1);
10297c478bd9Sstevel@tonic-gate 					}
10307c478bd9Sstevel@tonic-gate 					break;
10317c478bd9Sstevel@tonic-gate 				}
10327c478bd9Sstevel@tonic-gate 				i++;
10337c478bd9Sstevel@tonic-gate 				mac++;
1034952d685eScf 
10357c478bd9Sstevel@tonic-gate 				if (av[i] == NULL) {
10367c478bd9Sstevel@tonic-gate 					mav[mac] = NULL;
10377c478bd9Sstevel@tonic-gate 					return;
10387c478bd9Sstevel@tonic-gate 				}
10397c478bd9Sstevel@tonic-gate 				if ((mav[mac] = strdup(av[i])) == NULL) {
104003fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
10417c478bd9Sstevel@tonic-gate 					exit(1);
10427c478bd9Sstevel@tonic-gate 				}
10437c478bd9Sstevel@tonic-gate 				break;
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 			/* flags */
10467c478bd9Sstevel@tonic-gate 			case 'p' :
10477c478bd9Sstevel@tonic-gate 			case 't' :
10487c478bd9Sstevel@tonic-gate 			case 'x' :
10491daace1dSGarrett D'Amore 			case '0' :
10507c478bd9Sstevel@tonic-gate 				break;
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 			case '-' :
10537c478bd9Sstevel@tonic-gate 			default:
10547c478bd9Sstevel@tonic-gate 				/*
10557c478bd9Sstevel@tonic-gate 				 * here we've hit the cmd argument. so
10567c478bd9Sstevel@tonic-gate 				 * we'll stop special processing, as the
10577c478bd9Sstevel@tonic-gate 				 * cmd may have a "-i" etc., argument,
10587c478bd9Sstevel@tonic-gate 				 * and we don't want to add a "" to it.
10597c478bd9Sstevel@tonic-gate 				 */
10607c478bd9Sstevel@tonic-gate 				cflag = 1;
10617c478bd9Sstevel@tonic-gate 				break;
10627c478bd9Sstevel@tonic-gate 			}
10637c478bd9Sstevel@tonic-gate 		} else if (i > 0) {	/* if we're not the 1st arg	*/
10647c478bd9Sstevel@tonic-gate 			/*
10657c478bd9Sstevel@tonic-gate 			 * if it's not a flag, then it *must* be the cmd.
10667c478bd9Sstevel@tonic-gate 			 * set cflag, so we don't mishandle the -[eil] flags.
10677c478bd9Sstevel@tonic-gate 			 */
10687c478bd9Sstevel@tonic-gate 			cflag = 1;
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 	}
10717c478bd9Sstevel@tonic-gate 
10727c478bd9Sstevel@tonic-gate 	mav[mac] = NULL;
10737c478bd9Sstevel@tonic-gate }
1074