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 6691045075SJuli Mallett static void parse_input(int, char **); 6791045075SJuli Mallett static void prerun(int, char **); 68fc17b349SJuli Mallett static void run(char **); 6973385ac6SJuli Mallett static void usage(void); 70fc17b349SJuli Mallett void strnsubst(char **, const char *, const char *, size_t); 719b50d902SRodney W. Grimes 7216b07a33SMark Murray static char echo[] = _PATH_ECHO; 7391045075SJuli Mallett static char **av, **bxp, **ep, **exp, **xp; 7491045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr; 7591045075SJuli Mallett static const char *eofstr; 7691045075SJuli Mallett static int count, insingle, indouble, pflag, tflag, Rflag, rval, zflag; 7791045075SJuli Mallett static int cnt, Iflag, jfound, Lflag, wasquoted, xflag; 7816b07a33SMark Murray 7973385ac6SJuli Mallett extern char *environ[]; 8073385ac6SJuli Mallett 81a51024e2SPhilippe Charnier int 8273385ac6SJuli Mallett main(int argc, char **argv) 839b50d902SRodney W. Grimes { 84a3e5bc4fSJoseph Koshy long arg_max; 8591045075SJuli Mallett int ch, Jflag, nargs, nflag, nline; 86fc17b349SJuli Mallett size_t linelen; 879b50d902SRodney W. Grimes 88fc17b349SJuli Mallett inpline = replstr = NULL; 8991045075SJuli Mallett ep = environ; 90fc17b349SJuli Mallett eofstr = ""; 9191045075SJuli Mallett Jflag = nflag = 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; 11091045075SJuli Mallett while (*ep != NULL) { 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) 17991045075SJuli Mallett errx(1, "malloc failed"); 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 */ 1863dca1afcSJuli Mallett if (*argv == NULL) 1879bf450b8SJuli Mallett cnt = strlen(*bxp++ = echo); 1889b50d902SRodney W. Grimes else { 1899b50d902SRodney W. Grimes do { 190b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 19191045075SJuli Mallett char **avj; 1928d904f15SDima Dorfman jfound = 1; 1938d904f15SDima Dorfman argv++; 1948d904f15SDima Dorfman for (avj = argv; *avj; avj++) 1958d904f15SDima Dorfman cnt += strlen(*avj) + 1; 1968d904f15SDima Dorfman break; 1978d904f15SDima Dorfman } 1989b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 1993dca1afcSJuli Mallett } while (*++argv != NULL); 2009b50d902SRodney W. Grimes } 2019b50d902SRodney W. Grimes 2029b50d902SRodney W. Grimes /* 2039b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2049b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2059b50d902SRodney W. Grimes * added in an extra slot. 2069b50d902SRodney W. Grimes */ 2079b50d902SRodney W. Grimes exp = (xp = bxp) + nargs; 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes /* 2109b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2119b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2129b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2139b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2149b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2159b50d902SRodney W. Grimes * slot. 2169b50d902SRodney W. Grimes */ 2179b50d902SRodney W. Grimes nline -= cnt; 2189b50d902SRodney W. Grimes if (nline <= 0) 219a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2209b50d902SRodney W. Grimes 2219bf450b8SJuli Mallett if ((bbp = malloc((size_t)(nline + 1))) == NULL) 22291045075SJuli Mallett errx(1, "malloc failed"); 2239b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 22491045075SJuli Mallett for (;;) 22591045075SJuli Mallett parse_input(argc, argv); 22691045075SJuli Mallett } 2279b50d902SRodney W. Grimes 22891045075SJuli Mallett static void 22991045075SJuli Mallett parse_input(int argc, char **argv) 23091045075SJuli Mallett { 23191045075SJuli Mallett int ch, foundeof; 23291045075SJuli Mallett char **avj; 23391045075SJuli Mallett 23491045075SJuli Mallett foundeof = 0; 23591045075SJuli Mallett 2369b50d902SRodney W. Grimes switch(ch = getchar()) { 2379b50d902SRodney W. Grimes case EOF: 2389b50d902SRodney W. Grimes /* No arguments since last exec. */ 2399b50d902SRodney W. Grimes if (p == bbp) 2409b50d902SRodney W. Grimes exit(rval); 2419b50d902SRodney W. Grimes goto arg1; 2429b50d902SRodney W. Grimes case ' ': 2439b50d902SRodney W. Grimes case '\t': 2449b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 245d9198881SWarner Losh if (insingle || indouble || zflag) 2469b50d902SRodney W. Grimes goto addch; 2479b50d902SRodney W. Grimes goto arg2; 248d9198881SWarner Losh case '\0': 249d9198881SWarner Losh if (zflag) 250d9198881SWarner Losh goto arg2; 251d9198881SWarner Losh goto addch; 2529b50d902SRodney W. Grimes case '\n': 253fc17b349SJuli Mallett count++; 254d9198881SWarner Losh if (zflag) 255d9198881SWarner Losh goto addch; 256d9198881SWarner Losh 2579b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 2589b50d902SRodney W. Grimes arg1: if (insingle || indouble) 259a51024e2SPhilippe Charnier errx(1, "unterminated quote"); 260ef9866beSJean-Marc Zucconi arg2: 261fc17b349SJuli Mallett foundeof = *eofstr != '\0' && 262fc17b349SJuli Mallett strcmp(argp, eofstr) == 0; 263fc17b349SJuli Mallett 264ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 265fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 266ef9866beSJean-Marc Zucconi *p++ = '\0'; 2679b50d902SRodney W. Grimes *xp++ = argp; 268fc17b349SJuli Mallett if (Iflag) { 269fc17b349SJuli Mallett size_t curlen; 270b9b03ba0SJuli Mallett 271b9b03ba0SJuli Mallett if (inpline == NULL) 272fc17b349SJuli Mallett curlen = 0; 273fc17b349SJuli Mallett else { 2741925cb24SJuli Mallett /* 2751925cb24SJuli Mallett * If this string is not zero 2761925cb24SJuli Mallett * length, append a space for 2771925cb24SJuli Mallett * seperation before the next 2781925cb24SJuli Mallett * argument. 2791925cb24SJuli Mallett */ 280b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 281fc17b349SJuli Mallett strcat(inpline, " "); 282fc17b349SJuli Mallett } 283fc17b349SJuli Mallett curlen++; 2841925cb24SJuli Mallett /* 2851925cb24SJuli Mallett * Allocate enough to hold what we will 2865eb40323SJuli Mallett * be holding in a second, and to append 2871925cb24SJuli Mallett * a space next time through, if we have 2881925cb24SJuli Mallett * to. 2891925cb24SJuli Mallett */ 290b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 291b9b03ba0SJuli Mallett strlen(argp)); 292fc17b349SJuli Mallett if (inpline == NULL) 29391045075SJuli Mallett errx(1, "realloc failed"); 294fc17b349SJuli Mallett if (curlen == 1) 295fc17b349SJuli Mallett strcpy(inpline, argp); 296fc17b349SJuli Mallett else 297fc17b349SJuli Mallett strcat(inpline, argp); 298fc17b349SJuli Mallett } 299ef9866beSJean-Marc Zucconi } 3009b50d902SRodney W. Grimes 3019b50d902SRodney W. Grimes /* 3029b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 3039b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 3041925cb24SJuli Mallett * but not on args, object. Having reached the limit 3051925cb24SJuli Mallett * of input lines, as specified by -L is the same as 3061925cb24SJuli Mallett * maxing out on arguments. 3079b50d902SRodney W. Grimes */ 3085eb40323SJuli Mallett if (xp == exp || p > ebp || ch == EOF || 3095eb40323SJuli Mallett (Lflag <= count && xflag) || foundeof) { 310ef9866beSJean-Marc Zucconi if (xflag && xp != exp && p > ebp) 311a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3128d904f15SDima Dorfman if (jfound) { 3138d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3148d904f15SDima Dorfman *xp++ = *avj; 3158d904f15SDima Dorfman } 31691045075SJuli Mallett prerun(argc, av); 317fc17b349SJuli Mallett if (ch == EOF || foundeof) 3189b50d902SRodney W. Grimes exit(rval); 3199b50d902SRodney W. Grimes p = bbp; 3209b50d902SRodney W. Grimes xp = bxp; 321fc17b349SJuli Mallett count = 0; 322ef9866beSJean-Marc Zucconi } 3239b50d902SRodney W. Grimes argp = p; 324ef9866beSJean-Marc Zucconi wasquoted = 0; 3259b50d902SRodney W. Grimes break; 3269b50d902SRodney W. Grimes case '\'': 327d9198881SWarner Losh if (indouble || zflag) 3289b50d902SRodney W. Grimes goto addch; 3299b50d902SRodney W. Grimes insingle = !insingle; 330ef9866beSJean-Marc Zucconi wasquoted = 1; 3319b50d902SRodney W. Grimes break; 3329b50d902SRodney W. Grimes case '"': 333d9198881SWarner Losh if (insingle || zflag) 3349b50d902SRodney W. Grimes goto addch; 3359b50d902SRodney W. Grimes indouble = !indouble; 336ef9866beSJean-Marc Zucconi wasquoted = 1; 3379b50d902SRodney W. Grimes break; 3389b50d902SRodney W. Grimes case '\\': 339d9198881SWarner Losh if (zflag) 340d9198881SWarner Losh goto addch; 3419b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 3429b50d902SRodney W. Grimes if (!insingle && !indouble && (ch = getchar()) == EOF) 343a51024e2SPhilippe Charnier errx(1, "backslash at EOF"); 3449b50d902SRodney W. Grimes /* FALLTHROUGH */ 3459b50d902SRodney W. Grimes default: 3469b50d902SRodney W. Grimes addch: if (p < ebp) { 3479b50d902SRodney W. Grimes *p++ = ch; 3489b50d902SRodney W. Grimes break; 3499b50d902SRodney W. Grimes } 3509b50d902SRodney W. Grimes 3519b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 3529b50d902SRodney W. Grimes if (bxp == xp) 353a51024e2SPhilippe Charnier errx(1, "insufficient space for argument"); 3549b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 3559b50d902SRodney W. Grimes if (xflag) 356a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3579b50d902SRodney W. Grimes 3588d904f15SDima Dorfman if (jfound) { 3598d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3608d904f15SDima Dorfman *xp++ = *avj; 3618d904f15SDima Dorfman } 36291045075SJuli Mallett prerun(argc, av); 3639b50d902SRodney W. Grimes xp = bxp; 3649b50d902SRodney W. Grimes cnt = ebp - argp; 365fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 3669b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 3679b50d902SRodney W. Grimes *p++ = ch; 3689b50d902SRodney W. Grimes break; 3699b50d902SRodney W. Grimes } 37091045075SJuli Mallett return; 3719b50d902SRodney W. Grimes } 3729b50d902SRodney W. Grimes 373263dc775SJuli Mallett /* 374263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 375263dc775SJuli Mallett * and then call run(). 376263dc775SJuli Mallett */ 377263dc775SJuli Mallett static void 37891045075SJuli Mallett prerun(int argc, char **argv) 379263dc775SJuli Mallett { 380263dc775SJuli Mallett char **tmp, **tmp2, **avj; 38191045075SJuli Mallett int repls; 38291045075SJuli Mallett 38391045075SJuli Mallett repls = Rflag; 384263dc775SJuli Mallett 385263dc775SJuli Mallett if (repls == 0) { 386263dc775SJuli Mallett *xp = NULL; 387263dc775SJuli Mallett run(argv); 388263dc775SJuli Mallett return; 389263dc775SJuli Mallett } 390263dc775SJuli Mallett 391263dc775SJuli Mallett avj = argv; 392263dc775SJuli Mallett 393263dc775SJuli Mallett /* 394263dc775SJuli Mallett * Allocate memory to hold the argument list, and 395263dc775SJuli Mallett * a NULL at the tail. 396263dc775SJuli Mallett */ 397b6594dbaSJuli Mallett tmp = malloc((argc + 1) * sizeof(char**)); 398263dc775SJuli Mallett if (tmp == NULL) 39991045075SJuli Mallett errx(1, "malloc failed"); 400263dc775SJuli Mallett tmp2 = tmp; 401263dc775SJuli Mallett 402263dc775SJuli Mallett /* 403263dc775SJuli Mallett * Save the first argument and iterate over it, we 404263dc775SJuli Mallett * cannot do strnsubst() to it. 405263dc775SJuli Mallett */ 406263dc775SJuli Mallett if ((*tmp++ = strdup(*avj++)) == NULL) 40791045075SJuli Mallett errx(1, "strdup failed"); 408263dc775SJuli Mallett 409263dc775SJuli Mallett /* 410263dc775SJuli Mallett * For each argument to utility, if we have not used up 411263dc775SJuli Mallett * the number of replacements we are allowed to do, and 412263dc775SJuli Mallett * if the argument contains at least one occurance of 413263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 414263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 415263dc775SJuli Mallett * where appropriate. 416263dc775SJuli Mallett */ 417263dc775SJuli Mallett while (--argc) { 418263dc775SJuli Mallett *tmp = *avj++; 419263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 42091045075SJuli Mallett strnsubst(tmp++, replstr, inpline, (size_t)255); 421263dc775SJuli Mallett repls--; 422263dc775SJuli Mallett } else { 423263dc775SJuli Mallett if ((*tmp = strdup(*tmp)) == NULL) 42491045075SJuli Mallett errx(1, "strdup failed"); 425263dc775SJuli Mallett tmp++; 426263dc775SJuli Mallett } 427263dc775SJuli Mallett } 428263dc775SJuli Mallett 429263dc775SJuli Mallett /* 430263dc775SJuli Mallett * Run it. 431263dc775SJuli Mallett */ 432b6594dbaSJuli Mallett *tmp = NULL; 433263dc775SJuli Mallett run(tmp2); 434263dc775SJuli Mallett 435263dc775SJuli Mallett /* 436263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 437263dc775SJuli Mallett */ 438263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 439263dc775SJuli Mallett free(*tmp); 440263dc775SJuli Mallett /* 441263dc775SJuli Mallett * Now free the list itself. 442263dc775SJuli Mallett */ 443263dc775SJuli Mallett free(tmp2); 444263dc775SJuli Mallett 445263dc775SJuli Mallett /* 446263dc775SJuli Mallett * Free the input line buffer, and create a new dummy. 447263dc775SJuli Mallett */ 44891045075SJuli Mallett free(inpline); 44991045075SJuli Mallett inpline = strdup(""); 450263dc775SJuli Mallett } 451263dc775SJuli Mallett 452fc17b349SJuli Mallett static void 45373385ac6SJuli Mallett run(char **argv) 4549b50d902SRodney W. Grimes { 4558ad749a4SDag-Erling Smørgrav volatile int childerr; 45691045075SJuli Mallett char **avec; 457fc17b349SJuli Mallett FILE *ttyfp; 4589b50d902SRodney W. Grimes pid_t pid; 459fc17b349SJuli Mallett int ch, status; 4609b50d902SRodney W. Grimes 461fc17b349SJuli Mallett if (tflag || pflag) { 4629b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 46391045075SJuli Mallett for (avec = argv + 1; *avec != NULL; ++avec) 46491045075SJuli Mallett (void)fprintf(stderr, " %s", *avec); 46591ae52cbSJuli Mallett if (pflag && (ttyfp = fopen("/dev/tty", "r")) != NULL) { 466fc17b349SJuli Mallett (void)fprintf(stderr, "?"); 467fc17b349SJuli Mallett (void)fflush(stderr); 468fc17b349SJuli Mallett ch = getc(ttyfp); 469fc17b349SJuli Mallett fclose(ttyfp); 470fc17b349SJuli Mallett if (ch != 'y') 471fc17b349SJuli Mallett return; 472fc17b349SJuli Mallett } else { 4739b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 4749b50d902SRodney W. Grimes (void)fflush(stderr); 4759b50d902SRodney W. Grimes } 476fc17b349SJuli Mallett } 4778ad749a4SDag-Erling Smørgrav childerr = 0; 4788ad749a4SDag-Erling Smørgrav switch(pid = vfork()) { 4799b50d902SRodney W. Grimes case -1: 4808ad749a4SDag-Erling Smørgrav err(1, "vfork"); 4819b50d902SRodney W. Grimes case 0: 4829b50d902SRodney W. Grimes execvp(argv[0], argv); 4838ad749a4SDag-Erling Smørgrav childerr = errno; 4849b50d902SRodney W. Grimes _exit(1); 4859b50d902SRodney W. Grimes } 4869b50d902SRodney W. Grimes pid = waitpid(pid, &status, 0); 4879b50d902SRodney W. Grimes if (pid == -1) 488a51024e2SPhilippe Charnier err(1, "waitpid"); 489fc17b349SJuli Mallett /* If we couldn't invoke the utility, exit. */ 490fc17b349SJuli Mallett if (childerr != 0) 491fc17b349SJuli Mallett err(childerr == ENOENT ? 127 : 126, "%s", *argv); 4929b50d902SRodney W. Grimes /* If utility signaled or exited with a value of 255, exit 1-125. */ 4939b50d902SRodney W. Grimes if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255) 4949b50d902SRodney W. Grimes exit(1); 4959b50d902SRodney W. Grimes if (WEXITSTATUS(status)) 4969b50d902SRodney W. Grimes rval = 1; 4979b50d902SRodney W. Grimes } 4989b50d902SRodney W. Grimes 499a51024e2SPhilippe Charnier static void 50073385ac6SJuli Mallett usage(void) 5019b50d902SRodney W. Grimes { 5028d904f15SDima Dorfman fprintf(stderr, 503b50a7286SJuli Mallett "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n" 504b50a7286SJuli Mallett " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n"); 5059b50d902SRodney W. Grimes exit(1); 5069b50d902SRodney W. Grimes } 507