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