Various fixes to Martin Lambers' gettimeofday patch.
[gnulib.git] / m4 / gettimeofday.m4
1 #serial 8
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 AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
9 [
10   AC_REQUIRE([AC_C_RESTRICT])
11   AC_REQUIRE([AC_HEADER_TIME])
12   AC_CHECK_HEADERS_ONCE([sys/time.h])
13   AC_CHECK_FUNCS([gettimeofday])
14   
15   AC_CHECK_TYPE([suseconds_t], ,
16     [AC_DEFINE([suseconds_t], [int],
17        [Define to `int' if `suseconds_t' is missing.])
18     ],
19     [
20 #    if TIME_WITH_SYS_TIME
21 #     include <sys/time.h>
22 #     include <time.h>
23 #    else
24 #     if HAVE_SYS_TIME_H
25 #      include <sys/time.h>
26 #     else
27 #      include <time.h>
28 #     endif
29 #    endif
30     ])
31
32   AC_CACHE_CHECK([for struct timeval], [fu_cv_sys_struct_timeval],
33     [AC_TRY_COMPILE(
34       [
35 #      if TIME_WITH_SYS_TIME
36 #       include <sys/time.h>
37 #       include <time.h>
38 #      else
39 #       if HAVE_SYS_TIME_H
40 #        include <sys/time.h>
41 #       else
42 #        include <time.h>
43 #       endif
44 #      endif
45       ],
46       [static struct timeval x; x.tv_sec = x.tv_usec;],
47       fu_cv_sys_struct_timeval=yes,
48       fu_cv_sys_struct_timeval=no)
49     ])
50
51   if test $fu_cv_sys_struct_timeval = yes; then
52     AC_DEFINE(HAVE_STRUCT_TIMEVAL, 1,
53       [Define if struct timeval is declared in <time.h> or <sys/time.h>. ])
54   fi
55   
56   AC_CACHE_CHECK([for gettimeofday whose signature conforms to POSIX],
57     [ac_cv_func_gettimeofday_posix_signature],
58     [AC_LINK_IFELSE(
59        [AC_LANG_PROGRAM(
60           [[#include <sys/time.h>
61             time_t a;
62             suseconds_t b;
63             struct timeval c;
64           ]],
65           [[
66             int x = gettimeofday (&c, 0);
67             int (*f) (struct timeval *restrict, void *restrict) = gettimeofday;
68             return !(x | c.tv_sec | c.tv_usec);
69           ]])],
70        [ac_cv_func_gettimeofday_posix_signature=yes],
71        [ac_cv_func_gettimeofday_posix_signature=no])])
72   if test $ac_cv_func_gettimeofday_posix_signature = yes; then
73     AC_DEFINE([HAVE_GETTIMEOFDAY_POSIX_SIGNATURE], 1,
74       [Define if gettimeofday's signature conforms to POSIX.])
75     AC_FUNC_GETTIMEOFDAY_CLOBBER
76   else
77     gl_PREREQ_GETTIMEOFDAY
78     AC_LIBOBJ([gettimeofday])
79   fi
80 ])
81
82 dnl From Jim Meyering.
83 dnl
84 dnl See if gettimeofday clobbers the static buffer that localtime uses
85 dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
86 dnl (i.e., Darwin 1.3.7) has this problem.
87 dnl
88 dnl If it does, then arrange to use gettimeofday and localtime only via
89 dnl the wrapper functions that work around the problem.
90
91 AC_DEFUN([AC_FUNC_GETTIMEOFDAY_CLOBBER],
92 [
93  AC_REQUIRE([AC_HEADER_TIME])
94  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
95   [jm_cv_func_gettimeofday_clobber],
96   [AC_TRY_RUN([
97 #include <stdio.h>
98 #include <string.h>
99
100 #if TIME_WITH_SYS_TIME
101 # include <sys/time.h>
102 # include <time.h>
103 #else
104 # if HAVE_SYS_TIME_H
105 #  include <sys/time.h>
106 # else
107 #  include <time.h>
108 # endif
109 #endif
110
111 #include <stdlib.h>
112
113 int
114 main ()
115 {
116   time_t t = 0;
117   struct tm *lt;
118   struct tm saved_lt;
119   struct timeval tv;
120   lt = localtime (&t);
121   saved_lt = *lt;
122   gettimeofday (&tv, NULL);
123   if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0)
124     exit (1);
125
126   exit (0);
127 }
128           ],
129          jm_cv_func_gettimeofday_clobber=no,
130          jm_cv_func_gettimeofday_clobber=yes,
131          dnl When crosscompiling, assume it is broken.
132          jm_cv_func_gettimeofday_clobber=yes)
133   ])
134   if test $jm_cv_func_gettimeofday_clobber = yes; then
135     gl_GETTIMEOFDAY_REPLACE_LOCALTIME
136   fi
137 ])
138
139 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
140   AC_LIBOBJ(gettimeofday)
141   gl_PREREQ_GETTIMEOFDAY
142   AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], 1,
143     [Define if gettimeofday clobbers the localtime buffer.])
144   AC_DEFINE([gmtime], [rpl_gmtime],
145     [Define to rpl_gmtime if the replacement function should be used.])
146   AC_DEFINE([localtime], [rpl_localtime],
147     [Define to rpl_localtime if the replacement function should be used.])
148   AC_DEFINE([tzset], [rpl_tzset],
149     [Define to rpl_tzset if the replacement function should be used.])
150 ])
151
152 # Prerequisites of lib/gettimeofday.c.
153 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
154   AC_REQUIRE([AC_HEADER_TIME])
155   AC_CHECK_HEADERS([sys/timeb.h])
156   AC_CHECK_FUNCS([_ftime])
157 ])