2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 The Regents of the University of California.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * File: am-utils/libamu/strutil.c
50 #endif /* HAVE_CONFIG_H */
56 strnsave(const char *str, int len)
58 char *sp = (char *) xmalloc(len + 1);
59 memmove(sp, str, len);
67 * Concatenate three strings and store the result in the buffer pointed to
68 * by p, making p large enough to hold the strings
71 str3cat(char *p, char *s1, char *s2, char *s3)
77 p = (char *) xrealloc(p, l1 + l2 + l3 + 1);
79 memmove(p + l1, s2, l2);
80 memmove(p + l1 + l2, s3, l3 + 1);
86 * Split s using ch as delimiter and qc as quote character
89 strsplit(char *s, int ch, int qc)
95 ivec = (char **) xmalloc((ic + 1) * sizeof(char *));
103 while (*s && (ch == ' ' ? (isascii((unsigned char)*s) && isspace((unsigned char)*s)) : *s == ch))
113 * remember start of string
120 while (*s && !(ch == ' ' ? (isascii((unsigned char)*s) && isspace((unsigned char)*s)) : *s == ch)) {
126 while (*s && *s != qc)
138 * save string in new ivec slot
141 ivec = (char **) xrealloc((voidp) ivec, (ic + 1) * sizeof(char *));
143 plog(XLOG_DEBUG, "strsplit saved \"%s\"", v);
147 plog(XLOG_DEBUG, "strsplit saved a total of %d strings", ic);
156 * Use generic strlcpy to copy a string more carefully, null-terminating it
157 * as needed. However, if the copied string was truncated due to lack of
158 * space, then warn us.
160 * For now, xstrlcpy returns VOID because it doesn't look like anywhere in
161 * the Amd code do we actually use the return value of strncpy/strlcpy.
165 _xstrlcpy(const char *filename, int lineno, char *dst, const char *src, size_t len)
166 #else /* not DEBUG */
167 xstrlcpy(char *dst, const char *src, size_t len)
168 #endif /* not DEBUG */
172 if (strlcpy(dst, src, len) >= len)
174 plog(XLOG_ERROR, "xstrlcpy(%s:%d): string \"%s\" truncated to \"%s\"",
175 filename, lineno, src, dst);
176 #else /* not DEBUG */
177 plog(XLOG_ERROR, "xstrlcpy: string \"%s\" truncated to \"%s\"", src, dst);
178 #endif /* not DEBUG */
183 * Use generic strlcat to concatenate a string more carefully,
184 * null-terminating it as needed. However, if the copied string was
185 * truncated due to lack of space, then warn us.
187 * For now, xstrlcat returns VOID because it doesn't look like anywhere in
188 * the Amd code do we actually use the return value of strncat/strlcat.
192 _xstrlcat(const char *filename, int lineno, char *dst, const char *src, size_t len)
193 #else /* not DEBUG */
194 xstrlcat(char *dst, const char *src, size_t len)
195 #endif /* not DEBUG */
199 if (strlcat(dst, src, len) >= len) {
200 /* strlcat does not null terminate if the size of src is equal to len. */
201 dst[strlen(dst) - 1] = '\0';
203 plog(XLOG_ERROR, "xstrlcat(%s:%d): string \"%s\" truncated to \"%s\"",
204 filename, lineno, src, dst);
205 #else /* not DEBUG */
206 plog(XLOG_ERROR, "xstrlcat: string \"%s\" truncated to \"%s\"", src, dst);
207 #endif /* not DEBUG */
212 /* our version of snprintf */
214 #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
215 _xsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, ...)
216 #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
217 xsnprintf(char *str, size_t size, const char *format, ...)
218 #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
223 va_start(ap, format);
224 #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
225 ret = _xvsnprintf(filename, lineno, str, size, format, ap);
226 #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
227 ret = xvsnprintf(str, size, format, ap);
228 #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
235 /* our version of vsnprintf */
237 #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
238 _xvsnprintf(const char *filename, int lineno, char *str, size_t size, const char *format, va_list ap)
239 #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
240 xvsnprintf(char *str, size_t size, const char *format, va_list ap)
241 #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
245 #ifdef HAVE_VSNPRINTF
246 ret = vsnprintf(str, size, format, ap);
247 #else /* not HAVE_VSNPRINTF */
248 ret = vsprintf(str, format, ap); /* less secure version */
249 #endif /* not HAVE_VSNPRINTF */
251 * If error or truncation, plog error.
253 * WARNING: we use the static 'maxtrunc' variable below to break out any
254 * possible infinite recursion between plog() and xvsnprintf(). If it
255 * ever happens, it'd indicate a bug in Amd.
257 if (ret < 0 || (size_t) ret >= size) { /* error or truncation occured */
258 static int maxtrunc; /* hack to avoid inifinite loop */
260 #if defined(DEBUG) && (defined(HAVE_C99_VARARGS_MACROS) || defined(HAVE_GCC_VARARGS_MACROS))
261 plog(XLOG_ERROR, "xvsnprintf(%s:%d): string %p truncated (ret=%d, format=\"%s\")",
262 filename, lineno, str, ret, format);
263 #else /* not DEBUG or no C99/GCC-style vararg cpp macros supported */
264 plog(XLOG_ERROR, "xvsnprintf: string %p truncated (ret=%d, format=\"%s\")",
266 #endif /* not DEBUG or no C99/GCC-style vararg cpp macros supported */