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; 8391045075SJuli Mallett static int cnt, Iflag, jfound, Lflag, 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 103305e39f4SJuli Mallett (void)setlocale(LC_MESSAGES, ""); 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; 12798186e89SMaxime Henrion while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:s:tx")) != -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; 1669b50d902SRodney W. Grimes case 's': 1679b50d902SRodney W. Grimes nline = atoi(optarg); 1689b50d902SRodney W. Grimes break; 1699b50d902SRodney W. Grimes case 't': 1709b50d902SRodney W. Grimes tflag = 1; 1719b50d902SRodney W. Grimes break; 1729b50d902SRodney W. Grimes case 'x': 1739b50d902SRodney W. Grimes xflag = 1; 1749b50d902SRodney W. Grimes break; 175d9198881SWarner Losh case '0': 176d9198881SWarner Losh zflag = 1; 177d9198881SWarner Losh break; 1789b50d902SRodney W. Grimes case '?': 1799b50d902SRodney W. Grimes default: 1809b50d902SRodney W. Grimes usage(); 1819b50d902SRodney W. Grimes } 1829b50d902SRodney W. Grimes argc -= optind; 1839b50d902SRodney W. Grimes argv += optind; 1849b50d902SRodney W. Grimes 1856ea89183SJuli Mallett if (!Iflag && Rflag) 1866ea89183SJuli Mallett usage(); 1876ea89183SJuli Mallett if (Iflag && !Rflag) 1886ea89183SJuli Mallett Rflag = 5; 1899b50d902SRodney W. Grimes if (xflag && !nflag) 1909b50d902SRodney W. Grimes usage(); 1914f49da74SJuli Mallett if (Iflag || Lflag) 192fc17b349SJuli Mallett xflag = 1; 193fc17b349SJuli Mallett if (replstr != NULL && *replstr == '\0') 194fc17b349SJuli Mallett errx(1, "replstr may not be empty"); 1959b50d902SRodney W. Grimes 1969b50d902SRodney W. Grimes /* 1979b50d902SRodney W. Grimes * Allocate pointers for the utility name, the utility arguments, 1989b50d902SRodney W. Grimes * the maximum arguments to be read from stdin and the trailing 1999b50d902SRodney W. Grimes * NULL. 2009b50d902SRodney W. Grimes */ 201fc17b349SJuli Mallett linelen = 1 + argc + nargs + 1; 2020fa5e8dcSJuli Mallett if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL) 20391045075SJuli Mallett errx(1, "malloc failed"); 2049b50d902SRodney W. Grimes 2059b50d902SRodney W. Grimes /* 2069b50d902SRodney W. Grimes * Use the user's name for the utility as argv[0], just like the 2079b50d902SRodney W. Grimes * shell. Echo is the default. Set up pointers for the user's 2089b50d902SRodney W. Grimes * arguments. 2099b50d902SRodney W. Grimes */ 2103dca1afcSJuli Mallett if (*argv == NULL) 2119bf450b8SJuli Mallett cnt = strlen(*bxp++ = echo); 2129b50d902SRodney W. Grimes else { 2139b50d902SRodney W. Grimes do { 214b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 21591045075SJuli Mallett char **avj; 2168d904f15SDima Dorfman jfound = 1; 2178d904f15SDima Dorfman argv++; 2188d904f15SDima Dorfman for (avj = argv; *avj; avj++) 2198d904f15SDima Dorfman cnt += strlen(*avj) + 1; 2208d904f15SDima Dorfman break; 2218d904f15SDima Dorfman } 2229b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 2233dca1afcSJuli Mallett } while (*++argv != NULL); 2249b50d902SRodney W. Grimes } 2259b50d902SRodney W. Grimes 2269b50d902SRodney W. Grimes /* 2279b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2289b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2299b50d902SRodney W. Grimes * added in an extra slot. 2309b50d902SRodney W. Grimes */ 2318eb2a3deSDavid E. O'Brien endxp = (xp = bxp) + nargs; 2329b50d902SRodney W. Grimes 2339b50d902SRodney W. Grimes /* 2349b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2359b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2369b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2379b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2389b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2399b50d902SRodney W. Grimes * slot. 2409b50d902SRodney W. Grimes */ 2419b50d902SRodney W. Grimes nline -= cnt; 2429b50d902SRodney W. Grimes if (nline <= 0) 243a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2449b50d902SRodney W. Grimes 2459bf450b8SJuli Mallett if ((bbp = malloc((size_t)(nline + 1))) == NULL) 24691045075SJuli Mallett errx(1, "malloc failed"); 2479b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 24891045075SJuli Mallett for (;;) 24991045075SJuli Mallett parse_input(argc, argv); 25091045075SJuli Mallett } 2519b50d902SRodney W. Grimes 25291045075SJuli Mallett static void 2531926d4aaSJuli Mallett parse_input(int argc, char *argv[]) 25491045075SJuli Mallett { 25591045075SJuli Mallett int ch, foundeof; 25691045075SJuli Mallett char **avj; 25791045075SJuli Mallett 25891045075SJuli Mallett foundeof = 0; 25991045075SJuli Mallett 2609b50d902SRodney W. Grimes switch(ch = getchar()) { 2619b50d902SRodney W. Grimes case EOF: 2629b50d902SRodney W. Grimes /* No arguments since last exec. */ 263330d23f5STim J. Robbins if (p == bbp) { 264330d23f5STim J. Robbins waitchildren(*argv, 1); 2659b50d902SRodney W. Grimes exit(rval); 266330d23f5STim J. Robbins } 2679b50d902SRodney W. Grimes goto arg1; 2689b50d902SRodney W. Grimes case ' ': 2699b50d902SRodney W. Grimes case '\t': 2709b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 271d9198881SWarner Losh if (insingle || indouble || zflag) 2729b50d902SRodney W. Grimes goto addch; 2739b50d902SRodney W. Grimes goto arg2; 274d9198881SWarner Losh case '\0': 275d9198881SWarner Losh if (zflag) 276d9198881SWarner Losh goto arg2; 277d9198881SWarner Losh goto addch; 2789b50d902SRodney W. Grimes case '\n': 279fc17b349SJuli Mallett count++; 280d9198881SWarner Losh if (zflag) 281d9198881SWarner Losh goto addch; 282d9198881SWarner Losh 2839b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 2849b50d902SRodney W. Grimes arg1: if (insingle || indouble) 285a51024e2SPhilippe Charnier errx(1, "unterminated quote"); 286ef9866beSJean-Marc Zucconi arg2: 287fc17b349SJuli Mallett foundeof = *eofstr != '\0' && 288fc17b349SJuli Mallett strcmp(argp, eofstr) == 0; 289fc17b349SJuli Mallett 290ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 291fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 292ef9866beSJean-Marc Zucconi *p++ = '\0'; 2939b50d902SRodney W. Grimes *xp++ = argp; 294fc17b349SJuli Mallett if (Iflag) { 295fc17b349SJuli Mallett size_t curlen; 296b9b03ba0SJuli Mallett 297b9b03ba0SJuli Mallett if (inpline == NULL) 298fc17b349SJuli Mallett curlen = 0; 299fc17b349SJuli Mallett else { 3001925cb24SJuli Mallett /* 3011925cb24SJuli Mallett * If this string is not zero 3021925cb24SJuli Mallett * length, append a space for 3032b239dd1SJens Schweikhardt * separation before the next 3041925cb24SJuli Mallett * argument. 3051925cb24SJuli Mallett */ 306b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 307fc17b349SJuli Mallett strcat(inpline, " "); 308fc17b349SJuli Mallett } 309fc17b349SJuli Mallett curlen++; 3101925cb24SJuli Mallett /* 3111925cb24SJuli Mallett * Allocate enough to hold what we will 3125eb40323SJuli Mallett * be holding in a second, and to append 3131925cb24SJuli Mallett * a space next time through, if we have 3141925cb24SJuli Mallett * to. 3151925cb24SJuli Mallett */ 316b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 317b9b03ba0SJuli Mallett strlen(argp)); 318fc17b349SJuli Mallett if (inpline == NULL) 31991045075SJuli Mallett errx(1, "realloc failed"); 320fc17b349SJuli Mallett if (curlen == 1) 321fc17b349SJuli Mallett strcpy(inpline, argp); 322fc17b349SJuli Mallett else 323fc17b349SJuli Mallett strcat(inpline, argp); 324fc17b349SJuli Mallett } 325ef9866beSJean-Marc Zucconi } 3269b50d902SRodney W. Grimes 3279b50d902SRodney W. Grimes /* 3289b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 3299b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 3301925cb24SJuli Mallett * but not on args, object. Having reached the limit 3311925cb24SJuli Mallett * of input lines, as specified by -L is the same as 3321925cb24SJuli Mallett * maxing out on arguments. 3339b50d902SRodney W. Grimes */ 3348eb2a3deSDavid E. O'Brien if (xp == endxp || p > ebp || ch == EOF || 3355eb40323SJuli Mallett (Lflag <= count && xflag) || foundeof) { 3368eb2a3deSDavid E. O'Brien if (xflag && xp != endxp && p > ebp) 337a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3388d904f15SDima Dorfman if (jfound) { 3398d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3408d904f15SDima Dorfman *xp++ = *avj; 3418d904f15SDima Dorfman } 34291045075SJuli Mallett prerun(argc, av); 343330d23f5STim J. Robbins if (ch == EOF || foundeof) { 344330d23f5STim J. Robbins waitchildren(*argv, 1); 3459b50d902SRodney W. Grimes exit(rval); 346330d23f5STim J. Robbins } 3479b50d902SRodney W. Grimes p = bbp; 3489b50d902SRodney W. Grimes xp = bxp; 349fc17b349SJuli Mallett count = 0; 350ef9866beSJean-Marc Zucconi } 3519b50d902SRodney W. Grimes argp = p; 352ef9866beSJean-Marc Zucconi wasquoted = 0; 3539b50d902SRodney W. Grimes break; 3549b50d902SRodney W. Grimes case '\'': 355d9198881SWarner Losh if (indouble || zflag) 3569b50d902SRodney W. Grimes goto addch; 3579b50d902SRodney W. Grimes insingle = !insingle; 358ef9866beSJean-Marc Zucconi wasquoted = 1; 3599b50d902SRodney W. Grimes break; 3609b50d902SRodney W. Grimes case '"': 361d9198881SWarner Losh if (insingle || zflag) 3629b50d902SRodney W. Grimes goto addch; 3639b50d902SRodney W. Grimes indouble = !indouble; 364ef9866beSJean-Marc Zucconi wasquoted = 1; 3659b50d902SRodney W. Grimes break; 3669b50d902SRodney W. Grimes case '\\': 367d9198881SWarner Losh if (zflag) 368d9198881SWarner Losh goto addch; 3699b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 3709b50d902SRodney W. Grimes if (!insingle && !indouble && (ch = getchar()) == EOF) 371a51024e2SPhilippe Charnier errx(1, "backslash at EOF"); 3729b50d902SRodney W. Grimes /* FALLTHROUGH */ 3739b50d902SRodney W. Grimes default: 3749b50d902SRodney W. Grimes addch: if (p < ebp) { 3759b50d902SRodney W. Grimes *p++ = ch; 3769b50d902SRodney W. Grimes break; 3779b50d902SRodney W. Grimes } 3789b50d902SRodney W. Grimes 3799b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 3809b50d902SRodney W. Grimes if (bxp == xp) 381a51024e2SPhilippe Charnier errx(1, "insufficient space for argument"); 3829b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 3839b50d902SRodney W. Grimes if (xflag) 384a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3859b50d902SRodney W. Grimes 3868d904f15SDima Dorfman if (jfound) { 3878d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3888d904f15SDima Dorfman *xp++ = *avj; 3898d904f15SDima Dorfman } 39091045075SJuli Mallett prerun(argc, av); 3919b50d902SRodney W. Grimes xp = bxp; 3929b50d902SRodney W. Grimes cnt = ebp - argp; 393fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 3949b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 3959b50d902SRodney W. Grimes *p++ = ch; 3969b50d902SRodney W. Grimes break; 3979b50d902SRodney W. Grimes } 3989b50d902SRodney W. Grimes } 3999b50d902SRodney W. Grimes 400263dc775SJuli Mallett /* 401263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 402263dc775SJuli Mallett * and then call run(). 403263dc775SJuli Mallett */ 404263dc775SJuli Mallett static void 4051926d4aaSJuli Mallett prerun(int argc, char *argv[]) 406263dc775SJuli Mallett { 407263dc775SJuli Mallett char **tmp, **tmp2, **avj; 40891045075SJuli Mallett int repls; 40991045075SJuli Mallett 41091045075SJuli Mallett repls = Rflag; 411263dc775SJuli Mallett 412be70f7d4SJuli Mallett if (argc == 0 || repls == 0) { 413263dc775SJuli Mallett *xp = NULL; 414263dc775SJuli Mallett run(argv); 415263dc775SJuli Mallett return; 416263dc775SJuli Mallett } 417263dc775SJuli Mallett 418263dc775SJuli Mallett avj = argv; 419263dc775SJuli Mallett 420263dc775SJuli Mallett /* 421263dc775SJuli Mallett * Allocate memory to hold the argument list, and 422263dc775SJuli Mallett * a NULL at the tail. 423263dc775SJuli Mallett */ 424b6594dbaSJuli Mallett tmp = malloc((argc + 1) * sizeof(char**)); 425263dc775SJuli Mallett if (tmp == NULL) 42691045075SJuli Mallett errx(1, "malloc failed"); 427263dc775SJuli Mallett tmp2 = tmp; 428263dc775SJuli Mallett 429263dc775SJuli Mallett /* 430263dc775SJuli Mallett * Save the first argument and iterate over it, we 431263dc775SJuli Mallett * cannot do strnsubst() to it. 432263dc775SJuli Mallett */ 433263dc775SJuli Mallett if ((*tmp++ = strdup(*avj++)) == NULL) 43491045075SJuli Mallett errx(1, "strdup failed"); 435263dc775SJuli Mallett 436263dc775SJuli Mallett /* 437263dc775SJuli Mallett * For each argument to utility, if we have not used up 438263dc775SJuli Mallett * the number of replacements we are allowed to do, and 4392b239dd1SJens Schweikhardt * if the argument contains at least one occurrence of 440263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 441263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 442263dc775SJuli Mallett * where appropriate. 443263dc775SJuli Mallett */ 444263dc775SJuli Mallett while (--argc) { 445263dc775SJuli Mallett *tmp = *avj++; 446263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 44791045075SJuli Mallett strnsubst(tmp++, replstr, inpline, (size_t)255); 4483c675167SJuli Mallett if (repls > 0) 449263dc775SJuli Mallett repls--; 450263dc775SJuli Mallett } else { 451263dc775SJuli Mallett if ((*tmp = strdup(*tmp)) == NULL) 45291045075SJuli Mallett errx(1, "strdup failed"); 453263dc775SJuli Mallett tmp++; 454263dc775SJuli Mallett } 455263dc775SJuli Mallett } 456263dc775SJuli Mallett 457263dc775SJuli Mallett /* 458263dc775SJuli Mallett * Run it. 459263dc775SJuli Mallett */ 460b6594dbaSJuli Mallett *tmp = NULL; 461263dc775SJuli Mallett run(tmp2); 462263dc775SJuli Mallett 463263dc775SJuli Mallett /* 464263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 465263dc775SJuli Mallett */ 466263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 467263dc775SJuli Mallett free(*tmp); 468263dc775SJuli Mallett /* 469263dc775SJuli Mallett * Now free the list itself. 470263dc775SJuli Mallett */ 471263dc775SJuli Mallett free(tmp2); 472263dc775SJuli Mallett 473263dc775SJuli Mallett /* 474986d829bSJuli Mallett * Free the input line buffer, if we have one. 475263dc775SJuli Mallett */ 47651f7a48bSJuli Mallett if (inpline != NULL) { 47791045075SJuli Mallett free(inpline); 47851f7a48bSJuli Mallett inpline = NULL; 47951f7a48bSJuli Mallett } 480263dc775SJuli Mallett } 481263dc775SJuli Mallett 482fc17b349SJuli Mallett static void 48373385ac6SJuli Mallett run(char **argv) 4849b50d902SRodney W. Grimes { 4859b50d902SRodney W. Grimes pid_t pid; 4860792992cSMaxime Henrion int fd; 487330d23f5STim J. Robbins char **avec; 4889b50d902SRodney W. Grimes 489305e39f4SJuli Mallett /* 490305e39f4SJuli Mallett * If the user wants to be notified of each command before it is 491305e39f4SJuli Mallett * executed, notify them. If they want the notification to be 492305e39f4SJuli Mallett * followed by a prompt, then prompt them. 493305e39f4SJuli Mallett */ 494fc17b349SJuli Mallett if (tflag || pflag) { 4959b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 49691045075SJuli Mallett for (avec = argv + 1; *avec != NULL; ++avec) 49791045075SJuli Mallett (void)fprintf(stderr, " %s", *avec); 498305e39f4SJuli Mallett /* 499305e39f4SJuli Mallett * If the user has asked to be prompted, do so. 500305e39f4SJuli Mallett */ 501305e39f4SJuli Mallett if (pflag) 502305e39f4SJuli Mallett /* 503305e39f4SJuli Mallett * If they asked not to exec, return without execution 504305e39f4SJuli Mallett * but if they asked to, go to the execution. If we 505305e39f4SJuli Mallett * could not open their tty, break the switch and drop 506305e39f4SJuli Mallett * back to -t behaviour. 507305e39f4SJuli Mallett */ 508305e39f4SJuli Mallett switch (prompt()) { 509305e39f4SJuli Mallett case 0: 510fc17b349SJuli Mallett return; 511305e39f4SJuli Mallett case 1: 512305e39f4SJuli Mallett goto exec; 513305e39f4SJuli Mallett case 2: 514305e39f4SJuli Mallett break; 515305e39f4SJuli Mallett } 5169b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 5179b50d902SRodney W. Grimes (void)fflush(stderr); 5189b50d902SRodney W. Grimes } 519305e39f4SJuli Mallett exec: 5208ad749a4SDag-Erling Smørgrav childerr = 0; 5218ad749a4SDag-Erling Smørgrav switch(pid = vfork()) { 5229b50d902SRodney W. Grimes case -1: 5238ad749a4SDag-Erling Smørgrav err(1, "vfork"); 5249b50d902SRodney W. Grimes case 0: 525cec1ba8cSMaxime Henrion if (oflag) { 5260792992cSMaxime Henrion if ((fd = open(_PATH_TTY, O_RDONLY)) == -1) 5270792992cSMaxime Henrion err(1, "can't open /dev/tty"); 528cec1ba8cSMaxime Henrion } else { 5290792992cSMaxime Henrion fd = open(_PATH_DEVNULL, O_RDONLY); 5300792992cSMaxime Henrion } 5310792992cSMaxime Henrion if (fd > STDIN_FILENO) { 5320792992cSMaxime Henrion if (dup2(fd, STDIN_FILENO) != 0) 5330792992cSMaxime Henrion err(1, "can't dup2 to stdin"); 5340792992cSMaxime Henrion close(fd); 53598186e89SMaxime Henrion } 5369b50d902SRodney W. Grimes execvp(argv[0], argv); 5378ad749a4SDag-Erling Smørgrav childerr = errno; 5389b50d902SRodney W. Grimes _exit(1); 5399b50d902SRodney W. Grimes } 540330d23f5STim J. Robbins curprocs++; 541330d23f5STim J. Robbins waitchildren(*argv, 0); 542330d23f5STim J. Robbins } 543330d23f5STim J. Robbins 544330d23f5STim J. Robbins static void 545330d23f5STim J. Robbins waitchildren(const char *name, int waitall) 546330d23f5STim J. Robbins { 547330d23f5STim J. Robbins pid_t pid; 548330d23f5STim J. Robbins int status; 549330d23f5STim J. Robbins 550004bd28eSJuli Mallett while ((pid = waitpid(-1, &status, !waitall && curprocs < maxprocs ? 551004bd28eSJuli Mallett WNOHANG : 0)) > 0) { 552330d23f5STim J. Robbins curprocs--; 553fc17b349SJuli Mallett /* If we couldn't invoke the utility, exit. */ 554330d23f5STim J. Robbins if (childerr != 0) { 555330d23f5STim J. Robbins errno = childerr; 556330d23f5STim J. Robbins err(errno == ENOENT ? 127 : 126, "%s", name); 557330d23f5STim J. Robbins } 558330d23f5STim J. Robbins /* 559330d23f5STim J. Robbins * If utility signaled or exited with a value of 255, 560330d23f5STim J. Robbins * exit 1-125. 561330d23f5STim J. Robbins */ 5629b50d902SRodney W. Grimes if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255) 5639b50d902SRodney W. Grimes exit(1); 5649b50d902SRodney W. Grimes if (WEXITSTATUS(status)) 5659b50d902SRodney W. Grimes rval = 1; 5669b50d902SRodney W. Grimes } 567330d23f5STim J. Robbins if (pid == -1 && errno != ECHILD) 568330d23f5STim J. Robbins err(1, "wait3"); 569330d23f5STim J. Robbins } 5709b50d902SRodney W. Grimes 571305e39f4SJuli Mallett /* 572305e39f4SJuli Mallett * Prompt the user about running a command. 573305e39f4SJuli Mallett */ 574305e39f4SJuli Mallett static int 575305e39f4SJuli Mallett prompt(void) 576305e39f4SJuli Mallett { 577305e39f4SJuli Mallett regex_t cre; 578305e39f4SJuli Mallett size_t rsize; 579305e39f4SJuli Mallett int match; 580305e39f4SJuli Mallett char *response; 581305e39f4SJuli Mallett FILE *ttyfp; 582305e39f4SJuli Mallett 583305e39f4SJuli Mallett if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) 584305e39f4SJuli Mallett return (2); /* Indicate that the TTY failed to open. */ 585305e39f4SJuli Mallett (void)fprintf(stderr, "?..."); 586305e39f4SJuli Mallett (void)fflush(stderr); 587305e39f4SJuli Mallett if ((response = fgetln(ttyfp, &rsize)) == NULL || 58830aaff11SWarner Losh regcomp(&cre, nl_langinfo(YESEXPR), REG_BASIC) != 0) { 589305e39f4SJuli Mallett (void)fclose(ttyfp); 590305e39f4SJuli Mallett return (0); 591305e39f4SJuli Mallett } 592305e39f4SJuli Mallett match = regexec(&cre, response, 0, NULL, 0); 593305e39f4SJuli Mallett (void)fclose(ttyfp); 594305e39f4SJuli Mallett regfree(&cre); 595305e39f4SJuli Mallett return (match == 0); 596305e39f4SJuli Mallett } 597305e39f4SJuli Mallett 598a51024e2SPhilippe Charnier static void 59973385ac6SJuli Mallett usage(void) 6009b50d902SRodney W. Grimes { 6018d904f15SDima Dorfman fprintf(stderr, 60298186e89SMaxime Henrion "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n" 60308f16c7aSJuli Mallett " [-L number] [-n number [-x]] [-P maxprocs] [-s size]\n" 604330d23f5STim J. Robbins " [utility [argument ...]]\n"); 6059b50d902SRodney W. Grimes exit(1); 6069b50d902SRodney W. Grimes } 607