2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
36 * $xMach: xargs.c,v 1.6 2002/02/23 05:27:47 tim Exp $
38 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
39 * @(#)xargs.c 8.1 (Berkeley) 6/6/93
40 * $FreeBSD: src/usr.bin/xargs/xargs.c,v 1.9.2.6 2003/06/01 21:40:35 mux Exp $
41 * $DragonFly: src/usr.bin/xargs/xargs.c,v 1.3 2004/07/27 21:42:48 dillon Exp $
44 #include <sys/types.h>
61 #include "pathnames.h"
63 static void parse_input(int, char *[]);
64 static void prerun(int, char *[]);
65 static int prompt(void);
66 static void run(char **);
67 static void usage(void);
68 void strnsubst(char **, const char *, const char *, size_t);
70 static char echo
[] = _PATH_ECHO
;
71 static char **av
, **bxp
, **ep
, **expx
, **xp
;
72 static char *argp
, *bbp
, *ebp
, *inpline
, *p
, *replstr
;
73 static const char *eofstr
;
74 static int count
, insingle
, indouble
, oflag
, pflag
, tflag
, Rflag
, rval
, zflag
;
75 static int cnt
, Iflag
, jfound
, Lflag
, wasquoted
, xflag
;
77 extern char **environ
;
80 main(int argc
, char *argv
[])
84 int ch
, Jflag
, nargs
, nflag
, nline
;
87 inpline
= replstr
= NULL
;
92 (void)setlocale(LC_MESSAGES
, "");
95 * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that
96 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given
97 * that the smallest argument is 2 bytes in length, this means that
98 * the number of arguments is limited to:
100 * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
102 * We arbitrarily limit the number of arguments to 5000. This is
103 * allowed by POSIX.2 as long as the resulting minimum exec line is
104 * at least LINE_MAX. Realloc'ing as necessary is possible, but
105 * probably not worthwhile.
108 if ((arg_max
= sysconf(_SC_ARG_MAX
)) == -1)
109 errx(1, "sysconf(_SC_ARG_MAX) failed");
110 nline
= arg_max
- 4 * 1024;
111 while (*ep
!= NULL
) {
112 /* 1 byte for each '\0' */
113 nline
-= strlen(*ep
++) + 1 + sizeof(*ep
);
115 while ((ch
= getopt(argc
, argv
, "0E:I:J:L:n:opR:s:tx")) != -1)
132 Lflag
= strtol(optarg
, &tmp
, 10);
133 if (*tmp
!= 0 || *optarg
== 0)
134 errx(1, "illegal argument count");
138 if ((nargs
= atoi(optarg
)) <= 0)
139 errx(1, "illegal argument count");
148 if ((Rflag
= atoi(optarg
)) <= 0)
149 errx(1, "illegal number of replacements");
152 nline
= atoi(optarg
);
178 if (replstr
!= NULL
&& *replstr
== '\0')
179 errx(1, "replstr may not be empty");
182 * Allocate pointers for the utility name, the utility arguments,
183 * the maximum arguments to be read from stdin and the trailing
186 linelen
= 1 + argc
+ nargs
+ 1;
187 if ((av
= bxp
= malloc(linelen
* sizeof(char **))) == NULL
)
188 errx(1, "malloc failed");
191 * Use the user's name for the utility as argv[0], just like the
192 * shell. Echo is the default. Set up pointers for the user's
196 cnt
= strlen(*bxp
++ = echo
);
199 if (Jflag
&& strcmp(*argv
, replstr
) == 0) {
203 for (avj
= argv
; *avj
; avj
++)
204 cnt
+= strlen(*avj
) + 1;
207 cnt
+= strlen(*bxp
++ = *argv
) + 1;
208 } while (*++argv
!= NULL
);
212 * Set up begin/end/traversing pointers into the array. The -n
213 * count doesn't include the trailing NULL pointer, so the malloc
214 * added in an extra slot.
216 expx
= (xp
= bxp
) + nargs
;
219 * Allocate buffer space for the arguments read from stdin and the
220 * trailing NULL. Buffer space is defined as the default or specified
221 * space, minus the length of the utility name and arguments. Set up
222 * begin/end/traversing pointers into the array. The -s count does
223 * include the trailing NULL, so the malloc didn't add in an extra
228 errx(1, "insufficient space for command");
230 if ((bbp
= malloc((size_t)(nline
+ 1))) == NULL
)
231 errx(1, "malloc failed");
232 ebp
= (argp
= p
= bbp
) + nline
- 1;
234 parse_input(argc
, argv
);
238 parse_input(int argc
, char *argv
[])
245 switch(ch
= getchar()) {
247 /* No arguments since last exec. */
253 /* Quotes escape tabs and spaces. */
254 if (insingle
|| indouble
|| zflag
)
266 /* Quotes do not escape newlines. */
267 arg1
: if (insingle
|| indouble
)
268 errx(1, "unterminated quote");
270 foundeof
= *eofstr
!= '\0' &&
271 strcmp(argp
, eofstr
) == 0;
273 /* Do not make empty args unless they are quoted */
274 if ((argp
!= p
|| wasquoted
) && !foundeof
) {
284 * If this string is not zero
285 * length, append a space for
286 * separation before the next
289 if ((curlen
= strlen(inpline
)))
290 strcat(inpline
, " ");
294 * Allocate enough to hold what we will
295 * be holding in a second, and to append
296 * a space next time through, if we have
299 inpline
= realloc(inpline
, curlen
+ 2 +
302 errx(1, "realloc failed");
304 strcpy(inpline
, argp
);
306 strcat(inpline
, argp
);
311 * If max'd out on args or buffer, or reached EOF,
312 * run the command. If xflag and max'd out on buffer
313 * but not on args, object. Having reached the limit
314 * of input lines, as specified by -L is the same as
315 * maxing out on arguments.
317 if (xp
== expx
|| p
> ebp
|| ch
== EOF
||
318 (Lflag
<= count
&& xflag
) || foundeof
) {
319 if (xflag
&& xp
!= expx
&& p
> ebp
)
320 errx(1, "insufficient space for arguments");
322 for (avj
= argv
; *avj
; avj
++)
326 if (ch
== EOF
|| foundeof
)
336 if (indouble
|| zflag
)
338 insingle
= !insingle
;
342 if (insingle
|| zflag
)
344 indouble
= !indouble
;
350 /* Backslash escapes anything, is escaped by quotes. */
351 if (!insingle
&& !indouble
&& (ch
= getchar()) == EOF
)
352 errx(1, "backslash at EOF");
355 addch
: if (p
< ebp
) {
360 /* If only one argument, not enough buffer space. */
362 errx(1, "insufficient space for argument");
363 /* Didn't hit argument limit, so if xflag object. */
365 errx(1, "insufficient space for arguments");
368 for (avj
= argv
; *avj
; avj
++)
374 memcpy(bbp
, argp
, (size_t)cnt
);
375 p
= (argp
= bbp
) + cnt
;
383 * Do things necessary before run()'ing, such as -I substitution,
384 * and then call run().
387 prerun(int argc
, char *argv
[])
389 char **tmp
, **tmp2
, **avj
;
394 if (argc
== 0 || repls
== 0) {
403 * Allocate memory to hold the argument list, and
404 * a NULL at the tail.
406 tmp
= malloc((argc
+ 1) * sizeof(char**));
408 errx(1, "malloc failed");
412 * Save the first argument and iterate over it, we
413 * cannot do strnsubst() to it.
415 if ((*tmp
++ = strdup(*avj
++)) == NULL
)
416 errx(1, "strdup failed");
419 * For each argument to utility, if we have not used up
420 * the number of replacements we are allowed to do, and
421 * if the argument contains at least one occurrence of
422 * replstr, call strnsubst(), else just save the string.
423 * Iterations over elements of avj and tmp are done
428 if (repls
&& strstr(*tmp
, replstr
) != NULL
) {
429 strnsubst(tmp
++, replstr
, inpline
, (size_t)255);
432 if ((*tmp
= strdup(*tmp
)) == NULL
)
433 errx(1, "strdup failed");
445 * Walk from the tail to the head, free along the way.
447 for (; tmp2
!= tmp
; tmp
--)
450 * Now free the list itself.
455 * Free the input line buffer, if we have one.
457 if (inpline
!= NULL
) {
466 volatile int childerr
;
472 * If the user wants to be notified of each command before it is
473 * executed, notify them. If they want the notification to be
474 * followed by a prompt, then prompt them.
476 if (tflag
|| pflag
) {
477 (void)fprintf(stderr
, "%s", *argv
);
478 for (avec
= argv
+ 1; *avec
!= NULL
; ++avec
)
479 (void)fprintf(stderr
, " %s", *avec
);
481 * If the user has asked to be prompted, do so.
485 * If they asked not to exec, return without execution
486 * but if they asked to, go to the execution. If we
487 * could not open their tty, break the switch and drop
488 * back to -t behaviour.
498 (void)fprintf(stderr
, "\n");
499 (void)fflush(stderr
);
503 switch(pid
= vfork()) {
509 if (open("/dev/tty", O_RDONLY
) == -1)
510 err(1, "open /dev/tty");
512 if (open("/dev/null", O_RDONLY
) == -1)
513 err(1, "open /dev/null");
515 execvp(argv
[0], argv
);
519 pid
= waitpid(pid
, &status
, 0);
522 /* If we couldn't invoke the utility, exit. */
524 err(childerr
== ENOENT
? 127 : 126, "%s", *argv
);
525 /* If utility signaled or exited with a value of 255, exit 1-125. */
526 if (WIFSIGNALED(status
) || WEXITSTATUS(status
) == 255)
528 if (WEXITSTATUS(status
))
533 * Prompt the user about running a command.
544 if ((ttyfp
= fopen(_PATH_TTY
, "r")) == NULL
)
545 return (2); /* Indicate that the TTY failed to open. */
546 (void)fprintf(stderr
, "?...");
547 (void)fflush(stderr
);
548 if ((response
= fgetln(ttyfp
, &rsize
)) == NULL
||
553 nl_langinfo(YESEXPR
),
559 match
= regexec(&cre
, response
, 0, NULL
, 0);
569 "usage: xargs [-0opt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
570 " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n");