Backport the :end-of-capability fix
[emacs.git] / m4 / gettimeofday.m4
blob95317d3c1e846b276f8195832590a0e24697fe57
1 # serial 21
3 # Copyright (C) 2001-2003, 2005, 2007, 2009-2015 Free Software
4 # Foundation, Inc.
5 # This file is free software; the Free Software Foundation
6 # gives unlimited permission to copy and/or distribute it,
7 # with or without modifications, as long as this notice is preserved.
9 dnl From Jim Meyering.
11 AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
13   AC_REQUIRE([AC_C_RESTRICT])
14   AC_REQUIRE([gl_HEADER_SYS_TIME_H])
15   AC_REQUIRE([gl_HEADER_SYS_TIME_H_DEFAULTS])
16   AC_CHECK_FUNCS_ONCE([gettimeofday])
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>
45 int 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     m4_ifdef([gl_FUNC_TZSET_CLOBBER], [
59       gl_FUNC_TZSET_CLOBBER
60       case "$gl_cv_func_tzset_clobber" in
61         *yes)
62           REPLACE_GETTIMEOFDAY=1
63           gl_GETTIMEOFDAY_REPLACE_LOCALTIME
64           AC_DEFINE([tzset], [rpl_tzset],
65             [Define to rpl_tzset if the wrapper function should be used.])
66           AC_DEFINE([TZSET_CLOBBERS_LOCALTIME], [1],
67             [Define if tzset clobbers localtime's static buffer.])
68           ;;
69       esac
70     ])
71   fi
72   AC_DEFINE_UNQUOTED([GETTIMEOFDAY_TIMEZONE], [$gl_gettimeofday_timezone],
73     [Define this to 'void' or 'struct timezone' to match the system's
74      declaration of the second argument to gettimeofday.])
78 dnl See if gettimeofday clobbers the static buffer that localtime uses
79 dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
80 dnl (i.e., Darwin 1.3.7) has this problem.
81 dnl
82 dnl If it does, then arrange to use gettimeofday and localtime only via
83 dnl the wrapper functions that work around the problem.
85 AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
87  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
88  AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
90  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
91   [gl_cv_func_gettimeofday_clobber],
92   [AC_RUN_IFELSE(
93      [AC_LANG_PROGRAM(
94         [[#include <string.h>
95           #include <sys/time.h>
96           #include <time.h>
97           #include <stdlib.h>
98         ]],
99         [[
100           time_t t = 0;
101           struct tm *lt;
102           struct tm saved_lt;
103           struct timeval tv;
104           lt = localtime (&t);
105           saved_lt = *lt;
106           gettimeofday (&tv, NULL);
107           return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
108         ]])],
109      [gl_cv_func_gettimeofday_clobber=no],
110      [gl_cv_func_gettimeofday_clobber=yes],
111      [# When cross-compiling:
112       case "$host_os" in
113                 # Guess all is fine on glibc systems.
114         *-gnu*) gl_cv_func_gettimeofday_clobber="guessing no" ;;
115                 # If we don't know, assume the worst.
116         *)      gl_cv_func_gettimeofday_clobber="guessing yes" ;;
117       esac
118      ])])
120  case "$gl_cv_func_gettimeofday_clobber" in
121    *yes)
122      REPLACE_GETTIMEOFDAY=1
123      gl_GETTIMEOFDAY_REPLACE_LOCALTIME
124      AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], [1],
125        [Define if gettimeofday clobbers the localtime buffer.])
126      ;;
127  esac
130 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
131   REPLACE_GMTIME=1
132   REPLACE_LOCALTIME=1
135 # Prerequisites of lib/gettimeofday.c.
136 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
137   AC_CHECK_HEADERS([sys/timeb.h])
138   AC_CHECK_FUNCS([_ftime])