]> pd.if.org Git - pdclib.old/blobdiff - functions/string/strlcpy.c
Add /strl(cpy|cat)/ under _BSD_SOURCE guards
[pdclib.old] / functions / string / strlcpy.c
diff --git a/functions/string/strlcpy.c b/functions/string/strlcpy.c
new file mode 100644 (file)
index 0000000..73b9165
--- /dev/null
@@ -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 <string.h>
+
+#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