diff options
-rw-r--r-- | libc/include/string.h | 1 | ||||
-rw-r--r-- | libc/string/strlen.c | 13 |
2 files changed, 14 insertions, 0 deletions
diff --git a/libc/include/string.h b/libc/include/string.h index e4b4fac..2172bdc 100644 --- a/libc/include/string.h +++ b/libc/include/string.h @@ -25,6 +25,7 @@ int strncasecmp(const char *s1, const char *s2, size_t n); char *strchr(const char *s, int c); char *strrchr(const char *s, int c); size_t strlen(const char *s); +size_t strnlen(const char *s, size_t n); char *strstr(const char *hay, const char *needle); char *strtok(char *src, const char *pattern); char *strdup(const char *src); diff --git a/libc/string/strlen.c b/libc/string/strlen.c index 95b99d2..5b408e7 100644 --- a/libc/string/strlen.c +++ b/libc/string/strlen.c @@ -25,3 +25,16 @@ strlen(const char *s) return len; } +size_t +strnlen(const char *s, size_t n) +{ + size_t len = 0; + + while (*s != 0 && n) { + len += 1; + s += 1; + n--; + } + + return len; +} |