From: Pavel TvrdĂ­k Date: Mon, 2 Nov 2015 15:49:06 +0000 (+0100) Subject: Add strlcpy() from libbsd or bird's own impl. X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e547061f33e7697c0c6c861d01d84be0767e8a45;p=thirdparty%2Fbird.git Add strlcpy() from libbsd or bird's own impl. --- diff --git a/configure.in b/configure.in index c81709e61..14a208e01 100644 --- a/configure.in +++ b/configure.in @@ -262,6 +262,8 @@ if test "$enable_debug" = yes ; then fi fi +AC_CHECK_LIB(bsd, strlcpy, LIBS="$LIBS -lbsd"; AC_DEFINE(HAVE_LIBBSD)) + CLIENT= CLIENT_LIBS= if test "$enable_client" = yes ; then diff --git a/lib/printf.c b/lib/printf.c index e4cc30067..8b4ddab78 100644 --- a/lib/printf.c +++ b/lib/printf.c @@ -457,3 +457,32 @@ buffer_puts(buffer *buf, const char *str) buf->pos = bp; } + +#ifndef HAVE_LIBBSD +/** + * strlcpy - BIRD's implementation of strlcpy() + * + * @dest: destination string + * @src: string must be NUL terminated + * @max_len: useable length in dest including the NUL terminator + * @return: the length of src string without the NUL terminator + * + * Size-bounded string copying and concatenation + */ +size_t +strlcpy(char *dest, const char *src, size_t max_len) +{ + size_t len = strlen(src); + + if (len < max_len) + memcpy(dest, src, len + 1); + else + { + max_len--; + memcpy(dest, src, max_len); + dest[max_len] = 0; + } + + return len; +} +#endif diff --git a/lib/string.h b/lib/string.h index 528a1a192..484f62ad7 100644 --- a/lib/string.h +++ b/lib/string.h @@ -24,4 +24,10 @@ void buffer_puts(buffer *buf, const char *str); int patmatch(byte *pat, byte *str); +#ifdef HAVE_LIBBSD +#include +#else +size_t strlcpy(char *dst, const char *src, size_t size); +#endif + #endif diff --git a/sysdep/autoconf.h.in b/sysdep/autoconf.h.in index a9e46e274..619585241 100644 --- a/sysdep/autoconf.h.in +++ b/sysdep/autoconf.h.in @@ -66,4 +66,7 @@ /* We have stdint.h */ #undef HAVE_STDINT_H +/* We have and strlcpy() */ +#undef HAVE_LIBBSD + #define CONFIG_PATH ?