1 /* [XSI] char* strndup(const char *, size_t)
\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
12 char *strndup( const char * s, size_t len )
\r
16 ns = malloc(len + 1);
\r
19 // strncpy to be pedantic about modification in multithreaded
\r
21 return strncpy(ns, s, len);
\r
30 #include <_PDCLIB_test.h>
\r
35 /* Missing on Windows. Maybe use conditionals? */
\r
36 const char *teststr = "Hello, world";
\r
37 const char *teststr2 = "\xFE\x8C\n";
\r
38 char *testres, *testres2;
\r
40 TESTCASE(testres = strndup(teststr, 5));
\r
41 TESTCASE(testres2 = strndup(teststr2, 1));
\r
42 TESTCASE(strcmp(testres, teststr) != 0);
\r
43 TESTCASE(strncmp(testres, teststr, 5) == 0);
\r
44 TESTCASE(strcmp(testres2, teststr2) != 0);
\r
45 TESTCASE(strncmp(testres2, teststr2, 1) == 0);
\r
48 TESTCASE(testres = strndup(teststr, 20));
\r
49 TESTCASE(testres2 = strndup(teststr2, 5));
\r
50 TESTCASE(strcmp(testres, teststr) == 0);
\r
51 TESTCASE(strcmp(testres2, teststr2) == 0);
\r
56 return TEST_RESULTS;
\r