gettimeofday: expose type of second argument
[gnulib.git] / m4 / gettimeofday.m4
1 # serial 14
2
3 # Copyright (C) 2001-2003, 2005, 2007, 2009-2010 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_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     gl_FUNC_GETTIMEOFDAY_CLOBBER
20     AC_CACHE_CHECK([for gettimeofday with POSIX signature],
21       [gl_cv_func_gettimeofday_posix_signature],
22       [AC_COMPILE_IFELSE(
23          [AC_LANG_PROGRAM(
24             [[#include <sys/time.h>
25               struct timeval c;
26               int gettimeofday (struct timeval *restrict, void *restrict);
27             ]],
28             [[/* glibc uses struct timezone * rather than the POSIX void *
29                  if _GNU_SOURCE is defined.  However, since the only portable
30                  use of gettimeofday uses NULL as the second parameter, and
31                  since the glibc definition is actually more typesafe, it is
32                  not worth wrapping this to get a compliant signature.  */
33               int (*f) (struct timeval *restrict, void *restrict)
34                 = gettimeofday;
35               int x = f (&c, 0);
36               return !(x | c.tv_sec | c.tv_usec);
37             ]])],
38           [gl_cv_func_gettimeofday_posix_signature=yes],
39           [AC_COMPILE_IFELSE(
40             [AC_LANG_PROGRAM(
41               [[#include <sys/time.h>
42 int gettimeofday (struct timeval *restrict, struct timezone *restrict);
43               ]])],
44             [gl_cv_func_gettimeofday_posix_signature=almost],
45             [gl_cv_func_gettimeofday_posix_signature=no])])])
46     if test $gl_cv_func_gettimeofday_posix_signature = almost; then
47       gl_gettimeofday_timezone='struct timezone'
48     elif test $gl_cv_func_gettimeofday_posix_signature != yes; then
49       REPLACE_GETTIMEOFDAY=1
50       AC_LIBOBJ([gettimeofday])
51       gl_PREREQ_GETTIMEOFDAY
52     fi
53   fi
54   AC_DEFINE_UNQUOTED([GETTIMEOFDAY_TIMEZONE], [$gl_gettimeofday_timezone],
55     [Define this to 'void' or 'struct timezone' to match the system's
56      declaration of the second argument to gettimeofday.])
57 ])
58
59
60 dnl See if gettimeofday clobbers the static buffer that localtime uses
61 dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
62 dnl (i.e., Darwin 1.3.7) has this problem.
63 dnl
64 dnl If it does, then arrange to use gettimeofday and localtime only via
65 dnl the wrapper functions that work around the problem.
66
67 AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
68 [
69  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
70
71  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
72   [gl_cv_func_gettimeofday_clobber],
73   [AC_RUN_IFELSE(
74      [AC_LANG_PROGRAM(
75         [[#include <string.h>
76           #include <sys/time.h>
77           #include <time.h>
78           #include <stdlib.h>
79         ]],
80         [[
81           time_t t = 0;
82           struct tm *lt;
83           struct tm saved_lt;
84           struct timeval tv;
85           lt = localtime (&t);
86           saved_lt = *lt;
87           gettimeofday (&tv, NULL);
88           return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
89         ]])],
90      [gl_cv_func_gettimeofday_clobber=no],
91      [gl_cv_func_gettimeofday_clobber=yes],
92      dnl When crosscompiling, assume it is broken.
93      [gl_cv_func_gettimeofday_clobber=yes])])
94
95  if test $gl_cv_func_gettimeofday_clobber = yes; then
96    REPLACE_GETTIMEOFDAY=1
97    gl_GETTIMEOFDAY_REPLACE_LOCALTIME
98    AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], [1],
99      [Define if gettimeofday clobbers the localtime buffer.])
100  fi
101 ])
102
103 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
104   AC_LIBOBJ([gettimeofday])
105   gl_PREREQ_GETTIMEOFDAY
106   AC_DEFINE([gmtime], [rpl_gmtime],
107     [Define to rpl_gmtime if the replacement function should be used.])
108   AC_DEFINE([localtime], [rpl_localtime],
109     [Define to rpl_localtime if the replacement function should be used.])
110 ])
111
112 # Prerequisites of lib/gettimeofday.c.
113 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
114   AC_CHECK_HEADERS([sys/timeb.h])
115   AC_CHECK_FUNCS([_ftime])
116 ])