19b50d902SRodney W. Grimes /*- 28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 38a16b7a1SPedro F. Giffuni * 49b50d902SRodney W. Grimes * Copyright (c) 1990, 1993 59b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 69b50d902SRodney W. Grimes * 79b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 89b50d902SRodney W. Grimes * John B. Roll Jr. 99b50d902SRodney W. Grimes * 109b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 119b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 129b50d902SRodney W. Grimes * are met: 139b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 149b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 159b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 169b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 179b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 199b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 209b50d902SRodney W. Grimes * without specific prior written permission. 219b50d902SRodney W. Grimes * 229b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 239b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 249b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 259b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 269b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 279b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 289b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 299b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 309b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 319b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 329b50d902SRodney W. Grimes * SUCH DAMAGE. 33fc17b349SJuli Mallett * 34fc17b349SJuli Mallett * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $ 359b50d902SRodney W. Grimes */ 369b50d902SRodney W. Grimes 37e896b4a4SAllan Jude #include <sys/types.h> 389b50d902SRodney W. Grimes #include <sys/wait.h> 39e896b4a4SAllan Jude #include <sys/time.h> 40e896b4a4SAllan Jude #include <sys/limits.h> 41e896b4a4SAllan Jude #include <sys/resource.h> 42a51024e2SPhilippe Charnier #include <err.h> 438ad749a4SDag-Erling Smørgrav #include <errno.h> 4498186e89SMaxime Henrion #include <fcntl.h> 45dc940832SKyle Evans #include <getopt.h> 46305e39f4SJuli Mallett #include <langinfo.h> 47305e39f4SJuli Mallett #include <locale.h> 48305e39f4SJuli Mallett #include <paths.h> 49305e39f4SJuli Mallett #include <regex.h> 50f058359bSTom Jones #include <stdbool.h> 519b50d902SRodney W. Grimes #include <stdio.h> 529b50d902SRodney W. Grimes #include <stdlib.h> 539b50d902SRodney W. Grimes #include <string.h> 549b50d902SRodney W. Grimes #include <unistd.h> 5516b07a33SMark Murray 569b50d902SRodney W. Grimes #include "pathnames.h" 579b50d902SRodney W. Grimes 581926d4aaSJuli Mallett static void parse_input(int, char *[]); 591926d4aaSJuli Mallett static void prerun(int, char *[]); 60305e39f4SJuli Mallett static int prompt(void); 61fc17b349SJuli Mallett static void run(char **); 6273385ac6SJuli Mallett static void usage(void); 63f058359bSTom Jones bool strnsubst(char **, const char *, const char *, size_t); 6492095ab6SStephen McKay static pid_t xwait(int block, int *status); 6570945890SJilles Tjoelker static void xexit(const char *, const int); 66330d23f5STim J. Robbins static void waitchildren(const char *, int); 6792095ab6SStephen McKay static void pids_init(void); 6892095ab6SStephen McKay static int pids_empty(void); 6992095ab6SStephen McKay static int pids_full(void); 7092095ab6SStephen McKay static void pids_add(pid_t pid); 7192095ab6SStephen McKay static int pids_remove(pid_t pid); 7292095ab6SStephen McKay static int findslot(pid_t pid); 7392095ab6SStephen McKay static int findfreeslot(void); 7492095ab6SStephen McKay static void clearslot(int slot); 759b50d902SRodney W. Grimes 7616b07a33SMark Murray static char echo[] = _PATH_ECHO; 778eb2a3deSDavid E. O'Brien static char **av, **bxp, **ep, **endxp, **xp; 7891045075SJuli Mallett static char *argp, *bbp, *ebp, *inpline, *p, *replstr; 7991045075SJuli Mallett static const char *eofstr; 8021ef48afSYuri Pankov static long eoflen; 8198186e89SMaxime Henrion static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag; 82ba084f6aSJuli Mallett static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag; 83330d23f5STim J. Robbins static int curprocs, maxprocs; 8492095ab6SStephen McKay static pid_t *childpids; 85330d23f5STim J. Robbins 86330d23f5STim J. Robbins static volatile int childerr; 8716b07a33SMark Murray 8899a84ce1STim J. Robbins extern char **environ; 8973385ac6SJuli Mallett 90dc940832SKyle Evans static const char *optstr = "+0E:I:J:L:n:oP:pR:S:s:rtx"; 91dc940832SKyle Evans 92dc940832SKyle Evans static const struct option long_options[] = 93dc940832SKyle Evans { 94dc940832SKyle Evans {"exit", no_argument, NULL, 'x'}, 95dc940832SKyle Evans {"interactive", no_argument, NULL, 'p'}, 96dc940832SKyle Evans {"max-args", required_argument, NULL, 'n'}, 97dc940832SKyle Evans {"max-chars", required_argument, NULL, 's'}, 98dc940832SKyle Evans {"max-procs", required_argument, NULL, 'P'}, 99dc940832SKyle Evans {"no-run-if-empty", no_argument, NULL, 'r'}, 100dc940832SKyle Evans {"null", no_argument, NULL, '0'}, 101dc940832SKyle Evans {"verbose", no_argument, NULL, 't'}, 102dc940832SKyle Evans 103dc940832SKyle Evans {NULL, no_argument, NULL, 0}, 104dc940832SKyle Evans }; 105dc940832SKyle Evans 106a51024e2SPhilippe Charnier int 1071926d4aaSJuli Mallett main(int argc, char *argv[]) 1089b50d902SRodney W. Grimes { 109a3e5bc4fSJoseph Koshy long arg_max; 11091045075SJuli Mallett int ch, Jflag, nargs, nflag, nline; 111fc17b349SJuli Mallett size_t linelen; 112e896b4a4SAllan Jude struct rlimit rl; 113cffa7aa6SBaptiste Daroussin const char *errstr; 1149b50d902SRodney W. Grimes 115fc17b349SJuli Mallett inpline = replstr = NULL; 11691045075SJuli Mallett ep = environ; 117fc17b349SJuli Mallett eofstr = ""; 11821ef48afSYuri Pankov eoflen = 0; 11991045075SJuli Mallett Jflag = nflag = 0; 1208d904f15SDima Dorfman 121f58b94cbSTim J. Robbins (void)setlocale(LC_ALL, ""); 122305e39f4SJuli Mallett 1239b50d902SRodney W. Grimes /* 1249b50d902SRodney W. Grimes * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that 1259b50d902SRodney W. Grimes * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given 1269b50d902SRodney W. Grimes * that the smallest argument is 2 bytes in length, this means that 1279b50d902SRodney W. Grimes * the number of arguments is limited to: 1289b50d902SRodney W. Grimes * 1299b50d902SRodney W. Grimes * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2. 1309b50d902SRodney W. Grimes * 1319b50d902SRodney W. Grimes * We arbitrarily limit the number of arguments to 5000. This is 1329b50d902SRodney W. Grimes * allowed by POSIX.2 as long as the resulting minimum exec line is 1339b50d902SRodney W. Grimes * at least LINE_MAX. Realloc'ing as necessary is possible, but 1349b50d902SRodney W. Grimes * probably not worthwhile. 1359b50d902SRodney W. Grimes */ 1369b50d902SRodney W. Grimes nargs = 5000; 137a3e5bc4fSJoseph Koshy if ((arg_max = sysconf(_SC_ARG_MAX)) == -1) 138a3e5bc4fSJoseph Koshy errx(1, "sysconf(_SC_ARG_MAX) failed"); 139a3e5bc4fSJoseph Koshy nline = arg_max - 4 * 1024; 14091045075SJuli Mallett while (*ep != NULL) { 141e5009da0SSatoshi Asami /* 1 byte for each '\0' */ 142e5009da0SSatoshi Asami nline -= strlen(*ep++) + 1 + sizeof(*ep); 143e5009da0SSatoshi Asami } 144330d23f5STim J. Robbins maxprocs = 1; 145dc940832SKyle Evans while ((ch = getopt_long(argc, argv, optstr, long_options, NULL)) != -1) 1469b50d902SRodney W. Grimes switch (ch) { 147fc17b349SJuli Mallett case 'E': 148fc17b349SJuli Mallett eofstr = optarg; 14921ef48afSYuri Pankov eoflen = strlen(eofstr); 150fc17b349SJuli Mallett break; 151fc17b349SJuli Mallett case 'I': 1526ea89183SJuli Mallett Jflag = 0; 153fc17b349SJuli Mallett Iflag = 1; 1544f49da74SJuli Mallett Lflag = 1; 155fc17b349SJuli Mallett replstr = optarg; 156fc17b349SJuli Mallett break; 1578d904f15SDima Dorfman case 'J': 1586ea89183SJuli Mallett Iflag = 0; 159b50a7286SJuli Mallett Jflag = 1; 1608d904f15SDima Dorfman replstr = optarg; 1618d904f15SDima Dorfman break; 162fc17b349SJuli Mallett case 'L': 1631048a870SDaniel Tameling Lflag = (int)strtonum(optarg, 1, INT_MAX, &errstr); 164cffa7aa6SBaptiste Daroussin if (errstr) 165fbc445adSDag-Erling Smørgrav errx(1, "-%c %s: %s", ch, optarg, errstr); 166fc17b349SJuli Mallett break; 1679b50d902SRodney W. Grimes case 'n': 1689b50d902SRodney W. Grimes nflag = 1; 169fbc445adSDag-Erling Smørgrav nargs = (int)strtonum(optarg, 1, INT_MAX, &errstr); 170cffa7aa6SBaptiste Daroussin if (errstr) 171fbc445adSDag-Erling Smørgrav errx(1, "-%c %s: %s", ch, optarg, errstr); 1729b50d902SRodney W. Grimes break; 17398186e89SMaxime Henrion case 'o': 17498186e89SMaxime Henrion oflag = 1; 17598186e89SMaxime Henrion break; 176330d23f5STim J. Robbins case 'P': 177fbc445adSDag-Erling Smørgrav maxprocs = (int)strtonum(optarg, 0, INT_MAX, &errstr); 178cffa7aa6SBaptiste Daroussin if (errstr) 179fbc445adSDag-Erling Smørgrav errx(1, "-%c %s: %s", ch, optarg, errstr); 180e896b4a4SAllan Jude if (getrlimit(RLIMIT_NPROC, &rl) != 0) 181e896b4a4SAllan Jude errx(1, "getrlimit failed"); 182e896b4a4SAllan Jude if (maxprocs == 0 || maxprocs > rl.rlim_cur) 183e896b4a4SAllan Jude maxprocs = rl.rlim_cur; 184330d23f5STim J. Robbins break; 185fc17b349SJuli Mallett case 'p': 186fc17b349SJuli Mallett pflag = 1; 187fc17b349SJuli Mallett break; 188b50a7286SJuli Mallett case 'R': 189202adb22SDaniel Tameling Rflag = (int)strtonum(optarg, INT_MIN, INT_MAX, &errstr); 190fbc445adSDag-Erling Smørgrav if (errstr) 191fbc445adSDag-Erling Smørgrav errx(1, "-%c %s: %s", ch, optarg, errstr); 1921048a870SDaniel Tameling if (!Rflag) 1931048a870SDaniel Tameling errx(1, "-%c %s: %s", ch, optarg, "must be non-zero"); 194b50a7286SJuli Mallett break; 1952d14e0e5SDag-Erling Smørgrav case 'r': 1962d14e0e5SDag-Erling Smørgrav /* GNU compatibility */ 1972d14e0e5SDag-Erling Smørgrav break; 198ba084f6aSJuli Mallett case 'S': 199fbc445adSDag-Erling Smørgrav Sflag = (int)strtonum(optarg, 0, INT_MAX, &errstr); 200fbc445adSDag-Erling Smørgrav if (errstr) 201fbc445adSDag-Erling Smørgrav errx(1, "-%c %s: %s", ch, optarg, errstr); 202ba084f6aSJuli Mallett break; 2039b50d902SRodney W. Grimes case 's': 204fbc445adSDag-Erling Smørgrav nline = (int)strtonum(optarg, 0, INT_MAX, &errstr); 205cffa7aa6SBaptiste Daroussin if (errstr) 206fbc445adSDag-Erling Smørgrav errx(1, "-%c %s: %s", ch, optarg, errstr); 2079b50d902SRodney W. Grimes break; 2089b50d902SRodney W. Grimes case 't': 2099b50d902SRodney W. Grimes tflag = 1; 2109b50d902SRodney W. Grimes break; 2119b50d902SRodney W. Grimes case 'x': 2129b50d902SRodney W. Grimes xflag = 1; 2139b50d902SRodney W. Grimes break; 214d9198881SWarner Losh case '0': 215d9198881SWarner Losh zflag = 1; 216d9198881SWarner Losh break; 2179b50d902SRodney W. Grimes case '?': 2189b50d902SRodney W. Grimes default: 2199b50d902SRodney W. Grimes usage(); 2209b50d902SRodney W. Grimes } 2219b50d902SRodney W. Grimes argc -= optind; 2229b50d902SRodney W. Grimes argv += optind; 2239b50d902SRodney W. Grimes 2246ea89183SJuli Mallett if (!Iflag && Rflag) 2256ea89183SJuli Mallett usage(); 226ba084f6aSJuli Mallett if (!Iflag && Sflag) 227ba084f6aSJuli Mallett usage(); 2286ea89183SJuli Mallett if (Iflag && !Rflag) 2296ea89183SJuli Mallett Rflag = 5; 230ba084f6aSJuli Mallett if (Iflag && !Sflag) 231ba084f6aSJuli Mallett Sflag = 255; 2329b50d902SRodney W. Grimes if (xflag && !nflag) 2339b50d902SRodney W. Grimes usage(); 2344f49da74SJuli Mallett if (Iflag || Lflag) 235fc17b349SJuli Mallett xflag = 1; 236fc17b349SJuli Mallett if (replstr != NULL && *replstr == '\0') 237fc17b349SJuli Mallett errx(1, "replstr may not be empty"); 2389b50d902SRodney W. Grimes 23992095ab6SStephen McKay pids_init(); 24092095ab6SStephen McKay 2419b50d902SRodney W. Grimes /* 2429b50d902SRodney W. Grimes * Allocate pointers for the utility name, the utility arguments, 2439b50d902SRodney W. Grimes * the maximum arguments to be read from stdin and the trailing 2449b50d902SRodney W. Grimes * NULL. 2459b50d902SRodney W. Grimes */ 246eab91d00SDag-Erling Smørgrav linelen = 1 + argc + (size_t)nargs + 1; 247c9e1c304SUlrich Spörlein if ((av = bxp = malloc(linelen * sizeof(char *))) == NULL) 24891045075SJuli Mallett errx(1, "malloc failed"); 2499b50d902SRodney W. Grimes 2509b50d902SRodney W. Grimes /* 2519b50d902SRodney W. Grimes * Use the user's name for the utility as argv[0], just like the 2529b50d902SRodney W. Grimes * shell. Echo is the default. Set up pointers for the user's 2539b50d902SRodney W. Grimes * arguments. 2549b50d902SRodney W. Grimes */ 2553dca1afcSJuli Mallett if (*argv == NULL) 2569bf450b8SJuli Mallett cnt = strlen(*bxp++ = echo); 2579b50d902SRodney W. Grimes else { 2589b50d902SRodney W. Grimes do { 259b50a7286SJuli Mallett if (Jflag && strcmp(*argv, replstr) == 0) { 26091045075SJuli Mallett char **avj; 2618d904f15SDima Dorfman jfound = 1; 2628d904f15SDima Dorfman argv++; 2638d904f15SDima Dorfman for (avj = argv; *avj; avj++) 2648d904f15SDima Dorfman cnt += strlen(*avj) + 1; 2658d904f15SDima Dorfman break; 2668d904f15SDima Dorfman } 2679b50d902SRodney W. Grimes cnt += strlen(*bxp++ = *argv) + 1; 2683dca1afcSJuli Mallett } while (*++argv != NULL); 2699b50d902SRodney W. Grimes } 2709b50d902SRodney W. Grimes 2719b50d902SRodney W. Grimes /* 2729b50d902SRodney W. Grimes * Set up begin/end/traversing pointers into the array. The -n 2739b50d902SRodney W. Grimes * count doesn't include the trailing NULL pointer, so the malloc 2749b50d902SRodney W. Grimes * added in an extra slot. 2759b50d902SRodney W. Grimes */ 2768eb2a3deSDavid E. O'Brien endxp = (xp = bxp) + nargs; 2779b50d902SRodney W. Grimes 2789b50d902SRodney W. Grimes /* 2799b50d902SRodney W. Grimes * Allocate buffer space for the arguments read from stdin and the 2809b50d902SRodney W. Grimes * trailing NULL. Buffer space is defined as the default or specified 2819b50d902SRodney W. Grimes * space, minus the length of the utility name and arguments. Set up 2829b50d902SRodney W. Grimes * begin/end/traversing pointers into the array. The -s count does 2839b50d902SRodney W. Grimes * include the trailing NULL, so the malloc didn't add in an extra 2849b50d902SRodney W. Grimes * slot. 2859b50d902SRodney W. Grimes */ 2869b50d902SRodney W. Grimes nline -= cnt; 2879b50d902SRodney W. Grimes if (nline <= 0) 288a51024e2SPhilippe Charnier errx(1, "insufficient space for command"); 2899b50d902SRodney W. Grimes 2909bf450b8SJuli Mallett if ((bbp = malloc((size_t)(nline + 1))) == NULL) 29191045075SJuli Mallett errx(1, "malloc failed"); 2929b50d902SRodney W. Grimes ebp = (argp = p = bbp) + nline - 1; 29391045075SJuli Mallett for (;;) 29491045075SJuli Mallett parse_input(argc, argv); 29591045075SJuli Mallett } 2969b50d902SRodney W. Grimes 29791045075SJuli Mallett static void 2981926d4aaSJuli Mallett parse_input(int argc, char *argv[]) 29991045075SJuli Mallett { 30091045075SJuli Mallett int ch, foundeof; 30191045075SJuli Mallett char **avj; 30291045075SJuli Mallett 30391045075SJuli Mallett foundeof = 0; 30491045075SJuli Mallett 3059b50d902SRodney W. Grimes switch (ch = getchar()) { 3069b50d902SRodney W. Grimes case EOF: 3079b50d902SRodney W. Grimes /* No arguments since last exec. */ 3080ca740d9Sliu-du if (p == bbp) { 3090ca740d9Sliu-du waitchildren(*av, 1); 3100ca740d9Sliu-du exit(rval); 3110ca740d9Sliu-du } 3129b50d902SRodney W. Grimes goto arg1; 3139b50d902SRodney W. Grimes case ' ': 3149b50d902SRodney W. Grimes case '\t': 3159b50d902SRodney W. Grimes /* Quotes escape tabs and spaces. */ 316d9198881SWarner Losh if (insingle || indouble || zflag) 3179b50d902SRodney W. Grimes goto addch; 3189b50d902SRodney W. Grimes goto arg2; 319d9198881SWarner Losh case '\0': 32046793db9SGarance A Drosehn if (zflag) { 32146793db9SGarance A Drosehn /* 32246793db9SGarance A Drosehn * Increment 'count', so that nulls will be treated 32346793db9SGarance A Drosehn * as end-of-line, as well as end-of-argument. This 32446793db9SGarance A Drosehn * is needed so -0 works properly with -I and -L. 32546793db9SGarance A Drosehn */ 32646793db9SGarance A Drosehn count++; 327d9198881SWarner Losh goto arg2; 32846793db9SGarance A Drosehn } 329d9198881SWarner Losh goto addch; 3309b50d902SRodney W. Grimes case '\n': 331d9198881SWarner Losh if (zflag) 332d9198881SWarner Losh goto addch; 33346793db9SGarance A Drosehn count++; /* Indicate end-of-line (used by -L) */ 334d9198881SWarner Losh 3359b50d902SRodney W. Grimes /* Quotes do not escape newlines. */ 33670945890SJilles Tjoelker arg1: if (insingle || indouble) { 33770945890SJilles Tjoelker warnx("unterminated quote"); 33870945890SJilles Tjoelker xexit(*av, 1); 33970945890SJilles Tjoelker } 340ef9866beSJean-Marc Zucconi arg2: 34121ef48afSYuri Pankov foundeof = eoflen != 0 && p - argp == eoflen && 34221ef48afSYuri Pankov strncmp(argp, eofstr, eoflen) == 0; 343fc17b349SJuli Mallett 344ef9866beSJean-Marc Zucconi /* Do not make empty args unless they are quoted */ 345fc17b349SJuli Mallett if ((argp != p || wasquoted) && !foundeof) { 346ef9866beSJean-Marc Zucconi *p++ = '\0'; 3479b50d902SRodney W. Grimes *xp++ = argp; 348fc17b349SJuli Mallett if (Iflag) { 349fc17b349SJuli Mallett size_t curlen; 350b9b03ba0SJuli Mallett 351b9b03ba0SJuli Mallett if (inpline == NULL) 352fc17b349SJuli Mallett curlen = 0; 353fc17b349SJuli Mallett else { 3541925cb24SJuli Mallett /* 3551925cb24SJuli Mallett * If this string is not zero 3561925cb24SJuli Mallett * length, append a space for 3572b239dd1SJens Schweikhardt * separation before the next 3581925cb24SJuli Mallett * argument. 3591925cb24SJuli Mallett */ 360b9b03ba0SJuli Mallett if ((curlen = strlen(inpline))) 361fc17b349SJuli Mallett strcat(inpline, " "); 362fc17b349SJuli Mallett } 363fc17b349SJuli Mallett curlen++; 3641925cb24SJuli Mallett /* 3651925cb24SJuli Mallett * Allocate enough to hold what we will 3665eb40323SJuli Mallett * be holding in a second, and to append 3671925cb24SJuli Mallett * a space next time through, if we have 3681925cb24SJuli Mallett * to. 3691925cb24SJuli Mallett */ 370b9b03ba0SJuli Mallett inpline = realloc(inpline, curlen + 2 + 371b9b03ba0SJuli Mallett strlen(argp)); 37270945890SJilles Tjoelker if (inpline == NULL) { 37370945890SJilles Tjoelker warnx("realloc failed"); 37470945890SJilles Tjoelker xexit(*av, 1); 37570945890SJilles Tjoelker } 376fc17b349SJuli Mallett if (curlen == 1) 377fc17b349SJuli Mallett strcpy(inpline, argp); 378fc17b349SJuli Mallett else 379fc17b349SJuli Mallett strcat(inpline, argp); 380fc17b349SJuli Mallett } 381ef9866beSJean-Marc Zucconi } 3829b50d902SRodney W. Grimes 3839b50d902SRodney W. Grimes /* 3849b50d902SRodney W. Grimes * If max'd out on args or buffer, or reached EOF, 3859b50d902SRodney W. Grimes * run the command. If xflag and max'd out on buffer 3861925cb24SJuli Mallett * but not on args, object. Having reached the limit 3871925cb24SJuli Mallett * of input lines, as specified by -L is the same as 3881925cb24SJuli Mallett * maxing out on arguments. 3899b50d902SRodney W. Grimes */ 3908eb2a3deSDavid E. O'Brien if (xp == endxp || p > ebp || ch == EOF || 3915eb40323SJuli Mallett (Lflag <= count && xflag) || foundeof) { 39270945890SJilles Tjoelker if (xflag && xp != endxp && p > ebp) { 39370945890SJilles Tjoelker warnx("insufficient space for arguments"); 39470945890SJilles Tjoelker xexit(*av, 1); 39570945890SJilles Tjoelker } 3968d904f15SDima Dorfman if (jfound) { 3978d904f15SDima Dorfman for (avj = argv; *avj; avj++) 3988d904f15SDima Dorfman *xp++ = *avj; 3998d904f15SDima Dorfman } 40091045075SJuli Mallett prerun(argc, av); 4010ca740d9Sliu-du if (ch == EOF || foundeof) { 4020ca740d9Sliu-du waitchildren(*av, 1); 4030ca740d9Sliu-du exit(rval); 4040ca740d9Sliu-du } 4059b50d902SRodney W. Grimes p = bbp; 4069b50d902SRodney W. Grimes xp = bxp; 407fc17b349SJuli Mallett count = 0; 408ef9866beSJean-Marc Zucconi } 4099b50d902SRodney W. Grimes argp = p; 410ef9866beSJean-Marc Zucconi wasquoted = 0; 4119b50d902SRodney W. Grimes break; 4129b50d902SRodney W. Grimes case '\'': 413d9198881SWarner Losh if (indouble || zflag) 4149b50d902SRodney W. Grimes goto addch; 4159b50d902SRodney W. Grimes insingle = !insingle; 416ef9866beSJean-Marc Zucconi wasquoted = 1; 4179b50d902SRodney W. Grimes break; 4189b50d902SRodney W. Grimes case '"': 419d9198881SWarner Losh if (insingle || zflag) 4209b50d902SRodney W. Grimes goto addch; 4219b50d902SRodney W. Grimes indouble = !indouble; 422ef9866beSJean-Marc Zucconi wasquoted = 1; 4239b50d902SRodney W. Grimes break; 4249b50d902SRodney W. Grimes case '\\': 425d9198881SWarner Losh if (zflag) 426d9198881SWarner Losh goto addch; 4279b50d902SRodney W. Grimes /* Backslash escapes anything, is escaped by quotes. */ 42870945890SJilles Tjoelker if (!insingle && !indouble && (ch = getchar()) == EOF) { 42970945890SJilles Tjoelker warnx("backslash at EOF"); 43070945890SJilles Tjoelker xexit(*av, 1); 43170945890SJilles Tjoelker } 4329b50d902SRodney W. Grimes /* FALLTHROUGH */ 4339b50d902SRodney W. Grimes default: 4349b50d902SRodney W. Grimes addch: if (p < ebp) { 4359b50d902SRodney W. Grimes *p++ = ch; 4369b50d902SRodney W. Grimes break; 4379b50d902SRodney W. Grimes } 4389b50d902SRodney W. Grimes 4399b50d902SRodney W. Grimes /* If only one argument, not enough buffer space. */ 44070945890SJilles Tjoelker if (bxp == xp) { 44170945890SJilles Tjoelker warnx("insufficient space for argument"); 44270945890SJilles Tjoelker xexit(*av, 1); 44370945890SJilles Tjoelker } 4449b50d902SRodney W. Grimes /* Didn't hit argument limit, so if xflag object. */ 44570945890SJilles Tjoelker if (xflag) { 44670945890SJilles Tjoelker warnx("insufficient space for arguments"); 44770945890SJilles Tjoelker xexit(*av, 1); 44870945890SJilles Tjoelker } 4499b50d902SRodney W. Grimes 4508d904f15SDima Dorfman if (jfound) { 4518d904f15SDima Dorfman for (avj = argv; *avj; avj++) 4528d904f15SDima Dorfman *xp++ = *avj; 4538d904f15SDima Dorfman } 45491045075SJuli Mallett prerun(argc, av); 4559b50d902SRodney W. Grimes xp = bxp; 4569b50d902SRodney W. Grimes cnt = ebp - argp; 457fc17b349SJuli Mallett memcpy(bbp, argp, (size_t)cnt); 4589b50d902SRodney W. Grimes p = (argp = bbp) + cnt; 4599b50d902SRodney W. Grimes *p++ = ch; 4609b50d902SRodney W. Grimes break; 4619b50d902SRodney W. Grimes } 4629b50d902SRodney W. Grimes } 4639b50d902SRodney W. Grimes 464263dc775SJuli Mallett /* 465263dc775SJuli Mallett * Do things necessary before run()'ing, such as -I substitution, 466263dc775SJuli Mallett * and then call run(). 467263dc775SJuli Mallett */ 468263dc775SJuli Mallett static void 4691926d4aaSJuli Mallett prerun(int argc, char *argv[]) 470263dc775SJuli Mallett { 471263dc775SJuli Mallett char **tmp, **tmp2, **avj; 47291045075SJuli Mallett int repls; 47391045075SJuli Mallett 47491045075SJuli Mallett repls = Rflag; 475263dc775SJuli Mallett 476be70f7d4SJuli Mallett if (argc == 0 || repls == 0) { 477263dc775SJuli Mallett *xp = NULL; 478263dc775SJuli Mallett run(argv); 479263dc775SJuli Mallett return; 480263dc775SJuli Mallett } 481263dc775SJuli Mallett 482263dc775SJuli Mallett avj = argv; 483263dc775SJuli Mallett 484263dc775SJuli Mallett /* 485263dc775SJuli Mallett * Allocate memory to hold the argument list, and 486263dc775SJuli Mallett * a NULL at the tail. 487263dc775SJuli Mallett */ 488c9e1c304SUlrich Spörlein tmp = malloc((argc + 1) * sizeof(char *)); 48970945890SJilles Tjoelker if (tmp == NULL) { 49070945890SJilles Tjoelker warnx("malloc failed"); 49170945890SJilles Tjoelker xexit(*argv, 1); 49270945890SJilles Tjoelker } 493263dc775SJuli Mallett tmp2 = tmp; 494263dc775SJuli Mallett 495263dc775SJuli Mallett /* 496263dc775SJuli Mallett * Save the first argument and iterate over it, we 497263dc775SJuli Mallett * cannot do strnsubst() to it. 498263dc775SJuli Mallett */ 49970945890SJilles Tjoelker if ((*tmp++ = strdup(*avj++)) == NULL) { 50070945890SJilles Tjoelker warnx("strdup failed"); 50170945890SJilles Tjoelker xexit(*argv, 1); 50270945890SJilles Tjoelker } 503263dc775SJuli Mallett 504263dc775SJuli Mallett /* 505263dc775SJuli Mallett * For each argument to utility, if we have not used up 506263dc775SJuli Mallett * the number of replacements we are allowed to do, and 5072b239dd1SJens Schweikhardt * if the argument contains at least one occurrence of 508263dc775SJuli Mallett * replstr, call strnsubst(), else just save the string. 509263dc775SJuli Mallett * Iterations over elements of avj and tmp are done 510263dc775SJuli Mallett * where appropriate. 511263dc775SJuli Mallett */ 512263dc775SJuli Mallett while (--argc) { 513263dc775SJuli Mallett *tmp = *avj++; 514263dc775SJuli Mallett if (repls && strstr(*tmp, replstr) != NULL) { 515f058359bSTom Jones if (strnsubst(tmp++, replstr, inpline, (size_t)Sflag)) { 5166d777389SDag-Erling Smørgrav warnx("command line cannot be assembled, too long"); 517f058359bSTom Jones xexit(*argv, 1); 518f058359bSTom Jones } 5193c675167SJuli Mallett if (repls > 0) 520263dc775SJuli Mallett repls--; 521263dc775SJuli Mallett } else { 52270945890SJilles Tjoelker if ((*tmp = strdup(*tmp)) == NULL) { 52370945890SJilles Tjoelker warnx("strdup failed"); 52470945890SJilles Tjoelker xexit(*argv, 1); 52570945890SJilles Tjoelker } 526263dc775SJuli Mallett tmp++; 527263dc775SJuli Mallett } 528263dc775SJuli Mallett } 529263dc775SJuli Mallett 530263dc775SJuli Mallett /* 531263dc775SJuli Mallett * Run it. 532263dc775SJuli Mallett */ 533b6594dbaSJuli Mallett *tmp = NULL; 534263dc775SJuli Mallett run(tmp2); 535263dc775SJuli Mallett 536263dc775SJuli Mallett /* 537263dc775SJuli Mallett * Walk from the tail to the head, free along the way. 538263dc775SJuli Mallett */ 539263dc775SJuli Mallett for (; tmp2 != tmp; tmp--) 540263dc775SJuli Mallett free(*tmp); 541263dc775SJuli Mallett /* 542263dc775SJuli Mallett * Now free the list itself. 543263dc775SJuli Mallett */ 544263dc775SJuli Mallett free(tmp2); 545263dc775SJuli Mallett 546263dc775SJuli Mallett /* 547986d829bSJuli Mallett * Free the input line buffer, if we have one. 548263dc775SJuli Mallett */ 54951f7a48bSJuli Mallett if (inpline != NULL) { 55091045075SJuli Mallett free(inpline); 55151f7a48bSJuli Mallett inpline = NULL; 55251f7a48bSJuli Mallett } 553263dc775SJuli Mallett } 554263dc775SJuli Mallett 555fc17b349SJuli Mallett static void 55673385ac6SJuli Mallett run(char **argv) 5579b50d902SRodney W. Grimes { 5589b50d902SRodney W. Grimes pid_t pid; 5590792992cSMaxime Henrion int fd; 560330d23f5STim J. Robbins char **avec; 5619b50d902SRodney W. Grimes 562305e39f4SJuli Mallett /* 563305e39f4SJuli Mallett * If the user wants to be notified of each command before it is 564305e39f4SJuli Mallett * executed, notify them. If they want the notification to be 565305e39f4SJuli Mallett * followed by a prompt, then prompt them. 566305e39f4SJuli Mallett */ 567fc17b349SJuli Mallett if (tflag || pflag) { 5689b50d902SRodney W. Grimes (void)fprintf(stderr, "%s", *argv); 56991045075SJuli Mallett for (avec = argv + 1; *avec != NULL; ++avec) 57091045075SJuli Mallett (void)fprintf(stderr, " %s", *avec); 571305e39f4SJuli Mallett /* 572305e39f4SJuli Mallett * If the user has asked to be prompted, do so. 573305e39f4SJuli Mallett */ 574305e39f4SJuli Mallett if (pflag) 575305e39f4SJuli Mallett /* 576305e39f4SJuli Mallett * If they asked not to exec, return without execution 577305e39f4SJuli Mallett * but if they asked to, go to the execution. If we 578305e39f4SJuli Mallett * could not open their tty, break the switch and drop 579305e39f4SJuli Mallett * back to -t behaviour. 580305e39f4SJuli Mallett */ 581305e39f4SJuli Mallett switch (prompt()) { 582305e39f4SJuli Mallett case 0: 583fc17b349SJuli Mallett return; 584305e39f4SJuli Mallett case 1: 585305e39f4SJuli Mallett goto exec; 586305e39f4SJuli Mallett case 2: 587305e39f4SJuli Mallett break; 588305e39f4SJuli Mallett } 5899b50d902SRodney W. Grimes (void)fprintf(stderr, "\n"); 5909b50d902SRodney W. Grimes (void)fflush(stderr); 5919b50d902SRodney W. Grimes } 592305e39f4SJuli Mallett exec: 5938ad749a4SDag-Erling Smørgrav childerr = 0; 5948ad749a4SDag-Erling Smørgrav switch (pid = vfork()) { 5959b50d902SRodney W. Grimes case -1: 59670945890SJilles Tjoelker warn("vfork"); 59770945890SJilles Tjoelker xexit(*argv, 1); 5989b50d902SRodney W. Grimes case 0: 599cec1ba8cSMaxime Henrion if (oflag) { 6000792992cSMaxime Henrion if ((fd = open(_PATH_TTY, O_RDONLY)) == -1) 6010792992cSMaxime Henrion err(1, "can't open /dev/tty"); 602cec1ba8cSMaxime Henrion } else { 6030792992cSMaxime Henrion fd = open(_PATH_DEVNULL, O_RDONLY); 6040792992cSMaxime Henrion } 6050792992cSMaxime Henrion if (fd > STDIN_FILENO) { 6060792992cSMaxime Henrion if (dup2(fd, STDIN_FILENO) != 0) 6070792992cSMaxime Henrion err(1, "can't dup2 to stdin"); 6080792992cSMaxime Henrion close(fd); 60998186e89SMaxime Henrion } 6109b50d902SRodney W. Grimes execvp(argv[0], argv); 6118ad749a4SDag-Erling Smørgrav childerr = errno; 6129b50d902SRodney W. Grimes _exit(1); 6139b50d902SRodney W. Grimes } 61492095ab6SStephen McKay pids_add(pid); 615330d23f5STim J. Robbins waitchildren(*argv, 0); 616330d23f5STim J. Robbins } 617330d23f5STim J. Robbins 61892095ab6SStephen McKay /* 61992095ab6SStephen McKay * Wait for a tracked child to exit and return its pid and exit status. 62092095ab6SStephen McKay * 62192095ab6SStephen McKay * Ignores (discards) all untracked child processes. 62292095ab6SStephen McKay * Returns -1 and sets errno to ECHILD if no tracked children exist. 62392095ab6SStephen McKay * If block is set, waits indefinitely for a child process to exit. 62492095ab6SStephen McKay * If block is not set and no children have exited, returns 0 immediately. 62592095ab6SStephen McKay */ 62692095ab6SStephen McKay static pid_t 62792095ab6SStephen McKay xwait(int block, int *status) { 62892095ab6SStephen McKay pid_t pid; 62992095ab6SStephen McKay 63092095ab6SStephen McKay if (pids_empty()) { 63192095ab6SStephen McKay errno = ECHILD; 63229afe03fSStephen McKay return (-1); 63392095ab6SStephen McKay } 63492095ab6SStephen McKay 63592095ab6SStephen McKay while ((pid = waitpid(-1, status, block ? 0 : WNOHANG)) > 0) 63692095ab6SStephen McKay if (pids_remove(pid)) 63792095ab6SStephen McKay break; 63892095ab6SStephen McKay 63929afe03fSStephen McKay return (pid); 64092095ab6SStephen McKay } 64192095ab6SStephen McKay 642330d23f5STim J. Robbins static void 64370945890SJilles Tjoelker xexit(const char *name, const int exit_code) { 64470945890SJilles Tjoelker waitchildren(name, 1); 64570945890SJilles Tjoelker exit(exit_code); 64670945890SJilles Tjoelker } 64770945890SJilles Tjoelker 64870945890SJilles Tjoelker static void 649330d23f5STim J. Robbins waitchildren(const char *name, int waitall) 650330d23f5STim J. Robbins { 651330d23f5STim J. Robbins pid_t pid; 652330d23f5STim J. Robbins int status; 65370945890SJilles Tjoelker int cause_exit = 0; 654330d23f5STim J. Robbins 65592095ab6SStephen McKay while ((pid = xwait(waitall || pids_full(), &status)) > 0) { 65670945890SJilles Tjoelker /* 65770945890SJilles Tjoelker * If we couldn't invoke the utility or if utility exited 65870945890SJilles Tjoelker * because of a signal or with a value of 255, warn (per 65970945890SJilles Tjoelker * POSIX), and then wait until all other children have 66070945890SJilles Tjoelker * exited before exiting 1-125. POSIX requires us to stop 66170945890SJilles Tjoelker * reading if child exits because of a signal or with 255, 66270945890SJilles Tjoelker * but it does not require us to exit immediately; waiting 66370945890SJilles Tjoelker * is preferable to orphaning. 66470945890SJilles Tjoelker */ 66570945890SJilles Tjoelker if (childerr != 0 && cause_exit == 0) { 666330d23f5STim J. Robbins errno = childerr; 66770945890SJilles Tjoelker waitall = 1; 668ab825606SMark Johnston cause_exit = errno == ENOENT ? 127 : 126; 66970945890SJilles Tjoelker warn("%s", name); 67070945890SJilles Tjoelker } else if (WIFSIGNALED(status)) { 67170945890SJilles Tjoelker waitall = cause_exit = 1; 67270945890SJilles Tjoelker warnx("%s: terminated with signal %d; aborting", 67323583c4fSJilles Tjoelker name, WTERMSIG(status)); 67470945890SJilles Tjoelker } else if (WEXITSTATUS(status) == 255) { 67570945890SJilles Tjoelker waitall = cause_exit = 1; 67670945890SJilles Tjoelker warnx("%s: exited with status 255; aborting", name); 67770945890SJilles Tjoelker } else if (WEXITSTATUS(status)) 6789b50d902SRodney W. Grimes rval = 1; 6799b50d902SRodney W. Grimes } 68092095ab6SStephen McKay 68170945890SJilles Tjoelker if (cause_exit) 68270945890SJilles Tjoelker exit(cause_exit); 683330d23f5STim J. Robbins if (pid == -1 && errno != ECHILD) 68492095ab6SStephen McKay err(1, "waitpid"); 68592095ab6SStephen McKay } 68692095ab6SStephen McKay 68792095ab6SStephen McKay #define NOPID (0) 68892095ab6SStephen McKay 68992095ab6SStephen McKay static void 69029afe03fSStephen McKay pids_init(void) 69192095ab6SStephen McKay { 69292095ab6SStephen McKay int i; 69392095ab6SStephen McKay 69492095ab6SStephen McKay if ((childpids = malloc(maxprocs * sizeof(*childpids))) == NULL) 69592095ab6SStephen McKay errx(1, "malloc failed"); 69692095ab6SStephen McKay 69792095ab6SStephen McKay for (i = 0; i < maxprocs; i++) 69892095ab6SStephen McKay clearslot(i); 69992095ab6SStephen McKay } 70092095ab6SStephen McKay 70192095ab6SStephen McKay static int 70229afe03fSStephen McKay pids_empty(void) 70392095ab6SStephen McKay { 704a274be77SEitan Adler 70529afe03fSStephen McKay return (curprocs == 0); 70692095ab6SStephen McKay } 70792095ab6SStephen McKay 70892095ab6SStephen McKay static int 70929afe03fSStephen McKay pids_full(void) 71092095ab6SStephen McKay { 711a274be77SEitan Adler 71229afe03fSStephen McKay return (curprocs >= maxprocs); 71392095ab6SStephen McKay } 71492095ab6SStephen McKay 71592095ab6SStephen McKay static void 71692095ab6SStephen McKay pids_add(pid_t pid) 71792095ab6SStephen McKay { 71892095ab6SStephen McKay int slot; 71992095ab6SStephen McKay 72092095ab6SStephen McKay slot = findfreeslot(); 72192095ab6SStephen McKay childpids[slot] = pid; 72292095ab6SStephen McKay curprocs++; 72392095ab6SStephen McKay } 72492095ab6SStephen McKay 72592095ab6SStephen McKay static int 72692095ab6SStephen McKay pids_remove(pid_t pid) 72792095ab6SStephen McKay { 72892095ab6SStephen McKay int slot; 72992095ab6SStephen McKay 73092095ab6SStephen McKay if ((slot = findslot(pid)) < 0) 73129afe03fSStephen McKay return (0); 73292095ab6SStephen McKay 73392095ab6SStephen McKay clearslot(slot); 73492095ab6SStephen McKay curprocs--; 73529afe03fSStephen McKay return (1); 73692095ab6SStephen McKay } 73792095ab6SStephen McKay 73892095ab6SStephen McKay static int 73929afe03fSStephen McKay findfreeslot(void) 74092095ab6SStephen McKay { 74192095ab6SStephen McKay int slot; 74292095ab6SStephen McKay 74392095ab6SStephen McKay if ((slot = findslot(NOPID)) < 0) 74492095ab6SStephen McKay errx(1, "internal error: no free pid slot"); 74529afe03fSStephen McKay return (slot); 74692095ab6SStephen McKay } 74792095ab6SStephen McKay 74892095ab6SStephen McKay static int 74992095ab6SStephen McKay findslot(pid_t pid) 75092095ab6SStephen McKay { 75192095ab6SStephen McKay int slot; 75292095ab6SStephen McKay 75392095ab6SStephen McKay for (slot = 0; slot < maxprocs; slot++) 75492095ab6SStephen McKay if (childpids[slot] == pid) 75529afe03fSStephen McKay return (slot); 75629afe03fSStephen McKay return (-1); 75792095ab6SStephen McKay } 75892095ab6SStephen McKay 75992095ab6SStephen McKay static void 76092095ab6SStephen McKay clearslot(int slot) 76192095ab6SStephen McKay { 762a274be77SEitan Adler 76392095ab6SStephen McKay childpids[slot] = NOPID; 764330d23f5STim J. Robbins } 7659b50d902SRodney W. Grimes 766305e39f4SJuli Mallett /* 767305e39f4SJuli Mallett * Prompt the user about running a command. 768305e39f4SJuli Mallett */ 769305e39f4SJuli Mallett static int 770305e39f4SJuli Mallett prompt(void) 771305e39f4SJuli Mallett { 772305e39f4SJuli Mallett regex_t cre; 773*5fbdcd65SMartin Tournoij size_t rsize = 0; 774305e39f4SJuli Mallett int match; 775*5fbdcd65SMartin Tournoij char *response = NULL; 776305e39f4SJuli Mallett FILE *ttyfp; 777305e39f4SJuli Mallett 778305e39f4SJuli Mallett if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL) 779305e39f4SJuli Mallett return (2); /* Indicate that the TTY failed to open. */ 780305e39f4SJuli Mallett (void)fprintf(stderr, "?..."); 781305e39f4SJuli Mallett (void)fflush(stderr); 782*5fbdcd65SMartin Tournoij if (getline(&response, &rsize, ttyfp) < 0 || 7830d2dcf21SYuri Pankov regcomp(&cre, nl_langinfo(YESEXPR), REG_EXTENDED) != 0) { 784305e39f4SJuli Mallett (void)fclose(ttyfp); 785305e39f4SJuli Mallett return (0); 786305e39f4SJuli Mallett } 787305e39f4SJuli Mallett match = regexec(&cre, response, 0, NULL, 0); 788*5fbdcd65SMartin Tournoij free(response); 789305e39f4SJuli Mallett (void)fclose(ttyfp); 790305e39f4SJuli Mallett regfree(&cre); 791305e39f4SJuli Mallett return (match == 0); 792305e39f4SJuli Mallett } 793305e39f4SJuli Mallett 794a51024e2SPhilippe Charnier static void 79573385ac6SJuli Mallett usage(void) 7969b50d902SRodney W. Grimes { 797a274be77SEitan Adler 7988d904f15SDima Dorfman fprintf(stderr, 799ba084f6aSJuli Mallett "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]\n" 800ba084f6aSJuli Mallett " [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]\n" 801ba084f6aSJuli Mallett " [-s size] [utility [argument ...]]\n"); 8029b50d902SRodney W. Grimes exit(1); 8039b50d902SRodney W. Grimes } 804