19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1990, 1993 39b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 49b50d902SRodney W. Grimes * 59b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 69b50d902SRodney W. Grimes * John B. Roll Jr. 79b50d902SRodney W. Grimes * 89b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 99b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 109b50d902SRodney W. Grimes * are met: 119b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 129b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 139b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 159b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 169b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 179b50d902SRodney W. Grimes * must display the following acknowledgement: 189b50d902SRodney W. Grimes * This product includes software developed by the University of 199b50d902SRodney W. Grimes * California, Berkeley and its contributors. 209b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 219b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 229b50d902SRodney W. Grimes * without specific prior written permission. 239b50d902SRodney W. Grimes * 249b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 259b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 269b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 279b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 289b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 299b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 309b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 319b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 329b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 339b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 349b50d902SRodney W. Grimes * SUCH DAMAGE. 35fc17b349SJuli Mallett * 36fc17b349SJuli Mallett * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $ 379b50d902SRodney W. Grimes */ 389b50d902SRodney W. Grimes 39d7a43b24SJuli Mallett #ifndef lint 40d7a43b24SJuli Mallett static const char copyright[] = 41d7a43b24SJuli Mallett "@(#) Copyright (c) 1990, 1993\n\ 42d7a43b24SJuli Mallett The Regents of the University of California. All rights reserved.\n"; 43d7a43b24SJuli Mallett #endif /* not lint */ 44d7a43b24SJuli Mallett 45d7a43b24SJuli Mallett #if 0 46d7a43b24SJuli Mallett #ifndef lint 47d7a43b24SJuli Mallett static char sccsid[] = "@(#)xargs.c 8.1 (Berkeley) 6/6/93"; 48d7a43b24SJuli Mallett #endif /* not lint */ 49d7a43b24SJuli Mallett #endif 50d7a43b24SJuli Mallett 5151883012SMike Barcroft #include <sys/cdefs.h> 5251883012SMike Barcroft __FBSDID("$FreeBSD$"); 5351883012SMike Barcroft 549b50d902SRodney W. Grimes #include <sys/types.h> 559b50d902SRodney W. Grimes #include <sys/wait.h> 5616b07a33SMark Murray 57a51024e2SPhilippe Charnier #include <err.h> 588ad749a4SDag-Erling Smørgrav #include <errno.h> 599b50d902SRodney W. Grimes #include <stdio.h> 609b50d902SRodney W. Grimes #include <stdlib.h> 619b50d902SRodney W. Grimes #include <string.h> 629b50d902SRodney W. Grimes #include <unistd.h> 6316b07a33SMark Murray 649b50d902SRodney W. Grimes #include "pathnames.h" 659b50d902SRodney W. Grimes 66263dc775SJuli Mallett static void prerun(int, char **, int, const char *, char **, char **); 67fc17b349SJuli Mallett static void run(char **); 6873385ac6SJuli Mallett static void usage(void); 69fc17b349SJuli Mallett void strnsubst(char **, const char *, const char *, size_t); 709b50d902SRodney W. Grimes 7116b07a33SMark Murray static char echo[] = _PATH_ECHO; 72fc17b349SJuli Mallett static int pflag, tflag, rval, zflag; 7316b07a33SMark Murray 7473385ac6SJuli Mallett extern char *environ[]; 7573385ac6SJuli Mallett 76a51024e2SPhilippe Charnier int 7773385ac6SJuli Mallett main(int argc, char **argv) 789b50d902SRodney W. Grimes { 79a3e5bc4fSJoseph Koshy long arg_max; 804f49da74SJuli Mallett int ch, cnt, count, Iflag, indouble, insingle, Jflag, jfound, Lflag; 81b50a7286SJuli Mallett int nargs, nflag, nline, Rflag, wasquoted, foundeof, xflag; 82fc17b349SJuli Mallett size_t linelen; 83fc17b349SJuli Mallett const char *eofstr; 84fc17b349SJuli Mallett char **av, **avj, **bxp, **ep, **exp, **xp; 85fc17b349SJuli Mallett char *argp, *bbp, *ebp, *inpline, *p, *replstr; 869b50d902SRodney W. Grimes 8773385ac6SJuli Mallett ep = environ; 88fc17b349SJuli Mallett inpline = replstr = NULL; 89fc17b349SJuli Mallett eofstr = ""; 904f49da74SJuli Mallett cnt = count = Iflag = Jflag = jfound = Lflag = nflag = Rflag = xflag = 91b50a7286SJuli Mallett wasquoted = 0; 928d904f15SDima Dorfman 939b50d902SRodney W. Grimes /* 949b50d902SRodney W. Grimes * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that 959b50d902SRodney W. Grimes * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given 969b50d902SRodney W. Grimes * that the smallest argument is 2 bytes in length, this means that 979b50d902SRodney W. Grimes * the number of arguments is limited to: 989b50d902SRodney W. Grimes * 999b50d902SRodney W. Grimes * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2. 1009b50d902SRodney W. Grimes * 1019b50d902SRodney W. Grimes * We arbitrarily limit the number of arguments to 5000. This is 1029b50d902SRodney W. Grimes * allowed by POSIX.2 as long as the resulting minimum exec line is 1039b50d902SRodney W. Grimes * at least LINE_MAX. Realloc'ing as necessary is possible, but 1049b50d902SRodney W. Grimes * probably not worthwhile. 1059b50d902SRodney W. Grimes */ 1069b50d902SRodney W. Grimes nargs = 5000; 107a3e5bc4fSJoseph Koshy if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 108a3e5bc4fSJoseph Koshy errx(1, "sysconf(_SC_ARG_MAX) failed"); 109a3e5bc4fSJoseph Koshy nline = arg_max - 4 * 1024; 110e5009da0SSatoshi Asami while (*ep) { 111e5009da0SSatoshi Asami /* 1 byte for each '\0' */ 112e5009da0SSatoshi Asami nline -= strlen(*ep++) + 1 + sizeof(*ep); 113e5009da0SSatoshi Asami } 114b50a7286SJuli Mallett while ((ch = getopt(argc, argv, "0E:I:J:L:n:pR:s:tx")) != -1) 1159b50d902SRodney W. Grimes switch(ch) { 116fc17b349SJuli Mallett case 'E': 117fc17b349SJuli Mallett eofstr = optarg; 118fc17b349SJuli Mallett break; 119fc17b349SJuli Mallett case 'I': 120fc17b349SJuli Mallett Iflag = 1; 1214f49da74SJuli Mallett Lflag = 1; 122b50a7286SJuli Mallett Rflag = 5; 123fc17b349SJuli Mallett replstr = optarg; 124fc17b349SJuli Mallett break; 1258d904f15SDima Dorfman case 'J': 126b50a7286SJuli Mallett Jflag = 1; 1278d904f15SDima Dorfman replstr = optarg; 1288d904f15SDima Dorfman break; 129fc17b349SJuli Mallett case 'L': 1304f49da74SJuli Mallett Lflag = atoi(optarg); 131fc17b349SJuli Mallett break; 1329b50d902SRodney W. Grimes case 'n': 1339b50d902SRodney W. Grimes nflag = 1; 1349b50d902SRodney W. Grimes if ((nargs = atoi(optarg)) <= 0) 135a51024e2SPhilippe Charnier errx(1, "illegal argument count"); 1369b50d902SRodney W. Grimes break; 137fc17b349SJuli Mallett case 'p': 138fc17b349SJuli Mallett pflag = 1; 139fc17b349SJuli Mallett break; 140b50a7286SJuli Mallett case 'R': 141b50a7286SJuli Mallett if (!Iflag) 142b50a7286SJuli Mallett usage(); 143b50a7286SJuli Mallett if ((Rflag = atoi(optarg)) <= 0) 144b50a7286SJuli Mallett errx(1, "illegal number of replacements"); 145b50a7286SJuli Mallett break; 1469b50d902SRodney W. Grimes case 's': 1479b50d902SRodney W. Grimes nline = atoi(optarg); 1489b50d902SRodney W. Grimes break; 1499b50d902SRodney W. Grimes case 't': 1509b50d902SRodney W. Grimes tflag = 1; 1519b50d902SRodney W. Grimes break; 1529b50d902SRodney W. Grimes case 'x': 1539b50d902SRodney W. Grimes xflag = 1; 1549b50d902SRodney W. Grimes break; 155d9198881SWarner Losh case '0': 156d9198881SWarner Losh zflag = 1; 157d9198881SWarner Losh break; 1589b50d902SRodney W. Grimes case '?': 1599b50d902SRodney W. Grimes default: 1609b50d902SRodney W. Grimes usage(); 1619b50d902SRodney W. Grimes } 1629b50d902SRodney W. Grimes argc -= optind; 1639b50d902SRodney W. Grimes argv += optind; 1649b50d902SRodney W. Grimes 1659b50d902SRodney W. Grimes if (xflag && !nflag) 1669b50d902SRodney W. Grimes usage(); 1674f49da74SJuli Mallett if (Iflag || Lflag) 168fc17b349SJuli Mallett xflag = 1; 169fc17b349SJuli Mallett if (replstr != NULL && *replstr == '\0') 170fc17b349SJuli Mallett errx(1, "replstr may not be empty"); 1719b50d902SRodney W. Grimes 1729b50d902SRodney W. Grimes /* 1739b50d902SRodney W. Grimes * Allocate pointers for the utility name, the utility arguments, 1749b50d902SRodney W. Grimes * the maximum arguments to be read from stdin and the trailing 1759b50d902SRodney W. Grimes * NULL. 1769b50d902SRodney W. Grimes */ 177fc17b349SJuli Mallett linelen = 1 + argc + nargs + 1; 1780fa5e8dcSJuli Mallett if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL) 1790fa5e8dcSJuli Mallett err(1, "malloc"); 1809b50d902SRodney W. Grimes 1819b50d902SRodney W. Grimes /* 1829b50d902SRodney W. Grimes * Use the user's name for the utility as argv[0], just like the 1839b50d902SRodney W. Grimes * shell. Echo is the default. Set up pointers for the user's 1849b50d902SRodney W. Grimes * arguments. 1859b50d902SRodney W. Grimes */ 1869b50d902SRodney W. Grimes if (!*argv) 18716b07a33SMark Murray cnt = strlen((*bxp++ = echo)); 1889b50d902SRodney W. Grimes else { 1899b50d902SRodney W. Grimes do { 190b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 1918d904f15SDima Dorfman jfound = 1; 1928d904f15SDima Dorfman argv++; 1938d904f15SDima Dorfman for (avj = argv; *avj; avj++) 1948d904f15SDima Dorfman cnt += strlen(*avj) + 1; 1958d904f15SDima Dorfman break; 1968d904f15SDima Dorfman } 1979b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 1989b50d902SRodney W. Grimes } while (*++argv); 1999b50d902SRodney W. Grimes } 2009b50d902SRodney W. Grimes 2019b50d902SRodney W. Grimes /* 2029b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2039b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2049b50d902SRodney W. Grimes * added in an extra slot. 2059b50d902SRodney W. Grimes */ 2069b50d902SRodney W. Grimes exp = (xp = bxp) + nargs; 2079b50d902SRodney W. Grimes 2089b50d902SRodney W. Grimes /* 2099b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2109b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2119b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2129b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2139b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2149b50d902SRodney W. Grimes * slot. 2159b50d902SRodney W. Grimes */ 2169b50d902SRodney W. Grimes nline -= cnt; 2179b50d902SRodney W. Grimes if (nline <= 0) 218a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2199b50d902SRodney W. Grimes 22076ccb81eSJuli Mallett if ((bbp = malloc((size_t)nline + 1)) == NULL) 221fc17b349SJuli Mallett err(1, "malloc"); 2229b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 2239b50d902SRodney W. Grimes 2249b50d902SRodney W. Grimes for (insingle = indouble = 0;;) 2259b50d902SRodney W. Grimes switch(ch = getchar()) { 2269b50d902SRodney W. Grimes case EOF: 2279b50d902SRodney W. Grimes /* No arguments since last exec. */ 2289b50d902SRodney W. Grimes if (p == bbp) 2299b50d902SRodney W. Grimes exit(rval); 2309b50d902SRodney W. Grimes goto arg1; 2319b50d902SRodney W. Grimes case ' ': 2329b50d902SRodney W. Grimes case '\t': 2339b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 234d9198881SWarner Losh if (insingle || indouble || zflag) 2359b50d902SRodney W. Grimes goto addch; 2369b50d902SRodney W. Grimes goto arg2; 237d9198881SWarner Losh case '\0': 238d9198881SWarner Losh if (zflag) 239d9198881SWarner Losh goto arg2; 240d9198881SWarner Losh goto addch; 2419b50d902SRodney W. Grimes case '\n': 242fc17b349SJuli Mallett count++; 243d9198881SWarner Losh if (zflag) 244d9198881SWarner Losh goto addch; 245d9198881SWarner Losh 2469b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 2479b50d902SRodney W. Grimes arg1: if (insingle || indouble) 248a51024e2SPhilippe Charnier errx(1, "unterminated quote"); 2499b50d902SRodney W. Grimes 250ef9866beSJean-Marc Zucconi arg2: 251fc17b349SJuli Mallett foundeof = *eofstr != '\0' && 252fc17b349SJuli Mallett strcmp(argp, eofstr) == 0; 253fc17b349SJuli Mallett 254ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 255fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 256ef9866beSJean-Marc Zucconi *p++ = '\0'; 2579b50d902SRodney W. Grimes *xp++ = argp; 258fc17b349SJuli Mallett if (Iflag) { 259fc17b349SJuli Mallett size_t curlen; 260b9b03ba0SJuli Mallett 261b9b03ba0SJuli Mallett if (inpline == NULL) 262fc17b349SJuli Mallett curlen = 0; 263fc17b349SJuli Mallett else { 2641925cb24SJuli Mallett /* 2651925cb24SJuli Mallett * If this string is not zero 2661925cb24SJuli Mallett * length, append a space for 2671925cb24SJuli Mallett * seperation before the next 2681925cb24SJuli Mallett * argument. 2691925cb24SJuli Mallett */ 270b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 271fc17b349SJuli Mallett strcat(inpline, " "); 272fc17b349SJuli Mallett } 273fc17b349SJuli Mallett curlen++; 2741925cb24SJuli Mallett /* 2751925cb24SJuli Mallett * Allocate enough to hold what we will 2761925cb24SJuli Mallett * be holding in a secont, and to append 2771925cb24SJuli Mallett * a space next time through, if we have 2781925cb24SJuli Mallett * to. 2791925cb24SJuli Mallett */ 280b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 281b9b03ba0SJuli Mallett strlen(argp)); 282fc17b349SJuli Mallett if (inpline == NULL) 283fc17b349SJuli Mallett err(1, "realloc"); 284fc17b349SJuli Mallett if (curlen == 1) 285fc17b349SJuli Mallett strcpy(inpline, argp); 286fc17b349SJuli Mallett else 287fc17b349SJuli Mallett strcat(inpline, argp); 288fc17b349SJuli Mallett } 289ef9866beSJean-Marc Zucconi } 2909b50d902SRodney W. Grimes 2919b50d902SRodney W. Grimes /* 2929b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 2939b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 2941925cb24SJuli Mallett * but not on args, object. Having reached the limit 2951925cb24SJuli Mallett * of input lines, as specified by -L is the same as 2961925cb24SJuli Mallett * maxing out on arguments. 2979b50d902SRodney W. Grimes */ 2984f49da74SJuli Mallett if (xp == exp || p > ebp || ch == EOF || (Lflag <= count && xflag) || foundeof) { 299ef9866beSJean-Marc Zucconi if (xflag && xp != exp && p > ebp) 300a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3018d904f15SDima Dorfman if (jfound) { 3028d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3038d904f15SDima Dorfman *xp++ = *avj; 3048d904f15SDima Dorfman } 305263dc775SJuli Mallett prerun(argc, av, Rflag, replstr, xp, &inpline); 306fc17b349SJuli Mallett if (ch == EOF || foundeof) 3079b50d902SRodney W. Grimes exit(rval); 3089b50d902SRodney W. Grimes p = bbp; 3099b50d902SRodney W. Grimes xp = bxp; 310fc17b349SJuli Mallett count = 0; 311ef9866beSJean-Marc Zucconi } 3129b50d902SRodney W. Grimes argp = p; 313ef9866beSJean-Marc Zucconi wasquoted = 0; 3149b50d902SRodney W. Grimes break; 3159b50d902SRodney W. Grimes case '\'': 316d9198881SWarner Losh if (indouble || zflag) 3179b50d902SRodney W. Grimes goto addch; 3189b50d902SRodney W. Grimes insingle = !insingle; 319ef9866beSJean-Marc Zucconi wasquoted = 1; 3209b50d902SRodney W. Grimes break; 3219b50d902SRodney W. Grimes case '"': 322d9198881SWarner Losh if (insingle || zflag) 3239b50d902SRodney W. Grimes goto addch; 3249b50d902SRodney W. Grimes indouble = !indouble; 325ef9866beSJean-Marc Zucconi wasquoted = 1; 3269b50d902SRodney W. Grimes break; 3279b50d902SRodney W. Grimes case '\\': 328d9198881SWarner Losh if (zflag) 329d9198881SWarner Losh goto addch; 3309b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 3319b50d902SRodney W. Grimes if (!insingle && !indouble && (ch = getchar()) == EOF) 332a51024e2SPhilippe Charnier errx(1, "backslash at EOF"); 3339b50d902SRodney W. Grimes /* FALLTHROUGH */ 3349b50d902SRodney W. Grimes default: 3359b50d902SRodney W. Grimes addch: if (p < ebp) { 3369b50d902SRodney W. Grimes *p++ = ch; 3379b50d902SRodney W. Grimes break; 3389b50d902SRodney W. Grimes } 3399b50d902SRodney W. Grimes 3409b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 3419b50d902SRodney W. Grimes if (bxp == xp) 342a51024e2SPhilippe Charnier errx(1, "insufficient space for argument"); 3439b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 3449b50d902SRodney W. Grimes if (xflag) 345a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3469b50d902SRodney W. Grimes 3478d904f15SDima Dorfman if (jfound) { 3488d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3498d904f15SDima Dorfman *xp++ = *avj; 3508d904f15SDima Dorfman } 351263dc775SJuli Mallett prerun(argc, av, Rflag, replstr, xp, &inpline); 3529b50d902SRodney W. Grimes xp = bxp; 3539b50d902SRodney W. Grimes cnt = ebp - argp; 354fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 3559b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 3569b50d902SRodney W. Grimes *p++ = ch; 3579b50d902SRodney W. Grimes break; 3589b50d902SRodney W. Grimes } 3599b50d902SRodney W. Grimes /* NOTREACHED */ 3609b50d902SRodney W. Grimes } 3619b50d902SRodney W. Grimes 362263dc775SJuli Mallett /* 363263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 364263dc775SJuli Mallett * and then call run(). 365263dc775SJuli Mallett */ 366263dc775SJuli Mallett static void 367263dc775SJuli Mallett prerun(int argc, char **argv, int repls, const char *replstr, char **xp, char **inpline) 368263dc775SJuli Mallett { 369263dc775SJuli Mallett char **tmp, **tmp2, **avj; 370263dc775SJuli Mallett 371263dc775SJuli Mallett if (repls == 0) { 372263dc775SJuli Mallett *xp = NULL; 373263dc775SJuli Mallett run(argv); 374263dc775SJuli Mallett return; 375263dc775SJuli Mallett } 376263dc775SJuli Mallett 377263dc775SJuli Mallett avj = argv; 378263dc775SJuli Mallett 379263dc775SJuli Mallett /* 380263dc775SJuli Mallett * Allocate memory to hold the argument list, and 381263dc775SJuli Mallett * a NULL at the tail. 382263dc775SJuli Mallett */ 383b6594dbaSJuli Mallett tmp = malloc((argc + 1) * sizeof(char**)); 384263dc775SJuli Mallett if (tmp == NULL) 385b6594dbaSJuli Mallett err(1, "malloc"); 386263dc775SJuli Mallett tmp2 = tmp; 387263dc775SJuli Mallett 388263dc775SJuli Mallett /* 389263dc775SJuli Mallett * Save the first argument and iterate over it, we 390263dc775SJuli Mallett * cannot do strnsubst() to it. 391263dc775SJuli Mallett */ 392263dc775SJuli Mallett if ((*tmp++ = strdup(*avj++)) == NULL) 393263dc775SJuli Mallett err(1, "strdup"); 394263dc775SJuli Mallett 395263dc775SJuli Mallett /* 396263dc775SJuli Mallett * For each argument to utility, if we have not used up 397263dc775SJuli Mallett * the number of replacements we are allowed to do, and 398263dc775SJuli Mallett * if the argument contains at least one occurance of 399263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 400263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 401263dc775SJuli Mallett * where appropriate. 402263dc775SJuli Mallett */ 403263dc775SJuli Mallett while (--argc) { 404263dc775SJuli Mallett *tmp = *avj++; 405263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 406263dc775SJuli Mallett strnsubst(tmp++, replstr, *inpline, (size_t)255); 407263dc775SJuli Mallett repls--; 408263dc775SJuli Mallett } else { 409263dc775SJuli Mallett if ((*tmp = strdup(*tmp)) == NULL) 410263dc775SJuli Mallett err(1, "strdup"); 411263dc775SJuli Mallett tmp++; 412263dc775SJuli Mallett } 413263dc775SJuli Mallett } 414263dc775SJuli Mallett 415263dc775SJuli Mallett /* 416263dc775SJuli Mallett * Run it. 417263dc775SJuli Mallett */ 418b6594dbaSJuli Mallett *tmp = NULL; 419263dc775SJuli Mallett run(tmp2); 420263dc775SJuli Mallett 421263dc775SJuli Mallett /* 422263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 423263dc775SJuli Mallett */ 424263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 425263dc775SJuli Mallett free(*tmp); 426263dc775SJuli Mallett /* 427263dc775SJuli Mallett * Now free the list itself. 428263dc775SJuli Mallett */ 429263dc775SJuli Mallett free(tmp2); 430263dc775SJuli Mallett 431263dc775SJuli Mallett /* 432263dc775SJuli Mallett * Free the input line buffer, and create a new dummy. 433263dc775SJuli Mallett */ 434263dc775SJuli Mallett free(*inpline); 435263dc775SJuli Mallett *inpline = strdup(""); 436263dc775SJuli Mallett } 437263dc775SJuli Mallett 438fc17b349SJuli Mallett static void 43973385ac6SJuli Mallett run(char **argv) 4409b50d902SRodney W. Grimes { 4418ad749a4SDag-Erling Smørgrav volatile int childerr; 44216b07a33SMark Murray char **p; 443fc17b349SJuli Mallett FILE *ttyfp; 4449b50d902SRodney W. Grimes pid_t pid; 445fc17b349SJuli Mallett int ch, status; 4469b50d902SRodney W. Grimes 447fc17b349SJuli Mallett if (tflag || pflag) { 4489b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 4499b50d902SRodney W. Grimes for (p = argv + 1; *p; ++p) 4509b50d902SRodney W. Grimes (void)fprintf(stderr, " %s", *p); 45191ae52cbSJuli Mallett if (pflag && (ttyfp = fopen("/dev/tty", "r")) != NULL) { 452fc17b349SJuli Mallett (void)fprintf(stderr, "?"); 453fc17b349SJuli Mallett (void)fflush(stderr); 454fc17b349SJuli Mallett ch = getc(ttyfp); 455fc17b349SJuli Mallett fclose(ttyfp); 456fc17b349SJuli Mallett if (ch != 'y') 457fc17b349SJuli Mallett return; 458fc17b349SJuli Mallett } else { 4599b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 4609b50d902SRodney W. Grimes (void)fflush(stderr); 4619b50d902SRodney W. Grimes } 462fc17b349SJuli Mallett } 4638ad749a4SDag-Erling Smørgrav childerr = 0; 4648ad749a4SDag-Erling Smørgrav switch(pid = vfork()) { 4659b50d902SRodney W. Grimes case -1: 4668ad749a4SDag-Erling Smørgrav err(1, "vfork"); 4679b50d902SRodney W. Grimes case 0: 4689b50d902SRodney W. Grimes execvp(argv[0], argv); 4698ad749a4SDag-Erling Smørgrav childerr = errno; 4709b50d902SRodney W. Grimes _exit(1); 4719b50d902SRodney W. Grimes } 4729b50d902SRodney W. Grimes pid = waitpid(pid, &status, 0); 4739b50d902SRodney W. Grimes if (pid == -1) 474a51024e2SPhilippe Charnier err(1, "waitpid"); 475fc17b349SJuli Mallett /* If we couldn't invoke the utility, exit. */ 476fc17b349SJuli Mallett if (childerr != 0) 477fc17b349SJuli Mallett err(childerr == ENOENT ? 127 : 126, "%s", *argv); 4789b50d902SRodney W. Grimes /* If utility signaled or exited with a value of 255, exit 1-125. */ 4799b50d902SRodney W. Grimes if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255) 4809b50d902SRodney W. Grimes exit(1); 4819b50d902SRodney W. Grimes if (WEXITSTATUS(status)) 4829b50d902SRodney W. Grimes rval = 1; 4839b50d902SRodney W. Grimes } 4849b50d902SRodney W. Grimes 485a51024e2SPhilippe Charnier static void 48673385ac6SJuli Mallett usage(void) 4879b50d902SRodney W. Grimes { 4888d904f15SDima Dorfman fprintf(stderr, 489b50a7286SJuli Mallett "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n" 490b50a7286SJuli Mallett " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n"); 4919b50d902SRodney W. Grimes exit(1); 4929b50d902SRodney W. Grimes } 493