From fc0de83a1e5cca98557f603670284f247b34f491 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 17 Jan 2007 00:42:21 +0000 Subject: [PATCH] Patch from Martin Lambers , from 2005-10-08 --- lib/gettimeofday.c | 58 ++++++++++++++++++++++++++++----- lib/gettimeofday.h | 41 ++++++++++++++++++++++++ m4/gettimeofday.m4 | 82 +++++++++++++++++++++++++++++++++++++++++++++-- modules/gettimeofday | 5 ++- tests/test-gettimeofday.c | 60 ++++++++++++++++++++++++++++++++++ 5 files changed, 235 insertions(+), 11 deletions(-) create mode 100644 lib/gettimeofday.h create mode 100644 tests/test-gettimeofday.c diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c index f2fad8cf4..99f49b04f 100644 --- a/lib/gettimeofday.c +++ b/lib/gettimeofday.c @@ -1,9 +1,10 @@ -/* Work around the bug in some systems whereby gettimeofday clobbers the +/* 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, 2006 Free Software Foundation, Inc. + Copyright (C) 2001, 2002, 2003, 2005, 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 @@ -45,7 +46,13 @@ #include +#if HAVE_SYS_TIMEB_H +#include +#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. @@ -53,6 +60,7 @@ 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) { @@ -63,8 +71,10 @@ 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) { @@ -75,16 +85,20 @@ rpl_gmtime (const time_t *timep) 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; @@ -99,12 +113,39 @@ rpl_gettimeofday (struct timeval *tv, struct timezone *tz) *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) { @@ -120,3 +161,4 @@ rpl_tzset (void) tzset (); *localtime_buffer_addr = save; } +#endif diff --git a/lib/gettimeofday.h b/lib/gettimeofday.h new file mode 100644 index 000000000..6a8230bad --- /dev/null +++ b/lib/gettimeofday.h @@ -0,0 +1,41 @@ +/* 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. + + 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 + +#include + +#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 --git a/m4/gettimeofday.m4 b/m4/gettimeofday.m4 index 2fdc531cb..e4d75da00 100644 --- a/m4/gettimeofday.m4 +++ b/m4/gettimeofday.m4 @@ -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 +# include +# else +# if HAVE_SYS_TIME_H +# include +# else +# include +# endif +# endif + ]) + + AC_CACHE_CHECK([for struct timeval], fu_cv_sys_struct_timeval, + [AC_TRY_COMPILE( + [ +# if TIME_WITH_SYS_TIME +# include +# include +# else +# if HAVE_SYS_TIME_H +# include +# else +# include +# 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 or . ]) + fi + + AC_CACHE_CHECK([for gettimeofday whose signature conforms to POSIX], + [ac_cv_func_gettimeofday_posix_signature], + AC_LINK_IFELSE( + [AC_LANG_PROGRAM( + [[#include + 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 @@ main () 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 @@ AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [ # 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 --git a/modules/gettimeofday b/modules/gettimeofday index b4874baed..270029bba 100644 --- a/modules/gettimeofday +++ b/modules/gettimeofday @@ -3,17 +3,20 @@ gettimeofday() function: return current time. 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: +"gettimeofday.h" License: GPL diff --git a/tests/test-gettimeofday.c b/tests/test-gettimeofday.c new file mode 100644 index 000000000..50897a122 --- /dev/null +++ b/tests/test-gettimeofday.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2005 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 +#endif + +#include +#include + +#if TIME_WITH_SYS_TIME +# include +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# 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; + } +} -- 2.11.0