gettimeofday.m4 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # serial 21
  2. # Copyright (C) 2001-2003, 2005, 2007, 2009-2017 Free Software Foundation, Inc.
  3. # This file is free software; the Free Software Foundation
  4. # gives unlimited permission to copy and/or distribute it,
  5. # with or without modifications, as long as this notice is preserved.
  6. dnl From Jim Meyering.
  7. AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
  8. [
  9. AC_REQUIRE([AC_C_RESTRICT])
  10. AC_REQUIRE([gl_HEADER_SYS_TIME_H])
  11. AC_REQUIRE([gl_HEADER_SYS_TIME_H_DEFAULTS])
  12. AC_CHECK_FUNCS_ONCE([gettimeofday])
  13. gl_gettimeofday_timezone=void
  14. if test $ac_cv_func_gettimeofday != yes; then
  15. HAVE_GETTIMEOFDAY=0
  16. else
  17. gl_FUNC_GETTIMEOFDAY_CLOBBER
  18. AC_CACHE_CHECK([for gettimeofday with POSIX signature],
  19. [gl_cv_func_gettimeofday_posix_signature],
  20. [AC_COMPILE_IFELSE(
  21. [AC_LANG_PROGRAM(
  22. [[#include <sys/time.h>
  23. struct timeval c;
  24. int gettimeofday (struct timeval *restrict, void *restrict);
  25. ]],
  26. [[/* glibc uses struct timezone * rather than the POSIX void *
  27. if _GNU_SOURCE is defined. However, since the only portable
  28. use of gettimeofday uses NULL as the second parameter, and
  29. since the glibc definition is actually more typesafe, it is
  30. not worth wrapping this to get a compliant signature. */
  31. int (*f) (struct timeval *restrict, void *restrict)
  32. = gettimeofday;
  33. int x = f (&c, 0);
  34. return !(x | c.tv_sec | c.tv_usec);
  35. ]])],
  36. [gl_cv_func_gettimeofday_posix_signature=yes],
  37. [AC_COMPILE_IFELSE(
  38. [AC_LANG_PROGRAM(
  39. [[#include <sys/time.h>
  40. int gettimeofday (struct timeval *restrict, struct timezone *restrict);
  41. ]])],
  42. [gl_cv_func_gettimeofday_posix_signature=almost],
  43. [gl_cv_func_gettimeofday_posix_signature=no])])])
  44. if test $gl_cv_func_gettimeofday_posix_signature = almost; then
  45. gl_gettimeofday_timezone='struct timezone'
  46. elif test $gl_cv_func_gettimeofday_posix_signature != yes; then
  47. REPLACE_GETTIMEOFDAY=1
  48. fi
  49. dnl If we override 'struct timeval', we also have to override gettimeofday.
  50. if test $REPLACE_STRUCT_TIMEVAL = 1; then
  51. REPLACE_GETTIMEOFDAY=1
  52. fi
  53. m4_ifdef([gl_FUNC_TZSET_CLOBBER], [
  54. gl_FUNC_TZSET_CLOBBER
  55. case "$gl_cv_func_tzset_clobber" in
  56. *yes)
  57. REPLACE_GETTIMEOFDAY=1
  58. gl_GETTIMEOFDAY_REPLACE_LOCALTIME
  59. AC_DEFINE([tzset], [rpl_tzset],
  60. [Define to rpl_tzset if the wrapper function should be used.])
  61. AC_DEFINE([TZSET_CLOBBERS_LOCALTIME], [1],
  62. [Define if tzset clobbers localtime's static buffer.])
  63. ;;
  64. esac
  65. ])
  66. fi
  67. AC_DEFINE_UNQUOTED([GETTIMEOFDAY_TIMEZONE], [$gl_gettimeofday_timezone],
  68. [Define this to 'void' or 'struct timezone' to match the system's
  69. declaration of the second argument to gettimeofday.])
  70. ])
  71. dnl See if gettimeofday clobbers the static buffer that localtime uses
  72. dnl for its return value. The gettimeofday function from Mac OS X 10.0.4
  73. dnl (i.e., Darwin 1.3.7) has this problem.
  74. dnl
  75. dnl If it does, then arrange to use gettimeofday and localtime only via
  76. dnl the wrapper functions that work around the problem.
  77. AC_DEFUN([gl_FUNC_GETTIMEOFDAY_CLOBBER],
  78. [
  79. AC_REQUIRE([gl_HEADER_SYS_TIME_H])
  80. AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
  81. AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
  82. [gl_cv_func_gettimeofday_clobber],
  83. [AC_RUN_IFELSE(
  84. [AC_LANG_PROGRAM(
  85. [[#include <string.h>
  86. #include <sys/time.h>
  87. #include <time.h>
  88. #include <stdlib.h>
  89. ]],
  90. [[
  91. time_t t = 0;
  92. struct tm *lt;
  93. struct tm saved_lt;
  94. struct timeval tv;
  95. lt = localtime (&t);
  96. saved_lt = *lt;
  97. gettimeofday (&tv, NULL);
  98. return memcmp (lt, &saved_lt, sizeof (struct tm)) != 0;
  99. ]])],
  100. [gl_cv_func_gettimeofday_clobber=no],
  101. [gl_cv_func_gettimeofday_clobber=yes],
  102. [# When cross-compiling:
  103. case "$host_os" in
  104. # Guess all is fine on glibc systems.
  105. *-gnu*) gl_cv_func_gettimeofday_clobber="guessing no" ;;
  106. # If we don't know, assume the worst.
  107. *) gl_cv_func_gettimeofday_clobber="guessing yes" ;;
  108. esac
  109. ])])
  110. case "$gl_cv_func_gettimeofday_clobber" in
  111. *yes)
  112. REPLACE_GETTIMEOFDAY=1
  113. gl_GETTIMEOFDAY_REPLACE_LOCALTIME
  114. AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], [1],
  115. [Define if gettimeofday clobbers the localtime buffer.])
  116. ;;
  117. esac
  118. ])
  119. AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
  120. REPLACE_GMTIME=1
  121. REPLACE_LOCALTIME=1
  122. ])
  123. # Prerequisites of lib/gettimeofday.c.
  124. AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
  125. AC_CHECK_HEADERS([sys/timeb.h])
  126. AC_CHECK_FUNCS([_ftime])
  127. ])