Functions strcpy_s, strncpy_s and strcat_s must return errno_t

* Use cmake for determinating if system has above functions
* Compile our implementations only if system does not have these functions
* Define macro _TRUNCATE if is not defined
* Define errno_t if is not in any standard header include file
* Define prototypes of above functions only if system does not have them

* This commit fix compilation with ubuntu mingw-w64 cross compiler
This commit is contained in:
Pali Rohár 2013-02-05 15:28:21 +01:00
parent 42a30a661d
commit 606d5ef476
3 changed files with 52 additions and 8 deletions

View file

@ -614,7 +614,9 @@ find_package(Sqlite)
find_package(Doxygen)
find_package(SelfPackers)
include(CheckTypeSize)
include(CheckFunctionExists)
include(CheckSymbolExists)
# Windows RC compiler definitions
@ -781,9 +783,32 @@ if(CMAKE_COMPILER_IS_GNUCXX)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsigned-char")
endif()
check_type_size(errno_t ERRNOT)
check_function_exists("strcpy_s" HAVE_STRCPYS)
check_function_exists("strncpy_s" HAVE_STRNCPYS)
check_function_exists("strcasestr" HAVE_STRCASESTR)
check_function_exists("strnlen" HAVE_STRNLEN)
# mingw-w64 does not have strcat_s in any include file, but function symbol in library exists
# so rather check if we have strcat_s in string.h file
check_symbol_exists("strcat_s" "string.h" HAVE_STRCATS)
if(HAVE_ERRNOT)
add_definitions(-DHAVE_ERRNOT)
endif()
if(HAVE_STRCPYS)
add_definitions(-DHAVE_STRCPYS)
endif()
if(HAVE_STRNCPYS)
add_definitions(-DHAVE_STRNCPYS)
endif()
if(HAVE_STRCATS)
add_definitions(-DHAVE_STRCATS)
endif()
if(HAVE_STRCASESTR)
add_definitions(-DHAVE_STRCASESTR)
endif()

View file

@ -81,11 +81,26 @@ void clamp(T *value, T minValue, T maxValue)
-- Strings
----------------------------------------------------------------------------*/
#if !defined(_MSC_VER) || _MSC_VER < 1400
#include <string.h>
#ifndef _TRUNCATE
#define _TRUNCATE ((size_t)-1)
extern unsigned int strcpy_s(char *dst, size_t dstsize, const char *src);
extern unsigned int strncpy_s(char *dst, size_t dstsize, const char *src, size_t count);
extern unsigned int strcat_s(char *dst, size_t dstsize, const char *src);
#endif
#ifndef HAVE_ERRNOT
typedef int errno_t;
#endif
#ifndef HAVE_STRCPYS
extern errno_t strcpy_s(char *dst, size_t dstsize, const char *src);
#endif
#ifndef HAVE_STRNCPYS
extern errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count);
#endif
#ifndef HAVE_STRCATS
extern errno_t strcat_s(char *dst, size_t dstsize, const char *src);
#endif
#ifndef HAVE_STRCASESTR

View file

@ -150,8 +150,8 @@ long isqrt(long num)
-- Strings
----------------------------------------------------------------------------*/
#if !defined(_MSC_VER) || _MSC_VER < 1400
unsigned int strcpy_s(char *dst, size_t dstsize, const char *src)
#ifndef HAVE_STRCPYS
errno_t strcpy_s(char *dst, size_t dstsize, const char *src)
{
if (dst == NULL || src == NULL) {
return EINVAL;
@ -162,6 +162,7 @@ unsigned int strcpy_s(char *dst, size_t dstsize, const char *src)
strcpy(dst, src);
return 0;
}
#endif
#ifndef HAVE_STRNLEN
size_t strnlen(const char *str, size_t strsize)
@ -178,7 +179,8 @@ size_t strnlen(const char *str, size_t strsize)
}
#endif
unsigned int strncpy_s(char *dst, size_t dstsize, const char *src, size_t count)
#ifndef HAVE_STRNCPYS
errno_t strncpy_s(char *dst, size_t dstsize, const char *src, size_t count)
{
if (dst == NULL || src == NULL || dstsize == 0) {
return EINVAL;
@ -204,8 +206,10 @@ unsigned int strncpy_s(char *dst, size_t dstsize, const char *src, size_t count)
*dst = '\0';
return 0;
}
#endif
unsigned int strcat_s(char *dst, size_t dstsize, const char *src)
#ifndef STRCATS
errno_t strcat_s(char *dst, size_t dstsize, const char *src)
{
if (dst == NULL || src == NULL) {
return EINVAL;