summaryrefslogtreecommitdiff
path: root/tools/include/nolibc
diff options
context:
space:
mode:
authorAmmar Faizi <ammarfaizi2@gnuweeb.org>2022-03-29 13:17:36 +0300
committerPaul E. McKenney <paulmck@kernel.org>2022-04-21 03:05:46 +0300
commitb26823c19a12d9a06207ad3051e3d1059a9e1005 (patch)
tree2556910069b586e8f75c67efbbdf675191b69031 /tools/include/nolibc
parent0e0ff638400be8f497a35b51a4751fd823f6bd6a (diff)
downloadlinux-b26823c19a12d9a06207ad3051e3d1059a9e1005.tar.xz
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.com Suggested-by: Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org> Acked-by: Willy Tarreau <w@1wt.eu> Signed-off-by: Ammar Faizi <ammarfaizi2@gnuweeb.org> Signed-off-by: Paul E. McKenney <paulmck@kernel.org>
Diffstat (limited to 'tools/include/nolibc')
-rw-r--r--tools/include/nolibc/string.h9
1 files changed, 9 insertions, 0 deletions
diff --git a/tools/include/nolibc/string.h b/tools/include/nolibc/string.h
index 75a453870498..f43d52a44d09 100644
--- a/tools/include/nolibc/string.h
+++ b/tools/include/nolibc/string.h
@@ -148,6 +148,15 @@ size_t nolibc_strlen(const char *str)
#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))
size_t strlcat(char *dst, const char *src, size_t size)
{
size_t len;