xref: /freebsd/usr.bin/xargs/xargs.c (revision 380a989b3223d455375b4fae70fd0b9bdd43bafb)
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.6 1998/06/17 12:58:43 jkoshy 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;
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 = 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 
181 			/* Nothing since end of last argument. */
182 			if (argp == p) {
183 				*xp = NULL;
184 				run(av);
185 				exit(rval);
186 			}
187 			goto arg1;
188 		case ' ':
189 		case '\t':
190 			/* Quotes escape tabs and spaces. */
191 			if (insingle || indouble || zflag)
192 				goto addch;
193 			goto arg2;
194 		case '\0':
195 			if (zflag)
196 				goto arg2;
197 			goto addch;
198 		case '\n':
199 			if (zflag)
200 				goto addch;
201 
202 			/* Empty lines are skipped. */
203 			if (argp == p)
204 				continue;
205 
206 			/* Quotes do not escape newlines. */
207 arg1:			if (insingle || indouble)
208 				 errx(1, "unterminated quote");
209 
210 arg2:			*p = '\0';
211 			*xp++ = argp;
212 
213 			/*
214 			 * If max'd out on args or buffer, or reached EOF,
215 			 * run the command.  If xflag and max'd out on buffer
216 			 * but not on args, object.
217 			 */
218 			if (xp == exp || p == ebp || ch == EOF) {
219 				if (xflag && xp != exp && p == ebp)
220 					errx(1, "insufficient space for arguments");
221 				*xp = NULL;
222 				run(av);
223 				if (ch == EOF)
224 					exit(rval);
225 				p = bbp;
226 				xp = bxp;
227 			} else
228 				++p;
229 			argp = p;
230 			break;
231 		case '\'':
232 			if (indouble || zflag)
233 				goto addch;
234 			insingle = !insingle;
235 			break;
236 		case '"':
237 			if (insingle || zflag)
238 				goto addch;
239 			indouble = !indouble;
240 			break;
241 		case '\\':
242 			if (zflag)
243 				goto addch;
244 			/* Backslash escapes anything, is escaped by quotes. */
245 			if (!insingle && !indouble && (ch = getchar()) == EOF)
246 				errx(1, "backslash at EOF");
247 			/* FALLTHROUGH */
248 		default:
249 addch:			if (p < ebp) {
250 				*p++ = ch;
251 				break;
252 			}
253 
254 			/* If only one argument, not enough buffer space. */
255 			if (bxp == xp)
256 				errx(1, "insufficient space for argument");
257 			/* Didn't hit argument limit, so if xflag object. */
258 			if (xflag)
259 				errx(1, "insufficient space for arguments");
260 
261 			*xp = NULL;
262 			run(av);
263 			xp = bxp;
264 			cnt = ebp - argp;
265 			bcopy(argp, bbp, cnt);
266 			p = (argp = bbp) + cnt;
267 			*p++ = ch;
268 			break;
269 		}
270 	/* NOTREACHED */
271 }
272 
273 void
274 run(argv)
275 	char **argv;
276 {
277 	volatile int noinvoke;
278 	register char **p;
279 	pid_t pid;
280 	int status;
281 
282 	if (tflag) {
283 		(void)fprintf(stderr, "%s", *argv);
284 		for (p = argv + 1; *p; ++p)
285 			(void)fprintf(stderr, " %s", *p);
286 		(void)fprintf(stderr, "\n");
287 		(void)fflush(stderr);
288 	}
289 	noinvoke = 0;
290 	switch(pid = fork()) {
291 	case -1:
292 		err(1, "fork");
293 	case 0:
294 		execvp(argv[0], argv);
295 		warn("%s", argv[0]);
296 		noinvoke = 1;
297 		_exit(1);
298 	}
299 	pid = waitpid(pid, &status, 0);
300 	if (pid == -1)
301 		err(1, "waitpid");
302 	/* If we couldn't invoke the utility, exit 127. */
303 	if (noinvoke)
304 		exit(127);
305 	/* If utility signaled or exited with a value of 255, exit 1-125. */
306 	if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
307 		exit(1);
308 	if (WEXITSTATUS(status))
309 		rval = 1;
310 }
311 
312 static void
313 usage()
314 {
315 	(void)fprintf(stderr,
316 "usage: xargs [-0] [-t] [-n number [-x]] [-s size] [utility [argument ...]]\n");
317 	exit(1);
318 }
319