* sysdeps/mach/hurd/i386/init-first.c (_hurd_stack_setup): Let gcc
[glibc.git] / time / tzfile.c
blob8c2756e7bbc56dfeacfd589c1123b01faaa55dfa
1 /* Copyright (C) 1991-1993,1995-2001,2003,2004 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, write to the Free
16 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17 02111-1307 USA. */
19 #include <assert.h>
20 #include <limits.h>
21 #include <stdio.h>
22 #include <stdio_ext.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <time.h>
26 #include <unistd.h>
27 #include <sys/stat.h>
29 #define NOID
30 #include <timezone/tzfile.h>
32 int __use_tzfile;
33 static dev_t tzfile_dev;
34 static ino64_t tzfile_ino;
36 struct ttinfo
38 long int offset; /* Seconds east of GMT. */
39 unsigned char isdst; /* Used to set tm_isdst. */
40 unsigned char idx; /* Index into `zone_names'. */
41 unsigned char isstd; /* Transition times are in standard time. */
42 unsigned char isgmt; /* Transition times are in GMT. */
45 struct leap
47 time_t transition; /* Time the transition takes effect. */
48 long int change; /* Seconds of correction to apply. */
51 static struct ttinfo *find_transition (time_t timer) internal_function;
52 static void compute_tzname_max (size_t) internal_function;
54 static size_t num_transitions;
55 libc_freeres_ptr (static time_t *transitions);
56 static unsigned char *type_idxs;
57 static size_t num_types;
58 static struct ttinfo *types;
59 static char *zone_names;
60 static long int rule_stdoff;
61 static long int rule_dstoff;
62 static size_t num_leaps;
63 static struct leap *leaps;
65 #include <endian.h>
66 #include <byteswap.h>
68 /* Decode the four bytes at PTR as a signed integer in network byte order. */
69 static inline int
70 __attribute ((always_inline))
71 decode (const void *ptr)
73 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
74 return *(const int *) ptr;
75 else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
76 return bswap_32 (*(const int *) ptr);
77 else
79 const unsigned char *p = ptr;
80 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
82 result = (result << 8) | *p++;
83 result = (result << 8) | *p++;
84 result = (result << 8) | *p++;
85 result = (result << 8) | *p++;
87 return result;
91 void
92 __tzfile_read (const char *file, size_t extra, char **extrap)
94 static const char default_tzdir[] = TZDIR;
95 size_t num_isstd, num_isgmt;
96 register FILE *f;
97 struct tzhead tzhead;
98 size_t chars;
99 register size_t i;
100 size_t total_size;
101 size_t types_idx;
102 size_t leaps_idx;
103 int was_using_tzfile = __use_tzfile;
105 __use_tzfile = 0;
107 if (file == NULL)
108 /* No user specification; use the site-wide default. */
109 file = TZDEFAULT;
110 else if (*file == '\0')
111 /* User specified the empty string; use UTC with no leap seconds. */
112 goto ret_free_transitions;
113 else
115 /* We must not allow to read an arbitrary file in a setuid
116 program. So we fail for any file which is not in the
117 directory hierachy starting at TZDIR
118 and which is not the system wide default TZDEFAULT. */
119 if (__libc_enable_secure
120 && ((*file == '/'
121 && memcmp (file, TZDEFAULT, sizeof TZDEFAULT)
122 && memcmp (file, default_tzdir, sizeof (default_tzdir) - 1))
123 || strstr (file, "../") != NULL))
124 /* This test is certainly a bit too restrictive but it should
125 catch all critical cases. */
126 goto ret_free_transitions;
129 if (*file != '/')
131 const char *tzdir;
132 unsigned int len, tzdir_len;
133 char *new, *tmp;
135 tzdir = getenv ("TZDIR");
136 if (tzdir == NULL || *tzdir == '\0')
138 tzdir = default_tzdir;
139 tzdir_len = sizeof (default_tzdir) - 1;
141 else
142 tzdir_len = strlen (tzdir);
143 len = strlen (file) + 1;
144 new = (char *) __alloca (tzdir_len + 1 + len);
145 tmp = __mempcpy (new, tzdir, tzdir_len);
146 *tmp++ = '/';
147 memcpy (tmp, file, len);
148 file = new;
151 /* Note the file is opened with cancellation in the I/O functions
152 disabled. */
153 f = fopen (file, "rc");
154 if (f == NULL)
155 goto ret_free_transitions;
157 /* Get information about the file. */
158 struct stat64 st;
159 if (fstat64 (fileno (f), &st) != 0)
161 fclose (f);
162 goto ret_free_transitions;
164 if (was_using_tzfile && tzfile_ino == st.st_ino && tzfile_dev == st.st_dev)
166 /* It's the same file. No further work needed. */
167 fclose (f);
168 __use_tzfile = 1;
169 return;
172 free ((void *) transitions);
173 transitions = NULL;
175 /* Remember the inode and device number. */
176 tzfile_dev = st.st_dev;
177 tzfile_ino = st.st_ino;
179 /* No threads reading this stream. */
180 __fsetlocking (f, FSETLOCKING_BYCALLER);
182 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
183 1, f) != 1, 0))
184 goto lose;
186 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
187 num_types = (size_t) decode (tzhead.tzh_typecnt);
188 chars = (size_t) decode (tzhead.tzh_charcnt);
189 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
190 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
191 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
193 total_size = num_transitions * (sizeof (time_t) + 1);
194 total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
195 & ~(__alignof__ (struct ttinfo) - 1));
196 types_idx = total_size;
197 total_size += num_types * sizeof (struct ttinfo) + chars;
198 total_size = ((total_size + __alignof__ (struct leap) - 1)
199 & ~(__alignof__ (struct leap) - 1));
200 leaps_idx = total_size;
201 total_size += num_leaps * sizeof (struct leap);
202 /* This is for the extra memory required by the caller. */
203 total_size += extra;
205 transitions = (time_t *) malloc (total_size);
206 if (transitions == NULL)
207 goto lose;
209 type_idxs = (unsigned char *) transitions + (num_transitions
210 * sizeof (time_t));
211 types = (struct ttinfo *) ((char *) transitions + types_idx);
212 zone_names = (char *) types + num_types * sizeof (struct ttinfo);
213 leaps = (struct leap *) ((char *) transitions + leaps_idx);
214 if (extra > 0)
215 *extrap = (char *) &leaps[num_leaps];
217 if (sizeof (time_t) < 4)
218 abort ();
220 if (sizeof (time_t) == 4)
222 if (__builtin_expect (fread_unlocked (transitions, 1,
223 (4 + 1) * num_transitions, f)
224 != (4 + 1) * num_transitions, 0))
225 goto lose;
227 else
229 if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
230 != num_transitions, 0)
231 || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
232 f) != num_transitions, 0))
233 goto lose;
236 /* Check for bogus indices in the data file, so we can hereafter
237 safely use type_idxs[T] as indices into `types' and never crash. */
238 for (i = 0; i < num_transitions; ++i)
239 if (__builtin_expect (type_idxs[i] >= num_types, 0))
240 goto lose;
242 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
244 /* Decode the transition times, stored as 4-byte integers in
245 network (big-endian) byte order. We work from the end of
246 the array so as not to clobber the next element to be
247 processed when sizeof (time_t) > 4. */
248 i = num_transitions;
249 while (i-- > 0)
250 transitions[i] = decode ((char *) transitions + i * 4);
253 for (i = 0; i < num_types; ++i)
255 unsigned char x[4];
256 int c;
257 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
259 goto lose;
260 c = getc_unlocked (f);
261 if (__builtin_expect ((unsigned int) c > 1u, 0))
262 goto lose;
263 types[i].isdst = c;
264 c = getc_unlocked (f);
265 if (__builtin_expect ((size_t) c > chars, 0))
266 /* Bogus index in data file. */
267 goto lose;
268 types[i].idx = c;
269 types[i].offset = (long int) decode (x);
272 if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
273 goto lose;
275 for (i = 0; i < num_leaps; ++i)
277 unsigned char x[4];
278 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
280 goto lose;
281 leaps[i].transition = (time_t) decode (x);
282 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
284 goto lose;
285 leaps[i].change = (long int) decode (x);
288 for (i = 0; i < num_isstd; ++i)
290 int c = getc_unlocked (f);
291 if (__builtin_expect (c == EOF, 0))
292 goto lose;
293 types[i].isstd = c != 0;
295 while (i < num_types)
296 types[i++].isstd = 0;
298 for (i = 0; i < num_isgmt; ++i)
300 int c = getc_unlocked (f);
301 if (__builtin_expect (c == EOF, 0))
302 goto lose;
303 types[i].isgmt = c != 0;
305 while (i < num_types)
306 types[i++].isgmt = 0;
308 fclose (f);
310 /* First "register" all timezone names. */
311 for (i = 0; i < num_types; ++i)
312 (void) __tzstring (&zone_names[types[i].idx]);
314 /* Find the standard and daylight time offsets used by the rule file.
315 We choose the offsets in the types of each flavor that are
316 transitioned to earliest in time. */
317 __tzname[0] = NULL;
318 __tzname[1] = NULL;
319 for (i = num_transitions; i > 0; )
321 int type = type_idxs[--i];
322 int dst = types[type].isdst;
324 if (__tzname[dst] == NULL)
326 int idx = types[type].idx;
328 __tzname[dst] = __tzstring (&zone_names[idx]);
330 if (__tzname[1 - dst] != NULL)
331 break;
334 if (__tzname[0] == NULL)
336 /* This should only happen if there are no transition rules.
337 In this case there should be only one single type. */
338 assert (num_types == 1);
339 __tzname[0] = __tzstring (zone_names);
341 if (__tzname[1] == NULL)
342 __tzname[1] = __tzname[0];
344 compute_tzname_max (chars);
346 if (num_transitions == 0)
347 /* Use the first rule (which should also be the only one). */
348 rule_stdoff = rule_dstoff = types[0].offset;
349 else
351 int stdoff_set = 0, dstoff_set = 0;
352 rule_stdoff = rule_dstoff = 0;
353 i = num_transitions - 1;
356 if (!stdoff_set && !types[type_idxs[i]].isdst)
358 stdoff_set = 1;
359 rule_stdoff = types[type_idxs[i]].offset;
361 else if (!dstoff_set && types[type_idxs[i]].isdst)
363 dstoff_set = 1;
364 rule_dstoff = types[type_idxs[i]].offset;
366 if (stdoff_set && dstoff_set)
367 break;
369 while (i-- > 0);
371 if (!dstoff_set)
372 rule_dstoff = rule_stdoff;
375 __daylight = rule_stdoff != rule_dstoff;
376 __timezone = -rule_stdoff;
378 __use_tzfile = 1;
379 return;
381 lose:
382 fclose (f);
383 ret_free_transitions:
384 free ((void *) transitions);
385 transitions = NULL;
388 /* The user specified a hand-made timezone, but not its DST rules.
389 We will use the names and offsets from the user, and the rules
390 from the TZDEFRULES file. */
392 void
393 __tzfile_default (const char *std, const char *dst,
394 long int stdoff, long int dstoff)
396 size_t stdlen = strlen (std) + 1;
397 size_t dstlen = strlen (dst) + 1;
398 size_t i;
399 int isdst;
400 char *cp;
402 __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
403 if (!__use_tzfile)
404 return;
406 if (num_types < 2)
408 __use_tzfile = 0;
409 return;
412 /* Ignore the zone names read from the file and use the given ones
413 instead. */
414 __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
415 zone_names = cp;
417 /* Now there are only two zones, regardless of what the file contained. */
418 num_types = 2;
420 /* Now correct the transition times for the user-specified standard and
421 daylight offsets from GMT. */
422 isdst = 0;
423 for (i = 0; i < num_transitions; ++i)
425 struct ttinfo *trans_type = &types[type_idxs[i]];
427 /* We will use only types 0 (standard) and 1 (daylight).
428 Fix up this transition to point to whichever matches
429 the flavor of its original type. */
430 type_idxs[i] = trans_type->isdst;
432 if (trans_type->isgmt)
433 /* The transition time is in GMT. No correction to apply. */ ;
434 else if (isdst && !trans_type->isstd)
435 /* The type says this transition is in "local wall clock time", and
436 wall clock time as of the previous transition was DST. Correct
437 for the difference between the rule's DST offset and the user's
438 DST offset. */
439 transitions[i] += dstoff - rule_dstoff;
440 else
441 /* This transition is in "local wall clock time", and wall clock
442 time as of this iteration is non-DST. Correct for the
443 difference between the rule's standard offset and the user's
444 standard offset. */
445 transitions[i] += stdoff - rule_stdoff;
447 /* The DST state of "local wall clock time" for the next iteration is
448 as specified by this transition. */
449 isdst = trans_type->isdst;
452 /* Now that we adjusted the transitions to the requested offsets,
453 reset the rule_stdoff and rule_dstoff values appropriately. They
454 are used elsewhere. */
455 rule_stdoff = stdoff;
456 rule_dstoff = dstoff;
458 /* Reset types 0 and 1 to describe the user's settings. */
459 types[0].idx = 0;
460 types[0].offset = stdoff;
461 types[0].isdst = 0;
462 types[1].idx = stdlen;
463 types[1].offset = dstoff;
464 types[1].isdst = 1;
466 /* Reset the zone names to point to the user's names. */
467 __tzname[0] = (char *) std;
468 __tzname[1] = (char *) dst;
470 /* Set the timezone. */
471 __timezone = -types[0].offset;
473 compute_tzname_max (stdlen + dstlen);
476 static struct ttinfo *
477 internal_function
478 find_transition (time_t timer)
480 size_t i;
482 if (num_transitions == 0 || timer < transitions[0])
484 /* TIMER is before any transition (or there are no transitions).
485 Choose the first non-DST type
486 (or the first if they're all DST types). */
487 i = 0;
488 while (i < num_types && types[i].isdst)
489 ++i;
490 if (i == num_types)
491 i = 0;
493 else
495 /* Find the first transition after TIMER, and
496 then pick the type of the transition before it. */
497 for (i = 1; i < num_transitions; ++i)
498 if (timer < transitions[i])
499 break;
500 i = type_idxs[i - 1];
503 return &types[i];
506 void
507 __tzfile_compute (time_t timer, int use_localtime,
508 long int *leap_correct, int *leap_hit,
509 struct tm *tp)
511 register size_t i;
513 if (use_localtime)
515 struct ttinfo *info = find_transition (timer);
516 __daylight = rule_stdoff != rule_dstoff;
517 __timezone = -rule_stdoff;
518 __tzname[0] = NULL;
519 __tzname[1] = NULL;
520 for (i = num_transitions; i > 0; )
522 int type = type_idxs[--i];
523 int dst = types[type].isdst;
524 int idx = types[type].idx;
526 if (__tzname[dst] == NULL)
528 __tzname[dst] = __tzstring (&zone_names[idx]);
530 if (__tzname[1 - dst] != NULL)
531 break;
534 if (__tzname[0] == NULL)
536 /* This should only happen if there are no transition rules.
537 In this case there should be only one single type. */
538 assert (num_types == 1);
539 __tzname[0] = __tzstring (zone_names);
541 if (__tzname[1] == NULL)
542 /* There is no daylight saving time. */
543 __tzname[1] = __tzname[0];
544 tp->tm_isdst = info->isdst;
545 tp->tm_zone = __tzstring (&zone_names[info->idx]);
546 tp->tm_gmtoff = info->offset;
549 *leap_correct = 0L;
550 *leap_hit = 0;
552 /* Find the last leap second correction transition time before TIMER. */
553 i = num_leaps;
555 if (i-- == 0)
556 return;
557 while (timer < leaps[i].transition);
559 /* Apply its correction. */
560 *leap_correct = leaps[i].change;
562 if (timer == leaps[i].transition && /* Exactly at the transition time. */
563 ((i == 0 && leaps[i].change > 0) ||
564 leaps[i].change > leaps[i - 1].change))
566 *leap_hit = 1;
567 while (i > 0
568 && leaps[i].transition == leaps[i - 1].transition + 1
569 && leaps[i].change == leaps[i - 1].change + 1)
571 ++*leap_hit;
572 --i;
577 static void
578 internal_function
579 compute_tzname_max (size_t chars)
581 const char *p;
583 p = zone_names;
586 const char *start = p;
587 while (*p != '\0')
588 ++p;
589 if ((size_t) (p - start) > __tzname_cur_max)
590 __tzname_cur_max = p - start;
592 while (++p < &zone_names[chars]);