]>
git.saurik.com Git - apple/shell_cmds.git/blob - xargs/xargs.c
ed3eba78b4b97bba2ce6c85fe8fb1b469484fc33
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"
69 static void parse_input(int, char *[]);
70 static void prerun(int, char *[]);
71 static int prompt(void);
72 static void run(char **);
73 static void usage(void);
74 void strnsubst(char **, const char *, const char *, size_t);
76 static char echo
[] = _PATH_ECHO
;
77 static char **av
, **bxp
, **ep
, **exp
, **xp
;
78 static char *argp
, *bbp
, *ebp
, *inpline
, *p
, *replstr
;
79 static const char *eofstr
;
80 static int count
, insingle
, indouble
, pflag
, tflag
, Rflag
, rval
, zflag
;
81 static int cnt
, Iflag
, jfound
, Lflag
, wasquoted
, xflag
;
83 extern char **environ
;
86 main(int argc
, char *argv
[])
89 int ch
, Jflag
, nargs
, nflag
, nline
;
92 inpline
= replstr
= NULL
;
98 * POSIX.2 limits the exec line length to ARG_MAX - 2K. Running that
99 * caused some E2BIG errors, so it was changed to ARG_MAX - 4K. Given
100 * that the smallest argument is 2 bytes in length, this means that
101 * the number of arguments is limited to:
103 * (ARG_MAX - 4K - LENGTH(utility + arguments)) / 2.
105 * We arbitrarily limit the number of arguments to 5000. This is
106 * allowed by POSIX.2 as long as the resulting minimum exec line is
107 * at least LINE_MAX. Realloc'ing as necessary is possible, but
108 * probably not worthwhile.
111 if ((arg_max
= sysconf(_SC_ARG_MAX
)) == -1)
112 errx(1, "sysconf(_SC_ARG_MAX) failed");
113 nline
= arg_max
- 4 * 1024;
114 while (*ep
!= NULL
) {
115 /* 1 byte for each '\0' */
116 nline
-= strlen(*ep
++) + 1 + sizeof(*ep
);
118 while ((ch
= getopt(argc
, argv
, "0E:I:J:L:n:pR:s:tx")) != -1)
135 Lflag
= atoi(optarg
);
139 if ((nargs
= atoi(optarg
)) <= 0)
140 errx(1, "illegal argument count");
146 if ((Rflag
= atoi(optarg
)) <= 0)
147 errx(1, "illegal number of replacements");
150 nline
= atoi(optarg
);
176 if (replstr
!= NULL
&& *replstr
== '\0')
177 errx(1, "replstr may not be empty");
180 * Allocate pointers for the utility name, the utility arguments,
181 * the maximum arguments to be read from stdin and the trailing
184 linelen
= 1 + argc
+ nargs
+ 1;
185 if ((av
= bxp
= malloc(linelen
* sizeof(char **))) == NULL
)
186 errx(1, "malloc failed");
189 * Use the user's name for the utility as argv[0], just like the
190 * shell. Echo is the default. Set up pointers for the user's
194 cnt
= strlen(*bxp
++ = echo
);
197 if (Jflag
&& strcmp(*argv
, replstr
) == 0) {
201 for (avj
= argv
; *avj
; avj
++)
202 cnt
+= strlen(*avj
) + 1;
205 cnt
+= strlen(*bxp
++ = *argv
) + 1;
206 } while (*++argv
!= NULL
);
210 * Set up begin/end/traversing pointers into the array. The -n
211 * count doesn't include the trailing NULL pointer, so the malloc
212 * added in an extra slot.
214 exp
= (xp
= bxp
) + nargs
;
217 * Allocate buffer space for the arguments read from stdin and the
218 * trailing NULL. Buffer space is defined as the default or specified
219 * space, minus the length of the utility name and arguments. Set up
220 * begin/end/traversing pointers into the array. The -s count does
221 * include the trailing NULL, so the malloc didn't add in an extra
226 errx(1, "insufficient space for command");
228 if ((bbp
= malloc((size_t)(nline
+ 1))) == NULL
)
229 errx(1, "malloc failed");
230 ebp
= (argp
= p
= bbp
) + nline
- 1;
232 parse_input(argc
, argv
);
236 parse_input(int argc
, char *argv
[])
243 switch(ch
= getchar()) {
245 /* No arguments since last exec. */
251 /* Quotes escape tabs and spaces. */
252 if (insingle
|| indouble
|| zflag
)
264 /* Quotes do not escape newlines. */
265 arg1
: if (insingle
|| indouble
)
266 errx(1, "unterminated quote");
268 foundeof
= *eofstr
!= '\0' &&
269 strcmp(argp
, eofstr
) == 0;
271 /* Do not make empty args unless they are quoted */
272 if ((argp
!= p
|| wasquoted
) && !foundeof
) {
282 * If this string is not zero
283 * length, append a space for
284 * seperation before the next
287 if ((curlen
= strlen(inpline
)))
288 strcat(inpline
, " ");
292 * Allocate enough to hold what we will
293 * be holding in a second, and to append
294 * a space next time through, if we have
297 inpline
= realloc(inpline
, curlen
+ 2 +
300 errx(1, "realloc failed");
302 strcpy(inpline
, argp
);
304 strcat(inpline
, argp
);
309 * If max'd out on args or buffer, or reached EOF,
310 * run the command. If xflag and max'd out on buffer
311 * but not on args, object. Having reached the limit
312 * of input lines, as specified by -L is the same as
313 * maxing out on arguments.
315 if (xp
== exp
|| p
> ebp
|| ch
== EOF
||
316 (Lflag
<= count
&& xflag
) || foundeof
) {
317 if (xflag
&& xp
!= exp
&& p
> ebp
)
318 errx(1, "insufficient space for arguments");
320 for (avj
= argv
; *avj
; avj
++)
324 if (ch
== EOF
|| foundeof
)
334 if (indouble
|| zflag
)
336 insingle
= !insingle
;
340 if (insingle
|| zflag
)
342 indouble
= !indouble
;
348 /* Backslash escapes anything, is escaped by quotes. */
349 if (!insingle
&& !indouble
&& (ch
= getchar()) == EOF
)
350 errx(1, "backslash at EOF");
353 addch
: if (p
< ebp
) {
358 /* If only one argument, not enough buffer space. */
360 errx(1, "insufficient space for argument");
361 /* Didn't hit argument limit, so if xflag object. */
363 errx(1, "insufficient space for arguments");
366 for (avj
= argv
; *avj
; avj
++)
372 memcpy(bbp
, argp
, (size_t)cnt
);
373 p
= (argp
= bbp
) + cnt
;
381 * Do things necessary before run()'ing, such as -I substitution,
382 * and then call run().
385 prerun(int argc
, char *argv
[])
387 char **tmp
, **tmp2
, **avj
;
392 if (argc
== 0 || repls
== 0) {
401 * Allocate memory to hold the argument list, and
402 * a NULL at the tail.
404 tmp
= malloc((argc
+ 1) * sizeof(char**));
406 errx(1, "malloc failed");
410 * Save the first argument and iterate over it, we
411 * cannot do strnsubst() to it.
413 if ((*tmp
++ = strdup(*avj
++)) == NULL
)
414 errx(1, "strdup failed");
417 * For each argument to utility, if we have not used up
418 * the number of replacements we are allowed to do, and
419 * if the argument contains at least one occurance of
420 * replstr, call strnsubst(), else just save the string.
421 * Iterations over elements of avj and tmp are done
426 if (repls
&& strstr(*tmp
, replstr
) != NULL
) {
427 strnsubst(tmp
++, replstr
, inpline
, (size_t)255);
430 if ((*tmp
= strdup(*tmp
)) == NULL
)
431 errx(1, "strdup failed");
443 * Walk from the tail to the head, free along the way.
445 for (; tmp2
!= tmp
; tmp
--)
448 * Now free the list itself.
453 * Free the input line buffer, if we have one.
455 if (inpline
!= NULL
) {
464 volatile int childerr
;
470 * If the user wants to be notified of each command before it is
471 * executed, notify them. If they want the notification to be
472 * followed by a prompt, then prompt them.
474 if (tflag
|| pflag
) {
475 (void)fprintf(stderr
, "%s", *argv
);
476 for (avec
= argv
+ 1; *avec
!= NULL
; ++avec
)
477 (void)fprintf(stderr
, " %s", *avec
);
479 * If the user has asked to be prompted, do so.
483 * If they asked not to exec, return without execution
484 * but if they asked to, go to the execution. If we
485 * could not open their tty, break the switch and drop
486 * back to -t behaviour.
496 (void)fprintf(stderr
, "\n");
497 (void)fflush(stderr
);
501 switch(pid
= vfork()) {
505 execvp(argv
[0], argv
);
509 pid
= waitpid(pid
, &status
, 0);
512 /* If we couldn't invoke the utility, exit. */
514 err(childerr
== ENOENT
? 127 : 126, "%s", *argv
);
515 /* If utility signaled or exited with a value of 255, exit 1-125. */
516 if (WIFSIGNALED(status
) || WEXITSTATUS(status
) == 255)
518 if (WEXITSTATUS(status
))
523 * Prompt the user about running a command.
534 if ((ttyfp
= fopen(_PATH_TTY
, "r")) == NULL
)
535 return (2); /* Indicate that the TTY failed to open. */
536 (void)fprintf(stderr
, "?...");
537 (void)fflush(stderr
);
538 if ((response
= fgetln(ttyfp
, &rsize
)) == NULL
||
545 match
= regexec(&cre
, response
, 0, NULL
, 0);
555 "usage: xargs [-0pt] [-E eofstr] [-I replstr [-R replacements]] [-J replstr]\n"
556 " [-L number] [-n number [-x] [-s size] [utility [argument ...]]\n");