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