paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 11 | #ifndef HAVE_SNPRINTF |
| 12 | /* |
| 13 | * snprint() is a real basic wrapper around the standard sprintf() |
| 14 | * without any bounds checking |
| 15 | */ |
| 16 | int |
| 17 | snprintf(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 | */ |
| 32 | size_t |
| 33 | strlcpy(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 | */ |
| 46 | size_t |
| 47 | strlcat(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 |
ajs | 3cb98de | 2005-04-02 16:01:05 +0000 | [diff] [blame^] | 61 | |
| 62 | #ifndef HAVE_STRNLEN |
| 63 | size_t |
| 64 | strnlen(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 |