[hackers] [sbase] xargs: Apply -I substitution to all the parameters || Roberto E. Vargas Caballero

From: <git_AT_suckless.org>
Date: Fri, 22 Sep 2023 12:50:23 +0200 (CEST)

commit a1f0426699523809ba50db47829c038ee4b762ab
Author: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
AuthorDate: Fri Sep 22 11:07:07 2023 +0200
Commit: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
CommitDate: Fri Sep 22 11:45:46 2023 +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.

diff --git a/xargs.c b/xargs.c
index 57e624d..30b09ab 100644
--- a/xargs.c
+++ b/xargs.c
_AT_@ -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;
_AT_@ -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 = "";
_AT_@ -227,6 +227,7 @@ main(int argc, char *argv[])
                 eofstr = EARGF(usage());
                 break;
         case 'I':
+ Iflag = 1;
                 xflag = 1;
                 nflag = 1;
                 maxargs = 1;
_AT_@ -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");
_AT_@ -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;
Received on Fri Sep 22 2023 - 12:50:23 CEST

This archive was generated by hypermail 2.3.0 : Fri Sep 22 2023 - 13:00:39 CEST