diff options
author | Max Kellermann <max@duempel.org> | 2013-01-27 20:26:07 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-27 21:43:27 +0100 |
commit | 81557849909e4833a075f7be104031d751a31357 (patch) | |
tree | 1dc9f3924eea32e7edca2fa599b179274fa28a3d | |
parent | 44725e485db84c834782ade925dd31ae4341fecd (diff) | |
download | mpd-81557849909e4833a075f7be104031d751a31357.tar.gz mpd-81557849909e4833a075f7be104031d751a31357.tar.xz mpd-81557849909e4833a075f7be104031d751a31357.zip |
string_util: add fallback for strnlen()
Usually, when strndup() is not available, strndup() isn't either,
because both are POSIX 2008.
Diffstat (limited to '')
-rw-r--r-- | configure.ac | 2 | ||||
-rw-r--r-- | src/string_util.c | 15 | ||||
-rw-r--r-- | src/string_util.h | 8 |
3 files changed, 24 insertions, 1 deletions
diff --git a/configure.ac b/configure.ac index a15733f19..073141d7a 100644 --- a/configure.ac +++ b/configure.ac @@ -136,7 +136,7 @@ AC_SEARCH_LIBS([gethostbyname], [nsl]) AC_CHECK_FUNCS(pipe2 accept4 eventfd) -AC_CHECK_FUNCS(strndup) +AC_CHECK_FUNCS(strnlen strndup) AC_SEARCH_LIBS([exp], [m],, [AC_MSG_ERROR([exp() not found])]) diff --git a/src/string_util.c b/src/string_util.c index b76b257ba..5d9feccf9 100644 --- a/src/string_util.c +++ b/src/string_util.c @@ -48,6 +48,21 @@ string_array_contains(const char *const* haystack, const char *needle) 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) char * diff --git a/src/string_util.h b/src/string_util.h index 374fd0f91..62de53873 100644 --- a/src/string_util.h +++ b/src/string_util.h @@ -83,6 +83,14 @@ strchug_fast(char *p) bool 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) /** |