gdb/
[deliverable/binutils-gdb.git] / gdb / utils.c
index b70edd8988b2dce9a85e97eea590c385f93ad1c4..15956b7670cb0acea756b45be00ee832fe0c40de 100644 (file)
@@ -3724,6 +3724,48 @@ dirnames_to_char_ptr_vec (const char *dirnames)
   return retval;
 }
 
+/* Substitute all occurences of string FROM by string TO in *STRINGP.  *STRINGP
+   must come from xrealloc-compatible allocator and it may be updated.  FROM
+   needs to be delimited by IS_DIR_SEPARATOR (or be located at the start or
+   end of *STRINGP.  */
+
+void
+substitute_path_component (char **stringp, const char *from, const char *to)
+{
+  char *string = *stringp, *s;
+  const size_t from_len = strlen (from);
+  const size_t to_len = strlen (to);
+
+  for (s = string;;)
+    {
+      s = strstr (s, from);
+      if (s == NULL)
+       break;
+
+      if ((s == string || IS_DIR_SEPARATOR (s[-1]))
+          && (s[from_len] == '\0' || IS_DIR_SEPARATOR (s[from_len])))
+       {
+         char *string_new;
+
+         string_new = xrealloc (string, (strlen (string) + to_len + 1));
+
+         /* Relocate the current S pointer.  */
+         s = s - string + string_new;
+         string = string_new;
+
+         /* Replace from by to.  */
+         memmove (&s[to_len], &s[from_len], strlen (&s[from_len]) + 1);
+         memcpy (s, to, to_len);
+
+         s += to_len;
+       }
+      else
+       s++;
+    }
+
+  *stringp = string;
+}
+
 #ifdef HAVE_WAITPID
 
 #ifdef SIGALRM
This page took 0.024667 seconds and 4 git commands to generate.