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