xref: /freebsd/usr.bin/xargs/xargs.c (revision d919888163928142efa71fec61565bd849ebb9a9)
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.
359b50d902SRodney W. Grimes  */
369b50d902SRodney W. Grimes 
379b50d902SRodney W. Grimes #ifndef lint
389b50d902SRodney W. Grimes static char copyright[] =
399b50d902SRodney W. Grimes "@(#) Copyright (c) 1990, 1993\n\
409b50d902SRodney W. Grimes 	The Regents of the University of California.  All rights reserved.\n";
419b50d902SRodney W. Grimes #endif /* not lint */
429b50d902SRodney W. Grimes 
439b50d902SRodney W. Grimes #ifndef lint
449b50d902SRodney W. Grimes static char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
459b50d902SRodney W. Grimes #endif /* not lint */
469b50d902SRodney W. Grimes 
479b50d902SRodney W. Grimes #include <sys/types.h>
489b50d902SRodney W. Grimes #include <sys/wait.h>
499b50d902SRodney W. Grimes #include <errno.h>
509b50d902SRodney W. Grimes #include <stdio.h>
519b50d902SRodney W. Grimes #include <stdlib.h>
529b50d902SRodney W. Grimes #include <string.h>
539b50d902SRodney W. Grimes #include <unistd.h>
549b50d902SRodney W. Grimes #include <limits.h>
559b50d902SRodney W. Grimes #include "pathnames.h"
569b50d902SRodney W. Grimes 
579b50d902SRodney W. Grimes int tflag, rval;
58d9198881SWarner Losh int zflag;
599b50d902SRodney W. Grimes 
609b50d902SRodney W. Grimes void err __P((const char *, ...));
619b50d902SRodney W. Grimes void run __P((char **));
629b50d902SRodney W. Grimes void usage __P((void));
639b50d902SRodney W. Grimes 
64e5009da0SSatoshi Asami main(argc, argv, env)
659b50d902SRodney W. Grimes 	int argc;
66e5009da0SSatoshi Asami 	char **argv, **env;
679b50d902SRodney W. Grimes {
689b50d902SRodney W. Grimes 	register int ch;
699b50d902SRodney W. Grimes 	register char *p, *bbp, *ebp, **bxp, **exp, **xp;
709b50d902SRodney W. Grimes 	int cnt, indouble, insingle, nargs, nflag, nline, xflag;
71e5009da0SSatoshi Asami 	char **av, *argp, **ep = env;
729b50d902SRodney W. Grimes 
739b50d902SRodney W. Grimes 	/*
749b50d902SRodney W. Grimes 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
759b50d902SRodney W. Grimes 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
769b50d902SRodney W. Grimes 	 * that the smallest argument is 2 bytes in length, this means that
779b50d902SRodney W. Grimes 	 * the number of arguments is limited to:
789b50d902SRodney W. Grimes 	 *
799b50d902SRodney W. Grimes 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
809b50d902SRodney W. Grimes 	 *
819b50d902SRodney W. Grimes 	 * We arbitrarily limit the number of arguments to 5000.  This is
829b50d902SRodney W. Grimes 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
839b50d902SRodney W. Grimes 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
849b50d902SRodney W. Grimes 	 * probably not worthwhile.
859b50d902SRodney W. Grimes 	 */
869b50d902SRodney W. Grimes 	nargs = 5000;
879b50d902SRodney W. Grimes 	nline = ARG_MAX - 4 * 1024;
88e5009da0SSatoshi Asami 	while (*ep) {
89e5009da0SSatoshi Asami 		/* 1 byte for each '\0' */
90e5009da0SSatoshi Asami 		nline -= strlen(*ep++) + 1 + sizeof(*ep);
91e5009da0SSatoshi Asami 	}
929b50d902SRodney W. Grimes 	nflag = xflag = 0;
93d9198881SWarner Losh 	while ((ch = getopt(argc, argv, "0n:s:tx")) != EOF)
949b50d902SRodney W. Grimes 		switch(ch) {
959b50d902SRodney W. Grimes 		case 'n':
969b50d902SRodney W. Grimes 			nflag = 1;
979b50d902SRodney W. Grimes 			if ((nargs = atoi(optarg)) <= 0)
989b50d902SRodney W. Grimes 				err("illegal argument count");
999b50d902SRodney W. Grimes 			break;
1009b50d902SRodney W. Grimes 		case 's':
1019b50d902SRodney W. Grimes 			nline = atoi(optarg);
1029b50d902SRodney W. Grimes 			break;
1039b50d902SRodney W. Grimes 		case 't':
1049b50d902SRodney W. Grimes 			tflag = 1;
1059b50d902SRodney W. Grimes 			break;
1069b50d902SRodney W. Grimes 		case 'x':
1079b50d902SRodney W. Grimes 			xflag = 1;
1089b50d902SRodney W. Grimes 			break;
109d9198881SWarner Losh 		case '0':
110d9198881SWarner Losh 			zflag = 1;
111d9198881SWarner Losh 			break;
1129b50d902SRodney W. Grimes 		case '?':
1139b50d902SRodney W. Grimes 		default:
1149b50d902SRodney W. Grimes 			usage();
1159b50d902SRodney W. Grimes 	}
1169b50d902SRodney W. Grimes 	argc -= optind;
1179b50d902SRodney W. Grimes 	argv += optind;
1189b50d902SRodney W. Grimes 
1199b50d902SRodney W. Grimes 	if (xflag && !nflag)
1209b50d902SRodney W. Grimes 		usage();
1219b50d902SRodney W. Grimes 
1229b50d902SRodney W. Grimes 	/*
1239b50d902SRodney W. Grimes 	 * Allocate pointers for the utility name, the utility arguments,
1249b50d902SRodney W. Grimes 	 * the maximum arguments to be read from stdin and the trailing
1259b50d902SRodney W. Grimes 	 * NULL.
1269b50d902SRodney W. Grimes 	 */
1279b50d902SRodney W. Grimes 	if (!(av = bxp =
1289b50d902SRodney W. Grimes 	    malloc((u_int)(1 + argc + nargs + 1) * sizeof(char **))))
1299b50d902SRodney W. Grimes 		err("%s", strerror(errno));
1309b50d902SRodney W. Grimes 
1319b50d902SRodney W. Grimes 	/*
1329b50d902SRodney W. Grimes 	 * Use the user's name for the utility as argv[0], just like the
1339b50d902SRodney W. Grimes 	 * shell.  Echo is the default.  Set up pointers for the user's
1349b50d902SRodney W. Grimes 	 * arguments.
1359b50d902SRodney W. Grimes 	 */
1369b50d902SRodney W. Grimes 	if (!*argv)
1379b50d902SRodney W. Grimes 		cnt = strlen(*bxp++ = _PATH_ECHO);
1389b50d902SRodney W. Grimes 	else {
1399b50d902SRodney W. Grimes 		cnt = 0;
1409b50d902SRodney W. Grimes 		do {
1419b50d902SRodney W. Grimes 			cnt += strlen(*bxp++ = *argv) + 1;
1429b50d902SRodney W. Grimes 		} while (*++argv);
1439b50d902SRodney W. Grimes 	}
1449b50d902SRodney W. Grimes 
1459b50d902SRodney W. Grimes 	/*
1469b50d902SRodney W. Grimes 	 * Set up begin/end/traversing pointers into the array.  The -n
1479b50d902SRodney W. Grimes 	 * count doesn't include the trailing NULL pointer, so the malloc
1489b50d902SRodney W. Grimes 	 * added in an extra slot.
1499b50d902SRodney W. Grimes 	 */
1509b50d902SRodney W. Grimes 	exp = (xp = bxp) + nargs;
1519b50d902SRodney W. Grimes 
1529b50d902SRodney W. Grimes 	/*
1539b50d902SRodney W. Grimes 	 * Allocate buffer space for the arguments read from stdin and the
1549b50d902SRodney W. Grimes 	 * trailing NULL.  Buffer space is defined as the default or specified
1559b50d902SRodney W. Grimes 	 * space, minus the length of the utility name and arguments.  Set up
1569b50d902SRodney W. Grimes 	 * begin/end/traversing pointers into the array.  The -s count does
1579b50d902SRodney W. Grimes 	 * include the trailing NULL, so the malloc didn't add in an extra
1589b50d902SRodney W. Grimes 	 * slot.
1599b50d902SRodney W. Grimes 	 */
1609b50d902SRodney W. Grimes 	nline -= cnt;
1619b50d902SRodney W. Grimes 	if (nline <= 0)
1629b50d902SRodney W. Grimes 		err("insufficient space for command");
1639b50d902SRodney W. Grimes 
1649b50d902SRodney W. Grimes 	if (!(bbp = malloc((u_int)nline + 1)))
1659b50d902SRodney W. Grimes 		err("%s", strerror(errno));
1669b50d902SRodney W. Grimes 	ebp = (argp = p = bbp) + nline - 1;
1679b50d902SRodney W. Grimes 
1689b50d902SRodney W. Grimes 	for (insingle = indouble = 0;;)
1699b50d902SRodney W. Grimes 		switch(ch = getchar()) {
1709b50d902SRodney W. Grimes 		case EOF:
1719b50d902SRodney W. Grimes 			/* No arguments since last exec. */
1729b50d902SRodney W. Grimes 			if (p == bbp)
1739b50d902SRodney W. Grimes 				exit(rval);
1749b50d902SRodney W. Grimes 
1759b50d902SRodney W. Grimes 			/* Nothing since end of last argument. */
1769b50d902SRodney W. Grimes 			if (argp == p) {
1779b50d902SRodney W. Grimes 				*xp = NULL;
1789b50d902SRodney W. Grimes 				run(av);
1799b50d902SRodney W. Grimes 				exit(rval);
1809b50d902SRodney W. Grimes 			}
1819b50d902SRodney W. Grimes 			goto arg1;
1829b50d902SRodney W. Grimes 		case ' ':
1839b50d902SRodney W. Grimes 		case '\t':
1849b50d902SRodney W. Grimes 			/* Quotes escape tabs and spaces. */
185d9198881SWarner Losh 			if (insingle || indouble || zflag)
1869b50d902SRodney W. Grimes 				goto addch;
1879b50d902SRodney W. Grimes 			goto arg2;
188d9198881SWarner Losh 		case '\0':
189d9198881SWarner Losh 			if (zflag)
190d9198881SWarner Losh 				goto arg2;
191d9198881SWarner Losh 			goto addch;
1929b50d902SRodney W. Grimes 		case '\n':
193d9198881SWarner Losh 			if (zflag)
194d9198881SWarner Losh 				goto addch;
195d9198881SWarner Losh 
1969b50d902SRodney W. Grimes 			/* Empty lines are skipped. */
1979b50d902SRodney W. Grimes 			if (argp == p)
1989b50d902SRodney W. Grimes 				continue;
1999b50d902SRodney W. Grimes 
2009b50d902SRodney W. Grimes 			/* Quotes do not escape newlines. */
2019b50d902SRodney W. Grimes arg1:			if (insingle || indouble)
2029b50d902SRodney W. Grimes 				 err("unterminated quote");
2039b50d902SRodney W. Grimes 
2049b50d902SRodney W. Grimes arg2:			*p = '\0';
2059b50d902SRodney W. Grimes 			*xp++ = argp;
2069b50d902SRodney W. Grimes 
2079b50d902SRodney W. Grimes 			/*
2089b50d902SRodney W. Grimes 			 * If max'd out on args or buffer, or reached EOF,
2099b50d902SRodney W. Grimes 			 * run the command.  If xflag and max'd out on buffer
2109b50d902SRodney W. Grimes 			 * but not on args, object.
2119b50d902SRodney W. Grimes 			 */
2129b50d902SRodney W. Grimes 			if (xp == exp || p == ebp || ch == EOF) {
2139b50d902SRodney W. Grimes 				if (xflag && xp != exp && p == ebp)
2149b50d902SRodney W. Grimes 					err("insufficient space for arguments");
2159b50d902SRodney W. Grimes 				*xp = NULL;
2169b50d902SRodney W. Grimes 				run(av);
2179b50d902SRodney W. Grimes 				if (ch == EOF)
2189b50d902SRodney W. Grimes 					exit(rval);
2199b50d902SRodney W. Grimes 				p = bbp;
2209b50d902SRodney W. Grimes 				xp = bxp;
2219b50d902SRodney W. Grimes 			} else
2229b50d902SRodney W. Grimes 				++p;
2239b50d902SRodney W. Grimes 			argp = p;
2249b50d902SRodney W. Grimes 			break;
2259b50d902SRodney W. Grimes 		case '\'':
226d9198881SWarner Losh 			if (indouble || zflag)
2279b50d902SRodney W. Grimes 				goto addch;
2289b50d902SRodney W. Grimes 			insingle = !insingle;
2299b50d902SRodney W. Grimes 			break;
2309b50d902SRodney W. Grimes 		case '"':
231d9198881SWarner Losh 			if (insingle || zflag)
2329b50d902SRodney W. Grimes 				goto addch;
2339b50d902SRodney W. Grimes 			indouble = !indouble;
2349b50d902SRodney W. Grimes 			break;
2359b50d902SRodney W. Grimes 		case '\\':
236d9198881SWarner Losh 			if (zflag)
237d9198881SWarner Losh 				goto addch;
2389b50d902SRodney W. Grimes 			/* Backslash escapes anything, is escaped by quotes. */
2399b50d902SRodney W. Grimes 			if (!insingle && !indouble && (ch = getchar()) == EOF)
2409b50d902SRodney W. Grimes 				err("backslash at EOF");
2419b50d902SRodney W. Grimes 			/* FALLTHROUGH */
2429b50d902SRodney W. Grimes 		default:
2439b50d902SRodney W. Grimes addch:			if (p < ebp) {
2449b50d902SRodney W. Grimes 				*p++ = ch;
2459b50d902SRodney W. Grimes 				break;
2469b50d902SRodney W. Grimes 			}
2479b50d902SRodney W. Grimes 
2489b50d902SRodney W. Grimes 			/* If only one argument, not enough buffer space. */
2499b50d902SRodney W. Grimes 			if (bxp == xp)
2509b50d902SRodney W. Grimes 				err("insufficient space for argument");
2519b50d902SRodney W. Grimes 			/* Didn't hit argument limit, so if xflag object. */
2529b50d902SRodney W. Grimes 			if (xflag)
2539b50d902SRodney W. Grimes 				err("insufficient space for arguments");
2549b50d902SRodney W. Grimes 
2559b50d902SRodney W. Grimes 			*xp = NULL;
2569b50d902SRodney W. Grimes 			run(av);
2579b50d902SRodney W. Grimes 			xp = bxp;
2589b50d902SRodney W. Grimes 			cnt = ebp - argp;
2599b50d902SRodney W. Grimes 			bcopy(argp, bbp, cnt);
2609b50d902SRodney W. Grimes 			p = (argp = bbp) + cnt;
2619b50d902SRodney W. Grimes 			*p++ = ch;
2629b50d902SRodney W. Grimes 			break;
2639b50d902SRodney W. Grimes 		}
2649b50d902SRodney W. Grimes 	/* NOTREACHED */
2659b50d902SRodney W. Grimes }
2669b50d902SRodney W. Grimes 
2679b50d902SRodney W. Grimes void
2689b50d902SRodney W. Grimes run(argv)
2699b50d902SRodney W. Grimes 	char **argv;
2709b50d902SRodney W. Grimes {
2719b50d902SRodney W. Grimes 	volatile int noinvoke;
2729b50d902SRodney W. Grimes 	register char **p;
2739b50d902SRodney W. Grimes 	pid_t pid;
2749b50d902SRodney W. Grimes 	int status;
2759b50d902SRodney W. Grimes 
2769b50d902SRodney W. Grimes 	if (tflag) {
2779b50d902SRodney W. Grimes 		(void)fprintf(stderr, "%s", *argv);
2789b50d902SRodney W. Grimes 		for (p = argv + 1; *p; ++p)
2799b50d902SRodney W. Grimes 			(void)fprintf(stderr, " %s", *p);
2809b50d902SRodney W. Grimes 		(void)fprintf(stderr, "\n");
2819b50d902SRodney W. Grimes 		(void)fflush(stderr);
2829b50d902SRodney W. Grimes 	}
2839b50d902SRodney W. Grimes 	noinvoke = 0;
2849b50d902SRodney W. Grimes 	switch(pid = vfork()) {
2859b50d902SRodney W. Grimes 	case -1:
2869b50d902SRodney W. Grimes 		err("vfork: %s", strerror(errno));
2879b50d902SRodney W. Grimes 	case 0:
2889b50d902SRodney W. Grimes 		execvp(argv[0], argv);
2899b50d902SRodney W. Grimes 		(void)fprintf(stderr,
2909b50d902SRodney W. Grimes 		    "xargs: %s: %s\n", argv[0], strerror(errno));
2919b50d902SRodney W. Grimes 		noinvoke = 1;
2929b50d902SRodney W. Grimes 		_exit(1);
2939b50d902SRodney W. Grimes 	}
2949b50d902SRodney W. Grimes 	pid = waitpid(pid, &status, 0);
2959b50d902SRodney W. Grimes 	if (pid == -1)
2969b50d902SRodney W. Grimes 		err("waitpid: %s", strerror(errno));
2979b50d902SRodney W. Grimes 	/* If we couldn't invoke the utility, exit 127. */
2989b50d902SRodney W. Grimes 	if (noinvoke)
2999b50d902SRodney W. Grimes 		exit(127);
3009b50d902SRodney W. Grimes 	/* If utility signaled or exited with a value of 255, exit 1-125. */
3019b50d902SRodney W. Grimes 	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
3029b50d902SRodney W. Grimes 		exit(1);
3039b50d902SRodney W. Grimes 	if (WEXITSTATUS(status))
3049b50d902SRodney W. Grimes 		rval = 1;
3059b50d902SRodney W. Grimes }
3069b50d902SRodney W. Grimes 
3079b50d902SRodney W. Grimes void
3089b50d902SRodney W. Grimes usage()
3099b50d902SRodney W. Grimes {
3109b50d902SRodney W. Grimes 	(void)fprintf(stderr,
311d9198881SWarner Losh "usage: xargs [-0] [-t] [-n number [-x]] [-s size] [utility [argument ...]]\n");
3129b50d902SRodney W. Grimes 	exit(1);
3139b50d902SRodney W. Grimes }
3149b50d902SRodney W. Grimes 
3159b50d902SRodney W. Grimes #if __STDC__
3169b50d902SRodney W. Grimes #include <stdarg.h>
3179b50d902SRodney W. Grimes #else
3189b50d902SRodney W. Grimes #include <varargs.h>
3199b50d902SRodney W. Grimes #endif
3209b50d902SRodney W. Grimes 
3219b50d902SRodney W. Grimes void
3229b50d902SRodney W. Grimes #if __STDC__
3239b50d902SRodney W. Grimes err(const char *fmt, ...)
3249b50d902SRodney W. Grimes #else
3259b50d902SRodney W. Grimes err(fmt, va_alist)
3269b50d902SRodney W. Grimes 	char *fmt;
3279b50d902SRodney W. Grimes         va_dcl
3289b50d902SRodney W. Grimes #endif
3299b50d902SRodney W. Grimes {
3309b50d902SRodney W. Grimes 	va_list ap;
3319b50d902SRodney W. Grimes #if __STDC__
3329b50d902SRodney W. Grimes 	va_start(ap, fmt);
3339b50d902SRodney W. Grimes #else
3349b50d902SRodney W. Grimes 	va_start(ap);
3359b50d902SRodney W. Grimes #endif
3369b50d902SRodney W. Grimes 	(void)fprintf(stderr, "xargs: ");
3379b50d902SRodney W. Grimes 	(void)vfprintf(stderr, fmt, ap);
3389b50d902SRodney W. Grimes 	va_end(ap);
3399b50d902SRodney W. Grimes 	(void)fprintf(stderr, "\n");
3409b50d902SRodney W. Grimes 	exit(1);
3419b50d902SRodney W. Grimes 	/* NOTREACHED */
3429b50d902SRodney W. Grimes }
343