Commit 81557849 authored by Max Kellermann's avatar Max Kellermann

string_util: add fallback for strnlen()

Usually, when strndup() is not available, strndup() isn't either, because both are POSIX 2008.
parent 44725e48
...@@ -136,7 +136,7 @@ AC_SEARCH_LIBS([gethostbyname], [nsl]) ...@@ -136,7 +136,7 @@ AC_SEARCH_LIBS([gethostbyname], [nsl])
AC_CHECK_FUNCS(pipe2 accept4 eventfd) AC_CHECK_FUNCS(pipe2 accept4 eventfd)
AC_CHECK_FUNCS(strndup) AC_CHECK_FUNCS(strnlen strndup)
AC_SEARCH_LIBS([exp], [m],, AC_SEARCH_LIBS([exp], [m],,
[AC_MSG_ERROR([exp() not found])]) [AC_MSG_ERROR([exp() not found])])
......
...@@ -48,6 +48,21 @@ string_array_contains(const char *const* haystack, const char *needle) ...@@ -48,6 +48,21 @@ string_array_contains(const char *const* haystack, const char *needle)
return false; return false;
} }
#ifndef HAVE_STRNLEN
size_t
strnlen(const char *s, size_t max)
{
assert(s != NULL);
const char *t = memchr(s, 0, max);
return t != NULL
? (size_t)(t - s)
: max;
}
#endif
#if !defined(HAVE_STRNDUP) #if !defined(HAVE_STRNDUP)
char * char *
......
...@@ -83,6 +83,14 @@ strchug_fast(char *p) ...@@ -83,6 +83,14 @@ strchug_fast(char *p)
bool bool
string_array_contains(const char *const* haystack, const char *needle); string_array_contains(const char *const* haystack, const char *needle);
#ifndef HAVE_STRNLEN
gcc_pure
size_t
strnlen(const char *s, size_t max);
#endif
#if !defined(HAVE_STRNDUP) #if !defined(HAVE_STRNDUP)
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment