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> 598e0a87c1SRuslan Ermilov #ifndef BOOTSTRAPPING 60305e39f4SJuli Mallett #include <langinfo.h> 618e0a87c1SRuslan Ermilov #endif 62305e39f4SJuli Mallett #include <locale.h> 63305e39f4SJuli Mallett #include <paths.h> 64305e39f4SJuli Mallett #include <regex.h> 659b50d902SRodney W. Grimes #include <stdio.h> 669b50d902SRodney W. Grimes #include <stdlib.h> 679b50d902SRodney W. Grimes #include <string.h> 689b50d902SRodney W. Grimes #include <unistd.h> 6916b07a33SMark Murray 709b50d902SRodney W. Grimes #include "pathnames.h" 719b50d902SRodney W. Grimes 7291045075SJuli Mallett static void parse_input(int, char **); 7391045075SJuli Mallett static void prerun(int, char **); 74305e39f4SJuli Mallett static int prompt(void); 75fc17b349SJuli Mallett static void run(char **); 7673385ac6SJuli Mallett static void usage(void); 77fc17b349SJuli Mallett void strnsubst(char **, const char *, const char *, size_t); 789b50d902SRodney W. Grimes 7916b07a33SMark Murray static char echo[] = _PATH_ECHO; 8091045075SJuli Mallett static char **av, **bxp, **ep, **exp, **xp; 8191045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr; 8291045075SJuli Mallett static const char *eofstr; 8391045075SJuli Mallett static int count, insingle, indouble, pflag, tflag, Rflag, rval, zflag; 8491045075SJuli Mallett static int cnt, Iflag, jfound, Lflag, wasquoted, xflag; 8516b07a33SMark Murray 8673385ac6SJuli Mallett extern char *environ[]; 8773385ac6SJuli Mallett 88a51024e2SPhilippe Charnier int 8973385ac6SJuli Mallett main(int argc, char **argv) 909b50d902SRodney W. Grimes { 91a3e5bc4fSJoseph Koshy long arg_max; 9291045075SJuli Mallett int ch, Jflag, nargs, nflag, nline; 93fc17b349SJuli Mallett size_t linelen; 949b50d902SRodney W. Grimes 95fc17b349SJuli Mallett inpline = replstr = NULL; 9691045075SJuli Mallett ep = environ; 97fc17b349SJuli Mallett eofstr = ""; 9891045075SJuli Mallett Jflag = nflag = 0; 998d904f15SDima Dorfman 100305e39f4SJuli Mallett (void)setlocale(LC_MESSAGES, ""); 101305e39f4SJuli Mallett 1029b50d902SRodney W. Grimes /* 1039b50d902SRodney W. Grimes * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that 1049b50d902SRodney W. Grimes * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given 1059b50d902SRodney W. Grimes * that the smallest argument is 2 bytes in length, this means that 1069b50d902SRodney W. Grimes * the number of arguments is limited to: 1079b50d902SRodney W. Grimes * 1089b50d902SRodney W. Grimes * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2. 1099b50d902SRodney W. Grimes * 1109b50d902SRodney W. Grimes * We arbitrarily limit the number of arguments to 5000. This is 1119b50d902SRodney W. Grimes * allowed by POSIX.2 as long as the resulting minimum exec line is 1129b50d902SRodney W. Grimes * at least LINE_MAX. Realloc'ing as necessary is possible, but 1139b50d902SRodney W. Grimes * probably not worthwhile. 1149b50d902SRodney W. Grimes */ 1159b50d902SRodney W. Grimes nargs = 5000; 116a3e5bc4fSJoseph Koshy if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 117a3e5bc4fSJoseph Koshy errx(1, "sysconf(_SC_ARG_MAX) failed"); 118a3e5bc4fSJoseph Koshy nline = arg_max - 4 * 1024; 11991045075SJuli Mallett while (*ep != NULL) { 120e5009da0SSatoshi Asami /* 1 byte for each '\0' */ 121e5009da0SSatoshi Asami nline -= strlen(*ep++) + 1 + sizeof(*ep); 122e5009da0SSatoshi Asami } 123b50a7286SJuli Mallett while ((ch = getopt(argc, argv, "0E:I:J:L:n:pR:s:tx")) != -1) 1249b50d902SRodney W. Grimes switch(ch) { 125fc17b349SJuli Mallett case 'E': 126fc17b349SJuli Mallett eofstr = optarg; 127fc17b349SJuli Mallett break; 128fc17b349SJuli Mallett case 'I': 129fc17b349SJuli Mallett Iflag = 1; 1304f49da74SJuli Mallett Lflag = 1; 131b50a7286SJuli Mallett Rflag = 5; 132fc17b349SJuli Mallett replstr = optarg; 133fc17b349SJuli Mallett break; 1348d904f15SDima Dorfman case 'J': 135b50a7286SJuli Mallett Jflag = 1; 1368d904f15SDima Dorfman replstr = optarg; 1378d904f15SDima Dorfman break; 138fc17b349SJuli Mallett case 'L': 1394f49da74SJuli Mallett Lflag = atoi(optarg); 140fc17b349SJuli Mallett break; 1419b50d902SRodney W. Grimes case 'n': 1429b50d902SRodney W. Grimes nflag = 1; 1439b50d902SRodney W. Grimes if ((nargs = atoi(optarg)) <= 0) 144a51024e2SPhilippe Charnier errx(1, "illegal argument count"); 1459b50d902SRodney W. Grimes break; 146fc17b349SJuli Mallett case 'p': 147fc17b349SJuli Mallett pflag = 1; 148fc17b349SJuli Mallett break; 149b50a7286SJuli Mallett case 'R': 150b50a7286SJuli Mallett if (!Iflag) 151b50a7286SJuli Mallett usage(); 152b50a7286SJuli Mallett if ((Rflag = atoi(optarg)) <= 0) 153b50a7286SJuli Mallett errx(1, "illegal number of replacements"); 154b50a7286SJuli Mallett break; 1559b50d902SRodney W. Grimes case 's': 1569b50d902SRodney W. Grimes nline = atoi(optarg); 1579b50d902SRodney W. Grimes break; 1589b50d902SRodney W. Grimes case 't': 1599b50d902SRodney W. Grimes tflag = 1; 1609b50d902SRodney W. Grimes break; 1619b50d902SRodney W. Grimes case 'x': 1629b50d902SRodney W. Grimes xflag = 1; 1639b50d902SRodney W. Grimes break; 164d9198881SWarner Losh case '0': 165d9198881SWarner Losh zflag = 1; 166d9198881SWarner Losh break; 1679b50d902SRodney W. Grimes case '?': 1689b50d902SRodney W. Grimes default: 1699b50d902SRodney W. Grimes usage(); 1709b50d902SRodney W. Grimes } 1719b50d902SRodney W. Grimes argc -= optind; 1729b50d902SRodney W. Grimes argv += optind; 1739b50d902SRodney W. Grimes 174b23de8a3SJuli Mallett if (Iflag && Jflag) 175b23de8a3SJuli Mallett errx(1, "the -I and -J options may not be used together"); 1769b50d902SRodney W. Grimes if (xflag && !nflag) 1779b50d902SRodney W. Grimes usage(); 1784f49da74SJuli Mallett if (Iflag || Lflag) 179fc17b349SJuli Mallett xflag = 1; 180fc17b349SJuli Mallett if (replstr != NULL && *replstr == '\0') 181fc17b349SJuli Mallett errx(1, "replstr may not be empty"); 1829b50d902SRodney W. Grimes 1839b50d902SRodney W. Grimes /* 1849b50d902SRodney W. Grimes * Allocate pointers for the utility name, the utility arguments, 1859b50d902SRodney W. Grimes * the maximum arguments to be read from stdin and the trailing 1869b50d902SRodney W. Grimes * NULL. 1879b50d902SRodney W. Grimes */ 188fc17b349SJuli Mallett linelen = 1 + argc + nargs + 1; 1890fa5e8dcSJuli Mallett if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL) 19091045075SJuli Mallett errx(1, "malloc failed"); 1919b50d902SRodney W. Grimes 1929b50d902SRodney W. Grimes /* 1939b50d902SRodney W. Grimes * Use the user's name for the utility as argv[0], just like the 1949b50d902SRodney W. Grimes * shell. Echo is the default. Set up pointers for the user's 1959b50d902SRodney W. Grimes * arguments. 1969b50d902SRodney W. Grimes */ 1973dca1afcSJuli Mallett if (*argv == NULL) 1989bf450b8SJuli Mallett cnt = strlen(*bxp++ = echo); 1999b50d902SRodney W. Grimes else { 2009b50d902SRodney W. Grimes do { 201b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 20291045075SJuli Mallett char **avj; 2038d904f15SDima Dorfman jfound = 1; 2048d904f15SDima Dorfman argv++; 2058d904f15SDima Dorfman for (avj = argv; *avj; avj++) 2068d904f15SDima Dorfman cnt += strlen(*avj) + 1; 2078d904f15SDima Dorfman break; 2088d904f15SDima Dorfman } 2099b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 2103dca1afcSJuli Mallett } while (*++argv != NULL); 2119b50d902SRodney W. Grimes } 2129b50d902SRodney W. Grimes 2139b50d902SRodney W. Grimes /* 2149b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2159b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2169b50d902SRodney W. Grimes * added in an extra slot. 2179b50d902SRodney W. Grimes */ 2189b50d902SRodney W. Grimes exp = (xp = bxp) + nargs; 2199b50d902SRodney W. Grimes 2209b50d902SRodney W. Grimes /* 2219b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2229b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2239b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2249b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2259b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2269b50d902SRodney W. Grimes * slot. 2279b50d902SRodney W. Grimes */ 2289b50d902SRodney W. Grimes nline -= cnt; 2299b50d902SRodney W. Grimes if (nline <= 0) 230a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2319b50d902SRodney W. Grimes 2329bf450b8SJuli Mallett if ((bbp = malloc((size_t)(nline + 1))) == NULL) 23391045075SJuli Mallett errx(1, "malloc failed"); 2349b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 23591045075SJuli Mallett for (;;) 23691045075SJuli Mallett parse_input(argc, argv); 23791045075SJuli Mallett } 2389b50d902SRodney W. Grimes 23991045075SJuli Mallett static void 24091045075SJuli Mallett parse_input(int argc, char **argv) 24191045075SJuli Mallett { 24291045075SJuli Mallett int ch, foundeof; 24391045075SJuli Mallett char **avj; 24491045075SJuli Mallett 24591045075SJuli Mallett foundeof = 0; 24691045075SJuli Mallett 2479b50d902SRodney W. Grimes switch(ch = getchar()) { 2489b50d902SRodney W. Grimes case EOF: 2499b50d902SRodney W. Grimes /* No arguments since last exec. */ 2509b50d902SRodney W. Grimes if (p == bbp) 2519b50d902SRodney W. Grimes exit(rval); 2529b50d902SRodney W. Grimes goto arg1; 2539b50d902SRodney W. Grimes case ' ': 2549b50d902SRodney W. Grimes case '\t': 2559b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 256d9198881SWarner Losh if (insingle || indouble || zflag) 2579b50d902SRodney W. Grimes goto addch; 2589b50d902SRodney W. Grimes goto arg2; 259d9198881SWarner Losh case '\0': 260d9198881SWarner Losh if (zflag) 261d9198881SWarner Losh goto arg2; 262d9198881SWarner Losh goto addch; 2639b50d902SRodney W. Grimes case '\n': 264fc17b349SJuli Mallett count++; 265d9198881SWarner Losh if (zflag) 266d9198881SWarner Losh goto addch; 267d9198881SWarner Losh 2689b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 2699b50d902SRodney W. Grimes arg1: if (insingle || indouble) 270a51024e2SPhilippe Charnier errx(1, "unterminated quote"); 271ef9866beSJean-Marc Zucconi arg2: 272fc17b349SJuli Mallett foundeof = *eofstr != '\0' && 273fc17b349SJuli Mallett strcmp(argp, eofstr) == 0; 274fc17b349SJuli Mallett 275ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 276fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 277ef9866beSJean-Marc Zucconi *p++ = '\0'; 2789b50d902SRodney W. Grimes *xp++ = argp; 279fc17b349SJuli Mallett if (Iflag) { 280fc17b349SJuli Mallett size_t curlen; 281b9b03ba0SJuli Mallett 282b9b03ba0SJuli Mallett if (inpline == NULL) 283fc17b349SJuli Mallett curlen = 0; 284fc17b349SJuli Mallett else { 2851925cb24SJuli Mallett /* 2861925cb24SJuli Mallett * If this string is not zero 2871925cb24SJuli Mallett * length, append a space for 2881925cb24SJuli Mallett * seperation before the next 2891925cb24SJuli Mallett * argument. 2901925cb24SJuli Mallett */ 291b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 292fc17b349SJuli Mallett strcat(inpline, " "); 293fc17b349SJuli Mallett } 294fc17b349SJuli Mallett curlen++; 2951925cb24SJuli Mallett /* 2961925cb24SJuli Mallett * Allocate enough to hold what we will 2975eb40323SJuli Mallett * be holding in a second, and to append 2981925cb24SJuli Mallett * a space next time through, if we have 2991925cb24SJuli Mallett * to. 3001925cb24SJuli Mallett */ 301b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 302b9b03ba0SJuli Mallett strlen(argp)); 303fc17b349SJuli Mallett if (inpline == NULL) 30491045075SJuli Mallett errx(1, "realloc failed"); 305fc17b349SJuli Mallett if (curlen == 1) 306fc17b349SJuli Mallett strcpy(inpline, argp); 307fc17b349SJuli Mallett else 308fc17b349SJuli Mallett strcat(inpline, argp); 309fc17b349SJuli Mallett } 310ef9866beSJean-Marc Zucconi } 3119b50d902SRodney W. Grimes 3129b50d902SRodney W. Grimes /* 3139b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 3149b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 3151925cb24SJuli Mallett * but not on args, object. Having reached the limit 3161925cb24SJuli Mallett * of input lines, as specified by -L is the same as 3171925cb24SJuli Mallett * maxing out on arguments. 3189b50d902SRodney W. Grimes */ 3195eb40323SJuli Mallett if (xp == exp || p > ebp || ch == EOF || 3205eb40323SJuli Mallett (Lflag <= count && xflag) || foundeof) { 321ef9866beSJean-Marc Zucconi if (xflag && xp != exp && p > ebp) 322a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3238d904f15SDima Dorfman if (jfound) { 3248d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3258d904f15SDima Dorfman *xp++ = *avj; 3268d904f15SDima Dorfman } 32791045075SJuli Mallett prerun(argc, av); 328fc17b349SJuli Mallett if (ch == EOF || foundeof) 3299b50d902SRodney W. Grimes exit(rval); 3309b50d902SRodney W. Grimes p = bbp; 3319b50d902SRodney W. Grimes xp = bxp; 332fc17b349SJuli Mallett count = 0; 333ef9866beSJean-Marc Zucconi } 3349b50d902SRodney W. Grimes argp = p; 335ef9866beSJean-Marc Zucconi wasquoted = 0; 3369b50d902SRodney W. Grimes break; 3379b50d902SRodney W. Grimes case '\'': 338d9198881SWarner Losh if (indouble || zflag) 3399b50d902SRodney W. Grimes goto addch; 3409b50d902SRodney W. Grimes insingle = !insingle; 341ef9866beSJean-Marc Zucconi wasquoted = 1; 3429b50d902SRodney W. Grimes break; 3439b50d902SRodney W. Grimes case '"': 344d9198881SWarner Losh if (insingle || zflag) 3459b50d902SRodney W. Grimes goto addch; 3469b50d902SRodney W. Grimes indouble = !indouble; 347ef9866beSJean-Marc Zucconi wasquoted = 1; 3489b50d902SRodney W. Grimes break; 3499b50d902SRodney W. Grimes case '\\': 350d9198881SWarner Losh if (zflag) 351d9198881SWarner Losh goto addch; 3529b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 3539b50d902SRodney W. Grimes if (!insingle && !indouble && (ch = getchar()) == EOF) 354a51024e2SPhilippe Charnier errx(1, "backslash at EOF"); 3559b50d902SRodney W. Grimes /* FALLTHROUGH */ 3569b50d902SRodney W. Grimes default: 3579b50d902SRodney W. Grimes addch: if (p < ebp) { 3589b50d902SRodney W. Grimes *p++ = ch; 3599b50d902SRodney W. Grimes break; 3609b50d902SRodney W. Grimes } 3619b50d902SRodney W. Grimes 3629b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 3639b50d902SRodney W. Grimes if (bxp == xp) 364a51024e2SPhilippe Charnier errx(1, "insufficient space for argument"); 3659b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 3669b50d902SRodney W. Grimes if (xflag) 367a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3689b50d902SRodney W. Grimes 3698d904f15SDima Dorfman if (jfound) { 3708d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3718d904f15SDima Dorfman *xp++ = *avj; 3728d904f15SDima Dorfman } 37391045075SJuli Mallett prerun(argc, av); 3749b50d902SRodney W. Grimes xp = bxp; 3759b50d902SRodney W. Grimes cnt = ebp - argp; 376fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 3779b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 3789b50d902SRodney W. Grimes *p++ = ch; 3799b50d902SRodney W. Grimes break; 3809b50d902SRodney W. Grimes } 38191045075SJuli Mallett return; 3829b50d902SRodney W. Grimes } 3839b50d902SRodney W. Grimes 384263dc775SJuli Mallett /* 385263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 386263dc775SJuli Mallett * and then call run(). 387263dc775SJuli Mallett */ 388263dc775SJuli Mallett static void 38991045075SJuli Mallett prerun(int argc, char **argv) 390263dc775SJuli Mallett { 391263dc775SJuli Mallett char **tmp, **tmp2, **avj; 39291045075SJuli Mallett int repls; 39391045075SJuli Mallett 39491045075SJuli Mallett repls = Rflag; 395263dc775SJuli Mallett 396be70f7d4SJuli Mallett if (argc == 0 || repls == 0) { 397263dc775SJuli Mallett *xp = NULL; 398263dc775SJuli Mallett run(argv); 399263dc775SJuli Mallett return; 400263dc775SJuli Mallett } 401263dc775SJuli Mallett 402263dc775SJuli Mallett avj = argv; 403263dc775SJuli Mallett 404263dc775SJuli Mallett /* 405263dc775SJuli Mallett * Allocate memory to hold the argument list, and 406263dc775SJuli Mallett * a NULL at the tail. 407263dc775SJuli Mallett */ 408b6594dbaSJuli Mallett tmp = malloc((argc + 1) * sizeof(char**)); 409263dc775SJuli Mallett if (tmp == NULL) 41091045075SJuli Mallett errx(1, "malloc failed"); 411263dc775SJuli Mallett tmp2 = tmp; 412263dc775SJuli Mallett 413263dc775SJuli Mallett /* 414263dc775SJuli Mallett * Save the first argument and iterate over it, we 415263dc775SJuli Mallett * cannot do strnsubst() to it. 416263dc775SJuli Mallett */ 417263dc775SJuli Mallett if ((*tmp++ = strdup(*avj++)) == NULL) 41891045075SJuli Mallett errx(1, "strdup failed"); 419263dc775SJuli Mallett 420263dc775SJuli Mallett /* 421263dc775SJuli Mallett * For each argument to utility, if we have not used up 422263dc775SJuli Mallett * the number of replacements we are allowed to do, and 423263dc775SJuli Mallett * if the argument contains at least one occurance of 424263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 425263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 426263dc775SJuli Mallett * where appropriate. 427263dc775SJuli Mallett */ 428263dc775SJuli Mallett while (--argc) { 429263dc775SJuli Mallett *tmp = *avj++; 430263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 43191045075SJuli Mallett strnsubst(tmp++, replstr, inpline, (size_t)255); 432263dc775SJuli Mallett repls--; 433263dc775SJuli Mallett } else { 434263dc775SJuli Mallett if ((*tmp = strdup(*tmp)) == NULL) 43591045075SJuli Mallett errx(1, "strdup failed"); 436263dc775SJuli Mallett tmp++; 437263dc775SJuli Mallett } 438263dc775SJuli Mallett } 439263dc775SJuli Mallett 440263dc775SJuli Mallett /* 441263dc775SJuli Mallett * Run it. 442263dc775SJuli Mallett */ 443b6594dbaSJuli Mallett *tmp = NULL; 444263dc775SJuli Mallett run(tmp2); 445263dc775SJuli Mallett 446263dc775SJuli Mallett /* 447263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 448263dc775SJuli Mallett */ 449263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 450263dc775SJuli Mallett free(*tmp); 451263dc775SJuli Mallett /* 452263dc775SJuli Mallett * Now free the list itself. 453263dc775SJuli Mallett */ 454263dc775SJuli Mallett free(tmp2); 455263dc775SJuli Mallett 456263dc775SJuli Mallett /* 457263dc775SJuli Mallett * Free the input line buffer, and create a new dummy. 458263dc775SJuli Mallett */ 45991045075SJuli Mallett free(inpline); 46091045075SJuli Mallett inpline = strdup(""); 461263dc775SJuli Mallett } 462263dc775SJuli Mallett 463fc17b349SJuli Mallett static void 46473385ac6SJuli Mallett run(char **argv) 4659b50d902SRodney W. Grimes { 4668ad749a4SDag-Erling Smørgrav volatile int childerr; 46791045075SJuli Mallett char **avec; 4689b50d902SRodney W. Grimes pid_t pid; 469305e39f4SJuli Mallett int status; 4709b50d902SRodney W. Grimes 471305e39f4SJuli Mallett /* 472305e39f4SJuli Mallett * If the user wants to be notified of each command before it is 473305e39f4SJuli Mallett * executed, notify them. If they want the notification to be 474305e39f4SJuli Mallett * followed by a prompt, then prompt them. 475305e39f4SJuli Mallett */ 476fc17b349SJuli Mallett if (tflag || pflag) { 4779b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 47891045075SJuli Mallett for (avec = argv + 1; *avec != NULL; ++avec) 47991045075SJuli Mallett (void)fprintf(stderr, " %s", *avec); 480305e39f4SJuli Mallett /* 481305e39f4SJuli Mallett * If the user has asked to be prompted, do so. 482305e39f4SJuli Mallett */ 483305e39f4SJuli Mallett if (pflag) 484305e39f4SJuli Mallett /* 485305e39f4SJuli Mallett * If they asked not to exec, return without execution 486305e39f4SJuli Mallett * but if they asked to, go to the execution. If we 487305e39f4SJuli Mallett * could not open their tty, break the switch and drop 488305e39f4SJuli Mallett * back to -t behaviour. 489305e39f4SJuli Mallett */ 490305e39f4SJuli Mallett switch (prompt()) { 491305e39f4SJuli Mallett case 0: 492fc17b349SJuli Mallett return; 493305e39f4SJuli Mallett case 1: 494305e39f4SJuli Mallett goto exec; 495305e39f4SJuli Mallett case 2: 496305e39f4SJuli Mallett break; 497305e39f4SJuli Mallett } 4989b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 4999b50d902SRodney W. Grimes (void)fflush(stderr); 5009b50d902SRodney W. Grimes } 501305e39f4SJuli Mallett exec: 5028ad749a4SDag-Erling Smørgrav childerr = 0; 5038ad749a4SDag-Erling Smørgrav switch(pid = vfork()) { 5049b50d902SRodney W. Grimes case -1: 5058ad749a4SDag-Erling Smørgrav err(1, "vfork"); 5069b50d902SRodney W. Grimes case 0: 5079b50d902SRodney W. Grimes execvp(argv[0], argv); 5088ad749a4SDag-Erling Smørgrav childerr = errno; 5099b50d902SRodney W. Grimes _exit(1); 5109b50d902SRodney W. Grimes } 5119b50d902SRodney W. Grimes pid = waitpid(pid, &status, 0); 5129b50d902SRodney W. Grimes if (pid == -1) 513a51024e2SPhilippe Charnier err(1, "waitpid"); 514fc17b349SJuli Mallett /* If we couldn't invoke the utility, exit. */ 515fc17b349SJuli Mallett if (childerr != 0) 516fc17b349SJuli Mallett err(childerr == ENOENT ? 127 : 126, "%s", *argv); 5179b50d902SRodney W. Grimes /* If utility signaled or exited with a value of 255, exit 1-125. */ 5189b50d902SRodney W. Grimes if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255) 5199b50d902SRodney W. Grimes exit(1); 5209b50d902SRodney W. Grimes if (WEXITSTATUS(status)) 5219b50d902SRodney W. Grimes rval = 1; 5229b50d902SRodney W. Grimes } 5239b50d902SRodney W. Grimes 524305e39f4SJuli Mallett /* 525305e39f4SJuli Mallett * Prompt the user about running a command. 526305e39f4SJuli Mallett */ 527305e39f4SJuli Mallett static int 528305e39f4SJuli Mallett prompt(void) 529305e39f4SJuli Mallett { 530305e39f4SJuli Mallett regex_t cre; 531305e39f4SJuli Mallett size_t rsize; 532305e39f4SJuli Mallett int match; 533305e39f4SJuli Mallett char *response; 534305e39f4SJuli Mallett FILE *ttyfp; 535305e39f4SJuli Mallett 536305e39f4SJuli Mallett if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) 537305e39f4SJuli Mallett return (2); /* Indicate that the TTY failed to open. */ 538305e39f4SJuli Mallett (void)fprintf(stderr, "?..."); 539305e39f4SJuli Mallett (void)fflush(stderr); 540305e39f4SJuli Mallett if ((response = fgetln(ttyfp, &rsize)) == NULL || 5418e0a87c1SRuslan Ermilov regcomp(&cre, 5428e0a87c1SRuslan Ermilov #ifdef BOOTSTRAPPING 5438e0a87c1SRuslan Ermilov "^[yY]", 5448e0a87c1SRuslan Ermilov #else 5458e0a87c1SRuslan Ermilov nl_langinfo(YESEXPR), 5468e0a87c1SRuslan Ermilov #endif 5478e0a87c1SRuslan Ermilov REG_BASIC) != 0) { 548305e39f4SJuli Mallett (void)fclose(ttyfp); 549305e39f4SJuli Mallett return (0); 550305e39f4SJuli Mallett } 551305e39f4SJuli Mallett match = regexec(&cre, response, 0, NULL, 0); 552305e39f4SJuli Mallett (void)fclose(ttyfp); 553305e39f4SJuli Mallett regfree(&cre); 554305e39f4SJuli Mallett return (match == 0); 555305e39f4SJuli Mallett } 556305e39f4SJuli Mallett 557a51024e2SPhilippe Charnier static void 55873385ac6SJuli Mallett usage(void) 5599b50d902SRodney W. Grimes { 5608d904f15SDima Dorfman fprintf(stderr, 561b50a7286SJuli Mallett "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n" 562b50a7286SJuli Mallett " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n"); 5639b50d902SRodney W. Grimes exit(1); 5649b50d902SRodney W. Grimes } 565