summaryrefslogtreecommitdiffstats
path: root/src/malloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/malloc.c')
-rw-r--r--src/malloc.c25
1 files changed, 25 insertions, 0 deletions
diff --git a/src/malloc.c b/src/malloc.c
index e32cc1b..9592af4 100644
--- a/src/malloc.c
+++ b/src/malloc.c
@@ -58,3 +58,28 @@ char *ca_sprintf_malloc(const char *format, ...) {
size *= 2;
}
}
+
+#ifndef HAVE_STRNDUP
+char *ca_strndup(const char *s, size_t n) {
+ size_t n_avail;
+ char *p;
+
+ if (!s)
+ return NULL;
+
+ if (memchr(s, '\0', n)) {
+ n_avail = strlen(s);
+ if (n_avail > n)
+ n_avail = n;
+ } else
+ n_avail = n;
+
+ if (!(p = ca_new(char, n_avail + 1)))
+ return NULL;
+
+ memcpy(p, s, n_avail);
+ p[n_avail] = '\0';
+
+ return p;
+}
+#endif