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