]> git.cworth.org Git - tar/blob - lib/gettimeofday.c
b8ec4fa5b0e34576e0e451b1dba2c3bf3b639ca0
[tar] / lib / 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, 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 /* This is a wrapper for localtime.  It is used only on systems for which
47    gettimeofday clobbers the static buffer used for localtime's result.
48
49    On the first call, record the address of the static buffer that
50    localtime uses for its result.  */
51
52 struct tm *
53 rpl_localtime (time_t const *timep)
54 {
55 #undef localtime
56   extern struct tm *localtime (time_t const *);
57   struct tm *tm = localtime (timep);
58
59   if (localtime_buffer_addr == &tm_zero_buffer)
60     localtime_buffer_addr = tm;
61
62   return tm;
63 }
64
65 /* Same as above, since gmtime and localtime use the same buffer.  */
66 struct tm *
67 rpl_gmtime (time_t const *timep)
68 {
69 #undef gmtime
70   extern struct tm *gmtime (time_t const *);
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 /* This is a wrapper for tzset, for systems on which tzset may clobber
83    the static buffer used for localtime's result.  */
84 void
85 rpl_tzset (void)
86 {
87 #undef tzset
88   extern void 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 rpl_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, 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 }