Remove K&R cruft.
[gnulib.git] / m4 / gettimeofday.m4
1 #serial 5
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_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
16   jm_cv_func_gettimeofday_clobber,
17   [AC_TRY_RUN([
18 #include <stdio.h>
19 #include <string.h>
20
21 #if TIME_WITH_SYS_TIME
22 # include <sys/time.h>
23 # include <time.h>
24 #else
25 # if HAVE_SYS_TIME_H
26 #  include <sys/time.h>
27 # else
28 #  include <time.h>
29 # endif
30 #endif
31
32 #include <stdlib.h>
33
34 int
35 main ()
36 {
37   time_t t = 0;
38   struct tm *lt;
39   struct tm saved_lt;
40   struct timeval tv;
41   lt = localtime (&t);
42   saved_lt = *lt;
43   gettimeofday (&tv, NULL);
44   if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0)
45     exit (1);
46
47   exit (0);
48 }
49           ],
50          jm_cv_func_gettimeofday_clobber=no,
51          jm_cv_func_gettimeofday_clobber=yes,
52          dnl When crosscompiling, assume it is broken.
53          jm_cv_func_gettimeofday_clobber=yes)
54   ])
55   if test $jm_cv_func_gettimeofday_clobber = yes; then
56     gl_GETTIMEOFDAY_REPLACE_LOCALTIME
57
58     AC_DEFINE(gettimeofday, rpl_gettimeofday,
59       [Define to rpl_gettimeofday if the replacement function should be used.])
60     AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME_BUFFER, 1,
61       [Define if gettimeofday clobbers localtime's static buffer.])
62     gl_PREREQ_GETTIMEOFDAY
63   fi
64 ])
65
66 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
67   AC_LIBOBJ(gettimeofday)
68   AC_DEFINE(gmtime, rpl_gmtime,
69     [Define to rpl_gmtime if the replacement function should be used.])
70   AC_DEFINE(localtime, rpl_localtime,
71     [Define to rpl_localtime if the replacement function should be used.])
72 ])
73
74 # Prerequisites of lib/gettimeofday.c.
75 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
76   AC_REQUIRE([AC_HEADER_TIME])
77 ])