1 /* [XSI] char * strndup( const char *, size_t )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
8 #define _POSIX_C_SOURCE 200809L
16 char *strndup( const char * s, size_t len )
23 // strncpy to be pedantic about modification in multithreaded
25 return strncpy(ns, s, len);
34 #include "_PDCLIB_test.h"
39 /* Missing on Windows. Maybe use conditionals? */
40 const char *teststr = "Hello, world";
41 const char *teststr2 = "\xFE\x8C\n";
42 char *testres, *testres2;
44 TESTCASE((testres = strndup(teststr, 5)) != NULL);
45 TESTCASE((testres2 = strndup(teststr2, 1)) != NULL);
46 TESTCASE(strcmp(testres, teststr) != 0);
47 TESTCASE(strncmp(testres, teststr, 5) == 0);
48 TESTCASE(strcmp(testres2, teststr2) != 0);
49 TESTCASE(strncmp(testres2, teststr2, 1) == 0);
52 TESTCASE((testres = strndup(teststr, 20)) != NULL);
53 TESTCASE((testres2 = strndup(teststr2, 5)) != NULL);
54 TESTCASE(strcmp(testres, teststr) == 0);
55 TESTCASE(strcmp(testres2, teststr2) == 0);