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