paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * zebra string function |
| 3 | * |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 4 | * XXX This version of snprintf does not check bounds! |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 7 | /* |
| 8 | The implementations of strlcpy and strlcat are copied from rsync (GPL): |
| 9 | Copyright (C) Andrew Tridgell 1998 |
| 10 | Copyright (C) 2002 by Martin Pool |
| 11 | |
| 12 | Note that these are not terribly efficient, since they make more than one |
| 13 | pass over the argument strings. At some point, they should be optimized. |
hasso | e6a4feb | 2005-09-19 09:53:21 +0000 | [diff] [blame^] | 14 | |
| 15 | The implementation of strndup is copied from glibc-2.3.5: |
| 16 | Copyright (C) 1996, 1997, 1998, 2001, 2002 Free Software Foundation, Inc. |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 20 | #include <zebra.h> |
| 21 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 22 | #ifndef HAVE_SNPRINTF |
| 23 | /* |
| 24 | * snprint() is a real basic wrapper around the standard sprintf() |
| 25 | * without any bounds checking |
| 26 | */ |
| 27 | int |
| 28 | snprintf(char *str, size_t size, const char *format, ...) |
| 29 | { |
| 30 | va_list args; |
| 31 | |
| 32 | va_start (args, format); |
| 33 | |
| 34 | return vsprintf (str, format, args); |
| 35 | } |
| 36 | #endif |
| 37 | |
| 38 | #ifndef HAVE_STRLCPY |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 39 | /** |
| 40 | * Like strncpy but does not 0 fill the buffer and always null |
| 41 | * terminates. |
| 42 | * |
| 43 | * @param bufsize is the size of the destination buffer. |
| 44 | * |
| 45 | * @return index of the terminating byte. |
| 46 | **/ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | size_t |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 48 | strlcpy(char *d, const char *s, size_t bufsize) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 49 | { |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 50 | size_t len = strlen(s); |
| 51 | size_t ret = len; |
| 52 | if (bufsize > 0) { |
| 53 | if (len >= bufsize) |
| 54 | len = bufsize-1; |
| 55 | memcpy(d, s, len); |
| 56 | d[len] = 0; |
| 57 | } |
| 58 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 59 | } |
| 60 | #endif |
| 61 | |
| 62 | #ifndef HAVE_STRLCAT |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 63 | /** |
| 64 | * Like strncat() but does not 0 fill the buffer and always null |
| 65 | * terminates. |
| 66 | * |
| 67 | * @param bufsize length of the buffer, which should be one more than |
| 68 | * the maximum resulting string length. |
| 69 | **/ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 70 | size_t |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 71 | strlcat(char *d, const char *s, size_t bufsize) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | { |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 73 | size_t len1 = strlen(d); |
| 74 | size_t len2 = strlen(s); |
| 75 | size_t ret = len1 + len2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 76 | |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 77 | if (len1 < bufsize - 1) { |
| 78 | if (len2 >= bufsize - len1) |
| 79 | len2 = bufsize - len1 - 1; |
| 80 | memcpy(d+len1, s, len2); |
| 81 | d[len1+len2] = 0; |
| 82 | } |
| 83 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 84 | } |
| 85 | #endif |
ajs | 3cb98de | 2005-04-02 16:01:05 +0000 | [diff] [blame] | 86 | |
| 87 | #ifndef HAVE_STRNLEN |
| 88 | size_t |
| 89 | strnlen(const char *s, size_t maxlen) |
| 90 | { |
| 91 | const char *p; |
| 92 | return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen; |
| 93 | } |
| 94 | #endif |
hasso | e6a4feb | 2005-09-19 09:53:21 +0000 | [diff] [blame^] | 95 | |
| 96 | #ifndef HAVE_STRNDUP |
| 97 | char * |
| 98 | strndup (const char *s, size_t maxlen) |
| 99 | { |
| 100 | size_t len = strnlen (s, maxlen); |
| 101 | char *new = (char *) malloc (len + 1); |
| 102 | |
| 103 | if (new == NULL) |
| 104 | return NULL; |
| 105 | |
| 106 | new[len] = '\0'; |
| 107 | return (char *) memcpy (new, s, len); |
| 108 | } |
| 109 | #endif |