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;
80
argz_create_sep (const char *str, int delim, char **pargz, size_t *pargz_len)
89
/* Make a copy of STR, but replacing each occurrence of
91
argz_len = 1+ strlen (str);
97
argz = (char *) malloc (argz_len);
101
for (p = str, q = argz; *p != EOS_CHAR; ++p)
105
/* Ignore leading delimiters, and fold consecutive
106
delimiters in STR into a single '\0' in ARGZ. */
107
if ((q > argz) && (q[-1] != EOS_CHAR))
115
/* Copy terminating EOS_CHAR. */
119
/* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory. */
121
argz = (free (argz), (char *) 0);
123
/* Assign new values. */
125
*pargz_len = argz_len;
132
argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry)
136
assert (entry && *entry);
138
/* No BEFORE address indicates ENTRY should be inserted after the
139
current last element. */
141
return argz_append (pargz, pargz_len, entry, 1+ strlen (entry));
143
/* This probably indicates a programmer error, but to preserve
144
semantics, scan back to the start of an entry if BEFORE points
145
into the middle of it. */
146
while ((before > *pargz) && (before[-1] != EOS_CHAR))
150
size_t entry_len = 1+ strlen (entry);
151
size_t argz_len = *pargz_len + entry_len;
152
size_t offset = before - *pargz;
153
char *argz = (char *) realloc (*pargz, argz_len);
158
/* Make BEFORE point to the equivalent offset in ARGZ that it
159
used to have in *PARGZ incase realloc() moved the block. */
160
before = argz + offset;
162
/* Move the ARGZ entries starting at BEFORE up into the new
163
space at the end -- making room to copy ENTRY into the
165
memmove (before + entry_len, before, *pargz_len - offset);
166
memcpy (before, entry, entry_len);
168
/* Assign new values. */
170
*pargz_len = argz_len;
178
argz_next (char *argz, size_t argz_len, const char *entry)
180
assert ((argz && argz_len) || (!argz && !argz_len));
184
/* Either ARGZ/ARGZ_LEN is empty, or ENTRY points into an address
185
within the ARGZ vector. */
186
assert ((!argz && !argz_len)
187
|| ((argz <= entry) && (entry < (argz + argz_len))));
189
/* Move to the char immediately after the terminating
191
entry = 1+ strchr (entry, EOS_CHAR);
193
/* Return either the new ENTRY, or else NULL if ARGZ is
195
return (entry >= argz + argz_len) ? 0 : (char *) entry;
199
/* This should probably be flagged as a programmer error,
200
since starting an argz_next loop with the iterator set
201
to ARGZ is safer. To preserve semantics, handle the NULL
202
case by returning the start of ARGZ (if any). */
212
argz_stringify (char *argz, size_t argz_len, int sep)
214
assert ((argz && argz_len) || (!argz && !argz_len));
218
--argz_len; /* don't stringify the terminating EOS */
219
while (--argz_len > 0)
221
if (argz[argz_len] == EOS_CHAR)
222
argz[argz_len] = sep;