]> pd.if.org Git - pdclib/blob - functions/string/strdup.c
* Change the style of inclusion of the internal/ headers. Modern preprocessors
[pdclib] / functions / string / strdup.c
1 /* [XSI] char* strdup(const char *)\r
2 \r
3    This file is part of the Public Domain C Library (PDCLib).\r
4    Permission is granted to use, modify, and / or redistribute at will.\r
5 */\r
6 \r
7 #include <string.h>\r
8 #include <stdlib.h>\r
9 \r
10 #ifndef REGTEST\r
11 \r
12 #pragma weak strdup\r
13 char *strdup(const char *s)\r
14 {\r
15     char* ns = NULL;\r
16     if(s) {\r
17         size_t len = strlen(s) + 1;\r
18         ns = malloc(len);\r
19         if(ns)\r
20             strncpy(ns, s, len);\r
21     }\r
22     return ns;\r
23 }\r
24 \r
25 #endif\r
26 \r
27 #ifdef TEST\r
28 #include <_PDCLIB_test.h>\r
29 \r
30 int main( void )\r
31 {\r
32     const char *teststr  = "Hello, world";\r
33     const char *teststr2 = "An alternative test string with non-7-bit characters \xFE\x8C\n";\r
34     char *testres, *testres2;\r
35 \r
36     TESTCASE(testres  = strdup(teststr));\r
37     TESTCASE(testres2 = strdup(teststr2));\r
38     TESTCASE(strcmp(testres, teststr) == 0);\r
39     TESTCASE(strcmp(testres2, teststr2) == 0);\r
40     free(testres);\r
41     free(testres2);\r
42     \r
43     return TEST_RESULTS;\r
44 }\r
45 \r
46 #endif\r