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 5440c6b893SRuslan Ermilov #include <sys/param.h> 559b50d902SRodney W. Grimes #include <sys/wait.h> 5616b07a33SMark Murray 57a51024e2SPhilippe Charnier #include <err.h> 588ad749a4SDag-Erling Smørgrav #include <errno.h> 5998186e89SMaxime Henrion #include <fcntl.h> 6040c6b893SRuslan Ermilov #if (__FreeBSD_version >= 450002 && __FreeBSD_version < 500000) || \ 6140c6b893SRuslan Ermilov __FreeBSD_version >= 500017 62305e39f4SJuli Mallett #include <langinfo.h> 638e0a87c1SRuslan Ermilov #endif 64305e39f4SJuli Mallett #include <locale.h> 65305e39f4SJuli Mallett #include <paths.h> 66305e39f4SJuli Mallett #include <regex.h> 679b50d902SRodney W. Grimes #include <stdio.h> 689b50d902SRodney W. Grimes #include <stdlib.h> 699b50d902SRodney W. Grimes #include <string.h> 709b50d902SRodney W. Grimes #include <unistd.h> 7116b07a33SMark Murray 729b50d902SRodney W. Grimes #include "pathnames.h" 739b50d902SRodney W. Grimes 741926d4aaSJuli Mallett static void parse_input(int, char *[]); 751926d4aaSJuli Mallett static void prerun(int, char *[]); 76305e39f4SJuli Mallett static int prompt(void); 77fc17b349SJuli Mallett static void run(char **); 7873385ac6SJuli Mallett static void usage(void); 79fc17b349SJuli Mallett void strnsubst(char **, const char *, const char *, size_t); 80330d23f5STim J. Robbins static void waitchildren(const char *, int); 819b50d902SRodney W. Grimes 8216b07a33SMark Murray static char echo[] = _PATH_ECHO; 8391045075SJuli Mallett static char **av, **bxp, **ep, **exp, **xp; 8491045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr; 8591045075SJuli Mallett static const char *eofstr; 8698186e89SMaxime Henrion static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag; 8791045075SJuli Mallett static int cnt, Iflag, jfound, Lflag, wasquoted, xflag; 88330d23f5STim J. Robbins static int curprocs, maxprocs; 89330d23f5STim J. Robbins 90330d23f5STim J. Robbins static volatile int childerr; 9116b07a33SMark Murray 9299a84ce1STim J. Robbins extern char **environ; 9373385ac6SJuli Mallett 94a51024e2SPhilippe Charnier int 951926d4aaSJuli Mallett main(int argc, char *argv[]) 969b50d902SRodney W. Grimes { 97a3e5bc4fSJoseph Koshy long arg_max; 9891045075SJuli Mallett int ch, Jflag, nargs, nflag, nline; 99fc17b349SJuli Mallett size_t linelen; 1003c675167SJuli Mallett char *endptr; 1019b50d902SRodney W. Grimes 102fc17b349SJuli Mallett inpline = replstr = NULL; 10391045075SJuli Mallett ep = environ; 104fc17b349SJuli Mallett eofstr = ""; 10591045075SJuli Mallett Jflag = nflag = 0; 1068d904f15SDima Dorfman 107305e39f4SJuli Mallett (void)setlocale(LC_MESSAGES, ""); 108305e39f4SJuli Mallett 1099b50d902SRodney W. Grimes /* 1109b50d902SRodney W. Grimes * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that 1119b50d902SRodney W. Grimes * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given 1129b50d902SRodney W. Grimes * that the smallest argument is 2 bytes in length, this means that 1139b50d902SRodney W. Grimes * the number of arguments is limited to: 1149b50d902SRodney W. Grimes * 1159b50d902SRodney W. Grimes * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2. 1169b50d902SRodney W. Grimes * 1179b50d902SRodney W. Grimes * We arbitrarily limit the number of arguments to 5000. This is 1189b50d902SRodney W. Grimes * allowed by POSIX.2 as long as the resulting minimum exec line is 1199b50d902SRodney W. Grimes * at least LINE_MAX. Realloc'ing as necessary is possible, but 1209b50d902SRodney W. Grimes * probably not worthwhile. 1219b50d902SRodney W. Grimes */ 1229b50d902SRodney W. Grimes nargs = 5000; 123a3e5bc4fSJoseph Koshy if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 124a3e5bc4fSJoseph Koshy errx(1, "sysconf(_SC_ARG_MAX) failed"); 125a3e5bc4fSJoseph Koshy nline = arg_max - 4 * 1024; 12691045075SJuli Mallett while (*ep != NULL) { 127e5009da0SSatoshi Asami /* 1 byte for each '\0' */ 128e5009da0SSatoshi Asami nline -= strlen(*ep++) + 1 + sizeof(*ep); 129e5009da0SSatoshi Asami } 130330d23f5STim J. Robbins maxprocs = 1; 13198186e89SMaxime Henrion while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:s:tx")) != -1) 1329b50d902SRodney W. Grimes switch(ch) { 133fc17b349SJuli Mallett case 'E': 134fc17b349SJuli Mallett eofstr = optarg; 135fc17b349SJuli Mallett break; 136fc17b349SJuli Mallett case 'I': 1376ea89183SJuli Mallett Jflag = 0; 138fc17b349SJuli Mallett Iflag = 1; 1394f49da74SJuli Mallett Lflag = 1; 140fc17b349SJuli Mallett replstr = optarg; 141fc17b349SJuli Mallett break; 1428d904f15SDima Dorfman case 'J': 1436ea89183SJuli Mallett Iflag = 0; 144b50a7286SJuli Mallett Jflag = 1; 1458d904f15SDima Dorfman replstr = optarg; 1468d904f15SDima Dorfman break; 147fc17b349SJuli Mallett case 'L': 1484f49da74SJuli Mallett Lflag = atoi(optarg); 149fc17b349SJuli Mallett break; 1509b50d902SRodney W. Grimes case 'n': 1519b50d902SRodney W. Grimes nflag = 1; 1529b50d902SRodney W. Grimes if ((nargs = atoi(optarg)) <= 0) 153a51024e2SPhilippe Charnier errx(1, "illegal argument count"); 1549b50d902SRodney W. Grimes break; 15598186e89SMaxime Henrion case 'o': 15698186e89SMaxime Henrion oflag = 1; 15798186e89SMaxime Henrion break; 158330d23f5STim J. Robbins case 'P': 159330d23f5STim J. Robbins if ((maxprocs = atoi(optarg)) <= 0) 160330d23f5STim J. Robbins errx(1, "max. processes must be >0"); 161330d23f5STim J. Robbins break; 162fc17b349SJuli Mallett case 'p': 163fc17b349SJuli Mallett pflag = 1; 164fc17b349SJuli Mallett break; 165b50a7286SJuli Mallett case 'R': 1663c675167SJuli Mallett Rflag = strtol(optarg, &endptr, 10); 1673c675167SJuli Mallett if (*endptr != '\0') 1683c675167SJuli Mallett errx(1, "replacements must be a number"); 169b50a7286SJuli Mallett break; 1709b50d902SRodney W. Grimes case 's': 1719b50d902SRodney W. Grimes nline = atoi(optarg); 1729b50d902SRodney W. Grimes break; 1739b50d902SRodney W. Grimes case 't': 1749b50d902SRodney W. Grimes tflag = 1; 1759b50d902SRodney W. Grimes break; 1769b50d902SRodney W. Grimes case 'x': 1779b50d902SRodney W. Grimes xflag = 1; 1789b50d902SRodney W. Grimes break; 179d9198881SWarner Losh case '0': 180d9198881SWarner Losh zflag = 1; 181d9198881SWarner Losh break; 1829b50d902SRodney W. Grimes case '?': 1839b50d902SRodney W. Grimes default: 1849b50d902SRodney W. Grimes usage(); 1859b50d902SRodney W. Grimes } 1869b50d902SRodney W. Grimes argc -= optind; 1879b50d902SRodney W. Grimes argv += optind; 1889b50d902SRodney W. Grimes 1896ea89183SJuli Mallett if (!Iflag && Rflag) 1906ea89183SJuli Mallett usage(); 1916ea89183SJuli Mallett if (Iflag && !Rflag) 1926ea89183SJuli Mallett Rflag = 5; 1939b50d902SRodney W. Grimes if (xflag && !nflag) 1949b50d902SRodney W. Grimes usage(); 1954f49da74SJuli Mallett if (Iflag || Lflag) 196fc17b349SJuli Mallett xflag = 1; 197fc17b349SJuli Mallett if (replstr != NULL && *replstr == '\0') 198fc17b349SJuli Mallett errx(1, "replstr may not be empty"); 1999b50d902SRodney W. Grimes 2009b50d902SRodney W. Grimes /* 2019b50d902SRodney W. Grimes * Allocate pointers for the utility name, the utility arguments, 2029b50d902SRodney W. Grimes * the maximum arguments to be read from stdin and the trailing 2039b50d902SRodney W. Grimes * NULL. 2049b50d902SRodney W. Grimes */ 205fc17b349SJuli Mallett linelen = 1 + argc + nargs + 1; 2060fa5e8dcSJuli Mallett if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL) 20791045075SJuli Mallett errx(1, "malloc failed"); 2089b50d902SRodney W. Grimes 2099b50d902SRodney W. Grimes /* 2109b50d902SRodney W. Grimes * Use the user's name for the utility as argv[0], just like the 2119b50d902SRodney W. Grimes * shell. Echo is the default. Set up pointers for the user's 2129b50d902SRodney W. Grimes * arguments. 2139b50d902SRodney W. Grimes */ 2143dca1afcSJuli Mallett if (*argv == NULL) 2159bf450b8SJuli Mallett cnt = strlen(*bxp++ = echo); 2169b50d902SRodney W. Grimes else { 2179b50d902SRodney W. Grimes do { 218b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 21991045075SJuli Mallett char **avj; 2208d904f15SDima Dorfman jfound = 1; 2218d904f15SDima Dorfman argv++; 2228d904f15SDima Dorfman for (avj = argv; *avj; avj++) 2238d904f15SDima Dorfman cnt += strlen(*avj) + 1; 2248d904f15SDima Dorfman break; 2258d904f15SDima Dorfman } 2269b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 2273dca1afcSJuli Mallett } while (*++argv != NULL); 2289b50d902SRodney W. Grimes } 2299b50d902SRodney W. Grimes 2309b50d902SRodney W. Grimes /* 2319b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2329b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2339b50d902SRodney W. Grimes * added in an extra slot. 2349b50d902SRodney W. Grimes */ 2359b50d902SRodney W. Grimes exp = (xp = bxp) + nargs; 2369b50d902SRodney W. Grimes 2379b50d902SRodney W. Grimes /* 2389b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2399b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2409b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2419b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2429b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2439b50d902SRodney W. Grimes * slot. 2449b50d902SRodney W. Grimes */ 2459b50d902SRodney W. Grimes nline -= cnt; 2469b50d902SRodney W. Grimes if (nline <= 0) 247a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2489b50d902SRodney W. Grimes 2499bf450b8SJuli Mallett if ((bbp = malloc((size_t)(nline + 1))) == NULL) 25091045075SJuli Mallett errx(1, "malloc failed"); 2519b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 25291045075SJuli Mallett for (;;) 25391045075SJuli Mallett parse_input(argc, argv); 25491045075SJuli Mallett } 2559b50d902SRodney W. Grimes 25691045075SJuli Mallett static void 2571926d4aaSJuli Mallett parse_input(int argc, char *argv[]) 25891045075SJuli Mallett { 25991045075SJuli Mallett int ch, foundeof; 26091045075SJuli Mallett char **avj; 26191045075SJuli Mallett 26291045075SJuli Mallett foundeof = 0; 26391045075SJuli Mallett 2649b50d902SRodney W. Grimes switch(ch = getchar()) { 2659b50d902SRodney W. Grimes case EOF: 2669b50d902SRodney W. Grimes /* No arguments since last exec. */ 267330d23f5STim J. Robbins if (p == bbp) { 268330d23f5STim J. Robbins waitchildren(*argv, 1); 2699b50d902SRodney W. Grimes exit(rval); 270330d23f5STim J. Robbins } 2719b50d902SRodney W. Grimes goto arg1; 2729b50d902SRodney W. Grimes case ' ': 2739b50d902SRodney W. Grimes case '\t': 2749b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 275d9198881SWarner Losh if (insingle || indouble || zflag) 2769b50d902SRodney W. Grimes goto addch; 2779b50d902SRodney W. Grimes goto arg2; 278d9198881SWarner Losh case '\0': 279d9198881SWarner Losh if (zflag) 280d9198881SWarner Losh goto arg2; 281d9198881SWarner Losh goto addch; 2829b50d902SRodney W. Grimes case '\n': 283fc17b349SJuli Mallett count++; 284d9198881SWarner Losh if (zflag) 285d9198881SWarner Losh goto addch; 286d9198881SWarner Losh 2879b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 2889b50d902SRodney W. Grimes arg1: if (insingle || indouble) 289a51024e2SPhilippe Charnier errx(1, "unterminated quote"); 290ef9866beSJean-Marc Zucconi arg2: 291fc17b349SJuli Mallett foundeof = *eofstr != '\0' && 292fc17b349SJuli Mallett strcmp(argp, eofstr) == 0; 293fc17b349SJuli Mallett 294ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 295fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 296ef9866beSJean-Marc Zucconi *p++ = '\0'; 2979b50d902SRodney W. Grimes *xp++ = argp; 298fc17b349SJuli Mallett if (Iflag) { 299fc17b349SJuli Mallett size_t curlen; 300b9b03ba0SJuli Mallett 301b9b03ba0SJuli Mallett if (inpline == NULL) 302fc17b349SJuli Mallett curlen = 0; 303fc17b349SJuli Mallett else { 3041925cb24SJuli Mallett /* 3051925cb24SJuli Mallett * If this string is not zero 3061925cb24SJuli Mallett * length, append a space for 3072b239dd1SJens Schweikhardt * separation before the next 3081925cb24SJuli Mallett * argument. 3091925cb24SJuli Mallett */ 310b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 311fc17b349SJuli Mallett strcat(inpline, " "); 312fc17b349SJuli Mallett } 313fc17b349SJuli Mallett curlen++; 3141925cb24SJuli Mallett /* 3151925cb24SJuli Mallett * Allocate enough to hold what we will 3165eb40323SJuli Mallett * be holding in a second, and to append 3171925cb24SJuli Mallett * a space next time through, if we have 3181925cb24SJuli Mallett * to. 3191925cb24SJuli Mallett */ 320b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 321b9b03ba0SJuli Mallett strlen(argp)); 322fc17b349SJuli Mallett if (inpline == NULL) 32391045075SJuli Mallett errx(1, "realloc failed"); 324fc17b349SJuli Mallett if (curlen == 1) 325fc17b349SJuli Mallett strcpy(inpline, argp); 326fc17b349SJuli Mallett else 327fc17b349SJuli Mallett strcat(inpline, argp); 328fc17b349SJuli Mallett } 329ef9866beSJean-Marc Zucconi } 3309b50d902SRodney W. Grimes 3319b50d902SRodney W. Grimes /* 3329b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 3339b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 3341925cb24SJuli Mallett * but not on args, object. Having reached the limit 3351925cb24SJuli Mallett * of input lines, as specified by -L is the same as 3361925cb24SJuli Mallett * maxing out on arguments. 3379b50d902SRodney W. Grimes */ 3385eb40323SJuli Mallett if (xp == exp || p > ebp || ch == EOF || 3395eb40323SJuli Mallett (Lflag <= count && xflag) || foundeof) { 340ef9866beSJean-Marc Zucconi if (xflag && xp != exp && p > ebp) 341a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3428d904f15SDima Dorfman if (jfound) { 3438d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3448d904f15SDima Dorfman *xp++ = *avj; 3458d904f15SDima Dorfman } 34691045075SJuli Mallett prerun(argc, av); 347330d23f5STim J. Robbins if (ch == EOF || foundeof) { 348330d23f5STim J. Robbins waitchildren(*argv, 1); 3499b50d902SRodney W. Grimes exit(rval); 350330d23f5STim J. Robbins } 3519b50d902SRodney W. Grimes p = bbp; 3529b50d902SRodney W. Grimes xp = bxp; 353fc17b349SJuli Mallett count = 0; 354ef9866beSJean-Marc Zucconi } 3559b50d902SRodney W. Grimes argp = p; 356ef9866beSJean-Marc Zucconi wasquoted = 0; 3579b50d902SRodney W. Grimes break; 3589b50d902SRodney W. Grimes case '\'': 359d9198881SWarner Losh if (indouble || zflag) 3609b50d902SRodney W. Grimes goto addch; 3619b50d902SRodney W. Grimes insingle = !insingle; 362ef9866beSJean-Marc Zucconi wasquoted = 1; 3639b50d902SRodney W. Grimes break; 3649b50d902SRodney W. Grimes case '"': 365d9198881SWarner Losh if (insingle || zflag) 3669b50d902SRodney W. Grimes goto addch; 3679b50d902SRodney W. Grimes indouble = !indouble; 368ef9866beSJean-Marc Zucconi wasquoted = 1; 3699b50d902SRodney W. Grimes break; 3709b50d902SRodney W. Grimes case '\\': 371d9198881SWarner Losh if (zflag) 372d9198881SWarner Losh goto addch; 3739b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 3749b50d902SRodney W. Grimes if (!insingle && !indouble && (ch = getchar()) == EOF) 375a51024e2SPhilippe Charnier errx(1, "backslash at EOF"); 3769b50d902SRodney W. Grimes /* FALLTHROUGH */ 3779b50d902SRodney W. Grimes default: 3789b50d902SRodney W. Grimes addch: if (p < ebp) { 3799b50d902SRodney W. Grimes *p++ = ch; 3809b50d902SRodney W. Grimes break; 3819b50d902SRodney W. Grimes } 3829b50d902SRodney W. Grimes 3839b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 3849b50d902SRodney W. Grimes if (bxp == xp) 385a51024e2SPhilippe Charnier errx(1, "insufficient space for argument"); 3869b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 3879b50d902SRodney W. Grimes if (xflag) 388a51024e2SPhilippe Charnier errx(1, "insufficient space for arguments"); 3899b50d902SRodney W. Grimes 3908d904f15SDima Dorfman if (jfound) { 3918d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3928d904f15SDima Dorfman *xp++ = *avj; 3938d904f15SDima Dorfman } 39491045075SJuli Mallett prerun(argc, av); 3959b50d902SRodney W. Grimes xp = bxp; 3969b50d902SRodney W. Grimes cnt = ebp - argp; 397fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 3989b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 3999b50d902SRodney W. Grimes *p++ = ch; 4009b50d902SRodney W. Grimes break; 4019b50d902SRodney W. Grimes } 40291045075SJuli Mallett return; 4039b50d902SRodney W. Grimes } 4049b50d902SRodney W. Grimes 405263dc775SJuli Mallett /* 406263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 407263dc775SJuli Mallett * and then call run(). 408263dc775SJuli Mallett */ 409263dc775SJuli Mallett static void 4101926d4aaSJuli Mallett prerun(int argc, char *argv[]) 411263dc775SJuli Mallett { 412263dc775SJuli Mallett char **tmp, **tmp2, **avj; 41391045075SJuli Mallett int repls; 41491045075SJuli Mallett 41591045075SJuli Mallett repls = Rflag; 416263dc775SJuli Mallett 417be70f7d4SJuli Mallett if (argc == 0 || repls == 0) { 418263dc775SJuli Mallett *xp = NULL; 419263dc775SJuli Mallett run(argv); 420263dc775SJuli Mallett return; 421263dc775SJuli Mallett } 422263dc775SJuli Mallett 423263dc775SJuli Mallett avj = argv; 424263dc775SJuli Mallett 425263dc775SJuli Mallett /* 426263dc775SJuli Mallett * Allocate memory to hold the argument list, and 427263dc775SJuli Mallett * a NULL at the tail. 428263dc775SJuli Mallett */ 429b6594dbaSJuli Mallett tmp = malloc((argc + 1) * sizeof(char**)); 430263dc775SJuli Mallett if (tmp == NULL) 43191045075SJuli Mallett errx(1, "malloc failed"); 432263dc775SJuli Mallett tmp2 = tmp; 433263dc775SJuli Mallett 434263dc775SJuli Mallett /* 435263dc775SJuli Mallett * Save the first argument and iterate over it, we 436263dc775SJuli Mallett * cannot do strnsubst() to it. 437263dc775SJuli Mallett */ 438263dc775SJuli Mallett if ((*tmp++ = strdup(*avj++)) == NULL) 43991045075SJuli Mallett errx(1, "strdup failed"); 440263dc775SJuli Mallett 441263dc775SJuli Mallett /* 442263dc775SJuli Mallett * For each argument to utility, if we have not used up 443263dc775SJuli Mallett * the number of replacements we are allowed to do, and 4442b239dd1SJens Schweikhardt * if the argument contains at least one occurrence of 445263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 446263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 447263dc775SJuli Mallett * where appropriate. 448263dc775SJuli Mallett */ 449263dc775SJuli Mallett while (--argc) { 450263dc775SJuli Mallett *tmp = *avj++; 451263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 45291045075SJuli Mallett strnsubst(tmp++, replstr, inpline, (size_t)255); 4533c675167SJuli Mallett if (repls > 0) 454263dc775SJuli Mallett repls--; 455263dc775SJuli Mallett } else { 456263dc775SJuli Mallett if ((*tmp = strdup(*tmp)) == NULL) 45791045075SJuli Mallett errx(1, "strdup failed"); 458263dc775SJuli Mallett tmp++; 459263dc775SJuli Mallett } 460263dc775SJuli Mallett } 461263dc775SJuli Mallett 462263dc775SJuli Mallett /* 463263dc775SJuli Mallett * Run it. 464263dc775SJuli Mallett */ 465b6594dbaSJuli Mallett *tmp = NULL; 466263dc775SJuli Mallett run(tmp2); 467263dc775SJuli Mallett 468263dc775SJuli Mallett /* 469263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 470263dc775SJuli Mallett */ 471263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 472263dc775SJuli Mallett free(*tmp); 473263dc775SJuli Mallett /* 474263dc775SJuli Mallett * Now free the list itself. 475263dc775SJuli Mallett */ 476263dc775SJuli Mallett free(tmp2); 477263dc775SJuli Mallett 478263dc775SJuli Mallett /* 479986d829bSJuli Mallett * Free the input line buffer, if we have one. 480263dc775SJuli Mallett */ 48151f7a48bSJuli Mallett if (inpline != NULL) { 48291045075SJuli Mallett free(inpline); 48351f7a48bSJuli Mallett inpline = NULL; 48451f7a48bSJuli Mallett } 485263dc775SJuli Mallett } 486263dc775SJuli Mallett 487fc17b349SJuli Mallett static void 48873385ac6SJuli Mallett run(char **argv) 4899b50d902SRodney W. Grimes { 4909b50d902SRodney W. Grimes pid_t pid; 491330d23f5STim J. Robbins char **avec; 4929b50d902SRodney W. Grimes 493305e39f4SJuli Mallett /* 494305e39f4SJuli Mallett * If the user wants to be notified of each command before it is 495305e39f4SJuli Mallett * executed, notify them. If they want the notification to be 496305e39f4SJuli Mallett * followed by a prompt, then prompt them. 497305e39f4SJuli Mallett */ 498fc17b349SJuli Mallett if (tflag || pflag) { 4999b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 50091045075SJuli Mallett for (avec = argv + 1; *avec != NULL; ++avec) 50191045075SJuli Mallett (void)fprintf(stderr, " %s", *avec); 502305e39f4SJuli Mallett /* 503305e39f4SJuli Mallett * If the user has asked to be prompted, do so. 504305e39f4SJuli Mallett */ 505305e39f4SJuli Mallett if (pflag) 506305e39f4SJuli Mallett /* 507305e39f4SJuli Mallett * If they asked not to exec, return without execution 508305e39f4SJuli Mallett * but if they asked to, go to the execution. If we 509305e39f4SJuli Mallett * could not open their tty, break the switch and drop 510305e39f4SJuli Mallett * back to -t behaviour. 511305e39f4SJuli Mallett */ 512305e39f4SJuli Mallett switch (prompt()) { 513305e39f4SJuli Mallett case 0: 514fc17b349SJuli Mallett return; 515305e39f4SJuli Mallett case 1: 516305e39f4SJuli Mallett goto exec; 517305e39f4SJuli Mallett case 2: 518305e39f4SJuli Mallett break; 519305e39f4SJuli Mallett } 5209b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 5219b50d902SRodney W. Grimes (void)fflush(stderr); 5229b50d902SRodney W. Grimes } 523305e39f4SJuli Mallett exec: 5248ad749a4SDag-Erling Smørgrav childerr = 0; 5258ad749a4SDag-Erling Smørgrav switch(pid = vfork()) { 5269b50d902SRodney W. Grimes case -1: 5278ad749a4SDag-Erling Smørgrav err(1, "vfork"); 5289b50d902SRodney W. Grimes case 0: 52998186e89SMaxime Henrion if (oflag) { 53098186e89SMaxime Henrion close(0); 53198186e89SMaxime Henrion if (open("/dev/tty", O_RDONLY) == -1) 53298186e89SMaxime Henrion err(1, "open"); 53398186e89SMaxime Henrion } 5349b50d902SRodney W. Grimes execvp(argv[0], argv); 5358ad749a4SDag-Erling Smørgrav childerr = errno; 5369b50d902SRodney W. Grimes _exit(1); 5379b50d902SRodney W. Grimes } 538330d23f5STim J. Robbins curprocs++; 539330d23f5STim J. Robbins waitchildren(*argv, 0); 540330d23f5STim J. Robbins } 541330d23f5STim J. Robbins 542330d23f5STim J. Robbins static void 543330d23f5STim J. Robbins waitchildren(const char *name, int waitall) 544330d23f5STim J. Robbins { 545330d23f5STim J. Robbins pid_t pid; 546330d23f5STim J. Robbins int status; 547330d23f5STim J. Robbins 548330d23f5STim J. Robbins while ((pid = wait3(&status, !waitall && curprocs < maxprocs ? 549330d23f5STim J. Robbins WNOHANG : 0, NULL)) > 0) { 550330d23f5STim J. Robbins curprocs--; 551fc17b349SJuli Mallett /* If we couldn't invoke the utility, exit. */ 552330d23f5STim J. Robbins if (childerr != 0) { 553330d23f5STim J. Robbins errno = childerr; 554330d23f5STim J. Robbins err(errno == ENOENT ? 127 : 126, "%s", name); 555330d23f5STim J. Robbins } 556330d23f5STim J. Robbins /* 557330d23f5STim J. Robbins * If utility signaled or exited with a value of 255, 558330d23f5STim J. Robbins * exit 1-125. 559330d23f5STim J. Robbins */ 5609b50d902SRodney W. Grimes if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255) 5619b50d902SRodney W. Grimes exit(1); 5629b50d902SRodney W. Grimes if (WEXITSTATUS(status)) 5639b50d902SRodney W. Grimes rval = 1; 5649b50d902SRodney W. Grimes } 565330d23f5STim J. Robbins if (pid == -1 && errno != ECHILD) 566330d23f5STim J. Robbins err(1, "wait3"); 567330d23f5STim J. Robbins } 5689b50d902SRodney W. Grimes 569305e39f4SJuli Mallett /* 570305e39f4SJuli Mallett * Prompt the user about running a command. 571305e39f4SJuli Mallett */ 572305e39f4SJuli Mallett static int 573305e39f4SJuli Mallett prompt(void) 574305e39f4SJuli Mallett { 575305e39f4SJuli Mallett regex_t cre; 576305e39f4SJuli Mallett size_t rsize; 577305e39f4SJuli Mallett int match; 578305e39f4SJuli Mallett char *response; 579305e39f4SJuli Mallett FILE *ttyfp; 580305e39f4SJuli Mallett 581305e39f4SJuli Mallett if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) 582305e39f4SJuli Mallett return (2); /* Indicate that the TTY failed to open. */ 583305e39f4SJuli Mallett (void)fprintf(stderr, "?..."); 584305e39f4SJuli Mallett (void)fflush(stderr); 585305e39f4SJuli Mallett if ((response = fgetln(ttyfp, &rsize)) == NULL || 5868e0a87c1SRuslan Ermilov regcomp(&cre, 58740c6b893SRuslan Ermilov #if (__FreeBSD_version >= 450002 && __FreeBSD_version < 500000) || \ 58840c6b893SRuslan Ermilov __FreeBSD_version >= 500017 5898e0a87c1SRuslan Ermilov nl_langinfo(YESEXPR), 59040c6b893SRuslan Ermilov #else 59140c6b893SRuslan Ermilov "^[yY]", 5928e0a87c1SRuslan Ermilov #endif 5938e0a87c1SRuslan Ermilov REG_BASIC) != 0) { 594305e39f4SJuli Mallett (void)fclose(ttyfp); 595305e39f4SJuli Mallett return (0); 596305e39f4SJuli Mallett } 597305e39f4SJuli Mallett match = regexec(&cre, response, 0, NULL, 0); 598305e39f4SJuli Mallett (void)fclose(ttyfp); 599305e39f4SJuli Mallett regfree(&cre); 600305e39f4SJuli Mallett return (match == 0); 601305e39f4SJuli Mallett } 602305e39f4SJuli Mallett 603a51024e2SPhilippe Charnier static void 60473385ac6SJuli Mallett usage(void) 6059b50d902SRodney W. Grimes { 6068d904f15SDima Dorfman fprintf(stderr, 60798186e89SMaxime Henrion "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n" 60808f16c7aSJuli Mallett " [-L number] [-n number [-x]] [-P maxprocs] [-s size]\n" 609330d23f5STim J. Robbins " [utility [argument ...]]\n"); 6109b50d902SRodney W. Grimes exit(1); 6119b50d902SRodney W. Grimes } 612