xref: /freebsd/usr.bin/xargs/xargs.c (revision 6e8394b8baa7d5d9153ab90de6824bcd19b3b4e1)
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 
37 #ifndef lint
38 static const char copyright[] =
39 "@(#) Copyright (c) 1990, 1993\n\
40 	The Regents of the University of California.  All rights reserved.\n";
41 #endif /* not lint */
42 
43 #ifndef lint
44 #if 0
45 static char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
46 #endif
47 static const char rcsid[] =
48 	"$Id: xargs.c,v 1.7 1998/10/13 14:52:32 des Exp $";
49 #endif /* not lint */
50 
51 #include <sys/types.h>
52 #include <sys/wait.h>
53 #include <err.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include "pathnames.h"
59 
60 int tflag, rval;
61 int zflag;
62 
63 void run __P((char **));
64 static void usage __P((void));
65 
66 int
67 main(argc, argv, env)
68 	int argc;
69 	char **argv, **env;
70 {
71 	register int ch;
72 	register char *p, *bbp, *ebp, **bxp, **exp, **xp;
73 	int cnt, indouble, insingle, nargs, nflag, nline, xflag, wasquoted;
74 	char **av, *argp, **ep = env;
75 	long arg_max;
76 
77 	/*
78 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
79 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
80 	 * that the smallest argument is 2 bytes in length, this means that
81 	 * the number of arguments is limited to:
82 	 *
83 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
84 	 *
85 	 * We arbitrarily limit the number of arguments to 5000.  This is
86 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
87 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
88 	 * probably not worthwhile.
89 	 */
90 	nargs = 5000;
91 	if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
92 		errx(1, "sysconf(_SC_ARG_MAX) failed");
93 	nline = arg_max - 4 * 1024;
94 	while (*ep) {
95 		/* 1 byte for each '\0' */
96 		nline -= strlen(*ep++) + 1 + sizeof(*ep);
97 	}
98 	nflag = xflag = wasquoted = 0;
99 	while ((ch = getopt(argc, argv, "0n:s:tx")) != -1)
100 		switch(ch) {
101 		case 'n':
102 			nflag = 1;
103 			if ((nargs = atoi(optarg)) <= 0)
104 				errx(1, "illegal argument count");
105 			break;
106 		case 's':
107 			nline = atoi(optarg);
108 			break;
109 		case 't':
110 			tflag = 1;
111 			break;
112 		case 'x':
113 			xflag = 1;
114 			break;
115 		case '0':
116 			zflag = 1;
117 			break;
118 		case '?':
119 		default:
120 			usage();
121 	}
122 	argc -= optind;
123 	argv += optind;
124 
125 	if (xflag && !nflag)
126 		usage();
127 
128 	/*
129 	 * Allocate pointers for the utility name, the utility arguments,
130 	 * the maximum arguments to be read from stdin and the trailing
131 	 * NULL.
132 	 */
133 	if (!(av = bxp =
134 	    malloc((u_int)(1 + argc + nargs + 1) * sizeof(char **))))
135 		errx(1, "malloc");
136 
137 	/*
138 	 * Use the user's name for the utility as argv[0], just like the
139 	 * shell.  Echo is the default.  Set up pointers for the user's
140 	 * arguments.
141 	 */
142 	if (!*argv)
143 		cnt = strlen(*bxp++ = _PATH_ECHO);
144 	else {
145 		cnt = 0;
146 		do {
147 			cnt += strlen(*bxp++ = *argv) + 1;
148 		} while (*++argv);
149 	}
150 
151 	/*
152 	 * Set up begin/end/traversing pointers into the array.  The -n
153 	 * count doesn't include the trailing NULL pointer, so the malloc
154 	 * added in an extra slot.
155 	 */
156 	exp = (xp = bxp) + nargs;
157 
158 	/*
159 	 * Allocate buffer space for the arguments read from stdin and the
160 	 * trailing NULL.  Buffer space is defined as the default or specified
161 	 * space, minus the length of the utility name and arguments.  Set up
162 	 * begin/end/traversing pointers into the array.  The -s count does
163 	 * include the trailing NULL, so the malloc didn't add in an extra
164 	 * slot.
165 	 */
166 	nline -= cnt;
167 	if (nline <= 0)
168 		errx(1, "insufficient space for command");
169 
170 	if (!(bbp = malloc((u_int)nline + 1)))
171 		errx(1, "malloc");
172 	ebp = (argp = p = bbp) + nline - 1;
173 
174 	for (insingle = indouble = 0;;)
175 		switch(ch = getchar()) {
176 		case EOF:
177 			/* No arguments since last exec. */
178 			if (p == bbp)
179 				exit(rval);
180 			goto arg1;
181 		case ' ':
182 		case '\t':
183 			/* Quotes escape tabs and spaces. */
184 			if (insingle || indouble || zflag)
185 				goto addch;
186 			goto arg2;
187 		case '\0':
188 			if (zflag)
189 				goto arg2;
190 			goto addch;
191 		case '\n':
192 			if (zflag)
193 				goto addch;
194 
195 			/* Quotes do not escape newlines. */
196 arg1:			if (insingle || indouble)
197 				 errx(1, "unterminated quote");
198 
199 arg2:
200 			/* Do not make empty args unless they are quoted */
201 			if (argp != p || wasquoted) {
202 				*p++ = '\0';
203 				*xp++ = argp;
204 			}
205 
206 			/*
207 			 * If max'd out on args or buffer, or reached EOF,
208 			 * run the command.  If xflag and max'd out on buffer
209 			 * but not on args, object.
210 			 */
211 			if (xp == exp || p > ebp || ch == EOF) {
212 				if (xflag && xp != exp && p > ebp)
213 					errx(1, "insufficient space for arguments");
214 				*xp = NULL;
215 				run(av);
216 				if (ch == EOF)
217 					exit(rval);
218 				p = bbp;
219 				xp = bxp;
220 			}
221 			argp = p;
222 			wasquoted = 0;
223 			break;
224 		case '\'':
225 			if (indouble || zflag)
226 				goto addch;
227 			insingle = !insingle;
228 			wasquoted = 1;
229 			break;
230 		case '"':
231 			if (insingle || zflag)
232 				goto addch;
233 			indouble = !indouble;
234 			wasquoted = 1;
235 			break;
236 		case '\\':
237 			if (zflag)
238 				goto addch;
239 			/* Backslash escapes anything, is escaped by quotes. */
240 			if (!insingle && !indouble && (ch = getchar()) == EOF)
241 				errx(1, "backslash at EOF");
242 			/* FALLTHROUGH */
243 		default:
244 addch:			if (p < ebp) {
245 				*p++ = ch;
246 				break;
247 			}
248 
249 			/* If only one argument, not enough buffer space. */
250 			if (bxp == xp)
251 				errx(1, "insufficient space for argument");
252 			/* Didn't hit argument limit, so if xflag object. */
253 			if (xflag)
254 				errx(1, "insufficient space for arguments");
255 
256 			*xp = NULL;
257 			run(av);
258 			xp = bxp;
259 			cnt = ebp - argp;
260 			bcopy(argp, bbp, cnt);
261 			p = (argp = bbp) + cnt;
262 			*p++ = ch;
263 			break;
264 		}
265 	/* NOTREACHED */
266 }
267 
268 void
269 run(argv)
270 	char **argv;
271 {
272 	volatile int noinvoke;
273 	register char **p;
274 	pid_t pid;
275 	int status;
276 
277 	if (tflag) {
278 		(void)fprintf(stderr, "%s", *argv);
279 		for (p = argv + 1; *p; ++p)
280 			(void)fprintf(stderr, " %s", *p);
281 		(void)fprintf(stderr, "\n");
282 		(void)fflush(stderr);
283 	}
284 	noinvoke = 0;
285 	switch(pid = fork()) {
286 	case -1:
287 		err(1, "fork");
288 	case 0:
289 		execvp(argv[0], argv);
290 		warn("%s", argv[0]);
291 		noinvoke = 1;
292 		_exit(1);
293 	}
294 	pid = waitpid(pid, &status, 0);
295 	if (pid == -1)
296 		err(1, "waitpid");
297 	/* If we couldn't invoke the utility, exit 127. */
298 	if (noinvoke)
299 		exit(127);
300 	/* If utility signaled or exited with a value of 255, exit 1-125. */
301 	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
302 		exit(1);
303 	if (WEXITSTATUS(status))
304 		rval = 1;
305 }
306 
307 static void
308 usage()
309 {
310 	(void)fprintf(stderr,
311 "usage: xargs [-0] [-t] [-n number [-x]] [-s size] [utility [argument ...]]\n");
312 	exit(1);
313 }
314