]> git.cworth.org Git - tar/blob - m4/gettimeofday.m4
b9bbb1081bcf18e1c322f4b1395f6c787f16a773
[tar] / m4 / gettimeofday.m4
1 #serial 11
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 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_CHECK_FUNCS_ONCE([gettimeofday])
15
16   AC_CACHE_CHECK([for gettimeofday with POSIX signature],
17     [gl_cv_func_gettimeofday_posix_signature],
18     [AC_COMPILE_IFELSE(
19        [AC_LANG_PROGRAM(
20           [[#include <sys/time.h>
21             struct timeval c;
22           ]],
23           [[
24             int (*f) (struct timeval *restrict, void *restrict) = gettimeofday;
25             int x = f (&c, 0);
26             return !(x | c.tv_sec | c.tv_usec);
27           ]])],
28         [gl_cv_func_gettimeofday_posix_signature=yes],
29         [gl_cv_func_gettimeofday_posix_signature=no])])
30
31   gl_FUNC_GETTIMEOFDAY_CLOBBER
32
33   if test $gl_cv_func_gettimeofday_posix_signature != yes; then
34     REPLACE_GETTIMEOFDAY=1
35     SYS_TIME_H=sys/time.h
36     if test $gl_cv_func_gettimeofday_clobber != yes; then
37       AC_LIBOBJ(gettimeofday)
38       gl_PREREQ_GETTIMEOFDAY
39     fi
40   fi
41 ])
42
43
44 dnl See if gettimeofday clobbers the static buffer that localtime uses
45 dnl for its return value.  The gettimeofday function from Mac OS X 10.0.4
46 dnl (i.e., Darwin 1.3.7) has this problem.
47 dnl
48 dnl If it does, then arrange to use gettimeofday and localtime only via
49 dnl the wrapper functions that work around the problem.
50
51 AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
52 [
53  AC_REQUIRE([gl_HEADER_SYS_TIME_H])
54
55  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
56   [gl_cv_func_gettimeofday_clobber],
57   [AC_RUN_IFELSE(
58      [AC_LANG_PROGRAM(
59         [[#include <string.h>
60           #include <sys/time.h>
61           #include <time.h>
62           #include <stdlib.h>
63         ]],
64         [[
65           time_t t = 0;
66           struct tm *lt;
67           struct tm saved_lt;
68           struct timeval tv;
69           lt = localtime (&t);
70           saved_lt = *lt;
71           gettimeofday (&tv, NULL);
72           return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
73         ]])],
74      [gl_cv_func_gettimeofday_clobber=no],
75      [gl_cv_func_gettimeofday_clobber=yes],
76      dnl When crosscompiling, assume it is broken.
77      [gl_cv_func_gettimeofday_clobber=yes])])
78
79  if test $gl_cv_func_gettimeofday_clobber = yes; then
80    REPLACE_GETTIMEOFDAY=1
81    SYS_TIME_H=sys/time.h
82    gl_GETTIMEOFDAY_REPLACE_LOCALTIME
83    AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], 1,
84      [Define if gettimeofday clobbers the localtime buffer.])
85  fi
86 ])
87
88 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
89   AC_LIBOBJ(gettimeofday)
90   gl_PREREQ_GETTIMEOFDAY
91   AC_DEFINE([gmtime], [rpl_gmtime],
92     [Define to rpl_gmtime if the replacement function should be used.])
93   AC_DEFINE([localtime], [rpl_localtime],
94     [Define to rpl_localtime if the replacement function should be used.])
95 ])
96
97 # Prerequisites of lib/gettimeofday.c.
98 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
99   AC_CHECK_HEADERS([sys/timeb.h])
100   AC_CHECK_FUNCS([_ftime])
101 ])