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
This commit is contained in:
Aurelien DARRAGON 2024-10-25 17:04:37 +02:00
parent 7855069655
commit 24131dee30
1 changed files with 15 additions and 0 deletions

View File

@ -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 <s>, excluding
* the terminating null byte, but at most <maxlen>. The function does not
* look at characters passed <maxlen>.
*/
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.