1*00785f56SLionel Sambuc /* $NetBSD: xargs.c,v 1.20 2010/12/17 11:32:57 plunky Exp $ */
2*00785f56SLionel Sambuc
3*00785f56SLionel Sambuc /*-
4*00785f56SLionel Sambuc * Copyright (c) 1990, 1993
5*00785f56SLionel Sambuc * The Regents of the University of California. All rights reserved.
6*00785f56SLionel Sambuc *
7*00785f56SLionel Sambuc * This code is derived from software contributed to Berkeley by
8*00785f56SLionel Sambuc * John B. Roll Jr.
9*00785f56SLionel Sambuc *
10*00785f56SLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*00785f56SLionel Sambuc * modification, are permitted provided that the following conditions
12*00785f56SLionel Sambuc * are met:
13*00785f56SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*00785f56SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*00785f56SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*00785f56SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*00785f56SLionel Sambuc * documentation and/or other materials provided with the distribution.
18*00785f56SLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19*00785f56SLionel Sambuc * may be used to endorse or promote products derived from this software
20*00785f56SLionel Sambuc * without specific prior written permission.
21*00785f56SLionel Sambuc *
22*00785f56SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*00785f56SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*00785f56SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*00785f56SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*00785f56SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*00785f56SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*00785f56SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*00785f56SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*00785f56SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*00785f56SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*00785f56SLionel Sambuc * SUCH DAMAGE.
33*00785f56SLionel Sambuc *
34*00785f56SLionel Sambuc * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $
35*00785f56SLionel Sambuc */
36*00785f56SLionel Sambuc
37*00785f56SLionel Sambuc #include <sys/cdefs.h>
38*00785f56SLionel Sambuc #ifndef lint
39*00785f56SLionel Sambuc __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
40*00785f56SLionel Sambuc The Regents of the University of California. All rights reserved.");
41*00785f56SLionel Sambuc #if 0
42*00785f56SLionel Sambuc static char sccsid[] = "@(#)xargs.c 8.1 (Berkeley) 6/6/93";
43*00785f56SLionel Sambuc __FBSDID("$FreeBSD: src/usr.bin/xargs/xargs.c,v 1.62 2006/01/01 22:59:54 jmallett Exp $");
44*00785f56SLionel Sambuc #endif
45*00785f56SLionel Sambuc __RCSID("$NetBSD: xargs.c,v 1.20 2010/12/17 11:32:57 plunky Exp $");
46*00785f56SLionel Sambuc #endif /* not lint */
47*00785f56SLionel Sambuc
48*00785f56SLionel Sambuc #include <sys/param.h>
49*00785f56SLionel Sambuc #include <sys/wait.h>
50*00785f56SLionel Sambuc
51*00785f56SLionel Sambuc #include <err.h>
52*00785f56SLionel Sambuc #include <errno.h>
53*00785f56SLionel Sambuc #include <fcntl.h>
54*00785f56SLionel Sambuc #include <langinfo.h>
55*00785f56SLionel Sambuc #include <locale.h>
56*00785f56SLionel Sambuc #include <paths.h>
57*00785f56SLionel Sambuc #include <regex.h>
58*00785f56SLionel Sambuc #include <stdio.h>
59*00785f56SLionel Sambuc #include <stdlib.h>
60*00785f56SLionel Sambuc #include <string.h>
61*00785f56SLionel Sambuc #include <signal.h>
62*00785f56SLionel Sambuc #include <unistd.h>
63*00785f56SLionel Sambuc
64*00785f56SLionel Sambuc #include "pathnames.h"
65*00785f56SLionel Sambuc
66*00785f56SLionel Sambuc static void parse_input(int, char *[]);
67*00785f56SLionel Sambuc static void prerun(int, char *[]);
68*00785f56SLionel Sambuc static int prompt(void);
69*00785f56SLionel Sambuc static void run(char **);
70*00785f56SLionel Sambuc static void usage(void) __dead;
71*00785f56SLionel Sambuc void strnsubst(char **, const char *, const char *, size_t);
72*00785f56SLionel Sambuc static void waitchildren(const char *, int);
73*00785f56SLionel Sambuc
74*00785f56SLionel Sambuc static char echo[] = _PATH_ECHO;
75*00785f56SLionel Sambuc static char **av, **bxp, **ep, **endxp, **xp;
76*00785f56SLionel Sambuc static char *argp, *bbp, *ebp, *inpline, *p, *replstr;
77*00785f56SLionel Sambuc static const char *eofstr;
78*00785f56SLionel Sambuc static int count, insingle, indouble, oflag, pflag, tflag, Rflag, rval, zflag;
79*00785f56SLionel Sambuc static int cnt, Iflag, jfound, Lflag, Sflag, wasquoted, xflag;
80*00785f56SLionel Sambuc static int curprocs, maxprocs;
81*00785f56SLionel Sambuc
82*00785f56SLionel Sambuc static volatile int childerr;
83*00785f56SLionel Sambuc
84*00785f56SLionel Sambuc extern char **environ;
85*00785f56SLionel Sambuc
86*00785f56SLionel Sambuc int
main(int argc,char * argv[])87*00785f56SLionel Sambuc main(int argc, char *argv[])
88*00785f56SLionel Sambuc {
89*00785f56SLionel Sambuc long arg_max;
90*00785f56SLionel Sambuc int ch, Jflag, nargs, nflag, nline;
91*00785f56SLionel Sambuc size_t linelen;
92*00785f56SLionel Sambuc char *endptr;
93*00785f56SLionel Sambuc
94*00785f56SLionel Sambuc setprogname(argv[0]);
95*00785f56SLionel Sambuc
96*00785f56SLionel Sambuc inpline = replstr = NULL;
97*00785f56SLionel Sambuc ep = environ;
98*00785f56SLionel Sambuc eofstr = "";
99*00785f56SLionel Sambuc Jflag = nflag = 0;
100*00785f56SLionel Sambuc
101*00785f56SLionel Sambuc (void)setlocale(LC_ALL, "");
102*00785f56SLionel Sambuc
103*00785f56SLionel Sambuc /*
104*00785f56SLionel Sambuc * SUSv3 says of the exec family of functions:
105*00785f56SLionel Sambuc * The number of bytes available for the new process'
106*00785f56SLionel Sambuc * combined argument and environment lists is {ARG_MAX}. It
107*00785f56SLionel Sambuc * is implementation-defined whether null terminators,
108*00785f56SLionel Sambuc * pointers, and/or any alignment bytes are included in this
109*00785f56SLionel Sambuc * total.
110*00785f56SLionel Sambuc *
111*00785f56SLionel Sambuc * SUSv3 says of xargs:
112*00785f56SLionel Sambuc * ... the combined argument and environment lists ...
113*00785f56SLionel Sambuc * shall not exceed {ARG_MAX}-2048.
114*00785f56SLionel Sambuc *
115*00785f56SLionel Sambuc * To be conservative, we use ARG_MAX - 4K, and we do include
116*00785f56SLionel Sambuc * nul terminators and pointers in the calculation.
117*00785f56SLionel Sambuc *
118*00785f56SLionel Sambuc * Given that the smallest argument is 2 bytes in length, this
119*00785f56SLionel Sambuc * means that the number of arguments is limited to:
120*00785f56SLionel Sambuc *
121*00785f56SLionel Sambuc * (ARG_MAX - 4K - LENGTH(env + utility + arguments)) / 2.
122*00785f56SLionel Sambuc *
123*00785f56SLionel Sambuc * We arbitrarily limit the number of arguments to 5000. This is
124*00785f56SLionel Sambuc * allowed by POSIX.2 as long as the resulting minimum exec line is
125*00785f56SLionel Sambuc * at least LINE_MAX. Realloc'ing as necessary is possible, but
126*00785f56SLionel Sambuc * probably not worthwhile.
127*00785f56SLionel Sambuc */
128*00785f56SLionel Sambuc nargs = 5000;
129*00785f56SLionel Sambuc if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
130*00785f56SLionel Sambuc errx(1, "sysconf(_SC_ARG_MAX) failed");
131*00785f56SLionel Sambuc nline = arg_max - 4 * 1024;
132*00785f56SLionel Sambuc while (*ep != NULL) {
133*00785f56SLionel Sambuc /* 1 byte for each '\0' */
134*00785f56SLionel Sambuc nline -= strlen(*ep++) + 1 + sizeof(*ep);
135*00785f56SLionel Sambuc }
136*00785f56SLionel Sambuc maxprocs = 1;
137*00785f56SLionel Sambuc while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:S:s:rtx")) != -1)
138*00785f56SLionel Sambuc switch (ch) {
139*00785f56SLionel Sambuc case 'E':
140*00785f56SLionel Sambuc eofstr = optarg;
141*00785f56SLionel Sambuc break;
142*00785f56SLionel Sambuc case 'I':
143*00785f56SLionel Sambuc Jflag = 0;
144*00785f56SLionel Sambuc Iflag = 1;
145*00785f56SLionel Sambuc Lflag = 1;
146*00785f56SLionel Sambuc replstr = optarg;
147*00785f56SLionel Sambuc break;
148*00785f56SLionel Sambuc case 'J':
149*00785f56SLionel Sambuc Iflag = 0;
150*00785f56SLionel Sambuc Jflag = 1;
151*00785f56SLionel Sambuc replstr = optarg;
152*00785f56SLionel Sambuc break;
153*00785f56SLionel Sambuc case 'L':
154*00785f56SLionel Sambuc Lflag = atoi(optarg);
155*00785f56SLionel Sambuc break;
156*00785f56SLionel Sambuc case 'n':
157*00785f56SLionel Sambuc nflag = 1;
158*00785f56SLionel Sambuc if ((nargs = atoi(optarg)) <= 0)
159*00785f56SLionel Sambuc errx(1, "illegal argument count");
160*00785f56SLionel Sambuc break;
161*00785f56SLionel Sambuc case 'o':
162*00785f56SLionel Sambuc oflag = 1;
163*00785f56SLionel Sambuc break;
164*00785f56SLionel Sambuc case 'P':
165*00785f56SLionel Sambuc if ((maxprocs = atoi(optarg)) <= 0)
166*00785f56SLionel Sambuc errx(1, "max. processes must be >0");
167*00785f56SLionel Sambuc break;
168*00785f56SLionel Sambuc case 'p':
169*00785f56SLionel Sambuc pflag = 1;
170*00785f56SLionel Sambuc break;
171*00785f56SLionel Sambuc case 'R':
172*00785f56SLionel Sambuc Rflag = strtol(optarg, &endptr, 10);
173*00785f56SLionel Sambuc if (*endptr != '\0')
174*00785f56SLionel Sambuc errx(1, "replacements must be a number");
175*00785f56SLionel Sambuc break;
176*00785f56SLionel Sambuc case 'r':
177*00785f56SLionel Sambuc /* GNU compatibility */
178*00785f56SLionel Sambuc break;
179*00785f56SLionel Sambuc case 'S':
180*00785f56SLionel Sambuc Sflag = strtoul(optarg, &endptr, 10);
181*00785f56SLionel Sambuc if (*endptr != '\0')
182*00785f56SLionel Sambuc errx(1, "replsize must be a number");
183*00785f56SLionel Sambuc break;
184*00785f56SLionel Sambuc case 's':
185*00785f56SLionel Sambuc nline = atoi(optarg);
186*00785f56SLionel Sambuc break;
187*00785f56SLionel Sambuc case 't':
188*00785f56SLionel Sambuc tflag = 1;
189*00785f56SLionel Sambuc break;
190*00785f56SLionel Sambuc case 'x':
191*00785f56SLionel Sambuc xflag = 1;
192*00785f56SLionel Sambuc break;
193*00785f56SLionel Sambuc case '0':
194*00785f56SLionel Sambuc zflag = 1;
195*00785f56SLionel Sambuc break;
196*00785f56SLionel Sambuc case '?':
197*00785f56SLionel Sambuc default:
198*00785f56SLionel Sambuc usage();
199*00785f56SLionel Sambuc }
200*00785f56SLionel Sambuc argc -= optind;
201*00785f56SLionel Sambuc argv += optind;
202*00785f56SLionel Sambuc
203*00785f56SLionel Sambuc if (!Iflag && Rflag)
204*00785f56SLionel Sambuc usage();
205*00785f56SLionel Sambuc if (!Iflag && Sflag)
206*00785f56SLionel Sambuc usage();
207*00785f56SLionel Sambuc if (Iflag && !Rflag)
208*00785f56SLionel Sambuc Rflag = 5;
209*00785f56SLionel Sambuc if (Iflag && !Sflag)
210*00785f56SLionel Sambuc Sflag = 255;
211*00785f56SLionel Sambuc if (xflag && !nflag)
212*00785f56SLionel Sambuc usage();
213*00785f56SLionel Sambuc if (Iflag || Lflag)
214*00785f56SLionel Sambuc xflag = 1;
215*00785f56SLionel Sambuc if (replstr != NULL && *replstr == '\0')
216*00785f56SLionel Sambuc errx(1, "replstr may not be empty");
217*00785f56SLionel Sambuc
218*00785f56SLionel Sambuc /*
219*00785f56SLionel Sambuc * Allocate pointers for the utility name, the utility arguments,
220*00785f56SLionel Sambuc * the maximum arguments to be read from stdin and the trailing
221*00785f56SLionel Sambuc * NULL.
222*00785f56SLionel Sambuc */
223*00785f56SLionel Sambuc linelen = 1 + argc + nargs + 1;
224*00785f56SLionel Sambuc if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL)
225*00785f56SLionel Sambuc errx(1, "malloc failed");
226*00785f56SLionel Sambuc
227*00785f56SLionel Sambuc /*
228*00785f56SLionel Sambuc * Use the user's name for the utility as argv[0], just like the
229*00785f56SLionel Sambuc * shell. Echo is the default. Set up pointers for the user's
230*00785f56SLionel Sambuc * arguments.
231*00785f56SLionel Sambuc */
232*00785f56SLionel Sambuc if (*argv == NULL)
233*00785f56SLionel Sambuc cnt = strlen(*bxp++ = echo);
234*00785f56SLionel Sambuc else {
235*00785f56SLionel Sambuc do {
236*00785f56SLionel Sambuc if (Jflag && strcmp(*argv, replstr) == 0) {
237*00785f56SLionel Sambuc char **avj;
238*00785f56SLionel Sambuc jfound = 1;
239*00785f56SLionel Sambuc argv++;
240*00785f56SLionel Sambuc for (avj = argv; *avj; avj++)
241*00785f56SLionel Sambuc cnt += strlen(*avj) + 1;
242*00785f56SLionel Sambuc break;
243*00785f56SLionel Sambuc }
244*00785f56SLionel Sambuc cnt += strlen(*bxp++ = *argv) + 1;
245*00785f56SLionel Sambuc } while (*++argv != NULL);
246*00785f56SLionel Sambuc }
247*00785f56SLionel Sambuc
248*00785f56SLionel Sambuc /*
249*00785f56SLionel Sambuc * Set up begin/end/traversing pointers into the array. The -n
250*00785f56SLionel Sambuc * count doesn't include the trailing NULL pointer, so the malloc
251*00785f56SLionel Sambuc * added in an extra slot.
252*00785f56SLionel Sambuc */
253*00785f56SLionel Sambuc endxp = (xp = bxp) + nargs;
254*00785f56SLionel Sambuc
255*00785f56SLionel Sambuc /*
256*00785f56SLionel Sambuc * Allocate buffer space for the arguments read from stdin and the
257*00785f56SLionel Sambuc * trailing NULL. Buffer space is defined as the default or specified
258*00785f56SLionel Sambuc * space, minus the length of the utility name and arguments. Set up
259*00785f56SLionel Sambuc * begin/end/traversing pointers into the array. The -s count does
260*00785f56SLionel Sambuc * include the trailing NULL, so the malloc didn't add in an extra
261*00785f56SLionel Sambuc * slot.
262*00785f56SLionel Sambuc */
263*00785f56SLionel Sambuc nline -= cnt;
264*00785f56SLionel Sambuc if (nline <= 0)
265*00785f56SLionel Sambuc errx(1, "insufficient space for command");
266*00785f56SLionel Sambuc
267*00785f56SLionel Sambuc if ((bbp = malloc((size_t)(nline + 1))) == NULL)
268*00785f56SLionel Sambuc errx(1, "malloc failed");
269*00785f56SLionel Sambuc ebp = (argp = p = bbp) + nline - 1;
270*00785f56SLionel Sambuc for (;;)
271*00785f56SLionel Sambuc parse_input(argc, argv);
272*00785f56SLionel Sambuc }
273*00785f56SLionel Sambuc
274*00785f56SLionel Sambuc static void
parse_input(int argc,char * argv[])275*00785f56SLionel Sambuc parse_input(int argc, char *argv[])
276*00785f56SLionel Sambuc {
277*00785f56SLionel Sambuc int ch, foundeof;
278*00785f56SLionel Sambuc char **avj;
279*00785f56SLionel Sambuc
280*00785f56SLionel Sambuc foundeof = 0;
281*00785f56SLionel Sambuc
282*00785f56SLionel Sambuc switch (ch = getchar()) {
283*00785f56SLionel Sambuc case EOF:
284*00785f56SLionel Sambuc /* No arguments since last exec. */
285*00785f56SLionel Sambuc if (p == bbp) {
286*00785f56SLionel Sambuc waitchildren(*argv, 1);
287*00785f56SLionel Sambuc exit(rval);
288*00785f56SLionel Sambuc }
289*00785f56SLionel Sambuc goto arg1;
290*00785f56SLionel Sambuc case ' ':
291*00785f56SLionel Sambuc case '\t':
292*00785f56SLionel Sambuc /* Quotes escape tabs and spaces. */
293*00785f56SLionel Sambuc if (insingle || indouble || zflag)
294*00785f56SLionel Sambuc goto addch;
295*00785f56SLionel Sambuc goto arg2;
296*00785f56SLionel Sambuc case '\0':
297*00785f56SLionel Sambuc if (zflag) {
298*00785f56SLionel Sambuc /*
299*00785f56SLionel Sambuc * Increment 'count', so that nulls will be treated
300*00785f56SLionel Sambuc * as end-of-line, as well as end-of-argument. This
301*00785f56SLionel Sambuc * is needed so -0 works properly with -I and -L.
302*00785f56SLionel Sambuc */
303*00785f56SLionel Sambuc count++;
304*00785f56SLionel Sambuc goto arg2;
305*00785f56SLionel Sambuc }
306*00785f56SLionel Sambuc goto addch;
307*00785f56SLionel Sambuc case '\n':
308*00785f56SLionel Sambuc if (zflag)
309*00785f56SLionel Sambuc goto addch;
310*00785f56SLionel Sambuc count++; /* Indicate end-of-line (used by -L) */
311*00785f56SLionel Sambuc
312*00785f56SLionel Sambuc /* Quotes do not escape newlines. */
313*00785f56SLionel Sambuc arg1: if (insingle || indouble)
314*00785f56SLionel Sambuc errx(1, "unterminated quote");
315*00785f56SLionel Sambuc arg2:
316*00785f56SLionel Sambuc foundeof = *eofstr != '\0' &&
317*00785f56SLionel Sambuc strncmp(argp, eofstr, (size_t)(p - argp)) == 0;
318*00785f56SLionel Sambuc
319*00785f56SLionel Sambuc /* Do not make empty args unless they are quoted */
320*00785f56SLionel Sambuc if ((argp != p || wasquoted) && !foundeof) {
321*00785f56SLionel Sambuc *p++ = '\0';
322*00785f56SLionel Sambuc *xp++ = argp;
323*00785f56SLionel Sambuc if (Iflag) {
324*00785f56SLionel Sambuc size_t curlen;
325*00785f56SLionel Sambuc
326*00785f56SLionel Sambuc if (inpline == NULL)
327*00785f56SLionel Sambuc curlen = 0;
328*00785f56SLionel Sambuc else {
329*00785f56SLionel Sambuc /*
330*00785f56SLionel Sambuc * If this string is not zero
331*00785f56SLionel Sambuc * length, append a space for
332*00785f56SLionel Sambuc * separation before the next
333*00785f56SLionel Sambuc * argument.
334*00785f56SLionel Sambuc */
335*00785f56SLionel Sambuc if ((curlen = strlen(inpline)) != 0)
336*00785f56SLionel Sambuc (void)strcat(inpline, " ");
337*00785f56SLionel Sambuc }
338*00785f56SLionel Sambuc curlen++;
339*00785f56SLionel Sambuc /*
340*00785f56SLionel Sambuc * Allocate enough to hold what we will
341*00785f56SLionel Sambuc * be holding in a second, and to append
342*00785f56SLionel Sambuc * a space next time through, if we have
343*00785f56SLionel Sambuc * to.
344*00785f56SLionel Sambuc */
345*00785f56SLionel Sambuc inpline = realloc(inpline, curlen + 2 +
346*00785f56SLionel Sambuc strlen(argp));
347*00785f56SLionel Sambuc if (inpline == NULL)
348*00785f56SLionel Sambuc errx(1, "realloc failed");
349*00785f56SLionel Sambuc if (curlen == 1)
350*00785f56SLionel Sambuc (void)strcpy(inpline, argp);
351*00785f56SLionel Sambuc else
352*00785f56SLionel Sambuc (void)strcat(inpline, argp);
353*00785f56SLionel Sambuc }
354*00785f56SLionel Sambuc }
355*00785f56SLionel Sambuc
356*00785f56SLionel Sambuc /*
357*00785f56SLionel Sambuc * If max'd out on args or buffer, or reached EOF,
358*00785f56SLionel Sambuc * run the command. If xflag and max'd out on buffer
359*00785f56SLionel Sambuc * but not on args, object. Having reached the limit
360*00785f56SLionel Sambuc * of input lines, as specified by -L is the same as
361*00785f56SLionel Sambuc * maxing out on arguments.
362*00785f56SLionel Sambuc */
363*00785f56SLionel Sambuc if (xp == endxp || p > ebp || ch == EOF ||
364*00785f56SLionel Sambuc (Lflag <= count && xflag) || foundeof) {
365*00785f56SLionel Sambuc if (xflag && xp != endxp && p > ebp)
366*00785f56SLionel Sambuc errx(1, "insufficient space for arguments");
367*00785f56SLionel Sambuc if (jfound) {
368*00785f56SLionel Sambuc for (avj = argv; *avj; avj++)
369*00785f56SLionel Sambuc *xp++ = *avj;
370*00785f56SLionel Sambuc }
371*00785f56SLionel Sambuc prerun(argc, av);
372*00785f56SLionel Sambuc if (ch == EOF || foundeof) {
373*00785f56SLionel Sambuc waitchildren(*argv, 1);
374*00785f56SLionel Sambuc exit(rval);
375*00785f56SLionel Sambuc }
376*00785f56SLionel Sambuc p = bbp;
377*00785f56SLionel Sambuc xp = bxp;
378*00785f56SLionel Sambuc count = 0;
379*00785f56SLionel Sambuc }
380*00785f56SLionel Sambuc argp = p;
381*00785f56SLionel Sambuc wasquoted = 0;
382*00785f56SLionel Sambuc break;
383*00785f56SLionel Sambuc case '\'':
384*00785f56SLionel Sambuc if (indouble || zflag)
385*00785f56SLionel Sambuc goto addch;
386*00785f56SLionel Sambuc insingle = !insingle;
387*00785f56SLionel Sambuc wasquoted = 1;
388*00785f56SLionel Sambuc break;
389*00785f56SLionel Sambuc case '"':
390*00785f56SLionel Sambuc if (insingle || zflag)
391*00785f56SLionel Sambuc goto addch;
392*00785f56SLionel Sambuc indouble = !indouble;
393*00785f56SLionel Sambuc wasquoted = 1;
394*00785f56SLionel Sambuc break;
395*00785f56SLionel Sambuc case '\\':
396*00785f56SLionel Sambuc if (zflag)
397*00785f56SLionel Sambuc goto addch;
398*00785f56SLionel Sambuc /* Backslash escapes anything, is escaped by quotes. */
399*00785f56SLionel Sambuc if (!insingle && !indouble && (ch = getchar()) == EOF)
400*00785f56SLionel Sambuc errx(1, "backslash at EOF");
401*00785f56SLionel Sambuc /* FALLTHROUGH */
402*00785f56SLionel Sambuc default:
403*00785f56SLionel Sambuc addch: if (p < ebp) {
404*00785f56SLionel Sambuc *p++ = ch;
405*00785f56SLionel Sambuc break;
406*00785f56SLionel Sambuc }
407*00785f56SLionel Sambuc
408*00785f56SLionel Sambuc /* If only one argument, not enough buffer space. */
409*00785f56SLionel Sambuc if (bxp == xp)
410*00785f56SLionel Sambuc errx(1, "insufficient space for argument");
411*00785f56SLionel Sambuc /* Didn't hit argument limit, so if xflag object. */
412*00785f56SLionel Sambuc if (xflag)
413*00785f56SLionel Sambuc errx(1, "insufficient space for arguments");
414*00785f56SLionel Sambuc
415*00785f56SLionel Sambuc if (jfound) {
416*00785f56SLionel Sambuc for (avj = argv; *avj; avj++)
417*00785f56SLionel Sambuc *xp++ = *avj;
418*00785f56SLionel Sambuc }
419*00785f56SLionel Sambuc prerun(argc, av);
420*00785f56SLionel Sambuc xp = bxp;
421*00785f56SLionel Sambuc cnt = ebp - argp;
422*00785f56SLionel Sambuc (void)memcpy(bbp, argp, (size_t)cnt);
423*00785f56SLionel Sambuc p = (argp = bbp) + cnt;
424*00785f56SLionel Sambuc *p++ = ch;
425*00785f56SLionel Sambuc break;
426*00785f56SLionel Sambuc }
427*00785f56SLionel Sambuc }
428*00785f56SLionel Sambuc
429*00785f56SLionel Sambuc /*
430*00785f56SLionel Sambuc * Do things necessary before run()'ing, such as -I substitution,
431*00785f56SLionel Sambuc * and then call run().
432*00785f56SLionel Sambuc */
433*00785f56SLionel Sambuc static void
prerun(int argc,char * argv[])434*00785f56SLionel Sambuc prerun(int argc, char *argv[])
435*00785f56SLionel Sambuc {
436*00785f56SLionel Sambuc char **tmp, **tmp2, **avj;
437*00785f56SLionel Sambuc int repls;
438*00785f56SLionel Sambuc
439*00785f56SLionel Sambuc repls = Rflag;
440*00785f56SLionel Sambuc
441*00785f56SLionel Sambuc if (argc == 0 || repls == 0) {
442*00785f56SLionel Sambuc *xp = NULL;
443*00785f56SLionel Sambuc run(argv);
444*00785f56SLionel Sambuc return;
445*00785f56SLionel Sambuc }
446*00785f56SLionel Sambuc
447*00785f56SLionel Sambuc avj = argv;
448*00785f56SLionel Sambuc
449*00785f56SLionel Sambuc /*
450*00785f56SLionel Sambuc * Allocate memory to hold the argument list, and
451*00785f56SLionel Sambuc * a NULL at the tail.
452*00785f56SLionel Sambuc */
453*00785f56SLionel Sambuc tmp = malloc((argc + 1) * sizeof(char**));
454*00785f56SLionel Sambuc if (tmp == NULL)
455*00785f56SLionel Sambuc errx(1, "malloc failed");
456*00785f56SLionel Sambuc tmp2 = tmp;
457*00785f56SLionel Sambuc
458*00785f56SLionel Sambuc /*
459*00785f56SLionel Sambuc * Save the first argument and iterate over it, we
460*00785f56SLionel Sambuc * cannot do strnsubst() to it.
461*00785f56SLionel Sambuc */
462*00785f56SLionel Sambuc if ((*tmp++ = strdup(*avj++)) == NULL)
463*00785f56SLionel Sambuc errx(1, "strdup failed");
464*00785f56SLionel Sambuc
465*00785f56SLionel Sambuc /*
466*00785f56SLionel Sambuc * For each argument to utility, if we have not used up
467*00785f56SLionel Sambuc * the number of replacements we are allowed to do, and
468*00785f56SLionel Sambuc * if the argument contains at least one occurrence of
469*00785f56SLionel Sambuc * replstr, call strnsubst(), else just save the string.
470*00785f56SLionel Sambuc * Iterations over elements of avj and tmp are done
471*00785f56SLionel Sambuc * where appropriate.
472*00785f56SLionel Sambuc */
473*00785f56SLionel Sambuc while (--argc) {
474*00785f56SLionel Sambuc *tmp = *avj++;
475*00785f56SLionel Sambuc if (repls && strstr(*tmp, replstr) != NULL) {
476*00785f56SLionel Sambuc strnsubst(tmp++, replstr, inpline, (size_t)Sflag);
477*00785f56SLionel Sambuc if (repls > 0)
478*00785f56SLionel Sambuc repls--;
479*00785f56SLionel Sambuc } else {
480*00785f56SLionel Sambuc if ((*tmp = strdup(*tmp)) == NULL)
481*00785f56SLionel Sambuc errx(1, "strdup failed");
482*00785f56SLionel Sambuc tmp++;
483*00785f56SLionel Sambuc }
484*00785f56SLionel Sambuc }
485*00785f56SLionel Sambuc
486*00785f56SLionel Sambuc /*
487*00785f56SLionel Sambuc * Run it.
488*00785f56SLionel Sambuc */
489*00785f56SLionel Sambuc *tmp = NULL;
490*00785f56SLionel Sambuc run(tmp2);
491*00785f56SLionel Sambuc
492*00785f56SLionel Sambuc /*
493*00785f56SLionel Sambuc * Walk from the tail to the head, free along the way.
494*00785f56SLionel Sambuc */
495*00785f56SLionel Sambuc for (; tmp2 != tmp; tmp--)
496*00785f56SLionel Sambuc free(*tmp);
497*00785f56SLionel Sambuc /*
498*00785f56SLionel Sambuc * Now free the list itself.
499*00785f56SLionel Sambuc */
500*00785f56SLionel Sambuc free(tmp2);
501*00785f56SLionel Sambuc
502*00785f56SLionel Sambuc /*
503*00785f56SLionel Sambuc * Free the input line buffer, if we have one.
504*00785f56SLionel Sambuc */
505*00785f56SLionel Sambuc if (inpline != NULL) {
506*00785f56SLionel Sambuc free(inpline);
507*00785f56SLionel Sambuc inpline = NULL;
508*00785f56SLionel Sambuc }
509*00785f56SLionel Sambuc }
510*00785f56SLionel Sambuc
511*00785f56SLionel Sambuc static void
run(char ** argv)512*00785f56SLionel Sambuc run(char **argv)
513*00785f56SLionel Sambuc {
514*00785f56SLionel Sambuc int fd;
515*00785f56SLionel Sambuc char **avec;
516*00785f56SLionel Sambuc
517*00785f56SLionel Sambuc /*
518*00785f56SLionel Sambuc * If the user wants to be notified of each command before it is
519*00785f56SLionel Sambuc * executed, notify them. If they want the notification to be
520*00785f56SLionel Sambuc * followed by a prompt, then prompt them.
521*00785f56SLionel Sambuc */
522*00785f56SLionel Sambuc if (tflag || pflag) {
523*00785f56SLionel Sambuc (void)fprintf(stderr, "%s", *argv);
524*00785f56SLionel Sambuc for (avec = argv + 1; *avec != NULL; ++avec)
525*00785f56SLionel Sambuc (void)fprintf(stderr, " %s", *avec);
526*00785f56SLionel Sambuc /*
527*00785f56SLionel Sambuc * If the user has asked to be prompted, do so.
528*00785f56SLionel Sambuc */
529*00785f56SLionel Sambuc if (pflag)
530*00785f56SLionel Sambuc /*
531*00785f56SLionel Sambuc * If they asked not to exec, return without execution
532*00785f56SLionel Sambuc * but if they asked to, go to the execution. If we
533*00785f56SLionel Sambuc * could not open their tty, break the switch and drop
534*00785f56SLionel Sambuc * back to -t behaviour.
535*00785f56SLionel Sambuc */
536*00785f56SLionel Sambuc switch (prompt()) {
537*00785f56SLionel Sambuc case 0:
538*00785f56SLionel Sambuc return;
539*00785f56SLionel Sambuc case 1:
540*00785f56SLionel Sambuc goto exec;
541*00785f56SLionel Sambuc case 2:
542*00785f56SLionel Sambuc break;
543*00785f56SLionel Sambuc }
544*00785f56SLionel Sambuc (void)fprintf(stderr, "\n");
545*00785f56SLionel Sambuc (void)fflush(stderr);
546*00785f56SLionel Sambuc }
547*00785f56SLionel Sambuc exec:
548*00785f56SLionel Sambuc childerr = 0;
549*00785f56SLionel Sambuc switch (vfork()) {
550*00785f56SLionel Sambuc case -1:
551*00785f56SLionel Sambuc err(1, "vfork");
552*00785f56SLionel Sambuc /*NOTREACHED*/
553*00785f56SLionel Sambuc case 0:
554*00785f56SLionel Sambuc if (oflag) {
555*00785f56SLionel Sambuc if ((fd = open(_PATH_TTY, O_RDONLY)) == -1)
556*00785f56SLionel Sambuc err(1, "can't open /dev/tty");
557*00785f56SLionel Sambuc } else {
558*00785f56SLionel Sambuc fd = open(_PATH_DEVNULL, O_RDONLY);
559*00785f56SLionel Sambuc }
560*00785f56SLionel Sambuc if (fd > STDIN_FILENO) {
561*00785f56SLionel Sambuc if (dup2(fd, STDIN_FILENO) != 0)
562*00785f56SLionel Sambuc err(1, "can't dup2 to stdin");
563*00785f56SLionel Sambuc (void)close(fd);
564*00785f56SLionel Sambuc }
565*00785f56SLionel Sambuc (void)execvp(argv[0], argv);
566*00785f56SLionel Sambuc childerr = errno;
567*00785f56SLionel Sambuc _exit(1);
568*00785f56SLionel Sambuc }
569*00785f56SLionel Sambuc curprocs++;
570*00785f56SLionel Sambuc waitchildren(*argv, 0);
571*00785f56SLionel Sambuc }
572*00785f56SLionel Sambuc
573*00785f56SLionel Sambuc static void
waitchildren(const char * name,int waitall)574*00785f56SLionel Sambuc waitchildren(const char *name, int waitall)
575*00785f56SLionel Sambuc {
576*00785f56SLionel Sambuc pid_t pid;
577*00785f56SLionel Sambuc int status;
578*00785f56SLionel Sambuc
579*00785f56SLionel Sambuc while ((pid = waitpid(-1, &status, !waitall && curprocs < maxprocs ?
580*00785f56SLionel Sambuc WNOHANG : 0)) > 0) {
581*00785f56SLionel Sambuc curprocs--;
582*00785f56SLionel Sambuc /* If we couldn't invoke the utility, exit. */
583*00785f56SLionel Sambuc if (childerr != 0) {
584*00785f56SLionel Sambuc errno = childerr;
585*00785f56SLionel Sambuc err(errno == ENOENT ? 127 : 126, "%s", name);
586*00785f56SLionel Sambuc }
587*00785f56SLionel Sambuc /*
588*00785f56SLionel Sambuc * According to POSIX, we have to exit if the utility exits
589*00785f56SLionel Sambuc * with a 255 status, or is interrupted by a signal. xargs
590*00785f56SLionel Sambuc * is allowed to return any exit status between 1 and 125
591*00785f56SLionel Sambuc * in these cases, but we'll use 124 and 125, the same
592*00785f56SLionel Sambuc * values used by GNU xargs.
593*00785f56SLionel Sambuc */
594*00785f56SLionel Sambuc if (WIFEXITED(status)) {
595*00785f56SLionel Sambuc if (WEXITSTATUS (status) == 255) {
596*00785f56SLionel Sambuc warnx ("%s exited with status 255", name);
597*00785f56SLionel Sambuc exit(124);
598*00785f56SLionel Sambuc } else if (WEXITSTATUS (status) != 0) {
599*00785f56SLionel Sambuc rval = 123;
600*00785f56SLionel Sambuc }
601*00785f56SLionel Sambuc } else if (WIFSIGNALED (status)) {
602*00785f56SLionel Sambuc if (WTERMSIG(status) < NSIG) {
603*00785f56SLionel Sambuc warnx("%s terminated by SIG%s", name,
604*00785f56SLionel Sambuc sys_signame[WTERMSIG(status)]);
605*00785f56SLionel Sambuc } else {
606*00785f56SLionel Sambuc warnx("%s terminated by signal %d", name,
607*00785f56SLionel Sambuc WTERMSIG(status));
608*00785f56SLionel Sambuc }
609*00785f56SLionel Sambuc exit(125);
610*00785f56SLionel Sambuc }
611*00785f56SLionel Sambuc }
612*00785f56SLionel Sambuc if (pid == -1 && errno != ECHILD)
613*00785f56SLionel Sambuc err(1, "waitpid");
614*00785f56SLionel Sambuc }
615*00785f56SLionel Sambuc
616*00785f56SLionel Sambuc /*
617*00785f56SLionel Sambuc * Prompt the user about running a command.
618*00785f56SLionel Sambuc */
619*00785f56SLionel Sambuc static int
prompt(void)620*00785f56SLionel Sambuc prompt(void)
621*00785f56SLionel Sambuc {
622*00785f56SLionel Sambuc regex_t cre;
623*00785f56SLionel Sambuc size_t rsize;
624*00785f56SLionel Sambuc int match;
625*00785f56SLionel Sambuc char *response;
626*00785f56SLionel Sambuc FILE *ttyfp;
627*00785f56SLionel Sambuc
628*00785f56SLionel Sambuc if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
629*00785f56SLionel Sambuc return (2); /* Indicate that the TTY failed to open. */
630*00785f56SLionel Sambuc (void)fprintf(stderr, "?...");
631*00785f56SLionel Sambuc (void)fflush(stderr);
632*00785f56SLionel Sambuc if ((response = fgetln(ttyfp, &rsize)) == NULL ||
633*00785f56SLionel Sambuc regcomp(&cre, nl_langinfo(YESEXPR), REG_BASIC) != 0) {
634*00785f56SLionel Sambuc (void)fclose(ttyfp);
635*00785f56SLionel Sambuc return (0);
636*00785f56SLionel Sambuc }
637*00785f56SLionel Sambuc response[rsize - 1] = '\0';
638*00785f56SLionel Sambuc match = regexec(&cre, response, 0, NULL, 0);
639*00785f56SLionel Sambuc (void)fclose(ttyfp);
640*00785f56SLionel Sambuc regfree(&cre);
641*00785f56SLionel Sambuc return (match == 0);
642*00785f56SLionel Sambuc }
643*00785f56SLionel Sambuc
644*00785f56SLionel Sambuc static void
usage(void)645*00785f56SLionel Sambuc usage(void)
646*00785f56SLionel Sambuc {
647*00785f56SLionel Sambuc (void)fprintf(stderr,
648*00785f56SLionel Sambuc "Usage: %s [-0opt] [-E eofstr] [-I replstr [-R replacements] [-S replsize]]\n"
649*00785f56SLionel Sambuc " [-J replstr] [-L number] [-n number [-x]] [-P maxprocs]\n"
650*00785f56SLionel Sambuc " [-s size] [utility [argument ...]]\n", getprogname());
651*00785f56SLionel Sambuc exit(1);
652*00785f56SLionel Sambuc }
653