gettimeofday.c revision 1.1 1 1.1 christos /* Work around the bug in some systems whereby gettimeofday clobbers the
2 1.1 christos static buffer that localtime uses for it's return value. The gettimeofday
3 1.1 christos function from Mac OS X 10.0.4, i.e. Darwin 1.3.7 has this problem.
4 1.1 christos The tzset replacement is necessary for at least Solaris 2.5, 2.5.1, and 2.6.
5 1.1 christos Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
6 1.1 christos
7 1.1 christos This program is free software; you can redistribute it and/or modify
8 1.1 christos it under the terms of the GNU General Public License as published by
9 1.1 christos the Free Software Foundation; either version 2, or (at your option)
10 1.1 christos any later version.
11 1.1 christos
12 1.1 christos This program is distributed in the hope that it will be useful,
13 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of
14 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 1.1 christos GNU General Public License for more details.
16 1.1 christos
17 1.1 christos You should have received a copy of the GNU General Public License
18 1.1 christos along with this program; if not, write to the Free Software Foundation,
19 1.1 christos Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 1.1 christos
21 1.1 christos /* written by Jim Meyering */
22 1.1 christos
23 1.1 christos #ifdef HAVE_CONFIG_H
24 1.1 christos # include <config.h>
25 1.1 christos #endif
26 1.1 christos
27 1.1 christos /* Disable the definitions of these functions (from config.h)
28 1.1 christos so we can use the library versions here. */
29 1.1 christos #undef gettimeofday
30 1.1 christos #undef gmtime
31 1.1 christos #undef localtime
32 1.1 christos #undef tzset
33 1.1 christos
34 1.1 christos #include <sys/types.h>
35 1.1 christos
36 1.1 christos #if TIME_WITH_SYS_TIME
37 1.1 christos # include <sys/time.h>
38 1.1 christos # include <time.h>
39 1.1 christos #else
40 1.1 christos # if HAVE_SYS_TIME_H
41 1.1 christos # include <sys/time.h>
42 1.1 christos # else
43 1.1 christos # include <time.h>
44 1.1 christos # endif
45 1.1 christos #endif
46 1.1 christos
47 1.1 christos #include <stdlib.h>
48 1.1 christos
49 1.1 christos static struct tm *localtime_buffer_addr;
50 1.1 christos
51 1.1 christos /* This is a wrapper for localtime. It is used only on systems for which
52 1.1 christos gettimeofday clobbers the static buffer used for localtime's result.
53 1.1 christos
54 1.1 christos On the first call, record the address of the static buffer that
55 1.1 christos localtime uses for its result. */
56 1.1 christos
57 1.1 christos struct tm *
58 1.1 christos rpl_localtime (const time_t *timep)
59 1.1 christos {
60 1.1 christos struct tm *tm = localtime (timep);
61 1.1 christos
62 1.1 christos if (! localtime_buffer_addr)
63 1.1 christos localtime_buffer_addr = tm;
64 1.1 christos
65 1.1 christos return tm;
66 1.1 christos }
67 1.1 christos
68 1.1 christos /* Same as above, since gmtime and localtime use the same buffer. */
69 1.1 christos struct tm *
70 1.1 christos rpl_gmtime (const time_t *timep)
71 1.1 christos {
72 1.1 christos struct tm *tm = gmtime (timep);
73 1.1 christos
74 1.1 christos if (! localtime_buffer_addr)
75 1.1 christos localtime_buffer_addr = tm;
76 1.1 christos
77 1.1 christos return tm;
78 1.1 christos }
79 1.1 christos
80 1.1 christos /* This is a wrapper for gettimeofday. It is used only on systems for which
81 1.1 christos gettimeofday clobbers the static buffer used for localtime's result.
82 1.1 christos
83 1.1 christos Save and restore the contents of the buffer used for localtime's result
84 1.1 christos around the call to gettimeofday. */
85 1.1 christos
86 1.1 christos int
87 1.1 christos rpl_gettimeofday (struct timeval *tv, struct timezone *tz)
88 1.1 christos {
89 1.1 christos struct tm save;
90 1.1 christos int result;
91 1.1 christos
92 1.1 christos if (! localtime_buffer_addr)
93 1.1 christos {
94 1.1 christos time_t t = 0;
95 1.1 christos localtime_buffer_addr = localtime (&t);
96 1.1 christos }
97 1.1 christos
98 1.1 christos save = *localtime_buffer_addr;
99 1.1 christos result = gettimeofday (tv, tz);
100 1.1 christos *localtime_buffer_addr = save;
101 1.1 christos
102 1.1 christos return result;
103 1.1 christos }
104 1.1 christos
105 1.1 christos /* This is a wrapper for tzset. It is used only on systems for which
106 1.1 christos tzset may clobber the static buffer used for localtime's result.
107 1.1 christos Save and restore the contents of the buffer used for localtime's
108 1.1 christos result around the call to tzset. */
109 1.1 christos void
110 1.1 christos rpl_tzset (void)
111 1.1 christos {
112 1.1 christos struct tm save;
113 1.1 christos
114 1.1 christos if (! localtime_buffer_addr)
115 1.1 christos {
116 1.1 christos time_t t = 0;
117 1.1 christos localtime_buffer_addr = localtime (&t);
118 1.1 christos }
119 1.1 christos
120 1.1 christos save = *localtime_buffer_addr;
121 1.1 christos tzset ();
122 1.1 christos *localtime_buffer_addr = save;
123 1.1 christos }
124