Blame gl/m4/gettimeofday.m4

Packit a4aae4
# serial 24
Packit a4aae4
Packit a4aae4
# Copyright (C) 2001-2003, 2005, 2007, 2009-2017 Free Software Foundation, Inc.
Packit a4aae4
# This file is free software; the Free Software Foundation
Packit a4aae4
# gives unlimited permission to copy and/or distribute it,
Packit a4aae4
# with or without modifications, as long as this notice is preserved.
Packit a4aae4
Packit a4aae4
dnl From Jim Meyering.
Packit a4aae4
Packit a4aae4
AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
Packit a4aae4
[
Packit a4aae4
  AC_REQUIRE([gl_HEADER_SYS_TIME_H_DEFAULTS])
Packit a4aae4
  AC_REQUIRE([AC_C_RESTRICT])
Packit a4aae4
  AC_REQUIRE([AC_CANONICAL_HOST])
Packit a4aae4
  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
Packit a4aae4
  AC_CHECK_FUNCS_ONCE([gettimeofday])
Packit a4aae4
Packit a4aae4
  gl_gettimeofday_timezone=void
Packit a4aae4
  if test $ac_cv_func_gettimeofday != yes; then
Packit a4aae4
    HAVE_GETTIMEOFDAY=0
Packit a4aae4
  else
Packit a4aae4
    gl_FUNC_GETTIMEOFDAY_CLOBBER
Packit a4aae4
    AC_CACHE_CHECK([for gettimeofday with POSIX signature],
Packit a4aae4
      [gl_cv_func_gettimeofday_posix_signature],
Packit a4aae4
      [AC_COMPILE_IFELSE(
Packit a4aae4
         [AC_LANG_PROGRAM(
Packit a4aae4
            [[#include <sys/time.h>
Packit a4aae4
              struct timeval c;
Packit a4aae4
              int gettimeofday (struct timeval *restrict, void *restrict);
Packit a4aae4
            ]],
Packit a4aae4
            [[/* glibc uses struct timezone * rather than the POSIX void *
Packit a4aae4
                 if _GNU_SOURCE is defined.  However, since the only portable
Packit a4aae4
                 use of gettimeofday uses NULL as the second parameter, and
Packit a4aae4
                 since the glibc definition is actually more typesafe, it is
Packit a4aae4
                 not worth wrapping this to get a compliant signature.  */
Packit a4aae4
              int (*f) (struct timeval *restrict, void *restrict)
Packit a4aae4
                = gettimeofday;
Packit a4aae4
              int x = f (&c, 0);
Packit a4aae4
              return !(x | c.tv_sec | c.tv_usec);
Packit a4aae4
            ]])],
Packit a4aae4
          [gl_cv_func_gettimeofday_posix_signature=yes],
Packit a4aae4
          [AC_COMPILE_IFELSE(
Packit a4aae4
            [AC_LANG_PROGRAM(
Packit a4aae4
              [[#include <sys/time.h>
Packit a4aae4
int gettimeofday (struct timeval *restrict, struct timezone *restrict);
Packit a4aae4
              ]])],
Packit a4aae4
            [gl_cv_func_gettimeofday_posix_signature=almost],
Packit a4aae4
            [gl_cv_func_gettimeofday_posix_signature=no])])])
Packit a4aae4
    if test $gl_cv_func_gettimeofday_posix_signature = almost; then
Packit a4aae4
      gl_gettimeofday_timezone='struct timezone'
Packit a4aae4
    elif test $gl_cv_func_gettimeofday_posix_signature != yes; then
Packit a4aae4
      REPLACE_GETTIMEOFDAY=1
Packit a4aae4
    fi
Packit a4aae4
    dnl If we override 'struct timeval', we also have to override gettimeofday.
Packit a4aae4
    if test $REPLACE_STRUCT_TIMEVAL = 1; then
Packit a4aae4
      REPLACE_GETTIMEOFDAY=1
Packit a4aae4
    fi
Packit a4aae4
    dnl On mingw, the original gettimeofday has only a precision of 15.6
Packit a4aae4
    dnl milliseconds. So override it.
Packit a4aae4
    case "$host_os" in
Packit a4aae4
      mingw*) REPLACE_GETTIMEOFDAY=1 ;;
Packit a4aae4
    esac
Packit a4aae4
  fi
Packit a4aae4
  AC_DEFINE_UNQUOTED([GETTIMEOFDAY_TIMEZONE], [$gl_gettimeofday_timezone],
Packit a4aae4
    [Define this to 'void' or 'struct timezone' to match the system's
Packit a4aae4
     declaration of the second argument to gettimeofday.])
Packit a4aae4
])
Packit a4aae4
Packit a4aae4
Packit a4aae4
dnl See if gettimeofday clobbers the static buffer that localtime uses
Packit a4aae4
dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
Packit a4aae4
dnl (i.e., Darwin 1.3.7) has this problem.
Packit a4aae4
dnl
Packit a4aae4
dnl If it does, then arrange to use gettimeofday and localtime only via
Packit a4aae4
dnl the wrapper functions that work around the problem.
Packit a4aae4
Packit a4aae4
AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
Packit a4aae4
[
Packit a4aae4
 AC_REQUIRE([gl_HEADER_SYS_TIME_H])
Packit a4aae4
 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
Packit a4aae4
 AC_REQUIRE([gl_LOCALTIME_BUFFER_DEFAULTS])
Packit a4aae4
Packit a4aae4
 AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
Packit a4aae4
  [gl_cv_func_gettimeofday_clobber],
Packit a4aae4
  [AC_RUN_IFELSE(
Packit a4aae4
     [AC_LANG_PROGRAM(
Packit a4aae4
        [[#include <string.h>
Packit a4aae4
          #include <sys/time.h>
Packit a4aae4
          #include <time.h>
Packit a4aae4
          #include <stdlib.h>
Packit a4aae4
        ]],
Packit a4aae4
        [[
Packit a4aae4
          time_t t = 0;
Packit a4aae4
          struct tm *lt;
Packit a4aae4
          struct tm saved_lt;
Packit a4aae4
          struct timeval tv;
Packit a4aae4
          lt = localtime (&t);
Packit a4aae4
          saved_lt = *lt;
Packit a4aae4
          gettimeofday (&tv, NULL);
Packit a4aae4
          return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
Packit a4aae4
        ]])],
Packit a4aae4
     [gl_cv_func_gettimeofday_clobber=no],
Packit a4aae4
     [gl_cv_func_gettimeofday_clobber=yes],
Packit a4aae4
     [# When cross-compiling:
Packit a4aae4
      case "$host_os" in
Packit a4aae4
                # Guess all is fine on glibc systems.
Packit a4aae4
        *-gnu*) gl_cv_func_gettimeofday_clobber="guessing no" ;;
Packit a4aae4
                # Guess no on native Windows.
Packit a4aae4
        mingw*) gl_cv_func_gettimeofday_clobber="guessing no" ;;
Packit a4aae4
                # If we don't know, assume the worst.
Packit a4aae4
        *)      gl_cv_func_gettimeofday_clobber="guessing yes" ;;
Packit a4aae4
      esac
Packit a4aae4
     ])])
Packit a4aae4
Packit a4aae4
 case "$gl_cv_func_gettimeofday_clobber" in
Packit a4aae4
   *yes)
Packit a4aae4
     REPLACE_GETTIMEOFDAY=1
Packit a4aae4
     AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], [1],
Packit a4aae4
       [Define if gettimeofday clobbers the localtime buffer.])
Packit a4aae4
     gl_LOCALTIME_BUFFER_NEEDED
Packit a4aae4
     ;;
Packit a4aae4
 esac
Packit a4aae4
])
Packit a4aae4
Packit a4aae4
# Prerequisites of lib/gettimeofday.c.
Packit a4aae4
AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [:])