Various fixes to Martin Lambers' gettimeofday patch.
authorBruno Haible <bruno@clisp.org>
Wed, 17 Jan 2007 01:04:17 +0000 (01:04 +0000)
committerBruno Haible <bruno@clisp.org>
Wed, 17 Jan 2007 01:04:17 +0000 (01:04 +0000)
ChangeLog
lib/gettimeofday.c
lib/gettimeofday.h
m4/gettimeofday.m4
modules/gettimeofday
tests/test-gettimeofday.c

index 5fe7101..6081d0a 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,26 @@
+2005-10-08  Martin Lambers  <marlam@marlam.de>
+2005-10-08  Paul Eggert  <eggert@cs.ucla.edu>
+2007-01-16  Bruno Haible  <bruno@clisp.org>
+
+       * modules/gettimeofday (Files): Add lib/gettimeofday.h.
+       (configure.ac): Remove AC_FUNC_GETTIMEOFDAY_CLOBBER. Add
+       gl_FUNC_GETTIMEOFDAY.
+       (Include): Add gettimeofday.h.
+       * m4/gettimeofday.m4 (gl_FUNC_GETTIMEOFDAY): New macro.
+       (AC_FUNC_GETTIMEOFDAY_CLOBBER): Don't invoke gl_PREREQ_GETTIMEOFDAY.
+       (gl_GETTIMEOFDAY_REPLACE_LOCALTIME): Define
+       GETTIMEOFDAY_CLOBBERS_LOCALTIME. Invoke gl_PREREQ_GETTIMEOFDAY here.
+       (gl_PREREQ_GETTIMEOFDAY): Check for <sys/timeb.h> and _ftime.
+       * lib/gettimeofday.h: New file.
+       * lib/gettimeofday.c: Include <sys/timeb.h>.
+       (localtime_buffer_addr, rpl_localtime, rpl_gmtime, rpl_tzset): Define
+       only if GETTIMEOFDAY_CLOBBERS_LOCALTIME.
+       (rpl_gettimeofday) [!HAVE_GETTIMEOFDAY]: Use _ftime() when available;
+       fall back on time().
+
+       * tests/test-gettimeofday.c: New file.
+       * modules/gettimeofday-tests: New file.
+
 2007-01-16  Eric Blake  <ebb9@byu.net>
 
        * modules/fnmatch (Depends-on): Depend on wchar.
index 99f49b0..e0afa49 100644 (file)
@@ -1,10 +1,12 @@
-/* Provide gettimeofday for systems that don't have it, or,
-   work around the bug in some systems whereby gettimeofday clobbers the
-   static buffer that localtime uses for it's return value.  The gettimeofday
-   function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
-   The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6.
+/* Provide gettimeofday
+   1. for systems that don't have it,
+   2. for some systems where gettimeofday clobbers the static buffer that
+      localtime uses for it's return value.  The gettimeofday function from
+      Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
+      The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and
+      2.6.
 
-   Copyright (C) 2001, 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <config.h>
 
-/* Disable the definitions of these functions (from config.h)
-   so we can use the library versions here.  */
-#undef gettimeofday
-#undef gmtime
-#undef localtime
-#undef tzset
+/* Specification.  */
+#include "gettimeofday.h"
 
 #include <sys/types.h>
-
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
 #include <stdlib.h>
 
 #if HAVE_SYS_TIMEB_H
-#include <sys/timeb.h>
+# include <sys/timeb.h>
 #endif
 
 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME
+
 static struct tm *localtime_buffer_addr;
-#endif
 
 /* This is a wrapper for localtime.  It is used only on systems for which
    gettimeofday clobbers the static buffer used for localtime's result.
@@ -60,10 +46,11 @@ static struct tm *localtime_buffer_addr;
    On the first call, record the address of the static buffer that
    localtime uses for its result.  */
 
-#if GETTIMEOFDAY_CLOBBERS_LOCALTIME
 struct tm *
-rpl_localtime (const time_t *timep)
+localtime (const time_t *timep)
+#undef localtime
 {
+  extern struct tm *localtime (const time_t *);
   struct tm *tm = localtime (timep);
 
   if (! localtime_buffer_addr)
@@ -71,13 +58,13 @@ rpl_localtime (const time_t *timep)
 
   return tm;
 }
-#endif
 
 /* Same as above, since gmtime and localtime use the same buffer.  */
-#if GETTIMEOFDAY_CLOBBERS_LOCALTIME
 struct tm *
-rpl_gmtime (const time_t *timep)
+gmtime (const time_t *timep)
+#undef gmtime
 {
+  extern struct tm *gmtime (const time_t *);
   struct tm *tm = gmtime (timep);
 
   if (! localtime_buffer_addr)
@@ -85,6 +72,30 @@ rpl_gmtime (const time_t *timep)
 
   return tm;
 }
+
+/* This is a wrapper for tzset. It is used only on systems for which
+   tzset may clobber the static buffer used for localtime's result.
+   Save and restore the contents of the buffer used for localtime's
+   result around the call to tzset.  */
+void
+tzset (void)
+#undef tzset
+{
+  extern struct tm *localtime (const time_t *);
+  extern void tzset (void);
+  struct tm save;
+
+  if (! localtime_buffer_addr)
+    {
+      time_t t = 0;
+      localtime_buffer_addr = localtime (&t);
+    }
+
+  save = *localtime_buffer_addr;
+  tzset ();
+  *localtime_buffer_addr = save;
+}
+
 #endif
 
 /* This is a wrapper for gettimeofday.  
@@ -92,10 +103,13 @@ rpl_gmtime (const time_t *timep)
    implementation of this function causes problems. */
 
 int
-rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
+gettimeofday (struct timeval *restrict tv, void *restrict tz)
+#undef gettimeofday
 {
 #if HAVE_GETTIMEOFDAY
 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
+  extern struct tm *localtime (const time_t *);
+  extern int gettimeofday (/* unspecified arguments */);
 
   /* Save and restore the contents of the buffer used for localtime's result
      around the call to gettimeofday. */
@@ -140,25 +154,3 @@ rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
 # endif
 #endif
 }
-
-/* This is a wrapper for tzset. It is used only on systems for which
-   tzset may clobber the static buffer used for localtime's result.
-   Save and restore the contents of the buffer used for localtime's
-   result around the call to tzset.  */
-#if GETTIMEOFDAY_CLOBBERS_LOCALTIME
-void
-rpl_tzset (void)
-{
-  struct tm save;
-
-  if (! localtime_buffer_addr)
-    {
-      time_t t = 0;
-      localtime_buffer_addr = localtime (&t);
-    }
-
-  save = *localtime_buffer_addr;
-  tzset ();
-  *localtime_buffer_addr = save;
-}
-#endif
index 6a8230b..d361ed1 100644 (file)
@@ -1,9 +1,12 @@
-/* Provide gettimeofday for systems that don't have it, or 
-   work around the bug in some systems whereby gettimeofday clobbers the
-   static buffer that localtime uses for it's return value.  The gettimeofday
-   function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
-   The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6.
-   Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
+/* Provide gettimeofday
+   1. for systems that don't have it,
+   2. for some systems where gettimeofday clobbers the static buffer that
+      localtime uses for it's return value.  The gettimeofday function from
+      Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
+      The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and
+      2.6.
+
+   Copyright (C) 2001, 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 #ifndef GETTIMEOFDAY_H
 #define GETTIMEOFDAY_H
 
-#include <config.h>
+#if TIME_WITH_SYS_TIME
+# include <sys/time.h>
+# include <time.h>
+#else
+# if HAVE_SYS_TIME_H
+#  include <sys/time.h>
+# else
+#  include <time.h>
+# endif
+#endif
 
-#ifndef HAVE_STRUCT_TIMEVAL
+#if !HAVE_STRUCT_TIMEVAL
 struct timeval
 {
   time_t tv_sec;
@@ -32,10 +44,10 @@ struct timeval
 };
 #endif
 
-#ifndef HAVE_GETTIMEOFDAY_POSIX_SIGNATURE
+#if !HAVE_GETTIMEOFDAY_POSIX_SIGNATURE || GETTIMEOFDAY_CLOBBERS_LOCALTIME
 # undef gettimeofday
 # define gettimeofday rpl_gettimeofday
-int gettimeofday (struct timeval *restrict tp, void *restrict tzp);
+extern int gettimeofday (struct timeval *restrict tp, void *restrict tzp);
 #endif
 
 #endif /* GETTIMEOFDAY_H */
index e4d75da..51b916a 100644 (file)
@@ -1,14 +1,15 @@
 #serial 8
 
-# Copyright (C) 2001, 2002, 2003, 2005 Free Software Foundation, Inc.
+# Copyright (C) 2001, 2002, 2003, 2005, 2007 Free Software Foundation, Inc.
 # This file is free software; the Free Software Foundation
 # gives unlimited permission to copy and/or distribute it,
 # with or without modifications, as long as this notice is preserved.
 
 AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
 [
-  AC_LIBSOURCES([gettimeofday.c, gettimeofday.h])
-  AC_REQUIRE([gl_C_RESTRICT])
+  AC_REQUIRE([AC_C_RESTRICT])
+  AC_REQUIRE([AC_HEADER_TIME])
+  AC_CHECK_HEADERS_ONCE([sys/time.h])
   AC_CHECK_FUNCS([gettimeofday])
   
   AC_CHECK_TYPE([suseconds_t], ,
@@ -28,7 +29,7 @@ AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
 #    endif
     ])
 
-  AC_CACHE_CHECK([for struct timeval], fu_cv_sys_struct_timeval,
+  AC_CACHE_CHECK([for struct timeval], [fu_cv_sys_struct_timeval],
     [AC_TRY_COMPILE(
       [
 #      if TIME_WITH_SYS_TIME
@@ -54,26 +55,25 @@ AC_DEFUN([gl_FUNC_GETTIMEOFDAY],
   
   AC_CACHE_CHECK([for gettimeofday whose signature conforms to POSIX],
     [ac_cv_func_gettimeofday_posix_signature],
-    AC_LINK_IFELSE(
-      [AC_LANG_PROGRAM(
-         [[#include <sys/time.h>
-          time_t a;
-          suseconds_t b;
-          struct timeval c;
-        ]],
-        [[
-          int x = gettimeofday (&c, 0);
-          int (*f) (struct timeval *restrict, void *restrict) = gettimeofday;
-          return !(x | c.tv_sec | c.tv_usec);
-        ]])],
+    [AC_LINK_IFELSE(
+       [AC_LANG_PROGRAM(
+          [[#include <sys/time.h>
+           time_t a;
+           suseconds_t b;
+           struct timeval c;
+         ]],
+         [[
+           int x = gettimeofday (&c, 0);
+           int (*f) (struct timeval *restrict, void *restrict) = gettimeofday;
+           return !(x | c.tv_sec | c.tv_usec);
+         ]])],
        [ac_cv_func_gettimeofday_posix_signature=yes],
-       [ac_cv_func_gettimeofday_posix_signature=no]))
+       [ac_cv_func_gettimeofday_posix_signature=no])])
   if test $ac_cv_func_gettimeofday_posix_signature = yes; then
     AC_DEFINE([HAVE_GETTIMEOFDAY_POSIX_SIGNATURE], 1,
       [Define if gettimeofday's signature conforms to POSIX.])
     AC_FUNC_GETTIMEOFDAY_CLOBBER
-  fi
-  if test $ac_cv_func_gettimeofday_posix_signature != yes; then
+  else
     gl_PREREQ_GETTIMEOFDAY
     AC_LIBOBJ([gettimeofday])
   fi
@@ -92,7 +92,7 @@ AC_DEFUN([AC_FUNC_GETTIMEOFDAY_CLOBBER],
 [
  AC_REQUIRE([AC_HEADER_TIME])
  AC_CACHE_CHECK([whether gettimeofday clobbers localtime buffer],
-  jm_cv_func_gettimeofday_clobber,
+  [jm_cv_func_gettimeofday_clobber],
   [AC_TRY_RUN([
 #include <stdio.h>
 #include <string.h>
@@ -133,21 +133,20 @@ main ()
   ])
   if test $jm_cv_func_gettimeofday_clobber = yes; then
     gl_GETTIMEOFDAY_REPLACE_LOCALTIME
-
-    AC_DEFINE(gettimeofday, rpl_gettimeofday,
-      [Define to rpl_gettimeofday if the replacement function should be used.])
   fi
 ])
 
 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
+  AC_LIBOBJ(gettimeofday)
   gl_PREREQ_GETTIMEOFDAY
-  AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME, 1,
+  AC_DEFINE([GETTIMEOFDAY_CLOBBERS_LOCALTIME], 1,
     [Define if gettimeofday clobbers the localtime buffer.])
-  AC_LIBOBJ(gettimeofday)
-  AC_DEFINE(gmtime, rpl_gmtime,
+  AC_DEFINE([gmtime], [rpl_gmtime],
     [Define to rpl_gmtime if the replacement function should be used.])
-  AC_DEFINE(localtime, rpl_localtime,
+  AC_DEFINE([localtime], [rpl_localtime],
     [Define to rpl_localtime if the replacement function should be used.])
+  AC_DEFINE([tzset], [rpl_tzset],
+    [Define to rpl_tzset if the replacement function should be used.])
 ])
 
 # Prerequisites of lib/gettimeofday.c.
index 270029b..9b9b35a 100644 (file)
@@ -2,12 +2,11 @@ Description:
 gettimeofday() function: return current time.
 
 Files:
-lib/gettimeofday.c
 lib/gettimeofday.h
+lib/gettimeofday.c
 m4/gettimeofday.m4
 
 Depends-on:
-restrict
 
 configure.ac:
 gl_FUNC_GETTIMEOFDAY
@@ -15,7 +14,6 @@ gl_FUNC_GETTIMEOFDAY
 Makefile.am:
 
 Include:
-<sys/time.h>
 "gettimeofday.h"
 
 License:
index 50897a1..e98266a 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2005 Free Software Foundation
+ * Copyright (C) 2005, 2007 Free Software Foundation
  * Written by Jim Meyering.
  *
  * This program is free software; you can redistribute it and/or modify
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301, USA.  */
 
-#if HAVE_CONFIG_H
-# include <config.h>
-#endif
+#include <config.h>
 
 #include <stdio.h>
 #include <string.h>
 
-#if TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# if HAVE_SYS_TIME_H
-#  include <sys/time.h>
-# else
-#  include <time.h>
-# endif
-#endif
-
 #include "gettimeofday.h"
 
+suseconds_t dummy = 0; /* just to test if this type is available */
+
 int
 main (int argc, char *argv[])
 {
-  suseconds_t dummy = 0;       /* just to test if this type is available */
   time_t t = 0;
   struct tm *lt;
   struct tm saved_lt;