This source file includes following definitions.
- argz_append
- argz_create_sep
- argz_insert
- argz_next
- argz_stringify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
59 if (buf_len == 0)
60 return 0;
61
62
63 argz_len = *pargz_len + buf_len;
64 argz = (char *) realloc (*pargz, argz_len);
65 if (!argz)
66 return ENOMEM;
67
68
69 memcpy (argz + *pargz_len, buf, buf_len);
70
71
72 *pargz = argz;
73 *pargz_len = argz_len;
74
75 return 0;
76 }
77
78
79 error_t
80 argz_create_sep (const char *str, int delim, char **pargz, size_t *pargz_len)
81 {
82 size_t argz_len;
83 char *argz = 0;
84
85 assert (str);
86 assert (pargz);
87 assert (pargz_len);
88
89
90
91 argz_len = 1+ strlen (str);
92 if (argz_len)
93 {
94 const char *p;
95 char *q;
96
97 argz = (char *) malloc (argz_len);
98 if (!argz)
99 return ENOMEM;
100
101 for (p = str, q = argz; *p != EOS_CHAR; ++p)
102 {
103 if (*p == delim)
104 {
105
106
107 if ((q > argz) && (q[-1] != EOS_CHAR))
108 *q++ = EOS_CHAR;
109 else
110 --argz_len;
111 }
112 else
113 *q++ = *p;
114 }
115
116 *q = *p;
117 }
118
119
120 if (!argz_len)
121 argz = (free (argz), (char *) 0);
122
123
124 *pargz = argz;
125 *pargz_len = argz_len;
126
127 return 0;
128 }
129
130
131 error_t
132 argz_insert (char **pargz, size_t *pargz_len, char *before, const char *entry)
133 {
134 assert (pargz);
135 assert (pargz_len);
136 assert (entry && *entry);
137
138
139
140 if (!before)
141 return argz_append (pargz, pargz_len, entry, 1+ strlen (entry));
142
143
144
145
146 while ((before > *pargz) && (before[-1] != EOS_CHAR))
147 --before;
148
149 {
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);
154
155 if (!argz)
156 return ENOMEM;
157
158
159
160 before = argz + offset;
161
162
163
164
165 memmove (before + entry_len, before, *pargz_len - offset);
166 memcpy (before, entry, entry_len);
167
168
169 *pargz = argz;
170 *pargz_len = argz_len;
171 }
172
173 return 0;
174 }
175
176
177 char *
178 argz_next (char *argz, size_t argz_len, const char *entry)
179 {
180 assert ((argz && argz_len) || (!argz && !argz_len));
181
182 if (entry)
183 {
184
185
186 assert ((!argz && !argz_len)
187 || ((argz <= entry) && (entry < (argz + argz_len))));
188
189
190
191 entry = 1+ strchr (entry, EOS_CHAR);
192
193
194
195 return (entry >= argz + argz_len) ? 0 : (char *) entry;
196 }
197 else
198 {
199
200
201
202
203 if (argz_len > 0)
204 return argz;
205 else
206 return 0;
207 }
208 }
209
210
211 void
212 argz_stringify (char *argz, size_t argz_len, int sep)
213 {
214 assert ((argz && argz_len) || (!argz && !argz_len));
215
216 if (sep)
217 {
218 --argz_len;
219 while (--argz_len > 0)
220 {
221 if (argz[argz_len] == EOS_CHAR)
222 argz[argz_len] = sep;
223 }
224 }
225 }
226