Blame m4/gettimeofday.m4

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