Import upstream version 1.29
[debian/tar] / gnu / time_rz.c
1 /* Time zone functions such as tzalloc and localtime_rz
2
3    Copyright 2015 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 along
16    with this program; if not, see <http://www.gnu.org/licenses/>.  */
17
18 /* Written by Paul Eggert.  */
19
20 /* Although this module is not thread-safe, any races should be fairly
21    rare and reasonably benign.  For complete thread-safety, use a C
22    library with a working timezone_t type, so that this module is not
23    needed.  */
24
25 #include <config.h>
26
27 #include <time.h>
28
29 #include <errno.h>
30 #include <stdbool.h>
31 #include <stddef.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "time-internal.h"
36
37 #if !HAVE_TZSET
38 static void tzset (void) { }
39 #endif
40
41 /* The approximate size to use for small allocation requests.  This is
42    the largest "small" request for the GNU C library malloc.  */
43 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 };
44
45 /* Minimum size of the ABBRS member of struct abbr.  ABBRS is larger
46    only in the unlikely case where an abbreviation longer than this is
47    used.  */
48 enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) };
49
50 static char const TZ[] = "TZ";
51
52 /* Magic cookie timezone_t value, for local time.  It differs from
53    NULL and from all other timezone_t values.  Only the address
54    matters; the pointer is never dereferenced.  */
55 static timezone_t const local_tz = (timezone_t) 1;
56
57 #if HAVE_TM_ZONE || HAVE_TZNAME
58
59 /* Return true if the values A and B differ according to the rules for
60    tm_isdst: A and B differ if one is zero and the other positive.  */
61 static bool
62 isdst_differ (int a, int b)
63 {
64   return !a != !b && 0 <= a && 0 <= b;
65 }
66
67 /* Return true if A and B are equal.  */
68 static int
69 equal_tm (const struct tm *a, const struct tm *b)
70 {
71   return ! ((a->tm_sec ^ b->tm_sec)
72             | (a->tm_min ^ b->tm_min)
73             | (a->tm_hour ^ b->tm_hour)
74             | (a->tm_mday ^ b->tm_mday)
75             | (a->tm_mon ^ b->tm_mon)
76             | (a->tm_year ^ b->tm_year)
77             | isdst_differ (a->tm_isdst, b->tm_isdst));
78 }
79
80 #endif
81
82 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this
83    includes its trailing null byte).  Append an extra null byte to
84    mark the end of ABBRS.  */
85 static void
86 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size)
87 {
88   memcpy (abbrs, abbr, abbr_size);
89   abbrs[abbr_size] = '\0';
90 }
91
92 /* Return a newly allocated time zone for NAME, or NULL on failure.
93    As a special case, return a nonzero constant for wall clock time, a
94    constant that survives freeing.  */
95 timezone_t
96 tzalloc (char const *name)
97 {
98   size_t name_size = name ? strlen (name) + 1 : 0;
99   size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1;
100   timezone_t tz = malloc (offsetof (struct tm_zone, abbrs) + abbr_size);
101   if (tz)
102     {
103       tz->next = NULL;
104 #if HAVE_TZNAME && !HAVE_TM_ZONE
105       tz->tzname_copy[0] = tz->tzname_copy[1] = NULL;
106 #endif
107       tz->tz_is_set = !!name;
108       tz->abbrs[0] = '\0';
109       if (name)
110         extend_abbrs (tz->abbrs, name, name_size);
111     }
112   return tz;
113 }
114
115 /* Save into TZ any nontrivial time zone abbreviation used by TM, and
116    update *TM (if HAVE_TM_ZONE) or *TZ (if !HAVE_TM_ZONE &&
117    HAVE_TZNAME) if they use the abbreviation.  Return true if
118    successful, false (setting errno) otherwise.  */
119 static bool
120 save_abbr (timezone_t tz, struct tm *tm)
121 {
122 #if HAVE_TM_ZONE || HAVE_TZNAME
123   char const *zone = NULL;
124   char *zone_copy = (char *) "";
125
126 # if HAVE_TZNAME
127   int tzname_index = -1;
128 # endif
129
130 # if HAVE_TM_ZONE
131   zone = tm->tm_zone;
132 # endif
133
134 # if HAVE_TZNAME
135   if (! (zone && *zone) && 0 <= tm->tm_isdst)
136     {
137       tzname_index = tm->tm_isdst != 0;
138       zone = tzname[tzname_index];
139     }
140 # endif
141
142   /* No need to replace null zones, or zones within the struct tm.  */
143   if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1)))
144     return true;
145
146   if (*zone)
147     {
148       zone_copy = tz->abbrs;
149
150       while (strcmp (zone_copy, zone) != 0)
151         {
152           if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set)))
153             {
154               size_t zone_size = strlen (zone) + 1;
155               if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy)
156                 extend_abbrs (zone_copy, zone, zone_size);
157               else
158                 {
159                   tz = tz->next = tzalloc (zone);
160                   if (!tz)
161                     return false;
162                   tz->tz_is_set = 0;
163                   zone_copy = tz->abbrs;
164                 }
165               break;
166             }
167
168           zone_copy += strlen (zone_copy) + 1;
169           if (!*zone_copy && tz->next)
170             {
171               tz = tz->next;
172               zone_copy = tz->abbrs;
173             }
174         }
175     }
176
177   /* Replace the zone name so that its lifetime matches that of TZ.  */
178 # if HAVE_TM_ZONE
179   tm->tm_zone = zone_copy;
180 # else
181   if (0 <= tzname_index)
182     tz->tzname_copy[tzname_index] = zone_copy;
183 # endif
184 #endif
185
186   return true;
187 }
188
189 /* Free a time zone.  */
190 void
191 tzfree (timezone_t tz)
192 {
193   if (tz != local_tz)
194     while (tz)
195       {
196         timezone_t next = tz->next;
197         free (tz);
198         tz = next;
199       }
200 }
201
202 /* Get and set the TZ environment variable.  These functions can be
203    overridden by programs like Emacs that manage their own environment.  */
204
205 #ifndef getenv_TZ
206 static char *
207 getenv_TZ (void)
208 {
209   return getenv (TZ);
210 }
211 #endif
212
213 #ifndef setenv_TZ
214 static int
215 setenv_TZ (char const *tz)
216 {
217   return tz ? setenv (TZ, tz, 1) : unsetenv (TZ);
218 }
219 #endif
220
221 /* Change the environment to match the specified timezone_t value.
222    Return true if successful, false (setting errno) otherwise.  */
223 static bool
224 change_env (timezone_t tz)
225 {
226   if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0)
227     return false;
228   tzset ();
229   return true;
230 }
231
232 /* Temporarily set the time zone to TZ, which must not be null.
233    Return LOCAL_TZ if the time zone setting is already correct.
234    Otherwise return a newly allocated time zone representing the old
235    setting, or NULL (setting errno) on failure.  */
236 static timezone_t
237 set_tz (timezone_t tz)
238 {
239   char *env_tz = getenv_TZ ();
240   if (env_tz
241       ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0
242       : !tz->tz_is_set)
243     return local_tz;
244   else
245     {
246       timezone_t old_tz = tzalloc (env_tz);
247       if (!old_tz)
248         return old_tz;
249       if (! change_env (tz))
250         {
251           int saved_errno = errno;
252           tzfree (old_tz);
253           errno = saved_errno;
254           return NULL;
255         }
256       return old_tz;
257     }
258 }
259
260 /* Restore an old setting returned by set_tz.  It must not be null.
261    Return true (preserving errno) if successful, false (setting errno)
262    otherwise.  */
263 static bool
264 revert_tz (timezone_t tz)
265 {
266   if (tz == local_tz)
267     return true;
268   else
269     {
270       int saved_errno = errno;
271       bool ok = change_env (tz);
272       if (!ok)
273         saved_errno = errno;
274       tzfree (tz);
275       errno = saved_errno;
276       return ok;
277     }
278 }
279
280 /* Use time zone TZ to compute localtime_r (T, TM).  */
281 struct tm *
282 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm)
283 {
284   if (!tz)
285     return gmtime_r (t, tm);
286   else
287     {
288       timezone_t old_tz = set_tz (tz);
289       if (old_tz)
290         {
291           tm = localtime_r (t, tm);
292           if (tm && !save_abbr (tz, tm))
293             tm = NULL;
294           if (revert_tz (old_tz))
295             return tm;
296         }
297       return NULL;
298     }
299 }
300
301 /* Use time zone TZ to compute mktime (TM).  */
302 time_t
303 mktime_z (timezone_t tz, struct tm *tm)
304 {
305   if (!tz)
306     return timegm (tm);
307   else
308     {
309       timezone_t old_tz = set_tz (tz);
310       if (old_tz)
311         {
312           time_t t = mktime (tm);
313 #if HAVE_TM_ZONE || HAVE_TZNAME
314           time_t badtime = -1;
315           struct tm tm_1;
316           if ((t != badtime
317                || (localtime_r (&t, &tm_1) && equal_tm (tm, &tm_1)))
318               && !save_abbr (tz, tm))
319             t = badtime;
320 #endif
321           if (revert_tz (old_tz))
322             return t;
323         }
324       return -1;
325     }
326 }