1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Provide gettimeofday for systems that don't have it or for which it's broken.
4
5 Copyright (C) 2001, 2002, 2003, 2005, 2006, 2007 Free Software
6 Foundation, Inc.
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software Foundation,
20 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21
22 /* written by Jim Meyering */
23
24 #include <config.h>
25
26 /* Specification. */
27 #include <sys/time.h>
28
29 #include <time.h>
30
31 #if HAVE_SYS_TIMEB_H
32 # include <sys/timeb.h>
33 #endif
34
35 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
36
37 /* Work around the bug in some systems whereby gettimeofday clobbers
38 the static buffer that localtime uses for its return value. The
39 gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
40 this problem. The tzset replacement is necessary for at least
41 Solaris 2.5, 2.5.1, and 2.6. */
42
43 static struct tm tm_zero_buffer;
44 static struct tm *localtime_buffer_addr = &tm_zero_buffer;
45
46 #undef localtime
47 extern struct tm *localtime (time_t const *);
48
49 #undef gmtime
50 extern struct tm *gmtime (time_t const *);
51
52 /* This is a wrapper for localtime. It is used only on systems for which
53 gettimeofday clobbers the static buffer used for localtime's result.
54
55 On the first call, record the address of the static buffer that
56 localtime uses for its result. */
57
58 struct tm *
rpl_localtime(time_t const * timep)59 rpl_localtime (time_t const *timep)
60 {
61 struct tm *tm = localtime (timep);
62
63 if (localtime_buffer_addr == &tm_zero_buffer)
64 localtime_buffer_addr = tm;
65
66 return tm;
67 }
68
69 /* Same as above, since gmtime and localtime use the same buffer. */
70 struct tm *
rpl_gmtime(time_t const * timep)71 rpl_gmtime (time_t const *timep)
72 {
73 struct tm *tm = gmtime (timep);
74
75 if (localtime_buffer_addr == &tm_zero_buffer)
76 localtime_buffer_addr = tm;
77
78 return tm;
79 }
80
81 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
82
83 #if TZSET_CLOBBERS_LOCALTIME
84
85 #undef tzset
86 extern void tzset (void);
87
88 /* This is a wrapper for tzset, for systems on which tzset may clobber
89 the static buffer used for localtime's result. */
90 void
rpl_tzset(void)91 rpl_tzset (void)
92 {
93 /* Save and restore the contents of the buffer used for localtime's
94 result around the call to tzset. */
95 struct tm save = *localtime_buffer_addr;
96 tzset ();
97 *localtime_buffer_addr = save;
98 }
99 #endif
100
101 /* This is a wrapper for gettimeofday. It is used only on systems
102 that lack this function, or whose implementation of this function
103 causes problems. */
104
105 int
rpl_gettimeofday(struct timeval * restrict tv,void * restrict tz)106 rpl_gettimeofday (struct timeval *restrict tv, void *restrict tz)
107 {
108 #undef gettimeofday
109 #if HAVE_GETTIMEOFDAY
110 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
111 /* Save and restore the contents of the buffer used for localtime's
112 result around the call to gettimeofday. */
113 struct tm save = *localtime_buffer_addr;
114 # endif
115
116 int result = gettimeofday (tv, tz);
117
118 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
119 *localtime_buffer_addr = save;
120 # endif
121
122 return result;
123
124 #else
125
126 # if HAVE__FTIME
127
128 struct _timeb timebuf;
129 _ftime (&timebuf);
130 tv->tv_sec = timebuf.time;
131 tv->tv_usec = timebuf.millitm * 1000;
132
133 # else
134
135 # if !defined OK_TO_USE_1S_CLOCK
136 # error "Only 1-second nominal clock resolution found. Is that intended?" \
137 "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
138 # endif
139 tv->tv_sec = time (NULL);
140 tv->tv_usec = 0;
141
142 # endif
143
144 return 0;
145
146 #endif
147 }
148