1 /* argz.c -- argz implementation for non-glibc systems
3 Copyright (C) 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
4 Written by Gary V. Vaughan, 2004
6 NOTE: The canonical source of this file is maintained with the
7 GNU Libtool package. Report bugs to bug-libtool@gnu.org.
9 GNU Libltdl is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 As a special exception to the GNU Lesser General Public License,
15 if you distribute this file as part of a program or library that
16 is built using GNU Libtool, you may include this file under the
17 same distribution terms that you use for the rest of that program.
19 GNU Libltdl is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU Lesser General Public License for more details.
24 You should have received a copy of the GNU Lesser General Public
25 License along with GNU Libltdl; see the file COPYING.LIB. If not, a
26 copy can be downloaded from http://www.gnu.org/licenses/lgpl.html,
27 or obtained by writing to the Free Software Foundation, Inc.,
28 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
31 #if defined(LTDL) && defined LT_CONFIG_H
42 #include <sys/types.h>
49 argz_append (char **pargz, size_t *pargz_len, const char *buf, size_t buf_len)
56 assert ((*pargz && *pargz_len) || (!*pargz && !*pargz_len));
58 /* If nothing needs to be appended, no more work is required. */
62 /* Ensure there is enough room to append BUF_LEN. */
63 argz_len = *pargz_len + buf_len;
64 argz = (char *) realloc (*pargz, argz_len);
68 /* Copy characters from BUF after terminating '\0' in ARGZ. */
69 memcpy (argz + *pargz_len, buf, buf_len);
71 /* Assign new values. */
73 *pargz_len = argz_len;
79 /* Add a string to the argz vector. */
81 argz_add (char **pargz, size_t *pargz_len, const char *str)
83 return argz_append (pargz, pargz_len, str, strlen (str) + 1);
88 argz_create_sep (const char *str, int delim, char **pargz, size_t *pargz_len)
97 /* Make a copy of STR, but replacing each occurrence of
99 argz_len = 1+ strlen (str);
105 argz = (char *) malloc (argz_len);
109 for (p = str, q = argz; *p != EOS_CHAR; ++p)
113 /* Ignore leading delimiters, and fold consecutive
114 delimiters in STR into a single '\0' in ARGZ. */
115 if ((q > argz) && (q[-1] != EOS_CHAR))
123 /* Copy terminating EOS_CHAR. */
127 /* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory. */
129 argz = (free (argz), (char *) 0);
131 /* Assign new values. */
133 *pargz_len = argz_len;
140 argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry)
144 assert (entry && *entry);
146 /* No BEFORE address indicates ENTRY should be inserted after the
147 current last element. */
149 return argz_append (pargz, pargz_len, entry, 1+ strlen (entry));
151 /* This probably indicates a programmer error, but to preserve
152 semantics, scan back to the start of an entry if BEFORE points
153 into the middle of it. */
154 while ((before > *pargz) && (before[-1] != EOS_CHAR))
158 size_t entry_len = 1+ strlen (entry);
159 size_t argz_len = *pargz_len + entry_len;
160 size_t offset = before - *pargz;
161 char *argz = (char *) realloc (*pargz, argz_len);
166 /* Make BEFORE point to the equivalent offset in ARGZ that it
167 used to have in *PARGZ incase realloc() moved the block. */
168 before = argz + offset;
170 /* Move the ARGZ entries starting at BEFORE up into the new
171 space at the end -- making room to copy ENTRY into the
173 memmove (before + entry_len, before, *pargz_len - offset);
174 memcpy (before, entry, entry_len);
176 /* Assign new values. */
178 *pargz_len = argz_len;
186 argz_next (char *argz, size_t argz_len, const char *entry)
188 assert ((argz && argz_len) || (!argz && !argz_len));
192 /* Either ARGZ/ARGZ_LEN is empty, or ENTRY points into an address
193 within the ARGZ vector. */
194 assert ((!argz && !argz_len)
195 || ((argz <= entry) && (entry < (argz + argz_len))));
197 /* Move to the char immediately after the terminating
199 entry = 1+ strchr (entry, EOS_CHAR);
201 /* Return either the new ENTRY, or else NULL if ARGZ is
203 return (entry >= argz + argz_len) ? 0 : (char *) entry;
207 /* This should probably be flagged as a programmer error,
208 since starting an argz_next loop with the iterator set
209 to ARGZ is safer. To preserve semantics, handle the NULL
210 case by returning the start of ARGZ (if any). */
220 argz_stringify (char *argz, size_t argz_len, int sep)
222 assert ((argz && argz_len) || (!argz && !argz_len));
226 --argz_len; /* don't stringify the terminating EOS */
227 while (--argz_len > 0)
229 if (argz[argz_len] == EOS_CHAR)
230 argz[argz_len] = sep;
236 /* Count number of elements (null bytes) in argz vector. */
239 argz_count (const char *argz, size_t argz_len)
243 assert ((argz && argz_len) || (!argz && !argz_len));
247 size_t part_len = strlen (argz);
248 argz += part_len + 1;
249 argz_len -= part_len + 1;