From e5ba383c5c9684ed2abe901d35485b86725fbea8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Wed, 17 Jan 2007 01:04:17 +0000 Subject: [PATCH] Various fixes to Martin Lambers' gettimeofday patch. --- ChangeLog | 23 +++++++++++ lib/gettimeofday.c | 100 +++++++++++++++++++++------------------------- lib/gettimeofday.h | 32 ++++++++++----- m4/gettimeofday.m4 | 53 ++++++++++++------------ modules/gettimeofday | 4 +- tests/test-gettimeofday.c | 20 ++-------- 6 files changed, 122 insertions(+), 110 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5fe710191..6081d0aca 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,26 @@ +2005-10-08 Martin Lambers +2005-10-08 Paul Eggert +2007-01-16 Bruno Haible + + * 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 and _ftime. + * lib/gettimeofday.h: New file. + * lib/gettimeofday.c: Include . + (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 * modules/fnmatch (Depends-on): Depend on wchar. diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c index 99f49b04f..e0afa49cf 100644 --- a/lib/gettimeofday.c +++ b/lib/gettimeofday.c @@ -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 -/* 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 - -#if TIME_WITH_SYS_TIME -# include -# include -#else -# if HAVE_SYS_TIME_H -# include -# else -# include -# endif -#endif - #include #if HAVE_SYS_TIMEB_H -#include +# 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. @@ -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 diff --git a/lib/gettimeofday.h b/lib/gettimeofday.h index 6a8230bad..d361ed1e8 100644 --- a/lib/gettimeofday.h +++ b/lib/gettimeofday.h @@ -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 +#if TIME_WITH_SYS_TIME +# include +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# 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 */ diff --git a/m4/gettimeofday.m4 b/m4/gettimeofday.m4 index e4d75da00..51b916a81 100644 --- a/m4/gettimeofday.m4 +++ b/m4/gettimeofday.m4 @@ -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 - 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 + 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 #include @@ -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. diff --git a/modules/gettimeofday b/modules/gettimeofday index 270029bba..9b9b35a47 100644 --- a/modules/gettimeofday +++ b/modules/gettimeofday @@ -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: - "gettimeofday.h" License: diff --git a/tests/test-gettimeofday.c b/tests/test-gettimeofday.c index 50897a122..e98266acd 100644 --- a/tests/test-gettimeofday.c +++ b/tests/test-gettimeofday.c @@ -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 -#endif +#include #include #include -#if TIME_WITH_SYS_TIME -# include -# include -#else -# if HAVE_SYS_TIME_H -# include -# else -# include -# 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; -- 2.11.0