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