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