From 1d67b446694e122552b4e7387850e4e9a23d48d3 Mon Sep 17 00:00:00 2001 From: wb2osz Date: Wed, 30 Dec 2020 22:32:09 -0500 Subject: [PATCH] Issue 150 - Check whether platform provides strlcpy & strlcat or if we need to provide our own. --- external/misc/CMakeLists.txt | 31 ++++++++++++++++++++++------ src/CMakeLists.txt | 13 ++++++++++++ src/direwolf.h | 40 +++++++++++++++--------------------- 3 files changed, 54 insertions(+), 30 deletions(-) diff --git a/external/misc/CMakeLists.txt b/external/misc/CMakeLists.txt index 07b10b2..16125d0 100644 --- a/external/misc/CMakeLists.txt +++ b/external/misc/CMakeLists.txt @@ -6,12 +6,31 @@ include_directories( ) if(LINUX) - list(APPEND misc_SOURCES - # Provide our own copy of strlcpy and strlcat - # because they are not included with Linux. - ${CUSTOM_MISC_DIR}/strlcpy.c - ${CUSTOM_MISC_DIR}/strlcat.c - ) +# Previously - +# list(APPEND misc_SOURCES +# # Provide our own copy of strlcpy and strlcat +# # because they are not included with Linux. +# ${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 ${misc_SOURCES} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 46d3ac7..af747e5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 # not ideal but not so slow # otherwise use target_include_directories diff --git a/src/direwolf.h b/src/direwolf.h index efc329b..b43e5ff 100644 --- a/src/direwolf.h +++ b/src/direwolf.h @@ -176,6 +176,7 @@ #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_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_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); #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); -#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. - -#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 +#define DEBUG_STRL 1 // Extra Debug version when using our own strlcpy, strlcat. +#ifndef HAVE_STRLCPY // Need to supply our own. #if DEBUG_STRL - #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 strlcat_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz, const char *file, const char *func, int line); - #else - #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 strlcat_debug(char *__restrict__ dst, const char *__restrict__ src, size_t siz); - #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 */