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