1 /* Functions for dealing with '\0' separated arg vectors.
2 Copyright (C) 1995-1998, 2000-2002, 2006, 2008-2011 Free Software
4 This file is part of the GNU C Library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
29 /* Add BUF, of length BUF_LEN to the argz vector in ARGZ & ARGZ_LEN. */
31 argz_append (char **argz, size_t *argz_len, const char *buf, size_t buf_len)
33 size_t new_argz_len = *argz_len + buf_len;
34 char *new_argz = realloc (*argz, new_argz_len);
37 memcpy (new_argz + *argz_len, buf, buf_len);
39 *argz_len = new_argz_len;
46 /* Add STR to the argz vector in ARGZ & ARGZ_LEN. This should be moved into
47 argz.c in libshouldbelibc. */
49 argz_add (char **argz, size_t *argz_len, const char *str)
51 return argz_append (argz, argz_len, str, strlen (str) + 1);
57 argz_add_sep (char **argz, size_t *argz_len, const char *string, int delim)
59 size_t nlen = strlen (string) + 1;
66 *argz = (char *) realloc (*argz, *argz_len + nlen);
70 wp = *argz + *argz_len;
75 if (wp > *argz && wp[-1] != '\0')
82 while (*rp++ != '\0');
93 argz_create_sep (const char *string, int delim, char **argz, size_t *len)
95 size_t nlen = strlen (string) + 1;
102 *argz = (char *) malloc (nlen);
111 if (wp > *argz && wp[-1] != '\0')
118 while (*rp++ != '\0');
139 /* Insert ENTRY into ARGZ & ARGZ_LEN before BEFORE, which should be an
140 existing entry in ARGZ; if BEFORE is NULL, ENTRY is appended to the end.
141 Since ARGZ's first entry is the same as ARGZ, argz_insert (ARGZ, ARGZ_LEN,
142 ARGZ, ENTRY) will insert ENTRY at the beginning of ARGZ. If BEFORE is not
143 in ARGZ, EINVAL is returned, else if memory can't be allocated for the new
144 ARGZ, ENOMEM is returned, else 0. */
146 argz_insert (char **argz, size_t *argz_len, char *before, const char *entry)
149 return argz_add (argz, argz_len, entry);
151 if (before < *argz || before >= *argz + *argz_len)
155 /* Make sure before is actually the beginning of an entry. */
160 size_t after_before = *argz_len - (before - *argz);
161 size_t entry_len = strlen (entry) + 1;
162 size_t new_argz_len = *argz_len + entry_len;
163 char *new_argz = realloc (*argz, new_argz_len);
167 before = new_argz + (before - *argz);
168 memmove (before + entry_len, before, after_before);
169 memmove (before, entry, entry_len);
171 *argz_len = new_argz_len;
181 argz_next (const char *argz, size_t argz_len, const char *entry)
185 if (entry < argz + argz_len)
186 entry = strchr (entry, '\0') + 1;
188 return entry >= argz + argz_len ? NULL : (char *) entry;
192 return (char *) argz;
198 /* Make '\0' separated arg vector ARGZ printable by converting all the '\0's
199 except the last into the character SEP. */
201 argz_stringify (char *argz, size_t len, int sep)
206 size_t part_len = strnlen (argz, len);
209 if (len-- <= 1) /* includes final '\0' we want to stop at */
216 /* Returns the number of strings in ARGZ. */
218 argz_count (const char *argz, size_t len)
223 size_t part_len = strlen (argz);
224 argz += part_len + 1;
232 /* Puts pointers to each string in ARGZ, plus a terminating 0 element, into
233 ARGV, which must be large enough to hold them all. */
235 argz_extract (const char *argz, size_t len, char **argv)
239 size_t part_len = strlen (argz);
240 *argv++ = (char *) argz;
241 argz += part_len + 1;
248 /* Make a '\0' separated arg vector from a unix argv vector, returning it in
249 ARGZ, and the total length in LEN. If a memory allocation error occurs,
250 ENOMEM is returned, otherwise 0. */
252 argz_create (char *const argv[], char **argz, size_t *len)
259 for (argc = 0; argv[argc] != NULL; ++argc)
260 tlen += strlen (argv[argc]) + 1;
266 *argz = malloc (tlen);
270 for (p = *argz, ap = argv; *ap; ++ap, ++p)
279 /* Delete ENTRY from ARGZ & ARGZ_LEN, if any. */
281 argz_delete (char **argz, size_t *argz_len, char *entry)
284 /* Get rid of the old value for NAME. */
286 size_t entry_len = strlen (entry) + 1;
287 *argz_len -= entry_len;
288 memmove (entry, entry + entry_len, *argz_len - (entry - *argz));
298 /* Append BUF, of length BUF_LEN to *TO, of length *TO_LEN, reallocating and
299 updating *TO & *TO_LEN appropriately. If an allocation error occurs,
300 *TO's old value is freed, and *TO is set to 0. */
302 str_append (char **to, size_t *to_len, const char *buf, const size_t buf_len)
304 size_t new_len = *to_len + buf_len;
305 char *new_to = realloc (*to, new_len + 1);
309 *((char *) mempcpy (new_to + *to_len, buf, buf_len)) = '\0';
320 /* Replace any occurrences of the string STR in ARGZ with WITH, reallocating
321 ARGZ as necessary. If REPLACE_COUNT is non-zero, *REPLACE_COUNT will be
322 incremented by number of replacements performed. */
324 argz_replace (char **argz, size_t *argz_len, const char *str, const char *with,
325 unsigned *replace_count)
333 size_t src_len = *argz_len;
336 int delayed_copy = 1; /* True while we've avoided copying anything. */
337 size_t str_len = strlen (str), with_len = strlen (with);
339 while (!err && (arg = argz_next (src, src_len, arg)))
341 char *match = strstr (arg, str);
344 char *from = match + str_len;
345 size_t to_len = match - arg;
346 char *to = strndup (arg, to_len);
350 str_append (&to, &to_len, with, with_len);
353 match = strstr (from, str);
356 str_append (&to, &to_len, from, match - from);
357 from = match + str_len;
361 str_append (&to, &to_len, from, strlen (from));
370 /* We avoided copying SRC to DST until we found a match;
371 now that we've done so, copy everything from the start
375 err = argz_append (&dst, &dst_len, src, (arg - src));
379 err = argz_add (&dst, &dst_len, to);
388 else if (! delayed_copy)
389 err = argz_add (&dst, &dst_len, arg);
395 /* We never found any instances of str. */
402 else if (dst_len > 0)