Blame m4/gettimeofday.m4

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