X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstring%2Fstrnlen.c;fp=functions%2Fstring%2Fstrnlen.c;h=e7eab5712bfbd6b13e335c9750956367c2061549;hb=46e39489614dc4cacbcf60657d21edb954ee1f53;hp=0000000000000000000000000000000000000000;hpb=3ea605404733209c32d8e43915fa3790adbcb1c9;p=pdclib.old 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