src/utils_time.[ch]: Implement work-around for Mac OS X …
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 26 Nov 2010 21:46:26 +0000 (22:46 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Fri, 26 Nov 2010 21:46:26 +0000 (22:46 +0100)
… which, apparently, doesn't have clock_gettime(2).

configure.in
src/utils_time.c
src/utils_time.h

index fd6a257..3b46188 100644 (file)
@@ -574,13 +574,24 @@ AM_CONDITIONAL(BUILD_WITH_LIBSOCKET, test "x$socket_needs_socket" = "xyes")
 
 clock_gettime_needs_rt="no"
 clock_gettime_needs_posix4="no"
-AC_CHECK_FUNCS(clock_gettime,
-    [],
-    AC_CHECK_LIB(rt, clock_gettime,
-        [clock_gettime_needs_rt="yes"],
-        AC_CHECK_LIB(posix4, clock_gettime,
-            [clock_gettime_needs_posix4="yes"],
-            AC_MSG_ERROR(cannot find clock_gettime))))
+have_clock_gettime="no"
+AC_CHECK_FUNCS(clock_gettime, [have_clock_gettime="yes"])
+if test "x$have_clock_gettime" = "xno"
+then
+       AC_CHECK_LIB(rt, clock_gettime, [clock_gettime_needs_rt="yes"
+                                        have_clock_gettime="yes"])
+fi
+if test "x$have_clock_gettime" = "xno"
+then
+       AC_CHECK_LIB(posix4, clock_gettime, [clock_gettime_needs_posix4="yes"
+                                            have_clock_gettime="yes"])
+fi
+if test "x$have_clock_gettime" = "xyes"
+then
+       AC_DEFINE(HAVE_CLOCK_GETTIME, 1, [Define if the clock_gettime(2) function is available.])
+else
+       AC_MSG_WARN(cannot find clock_gettime)
+fi
 
 nanosleep_needs_rt="no"
 nanosleep_needs_posix4="no"
index 420b425..aac6135 100644 (file)
@@ -24,6 +24,7 @@
 #include "plugin.h"
 #include "common.h"
 
+#if HAVE_CLOCK_GETTIME
 cdtime_t cdtime (void) /* {{{ */
 {
   int status;
@@ -40,5 +41,24 @@ cdtime_t cdtime (void) /* {{{ */
 
   return (TIMESPEC_TO_CDTIME_T (&ts));
 } /* }}} cdtime_t cdtime */
+#else
+/* Work around for Mac OS X which doesn't have clock_gettime(2). *sigh* */
+cdtime_t cdtime (void) /* {{{ */
+{
+  int status;
+  struct timeval tv = { 0, 0 };
+
+  status = gettimeofday (&tv, /* struct timezone = */ NULL);
+  if (status != 0)
+  {
+    char errbuf[1024];
+    ERROR ("cdtime: gettimeofday failed: %s",
+        sstrerror (errno, errbuf, sizeof (errbuf)));
+    return (0);
+  }
+
+  return (TIMEVAL_TO_CDTIME_T (&tv));
+} /* }}} cdtime_t cdtime */
+#endif
 
 /* vim: set sw=2 sts=2 et fdm=marker : */
index da73492..0fd809a 100644 (file)
@@ -54,8 +54,8 @@
         (tvp)->tv_sec = CDTIME_T_TO_TIME_T (cdt);                            \
         (tvp)->tv_usec = CDTIME_T_TO_US ((cdt) % 1073741824);                \
 } while (0)
-#define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv).tv_sec)            \
-    + US_TO_CDTIME_T ((tv).tv_usec))
+#define TIMEVAL_TO_CDTIME_T(tv) (TIME_T_TO_CDTIME_T ((tv)->tv_sec)           \
+    + US_TO_CDTIME_T ((tv)->tv_usec))
 
 #define CDTIME_T_TO_TIMESPEC(cdt,tsp) do {                                   \
   (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt);                                  \