]> git.saurik.com Git - apple/shell_cmds.git/blame - xargs/xargs.c
shell_cmds-81.tar.gz
[apple/shell_cmds.git] / xargs / xargs.c
CommitLineData
44bd5ea7
A
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.
9bafe280
A
35 *
36 * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $
44bd5ea7
A
37 */
38
44bd5ea7 39#ifndef lint
9bafe280
A
40static const char copyright[] =
41"@(#) Copyright (c) 1990, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
44bd5ea7
A
43#endif /* not lint */
44
44bd5ea7 45#if 0
9bafe280 46#ifndef lint
44bd5ea7 47static char sccsid[] = "@(#)xargs.c 8.1 (Berkeley) 6/6/93";
44bd5ea7 48#endif /* not lint */
9bafe280
A
49#endif
50
51#include <sys/cdefs.h>
52__RCSID("$FreeBSD: src/usr.bin/xargs/xargs.c,v 1.41 2002/07/01 03:21:05 tjr Exp $");
44bd5ea7
A
53
54#include <sys/types.h>
55#include <sys/wait.h>
9bafe280
A
56
57#include <err.h>
44bd5ea7 58#include <errno.h>
9bafe280
A
59#include <locale.h>
60#include <paths.h>
61#include <regex.h>
44bd5ea7
A
62#include <stdio.h>
63#include <stdlib.h>
64#include <string.h>
65#include <unistd.h>
9bafe280 66
44bd5ea7
A
67#include "pathnames.h"
68
1a5bac72
A
69#ifdef __APPLE__
70#include <get_compat.h>
71#else
72#define COMPAT_MODE(a,b) (1)
73#endif /* __APPLE__ */
74
9bafe280
A
75static void parse_input(int, char *[]);
76static void prerun(int, char *[]);
77static int prompt(void);
78static void run(char **);
79static void usage(void);
80void strnsubst(char **, const char *, const char *, size_t);
81
82static char echo[] = _PATH_ECHO;
83static char **av, **bxp, **ep, **exp, **xp;
84static char *argp, *bbp, *ebp, *inpline, *p, *replstr;
85static const char *eofstr;
86static int count, insingle, indouble, pflag, tflag, Rflag, rval, zflag;
87static int cnt, Iflag, jfound, Lflag, wasquoted, xflag;
1a5bac72
A
88static int last_was_newline = 1;
89static int last_was_blank = 0;
44bd5ea7 90
9bafe280 91extern char **environ;
44bd5ea7
A
92
93int
9bafe280 94main(int argc, char *argv[])
44bd5ea7 95{
9bafe280
A
96 long arg_max;
97 int ch, Jflag, nargs, nflag, nline;
98 size_t linelen;
44bd5ea7 99
9bafe280
A
100 inpline = replstr = NULL;
101 ep = environ;
102 eofstr = "";
103 Jflag = nflag = 0;
44bd5ea7
A
104
105 /*
106 * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that
107 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given
108 * that the smallest argument is 2 bytes in length, this means that
109 * the number of arguments is limited to:
110 *
111 * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
112 *
113 * We arbitrarily limit the number of arguments to 5000. This is
114 * allowed by POSIX.2 as long as the resulting minimum exec line is
115 * at least LINE_MAX. Realloc'ing as necessary is possible, but
116 * probably not worthwhile.
117 */
118 nargs = 5000;
9bafe280
A
119 if ((arg_max = sysconf(_SC_ARG_MAX)) == -1)
120 errx(1, "sysconf(_SC_ARG_MAX) failed");
121 nline = arg_max - 4 * 1024;
122 while (*ep != NULL) {
123 /* 1 byte for each '\0' */
124 nline -= strlen(*ep++) + 1 + sizeof(*ep);
125 }
126 while ((ch = getopt(argc, argv, "0E:I:J:L:n:pR:s:tx")) != -1)
44bd5ea7 127 switch(ch) {
9bafe280
A
128 case 'E':
129 eofstr = optarg;
130 break;
131 case 'I':
132 Jflag = 0;
133 Iflag = 1;
134 Lflag = 1;
135 replstr = optarg;
136 break;
137 case 'J':
138 Iflag = 0;
139 Jflag = 1;
140 replstr = optarg;
141 break;
142 case 'L':
143 Lflag = atoi(optarg);
1a5bac72
A
144 if (COMPAT_MODE("bin/xargs", "Unix2003")) {
145 nflag = 0; /* Override */
146 nargs = 5000;
147 }
44bd5ea7
A
148 break;
149 case 'n':
150 nflag = 1;
1a5bac72
A
151 if (COMPAT_MODE("bin/xargs", "Unix2003")) {
152 Lflag = 0; /* Override */
153 }
44bd5ea7
A
154 if ((nargs = atoi(optarg)) <= 0)
155 errx(1, "illegal argument count");
156 break;
9bafe280
A
157 case 'p':
158 pflag = 1;
159 break;
160 case 'R':
161 if ((Rflag = atoi(optarg)) <= 0)
162 errx(1, "illegal number of replacements");
163 break;
44bd5ea7
A
164 case 's':
165 nline = atoi(optarg);
166 break;
167 case 't':
168 tflag = 1;
169 break;
170 case 'x':
171 xflag = 1;
172 break;
9bafe280
A
173 case '0':
174 zflag = 1;
175 break;
44bd5ea7
A
176 case '?':
177 default:
178 usage();
179 }
180 argc -= optind;
181 argv += optind;
182
9bafe280
A
183 if (!Iflag && Rflag)
184 usage();
185 if (Iflag && !Rflag)
186 Rflag = 5;
44bd5ea7
A
187 if (xflag && !nflag)
188 usage();
9bafe280
A
189 if (Iflag || Lflag)
190 xflag = 1;
191 if (replstr != NULL && *replstr == '\0')
192 errx(1, "replstr may not be empty");
44bd5ea7
A
193
194 /*
195 * Allocate pointers for the utility name, the utility arguments,
196 * the maximum arguments to be read from stdin and the trailing
197 * NULL.
198 */
9bafe280
A
199 linelen = 1 + argc + nargs + 1;
200 if ((av = bxp = malloc(linelen * sizeof(char **))) == NULL)
201 errx(1, "malloc failed");
44bd5ea7
A
202
203 /*
204 * Use the user's name for the utility as argv[0], just like the
205 * shell. Echo is the default. Set up pointers for the user's
206 * arguments.
207 */
9bafe280
A
208 if (*argv == NULL)
209 cnt = strlen(*bxp++ = echo);
44bd5ea7 210 else {
44bd5ea7 211 do {
9bafe280
A
212 if (Jflag && strcmp(*argv, replstr) == 0) {
213 char **avj;
214 jfound = 1;
215 argv++;
216 for (avj = argv; *avj; avj++)
217 cnt += strlen(*avj) + 1;
218 break;
219 }
44bd5ea7 220 cnt += strlen(*bxp++ = *argv) + 1;
9bafe280 221 } while (*++argv != NULL);
44bd5ea7
A
222 }
223
224 /*
225 * Set up begin/end/traversing pointers into the array. The -n
226 * count doesn't include the trailing NULL pointer, so the malloc
227 * added in an extra slot.
228 */
229 exp = (xp = bxp) + nargs;
230
231 /*
232 * Allocate buffer space for the arguments read from stdin and the
233 * trailing NULL. Buffer space is defined as the default or specified
234 * space, minus the length of the utility name and arguments. Set up
235 * begin/end/traversing pointers into the array. The -s count does
236 * include the trailing NULL, so the malloc didn't add in an extra
237 * slot.
238 */
239 nline -= cnt;
240 if (nline <= 0)
241 errx(1, "insufficient space for command");
242
9bafe280
A
243 if ((bbp = malloc((size_t)(nline + 1))) == NULL)
244 errx(1, "malloc failed");
44bd5ea7 245 ebp = (argp = p = bbp) + nline - 1;
9bafe280
A
246 for (;;)
247 parse_input(argc, argv);
248}
44bd5ea7 249
9bafe280
A
250static void
251parse_input(int argc, char *argv[])
252{
253 int ch, foundeof;
254 char **avj;
1a5bac72 255 int last_was_backslashed = 0;
44bd5ea7 256
9bafe280
A
257 foundeof = 0;
258
259 switch(ch = getchar()) {
260 case EOF:
261 /* No arguments since last exec. */
262 if (p == bbp)
263 exit(rval);
264 goto arg1;
265 case ' ':
1a5bac72 266 last_was_blank = 1;
9bafe280
A
267 case '\t':
268 /* Quotes escape tabs and spaces. */
269 if (insingle || indouble || zflag)
270 goto addch;
271 goto arg2;
272 case '\0':
273 if (zflag)
44bd5ea7 274 goto arg2;
9bafe280
A
275 goto addch;
276 case '\n':
1a5bac72
A
277 if (COMPAT_MODE("bin/xargs", "Unix2003")) {
278 if (last_was_newline) {
279 /* don't count empty line */
280 break;
281 }
282 if (!last_was_blank ) {
283 /* only count if NOT continuation line */
284 count++;
285 }
286 } else {
287 count++;
288 }
289 last_was_newline = 1;
9bafe280 290 if (zflag)
44bd5ea7 291 goto addch;
9bafe280
A
292
293 /* Quotes do not escape newlines. */
294arg1: if (insingle || indouble)
295 errx(1, "unterminated quote");
296arg2:
297 foundeof = *eofstr != '\0' &&
298 strcmp(argp, eofstr) == 0;
299
300 /* Do not make empty args unless they are quoted */
301 if ((argp != p || wasquoted) && !foundeof) {
302 *p++ = '\0';
44bd5ea7 303 *xp++ = argp;
9bafe280
A
304 if (Iflag) {
305 size_t curlen;
44bd5ea7 306
9bafe280
A
307 if (inpline == NULL)
308 curlen = 0;
309 else {
310 /*
311 * If this string is not zero
312 * length, append a space for
313 * seperation before the next
314 * argument.
315 */
316 if ((curlen = strlen(inpline)))
317 strcat(inpline, " ");
318 }
319 curlen++;
320 /*
321 * Allocate enough to hold what we will
322 * be holding in a second, and to append
323 * a space next time through, if we have
324 * to.
325 */
326 inpline = realloc(inpline, curlen + 2 +
327 strlen(argp));
328 if (inpline == NULL)
329 errx(1, "realloc failed");
330 if (curlen == 1)
331 strcpy(inpline, argp);
332 else
333 strcat(inpline, argp);
44bd5ea7 334 }
9bafe280 335 }
44bd5ea7 336
9bafe280
A
337 /*
338 * If max'd out on args or buffer, or reached EOF,
339 * run the command. If xflag and max'd out on buffer
340 * but not on args, object. Having reached the limit
341 * of input lines, as specified by -L is the same as
342 * maxing out on arguments.
343 */
344 if (xp == exp || p > ebp || ch == EOF ||
345 (Lflag <= count && xflag) || foundeof) {
346 if (xflag && xp != exp && p > ebp)
44bd5ea7 347 errx(1, "insufficient space for arguments");
9bafe280
A
348 if (jfound) {
349 for (avj = argv; *avj; avj++)
350 *xp++ = *avj;
351 }
352 prerun(argc, av);
353 if (ch == EOF || foundeof)
354 exit(rval);
355 p = bbp;
44bd5ea7 356 xp = bxp;
9bafe280
A
357 count = 0;
358 }
359 argp = p;
360 wasquoted = 0;
361 break;
362 case '\'':
363 if (indouble || zflag)
364 goto addch;
365 insingle = !insingle;
366 wasquoted = 1;
367 break;
368 case '"':
369 if (insingle || zflag)
370 goto addch;
371 indouble = !indouble;
372 wasquoted = 1;
373 break;
374 case '\\':
1a5bac72 375 last_was_backslashed = 1;
9bafe280
A
376 if (zflag)
377 goto addch;
378 /* Backslash escapes anything, is escaped by quotes. */
379 if (!insingle && !indouble && (ch = getchar()) == EOF)
380 errx(1, "backslash at EOF");
381 /* FALLTHROUGH */
382 default:
383addch: if (p < ebp) {
44bd5ea7
A
384 *p++ = ch;
385 break;
386 }
9bafe280
A
387
388 /* If only one argument, not enough buffer space. */
389 if (bxp == xp)
390 errx(1, "insufficient space for argument");
391 /* Didn't hit argument limit, so if xflag object. */
392 if (xflag)
393 errx(1, "insufficient space for arguments");
394
395 if (jfound) {
396 for (avj = argv; *avj; avj++)
397 *xp++ = *avj;
398 }
399 prerun(argc, av);
400 xp = bxp;
401 cnt = ebp - argp;
402 memcpy(bbp, argp, (size_t)cnt);
403 p = (argp = bbp) + cnt;
404 *p++ = ch;
405 break;
406 }
1a5bac72
A
407 if (ch != ' ')
408 last_was_blank = 0;
409 if (ch != '\n' || last_was_backslashed)
410 last_was_newline = 0;
9bafe280 411 return;
44bd5ea7
A
412}
413
9bafe280
A
414/*
415 * Do things necessary before run()'ing, such as -I substitution,
416 * and then call run().
417 */
418static void
419prerun(int argc, char *argv[])
44bd5ea7 420{
9bafe280
A
421 char **tmp, **tmp2, **avj;
422 int repls;
423
424 repls = Rflag;
425
426 if (argc == 0 || repls == 0) {
427 *xp = NULL;
428 run(argv);
429 return;
430 }
431
432 avj = argv;
433
434 /*
435 * Allocate memory to hold the argument list, and
436 * a NULL at the tail.
437 */
438 tmp = malloc((argc + 1) * sizeof(char**));
439 if (tmp == NULL)
440 errx(1, "malloc failed");
441 tmp2 = tmp;
442
443 /*
444 * Save the first argument and iterate over it, we
445 * cannot do strnsubst() to it.
446 */
447 if ((*tmp++ = strdup(*avj++)) == NULL)
448 errx(1, "strdup failed");
449
450 /*
451 * For each argument to utility, if we have not used up
452 * the number of replacements we are allowed to do, and
453 * if the argument contains at least one occurance of
454 * replstr, call strnsubst(), else just save the string.
455 * Iterations over elements of avj and tmp are done
456 * where appropriate.
457 */
458 while (--argc) {
459 *tmp = *avj++;
460 if (repls && strstr(*tmp, replstr) != NULL) {
461 strnsubst(tmp++, replstr, inpline, (size_t)255);
462 repls--;
463 } else {
464 if ((*tmp = strdup(*tmp)) == NULL)
465 errx(1, "strdup failed");
466 tmp++;
467 }
468 }
469
470 /*
471 * Run it.
472 */
473 *tmp = NULL;
474 run(tmp2);
475
476 /*
477 * Walk from the tail to the head, free along the way.
478 */
479 for (; tmp2 != tmp; tmp--)
480 free(*tmp);
481 /*
482 * Now free the list itself.
483 */
484 free(tmp2);
485
486 /*
487 * Free the input line buffer, if we have one.
488 */
489 if (inpline != NULL) {
490 free(inpline);
491 inpline = NULL;
492 }
493}
494
495static void
496run(char **argv)
497{
498 volatile int childerr;
499 char **avec;
44bd5ea7
A
500 pid_t pid;
501 int status;
502
9bafe280
A
503 /*
504 * If the user wants to be notified of each command before it is
505 * executed, notify them. If they want the notification to be
506 * followed by a prompt, then prompt them.
507 */
508 if (tflag || pflag) {
44bd5ea7 509 (void)fprintf(stderr, "%s", *argv);
9bafe280
A
510 for (avec = argv + 1; *avec != NULL; ++avec)
511 (void)fprintf(stderr, " %s", *avec);
512 /*
513 * If the user has asked to be prompted, do so.
514 */
515 if (pflag)
516 /*
517 * If they asked not to exec, return without execution
518 * but if they asked to, go to the execution. If we
519 * could not open their tty, break the switch and drop
520 * back to -t behaviour.
521 */
522 switch (prompt()) {
523 case 0:
524 return;
525 case 1:
526 goto exec;
527 case 2:
528 break;
529 }
44bd5ea7
A
530 (void)fprintf(stderr, "\n");
531 (void)fflush(stderr);
532 }
9bafe280
A
533exec:
534 childerr = 0;
44bd5ea7
A
535 switch(pid = vfork()) {
536 case -1:
537 err(1, "vfork");
538 case 0:
539 execvp(argv[0], argv);
9bafe280 540 childerr = errno;
44bd5ea7
A
541 _exit(1);
542 }
543 pid = waitpid(pid, &status, 0);
544 if (pid == -1)
545 err(1, "waitpid");
9bafe280
A
546 /* If we couldn't invoke the utility, exit. */
547 if (childerr != 0)
548 err(childerr == ENOENT ? 127 : 126, "%s", *argv);
549 /* If utility signaled or exited with a value of 255, exit 1-125. */
550 if (WIFSIGNALED(status) || WEXITSTATUS(status) == 255)
551 exit(1);
552 if (WEXITSTATUS(status))
553 rval = 1;
554}
44bd5ea7 555
9bafe280
A
556/*
557 * Prompt the user about running a command.
558 */
559static int
560prompt(void)
561{
562 regex_t cre;
563 size_t rsize;
564 int match;
565 char *response;
566 FILE *ttyfp;
44bd5ea7 567
9bafe280
A
568 if ((ttyfp = fopen(_PATH_TTY, "r")) == NULL)
569 return (2); /* Indicate that the TTY failed to open. */
570 (void)fprintf(stderr, "?...");
571 (void)fflush(stderr);
572 if ((response = fgetln(ttyfp, &rsize)) == NULL ||
573 regcomp(&cre,
574 "^[yY]",
575 REG_BASIC) != 0) {
576 (void)fclose(ttyfp);
577 return (0);
44bd5ea7 578 }
9bafe280
A
579 match = regexec(&cre, response, 0, NULL, 0);
580 (void)fclose(ttyfp);
581 regfree(&cre);
582 return (match == 0);
44bd5ea7
A
583}
584
9bafe280
A
585static void
586usage(void)
44bd5ea7 587{
9bafe280
A
588 fprintf(stderr,
589"usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
590" [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n");
44bd5ea7
A
591 exit(1);
592}