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
This commit is contained in:
@@ -20,6 +20,8 @@
|
||||
#include "config.h"
|
||||
#include "string_util.h"
|
||||
|
||||
#include <stdlib.h> /* for malloc() */
|
||||
#include <string.h> /* for strnlen() */
|
||||
#include <glib.h>
|
||||
|
||||
#include <assert.h>
|
||||
@@ -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
|
||||
|
Reference in New Issue
Block a user