]> pd.if.org Git - pdclib/blobdiff - functions/string/strdup.c
* Change the style of inclusion of the internal/ headers. Modern preprocessors
[pdclib] / functions / string / strdup.c
diff --git a/functions/string/strdup.c b/functions/string/strdup.c
new file mode 100644 (file)
index 0000000..ac2200e
--- /dev/null
@@ -0,0 +1,46 @@
+/* [XSI] char* strdup(const char *)\r
+\r
+   This file is part of the Public Domain C Library (PDCLib).\r
+   Permission is granted to use, modify, and / or redistribute at will.\r
+*/\r
+\r
+#include <string.h>\r
+#include <stdlib.h>\r
+\r
+#ifndef REGTEST\r
+\r
+#pragma weak strdup\r
+char *strdup(const char *s)\r
+{\r
+    char* ns = NULL;\r
+    if(s) {\r
+        size_t len = strlen(s) + 1;\r
+        ns = malloc(len);\r
+        if(ns)\r
+            strncpy(ns, s, len);\r
+    }\r
+    return ns;\r
+}\r
+\r
+#endif\r
+\r
+#ifdef TEST\r
+#include <_PDCLIB_test.h>\r
+\r
+int main( void )\r
+{\r
+    const char *teststr  = "Hello, world";\r
+    const char *teststr2 = "An alternative test string with non-7-bit characters \xFE\x8C\n";\r
+    char *testres, *testres2;\r
+\r
+    TESTCASE(testres  = strdup(teststr));\r
+    TESTCASE(testres2 = strdup(teststr2));\r
+    TESTCASE(strcmp(testres, teststr) == 0);\r
+    TESTCASE(strcmp(testres2, teststr2) == 0);\r
+    free(testres);\r
+    free(testres2);\r
+    \r
+    return TEST_RESULTS;\r
+}\r
+\r
+#endif\r