aboutsummaryrefslogtreecommitdiff
path: root/src/string_util.c
diff options
context:
space:
mode:
authorDenis Krjuchkov <denis@crazydev.net>2013-01-11 13:51:39 +0600
committerDenis Krjuchkov <denis@crazydev.net>2013-01-11 13:51:39 +0600
commita98aa666203cad87913303d2f8b9ca07640518c3 (patch)
tree7fc9aa4231dbe86e227cd50d84f16d937898c28b /src/string_util.c
parent631a26899610e11a6c6456c2a5750dace68958f7 (diff)
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
Diffstat (limited to 'src/string_util.c')
-rw-r--r--src/string_util.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/string_util.c b/src/string_util.c
index 6e542907..b76b257b 100644
--- a/src/string_util.c
+++ b/src/string_util.c
@@ -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