Replace HAVE_GETTIMEOFDAY_POSIX_SIGNATURE and GETTIMEOFDAY_CLOBBERS_LOCALTIME
[gnulib.git] / m4 / gettimeofday.m4
1 #serial 11
2
3 # Copyright (C) 2001, 2002, 2003, 2005, 2007 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
8 dnl From Jim Meyering.
9
10 AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
11 [
12   AC_REQUIRE([AC_C_RESTRICT])
13   AC_REQUIRE([gl_HEADER_SYS_TIME_H])
14   AC_CHECK_FUNCS_ONCE([gettimeofday])
15
16   AC_CACHE_CHECK([for gettimeofday with POSIX signature],
17     [gl_cv_func_gettimeofday_posix_signature],
18     [AC_COMPILE_IFELSE(
19        [AC_LANG_PROGRAM(
20           [[#include <sys/time.h>
21             struct timeval c;
22           ]],
23           [[
24             int (*f) (struct timeval *restrict, void *restrict) = gettimeofday;
25             int x = f (&c, 0);
26             return !(x | c.tv_sec | c.tv_usec);
27           ]])],
28         [gl_cv_func_gettimeofday_posix_signature=yes],
29         [gl_cv_func_gettimeofday_posix_signature=no])])
30
31   gl_FUNC_GETTIMEOFDAY_CLOBBER
32
33   if test $gl_cv_func_gettimeofday_posix_signature != yes; then
34     GETTIMEOFDAY_REPLACEMENT=1
35     if test $gl_cv_func_gettimeofday_clobber != yes; then
36       AC_LIBOBJ(gettimeofday)
37       gl_PREREQ_GETTIMEOFDAY
38     fi
39   fi
40 ])
41
42
43 dnl See if gettimeofday clobbers the static buffer that localtime uses
44 dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
45 dnl (i.e., Darwin 1.3.7) has this problem.
46 dnl
47 dnl If it does, then arrange to use gettimeofday and localtime only via
48 dnl the wrapper functions that work around the problem.
49
50 AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
51 [
52  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
53
54  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
55   [gl_cv_func_gettimeofday_clobber],
56   [AC_RUN_IFELSE(
57      [AC_LANG_PROGRAM(
58         [[#include <string.h>
59           #include <sys/time.h>
60           #include <time.h>
61           #include <stdlib.h>
62         ]],
63         [[
64           time_t t = 0;
65           struct tm *lt;
66           struct tm saved_lt;
67           struct timeval tv;
68           lt = localtime (&t);
69           saved_lt = *lt;
70           gettimeofday (&tv, NULL);
71           return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
72         ]])],
73      [gl_cv_func_gettimeofday_clobber=no],
74      [gl_cv_func_gettimeofday_clobber=yes],
75      dnl When crosscompiling, assume it is broken.
76      [gl_cv_func_gettimeofday_clobber=yes])])
77
78  if test $gl_cv_func_gettimeofday_clobber = yes; then
79    GETTIMEOFDAY_REPLACEMENT=1
80    gl_GETTIMEOFDAY_REPLACE_LOCALTIME
81    AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], 1,
82      [Define if gettimeofday clobbers the localtime buffer.])
83  fi
84 ])
85
86 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
87   AC_LIBOBJ(gettimeofday)
88   gl_PREREQ_GETTIMEOFDAY
89   AC_DEFINE([gmtime], [rpl_gmtime],
90     [Define to rpl_gmtime if the replacement function should be used.])
91   AC_DEFINE([localtime], [rpl_localtime],
92     [Define to rpl_localtime if the replacement function should be used.])
93 ])
94
95 # Prerequisites of lib/gettimeofday.c.
96 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
97   AC_CHECK_HEADERS([sys/timeb.h])
98   AC_CHECK_FUNCS([_ftime])
99 ])