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