Skip to content

Commit 24131de

Browse files
committed
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
1 parent 7855069 commit 24131de

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

include/haproxy/tools.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@
8282
*/
8383
extern int strlcpy2(char *dst, const char *src, int size);
8484

85+
/*
86+
* portable equivalent to POSIX strnlen():
87+
* returns the number of bytes in the string pointed to by <s>, excluding
88+
* the terminating null byte, but at most <maxlen>. The function does not
89+
* look at characters passed <maxlen>.
90+
*/
91+
static inline size_t strnlen2(const char *s, size_t maxlen)
92+
{
93+
size_t len;
94+
95+
for (len = 0; len < maxlen && s[len]; len++)
96+
;
97+
return len;
98+
}
99+
85100
/*
86101
* This function simply returns a locally allocated string containing
87102
* the ascii representation for number 'n' in decimal.

0 commit comments

Comments
 (0)