Add internal implementation of strndup

This commit is contained in:
Danny Robson 2013-02-26 17:50:56 +11:00
parent b26a86a6a7
commit c8e0086ce0
3 changed files with 36 additions and 0 deletions

View File

@ -119,6 +119,10 @@ esac
AC_FUNC_MMAP
AC_CHECK_FUNC([strndup], [
AC_DEFINE([HAVE_STRNDUP], [], [strndup appears to be present])
])
AC_CHECK_HEADER([execinfo.h], [break])
AM_CONDITIONAL([HAVE_EXECINFO], [test x$ac_cv_header_execinfo_h = "xyes"])

View File

@ -28,3 +28,29 @@ strbegins (const char *restrict str,
const char *restrict prefix) {
return 0 == strncmp (prefix, str, strlen (prefix));
}
#if !defined(HAVE_STRNDUP)
// Written by Niels Möller <nisse@lysator.liu.se>
// Placed in the public domain
char *restrict
strndup (const char *restrict s, size_t size)
{
char *r;
char *end = (char *)memchr(s, 0, size);
if (end)
// Length + 1
size = end - s + 1;
char * r = malloc(size);
if (size) {
memcpy(r, s, size-1);
r[size-1] = '\0';
}
return r;
}
#endif

View File

@ -24,5 +24,11 @@ bool
strbegins(const char *restrict str,
const char *restrict prefix);
#if !defined(HAVE_STRNDUP)
char *restrict
strndup (const char *restrict s, size_t size);
#endif
#endif // __UTIL_STRING_HPP