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
34 const char *teststr = "Hello, world";
\r
35 const char *teststr2 = "\xFE\x8C\n";
\r
36 char *testres, *testres2;
\r
38 TESTCASE(testres = strndup(teststr, 5));
\r
39 TESTCASE(testres2 = strndup(teststr2, 1));
\r
40 TESTCASE(strcmp(testres, teststr) != 0);
\r
41 TESTCASE(strncmp(testres, teststr, 5) == 0);
\r
42 TESTCASE(strcmp(testres2, teststr2) != 0);
\r
43 TESTCASE(strncmp(testres2, teststr2, 1) == 0);
\r
46 TESTCASE(testres = strndup(teststr, 20));
\r
47 TESTCASE(testres2 = strndup(teststr2, 5));
\r
48 TESTCASE(strcmp(testres, teststr) == 0);
\r
49 TESTCASE(strcmp(testres2, teststr2) == 0);
\r
53 return TEST_RESULTS;
\r