argz-replace.c 3.5 KB

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