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