Skip to content
Permalink
Browse files

libc: add strnlen implementation

This is standard function and useful for application writers.

Signed-off-by: Timo Teräs <timo.teras@iki.fi>
  • Loading branch information...
fabled authored and andrewboie committed Sep 26, 2018
1 parent c69081d commit 55dc481a156ee2b0c09bce1e267835423019a2df
Showing with 20 additions and 0 deletions.
  1. +1 −0 lib/libc/minimal/include/string.h
  2. +19 −0 lib/libc/minimal/source/string/string.c
@@ -22,6 +22,7 @@ extern char *strncpy(char *_MLIBC_RESTRICT d, const char *_MLIBC_RESTRICT s,
extern char *strchr(const char *s, int c);
extern char *strrchr(const char *s, int c);
extern size_t strlen(const char *s);
extern size_t strnlen(const char *s, size_t maxlen);
extern int strcmp(const char *s1, const char *s2);
extern int strncmp(const char *s1, const char *s2, size_t n);
extern char *strcat(char *_MLIBC_RESTRICT dest,
@@ -116,6 +116,25 @@ size_t strlen(const char *s)
return n;
}

/**
*
* @brief Get fixed-size string length
*
* @return number of bytes in fixed-size string <s>
*/

size_t strnlen(const char *s, size_t maxlen)
{
size_t n = 0;

while (*s != '\0' && n < maxlen) {
s++;
n++;
}

return n;
}

/**
*
* @brief Compare two strings

0 comments on commit 55dc481

Please sign in to comment.
You can’t perform that action at this time.