From: Owen Shepherd Date: Wed, 22 Oct 2014 22:11:32 +0000 (+0100) Subject: Add /strl(cpy|cat)/ under _BSD_SOURCE guards X-Git-Url: https://pd.if.org/git/?p=pdclib.old;a=commitdiff_plain;h=1088cf7324e77983cbfbd8cab9783408d547882a Add /strl(cpy|cat)/ under _BSD_SOURCE guards --- diff --git a/functions/string/strlcat.c b/functions/string/strlcat.c new file mode 100644 index 0000000..34a47d4 --- /dev/null +++ b/functions/string/strlcat.c @@ -0,0 +1,60 @@ +/* strlcat( + char *_PDCLIB_restrict _Dst, + const char *_PDCLIB_restict _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 strlcat = _PDCLIB_strlcat +size_t _PDCLIB_strlcat( + char *restrict dst, + const char *restrict src, + size_t dstsize); + +size_t _PDCLIB_strlcat( + char *restrict dst, + const char *restrict src, + size_t dstsize) +{ + size_t needed = 0; + size_t j = 0; + + while(dst[needed]) needed++; + + while(needed < dstsize && (dst[needed] = src[j])) + needed++, j++; + + while(src[j++]) needed++; + needed++; + + if (needed > dstsize && dstsize) + dst[dstsize - 1] = 0; + + return needed; +} + +#endif + +#ifdef TEST +#include <_PDCLIB_test.h> + +int main( void ) +{ + char dstbuf[16]; + + strcpy(dstbuf, "hi"); + TESTCASE_NOREG( strlcat(dstbuf, "", 16) == 3 ); + TESTCASE_NOREG( strlcat(dstbuf, "hi", 16) == 5 ); + TESTCASE_NOREG( strlcat(dstbuf, "hello, world", 16) == 17 ); + TESTCASE_NOREG( strlcat(dstbuf, "hi", 16) == 18 ); + TESTCASE_NOREG( strcmp(dstbuf, "hihihello, worl") == 0); + return TEST_RESULTS; +} + +#endif 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 diff --git a/includes/string.h b/includes/string.h index 599653a..27441df 100644 --- a/includes/string.h +++ b/includes/string.h @@ -196,5 +196,17 @@ char * strdup( const char* src ) _PDCLIB_nothrow; char * strndup( const char* src, size_t n ) _PDCLIB_nothrow; #endif +#if _PDCLIB_BSD_SOURCE +size_t strlcpy( + char *_PDCLIB_restrict _Dst, + const char *_PDCLIB_restrict _Src, + size_t _DstSize) _PDCLIB_nothrow; + +size_t strlcat( + char *_PDCLIB_restrict _Dst, + const char *_PDCLIB_restrict _Src, + size_t _DstSize) _PDCLIB_nothrow; +#endif + _PDCLIB_END_EXTERN_C #endif