Print out helpful error if a realm regex can't be parsed
[freeradius.git] / libltdl / argz.c
1 /* argz.c -- argz implementation for non-glibc systems
2
3    Copyright (C) 2004, 2006, 2007, 2008 Free Software Foundation, Inc.
4    Written by Gary V. Vaughan, 2004
5
6    NOTE: The canonical source of this file is maintained with the
7    GNU Libtool package.  Report bugs to bug-libtool@gnu.org.
8
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.
13
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.
18
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.
23
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.
29 */
30
31 #if defined(LTDL) && defined LT_CONFIG_H
32 #  include LT_CONFIG_H
33 #else
34 #  include <config.h>
35 #endif
36
37 #include <argz.h>
38
39 #include <assert.h>
40 #include <stddef.h>
41 #include <stdlib.h>
42 #include <sys/types.h>
43 #include <errno.h>
44 #include <string.h>
45
46 #define EOS_CHAR '\0'
47
48 error_t
49 argz_append (char **pargz, size_t *pargz_len, const char *buf, size_t buf_len)
50 {
51   size_t argz_len;
52   char  *argz;
53
54   assert (pargz);
55   assert (pargz_len);
56   assert ((*pargz && *pargz_len) || (!*pargz && !*pargz_len));
57
58   /* If nothing needs to be appended, no more work is required.  */
59   if (buf_len == 0)
60     return 0;
61
62   /* Ensure there is enough room to append BUF_LEN.  */
63   argz_len = *pargz_len + buf_len;
64   argz = (char *) realloc (*pargz, argz_len);
65   if (!argz)
66     return ENOMEM;
67
68   /* Copy characters from BUF after terminating '\0' in ARGZ.  */
69   memcpy (argz + *pargz_len, buf, buf_len);
70
71   /* Assign new values.  */
72   *pargz = argz;
73   *pargz_len = argz_len;
74
75   return 0;
76 }
77
78
79 /* Add a string to the argz vector.  */
80 error_t
81 argz_add (char **pargz, size_t *pargz_len, const char *str)
82 {
83   return argz_append (pargz, pargz_len, str, strlen (str) + 1);
84 }
85
86
87 error_t
88 argz_create_sep (const char *str, int delim, char **pargz, size_t *pargz_len)
89 {
90   size_t argz_len;
91   char *argz = 0;
92
93   assert (str);
94   assert (pargz);
95   assert (pargz_len);
96
97   /* Make a copy of STR, but replacing each occurrence of
98      DELIM with '\0'.  */
99   argz_len = 1+ strlen (str);
100   if (argz_len)
101     {
102       const char *p;
103       char *q;
104
105       argz = (char *) malloc (argz_len);
106       if (!argz)
107         return ENOMEM;
108
109       for (p = str, q = argz; *p != EOS_CHAR; ++p)
110         {
111           if (*p == delim)
112             {
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))
116                 *q++ = EOS_CHAR;
117               else
118                 --argz_len;
119             }
120           else
121             *q++ = *p;
122         }
123       /* Copy terminating EOS_CHAR.  */
124       *q = *p;
125     }
126
127   /* If ARGZ_LEN has shrunk to nothing, release ARGZ's memory.  */
128   if (!argz_len)
129     argz = (free (argz), (char *) 0);
130
131   /* Assign new values.  */
132   *pargz = argz;
133   *pargz_len = argz_len;
134
135   return 0;
136 }
137
138
139 error_t
140 argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry)
141 {
142   assert (pargz);
143   assert (pargz_len);
144   assert (entry && *entry);
145
146   /* No BEFORE address indicates ENTRY should be inserted after the
147      current last element.  */
148   if (!before)
149     return argz_append (pargz, pargz_len, entry, 1+ strlen (entry));
150
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))
155     --before;
156
157   {
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);
162
163     if (!argz)
164       return ENOMEM;
165
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;
169
170     /* Move the ARGZ entries starting at BEFORE up into the new
171        space at the end -- making room to copy ENTRY into the
172        resulting gap.  */
173     memmove (before + entry_len, before, *pargz_len - offset);
174     memcpy  (before, entry, entry_len);
175
176     /* Assign new values.  */
177     *pargz = argz;
178     *pargz_len = argz_len;
179   }
180
181   return 0;
182 }
183
184
185 char *
186 argz_next (char *argz, size_t argz_len, const char *entry)
187 {
188   assert ((argz && argz_len) || (!argz && !argz_len));
189
190   if (entry)
191     {
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))));
196
197       /* Move to the char immediately after the terminating
198          '\0' of ENTRY.  */
199       entry = 1+ strchr (entry, EOS_CHAR);
200
201       /* Return either the new ENTRY, or else NULL if ARGZ is
202          exhausted.  */
203       return (entry >= argz + argz_len) ? 0 : (char *) entry;
204     }
205   else
206     {
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).  */
211       if (argz_len > 0)
212         return argz;
213       else
214         return 0;
215     }
216 }
217
218
219 void
220 argz_stringify (char *argz, size_t argz_len, int sep)
221 {
222   assert ((argz && argz_len) || (!argz && !argz_len));
223
224   if (sep)
225     {
226       --argz_len;               /* don't stringify the terminating EOS */
227       while (--argz_len > 0)
228         {
229           if (argz[argz_len] == EOS_CHAR)
230             argz[argz_len] = sep;
231         }
232     }
233 }
234
235
236 /* Count number of elements (null bytes) in argz vector.  */
237
238 size_t
239 argz_count (const char *argz, size_t argz_len)
240 {
241   size_t count = 0;
242
243   assert ((argz && argz_len) || (!argz && !argz_len));
244   
245   while (argz_len > 0)
246     {
247       size_t part_len = strlen (argz);
248       argz += part_len + 1;
249       argz_len -= part_len + 1;
250       count++;
251     }
252   
253   return count;
254 }