[Skiboot] [PATCH 06/15] libc/string: Add strnlen

Claudio Carvalho cclaudio at linux.vnet.ibm.com
Thu Aug 11 15:23:48 AEST 2016


This adds strnlen. A more robust version of strlen.

Signed-off-by: Claudio Carvalho <cclaudio at linux.vnet.ibm.com>
---
 libc/include/string.h    |  1 +
 libc/string/Makefile.inc |  2 +-
 libc/string/strnlen.c    | 28 ++++++++++++++++++++++++++++
 3 files changed, 30 insertions(+), 1 deletion(-)
 create mode 100644 libc/string/strnlen.c

diff --git a/libc/include/string.h b/libc/include/string.h
index a53e4f4..527caa1 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/Makefile.inc b/libc/string/Makefile.inc
index 5f7fa27..804228f 100644
--- a/libc/string/Makefile.inc
+++ b/libc/string/Makefile.inc
@@ -12,7 +12,7 @@
 
 SUBDIRS += $(LIBCDIR)/string
 
-STRING_OBJS = strcat.o strchr.o strcmp.o strcpy.o strlen.o \
+STRING_OBJS = strcat.o strchr.o strcmp.o strcpy.o strlen.o strnlen.o \
 	      strncmp.o strncpy.o strstr.o memset.o memcpy.o memcpy_ci.o \
 	      memmove.o memchr.o memcmp.o strcasecmp.o strncasecmp.o \
 	      strtok.o strdup.o
diff --git a/libc/string/strnlen.c b/libc/string/strnlen.c
new file mode 100644
index 0000000..904c12e
--- /dev/null
+++ b/libc/string/strnlen.c
@@ -0,0 +1,28 @@
+/******************************************************************************
+ * Copyright (c) 2004, 2016 IBM Corporation
+ * All rights reserved.
+ * This program and the accompanying materials
+ * are made available under the terms of the BSD License
+ * which accompanies this distribution, and is available at
+ * http://www.opensource.org/licenses/bsd-license.php
+ *
+ * Contributors:
+ *     IBM Corporation - initial implementation
+ *****************************************************************************/
+
+#include <string.h>
+
+size_t strnlen(const char *s, size_t n)
+{
+	size_t len = 0;
+
+	if (n < 1)
+		return 0;
+
+	while (*s != 0 && n-- > 0) {
+		len++;
+		s++;
+	}
+
+	return len;
+}
-- 
1.9.1



More information about the Skiboot mailing list