Add internal implementation of strndup
This commit is contained in:
parent
b26a86a6a7
commit
c8e0086ce0
@ -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"])
|
||||
|
||||
|
26
string.cpp
26
string.cpp
@ -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
|
||||
|
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user