MFC: r285552

Use strtonum(3) instead of atoi(3)

MFC: r286289
  Introduce -P0, creating as many concurrent processes as possible

Relnotes:	yes

MFC: r286461
  Fix regression in xargs -Px, add more regression tests

MFC: r287004
MFC: r287005
  Fix the racy xargs -P0 -n2 test added in r286289

PR:		199976
Submitted by:	Nikolai Lifanov <lifanov@mail.lifanov.com>
This commit is contained in:
allanjude 2016-01-12 05:51:12 +00:00
parent 5d3275e0a0
commit e5730b26f0
7 changed files with 44 additions and 10 deletions

View File

@ -10,13 +10,16 @@ FILES+= regress.0.out
FILES+= regress.0I.out
FILES+= regress.0J.out
FILES+= regress.0L.out
FILES+= regress.0P1.out
FILES+= regress.I.out
FILES+= regress.J.out
FILES+= regress.L.out
FILES+= regress.P1.out
FILES+= regress.R.out
FILES+= regress.in
FILES+= regress.n1.out
FILES+= regress.n2.out
FILES+= regress.n2P0.out
FILES+= regress.n3.out
FILES+= regress.normal.out
FILES+= regress.quotes.in

View File

@ -0,0 +1,4 @@
quick ' brown fox jumped
over "the lazy dog
quick brown fox jumped over the lazy dog

View File

@ -0,0 +1 @@
quick brown fox jumped over the lazy dog

View File

@ -0,0 +1,4 @@
fox jumped
lazy dog
over the
quick brown

View File

@ -1,6 +1,6 @@
# $FreeBSD$
echo 1..13
echo 1..16
REGRESSION_START($1)
@ -8,14 +8,17 @@ REGRESSION_TEST(`normal', `xargs echo The <${SRCDIR}/regress.in')
REGRESSION_TEST(`I', `xargs -I% echo The % % % %% % % <${SRCDIR}/regress.in')
REGRESSION_TEST(`J', `xargs -J% echo The % again. <${SRCDIR}/regress.in')
REGRESSION_TEST(`L', `xargs -L3 echo <${SRCDIR}/regress.in')
REGRESSION_TEST(`P1', `xargs -P1 echo <${SRCDIR}/regress.in')
REGRESSION_TEST(`R', `xargs -I% -R1 echo The % % % %% % % <${SRCDIR}/regress.in')
REGRESSION_TEST(`n1', `xargs -n1 echo <${SRCDIR}/regress.in')
REGRESSION_TEST(`n2', `xargs -n2 echo <${SRCDIR}/regress.in')
REGRESSION_TEST(`n2P0',`xargs -n2 -P0 echo <${SRCDIR}/regress.in | sort')
REGRESSION_TEST(`n3', `xargs -n3 echo <${SRCDIR}/regress.in')
REGRESSION_TEST(`0', `xargs -0 -n1 echo <${SRCDIR}/regress.0.in')
REGRESSION_TEST(`0I', `xargs -0 -I% echo The % %% % <${SRCDIR}/regress.0.in')
REGRESSION_TEST(`0J', `xargs -0 -J% echo The % again. <${SRCDIR}/regress.0.in')
REGRESSION_TEST(`0L', `xargs -0 -L2 echo <${SRCDIR}/regress.0.in')
REGRESSION_TEST(`0P1', `xargs -0 -P1 echo <${SRCDIR}/regress.0.in')
REGRESSION_TEST(`quotes', `xargs -n1 echo <${SRCDIR}/regress.quotes.in')
REGRESSION_END()

View File

@ -33,7 +33,7 @@
.\" $FreeBSD$
.\" $xMach: xargs.1,v 1.2 2002/02/23 05:23:37 tim Exp $
.\"
.Dd March 16, 2012
.Dd August 4, 2015
.Dt XARGS 1
.Os
.Sh NAME
@ -208,6 +208,11 @@ Parallel mode: run at most
invocations of
.Ar utility
at once.
If
.Ar maxprocs
is set to 0,
.Nm
will run as many processes as possible.
.It Fl p
Echo each command to be executed and ask the user whether it should be
executed.

View File

@ -46,9 +46,11 @@ static char sccsid[] = "@(#)xargs.c 8.1 (Berkeley) 6/6/93";
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include <sys/param.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/time.h>
#include <sys/limits.h>
#include <sys/resource.h>
#include <err.h>
#include <errno.h>
#include <fcntl.h>
@ -100,7 +102,9 @@ main(int argc, char *argv[])
long arg_max;
int ch, Jflag, nargs, nflag, nline;
size_t linelen;
struct rlimit rl;
char *endptr;
const char *errstr;
inpline = replstr = NULL;
ep = environ;
@ -148,19 +152,27 @@ main(int argc, char *argv[])
replstr = optarg;
break;
case 'L':
Lflag = atoi(optarg);
Lflag = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr)
errx(1, "-L %s: %s", optarg, errstr);
break;
case 'n':
nflag = 1;
if ((nargs = atoi(optarg)) <= 0)
errx(1, "illegal argument count");
nargs = strtonum(optarg, 1, INT_MAX, &errstr);
if (errstr)
errx(1, "-n %s: %s", optarg, errstr);
break;
case 'o':
oflag = 1;
break;
case 'P':
if ((maxprocs = atoi(optarg)) <= 0)
errx(1, "max. processes must be >0");
maxprocs = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr)
errx(1, "-P %s: %s", optarg, errstr);
if (getrlimit(RLIMIT_NPROC, &rl) != 0)
errx(1, "getrlimit failed");
if (maxprocs == 0 || maxprocs > rl.rlim_cur)
maxprocs = rl.rlim_cur;
break;
case 'p':
pflag = 1;
@ -179,7 +191,9 @@ main(int argc, char *argv[])
errx(1, "replsize must be a number");
break;
case 's':
nline = atoi(optarg);
nline = strtonum(optarg, 0, INT_MAX, &errstr);
if (errstr)
errx(1, "-s %s: %s", optarg, errstr);
break;
case 't':
tflag = 1;