From d326f16dd40013e8bea639ff95c2b249e7867e5e Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 9 Jun 2016 16:30:17 +0300 Subject: Eliminate the use of *_s() functions on Windows --- butl/timestamp.cxx | 23 +++++++++++++++++++++-- tests/process/driver.cxx | 4 ++-- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/butl/timestamp.cxx b/butl/timestamp.cxx index bf02829..4c226fe 100644 --- a/butl/timestamp.cxx +++ b/butl/timestamp.cxx @@ -71,13 +71,27 @@ using namespace details; // original function is absent. However, MinGW GCC can sometimes provide them. // And so to avoid name clashes we hide them in the details namespace. // +// Previously we have used gmtime_s() and localtime_s() for gmtime() and +// localtime() implementations for Windows, but that required Security-Enhanced +// version of CRT to be present, which is not always the case. In particular if +// MinGW is configured with --disable-secure-api option then declarations of +// *_s() functions are not available. So we use ::gmtime() and ::localtime() +// for that purpose. Note that according to MSDN "gmtime and localtime all use +// one common tm structure per thread for the conversion", which mean that they +// are thread-safe. +// namespace details { static tm* gmtime (const time_t* t, tm* r) { #ifdef _WIN32 - return gmtime_s (r, t) != 0 ? nullptr : r; + const tm* gt (::gmtime (t)); + if (gt == nullptr) + return nullptr; + + *r = *gt; + return r; #else return gmtime_r (t, r); #endif @@ -87,7 +101,12 @@ namespace details localtime (const time_t* t, tm* r) { #ifdef _WIN32 - return localtime_s (r, t) != 0 ? nullptr : r; + const tm* lt (::localtime (t)); + if (lt == nullptr) + return nullptr; + + *r = *lt; + return r; #else return localtime_r (t, r); #endif diff --git a/tests/process/driver.cxx b/tests/process/driver.cxx index efb4ca3..741340c 100644 --- a/tests/process/driver.cxx +++ b/tests/process/driver.cxx @@ -2,7 +2,7 @@ // copyright : Copyright (c) 2014-2016 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file -#include // getenv(), setenv(), _putenv_s() +#include // getenv(), setenv(), _putenv() #include #include @@ -306,7 +306,7 @@ try #ifndef _WIN32 assert (setenv ("PATH", paths.c_str (), 1) == 0); #else - assert (_putenv_s ("PATH", paths.c_str ()) == 0); + assert (_putenv (("PATH=" + paths).c_str ()) == 0); #endif dir_path::current (fp.directory () / dir_path ("..")); -- cgit v1.1