bug-gnulib
[Top][All Lists]
Advanced

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

Re: gettimeofday() for Win32


From: Martin Lambers
Subject: Re: gettimeofday() for Win32
Date: Mon, 22 May 2006 22:16:55 +0200
User-agent: Mutt/1.5.11+cvs20060403

On Sun, 09. Oct 2005, 08:56:50 +0200, Ralf Wildenhues wrote:
> If I may, a couple of comments on the code:

Thanks! I'm sorry that it took me so long to respond to this.
(The original thread is archived here: 
http://www.mail-archive.com/bug-gnulib%40gnu.org/msg00823.html )

I updated the patch according to your comments. I'd be happy if it would
be reconsidered for inclusion.

Martin


diff -uNr gnulib/lib/gettimeofday.c gnulib-gettimeofday-2/lib/gettimeofday.c
--- gnulib/lib/gettimeofday.c   2005-09-19 19:28:14.000000000 +0200
+++ gnulib-gettimeofday-2/lib/gettimeofday.c    2006-05-22 22:03:19.000000000 
+0200
@@ -1,8 +1,9 @@
-/* Work around the bug in some systems whereby gettimeofday clobbers the
-   static buffer that localtime uses for it's return value.  The gettimeofday
+/* 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 its 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 Free Software Foundation, Inc.
+   Copyright (C) 2001, 2002, 2003, 2006 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
@@ -46,7 +47,13 @@
 
 #include <stdlib.h>
 
+#if HAVE_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.
@@ -54,6 +61,7 @@
    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)
 {
@@ -64,8 +72,10 @@
 
   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)
 {
@@ -76,16 +86,20 @@
 
   return tm;
 }
+#endif
 
-/* This is a wrapper for gettimeofday.  It is used only on systems for which
-   gettimeofday clobbers 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 gettimeofday.  */
+/* This is a wrapper for gettimeofday.  
+   It is used only on systems that lack this function, or for whose 
+   implementation of this function causes problems. */
 
 int
-rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
+rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
 {
+#if HAVE_GETTIMEOFDAY
+# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
+
+  /* Save and restore the contents of the buffer used for localtime's result
+     around the call to gettimeofday. */
   struct tm save;
   int result;
 
@@ -100,12 +114,39 @@
   *localtime_buffer_addr = save;
 
   return result;
+
+# endif
+#else
+# if HAVE__FTIME
+
+  struct _timeb timebuf;
+  
+  _ftime (&timebuf);
+  tv->tv_sec = timebuf.time;
+  tv->tv_usec = timebuf.millitm * 1000;
+
+  return 0;
+
+# else
+
+  time_t t = time (NULL);
+  
+  if (t == (time_t) -1)
+    return -1;
+  tv->tv_sec = t;
+  tv->tv_usec = 0;
+
+  return 0;
+
+# 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)
 {
@@ -121,3 +162,4 @@
   tzset ();
   *localtime_buffer_addr = save;
 }
+#endif
diff -uNr gnulib/lib/gettimeofday.h gnulib-gettimeofday-2/lib/gettimeofday.h
--- gnulib/lib/gettimeofday.h   1970-01-01 01:00:00.000000000 +0100
+++ gnulib-gettimeofday-2/lib/gettimeofday.h    2006-05-22 22:03:25.000000000 
+0200
@@ -0,0 +1,39 @@
+/* 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 its 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, 2006 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
+   the Free Software Foundation; either version 2, or (at your option)
+   any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software Foundation,
+   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+
+#ifndef GETTIMEOFDAY_H
+#define GETTIMEOFDAY_H
+
+#ifndef HAVE_STRUCT_TIMEVAL
+struct timeval
+{
+  time_t tv_sec;
+  suseconds_t tv_usec;
+};
+#endif
+
+#ifndef HAVE_GETTIMEOFDAY_POSIX_SIGNATURE
+# undef gettimeofday
+# define gettimeofday rpl_gettimeofday
+int gettimeofday (struct timeval *restrict tp, void *restrict tzp);
+#endif
+
+#endif /* GETTIMEOFDAY_H */
diff -uNr gnulib/m4/gettimeofday.m4 gnulib-gettimeofday-2/m4/gettimeofday.m4
--- gnulib/m4/gettimeofday.m4   2005-05-18 21:47:30.000000000 +0200
+++ gnulib-gettimeofday-2/m4/gettimeofday.m4    2006-05-22 21:52:55.000000000 
+0200
@@ -1,10 +1,84 @@
-#serial 7
+#serial 8
 
 # Copyright (C) 2001, 2002, 2003, 2005 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_CHECK_FUNCS([gettimeofday])
+  
+  AC_CHECK_TYPE([suseconds_t], ,
+    [AC_DEFINE([suseconds_t], [int],
+       [Define to `int' if `suseconds_t' is missing.])
+    ],
+    [
+#    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
+    ])
+
+  AC_CACHE_CHECK([for struct timeval], fu_cv_sys_struct_timeval,
+    [AC_TRY_COMPILE(
+      [
+#      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
+      ],
+      [static struct timeval x; x.tv_sec = x.tv_usec;],
+      fu_cv_sys_struct_timeval=yes,
+      fu_cv_sys_struct_timeval=no)
+    ])
+
+  if test $fu_cv_sys_struct_timeval = yes; then
+    AC_DEFINE(HAVE_STRUCT_TIMEVAL, 1,
+      [Define if struct timeval is declared in <time.h> or <sys/time.h>. ])
+  fi
+  
+  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_cv_func_gettimeofday_posix_signature=yes],
+       [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
+    gl_PREREQ_GETTIMEOFDAY
+    AC_LIBOBJ([gettimeofday])
+  fi
+])
+
 dnl From Jim Meyering.
 dnl
 dnl See if gettimeofday clobbers the static buffer that localtime uses
@@ -62,11 +136,13 @@
 
     AC_DEFINE(gettimeofday, rpl_gettimeofday,
       [Define to rpl_gettimeofday if the replacement function should be used.])
-    gl_PREREQ_GETTIMEOFDAY
   fi
 ])
 
 AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [
+  gl_PREREQ_GETTIMEOFDAY
+  AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME, 1,
+    [Define if gettimeofday clobbers the localtime buffer.])
   AC_LIBOBJ(gettimeofday)
   AC_DEFINE(gmtime, rpl_gmtime,
     [Define to rpl_gmtime if the replacement function should be used.])
@@ -77,4 +153,6 @@
 # Prerequisites of lib/gettimeofday.c.
 AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [
   AC_REQUIRE([AC_HEADER_TIME])
+  AC_CHECK_HEADERS([sys/timeb.h])
+  AC_CHECK_FUNCS([_ftime])
 ])
diff -uNr gnulib/modules/gettimeofday gnulib-gettimeofday-2/modules/gettimeofday
--- gnulib/modules/gettimeofday 2004-09-22 17:11:04.000000000 +0200
+++ gnulib-gettimeofday-2/modules/gettimeofday  2006-05-22 22:04:59.000000000 
+0200
@@ -3,17 +3,20 @@
 
 Files:
 lib/gettimeofday.c
+lib/gettimeofday.h
 m4/gettimeofday.m4
 
 Depends-on:
+restrict
 
 configure.ac:
-AC_FUNC_GETTIMEOFDAY_CLOBBER
+gl_FUNC_GETTIMEOFDAY
 
 Makefile.am:
 
 Include:
 <sys/time.h>
+"gettimeofday.h"
 
 License:
 GPL
diff -uNr gnulib/tests/test-gettimeofday.c 
gnulib-gettimeofday-2/tests/test-gettimeofday.c
--- gnulib/tests/test-gettimeofday.c    1970-01-01 01:00:00.000000000 +0100
+++ gnulib-gettimeofday-2/tests/test-gettimeofday.c     2006-05-22 
22:01:39.000000000 +0200
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2006 Free Software Foundation
+ * Written by Jim Meyering.
+ *
+ * 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
+ * the Free Software Foundation; either version 2, or (at your option)
+ * any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.  */
+
+#if HAVE_CONFIG_H
+# include <config.h>
+#endif
+
+#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"
+
+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;
+  struct timeval tv;
+  lt = localtime (&t);
+  saved_lt = *lt;
+  gettimeofday (&tv, NULL);
+  if (memcmp (lt, &saved_lt, sizeof (struct tm)) != 0)
+    {
+      fprintf (stderr, "gettimeofday still clobbers the localtime buffer!\n");
+      return 1;
+    }
+  else
+    {
+      return 0;
+    }
+}




reply via email to

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