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 398eb2a3deSDavid E. O'Brien #if 0 40d7a43b24SJuli Mallett #ifndef lint 41d7a43b24SJuli Mallett static const char copyright[] = 42d7a43b24SJuli Mallett "@(#) Copyright (c) 1990, 1993\n\ 43d7a43b24SJuli Mallett The Regents of the University of California. All rights reserved.\n"; 44d7a43b24SJuli Mallett #endif /* not lint */ 45d7a43b24SJuli Mallett 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 5051883012SMike Barcroft #include <sys/cdefs.h> 5151883012SMike Barcroft __FBSDID("$FreeBSD$"); 5251883012SMike Barcroft 5340c6b893SRuslan Ermilov #include <sys/param.h> 549b50d902SRodney W. Grimes #include <sys/wait.h> 5516b07a33SMark Murray 56a51024e2SPhilippe Charnier #include <err.h> 578ad749a4SDag-Erling Smørgrav #include <errno.h> 5898186e89SMaxime Henrion #include <fcntl.h> 59305e39f4SJuli Mallett #include <langinfo.h> 60305e39f4SJuli Mallett #include <locale.h> 61305e39f4SJuli Mallett #include <paths.h> 62305e39f4SJuli Mallett #include <regex.h> 639b50d902SRodney W. Grimes #include <stdio.h> 649b50d902SRodney W. Grimes #include <stdlib.h> 659b50d902SRodney W. Grimes #include <string.h> 669b50d902SRodney W. Grimes #include <unistd.h> 6716b07a33SMark Murray 689b50d902SRodney W. Grimes #include "pathnames.h" 699b50d902SRodney W. Grimes 701926d4aaSJuli Mallett static void parse_input(int, char *[]); 711926d4aaSJuli Mallett static void prerun(int, char *[]); 72305e39f4SJuli Mallett static int prompt(void); 73fc17b349SJuli Mallett static void run(char **); 7473385ac6SJuli Mallett static void usage(void); 75fc17b349SJuli Mallett void strnsubst(char **, const char *, const char *, size_t); 76330d23f5STim J. Robbins static void waitchildren(const char *, int); 779b50d902SRodney W. Grimes 7816b07a33SMark Murray static char echo[] = _PATH_ECHO; 798eb2a3deSDavid E. O'Brien static char **av, **bxp, **ep, **endxp, **xp; 8091045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr; 8191045075SJuli Mallett static const char *eofstr; 8298186e89SMaxime Henrion static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag; 83ba084f6aSJuli Mallett static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag; 84330d23f5STim J. Robbins static int curprocs, maxprocs; 85330d23f5STim J. Robbins 86330d23f5STim J. Robbins static volatile int childerr; 8716b07a33SMark Murray 8899a84ce1STim J. Robbins extern char **environ; 8973385ac6SJuli Mallett 90a51024e2SPhilippe Charnier int 911926d4aaSJuli Mallett main(int argc, char *argv[]) 929b50d902SRodney W. Grimes { 93a3e5bc4fSJoseph Koshy long arg_max; 9491045075SJuli Mallett int ch, Jflag, nargs, nflag, nline; 95fc17b349SJuli Mallett size_t linelen; 963c675167SJuli Mallett char *endptr; 979b50d902SRodney W. Grimes 98fc17b349SJuli Mallett inpline = replstr = NULL; 9991045075SJuli Mallett ep = environ; 100fc17b349SJuli Mallett eofstr = ""; 10191045075SJuli Mallett Jflag = nflag = 0; 1028d904f15SDima Dorfman 103f58b94cbSTim J. Robbins (void)setlocale(LC_ALL, ""); 104305e39f4SJuli Mallett 1059b50d902SRodney W. Grimes /* 1069b50d902SRodney W. Grimes * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that 1079b50d902SRodney W. Grimes * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given 1089b50d902SRodney W. Grimes * that the smallest argument is 2 bytes in length, this means that 1099b50d902SRodney W. Grimes * the number of arguments is limited to: 1109b50d902SRodney W. Grimes * 1119b50d902SRodney W. Grimes * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2. 1129b50d902SRodney W. Grimes * 1139b50d902SRodney W. Grimes * We arbitrarily limit the number of arguments to 5000. This is 1149b50d902SRodney W. Grimes * allowed by POSIX.2 as long as the resulting minimum exec line is 1159b50d902SRodney W. Grimes * at least LINE_MAX. Realloc'ing as necessary is possible, but 1169b50d902SRodney W. Grimes * probably not worthwhile. 1179b50d902SRodney W. Grimes */ 1189b50d902SRodney W. Grimes nargs = 5000; 119a3e5bc4fSJoseph Koshy if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 120a3e5bc4fSJoseph Koshy errx(1, "sysconf(_SC_ARG_MAX) failed"); 121a3e5bc4fSJoseph Koshy nline = arg_max - 4 * 1024; 12291045075SJuli Mallett while (*ep != NULL) { 123e5009da0SSatoshi Asami /* 1 byte for each '\0' */ 124e5009da0SSatoshi Asami nline -= strlen(*ep++) + 1 + sizeof(*ep); 125e5009da0SSatoshi Asami } 126330d23f5STim J. Robbins maxprocs = 1; 127ba084f6aSJuli Mallett while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:S:s:rtx")) != -1) 1289b50d902SRodney W. Grimes switch(ch) { 129fc17b349SJuli Mallett case 'E': 130fc17b349SJuli Mallett eofstr = optarg; 131fc17b349SJuli Mallett break; 132fc17b349SJuli Mallett case 'I': 1336ea89183SJuli Mallett Jflag = 0; 134fc17b349SJuli Mallett Iflag = 1; 1354f49da74SJuli Mallett Lflag = 1; 136fc17b349SJuli Mallett replstr = optarg; 137fc17b349SJuli Mallett break; 1388d904f15SDima Dorfman case 'J': 1396ea89183SJuli Mallett Iflag = 0; 140b50a7286SJuli Mallett Jflag = 1; 1418d904f15SDima Dorfman replstr = optarg; 1428d904f15SDima Dorfman break; 143fc17b349SJuli Mallett case 'L': 1444f49da74SJuli Mallett Lflag = atoi(optarg); 145fc17b349SJuli Mallett break; 1469b50d902SRodney W. Grimes case 'n': 1479b50d902SRodney W. Grimes nflag = 1; 1489b50d902SRodney W. Grimes if ((nargs = atoi(optarg)) <= 0) 149a51024e2SPhilippe Charnier errx(1, "illegal argument count"); 1509b50d902SRodney W. Grimes break; 15198186e89SMaxime Henrion case 'o': 15298186e89SMaxime Henrion oflag = 1; 15398186e89SMaxime Henrion break; 154330d23f5STim J. Robbins case 'P': 155330d23f5STim J. Robbins if ((maxprocs = atoi(optarg)) <= 0) 156330d23f5STim J. Robbins errx(1, "max. processes must be >0"); 157330d23f5STim J. Robbins break; 158fc17b349SJuli Mallett case 'p': 159fc17b349SJuli Mallett pflag = 1; 160fc17b349SJuli Mallett break; 161b50a7286SJuli Mallett case 'R': 1623c675167SJuli Mallett Rflag = strtol(optarg, &endptr, 10); 1633c675167SJuli Mallett if (*endptr != '\0') 1643c675167SJuli Mallett errx(1, "replacements must be a number"); 165b50a7286SJuli Mallett break; 1662d14e0e5SDag-Erling Smørgrav case 'r': 1672d14e0e5SDag-Erling Smørgrav /* GNU compatibility */ 1682d14e0e5SDag-Erling Smørgrav break; 169ba084f6aSJuli Mallett case 'S': 170ba084f6aSJuli Mallett Sflag = strtoul(optarg, &endptr, 10); 171ba084f6aSJuli Mallett if (*endptr != '\0') 172ba084f6aSJuli Mallett errx(1, "replsize must be a number"); 173ba084f6aSJuli Mallett break; 1749b50d902SRodney W. Grimes case 's': 1759b50d902SRodney W. Grimes nline = atoi(optarg); 1769b50d902SRodney W. Grimes break; 1779b50d902SRodney W. Grimes case 't': 1789b50d902SRodney W. Grimes tflag = 1; 1799b50d902SRodney W. Grimes break; 1809b50d902SRodney W. Grimes case 'x': 1819b50d902SRodney W. Grimes xflag = 1; 1829b50d902SRodney W. Grimes break; 183d9198881SWarner Losh case '0': 184d9198881SWarner Losh zflag = 1; 185d9198881SWarner Losh break; 1869b50d902SRodney W. Grimes case '?': 1879b50d902SRodney W. Grimes default: 1889b50d902SRodney W. Grimes usage(); 1899b50d902SRodney W. Grimes } 1909b50d902SRodney W. Grimes argc -= optind; 1919b50d902SRodney W. Grimes argv += optind; 1929b50d902SRodney W. Grimes 1936ea89183SJuli Mallett if (!Iflag && Rflag) 1946ea89183SJuli Mallett usage(); 195ba084f6aSJuli Mallett if (!Iflag && Sflag) 196ba084f6aSJuli Mallett usage(); 1976ea89183SJuli Mallett if (Iflag && !Rflag) 1986ea89183SJuli Mallett Rflag = 5; 199ba084f6aSJuli Mallett if (Iflag && !Sflag) 200ba084f6aSJuli Mallett Sflag = 255; 2019b50d902SRodney W. Grimes if (xflag && !nflag) 2029b50d902SRodney W. Grimes usage(); 2034f49da74SJuli Mallett if (Iflag || Lflag) 204fc17b349SJuli Mallett xflag = 1; 205fc17b349SJuli Mallett if (replstr != NULL && *replstr == '\0') 206fc17b349SJuli Mallett errx(1, "replstr may not be empty"); 2079b50d902SRodney W. Grimes 2089b50d902SRodney W. Grimes /* 2099b50d902SRodney W. Grimes * Allocate pointers for the utility name, the utility arguments, 2109b50d902SRodney W. Grimes * the maximum arguments to be read from stdin and the trailing 2119b50d902SRodney W. Grimes * NULL. 2129b50d902SRodney W. Grimes */ 213fc17b349SJuli Mallett linelen = 1 + argc + nargs + 1; 2140fa5e8dcSJuli Mallett if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL) 21591045075SJuli Mallett errx(1, "malloc failed"); 2169b50d902SRodney W. Grimes 2179b50d902SRodney W. Grimes /* 2189b50d902SRodney W. Grimes * Use the user's name for the utility as argv[0], just like the 2199b50d902SRodney W. Grimes * shell. Echo is the default. Set up pointers for the user's 2209b50d902SRodney W. Grimes * arguments. 2219b50d902SRodney W. Grimes */ 2223dca1afcSJuli Mallett if (*argv == NULL) 2239bf450b8SJuli Mallett cnt = strlen(*bxp++ = echo); 2249b50d902SRodney W. Grimes else { 2259b50d902SRodney W. Grimes do { 226b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 22791045075SJuli Mallett char **avj; 2288d904f15SDima Dorfman jfound = 1; 2298d904f15SDima Dorfman argv++; 2308d904f15SDima Dorfman for (avj = argv; *avj; avj++) 2318d904f15SDima Dorfman cnt += strlen(*avj) + 1; 2328d904f15SDima Dorfman break; 2338d904f15SDima Dorfman } 2349b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 2353dca1afcSJuli Mallett } while (*++argv != NULL); 2369b50d902SRodney W. Grimes } 2379b50d902SRodney W. Grimes 2389b50d902SRodney W. Grimes /* 2399b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2409b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2419b50d902SRodney W. Grimes * added in an extra slot. 2429b50d902SRodney W. Grimes */ 2438eb2a3deSDavid E. O'Brien endxp = (xp = bxp) + nargs; 2449b50d902SRodney W. Grimes 2459b50d902SRodney W. Grimes /* 2469b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2479b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2489b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2499b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2509b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2519b50d902SRodney W. Grimes * slot. 2529b50d902SRodney W. Grimes */ 2539b50d902SRodney W. Grimes nline -= cnt; 2549b50d902SRodney W. Grimes if (nline <= 0) 255a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2569b50d902SRodney W. Grimes 2579bf450b8SJuli Mallett if ((bbp = malloc((size_t)(nline + 1))) == NULL) 25891045075SJuli Mallett errx(1, "malloc failed"); 2599b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 26091045075SJuli Mallett for (;;) 26191045075SJuli Mallett parse_input(argc, argv); 26291045075SJuli Mallett } 2639b50d902SRodney W. Grimes 26491045075SJuli Mallett static void 2651926d4aaSJuli Mallett parse_input(int argc, char *argv[]) 26691045075SJuli Mallett { 26791045075SJuli Mallett int ch, foundeof; 26891045075SJuli Mallett char **avj; 26991045075SJuli Mallett 27091045075SJuli Mallett foundeof = 0; 27191045075SJuli Mallett 2729b50d902SRodney W. Grimes switch(ch = getchar()) { 2739b50d902SRodney W. Grimes case EOF: 2749b50d902SRodney W. Grimes /* No arguments since last exec. */ 275330d23f5STim J. Robbins if (p == bbp) { 276330d23f5STim J. Robbins waitchildren(*argv, 1); 2779b50d902SRodney W. Grimes exit(rval); 278330d23f5STim J. Robbins } 2799b50d902SRodney W. Grimes goto arg1; 2809b50d902SRodney W. Grimes case ' ': 2819b50d902SRodney W. Grimes case '\t': 2829b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 283d9198881SWarner Losh if (insingle || indouble || zflag) 2849b50d902SRodney W. Grimes goto addch; 2859b50d902SRodney W. Grimes goto arg2; 286d9198881SWarner Losh case '\0': 28746793db9SGarance A Drosehn if (zflag) { 28846793db9SGarance A Drosehn /* 28946793db9SGarance A Drosehn * Increment 'count', so that nulls will be treated 29046793db9SGarance A Drosehn * as end-of-line, as well as end-of-argument. This 29146793db9SGarance A Drosehn * is needed so -0 works properly with -I and -L. 29246793db9SGarance A Drosehn */ 29346793db9SGarance A Drosehn count++; 294d9198881SWarner Losh goto arg2; 29546793db9SGarance A Drosehn } 296d9198881SWarner Losh goto addch; 2979b50d902SRodney W. Grimes case '\n': 298d9198881SWarner Losh if (zflag) 299d9198881SWarner Losh goto addch; 30046793db9SGarance A Drosehn count++; /* Indicate end-of-line (used by -L) */ 301d9198881SWarner Losh 3029b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 3039b50d902SRodney W. Grimes arg1: if (insingle || indouble) 304a51024e2SPhilippe Charnier errx(1, "unterminated quote"); 305ef9866beSJean-Marc Zucconi arg2: 306fc17b349SJuli Mallett foundeof = *eofstr != '\0' && 307fc17b349SJuli Mallett strcmp(argp, eofstr) == 0; 308fc17b349SJuli Mallett 309ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 310fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 311ef9866beSJean-Marc Zucconi *p++ = '\0'; 3129b50d902SRodney W. Grimes *xp++ = argp; 313fc17b349SJuli Mallett if (Iflag) { 314fc17b349SJuli Mallett size_t curlen; 315b9b03ba0SJuli Mallett 316b9b03ba0SJuli Mallett if (inpline == NULL) 317fc17b349SJuli Mallett curlen = 0; 318fc17b349SJuli Mallett else { 3191925cb24SJuli Mallett /* 3201925cb24SJuli Mallett * If this string is not zero 3211925cb24SJuli Mallett * length, append a space for 3222b239dd1SJens Schweikhardt * separation before the next 3231925cb24SJuli Mallett * argument. 3241925cb24SJuli Mallett */ 325b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 326fc17b349SJuli Mallett strcat(inpline, " "); 327fc17b349SJuli Mallett } 328fc17b349SJuli Mallett curlen++; 3291925cb24SJuli Mallett /* 3301925cb24SJuli Mallett * Allocate enough to hold what we will 3315eb40323SJuli Mallett * be holding in a second, and to append 3321925cb24SJuli Mallett * a space next time through, if we have 3331925cb24SJuli Mallett * to. 3341925cb24SJuli Mallett */ 335b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 336b9b03ba0SJuli Mallett strlen(argp)); 337fc17b349SJuli Mallett if (inpline == NULL) 33891045075SJuli Mallett errx(1, "realloc failed"); 339fc17b349SJuli Mallett if (curlen == 1) 340fc17b349SJuli Mallett strcpy(inpline, argp); 341fc17b349SJuli Mallett else 342fc17b349SJuli Mallett strcat(inpline, argp); 343fc17b349SJuli Mallett } 344ef9866beSJean-Marc Zucconi } 3459b50d902SRodney W. Grimes 3469b50d902SRodney W. Grimes /* 3479b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 3489b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 3491925cb24SJuli Mallett * but not on args, object. Having reached the limit 3501925cb24SJuli Mallett * of input lines, as specified by -L is the same as 3511925cb24SJuli Mallett * maxing out on arguments. 3529b50d902SRodney W. Grimes */ 3538eb2a3deSDavid E. O'Brien if (xp == endxp || p > ebp || ch == EOF || 3545eb40323SJuli Mallett (Lflag <= count && xflag) || foundeof) { 3558eb2a3deSDavid E. O'Brien if (xflag && xp != endxp && p > ebp) 356a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3578d904f15SDima Dorfman if (jfound) { 3588d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3598d904f15SDima Dorfman *xp++ = *avj; 3608d904f15SDima Dorfman } 36191045075SJuli Mallett prerun(argc, av); 362330d23f5STim J. Robbins if (ch == EOF || foundeof) { 363330d23f5STim J. Robbins waitchildren(*argv, 1); 3649b50d902SRodney W. Grimes exit(rval); 365330d23f5STim J. Robbins } 3669b50d902SRodney W. Grimes p = bbp; 3679b50d902SRodney W. Grimes xp = bxp; 368fc17b349SJuli Mallett count = 0; 369ef9866beSJean-Marc Zucconi } 3709b50d902SRodney W. Grimes argp = p; 371ef9866beSJean-Marc Zucconi wasquoted = 0; 3729b50d902SRodney W. Grimes break; 3739b50d902SRodney W. Grimes case '\'': 374d9198881SWarner Losh if (indouble || zflag) 3759b50d902SRodney W. Grimes goto addch; 3769b50d902SRodney W. Grimes insingle = !insingle; 377ef9866beSJean-Marc Zucconi wasquoted = 1; 3789b50d902SRodney W. Grimes break; 3799b50d902SRodney W. Grimes case '"': 380d9198881SWarner Losh if (insingle || zflag) 3819b50d902SRodney W. Grimes goto addch; 3829b50d902SRodney W. Grimes indouble = !indouble; 383ef9866beSJean-Marc Zucconi wasquoted = 1; 3849b50d902SRodney W. Grimes break; 3859b50d902SRodney W. Grimes case '\\': 386d9198881SWarner Losh if (zflag) 387d9198881SWarner Losh goto addch; 3889b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 3899b50d902SRodney W. Grimes if (!insingle && !indouble && (ch = getchar()) == EOF) 390a51024e2SPhilippe Charnier errx(1, "backslash at EOF"); 3919b50d902SRodney W. Grimes /* FALLTHROUGH */ 3929b50d902SRodney W. Grimes default: 3939b50d902SRodney W. Grimes addch: if (p < ebp) { 3949b50d902SRodney W. Grimes *p++ = ch; 3959b50d902SRodney W. Grimes break; 3969b50d902SRodney W. Grimes } 3979b50d902SRodney W. Grimes 3989b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 3999b50d902SRodney W. Grimes if (bxp == xp) 400a51024e2SPhilippe Charnier errx(1, "insufficient space for argument"); 4019b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 4029b50d902SRodney W. Grimes if (xflag) 403a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 4049b50d902SRodney W. Grimes 4058d904f15SDima Dorfman if (jfound) { 4068d904f15SDima Dorfman for (avj = argv; *avj; avj++) 4078d904f15SDima Dorfman *xp++ = *avj; 4088d904f15SDima Dorfman } 40991045075SJuli Mallett prerun(argc, av); 4109b50d902SRodney W. Grimes xp = bxp; 4119b50d902SRodney W. Grimes cnt = ebp - argp; 412fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 4139b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 4149b50d902SRodney W. Grimes *p++ = ch; 4159b50d902SRodney W. Grimes break; 4169b50d902SRodney W. Grimes } 4179b50d902SRodney W. Grimes } 4189b50d902SRodney W. Grimes 419263dc775SJuli Mallett /* 420263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 421263dc775SJuli Mallett * and then call run(). 422263dc775SJuli Mallett */ 423263dc775SJuli Mallett static void 4241926d4aaSJuli Mallett prerun(int argc, char *argv[]) 425263dc775SJuli Mallett { 426263dc775SJuli Mallett char **tmp, **tmp2, **avj; 42791045075SJuli Mallett int repls; 42891045075SJuli Mallett 42991045075SJuli Mallett repls = Rflag; 430263dc775SJuli Mallett 431be70f7d4SJuli Mallett if (argc == 0 || repls == 0) { 432263dc775SJuli Mallett *xp = NULL; 433263dc775SJuli Mallett run(argv); 434263dc775SJuli Mallett return; 435263dc775SJuli Mallett } 436263dc775SJuli Mallett 437263dc775SJuli Mallett avj = argv; 438263dc775SJuli Mallett 439263dc775SJuli Mallett /* 440263dc775SJuli Mallett * Allocate memory to hold the argument list, and 441263dc775SJuli Mallett * a NULL at the tail. 442263dc775SJuli Mallett */ 443b6594dbaSJuli Mallett tmp = malloc((argc + 1) * sizeof(char**)); 444263dc775SJuli Mallett if (tmp == NULL) 44591045075SJuli Mallett errx(1, "malloc failed"); 446263dc775SJuli Mallett tmp2 = tmp; 447263dc775SJuli Mallett 448263dc775SJuli Mallett /* 449263dc775SJuli Mallett * Save the first argument and iterate over it, we 450263dc775SJuli Mallett * cannot do strnsubst() to it. 451263dc775SJuli Mallett */ 452263dc775SJuli Mallett if ((*tmp++ = strdup(*avj++)) == NULL) 45391045075SJuli Mallett errx(1, "strdup failed"); 454263dc775SJuli Mallett 455263dc775SJuli Mallett /* 456263dc775SJuli Mallett * For each argument to utility, if we have not used up 457263dc775SJuli Mallett * the number of replacements we are allowed to do, and 4582b239dd1SJens Schweikhardt * if the argument contains at least one occurrence of 459263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 460263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 461263dc775SJuli Mallett * where appropriate. 462263dc775SJuli Mallett */ 463263dc775SJuli Mallett while (--argc) { 464263dc775SJuli Mallett *tmp = *avj++; 465263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 466ba084f6aSJuli Mallett strnsubst(tmp++, replstr, inpline, (size_t)Sflag); 4673c675167SJuli Mallett if (repls > 0) 468263dc775SJuli Mallett repls--; 469263dc775SJuli Mallett } else { 470263dc775SJuli Mallett if ((*tmp = strdup(*tmp)) == NULL) 47191045075SJuli Mallett errx(1, "strdup failed"); 472263dc775SJuli Mallett tmp++; 473263dc775SJuli Mallett } 474263dc775SJuli Mallett } 475263dc775SJuli Mallett 476263dc775SJuli Mallett /* 477263dc775SJuli Mallett * Run it. 478263dc775SJuli Mallett */ 479b6594dbaSJuli Mallett *tmp = NULL; 480263dc775SJuli Mallett run(tmp2); 481263dc775SJuli Mallett 482263dc775SJuli Mallett /* 483263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 484263dc775SJuli Mallett */ 485263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 486263dc775SJuli Mallett free(*tmp); 487263dc775SJuli Mallett /* 488263dc775SJuli Mallett * Now free the list itself. 489263dc775SJuli Mallett */ 490263dc775SJuli Mallett free(tmp2); 491263dc775SJuli Mallett 492263dc775SJuli Mallett /* 493986d829bSJuli Mallett * Free the input line buffer, if we have one. 494263dc775SJuli Mallett */ 49551f7a48bSJuli Mallett if (inpline != NULL) { 49691045075SJuli Mallett free(inpline); 49751f7a48bSJuli Mallett inpline = NULL; 49851f7a48bSJuli Mallett } 499263dc775SJuli Mallett } 500263dc775SJuli Mallett 501fc17b349SJuli Mallett static void 50273385ac6SJuli Mallett run(char **argv) 5039b50d902SRodney W. Grimes { 5049b50d902SRodney W. Grimes pid_t pid; 5050792992cSMaxime Henrion int fd; 506330d23f5STim J. Robbins char **avec; 5079b50d902SRodney W. Grimes 508305e39f4SJuli Mallett /* 509305e39f4SJuli Mallett * If the user wants to be notified of each command before it is 510305e39f4SJuli Mallett * executed, notify them. If they want the notification to be 511305e39f4SJuli Mallett * followed by a prompt, then prompt them. 512305e39f4SJuli Mallett */ 513fc17b349SJuli Mallett if (tflag || pflag) { 5149b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 51591045075SJuli Mallett for (avec = argv + 1; *avec != NULL; ++avec) 51691045075SJuli Mallett (void)fprintf(stderr, " %s", *avec); 517305e39f4SJuli Mallett /* 518305e39f4SJuli Mallett * If the user has asked to be prompted, do so. 519305e39f4SJuli Mallett */ 520305e39f4SJuli Mallett if (pflag) 521305e39f4SJuli Mallett /* 522305e39f4SJuli Mallett * If they asked not to exec, return without execution 523305e39f4SJuli Mallett * but if they asked to, go to the execution. If we 524305e39f4SJuli Mallett * could not open their tty, break the switch and drop 525305e39f4SJuli Mallett * back to -t behaviour. 526305e39f4SJuli Mallett */ 527305e39f4SJuli Mallett switch (prompt()) { 528305e39f4SJuli Mallett case 0: 529fc17b349SJuli Mallett return; 530305e39f4SJuli Mallett case 1: 531305e39f4SJuli Mallett goto exec; 532305e39f4SJuli Mallett case 2: 533305e39f4SJuli Mallett break; 534305e39f4SJuli Mallett } 5359b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 5369b50d902SRodney W. Grimes (void)fflush(stderr); 5379b50d902SRodney W. Grimes } 538305e39f4SJuli Mallett exec: 5398ad749a4SDag-Erling Smørgrav childerr = 0; 5408ad749a4SDag-Erling Smørgrav switch(pid = vfork()) { 5419b50d902SRodney W. Grimes case -1: 5428ad749a4SDag-Erling Smørgrav err(1, "vfork"); 5439b50d902SRodney W. Grimes case 0: 544cec1ba8cSMaxime Henrion if (oflag) { 5450792992cSMaxime Henrion if ((fd = open(_PATH_TTY, O_RDONLY)) == -1) 5460792992cSMaxime Henrion err(1, "can't open /dev/tty"); 547cec1ba8cSMaxime Henrion } else { 5480792992cSMaxime Henrion fd = open(_PATH_DEVNULL, O_RDONLY); 5490792992cSMaxime Henrion } 5500792992cSMaxime Henrion if (fd > STDIN_FILENO) { 5510792992cSMaxime Henrion if (dup2(fd, STDIN_FILENO) != 0) 5520792992cSMaxime Henrion err(1, "can't dup2 to stdin"); 5530792992cSMaxime Henrion close(fd); 55498186e89SMaxime Henrion } 5559b50d902SRodney W. Grimes execvp(argv[0], argv); 5568ad749a4SDag-Erling Smørgrav childerr = errno; 5579b50d902SRodney W. Grimes _exit(1); 5589b50d902SRodney W. Grimes } 559330d23f5STim J. Robbins curprocs++; 560330d23f5STim J. Robbins waitchildren(*argv, 0); 561330d23f5STim J. Robbins } 562330d23f5STim J. Robbins 563330d23f5STim J. Robbins static void 564330d23f5STim J. Robbins waitchildren(const char *name, int waitall) 565330d23f5STim J. Robbins { 566330d23f5STim J. Robbins pid_t pid; 567330d23f5STim J. Robbins int status; 568330d23f5STim J. Robbins 569004bd28eSJuli Mallett while ((pid = waitpid(-1, &status, !waitall && curprocs < maxprocs ? 570004bd28eSJuli Mallett WNOHANG : 0)) > 0) { 571330d23f5STim J. Robbins curprocs--; 572fc17b349SJuli Mallett /* If we couldn't invoke the utility, exit. */ 573330d23f5STim J. Robbins if (childerr != 0) { 574330d23f5STim J. Robbins errno = childerr; 575330d23f5STim J. Robbins err(errno == ENOENT ? 127 : 126, "%s", name); 576330d23f5STim J. Robbins } 577330d23f5STim J. Robbins /* 578330d23f5STim J. Robbins * If utility signaled or exited with a value of 255, 579330d23f5STim J. Robbins * exit 1-125. 580330d23f5STim J. Robbins */ 5819b50d902SRodney W. Grimes if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255) 5829b50d902SRodney W. Grimes exit(1); 5839b50d902SRodney W. Grimes if (WEXITSTATUS(status)) 5849b50d902SRodney W. Grimes rval = 1; 5859b50d902SRodney W. Grimes } 586330d23f5STim J. Robbins if (pid == -1 && errno != ECHILD) 587330d23f5STim J. Robbins err(1, "wait3"); 588330d23f5STim J. Robbins } 5899b50d902SRodney W. Grimes 590305e39f4SJuli Mallett /* 591305e39f4SJuli Mallett * Prompt the user about running a command. 592305e39f4SJuli Mallett */ 593305e39f4SJuli Mallett static int 594305e39f4SJuli Mallett prompt(void) 595305e39f4SJuli Mallett { 596305e39f4SJuli Mallett regex_t cre; 597305e39f4SJuli Mallett size_t rsize; 598305e39f4SJuli Mallett int match; 599305e39f4SJuli Mallett char *response; 600305e39f4SJuli Mallett FILE *ttyfp; 601305e39f4SJuli Mallett 602305e39f4SJuli Mallett if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) 603305e39f4SJuli Mallett return (2); /* Indicate that the TTY failed to open. */ 604305e39f4SJuli Mallett (void)fprintf(stderr, "?..."); 605305e39f4SJuli Mallett (void)fflush(stderr); 606305e39f4SJuli Mallett if ((response = fgetln(ttyfp, &rsize)) == NULL || 60730aaff11SWarner Losh regcomp(&cre, nl_langinfo(YESEXPR), REG_BASIC) != 0) { 608305e39f4SJuli Mallett (void)fclose(ttyfp); 609305e39f4SJuli Mallett return (0); 610305e39f4SJuli Mallett } 611305e39f4SJuli Mallett match = regexec(&cre, response, 0, NULL, 0); 612305e39f4SJuli Mallett (void)fclose(ttyfp); 613305e39f4SJuli Mallett regfree(&cre); 614305e39f4SJuli Mallett return (match == 0); 615305e39f4SJuli Mallett } 616305e39f4SJuli Mallett 617a51024e2SPhilippe Charnier static void 61873385ac6SJuli Mallett usage(void) 6199b50d902SRodney W. Grimes { 6208d904f15SDima Dorfman fprintf(stderr, 621ba084f6aSJuli Mallett "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]\n" 622ba084f6aSJuli Mallett " [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]\n" 623ba084f6aSJuli Mallett " [-s size] [utility [argument ...]]\n"); 6249b50d902SRodney W. Grimes exit(1); 6259b50d902SRodney W. Grimes } 626