diff --git a/CMakeLists.txt b/CMakeLists.txt index fa6818f8ea7c2af7da871e03a667fa17b561618d..aa23113b3386adc757c750e248754ff1eea9d698 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -126,6 +126,7 @@ set(CMAKE_REQUIRED_LIBRARIES "") check_function_exists(clock_gettime HAVE_CLOCK_GETTIME_WITHOUT_RT) check_symbol_exists(__get_cpuid cpuid.h HAVE_CPUID) +check_symbol_exists(strlcpy string.h HAVE_STRLCPY) # Checks for libev include(CheckStructHasMember) diff --git a/src/lib/core/util.c b/src/lib/core/util.c index dfce317f07ca0ad12eef87ec9af419ddcd38c88a..f29886105819a2e50ed097037d84c3a93b8b3a18 100644 --- a/src/lib/core/util.c +++ b/src/lib/core/util.c @@ -250,6 +250,20 @@ int2str(long long int val) return buf; } +#ifndef HAVE_STRLCPY +size_t +strlcpy(char *dst, const char *src, size_t size) +{ + size_t src_len = strlen(src); + if (size != 0) { + size_t len = (src_len >= size) ? size - 1 : src_len; + memcpy(dst, src, len); + dst[len] = '\0'; + } + return src_len; +} +#endif + int utf8_check_printable(const char *start, size_t length) { diff --git a/src/trivia/config.h.cmake b/src/trivia/config.h.cmake index 89e0d39c635368766411fd81f42ca5e52da25758..21efb978edf555e3788571e086921b63c7e27b42 100644 --- a/src/trivia/config.h.cmake +++ b/src/trivia/config.h.cmake @@ -47,6 +47,11 @@ */ #cmakedefine HAVE_CPUID 1 +/** + * Defined if strlcpy() string extension helper present. + */ + #cmakedefine HAVE_STRLCPY 1 + /* * Defined if gcov instrumentation should be enabled. */ diff --git a/src/trivia/util.h b/src/trivia/util.h index da5a3705ee4436072a297c50d6e7f29d089e9948..15f9881db873c32e094c9c740537c9fd8a999cc1 100644 --- a/src/trivia/util.h +++ b/src/trivia/util.h @@ -450,6 +450,21 @@ fpconv_strtod(const char *nptr, char **endptr) return strtod(nptr, endptr); } +#ifndef HAVE_STRLCPY +/** + * Copy string. Unlike @a strncpy the result string + * is always null-terminated. + * + * @param dst destination buffer. + * @param src source string. + * @param size destination buffer size. + * + * @return size of @a src string. + */ +size_t +strlcpy(char *dst, const char *src, size_t size); +#endif + /** * Check that @a str is valid utf-8 sequence and can be printed * unescaped.