Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/MediaArea/ZenLib.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÉrico Rolim <erico.erc@gmail.com>2021-01-28 01:52:20 +0300
committerÉrico Rolim <erico.erc@gmail.com>2021-01-29 03:32:19 +0300
commite7be9f2eb8e46da62c865467f6bc7a4f3d588f5a (patch)
tree9b817d682a9bfd32470fe799349e4f1296f2f5c0
parentdfbded229377b2115cce66e191b04306b855d8b1 (diff)
Fix checks for gmtime_r and localtime_r availability.
Actually check for the functions in configure.ac and CMakeLists.txt, which is the only way of definitively testing for them, since feature test macros such as _POSIX_C_SOURCE are supposed to be used to tell system headers what functions should be exposed, and the application shouldn't check their values. Add a warning for cases where gmtime() or localtime() are used. Also fix Http_Cookies where gmtime() was still being used.
-rw-r--r--Project/CMake/CMakeLists.txt10
-rw-r--r--Project/GNU/Library/configure.ac5
-rw-r--r--Source/ZenLib/Format/Http/Http_Cookies.cpp16
-rw-r--r--Source/ZenLib/Ztring.cpp10
4 files changed, 38 insertions, 3 deletions
diff --git a/Project/CMake/CMakeLists.txt b/Project/CMake/CMakeLists.txt
index 8e431b5..6a25aee 100644
--- a/Project/CMake/CMakeLists.txt
+++ b/Project/CMake/CMakeLists.txt
@@ -133,6 +133,16 @@ if((LONG_SIZE GREATER 4) AND (SIZE_T_SIZE EQUAL LONG_SIZE))
target_compile_definitions(zen PUBLIC SIZE_T_IS_LONG)
endif()
+include(CheckSymbolExists)
+check_symbol_exists(gmtime_r time.h HAVE_GMTIME_R)
+if(HAVE_GMTIME_R)
+ target_compile_definitions(zen PUBLIC HAVE_GMTIME_R)
+endif()
+check_symbol_exists(localtime_r time.h HAVE_LOCALTIME_R)
+if(HAVE_LOCALTIME_R)
+ target_compile_definitions(zen PUBLIC HAVE_LOCALTIME_R)
+endif()
+
target_include_directories(zen PUBLIC
$<BUILD_INTERFACE:${ZenLib_SOURCES_PATH}>
$<INSTALL_INTERFACE:${INCLUDE_INSTALL_DIR}>)
diff --git a/Project/GNU/Library/configure.ac b/Project/GNU/Library/configure.ac
index 1d8bb3e..cbed25b 100644
--- a/Project/GNU/Library/configure.ac
+++ b/Project/GNU/Library/configure.ac
@@ -254,6 +254,11 @@ dnl External libs
dnl
LDFLAGS="$LDFLAGS -lpthread"
+dnl -------------------------------------------------------------------------
+dnl Check if thread safe variants of time functions are available
+dnl
+AC_CHECK_FUNCS(gmtime_r localtime_r)
+
dnl #########################################################################
dnl ### Output
dnl #########################################################################
diff --git a/Source/ZenLib/Format/Http/Http_Cookies.cpp b/Source/ZenLib/Format/Http/Http_Cookies.cpp
index 1345aa0..eb41690 100644
--- a/Source/ZenLib/Format/Http/Http_Cookies.cpp
+++ b/Source/ZenLib/Format/Http/Http_Cookies.cpp
@@ -86,7 +86,21 @@ void Cookies::Create_Lines(std::ostream& Out)
if (Cookie->second.Expires!=(time_t)-1)
{
char Temp[200];
- if (strftime(Temp, 200, "%a, %d-%b-%Y %H:%M:%S GMT", gmtime(&Cookie->second.Expires)))
+ #if defined(HAVE_GMTIME_R)
+ struct tm Gmt_Temp;
+ struct tm *Gmt=gmtime_r(&Cookie->second.Expires, &Gmt_Temp);
+ #elif defined(_MSC_VER)
+ struct tm Gmt_Temp;
+ errno_t gmtime_s_Result=gmtime_s(&Gmt_Temp , &Cookie->second.Expires);
+ struct tm* Gmt=gmtime_s_Result?NULL:&Gmt_Temp;
+ #else
+ #ifdef __GNUC__
+ #warning "This version of ZenLib is not thread safe"
+ #endif
+ struct tm *Gmt=gmtime(&Cookie->second.Expires);
+ #endif
+
+ if (strftime(Temp, 200, "%a, %d-%b-%Y %H:%M:%S GMT", Gmt))
Out << "; expires=" << Temp;
}
if (!Cookie->second.Path.empty())
diff --git a/Source/ZenLib/Ztring.cpp b/Source/ZenLib/Ztring.cpp
index 21bbc58..31525b5 100644
--- a/Source/ZenLib/Ztring.cpp
+++ b/Source/ZenLib/Ztring.cpp
@@ -1312,7 +1312,7 @@ Ztring& Ztring::Date_From_Seconds_1970 (const int32s Value)
Ztring& Ztring::Date_From_Seconds_1970 (const int64s Value)
{
time_t Time=(time_t)Value;
- #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
+ #if defined(HAVE_GMTIME_R)
struct tm Gmt_Temp;
struct tm *Gmt=gmtime_r(&Time, &Gmt_Temp);
#elif defined(_MSC_VER)
@@ -1320,6 +1320,9 @@ Ztring& Ztring::Date_From_Seconds_1970 (const int64s Value)
errno_t gmtime_s_Result=gmtime_s(&Gmt_Temp , &Time);
struct tm* Gmt=gmtime_s_Result?NULL:&Gmt_Temp;
#else
+ #ifdef __GNUC__
+ #warning "This version of ZenLib is not thread safe"
+ #endif
struct tm *Gmt=gmtime(&Time);
#endif
if (!Gmt)
@@ -1352,7 +1355,7 @@ Ztring& Ztring::Date_From_Seconds_1970 (const int64s Value)
Ztring& Ztring::Date_From_Seconds_1970_Local (const int32u Value)
{
time_t Time=(time_t)Value;
- #if _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || _POSIX_SOURCE
+ #if defined(HAVE_LOCALTIME_R)
struct tm Gmt_Temp;
struct tm *Gmt=localtime_r(&Time, &Gmt_Temp);
#elif defined(_MSC_VER)
@@ -1360,6 +1363,9 @@ Ztring& Ztring::Date_From_Seconds_1970_Local (const int32u Value)
errno_t localtime_s_Result=localtime_s(&Gmt_Temp , &Time);
struct tm* Gmt=localtime_s_Result?NULL:&Gmt_Temp;
#else
+ #ifdef __GNUC__
+ #warning "This version of ZenLib is not thread safe"
+ #endif
struct tm *Gmt=localtime(&Time);
#endif
Ztring DateT;