]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - stringlib.c
bash-5.2 distribution sources and documentation
[thirdparty/bash.git] / stringlib.c
index 745e5363a99ab14e194248e756b0a96bb0a6d489..73304966f45be0605690744754e0326ef431e91d 100644 (file)
@@ -146,7 +146,8 @@ strsub (string, pat, rep, global)
      char *string, *pat, *rep;
      int global;
 {
-  int patlen, replen, templen, tempsize, repl, i;
+  size_t patlen, replen, templen, tempsize, i;
+  int repl;
   char *temp, *r;
 
   patlen = strlen (pat);
@@ -178,17 +179,22 @@ strsub (string, pat, rep, global)
 }
 
 /* Replace all instances of C in STRING with TEXT.  TEXT may be empty or
-   NULL.  If DO_GLOB is non-zero, we quote the replacement text for
-   globbing.  Backslash may be used to quote C. */
+   NULL.  If (FLAGS & 1) is non-zero, we quote the replacement text for
+   globbing.  Backslash may be used to quote C. If (FLAGS & 2) we allow
+   backslash to escape backslash as well. */
 char *
-strcreplace (string, c, text, do_glob)
+strcreplace (string, c, text, flags)
      char *string;
      int c;
      const char *text;
-     int do_glob;
+     int flags;
 {
   char *ret, *p, *r, *t;
-  int len, rlen, ind, tlen;
+  size_t len, rlen, ind, tlen;
+  int do_glob, escape_backslash;
+
+  do_glob = flags & 1;
+  escape_backslash = flags & 2;
 
   len = STRLEN (text);
   rlen = len + strlen (string) + 2;
@@ -225,6 +231,8 @@ strcreplace (string, c, text, do_glob)
 
       if (*p == '\\' && p[1] == c)
        p++;
+      else if (escape_backslash && *p == '\\' && p[1] == '\\')
+       p++;
 
       ind = r - ret;
       RESIZE_MALLOCED_BUFFER (ret, ind, 2, rlen, rlen);