summaryrefslogtreecommitdiff
path: root/lib/gettimeofday.c
diff options
context:
space:
mode:
authorPaul Eggert <eggert@cs.ucla.edu>2007-01-18 08:33:34 +0000
committerPaul Eggert <eggert@cs.ucla.edu>2007-01-18 08:33:34 +0000
commit5661ab9d49512b4cf3c19caa11bb581d8ce561ba (patch)
tree9c6c5d382b0d1ad236805f2012ab169b237f2595 /lib/gettimeofday.c
parent0a2f71be9fe8da7b21600957e5e56346d3b027a6 (diff)
downloadgnulib-5661ab9d49512b4cf3c19caa11bb581d8ce561ba.tar.gz
* MODULES.html.sh (Support for systems lacking POSIX:2001): New
module sys_time. * lib/gethrxtime.c: Include <sys/time.h>, since we can no longer assume timespec.h defines struct timeval. * lib/settime.c: Likewise. * lib/utimens.c: Likewise. * lib/gettime.c (gettime): Remove test against HAVE_GETTIMEOFDAY, since we now assume the gettimeofday module. * lib/tempname.c (__gen_tempname): Likewise. * lib/gettimeofday.h: Remove. * lib/gettimeofday.c: Include <sys/time.h> instead of "gettimeofday.h". Don't include <sys/types.h> and <stdlib.h>; shouldn't be needed. Include <time.h>, for 'time()'. (localtime_buffer_addr): Also use this workaround if TZSET_CLOBBERS_LOCALTIME. Set to a dummy static variable by default, to simplify the uses. All uses changed. (localtime, gmtime, tzset, gettimeofday): Reformat slightly so that #undef is inside {}, and 'const' follows type name consistently. (tzset): Define replacement only if TZSET_CLOBBERS_LOCALTIME. (gettimeofday): Do not use the maximum possible value for tv->tv_usec, since that might break usages other than ls.c. Instead, we'll leave ls.c alone. This undoes today's patch by Bruno. Add a compile-time warning for 1s-clock resolution; we've never observed the problem but might as well keep the canary. * lib/nanosleep.c: Include timespec.h first, for interface check. * lib/nanosleep.c: Include <sys/time.h> unconditionally, since we now assume the sys_time module. * lib/tempname.c: Likewise. * lib/timespec.h: Likewise. * lib/nanosleep.c: Don't worry about TIME_WITH_SYS_TIME; no longer needed. * lib/strftime.c: Likewise. * lib/timespec.h: Likewise. * lib/posixtm.c: Include posixtm.h first, for interface check. Don't worry about TM_IN_SYS_TIME; that's wayyy obsolete. * lib/posixtm.h: Include stdbool.h and time.h, for proper interface. * lib/strftime.c: Don't include <sys/types.h>; shouldn't be needed. * lib/sys_time_.h: New file. * lib/timespec.h (struct timespec): Use long int, not long. * m4/gettimeofday.m4 (gl_FUNC_GETTIMEOFDAY): (gl_FUNC_GETTIMEOFDAY_CLOBBER, gl_PREREQ_GETTIMEOFDAY): Remove obsolescent call to AC_HEADER_TIME. * m4/mktime.m4 (AC_FUNC_MKTIME): Likewise. * m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Likewise. * m4/strftime.m4 (gl_FUNC_STRFTIME): Likewise. * m4/timespec.m4 (gl_TIMESPEC, gl_CHECK_TYPE_STRUCT_TIMESPEC): Likewise. * m4/tzset.m4 (gl_FUNC_TZSET_CLOBBER): Likewise. * m4/utimbuf.m4 (gl_CHECK_TYPE_STRUCT_UTIMBUF): Likewise. * m4/gettimeofday.m4 (gl_FUNC_GETTIMEOFDAY): Move sys/time.h tests into the sys_time module. Check for gettimeofday just once. Prefix our variables with gl_, not with ac_ or jm_. Tighten test for gettimeofday signature to just check the signature. Merely compile it, since linking doesn't test signature. Improve test for whether gettimeofday.o is actually needed. (gl_FUNC_GETTIMEOFDAY_CLOBBER): Renamed from AC_FUNC_GETTIMEOFDAY_CLOBBER. All uses changed. Use AC_RUN_IFELSE rather than AC_TRY_RUN. If clobbering, set and define GETTIMEOFDAY_CLOBBERS_LOCALTIME. (gl_GETTIMEOFDAY_REPLACE_LOCALTIME): Don't define GETTIMEOFDAY_CLOBBERS_LOCALTIME; that's gl_FUNC_GETTIMEOFDAY_CLOBBER's job. Don't define tzset; that's gl_FUNC_TZSET_CLOBBER's job. * m4/mktime.m4 (AC_FUNC_MKTIME): Just include <time.h> rather than worrying about sys/time.h. * m4/nanosleep.m4 (gl_FUNC_NANOSLEEP): Don't bother worrying about TIME_WITH_SYS_TIME. * m4/stat-time.m4 (gl_STAT_TIME): Likewise. * m4/posixtm.m4 (gl_POSIXTM): Remove obsolescent call to AC_STRUCT_TM. * m4/sys_time_h.m4: New file. * m4/tzset.m4 (gl_FUNC_TZSET_CLOBBER): Require gl_HEADER_SYS_TIME_H. Don't include sys/time.h. Return from main rather than exiting. Define TZSET_CLOBBERS_LOCALTIME, for consistency with other names; all uses changed. * modules/gethrxtime (Depends-on): Add sys_time. * modules/gettime (Depends-on): Likewise. * modules/gettimeofday (Depends-on): Likewise. * modules/nanosleep (Depends-on): Likewise. * modules/settime (Depends-on): Likewise. * modules/tempname (Depends-on): Likewise. * modules/utimens (Depends-on): Likewise. * modules/gettimeofday (Files): Remove lib/gettimeofday.h. (Include:) Change back to <sys/time.h>. (Maintainer:) Add self. * modules/sys_time: New file. * modules/tempname (Depends-on): Add gettimeofday. * tests/test-gettimeofday.c: Include <sys/time.h> rather than gettimeofday.h.
Diffstat (limited to 'lib/gettimeofday.c')
-rw-r--r--lib/gettimeofday.c125
1 files changed, 51 insertions, 74 deletions
diff --git a/lib/gettimeofday.c b/lib/gettimeofday.c
index 38094b039d..1aee107607 100644
--- a/lib/gettimeofday.c
+++ b/lib/gettimeofday.c
@@ -1,12 +1,7 @@
-/* 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.
+/* Provide gettimeofday for systems that don't have it or for which it's broken.
- Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 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
@@ -27,18 +22,24 @@
#include <config.h>
/* Specification. */
-#include "gettimeofday.h"
+#include <sys/time.h>
-#include <sys/types.h>
-#include <stdlib.h>
+#include <time.h>
#if HAVE_SYS_TIMEB_H
# include <sys/timeb.h>
#endif
-#if GETTIMEOFDAY_CLOBBERS_LOCALTIME
+#if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
-static struct tm *localtime_buffer_addr;
+/* 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. */
+
+static struct tm tm_zero_buffer;
+static struct tm *localtime_buffer_addr = &tm_zero_buffer;
/* This is a wrapper for localtime. It is used only on systems for which
gettimeofday clobbers the static buffer used for localtime's result.
@@ -47,13 +48,13 @@ static struct tm *localtime_buffer_addr;
localtime uses for its result. */
struct tm *
-localtime (const time_t *timep)
-#undef localtime
+localtime (time_t const *timep)
{
- extern struct tm *localtime (const time_t *);
+#undef localtime
+ extern struct tm *localtime (time_t const *);
struct tm *tm = localtime (timep);
- if (! localtime_buffer_addr)
+ if (localtime_buffer_addr == &tm_zero_buffer)
localtime_buffer_addr = tm;
return tm;
@@ -61,107 +62,83 @@ localtime (const time_t *timep)
/* Same as above, since gmtime and localtime use the same buffer. */
struct tm *
-gmtime (const time_t *timep)
-#undef gmtime
+gmtime (time_t const *timep)
{
- extern struct tm *gmtime (const time_t *);
+#undef gmtime
+ extern struct tm *gmtime (time_t const *);
struct tm *tm = gmtime (timep);
- if (! localtime_buffer_addr)
+ if (localtime_buffer_addr == &tm_zero_buffer)
localtime_buffer_addr = tm;
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. */
+#endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
+
+#if TZSET_CLOBBERS_LOCALTIME
+/* This is a wrapper for tzset, for systems on which tzset may clobber
+ the static buffer used for localtime's result. */
void
tzset (void)
-#undef tzset
{
- extern struct tm *localtime (const time_t *);
+#undef tzset
extern void tzset (void);
- struct tm save;
-
- if (! localtime_buffer_addr)
- {
- time_t t = 0;
- localtime_buffer_addr = localtime (&t);
- }
- save = *localtime_buffer_addr;
+ /* Save and restore the contents of the buffer used for localtime's
+ result around the call to tzset. */
+ struct tm save = *localtime_buffer_addr;
tzset ();
*localtime_buffer_addr = save;
}
-
#endif
-/* 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. */
+/* This is a wrapper for gettimeofday. It is used only on systems
+ that lack this function, or whose implementation of this function
+ causes problems. */
int
gettimeofday (struct timeval *restrict tv, void *restrict tz)
-#undef gettimeofday
{
+#undef gettimeofday
#if HAVE_GETTIMEOFDAY
extern int gettimeofday (/* unspecified arguments */);
-# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
- extern struct tm *localtime (const time_t *);
- /* Save and restore the contents of the buffer used for localtime's result
- around the call to gettimeofday. */
- struct tm save;
- int result;
+# 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 = *localtime_buffer_addr;
+# endif
- if (! localtime_buffer_addr)
- {
- time_t t = 0;
- localtime_buffer_addr = localtime (&t);
- }
+ int result = gettimeofday (tv, tz);
- save = *localtime_buffer_addr;
- result = gettimeofday (tv, tz);
+# if GETTIMEOFDAY_CLOBBERS_LOCALTIME
*localtime_buffer_addr = save;
+# endif
return result;
-# else
-
- return gettimeofday (tv, tz);
-
-# endif
#else
- /* The clock does not have microsecond resolution, so get the maximum
- possible value for the current time that is consistent with the
- reported clock. That way, files are not considered to be in the
- future merely because their time stamps have higher resolution
- than the clock resolution. */
-
# if HAVE__FTIME
struct _timeb timebuf;
-
_ftime (&timebuf);
tv->tv_sec = timebuf.time;
- tv->tv_usec = timebuf.millitm * 1000 + 999;
-
- return 0;
+ tv->tv_usec = timebuf.millitm * 1000;
# else
- time_t t = time (NULL);
+# if !defined OK_TO_USE_1S_CLOCK
+# error "Only 1-second nominal clock resolution found. Is that intended?" \
+ "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
+# endif
+ tv->tv_sec = time (NULL);
+ tv->tv_usec = 0;
- if (t == (time_t) -1)
- return -1;
- tv->tv_sec = t;
- tv->tv_usec = 999999;
+# endif
return 0;
-# endif
#endif
}