]> pd.if.org Git - pdclib/blobdiff - functions/string/strnlen.c
<string.h>: add POSIX 2008 extension strnlen (general utility + use by printf).
[pdclib] / functions / string / strnlen.c
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