]> pd.if.org Git - pdclib/blob - functions/string/strdup.c
RegTest gives warning about implicitly declared functions. Fixed.
[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 #ifdef REGTEST\r
8 #define _POSIX_C_SOURCE 200809L\r
9 #endif\r
10 \r
11 #include <string.h>\r
12 #include <stdlib.h>\r
13 \r
14 #ifndef REGTEST\r
15 \r
16 char *strdup(const char *s)\r
17 {\r
18     char* ns = NULL;\r
19     if(s) {\r
20         size_t len = strlen(s) + 1;\r
21         ns = malloc(len);\r
22         if(ns)\r
23             strncpy(ns, s, len);\r
24     }\r
25     return ns;\r
26 }\r
27 \r
28 #endif\r
29 \r
30 #ifdef TEST\r
31 #include <_PDCLIB_test.h>\r
32 \r
33 int main( void )\r
34 {\r
35     const char *teststr  = "Hello, world";\r
36     const char *teststr2 = "An alternative test string with non-7-bit characters \xFE\x8C\n";\r
37     char *testres, *testres2;\r
38 \r
39     TESTCASE((testres  = strdup(teststr)) != NULL);\r
40     TESTCASE((testres2 = strdup(teststr2)) != NULL);\r
41     TESTCASE(strcmp(testres, teststr) == 0);\r
42     TESTCASE(strcmp(testres2, teststr2) == 0);\r
43     free(testres);\r
44     free(testres2);\r
45 \r
46     return TEST_RESULTS;\r
47 }\r
48 \r
49 #endif\r