4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2014 Garrett D'Amore <garrett@damore.org>
23 * Copyright 2012 DEY Storage Systems, Inc.
25 * Portions of this file developed by DEY Storage Systems, Inc. are licensed
26 * under the terms of the Common Development and Distribution License (CDDL)
27 * version 1.0 only. The use of subsequent versions of the License are
28 * is specifically prohibited unless those terms are not in conflict with
29 * version 1.0 of the License. You can find this license on-line at
30 * http://www.illumos.org/license/CDDL
33 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
34 * Use is subject to license terms.
37 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
38 /* All Rights Reserved */
42 #include <sys/types.h>
57 #include "getresponse.h"
66 #define BUFSIZE LINE_MAX
68 #define INSPAT_STR "{}" /* default replstr string for -[Ii] */
71 #define QBUF_STARTLEN 255 /* start size of growable string buffer */
72 #define QBUF_INC 100 /* how much to grow a growable string by */
74 /* We use these macros to help make formatting look "consistent" */
75 #define EMSG(s) ermsg(gettext(s "\n"))
76 #define EMSG2(s, a) ermsg(gettext(s "\n"), a)
77 #define PERR(s) perror(gettext("xargs: " s))
79 /* Some common error messages */
81 #define LIST2LONG "Argument list too long"
82 #define ARG2LONG "A single argument was greater than %d bytes"
83 #define MALLOCFAIL "Memory allocation failure"
84 #define CORRUPTFILE "Corrupt input file"
85 #define WAITFAIL "Wait failure"
86 #define CHILDSIG "Child killed with signal %d"
87 #define CHILDFAIL "Command could not continue processing data"
88 #define FORKFAIL "Could not fork child"
89 #define EXECFAIL "Could not exec command"
90 #define MISSQUOTE "Missing quote"
91 #define BADESCAPE "Incomplete escape"
92 #define IBUFOVERFLOW "Insert buffer overflow"
94 #define _(x) gettext(x)
96 static wctype_t blank
;
97 static char *arglist
[MAXARGS
+1];
98 static char argbuf
[BUFSIZE
* 2 + 1];
99 static char lastarg
[BUFSIZE
+ 1];
100 static char **ARGV
= arglist
;
101 static char *LEOF
= "_";
102 static char *INSPAT
= INSPAT_STR
;
103 static char ins_buf
[MAXIBUF
];
106 static struct inserts
{
107 char **p_ARGV
; /* where to put newarg ptr in arg list */
108 char *p_skel
; /* ptr to arg template */
109 } saveargv
[MAXINSERTS
];
111 static int PROMPT
= -1;
112 static int BUFLIM
= BUFSIZE
;
113 static int N_ARGS
= 0;
114 static int N_args
= 0;
115 static int N_lines
= 0;
116 static int DASHX
= FALSE
;
117 static int MORE
= TRUE
;
118 static int PER_LINE
= FALSE
;
119 static int LINE_CONT
= FALSE
;
120 static int EAT_LEAD
= FALSE
;
121 static int ERR
= FALSE
;
122 static int OK
= TRUE
;
123 static int LEGAL
= FALSE
;
124 static int TRACE
= FALSE
;
125 static int INSERT
= FALSE
;
126 static int ZERO
= FALSE
;
127 static int linesize
= 0;
128 static int ibufsize
= 0;
129 static int exitstat
= 0; /* our exit status */
130 static int mac
; /* modified argc, after parsing */
131 static char **mav
; /* modified argv, after parsing */
132 static int n_inserts
; /* # of insertions. */
134 /* our usage message: */
135 #define USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\
136 "[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\
139 static int echoargs();
140 static wint_t getwchr(char *, size_t *);
141 static int lcall(char *sub
, char **subargs
);
142 static void addibuf(struct inserts
*p
);
143 static void ermsg(char *messages
, ...);
144 static char *addarg(char *arg
);
145 static void store_str(char **, char *, size_t);
146 static char *getarg(char *);
147 static char *insert(char *pattern
, char *subst
);
149 static void parseargs();
152 main(int argc
, char **argv
)
155 struct inserts
*psave
;
158 char *cmdname
, **initlist
;
163 blank
= wctype("blank");
166 (void) setlocale(LC_ALL
, "");
167 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
168 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
170 (void) textdomain(TEXT_DOMAIN
);
171 if (init_yes() < 0) {
172 ermsg(_(ERR_MSG_INIT_YES
), strerror(errno
));
176 parseargs(argc
, argv
);
178 /* handling all of xargs arguments: */
179 while ((c
= getopt(mac
, mav
, "0tpe:E:I:i:L:l:n:s:x")) != EOF
) {
185 case 't': /* -t: turn trace mode on */
189 case 'p': /* -p: turn on prompt mode. */
190 if ((PROMPT
= open("/dev/tty", O_RDONLY
)) == -1) {
191 PERR("can't read from tty for -p");
199 * -e[eofstr]: set/disable end-of-file.
200 * N.B. that an argument *isn't* required here; but
201 * parseargs forced an argument if not was given. The
202 * forced argument is the default...
204 LEOF
= optarg
; /* can be empty */
209 * -E eofstr: change end-of-file string.
210 * eofstr *is* required here, but can be empty:
216 /* -I replstr: Insert mode. replstr *is* required. */
217 INSERT
= PER_LINE
= LEGAL
= EAT_LEAD
= TRUE
;
221 if (*optarg
== '\0') {
222 ermsg(_("Option requires an argument: -%c\n"),
229 * -i [replstr]: insert mode, with *optional* replstr.
230 * N.B. that an argument *isn't* required here; if
231 * it's not given, then the string INSPAT_STR will
234 * Since getopts(3C) doesn't handle the case of an
235 * optional variable argument at all, we have to
236 * parse this by hand:
239 INSERT
= PER_LINE
= LEGAL
= EAT_LEAD
= TRUE
;
242 if ((optarg
!= NULL
) && (*optarg
!= '\0')) {
246 * here, there is no next argument. so
247 * we reset INSPAT to the INSPAT_STR.
248 * we *have* to do this, as -i/I may have
249 * been given previously, and XCU4 requires
250 * that only "the last one specified takes
259 * -L number: # of times cmd is executed
260 * number *is* required here:
262 PER_LINE
= LINE_CONT
= TRUE
;
264 INSERT
= EAT_LEAD
= FALSE
;
265 if ((PER_LINE
= atoi(optarg
)) <= 0) {
266 ermsg(_("#lines must be positive int: %s\n"),
273 * -l [number]: # of times cmd is executed
274 * N.B. that an argument *isn't* required here; if
275 * it's not given, then 1 is assumed.
277 * parseargs handles the optional arg processing.
280 PER_LINE
= LINE_CONT
= LEGAL
= TRUE
;
282 INSERT
= EAT_LEAD
= FALSE
;
284 if ((optarg
!= NULL
) && (*optarg
!= '\0')) {
285 if ((PER_LINE
= atoi(optarg
)) <= 0)
290 case 'n': /* -n number: # stdin args */
292 * -n number: # stdin args.
293 * number *is* required here:
295 if ((N_ARGS
= atoi(optarg
)) <= 0) {
296 ermsg(_("#args must be positive int: %s\n"),
299 LEGAL
= DASHX
|| N_ARGS
== 1;
300 INSERT
= PER_LINE
= LINE_CONT
= FALSE
;
304 case 's': /* -s size: set max size of each arg list */
305 BUFLIM
= atoi(optarg
);
306 if (BUFLIM
> BUFSIZE
|| BUFLIM
<= 0) {
307 ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
312 case 'x': /* -x: terminate if args > size limit */
313 DASHX
= LEGAL
= TRUE
;
318 * bad argument. complain and get ready to die.
327 * if anything called ermsg(), something screwed up, so
336 * we're finished handling xargs's options, so now pick up
337 * the command name (if any), and it's options.
341 mac
-= optind
; /* dec arg count by what we've processed */
342 mav
+= optind
; /* inc to current mav */
344 if (mac
<= 0) { /* if there're no more args to process, */
345 cmdname
= "/usr/bin/echo"; /* our default command */
346 *ARGV
++ = addarg(cmdname
); /* use the default cmd. */
347 } else { /* otherwise keep parsing rest of the string. */
349 * note that we can't use getopts(3C), and *must* parse
350 * this by hand, as we don't know apriori what options the
353 cmdname
= *mav
; /* get the command name */
356 /* pick up the remaining args from the command line: */
357 while ((OK
== TRUE
) && (mac
-- > 0)) {
359 * while we haven't crapped out, and there's
362 if (INSERT
&& ! ERR
) {
363 if (strstr(*mav
, INSPAT
) != NULL
) {
364 if (++n_inserts
> MAXINSERTS
) {
365 ermsg(_("too many args "
366 "with %s\n"), INSPAT
);
369 psave
->p_ARGV
= ARGV
;
370 (psave
++)->p_skel
= *mav
;
373 *ARGV
++ = addarg(*mav
++);
377 /* pick up args from standard input */
390 while (MORE
|| (lastarg
[0] != '\0')) {
393 if (*lastarg
!= '\0') {
394 arg
= strcpy(next
, lastarg
);
396 } else if ((arg
= getarg(next
)) == NULL
) {
404 /* Inserts are handled specially later. */
405 if ((n_inserts
== 0) && (linesize
>= BUFLIM
)) {
407 * Legal indicates hard fail if the list is
408 * truncated due to size. So fail, or if we
409 * cannot create any list because it would be
412 if (LEGAL
|| N_args
== 0) {
419 * Otherwise just save argument for later.
421 (void) strcpy(lastarg
, arg
);
429 if ((PER_LINE
&& (N_lines
>= PER_LINE
)) ||
430 (N_ARGS
&& (N_args
>= N_ARGS
))) {
435 if ((ARGV
- arglist
) == MAXARGS
) {
442 /* Reached the end with no more work. */
446 /* insert arg if requested */
448 if (!ERR
&& INSERT
) {
453 for (psave
= saveargv
; ++j
<= n_inserts
; ++psave
) {
463 * if we've done any insertions, re-calculate the
464 * linesize. bomb out if we've exceeded our length.
467 for (ARGV
= arglist
; *ARGV
!= NULL
; ARGV
++) {
468 linesize
+= strlen(*ARGV
) + 1;
470 if (linesize
>= BUFLIM
) {
481 (PER_LINE
&& N_lines
== 0 || N_ARGS
&& N_args
== 0))
484 j
= TRACE
? echoargs() : TRUE
;
487 * for xcu4, all invocations of cmdname must
488 * return 0, in order for us to return 0.
489 * so if we have a non-zero status here,
492 exitstat
|= lcall(cmdname
, arglist
);
501 * if exitstat was set, to match XCU4 complience,
502 * return that value, otherwise, return 1.
504 return (exitstat
? exitstat
: 1);
510 linesize
+= (strlen(arg
) + 1);
516 store_str(char **buffer
, char *str
, size_t len
)
518 (void) memcpy(*buffer
, str
, len
);
519 (*buffer
)[len
] = '\0';
529 char mbc
[MB_LEN_MAX
];
541 c
= getwchr(mbc
, &len
);
543 if (((arg
- xarg
) + len
) > BUFLIM
) {
544 EMSG2(ARG2LONG
, BUFLIM
);
553 store_str(&arg
, mbc
, len
);
557 * NB: Some other versions rip off all of the trailing
558 * blanks. The spec only claims that this should
559 * be done for a single blank. We follow the spec.
561 if (LINE_CONT
&& iswctype(last
, blank
)) {
569 case WEOF
: /* Note WEOF == EOF */
586 if (ZERO
|| escape
|| (inquote
== 1)) {
587 /* treat it literally */
589 store_str(&arg
, mbc
, len
);
591 } else if (inquote
== 2) {
592 /* terminating double quote */
596 /* starting quoted string */
602 if (ZERO
|| escape
|| (inquote
== 2)) {
603 /* treat it literally */
605 store_str(&arg
, mbc
, len
);
607 } else if (inquote
== 1) {
608 /* terminating single quote */
612 /* starting quoted string */
619 * Any unquoted character can be escaped by
620 * preceding it with a backslash.
622 if (ZERO
|| inquote
|| escape
) {
624 store_str(&arg
, mbc
, len
);
631 /* most times we will just want to store it */
632 if (inquote
|| escape
|| ZERO
|| !iswctype(c
, blank
)) {
634 store_str(&arg
, mbc
, len
);
637 if (EAT_LEAD
&& last
== 0) {
638 c
= 0; /* Roll it back */
642 store_str(&arg
, mbc
, len
);
646 /* unquoted blank without special handling */
651 * At this point we are processing a complete argument.
653 if (strcmp(xarg
, LEOF
) == 0 && *LEOF
!= '\0') {
665 return (xarg
[0] == '\0' ? NULL
: xarg
);
669 * ermsg(): print out an error message, and indicate failure globally.
671 * Assumes that message has already been gettext()'d. It would be
672 * nice if we could just do the gettext() here, but we can't, since
673 * since xgettext(1M) wouldn't be able to pick up our error message.
677 ermsg(char *messages
, ...)
681 va_start(ap
, messages
);
683 (void) fprintf(stderr
, "xargs: ");
684 (void) vfprintf(stderr
, messages
, ap
);
694 char **tanarg
; /* tmp ptr */
696 char reply
[LINE_MAX
];
698 tanarg
= anarg
= arglist
-1;
701 * write out each argument, separated by a space. the tanarg
702 * nonsense is for xcu4 testsuite compliance - so that an
703 * extra space isn't echoed after the last argument.
705 while (*++anarg
) { /* while there's an argument */
706 ++tanarg
; /* follow anarg */
707 (void) write(2, *anarg
, strlen(*anarg
));
709 if (*++tanarg
) { /* if there's another argument: */
710 (void) write(2, " ", 1); /* add a space */
711 --tanarg
; /* reset back to anarg */
715 (void) write(2, "\n", 1);
719 (void) write(2, "?...", 4); /* ask the user for input */
721 for (i
= 0; i
< LINE_MAX
&& read(PROMPT
, &reply
[i
], 1) > 0; i
++) {
722 if (reply
[i
] == '\n') {
730 /* flush remainder of line if necessary */
734 while ((read(PROMPT
, &bitbucket
, 1) > 0) && (bitbucket
!= '\n'))
738 return (yes_check(reply
));
743 insert(char *pattern
, char *subst
)
745 static char buffer
[MAXSBUF
+1];
752 ipatlen
= strlen(INSPAT
) - 1;
755 bufend
= &buffer
[MAXSBUF
];
758 if (strncmp(pat
, INSPAT
, ipatlen
+ 1) == 0) {
759 if (pbuf
+ len
>= bufend
) {
762 (void) strcpy(pbuf
, subst
);
777 ermsg(gettext("Maximum argument size with insertion via %s's "
778 "exceeded\n"), INSPAT
);
786 addibuf(struct inserts
*p
)
788 char *newarg
, *skel
, *sub
;
793 newarg
= insert(skel
, sub
);
797 l
= strlen(newarg
) + 1;
798 if ((ibufsize
+= l
) > MAXIBUF
) {
802 (void) strcpy(p_ibuf
, newarg
);
803 *(p
->p_ARGV
) = p_ibuf
;
809 * getwchr(): get the next wide character.
811 * we get the next character from stdin. This returns WEOF if no
812 * character is present. If ZERO is set, it gets a single byte instead
816 getwchr(char *mbc
, size_t *sz
)
823 while (i
< MB_CUR_MAX
) {
825 if ((c
= fgetc(stdin
)) == EOF
) {
828 /* TRUE EOF has been reached */
833 * We have some characters in our buffer still so it
834 * must be an invalid character right before EOF.
840 /* If this succeeds then we are done */
845 if (mbtowc(&wch
, mbc
, i
) != -1) {
847 return ((wint_t)wch
);
852 * We have now encountered an illegal character sequence.
853 * There is nothing much we can do at this point but
854 * return an error. If we attempt to recover we may in fact
855 * return garbage as arguments, from the customer's point
856 * of view. After all what if they are feeding us a file
857 * generated in another locale?
867 lcall(char *sub
, char **subargs
)
869 int retcode
, retry
= 0;
873 switch (child
= fork()) {
875 while ((iwait
= wait(&retcode
)) != child
&&
878 if (iwait
== (pid_t
)-1) {
883 if (WIFSIGNALED(retcode
)) {
884 EMSG2(CHILDSIG
, WTERMSIG(retcode
));
888 if ((WEXITSTATUS(retcode
) & 0377) == 0377) {
893 return (WEXITSTATUS(retcode
));
895 (void) execvp(sub
, subargs
);
902 if (errno
!= EAGAIN
&& retry
++ < FORK_RETRY
) {
922 * parseargs(): modify the args
923 * since the -e, -i and -l flags all take optional subarguments,
924 * and getopts(3C) is clueless about this nonsense, we change the
925 * our local argument count and strings to separate this out,
926 * and make it easier to handle via getopts(3c).
936 * since the -e, -i and -l flags all take optional subarguments,
939 parseargs(int ac
, char **av
)
941 int i
; /* current argument */
942 int cflag
; /* 0 = not processing cmd arg */
944 if ((mav
= malloc((ac
* 2 + 1) * sizeof (char *))) == NULL
) {
949 /* for each argument, see if we need to change things: */
950 for (i
= mac
= cflag
= 0; (av
[i
] != NULL
) && i
< ac
; i
++, mac
++) {
951 if ((mav
[mac
] = strdup(av
[i
])) == NULL
) {
956 /* -- has been found or argument list is fully processes */
961 * if we're doing special processing, and we've got a flag
963 else if ((av
[i
][0] == '-') && (av
[i
][1] != NULL
)) {
968 def
= ""; /* -e with no arg turns off eof */
969 goto process_special
;
972 goto process_special
;
977 * if there's no sub-option, we *must* add
978 * a default one. this is because xargs must
979 * be able to distinguish between a valid
980 * suboption, and a command name.
982 if (av
[i
][2] == NULL
) {
983 mav
[++mac
] = strdup(def
);
985 /* clear out our version: */
987 mav
[++mac
] = strdup(&av
[i
][2]);
989 if (mav
[mac
] == NULL
) {
995 /* flags with required subarguments: */
998 * there are two separate cases here. either the
999 * flag can have the normal XCU4 handling
1000 * (of the form: -X subargument); or it can have
1001 * the old solaris 2.[0-4] handling (of the
1002 * form: -Xsubargument). in order to maintain
1003 * backwards compatibility, we must support the
1004 * latter case. we handle the latter possibility
1005 * first so both the old solaris way of handling
1006 * and the new XCU4 way of handling things are allowed.
1008 case 'n': /* FALLTHROUGH */
1009 case 's': /* FALLTHROUGH */
1010 case 'E': /* FALLTHROUGH */
1011 case 'I': /* FALLTHROUGH */
1014 * if the second character isn't null, then
1015 * the user has specified the old syntax.
1016 * we move the subargument into our
1017 * mod'd argument list.
1019 if (av
[i
][2] != NULL
) {
1020 /* first clean things up: */
1023 /* now add the separation: */
1024 ++mac
; /* inc to next mod'd arg */
1025 if ((mav
[mac
] = strdup(&av
[i
][2])) ==
1035 if (av
[i
] == NULL
) {
1039 if ((mav
[mac
] = strdup(av
[i
])) == NULL
) {
1055 * here we've hit the cmd argument. so
1056 * we'll stop special processing, as the
1057 * cmd may have a "-i" etc., argument,
1058 * and we don't want to add a "" to it.
1063 } else if (i
> 0) { /* if we're not the 1st arg */
1065 * if it's not a flag, then it *must* be the cmd.
1066 * set cflag, so we don't mishandle the -[eil] flags.