]>
git.saurik.com Git - apple/shell_cmds.git/blob - xargs/xargs.c
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 $
40 static const char copyright
[] =
41 "@(#) Copyright (c) 1990, 1993\n\
42 The Regents of the University of California. All rights reserved.\n";
47 static char sccsid
[] = "@(#)xargs.c 8.1 (Berkeley) 6/6/93";
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 $");
54 #include <sys/types.h>
67 #include "pathnames.h"
70 #include <get_compat.h>
72 #define COMPAT_MODE(a,b) (1)
73 #endif /* __APPLE__ */
75 static void parse_input(int, char *[]);
76 static void prerun(int, char *[]);
77 static int prompt(void);
78 static void run(char **);
79 static void usage(void);
80 void strnsubst(char **, const char *, const char *, size_t);
82 static char echo
[] = _PATH_ECHO
;
83 static char **av
, **bxp
, **ep
, **exp
, **xp
;
84 static char *argp
, *bbp
, *ebp
, *inpline
, *p
, *replstr
;
85 static const char *eofstr
;
86 static int count
, insingle
, indouble
, pflag
, tflag
, Rflag
, rval
, zflag
;
87 static int cnt
, Iflag
, jfound
, Lflag
, wasquoted
, xflag
;
88 static int last_was_newline
= 1;
89 static int last_was_blank
= 0;
91 extern char **environ
;
94 main(int argc
, char *argv
[])
97 int ch
, Jflag
, nargs
, nflag
, nline
;
100 inpline
= replstr
= NULL
;
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:
111 * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
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.
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
);
126 while ((ch
= getopt(argc
, argv
, "0E:I:J:L:n:pR:s:tx")) != -1)
143 Lflag
= atoi(optarg
);
144 if (COMPAT_MODE("bin/xargs", "Unix2003")) {
145 nflag
= 0; /* Override */
151 if (COMPAT_MODE("bin/xargs", "Unix2003")) {
152 Lflag
= 0; /* Override */
154 if ((nargs
= atoi(optarg
)) <= 0)
155 errx(1, "illegal argument count");
161 if ((Rflag
= atoi(optarg
)) <= 0)
162 errx(1, "illegal number of replacements");
165 nline
= atoi(optarg
);
191 if (replstr
!= NULL
&& *replstr
== '\0')
192 errx(1, "replstr may not be empty");
195 * Allocate pointers for the utility name, the utility arguments,
196 * the maximum arguments to be read from stdin and the trailing
199 linelen
= 1 + argc
+ nargs
+ 1;
200 if ((av
= bxp
= malloc(linelen
* sizeof(char **))) == NULL
)
201 errx(1, "malloc failed");
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
209 cnt
= strlen(*bxp
++ = echo
);
212 if (Jflag
&& strcmp(*argv
, replstr
) == 0) {
216 for (avj
= argv
; *avj
; avj
++)
217 cnt
+= strlen(*avj
) + 1;
220 cnt
+= strlen(*bxp
++ = *argv
) + 1;
221 } while (*++argv
!= NULL
);
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.
229 exp
= (xp
= bxp
) + nargs
;
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
241 errx(1, "insufficient space for command");
243 if ((bbp
= malloc((size_t)(nline
+ 1))) == NULL
)
244 errx(1, "malloc failed");
245 ebp
= (argp
= p
= bbp
) + nline
- 1;
247 parse_input(argc
, argv
);
251 parse_input(int argc
, char *argv
[])
255 int last_was_backslashed
= 0;
259 switch(ch
= getchar()) {
261 /* No arguments since last exec. */
268 /* Quotes escape tabs and spaces. */
269 if (insingle
|| indouble
|| zflag
)
277 if (COMPAT_MODE("bin/xargs", "Unix2003")) {
278 if (last_was_newline
) {
279 /* don't count empty line */
282 if (!last_was_blank
) {
283 /* only count if NOT continuation line */
289 last_was_newline
= 1;
293 /* Quotes do not escape newlines. */
294 arg1
: if (insingle
|| indouble
)
295 errx(1, "unterminated quote");
297 foundeof
= *eofstr
!= '\0' &&
298 strcmp(argp
, eofstr
) == 0;
300 /* Do not make empty args unless they are quoted */
301 if ((argp
!= p
|| wasquoted
) && !foundeof
) {
311 * If this string is not zero
312 * length, append a space for
313 * seperation before the next
316 if ((curlen
= strlen(inpline
)))
317 strcat(inpline
, " ");
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
326 inpline
= realloc(inpline
, curlen
+ 2 +
329 errx(1, "realloc failed");
331 strcpy(inpline
, argp
);
333 strcat(inpline
, argp
);
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.
344 if (xp
== exp
|| p
> ebp
|| ch
== EOF
||
345 (Lflag
<= count
&& xflag
) || foundeof
) {
346 if (xflag
&& xp
!= exp
&& p
> ebp
)
347 errx(1, "insufficient space for arguments");
349 for (avj
= argv
; *avj
; avj
++)
353 if (ch
== EOF
|| foundeof
)
363 if (indouble
|| zflag
)
365 insingle
= !insingle
;
369 if (insingle
|| zflag
)
371 indouble
= !indouble
;
375 last_was_backslashed
= 1;
378 /* Backslash escapes anything, is escaped by quotes. */
379 if (!insingle
&& !indouble
&& (ch
= getchar()) == EOF
)
380 errx(1, "backslash at EOF");
383 addch
: if (p
< ebp
) {
388 /* If only one argument, not enough buffer space. */
390 errx(1, "insufficient space for argument");
391 /* Didn't hit argument limit, so if xflag object. */
393 errx(1, "insufficient space for arguments");
396 for (avj
= argv
; *avj
; avj
++)
402 memcpy(bbp
, argp
, (size_t)cnt
);
403 p
= (argp
= bbp
) + cnt
;
409 if (ch
!= '\n' || last_was_backslashed
)
410 last_was_newline
= 0;
415 * Do things necessary before run()'ing, such as -I substitution,
416 * and then call run().
419 prerun(int argc
, char *argv
[])
421 char **tmp
, **tmp2
, **avj
;
426 if (argc
== 0 || repls
== 0) {
435 * Allocate memory to hold the argument list, and
436 * a NULL at the tail.
438 tmp
= malloc((argc
+ 1) * sizeof(char**));
440 errx(1, "malloc failed");
444 * Save the first argument and iterate over it, we
445 * cannot do strnsubst() to it.
447 if ((*tmp
++ = strdup(*avj
++)) == NULL
)
448 errx(1, "strdup failed");
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
460 if (repls
&& strstr(*tmp
, replstr
) != NULL
) {
461 strnsubst(tmp
++, replstr
, inpline
, (size_t)255);
464 if ((*tmp
= strdup(*tmp
)) == NULL
)
465 errx(1, "strdup failed");
477 * Walk from the tail to the head, free along the way.
479 for (; tmp2
!= tmp
; tmp
--)
482 * Now free the list itself.
487 * Free the input line buffer, if we have one.
489 if (inpline
!= NULL
) {
498 volatile int childerr
;
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.
508 if (tflag
|| pflag
) {
509 (void)fprintf(stderr
, "%s", *argv
);
510 for (avec
= argv
+ 1; *avec
!= NULL
; ++avec
)
511 (void)fprintf(stderr
, " %s", *avec
);
513 * If the user has asked to be prompted, do so.
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.
530 (void)fprintf(stderr
, "\n");
531 (void)fflush(stderr
);
535 switch(pid
= vfork()) {
539 execvp(argv
[0], argv
);
543 pid
= waitpid(pid
, &status
, 0);
546 /* If we couldn't invoke the utility, exit. */
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)
552 if (WEXITSTATUS(status
))
557 * Prompt the user about running a command.
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
||
579 match
= regexec(&cre
, response
, 0, NULL
, 0);
589 "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
590 " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n");