xargs.c revision a035dc19b8f2c7a52a7e5af76c0727dcb4941ea3
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <limits.h>
#include <wchar.h>
#include <locale.h>
#include <langinfo.h>
#include <stropts.h>
#include <poll.h>
#include <errno.h>
#include <stdarg.h>
#include "getresponse.h"
#define HEAD 0
#define TAIL 1
#define FALSE 0
#define TRUE 1
#define MAXSBUF 255
#define MAXIBUF 512
#define MAXINSERTS 5
#define MAXARGS 255
#define FORK_RETRY 5
static char *lastarg = "";
static char *LEOF = "_";
static char *INSPAT = INSPAT_STR;
static char *p_ibuf;
static struct inserts {
char **p_ARGV; /* where to put newarg ptr in arg list */
char *p_skel; /* ptr to arg template */
} saveargv[MAXINSERTS];
static off_t file_offset = 0;
static int PROMPT = -1;
static int N_ARGS = 0;
static int N_args = 0;
static int N_lines = 0;
static int linesize = 0;
static int ibufsize = 0;
static int exitstat = 0; /* our exit status */
static int mac; /* modified argc, after parsing */
static char **mav; /* modified argv, after parsing */
static int n_inserts; /* # of insertions. */
static int inquote = 0; /* processing a quoted string */
static int save_index = 0;
/*
* the pio structure is used to save any pending input before the
* user replies to a prompt. the pending input is saved here,
* for the appropriate processing later.
*/
typedef struct pio {
char *start; /* starting addr of the buffer */
char *cur; /* ptr to current char in buf */
} pio;
/* our usage message: */
#define USAGEMSG "Usage: xargs: [-t] [-p] [-e[eofstr]] [-E eofstr] "\
"[-I replstr] [-i[replstr]] [-L #] [-l[#]] [-n # [-x]] [-s size] "\
"[cmd [args ...]]\n"
static int echoargs();
static int getchr(void);
static char *getarg();
static void usage();
static void parseargs();
static void saveinput();
int
{
int j;
int c;
int initsize;
/* initialization */
n_inserts = 0;
#if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
#endif
(void) textdomain(TEXT_DOMAIN);
if (init_yes() < 0) {
exit(1);
}
/* handling all of xargs arguments: */
switch (c) {
case 't': /* -t: turn trace mode on */
break;
case 'p': /* -p: turn on prompt mode. */
} else {
}
break;
case 'e':
/*
* N.B. that an argument *isn't* required here; but
* parseargs forced an argument if not was given. The
* forced argument is the default...
*/
break;
case 'E':
/*
* -E eofstr: change end-of-file string.
* eofstr *is* required here, but can be empty:
*/
break;
case 'I':
/* -I replstr: Insert mode. replstr *is* required. */
N_ARGS = 0;
if (*optarg == '\0') {
"Option requires an argument: -%c\n"), c);
}
break;
case 'i':
/*
* -i [replstr]: insert mode, with *optional* replstr.
* N.B. that an argument *isn't* required here; if
* it's not given, then the string INSPAT_STR will
* be assumed.
*
* Since getopts(3C) doesn't handle the case of an
* optional variable argument at all, we have to
* parse this by hand:
*/
N_ARGS = 0;
} else {
/*
* here, there is no next argument. so
* we reset INSPAT to the INSPAT_STR.
* we *have* to do this, as -i/I may have
* been given previously, and XCU4 requires
* that only "the last one specified takes
* effect".
*/
INSPAT = INSPAT_STR;
}
break;
case 'L':
/*
* -L number: # of times cmd is executed
* number *is* required here:
*/
N_ARGS = 0;
"int: %s\n"), optarg);
}
break;
case 'l':
/*
* -l [number]: # of times cmd is executed
* N.B. that an argument *isn't* required here; if
* it's not given, then 1 is assumed.
*
* parseargs handles the optional arg processing.
*/
N_ARGS = 0;
PER_LINE = 1;
}
break;
case 'n': /* -n number: # stdin args */
/*
* -n number: # stdin args.
* number *is* required here:
*/
"int: %s\n"), optarg);
} else {
}
break;
case 's': /* -s size: set max size of each arg list */
"0 < max-cmd-line-size <= %d: "
}
break;
case 'x': /* -x: terminate if args > size limit */
break;
default:
/*
* bad argument. complain and get ready to die.
*/
usage();
exit(2);
break;
}
}
/*
* if anything called ermsg(), something screwed up, so
* we exit early.
*/
usage();
exit(2);
}
/*
* we're finished handling xargs's options, so now pick up
* the command name (if any), and it's options.
*/
if (mac <= 0) { /* if there're no more args to process, */
} else { /* otherwise keep parsing rest of the string. */
/*
* note that we can't use getopts(3C), and *must* parse
* this by hand, as we don't know apriori what options the
* command will take.
*/
/* pick up the remaining args from the command line: */
/*
* while we haven't crapped out, and there's
* work to do:
*/
if (++n_inserts > MAXINSERTS) {
"with %s\n"), INSPAT);
}
}
}
}
}
/* pick up args from standard input */
N_args = 0;
N_lines = 0;
if (*lastarg) {
lastarg = "";
}
break;
}
}
/* there were no more args after filling arglist */
}
/* insert arg if requested */
}
/* no more input lines */
ARGV--;
j = ibufsize = 0;
if (ERR)
break;
}
}
*ARGV = 0;
if (n_inserts > 0) {
int t_ninserts;
/*
* if we've done any insertions, re-calculate the
* linesize. bomb out if we've exceeded our length.
*/
n_inserts = 0; /* inserts have been done */
linesize = 0; /* recalculate this */
/* for each current argument in the list: */
/* recalculate everything. */
}
}
}
}
/* exec command */
if (!ERR) {
if (!MORE &&
if (j) {
/*
* for xcu4, all invocations of cmdname must
* return 0, in order for us to return 0.
* so if we have a non-zero status here,
* quit immediately.
*/
continue;
}
}
}
if (OK) {
return (exitstat);
} else {
/*
* if exitstat was set, to match XCU4 complience,
* return that value, otherwise, return 1.
*/
}
}
static void
{
exit(1);
}
if (queued_data == NULL) {
queued_data = new;
} else {
}
}
} else {
file_offset -= len;
queued_data = new;
}
}
static char *
{
int oklen;
/*
* we skip this if there're inserts. we'll handle the
* argument counting after all the insertions have
* been done.
*/
if (n_inserts == 0) {
if (LEGAL) {
} else if (N_args > 1) {
N_args = 1;
} else {
"the max arglist size of %d characters\n"),
BUFLIM);
}
}
}
}
static char *
{
return (arg);
}
return ((char *)0);
}
/*
* store_wchr() : append a wchar_t to a char buffer, resize buffer if required.
*
* Given a pointer to the beginning of a string buffer, the length of the
* buffer and an offset indicating the next place to write within that
* buffer, the passed wchar_t will be appended to the buffer if there is
* enough space. If there is not enough space, an attempt to reallocate the
* buffer will be made and if successful the passed pointer and size will be
* updated to describe the reallocated block. Returns the new value for
* 'offset' (it will be incremented by the number of bytes written).
*/
static size_t
{
int bytes;
/*
* Make sure that there is enough room in the buffer to store the
* maximum length of c.
*/
/*
* Not enough room so attempt to reallocate. Add 'MB_CUR_MAX' to
* buffer length to ensure that there is always enough room to
* store 'c' if realloc succeeds, no matter what QBUF_INC is
* defined as.
*/
exit(1);
}
}
/* store bytes from wchar into buffer */
if (bytes == -1) {
/* char was invalid */
bytes = 1;
}
/* return new value for offset */
}
static char *
getarg()
{
int bytes;
wchar_t c;
char *arg;
char *retarg, *requeue_buf;
char mbc[MB_LEN_MAX];
;
if (c == '\0') {
return (0);
}
/*
* While we are reading in an argument, it is possible that we will
* reach the maximum length of the overflow buffer and we'll have to
* requeue what we have read so far. To handle this we allocate an
* initial buffer here which will keep an unprocessed copy of the data
* that we read in (this buffer will grow as required).
*/
exit(1);
}
/*
* Store the char that we have read before processing it in case
* the current argument needs to be requeued.
*/
requeue_offset, c);
/* Check for overflow the input buffer */
/*
* It's only an error if there are no Args in buffer
* already.
*/
return (0);
} else if (N_args == 0) {
lastarg = "";
"the max arglist size of %d characters\n"),
BUFSIZE);
return (0);
}
/*
* Otherwise we put back the current argument
* and use what we have collected so far...
*/
/* reset inquote because we have requeued the quotes */
inquote = 0;
return (NULL);
}
if (INSERT) {
if (bytes == -1) {
*next++ = (char)c;
} else {
}
continue;
}
/* skip over trailing whitespace till next arg */
(c != '\n') && (c != '\0'))
;
/*
* if there was space till end of line then the last
* character was really a newline...
*/
if (c == L'\n' || c == L'\0') {
ungetwchr(L'\n');
} else {
/* later code needs to know this was a space */
ungetwchr(c);
c = L' ';
}
goto end_arg;
}
switch (c) {
case L'\0':
case L'\n':
if (inquote) {
*next++ = '\0';
return (0);
}
N_lines++;
/* we finished without requeuing so free requeue_buf */
/* absorb the rest of the line */
if ((c != '\n') && (c != '\0'))
while (c = getwchr())
if ((c == '\n') || (c == '\0'))
break;
/*
* Encountered EOF string.
* Don't read any more lines.
*/
N_lines = 0;
}
return (0);
} else {
++N_args;
if ((PER_LINE &&
(c == '\0' || c == '\n')) ||
lastarg = "";
}
}
return (retarg);
}
case '"':
goto is_default;
inquote = 0;
else /* starting quoted string */
inquote = 2;
break;
case '\'':
goto is_default;
inquote = 0;
else /* starting quoted string */
inquote = 1;
break;
case L'\\':
/*
* Any unquoted character can be escaped by
* preceding it with a backslash.
*/
if (inquote == 0) {
c = getwchr();
/* store quoted char for potential requeueing */
&requeue_len, requeue_offset, c);
}
default:
*next++ = (char)c;
} else {
}
break;
}
}
}
/*
* ermsg(): print out an error message, and indicate failure globally.
*
* Assumes that message has already been gettext()'d. It would be
* nice if we could just do the gettext() here, but we can't, since
* since xgettext(1M) wouldn't be able to pick up our error message.
*/
/* PRINTFLIKE1 */
static void
{
}
static int
echoargs()
{
char **anarg;
char **tanarg; /* tmp ptr */
int i;
/*
* write out each argument, separated by a space. the tanarg
* nonsense is for xcu4 testsuite compliance - so that an
* extra space isn't echoed after the last argument.
*/
while (*++anarg) { /* while there's an argument */
++tanarg; /* follow anarg */
if (*++tanarg) { /* if there's another argument: */
--tanarg; /* reset back to anarg */
}
}
if (PROMPT == -1) {
return (TRUE);
}
/*
* at this point, there may be unexpected input pending on stdin,
* if one has used the -n flag. this presents a problem, because
* if we simply do a read(), we'll get the extra input, instead
* of our desired y/n input. so, we see if there's any extra
* input, and if there is, then we will store it.
*/
saveinput();
if (reply[i] == '\n') {
if (i == 0)
return (FALSE);
break;
}
}
reply[i] = 0;
/* flush remainder of line if necessary */
if (i == LINE_MAX) {
char bitbucket;
;
}
}
static char *
{
char *pat;
char *bufend;
char *pbuf;
while (*++pat) {
break;
} else {
}
} else {
break;
}
}
if (!*pat) {
*pbuf = '\0';
return (buffer);
} else {
"exceeded\n"), INSPAT);
return (0);
}
}
static void
{
int l;
if (ERR)
return;
}
p_ibuf += l;
}
}
/*
* getchr(): get the next character.
* description:
* we get the next character from pio.structure, if there's a character
* but still wanted to preserve the characters for later processing.
*
* otherwise we just get the character from stdin.
*/
static int
getchr(void)
{
char c;
do {
if (queued_data == NULL) {
char *buffer;
int len;
"xargs: Memory allocation failure"));
exit(1);
}
return (0);
if (len == -1) {
exit(1);
}
}
file_offset++;
queued_data = nxt;
}
} while (c == '\0');
return (c);
}
static wchar_t
getwchr(void)
{
int i;
for (i = 0; i < (int)MB_CUR_MAX; ) {
/* We have reached EOF */
if (i == 1) {
/* TRUE EOF has been reached */
return (NULL);
}
/*
* We have some characters in our buffer still so it
* must be an invalid character right before EOF.
*/
break;
}
/* If this succeeds then we are done */
return (wch);
}
/*
* We have now encountered an illegal character sequence.
* There is nothing much we can do at this point but
* return an error. If we attempt to recover we may in fact
* return garbage as arguments, from the customer's point
* of view. After all what if they are feeding us a file
* generated in another locale?
*/
exit(1);
/* NOTREACHED */
}
static void
{
char *buffer;
int bytes;
exit(1);
}
}
static int
{
for (; ; ) {
default:
;
exit(122);
/* NOTREACHED */
}
if (WIFSIGNALED(retcode)) {
exit(125);
/* NOTREACHED */
}
"processing data\n"));
exit(124);
/* NOTREACHED */
}
return (WEXITSTATUS(retcode));
case 0:
exit(126);
exit(127);
/* NOTREACHED */
case -1:
exit(123);
}
(void) sleep(1);
}
}
}
/*
* If `s2' is a substring of `s1' return the offset of the first
* occurrence of `s2' in `s1', else return -1.
*/
static int
{
int offset;
c = *s2;
while (*s1) {
if (*s1++ == c) {
s2++;
;
if (c == 0)
return (offset);
c = *s2;
}
}
return (-1);
}
static void
usage()
{
}
/*
* parseargs(): modify the args
* since the -e, -i and -l flags all take optional subarguments,
* and getopts(3C) is clueless about this nonsense, we change the
* our local argument count and strings to separate this out,
* and make it easier to handle via getopts(3c).
*
* -e -> "-e ""
* -e3 -> "-e "3"
* -Estr -> "-E "str"
* -i -> "-i "{}"
* -irep -> "-i "rep"
* -l -> "-i "1"
* -l10 -> "-i "10"
*
* since the -e, -i and -l flags all take optional subarguments,
*/
static void
{
int i; /* current argument */
int cflag; /* 0 = not processing cmd arg */
exit(1);
}
/* for each argument, see if we need to change things: */
exit(1);
}
/* -- has been found or argument list is fully processes */
if (cflag)
continue;
/*
* if we're doing special processing, and we've got a flag
*/
char *def;
switch (av[i][1]) {
case 'e':
goto process_special;
case 'i':
def = INSPAT_STR;
goto process_special;
case 'l':
def = "1";
/*
* if there's no sub-option, we *must* add
* a default one. this is because xargs must
* be able to distinguish between a valid
* suboption, and a command name.
*/
} else {
/* clear out our version: */
}
" allocation failure"));
exit(1);
}
break;
/* flags with required subarguments: */
/*
* there are two separate cases here. either the
* flag can have the normal XCU4 handling
* (of the form: -X subargument); or it can have
* the old solaris 2.[0-4] handling (of the
* form: -Xsubargument). in order to maintain
* backwards compatibility, we must support the
* latter case. we handle the latter possibility
* first so both the old solaris way of handling
* and the new XCU4 way of handling things are allowed.
*/
case 'n': /* FALLTHROUGH */
case 's': /* FALLTHROUGH */
case 'E': /* FALLTHROUGH */
case 'I': /* FALLTHROUGH */
case 'L':
/*
* if the second character isn't null, then
* the user has specified the old syntax.
* we move the subargument into our
* mod'd argument list.
*/
/* first clean things up: */
/* now add the separation: */
++mac; /* inc to next mod'd arg */
NULL) {
" allocation failure"));
exit(1);
}
break;
}
i++;
mac++;
return;
}
" allocation failure"));
exit(1);
}
break;
/* flags */
case 'p' :
case 't' :
case 'x' :
break;
case '-' :
default:
/*
* here we've hit the cmd argument. so
* we'll stop special processing, as the
* cmd may have a "-i" etc., argument,
* and we don't want to add a "" to it.
*/
cflag = 1;
break;
}
} else if (i > 0) { /* if we're not the 1st arg */
/*
* if it's not a flag, then it *must* be the cmd.
* set cflag, so we don't mishandle the -[eil] flags.
*/
cflag = 1;
}
}
}
/*
* saveinput(): pick up any pending input, so it can be processed later.
*
* description:
* the purpose of this routine is to allow us to handle the user
* typing in a 'y' or 'n', when there's existing characters already
* in stdin. this happens when one gives the "-n" option along with
* "-p". the problem occurs when the user first types in more arguments
* than specified by the -n number. echoargs() wants to read stdin
* in order to get the user's response, but if there's already stuff
* there, echoargs() won't read the proper character.
*
* the solution provided by this routine is to pick up all characters
* (if any), and store them for later processing.
*/
void
{
char *buffer; /* ptr to the floating data buffer */
/* if we're not in -p mode, skip */
if (PROMPT == -1) {
return;
}
/* now see if there's any activity pending: */
exit(1);
}
exit(1);
}
int len;
exit(1);
}
}
}