xref: /titanic_50/usr/src/cmd/xargs/xargs.c (revision 03fc868668dd42b1b163d1fb8af3968f7283a7eb)
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
5ef69670dScf46844  * Common Development and Distribution License (the "License").
6ef69670dScf46844  * 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  */
217c478bd9Sstevel@tonic-gate /*
22*03fc8686SGarrett D'Amore  * Copyright 2012 DEY Storage Systems, Inc.  All rights reserved.
23*03fc8686SGarrett D'Amore  *
24*03fc8686SGarrett D'Amore  * Portions of this file developed by DEY Storage Systems, Inc. are licensed
25*03fc8686SGarrett D'Amore  * under the terms of the Common Development and Distribution License (CDDL)
26*03fc8686SGarrett D'Amore  * version 1.0 only.  The use of subsequent versions of the License are
27*03fc8686SGarrett D'Amore  * is specifically prohibited unless those terms are not in conflict with
28*03fc8686SGarrett D'Amore  * version 1.0 of the License.  You can find this license on-line at
29*03fc8686SGarrett D'Amore  * http://www.illumos.org/license/CDDL
30*03fc8686SGarrett D'Amore  */
31*03fc8686SGarrett D'Amore /*
32d2afb7a9Scf46844  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
337c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
377c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate #include <stdio.h>
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <sys/wait.h>
437c478bd9Sstevel@tonic-gate #include <unistd.h>
447c478bd9Sstevel@tonic-gate #include <fcntl.h>
457c478bd9Sstevel@tonic-gate #include <string.h>
467c478bd9Sstevel@tonic-gate #include <stdarg.h>
477c478bd9Sstevel@tonic-gate #include <stdlib.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <wchar.h>
507c478bd9Sstevel@tonic-gate #include <locale.h>
517c478bd9Sstevel@tonic-gate #include <langinfo.h>
527c478bd9Sstevel@tonic-gate #include <stropts.h>
537c478bd9Sstevel@tonic-gate #include <poll.h>
547c478bd9Sstevel@tonic-gate #include <errno.h>
557c478bd9Sstevel@tonic-gate #include <stdarg.h>
563d63ea05Sas145665 #include "getresponse.h"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #define	HEAD	0
597c478bd9Sstevel@tonic-gate #define	TAIL	1
607c478bd9Sstevel@tonic-gate #define	FALSE 0
617c478bd9Sstevel@tonic-gate #define	TRUE 1
627c478bd9Sstevel@tonic-gate #define	MAXSBUF 255
637c478bd9Sstevel@tonic-gate #define	MAXIBUF 512
647c478bd9Sstevel@tonic-gate #define	MAXINSERTS 5
657c478bd9Sstevel@tonic-gate #define	BUFSIZE LINE_MAX
667c478bd9Sstevel@tonic-gate #define	MAXARGS 255
677c478bd9Sstevel@tonic-gate #define	INSPAT_STR	"{}"	/* default replstr string for -[Ii]	*/
687c478bd9Sstevel@tonic-gate #define	FORK_RETRY	5
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	QBUF_STARTLEN 255  /* start size of growable string buffer */
717c478bd9Sstevel@tonic-gate #define	QBUF_INC 100	   /* how much to grow a growable string by */
727c478bd9Sstevel@tonic-gate 
73*03fc8686SGarrett D'Amore /* We use these macros to help make formatting look "consistent" */
74*03fc8686SGarrett D'Amore #define	EMSG(s)		ermsg(gettext(s "\n"))
75*03fc8686SGarrett D'Amore #define	EMSG2(s, a)	ermsg(gettext(s "\n"), a)
76*03fc8686SGarrett D'Amore #define	PERR(s)		perror(gettext("xargs: " s))
77*03fc8686SGarrett D'Amore 
78*03fc8686SGarrett D'Amore /* Some common error messages */
79*03fc8686SGarrett D'Amore 
80*03fc8686SGarrett D'Amore #define	LIST2LONG	"Argument list too long"
81*03fc8686SGarrett D'Amore #define	ARG2LONG	"A single argument was greater than %d bytes"
82*03fc8686SGarrett D'Amore #define	MALLOCFAIL	"Memory allocation failure"
83*03fc8686SGarrett D'Amore #define	CORRUPTFILE	"Corrupt input file"
84*03fc8686SGarrett D'Amore #define	WAITFAIL	"Wait failure"
85*03fc8686SGarrett D'Amore #define	CHILDSIG	"Child killed with signal %d"
86*03fc8686SGarrett D'Amore #define	CHILDFAIL	"Command could not continue processing data"
87*03fc8686SGarrett D'Amore #define	FORKFAIL	"Could not fork child"
88*03fc8686SGarrett D'Amore #define	EXECFAIL	"Could not exec command"
89*03fc8686SGarrett D'Amore #define	MISSQUOTE	"Missing quote"
90*03fc8686SGarrett D'Amore #define	BADESCAPE	"Incomplete escape"
91*03fc8686SGarrett D'Amore #define	IBUFOVERFLOW	"Insert buffer overflow"
92*03fc8686SGarrett D'Amore 
93*03fc8686SGarrett D'Amore #define	_(x)	gettext(x)
94*03fc8686SGarrett D'Amore 
957c478bd9Sstevel@tonic-gate static wctype_t	blank;
967c478bd9Sstevel@tonic-gate static char	*arglist[MAXARGS+1];
97*03fc8686SGarrett D'Amore static char	argbuf[BUFSIZE * 2 + 1];
98*03fc8686SGarrett D'Amore static char	lastarg[BUFSIZE + 1];
997c478bd9Sstevel@tonic-gate static char	**ARGV = arglist;
1007c478bd9Sstevel@tonic-gate static char	*LEOF = "_";
1017c478bd9Sstevel@tonic-gate static char	*INSPAT = INSPAT_STR;
1027c478bd9Sstevel@tonic-gate static char	ins_buf[MAXIBUF];
1037c478bd9Sstevel@tonic-gate static char	*p_ibuf;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate static struct inserts {
1067c478bd9Sstevel@tonic-gate 	char	**p_ARGV;	/* where to put newarg ptr in arg list */
1077c478bd9Sstevel@tonic-gate 	char	*p_skel;	/* ptr to arg template */
1087c478bd9Sstevel@tonic-gate } saveargv[MAXINSERTS];
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate static int	PROMPT = -1;
1117c478bd9Sstevel@tonic-gate static int	BUFLIM = BUFSIZE;
1127c478bd9Sstevel@tonic-gate static int	N_ARGS = 0;
1137c478bd9Sstevel@tonic-gate static int	N_args = 0;
1147c478bd9Sstevel@tonic-gate static int	N_lines = 0;
1157c478bd9Sstevel@tonic-gate static int	DASHX = FALSE;
1167c478bd9Sstevel@tonic-gate static int	MORE = TRUE;
1177c478bd9Sstevel@tonic-gate static int	PER_LINE = FALSE;
1187c478bd9Sstevel@tonic-gate static int	ERR = FALSE;
1197c478bd9Sstevel@tonic-gate static int	OK = TRUE;
1207c478bd9Sstevel@tonic-gate static int	LEGAL = FALSE;
1217c478bd9Sstevel@tonic-gate static int	TRACE = FALSE;
1227c478bd9Sstevel@tonic-gate static int	INSERT = FALSE;
123*03fc8686SGarrett D'Amore static int	ZERO = FALSE;
1247c478bd9Sstevel@tonic-gate static int	linesize = 0;
1257c478bd9Sstevel@tonic-gate static int	ibufsize = 0;
1267c478bd9Sstevel@tonic-gate static int	exitstat = 0;	/* our exit status			*/
1277c478bd9Sstevel@tonic-gate static int	mac;		/* modified argc, after parsing		*/
1287c478bd9Sstevel@tonic-gate static char	**mav;		/* modified argv, after parsing		*/
1297c478bd9Sstevel@tonic-gate static int	n_inserts;	/* # of insertions.			*/
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate /* our usage message:							*/
1327c478bd9Sstevel@tonic-gate #define	USAGEMSG "Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] "\
1337c478bd9Sstevel@tonic-gate 	"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\
1347c478bd9Sstevel@tonic-gate 	"[cmd [args ...]]\n"
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate static int	echoargs();
137*03fc8686SGarrett D'Amore static wint_t	getwchr(char *, size_t *);
1387c478bd9Sstevel@tonic-gate static int	lcall(char *sub, char **subargs);
1397c478bd9Sstevel@tonic-gate static void	addibuf(struct inserts *p);
1407c478bd9Sstevel@tonic-gate static void	ermsg(char *messages, ...);
1417c478bd9Sstevel@tonic-gate static char	*addarg(char *arg);
142*03fc8686SGarrett D'Amore static void	store_str(char **, char *, size_t);
143*03fc8686SGarrett D'Amore static char	*getarg(char *);
1447c478bd9Sstevel@tonic-gate static char	*insert(char *pattern, char *subst);
1457c478bd9Sstevel@tonic-gate static void	usage();
1467c478bd9Sstevel@tonic-gate static void	parseargs();
1477c478bd9Sstevel@tonic-gate 
14843a29105Srobbin int
1497c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 	int	j;
1527c478bd9Sstevel@tonic-gate 	struct inserts *psave;
1537c478bd9Sstevel@tonic-gate 	int c;
1547c478bd9Sstevel@tonic-gate 	int	initsize;
155*03fc8686SGarrett D'Amore 	char	*cmdname, **initlist;
156*03fc8686SGarrett D'Amore 	char	*arg;
157*03fc8686SGarrett D'Amore 	char	*next;
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	/* initialization */
1607c478bd9Sstevel@tonic-gate 	blank = wctype("blank");
1617c478bd9Sstevel@tonic-gate 	n_inserts = 0;
1627c478bd9Sstevel@tonic-gate 	psave = saveargv;
1637c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1647c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D 		*/
1657c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't 		*/
1667c478bd9Sstevel@tonic-gate #endif
1677c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1683d63ea05Sas145665 	if (init_yes() < 0) {
169*03fc8686SGarrett D'Amore 		ermsg(_(ERR_MSG_INIT_YES), strerror(errno));
1707c478bd9Sstevel@tonic-gate 		exit(1);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	parseargs(argc, argv);
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/* handling all of xargs arguments:				*/
176*03fc8686SGarrett D'Amore 	while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:s:x")) != EOF) {
1777c478bd9Sstevel@tonic-gate 		switch (c) {
178*03fc8686SGarrett D'Amore 		case '0':
179*03fc8686SGarrett D'Amore 			ZERO = TRUE;
180*03fc8686SGarrett D'Amore 			break;
181*03fc8686SGarrett D'Amore 
1827c478bd9Sstevel@tonic-gate 		case 't':	/* -t: turn trace mode on		*/
1837c478bd9Sstevel@tonic-gate 			TRACE = TRUE;
1847c478bd9Sstevel@tonic-gate 			break;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		case 'p':	/* -p: turn on prompt mode.		*/
1877c478bd9Sstevel@tonic-gate 			if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) {
188*03fc8686SGarrett D'Amore 				PERR("can't read from tty for -p");
1897c478bd9Sstevel@tonic-gate 			} else {
1907c478bd9Sstevel@tonic-gate 				TRACE = TRUE;
1917c478bd9Sstevel@tonic-gate 			}
1927c478bd9Sstevel@tonic-gate 			break;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 		case 'e':
1957c478bd9Sstevel@tonic-gate 			/*
1967c478bd9Sstevel@tonic-gate 			 * -e[eofstr]: set/disable end-of-file.
1977c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; but
1987c478bd9Sstevel@tonic-gate 			 * parseargs forced an argument if not was given.  The
1997c478bd9Sstevel@tonic-gate 			 * forced argument is the default...
2007c478bd9Sstevel@tonic-gate 			 */
2017c478bd9Sstevel@tonic-gate 			LEOF = optarg; /* can be empty */
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		case 'E':
2057c478bd9Sstevel@tonic-gate 			/*
2067c478bd9Sstevel@tonic-gate 			 * -E eofstr: change end-of-file string.
207952d685eScf46844 			 * eofstr *is* required here, but can be empty:
2087c478bd9Sstevel@tonic-gate 			 */
2097c478bd9Sstevel@tonic-gate 			LEOF = optarg;
2107c478bd9Sstevel@tonic-gate 			break;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		case 'I':
2137c478bd9Sstevel@tonic-gate 			/* -I replstr: Insert mode. replstr *is* required. */
2147c478bd9Sstevel@tonic-gate 			INSERT = PER_LINE = LEGAL = TRUE;
2157c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
2167c478bd9Sstevel@tonic-gate 			INSPAT = optarg;
217952d685eScf46844 			if (*optarg == '\0') {
218*03fc8686SGarrett D'Amore 				ermsg(_("Option requires an argument: -%c\n"),
219*03fc8686SGarrett D'Amore 				    c);
2207c478bd9Sstevel@tonic-gate 			}
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 		case 'i':
2247c478bd9Sstevel@tonic-gate 			/*
2257c478bd9Sstevel@tonic-gate 			 * -i [replstr]: insert mode, with *optional* replstr.
2267c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2277c478bd9Sstevel@tonic-gate 			 * it's not given, then the string INSPAT_STR will
2287c478bd9Sstevel@tonic-gate 			 * be assumed.
2297c478bd9Sstevel@tonic-gate 			 *
2307c478bd9Sstevel@tonic-gate 			 * Since getopts(3C) doesn't handle the case of an
2317c478bd9Sstevel@tonic-gate 			 * optional variable argument at all, we have to
2327c478bd9Sstevel@tonic-gate 			 * parse this by hand:
2337c478bd9Sstevel@tonic-gate 			 */
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 			INSERT = PER_LINE = LEGAL = TRUE;
2367c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
237788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2387c478bd9Sstevel@tonic-gate 				INSPAT = optarg;
2397c478bd9Sstevel@tonic-gate 			} else {
2407c478bd9Sstevel@tonic-gate 				/*
2417c478bd9Sstevel@tonic-gate 				 * here, there is no next argument. so
2427c478bd9Sstevel@tonic-gate 				 * we reset INSPAT to the INSPAT_STR.
2437c478bd9Sstevel@tonic-gate 				 * we *have* to do this, as -i/I may have
2447c478bd9Sstevel@tonic-gate 				 * been given previously, and XCU4 requires
2457c478bd9Sstevel@tonic-gate 				 * that only "the last one specified takes
2467c478bd9Sstevel@tonic-gate 				 * effect".
2477c478bd9Sstevel@tonic-gate 				 */
2487c478bd9Sstevel@tonic-gate 				INSPAT = INSPAT_STR;
2497c478bd9Sstevel@tonic-gate 			}
2507c478bd9Sstevel@tonic-gate 			break;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 		case 'L':
2537c478bd9Sstevel@tonic-gate 			/*
2547c478bd9Sstevel@tonic-gate 			 * -L number: # of times cmd is executed
2557c478bd9Sstevel@tonic-gate 			 * number *is* required here:
2567c478bd9Sstevel@tonic-gate 			 */
2577c478bd9Sstevel@tonic-gate 			PER_LINE = TRUE;
2587c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
2597c478bd9Sstevel@tonic-gate 			INSERT = FALSE;
260952d685eScf46844 			if ((PER_LINE = atoi(optarg)) <= 0) {
261*03fc8686SGarrett D'Amore 				ermsg(_("#lines must be positive int: %s\n"),
262*03fc8686SGarrett D'Amore 				    optarg);
2637c478bd9Sstevel@tonic-gate 			}
2647c478bd9Sstevel@tonic-gate 			break;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 		case 'l':
2677c478bd9Sstevel@tonic-gate 			/*
2687c478bd9Sstevel@tonic-gate 			 * -l [number]: # of times cmd is executed
2697c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2707c478bd9Sstevel@tonic-gate 			 * it's not given, then 1 is assumed.
2717c478bd9Sstevel@tonic-gate 			 *
2727c478bd9Sstevel@tonic-gate 			 * parseargs handles the optional arg processing.
2737c478bd9Sstevel@tonic-gate 			 */
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 			PER_LINE = LEGAL = TRUE;  /* initialization	*/
2767c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
2777c478bd9Sstevel@tonic-gate 			INSERT = FALSE;
2787c478bd9Sstevel@tonic-gate 
279788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2807c478bd9Sstevel@tonic-gate 				if ((PER_LINE = atoi(optarg)) <= 0)
2817c478bd9Sstevel@tonic-gate 					PER_LINE = 1;
2827c478bd9Sstevel@tonic-gate 			}
2837c478bd9Sstevel@tonic-gate 			break;
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 		case 'n':	/* -n number: # stdin args		*/
2867c478bd9Sstevel@tonic-gate 			/*
2877c478bd9Sstevel@tonic-gate 			 * -n number: # stdin args.
2887c478bd9Sstevel@tonic-gate 			 * number *is* required here:
2897c478bd9Sstevel@tonic-gate 			 */
290952d685eScf46844 			if ((N_ARGS = atoi(optarg)) <= 0) {
291*03fc8686SGarrett D'Amore 				ermsg(_("#args must be positive int: %s\n"),
292*03fc8686SGarrett D'Amore 				    optarg);
2937c478bd9Sstevel@tonic-gate 			} else {
2947c478bd9Sstevel@tonic-gate 				LEGAL = DASHX || N_ARGS == 1;
2957c478bd9Sstevel@tonic-gate 				INSERT = PER_LINE = FALSE;
2967c478bd9Sstevel@tonic-gate 			}
2977c478bd9Sstevel@tonic-gate 			break;
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 		case 's':	/* -s size: set max size of each arg list */
3007c478bd9Sstevel@tonic-gate 			BUFLIM = atoi(optarg);
3017c478bd9Sstevel@tonic-gate 			if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
302*03fc8686SGarrett D'Amore 				ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
303*03fc8686SGarrett D'Amore 				    BUFSIZE, optarg);
3047c478bd9Sstevel@tonic-gate 			}
3057c478bd9Sstevel@tonic-gate 			break;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 		case 'x':	/* -x: terminate if args > size limit	*/
3087c478bd9Sstevel@tonic-gate 			DASHX = LEGAL = TRUE;
3097c478bd9Sstevel@tonic-gate 			break;
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 		default:
3127c478bd9Sstevel@tonic-gate 			/*
3137c478bd9Sstevel@tonic-gate 			 * bad argument. complain and get ready to die.
3147c478bd9Sstevel@tonic-gate 			 */
3157c478bd9Sstevel@tonic-gate 			usage();
3167c478bd9Sstevel@tonic-gate 			exit(2);
3177c478bd9Sstevel@tonic-gate 			break;
3187c478bd9Sstevel@tonic-gate 		}
3197c478bd9Sstevel@tonic-gate 	}
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	/*
3227c478bd9Sstevel@tonic-gate 	 * if anything called ermsg(), something screwed up, so
3237c478bd9Sstevel@tonic-gate 	 * we exit early.
3247c478bd9Sstevel@tonic-gate 	 */
3257c478bd9Sstevel@tonic-gate 	if (OK == FALSE) {
3267c478bd9Sstevel@tonic-gate 		usage();
3277c478bd9Sstevel@tonic-gate 		exit(2);
3287c478bd9Sstevel@tonic-gate 	}
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate 	/*
3317c478bd9Sstevel@tonic-gate 	 * we're finished handling xargs's options, so now pick up
3327c478bd9Sstevel@tonic-gate 	 * the command name (if any), and it's options.
3337c478bd9Sstevel@tonic-gate 	 */
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	mac -= optind;	/* dec arg count by what we've processed 	*/
3377c478bd9Sstevel@tonic-gate 	mav += optind;	/* inc to current mav				*/
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (mac <= 0) {	/* if there're no more args to process,	*/
3407c478bd9Sstevel@tonic-gate 		cmdname = "/usr/bin/echo";	/* our default command	*/
3417c478bd9Sstevel@tonic-gate 		*ARGV++ = addarg(cmdname);	/* use the default cmd.	*/
3427c478bd9Sstevel@tonic-gate 	} else {	/* otherwise keep parsing rest of the string.	*/
3437c478bd9Sstevel@tonic-gate 		/*
3447c478bd9Sstevel@tonic-gate 		 * note that we can't use getopts(3C), and *must* parse
3457c478bd9Sstevel@tonic-gate 		 * this by hand, as we don't know apriori what options the
3467c478bd9Sstevel@tonic-gate 		 * command will take.
3477c478bd9Sstevel@tonic-gate 		 */
3487c478bd9Sstevel@tonic-gate 		cmdname = *mav;	/* get the command name	*/
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		/* pick up the remaining args from the command line:	*/
3527c478bd9Sstevel@tonic-gate 		while ((OK == TRUE) && (mac-- > 0)) {
3537c478bd9Sstevel@tonic-gate 			/*
3547c478bd9Sstevel@tonic-gate 			 * while we haven't crapped out, and there's
3557c478bd9Sstevel@tonic-gate 			 * work to do:
3567c478bd9Sstevel@tonic-gate 			 */
3577c478bd9Sstevel@tonic-gate 			if (INSERT && ! ERR) {
358*03fc8686SGarrett D'Amore 				if (strstr(*mav, INSPAT) != NULL) {
3597c478bd9Sstevel@tonic-gate 					if (++n_inserts > MAXINSERTS) {
360*03fc8686SGarrett D'Amore 						ermsg(_("too many args "
3617c478bd9Sstevel@tonic-gate 						    "with %s\n"), INSPAT);
3627c478bd9Sstevel@tonic-gate 						ERR = TRUE;
3637c478bd9Sstevel@tonic-gate 					}
3647c478bd9Sstevel@tonic-gate 					psave->p_ARGV = ARGV;
3657c478bd9Sstevel@tonic-gate 					(psave++)->p_skel = *mav;
3667c478bd9Sstevel@tonic-gate 				}
3677c478bd9Sstevel@tonic-gate 			}
3687c478bd9Sstevel@tonic-gate 			*ARGV++ = addarg(*mav++);
3697c478bd9Sstevel@tonic-gate 		}
3707c478bd9Sstevel@tonic-gate 	}
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate 	/* pick up args from standard input */
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 	initlist = ARGV;
3757c478bd9Sstevel@tonic-gate 	initsize = linesize;
376*03fc8686SGarrett D'Amore 	lastarg[0] = '\0';
3777c478bd9Sstevel@tonic-gate 
378*03fc8686SGarrett D'Amore 	while (OK) {
3797c478bd9Sstevel@tonic-gate 		N_args = 0;
3807c478bd9Sstevel@tonic-gate 		N_lines = 0;
3817c478bd9Sstevel@tonic-gate 		ARGV = initlist;
3827c478bd9Sstevel@tonic-gate 		linesize = initsize;
383*03fc8686SGarrett D'Amore 		next = argbuf;
384*03fc8686SGarrett D'Amore 
385*03fc8686SGarrett D'Amore 		while (MORE || (lastarg[0] != '\0')) {
386*03fc8686SGarrett D'Amore 			int l;
387*03fc8686SGarrett D'Amore 
388*03fc8686SGarrett D'Amore 			if (*lastarg != '\0') {
389*03fc8686SGarrett D'Amore 				arg = strcpy(next, lastarg);
390*03fc8686SGarrett D'Amore 				*lastarg = '\0';
391*03fc8686SGarrett D'Amore 			} else if ((arg = getarg(next)) == NULL) {
392*03fc8686SGarrett D'Amore 				break;
3937c478bd9Sstevel@tonic-gate 			}
3947c478bd9Sstevel@tonic-gate 
395*03fc8686SGarrett D'Amore 			l = strlen(arg) + 1;
396*03fc8686SGarrett D'Amore 			linesize += l;
397*03fc8686SGarrett D'Amore 			next += l;
398*03fc8686SGarrett D'Amore 
399*03fc8686SGarrett D'Amore 			/* Inserts are handled specially later. */
400*03fc8686SGarrett D'Amore 			if ((n_inserts == 0) && (linesize >= BUFLIM)) {
401*03fc8686SGarrett D'Amore 				/*
402*03fc8686SGarrett D'Amore 				 * Legal indicates hard fail if the list is
403*03fc8686SGarrett D'Amore 				 * truncated due to size.  So fail, or if we
404*03fc8686SGarrett D'Amore 				 * cannot create any list because it would be
405*03fc8686SGarrett D'Amore 				 * too big.
406*03fc8686SGarrett D'Amore 				 */
407*03fc8686SGarrett D'Amore 				if (LEGAL || N_args == 0) {
408*03fc8686SGarrett D'Amore 					EMSG(LIST2LONG);
409*03fc8686SGarrett D'Amore 					exit(2);
410*03fc8686SGarrett D'Amore 					/* NOTREACHED */
411*03fc8686SGarrett D'Amore 				}
412*03fc8686SGarrett D'Amore 
413*03fc8686SGarrett D'Amore 				/*
414*03fc8686SGarrett D'Amore 				 * Otherwise just save argument for later.
415*03fc8686SGarrett D'Amore 				 */
416*03fc8686SGarrett D'Amore 				(void) strcpy(lastarg, arg);
417*03fc8686SGarrett D'Amore 				break;
418*03fc8686SGarrett D'Amore 			}
419*03fc8686SGarrett D'Amore 
420*03fc8686SGarrett D'Amore 			*ARGV++ = arg;
421*03fc8686SGarrett D'Amore 
422*03fc8686SGarrett D'Amore 			N_args++;
423*03fc8686SGarrett D'Amore 
424*03fc8686SGarrett D'Amore 			if ((PER_LINE && N_lines >= PER_LINE) ||
425*03fc8686SGarrett D'Amore 			    (N_ARGS && (N_args) >= N_ARGS)) {
426*03fc8686SGarrett D'Amore 				break;
427*03fc8686SGarrett D'Amore 			}
428*03fc8686SGarrett D'Amore 
429*03fc8686SGarrett D'Amore 
430a035dc19Scf46844 			if ((ARGV - arglist) == MAXARGS) {
431a035dc19Scf46844 				break;
432a035dc19Scf46844 			}
433a035dc19Scf46844 		}
434*03fc8686SGarrett D'Amore 
435*03fc8686SGarrett D'Amore 		*ARGV = NULL;
436*03fc8686SGarrett D'Amore 		if (N_args == 0) {
437*03fc8686SGarrett D'Amore 			/* Reached the end with no more work. */
438a035dc19Scf46844 			exit(exitstat);
439a035dc19Scf46844 		}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 		/* insert arg if requested */
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 		if (!ERR && INSERT) {
444*03fc8686SGarrett D'Amore 
4457c478bd9Sstevel@tonic-gate 			p_ibuf = ins_buf;
4467c478bd9Sstevel@tonic-gate 			ARGV--;
4477c478bd9Sstevel@tonic-gate 			j = ibufsize = 0;
4487c478bd9Sstevel@tonic-gate 			for (psave = saveargv; ++j <= n_inserts; ++psave) {
4497c478bd9Sstevel@tonic-gate 				addibuf(psave);
4507c478bd9Sstevel@tonic-gate 				if (ERR)
4517c478bd9Sstevel@tonic-gate 					break;
4527c478bd9Sstevel@tonic-gate 			}
4537c478bd9Sstevel@tonic-gate 		}
454*03fc8686SGarrett D'Amore 		*ARGV = NULL;
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 		if (n_inserts > 0) {
4577c478bd9Sstevel@tonic-gate 			/*
4587c478bd9Sstevel@tonic-gate 			 * if we've done any insertions, re-calculate the
4597c478bd9Sstevel@tonic-gate 			 * linesize. bomb out if we've exceeded our length.
4607c478bd9Sstevel@tonic-gate 			 */
461*03fc8686SGarrett D'Amore 			linesize = 0;
4627c478bd9Sstevel@tonic-gate 			for (ARGV = arglist; *ARGV != NULL; ARGV++) {
463*03fc8686SGarrett D'Amore 				linesize += strlen(*ARGV) + 1;
4647c478bd9Sstevel@tonic-gate 			}
465*03fc8686SGarrett D'Amore 			if (linesize >= BUFLIM) {
466*03fc8686SGarrett D'Amore 				EMSG(LIST2LONG);
467*03fc8686SGarrett D'Amore 				exit(2);
468*03fc8686SGarrett D'Amore 				/* NOTREACHED */
4697c478bd9Sstevel@tonic-gate 			}
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		/* exec command */
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		if (!ERR) {
4757c478bd9Sstevel@tonic-gate 			if (!MORE &&
4767c478bd9Sstevel@tonic-gate 			    (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
4777c478bd9Sstevel@tonic-gate 				exit(exitstat);
4787c478bd9Sstevel@tonic-gate 			OK = TRUE;
4797c478bd9Sstevel@tonic-gate 			j = TRACE ? echoargs() : TRUE;
4807c478bd9Sstevel@tonic-gate 			if (j) {
4817c478bd9Sstevel@tonic-gate 				/*
4827c478bd9Sstevel@tonic-gate 				 * for xcu4, all invocations of cmdname must
4837c478bd9Sstevel@tonic-gate 				 * return 0, in order for us to return 0.
4847c478bd9Sstevel@tonic-gate 				 * so if we have a non-zero status here,
4857c478bd9Sstevel@tonic-gate 				 * quit immediately.
4867c478bd9Sstevel@tonic-gate 				 */
487*03fc8686SGarrett D'Amore 				exitstat |= lcall(cmdname, arglist);
4887c478bd9Sstevel@tonic-gate 			}
4897c478bd9Sstevel@tonic-gate 		}
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate 
492*03fc8686SGarrett D'Amore 	if (OK)
49343a29105Srobbin 		return (exitstat);
494*03fc8686SGarrett D'Amore 
4957c478bd9Sstevel@tonic-gate 	/*
4967c478bd9Sstevel@tonic-gate 	 * if exitstat was set, to match XCU4 complience,
4977c478bd9Sstevel@tonic-gate 	 * return that value, otherwise, return 1.
4987c478bd9Sstevel@tonic-gate 	 */
49943a29105Srobbin 	return (exitstat ? exitstat : 1);
5007c478bd9Sstevel@tonic-gate }
5017c478bd9Sstevel@tonic-gate 
5027c478bd9Sstevel@tonic-gate static char *
5037c478bd9Sstevel@tonic-gate addarg(char *arg)
5047c478bd9Sstevel@tonic-gate {
505*03fc8686SGarrett D'Amore 	linesize += (strlen(arg) + 1);
5067c478bd9Sstevel@tonic-gate 	return (arg);
5077c478bd9Sstevel@tonic-gate }
5087c478bd9Sstevel@tonic-gate 
509*03fc8686SGarrett D'Amore 
510*03fc8686SGarrett D'Amore static void
511*03fc8686SGarrett D'Amore store_str(char **buffer, char *str, size_t len)
5127c478bd9Sstevel@tonic-gate {
513*03fc8686SGarrett D'Amore 	(void) memcpy(*buffer, str, len);
514*03fc8686SGarrett D'Amore 	(*buffer)[len] = '\0';
515*03fc8686SGarrett D'Amore 	*buffer += len;
5167c478bd9Sstevel@tonic-gate }
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static char *
520*03fc8686SGarrett D'Amore getarg(char *arg)
5217c478bd9Sstevel@tonic-gate {
522*03fc8686SGarrett D'Amore 	char	*xarg = arg;
5237c478bd9Sstevel@tonic-gate 	wchar_t	c;
5247c478bd9Sstevel@tonic-gate 	char	mbc[MB_LEN_MAX];
525*03fc8686SGarrett D'Amore 	size_t	len;
526*03fc8686SGarrett D'Amore 	int	escape = 0;
527*03fc8686SGarrett D'Amore 	int	inquote = 0;
5287c478bd9Sstevel@tonic-gate 
529*03fc8686SGarrett D'Amore 	arg[0] = '\0';
5307c478bd9Sstevel@tonic-gate 
531*03fc8686SGarrett D'Amore 	while (MORE) {
5327c478bd9Sstevel@tonic-gate 
533*03fc8686SGarrett D'Amore 		len = 0;
534*03fc8686SGarrett D'Amore 		c = getwchr(mbc, &len);
5357c478bd9Sstevel@tonic-gate 
536*03fc8686SGarrett D'Amore 		if (((arg - xarg) + len) > BUFLIM) {
537*03fc8686SGarrett D'Amore 			EMSG2(ARG2LONG, BUFLIM);
538*03fc8686SGarrett D'Amore 			exit(2);
5397c478bd9Sstevel@tonic-gate 			ERR = TRUE;
5407c478bd9Sstevel@tonic-gate 			return (NULL);
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 
543*03fc8686SGarrett D'Amore 		switch (c) {
544*03fc8686SGarrett D'Amore 		case '\n':
545*03fc8686SGarrett D'Amore 			if (ZERO) {
546*03fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
5477c478bd9Sstevel@tonic-gate 				continue;
5487c478bd9Sstevel@tonic-gate 			}
549*03fc8686SGarrett D'Amore 			/* FALLTHRU */
5507c478bd9Sstevel@tonic-gate 
551*03fc8686SGarrett D'Amore 		case '\0':
552*03fc8686SGarrett D'Amore 		case WEOF:	/* Note WEOF == EOF */
5537c478bd9Sstevel@tonic-gate 
554*03fc8686SGarrett D'Amore 			if (escape) {
555*03fc8686SGarrett D'Amore 				EMSG(BADESCAPE);
5567c478bd9Sstevel@tonic-gate 				ERR = TRUE;
557*03fc8686SGarrett D'Amore 				return (NULL);
558*03fc8686SGarrett D'Amore 			}
559*03fc8686SGarrett D'Amore 			if (inquote) {
560*03fc8686SGarrett D'Amore 				EMSG(MISSQUOTE);
561*03fc8686SGarrett D'Amore 				ERR = TRUE;
562*03fc8686SGarrett D'Amore 				return (NULL);
5637c478bd9Sstevel@tonic-gate 			}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 			N_lines++;
5667c478bd9Sstevel@tonic-gate 			break;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 		case '"':
569*03fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 1)) {
570*03fc8686SGarrett D'Amore 				/* treat it literally */
571*03fc8686SGarrett D'Amore 				escape = 0;
572*03fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
573*03fc8686SGarrett D'Amore 
574*03fc8686SGarrett D'Amore 			} else if (inquote == 2) {
575*03fc8686SGarrett D'Amore 				/* terminating double quote */
5767c478bd9Sstevel@tonic-gate 				inquote = 0;
577*03fc8686SGarrett D'Amore 
578*03fc8686SGarrett D'Amore 			} else {
579*03fc8686SGarrett D'Amore 				/* starting quoted string */
5807c478bd9Sstevel@tonic-gate 				inquote = 2;
581*03fc8686SGarrett D'Amore 			}
582*03fc8686SGarrett D'Amore 			continue;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 		case '\'':
585*03fc8686SGarrett D'Amore 			if (ZERO || escape || (inquote == 2)) {
586*03fc8686SGarrett D'Amore 				/* treat it literally */
587*03fc8686SGarrett D'Amore 				escape = 0;
588*03fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
5897c478bd9Sstevel@tonic-gate 
590*03fc8686SGarrett D'Amore 			} else if (inquote == 1) {
591*03fc8686SGarrett D'Amore 				/* terminating single quote */
592*03fc8686SGarrett D'Amore 				inquote = 0;
593*03fc8686SGarrett D'Amore 
594*03fc8686SGarrett D'Amore 			} else {
595*03fc8686SGarrett D'Amore 				/* starting quoted string */
596*03fc8686SGarrett D'Amore 				inquote = 1;
597*03fc8686SGarrett D'Amore 			}
598*03fc8686SGarrett D'Amore 			continue;
599*03fc8686SGarrett D'Amore 
600*03fc8686SGarrett D'Amore 		case '\\':
601d2afb7a9Scf46844 			/*
602d2afb7a9Scf46844 			 * Any unquoted character can be escaped by
603d2afb7a9Scf46844 			 * preceding it with a backslash.
604d2afb7a9Scf46844 			 */
605*03fc8686SGarrett D'Amore 			if (ZERO || inquote || escape) {
606*03fc8686SGarrett D'Amore 				escape = 0;
607*03fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
608*03fc8686SGarrett D'Amore 			} else {
609*03fc8686SGarrett D'Amore 				escape = 1;
610d2afb7a9Scf46844 			}
611*03fc8686SGarrett D'Amore 			continue;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		default:
614*03fc8686SGarrett D'Amore 			/* most times we will just want to store it */
615*03fc8686SGarrett D'Amore 			if (inquote || escape || ZERO || !iswctype(c, blank)) {
616*03fc8686SGarrett D'Amore 				escape = 0;
617*03fc8686SGarrett D'Amore 				store_str(&arg, mbc, len);
618*03fc8686SGarrett D'Amore 				continue;
6197c478bd9Sstevel@tonic-gate 			}
620*03fc8686SGarrett D'Amore 			/* unquoted blank */
6217c478bd9Sstevel@tonic-gate 			break;
6227c478bd9Sstevel@tonic-gate 		}
623*03fc8686SGarrett D'Amore 
624*03fc8686SGarrett D'Amore 		/*
625*03fc8686SGarrett D'Amore 		 * At this point we are processing a complete argument.
626*03fc8686SGarrett D'Amore 		 */
627*03fc8686SGarrett D'Amore 		if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') {
628*03fc8686SGarrett D'Amore 			MORE = FALSE;
629*03fc8686SGarrett D'Amore 			return (NULL);
6307c478bd9Sstevel@tonic-gate 		}
631*03fc8686SGarrett D'Amore 		if (c == WEOF) {
632*03fc8686SGarrett D'Amore 			MORE = FALSE;
633*03fc8686SGarrett D'Amore 		}
634*03fc8686SGarrett D'Amore 		if (xarg[0] == '\0')
635*03fc8686SGarrett D'Amore 			continue;
636*03fc8686SGarrett D'Amore 		break;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 
639*03fc8686SGarrett D'Amore 	return (xarg[0] == '\0' ? NULL : xarg);
640*03fc8686SGarrett D'Amore }
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate /*
6437c478bd9Sstevel@tonic-gate  * ermsg():	print out an error message, and indicate failure globally.
6447c478bd9Sstevel@tonic-gate  *
6457c478bd9Sstevel@tonic-gate  *	Assumes that message has already been gettext()'d. It would be
6467c478bd9Sstevel@tonic-gate  *	nice if we could just do the gettext() here, but we can't, since
6477c478bd9Sstevel@tonic-gate  *	since xgettext(1M) wouldn't be able to pick up our error message.
6487c478bd9Sstevel@tonic-gate  */
6497c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
6507c478bd9Sstevel@tonic-gate static void
6517c478bd9Sstevel@tonic-gate ermsg(char *messages, ...)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	va_list	ap;
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	va_start(ap, messages);
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "xargs: ");
6587c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, messages, ap);
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	va_end(ap);
6617c478bd9Sstevel@tonic-gate 	OK = FALSE;
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate static int
6657c478bd9Sstevel@tonic-gate echoargs()
6667c478bd9Sstevel@tonic-gate {
6677c478bd9Sstevel@tonic-gate 	char	**anarg;
6687c478bd9Sstevel@tonic-gate 	char	**tanarg;	/* tmp ptr			*/
6697c478bd9Sstevel@tonic-gate 	int		i;
6707c478bd9Sstevel@tonic-gate 	char		reply[LINE_MAX];
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	tanarg = anarg = arglist-1;
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * write out each argument, separated by a space. the tanarg
6767c478bd9Sstevel@tonic-gate 	 * nonsense is for xcu4 testsuite compliance - so that an
6777c478bd9Sstevel@tonic-gate 	 * extra space isn't echoed after the last argument.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	while (*++anarg) {		/* while there's an argument	*/
6807c478bd9Sstevel@tonic-gate 		++tanarg;		/* follow anarg			*/
6817c478bd9Sstevel@tonic-gate 		(void) write(2, *anarg, strlen(*anarg));
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 		if (*++tanarg) {	/* if there's another argument:	*/
6847c478bd9Sstevel@tonic-gate 			(void) write(2, " ", 1); /* add a space		*/
6857c478bd9Sstevel@tonic-gate 			--tanarg;	/* reset back to anarg		*/
6867c478bd9Sstevel@tonic-gate 		}
6877c478bd9Sstevel@tonic-gate 	}
6887c478bd9Sstevel@tonic-gate 	if (PROMPT == -1) {
6897c478bd9Sstevel@tonic-gate 		(void) write(2, "\n", 1);
6907c478bd9Sstevel@tonic-gate 		return (TRUE);
6917c478bd9Sstevel@tonic-gate 	}
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 	(void) write(2, "?...", 4);	/* ask the user for input	*/
6947c478bd9Sstevel@tonic-gate 
6957c478bd9Sstevel@tonic-gate 	for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
6967c478bd9Sstevel@tonic-gate 		if (reply[i] == '\n') {
6977c478bd9Sstevel@tonic-gate 			if (i == 0)
6987c478bd9Sstevel@tonic-gate 				return (FALSE);
6997c478bd9Sstevel@tonic-gate 			break;
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 	}
7027c478bd9Sstevel@tonic-gate 	reply[i] = 0;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	/* flush remainder of line if necessary */
7057c478bd9Sstevel@tonic-gate 	if (i == LINE_MAX) {
7067c478bd9Sstevel@tonic-gate 		char	bitbucket;
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 		while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
7097c478bd9Sstevel@tonic-gate 			;
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7123d63ea05Sas145665 	return (yes_check(reply));
7137c478bd9Sstevel@tonic-gate }
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate static char *
7177c478bd9Sstevel@tonic-gate insert(char *pattern, char *subst)
7187c478bd9Sstevel@tonic-gate {
7197c478bd9Sstevel@tonic-gate 	static char	buffer[MAXSBUF+1];
7207c478bd9Sstevel@tonic-gate 	int		len, ipatlen;
7217c478bd9Sstevel@tonic-gate 	char	*pat;
7227c478bd9Sstevel@tonic-gate 	char	*bufend;
7237c478bd9Sstevel@tonic-gate 	char	*pbuf;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	len = strlen(subst);
7267c478bd9Sstevel@tonic-gate 	ipatlen = strlen(INSPAT) - 1;
7277c478bd9Sstevel@tonic-gate 	pat = pattern - 1;
7287c478bd9Sstevel@tonic-gate 	pbuf = buffer;
7297c478bd9Sstevel@tonic-gate 	bufend = &buffer[MAXSBUF];
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	while (*++pat) {
732*03fc8686SGarrett D'Amore 		if (strncmp(pat, INSPAT, ipatlen) == 0) {
7337c478bd9Sstevel@tonic-gate 			if (pbuf + len >= bufend) {
7347c478bd9Sstevel@tonic-gate 				break;
7357c478bd9Sstevel@tonic-gate 			} else {
7367c478bd9Sstevel@tonic-gate 				(void) strcpy(pbuf, subst);
7377c478bd9Sstevel@tonic-gate 				pat += ipatlen;
7387c478bd9Sstevel@tonic-gate 				pbuf += len;
7397c478bd9Sstevel@tonic-gate 			}
7407c478bd9Sstevel@tonic-gate 		} else {
7417c478bd9Sstevel@tonic-gate 			*pbuf++ = *pat;
7427c478bd9Sstevel@tonic-gate 			if (pbuf >= bufend)
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (!*pat) {
7487c478bd9Sstevel@tonic-gate 		*pbuf = '\0';
7497c478bd9Sstevel@tonic-gate 		return (buffer);
7507c478bd9Sstevel@tonic-gate 	} else {
7517c478bd9Sstevel@tonic-gate 		ermsg(gettext("Maximum argument size with insertion via %s's "
7527c478bd9Sstevel@tonic-gate 		    "exceeded\n"), INSPAT);
7537c478bd9Sstevel@tonic-gate 		ERR = TRUE;
754*03fc8686SGarrett D'Amore 		return (NULL);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate static void
7607c478bd9Sstevel@tonic-gate addibuf(struct inserts	*p)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	char	*newarg, *skel, *sub;
7637c478bd9Sstevel@tonic-gate 	int		l;
7647c478bd9Sstevel@tonic-gate 
7657c478bd9Sstevel@tonic-gate 	skel = p->p_skel;
7667c478bd9Sstevel@tonic-gate 	sub = *ARGV;
7677c478bd9Sstevel@tonic-gate 	newarg = insert(skel, sub);
7687c478bd9Sstevel@tonic-gate 	if (ERR)
7697c478bd9Sstevel@tonic-gate 		return;
7707c478bd9Sstevel@tonic-gate 
771*03fc8686SGarrett D'Amore 	l = strlen(newarg) + 1;
772*03fc8686SGarrett D'Amore 	if ((ibufsize += l) > MAXIBUF) {
773*03fc8686SGarrett D'Amore 		EMSG(IBUFOVERFLOW);
7747c478bd9Sstevel@tonic-gate 		ERR = TRUE;
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	(void) strcpy(p_ibuf, newarg);
7777c478bd9Sstevel@tonic-gate 	*(p->p_ARGV) = p_ibuf;
7787c478bd9Sstevel@tonic-gate 	p_ibuf += l;
7797c478bd9Sstevel@tonic-gate }
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
783*03fc8686SGarrett D'Amore  * getwchr():	get the next wide character.
7847c478bd9Sstevel@tonic-gate  * description:
785*03fc8686SGarrett D'Amore  *	we get the next character from stdin.  This returns WEOF if no
786*03fc8686SGarrett D'Amore  *	character is present.  If ZERO is set, it gets a single byte instead
787*03fc8686SGarrett D'Amore  *	a wide character.
7887c478bd9Sstevel@tonic-gate  */
789*03fc8686SGarrett D'Amore static wint_t
790*03fc8686SGarrett D'Amore getwchr(char *mbc, size_t *sz)
7917c478bd9Sstevel@tonic-gate {
792*03fc8686SGarrett D'Amore 	size_t		i;
793*03fc8686SGarrett D'Amore 	int		c;
7947c478bd9Sstevel@tonic-gate 	wchar_t		wch;
7957c478bd9Sstevel@tonic-gate 
796*03fc8686SGarrett D'Amore 	i = 0;
797*03fc8686SGarrett D'Amore 	while (i < MB_CUR_MAX) {
798*03fc8686SGarrett D'Amore 
799*03fc8686SGarrett D'Amore 		if ((c = fgetc(stdin)) == EOF) {
800*03fc8686SGarrett D'Amore 
801*03fc8686SGarrett D'Amore 			if (i == 0) {
8027c478bd9Sstevel@tonic-gate 				/* TRUE EOF has been reached */
803*03fc8686SGarrett D'Amore 				return (WEOF);
8047c478bd9Sstevel@tonic-gate 			}
805*03fc8686SGarrett D'Amore 
8067c478bd9Sstevel@tonic-gate 			/*
8077c478bd9Sstevel@tonic-gate 			 * We have some characters in our buffer still so it
8087c478bd9Sstevel@tonic-gate 			 * must be an invalid character right before EOF.
8097c478bd9Sstevel@tonic-gate 			 */
8107c478bd9Sstevel@tonic-gate 			break;
8117c478bd9Sstevel@tonic-gate 		}
812*03fc8686SGarrett D'Amore 		mbc[i++] = (char)c;
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 		/* If this succeeds then we are done */
815*03fc8686SGarrett D'Amore 		if (ZERO) {
816*03fc8686SGarrett D'Amore 			*sz = i;
817*03fc8686SGarrett D'Amore 			return ((char)c);
818*03fc8686SGarrett D'Amore 		}
819*03fc8686SGarrett D'Amore 		if (mbtowc(&wch, mbc, i) != -1) {
820*03fc8686SGarrett D'Amore 			*sz = i;
821*03fc8686SGarrett D'Amore 			return ((wint_t)wch);
822*03fc8686SGarrett D'Amore 		}
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * We have now encountered an illegal character sequence.
8277c478bd9Sstevel@tonic-gate 	 * There is nothing much we can do at this point but
8287c478bd9Sstevel@tonic-gate 	 * return an error.  If we attempt to recover we may in fact
8297c478bd9Sstevel@tonic-gate 	 * return garbage as arguments, from the customer's point
8307c478bd9Sstevel@tonic-gate 	 * of view.  After all what if they are feeding us a file
8317c478bd9Sstevel@tonic-gate 	 * generated in another locale?
8327c478bd9Sstevel@tonic-gate 	 */
8337c478bd9Sstevel@tonic-gate 	errno = EILSEQ;
834*03fc8686SGarrett D'Amore 	PERR(CORRUPTFILE);
8357c478bd9Sstevel@tonic-gate 	exit(1);
8367c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate static int
8417c478bd9Sstevel@tonic-gate lcall(char *sub, char **subargs)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	int retcode, retry = 0;
8447c478bd9Sstevel@tonic-gate 	pid_t iwait, child;
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 	for (;;) {
8477c478bd9Sstevel@tonic-gate 		switch (child = fork()) {
8487c478bd9Sstevel@tonic-gate 		default:
8497c478bd9Sstevel@tonic-gate 			while ((iwait = wait(&retcode)) != child &&
8507c478bd9Sstevel@tonic-gate 			    iwait != (pid_t)-1)
8517c478bd9Sstevel@tonic-gate 				;
8527c478bd9Sstevel@tonic-gate 			if (iwait == (pid_t)-1) {
853*03fc8686SGarrett D'Amore 				PERR(WAITFAIL);
8547c478bd9Sstevel@tonic-gate 				exit(122);
8557c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
8567c478bd9Sstevel@tonic-gate 			}
8577c478bd9Sstevel@tonic-gate 			if (WIFSIGNALED(retcode)) {
858*03fc8686SGarrett D'Amore 				EMSG2(CHILDSIG, WTERMSIG(retcode));
8597c478bd9Sstevel@tonic-gate 				exit(125);
8607c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
8617c478bd9Sstevel@tonic-gate 			}
8627c478bd9Sstevel@tonic-gate 			if ((WEXITSTATUS(retcode) & 0377) == 0377) {
863*03fc8686SGarrett D'Amore 				EMSG(CHILDFAIL);
8647c478bd9Sstevel@tonic-gate 				exit(124);
8657c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
8667c478bd9Sstevel@tonic-gate 			}
8677c478bd9Sstevel@tonic-gate 			return (WEXITSTATUS(retcode));
8687c478bd9Sstevel@tonic-gate 		case 0:
8697c478bd9Sstevel@tonic-gate 			(void) execvp(sub, subargs);
870*03fc8686SGarrett D'Amore 			PERR(EXECFAIL);
8717c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
8727c478bd9Sstevel@tonic-gate 				exit(126);
8737c478bd9Sstevel@tonic-gate 			exit(127);
8747c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
8757c478bd9Sstevel@tonic-gate 		case -1:
8767c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN && retry++ < FORK_RETRY) {
877*03fc8686SGarrett D'Amore 				PERR(FORKFAIL);
8787c478bd9Sstevel@tonic-gate 				exit(123);
8797c478bd9Sstevel@tonic-gate 			}
8807c478bd9Sstevel@tonic-gate 			(void) sleep(1);
8817c478bd9Sstevel@tonic-gate 		}
8827c478bd9Sstevel@tonic-gate 	}
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate static void
8877c478bd9Sstevel@tonic-gate usage()
8887c478bd9Sstevel@tonic-gate {
889*03fc8686SGarrett D'Amore 	ermsg(_(USAGEMSG));
8907c478bd9Sstevel@tonic-gate 	OK = FALSE;
8917c478bd9Sstevel@tonic-gate }
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 
8957c478bd9Sstevel@tonic-gate /*
8967c478bd9Sstevel@tonic-gate  * parseargs():		modify the args
8977c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
8987c478bd9Sstevel@tonic-gate  *	and getopts(3C) is clueless about this nonsense, we change the
8997c478bd9Sstevel@tonic-gate  *	our local argument count and strings to separate this out,
9007c478bd9Sstevel@tonic-gate  *	and make it easier to handle via getopts(3c).
9017c478bd9Sstevel@tonic-gate  *
9027c478bd9Sstevel@tonic-gate  *	-e	-> "-e ""
9037c478bd9Sstevel@tonic-gate  *	-e3	-> "-e "3"
9047c478bd9Sstevel@tonic-gate  *	-Estr	-> "-E "str"
9057c478bd9Sstevel@tonic-gate  *	-i	-> "-i "{}"
9067c478bd9Sstevel@tonic-gate  *	-irep	-> "-i "rep"
9077c478bd9Sstevel@tonic-gate  *	-l	-> "-i "1"
9087c478bd9Sstevel@tonic-gate  *	-l10	-> "-i "10"
9097c478bd9Sstevel@tonic-gate  *
9107c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
9117c478bd9Sstevel@tonic-gate  */
9127c478bd9Sstevel@tonic-gate static void
9137c478bd9Sstevel@tonic-gate parseargs(int ac, char **av)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
9167c478bd9Sstevel@tonic-gate 	int cflag;		/* 0 = not processing cmd arg		*/
9177c478bd9Sstevel@tonic-gate 
9187c478bd9Sstevel@tonic-gate 	if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
919*03fc8686SGarrett D'Amore 		PERR(MALLOCFAIL);
9207c478bd9Sstevel@tonic-gate 		exit(1);
9217c478bd9Sstevel@tonic-gate 	}
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	/* for each argument, see if we need to change things:		*/
9247c478bd9Sstevel@tonic-gate 	for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
9257c478bd9Sstevel@tonic-gate 		if ((mav[mac] = strdup(av[i])) == NULL) {
926*03fc8686SGarrett D'Amore 			PERR(MALLOCFAIL);
9277c478bd9Sstevel@tonic-gate 			exit(1);
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 		/* -- has been found or argument list is fully processes */
9317c478bd9Sstevel@tonic-gate 		if (cflag)
9327c478bd9Sstevel@tonic-gate 			continue;
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		/*
9357c478bd9Sstevel@tonic-gate 		 * if we're doing special processing, and we've got a flag
9367c478bd9Sstevel@tonic-gate 		 */
9377c478bd9Sstevel@tonic-gate 		else if ((av[i][0] == '-') && (av[i][1] != NULL)) {
9387c478bd9Sstevel@tonic-gate 			char	*def;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 			switch (av[i][1]) {
9417c478bd9Sstevel@tonic-gate 			case	'e':
9427c478bd9Sstevel@tonic-gate 				def = ""; /* -e with no arg turns off eof */
9437c478bd9Sstevel@tonic-gate 				goto process_special;
9447c478bd9Sstevel@tonic-gate 			case	'i':
9457c478bd9Sstevel@tonic-gate 				def = INSPAT_STR;
9467c478bd9Sstevel@tonic-gate 				goto process_special;
9477c478bd9Sstevel@tonic-gate 			case	'l':
9487c478bd9Sstevel@tonic-gate 				def = "1";
9497c478bd9Sstevel@tonic-gate process_special:
9507c478bd9Sstevel@tonic-gate 				/*
9517c478bd9Sstevel@tonic-gate 				 * if there's no sub-option, we *must* add
9527c478bd9Sstevel@tonic-gate 				 * a default one. this is because xargs must
9537c478bd9Sstevel@tonic-gate 				 * be able to distinguish between a valid
9547c478bd9Sstevel@tonic-gate 				 * suboption, and a command name.
9557c478bd9Sstevel@tonic-gate 				 */
9567c478bd9Sstevel@tonic-gate 				if (av[i][2] == NULL) {
9577c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(def);
9587c478bd9Sstevel@tonic-gate 				} else {
9597c478bd9Sstevel@tonic-gate 					/* clear out our version: */
9607c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
9617c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(&av[i][2]);
9627c478bd9Sstevel@tonic-gate 				}
9637c478bd9Sstevel@tonic-gate 				if (mav[mac] == NULL) {
964*03fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
9657c478bd9Sstevel@tonic-gate 					exit(1);
9667c478bd9Sstevel@tonic-gate 				}
9677c478bd9Sstevel@tonic-gate 				break;
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 			/* flags with required subarguments:		*/
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 			/*
9727c478bd9Sstevel@tonic-gate 			 * there are two separate cases here. either the
9737c478bd9Sstevel@tonic-gate 			 * flag can have the normal XCU4 handling
9747c478bd9Sstevel@tonic-gate 			 * (of the form: -X subargument); or it can have
9757c478bd9Sstevel@tonic-gate 			 * the old solaris 2.[0-4] handling (of the
9767c478bd9Sstevel@tonic-gate 			 * form: -Xsubargument). in order to maintain
9777c478bd9Sstevel@tonic-gate 			 * backwards compatibility, we must support the
9787c478bd9Sstevel@tonic-gate 			 * latter case. we handle the latter possibility
9797c478bd9Sstevel@tonic-gate 			 * first so both the old solaris way of handling
9807c478bd9Sstevel@tonic-gate 			 * and the new XCU4 way of handling things are allowed.
9817c478bd9Sstevel@tonic-gate 			 */
9827c478bd9Sstevel@tonic-gate 			case	'n':	/* FALLTHROUGH			*/
9837c478bd9Sstevel@tonic-gate 			case	's':	/* FALLTHROUGH			*/
9847c478bd9Sstevel@tonic-gate 			case	'E':	/* FALLTHROUGH			*/
9857c478bd9Sstevel@tonic-gate 			case	'I':	/* FALLTHROUGH			*/
9867c478bd9Sstevel@tonic-gate 			case	'L':
9877c478bd9Sstevel@tonic-gate 				/*
9887c478bd9Sstevel@tonic-gate 				 * if the second character isn't null, then
9897c478bd9Sstevel@tonic-gate 				 * the user has specified the old syntax.
9907c478bd9Sstevel@tonic-gate 				 * we move the subargument into our
9917c478bd9Sstevel@tonic-gate 				 * mod'd argument list.
9927c478bd9Sstevel@tonic-gate 				 */
9937c478bd9Sstevel@tonic-gate 				if (av[i][2] != NULL) {
9947c478bd9Sstevel@tonic-gate 					/* first clean things up:	*/
9957c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 					/* now add the separation:	*/
9987c478bd9Sstevel@tonic-gate 					++mac;	/* inc to next mod'd arg */
9997c478bd9Sstevel@tonic-gate 					if ((mav[mac] = strdup(&av[i][2])) ==
10007c478bd9Sstevel@tonic-gate 					    NULL) {
1001*03fc8686SGarrett D'Amore 						PERR(MALLOCFAIL);
10027c478bd9Sstevel@tonic-gate 						exit(1);
10037c478bd9Sstevel@tonic-gate 					}
10047c478bd9Sstevel@tonic-gate 					break;
10057c478bd9Sstevel@tonic-gate 				}
10067c478bd9Sstevel@tonic-gate 				i++;
10077c478bd9Sstevel@tonic-gate 				mac++;
1008952d685eScf46844 
10097c478bd9Sstevel@tonic-gate 				if (av[i] == NULL) {
10107c478bd9Sstevel@tonic-gate 					mav[mac] = NULL;
10117c478bd9Sstevel@tonic-gate 					return;
10127c478bd9Sstevel@tonic-gate 				}
10137c478bd9Sstevel@tonic-gate 				if ((mav[mac] = strdup(av[i])) == NULL) {
1014*03fc8686SGarrett D'Amore 					PERR(MALLOCFAIL);
10157c478bd9Sstevel@tonic-gate 					exit(1);
10167c478bd9Sstevel@tonic-gate 				}
10177c478bd9Sstevel@tonic-gate 				break;
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 			/* flags */
10207c478bd9Sstevel@tonic-gate 			case 'p' :
10217c478bd9Sstevel@tonic-gate 			case 't' :
10227c478bd9Sstevel@tonic-gate 			case 'x' :
10237c478bd9Sstevel@tonic-gate 				break;
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 			case '-' :
10267c478bd9Sstevel@tonic-gate 			default:
10277c478bd9Sstevel@tonic-gate 				/*
10287c478bd9Sstevel@tonic-gate 				 * here we've hit the cmd argument. so
10297c478bd9Sstevel@tonic-gate 				 * we'll stop special processing, as the
10307c478bd9Sstevel@tonic-gate 				 * cmd may have a "-i" etc., argument,
10317c478bd9Sstevel@tonic-gate 				 * and we don't want to add a "" to it.
10327c478bd9Sstevel@tonic-gate 				 */
10337c478bd9Sstevel@tonic-gate 				cflag = 1;
10347c478bd9Sstevel@tonic-gate 				break;
10357c478bd9Sstevel@tonic-gate 			}
10367c478bd9Sstevel@tonic-gate 		} else if (i > 0) {	/* if we're not the 1st arg	*/
10377c478bd9Sstevel@tonic-gate 			/*
10387c478bd9Sstevel@tonic-gate 			 * if it's not a flag, then it *must* be the cmd.
10397c478bd9Sstevel@tonic-gate 			 * set cflag, so we don't mishandle the -[eil] flags.
10407c478bd9Sstevel@tonic-gate 			 */
10417c478bd9Sstevel@tonic-gate 			cflag = 1;
10427c478bd9Sstevel@tonic-gate 		}
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	mav[mac] = NULL;
10467c478bd9Sstevel@tonic-gate }
1047