Implement -E eofstr for xargs(1)

This commit is contained in:
sin 2014-01-07 11:53:55 +00:00
parent 7a5369ae04
commit e27c55aec3
2 changed files with 15 additions and 4 deletions

View File

@ -4,8 +4,10 @@ xargs \- constuct argument list(s) and execute command
.SH SYNOPSIS .SH SYNOPSIS
.B xargs .B xargs
.RB [ \-r ] .RB [ \-r ]
.RB [ \-E
.IR eofstr ]
.RI [ cmd .RI [ cmd
.RI [arg... ] ] .IR [arg... ] ]
.SH DESCRIPTION .SH DESCRIPTION
xargs reads space, tab, newline and EOF delimited strings from stdin xargs reads space, tab, newline and EOF delimited strings from stdin
and executes the specified cmd with the strings as arguments. and executes the specified cmd with the strings as arguments.
@ -26,6 +28,9 @@ newlines, may be escaped by a backslash.
.BI \-r .BI \-r
Do not run the command if there are no arguments. Normally the command is Do not run the command if there are no arguments. Normally the command is
executed at least once even if there are no arguments. executed at least once even if there are no arguments.
.TP
.B \-E eofstr
Use eofstr as a logical EOF marker.
.SH EXIT STATUS .SH EXIT STATUS
xargs exits with one of the following values: xargs exits with one of the following values:

12
xargs.c
View File

@ -27,12 +27,13 @@ static char *argb;
static size_t argbsz = 1; static size_t argbsz = 1;
static size_t argbpos; static size_t argbpos;
static int nerrors = 0; static int nerrors = 0;
static char *eofstr;
static int rflag = 0; static int rflag = 0;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-r] [cmd [arg...]]\n", argv0); eprintf("usage: %s [-r] [-E eofstr] [cmd [arg...]]\n", argv0);
} }
int int
@ -46,6 +47,9 @@ main(int argc, char *argv[])
case 'r': case 'r':
rflag = 1; rflag = 1;
break; break;
case 'E':
eofstr = EARGF(usage());
break;
default: default:
usage(); usage();
} ARGEND; } ARGEND;
@ -186,9 +190,8 @@ poparg(void)
while ((ch = inputc()) != EOF) { while ((ch = inputc()) != EOF) {
switch (ch) { switch (ch) {
case ' ': case '\t': case '\n': case ' ': case '\t': case '\n':
fillbuf('\0');
deinputc(ch); deinputc(ch);
return argb; goto out;
case '\'': case '\'':
if (parsequote('\'') == -1) if (parsequote('\'') == -1)
enprintf(EXIT_FAILURE, enprintf(EXIT_FAILURE,
@ -209,8 +212,11 @@ poparg(void)
break; break;
} }
} }
out:
if (argbpos > 0) { if (argbpos > 0) {
fillbuf('\0'); fillbuf('\0');
if (eofstr && strcmp(argb, eofstr) == 0)
return NULL;
return argb; return argb;
} }
return NULL; return NULL;