argz-replace.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* String replacement in an argz vector
  2. Copyright (C) 1997-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <argz.h>
  18. /* Append BUF, of length BUF_LEN to *TO, of length *TO_LEN, reallocating and
  19. updating *TO & *TO_LEN appropriately. If an allocation error occurs,
  20. *TO's old value is freed, and *TO is set to 0. */
  21. static void
  22. str_append (char **to, size_t *to_len, const char *buf, const size_t buf_len)
  23. {
  24. size_t new_len = *to_len + buf_len;
  25. char *new_to = realloc (*to, new_len + 1);
  26. if (new_to)
  27. {
  28. *((char *) __mempcpy (new_to + *to_len, buf, buf_len)) = '\0';
  29. *to = new_to;
  30. *to_len = new_len;
  31. }
  32. else
  33. {
  34. free (*to);
  35. *to = NULL;
  36. }
  37. }
  38. /* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
  39. ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
  40. incremented by number of replacements performed. */
  41. error_t
  42. __argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
  43. unsigned *replace_count)
  44. {
  45. error_t err = 0;
  46. if (str && *str)
  47. {
  48. char *arg = NULL;
  49. char *src = *argz;
  50. size_t src_len = *argz_len;
  51. char *dst = NULL;
  52. size_t dst_len = 0;
  53. int delayed_copy = 1; /* True while we've avoided copying anything. */
  54. size_t str_len = strlen (str), with_len = strlen (with);
  55. while (!err && (arg = argz_next (src, src_len, arg)))
  56. {
  57. char *match = strstr (arg, str);
  58. if (match)
  59. {
  60. char *from = match + str_len;
  61. size_t to_len = match - arg;
  62. char *to = __strndup (arg, to_len);
  63. while (to && from)
  64. {
  65. str_append (&to, &to_len, with, with_len);
  66. if (to)
  67. {
  68. match = strstr (from, str);
  69. if (match)
  70. {
  71. str_append (&to, &to_len, from, match - from);
  72. from = match + str_len;
  73. }
  74. else
  75. {
  76. str_append (&to, &to_len, from, strlen (from));
  77. from = NULL;
  78. }
  79. }
  80. }
  81. if (to)
  82. {
  83. if (delayed_copy)
  84. /* We avoided copying SRC to DST until we found a match;
  85. now that we've done so, copy everything from the start
  86. of SRC. */
  87. {
  88. if (arg > src)
  89. err = __argz_append (&dst, &dst_len, src, (arg - src));
  90. delayed_copy = 0;
  91. }
  92. if (! err)
  93. err = __argz_add (&dst, &dst_len, to);
  94. free (to);
  95. }
  96. else
  97. err = ENOMEM;
  98. if (replace_count)
  99. (*replace_count)++;
  100. }
  101. else if (! delayed_copy)
  102. err = __argz_add (&dst, &dst_len, arg);
  103. }
  104. if (! err)
  105. {
  106. if (! delayed_copy)
  107. /* We never found any instances of str. */
  108. {
  109. free (src);
  110. *argz = dst;
  111. *argz_len = dst_len;
  112. }
  113. }
  114. else if (dst_len > 0)
  115. free (dst);
  116. }
  117. return err;
  118. }
  119. weak_alias (__argz_replace, argz_replace)