xargs: add some long options for GNU compatibility

These are low-effort to add, so let's just do it.

Reported by:	"LukeShu" on Hacker News (-r / --no-run-if-empty)
MFC after:	1 week
This commit is contained in:
Kyle Evans 2020-09-21 17:06:36 +00:00
parent 895a7e604d
commit dc94083273
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=365954
2 changed files with 27 additions and 10 deletions

View File

@ -33,7 +33,7 @@
.\" $FreeBSD$ .\" $FreeBSD$
.\" $xMach: xargs.1,v 1.2 2002/02/23 05:23:37 tim Exp $ .\" $xMach: xargs.1,v 1.2 2002/02/23 05:23:37 tim Exp $
.\" .\"
.Dd June 11, 2020 .Dd September 21, 2020
.Dt XARGS 1 .Dt XARGS 1
.Os .Os
.Sh NAME .Sh NAME
@ -84,7 +84,7 @@ Any single character, including newlines, may be escaped by a backslash.
.Pp .Pp
The options are as follows: The options are as follows:
.Bl -tag -width indent .Bl -tag -width indent
.It Fl 0 .It Fl 0 , Fl -null
Change Change
.Nm .Nm
to expect NUL to expect NUL
@ -175,7 +175,7 @@ If EOF is reached and fewer lines have been read than
then then
.Ar utility .Ar utility
will be called with the available lines. will be called with the available lines.
.It Fl n Ar number .It Fl n Ar number , Fl -max-args= Ns Ar number
Set the maximum number of arguments taken from standard input for each Set the maximum number of arguments taken from standard input for each
invocation of invocation of
.Ar utility . .Ar utility .
@ -201,7 +201,7 @@ in the child process before executing the command.
This is useful if you want This is useful if you want
.Nm .Nm
to run an interactive application. to run an interactive application.
.It Fl P Ar maxprocs .It Fl P Ar maxprocs , Fl -max-procs= Ns Ar maxprocs
Parallel mode: run at most Parallel mode: run at most
.Ar maxprocs .Ar maxprocs
invocations of invocations of
@ -212,7 +212,7 @@ If
is set to 0, is set to 0,
.Nm .Nm
will run as many processes as possible. will run as many processes as possible.
.It Fl p .It Fl p , Fl -interactive
Echo each command to be executed and ask the user whether it should be Echo each command to be executed and ask the user whether it should be
executed. executed.
An affirmative response, An affirmative response,
@ -221,7 +221,7 @@ in the POSIX locale,
causes the command to be executed, any other response causes it to be causes the command to be executed, any other response causes it to be
skipped. skipped.
No commands are executed if the process is not attached to a terminal. No commands are executed if the process is not attached to a terminal.
.It Fl r .It Fl r , Fl -no-run-if-empty
Compatibility with GNU Compatibility with GNU
.Nm . .Nm .
The GNU version of The GNU version of
@ -263,7 +263,7 @@ can use for replacements.
The default for The default for
.Ar replsize .Ar replsize
is 255. is 255.
.It Fl s Ar size .It Fl s Ar size , Fl -max-chars= Ns Ar size
Set the maximum number of bytes for the command line length provided to Set the maximum number of bytes for the command line length provided to
.Ar utility . .Ar utility .
The sum of the length of the utility name, the arguments passed to The sum of the length of the utility name, the arguments passed to
@ -277,10 +277,10 @@ The current default value for
is is
.Dv ARG_MAX .Dv ARG_MAX
- 4096. - 4096.
.It Fl t .It Fl t , Fl -verbose
Echo the command to be executed to standard error immediately before it Echo the command to be executed to standard error immediately before it
is executed. is executed.
.It Fl x .It Fl x , Fl -exit
Force Force
.Nm .Nm
to terminate immediately if a command line containing to terminate immediately if a command line containing

View File

@ -56,6 +56,7 @@ __FBSDID("$FreeBSD$");
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <getopt.h>
#include <langinfo.h> #include <langinfo.h>
#include <locale.h> #include <locale.h>
#include <paths.h> #include <paths.h>
@ -98,6 +99,22 @@ static volatile int childerr;
extern char **environ; extern char **environ;
static const char *optstr = "+0E:I:J:L:n:oP:pR:S:s:rtx";
static const struct option long_options[] =
{
{"exit", no_argument, NULL, 'x'},
{"interactive", no_argument, NULL, 'p'},
{"max-args", required_argument, NULL, 'n'},
{"max-chars", required_argument, NULL, 's'},
{"max-procs", required_argument, NULL, 'P'},
{"no-run-if-empty", no_argument, NULL, 'r'},
{"null", no_argument, NULL, '0'},
{"verbose", no_argument, NULL, 't'},
{NULL, no_argument, NULL, 0},
};
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
{ {
@ -137,7 +154,7 @@ main(int argc, char *argv[])
nline -= strlen(*ep++) + 1 + sizeof(*ep); nline -= strlen(*ep++) + 1 + sizeof(*ep);
} }
maxprocs = 1; maxprocs = 1;
while ((ch = getopt(argc, argv, "0E:I:J:L:n:oP:pR:S:s:rtx")) != -1) while ((ch = getopt_long(argc, argv, optstr, long_options, NULL)) != -1)
switch (ch) { switch (ch) {
case 'E': case 'E':
eofstr = optarg; eofstr = optarg;