From: Paul Eggert Date: Thu, 14 Aug 2003 21:59:28 +0000 (+0000) Subject: Add wrappers for gmtime and tzset, too. X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c68009f90028d2f69c6303cd98f08ac1d88923da;p=pspp Add wrappers for gmtime and tzset, too. --- diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c index 0d259be1cf..1ebe4d0ffd 100644 --- a/lib/gettimeofday.c +++ b/lib/gettimeofday.c @@ -1,7 +1,8 @@ /* 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. - Copyright (C) 2001, 2002 Free Software Foundation, Inc. + 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. 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 @@ -21,10 +22,12 @@ #include -/* Disable the definitions of gettimeofday and localtime (from 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 #include @@ -60,6 +63,18 @@ rpl_localtime (const time_t *timep) return tm; } +/* Same as above, since gmtime and localtime use the same buffer. */ +struct tm * +rpl_gmtime (const time_t *timep) +{ + struct tm *tm = gmtime (timep); + + if (! localtime_buffer_addr) + localtime_buffer_addr = tm; + + return tm; +} + /* This is a wrapper for gettimeofday. It is used only on systems for which gettimeofday clobbers the static buffer used for localtime's result. @@ -84,3 +99,23 @@ rpl_gettimeofday (struct timeval *tv, struct timezone *tz) return result; } + +/* 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 +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; +} diff --git a/m4/gettimeofday.m4 b/m4/gettimeofday.m4 index 836a4b91b9..e7a5a6db6e 100644 --- a/m4/gettimeofday.m4 +++ b/m4/gettimeofday.m4 @@ -1,4 +1,4 @@ -#serial 3 +#serial 4 dnl From Jim Meyering. dnl @@ -58,9 +58,8 @@ main () jm_cv_func_gettimeofday_clobber=yes) ]) if test $jm_cv_func_gettimeofday_clobber = yes; then - AC_LIBOBJ(gettimeofday) - AC_DEFINE(localtime, rpl_localtime, - [Define to rpl_localtime if the replacement function should be used.]) + gl_GETTIMEOFDAY_REPLACE_LOCALTIME + AC_DEFINE(gettimeofday, rpl_gettimeofday, [Define to rpl_gettimeofday if the replacement function should be used.]) AC_DEFINE(GETTIMEOFDAY_CLOBBERS_LOCALTIME_BUFFER, 1, @@ -69,6 +68,14 @@ main () fi ]) +AC_DEFUN([gl_GETTIMEOFDAY_REPLACE_LOCALTIME], [ + AC_LIBOBJ(gettimeofday) + AC_DEFINE(gmtime, rpl_gmtime, + [Define to rpl_gmtime if the replacement function should be used.]) + AC_DEFINE(localtime, rpl_localtime, + [Define to rpl_localtime if the replacement function should be used.]) +]) + # Prerequisites of lib/gettimeofday.c. AC_DEFUN([gl_PREREQ_GETTIMEOFDAY], [ AC_REQUIRE([AC_HEADER_TIME]) diff --git a/m4/tzset.m4 b/m4/tzset.m4 new file mode 100644 index 0000000000..6df4eb7f99 --- /dev/null +++ b/m4/tzset.m4 @@ -0,0 +1,62 @@ +#serial 1 +# See if we have a working tzset function. +# If so, arrange to compile the wrapper function. +# For at least Solaris 2.5.1 and 2.6, this is necessary +# because tzset can clobber the contents of the buffer +# used by localtime. + +# Written by Paul Eggert and Jim Meyering. + +AC_DEFUN([gl_FUNC_TZSET_CLOBBER], +[ + AC_REQUIRE([AC_HEADER_TIME]) + AC_CACHE_CHECK([whether tzset clobbers localtime buffer], + gl_cv_func_tzset_clobber, + [ + AC_RUN_IFELSE([AC_LANG_SOURCE([[ +#if TIME_WITH_SYS_TIME +# include +# include +#else +# if HAVE_SYS_TIME_H +# include +# else +# include +# endif +#endif +#include + +int +main () +{ + time_t t1 = 853958121; + struct tm *p, s; + putenv ("TZ=GMT0"); + p = localtime (&t1); + s = *p; + putenv ("TZ=EST+3EDT+2,M10.1.0/00:00:00,M2.3.0/00:00:00"); + tzset (); + exit (p->tm_year != s.tm_year + || p->tm_mon != s.tm_mon + || p->tm_mday != s.tm_mday + || p->tm_hour != s.tm_hour + || p->tm_min != s.tm_min + || p->tm_sec != s.tm_sec); +} + ]])], + [gl_cv_func_tzset_clobber=no], + [gl_cv_func_tzset_clobber=yes], + [gl_cv_func_tzset_clobber=yes])]) + + AC_DEFINE(HAVE_RUN_TZSET_TEST, 1, + [Define to 1 if you have run the test for working tzset.]) + + if test $gl_cv_func_tzset_clobber = yes; then + gl_GETTIMEOFDAY_REPLACE_LOCALTIME + + AC_DEFINE(tzset, rpl_tzset, + [Define to rpl_tzset if the wrapper function should be used.]) + AC_DEFINE(TZSET_CLOBBERS_LOCALTIME_BUFFER, 1, + [Define if tzset clobbers localtime's static buffer.]) + fi +]) diff --git a/modules/tzset b/modules/tzset new file mode 100644 index 0000000000..c0e9304747 --- /dev/null +++ b/modules/tzset @@ -0,0 +1,20 @@ +Description: +tzset - initialize time conversion information + +Files: +m4/tzset.m4 + +Depends-on: +gettimeofday + +configure.ac: +gl_FUNC_TZSET_CLOBBER + +Makefile.am: + +Include: + + +Maintainer: +Jim Meyering +