Issue 150 - Check whether platform provides strlcpy & strlcat

or if we need to provide our own.
This commit is contained in:
wb2osz 2020-12-30 22:32:09 -05:00
parent 07fdc7544f
commit 1d67b44669
3 changed files with 54 additions and 30 deletions

View File

@ -6,12 +6,31 @@ include_directories(
) )
if(LINUX) if(LINUX)
list(APPEND misc_SOURCES # Previously -
# Provide our own copy of strlcpy and strlcat # list(APPEND misc_SOURCES
# because they are not included with Linux. # # Provide our own copy of strlcpy and strlcat
${CUSTOM_MISC_DIR}/strlcpy.c # # because they are not included with Linux.
${CUSTOM_MISC_DIR}/strlcat.c # ${CUSTOM_MISC_DIR}/strlcpy.c
) # ${CUSTOM_MISC_DIR}/strlcat.c
# )
# It seems that Alpine Linux and Void Linux have strlcpy and
# strlcat so we need to handle the situation more delicately.
# When doing it this way, there is probably no reason to
# distinguish between Linux and BSD-like systems here.
# If we kept going, the same thing could be done for each
# of the functions and no OS check would be needed.
if (NOT HAVE_STRLCPY)
list(APPEND misc_SOURCES
${CUSTOM_MISC_DIR}/strlcpy.c
)
endif()
if (NOT HAVE_STRLCAT)
list(APPEND misc_SOURCES
${CUSTOM_MISC_DIR}/strlcat.c
)
endif()
add_library(misc STATIC add_library(misc STATIC
${misc_SOURCES} ${misc_SOURCES}

View File

@ -1,3 +1,16 @@
include(CheckSymbolExists)
# Some platforms provide their own strlcpy & strlcat. (BSD, MacOSX)
# Others don't so we provide our own. (Most, but not all Linux)
# Define the preprocessor macro so libgps does not supply its own version.
check_symbol_exists(strlcpy string.h HAVE_STRLCPY)
if(HAVE_STRLCPY)
add_compile_options(-DHAVE_STRLCPY)
endif()
check_symbol_exists(strlcat string.h HAVE_STRLCAT)
if(HAVE_STRLCAT)
add_compile_options(-DHAVE_STRLCAT)
endif()
# global includes # global includes
# not ideal but not so slow # not ideal but not so slow
# otherwise use target_include_directories # otherwise use target_include_directories

View File

@ -176,6 +176,7 @@
#define DW_METERS_TO_FEET(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 3.2808399) #define DW_METERS_TO_FEET(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 3.2808399)
#define DW_FEET_TO_METERS(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 0.3048) #define DW_FEET_TO_METERS(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 0.3048)
#define DW_KM_TO_MILES(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 0.621371192) #define DW_KM_TO_MILES(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 0.621371192)
#define DW_MILES_TO_KM(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 1.609344)
#define DW_KNOTS_TO_MPH(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 1.15077945) #define DW_KNOTS_TO_MPH(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 1.15077945)
#define DW_KNOTS_TO_METERS_PER_SEC(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 0.51444444444) #define DW_KNOTS_TO_METERS_PER_SEC(x) ((x) == G_UNKNOWN ? G_UNKNOWN : (x) * 0.51444444444)
@ -278,43 +279,34 @@ char *strsep(char **stringp, const char *delim);
char *strtok_r(char *str, const char *delim, char **saveptr); char *strtok_r(char *str, const char *delim, char **saveptr);
#endif #endif
// Don't recall why for everyone. // Don't recall why I added this for everyone rather than only for Windows.
char *strcasestr(const char *S, const char *FIND); char *strcasestr(const char *S, const char *FIND);
#if defined(__OpenBSD__) || defined(__FreeBSD__) || defined(__APPLE__) // cmake determines whether strlcpy and strlcat are available
// or if we need to supply our own.
// strlcpy and strlcat should be in string.h and the C library. #define DEBUG_STRL 1 // Extra Debug version when using our own strlcpy, strlcat.
#else // Use our own copy
// These prevent /usr/include/gps.h from providing its own definition.
#define HAVE_STRLCAT 1
#define HAVE_STRLCPY 1
#define DEBUG_STRL 1
#ifndef HAVE_STRLCPY // Need to supply our own.
#if DEBUG_STRL #if DEBUG_STRL
#define strlcpy(dst,src,siz) strlcpy_debug(dst,src,siz,__FILE__,__func__,__LINE__) #define strlcpy(dst,src,siz) strlcpy_debug(dst,src,siz,__FILE__,__func__,__LINE__)
#define strlcat(dst,src,siz) strlcat_debug(dst,src,siz,__FILE__,__func__,__LINE__)
size_t strlcpy_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz, const char *file, const char *func, int line); size_t strlcpy_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz, const char *file, const char *func, int line);
size_t strlcat_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz, const char *file, const char *func, int line);
#else #else
#define strlcpy(dst,src,siz) strlcpy_debug(dst,src,siz) #define strlcpy(dst,src,siz) strlcpy_debug(dst,src,siz)
#define strlcat(dst,src,siz) strlcat_debug(dst,src,siz)
size_t strlcpy_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz); size_t strlcpy_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz);
size_t strlcat_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz);
#endif /* DEBUG_STRL */ #endif /* DEBUG_STRL */
#endif
#endif /* BSD or Apple */ #ifndef HAVE_STRLCAT // Need to supply our own.
#if DEBUG_STRL
#define strlcat(dst,src,siz) strlcat_debug(dst,src,siz,__FILE__,__func__,__LINE__)
size_t strlcat_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz, const char *file, const char *func, int line);
#else
#define strlcat(dst,src,siz) strlcat_debug(dst,src,siz)
size_t strlcat_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz);
#endif /* DEBUG_STRL */
#endif
#endif /* ifndef DIREWOLF_H */ #endif /* ifndef DIREWOLF_H */