xref: /illumos-gate/usr/src/cmd/xargs/xargs.c (revision 788f581b)
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
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/wait.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <fcntl.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <stdarg.h>
407c478bd9Sstevel@tonic-gate #include <libgen.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate #include <limits.h>
437c478bd9Sstevel@tonic-gate #include <wchar.h>
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate #include <langinfo.h>
467c478bd9Sstevel@tonic-gate #include <stropts.h>
477c478bd9Sstevel@tonic-gate #include <poll.h>
487c478bd9Sstevel@tonic-gate #include <errno.h>
497c478bd9Sstevel@tonic-gate #include <stdarg.h>
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	HEAD	0
527c478bd9Sstevel@tonic-gate #define	TAIL	1
537c478bd9Sstevel@tonic-gate #define	FALSE 0
547c478bd9Sstevel@tonic-gate #define	TRUE 1
557c478bd9Sstevel@tonic-gate #define	MAXSBUF 255
567c478bd9Sstevel@tonic-gate #define	MAXIBUF 512
577c478bd9Sstevel@tonic-gate #define	MAXINSERTS 5
587c478bd9Sstevel@tonic-gate #define	BUFSIZE LINE_MAX
597c478bd9Sstevel@tonic-gate #define	MAXARGS 255
607c478bd9Sstevel@tonic-gate #define	INSPAT_STR	"{}"	/* default replstr string for -[Ii]	*/
617c478bd9Sstevel@tonic-gate #define	FORK_RETRY	5
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #define	QBUF_STARTLEN 255  /* start size of growable string buffer */
647c478bd9Sstevel@tonic-gate #define	QBUF_INC 100	   /* how much to grow a growable string by */
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static wctype_t	blank;
677c478bd9Sstevel@tonic-gate static char	*arglist[MAXARGS+1];
687c478bd9Sstevel@tonic-gate static char	argbuf[BUFSIZE+1];
697c478bd9Sstevel@tonic-gate static char	*next = argbuf;
707c478bd9Sstevel@tonic-gate static char	*lastarg = "";
717c478bd9Sstevel@tonic-gate static char	**ARGV = arglist;
727c478bd9Sstevel@tonic-gate static char	*LEOF = "_";
737c478bd9Sstevel@tonic-gate static char	*INSPAT = INSPAT_STR;
747c478bd9Sstevel@tonic-gate static char	ins_buf[MAXIBUF];
757c478bd9Sstevel@tonic-gate static char	*p_ibuf;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate static struct inserts {
787c478bd9Sstevel@tonic-gate 	char	**p_ARGV;	/* where to put newarg ptr in arg list */
797c478bd9Sstevel@tonic-gate 	char	*p_skel;	/* ptr to arg template */
807c478bd9Sstevel@tonic-gate } saveargv[MAXINSERTS];
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate static off_t	file_offset = 0;
837c478bd9Sstevel@tonic-gate static int	PROMPT = -1;
847c478bd9Sstevel@tonic-gate static int	BUFLIM = BUFSIZE;
857c478bd9Sstevel@tonic-gate static int	N_ARGS = 0;
867c478bd9Sstevel@tonic-gate static int	N_args = 0;
877c478bd9Sstevel@tonic-gate static int	N_lines = 0;
887c478bd9Sstevel@tonic-gate static int	DASHX = FALSE;
897c478bd9Sstevel@tonic-gate static int	MORE = TRUE;
907c478bd9Sstevel@tonic-gate static int	PER_LINE = FALSE;
917c478bd9Sstevel@tonic-gate static int	ERR = FALSE;
927c478bd9Sstevel@tonic-gate static int	OK = TRUE;
937c478bd9Sstevel@tonic-gate static int	LEGAL = FALSE;
947c478bd9Sstevel@tonic-gate static int	TRACE = FALSE;
957c478bd9Sstevel@tonic-gate static int	INSERT = FALSE;
967c478bd9Sstevel@tonic-gate static int	linesize = 0;
977c478bd9Sstevel@tonic-gate static int	ibufsize = 0;
987c478bd9Sstevel@tonic-gate static char	*yesstr;	/* the string contains int'l for "yes"	*/
997c478bd9Sstevel@tonic-gate static int	exitstat = 0;	/* our exit status			*/
1007c478bd9Sstevel@tonic-gate static int	mac;		/* modified argc, after parsing		*/
1017c478bd9Sstevel@tonic-gate static char	**mav;		/* modified argv, after parsing		*/
1027c478bd9Sstevel@tonic-gate static int	n_inserts;	/* # of insertions.			*/
1037c478bd9Sstevel@tonic-gate static int	inquote = 0;	/* processing a quoted string		*/
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate /*
1067c478bd9Sstevel@tonic-gate  * the pio structure is used to save any pending input before the
1077c478bd9Sstevel@tonic-gate  * user replies to a prompt. the pending input is saved here,
1087c478bd9Sstevel@tonic-gate  * for the appropriate processing later.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate typedef struct pio {
1117c478bd9Sstevel@tonic-gate 	struct pio *next;	/* next in stack			*/
1127c478bd9Sstevel@tonic-gate 	char *start;		/* starting addr of the buffer		*/
1137c478bd9Sstevel@tonic-gate 	char *cur;		/* ptr to current char in buf		*/
1147c478bd9Sstevel@tonic-gate 	size_t length;		/* number of bytes remaining		*/
1157c478bd9Sstevel@tonic-gate } pio;
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate static pio *queued_data = NULL;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /* our usage message:							*/
1207c478bd9Sstevel@tonic-gate #define	USAGEMSG "Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] "\
1217c478bd9Sstevel@tonic-gate 	"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\
1227c478bd9Sstevel@tonic-gate 	"[cmd [args ...]]\n"
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate static int	echoargs();
1257c478bd9Sstevel@tonic-gate static int	getchr(void);
1267c478bd9Sstevel@tonic-gate static wchar_t	getwchr(void);
1277c478bd9Sstevel@tonic-gate static void	ungetwchr(wchar_t);
1287c478bd9Sstevel@tonic-gate static int	lcall(char *sub, char **subargs);
1297c478bd9Sstevel@tonic-gate static int	xindex(char *as1, char *as2);
1307c478bd9Sstevel@tonic-gate static void	addibuf(struct inserts *p);
1317c478bd9Sstevel@tonic-gate static void	ermsg(char *messages, ...);
1327c478bd9Sstevel@tonic-gate static char	*addarg(char *arg);
1337c478bd9Sstevel@tonic-gate static char	*checklen(char *arg);
1347c478bd9Sstevel@tonic-gate static size_t   store_wchr(char **, size_t *, size_t, wchar_t);
1357c478bd9Sstevel@tonic-gate static char	*getarg();
1367c478bd9Sstevel@tonic-gate static char	*insert(char *pattern, char *subst);
1377c478bd9Sstevel@tonic-gate static void	usage();
1387c478bd9Sstevel@tonic-gate static void	parseargs();
1397c478bd9Sstevel@tonic-gate static void	saveinput();
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate 
14243a29105Srobbin int
1437c478bd9Sstevel@tonic-gate main(int argc, char **argv)
1447c478bd9Sstevel@tonic-gate {
1457c478bd9Sstevel@tonic-gate 	int	j;
1467c478bd9Sstevel@tonic-gate 	struct inserts *psave;
1477c478bd9Sstevel@tonic-gate 	int c;
1487c478bd9Sstevel@tonic-gate 	int	initsize;
1497c478bd9Sstevel@tonic-gate 	char	*cmdname, *initbuf, **initlist;
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	/* initialization */
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	blank = wctype("blank");
1557c478bd9Sstevel@tonic-gate 	n_inserts = 0;
1567c478bd9Sstevel@tonic-gate 	psave = saveargv;
1577c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
1587c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D 		*/
1597c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"	/* Use this only if it weren't 		*/
1607c478bd9Sstevel@tonic-gate #endif
1617c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * now we get the appropriate "yes" string for our locale.
1657c478bd9Sstevel@tonic-gate 	 * since this may be a multibyte character, we store the
1667c478bd9Sstevel@tonic-gate 	 * string which is returned. later on, when we're looking for
1677c478bd9Sstevel@tonic-gate 	 * a "y" in response to our prompt, we'll use the first
1687c478bd9Sstevel@tonic-gate 	 * multibyte character of yesstr as a comparision.
1697c478bd9Sstevel@tonic-gate 	 */
1707c478bd9Sstevel@tonic-gate 	initbuf = nl_langinfo(YESSTR);	/* initbuf is a tmp placeholder here */
1717c478bd9Sstevel@tonic-gate 	if ((yesstr = malloc(strlen(initbuf) + 1)) == NULL) {
1727c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: Memory allocation failure"));
1737c478bd9Sstevel@tonic-gate 		exit(1);
1747c478bd9Sstevel@tonic-gate 	}
1757c478bd9Sstevel@tonic-gate 	(void) strcpy(yesstr, initbuf);
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	parseargs(argc, argv);
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 	/* handling all of xargs arguments:				*/
1807c478bd9Sstevel@tonic-gate 	while ((c = getopt(mac, mav, "tpe:E:I:i:L:l:n:s:x")) != EOF) {
1817c478bd9Sstevel@tonic-gate 		switch (c) {
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) {
1887c478bd9Sstevel@tonic-gate 				perror(gettext("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.
2077c478bd9Sstevel@tonic-gate 			 * eofstr *is* required here:
2087c478bd9Sstevel@tonic-gate 			 */
2097c478bd9Sstevel@tonic-gate 			LEOF = optarg;
2107c478bd9Sstevel@tonic-gate #ifdef XPG6
2117c478bd9Sstevel@tonic-gate 			if (LEOF == NULL) {
2127c478bd9Sstevel@tonic-gate #else
2137c478bd9Sstevel@tonic-gate 			if ((LEOF == NULL) || (*LEOF == NULL)) {
2147c478bd9Sstevel@tonic-gate #endif
215*788f581bSceastha 				ermsg(gettext(
216*788f581bSceastha 				    "Option requires an argument: -%c\n"), c);
2177c478bd9Sstevel@tonic-gate 			}
2187c478bd9Sstevel@tonic-gate 			break;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		case 'I':
2217c478bd9Sstevel@tonic-gate 			/* -I replstr: Insert mode. replstr *is* required. */
2227c478bd9Sstevel@tonic-gate 			INSERT = PER_LINE = LEGAL = TRUE;
2237c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
224*788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2257c478bd9Sstevel@tonic-gate 				INSPAT = optarg;
2267c478bd9Sstevel@tonic-gate 			} else {
227*788f581bSceastha 				ermsg(gettext(
228*788f581bSceastha 				    "Option requires an argument: -%c\n"), c);
2297c478bd9Sstevel@tonic-gate 			}
2307c478bd9Sstevel@tonic-gate 			break;
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		case 'i':
2337c478bd9Sstevel@tonic-gate 			/*
2347c478bd9Sstevel@tonic-gate 			 * -i [replstr]: insert mode, with *optional* replstr.
2357c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2367c478bd9Sstevel@tonic-gate 			 * it's not given, then the string INSPAT_STR will
2377c478bd9Sstevel@tonic-gate 			 * be assumed.
2387c478bd9Sstevel@tonic-gate 			 *
2397c478bd9Sstevel@tonic-gate 			 * Since getopts(3C) doesn't handle the case of an
2407c478bd9Sstevel@tonic-gate 			 * optional variable argument at all, we have to
2417c478bd9Sstevel@tonic-gate 			 * parse this by hand:
2427c478bd9Sstevel@tonic-gate 			 */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 			INSERT = PER_LINE = LEGAL = TRUE;
2457c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
246*788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2477c478bd9Sstevel@tonic-gate 				INSPAT = optarg;
2487c478bd9Sstevel@tonic-gate 			} else {
2497c478bd9Sstevel@tonic-gate 				/*
2507c478bd9Sstevel@tonic-gate 				 * here, there is no next argument. so
2517c478bd9Sstevel@tonic-gate 				 * we reset INSPAT to the INSPAT_STR.
2527c478bd9Sstevel@tonic-gate 				 * we *have* to do this, as -i/I may have
2537c478bd9Sstevel@tonic-gate 				 * been given previously, and XCU4 requires
2547c478bd9Sstevel@tonic-gate 				 * that only "the last one specified takes
2557c478bd9Sstevel@tonic-gate 				 * effect".
2567c478bd9Sstevel@tonic-gate 				 */
2577c478bd9Sstevel@tonic-gate 				INSPAT = INSPAT_STR;
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 			break;
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 		case 'L':
2627c478bd9Sstevel@tonic-gate 			/*
2637c478bd9Sstevel@tonic-gate 			 * -L number: # of times cmd is executed
2647c478bd9Sstevel@tonic-gate 			 * number *is* required here:
2657c478bd9Sstevel@tonic-gate 			 */
2667c478bd9Sstevel@tonic-gate 			PER_LINE = TRUE;
2677c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
2687c478bd9Sstevel@tonic-gate 			INSERT = FALSE;
269*788f581bSceastha 			if ((optarg == NULL) || (*optarg == '\0')) {
270*788f581bSceastha 				ermsg(gettext(
271*788f581bSceastha 				    "Option requires an argument: -%c\n"), c);
272*788f581bSceastha 			} else if ((PER_LINE = atoi(optarg)) <= 0) {
2737c478bd9Sstevel@tonic-gate 				ermsg(gettext("#lines must be positive "
2747c478bd9Sstevel@tonic-gate 				    "int: %s\n"), optarg);
2757c478bd9Sstevel@tonic-gate 			}
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 		case 'l':
2797c478bd9Sstevel@tonic-gate 			/*
2807c478bd9Sstevel@tonic-gate 			 * -l [number]: # of times cmd is executed
2817c478bd9Sstevel@tonic-gate 			 * N.B. that an argument *isn't* required here; if
2827c478bd9Sstevel@tonic-gate 			 * it's not given, then 1 is assumed.
2837c478bd9Sstevel@tonic-gate 			 *
2847c478bd9Sstevel@tonic-gate 			 * parseargs handles the optional arg processing.
2857c478bd9Sstevel@tonic-gate 			 */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 			PER_LINE = LEGAL = TRUE;  /* initialization	*/
2887c478bd9Sstevel@tonic-gate 			N_ARGS = 0;
2897c478bd9Sstevel@tonic-gate 			INSERT = FALSE;
2907c478bd9Sstevel@tonic-gate 
291*788f581bSceastha 			if ((optarg != NULL) && (*optarg != '\0')) {
2927c478bd9Sstevel@tonic-gate 				if ((PER_LINE = atoi(optarg)) <= 0)
2937c478bd9Sstevel@tonic-gate 					PER_LINE = 1;
2947c478bd9Sstevel@tonic-gate 			}
2957c478bd9Sstevel@tonic-gate 			break;
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		case 'n':	/* -n number: # stdin args		*/
2987c478bd9Sstevel@tonic-gate 			/*
2997c478bd9Sstevel@tonic-gate 			 * -n number: # stdin args.
3007c478bd9Sstevel@tonic-gate 			 * number *is* required here:
3017c478bd9Sstevel@tonic-gate 			 */
302*788f581bSceastha 			if ((optarg == NULL) || (*optarg == '\0')) {
303*788f581bSceastha 				ermsg(gettext(
304*788f581bSceastha 				    "Option requires an argument: -%c\n"), c);
305*788f581bSceastha 			} else if ((N_ARGS = atoi(optarg)) <= 0) {
3067c478bd9Sstevel@tonic-gate 				ermsg(gettext("#args must be positive "
3077c478bd9Sstevel@tonic-gate 				    "int: %s\n"), optarg);
3087c478bd9Sstevel@tonic-gate 			} else {
3097c478bd9Sstevel@tonic-gate 				LEGAL = DASHX || N_ARGS == 1;
3107c478bd9Sstevel@tonic-gate 				INSERT = PER_LINE = FALSE;
3117c478bd9Sstevel@tonic-gate 			}
3127c478bd9Sstevel@tonic-gate 			break;
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 		case 's':	/* -s size: set max size of each arg list */
315*788f581bSceastha 			if ((optarg == NULL) || (*optarg == '\0')) {
316*788f581bSceastha 				ermsg(gettext(
317*788f581bSceastha 				    "Option requires an argument: -%c\n"), c);
318*788f581bSceastha 			} else {
319*788f581bSceastha 				BUFLIM = atoi(optarg);
320*788f581bSceastha 				if (BUFLIM > BUFSIZE || BUFLIM <= 0) {
321*788f581bSceastha 					ermsg(gettext(
322*788f581bSceastha 					    "0 < max-cmd-line-size <= %d: "
323*788f581bSceastha 					    "%s\n"), BUFSIZE, optarg);
324*788f581bSceastha 				}
3257c478bd9Sstevel@tonic-gate 			}
3267c478bd9Sstevel@tonic-gate 			break;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 		case 'x':	/* -x: terminate if args > size limit	*/
3297c478bd9Sstevel@tonic-gate 			DASHX = LEGAL = TRUE;
3307c478bd9Sstevel@tonic-gate 			break;
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 		default:
3337c478bd9Sstevel@tonic-gate 			/*
3347c478bd9Sstevel@tonic-gate 			 * bad argument. complain and get ready to die.
3357c478bd9Sstevel@tonic-gate 			 */
3367c478bd9Sstevel@tonic-gate 			ERR = TRUE;
3377c478bd9Sstevel@tonic-gate 			usage();
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 			exit(2);
3407c478bd9Sstevel@tonic-gate 			break;
3417c478bd9Sstevel@tonic-gate 		}
3427c478bd9Sstevel@tonic-gate 	}
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	/*
3457c478bd9Sstevel@tonic-gate 	 * if anything called ermsg(), something screwed up, so
3467c478bd9Sstevel@tonic-gate 	 * we exit early.
3477c478bd9Sstevel@tonic-gate 	 */
3487c478bd9Sstevel@tonic-gate 	if (OK == FALSE) {
3497c478bd9Sstevel@tonic-gate 		ERR = TRUE;
3507c478bd9Sstevel@tonic-gate 		usage();
3517c478bd9Sstevel@tonic-gate 		exit(2);
3527c478bd9Sstevel@tonic-gate 	}
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	/*
3557c478bd9Sstevel@tonic-gate 	 * we're finished handling xargs's options, so now pick up
3567c478bd9Sstevel@tonic-gate 	 * the command name (if any), and it's options.
3577c478bd9Sstevel@tonic-gate 	 */
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	mac -= optind;	/* dec arg count by what we've processed 	*/
3617c478bd9Sstevel@tonic-gate 	mav += optind;	/* inc to current mav				*/
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	if (mac <= 0) {	/* if there're no more args to process,	*/
3647c478bd9Sstevel@tonic-gate 		cmdname = "/usr/bin/echo";	/* our default command	*/
3657c478bd9Sstevel@tonic-gate 		*ARGV++ = addarg(cmdname);	/* use the default cmd.	*/
3667c478bd9Sstevel@tonic-gate 	} else {	/* otherwise keep parsing rest of the string.	*/
3677c478bd9Sstevel@tonic-gate 		/*
3687c478bd9Sstevel@tonic-gate 		 * note that we can't use getopts(3C), and *must* parse
3697c478bd9Sstevel@tonic-gate 		 * this by hand, as we don't know apriori what options the
3707c478bd9Sstevel@tonic-gate 		 * command will take.
3717c478bd9Sstevel@tonic-gate 		 */
3727c478bd9Sstevel@tonic-gate 		cmdname = *mav;	/* get the command name	*/
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 
3757c478bd9Sstevel@tonic-gate 		/* pick up the remaining args from the command line:	*/
3767c478bd9Sstevel@tonic-gate 		while ((OK == TRUE) && (mac-- > 0)) {
3777c478bd9Sstevel@tonic-gate 			/*
3787c478bd9Sstevel@tonic-gate 			 * while we haven't crapped out, and there's
3797c478bd9Sstevel@tonic-gate 			 * work to do:
3807c478bd9Sstevel@tonic-gate 			 */
3817c478bd9Sstevel@tonic-gate 			if (INSERT && ! ERR) {
3827c478bd9Sstevel@tonic-gate 				if (xindex(*mav, INSPAT) != -1) {
3837c478bd9Sstevel@tonic-gate 					if (++n_inserts > MAXINSERTS) {
3847c478bd9Sstevel@tonic-gate 						ermsg(gettext("too many args "
3857c478bd9Sstevel@tonic-gate 						    "with %s\n"), INSPAT);
3867c478bd9Sstevel@tonic-gate 						ERR = TRUE;
3877c478bd9Sstevel@tonic-gate 					}
3887c478bd9Sstevel@tonic-gate 					psave->p_ARGV = ARGV;
3897c478bd9Sstevel@tonic-gate 					(psave++)->p_skel = *mav;
3907c478bd9Sstevel@tonic-gate 				}
3917c478bd9Sstevel@tonic-gate 			}
3927c478bd9Sstevel@tonic-gate 			*ARGV++ = addarg(*mav++);
3937c478bd9Sstevel@tonic-gate 		}
3947c478bd9Sstevel@tonic-gate 	}
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	/* pick up args from standard input */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	initbuf = next;
3997c478bd9Sstevel@tonic-gate 	initlist = ARGV;
4007c478bd9Sstevel@tonic-gate 	initsize = linesize;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	while (OK && MORE) {
4037c478bd9Sstevel@tonic-gate 		N_args = 0;
4047c478bd9Sstevel@tonic-gate 		N_lines = 0;
4057c478bd9Sstevel@tonic-gate 		next = initbuf;
4067c478bd9Sstevel@tonic-gate 		ARGV = initlist;
4077c478bd9Sstevel@tonic-gate 		linesize = initsize;
4087c478bd9Sstevel@tonic-gate 		if (*lastarg) {
4097c478bd9Sstevel@tonic-gate 			*ARGV++ = addarg(lastarg);
4107c478bd9Sstevel@tonic-gate 			lastarg = "";
4117c478bd9Sstevel@tonic-gate 		}
4127c478bd9Sstevel@tonic-gate 
4137c478bd9Sstevel@tonic-gate 		while (((ARGV - arglist) < MAXARGS) &&
4147c478bd9Sstevel@tonic-gate 		    ((*ARGV++ = getarg()) != NULL) && OK)
4157c478bd9Sstevel@tonic-gate 			;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 		/* insert arg if requested */
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 		if (!ERR && INSERT) {
4207c478bd9Sstevel@tonic-gate 			if ((!MORE) && (N_lines == 0)) {
4217c478bd9Sstevel@tonic-gate 				exit(exitstat);
4227c478bd9Sstevel@tonic-gate 			}
4237c478bd9Sstevel@tonic-gate 					/* no more input lines */
4247c478bd9Sstevel@tonic-gate 			p_ibuf = ins_buf;
4257c478bd9Sstevel@tonic-gate 			ARGV--;
4267c478bd9Sstevel@tonic-gate 			j = ibufsize = 0;
4277c478bd9Sstevel@tonic-gate 			for (psave = saveargv; ++j <= n_inserts; ++psave) {
4287c478bd9Sstevel@tonic-gate 				addibuf(psave);
4297c478bd9Sstevel@tonic-gate 				if (ERR)
4307c478bd9Sstevel@tonic-gate 					break;
4317c478bd9Sstevel@tonic-gate 			}
4327c478bd9Sstevel@tonic-gate 		}
4337c478bd9Sstevel@tonic-gate 		*ARGV = 0;
4347c478bd9Sstevel@tonic-gate 
4357c478bd9Sstevel@tonic-gate 		if (n_inserts > 0) {
4367c478bd9Sstevel@tonic-gate 			int t_ninserts;
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 			/*
4397c478bd9Sstevel@tonic-gate 			 * if we've done any insertions, re-calculate the
4407c478bd9Sstevel@tonic-gate 			 * linesize. bomb out if we've exceeded our length.
4417c478bd9Sstevel@tonic-gate 			 */
4427c478bd9Sstevel@tonic-gate 			t_ninserts = n_inserts;
4437c478bd9Sstevel@tonic-gate 			n_inserts = 0;	/* inserts have been done 	*/
4447c478bd9Sstevel@tonic-gate 			linesize = 0;	/* recalculate this		*/
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 			/* for each current argument in the list:	*/
4477c478bd9Sstevel@tonic-gate 			for (ARGV = arglist; *ARGV != NULL; ARGV++) {
4487c478bd9Sstevel@tonic-gate 				/* recalculate everything.		*/
4497c478bd9Sstevel@tonic-gate 				if (checklen(*ARGV) != 0) {
4507c478bd9Sstevel@tonic-gate 					if (N_ARGS && (N_args >= N_ARGS)) {
4517c478bd9Sstevel@tonic-gate 						N_lines = N_args = 0;
4527c478bd9Sstevel@tonic-gate 						OK = FALSE;
4537c478bd9Sstevel@tonic-gate 						ERR = TRUE;
4547c478bd9Sstevel@tonic-gate 					}
4557c478bd9Sstevel@tonic-gate 				}
4567c478bd9Sstevel@tonic-gate 			}
4577c478bd9Sstevel@tonic-gate 			n_inserts = t_ninserts;
4587c478bd9Sstevel@tonic-gate 		}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 		/* exec command */
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 		if (!ERR) {
4637c478bd9Sstevel@tonic-gate 			if (!MORE &&
4647c478bd9Sstevel@tonic-gate 			    (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0))
4657c478bd9Sstevel@tonic-gate 				exit(exitstat);
4667c478bd9Sstevel@tonic-gate 			OK = TRUE;
4677c478bd9Sstevel@tonic-gate 			j = TRACE ? echoargs() : TRUE;
4687c478bd9Sstevel@tonic-gate 			if (j) {
4697c478bd9Sstevel@tonic-gate 				/*
4707c478bd9Sstevel@tonic-gate 				 * for xcu4, all invocations of cmdname must
4717c478bd9Sstevel@tonic-gate 				 * return 0, in order for us to return 0.
4727c478bd9Sstevel@tonic-gate 				 * so if we have a non-zero status here,
4737c478bd9Sstevel@tonic-gate 				 * quit immediately.
4747c478bd9Sstevel@tonic-gate 				 */
4757c478bd9Sstevel@tonic-gate 				if ((exitstat |= lcall(cmdname, arglist)) == 0)
4767c478bd9Sstevel@tonic-gate 					continue;
4777c478bd9Sstevel@tonic-gate 			}
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	(void) lseek(0, file_offset, SEEK_SET);
4827c478bd9Sstevel@tonic-gate 	if (OK) {
48343a29105Srobbin 		return (exitstat);
4847c478bd9Sstevel@tonic-gate 	} else {
4857c478bd9Sstevel@tonic-gate 		/*
4867c478bd9Sstevel@tonic-gate 		 * if exitstat was set, to match XCU4 complience,
4877c478bd9Sstevel@tonic-gate 		 * return that value, otherwise, return 1.
4887c478bd9Sstevel@tonic-gate 		 */
48943a29105Srobbin 		return (exitstat ? exitstat : 1);
4907c478bd9Sstevel@tonic-gate 	}
4917c478bd9Sstevel@tonic-gate }
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate static void
4947c478bd9Sstevel@tonic-gate queue(char *buffer, int len, int where)
4957c478bd9Sstevel@tonic-gate {
4967c478bd9Sstevel@tonic-gate 	pio *new, *element;
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if ((new = malloc(sizeof (pio))) == NULL) {
4997c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: Memory allocation failure"));
5007c478bd9Sstevel@tonic-gate 		exit(1);
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	new->cur = new->start = buffer;
5037c478bd9Sstevel@tonic-gate 	new->length = len;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	if (where == TAIL) {
5067c478bd9Sstevel@tonic-gate 		new->next = NULL;
5077c478bd9Sstevel@tonic-gate 		if (queued_data == NULL) {
5087c478bd9Sstevel@tonic-gate 			queued_data = new;
5097c478bd9Sstevel@tonic-gate 		} else {
5107c478bd9Sstevel@tonic-gate 			element = queued_data;
5117c478bd9Sstevel@tonic-gate 			while (element->next != NULL) {
5127c478bd9Sstevel@tonic-gate 				element = element->next;
5137c478bd9Sstevel@tonic-gate 			}
5147c478bd9Sstevel@tonic-gate 			element->next = new;
5157c478bd9Sstevel@tonic-gate 		}
5167c478bd9Sstevel@tonic-gate 	} else {
5177c478bd9Sstevel@tonic-gate 		file_offset -= len;
5187c478bd9Sstevel@tonic-gate 		new->next = queued_data;
5197c478bd9Sstevel@tonic-gate 		queued_data = new;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate static char *
5247c478bd9Sstevel@tonic-gate checklen(char *arg)
5257c478bd9Sstevel@tonic-gate {
5267c478bd9Sstevel@tonic-gate 	int	oklen;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	oklen = TRUE;
5297c478bd9Sstevel@tonic-gate 	linesize += strlen(arg) + 1;
5307c478bd9Sstevel@tonic-gate 	if (linesize >= BUFLIM) {
5317c478bd9Sstevel@tonic-gate 		/*
5327c478bd9Sstevel@tonic-gate 		 * we skip this if there're inserts. we'll handle the
5337c478bd9Sstevel@tonic-gate 		 * argument counting after all the insertions have
5347c478bd9Sstevel@tonic-gate 		 * been done.
5357c478bd9Sstevel@tonic-gate 		 */
5367c478bd9Sstevel@tonic-gate 		if (n_inserts == 0) {
5377c478bd9Sstevel@tonic-gate 			lastarg = arg;
5387c478bd9Sstevel@tonic-gate 			oklen = OK = FALSE;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 			if (LEGAL) {
5417c478bd9Sstevel@tonic-gate 				ERR = TRUE;
5427c478bd9Sstevel@tonic-gate 				ermsg(gettext("arg list too long\n"));
5437c478bd9Sstevel@tonic-gate 			} else if (N_args > 1) {
5447c478bd9Sstevel@tonic-gate 				N_args = 1;
5457c478bd9Sstevel@tonic-gate 			} else {
5467c478bd9Sstevel@tonic-gate 				ermsg(gettext("a single arg was greater than "
5477c478bd9Sstevel@tonic-gate 				    "the max arglist size of %d characters\n"),
5487c478bd9Sstevel@tonic-gate 				    BUFLIM);
5497c478bd9Sstevel@tonic-gate 				ERR = TRUE;
5507c478bd9Sstevel@tonic-gate 			}
5517c478bd9Sstevel@tonic-gate 		}
5527c478bd9Sstevel@tonic-gate 	}
5537c478bd9Sstevel@tonic-gate 	return (oklen ? arg : 0);
5547c478bd9Sstevel@tonic-gate }
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate static char *
5577c478bd9Sstevel@tonic-gate addarg(char *arg)
5587c478bd9Sstevel@tonic-gate {
5597c478bd9Sstevel@tonic-gate 	if (checklen(arg) != 0) {
5607c478bd9Sstevel@tonic-gate 		(void) strcpy(next, arg);
5617c478bd9Sstevel@tonic-gate 		arg = next;
5627c478bd9Sstevel@tonic-gate 		next += strlen(arg) + 1;
5637c478bd9Sstevel@tonic-gate 		return (arg);
5647c478bd9Sstevel@tonic-gate 	}
5657c478bd9Sstevel@tonic-gate 	return ((char *)0);
5667c478bd9Sstevel@tonic-gate }
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate /*
5697c478bd9Sstevel@tonic-gate  * store_wchr() : append a wchar_t to a char buffer, resize buffer if required.
5707c478bd9Sstevel@tonic-gate  *
5717c478bd9Sstevel@tonic-gate  *     Given a pointer to the beginning of a string buffer, the length of the
5727c478bd9Sstevel@tonic-gate  *     buffer and an offset indicating the next place to write within that
5737c478bd9Sstevel@tonic-gate  *     buffer, the passed wchar_t will be appended to the buffer if there is
5747c478bd9Sstevel@tonic-gate  *     enough space. If there is not enough space, an attempt to reallocate the
5757c478bd9Sstevel@tonic-gate  *     buffer will be made and if successful the passed pointer and size will be
5767c478bd9Sstevel@tonic-gate  *     updated to describe the reallocated block. Returns the new value for
5777c478bd9Sstevel@tonic-gate  *     'offset' (it will be incremented by the number of bytes written).
5787c478bd9Sstevel@tonic-gate  */
5797c478bd9Sstevel@tonic-gate static size_t
5807c478bd9Sstevel@tonic-gate store_wchr(char **buffer, size_t *buflen, size_t offset, wchar_t c)
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	int bytes;
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	/*
5857c478bd9Sstevel@tonic-gate 	 * Make sure that there is enough room in the buffer to store the
5867c478bd9Sstevel@tonic-gate 	 * maximum length of c.
5877c478bd9Sstevel@tonic-gate 	 */
5887c478bd9Sstevel@tonic-gate 	if ((offset + MB_CUR_MAX) > *buflen) {
5897c478bd9Sstevel@tonic-gate 		/*
5907c478bd9Sstevel@tonic-gate 		 * Not enough room so attempt to reallocate. Add 'MB_CUR_MAX' to
5917c478bd9Sstevel@tonic-gate 		 * buffer length to ensure that there is always enough room to
5927c478bd9Sstevel@tonic-gate 		 * store 'c' if realloc succeeds, no matter what QBUF_INC is
5937c478bd9Sstevel@tonic-gate 		 * defined as.
5947c478bd9Sstevel@tonic-gate 		 */
5957c478bd9Sstevel@tonic-gate 		*buflen += (QBUF_INC + MB_CUR_MAX);
5967c478bd9Sstevel@tonic-gate 		if ((*buffer = realloc(*buffer, *buflen)) == NULL) {
5977c478bd9Sstevel@tonic-gate 			perror(gettext("xargs: Memory allocation failure"));
5987c478bd9Sstevel@tonic-gate 			exit(1);
5997c478bd9Sstevel@tonic-gate 		}
6007c478bd9Sstevel@tonic-gate 	}
6017c478bd9Sstevel@tonic-gate 	/* store bytes from wchar into buffer */
6027c478bd9Sstevel@tonic-gate 	bytes = wctomb(*buffer + offset, c);
6037c478bd9Sstevel@tonic-gate 	if (bytes == -1) {
6047c478bd9Sstevel@tonic-gate 		/* char was invalid */
6057c478bd9Sstevel@tonic-gate 		bytes = 1;
6067c478bd9Sstevel@tonic-gate 		*(*buffer + offset) = (char)c;
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	/* return new value for offset */
6107c478bd9Sstevel@tonic-gate 	return (offset + bytes);
6117c478bd9Sstevel@tonic-gate }
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate static char *
6147c478bd9Sstevel@tonic-gate getarg()
6157c478bd9Sstevel@tonic-gate {
6167c478bd9Sstevel@tonic-gate 	int	bytes;
6177c478bd9Sstevel@tonic-gate 	wchar_t	c;
6187c478bd9Sstevel@tonic-gate 	char	*arg;
6197c478bd9Sstevel@tonic-gate 	char	*retarg, *requeue_buf;
6207c478bd9Sstevel@tonic-gate 	size_t  requeue_offset = 0, requeue_len;
6217c478bd9Sstevel@tonic-gate 	char	mbc[MB_LEN_MAX];
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	while (iswspace(c = getwchr()) || c == '\n')
6247c478bd9Sstevel@tonic-gate 		;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	if (c == '\0') {
6277c478bd9Sstevel@tonic-gate 		MORE = FALSE;
6287c478bd9Sstevel@tonic-gate 		return (0);
6297c478bd9Sstevel@tonic-gate 	}
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	/*
6327c478bd9Sstevel@tonic-gate 	 * While we are reading in an argument, it is possible that we will
6337c478bd9Sstevel@tonic-gate 	 * reach the maximum length of the overflow buffer and we'll have to
6347c478bd9Sstevel@tonic-gate 	 * requeue what we have read so far. To handle this we allocate an
6357c478bd9Sstevel@tonic-gate 	 * initial buffer here which will keep an unprocessed copy of the data
6367c478bd9Sstevel@tonic-gate 	 * that we read in (this buffer will grow as required).
6377c478bd9Sstevel@tonic-gate 	 */
6387c478bd9Sstevel@tonic-gate 	requeue_len = (size_t)QBUF_STARTLEN;
6397c478bd9Sstevel@tonic-gate 	if ((requeue_buf = (char *)malloc(requeue_len)) == NULL) {
6407c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: Memory allocation failure"));
6417c478bd9Sstevel@tonic-gate 		exit(1);
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 	for (arg = next; ; c = getwchr()) {
6457c478bd9Sstevel@tonic-gate 		bytes = wctomb(mbc, c);
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 		/*
6487c478bd9Sstevel@tonic-gate 		 * Store the char that we have read before processing it in case
6497c478bd9Sstevel@tonic-gate 		 * the current argument needs to be requeued.
6507c478bd9Sstevel@tonic-gate 		 */
6517c478bd9Sstevel@tonic-gate 		requeue_offset = store_wchr(&requeue_buf, &requeue_len,
6527c478bd9Sstevel@tonic-gate 		    requeue_offset, c);
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate 		/* Check for overflow the input buffer */
6557c478bd9Sstevel@tonic-gate 		if ((next + ((bytes == -1) ? 1 : bytes)) >= &argbuf[BUFLIM]) {
6567c478bd9Sstevel@tonic-gate 			/*
6577c478bd9Sstevel@tonic-gate 			 * It's only an error if there are no Args in buffer
6587c478bd9Sstevel@tonic-gate 			 * already.
6597c478bd9Sstevel@tonic-gate 			 */
6607c478bd9Sstevel@tonic-gate 			if ((N_ARGS || PER_LINE) && LEGAL) {
6617c478bd9Sstevel@tonic-gate 				ERR = TRUE;
6627c478bd9Sstevel@tonic-gate 				ermsg(gettext("Argument list too long\n"));
6637c478bd9Sstevel@tonic-gate 				free(requeue_buf);
6647c478bd9Sstevel@tonic-gate 				return (0);
6657c478bd9Sstevel@tonic-gate 			} else if (N_args == 0) {
6667c478bd9Sstevel@tonic-gate 				lastarg = "";
6677c478bd9Sstevel@tonic-gate 				ERR = TRUE;
6687c478bd9Sstevel@tonic-gate 				ermsg(gettext("A single arg was greater than "
6697c478bd9Sstevel@tonic-gate 				    "the max arglist size of %d characters\n"),
6707c478bd9Sstevel@tonic-gate 				    BUFSIZE);
6717c478bd9Sstevel@tonic-gate 				free(requeue_buf);
6727c478bd9Sstevel@tonic-gate 				return (0);
6737c478bd9Sstevel@tonic-gate 			}
6747c478bd9Sstevel@tonic-gate 			/*
6757c478bd9Sstevel@tonic-gate 			 * Otherwise we put back the current argument
6767c478bd9Sstevel@tonic-gate 			 * and use what we have collected so far...
6777c478bd9Sstevel@tonic-gate 			 */
6787c478bd9Sstevel@tonic-gate 			queue(requeue_buf, requeue_offset, HEAD);
6797c478bd9Sstevel@tonic-gate 			/* reset inquote because we have requeued the quotes */
6807c478bd9Sstevel@tonic-gate 			inquote = 0;
6817c478bd9Sstevel@tonic-gate 			return (NULL);
6827c478bd9Sstevel@tonic-gate 		}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 		if (iswctype(c, blank) && inquote == 0) {
6867c478bd9Sstevel@tonic-gate 			if (INSERT) {
6877c478bd9Sstevel@tonic-gate 				if (bytes == -1) {
6887c478bd9Sstevel@tonic-gate 					*next++ = (char)c;
6897c478bd9Sstevel@tonic-gate 				} else {
6907c478bd9Sstevel@tonic-gate 					(void) wctomb(next, c);
6917c478bd9Sstevel@tonic-gate 					next += bytes;
6927c478bd9Sstevel@tonic-gate 				}
6937c478bd9Sstevel@tonic-gate 				continue;
6947c478bd9Sstevel@tonic-gate 			}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 			/* skip over trailing whitespace till next arg */
6977c478bd9Sstevel@tonic-gate 			while (iswctype((c = getwchr()), blank) &&
6987c478bd9Sstevel@tonic-gate 			    (c != '\n') && (c != '\0'))
6997c478bd9Sstevel@tonic-gate 				;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			/*
7027c478bd9Sstevel@tonic-gate 			 * if there was space till end of line then the last
7037c478bd9Sstevel@tonic-gate 			 * character was really a newline...
7047c478bd9Sstevel@tonic-gate 			 */
7057c478bd9Sstevel@tonic-gate 			if (c == L'\n' || c == L'\0') {
7067c478bd9Sstevel@tonic-gate 				ungetwchr(L'\n');
7077c478bd9Sstevel@tonic-gate 			} else {
7087c478bd9Sstevel@tonic-gate 				/* later code needs to know this was a space */
7097c478bd9Sstevel@tonic-gate 				ungetwchr(c);
7107c478bd9Sstevel@tonic-gate 				c = L' ';
7117c478bd9Sstevel@tonic-gate 			}
7127c478bd9Sstevel@tonic-gate 			goto end_arg;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		switch (c) {
7157c478bd9Sstevel@tonic-gate 		case L'\0':
7167c478bd9Sstevel@tonic-gate 		case L'\n':
7177c478bd9Sstevel@tonic-gate 			if (inquote) {
7187c478bd9Sstevel@tonic-gate 				*next++ = '\0';
7197c478bd9Sstevel@tonic-gate 				ermsg(gettext("Missing quote: %s\n"), arg);
7207c478bd9Sstevel@tonic-gate 				ERR = TRUE;
7217c478bd9Sstevel@tonic-gate 				free(requeue_buf);
7227c478bd9Sstevel@tonic-gate 				return (0);
7237c478bd9Sstevel@tonic-gate 			}
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 			N_lines++;
7267c478bd9Sstevel@tonic-gate end_arg:		*next++ = '\0';
7277c478bd9Sstevel@tonic-gate 			/* we finished without requeuing so free requeue_buf */
7287c478bd9Sstevel@tonic-gate 			free(requeue_buf);
7297c478bd9Sstevel@tonic-gate 			if (strcmp(arg, LEOF) == 0 || (c == '\0' &&
7307c478bd9Sstevel@tonic-gate 			    strlen(arg) == 0)) {
7317c478bd9Sstevel@tonic-gate 				MORE = FALSE;
7327c478bd9Sstevel@tonic-gate 				/* absorb the rest of the line */
7337c478bd9Sstevel@tonic-gate 				if ((c != '\n') && (c != '\0'))
7347c478bd9Sstevel@tonic-gate 					while (c = getwchr())
7357c478bd9Sstevel@tonic-gate 						if ((c == '\n') || (c == '\0'))
7367c478bd9Sstevel@tonic-gate 							break;
7377c478bd9Sstevel@tonic-gate 				return (0);
7387c478bd9Sstevel@tonic-gate 			} else {
7397c478bd9Sstevel@tonic-gate 				++N_args;
7407c478bd9Sstevel@tonic-gate 				if (retarg = checklen(arg)) {
7417c478bd9Sstevel@tonic-gate 					if ((PER_LINE &&
7427c478bd9Sstevel@tonic-gate 					    N_lines >= PER_LINE &&
7437c478bd9Sstevel@tonic-gate 					    (c == '\0' || c == '\n')) ||
7447c478bd9Sstevel@tonic-gate 					    (N_ARGS && N_args >= N_ARGS)) {
7457c478bd9Sstevel@tonic-gate 						N_lines = N_args = 0;
7467c478bd9Sstevel@tonic-gate 						lastarg = "";
7477c478bd9Sstevel@tonic-gate 						OK = FALSE;
7487c478bd9Sstevel@tonic-gate 					}
7497c478bd9Sstevel@tonic-gate 				}
7507c478bd9Sstevel@tonic-gate 				return (retarg);
7517c478bd9Sstevel@tonic-gate 			}
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		case '"':
7547c478bd9Sstevel@tonic-gate 			if (inquote == 1)	/* in single quoted string */
7557c478bd9Sstevel@tonic-gate 				goto is_default;
7567c478bd9Sstevel@tonic-gate 			if (inquote == 2)	/* terminating double quote */
7577c478bd9Sstevel@tonic-gate 				inquote = 0;
7587c478bd9Sstevel@tonic-gate 			else			/* starting quoted string */
7597c478bd9Sstevel@tonic-gate 				inquote = 2;
7607c478bd9Sstevel@tonic-gate 			break;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 		case '\'':
7637c478bd9Sstevel@tonic-gate 			if (inquote == 2)	/* in double quoted string */
7647c478bd9Sstevel@tonic-gate 				goto is_default;
7657c478bd9Sstevel@tonic-gate 			if (inquote == 1)	/* terminating single quote */
7667c478bd9Sstevel@tonic-gate 				inquote = 0;
7677c478bd9Sstevel@tonic-gate 			else			/* starting quoted string */
7687c478bd9Sstevel@tonic-gate 				inquote = 1;
7697c478bd9Sstevel@tonic-gate 			break;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		case L'\\':
7727c478bd9Sstevel@tonic-gate 			c = getwchr();
7737c478bd9Sstevel@tonic-gate 			/* store quoted char for potential requeueing */
7747c478bd9Sstevel@tonic-gate 			requeue_offset = store_wchr(&requeue_buf, &requeue_len,
7757c478bd9Sstevel@tonic-gate 			    requeue_offset, c);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 		default:
7787c478bd9Sstevel@tonic-gate is_default:		if (bytes == -1) {
7797c478bd9Sstevel@tonic-gate 				*next++ = (char)c;
7807c478bd9Sstevel@tonic-gate 			} else {
7817c478bd9Sstevel@tonic-gate 				(void) wctomb(next, c);
7827c478bd9Sstevel@tonic-gate 				next += bytes;
7837c478bd9Sstevel@tonic-gate 			}
7847c478bd9Sstevel@tonic-gate 			break;
7857c478bd9Sstevel@tonic-gate 		}
7867c478bd9Sstevel@tonic-gate 	}
7877c478bd9Sstevel@tonic-gate }
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 
7907c478bd9Sstevel@tonic-gate /*
7917c478bd9Sstevel@tonic-gate  * ermsg():	print out an error message, and indicate failure globally.
7927c478bd9Sstevel@tonic-gate  *
7937c478bd9Sstevel@tonic-gate  *	Assumes that message has already been gettext()'d. It would be
7947c478bd9Sstevel@tonic-gate  *	nice if we could just do the gettext() here, but we can't, since
7957c478bd9Sstevel@tonic-gate  *	since xgettext(1M) wouldn't be able to pick up our error message.
7967c478bd9Sstevel@tonic-gate  */
7977c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */
7987c478bd9Sstevel@tonic-gate static void
7997c478bd9Sstevel@tonic-gate ermsg(char *messages, ...)
8007c478bd9Sstevel@tonic-gate {
8017c478bd9Sstevel@tonic-gate 	va_list	ap;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 	va_start(ap, messages);
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "xargs: ");
8067c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, messages, ap);
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	va_end(ap);
8097c478bd9Sstevel@tonic-gate 	OK = FALSE;
8107c478bd9Sstevel@tonic-gate }
8117c478bd9Sstevel@tonic-gate 
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * Function: int rpmatch(char *)
8157c478bd9Sstevel@tonic-gate  *
8167c478bd9Sstevel@tonic-gate  * Description:
8177c478bd9Sstevel@tonic-gate  *
8187c478bd9Sstevel@tonic-gate  *	Internationalized get yes / no answer.
8197c478bd9Sstevel@tonic-gate  *
8207c478bd9Sstevel@tonic-gate  * Inputs:
8217c478bd9Sstevel@tonic-gate  *	s	-> Pointer to answer to compare against.
8227c478bd9Sstevel@tonic-gate  *
8237c478bd9Sstevel@tonic-gate  * Returns:
8247c478bd9Sstevel@tonic-gate  *	TRUE	-> Answer was affirmative
8257c478bd9Sstevel@tonic-gate  *	FALSE	-> Answer was negative
8267c478bd9Sstevel@tonic-gate  */
8277c478bd9Sstevel@tonic-gate 
8287c478bd9Sstevel@tonic-gate static int
8297c478bd9Sstevel@tonic-gate rpmatch(char *s)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	static char	*default_yesexpr = "^[Yy].*";
8327c478bd9Sstevel@tonic-gate 	static char	*compiled_yesexpr = (char *)NULL;
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	/* Execute once to initialize */
8357c478bd9Sstevel@tonic-gate 	if (compiled_yesexpr == (char *)NULL) {
8367c478bd9Sstevel@tonic-gate 		char	*yesexpr;
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 		/* get yes expression according to current locale */
8397c478bd9Sstevel@tonic-gate 		yesexpr = nl_langinfo(YESEXPR);
8407c478bd9Sstevel@tonic-gate 		/*
8417c478bd9Sstevel@tonic-gate 		 * If the was no expression or if there is a compile error
8427c478bd9Sstevel@tonic-gate 		 * use default yes expression.  Anchor
8437c478bd9Sstevel@tonic-gate 		 */
8447c478bd9Sstevel@tonic-gate 		if ((yesexpr == (char *)NULL) || (*yesexpr == (char)NULL) ||
8457c478bd9Sstevel@tonic-gate 		    ((compiled_yesexpr =
8467c478bd9Sstevel@tonic-gate 		    regcmp(yesexpr, 0)) == NULL))
8477c478bd9Sstevel@tonic-gate 			compiled_yesexpr = regcmp(default_yesexpr, 0);
8487c478bd9Sstevel@tonic-gate 	}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	/* match yesexpr */
8517c478bd9Sstevel@tonic-gate 	if (regex(compiled_yesexpr, s) == NULL) {
8527c478bd9Sstevel@tonic-gate 		return (FALSE);
8537c478bd9Sstevel@tonic-gate 	}
8547c478bd9Sstevel@tonic-gate 	return (TRUE);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate static int
8587c478bd9Sstevel@tonic-gate echoargs()
8597c478bd9Sstevel@tonic-gate {
8607c478bd9Sstevel@tonic-gate 	char	**anarg;
8617c478bd9Sstevel@tonic-gate 	char	**tanarg;	/* tmp ptr			*/
8627c478bd9Sstevel@tonic-gate 	int		i;
8637c478bd9Sstevel@tonic-gate 	char		reply[LINE_MAX];
8647c478bd9Sstevel@tonic-gate 
8657c478bd9Sstevel@tonic-gate 	tanarg = anarg = arglist-1;
8667c478bd9Sstevel@tonic-gate 
8677c478bd9Sstevel@tonic-gate 	/*
8687c478bd9Sstevel@tonic-gate 	 * write out each argument, separated by a space. the tanarg
8697c478bd9Sstevel@tonic-gate 	 * nonsense is for xcu4 testsuite compliance - so that an
8707c478bd9Sstevel@tonic-gate 	 * extra space isn't echoed after the last argument.
8717c478bd9Sstevel@tonic-gate 	 */
8727c478bd9Sstevel@tonic-gate 	while (*++anarg) {		/* while there's an argument	*/
8737c478bd9Sstevel@tonic-gate 		++tanarg;		/* follow anarg			*/
8747c478bd9Sstevel@tonic-gate 		(void) write(2, *anarg, strlen(*anarg));
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate 		if (*++tanarg) {	/* if there's another argument:	*/
8777c478bd9Sstevel@tonic-gate 			(void) write(2, " ", 1); /* add a space		*/
8787c478bd9Sstevel@tonic-gate 			--tanarg;	/* reset back to anarg		*/
8797c478bd9Sstevel@tonic-gate 		}
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 	if (PROMPT == -1) {
8827c478bd9Sstevel@tonic-gate 		(void) write(2, "\n", 1);
8837c478bd9Sstevel@tonic-gate 		return (TRUE);
8847c478bd9Sstevel@tonic-gate 	}
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	/*
8877c478bd9Sstevel@tonic-gate 	 * at this point, there may be unexpected input pending on stdin,
8887c478bd9Sstevel@tonic-gate 	 * if one has used the -n flag. this presents a problem, because
8897c478bd9Sstevel@tonic-gate 	 * if we simply do a read(), we'll get the extra input, instead
8907c478bd9Sstevel@tonic-gate 	 * of our desired y/n input. so, we see if there's any extra
8917c478bd9Sstevel@tonic-gate 	 * input, and if there is, then we will store it.
8927c478bd9Sstevel@tonic-gate 	 */
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	saveinput();
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	(void) write(2, "?...", 4);	/* ask the user for input	*/
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) {
8997c478bd9Sstevel@tonic-gate 		if (reply[i] == '\n') {
9007c478bd9Sstevel@tonic-gate 			if (i == 0)
9017c478bd9Sstevel@tonic-gate 				return (FALSE);
9027c478bd9Sstevel@tonic-gate 			break;
9037c478bd9Sstevel@tonic-gate 		}
9047c478bd9Sstevel@tonic-gate 	}
9057c478bd9Sstevel@tonic-gate 	reply[i] = 0;
9067c478bd9Sstevel@tonic-gate 
9077c478bd9Sstevel@tonic-gate 	/* flush remainder of line if necessary */
9087c478bd9Sstevel@tonic-gate 	if (i == LINE_MAX) {
9097c478bd9Sstevel@tonic-gate 		char	bitbucket;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 		while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n'))
9127c478bd9Sstevel@tonic-gate 			;
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	/*
9167c478bd9Sstevel@tonic-gate 	 * now we have to figure out whether the user typed an
9177c478bd9Sstevel@tonic-gate 	 * internationalized version of 'y' for yes. note that in some
9187c478bd9Sstevel@tonic-gate 	 * countries, they've gotten used to typing an ASCII 'y'! so
9197c478bd9Sstevel@tonic-gate 	 * even if our int'l version fails, we will check for an ASCII
9207c478bd9Sstevel@tonic-gate 	 * 'y', in order to be backwards compatible.
9217c478bd9Sstevel@tonic-gate 	 */
9227c478bd9Sstevel@tonic-gate 	return (rpmatch(reply));
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate static char *
9277c478bd9Sstevel@tonic-gate insert(char *pattern, char *subst)
9287c478bd9Sstevel@tonic-gate {
9297c478bd9Sstevel@tonic-gate 	static char	buffer[MAXSBUF+1];
9307c478bd9Sstevel@tonic-gate 	int		len, ipatlen;
9317c478bd9Sstevel@tonic-gate 	char	*pat;
9327c478bd9Sstevel@tonic-gate 	char	*bufend;
9337c478bd9Sstevel@tonic-gate 	char	*pbuf;
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	len = strlen(subst);
9367c478bd9Sstevel@tonic-gate 	ipatlen = strlen(INSPAT) - 1;
9377c478bd9Sstevel@tonic-gate 	pat = pattern - 1;
9387c478bd9Sstevel@tonic-gate 	pbuf = buffer;
9397c478bd9Sstevel@tonic-gate 	bufend = &buffer[MAXSBUF];
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	while (*++pat) {
9427c478bd9Sstevel@tonic-gate 		if (xindex(pat, INSPAT) == 0) {
9437c478bd9Sstevel@tonic-gate 			if (pbuf + len >= bufend) {
9447c478bd9Sstevel@tonic-gate 				break;
9457c478bd9Sstevel@tonic-gate 			} else {
9467c478bd9Sstevel@tonic-gate 				(void) strcpy(pbuf, subst);
9477c478bd9Sstevel@tonic-gate 				pat += ipatlen;
9487c478bd9Sstevel@tonic-gate 				pbuf += len;
9497c478bd9Sstevel@tonic-gate 			}
9507c478bd9Sstevel@tonic-gate 		} else {
9517c478bd9Sstevel@tonic-gate 			*pbuf++ = *pat;
9527c478bd9Sstevel@tonic-gate 			if (pbuf >= bufend)
9537c478bd9Sstevel@tonic-gate 				break;
9547c478bd9Sstevel@tonic-gate 		}
9557c478bd9Sstevel@tonic-gate 	}
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 	if (!*pat) {
9587c478bd9Sstevel@tonic-gate 		*pbuf = '\0';
9597c478bd9Sstevel@tonic-gate 		return (buffer);
9607c478bd9Sstevel@tonic-gate 	} else {
9617c478bd9Sstevel@tonic-gate 		ermsg(gettext("Maximum argument size with insertion via %s's "
9627c478bd9Sstevel@tonic-gate 		    "exceeded\n"), INSPAT);
9637c478bd9Sstevel@tonic-gate 		ERR = TRUE;
9647c478bd9Sstevel@tonic-gate 		return (0);
9657c478bd9Sstevel@tonic-gate 	}
9667c478bd9Sstevel@tonic-gate }
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate static void
9707c478bd9Sstevel@tonic-gate addibuf(struct inserts	*p)
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	char	*newarg, *skel, *sub;
9737c478bd9Sstevel@tonic-gate 	int		l;
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	skel = p->p_skel;
9767c478bd9Sstevel@tonic-gate 	sub = *ARGV;
9777c478bd9Sstevel@tonic-gate 	linesize -= strlen(skel) + 1;
9787c478bd9Sstevel@tonic-gate 	newarg = insert(skel, sub);
9797c478bd9Sstevel@tonic-gate 	if (ERR)
980*788f581bSceastha 		return;
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	if (checklen(newarg)) {
9837c478bd9Sstevel@tonic-gate 		if ((ibufsize += (l = strlen(newarg) + 1)) > MAXIBUF) {
9847c478bd9Sstevel@tonic-gate 			ermsg(gettext("Insert buffer overflow\n"));
9857c478bd9Sstevel@tonic-gate 			ERR = TRUE;
9867c478bd9Sstevel@tonic-gate 		}
9877c478bd9Sstevel@tonic-gate 		(void) strcpy(p_ibuf, newarg);
9887c478bd9Sstevel@tonic-gate 		*(p->p_ARGV) = p_ibuf;
9897c478bd9Sstevel@tonic-gate 		p_ibuf += l;
9907c478bd9Sstevel@tonic-gate 	}
9917c478bd9Sstevel@tonic-gate }
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 
9947c478bd9Sstevel@tonic-gate /*
9957c478bd9Sstevel@tonic-gate  * getchr():	get the next character.
9967c478bd9Sstevel@tonic-gate  * description:
9977c478bd9Sstevel@tonic-gate  *	we get the next character from pio.structure, if there's a character
9987c478bd9Sstevel@tonic-gate  *	to get. this may happen when we've had to flush stdin=/dev/tty,
9997c478bd9Sstevel@tonic-gate  *	but still wanted to preserve the characters for later processing.
10007c478bd9Sstevel@tonic-gate  *
10017c478bd9Sstevel@tonic-gate  *	otherwise we just get the character from stdin.
10027c478bd9Sstevel@tonic-gate  */
10037c478bd9Sstevel@tonic-gate static int
10047c478bd9Sstevel@tonic-gate getchr(void)
10057c478bd9Sstevel@tonic-gate {
10067c478bd9Sstevel@tonic-gate 	char	c;
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate 	do {
10097c478bd9Sstevel@tonic-gate 		if (queued_data == NULL) {
10107c478bd9Sstevel@tonic-gate 			char	*buffer;
10117c478bd9Sstevel@tonic-gate 			int	len;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 			if ((buffer = malloc(BUFSIZE)) == NULL) {
10147c478bd9Sstevel@tonic-gate 				perror(gettext(
10157c478bd9Sstevel@tonic-gate 				    "xargs: Memory allocation failure"));
10167c478bd9Sstevel@tonic-gate 				exit(1);
10177c478bd9Sstevel@tonic-gate 			}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 			if ((len = read(0, buffer, BUFSIZE)) == 0)
10207c478bd9Sstevel@tonic-gate 				return (0);
10217c478bd9Sstevel@tonic-gate 			if (len == -1) {
10227c478bd9Sstevel@tonic-gate 				perror(gettext("xargs: Read failure"));
10237c478bd9Sstevel@tonic-gate 				exit(1);
10247c478bd9Sstevel@tonic-gate 			}
10257c478bd9Sstevel@tonic-gate 
10267c478bd9Sstevel@tonic-gate 			queue(buffer, len, TAIL);
10277c478bd9Sstevel@tonic-gate 		}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 		file_offset++;
10307c478bd9Sstevel@tonic-gate 		c = *queued_data->cur++;	 /* get the next character */
10317c478bd9Sstevel@tonic-gate 		if (--queued_data->length == 0) { /* at the end of buffer? */
10327c478bd9Sstevel@tonic-gate 			pio	*nxt = queued_data->next;
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate 			free(queued_data->start);
10357c478bd9Sstevel@tonic-gate 			free(queued_data);
10367c478bd9Sstevel@tonic-gate 			queued_data = nxt;
10377c478bd9Sstevel@tonic-gate 		}
10387c478bd9Sstevel@tonic-gate 	} while (c == '\0');
10397c478bd9Sstevel@tonic-gate 	return (c);
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate static wchar_t
10447c478bd9Sstevel@tonic-gate getwchr(void)
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	int		i;
10477c478bd9Sstevel@tonic-gate 	wchar_t		wch;
10487c478bd9Sstevel@tonic-gate 	unsigned char	buffer[MB_LEN_MAX + 1];
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	for (i = 0; i < (int)MB_CUR_MAX; ) {
10517c478bd9Sstevel@tonic-gate 		if ((buffer[i++] = getchr()) == NULL) {
10527c478bd9Sstevel@tonic-gate 			/* We have reached  EOF */
10537c478bd9Sstevel@tonic-gate 			if (i == 1) {
10547c478bd9Sstevel@tonic-gate 				/* TRUE EOF has been reached */
10557c478bd9Sstevel@tonic-gate 				return (NULL);
10567c478bd9Sstevel@tonic-gate 			}
10577c478bd9Sstevel@tonic-gate 			/*
10587c478bd9Sstevel@tonic-gate 			 * We have some characters in our buffer still so it
10597c478bd9Sstevel@tonic-gate 			 * must be an invalid character right before EOF.
10607c478bd9Sstevel@tonic-gate 			 */
10617c478bd9Sstevel@tonic-gate 			break;
10627c478bd9Sstevel@tonic-gate 		}
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 		/* If this succeeds then we are done */
10657c478bd9Sstevel@tonic-gate 		if (mbtowc(&wch, (char *)buffer, i) != -1)
10667c478bd9Sstevel@tonic-gate 			return (wch);
10677c478bd9Sstevel@tonic-gate 	}
10687c478bd9Sstevel@tonic-gate 
10697c478bd9Sstevel@tonic-gate 	/*
10707c478bd9Sstevel@tonic-gate 	 * We have now encountered an illegal character sequence.
10717c478bd9Sstevel@tonic-gate 	 * There is nothing much we can do at this point but
10727c478bd9Sstevel@tonic-gate 	 * return an error.  If we attempt to recover we may in fact
10737c478bd9Sstevel@tonic-gate 	 * return garbage as arguments, from the customer's point
10747c478bd9Sstevel@tonic-gate 	 * of view.  After all what if they are feeding us a file
10757c478bd9Sstevel@tonic-gate 	 * generated in another locale?
10767c478bd9Sstevel@tonic-gate 	 */
10777c478bd9Sstevel@tonic-gate 	errno = EILSEQ;
10787c478bd9Sstevel@tonic-gate 	perror(gettext("xargs: Corrupt input file"));
10797c478bd9Sstevel@tonic-gate 	exit(1);
10807c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
10817c478bd9Sstevel@tonic-gate }
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate static void
10857c478bd9Sstevel@tonic-gate ungetwchr(wchar_t wch)
10867c478bd9Sstevel@tonic-gate {
10877c478bd9Sstevel@tonic-gate 	char	*buffer;
10887c478bd9Sstevel@tonic-gate 	int	bytes;
10897c478bd9Sstevel@tonic-gate 
10907c478bd9Sstevel@tonic-gate 	if ((buffer = malloc(MB_LEN_MAX)) == NULL) {
10917c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: Memory allocation failure"));
10927c478bd9Sstevel@tonic-gate 		exit(1);
10937c478bd9Sstevel@tonic-gate 	}
10947c478bd9Sstevel@tonic-gate 	bytes = wctomb(buffer, wch);
10957c478bd9Sstevel@tonic-gate 	queue(buffer, bytes, HEAD);
10967c478bd9Sstevel@tonic-gate }
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate static int
11007c478bd9Sstevel@tonic-gate lcall(char *sub, char **subargs)
11017c478bd9Sstevel@tonic-gate {
11027c478bd9Sstevel@tonic-gate 	int retcode, retry = 0;
11037c478bd9Sstevel@tonic-gate 	pid_t iwait, child;
11047c478bd9Sstevel@tonic-gate 
11057c478bd9Sstevel@tonic-gate 	for (; ; ) {
11067c478bd9Sstevel@tonic-gate 		switch (child = fork()) {
11077c478bd9Sstevel@tonic-gate 		default:
11087c478bd9Sstevel@tonic-gate 			while ((iwait = wait(&retcode)) != child &&
11097c478bd9Sstevel@tonic-gate 			    iwait != (pid_t)-1)
11107c478bd9Sstevel@tonic-gate 				;
11117c478bd9Sstevel@tonic-gate 			if (iwait == (pid_t)-1) {
11127c478bd9Sstevel@tonic-gate 				perror(gettext("xargs: Wait failure"));
11137c478bd9Sstevel@tonic-gate 				exit(122);
11147c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
11157c478bd9Sstevel@tonic-gate 			}
11167c478bd9Sstevel@tonic-gate 			if (WIFSIGNALED(retcode)) {
11177c478bd9Sstevel@tonic-gate 				ermsg(gettext("Child killed with signal %d\n"),
11187c478bd9Sstevel@tonic-gate 				    WTERMSIG(retcode));
11197c478bd9Sstevel@tonic-gate 				exit(125);
11207c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
11217c478bd9Sstevel@tonic-gate 			}
11227c478bd9Sstevel@tonic-gate 			if ((WEXITSTATUS(retcode) & 0377) == 0377) {
11237c478bd9Sstevel@tonic-gate 				ermsg(gettext("Command could not continue "
11247c478bd9Sstevel@tonic-gate 				    "processing data\n"));
11257c478bd9Sstevel@tonic-gate 				exit(124);
11267c478bd9Sstevel@tonic-gate 				/* NOTREACHED */
11277c478bd9Sstevel@tonic-gate 			}
11287c478bd9Sstevel@tonic-gate 			return (WEXITSTATUS(retcode));
11297c478bd9Sstevel@tonic-gate 		case 0:
11307c478bd9Sstevel@tonic-gate 			(void) execvp(sub, subargs);
11317c478bd9Sstevel@tonic-gate 			perror(gettext("xargs: Could not exec command"));
11327c478bd9Sstevel@tonic-gate 			if (errno == EACCES)
11337c478bd9Sstevel@tonic-gate 				exit(126);
11347c478bd9Sstevel@tonic-gate 			exit(127);
11357c478bd9Sstevel@tonic-gate 			/* NOTREACHED */
11367c478bd9Sstevel@tonic-gate 		case -1:
11377c478bd9Sstevel@tonic-gate 			if (errno != EAGAIN && retry++ < FORK_RETRY) {
11387c478bd9Sstevel@tonic-gate 				perror(gettext("xargs: Could not fork child"));
11397c478bd9Sstevel@tonic-gate 				exit(123);
11407c478bd9Sstevel@tonic-gate 			}
11417c478bd9Sstevel@tonic-gate 			(void) sleep(1);
11427c478bd9Sstevel@tonic-gate 		}
11437c478bd9Sstevel@tonic-gate 	}
11447c478bd9Sstevel@tonic-gate }
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate /*
11487c478bd9Sstevel@tonic-gate  * If `s2' is a substring of `s1' return the offset of the first
11497c478bd9Sstevel@tonic-gate  * occurrence of `s2' in `s1', else return -1.
11507c478bd9Sstevel@tonic-gate  */
11517c478bd9Sstevel@tonic-gate static int
11527c478bd9Sstevel@tonic-gate xindex(char *as1, char *as2)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 	char	*s1, *s2, c;
11557c478bd9Sstevel@tonic-gate 	int		offset;
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 	s1 = as1;
11587c478bd9Sstevel@tonic-gate 	s2 = as2;
11597c478bd9Sstevel@tonic-gate 	c = *s2;
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate 	while (*s1) {
11627c478bd9Sstevel@tonic-gate 		if (*s1++ == c) {
11637c478bd9Sstevel@tonic-gate 			offset = s1 - as1 - 1;
11647c478bd9Sstevel@tonic-gate 			s2++;
11657c478bd9Sstevel@tonic-gate 			while ((c = *s2++) == *s1++ && c)
11667c478bd9Sstevel@tonic-gate 				;
11677c478bd9Sstevel@tonic-gate 			if (c == 0)
11687c478bd9Sstevel@tonic-gate 				return (offset);
11697c478bd9Sstevel@tonic-gate 			s1 = offset + as1 + 1;
11707c478bd9Sstevel@tonic-gate 			s2 = as2;
11717c478bd9Sstevel@tonic-gate 			c = *s2;
11727c478bd9Sstevel@tonic-gate 		}
11737c478bd9Sstevel@tonic-gate 	}
11747c478bd9Sstevel@tonic-gate 	return (-1);
11757c478bd9Sstevel@tonic-gate }
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate 
11787c478bd9Sstevel@tonic-gate static void
11797c478bd9Sstevel@tonic-gate usage()
11807c478bd9Sstevel@tonic-gate {
11817c478bd9Sstevel@tonic-gate 	ermsg(gettext(USAGEMSG));
11827c478bd9Sstevel@tonic-gate 	OK = FALSE;
11837c478bd9Sstevel@tonic-gate }
11847c478bd9Sstevel@tonic-gate 
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 
11877c478bd9Sstevel@tonic-gate /*
11887c478bd9Sstevel@tonic-gate  * parseargs():		modify the args
11897c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
11907c478bd9Sstevel@tonic-gate  *	and getopts(3C) is clueless about this nonsense, we change the
11917c478bd9Sstevel@tonic-gate  *	our local argument count and strings to separate this out,
11927c478bd9Sstevel@tonic-gate  *	and make it easier to handle via getopts(3c).
11937c478bd9Sstevel@tonic-gate  *
11947c478bd9Sstevel@tonic-gate  *	-e	-> "-e ""
11957c478bd9Sstevel@tonic-gate  *	-e3	-> "-e "3"
11967c478bd9Sstevel@tonic-gate  *	-Estr	-> "-E "str"
11977c478bd9Sstevel@tonic-gate  *	-i	-> "-i "{}"
11987c478bd9Sstevel@tonic-gate  *	-irep	-> "-i "rep"
11997c478bd9Sstevel@tonic-gate  *	-l	-> "-i "1"
12007c478bd9Sstevel@tonic-gate  *	-l10	-> "-i "10"
12017c478bd9Sstevel@tonic-gate  *
12027c478bd9Sstevel@tonic-gate  *	since the -e, -i and -l flags all take optional subarguments,
12037c478bd9Sstevel@tonic-gate  */
12047c478bd9Sstevel@tonic-gate static void
12057c478bd9Sstevel@tonic-gate parseargs(int ac, char **av)
12067c478bd9Sstevel@tonic-gate {
12077c478bd9Sstevel@tonic-gate 	int i;			/* current argument			*/
12087c478bd9Sstevel@tonic-gate 	int cflag;		/* 0 = not processing cmd arg		*/
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) {
12117c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: Memory allocation failure"));
12127c478bd9Sstevel@tonic-gate 		exit(1);
12137c478bd9Sstevel@tonic-gate 	}
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/* for each argument, see if we need to change things:		*/
12167c478bd9Sstevel@tonic-gate 	for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) {
12177c478bd9Sstevel@tonic-gate 		if ((mav[mac] = strdup(av[i])) == NULL) {
12187c478bd9Sstevel@tonic-gate 			perror(gettext("xargs: Memory allocation failure"));
12197c478bd9Sstevel@tonic-gate 			exit(1);
12207c478bd9Sstevel@tonic-gate 		}
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 		/* -- has been found or argument list is fully processes */
12237c478bd9Sstevel@tonic-gate 		if (cflag)
12247c478bd9Sstevel@tonic-gate 			continue;
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 		/*
12277c478bd9Sstevel@tonic-gate 		 * if we're doing special processing, and we've got a flag
12287c478bd9Sstevel@tonic-gate 		 */
12297c478bd9Sstevel@tonic-gate 		else if ((av[i][0] == '-') && (av[i][1] != NULL)) {
12307c478bd9Sstevel@tonic-gate 			char	*def;
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 			switch (av[i][1]) {
12337c478bd9Sstevel@tonic-gate 			case	'e':
12347c478bd9Sstevel@tonic-gate 				def = ""; /* -e with no arg turns off eof */
12357c478bd9Sstevel@tonic-gate 				goto process_special;
12367c478bd9Sstevel@tonic-gate 			case	'i':
12377c478bd9Sstevel@tonic-gate 				def = INSPAT_STR;
12387c478bd9Sstevel@tonic-gate 				goto process_special;
12397c478bd9Sstevel@tonic-gate 			case	'l':
12407c478bd9Sstevel@tonic-gate 				def = "1";
12417c478bd9Sstevel@tonic-gate process_special:
12427c478bd9Sstevel@tonic-gate 				/*
12437c478bd9Sstevel@tonic-gate 				 * if there's no sub-option, we *must* add
12447c478bd9Sstevel@tonic-gate 				 * a default one. this is because xargs must
12457c478bd9Sstevel@tonic-gate 				 * be able to distinguish between a valid
12467c478bd9Sstevel@tonic-gate 				 * suboption, and a command name.
12477c478bd9Sstevel@tonic-gate 				 */
12487c478bd9Sstevel@tonic-gate 				if (av[i][2] == NULL) {
12497c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(def);
12507c478bd9Sstevel@tonic-gate 				} else {
12517c478bd9Sstevel@tonic-gate 					/* clear out our version: */
12527c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
12537c478bd9Sstevel@tonic-gate 					mav[++mac] = strdup(&av[i][2]);
12547c478bd9Sstevel@tonic-gate 				}
12557c478bd9Sstevel@tonic-gate 				if (mav[mac] == NULL) {
12567c478bd9Sstevel@tonic-gate 					perror(gettext("xargs: Memory"
12577c478bd9Sstevel@tonic-gate 					    " allocation failure"));
12587c478bd9Sstevel@tonic-gate 					exit(1);
12597c478bd9Sstevel@tonic-gate 				}
12607c478bd9Sstevel@tonic-gate 				break;
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate 			/* flags with required subarguments:		*/
12637c478bd9Sstevel@tonic-gate 
12647c478bd9Sstevel@tonic-gate 			/*
12657c478bd9Sstevel@tonic-gate 			 * there are two separate cases here. either the
12667c478bd9Sstevel@tonic-gate 			 * flag can have the normal XCU4 handling
12677c478bd9Sstevel@tonic-gate 			 * (of the form: -X subargument); or it can have
12687c478bd9Sstevel@tonic-gate 			 * the old solaris 2.[0-4] handling (of the
12697c478bd9Sstevel@tonic-gate 			 * form: -Xsubargument). in order to maintain
12707c478bd9Sstevel@tonic-gate 			 * backwards compatibility, we must support the
12717c478bd9Sstevel@tonic-gate 			 * latter case. we handle the latter possibility
12727c478bd9Sstevel@tonic-gate 			 * first so both the old solaris way of handling
12737c478bd9Sstevel@tonic-gate 			 * and the new XCU4 way of handling things are allowed.
12747c478bd9Sstevel@tonic-gate 			 */
12757c478bd9Sstevel@tonic-gate 			case	'n':	/* FALLTHROUGH			*/
12767c478bd9Sstevel@tonic-gate 			case	's':	/* FALLTHROUGH			*/
12777c478bd9Sstevel@tonic-gate 			case	'E':	/* FALLTHROUGH			*/
12787c478bd9Sstevel@tonic-gate 			case	'I':	/* FALLTHROUGH			*/
12797c478bd9Sstevel@tonic-gate 			case	'L':
12807c478bd9Sstevel@tonic-gate 				/*
12817c478bd9Sstevel@tonic-gate 				 * if the second character isn't null, then
12827c478bd9Sstevel@tonic-gate 				 * the user has specified the old syntax.
12837c478bd9Sstevel@tonic-gate 				 * we move the subargument into our
12847c478bd9Sstevel@tonic-gate 				 * mod'd argument list.
12857c478bd9Sstevel@tonic-gate 				 */
12867c478bd9Sstevel@tonic-gate 				if (av[i][2] != NULL) {
12877c478bd9Sstevel@tonic-gate 					/* first clean things up:	*/
12887c478bd9Sstevel@tonic-gate 					mav[mac][2] = NULL;
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate 					/* now add the separation:	*/
12917c478bd9Sstevel@tonic-gate 					++mac;	/* inc to next mod'd arg */
12927c478bd9Sstevel@tonic-gate 					if ((mav[mac] = strdup(&av[i][2])) ==
12937c478bd9Sstevel@tonic-gate 					    NULL) {
12947c478bd9Sstevel@tonic-gate 						perror(gettext("xargs: Memory"
12957c478bd9Sstevel@tonic-gate 						    " allocation failure"));
12967c478bd9Sstevel@tonic-gate 						exit(1);
12977c478bd9Sstevel@tonic-gate 					}
12987c478bd9Sstevel@tonic-gate 					break;
12997c478bd9Sstevel@tonic-gate 				}
13007c478bd9Sstevel@tonic-gate 				i++;
13017c478bd9Sstevel@tonic-gate 				mac++;
13027c478bd9Sstevel@tonic-gate #ifdef XPG6
13037c478bd9Sstevel@tonic-gate 				if (av[i] != NULL) {
13047c478bd9Sstevel@tonic-gate 					if ((mav[mac] = strdup(av[i]))
1305*788f581bSceastha 					    == NULL) {
13067c478bd9Sstevel@tonic-gate 						perror(gettext("xargs: Memory"
13077c478bd9Sstevel@tonic-gate 						    " allocation failure"));
13087c478bd9Sstevel@tonic-gate 						exit(1);
13097c478bd9Sstevel@tonic-gate 					}
13107c478bd9Sstevel@tonic-gate 				}
13117c478bd9Sstevel@tonic-gate #else
13127c478bd9Sstevel@tonic-gate 				if (av[i] == NULL) {
13137c478bd9Sstevel@tonic-gate 					if ((mav[mac++] = strdup("")) == NULL) {
13147c478bd9Sstevel@tonic-gate 						perror(gettext("xargs: Memory "
13157c478bd9Sstevel@tonic-gate 						    " allocation failure"));
13167c478bd9Sstevel@tonic-gate 						exit(1);
13177c478bd9Sstevel@tonic-gate 					}
13187c478bd9Sstevel@tonic-gate 					mav[mac] = NULL;
13197c478bd9Sstevel@tonic-gate 					return;
13207c478bd9Sstevel@tonic-gate 				}
13217c478bd9Sstevel@tonic-gate 				if ((mav[mac] = strdup(av[i])) == NULL) {
13227c478bd9Sstevel@tonic-gate 					perror(gettext("xargs: Memory"
1323*788f581bSceastha 					    " allocation failure"));
13247c478bd9Sstevel@tonic-gate 					exit(1);
13257c478bd9Sstevel@tonic-gate 				}
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate #endif
13287c478bd9Sstevel@tonic-gate 				break;
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate 			/* flags */
13317c478bd9Sstevel@tonic-gate 			case 'p' :
13327c478bd9Sstevel@tonic-gate 			case 't' :
13337c478bd9Sstevel@tonic-gate 			case 'x' :
13347c478bd9Sstevel@tonic-gate 				break;
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate 			case '-' :
13377c478bd9Sstevel@tonic-gate 			default:
13387c478bd9Sstevel@tonic-gate 				/*
13397c478bd9Sstevel@tonic-gate 				 * here we've hit the cmd argument. so
13407c478bd9Sstevel@tonic-gate 				 * we'll stop special processing, as the
13417c478bd9Sstevel@tonic-gate 				 * cmd may have a "-i" etc., argument,
13427c478bd9Sstevel@tonic-gate 				 * and we don't want to add a "" to it.
13437c478bd9Sstevel@tonic-gate 				 */
13447c478bd9Sstevel@tonic-gate 				cflag = 1;
13457c478bd9Sstevel@tonic-gate 				break;
13467c478bd9Sstevel@tonic-gate 			}
13477c478bd9Sstevel@tonic-gate 		} else if (i > 0) {	/* if we're not the 1st arg	*/
13487c478bd9Sstevel@tonic-gate 			/*
13497c478bd9Sstevel@tonic-gate 			 * if it's not a flag, then it *must* be the cmd.
13507c478bd9Sstevel@tonic-gate 			 * set cflag, so we don't mishandle the -[eil] flags.
13517c478bd9Sstevel@tonic-gate 			 */
13527c478bd9Sstevel@tonic-gate 			cflag = 1;
13537c478bd9Sstevel@tonic-gate 		}
13547c478bd9Sstevel@tonic-gate 	}
13557c478bd9Sstevel@tonic-gate 
13567c478bd9Sstevel@tonic-gate 	mav[mac] = NULL;
13577c478bd9Sstevel@tonic-gate }
13587c478bd9Sstevel@tonic-gate 
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * saveinput(): pick up any pending input, so it can be processed later.
13627c478bd9Sstevel@tonic-gate  *
13637c478bd9Sstevel@tonic-gate  * description:
13647c478bd9Sstevel@tonic-gate  *	the purpose of this routine is to allow us to handle the user
13657c478bd9Sstevel@tonic-gate  *	typing in a 'y' or 'n', when there's existing characters already
13667c478bd9Sstevel@tonic-gate  *	in stdin. this happens when one gives the "-n" option along with
13677c478bd9Sstevel@tonic-gate  *	"-p". the problem occurs when the user first types in more arguments
13687c478bd9Sstevel@tonic-gate  *	than specified by the -n number. echoargs() wants to read stdin
13697c478bd9Sstevel@tonic-gate  *	in order to get the user's response, but if there's already stuff
13707c478bd9Sstevel@tonic-gate  *	there, echoargs() won't read the proper character.
13717c478bd9Sstevel@tonic-gate  *
13727c478bd9Sstevel@tonic-gate  *	the solution provided by this routine is to pick up all characters
13737c478bd9Sstevel@tonic-gate  *	(if any), and store them for later processing.
13747c478bd9Sstevel@tonic-gate  */
13757c478bd9Sstevel@tonic-gate 
13767c478bd9Sstevel@tonic-gate void
13777c478bd9Sstevel@tonic-gate saveinput()
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	char *buffer;		/* ptr to the floating data buffer	*/
13807c478bd9Sstevel@tonic-gate 	struct strpeek speek;	/* to see what's on the queue		*/
13817c478bd9Sstevel@tonic-gate 	struct strpeek *ps;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	/* if we're not in -p mode, skip				*/
13847c478bd9Sstevel@tonic-gate 	if (PROMPT == -1) {
13857c478bd9Sstevel@tonic-gate 		return;
13867c478bd9Sstevel@tonic-gate 	}
13877c478bd9Sstevel@tonic-gate 
13887c478bd9Sstevel@tonic-gate 
13897c478bd9Sstevel@tonic-gate 	/* now see if there's any activity pending:			*/
13907c478bd9Sstevel@tonic-gate 	ps = &speek;
13917c478bd9Sstevel@tonic-gate 	ps->ctlbuf.maxlen = 0;
13927c478bd9Sstevel@tonic-gate 	ps->ctlbuf.len = 0;
13937c478bd9Sstevel@tonic-gate 	ps->ctlbuf.buf = NULL;
13947c478bd9Sstevel@tonic-gate 	ps->flags = 0;
13957c478bd9Sstevel@tonic-gate 	ps->databuf.maxlen = MAX_INPUT;
13967c478bd9Sstevel@tonic-gate 	ps->databuf.len = 0;
13977c478bd9Sstevel@tonic-gate 	if ((buffer = malloc((size_t)MAX_INPUT)) == NULL) {
13987c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: Memory allocation failure"));
13997c478bd9Sstevel@tonic-gate 		exit(1);
14007c478bd9Sstevel@tonic-gate 	}
14017c478bd9Sstevel@tonic-gate 	ps->databuf.buf = (char *)buffer;
14027c478bd9Sstevel@tonic-gate 
14037c478bd9Sstevel@tonic-gate 	if (ioctl(PROMPT, I_PEEK, ps) == -1) {
14047c478bd9Sstevel@tonic-gate 		perror(gettext("xargs: I_PEEK failure"));
14057c478bd9Sstevel@tonic-gate 		exit(1);
14067c478bd9Sstevel@tonic-gate 	}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	if (ps->databuf.len > 0) {
14097c478bd9Sstevel@tonic-gate 		int	len;
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 		if ((len = read(PROMPT, buffer, ps->databuf.len)) == -1) {
14127c478bd9Sstevel@tonic-gate 			perror(gettext("xargs: read failure"));
14137c478bd9Sstevel@tonic-gate 			exit(1);
14147c478bd9Sstevel@tonic-gate 		}
14157c478bd9Sstevel@tonic-gate 		queue(buffer, len, TAIL);
14167c478bd9Sstevel@tonic-gate 	}
14177c478bd9Sstevel@tonic-gate }
1418