2 * zebra string function
4 * XXX This version of snprintf does not check bounds!
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
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.
15 The implementation of strndup is copied from glibc-2.3.5:
16 Copyright (C) 1996, 1997, 1998, 2001, 2002 Free Software Foundation, Inc.
20 * This file is part of Quagga.
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
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.
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
42 * snprint() is a real basic wrapper around the standard sprintf()
43 * without any bounds checking
46 snprintf(char *str, size_t size, const char *format, ...)
50 va_start (args, format);
52 return vsprintf (str, format, args);
58 * Like strncpy but does not 0 fill the buffer and always null
61 * @param bufsize is the size of the destination buffer.
63 * @return index of the terminating byte.
66 strlcpy(char *d, const char *s, size_t bufsize)
68 size_t len = strlen(s);
82 * Like strncat() but does not 0 fill the buffer and always null
85 * @param bufsize length of the buffer, which should be one more than
86 * the maximum resulting string length.
89 strlcat(char *d, const char *s, size_t bufsize)
91 size_t len1 = strlen(d);
92 size_t len2 = strlen(s);
93 size_t ret = len1 + len2;
95 if (len1 < bufsize - 1) {
96 if (len2 >= bufsize - len1)
97 len2 = bufsize - len1 - 1;
98 memcpy(d+len1, s, len2);
107 strnlen(const char *s, size_t maxlen)
110 return (p = (const char *)memchr(s, '\0', maxlen)) ? (size_t)(p-s) : maxlen;
116 strndup (const char *s, size_t maxlen)
118 size_t len = strnlen (s, maxlen);
119 char *new = (char *) malloc (len + 1);
125 return (char *) memcpy (new, s, len);