1# serial 25
2
3# Copyright (C) 2001-2003, 2005, 2007, 2009-2018 Free Software Foundation, Inc.
4# This file is free software; the Free Software Foundation
5# gives unlimited permission to copy and/or distribute it,
6# with or without modifications, as long as this notice is preserved.
7
8dnl From Jim Meyering.
9
10AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
11[
12  AC_REQUIRE([gl_HEADER_SYS_TIME_H_DEFAULTS])
13  AC_REQUIRE([AC_C_RESTRICT])
14  AC_REQUIRE([AC_CANONICAL_HOST])
15  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
16  AC_CHECK_FUNCS_ONCE([gettimeofday])
17
18  gl_gettimeofday_timezone=void
19  if test $ac_cv_func_gettimeofday != yes; then
20    HAVE_GETTIMEOFDAY=0
21  else
22    gl_FUNC_GETTIMEOFDAY_CLOBBER
23    AC_CACHE_CHECK([for gettimeofday with POSIX signature],
24      [gl_cv_func_gettimeofday_posix_signature],
25      [AC_COMPILE_IFELSE(
26         [AC_LANG_PROGRAM(
27            [[#include <sys/time.h>
28              struct timeval c;
29              int gettimeofday (struct timeval *restrict, void *restrict);
30            ]],
31            [[/* glibc uses struct timezone * rather than the POSIX void *
32                 if _GNU_SOURCE is defined.  However, since the only portable
33                 use of gettimeofday uses NULL as the second parameter, and
34                 since the glibc definition is actually more typesafe, it is
35                 not worth wrapping this to get a compliant signature.  */
36              int (*f) (struct timeval *restrict, void *restrict)
37                = gettimeofday;
38              int x = f (&c, 0);
39              return !(x | c.tv_sec | c.tv_usec);
40            ]])],
41          [gl_cv_func_gettimeofday_posix_signature=yes],
42          [AC_COMPILE_IFELSE(
43            [AC_LANG_PROGRAM(
44              [[#include <sys/time.h>
45int gettimeofday (struct timeval *restrict, struct timezone *restrict);
46              ]])],
47            [gl_cv_func_gettimeofday_posix_signature=almost],
48            [gl_cv_func_gettimeofday_posix_signature=no])])])
49    if test $gl_cv_func_gettimeofday_posix_signature = almost; then
50      gl_gettimeofday_timezone='struct timezone'
51    elif test $gl_cv_func_gettimeofday_posix_signature != yes; then
52      REPLACE_GETTIMEOFDAY=1
53    fi
54    dnl If we override 'struct timeval', we also have to override gettimeofday.
55    if test $REPLACE_STRUCT_TIMEVAL = 1; then
56      REPLACE_GETTIMEOFDAY=1
57    fi
58    dnl On mingw, the original gettimeofday has only a precision of 15.6
59    dnl milliseconds. So override it.
60    case "$host_os" in
61      mingw*) REPLACE_GETTIMEOFDAY=1 ;;
62    esac
63  fi
64  AC_DEFINE_UNQUOTED([GETTIMEOFDAY_TIMEZONE], [$gl_gettimeofday_timezone],
65    [Define this to 'void' or 'struct timezone' to match the system's
66     declaration of the second argument to gettimeofday.])
67])
68
69
70dnl See if gettimeofday clobbers the static buffer that localtime uses
71dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
72dnl (i.e., Darwin 1.3.7) has this problem.
73dnl
74dnl If it does, then arrange to use gettimeofday and localtime only via
75dnl the wrapper functions that work around the problem.
76
77AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
78[
79 AC_REQUIRE([gl_HEADER_SYS_TIME_H])
80 AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
81 AC_REQUIRE([gl_LOCALTIME_BUFFER_DEFAULTS])
82
83 AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
84  [gl_cv_func_gettimeofday_clobber],
85  [AC_RUN_IFELSE(
86     [AC_LANG_PROGRAM(
87        [[#include <string.h>
88          #include <sys/time.h>
89          #include <time.h>
90          #include <stdlib.h>
91        ]],
92        [[
93          time_t t = 0;
94          struct tm *lt;
95          struct tm saved_lt;
96          struct timeval tv;
97          lt = localtime (&t);
98          saved_lt = *lt;
99          gettimeofday (&tv, NULL);
100          return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
101        ]])],
102     [gl_cv_func_gettimeofday_clobber=no],
103     [gl_cv_func_gettimeofday_clobber=yes],
104     [# When cross-compiling:
105      case "$host_os" in
106                       # Guess all is fine on glibc systems.
107        *-gnu* | gnu*) gl_cv_func_gettimeofday_clobber="guessing no" ;;
108                       # Guess no on native Windows.
109        mingw*)        gl_cv_func_gettimeofday_clobber="guessing no" ;;
110                       # If we don't know, assume the worst.
111        *)             gl_cv_func_gettimeofday_clobber="guessing yes" ;;
112      esac
113     ])])
114
115 case "$gl_cv_func_gettimeofday_clobber" in
116   *yes)
117     REPLACE_GETTIMEOFDAY=1
118     AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], [1],
119       [Define if gettimeofday clobbers the localtime buffer.])
120     gl_LOCALTIME_BUFFER_NEEDED
121     ;;
122 esac
123])
124
125# Prerequisites of lib/gettimeofday.c.
126AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [:])
127