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 */ 2103fc8686SGarrett D'Amore /* 2203fc8686SGarrett D'Amore * Copyright 2012 DEY Storage Systems, Inc. All rights reserved. 2303fc8686SGarrett D'Amore * 2403fc8686SGarrett D'Amore * Portions of this file developed by DEY Storage Systems, Inc. are licensed 2503fc8686SGarrett D'Amore * under the terms of the Common Development and Distribution License (CDDL) 2603fc8686SGarrett D'Amore * version 1.0 only. The use of subsequent versions of the License are 2703fc8686SGarrett D'Amore * is specifically prohibited unless those terms are not in conflict with 2803fc8686SGarrett D'Amore * version 1.0 of the License. You can find this license on-line at 2903fc8686SGarrett D'Amore * http://www.illumos.org/license/CDDL 3003fc8686SGarrett D'Amore */ 317c478bd9Sstevel@tonic-gate /* 32d2afb7a9Scf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 337c478bd9Sstevel@tonic-gate * Use is subject to license terms. 347c478bd9Sstevel@tonic-gate */ 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 377c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include <stdio.h> 417c478bd9Sstevel@tonic-gate #include <sys/types.h> 427c478bd9Sstevel@tonic-gate #include <sys/wait.h> 437c478bd9Sstevel@tonic-gate #include <unistd.h> 447c478bd9Sstevel@tonic-gate #include <fcntl.h> 457c478bd9Sstevel@tonic-gate #include <string.h> 467c478bd9Sstevel@tonic-gate #include <stdarg.h> 477c478bd9Sstevel@tonic-gate #include <stdlib.h> 487c478bd9Sstevel@tonic-gate #include <limits.h> 497c478bd9Sstevel@tonic-gate #include <wchar.h> 507c478bd9Sstevel@tonic-gate #include <locale.h> 517c478bd9Sstevel@tonic-gate #include <langinfo.h> 527c478bd9Sstevel@tonic-gate #include <stropts.h> 537c478bd9Sstevel@tonic-gate #include <poll.h> 547c478bd9Sstevel@tonic-gate #include <errno.h> 557c478bd9Sstevel@tonic-gate #include <stdarg.h> 563d63ea05Sas #include "getresponse.h" 577c478bd9Sstevel@tonic-gate 587c478bd9Sstevel@tonic-gate #define HEAD 0 597c478bd9Sstevel@tonic-gate #define TAIL 1 607c478bd9Sstevel@tonic-gate #define FALSE 0 617c478bd9Sstevel@tonic-gate #define TRUE 1 627c478bd9Sstevel@tonic-gate #define MAXSBUF 255 637c478bd9Sstevel@tonic-gate #define MAXIBUF 512 647c478bd9Sstevel@tonic-gate #define MAXINSERTS 5 657c478bd9Sstevel@tonic-gate #define BUFSIZE LINE_MAX 667c478bd9Sstevel@tonic-gate #define MAXARGS 255 677c478bd9Sstevel@tonic-gate #define INSPAT_STR "{}" /* default replstr string for -[Ii] */ 687c478bd9Sstevel@tonic-gate #define FORK_RETRY 5 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #define QBUF_STARTLEN 255 /* start size of growable string buffer */ 717c478bd9Sstevel@tonic-gate #define QBUF_INC 100 /* how much to grow a growable string by */ 727c478bd9Sstevel@tonic-gate 7303fc8686SGarrett D'Amore /* We use these macros to help make formatting look "consistent" */ 7403fc8686SGarrett D'Amore #define EMSG(s) ermsg(gettext(s "\n")) 7503fc8686SGarrett D'Amore #define EMSG2(s, a) ermsg(gettext(s "\n"), a) 7603fc8686SGarrett D'Amore #define PERR(s) perror(gettext("xargs: " s)) 7703fc8686SGarrett D'Amore 7803fc8686SGarrett D'Amore /* Some common error messages */ 7903fc8686SGarrett D'Amore 8003fc8686SGarrett D'Amore #define LIST2LONG "Argument list too long" 8103fc8686SGarrett D'Amore #define ARG2LONG "A single argument was greater than %d bytes" 8203fc8686SGarrett D'Amore #define MALLOCFAIL "Memory allocation failure" 8303fc8686SGarrett D'Amore #define CORRUPTFILE "Corrupt input file" 8403fc8686SGarrett D'Amore #define WAITFAIL "Wait failure" 8503fc8686SGarrett D'Amore #define CHILDSIG "Child killed with signal %d" 8603fc8686SGarrett D'Amore #define CHILDFAIL "Command could not continue processing data" 8703fc8686SGarrett D'Amore #define FORKFAIL "Could not fork child" 8803fc8686SGarrett D'Amore #define EXECFAIL "Could not exec command" 8903fc8686SGarrett D'Amore #define MISSQUOTE "Missing quote" 9003fc8686SGarrett D'Amore #define BADESCAPE "Incomplete escape" 9103fc8686SGarrett D'Amore #define IBUFOVERFLOW "Insert buffer overflow" 9203fc8686SGarrett D'Amore 9303fc8686SGarrett D'Amore #define _(x) gettext(x) 9403fc8686SGarrett D'Amore 957c478bd9Sstevel@tonic-gate static wctype_t blank; 967c478bd9Sstevel@tonic-gate static char *arglist[MAXARGS+1]; 9703fc8686SGarrett D'Amore static char argbuf[BUFSIZE * 2 + 1]; 9803fc8686SGarrett D'Amore static char lastarg[BUFSIZE + 1]; 997c478bd9Sstevel@tonic-gate static char **ARGV = arglist; 1007c478bd9Sstevel@tonic-gate static char *LEOF = "_"; 1017c478bd9Sstevel@tonic-gate static char *INSPAT = INSPAT_STR; 1027c478bd9Sstevel@tonic-gate static char ins_buf[MAXIBUF]; 1037c478bd9Sstevel@tonic-gate static char *p_ibuf; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate static struct inserts { 1067c478bd9Sstevel@tonic-gate char **p_ARGV; /* where to put newarg ptr in arg list */ 1077c478bd9Sstevel@tonic-gate char *p_skel; /* ptr to arg template */ 1087c478bd9Sstevel@tonic-gate } saveargv[MAXINSERTS]; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static int PROMPT = -1; 1117c478bd9Sstevel@tonic-gate static int BUFLIM = BUFSIZE; 1127c478bd9Sstevel@tonic-gate static int N_ARGS = 0; 1137c478bd9Sstevel@tonic-gate static int N_args = 0; 1147c478bd9Sstevel@tonic-gate static int N_lines = 0; 1157c478bd9Sstevel@tonic-gate static int DASHX = FALSE; 1167c478bd9Sstevel@tonic-gate static int MORE = TRUE; 1177c478bd9Sstevel@tonic-gate static int PER_LINE = FALSE; 1187c478bd9Sstevel@tonic-gate static int ERR = FALSE; 1197c478bd9Sstevel@tonic-gate static int OK = TRUE; 1207c478bd9Sstevel@tonic-gate static int LEGAL = FALSE; 1217c478bd9Sstevel@tonic-gate static int TRACE = FALSE; 1227c478bd9Sstevel@tonic-gate static int INSERT = FALSE; 12303fc8686SGarrett D'Amore static int ZERO = FALSE; 1247c478bd9Sstevel@tonic-gate static int linesize = 0; 1257c478bd9Sstevel@tonic-gate static int ibufsize = 0; 1267c478bd9Sstevel@tonic-gate static int exitstat = 0; /* our exit status */ 1277c478bd9Sstevel@tonic-gate static int mac; /* modified argc, after parsing */ 1287c478bd9Sstevel@tonic-gate static char **mav; /* modified argv, after parsing */ 1297c478bd9Sstevel@tonic-gate static int n_inserts; /* # of insertions. */ 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* our usage message: */ 132*1daace1dSGarrett D'Amore #define USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\ 1337c478bd9Sstevel@tonic-gate "[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\ 1347c478bd9Sstevel@tonic-gate "[cmd [args ...]]\n" 1357c478bd9Sstevel@tonic-gate 1367c478bd9Sstevel@tonic-gate static int echoargs(); 13703fc8686SGarrett D'Amore static wint_t getwchr(char *, size_t *); 1387c478bd9Sstevel@tonic-gate static int lcall(char *sub, char **subargs); 1397c478bd9Sstevel@tonic-gate static void addibuf(struct inserts *p); 1407c478bd9Sstevel@tonic-gate static void ermsg(char *messages, ...); 1417c478bd9Sstevel@tonic-gate static char *addarg(char *arg); 14203fc8686SGarrett D'Amore static void store_str(char **, char *, size_t); 14303fc8686SGarrett D'Amore static char *getarg(char *); 1447c478bd9Sstevel@tonic-gate static char *insert(char *pattern, char *subst); 1457c478bd9Sstevel@tonic-gate static void usage(); 1467c478bd9Sstevel@tonic-gate static void parseargs(); 1477c478bd9Sstevel@tonic-gate 14843a29105Srobbin int 1497c478bd9Sstevel@tonic-gate main(int argc, char **argv) 1507c478bd9Sstevel@tonic-gate { 1517c478bd9Sstevel@tonic-gate int j; 1527c478bd9Sstevel@tonic-gate struct inserts *psave; 1537c478bd9Sstevel@tonic-gate int c; 1547c478bd9Sstevel@tonic-gate int initsize; 15503fc8686SGarrett D'Amore char *cmdname, **initlist; 15603fc8686SGarrett D'Amore char *arg; 15703fc8686SGarrett D'Amore char *next; 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate /* initialization */ 1607c478bd9Sstevel@tonic-gate blank = wctype("blank"); 1617c478bd9Sstevel@tonic-gate n_inserts = 0; 1627c478bd9Sstevel@tonic-gate psave = saveargv; 1637c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, ""); 1647c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */ 1657c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */ 1667c478bd9Sstevel@tonic-gate #endif 1677c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN); 1683d63ea05Sas if (init_yes() < 0) { 16903fc8686SGarrett D'Amore ermsg(_(ERR_MSG_INIT_YES), strerror(errno)); 1707c478bd9Sstevel@tonic-gate exit(1); 1717c478bd9Sstevel@tonic-gate } 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate parseargs(argc, argv); 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* handling all of xargs arguments: */ 17603fc8686SGarrett D'Amore while ((c = getopt(mac, mav, "0tpe:E:I:i:L:l:n:s:x")) != EOF) { 1777c478bd9Sstevel@tonic-gate switch (c) { 17803fc8686SGarrett D'Amore case '0': 17903fc8686SGarrett D'Amore ZERO = TRUE; 18003fc8686SGarrett D'Amore break; 18103fc8686SGarrett D'Amore 1827c478bd9Sstevel@tonic-gate case 't': /* -t: turn trace mode on */ 1837c478bd9Sstevel@tonic-gate TRACE = TRUE; 1847c478bd9Sstevel@tonic-gate break; 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate case 'p': /* -p: turn on prompt mode. */ 1877c478bd9Sstevel@tonic-gate if ((PROMPT = open("/dev/tty", O_RDONLY)) == -1) { 18803fc8686SGarrett D'Amore PERR("can't read from tty for -p"); 1897c478bd9Sstevel@tonic-gate } else { 1907c478bd9Sstevel@tonic-gate TRACE = TRUE; 1917c478bd9Sstevel@tonic-gate } 1927c478bd9Sstevel@tonic-gate break; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate case 'e': 1957c478bd9Sstevel@tonic-gate /* 1967c478bd9Sstevel@tonic-gate * -e[eofstr]: set/disable end-of-file. 1977c478bd9Sstevel@tonic-gate * N.B. that an argument *isn't* required here; but 1987c478bd9Sstevel@tonic-gate * parseargs forced an argument if not was given. The 1997c478bd9Sstevel@tonic-gate * forced argument is the default... 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate LEOF = optarg; /* can be empty */ 2027c478bd9Sstevel@tonic-gate break; 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate case 'E': 2057c478bd9Sstevel@tonic-gate /* 2067c478bd9Sstevel@tonic-gate * -E eofstr: change end-of-file string. 207952d685eScf * eofstr *is* required here, but can be empty: 2087c478bd9Sstevel@tonic-gate */ 2097c478bd9Sstevel@tonic-gate LEOF = optarg; 2107c478bd9Sstevel@tonic-gate break; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate case 'I': 2137c478bd9Sstevel@tonic-gate /* -I replstr: Insert mode. replstr *is* required. */ 2147c478bd9Sstevel@tonic-gate INSERT = PER_LINE = LEGAL = TRUE; 2157c478bd9Sstevel@tonic-gate N_ARGS = 0; 216952d685eScf INSPAT = optarg; 217952d685eScf if (*optarg == '\0') { 21803fc8686SGarrett D'Amore ermsg(_("Option requires an argument: -%c\n"), 21903fc8686SGarrett D'Amore c); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate break; 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate case 'i': 2247c478bd9Sstevel@tonic-gate /* 2257c478bd9Sstevel@tonic-gate * -i [replstr]: insert mode, with *optional* replstr. 2267c478bd9Sstevel@tonic-gate * N.B. that an argument *isn't* required here; if 2277c478bd9Sstevel@tonic-gate * it's not given, then the string INSPAT_STR will 2287c478bd9Sstevel@tonic-gate * be assumed. 2297c478bd9Sstevel@tonic-gate * 2307c478bd9Sstevel@tonic-gate * Since getopts(3C) doesn't handle the case of an 2317c478bd9Sstevel@tonic-gate * optional variable argument at all, we have to 2327c478bd9Sstevel@tonic-gate * parse this by hand: 2337c478bd9Sstevel@tonic-gate */ 2347c478bd9Sstevel@tonic-gate 2357c478bd9Sstevel@tonic-gate INSERT = PER_LINE = LEGAL = TRUE; 2367c478bd9Sstevel@tonic-gate N_ARGS = 0; 237788f581bSceastha if ((optarg != NULL) && (*optarg != '\0')) { 2387c478bd9Sstevel@tonic-gate INSPAT = optarg; 2397c478bd9Sstevel@tonic-gate } else { 2407c478bd9Sstevel@tonic-gate /* 2417c478bd9Sstevel@tonic-gate * here, there is no next argument. so 2427c478bd9Sstevel@tonic-gate * we reset INSPAT to the INSPAT_STR. 2437c478bd9Sstevel@tonic-gate * we *have* to do this, as -i/I may have 2447c478bd9Sstevel@tonic-gate * been given previously, and XCU4 requires 2457c478bd9Sstevel@tonic-gate * that only "the last one specified takes 2467c478bd9Sstevel@tonic-gate * effect". 2477c478bd9Sstevel@tonic-gate */ 2487c478bd9Sstevel@tonic-gate INSPAT = INSPAT_STR; 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 * number *is* required here: 2567c478bd9Sstevel@tonic-gate */ 2577c478bd9Sstevel@tonic-gate PER_LINE = TRUE; 2587c478bd9Sstevel@tonic-gate N_ARGS = 0; 2597c478bd9Sstevel@tonic-gate INSERT = FALSE; 260952d685eScf if ((PER_LINE = atoi(optarg)) <= 0) { 26103fc8686SGarrett D'Amore ermsg(_("#lines must be positive int: %s\n"), 26203fc8686SGarrett D'Amore optarg); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate break; 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate case 'l': 2677c478bd9Sstevel@tonic-gate /* 2687c478bd9Sstevel@tonic-gate * -l [number]: # of times cmd is executed 2697c478bd9Sstevel@tonic-gate * N.B. that an argument *isn't* required here; if 2707c478bd9Sstevel@tonic-gate * it's not given, then 1 is assumed. 2717c478bd9Sstevel@tonic-gate * 2727c478bd9Sstevel@tonic-gate * parseargs handles the optional arg processing. 2737c478bd9Sstevel@tonic-gate */ 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate PER_LINE = LEGAL = TRUE; /* initialization */ 2767c478bd9Sstevel@tonic-gate N_ARGS = 0; 2777c478bd9Sstevel@tonic-gate INSERT = FALSE; 2787c478bd9Sstevel@tonic-gate 279788f581bSceastha if ((optarg != NULL) && (*optarg != '\0')) { 2807c478bd9Sstevel@tonic-gate if ((PER_LINE = atoi(optarg)) <= 0) 2817c478bd9Sstevel@tonic-gate PER_LINE = 1; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate break; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate case 'n': /* -n number: # stdin args */ 2867c478bd9Sstevel@tonic-gate /* 2877c478bd9Sstevel@tonic-gate * -n number: # stdin args. 2887c478bd9Sstevel@tonic-gate * number *is* required here: 2897c478bd9Sstevel@tonic-gate */ 290952d685eScf if ((N_ARGS = atoi(optarg)) <= 0) { 29103fc8686SGarrett D'Amore ermsg(_("#args must be positive int: %s\n"), 29203fc8686SGarrett D'Amore optarg); 2937c478bd9Sstevel@tonic-gate } else { 2947c478bd9Sstevel@tonic-gate LEGAL = DASHX || N_ARGS == 1; 2957c478bd9Sstevel@tonic-gate INSERT = PER_LINE = FALSE; 2967c478bd9Sstevel@tonic-gate } 2977c478bd9Sstevel@tonic-gate break; 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate case 's': /* -s size: set max size of each arg list */ 300952d685eScf BUFLIM = atoi(optarg); 301952d685eScf if (BUFLIM > BUFSIZE || BUFLIM <= 0) { 30203fc8686SGarrett D'Amore ermsg(_("0 < max-cmd-line-size <= %d: %s\n"), 30303fc8686SGarrett D'Amore 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 usage(); 3167c478bd9Sstevel@tonic-gate exit(2); 3177c478bd9Sstevel@tonic-gate break; 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate } 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate /* 3227c478bd9Sstevel@tonic-gate * if anything called ermsg(), something screwed up, so 3237c478bd9Sstevel@tonic-gate * we exit early. 3247c478bd9Sstevel@tonic-gate */ 3257c478bd9Sstevel@tonic-gate if (OK == FALSE) { 3267c478bd9Sstevel@tonic-gate usage(); 3277c478bd9Sstevel@tonic-gate exit(2); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate /* 3317c478bd9Sstevel@tonic-gate * we're finished handling xargs's options, so now pick up 3327c478bd9Sstevel@tonic-gate * the command name (if any), and it's options. 3337c478bd9Sstevel@tonic-gate */ 3347c478bd9Sstevel@tonic-gate 3357c478bd9Sstevel@tonic-gate 3367c478bd9Sstevel@tonic-gate mac -= optind; /* dec arg count by what we've processed */ 3377c478bd9Sstevel@tonic-gate mav += optind; /* inc to current mav */ 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate if (mac <= 0) { /* if there're no more args to process, */ 3407c478bd9Sstevel@tonic-gate cmdname = "/usr/bin/echo"; /* our default command */ 3417c478bd9Sstevel@tonic-gate *ARGV++ = addarg(cmdname); /* use the default cmd. */ 3427c478bd9Sstevel@tonic-gate } else { /* otherwise keep parsing rest of the string. */ 3437c478bd9Sstevel@tonic-gate /* 3447c478bd9Sstevel@tonic-gate * note that we can't use getopts(3C), and *must* parse 3457c478bd9Sstevel@tonic-gate * this by hand, as we don't know apriori what options the 3467c478bd9Sstevel@tonic-gate * command will take. 3477c478bd9Sstevel@tonic-gate */ 3487c478bd9Sstevel@tonic-gate cmdname = *mav; /* get the command name */ 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate 3517c478bd9Sstevel@tonic-gate /* pick up the remaining args from the command line: */ 3527c478bd9Sstevel@tonic-gate while ((OK == TRUE) && (mac-- > 0)) { 3537c478bd9Sstevel@tonic-gate /* 3547c478bd9Sstevel@tonic-gate * while we haven't crapped out, and there's 3557c478bd9Sstevel@tonic-gate * work to do: 3567c478bd9Sstevel@tonic-gate */ 3577c478bd9Sstevel@tonic-gate if (INSERT && ! ERR) { 35803fc8686SGarrett D'Amore if (strstr(*mav, INSPAT) != NULL) { 3597c478bd9Sstevel@tonic-gate if (++n_inserts > MAXINSERTS) { 36003fc8686SGarrett D'Amore ermsg(_("too many args " 3617c478bd9Sstevel@tonic-gate "with %s\n"), INSPAT); 3627c478bd9Sstevel@tonic-gate ERR = TRUE; 3637c478bd9Sstevel@tonic-gate } 3647c478bd9Sstevel@tonic-gate psave->p_ARGV = ARGV; 3657c478bd9Sstevel@tonic-gate (psave++)->p_skel = *mav; 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate *ARGV++ = addarg(*mav++); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate /* pick up args from standard input */ 3737c478bd9Sstevel@tonic-gate 3747c478bd9Sstevel@tonic-gate initlist = ARGV; 3757c478bd9Sstevel@tonic-gate initsize = linesize; 37603fc8686SGarrett D'Amore lastarg[0] = '\0'; 3777c478bd9Sstevel@tonic-gate 37803fc8686SGarrett D'Amore while (OK) { 3797c478bd9Sstevel@tonic-gate N_args = 0; 3807c478bd9Sstevel@tonic-gate N_lines = 0; 3817c478bd9Sstevel@tonic-gate ARGV = initlist; 3827c478bd9Sstevel@tonic-gate linesize = initsize; 38303fc8686SGarrett D'Amore next = argbuf; 38403fc8686SGarrett D'Amore 38503fc8686SGarrett D'Amore while (MORE || (lastarg[0] != '\0')) { 38603fc8686SGarrett D'Amore int l; 38703fc8686SGarrett D'Amore 38803fc8686SGarrett D'Amore if (*lastarg != '\0') { 38903fc8686SGarrett D'Amore arg = strcpy(next, lastarg); 39003fc8686SGarrett D'Amore *lastarg = '\0'; 39103fc8686SGarrett D'Amore } else if ((arg = getarg(next)) == NULL) { 39203fc8686SGarrett D'Amore break; 39303fc8686SGarrett D'Amore } 39403fc8686SGarrett D'Amore 39503fc8686SGarrett D'Amore l = strlen(arg) + 1; 39603fc8686SGarrett D'Amore linesize += l; 39703fc8686SGarrett D'Amore next += l; 39803fc8686SGarrett D'Amore 39903fc8686SGarrett D'Amore /* Inserts are handled specially later. */ 40003fc8686SGarrett D'Amore if ((n_inserts == 0) && (linesize >= BUFLIM)) { 40103fc8686SGarrett D'Amore /* 40203fc8686SGarrett D'Amore * Legal indicates hard fail if the list is 40303fc8686SGarrett D'Amore * truncated due to size. So fail, or if we 40403fc8686SGarrett D'Amore * cannot create any list because it would be 40503fc8686SGarrett D'Amore * too big. 40603fc8686SGarrett D'Amore */ 40703fc8686SGarrett D'Amore if (LEGAL || N_args == 0) { 40803fc8686SGarrett D'Amore EMSG(LIST2LONG); 40903fc8686SGarrett D'Amore exit(2); 41003fc8686SGarrett D'Amore /* NOTREACHED */ 41103fc8686SGarrett D'Amore } 41203fc8686SGarrett D'Amore 41303fc8686SGarrett D'Amore /* 41403fc8686SGarrett D'Amore * Otherwise just save argument for later. 41503fc8686SGarrett D'Amore */ 41603fc8686SGarrett D'Amore (void) strcpy(lastarg, arg); 41703fc8686SGarrett D'Amore break; 41803fc8686SGarrett D'Amore } 41903fc8686SGarrett D'Amore 42003fc8686SGarrett D'Amore *ARGV++ = arg; 42103fc8686SGarrett D'Amore 42203fc8686SGarrett D'Amore N_args++; 42303fc8686SGarrett D'Amore 42403fc8686SGarrett D'Amore if ((PER_LINE && N_lines >= PER_LINE) || 42503fc8686SGarrett D'Amore (N_ARGS && (N_args) >= N_ARGS)) { 42603fc8686SGarrett D'Amore break; 42703fc8686SGarrett D'Amore } 42803fc8686SGarrett D'Amore 4297c478bd9Sstevel@tonic-gate 430a035dc19Scf if ((ARGV - arglist) == MAXARGS) { 431a035dc19Scf break; 432a035dc19Scf } 433a035dc19Scf } 43403fc8686SGarrett D'Amore 43503fc8686SGarrett D'Amore *ARGV = NULL; 43603fc8686SGarrett D'Amore if (N_args == 0) { 43703fc8686SGarrett D'Amore /* Reached the end with no more work. */ 438a035dc19Scf exit(exitstat); 439a035dc19Scf } 4407c478bd9Sstevel@tonic-gate 4417c478bd9Sstevel@tonic-gate /* insert arg if requested */ 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate if (!ERR && INSERT) { 44403fc8686SGarrett D'Amore 4457c478bd9Sstevel@tonic-gate p_ibuf = ins_buf; 4467c478bd9Sstevel@tonic-gate ARGV--; 4477c478bd9Sstevel@tonic-gate j = ibufsize = 0; 4487c478bd9Sstevel@tonic-gate for (psave = saveargv; ++j <= n_inserts; ++psave) { 4497c478bd9Sstevel@tonic-gate addibuf(psave); 4507c478bd9Sstevel@tonic-gate if (ERR) 4517c478bd9Sstevel@tonic-gate break; 4527c478bd9Sstevel@tonic-gate } 4537c478bd9Sstevel@tonic-gate } 45403fc8686SGarrett D'Amore *ARGV = NULL; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (n_inserts > 0) { 4577c478bd9Sstevel@tonic-gate /* 4587c478bd9Sstevel@tonic-gate * if we've done any insertions, re-calculate the 4597c478bd9Sstevel@tonic-gate * linesize. bomb out if we've exceeded our length. 4607c478bd9Sstevel@tonic-gate */ 46103fc8686SGarrett D'Amore linesize = 0; 4627c478bd9Sstevel@tonic-gate for (ARGV = arglist; *ARGV != NULL; ARGV++) { 46303fc8686SGarrett D'Amore linesize += strlen(*ARGV) + 1; 46403fc8686SGarrett D'Amore } 46503fc8686SGarrett D'Amore if (linesize >= BUFLIM) { 46603fc8686SGarrett D'Amore EMSG(LIST2LONG); 46703fc8686SGarrett D'Amore exit(2); 46803fc8686SGarrett D'Amore /* NOTREACHED */ 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate } 4717c478bd9Sstevel@tonic-gate 4727c478bd9Sstevel@tonic-gate /* exec command */ 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate if (!ERR) { 4757c478bd9Sstevel@tonic-gate if (!MORE && 4767c478bd9Sstevel@tonic-gate (PER_LINE && N_lines == 0 || N_ARGS && N_args == 0)) 4777c478bd9Sstevel@tonic-gate exit(exitstat); 4787c478bd9Sstevel@tonic-gate OK = TRUE; 4797c478bd9Sstevel@tonic-gate j = TRACE ? echoargs() : TRUE; 4807c478bd9Sstevel@tonic-gate if (j) { 4817c478bd9Sstevel@tonic-gate /* 4827c478bd9Sstevel@tonic-gate * for xcu4, all invocations of cmdname must 4837c478bd9Sstevel@tonic-gate * return 0, in order for us to return 0. 4847c478bd9Sstevel@tonic-gate * so if we have a non-zero status here, 4857c478bd9Sstevel@tonic-gate * quit immediately. 4867c478bd9Sstevel@tonic-gate */ 48703fc8686SGarrett D'Amore exitstat |= lcall(cmdname, arglist); 4887c478bd9Sstevel@tonic-gate } 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate } 4917c478bd9Sstevel@tonic-gate 49203fc8686SGarrett D'Amore if (OK) 49343a29105Srobbin return (exitstat); 4947c478bd9Sstevel@tonic-gate 49503fc8686SGarrett D'Amore /* 49603fc8686SGarrett D'Amore * if exitstat was set, to match XCU4 complience, 49703fc8686SGarrett D'Amore * return that value, otherwise, return 1. 49803fc8686SGarrett D'Amore */ 49903fc8686SGarrett D'Amore return (exitstat ? exitstat : 1); 5007c478bd9Sstevel@tonic-gate } 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate static char * 5037c478bd9Sstevel@tonic-gate addarg(char *arg) 5047c478bd9Sstevel@tonic-gate { 50503fc8686SGarrett D'Amore linesize += (strlen(arg) + 1); 50603fc8686SGarrett D'Amore return (arg); 5077c478bd9Sstevel@tonic-gate } 5087c478bd9Sstevel@tonic-gate 5097c478bd9Sstevel@tonic-gate 51003fc8686SGarrett D'Amore static void 51103fc8686SGarrett D'Amore store_str(char **buffer, char *str, size_t len) 51203fc8686SGarrett D'Amore { 51303fc8686SGarrett D'Amore (void) memcpy(*buffer, str, len); 51403fc8686SGarrett D'Amore (*buffer)[len] = '\0'; 51503fc8686SGarrett D'Amore *buffer += len; 5167c478bd9Sstevel@tonic-gate } 5177c478bd9Sstevel@tonic-gate 51803fc8686SGarrett D'Amore 5197c478bd9Sstevel@tonic-gate static char * 52003fc8686SGarrett D'Amore getarg(char *arg) 5217c478bd9Sstevel@tonic-gate { 52203fc8686SGarrett D'Amore char *xarg = arg; 5237c478bd9Sstevel@tonic-gate wchar_t c; 5247c478bd9Sstevel@tonic-gate char mbc[MB_LEN_MAX]; 52503fc8686SGarrett D'Amore size_t len; 52603fc8686SGarrett D'Amore int escape = 0; 52703fc8686SGarrett D'Amore int inquote = 0; 5287c478bd9Sstevel@tonic-gate 52903fc8686SGarrett D'Amore arg[0] = '\0'; 5307c478bd9Sstevel@tonic-gate 53103fc8686SGarrett D'Amore while (MORE) { 5327c478bd9Sstevel@tonic-gate 53303fc8686SGarrett D'Amore len = 0; 53403fc8686SGarrett D'Amore c = getwchr(mbc, &len); 5357c478bd9Sstevel@tonic-gate 53603fc8686SGarrett D'Amore if (((arg - xarg) + len) > BUFLIM) { 53703fc8686SGarrett D'Amore EMSG2(ARG2LONG, BUFLIM); 53803fc8686SGarrett D'Amore exit(2); 53903fc8686SGarrett D'Amore ERR = TRUE; 5407c478bd9Sstevel@tonic-gate return (NULL); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 54303fc8686SGarrett D'Amore switch (c) { 54403fc8686SGarrett D'Amore case '\n': 54503fc8686SGarrett D'Amore if (ZERO) { 54603fc8686SGarrett D'Amore store_str(&arg, mbc, len); 5477c478bd9Sstevel@tonic-gate continue; 5487c478bd9Sstevel@tonic-gate } 54903fc8686SGarrett D'Amore /* FALLTHRU */ 5507c478bd9Sstevel@tonic-gate 55103fc8686SGarrett D'Amore case '\0': 55203fc8686SGarrett D'Amore case WEOF: /* Note WEOF == EOF */ 5537c478bd9Sstevel@tonic-gate 55403fc8686SGarrett D'Amore if (escape) { 55503fc8686SGarrett D'Amore EMSG(BADESCAPE); 55603fc8686SGarrett D'Amore ERR = TRUE; 55703fc8686SGarrett D'Amore return (NULL); 5587c478bd9Sstevel@tonic-gate } 5597c478bd9Sstevel@tonic-gate if (inquote) { 56003fc8686SGarrett D'Amore EMSG(MISSQUOTE); 5617c478bd9Sstevel@tonic-gate ERR = TRUE; 56203fc8686SGarrett D'Amore return (NULL); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate N_lines++; 56603fc8686SGarrett D'Amore break; 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate case '"': 56903fc8686SGarrett D'Amore if (ZERO || escape || (inquote == 1)) { 57003fc8686SGarrett D'Amore /* treat it literally */ 57103fc8686SGarrett D'Amore escape = 0; 57203fc8686SGarrett D'Amore store_str(&arg, mbc, len); 57303fc8686SGarrett D'Amore 57403fc8686SGarrett D'Amore } else if (inquote == 2) { 57503fc8686SGarrett D'Amore /* terminating double quote */ 5767c478bd9Sstevel@tonic-gate inquote = 0; 57703fc8686SGarrett D'Amore 57803fc8686SGarrett D'Amore } else { 57903fc8686SGarrett D'Amore /* starting quoted string */ 5807c478bd9Sstevel@tonic-gate inquote = 2; 58103fc8686SGarrett D'Amore } 58203fc8686SGarrett D'Amore continue; 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate case '\'': 58503fc8686SGarrett D'Amore if (ZERO || escape || (inquote == 2)) { 58603fc8686SGarrett D'Amore /* treat it literally */ 58703fc8686SGarrett D'Amore escape = 0; 58803fc8686SGarrett D'Amore store_str(&arg, mbc, len); 58903fc8686SGarrett D'Amore 59003fc8686SGarrett D'Amore } else if (inquote == 1) { 59103fc8686SGarrett D'Amore /* terminating single quote */ 5927c478bd9Sstevel@tonic-gate inquote = 0; 59303fc8686SGarrett D'Amore 59403fc8686SGarrett D'Amore } else { 59503fc8686SGarrett D'Amore /* starting quoted string */ 5967c478bd9Sstevel@tonic-gate inquote = 1; 59703fc8686SGarrett D'Amore } 59803fc8686SGarrett D'Amore continue; 5997c478bd9Sstevel@tonic-gate 60003fc8686SGarrett D'Amore case '\\': 601d2afb7a9Scf /* 602d2afb7a9Scf * Any unquoted character can be escaped by 603d2afb7a9Scf * preceding it with a backslash. 604d2afb7a9Scf */ 60503fc8686SGarrett D'Amore if (ZERO || inquote || escape) { 60603fc8686SGarrett D'Amore escape = 0; 60703fc8686SGarrett D'Amore store_str(&arg, mbc, len); 60803fc8686SGarrett D'Amore } else { 60903fc8686SGarrett D'Amore escape = 1; 610d2afb7a9Scf } 61103fc8686SGarrett D'Amore continue; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate default: 61403fc8686SGarrett D'Amore /* most times we will just want to store it */ 61503fc8686SGarrett D'Amore if (inquote || escape || ZERO || !iswctype(c, blank)) { 61603fc8686SGarrett D'Amore escape = 0; 61703fc8686SGarrett D'Amore store_str(&arg, mbc, len); 61803fc8686SGarrett D'Amore continue; 6197c478bd9Sstevel@tonic-gate } 62003fc8686SGarrett D'Amore /* unquoted blank */ 6217c478bd9Sstevel@tonic-gate break; 6227c478bd9Sstevel@tonic-gate } 62303fc8686SGarrett D'Amore 62403fc8686SGarrett D'Amore /* 62503fc8686SGarrett D'Amore * At this point we are processing a complete argument. 62603fc8686SGarrett D'Amore */ 62703fc8686SGarrett D'Amore if (strcmp(xarg, LEOF) == 0 && *LEOF != '\0') { 62803fc8686SGarrett D'Amore MORE = FALSE; 62903fc8686SGarrett D'Amore return (NULL); 63003fc8686SGarrett D'Amore } 63103fc8686SGarrett D'Amore if (c == WEOF) { 63203fc8686SGarrett D'Amore MORE = FALSE; 63303fc8686SGarrett D'Amore } 63403fc8686SGarrett D'Amore if (xarg[0] == '\0') 63503fc8686SGarrett D'Amore continue; 63603fc8686SGarrett D'Amore break; 6377c478bd9Sstevel@tonic-gate } 6387c478bd9Sstevel@tonic-gate 63903fc8686SGarrett D'Amore return (xarg[0] == '\0' ? NULL : xarg); 64003fc8686SGarrett D'Amore } 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate /* 6437c478bd9Sstevel@tonic-gate * ermsg(): print out an error message, and indicate failure globally. 6447c478bd9Sstevel@tonic-gate * 6457c478bd9Sstevel@tonic-gate * Assumes that message has already been gettext()'d. It would be 6467c478bd9Sstevel@tonic-gate * nice if we could just do the gettext() here, but we can't, since 6477c478bd9Sstevel@tonic-gate * since xgettext(1M) wouldn't be able to pick up our error message. 6487c478bd9Sstevel@tonic-gate */ 6497c478bd9Sstevel@tonic-gate /* PRINTFLIKE1 */ 6507c478bd9Sstevel@tonic-gate static void 6517c478bd9Sstevel@tonic-gate ermsg(char *messages, ...) 6527c478bd9Sstevel@tonic-gate { 6537c478bd9Sstevel@tonic-gate va_list ap; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate va_start(ap, messages); 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "xargs: "); 6587c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, messages, ap); 6597c478bd9Sstevel@tonic-gate 6607c478bd9Sstevel@tonic-gate va_end(ap); 6617c478bd9Sstevel@tonic-gate OK = FALSE; 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate static int 6657c478bd9Sstevel@tonic-gate echoargs() 6667c478bd9Sstevel@tonic-gate { 6677c478bd9Sstevel@tonic-gate char **anarg; 6687c478bd9Sstevel@tonic-gate char **tanarg; /* tmp ptr */ 6697c478bd9Sstevel@tonic-gate int i; 6707c478bd9Sstevel@tonic-gate char reply[LINE_MAX]; 6717c478bd9Sstevel@tonic-gate 6727c478bd9Sstevel@tonic-gate tanarg = anarg = arglist-1; 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate /* 6757c478bd9Sstevel@tonic-gate * write out each argument, separated by a space. the tanarg 6767c478bd9Sstevel@tonic-gate * nonsense is for xcu4 testsuite compliance - so that an 6777c478bd9Sstevel@tonic-gate * extra space isn't echoed after the last argument. 6787c478bd9Sstevel@tonic-gate */ 6797c478bd9Sstevel@tonic-gate while (*++anarg) { /* while there's an argument */ 6807c478bd9Sstevel@tonic-gate ++tanarg; /* follow anarg */ 6817c478bd9Sstevel@tonic-gate (void) write(2, *anarg, strlen(*anarg)); 6827c478bd9Sstevel@tonic-gate 6837c478bd9Sstevel@tonic-gate if (*++tanarg) { /* if there's another argument: */ 6847c478bd9Sstevel@tonic-gate (void) write(2, " ", 1); /* add a space */ 6857c478bd9Sstevel@tonic-gate --tanarg; /* reset back to anarg */ 6867c478bd9Sstevel@tonic-gate } 6877c478bd9Sstevel@tonic-gate } 6887c478bd9Sstevel@tonic-gate if (PROMPT == -1) { 6897c478bd9Sstevel@tonic-gate (void) write(2, "\n", 1); 6907c478bd9Sstevel@tonic-gate return (TRUE); 6917c478bd9Sstevel@tonic-gate } 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate (void) write(2, "?...", 4); /* ask the user for input */ 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate for (i = 0; i < LINE_MAX && read(PROMPT, &reply[i], 1) > 0; i++) { 6967c478bd9Sstevel@tonic-gate if (reply[i] == '\n') { 6977c478bd9Sstevel@tonic-gate if (i == 0) 6987c478bd9Sstevel@tonic-gate return (FALSE); 6997c478bd9Sstevel@tonic-gate break; 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate } 7027c478bd9Sstevel@tonic-gate reply[i] = 0; 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate /* flush remainder of line if necessary */ 7057c478bd9Sstevel@tonic-gate if (i == LINE_MAX) { 7067c478bd9Sstevel@tonic-gate char bitbucket; 7077c478bd9Sstevel@tonic-gate 7087c478bd9Sstevel@tonic-gate while ((read(PROMPT, &bitbucket, 1) > 0) && (bitbucket != '\n')) 7097c478bd9Sstevel@tonic-gate ; 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate 7123d63ea05Sas return (yes_check(reply)); 7137c478bd9Sstevel@tonic-gate } 7147c478bd9Sstevel@tonic-gate 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate static char * 7177c478bd9Sstevel@tonic-gate insert(char *pattern, char *subst) 7187c478bd9Sstevel@tonic-gate { 7197c478bd9Sstevel@tonic-gate static char buffer[MAXSBUF+1]; 7207c478bd9Sstevel@tonic-gate int len, ipatlen; 7217c478bd9Sstevel@tonic-gate char *pat; 7227c478bd9Sstevel@tonic-gate char *bufend; 7237c478bd9Sstevel@tonic-gate char *pbuf; 7247c478bd9Sstevel@tonic-gate 7257c478bd9Sstevel@tonic-gate len = strlen(subst); 7267c478bd9Sstevel@tonic-gate ipatlen = strlen(INSPAT) - 1; 7277c478bd9Sstevel@tonic-gate pat = pattern - 1; 7287c478bd9Sstevel@tonic-gate pbuf = buffer; 7297c478bd9Sstevel@tonic-gate bufend = &buffer[MAXSBUF]; 7307c478bd9Sstevel@tonic-gate 7317c478bd9Sstevel@tonic-gate while (*++pat) { 73203fc8686SGarrett D'Amore if (strncmp(pat, INSPAT, ipatlen) == 0) { 7337c478bd9Sstevel@tonic-gate if (pbuf + len >= bufend) { 7347c478bd9Sstevel@tonic-gate break; 7357c478bd9Sstevel@tonic-gate } else { 7367c478bd9Sstevel@tonic-gate (void) strcpy(pbuf, subst); 7377c478bd9Sstevel@tonic-gate pat += ipatlen; 7387c478bd9Sstevel@tonic-gate pbuf += len; 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate } else { 7417c478bd9Sstevel@tonic-gate *pbuf++ = *pat; 7427c478bd9Sstevel@tonic-gate if (pbuf >= bufend) 7437c478bd9Sstevel@tonic-gate break; 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate if (!*pat) { 7487c478bd9Sstevel@tonic-gate *pbuf = '\0'; 7497c478bd9Sstevel@tonic-gate return (buffer); 7507c478bd9Sstevel@tonic-gate } else { 7517c478bd9Sstevel@tonic-gate ermsg(gettext("Maximum argument size with insertion via %s's " 7527c478bd9Sstevel@tonic-gate "exceeded\n"), INSPAT); 7537c478bd9Sstevel@tonic-gate ERR = TRUE; 75403fc8686SGarrett D'Amore return (NULL); 7557c478bd9Sstevel@tonic-gate } 7567c478bd9Sstevel@tonic-gate } 7577c478bd9Sstevel@tonic-gate 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate static void 7607c478bd9Sstevel@tonic-gate addibuf(struct inserts *p) 7617c478bd9Sstevel@tonic-gate { 7627c478bd9Sstevel@tonic-gate char *newarg, *skel, *sub; 7637c478bd9Sstevel@tonic-gate int l; 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate skel = p->p_skel; 7667c478bd9Sstevel@tonic-gate sub = *ARGV; 7677c478bd9Sstevel@tonic-gate newarg = insert(skel, sub); 7687c478bd9Sstevel@tonic-gate if (ERR) 769788f581bSceastha return; 7707c478bd9Sstevel@tonic-gate 77103fc8686SGarrett D'Amore l = strlen(newarg) + 1; 77203fc8686SGarrett D'Amore if ((ibufsize += l) > MAXIBUF) { 77303fc8686SGarrett D'Amore EMSG(IBUFOVERFLOW); 77403fc8686SGarrett D'Amore ERR = TRUE; 7757c478bd9Sstevel@tonic-gate } 77603fc8686SGarrett D'Amore (void) strcpy(p_ibuf, newarg); 77703fc8686SGarrett D'Amore *(p->p_ARGV) = p_ibuf; 77803fc8686SGarrett D'Amore p_ibuf += l; 7797c478bd9Sstevel@tonic-gate } 7807c478bd9Sstevel@tonic-gate 7817c478bd9Sstevel@tonic-gate 7827c478bd9Sstevel@tonic-gate /* 78303fc8686SGarrett D'Amore * getwchr(): get the next wide character. 7847c478bd9Sstevel@tonic-gate * description: 78503fc8686SGarrett D'Amore * we get the next character from stdin. This returns WEOF if no 78603fc8686SGarrett D'Amore * character is present. If ZERO is set, it gets a single byte instead 78703fc8686SGarrett D'Amore * a wide character. 7887c478bd9Sstevel@tonic-gate */ 78903fc8686SGarrett D'Amore static wint_t 79003fc8686SGarrett D'Amore getwchr(char *mbc, size_t *sz) 7917c478bd9Sstevel@tonic-gate { 79203fc8686SGarrett D'Amore size_t i; 79303fc8686SGarrett D'Amore int c; 79403fc8686SGarrett D'Amore wchar_t wch; 7957c478bd9Sstevel@tonic-gate 79603fc8686SGarrett D'Amore i = 0; 79703fc8686SGarrett D'Amore while (i < MB_CUR_MAX) { 7987c478bd9Sstevel@tonic-gate 79903fc8686SGarrett D'Amore if ((c = fgetc(stdin)) == EOF) { 8007c478bd9Sstevel@tonic-gate 80103fc8686SGarrett D'Amore if (i == 0) { 8027c478bd9Sstevel@tonic-gate /* TRUE EOF has been reached */ 80303fc8686SGarrett D'Amore return (WEOF); 8047c478bd9Sstevel@tonic-gate } 80503fc8686SGarrett D'Amore 8067c478bd9Sstevel@tonic-gate /* 8077c478bd9Sstevel@tonic-gate * We have some characters in our buffer still so it 8087c478bd9Sstevel@tonic-gate * must be an invalid character right before EOF. 8097c478bd9Sstevel@tonic-gate */ 8107c478bd9Sstevel@tonic-gate break; 8117c478bd9Sstevel@tonic-gate } 81203fc8686SGarrett D'Amore mbc[i++] = (char)c; 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate /* If this succeeds then we are done */ 81503fc8686SGarrett D'Amore if (ZERO) { 81603fc8686SGarrett D'Amore *sz = i; 81703fc8686SGarrett D'Amore return ((char)c); 81803fc8686SGarrett D'Amore } 81903fc8686SGarrett D'Amore if (mbtowc(&wch, mbc, i) != -1) { 82003fc8686SGarrett D'Amore *sz = i; 82103fc8686SGarrett D'Amore return ((wint_t)wch); 82203fc8686SGarrett D'Amore } 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate 8257c478bd9Sstevel@tonic-gate /* 8267c478bd9Sstevel@tonic-gate * We have now encountered an illegal character sequence. 8277c478bd9Sstevel@tonic-gate * There is nothing much we can do at this point but 8287c478bd9Sstevel@tonic-gate * return an error. If we attempt to recover we may in fact 8297c478bd9Sstevel@tonic-gate * return garbage as arguments, from the customer's point 8307c478bd9Sstevel@tonic-gate * of view. After all what if they are feeding us a file 8317c478bd9Sstevel@tonic-gate * generated in another locale? 8327c478bd9Sstevel@tonic-gate */ 8337c478bd9Sstevel@tonic-gate errno = EILSEQ; 83403fc8686SGarrett D'Amore PERR(CORRUPTFILE); 8357c478bd9Sstevel@tonic-gate exit(1); 8367c478bd9Sstevel@tonic-gate /* NOTREACHED */ 8377c478bd9Sstevel@tonic-gate } 8387c478bd9Sstevel@tonic-gate 8397c478bd9Sstevel@tonic-gate 8407c478bd9Sstevel@tonic-gate static int 8417c478bd9Sstevel@tonic-gate lcall(char *sub, char **subargs) 8427c478bd9Sstevel@tonic-gate { 8437c478bd9Sstevel@tonic-gate int retcode, retry = 0; 8447c478bd9Sstevel@tonic-gate pid_t iwait, child; 8457c478bd9Sstevel@tonic-gate 84603fc8686SGarrett D'Amore for (;;) { 8477c478bd9Sstevel@tonic-gate switch (child = fork()) { 8487c478bd9Sstevel@tonic-gate default: 8497c478bd9Sstevel@tonic-gate while ((iwait = wait(&retcode)) != child && 8507c478bd9Sstevel@tonic-gate iwait != (pid_t)-1) 8517c478bd9Sstevel@tonic-gate ; 8527c478bd9Sstevel@tonic-gate if (iwait == (pid_t)-1) { 85303fc8686SGarrett D'Amore PERR(WAITFAIL); 8547c478bd9Sstevel@tonic-gate exit(122); 8557c478bd9Sstevel@tonic-gate /* NOTREACHED */ 8567c478bd9Sstevel@tonic-gate } 8577c478bd9Sstevel@tonic-gate if (WIFSIGNALED(retcode)) { 85803fc8686SGarrett D'Amore EMSG2(CHILDSIG, WTERMSIG(retcode)); 8597c478bd9Sstevel@tonic-gate exit(125); 8607c478bd9Sstevel@tonic-gate /* NOTREACHED */ 8617c478bd9Sstevel@tonic-gate } 8627c478bd9Sstevel@tonic-gate if ((WEXITSTATUS(retcode) & 0377) == 0377) { 86303fc8686SGarrett D'Amore EMSG(CHILDFAIL); 8647c478bd9Sstevel@tonic-gate exit(124); 8657c478bd9Sstevel@tonic-gate /* NOTREACHED */ 8667c478bd9Sstevel@tonic-gate } 8677c478bd9Sstevel@tonic-gate return (WEXITSTATUS(retcode)); 8687c478bd9Sstevel@tonic-gate case 0: 8697c478bd9Sstevel@tonic-gate (void) execvp(sub, subargs); 87003fc8686SGarrett D'Amore PERR(EXECFAIL); 8717c478bd9Sstevel@tonic-gate if (errno == EACCES) 8727c478bd9Sstevel@tonic-gate exit(126); 8737c478bd9Sstevel@tonic-gate exit(127); 8747c478bd9Sstevel@tonic-gate /* NOTREACHED */ 8757c478bd9Sstevel@tonic-gate case -1: 8767c478bd9Sstevel@tonic-gate if (errno != EAGAIN && retry++ < FORK_RETRY) { 87703fc8686SGarrett D'Amore PERR(FORKFAIL); 8787c478bd9Sstevel@tonic-gate exit(123); 8797c478bd9Sstevel@tonic-gate } 8807c478bd9Sstevel@tonic-gate (void) sleep(1); 8817c478bd9Sstevel@tonic-gate } 8827c478bd9Sstevel@tonic-gate } 8837c478bd9Sstevel@tonic-gate } 8847c478bd9Sstevel@tonic-gate 8857c478bd9Sstevel@tonic-gate 8867c478bd9Sstevel@tonic-gate static void 8877c478bd9Sstevel@tonic-gate usage() 8887c478bd9Sstevel@tonic-gate { 88903fc8686SGarrett D'Amore ermsg(_(USAGEMSG)); 8907c478bd9Sstevel@tonic-gate OK = FALSE; 8917c478bd9Sstevel@tonic-gate } 8927c478bd9Sstevel@tonic-gate 8937c478bd9Sstevel@tonic-gate 8947c478bd9Sstevel@tonic-gate 8957c478bd9Sstevel@tonic-gate /* 8967c478bd9Sstevel@tonic-gate * parseargs(): modify the args 8977c478bd9Sstevel@tonic-gate * since the -e, -i and -l flags all take optional subarguments, 8987c478bd9Sstevel@tonic-gate * and getopts(3C) is clueless about this nonsense, we change the 8997c478bd9Sstevel@tonic-gate * our local argument count and strings to separate this out, 9007c478bd9Sstevel@tonic-gate * and make it easier to handle via getopts(3c). 9017c478bd9Sstevel@tonic-gate * 9027c478bd9Sstevel@tonic-gate * -e -> "-e "" 9037c478bd9Sstevel@tonic-gate * -e3 -> "-e "3" 9047c478bd9Sstevel@tonic-gate * -Estr -> "-E "str" 9057c478bd9Sstevel@tonic-gate * -i -> "-i "{}" 9067c478bd9Sstevel@tonic-gate * -irep -> "-i "rep" 9077c478bd9Sstevel@tonic-gate * -l -> "-i "1" 9087c478bd9Sstevel@tonic-gate * -l10 -> "-i "10" 9097c478bd9Sstevel@tonic-gate * 9107c478bd9Sstevel@tonic-gate * since the -e, -i and -l flags all take optional subarguments, 9117c478bd9Sstevel@tonic-gate */ 9127c478bd9Sstevel@tonic-gate static void 9137c478bd9Sstevel@tonic-gate parseargs(int ac, char **av) 9147c478bd9Sstevel@tonic-gate { 9157c478bd9Sstevel@tonic-gate int i; /* current argument */ 9167c478bd9Sstevel@tonic-gate int cflag; /* 0 = not processing cmd arg */ 9177c478bd9Sstevel@tonic-gate 9187c478bd9Sstevel@tonic-gate if ((mav = malloc((ac * 2 + 1) * sizeof (char *))) == NULL) { 91903fc8686SGarrett D'Amore PERR(MALLOCFAIL); 9207c478bd9Sstevel@tonic-gate exit(1); 9217c478bd9Sstevel@tonic-gate } 9227c478bd9Sstevel@tonic-gate 9237c478bd9Sstevel@tonic-gate /* for each argument, see if we need to change things: */ 9247c478bd9Sstevel@tonic-gate for (i = mac = cflag = 0; (av[i] != NULL) && i < ac; i++, mac++) { 9257c478bd9Sstevel@tonic-gate if ((mav[mac] = strdup(av[i])) == NULL) { 92603fc8686SGarrett D'Amore PERR(MALLOCFAIL); 9277c478bd9Sstevel@tonic-gate exit(1); 9287c478bd9Sstevel@tonic-gate } 9297c478bd9Sstevel@tonic-gate 9307c478bd9Sstevel@tonic-gate /* -- has been found or argument list is fully processes */ 9317c478bd9Sstevel@tonic-gate if (cflag) 9327c478bd9Sstevel@tonic-gate continue; 9337c478bd9Sstevel@tonic-gate 9347c478bd9Sstevel@tonic-gate /* 9357c478bd9Sstevel@tonic-gate * if we're doing special processing, and we've got a flag 9367c478bd9Sstevel@tonic-gate */ 9377c478bd9Sstevel@tonic-gate else if ((av[i][0] == '-') && (av[i][1] != NULL)) { 9387c478bd9Sstevel@tonic-gate char *def; 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate switch (av[i][1]) { 9417c478bd9Sstevel@tonic-gate case 'e': 9427c478bd9Sstevel@tonic-gate def = ""; /* -e with no arg turns off eof */ 9437c478bd9Sstevel@tonic-gate goto process_special; 9447c478bd9Sstevel@tonic-gate case 'i': 9457c478bd9Sstevel@tonic-gate def = INSPAT_STR; 9467c478bd9Sstevel@tonic-gate goto process_special; 9477c478bd9Sstevel@tonic-gate case 'l': 9487c478bd9Sstevel@tonic-gate def = "1"; 9497c478bd9Sstevel@tonic-gate process_special: 9507c478bd9Sstevel@tonic-gate /* 9517c478bd9Sstevel@tonic-gate * if there's no sub-option, we *must* add 9527c478bd9Sstevel@tonic-gate * a default one. this is because xargs must 9537c478bd9Sstevel@tonic-gate * be able to distinguish between a valid 9547c478bd9Sstevel@tonic-gate * suboption, and a command name. 9557c478bd9Sstevel@tonic-gate */ 9567c478bd9Sstevel@tonic-gate if (av[i][2] == NULL) { 9577c478bd9Sstevel@tonic-gate mav[++mac] = strdup(def); 9587c478bd9Sstevel@tonic-gate } else { 9597c478bd9Sstevel@tonic-gate /* clear out our version: */ 9607c478bd9Sstevel@tonic-gate mav[mac][2] = NULL; 9617c478bd9Sstevel@tonic-gate mav[++mac] = strdup(&av[i][2]); 9627c478bd9Sstevel@tonic-gate } 9637c478bd9Sstevel@tonic-gate if (mav[mac] == NULL) { 96403fc8686SGarrett D'Amore PERR(MALLOCFAIL); 9657c478bd9Sstevel@tonic-gate exit(1); 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate break; 9687c478bd9Sstevel@tonic-gate 9697c478bd9Sstevel@tonic-gate /* flags with required subarguments: */ 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * there are two separate cases here. either the 9737c478bd9Sstevel@tonic-gate * flag can have the normal XCU4 handling 9747c478bd9Sstevel@tonic-gate * (of the form: -X subargument); or it can have 9757c478bd9Sstevel@tonic-gate * the old solaris 2.[0-4] handling (of the 9767c478bd9Sstevel@tonic-gate * form: -Xsubargument). in order to maintain 9777c478bd9Sstevel@tonic-gate * backwards compatibility, we must support the 9787c478bd9Sstevel@tonic-gate * latter case. we handle the latter possibility 9797c478bd9Sstevel@tonic-gate * first so both the old solaris way of handling 9807c478bd9Sstevel@tonic-gate * and the new XCU4 way of handling things are allowed. 9817c478bd9Sstevel@tonic-gate */ 9827c478bd9Sstevel@tonic-gate case 'n': /* FALLTHROUGH */ 9837c478bd9Sstevel@tonic-gate case 's': /* FALLTHROUGH */ 9847c478bd9Sstevel@tonic-gate case 'E': /* FALLTHROUGH */ 9857c478bd9Sstevel@tonic-gate case 'I': /* FALLTHROUGH */ 9867c478bd9Sstevel@tonic-gate case 'L': 9877c478bd9Sstevel@tonic-gate /* 9887c478bd9Sstevel@tonic-gate * if the second character isn't null, then 9897c478bd9Sstevel@tonic-gate * the user has specified the old syntax. 9907c478bd9Sstevel@tonic-gate * we move the subargument into our 9917c478bd9Sstevel@tonic-gate * mod'd argument list. 9927c478bd9Sstevel@tonic-gate */ 9937c478bd9Sstevel@tonic-gate if (av[i][2] != NULL) { 9947c478bd9Sstevel@tonic-gate /* first clean things up: */ 9957c478bd9Sstevel@tonic-gate mav[mac][2] = NULL; 9967c478bd9Sstevel@tonic-gate 9977c478bd9Sstevel@tonic-gate /* now add the separation: */ 9987c478bd9Sstevel@tonic-gate ++mac; /* inc to next mod'd arg */ 9997c478bd9Sstevel@tonic-gate if ((mav[mac] = strdup(&av[i][2])) == 10007c478bd9Sstevel@tonic-gate NULL) { 100103fc8686SGarrett D'Amore PERR(MALLOCFAIL); 10027c478bd9Sstevel@tonic-gate exit(1); 10037c478bd9Sstevel@tonic-gate } 10047c478bd9Sstevel@tonic-gate break; 10057c478bd9Sstevel@tonic-gate } 10067c478bd9Sstevel@tonic-gate i++; 10077c478bd9Sstevel@tonic-gate mac++; 1008952d685eScf 10097c478bd9Sstevel@tonic-gate if (av[i] == NULL) { 10107c478bd9Sstevel@tonic-gate mav[mac] = NULL; 10117c478bd9Sstevel@tonic-gate return; 10127c478bd9Sstevel@tonic-gate } 10137c478bd9Sstevel@tonic-gate if ((mav[mac] = strdup(av[i])) == NULL) { 101403fc8686SGarrett D'Amore PERR(MALLOCFAIL); 10157c478bd9Sstevel@tonic-gate exit(1); 10167c478bd9Sstevel@tonic-gate } 10177c478bd9Sstevel@tonic-gate break; 10187c478bd9Sstevel@tonic-gate 10197c478bd9Sstevel@tonic-gate /* flags */ 10207c478bd9Sstevel@tonic-gate case 'p' : 10217c478bd9Sstevel@tonic-gate case 't' : 10227c478bd9Sstevel@tonic-gate case 'x' : 1023*1daace1dSGarrett D'Amore case '0' : 10247c478bd9Sstevel@tonic-gate break; 10257c478bd9Sstevel@tonic-gate 10267c478bd9Sstevel@tonic-gate case '-' : 10277c478bd9Sstevel@tonic-gate default: 10287c478bd9Sstevel@tonic-gate /* 10297c478bd9Sstevel@tonic-gate * here we've hit the cmd argument. so 10307c478bd9Sstevel@tonic-gate * we'll stop special processing, as the 10317c478bd9Sstevel@tonic-gate * cmd may have a "-i" etc., argument, 10327c478bd9Sstevel@tonic-gate * and we don't want to add a "" to it. 10337c478bd9Sstevel@tonic-gate */ 10347c478bd9Sstevel@tonic-gate cflag = 1; 10357c478bd9Sstevel@tonic-gate break; 10367c478bd9Sstevel@tonic-gate } 10377c478bd9Sstevel@tonic-gate } else if (i > 0) { /* if we're not the 1st arg */ 10387c478bd9Sstevel@tonic-gate /* 10397c478bd9Sstevel@tonic-gate * if it's not a flag, then it *must* be the cmd. 10407c478bd9Sstevel@tonic-gate * set cflag, so we don't mishandle the -[eil] flags. 10417c478bd9Sstevel@tonic-gate */ 10427c478bd9Sstevel@tonic-gate cflag = 1; 10437c478bd9Sstevel@tonic-gate } 10447c478bd9Sstevel@tonic-gate } 10457c478bd9Sstevel@tonic-gate 10467c478bd9Sstevel@tonic-gate mav[mac] = NULL; 10477c478bd9Sstevel@tonic-gate } 1048