X-Git-Url: https://pd.if.org/git/?p=pdclib;a=blobdiff_plain;f=functions%2Fstring%2Fstrndup.c;h=b7baca2cc506895abd974618c6b50c0fb4e7087f;hp=446fb5d6a6ac87e0fc9aeed516a27ae81931ac3b;hb=abc15df6b9fae3374d24c7cf5c3ab94c605b2a6d;hpb=8894c921674bb116d0a7b8f23a55311e7a768019 diff --git a/functions/string/strndup.c b/functions/string/strndup.c index 446fb5d..b7baca2 100644 --- a/functions/string/strndup.c +++ b/functions/string/strndup.c @@ -1,63 +1,63 @@ -/* [XSI] char * strndup( const char *, size_t ) - - This file is part of the Public Domain C Library (PDCLib). - Permission is granted to use, modify, and / or redistribute at will. -*/ - -#ifdef REGTEST -#define _POSIX_C_SOURCE 200809L -#endif - -#include -#include - -#ifndef REGTEST - -char *strndup( const char * s, size_t len ) -{ - char* ns = NULL; - if(s) { - ns = malloc(len + 1); - if(ns) { - ns[len] = 0; - // strncpy to be pedantic about modification in multithreaded - // applications - return strncpy(ns, s, len); - } - } - return ns; -} - -#endif - -#ifdef TEST -#include "_PDCLIB_test.h" - -int main( void ) -{ -#ifndef REGTEST - /* Missing on Windows. Maybe use conditionals? */ - const char *teststr = "Hello, world"; - const char *teststr2 = "\xFE\x8C\n"; - char *testres, *testres2; - - TESTCASE((testres = strndup(teststr, 5)) != NULL); - TESTCASE((testres2 = strndup(teststr2, 1)) != NULL); - TESTCASE(strcmp(testres, teststr) != 0); - TESTCASE(strncmp(testres, teststr, 5) == 0); - TESTCASE(strcmp(testres2, teststr2) != 0); - TESTCASE(strncmp(testres2, teststr2, 1) == 0); - free(testres); - free(testres2); - TESTCASE((testres = strndup(teststr, 20)) != NULL); - TESTCASE((testres2 = strndup(teststr2, 5)) != NULL); - TESTCASE(strcmp(testres, teststr) == 0); - TESTCASE(strcmp(testres2, teststr2) == 0); - free(testres); - free(testres2); -#endif - - return TEST_RESULTS; -} - -#endif +/* [XSI] char * strndup( const char *, size_t ) + + This file is part of the Public Domain C Library (PDCLib). + Permission is granted to use, modify, and / or redistribute at will. +*/ + +#ifdef REGTEST +#define _POSIX_C_SOURCE 200809L +#endif + +#include +#include + +#ifndef REGTEST + +char *strndup( const char * s, size_t len ) +{ + char* ns = NULL; + if(s) { + ns = malloc(len + 1); + if(ns) { + ns[len] = 0; + // strncpy to be pedantic about modification in multithreaded + // applications + return strncpy(ns, s, len); + } + } + return ns; +} + +#endif + +#ifdef TEST +#include "_PDCLIB_test.h" + +int main( void ) +{ +#ifndef REGTEST + /* Missing on Windows. Maybe use conditionals? */ + const char *teststr = "Hello, world"; + const char *teststr2 = "\xFE\x8C\n"; + char *testres, *testres2; + + TESTCASE((testres = strndup(teststr, 5)) != NULL); + TESTCASE((testres2 = strndup(teststr2, 1)) != NULL); + TESTCASE(strcmp(testres, teststr) != 0); + TESTCASE(strncmp(testres, teststr, 5) == 0); + TESTCASE(strcmp(testres2, teststr2) != 0); + TESTCASE(strncmp(testres2, teststr2, 1) == 0); + free(testres); + free(testres2); + TESTCASE((testres = strndup(teststr, 20)) != NULL); + TESTCASE((testres2 = strndup(teststr2, 5)) != NULL); + TESTCASE(strcmp(testres, teststr) == 0); + TESTCASE(strcmp(testres2, teststr2) == 0); + free(testres); + free(testres2); +#endif + + return TEST_RESULTS; +} + +#endif