Blame m4/gettimeofday.m4

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