Mercurial > hg > octave-jordi > gnulib-hg
annotate lib/time_rz.c @ 18062:a1744ab6ea8e
time_rz: fix off-by-one typo
* lib/time_rz.c (extend_abbrs): Fix off-by-one typo.
author | Paul Eggert <eggert@cs.ucla.edu> |
---|---|
date | Sat, 25 Jul 2015 15:10:16 -0700 |
parents | 20ad7d4822c8 |
children | 29e0bb74097d |
rev | line source |
---|---|
18059 | 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 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 <stdbool.h> | |
31 #include <stddef.h> | |
32 #include <stdlib.h> | |
33 #include <string.h> | |
34 | |
35 #if !HAVE_TZSET | |
36 static void tzset (void) { } | |
37 #endif | |
38 | |
39 /* A time zone rule. */ | |
40 struct tm_zone | |
41 { | |
42 /* More abbreviations, should they be needed. Their TZ_IS_SET | |
43 members are zero. */ | |
44 timezone_t next; | |
45 | |
46 /* If nonzero, the rule represents the TZ environment variable set | |
47 to the first "abbreviation" (this may be the empty string). | |
48 Otherwise, it represents an unset TZ. */ | |
49 char tz_is_set; | |
50 | |
51 /* A sequence of null-terminated strings packed next to each other. | |
52 The strings are followed by an extra null byte. If TZ_IS_SET, | |
53 there must be at least one string and the first string (which is | |
54 actually a TZ environment value value) may be empty. Otherwise | |
55 all strings must be nonempty. | |
56 | |
57 Abbreviations are stored here because otherwise the values of | |
58 tm_zone and/or tzname would be dead after changing TZ and calling | |
59 tzset. Abbreviations never move once allocated, and are live | |
60 until tzfree is called. */ | |
61 char abbrs[FLEXIBLE_ARRAY_MEMBER]; | |
62 }; | |
63 | |
64 /* The approximate size to use for small allocation requests. This is | |
65 the largest "small" request for the GNU C library malloc. */ | |
66 enum { DEFAULT_MXFAST = 64 * sizeof (size_t) / 4 }; | |
67 | |
68 /* Minimum size of the ABBRS member of struct abbr. ABBRS is larger | |
69 only in the unlikely case where an abbreviation longer than this is | |
70 used. */ | |
71 enum { ABBR_SIZE_MIN = DEFAULT_MXFAST - offsetof (struct tm_zone, abbrs) }; | |
72 | |
73 static char const TZ[] = "TZ"; | |
74 | |
75 /* Magic cookie timezone_t value, for local time. It differs from | |
76 NULL and from all other timezone_t values. Only the address | |
77 matters; the pointer is never dereferenced. */ | |
78 static timezone_t local_tz = (timezone_t) 1; | |
79 | |
80 #if HAVE_TM_ZONE || HAVE_TZNAME | |
81 | |
82 /* Return true if the values A and B differ according to the rules for | |
83 tm_isdst: A and B differ if one is zero and the other positive. */ | |
84 static bool | |
85 isdst_differ (int a, int b) | |
86 { | |
87 return !a != !b && 0 <= a && 0 <= b; | |
88 } | |
89 | |
90 /* Return true if A and B are equal. */ | |
91 static int | |
92 equal_tm (const struct tm *a, const struct tm *b) | |
93 { | |
94 return ! ((a->tm_sec ^ b->tm_sec) | |
95 | (a->tm_min ^ b->tm_min) | |
96 | (a->tm_hour ^ b->tm_hour) | |
97 | (a->tm_mday ^ b->tm_mday) | |
98 | (a->tm_mon ^ b->tm_mon) | |
99 | (a->tm_year ^ b->tm_year) | |
100 | isdst_differ (a->tm_isdst, b->tm_isdst)); | |
101 } | |
102 | |
103 #endif | |
104 | |
105 /* Copy to ABBRS the abbreviation at ABBR with size ABBR_SIZE (this | |
106 includes its trailing null byte). Append an extra null byte to | |
107 mark the end of ABBRS. */ | |
108 static void | |
109 extend_abbrs (char *abbrs, char const *abbr, size_t abbr_size) | |
110 { | |
111 memcpy (abbrs, abbr, abbr_size); | |
18062
a1744ab6ea8e
time_rz: fix off-by-one typo
Paul Eggert <eggert@cs.ucla.edu>
parents:
18059
diff
changeset
|
112 abbrs[abbr_size] = '\0'; |
18059 | 113 } |
114 | |
115 /* Return a newly allocated time zone for NAME, or NULL on failure. | |
116 As a special case, return a nonzero constant for wall clock time, a | |
117 constant that survives freeing. */ | |
118 timezone_t | |
119 tzalloc (char const *name) | |
120 { | |
121 size_t name_size = name ? strlen (name) + 1 : 0; | |
122 size_t abbr_size = name_size < ABBR_SIZE_MIN ? ABBR_SIZE_MIN : name_size + 1; | |
123 timezone_t tz = malloc (offsetof (struct tm_zone, abbrs) + abbr_size); | |
124 if (tz) | |
125 { | |
126 tz->next = NULL; | |
127 tz->tz_is_set = !!name; | |
128 extend_abbrs (tz->abbrs, name, name_size); | |
129 } | |
130 return tz; | |
131 } | |
132 | |
133 /* Save into TZ any nontrivial time zone abbreviation used by TM, | |
134 and update *TM or tzname if they point to the abbreviation. | |
135 Return true if successful, false (setting errno) otherwise. */ | |
136 static bool | |
137 save_abbr (timezone_t tz, struct tm *tm) | |
138 { | |
139 #if HAVE_TM_ZONE || HAVE_TZNAME | |
140 char const *zone = NULL; | |
141 char **tzname_zone = NULL; | |
142 char *zone_copy = (char *) ""; | |
143 # if HAVE_TM_ZONE | |
144 zone = tm->tm_zone; | |
145 # endif | |
146 # if HAVE_TZNAME | |
147 if (! (zone && *zone) && 0 <= tm->tm_isdst) | |
148 zone = *(tzname_zone = &tzname[0 < tm->tm_isdst]); | |
149 # endif | |
150 | |
151 /* No need to replace null zones, or zones within the struct tm. */ | |
152 if (!zone || ((char *) tm <= zone && zone < (char *) (tm + 1))) | |
153 return true; | |
154 | |
155 if (*zone) | |
156 { | |
157 zone_copy = tz->abbrs; | |
158 | |
159 while (strcmp (zone_copy, zone) != 0) | |
160 { | |
161 if (! (*zone_copy || (zone_copy == tz->abbrs && tz->tz_is_set))) | |
162 { | |
163 size_t zone_size = strlen (zone) + 1; | |
164 if (zone_size < tz->abbrs + ABBR_SIZE_MIN - zone_copy) | |
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 if (!tzname_zone) | |
189 tm->tm_zone = zone_copy; | |
190 # endif | |
191 # if HAVE_TZNAME | |
192 if (tzname_zone) | |
193 *tzname_zone = zone_copy; | |
194 # endif | |
195 #endif | |
196 return true; | |
197 } | |
198 | |
199 /* Free a time zone. */ | |
200 void | |
201 tzfree (timezone_t tz) | |
202 { | |
203 if (tz != local_tz) | |
204 while (tz) | |
205 { | |
206 timezone_t next = tz->next; | |
207 free (tz); | |
208 tz = next; | |
209 } | |
210 } | |
211 | |
212 /* Get and set the TZ environment variable. These functions can be | |
213 overridden by programs like Emacs that manage their own environment. */ | |
214 | |
215 #ifndef getenv_TZ | |
216 static char * | |
217 getenv_TZ (void) | |
218 { | |
219 return getenv (TZ); | |
220 } | |
221 #endif | |
222 | |
223 #ifndef setenv_TZ | |
224 static int | |
225 setenv_TZ (char const *tz) | |
226 { | |
227 return tz ? setenv (TZ, tz, 1) : unsetenv (TZ); | |
228 } | |
229 #endif | |
230 | |
231 /* Change the environment to match the specified timezone_t value. | |
232 Return true if successful, false (setting errno) otherwise. */ | |
233 static bool | |
234 change_env (timezone_t tz) | |
235 { | |
236 if (setenv_TZ (tz->tz_is_set ? tz->abbrs : NULL) != 0) | |
237 return false; | |
238 tzset (); | |
239 return true; | |
240 } | |
241 | |
242 /* Temporarily set the time zone to TZ, which must not be null. | |
243 Return LOCAL_TZ if the time zone setting is already correct. | |
244 Otherwise return a newly allocated time zone representing the old | |
245 setting, or NULL (setting errno) on failure. */ | |
246 static timezone_t | |
247 set_tz (timezone_t tz) | |
248 { | |
249 char *env_tz = getenv_TZ (); | |
250 if (env_tz | |
251 ? tz->tz_is_set && strcmp (tz->abbrs, env_tz) == 0 | |
252 : !tz->tz_is_set) | |
253 return local_tz; | |
254 else | |
255 { | |
256 timezone_t old_tz = tzalloc (env_tz); | |
257 if (!old_tz) | |
258 return old_tz; | |
259 if (! change_env (tz)) | |
260 { | |
261 int saved_errno = errno; | |
262 tzfree (old_tz); | |
263 errno = saved_errno; | |
264 return NULL; | |
265 } | |
266 return old_tz; | |
267 } | |
268 } | |
269 | |
270 /* Restore an old setting returned by set_tz. It must not be null. | |
271 Return true (preserving errno) if successful, false (setting errno) | |
272 otherwise. */ | |
273 static bool | |
274 revert_tz (timezone_t tz) | |
275 { | |
276 if (tz == local_tz) | |
277 return true; | |
278 else | |
279 { | |
280 int saved_errno = errno; | |
281 bool ok = change_env (tz); | |
282 if (!ok) | |
283 saved_errno = errno; | |
284 tzfree (tz); | |
285 errno = saved_errno; | |
286 return ok; | |
287 } | |
288 } | |
289 | |
290 /* Use time zone TZ to compute localtime_r (T, TM). */ | |
291 struct tm * | |
292 localtime_rz (timezone_t tz, time_t const *t, struct tm *tm) | |
293 { | |
294 if (!tz) | |
295 return gmtime_r (t, tm); | |
296 else | |
297 { | |
298 timezone_t old_tz = set_tz (tz); | |
299 if (old_tz) | |
300 { | |
301 tm = localtime_r (t, tm); | |
302 if (tm && !save_abbr (tz, tm)) | |
303 tm = NULL; | |
304 if (revert_tz (old_tz)) | |
305 return tm; | |
306 } | |
307 return NULL; | |
308 } | |
309 } | |
310 | |
311 /* Use time zone TZ to compute mktime (TM). */ | |
312 time_t | |
313 mktime_z (timezone_t tz, struct tm *tm) | |
314 { | |
315 if (!tz) | |
316 return timegm (tm); | |
317 else | |
318 { | |
319 timezone_t old_tz = set_tz (tz); | |
320 if (old_tz) | |
321 { | |
322 time_t t = mktime (tm); | |
323 #if HAVE_TM_ZONE || HAVE_TZNAME | |
324 time_t badtime = -1; | |
325 struct tm tm_1; | |
326 if ((t != badtime | |
327 || (localtime_r (&t, &tm_1) && equal_tm (tm, &tm_1))) | |
328 && !save_abbr (tz, tm)) | |
329 t = badtime; | |
330 #endif | |
331 if (revert_tz (old_tz)) | |
332 return t; | |
333 } | |
334 return -1; | |
335 } | |
336 } |