summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoe Marcus Clarke <marcus@marcuscom.com>2008-08-20 20:51:29 +0200
committerLennart Poettering <lennart@poettering.net>2008-08-20 20:51:29 +0200
commitf1ca2213fa649a9e3635ec7638b6c5bced594787 (patch)
tree0edefc1891d4a84e0f01369d0c61fddc416509c3
parent5badd28d4f7329fceaa4c79dc37cad201a282943 (diff)
Implement ca_strdup for platforms that lack it natively
Signed-off-by: Lennart Poettering <lennart@poettering.net>
-rw-r--r--configure.ac3
-rw-r--r--src/malloc.c25
-rw-r--r--src/malloc.h8
3 files changed, 36 insertions, 0 deletions
diff --git a/configure.ac b/configure.ac
index 551073d..2a48177 100644
--- a/configure.ac
+++ b/configure.ac
@@ -139,6 +139,9 @@ AC_CHECK_FUNCS([strerror_r])
# BSD
AC_CHECK_FUNCS([lstat])
+# GNU
+AC_CHECK_FUNCS([strndup])
+
#### POSIX threads ####
ACX_PTHREAD
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
diff --git a/src/malloc.h b/src/malloc.h
index 30f0216..6073e03 100644
--- a/src/malloc.h
+++ b/src/malloc.h
@@ -27,11 +27,19 @@
#include "canberra.h"
#include "macro.h"
+#ifndef PACKAGE
+#error "Please include config.h before including this file!"
+#endif
+
#define ca_malloc malloc
#define ca_free free
#define ca_malloc0(size) calloc(1, (size))
#define ca_strdup strdup
+#ifdef HAVE_STRNDUP
#define ca_strndup strndup
+#else
+char *ca_strndup(const char *s, size_t n);
+#endif
void* ca_memdup(const void* p, size_t size);