From a542401946f00cdada5c61c32eeb67034eaef15a Mon Sep 17 00:00:00 2001 From: "ehlertjd@gmail.com" Date: Thu, 15 Sep 2016 16:10:49 -0500 Subject: [PATCH] Second fix attempt for issue 349. This is the only way to safely call clock_gettime (that I know of) when compiling for OSX 10.12 or iOS 10. Refs #349. --- src/civetweb.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/civetweb.c b/src/civetweb.c index 799f063f..9abe1dd7 100644 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -136,23 +136,11 @@ mg_static_assert(sizeof(void *) >= sizeof(int), "data type size check"); #include #include -/* Determine if the current OSX version supports clock_gettime */ -#ifdef __APPLE__ -#include -#ifndef MAC_OS_X_VERSION_10_12 -#define MAC_OS_X_VERSION_10_12 101200 -#endif -#endif -#define CIVETWEB_APPLE_HAVE_CLOCK_GETTIME \ - defined(__APPLE__) \ - && MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_12 - -#if !(CIVETWEB_APPLE_HAVE_CLOCK_GETTIME) /* clock_gettime is not implemented on OSX prior to 10.12 */ -int clock_gettime(int clk_id, struct timespec *t); +int _civet_clock_gettime(int clk_id, struct timespec *t); int -clock_gettime(int clk_id, struct timespec *t) +_civet_clock_gettime(int clk_id, struct timespec *t) { memset(t, 0, sizeof(*t)); if (clk_id == CLOCK_REALTIME) { @@ -192,7 +180,25 @@ clock_gettime(int clk_id, struct timespec *t) } return -1; /* EINVAL - Clock ID is unknown */ } + +/* if clock_gettime is declared, then __CLOCK_AVAILABILITY will be defined */ +#ifdef __CLOCK_AVAILABILITY +/* If we compiled with Mac OSX 10.12 or later, then clock_gettime will be declared + * but it may be NULL at runtime. So we need to check before using it. */ +int _civet_safe_clock_gettime(int clk_id, struct timespec *t); + +int +_civet_safe_clock_gettime(int clk_id, struct timespec *t) { + if( clock_gettime ) { + return clock_gettime(clk_id, t); + } + return _civet_clock_gettime(clk_id, t); +} +#define clock_gettime _civet_safe_clock_gettime +#else +#define clock_gettime _civet_clock_gettime #endif + #endif