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 | |
Paul Jakma | 010ebbb | 2014-09-16 11:53:49 +0100 | [diff] [blame] | 19 | /* |
| 20 | * This file is part of Quagga. |
| 21 | * |
| 22 | * Quagga is free software; you can redistribute it and/or modify it |
| 23 | * under the terms of the GNU General Public License as published by the |
| 24 | * Free Software Foundation; either version 2, or (at your option) any |
| 25 | * later version. |
| 26 | * |
| 27 | * Quagga is distributed in the hope that it will be useful, but |
| 28 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 29 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 30 | * General Public License for more details. |
| 31 | * |
| 32 | * You should have received a copy of the GNU General Public License |
| 33 | * along with Quagga; see the file COPYING. If not, write to the Free |
| 34 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 35 | * 02111-1307, USA. |
| 36 | */ |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 37 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | #include <zebra.h> |
| 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | #ifndef HAVE_SNPRINTF |
| 41 | /* |
| 42 | * snprint() is a real basic wrapper around the standard sprintf() |
| 43 | * without any bounds checking |
| 44 | */ |
| 45 | int |
| 46 | snprintf(char *str, size_t size, const char *format, ...) |
| 47 | { |
| 48 | va_list args; |
| 49 | |
| 50 | va_start (args, format); |
| 51 | |
| 52 | return vsprintf (str, format, args); |
| 53 | } |
| 54 | #endif |
| 55 | |
| 56 | #ifndef HAVE_STRLCPY |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 57 | /** |
| 58 | * Like strncpy but does not 0 fill the buffer and always null |
| 59 | * terminates. |
| 60 | * |
| 61 | * @param bufsize is the size of the destination buffer. |
| 62 | * |
| 63 | * @return index of the terminating byte. |
| 64 | **/ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 65 | size_t |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 66 | strlcpy(char *d, const char *s, size_t bufsize) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | { |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 68 | size_t len = strlen(s); |
| 69 | size_t ret = len; |
| 70 | if (bufsize > 0) { |
| 71 | if (len >= bufsize) |
| 72 | len = bufsize-1; |
| 73 | memcpy(d, s, len); |
| 74 | d[len] = 0; |
| 75 | } |
| 76 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | } |
| 78 | #endif |
| 79 | |
| 80 | #ifndef HAVE_STRLCAT |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Like strncat() but does not 0 fill the buffer and always null |
| 83 | * terminates. |
| 84 | * |
| 85 | * @param bufsize length of the buffer, which should be one more than |
| 86 | * the maximum resulting string length. |
| 87 | **/ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 88 | size_t |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 89 | strlcat(char *d, const char *s, size_t bufsize) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 90 | { |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 91 | size_t len1 = strlen(d); |
| 92 | size_t len2 = strlen(s); |
| 93 | size_t ret = len1 + len2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 94 | |
ajs | 851adbd | 2005-04-02 18:48:39 +0000 | [diff] [blame] | 95 | if (len1 < bufsize - 1) { |
| 96 | if (len2 >= bufsize - len1) |
| 97 | len2 = bufsize - len1 - 1; |
| 98 | memcpy(d+len1, s, len2); |
| 99 | d[len1+len2] = 0; |
| 100 | } |
| 101 | return ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | } |
| 103 | #endif |
ajs | 3cb98de | 2005-04-02 16:01:05 +0000 | [diff] [blame] | 104 | |
| 105 | #ifndef HAVE_STRNLEN |
| 106 | size_t |
| 107 | strnlen(const char *s, size_t maxlen) |
| 108 | { |
| 109 | const char *p; |
| 110 | return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen; |
| 111 | } |
| 112 | #endif |
hasso | e6a4feb | 2005-09-19 09:53:21 +0000 | [diff] [blame] | 113 | |
| 114 | #ifndef HAVE_STRNDUP |
| 115 | char * |
| 116 | strndup (const char *s, size_t maxlen) |
| 117 | { |
| 118 | size_t len = strnlen (s, maxlen); |
| 119 | char *new = (char *) malloc (len + 1); |
| 120 | |
| 121 | if (new == NULL) |
| 122 | return NULL; |
| 123 | |
| 124 | new[len] = '\0'; |
| 125 | return (char *) memcpy (new, s, len); |
| 126 | } |
| 127 | #endif |