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.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'src/string_util.h') diff --git a/src/string_util.h b/src/string_util.h index c1d316f0c..374fd0f91 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -23,6 +23,11 @@ #include "gcc.h" #include +#include /* for size_t */ + +#ifdef __cplusplus +extern "C" { +#endif /** * Remove the "const" attribute from a string pointer. This is a @@ -78,4 +83,25 @@ strchug_fast(char *p) bool string_array_contains(const char *const* haystack, const char *needle); +#if !defined(HAVE_STRNDUP) + +/** + * Duplicates the string to a newly allocated buffer + * copying at most n characters. + * + * @param str a string to duplicate + * @param n maximal number of characters to copy + * @return a pointer to the duplicated string, + * or NULL if memory allocation failed. + */ +gcc_malloc +char * +strndup(const char *str, size_t n); + +#endif + +#ifdef __cplusplus +} /* extern "C" */ +#endif + #endif -- cgit v1.2.3