bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Status of the win32 gettimeofday module


From: Bruno Haible
Subject: Re: Status of the win32 gettimeofday module
Date: Wed, 17 Jan 2007 02:05:08 +0100 (MET)
User-agent: KMail/1.5.4

Yoann Vandoorselaere asked:
> I'm currently working on a win32 port for libprelude, and we're missing
> a gettimeofday module working under win32. 
>
> I've noticed an attempt to implement win32 support to the current module
> was discussed previously:
> http://thread.gmane.org/gmane.comp.lib.gnulib.bugs/4155/focus=5770
>
> However, the patch never made it to the repository. Are there any plan
> to fix that?

Indeed, Martin Lambers' last patch from 2005-10-08 looks already quite
good. He said he did the paperwork with the FSF.

Reading through the new code and doing cross-compiles to cygwin and mingw
I found and fixed a few further issues:

- The specification header gettimeofday.h should be self-contained,
  except for <config.h>.
- gettimeofday.c should include the specification header first.
- gettimeofday.c: Reorder the functions localtime, gmtime, tzset and
  place the #undefs in such a way that a package can do
    #define rpl_gettimeofday myprefix_gettimeofday
    #define rpl_localtime myprefix_localtime
    #define rpl_gmtime myprefix_gmtime
    #define rpl_tzset myprefix_tzset
  (for namespace cleanliness).
- Use #include <config.h> unconditionally, use #if HAVE_... instead of
  #ifdef HAVE_...
- Activate the POSIX prototype of gettimeofday also if
  GETTIMEOFDAY_CLOBBERS_LOCALTIME, otherwise a compilation error occurs due
  to different prototypes of gettimeofday in <time.h> and gettimeofday.c.
- gettimeofday.m4: AC_LIBSOURCES is considered harmful.
- gettimeofday.m4: gl_C_RESTRICT has been dropped, now we use AC_C_RESTRICT.
- gettimeofday.m4: Ensure TIME_WITH_SYS_TIME and HAVE_SYS_TIME_H are
  properly defined before using them.
- gettimeofday.m4: An autoconf quoting bug.
- The #define gettimeofday rpl_gettimeofday must *not* be done in
  <config.h>, otherwise when <time.h> is included it defines a prototype
  for rpl_gettimeofday that conflicts with the definition of
  rpl_gettimeofday in gettimeofday.c.
- The #define for tzset was missing.
- Module description: The 'restrict' module is gone.
- test-gettimeofday.c: Avoid an "unused variable" warning.

--- lib/gettimeofday.c  17 Jan 2007 00:42:21 -0000      1.9
+++ lib/gettimeofday.c  17 Jan 2007 00:48:44 -0000
@@ -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
@@ -24,35 +26,19 @@
 
 #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 @@
    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 @@
 
   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 @@
 
   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 @@
    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 @@
 # 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
--- lib/gettimeofday.h  17 Jan 2007 00:42:21 -0000      1.1
+++ lib/gettimeofday.h  17 Jan 2007 00:48:44 -0000
@@ -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
@@ -22,9 +25,18 @@
 #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 @@
 };
 #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 */
--- m4/gettimeofday.m4  17 Jan 2007 00:42:21 -0000      1.11
+++ m4/gettimeofday.m4  17 Jan 2007 00:48:45 -0000
@@ -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 @@
 #    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_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_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 @@
   ])
   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.
--- modules/gettimeofday        17 Jan 2007 00:42:21 -0000      1.7
+++ modules/gettimeofday        17 Jan 2007 00:48:46 -0000
@@ -2,12 +2,11 @@
 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 @@
 Makefile.am:
 
 Include:
-<sys/time.h>
 "gettimeofday.h"
 
 License:
--- tests/test-gettimeofday.c   17 Jan 2007 00:42:21 -0000      1.1
+++ tests/test-gettimeofday.c   17 Jan 2007 00:48:46 -0000
@@ -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
@@ -17,30 +17,18 @@
  * 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;


The complete ChangeLog entry is as follows:

2005-10-08  Martin Lambers  <address@hidden>
2005-10-08  Paul Eggert  <address@hidden>
2007-01-16  Bruno Haible  <address@hidden>

        * 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.






reply via email to

[Prev in Thread] Current Thread [Next in Thread]