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