From 858b2c58bf3d966dbcb8374977bcacd2f95e1b33 Mon Sep 17 00:00:00 2001 From: Takahiro Ueda Date: Thu, 13 Jul 2017 20:45:21 +0200 Subject: [PATCH] Use gettimeofday() instead of ftime() ftime() was removed in POSIX.1-2008. gettimeofday() is deprecated but the recommended function clock_gettime() is often not available in some systems (e.g., macOS 10.11 or earlier). --- configure.ac | 16 ++++++++++++++-- sources/tools.c | 20 ++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index b79bd1ea..6d33d246 100644 --- a/configure.ac +++ b/configure.ac @@ -609,8 +609,20 @@ AS_IF([test "x$enable_parform" = xyes && test "x$enable_debug" = xyes], [build_p AM_CONDITIONAL([BUILD_PARFORM], [test "x$build_parform" = xyes]) AM_CONDITIONAL([BUILD_PARVORM], [test "x$build_parvorm" = xyes]) -# Check for ftime -AC_SEARCH_LIBS([ftime], [compat], [], []) +# Check for wall-clock time +ok=no +AS_IF([test $ok != yes], + # TODO: gettimeofday is also deprecated. + [AC_SEARCH_LIBS([gettimeofday], [], + [AC_DEFINE([HAVE_GETTIMEOFDAY], [1], [Define to 1 if you have gettimeofday.]) + ok=yes])]) +AS_IF([test $ok != yes], + # Fallback: ftime. Available also on Windows. Some BSDs require -lcompat. + [AC_SEARCH_LIBS([ftime], [compat], + [AC_DEFINE([HAVE_FTIME], [1], [Define to 1 if you have ftime.]) + ok=yes])]) +AS_IF([test $ok != yes], + [AC_MSG_FAILURE([Wall-clock time not available])]) # Check for static linking STATIC_LDFLAGS= diff --git a/sources/tools.c b/sources/tools.c index fc15ab73..767ca7de 100644 --- a/sources/tools.c +++ b/sources/tools.c @@ -3366,7 +3366,11 @@ int CompArg(WORD *s1, WORD *s2) #[ TimeWallClock : */ +#ifdef HAVE_GETTIMEOFDAY +#include +#else #include +#endif /** * Returns the wall-clock time. @@ -3379,6 +3383,21 @@ LONG TimeWallClock(WORD par) /* * NOTE: this function is not thread-safe. Operations on tp are not atomic. */ +#ifdef HAVE_GETTIMEOFDAY + struct timeval t; + LONG sec, msec; + gettimeofday(&t, NULL); + sec = (LONG)t.tv_sec; + msec = (LONG)(t.tv_usec/1000); + if ( par ) { + return (sec-AM.OldSecTime)*100 + (msec-AM.OldMilliTime)/10; + } + else { + AM.OldSecTime = sec; + AM.OldMilliTime = msec; + return(0L); + } +#else struct timeb tp; ftime(&tp); if ( par ) { @@ -3390,6 +3409,7 @@ LONG TimeWallClock(WORD par) AM.OldMilliTime = (LONG)(tp.millitm); return(0L); } +#endif } /*