From c07c7f5e9e09a4f436ee7f1514cdae759c34b0ba Mon Sep 17 00:00:00 2001 From: Ben Pfaff Date: Wed, 15 Oct 2008 14:35:13 -0700 Subject: [PATCH] New functions xvasprintf() and strlcpy() and macro va_copy(). --- include/util.h | 15 +++++++++++++++ lib/util.c | 32 +++++++++++++++++++++++++++----- m4/libopenflow.m4 | 4 +++- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/include/util.h b/include/util.h index d9c0b707a..8dee9b6d4 100644 --- a/include/util.h +++ b/include/util.h @@ -34,12 +34,22 @@ #ifndef UTIL_H #define UTIL_H 1 +#include #include #include #include #include +#include #include "compiler.h" +#ifndef va_copy +#ifdef __va_copy +#define va_copy __va_copy +#else +#define va_copy(dst, src) ((dst) = (src)) +#endif +#endif + #ifndef __cplusplus /* Build-time assertion for use in a statement context. */ #define BUILD_ASSERT(EXPR) \ @@ -91,6 +101,11 @@ void *xmemdup(const void *, size_t); char *xmemdup0(const char *, size_t); char *xstrdup(const char *); char *xasprintf(const char *format, ...) PRINTF_FORMAT(1, 2); +char *xvasprintf(const char *format, va_list) PRINTF_FORMAT(1, 0); + +#ifndef HAVE_STRLCPY +void strlcpy(char *dst, const char *src, size_t size); +#endif void ofp_fatal(int err_no, const char *format, ...) PRINTF_FORMAT(2, 3) NO_RETURN; diff --git a/lib/util.c b/lib/util.c index ff4325616..72138a066 100644 --- a/lib/util.c +++ b/lib/util.c @@ -101,25 +101,47 @@ xstrdup(const char *s) } char * -xasprintf(const char *format, ...) +xvasprintf(const char *format, va_list args) { - va_list args; + va_list args2; size_t needed; char *s; - va_start(args, format); + va_copy(args2, args); needed = vsnprintf(NULL, 0, format, args); - va_end(args); s = xmalloc(needed + 1); + vsnprintf(s, needed + 1, format, args2); + va_end(args2); + + return s; +} + +char * +xasprintf(const char *format, ...) +{ + va_list args; + char *s; + va_start(args, format); - vsnprintf(s, needed + 1, format, args); + s = xvasprintf(format, args); va_end(args); return s; } +void +strlcpy(char *dst, const char *src, size_t size) +{ + if (size > 0) { + size_t n = strlen(src); + size_t n_copy = MIN(n, size - 1); + memcpy(dst, src, n_copy); + dst[n_copy] = '\0'; + } +} + void ofp_fatal(int err_no, const char *format, ...) { diff --git a/m4/libopenflow.m4 b/m4/libopenflow.m4 index adee69ee0..4fa5b54ed 100644 --- a/m4/libopenflow.m4 +++ b/m4/libopenflow.m4 @@ -118,6 +118,7 @@ AC_DEFUN([OFP_CHECK_SOCKET_LIBS], [AC_CHECK_LIB([socket], [connect]) AC_SEARCH_LIBS([gethostbyname], [resolv], [RESOLVER_LIBS=-lresolv])]) + dnl Runs the checks required to include the headers in include/ and dnl link against lib/libopenflow.a. AC_DEFUN([OFP_CHECK_LIBOPENFLOW], @@ -127,5 +128,6 @@ AC_DEFUN([OFP_CHECK_LIBOPENFLOW], AC_REQUIRE([OFP_CHECK_OPENSSL]) AC_REQUIRE([OFP_CHECK_SNAT]) AC_REQUIRE([OFP_CHECK_FAULT_LIBS]) - AC_REQUIRE([OFP_CHECK_SOCKET_LIBS])]) + AC_REQUIRE([OFP_CHECK_SOCKET_LIBS]) + AC_CHECK_FUNCS([strlcpy])]) -- 2.43.0