commit a1f0426699523809ba50db47829c038ee4b762ab
parent 22f110db281176b477ec8508b077750ab3449f97
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 22 Sep 2023 11:07:07 +0200
xargs: Apply -I substitution to all the parameters
The substitution must happen in all the parameters that contain
the replacement string, but the code was soing the substitution
only once and in the parameter with an exact match.
Also, the argument length was not updated correctly, assuming
that the final argument had the size read from stdin.
Diffstat:
1 file changed, 13 insertions(+), 8 deletions(-)
diff --git a/xargs.c b/xargs.c
@@ -26,7 +26,7 @@ static size_t argbsz;
static size_t argbpos;
static size_t maxargs = 0;
static int nerrors = 0;
-static int rflag = 0, nflag = 0, tflag = 0, xflag = 0;
+static int rflag = 0, nflag = 0, tflag = 0, xflag = 0, Iflag = 0;
static char *argb;
static char *cmd[NARGS];
static char *eofstr;
@@ -195,7 +195,7 @@ usage(void)
int
main(int argc, char *argv[])
{
- int ret = 0, leftover = 0, ri = 0, i;
+ int ret = 0, leftover = 0, i, j;
size_t argsz, argmaxsz;
size_t arglen, a;
char *arg = "";
@@ -227,6 +227,7 @@ main(int argc, char *argv[])
eofstr = EARGF(usage());
break;
case 'I':
+ Iflag = 1;
xflag = 1;
nflag = 1;
maxargs = 1;
@@ -242,8 +243,6 @@ main(int argc, char *argv[])
for (; i < argc; i++) {
cmd[i] = estrdup(argv[i]);
argsz += strlen(cmd[i]) + 1;
- if (!strcmp(cmd[i], replstr))
- ri = i;
}
} else {
cmd[i] = estrdup("/bin/echo");
@@ -261,12 +260,18 @@ main(int argc, char *argv[])
leftover = 1;
break;
}
- if (ri > 0)
- strnsubst(&cmd[ri], replstr, arg, 255);
- else
+
+ if (!Iflag) {
cmd[i] = estrdup(arg);
+ argsz += arglen + 1;
+ } else {
+ for (j = 1; j < i; j++) {
+ argsz -= strlen(cmd[j]);
+ strnsubst(&cmd[j], replstr, arg, 255);
+ argsz += strlen(cmd[j]);
+ }
+ }
- argsz += arglen + 1;
i++;
a++;
leftover = 0;