X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstring%2Fstrnlen.c;fp=functions%2Fstring%2Fstrnlen.c;h=e7eab5712bfbd6b13e335c9750956367c2061549;hb=70713966ce3d6ef8965458e804fc47e2640ab51f;hp=0000000000000000000000000000000000000000;hpb=c3d04bb8ba4b67e327d2de9f7ffe47ee7c2c0d72;p=pdclib diff --git a/functions/string/strnlen.c b/functions/string/strnlen.c new file mode 100644 index 0000000..e7eab57 --- /dev/null +++ b/functions/string/strnlen.c @@ -0,0 +1,38 @@ +/* $Id$ */ + +/* strnlen( const char *, size_t len ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include +#include + +#ifndef REGTEST + +size_t strnlen( const char * s, size_t maxlen ) +{ + for( size_t len = 0; len != maxlen; len++ ) + { + if(s[len] == '\0') + return len - 1; + } + return maxlen; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ +#ifndef REGTEST + TESTCASE( strnlen( abcde, 5 ) == 5 ); + TESTCASE( strnlen( abcde, 3 ) == 3 ) + TESTCASE( strnlen( "", SIZE_MAX ) == 0 ); +#endif + return TEST_RESULTS; +} +#endif