From 81557849909e4833a075f7be104031d751a31357 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Sun, 27 Jan 2013 20:26:07 +0100 Subject: string_util: add fallback for strnlen() Usually, when strndup() is not available, strndup() isn't either, because both are POSIX 2008. --- src/string_util.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/string_util.c') diff --git a/src/string_util.c b/src/string_util.c index b76b257b..5d9feccf 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 * -- cgit v1.2.3