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.
This commit is contained in:
Roberto E. Vargas Caballero 2023-09-22 11:07:07 +02:00
parent 22f110db28
commit a1f0426699
1 changed files with 14 additions and 9 deletions

23
xargs.c
View File

@ -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
cmd[i] = estrdup(arg);
argsz += arglen + 1;
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]);
}
}
i++;
a++;
leftover = 0;