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
8 #define _POSIX_C_SOURCE 200809L
\r
16 char *strndup( const char * s, size_t len )
\r
20 ns = malloc(len + 1);
\r
23 // strncpy to be pedantic about modification in multithreaded
\r
25 return strncpy(ns, s, len);
\r
34 #include <_PDCLIB_test.h>
\r
39 /* Missing on Windows. Maybe use conditionals? */
\r
40 const char *teststr = "Hello, world";
\r
41 const char *teststr2 = "\xFE\x8C\n";
\r
42 char *testres, *testres2;
\r
44 TESTCASE((testres = strndup(teststr, 5)) != NULL);
\r
45 TESTCASE((testres2 = strndup(teststr2, 1)) != NULL);
\r
46 TESTCASE(strcmp(testres, teststr) != 0);
\r
47 TESTCASE(strncmp(testres, teststr, 5) == 0);
\r
48 TESTCASE(strcmp(testres2, teststr2) != 0);
\r
49 TESTCASE(strncmp(testres2, teststr2, 1) == 0);
\r
52 TESTCASE((testres = strndup(teststr, 20)) != NULL);
\r
53 TESTCASE((testres2 = strndup(teststr2, 5)) != NULL);
\r
54 TESTCASE(strcmp(testres, teststr) == 0);
\r
55 TESTCASE(strcmp(testres2, teststr2) == 0);
\r
60 return TEST_RESULTS;
\r