shithub: riscv

Download patch

ref: 55c122d59f504ae48f834b3299484ef4f56283d6
parent: 3b376b43af24f8013d330eb2eb9c3b78849bb7c3
author: spew <devnull@localhost>
date: Thu Jun 21 07:56:37 EDT 2018

ape: add strnlen

--- a/sys/include/ape/string.h
+++ b/sys/include/ape/string.h
@@ -32,6 +32,7 @@
 extern void *memset(void *, int, size_t);
 extern char *strerror(int);
 extern size_t strlen(const char *);
+extern size_t strnlen(const char *, size_t);
 
 #ifdef _REENTRANT_SOURCE
 extern char *strerror_r(int, const char *, int);
--- a/sys/src/ape/lib/ap/gen/mkfile
+++ b/sys/src/ape/lib/ap/gen/mkfile
@@ -43,6 +43,7 @@
 	strncat.$O\
 	strncmp.$O\
 	strncpy.$O\
+	strnlen.$O\
 	strpbrk.$O\
 	strrchr.$O\
 	strspn.$O\
--- a/sys/src/ape/lib/ap/gen/strlen.c
+++ b/sys/src/ape/lib/ap/gen/strlen.c
@@ -3,6 +3,5 @@
 size_t
 strlen(const char *s)
 {
-
 	return strchr(s, 0) - s;
 }
--- /dev/null
+++ b/sys/src/ape/lib/ap/gen/strnlen.c
@@ -1,0 +1,11 @@
+#include <string.h>
+
+size_t
+strnlen(const char *s, size_t maxlen)
+{
+	size_t i;
+
+	for (i = 0; i < maxlen && s[i] != '\0'; i++)
+		;
+	return i;
+}