X-Git-Url: https://pd.if.org/git/?p=pdclib.old;a=blobdiff_plain;f=functions%2Fstring%2Fstrlcpy.c;fp=functions%2Fstring%2Fstrlcpy.c;h=73b9165dad1199fa3734073f897ca3583ed8afc7;hp=0000000000000000000000000000000000000000;hb=1088cf7324e77983cbfbd8cab9783408d547882a;hpb=cd6cfe0f578c4f744ddc9a342243aff6b42f8027 diff --git a/functions/string/strlcpy.c b/functions/string/strlcpy.c new file mode 100644 index 0000000..73b9165 --- /dev/null +++ b/functions/string/strlcpy.c @@ -0,0 +1,53 @@ +/* strlcpy( + char *_PDCLIB_restrict _Dst, + const char *_PDCLIB_restrict _Src, + size_t _DstSize) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#include + +#ifndef REGTEST + +#pragma weak strlcpy = _PDCLIB_strlcpy +size_t _PDCLIB_strlcpy( + char *restrict dst, + const char *restrict src, + size_t dstsize); + +size_t _PDCLIB_strlcpy( + char *restrict dst, + const char *restrict src, + size_t dstsize) +{ + size_t needed = 0; + while(needed < dstsize && (dst[needed] = src[needed])) + needed++; + + while(src[needed++]); + + if (needed > dstsize && dstsize) + dst[dstsize - 1] = 0; + + return needed; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + char destbuf[10]; + TESTCASE_NOREG( strlcpy(NULL, "a", 0) == 2 ); + TESTCASE_NOREG( strlcpy(destbuf, "a", 10) == 2 ); + TESTCASE_NOREG( strcmp(destbuf, "a") == 0 ); + TESTCASE_NOREG( strlcpy(destbuf, "helloworld", 10) == 11 ); + TESTCASE_NOREG( strcmp(destbuf, "helloworl") == 0 ); + return TEST_RESULTS; +} + +#endif