From a98aa666203cad87913303d2f8b9ca07640518c3 Mon Sep 17 00:00:00 2001 From: Denis Krjuchkov Date: Fri, 11 Jan 2013 13:51:39 +0600 Subject: string_util.c: provide fallback strndup() implementation This patch also adds extern "C" { } wrapper around string_util.h to allow its usage in C++ code --- src/string_util.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/string_util.c') diff --git a/src/string_util.c b/src/string_util.c index 6e5429076..b76b257ba 100644 --- a/src/string_util.c +++ b/src/string_util.c @@ -20,6 +20,8 @@ #include "config.h" #include "string_util.h" +#include /* for malloc() */ +#include /* for strnlen() */ #include #include @@ -45,3 +47,22 @@ string_array_contains(const char *const* haystack, const char *needle) return false; } + +#if !defined(HAVE_STRNDUP) + +char * +strndup(const char *str, size_t n) +{ + assert(str != NULL); + + size_t len = strnlen(str, n); + char* ret = (char *) malloc(len + 1); + if (ret == NULL) + return NULL; + + memcpy(ret, str, len); + ret[len] = '\0'; + return ret; +} + +#endif -- cgit v1.2.3