[hackers] [sbase] xargs: implement -n || Hiltjo Posthuma
commit 1be58188ad3487e07f72c0190eb4775c52beced5
Author: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
Date: Wed Apr 9 17:35:29 2014 +0200
xargs: implement -n
Signed-off-by: Hiltjo Posthuma <hiltjo_AT_codemadness.org>
diff --git a/xargs.c b/xargs.c
index 8e6ba61..564eee8 100644
--- a/xargs.c
+++ b/xargs.c
_AT_@ -26,14 +26,15 @@ static char *cmd[NARGS];
static char *argb;
static size_t argbsz;
static size_t argbpos;
+static long maxargs = 0;
static int nerrors = 0;
static char *eofstr;
-static int rflag = 0;
+static int rflag = 0, nflag = 0;
static void
usage(void)
{
- eprintf("usage: %s [-r] [-E eofstr] [cmd [arg...]]
", argv0);
+ eprintf("usage: %s [n maxargs] [-r] [-E eofstr] [cmd [arg...]]
", argv0);
}
int
_AT_@ -42,9 +43,14 @@ main(int argc, char *argv[])
int leftover;
long argsz, argmaxsz;
char *arg;
- int i;
+ int i, a;
ARGBEGIN {
+ case 'n':
+ nflag = 1;
+ if((maxargs = strtol(EARGF(usage()), NULL, 10)) <= 0)
+ eprintf("%s: value for -n option should be >= 1
", argv0);
+ break;
case 'r':
rflag = 1;
break;
_AT_@ -63,7 +69,7 @@ main(int argc, char *argv[])
leftover = 0;
do {
- argsz = 0; i = 0;
+ argsz = 0; i = 0; a = 0;
if (argc > 0) {
for (; i < argc; i++) {
cmd[i] = strdup(argv[i]);
_AT_@ -85,10 +91,18 @@ main(int argc, char *argv[])
cmd[i] = strdup(arg);
argsz += strlen(cmd[i]) + 1;
i++;
+ a++;
leftover = 0;
+ if(nflag == 1 && a >= maxargs)
+ break;
}
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--)
free(cmd[i]);
} while (arg);
Received on Wed Apr 09 2014 - 18:11:53 CEST
This archive was generated by hypermail 2.3.0
: Wed Apr 09 2014 - 18:12:13 CEST