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