Commit b26823c1 authored by Ammar Faizi's avatar Ammar Faizi Committed by Paul E. McKenney

tools/nolibc/string: Implement `strnlen()`

  size_t strnlen(const char *str, size_t maxlen);

The strnlen() function returns the number of bytes in the string
pointed to by sstr, excluding the terminating null byte ('\0'), but at
most maxlen. In doing this, strnlen() looks only at the first maxlen
characters in the string pointed to by str and never beyond str[maxlen-1].

The first use case of this function is for determining the memory
allocation size in the strndup() function.

Link: https://lore.kernel.org/lkml/CAOG64qMpEMh+EkOfjNdAoueC+uQyT2Uv3689_sOr37-JxdJf4g@mail.gmail.comSuggested-by: default avatarAlviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>
Acked-by: default avatarWilly Tarreau <w@1wt.eu>
Signed-off-by: default avatarAmmar Faizi <ammarfaizi2@gnuweeb.org>
Signed-off-by: default avatarPaul E. McKenney <paulmck@kernel.org>
parent 0e0ff638
...@@ -147,6 +147,15 @@ size_t nolibc_strlen(const char *str) ...@@ -147,6 +147,15 @@ size_t nolibc_strlen(const char *str)
#define strlen(str) nolibc_strlen((str)) #define strlen(str) nolibc_strlen((str))
#endif #endif
static __attribute__((unused))
size_t strnlen(const char *str, size_t maxlen)
{
size_t len;
for (len = 0; (len < maxlen) && str[len]; len++);
return len;
}
static __attribute__((unused)) static __attribute__((unused))
size_t strlcat(char *dst, const char *src, size_t size) size_t strlcat(char *dst, const char *src, size_t size)
{ {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment