From 66ee55e5947682d10eed7a36b9da72a8cf6a40f2 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Sun, 3 Mar 2013 09:52:21 +0100 Subject: Add compat strndup() function Not available on Win32 and ancient unixes --- common/compat.c | 22 ++++++++++++++++++++++ common/compat.h | 7 +++++++ configure.ac | 1 + 3 files changed, 30 insertions(+) diff --git a/common/compat.c b/common/compat.c index af28e1d..ff8ee08 100644 --- a/common/compat.c +++ b/common/compat.c @@ -424,6 +424,28 @@ memdup (const void *data, #endif /* HAVE_MEMDUP */ +#ifndef HAVE_STRNDUP + +char * +strndup (const char *data, + size_t length) +{ + char *ret; + size_t len; + + len = strlen (data); + if (length > len) + length = len; + + ret = memdup (data, length + 1); + if (ret != NULL) + ret[length] = 0; + + return ret; +} + +#endif /* HAVE_STRDUP */ + #ifndef HAVE_STRCONCAT #include diff --git a/common/compat.h b/common/compat.h index ad80ca5..27e4403 100644 --- a/common/compat.h +++ b/common/compat.h @@ -223,6 +223,13 @@ void * memdup (const void *data, #endif /* HAVE_MEMDUP */ +#ifndef HAVE_STRNDUP + +char * strndup (const char *data, + size_t length); + +#endif /* HAVE_STRDUP */ + #ifdef HAVE_STDBOOL_H #include #else diff --git a/configure.ac b/configure.ac index ef6f06f..11ac492 100644 --- a/configure.ac +++ b/configure.ac @@ -76,6 +76,7 @@ if test "$os_unix" = "yes"; then # These are thngs we can work around AC_CHECK_MEMBERS([struct dirent.d_type],,,[#include ]) AC_CHECK_FUNCS([getprogname getexecname basename mkstemp mkdtemp]) + AC_CHECK_FUNCS([strnstr memdup strndup]) # Check if these are declared and/or available to link against AC_CHECK_DECLS([program_invocation_short_name]) -- cgit v1.1