Util: Add strdup implementation for platforms without it
Jeffrey Pfau jeffrey@endrift.com
Sun, 14 Jun 2015 23:25:49 -0700
3 files changed,
19 insertions(+),
0 deletions(-)
M
CMakeLists.txt
→
CMakeLists.txt
@@ -211,6 +211,7 @@ enable_language(ASM)
endif() include(CheckFunctionExists) +check_function_exists(strdup HAVE_STRDUP) check_function_exists(strndup HAVE_STRNDUP) check_function_exists(snprintf_l HAVE_SNPRINTF_L) if(CMAKE_SYSTEM_NAME STREQUAL "Linux")@@ -222,6 +223,10 @@ endif()
check_function_exists(newlocale HAVE_NEWLOCALE) check_function_exists(freelocale HAVE_FREELOCALE) check_function_exists(uselocale HAVE_USELOCALE) + +if(HAVE_STRDUP) + add_definitions(-DHAVE_STRDUP) +endif() if(HAVE_STRNDUP) add_definitions(-DHAVE_STRNDUP)
M
src/util/string.c
→
src/util/string.c
@@ -17,6 +17,16 @@ return out;
} #endif +#ifndef HAVE_STRDUP +char* strdup(const char* str) { + size_t len = strlen(str); + char* out = malloc(len + 1); + strncpy(out, str, len); + out[len] = '\0'; + return out; +} +#endif + char* strnrstr(const char* restrict haystack, const char* restrict needle, size_t len) { char* last = 0; const char* next = haystack;
M
src/util/string.h
→
src/util/string.h
@@ -13,6 +13,10 @@ // This is sometimes a macro
char* strndup(const char* start, size_t len); #endif +#ifndef strdup +char* strdup(const char* str); +#endif + char* strnrstr(const char* restrict s1, const char* restrict s2, size_t len); int utfcmp(const uint16_t* utf16, const char* utf8, size_t utf16Length, size_t utf8Length);