An independent .m4 file for each module.
[gnulib.git] / m4 / gettimeofday.m4
1 #serial 3
2
3 dnl From Jim Meyering.
4 dnl
5 dnl See if gettimeofday clobbers the static buffer that localtime uses
6 dnl for it's return value.  The gettimeofday function from Mac OS X 10.0.4,
7 dnl i.e. Darwin 1.3.7 has this problem.
8 dnl
9 dnl If it does, then arrange to use gettimeofday and localtime only via
10 dnl the wrapper functions that work around the problem.
11
12 AC_DEFUN([AC_FUNC_GETTIMEOFDAY_CLOBBER],
13 [
14  AC_REQUIRE([AC_HEADER_TIME])
15  AC_CHECK_HEADERS_ONCE(stdlib.h string.h)
16  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
17   jm_cv_func_gettimeofday_clobber,
18   [AC_TRY_RUN([
19 #include <stdio.h>
20 #if HAVE_STRING_H
21 # include <string.h>
22 #endif
23
24 #if TIME_WITH_SYS_TIME
25 # include <sys/time.h>
26 # include <time.h>
27 #else
28 # if HAVE_SYS_TIME_H
29 #  include <sys/time.h>
30 # else
31 #  include <time.h>
32 # endif
33 #endif
34
35 #if HAVE_STDLIB_H
36 # include <stdlib.h>
37 #endif
38
39 int
40 main ()
41 {
42   time_t t = 0;
43   struct tm *lt;
44   struct tm saved_lt;
45   struct timeval tv;
46   lt = localtime (&t);
47   saved_lt = *lt;
48   gettimeofday (&tv, NULL);
49   if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0)
50     exit (1);
51
52   exit (0);
53 }
54           ],
55          jm_cv_func_gettimeofday_clobber=no,
56          jm_cv_func_gettimeofday_clobber=yes,
57          dnl When crosscompiling, assume it is broken.
58          jm_cv_func_gettimeofday_clobber=yes)
59   ])
60   if test $jm_cv_func_gettimeofday_clobber = yes; then
61     AC_LIBOBJ(gettimeofday)
62     AC_DEFINE(localtime, rpl_localtime,
63       [Define to rpl_localtime if the replacement function should be used.])
64     AC_DEFINE(gettimeofday, rpl_gettimeofday,
65       [Define to rpl_gettimeofday if the replacement function should be used.])
66     AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME_BUFFER, 1,
67       [Define if gettimeofday clobbers localtime's static buffer.])
68     gl_PREREQ_GETTIMEOFDAY
69   fi
70 ])
71
72 # Prerequisites of lib/gettimeofday.c.
73 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
74   AC_REQUIRE([AC_HEADER_TIME])
75 ])