blob: 18a5d8edd841a9263b27e4fb227e51d83a2ad355 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * zebra string function
3 *
4 * these functions are just very basic wrappers around exiting ones and
5 * do not offer the protection that might be expected against buffer
6 * overruns etc
7 */
8
9#include <zebra.h>
10
paul718e3742002-12-13 20:15:29 +000011#ifndef HAVE_SNPRINTF
12/*
13 * snprint() is a real basic wrapper around the standard sprintf()
14 * without any bounds checking
15 */
16int
17snprintf(char *str, size_t size, const char *format, ...)
18{
19 va_list args;
20
21 va_start (args, format);
22
23 return vsprintf (str, format, args);
24}
25#endif
26
27#ifndef HAVE_STRLCPY
28/*
29 * strlcpy is a safer version of strncpy(), checking the total
30 * size of the buffer
31 */
32size_t
33strlcpy(char *dst, const char *src, size_t size)
34{
35 strncpy(dst, src, size);
36
37 return (strlen(dst));
38}
39#endif
40
41#ifndef HAVE_STRLCAT
42/*
43 * strlcat is a safer version of strncat(), checking the total
44 * size of the buffer
45 */
46size_t
47strlcat(char *dst, const char *src, size_t size)
48{
49 /* strncpy(dst, src, size - strlen(dst)); */
50
51 /* I've just added below code only for workable under Linux. So
52 need rewrite -- Kunihiro. */
53 if (strlen (dst) + strlen (src) >= size)
54 return -1;
55
56 strcat (dst, src);
57
58 return (strlen(dst));
59}
60#endif
ajs3cb98de2005-04-02 16:01:05 +000061
62#ifndef HAVE_STRNLEN
63size_t
64strnlen(const char *s, size_t maxlen)
65{
66 const char *p;
67 return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen;
68}
69#endif