diff mbox series

[09/14] libc: add strnlen()

Message ID 20170915054059.32109-9-oohall@gmail.com
State Accepted
Headers show
Series [01/14] core/pci-dt-slot: Represent PCIe slots in the devicetree | expand

Commit Message

Oliver O'Halloran Sept. 15, 2017, 5:40 a.m. UTC
Sometimes handy.

Signed-off-by: Oliver O'Halloran <oohall@gmail.com>
---
 libc/include/string.h |  1 +
 libc/string/strlen.c  | 13 +++++++++++++
 2 files changed, 14 insertions(+)
diff mbox series

Patch

diff --git a/libc/include/string.h b/libc/include/string.h
index e4b4fac95ef9..2172bdcbf9f5 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 95b99d25455f..5b408e7ef899 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;
+}