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 | |
| 11 | #include "str.h" |
| 12 | |
| 13 | #ifndef HAVE_SNPRINTF |
| 14 | /* |
| 15 | * snprint() is a real basic wrapper around the standard sprintf() |
| 16 | * without any bounds checking |
| 17 | */ |
| 18 | int |
| 19 | snprintf(char *str, size_t size, const char *format, ...) |
| 20 | { |
| 21 | va_list args; |
| 22 | |
| 23 | va_start (args, format); |
| 24 | |
| 25 | return vsprintf (str, format, args); |
| 26 | } |
| 27 | #endif |
| 28 | |
| 29 | #ifndef HAVE_STRLCPY |
| 30 | /* |
| 31 | * strlcpy is a safer version of strncpy(), checking the total |
| 32 | * size of the buffer |
| 33 | */ |
| 34 | size_t |
| 35 | strlcpy(char *dst, const char *src, size_t size) |
| 36 | { |
| 37 | strncpy(dst, src, size); |
| 38 | |
| 39 | return (strlen(dst)); |
| 40 | } |
| 41 | #endif |
| 42 | |
| 43 | #ifndef HAVE_STRLCAT |
| 44 | /* |
| 45 | * strlcat is a safer version of strncat(), checking the total |
| 46 | * size of the buffer |
| 47 | */ |
| 48 | size_t |
| 49 | strlcat(char *dst, const char *src, size_t size) |
| 50 | { |
| 51 | /* strncpy(dst, src, size - strlen(dst)); */ |
| 52 | |
| 53 | /* I've just added below code only for workable under Linux. So |
| 54 | need rewrite -- Kunihiro. */ |
| 55 | if (strlen (dst) + strlen (src) >= size) |
| 56 | return -1; |
| 57 | |
| 58 | strcat (dst, src); |
| 59 | |
| 60 | return (strlen(dst)); |
| 61 | } |
| 62 | #endif |