Blame m4/gettimeofday.m4

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