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