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 2012 DEY Storage Systems, Inc. All rights reserved.
24 * Portions of this file developed by DEY Storage Systems, Inc. are licensed
25 * under the terms of the Common Development and Distribution License (CDDL)
26 * version 1.0 only. The use of subsequent versions of the License are
27 * is specifically prohibited unless those terms are not in conflict with
28 * version 1.0 of the License. You can find this license on-line at
29 * http://www.illumos.org/license/CDDL
32 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
33 * Use is subject to license terms.
36 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
37 /* All Rights Reserved */
41 #include <sys/types.h>
56 #include "getresponse.h"
65 #define BUFSIZE LINE_MAX
67 #define INSPAT_STR "{}" /* default replstr string for -[Ii] */
70 #define QBUF_STARTLEN 255 /* start size of growable string buffer */
71 #define QBUF_INC 100 /* how much to grow a growable string by */
73 /* We use these macros to help make formatting look "consistent" */
74 #define EMSG(s) ermsg(gettext(s "\n"))
75 #define EMSG2(s, a) ermsg(gettext(s "\n"), a)
76 #define PERR(s) perror(gettext("xargs: " s))
78 /* Some common error messages */
80 #define LIST2LONG "Argument list too long"
81 #define ARG2LONG "A single argument was greater than %d bytes"
82 #define MALLOCFAIL "Memory allocation failure"
83 #define CORRUPTFILE "Corrupt input file"
84 #define WAITFAIL "Wait failure"
85 #define CHILDSIG "Child killed with signal %d"
86 #define CHILDFAIL "Command could not continue processing data"
87 #define FORKFAIL "Could not fork child"
88 #define EXECFAIL "Could not exec command"
89 #define MISSQUOTE "Missing quote"
90 #define BADESCAPE "Incomplete escape"
91 #define IBUFOVERFLOW "Insert buffer overflow"
93 #define _(x) gettext(x)
95 static wctype_t blank
;
96 static char *arglist
[MAXARGS
+1];
97 static char argbuf
[BUFSIZE
* 2 + 1];
98 static char lastarg
[BUFSIZE
+ 1];
99 static char **ARGV
= arglist
;
100 static char *LEOF
= "_";
101 static char *INSPAT
= INSPAT_STR
;
102 static char ins_buf
[MAXIBUF
];
105 static struct inserts
{
106 char **p_ARGV
; /* where to put newarg ptr in arg list */
107 char *p_skel
; /* ptr to arg template */
108 } saveargv
[MAXINSERTS
];
110 static int PROMPT
= -1;
111 static int BUFLIM
= BUFSIZE
;
112 static int N_ARGS
= 0;
113 static int N_args
= 0;
114 static int N_lines
= 0;
115 static int DASHX
= FALSE
;
116 static int MORE
= TRUE
;
117 static int PER_LINE
= FALSE
;
118 static int ERR
= FALSE
;
119 static int OK
= TRUE
;
120 static int LEGAL
= FALSE
;
121 static int TRACE
= FALSE
;
122 static int INSERT
= FALSE
;
123 static int ZERO
= FALSE
;
124 static int linesize
= 0;
125 static int ibufsize
= 0;
126 static int exitstat
= 0; /* our exit status */
127 static int mac
; /* modified argc, after parsing */
128 static char **mav
; /* modified argv, after parsing */
129 static int n_inserts
; /* # of insertions. */
131 /* our usage message: */
132 #define USAGEMSG "Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] "\
133 "[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\
136 static int echoargs();
137 static wint_t getwchr(char *, size_t *);
138 static int lcall(char *sub
, char **subargs
);
139 static void addibuf(struct inserts
*p
);
140 static void ermsg(char *messages
, ...);
141 static char *addarg(char *arg
);
142 static void store_str(char **, char *, size_t);
143 static char *getarg(char *);
144 static char *insert(char *pattern
, char *subst
);
146 static void parseargs();
149 main(int argc
, char **argv
)
152 struct inserts
*psave
;
155 char *cmdname
, **initlist
;
160 blank
= wctype("blank");
163 (void) setlocale(LC_ALL
, "");
164 #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
165 #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
167 (void) textdomain(TEXT_DOMAIN
);
168 if (init_yes() < 0) {
169 ermsg(_(ERR_MSG_INIT_YES
), strerror(errno
));
173 parseargs(argc
, argv
);
175 /* handling all of xargs arguments: */
176 while ((c
= getopt(mac
, mav
, "0tpe:E:I:i:L:l:n:s:x")) != EOF
) {
182 case 't': /* -t: turn trace mode on */
186 case 'p': /* -p: turn on prompt mode. */
187 if ((PROMPT
= open("/dev/tty", O_RDONLY
)) == -1) {
188 PERR("can't read from tty for -p");
196 * -e[eofstr]: set/disable end-of-file.
197 * N.B. that an argument *isn't* required here; but
198 * parseargs forced an argument if not was given. The
199 * forced argument is the default...
201 LEOF
= optarg
; /* can be empty */
206 * -E eofstr: change end-of-file string.
207 * eofstr *is* required here, but can be empty:
213 /* -I replstr: Insert mode. replstr *is* required. */
214 INSERT
= PER_LINE
= LEGAL
= TRUE
;
217 if (*optarg
== '\0') {
218 ermsg(_("Option requires an argument: -%c\n"),
225 * -i [replstr]: insert mode, with *optional* replstr.
226 * N.B. that an argument *isn't* required here; if
227 * it's not given, then the string INSPAT_STR will
230 * Since getopts(3C) doesn't handle the case of an
231 * optional variable argument at all, we have to
232 * parse this by hand:
235 INSERT
= PER_LINE
= LEGAL
= TRUE
;
237 if ((optarg
!= NULL
) && (*optarg
!= '\0')) {
241 * here, there is no next argument. so
242 * we reset INSPAT to the INSPAT_STR.
243 * we *have* to do this, as -i/I may have
244 * been given previously, and XCU4 requires
245 * that only "the last one specified takes
254 * -L number: # of times cmd is executed
255 * number *is* required here:
260 if ((PER_LINE
= atoi(optarg
)) <= 0) {
261 ermsg(_("#lines must be positive int: %s\n"),
268 * -l [number]: # of times cmd is executed
269 * N.B. that an argument *isn't* required here; if
270 * it's not given, then 1 is assumed.
272 * parseargs handles the optional arg processing.
275 PER_LINE
= LEGAL
= TRUE
; /* initialization */
279 if ((optarg
!= NULL
) && (*optarg
!= '\0')) {
280 if ((PER_LINE
= atoi(optarg
)) <= 0)
285 case 'n': /* -n number: # stdin args */
287 * -n number: # stdin args.
288 * number *is* required here:
290 if ((N_ARGS
= atoi(optarg
)) <= 0) {
291 ermsg(_("#args must be positive int: %s\n"),
294 LEGAL
= DASHX
|| N_ARGS
== 1;
295 INSERT
= PER_LINE
= FALSE
;
299 case 's': /* -s size: set max size of each arg list */
300 BUFLIM
= atoi(optarg
);
301 if (BUFLIM
> BUFSIZE
|| BUFLIM
<= 0) {
302 ermsg(_("0 < max-cmd-line-size <= %d: %s\n"),
307 case 'x': /* -x: terminate if args > size limit */
308 DASHX
= LEGAL
= TRUE
;
313 * bad argument. complain and get ready to die.
322 * if anything called ermsg(), something screwed up, so
331 * we're finished handling xargs's options, so now pick up
332 * the command name (if any), and it's options.
336 mac
-= optind
; /* dec arg count by what we've processed */
337 mav
+= optind
; /* inc to current mav */
339 if (mac
<= 0) { /* if there're no more args to process, */
340 cmdname
= "/usr/bin/echo"; /* our default command */
341 *ARGV
++ = addarg(cmdname
); /* use the default cmd. */
342 } else { /* otherwise keep parsing rest of the string. */
344 * note that we can't use getopts(3C), and *must* parse
345 * this by hand, as we don't know apriori what options the
348 cmdname
= *mav
; /* get the command name */
351 /* pick up the remaining args from the command line: */
352 while ((OK
== TRUE
) && (mac
-- > 0)) {
354 * while we haven't crapped out, and there's
357 if (INSERT
&& ! ERR
) {
358 if (strstr(*mav
, INSPAT
) != NULL
) {
359 if (++n_inserts
> MAXINSERTS
) {
360 ermsg(_("too many args "
361 "with %s\n"), INSPAT
);
364 psave
->p_ARGV
= ARGV
;
365 (psave
++)->p_skel
= *mav
;
368 *ARGV
++ = addarg(*mav
++);
372 /* pick up args from standard input */
385 while (MORE
|| (lastarg
[0] != '\0')) {
388 if (*lastarg
!= '\0') {
389 arg
= strcpy(next
, lastarg
);
391 } else if ((arg
= getarg(next
)) == NULL
) {
399 /* Inserts are handled specially later. */
400 if ((n_inserts
== 0) && (linesize
>= BUFLIM
)) {
402 * Legal indicates hard fail if the list is
403 * truncated due to size. So fail, or if we
404 * cannot create any list because it would be
407 if (LEGAL
|| N_args
== 0) {
414 * Otherwise just save argument for later.
416 (void) strcpy(lastarg
, arg
);
424 if ((PER_LINE
&& N_lines
>= PER_LINE
) ||
425 (N_ARGS
&& (N_args
) >= N_ARGS
)) {
430 if ((ARGV
- arglist
) == MAXARGS
) {
437 /* Reached the end with no more work. */
441 /* insert arg if requested */
443 if (!ERR
&& INSERT
) {
448 for (psave
= saveargv
; ++j
<= n_inserts
; ++psave
) {
458 * if we've done any insertions, re-calculate the
459 * linesize. bomb out if we've exceeded our length.
462 for (ARGV
= arglist
; *ARGV
!= NULL
; ARGV
++) {
463 linesize
+= strlen(*ARGV
) + 1;
465 if (linesize
>= BUFLIM
) {
476 (PER_LINE
&& N_lines
== 0 || N_ARGS
&& N_args
== 0))
479 j
= TRACE
? echoargs() : TRUE
;
482 * for xcu4, all invocations of cmdname must
483 * return 0, in order for us to return 0.
484 * so if we have a non-zero status here,
487 exitstat
|= lcall(cmdname
, arglist
);
496 * if exitstat was set, to match XCU4 complience,
497 * return that value, otherwise, return 1.
499 return (exitstat
? exitstat
: 1);
505 linesize
+= (strlen(arg
) + 1);
511 store_str(char **buffer
, char *str
, size_t len
)
513 (void) memcpy(*buffer
, str
, len
);
514 (*buffer
)[len
] = '\0';
524 char mbc
[MB_LEN_MAX
];
534 c
= getwchr(mbc
, &len
);
536 if (((arg
- xarg
) + len
) > BUFLIM
) {
537 EMSG2(ARG2LONG
, BUFLIM
);
546 store_str(&arg
, mbc
, len
);
552 case WEOF
: /* Note WEOF == EOF */
569 if (ZERO
|| escape
|| (inquote
== 1)) {
570 /* treat it literally */
572 store_str(&arg
, mbc
, len
);
574 } else if (inquote
== 2) {
575 /* terminating double quote */
579 /* starting quoted string */
585 if (ZERO
|| escape
|| (inquote
== 2)) {
586 /* treat it literally */
588 store_str(&arg
, mbc
, len
);
590 } else if (inquote
== 1) {
591 /* terminating single quote */
595 /* starting quoted string */
602 * Any unquoted character can be escaped by
603 * preceding it with a backslash.
605 if (ZERO
|| inquote
|| escape
) {
607 store_str(&arg
, mbc
, len
);
614 /* most times we will just want to store it */
615 if (inquote
|| escape
|| ZERO
|| !iswctype(c
, blank
)) {
617 store_str(&arg
, mbc
, len
);
625 * At this point we are processing a complete argument.
627 if (strcmp(xarg
, LEOF
) == 0 && *LEOF
!= '\0') {
639 return (xarg
[0] == '\0' ? NULL
: xarg
);
643 * ermsg(): print out an error message, and indicate failure globally.
645 * Assumes that message has already been gettext()'d. It would be
646 * nice if we could just do the gettext() here, but we can't, since
647 * since xgettext(1M) wouldn't be able to pick up our error message.
651 ermsg(char *messages
, ...)
655 va_start(ap
, messages
);
657 (void) fprintf(stderr
, "xargs: ");
658 (void) vfprintf(stderr
, messages
, ap
);
668 char **tanarg
; /* tmp ptr */
670 char reply
[LINE_MAX
];
672 tanarg
= anarg
= arglist
-1;
675 * write out each argument, separated by a space. the tanarg
676 * nonsense is for xcu4 testsuite compliance - so that an
677 * extra space isn't echoed after the last argument.
679 while (*++anarg
) { /* while there's an argument */
680 ++tanarg
; /* follow anarg */
681 (void) write(2, *anarg
, strlen(*anarg
));
683 if (*++tanarg
) { /* if there's another argument: */
684 (void) write(2, " ", 1); /* add a space */
685 --tanarg
; /* reset back to anarg */
689 (void) write(2, "\n", 1);
693 (void) write(2, "?...", 4); /* ask the user for input */
695 for (i
= 0; i
< LINE_MAX
&& read(PROMPT
, &reply
[i
], 1) > 0; i
++) {
696 if (reply
[i
] == '\n') {
704 /* flush remainder of line if necessary */
708 while ((read(PROMPT
, &bitbucket
, 1) > 0) && (bitbucket
!= '\n'))
712 return (yes_check(reply
));
717 insert(char *pattern
, char *subst
)
719 static char buffer
[MAXSBUF
+1];
726 ipatlen
= strlen(INSPAT
) - 1;
729 bufend
= &buffer
[MAXSBUF
];
732 if (strncmp(pat
, INSPAT
, ipatlen
) == 0) {
733 if (pbuf
+ len
>= bufend
) {
736 (void) strcpy(pbuf
, subst
);
751 ermsg(gettext("Maximum argument size with insertion via %s's "
752 "exceeded\n"), INSPAT
);
760 addibuf(struct inserts
*p
)
762 char *newarg
, *skel
, *sub
;
767 newarg
= insert(skel
, sub
);
771 l
= strlen(newarg
) + 1;
772 if ((ibufsize
+= l
) > MAXIBUF
) {
776 (void) strcpy(p_ibuf
, newarg
);
777 *(p
->p_ARGV
) = p_ibuf
;
783 * getwchr(): get the next wide character.
785 * we get the next character from stdin. This returns WEOF if no
786 * character is present. If ZERO is set, it gets a single byte instead
790 getwchr(char *mbc
, size_t *sz
)
797 while (i
< MB_CUR_MAX
) {
799 if ((c
= fgetc(stdin
)) == EOF
) {
802 /* TRUE EOF has been reached */
807 * We have some characters in our buffer still so it
808 * must be an invalid character right before EOF.
814 /* If this succeeds then we are done */
819 if (mbtowc(&wch
, mbc
, i
) != -1) {
821 return ((wint_t)wch
);
826 * We have now encountered an illegal character sequence.
827 * There is nothing much we can do at this point but
828 * return an error. If we attempt to recover we may in fact
829 * return garbage as arguments, from the customer's point
830 * of view. After all what if they are feeding us a file
831 * generated in another locale?
841 lcall(char *sub
, char **subargs
)
843 int retcode
, retry
= 0;
847 switch (child
= fork()) {
849 while ((iwait
= wait(&retcode
)) != child
&&
852 if (iwait
== (pid_t
)-1) {
857 if (WIFSIGNALED(retcode
)) {
858 EMSG2(CHILDSIG
, WTERMSIG(retcode
));
862 if ((WEXITSTATUS(retcode
) & 0377) == 0377) {
867 return (WEXITSTATUS(retcode
));
869 (void) execvp(sub
, subargs
);
876 if (errno
!= EAGAIN
&& retry
++ < FORK_RETRY
) {
896 * parseargs(): modify the args
897 * since the -e, -i and -l flags all take optional subarguments,
898 * and getopts(3C) is clueless about this nonsense, we change the
899 * our local argument count and strings to separate this out,
900 * and make it easier to handle via getopts(3c).
910 * since the -e, -i and -l flags all take optional subarguments,
913 parseargs(int ac
, char **av
)
915 int i
; /* current argument */
916 int cflag
; /* 0 = not processing cmd arg */
918 if ((mav
= malloc((ac
* 2 + 1) * sizeof (char *))) == NULL
) {
923 /* for each argument, see if we need to change things: */
924 for (i
= mac
= cflag
= 0; (av
[i
] != NULL
) && i
< ac
; i
++, mac
++) {
925 if ((mav
[mac
] = strdup(av
[i
])) == NULL
) {
930 /* -- has been found or argument list is fully processes */
935 * if we're doing special processing, and we've got a flag
937 else if ((av
[i
][0] == '-') && (av
[i
][1] != NULL
)) {
942 def
= ""; /* -e with no arg turns off eof */
943 goto process_special
;
946 goto process_special
;
951 * if there's no sub-option, we *must* add
952 * a default one. this is because xargs must
953 * be able to distinguish between a valid
954 * suboption, and a command name.
956 if (av
[i
][2] == NULL
) {
957 mav
[++mac
] = strdup(def
);
959 /* clear out our version: */
961 mav
[++mac
] = strdup(&av
[i
][2]);
963 if (mav
[mac
] == NULL
) {
969 /* flags with required subarguments: */
972 * there are two separate cases here. either the
973 * flag can have the normal XCU4 handling
974 * (of the form: -X subargument); or it can have
975 * the old solaris 2.[0-4] handling (of the
976 * form: -Xsubargument). in order to maintain
977 * backwards compatibility, we must support the
978 * latter case. we handle the latter possibility
979 * first so both the old solaris way of handling
980 * and the new XCU4 way of handling things are allowed.
982 case 'n': /* FALLTHROUGH */
983 case 's': /* FALLTHROUGH */
984 case 'E': /* FALLTHROUGH */
985 case 'I': /* FALLTHROUGH */
988 * if the second character isn't null, then
989 * the user has specified the old syntax.
990 * we move the subargument into our
991 * mod'd argument list.
993 if (av
[i
][2] != NULL
) {
994 /* first clean things up: */
997 /* now add the separation: */
998 ++mac
; /* inc to next mod'd arg */
999 if ((mav
[mac
] = strdup(&av
[i
][2])) ==
1009 if (av
[i
] == NULL
) {
1013 if ((mav
[mac
] = strdup(av
[i
])) == NULL
) {
1028 * here we've hit the cmd argument. so
1029 * we'll stop special processing, as the
1030 * cmd may have a "-i" etc., argument,
1031 * and we don't want to add a "" to it.
1036 } else if (i
> 0) { /* if we're not the 1st arg */
1038 * if it's not a flag, then it *must* be the cmd.
1039 * set cflag, so we don't mishandle the -[eil] flags.