xargs: implement -n

Signed-off-by: Hiltjo Posthuma <hiltjo@codemadness.org>
This commit is contained in:
Hiltjo Posthuma 2014-04-09 17:35:29 +02:00 committed by sin
parent 6dd138613e
commit 1be58188ad

24
xargs.c
View File

@ -26,14 +26,15 @@ static char *cmd[NARGS];
static char *argb; static char *argb;
static size_t argbsz; static size_t argbsz;
static size_t argbpos; static size_t argbpos;
static long maxargs = 0;
static int nerrors = 0; static int nerrors = 0;
static char *eofstr; static char *eofstr;
static int rflag = 0; static int rflag = 0, nflag = 0;
static void static void
usage(void) usage(void)
{ {
eprintf("usage: %s [-r] [-E eofstr] [cmd [arg...]]\n", argv0); eprintf("usage: %s [n maxargs] [-r] [-E eofstr] [cmd [arg...]]\n", argv0);
} }
int int
@ -42,9 +43,14 @@ main(int argc, char *argv[])
int leftover; int leftover;
long argsz, argmaxsz; long argsz, argmaxsz;
char *arg; char *arg;
int i; int i, a;
ARGBEGIN { ARGBEGIN {
case 'n':
nflag = 1;
if((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0)
eprintf("%s: value for -n option should be >= 1\n", argv0);
break;
case 'r': case 'r':
rflag = 1; rflag = 1;
break; break;
@ -63,7 +69,7 @@ main(int argc, char *argv[])
leftover = 0; leftover = 0;
do { do {
argsz = 0; i = 0; argsz = 0; i = 0; a = 0;
if (argc > 0) { if (argc > 0) {
for (; i < argc; i++) { for (; i < argc; i++) {
cmd[i] = strdup(argv[i]); cmd[i] = strdup(argv[i]);
@ -85,10 +91,18 @@ main(int argc, char *argv[])
cmd[i] = strdup(arg); cmd[i] = strdup(arg);
argsz += strlen(cmd[i]) + 1; argsz += strlen(cmd[i]) + 1;
i++; i++;
a++;
leftover = 0; leftover = 0;
if(nflag == 1 && a >= maxargs)
break;
} }
cmd[i] = NULL; cmd[i] = NULL;
if (i == 1 && rflag == 1); else spawn(); if(a >= maxargs && nflag == 1)
spawn();
else if(!a || (i == 1 && rflag == 1))
;
else
spawn();
for (; i >= 0; i--) for (; i >= 0; i--)
free(cmd[i]); free(cmd[i]);
} while (arg); } while (arg);