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