]> pd.if.org Git - pdclib.old/commitdiff
<string.h>: add POSIX 2008 extension strnlen (general utility + use by printf).
authorOwen Shepherd <owen.shepherd@e43.eu>
Sat, 25 Aug 2012 14:32:11 +0000 (15:32 +0100)
committerOwen Shepherd <owen.shepherd@e43.eu>
Sat, 25 Aug 2012 14:32:11 +0000 (15:32 +0100)
functions/string/strnlen.c [new file with mode: 0644]
includes/string.h

diff --git a/functions/string/strnlen.c b/functions/string/strnlen.c
new file mode 100644 (file)
index 0000000..e7eab57
--- /dev/null
@@ -0,0 +1,38 @@
+/* $Id$ */\r
+\r
+/* strnlen( const char *, size_t len )\r
+\r
+   This file is part of the Public Domain C Library (PDCLib).\r
+   Permission is granted to use, modify, and / or redistribute at will.\r
+*/\r
+\r
+#include <string.h>\r
+#include <stdint.h>\r
+\r
+#ifndef REGTEST\r
+\r
+size_t strnlen( const char * s, size_t maxlen )\r
+{\r
+    for( size_t len = 0; len != maxlen; len++ )\r
+    {\r
+        if(s[len] == '\0')\r
+            return len - 1;\r
+    }\r
+    return maxlen;\r
+}\r
+\r
+#endif\r
+\r
+#ifdef TEST\r
+#include <_PDCLIB_test.h>\r
+\r
+int main( void )\r
+{\r
+#ifndef REGTEST\r
+    TESTCASE( strnlen( abcde, 5 ) == 5 );\r
+    TESTCASE( strnlen( abcde, 3 ) == 3 )\r
+    TESTCASE( strnlen( "", SIZE_MAX ) == 0 );\r
+#endif\r
+    return TEST_RESULTS;\r
+}\r
+#endif\r
index 8c688e903cc0c77e352e8eb858b05b262e37d14e..599653a384539dd632cb0e0b8ee4a1195f152b97 100644 (file)
@@ -184,6 +184,13 @@ char * strerror( int errnum ) _PDCLIB_nothrow;
 */
 size_t strlen( const char * s ) _PDCLIB_nothrow;
 
+#if _PDCLIB_POSIX_MIN(2008098L)
+/* Returns the length of the string s (excluding terminating '\0') or maxlen if
+ * no terminating '\0' is found in the first maxlen characters.
+ */
+size_t strnlen( const char * s, size_t maxlen ) _PDCLIB_nothrow;
+#endif
+
 #if _PDCLIB_POSIX_MIN(2008098L) || _PDCLIB_XOPEN_MIN(0)
 char * strdup( const char* src ) _PDCLIB_nothrow;
 char * strndup( const char* src, size_t n ) _PDCLIB_nothrow;