xref: /netbsd-src/usr.bin/xargs/xargs.c (revision fdecd6a253f999ae92b139670d9e15cc9df4497c)
1 /*	$NetBSD: xargs.c,v 1.8 1997/06/24 00:45:29 lukem Exp $	*/
2 
3 /*-
4  * Copyright (c) 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * John B. Roll Jr.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the University of
21  *	California, Berkeley and its contributors.
22  * 4. Neither the name of the University nor the names of its contributors
23  *    may be used to endorse or promote products derived from this software
24  *    without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36  * SUCH DAMAGE.
37  */
38 
39 #ifndef lint
40 static char copyright[] =
41 "@(#) Copyright (c) 1990, 1993\n\
42 	The Regents of the University of California.  All rights reserved.\n";
43 #endif /* not lint */
44 
45 #ifndef lint
46 #if 0
47 static char sccsid[] = "@(#)xargs.c	8.1 (Berkeley) 6/6/93";
48 #endif
49 static char rcsid[] = "$NetBSD: xargs.c,v 1.8 1997/06/24 00:45:29 lukem Exp $";
50 #endif /* not lint */
51 
52 #include <sys/types.h>
53 #include <sys/wait.h>
54 #include <errno.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <string.h>
58 #include <unistd.h>
59 #include <limits.h>
60 #include <locale.h>
61 #include <err.h>
62 #include "pathnames.h"
63 
64 int tflag, zflag, rval;
65 
66 void run __P((char **));
67 void usage __P((void));
68 
69 int
70 main(argc, argv)
71 	int argc;
72 	char **argv;
73 {
74 	register int ch;
75 	register char *p, *bbp, *ebp, **bxp, **exp, **xp;
76 	int cnt, indouble, insingle, nargs, nflag, nline, xflag;
77 	char **av, *argp;
78 
79 	setlocale(LC_ALL, "");
80 
81 	/*
82 	 * POSIX.2 limits the exec line length to ARG_MAX - 2K.  Running that
83 	 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K.  Given
84 	 * that the smallest argument is 2 bytes in length, this means that
85 	 * the number of arguments is limited to:
86 	 *
87 	 *	 (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
88 	 *
89 	 * We arbitrarily limit the number of arguments to 5000.  This is
90 	 * allowed by POSIX.2 as long as the resulting minimum exec line is
91 	 * at least LINE_MAX.  Realloc'ing as necessary is possible, but
92 	 * probably not worthwhile.
93 	 */
94 	nargs = 5000;
95 	nline = ARG_MAX - 4 * 1024;
96 	nflag = xflag = 0;
97 	while ((ch = getopt(argc, argv, "0n:s:tx")) != -1)
98 		switch(ch) {
99 		case '0':
100 			zflag = 1;
101 			break;
102 		case 'n':
103 			nflag = 1;
104 			if ((nargs = atoi(optarg)) <= 0)
105 				errx(1, "illegal argument count");
106 			break;
107 		case 's':
108 			nline = atoi(optarg);
109 			break;
110 		case 't':
111 			tflag = 1;
112 			break;
113 		case 'x':
114 			xflag = 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 		err(1, NULL);
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 		err(1, NULL);
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 			/* Empty lines are skipped. */
200 			if (argp == p)
201 				continue;
202 
203 			/* Quotes do not escape newlines. */
204 arg1:			if (insingle || indouble)
205 				 errx(1, "unterminated quote");
206 
207 arg2:			*p = '\0';
208 			*xp++ = argp;
209 
210 			/*
211 			 * If max'd out on args or buffer, or reached EOF,
212 			 * run the command.  If xflag and max'd out on buffer
213 			 * but not on args, object.
214 			 */
215 			if (xp == exp || p == ebp || ch == EOF) {
216 				if (xflag && xp != exp && p == ebp)
217 					errx(1, "insufficient space for arguments");
218 				*xp = NULL;
219 				run(av);
220 				if (ch == EOF)
221 					exit(rval);
222 				p = bbp;
223 				xp = bxp;
224 			} else
225 				++p;
226 			argp = p;
227 			break;
228 		case '\'':
229 			if (indouble || zflag)
230 				goto addch;
231 			insingle = !insingle;
232 			break;
233 		case '"':
234 			if (insingle || zflag)
235 				goto addch;
236 			indouble = !indouble;
237 			break;
238 		case '\\':
239 			if (zflag)
240 				goto addch;
241 			/* Backslash escapes anything, is escaped by quotes. */
242 			if (!insingle && !indouble && (ch = getchar()) == EOF)
243 				errx(1, "backslash at EOF");
244 			/* FALLTHROUGH */
245 		default:
246 addch:			if (p < ebp) {
247 				*p++ = ch;
248 				break;
249 			}
250 
251 			/* If only one argument, not enough buffer space. */
252 			if (bxp == xp)
253 				errx(1, "insufficient space for argument");
254 			/* Didn't hit argument limit, so if xflag object. */
255 			if (xflag)
256 				errx(1, "insufficient space for arguments");
257 
258 			*xp = NULL;
259 			run(av);
260 			xp = bxp;
261 			cnt = ebp - argp;
262 			bcopy(argp, bbp, cnt);
263 			p = (argp = bbp) + cnt;
264 			*p++ = ch;
265 			break;
266 		}
267 	/* NOTREACHED */
268 }
269 
270 void
271 run(argv)
272 	char **argv;
273 {
274 	volatile int noinvoke;
275 	register char **p;
276 	pid_t pid;
277 	int status;
278 
279 	if (tflag) {
280 		(void)fprintf(stderr, "%s", *argv);
281 		for (p = argv + 1; *p; ++p)
282 			(void)fprintf(stderr, " %s", *p);
283 		(void)fprintf(stderr, "\n");
284 		(void)fflush(stderr);
285 	}
286 	noinvoke = 0;
287 	switch(pid = vfork()) {
288 	case -1:
289 		err(1, "vfork");
290 	case 0:
291 		execvp(argv[0], argv);
292 		noinvoke = (errno == ENOENT) ? 127 : 126;
293 		warn("%s", argv[0]);;
294 		_exit(1);
295 	}
296 	pid = waitpid(pid, &status, 0);
297 	if (pid == -1)
298 		err(1, "waitpid");
299 
300 	/*
301 	 * If we couldn't invoke the utility or the utility didn't exit
302 	 * properly, quit with 127 or 126 respectively.
303 	 */
304 	if (noinvoke)
305 		exit(noinvoke);
306 
307 	/*
308 	 * According to POSIX, we have to exit if the utility exits with
309 	 * a 255 status, or is interrupted by a signal.   xargs is allowed
310 	 * to return any exit status between 1 and 125 in these cases, but
311 	 * we'll use 124 and 125, the same values used by GNU xargs.
312 	 */
313 	if (WIFEXITED(status)) {
314 		if (WEXITSTATUS (status) == 255) {
315 			warnx ("%s exited with status 255", argv[0]);
316 			exit(124);
317 		} else if (WEXITSTATUS (status) != 0) {
318 			rval = 123;
319 		}
320 	} else if (WIFSIGNALED (status)) {
321 		warnx ("%s terminated by signal %d", argv[0], WTERMSIG(status));
322 		exit(125);
323 	}
324 }
325 
326 void
327 usage()
328 {
329 	(void)fprintf(stderr,
330 "usage: xargs [-0t] [-n number [-x]] [-s size] [utility [argument ...]]\n");
331 	exit(1);
332 }
333