]> git.cworth.org Git - tar/blob - gnu/gettimeofday.c
upstream: Fix extraction of device nodes.
[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-2010 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, write to the Free Software Foundation,
19    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* written by Jim Meyering */
22
23 #include <config.h>
24
25 /* Specification.  */
26 #include <sys/time.h>
27
28 #include <time.h>
29
30 #if HAVE_SYS_TIMEB_H
31 # include <sys/timeb.h>
32 #endif
33
34 #if GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME
35
36 /* Work around the bug in some systems whereby gettimeofday clobbers
37    the static buffer that localtime uses for its return value.  The
38    gettimeofday function from Mac OS X 10.0.4 (i.e., Darwin 1.3.7) has
39    this problem.  The tzset replacement is necessary for at least
40    Solaris 2.5, 2.5.1, and 2.6.  */
41
42 static struct tm tm_zero_buffer;
43 static struct tm *localtime_buffer_addr = &tm_zero_buffer;
44
45 #undef localtime
46 extern struct tm *localtime (time_t const *);
47
48 #undef gmtime
49 extern struct tm *gmtime (time_t const *);
50
51 /* This is a wrapper for localtime.  It is used only on systems for which
52    gettimeofday clobbers the static buffer used for localtime's result.
53
54    On the first call, record the address of the static buffer that
55    localtime uses for its result.  */
56
57 struct tm *
58 rpl_localtime (time_t const *timep)
59 {
60   struct tm *tm = localtime (timep);
61
62   if (localtime_buffer_addr == &tm_zero_buffer)
63     localtime_buffer_addr = tm;
64
65   return tm;
66 }
67
68 /* Same as above, since gmtime and localtime use the same buffer.  */
69 struct tm *
70 rpl_gmtime (time_t const *timep)
71 {
72   struct tm *tm = gmtime (timep);
73
74   if (localtime_buffer_addr == &tm_zero_buffer)
75     localtime_buffer_addr = tm;
76
77   return tm;
78 }
79
80 #endif /* GETTIMEOFDAY_CLOBBERS_LOCALTIME || TZSET_CLOBBERS_LOCALTIME */
81
82 #if TZSET_CLOBBERS_LOCALTIME
83
84 #undef tzset
85 extern void tzset (void);
86
87 /* This is a wrapper for tzset, for systems on which tzset may clobber
88    the static buffer used for localtime's result.  */
89 void
90 rpl_tzset (void)
91 {
92   /* Save and restore the contents of the buffer used for localtime's
93      result around the call to tzset.  */
94   struct tm save = *localtime_buffer_addr;
95   tzset ();
96   *localtime_buffer_addr = save;
97 }
98 #endif
99
100 /* This is a wrapper for gettimeofday.  It is used only on systems
101    that lack this function, or whose implementation of this function
102    causes problems.  */
103
104 int
105 gettimeofday (struct timeval *restrict tv, void *restrict tz)
106 {
107 #undef gettimeofday
108 #if HAVE_GETTIMEOFDAY
109 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
110   /* Save and restore the contents of the buffer used for localtime's
111      result around the call to gettimeofday.  */
112   struct tm save = *localtime_buffer_addr;
113 # endif
114
115   int result = gettimeofday (tv, (struct timezone *) tz);
116
117 # if GETTIMEOFDAY_CLOBBERS_LOCALTIME
118   *localtime_buffer_addr = save;
119 # endif
120
121   return result;
122
123 #else
124
125 # if HAVE__FTIME
126
127   struct _timeb timebuf;
128   _ftime (&timebuf);
129   tv->tv_sec = timebuf.time;
130   tv->tv_usec = timebuf.millitm * 1000;
131
132 # else
133
134 #  if !defined OK_TO_USE_1S_CLOCK
135 #   error "Only 1-second nominal clock resolution found.  Is that intended?" \
136           "If so, compile with the -DOK_TO_USE_1S_CLOCK option."
137 #  endif
138   tv->tv_sec = time (NULL);
139   tv->tv_usec = 0;
140
141 # endif
142
143   return 0;
144
145 #endif
146 }