aboutsummaryrefslogtreecommitdiffstats
path: root/src/string_util.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/string_util.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/string_util.c b/src/string_util.c
index 6e5429076..5d9feccf9 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,37 @@ string_array_contains(const char *const* haystack, const char *needle)
return false;
}
+
+#ifndef HAVE_STRNLEN
+
+size_t
+strnlen(const char *s, size_t max)
+{
+ assert(s != NULL);
+
+ const char *t = memchr(s, 0, max);
+ return t != NULL
+ ? (size_t)(t - s)
+ : max;
+}
+
+#endif
+
+#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