e4d75da00dba50bdd5ab510b96ba497d59bf6fad
[gnulib.git] / m4 / gettimeofday.m4
1 #serial 8
2
3 # Copyright (C) 2001, 2002, 2003, 2005 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_LIBSOURCES([gettimeofday.c, gettimeofday.h])
11   AC_REQUIRE([gl_C_RESTRICT])
12   AC_CHECK_FUNCS([gettimeofday])
13   
14   AC_CHECK_TYPE([suseconds_t], ,
15     [AC_DEFINE([suseconds_t], [int],
16        [Define to `int' if `suseconds_t' is missing.])
17     ],
18     [
19 #    if TIME_WITH_SYS_TIME
20 #     include <sys/time.h>
21 #     include <time.h>
22 #    else
23 #     if HAVE_SYS_TIME_H
24 #      include <sys/time.h>
25 #     else
26 #      include <time.h>
27 #     endif
28 #    endif
29     ])
30
31   AC_CACHE_CHECK([for struct timeval], fu_cv_sys_struct_timeval,
32     [AC_TRY_COMPILE(
33       [
34 #      if TIME_WITH_SYS_TIME
35 #       include <sys/time.h>
36 #       include <time.h>
37 #      else
38 #       if HAVE_SYS_TIME_H
39 #        include <sys/time.h>
40 #       else
41 #        include <time.h>
42 #       endif
43 #      endif
44       ],
45       [static struct timeval x; x.tv_sec = x.tv_usec;],
46       fu_cv_sys_struct_timeval=yes,
47       fu_cv_sys_struct_timeval=no)
48     ])
49
50   if test $fu_cv_sys_struct_timeval = yes; then
51     AC_DEFINE(HAVE_STRUCT_TIMEVAL, 1,
52       [Define if struct timeval is declared in <time.h> or <sys/time.h>. ])
53   fi
54   
55   AC_CACHE_CHECK([for gettimeofday whose signature conforms to POSIX],
56     [ac_cv_func_gettimeofday_posix_signature],
57     AC_LINK_IFELSE(
58       [AC_LANG_PROGRAM(
59          [[#include <sys/time.h>
60            time_t a;
61            suseconds_t b;
62            struct timeval c;
63          ]],
64          [[
65            int x = gettimeofday (&c, 0);
66            int (*f) (struct timeval *restrict, void *restrict) = gettimeofday;
67            return !(x | c.tv_sec | c.tv_usec);
68          ]])],
69        [ac_cv_func_gettimeofday_posix_signature=yes],
70        [ac_cv_func_gettimeofday_posix_signature=no]))
71   if test $ac_cv_func_gettimeofday_posix_signature = yes; then
72     AC_DEFINE([HAVE_GETTIMEOFDAY_POSIX_SIGNATURE], 1,
73       [Define if gettimeofday's signature conforms to POSIX.])
74     AC_FUNC_GETTIMEOFDAY_CLOBBER
75   fi
76   if test $ac_cv_func_gettimeofday_posix_signature != yes; then
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
137     AC_DEFINE(gettimeofday, rpl_gettimeofday,
138       [Define to rpl_gettimeofday if the replacement function should be used.])
139   fi
140 ])
141
142 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
143   gl_PREREQ_GETTIMEOFDAY
144   AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME, 1,
145     [Define if gettimeofday clobbers the localtime buffer.])
146   AC_LIBOBJ(gettimeofday)
147   AC_DEFINE(gmtime, rpl_gmtime,
148     [Define to rpl_gmtime if the replacement function should be used.])
149   AC_DEFINE(localtime, rpl_localtime,
150     [Define to rpl_localtime if the replacement function should be used.])
151 ])
152
153 # Prerequisites of lib/gettimeofday.c.
154 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
155   AC_REQUIRE([AC_HEADER_TIME])
156   AC_CHECK_HEADERS([sys/timeb.h])
157   AC_CHECK_FUNCS([_ftime])
158 ])