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