From: Aurelien DARRAGON Date: Fri, 25 Oct 2024 15:04:37 +0000 (+0200) Subject: MINOR: tools: add strnlen2() helper X-Git-Tag: v3.1-dev11~42 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=24131dee30bd7eedb4d99bd13bbb9792f7361937;p=thirdparty%2Fhaproxy.git MINOR: tools: add strnlen2() helper strnlen2() is functionally equivalent to strnlen(). Goal is to provide an alternative to strnlen() which is not portable since it requires _POSIX_C_SOURCE >= 200809L --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index 8d1afb5d7c..0bade0fd19 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -82,6 +82,21 @@ */ extern int strlcpy2(char *dst, const char *src, int size); +/* + * portable equivalent to POSIX strnlen(): + * returns the number of bytes in the string pointed to by , excluding + * the terminating null byte, but at most . The function does not + * look at characters passed . + */ +static inline size_t strnlen2(const char *s, size_t maxlen) +{ + size_t len; + + for (len = 0; len < maxlen && s[len]; len++) + ; + return len; +} + /* * This function simply returns a locally allocated string containing * the ascii representation for number 'n' in decimal.