Debian Codegraph @

lib/util/nstring.c

raw
1 /*=============================================================================
2 nstring.c
3 ===============================================================================
4 Code in this file is contributed to the public domain by its authors.
5 =============================================================================*/
6 #define _DEFAULT_SOURCE /* New name for SVID & BSD source defines */
7 #define _C99_SOURCE /* Make sure snprintf() is in stdio.h */
8 #define _XOPEN_SOURCE 500 /* Make sure strdup() is in string.h */
9 #define _BSD_SOURCE /* Make sure strdup() is in string.h */
10 #define _GNU_SOURCE
11 /* Because of conditional compilation, this is GNU source only if the C
12 library is GNU.
13 */
14 #include <sys/types.h>
15 #include <limits.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <stdio.h>
19 #include <stdarg.h>
20 #include <assert.h>
21 #include <errno.h>
22
23 #include "pm.h"
24 #include "pm_c_util.h"
25
26 #include "nstring.h"
27
28 /* MacOS X before 10.7, for one, does not have strnlen */
29 size_t
30 pm_strnlen(const char * const s,
31 size_t const maxlen) {
32
33 unsigned int i;
34
35 for (i = 0; i < maxlen && s[i]; ++i) {}
36
37 return i;
38 }
39
40
41
42 /* When a function that is supposed to return a malloc'ed string cannot
43 get the memory for it, it should return 'pm_strsol'. That has a much
44 better effect on the caller, if the caller doesn't explicitly allow for
45 the out of memory case, than returning NULL. Note that it is very
46 rare for the system not to have enough memory to return a small string,
47 so it's OK to have somewhat nonsensical behavior when it happens. We
48 just don't want catastrophic behavior.
49
50 'pm_strsol' is an external symbol, so if Caller wants to detect the
51 out-of-memory failure, he certainly can.
52 */
53 const char * const pm_strsol = "NO MEMORY TO CREATE STRING!";
54
55
56
57 const char *
58 pm_strdup(const char * const arg) {
59
60 const char * const dup = strdup(arg);
61
62 return dup ? dup : pm_strsol;
63 }
64
65
66
67 void PM_GNU_PRINTF_ATTR(2,3)
68 pm_asprintf(const char ** const resultP,
69 const char * const fmt,
70 ...) {
71
72 const char * result;
73 va_list varargs;
74
75 #if HAVE_VASPRINTF
76 int rc;
77 va_start(varargs, fmt);
78 rc = vasprintf((char **)&result, fmt, varargs);
79 va_end(varargs);
80 if (rc < 0)
81 result = pm_strsol;
82 #else
83 size_t dryRunLen;
84
85 va_start(varargs, fmt);
86
87 dryRunLen = vsnprintf(NULL, 0, fmt, varargs);
88
89 va_end(varargs);
90
91 if (dryRunLen + 1 < dryRunLen)
92 /* arithmetic overflow */
93 result = pm_strsol;
94 else {
95 size_t const allocSize = dryRunLen + 1;
96 char * buffer;
97 buffer = malloc(allocSize);
98 if (buffer != NULL) {
99 va_list varargs;
100 size_t realLen;
101
102 va_start(varargs, fmt);
103
104 realLen = vsnprintf(buffer, allocSize, fmt, varargs);
105
106 assert(realLen == dryRunLen);
107 va_end(varargs);
108
109 result = buffer;
110 }
111 }
112 #endif
113
114 if (result == NULL)
115 *resultP = pm_strsol;
116 else
117 *resultP = result;
118 }
119
120
121
122 void
123 pm_strfree(const char * const string) {
124
125 if (string != pm_strsol)
126 free((void *) string);
127 }
128
129
130
131 const char *
132 pm_strsep(char ** const stringP, const char * const delim) {
133 const char * retval;
134
135 if (stringP == NULL || *stringP == NULL)
136 retval = NULL;
137 else {
138 char * p;
139
140 retval = *stringP;
141
142 for (p = *stringP; *p && strchr(delim, *p) == NULL; ++p);
143
144 if (*p) {
145 /* We hit a delimiter, not end-of-string. So null out the
146 delimiter and advance user's pointer to the next token
147 */
148 *p++ = '\0';
149 *stringP = p;
150 } else {
151 /* We ran out of string. So the end-of-string delimiter is
152 already there, and we set the user's pointer to NULL to
153 indicate there are no more tokens.
154 */
155 *stringP = NULL;
156 }
157 }
158 return retval;
159 }
160
161
162
163 int
164 pm_stripeq(const char * const comparand,
165 const char * const comparator) {
166 /*----------------------------------------------------------------------------
167 Compare two strings, ignoring leading and trailing white space.
168
169 Return 1 (true) if the strings are identical; 0 (false) otherwise.
170 -----------------------------------------------------------------------------*/
171 const char * p;
172 const char * q;
173 const char * px;
174 const char * qx;
175 bool equal;
176
177 /* Make p and q point to the first non-blank character in each string.
178 If there are no non-blank characters, make them point to the terminating
179 NUL.
180 */
181
182 p = &comparand[0];
183 while (ISSPACE(*p))
184 p++;
185 q = &comparator[0];
186 while (ISSPACE(*q))
187 q++;
188
189 /* Make px and qx point to the last non-blank character in each string.
190 If there are no nonblank characters (which implies the string is
191 null), make them point to the terminating NUL.
192 */
193
194 if (*p == '\0')
195 px = p;
196 else {
197 px = p + strlen(p) - 1;
198 while (ISSPACE(*px))
199 --px;
200 }
201
202 if (*q == '\0')
203 qx = q;
204 else {
205 qx = q + strlen(q) - 1;
206 while (ISSPACE(*qx))
207 --qx;
208 }
209
210 if (px - p != qx - q) {
211 /* The stripped strings aren't the same length, so we know they aren't
212 equal.
213 */
214 equal = false;
215 } else {
216 /* Move p and q through the nonblank characters, comparing. */
217 for (equal = true; p <= px; ++p, ++q) {
218 assert(q <= qx); /* Because stripped strings are same length */
219 if (*p != *q)
220 equal = false;
221 }
222 }
223 return equal ? 1 : 0;
224 }
225
226
227
228 const void *
229 pm_memmem(const void * const haystackArg,
230 size_t const haystacklen,
231 const void * const needleArg,
232 size_t const needlelen) {
233
234 const unsigned char * const haystack = haystackArg;
235 const unsigned char * const needle = needleArg;
236
237 /* This does the same as the function of the same name in the GNU
238 C library
239 */
240 const unsigned char * p;
241
242 for (p = haystack; p <= haystack + haystacklen - needlelen; ++p)
243 if (memeq(p, needle, needlelen))
244 return p;
245
246 return NULL;
247 }
248
249
250
251 bool
252 pm_strishex(const char * const subject) {
253
254 bool retval;
255 unsigned int i;
256
257 retval = TRUE; /* initial assumption */
258
259 for (i = 0; i < strlen(subject); ++i)
260 if (!ISXDIGIT(subject[i]))
261 retval = FALSE;
262
263 return retval;
264 }
265
266
267
268 void
269 pm_string_to_long(const char * const string,
270 long * const longP,
271 const char ** const errorP) {
272
273 if (strlen(string) == 0)
274 pm_asprintf(errorP, "Value is a null string");
275 else {
276 char * tailptr;
277
278 /* strtol() does a bizarre thing where if the number is out
279 of range, it returns a clamped value but tells you about it
280 by setting errno = ERANGE. If it is not out of range,
281 strtol() leaves errno alone.
282 */
283 errno = 0; /* So we can tell if strtol() overflowed */
284
285 *longP = strtol(string, &tailptr, 10);
286
287 if (*tailptr != '\0')
288 pm_asprintf(errorP, "Non-numeric crap in string: '%s'", tailptr);
289 else {
290 if (errno == ERANGE)
291 pm_asprintf(errorP, "Number is too large for computation");
292 else
293 *errorP = NULL;
294 }
295 }
296 }
297
298
299
300 void
301 pm_string_to_int(const char * const string,
302 int * const intP,
303 const char ** const errorP) {
304
305 long longValue;
306
307 pm_string_to_long(string, &longValue, errorP);
308
309 if (!*errorP) {
310 if ((int)longValue != longValue)
311 pm_asprintf(errorP,
312 "Number is too large for computation");
313 else {
314 *intP = (int)longValue;
315 *errorP = NULL;
316 }
317 }
318 }
319
320
321
322 void
323 pm_string_to_uint(const char * const string,
324 unsigned int * const uintP,
325 const char ** const errorP) {
326
327 /* We can't use 'strtoul'. Contrary to expectations, though as
328 designed, it returns junk if there is a minus sign.
329 */
330
331 long longValue;
332
333 pm_string_to_long(string, &longValue, errorP);
334
335 if (!*errorP) {
336 if (longValue < 0)
337 pm_asprintf(errorP, "Number is negative");
338 else {
339 if ((unsigned int)longValue != longValue)
340 pm_asprintf(errorP,
341 "Number is too large for computation");
342 else {
343 *uintP = (unsigned int)longValue;
344 *errorP = NULL;
345 }
346 }
347 }
348 }
349
350
351