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.
24 * Copyright (c) 2017, Joyent, Inc.
26 * Portions of this file developed by DEY Storage Systems, Inc. are licensed
27 * under the terms of the Common Development and Distribution License (CDDL)
28 * version 1.0 only. The use of subsequent versions of the License are
29 * is specifically prohibited unless those terms are not in conflict with
30 * version 1.0 of the License. You can find this license on-line at
31 * http://www.illumos.org/license/CDDL
34 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
35 * Use is subject to license terms.
38 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
39 /* All Rights Reserved */
43 #include <sys/types.h>
59 #include "getresponse.h"
68 #define BUFSIZE LINE_MAX
70 #define INSPAT_STR "{}" /* default replstr string for -[Ii] */
73 #define QBUF_STARTLEN 255 /* start size of growable string buffer */
74 #define QBUF_INC 100 /* how much to grow a growable string by */
76 /* We use these macros to help make formatting look "consistent" */
77 #define EMSG(s) ermsg(gettext(s "\n"))
78 #define EMSG2(s, a) ermsg(gettext(s "\n"), a)
79 #define PERR(s) perror(gettext("xargs: " s))
81 /* Some common error messages */
83 #define LIST2LONG "Argument list too long"
84 #define ARG2LONG "A single argument was greater than %d bytes"
85 #define MALLOCFAIL "Memory allocation failure"
86 #define CORRUPTFILE "Corrupt input file"
87 #define WAITFAIL "Wait failure"
88 #define CHILDSIG "Child killed with signal %d"
89 #define CHILDFAIL "Command could not continue processing data"
90 #define FORKFAIL "Could not fork child"
91 #define EXECFAIL "Could not exec command"
92 #define MISSQUOTE "Missing quote"
93 #define BADESCAPE "Incomplete escape"
94 #define IBUFOVERFLOW "Insert buffer overflow"
95 #define NOCHILDSLOT "No free child slot available"
97 #define _(x) gettext(x)
99 static wctype_t blank
;
100 static char *arglist
[MAXARGS
+1];
101 static char argbuf
[BUFSIZE
* 2 + 1];
102 static char lastarg
[BUFSIZE
+ 1];
103 static char **ARGV
= arglist
;
104 static char *LEOF
= "_";
105 static char *INSPAT
= INSPAT_STR
;
106 static char ins_buf
[MAXIBUF
];
109 static struct inserts
{
110 char **p_ARGV
; /* where to put newarg ptr in arg list */
111 char *p_skel
; /* ptr to arg template */
112 } saveargv
[MAXINSERTS
];
114 static int PROMPT
= -1;
115 static int BUFLIM
= BUFSIZE
;
116 static int MAXPROCS
= 1;
117 static int N_ARGS
= 0;
118 static int N_args
= 0;
119 static int N_lines
= 0;
120 static int DASHX
= FALSE
;
121 static int MORE
= TRUE
;
122 static int PER_LINE
= FALSE
;
123 static int LINE_CONT
= FALSE
;
124 static int EAT_LEAD
= FALSE
;
125 static int ERR
= FALSE
;
126 static int OK
= TRUE
;
127 static int LEGAL
= FALSE
;
128 static int TRACE
= FALSE
;
129 static int INSERT
= FALSE
;
130 static int ZERO
= FALSE
;
131 static int linesize
= 0;
132 static int ibufsize
= 0;
133 static int exitstat
= 0; /* our exit status */
134 static int mac
; /* modified argc, after parsing */
135 static char **mav
; /* modified argv, after parsing */
136 static int n_inserts
; /* # of insertions. */
137 static pid_t
*procs
; /* pids of children */
138 static int n_procs
; /* # of child processes. */
140 /* our usage message: */
141 #define USAGEMSG "Usage: xargs: [-t] [-p] [-0] [-e[eofstr]] [-E eofstr] "\
142 "[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-P maxprocs] "\
143 "[-s size] [cmd [args ...]]\n"
145 static int echoargs();
146 static wint_t getwchr(char *, size_t *);
147 static void lcall(char *sub
, char **subargs
);
148 static void addibuf(struct inserts
*p
);
149 static void ermsg(char *messages
, ...);
150 static char *addarg(char *arg
);
151 static void store_str(char **, char *, size_t);
152 static char *getarg(char *);
153 static char *insert(char *pattern
, char *subst
);
155 static void parseargs();
156 static int procs_find(pid_t child
);
157 static void procs_store(pid_t child
);
158 static boolean_t
procs_delete(pid_t child
);
159 static pid_t
procs_waitpid(boolean_t blocking
, int *stat_loc
);
160 static void procs_wait(boolean_t blocking
);
163 main(int argc
, char **argv
)
167 struct inserts
*psave
;
170 char *cmdname
, **initlist
;
176 blank
= wctype("blank");
179 (void) setlocale(LC_ALL
, "");
180 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
181 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
183 (void) textdomain(TEXT_DOMAIN
);
184 if (init_yes() < 0) {
185 ermsg(_(ERR_MSG_INIT_YES
), strerror(errno
));
189 parseargs(argc
, argv
);
191 /* handling all of xargs arguments: */
192 while ((c
= getopt(mac
, mav
, "0tpe:E:I:i:L:l:n:P:s:x")) != EOF
) {
198 case 't': /* -t: turn trace mode on */
202 case 'p': /* -p: turn on prompt mode. */
203 if ((PROMPT
= open("/dev/tty", O_RDONLY
)) == -1) {
204 PERR("can't read from tty for -p");
212 * -e[eofstr]: set/disable end-of-file.
213 * N.B. that an argument *isn't* required here; but
214 * parseargs forced an argument if not was given. The
215 * forced argument is the default...
217 LEOF
= optarg
; /* can be empty */
222 * -E eofstr: change end-of-file string.
223 * eofstr *is* required here, but can be empty:
229 /* -I replstr: Insert mode. replstr *is* required. */
230 INSERT
= PER_LINE
= LEGAL
= EAT_LEAD
= TRUE
;
234 if (*optarg
== '\0') {
235 ermsg(_("Option requires an argument: -%c\n"),
242 * -i [replstr]: insert mode, with *optional* replstr.
243 * N.B. that an argument *isn't* required here; if
244 * it's not given, then the string INSPAT_STR will
247 * Since getopts(3C) doesn't handle the case of an
248 * optional variable argument at all, we have to
249 * parse this by hand:
252 INSERT
= PER_LINE
= LEGAL
= EAT_LEAD
= TRUE
;
255 if ((optarg
!= NULL
) && (*optarg
!= '\0')) {
259 * here, there is no next argument. so
260 * we reset INSPAT to the INSPAT_STR.
261 * we *have* to do this, as -i/I may have
262 * been given previously, and XCU4 requires
263 * that only "the last one specified takes
272 * -L number: # of times cmd is executed
273 * number *is* required here:
275 PER_LINE
= LINE_CONT
= TRUE
;
277 INSERT
= EAT_LEAD
= FALSE
;
278 if ((PER_LINE
= atoi(optarg
)) <= 0) {
279 ermsg(_("#lines must be positive int: %s\n"),
286 * -l [number]: # of times cmd is executed
287 * N.B. that an argument *isn't* required here; if
288 * it's not given, then 1 is assumed.
290 * parseargs handles the optional arg processing.
293 PER_LINE
= LINE_CONT
= LEGAL
= TRUE
;
295 INSERT
= EAT_LEAD
= FALSE
;
297 if ((optarg
!= NULL
) && (*optarg
!= '\0')) {
298 if ((PER_LINE
= atoi(optarg
)) <= 0)
303 case 'n': /* -n number: # stdin args */
305 * -n number: # stdin args.
306 * number *is* required here:
308 if ((N_ARGS
= atoi(optarg
)) <= 0) {
309 ermsg(_("#args must be positive int: %s\n"),
312 LEGAL
= DASHX
|| N_ARGS
== 1;
313 INSERT
= PER_LINE
= LINE_CONT
= FALSE
;
317 case 'P': /* -P maxprocs: # of child processses */
319 l
= strtoul(optarg
, &eptr
, 10);
320 if (*eptr
!= '\0' || errno
!= 0) {
321 ermsg(_("failed to parse maxprocs (-P): %s\n"),
327 * Come up with an upper bound that'll probably fit in
330 if (l
== 0 || l
> ((INT_MAX
/ sizeof (pid_t
) >> 1))) {
331 l
= INT_MAX
/ sizeof (pid_t
) >> 1;
336 case 's': /* -s size: set max size of each arg list */
337 BUFLIM
= atoi(optarg
);
338 if (BUFLIM
> BUFSIZE
|| BUFLIM
<= 0) {
339 ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
344 case 'x': /* -x: terminate if args > size limit */
345 DASHX
= LEGAL
= TRUE
;
350 * bad argument. complain and get ready to die.
359 * if anything called ermsg(), something screwed up, so
368 * we're finished handling xargs's options, so now pick up
369 * the command name (if any), and it's options.
373 mac
-= optind
; /* dec arg count by what we've processed */
374 mav
+= optind
; /* inc to current mav */
376 procs
= calloc(MAXPROCS
, sizeof (pid_t
));
382 if (mac
<= 0) { /* if there're no more args to process, */
383 cmdname
= "/usr/bin/echo"; /* our default command */
384 *ARGV
++ = addarg(cmdname
); /* use the default cmd. */
385 } else { /* otherwise keep parsing rest of the string. */
387 * note that we can't use getopts(3C), and *must* parse
388 * this by hand, as we don't know apriori what options the
391 cmdname
= *mav
; /* get the command name */
394 /* pick up the remaining args from the command line: */
395 while ((OK
== TRUE
) && (mac
-- > 0)) {
397 * while we haven't crapped out, and there's
400 if (INSERT
&& ! ERR
) {
401 if (strstr(*mav
, INSPAT
) != NULL
) {
402 if (++n_inserts
> MAXINSERTS
) {
403 ermsg(_("too many args "
404 "with %s\n"), INSPAT
);
407 psave
->p_ARGV
= ARGV
;
408 (psave
++)->p_skel
= *mav
;
411 *ARGV
++ = addarg(*mav
++);
415 /* pick up args from standard input */
428 while (MORE
|| (lastarg
[0] != '\0')) {
431 if (*lastarg
!= '\0') {
432 arg
= strcpy(next
, lastarg
);
434 } else if ((arg
= getarg(next
)) == NULL
) {
442 /* Inserts are handled specially later. */
443 if ((n_inserts
== 0) && (linesize
>= BUFLIM
)) {
445 * Legal indicates hard fail if the list is
446 * truncated due to size. So fail, or if we
447 * cannot create any list because it would be
450 if (LEGAL
|| N_args
== 0) {
458 * Otherwise just save argument for later.
460 (void) strcpy(lastarg
, arg
);
468 if ((PER_LINE
&& (N_lines
>= PER_LINE
)) ||
469 (N_ARGS
&& (N_args
>= N_ARGS
))) {
474 if ((ARGV
- arglist
) == MAXARGS
) {
481 /* Reached the end with no more work. */
485 /* insert arg if requested */
487 if (!ERR
&& INSERT
) {
492 for (psave
= saveargv
; ++j
<= n_inserts
; ++psave
) {
502 * if we've done any insertions, re-calculate the
503 * linesize. bomb out if we've exceeded our length.
506 for (ARGV
= arglist
; *ARGV
!= NULL
; ARGV
++) {
507 linesize
+= strlen(*ARGV
) + 1;
509 if (linesize
>= BUFLIM
) {
521 (PER_LINE
&& N_lines
== 0 || N_ARGS
&& N_args
== 0))
524 j
= TRACE
? echoargs() : TRUE
;
527 * for xcu4, all invocations of cmdname must
528 * return 0, in order for us to return 0.
529 * so if we have a non-zero status here,
532 (void) lcall(cmdname
, arglist
);
543 * if exitstat was set, to match XCU4 complience,
544 * return that value, otherwise, return 1.
546 return (exitstat
? exitstat
: 1);
552 linesize
+= (strlen(arg
) + 1);
558 store_str(char **buffer
, char *str
, size_t len
)
560 (void) memcpy(*buffer
, str
, len
);
561 (*buffer
)[len
] = '\0';
571 char mbc
[MB_LEN_MAX
];
583 c
= getwchr(mbc
, &len
);
585 if (((arg
- xarg
) + len
) > BUFLIM
) {
586 EMSG2(ARG2LONG
, BUFLIM
);
595 store_str(&arg
, mbc
, len
);
599 * NB: Some other versions rip off all of the trailing
600 * blanks. The spec only claims that this should
601 * be done for a single blank. We follow the spec.
603 if (LINE_CONT
&& iswctype(last
, blank
)) {
611 case WEOF
: /* Note WEOF == EOF */
628 if (ZERO
|| escape
|| (inquote
== 1)) {
629 /* treat it literally */
631 store_str(&arg
, mbc
, len
);
633 } else if (inquote
== 2) {
634 /* terminating double quote */
638 /* starting quoted string */
644 if (ZERO
|| escape
|| (inquote
== 2)) {
645 /* treat it literally */
647 store_str(&arg
, mbc
, len
);
649 } else if (inquote
== 1) {
650 /* terminating single quote */
654 /* starting quoted string */
661 * Any unquoted character can be escaped by
662 * preceding it with a backslash.
664 if (ZERO
|| inquote
|| escape
) {
666 store_str(&arg
, mbc
, len
);
673 /* most times we will just want to store it */
674 if (inquote
|| escape
|| ZERO
|| !iswctype(c
, blank
)) {
676 store_str(&arg
, mbc
, len
);
679 if (EAT_LEAD
&& last
== 0) {
680 c
= 0; /* Roll it back */
684 store_str(&arg
, mbc
, len
);
688 /* unquoted blank without special handling */
693 * At this point we are processing a complete argument.
695 if (strcmp(xarg
, LEOF
) == 0 && *LEOF
!= '\0') {
707 return (xarg
[0] == '\0' ? NULL
: xarg
);
711 * ermsg(): print out an error message, and indicate failure globally.
713 * Assumes that message has already been gettext()'d. It would be
714 * nice if we could just do the gettext() here, but we can't, since
715 * since xgettext(1M) wouldn't be able to pick up our error message.
719 ermsg(char *messages
, ...)
723 va_start(ap
, messages
);
725 (void) fprintf(stderr
, "xargs: ");
726 (void) vfprintf(stderr
, messages
, ap
);
736 char **tanarg
; /* tmp ptr */
738 char reply
[LINE_MAX
];
740 tanarg
= anarg
= arglist
-1;
743 * write out each argument, separated by a space. the tanarg
744 * nonsense is for xcu4 testsuite compliance - so that an
745 * extra space isn't echoed after the last argument.
747 while (*++anarg
) { /* while there's an argument */
748 ++tanarg
; /* follow anarg */
749 (void) write(2, *anarg
, strlen(*anarg
));
751 if (*++tanarg
) { /* if there's another argument: */
752 (void) write(2, " ", 1); /* add a space */
753 --tanarg
; /* reset back to anarg */
757 (void) write(2, "\n", 1);
761 (void) write(2, "?...", 4); /* ask the user for input */
763 for (i
= 0; i
< LINE_MAX
&& read(PROMPT
, &reply
[i
], 1) > 0; i
++) {
764 if (reply
[i
] == '\n') {
772 /* flush remainder of line if necessary */
776 while ((read(PROMPT
, &bitbucket
, 1) > 0) && (bitbucket
!= '\n'))
780 return (yes_check(reply
));
785 insert(char *pattern
, char *subst
)
787 static char buffer
[MAXSBUF
+1];
794 ipatlen
= strlen(INSPAT
) - 1;
797 bufend
= &buffer
[MAXSBUF
];
800 if (strncmp(pat
, INSPAT
, ipatlen
+ 1) == 0) {
801 if (pbuf
+ len
>= bufend
) {
804 (void) strcpy(pbuf
, subst
);
819 ermsg(gettext("Maximum argument size with insertion via %s's "
820 "exceeded\n"), INSPAT
);
828 addibuf(struct inserts
*p
)
830 char *newarg
, *skel
, *sub
;
835 newarg
= insert(skel
, sub
);
839 l
= strlen(newarg
) + 1;
840 if ((ibufsize
+= l
) > MAXIBUF
) {
844 (void) strcpy(p_ibuf
, newarg
);
845 *(p
->p_ARGV
) = p_ibuf
;
851 * getwchr(): get the next wide character.
853 * we get the next character from stdin. This returns WEOF if no
854 * character is present. If ZERO is set, it gets a single byte instead
858 getwchr(char *mbc
, size_t *sz
)
865 while (i
< MB_CUR_MAX
) {
867 if ((c
= fgetc(stdin
)) == EOF
) {
870 /* TRUE EOF has been reached */
875 * We have some characters in our buffer still so it
876 * must be an invalid character right before EOF.
882 /* If this succeeds then we are done */
887 if (mbtowc(&wch
, mbc
, i
) != -1) {
889 return ((wint_t)wch
);
894 * We have now encountered an illegal character sequence.
895 * There is nothing much we can do at this point but
896 * return an error. If we attempt to recover we may in fact
897 * return garbage as arguments, from the customer's point
898 * of view. After all what if they are feeding us a file
899 * generated in another locale?
909 lcall(char *sub
, char **subargs
)
915 switch (child
= forkx(FORK_NOSIGCHLD
)) {
919 * Note, if we have used up all of our slots, then this
920 * call may end up blocking.
925 (void) execvp(sub
, subargs
);
932 if (errno
!= EAGAIN
&& retry
++ < FORK_RETRY
) {
942 * Return the index of child in the procs array.
945 procs_find(pid_t child
)
949 for (i
= 0; i
< MAXPROCS
; i
++) {
950 if (procs
[i
] == child
) {
959 procs_store(pid_t child
)
973 procs_delete(pid_t child
)
977 i
= procs_find(child
);
989 procs_waitpid(boolean_t blocking
, int *stat_loc
)
1004 while ((child
= waitpid((pid_t
)-1, stat_loc
, options
)) > 0) {
1005 if (procs_delete(child
)) {
1014 procs_wait(boolean_t blocking
)
1020 * If we currently have filled all of our slots, then we need to block
1021 * further execution.
1023 if (n_procs
>= MAXPROCS
)
1025 while ((child
= procs_waitpid(blocking
, &stat_loc
)) > 0) {
1026 if (WIFSIGNALED(stat_loc
)) {
1027 EMSG2(CHILDSIG
, WTERMSIG(stat_loc
));
1030 } else if ((WEXITSTATUS(stat_loc
) & 0377) == 0377) {
1035 exitstat
|= WEXITSTATUS(stat_loc
);
1039 if (child
== (pid_t
)(-1) && errno
!= ECHILD
) {
1056 * parseargs(): modify the args
1057 * since the -e, -i and -l flags all take optional subarguments,
1058 * and getopts(3C) is clueless about this nonsense, we change the
1059 * our local argument count and strings to separate this out,
1060 * and make it easier to handle via getopts(3c).
1064 * -Estr -> "-E "str"
1066 * -irep -> "-i "rep"
1070 * since the -e, -i and -l flags all take optional subarguments,
1073 parseargs(int ac
, char **av
)
1075 int i
; /* current argument */
1076 int cflag
; /* 0 = not processing cmd arg */
1078 if ((mav
= malloc((ac
* 2 + 1) * sizeof (char *))) == NULL
) {
1083 /* for each argument, see if we need to change things: */
1084 for (i
= mac
= cflag
= 0; (av
[i
] != NULL
) && i
< ac
; i
++, mac
++) {
1085 if ((mav
[mac
] = strdup(av
[i
])) == NULL
) {
1090 /* -- has been found or argument list is fully processes */
1095 * if we're doing special processing, and we've got a flag
1097 else if ((av
[i
][0] == '-') && (av
[i
][1] != '\0')) {
1102 def
= ""; /* -e with no arg turns off eof */
1103 goto process_special
;
1106 goto process_special
;
1111 * if there's no sub-option, we *must* add
1112 * a default one. this is because xargs must
1113 * be able to distinguish between a valid
1114 * suboption, and a command name.
1116 if (av
[i
][2] == '\0') {
1117 mav
[++mac
] = strdup(def
);
1119 /* clear out our version: */
1121 mav
[++mac
] = strdup(&av
[i
][2]);
1123 if (mav
[mac
] == NULL
) {
1129 /* flags with required subarguments: */
1132 * there are two separate cases here. either the
1133 * flag can have the normal XCU4 handling
1134 * (of the form: -X subargument); or it can have
1135 * the old solaris 2.[0-4] handling (of the
1136 * form: -Xsubargument). in order to maintain
1137 * backwards compatibility, we must support the
1138 * latter case. we handle the latter possibility
1139 * first so both the old solaris way of handling
1140 * and the new XCU4 way of handling things are allowed.
1142 case 'n': /* FALLTHROUGH */
1143 case 'P': /* FALLTHROUGH */
1144 case 's': /* FALLTHROUGH */
1145 case 'E': /* FALLTHROUGH */
1146 case 'I': /* FALLTHROUGH */
1149 * if the second character isn't null, then
1150 * the user has specified the old syntax.
1151 * we move the subargument into our
1152 * mod'd argument list.
1154 if (av
[i
][2] != '\0') {
1155 /* first clean things up: */
1158 /* now add the separation: */
1159 ++mac
; /* inc to next mod'd arg */
1160 if ((mav
[mac
] = strdup(&av
[i
][2])) ==
1170 if (av
[i
] == NULL
) {
1174 if ((mav
[mac
] = strdup(av
[i
])) == NULL
) {
1190 * here we've hit the cmd argument. so
1191 * we'll stop special processing, as the
1192 * cmd may have a "-i" etc., argument,
1193 * and we don't want to add a "" to it.
1198 } else if (i
> 0) { /* if we're not the 1st arg */
1200 * if it's not a flag, then it *must* be the cmd.
1201 * set cflag, so we don't mishandle the -[eil] flags.