* pthread_create.c (__pthread_create_2_0): Clear new_attr.cpuset.
[glibc.git] / time / tzfile.c
blob2d1775b05b90c13b09523a6c47b34ae6e0d0aa58
1 /* Copyright (C) 1991-1993, 1995-2001, 2003 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>
28 #define NOID
29 #include <timezone/tzfile.h>
31 int __use_tzfile;
33 struct ttinfo
35 long int offset; /* Seconds east of GMT. */
36 unsigned char isdst; /* Used to set tm_isdst. */
37 unsigned char idx; /* Index into `zone_names'. */
38 unsigned char isstd; /* Transition times are in standard time. */
39 unsigned char isgmt; /* Transition times are in GMT. */
42 struct leap
44 time_t transition; /* Time the transition takes effect. */
45 long int change; /* Seconds of correction to apply. */
48 static struct ttinfo *find_transition (time_t timer) internal_function;
49 static void compute_tzname_max (size_t) internal_function;
51 static size_t num_transitions;
52 libc_freeres_ptr (static time_t *transitions);
53 static unsigned char *type_idxs;
54 static size_t num_types;
55 static struct ttinfo *types;
56 static char *zone_names;
57 static long int rule_stdoff;
58 static long int rule_dstoff;
59 static size_t num_leaps;
60 static struct leap *leaps;
62 #include <endian.h>
63 #include <byteswap.h>
65 /* Decode the four bytes at PTR as a signed integer in network byte order. */
66 static inline int
67 __attribute ((always_inline))
68 decode (const void *ptr)
70 if ((BYTE_ORDER == BIG_ENDIAN) && sizeof (int) == 4)
71 return *(const int *) ptr;
72 else if (BYTE_ORDER == LITTLE_ENDIAN && sizeof (int) == 4)
73 return bswap_32 (*(const int *) ptr);
74 else
76 const unsigned char *p = ptr;
77 int result = *p & (1 << (CHAR_BIT - 1)) ? ~0 : 0;
79 result = (result << 8) | *p++;
80 result = (result << 8) | *p++;
81 result = (result << 8) | *p++;
82 result = (result << 8) | *p++;
84 return result;
88 void
89 __tzfile_read (const char *file, size_t extra, char **extrap)
91 static const char default_tzdir[] = TZDIR;
92 size_t num_isstd, num_isgmt;
93 register FILE *f;
94 struct tzhead tzhead;
95 size_t chars;
96 register size_t i;
97 size_t total_size;
98 size_t types_idx;
99 size_t leaps_idx;
101 __use_tzfile = 0;
103 if (transitions != NULL)
104 free ((void *) transitions);
105 transitions = NULL;
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 return;
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 return;
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 f = fopen (file, "r");
152 if (f == NULL)
153 return;
155 /* No threads reading this stream. */
156 __fsetlocking (f, FSETLOCKING_BYCALLER);
158 if (__builtin_expect (fread_unlocked ((void *) &tzhead, sizeof (tzhead),
159 1, f) != 1, 0))
160 goto lose;
162 num_transitions = (size_t) decode (tzhead.tzh_timecnt);
163 num_types = (size_t) decode (tzhead.tzh_typecnt);
164 chars = (size_t) decode (tzhead.tzh_charcnt);
165 num_leaps = (size_t) decode (tzhead.tzh_leapcnt);
166 num_isstd = (size_t) decode (tzhead.tzh_ttisstdcnt);
167 num_isgmt = (size_t) decode (tzhead.tzh_ttisgmtcnt);
169 total_size = num_transitions * (sizeof (time_t) + 1);
170 total_size = ((total_size + __alignof__ (struct ttinfo) - 1)
171 & ~(__alignof__ (struct ttinfo) - 1));
172 types_idx = total_size;
173 total_size += num_types * sizeof (struct ttinfo) + chars;
174 total_size = ((total_size + __alignof__ (struct leap) - 1)
175 & ~(__alignof__ (struct leap) - 1));
176 leaps_idx = total_size;
177 total_size += num_leaps * sizeof (struct leap);
178 /* This is for the extra memory required by the caller. */
179 total_size += extra;
181 transitions = (time_t *) malloc (total_size);
182 if (transitions == NULL)
183 goto lose;
185 type_idxs = (unsigned char *) transitions + (num_transitions
186 * sizeof (time_t));
187 types = (struct ttinfo *) ((char *) transitions + types_idx);
188 zone_names = (char *) types + num_types * sizeof (struct ttinfo);
189 leaps = (struct leap *) ((char *) transitions + leaps_idx);
190 if (extra > 0)
191 *extrap = (char *) &leaps[num_leaps];
193 if (sizeof (time_t) < 4)
194 abort ();
196 if (sizeof (time_t) == 4)
198 if (__builtin_expect (fread_unlocked (transitions, 1,
199 (4 + 1) * num_transitions, f)
200 != (4 + 1) * num_transitions, 0))
201 goto lose;
203 else
205 if (__builtin_expect (fread_unlocked (transitions, 4, num_transitions, f)
206 != num_transitions, 0)
207 || __builtin_expect (fread_unlocked (type_idxs, 1, num_transitions,
208 f) != num_transitions, 0))
209 goto lose;
212 /* Check for bogus indices in the data file, so we can hereafter
213 safely use type_idxs[T] as indices into `types' and never crash. */
214 for (i = 0; i < num_transitions; ++i)
215 if (__builtin_expect (type_idxs[i] >= num_types, 0))
216 goto lose;
218 if (BYTE_ORDER != BIG_ENDIAN || sizeof (time_t) != 4)
220 /* Decode the transition times, stored as 4-byte integers in
221 network (big-endian) byte order. We work from the end of
222 the array so as not to clobber the next element to be
223 processed when sizeof (time_t) > 4. */
224 i = num_transitions;
225 while (i-- > 0)
226 transitions[i] = decode ((char *) transitions + i * 4);
229 for (i = 0; i < num_types; ++i)
231 unsigned char x[4];
232 int c;
233 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
235 goto lose;
236 c = getc_unlocked (f);
237 if (__builtin_expect ((unsigned int) c > 1u, 0))
238 goto lose;
239 types[i].isdst = c;
240 c = getc_unlocked (f);
241 if (__builtin_expect ((size_t) c > chars, 0))
242 /* Bogus index in data file. */
243 goto lose;
244 types[i].idx = c;
245 types[i].offset = (long int) decode (x);
248 if (__builtin_expect (fread_unlocked (zone_names, 1, chars, f) != chars, 0))
249 goto lose;
251 for (i = 0; i < num_leaps; ++i)
253 unsigned char x[4];
254 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
256 goto lose;
257 leaps[i].transition = (time_t) decode (x);
258 if (__builtin_expect (fread_unlocked (x, 1, sizeof (x), f) != sizeof (x),
260 goto lose;
261 leaps[i].change = (long int) decode (x);
264 for (i = 0; i < num_isstd; ++i)
266 int c = getc_unlocked (f);
267 if (__builtin_expect (c == EOF, 0))
268 goto lose;
269 types[i].isstd = c != 0;
271 while (i < num_types)
272 types[i++].isstd = 0;
274 for (i = 0; i < num_isgmt; ++i)
276 int c = getc_unlocked (f);
277 if (__builtin_expect (c == EOF, 0))
278 goto lose;
279 types[i].isgmt = c != 0;
281 while (i < num_types)
282 types[i++].isgmt = 0;
284 fclose (f);
286 /* First "register" all timezone names. */
287 for (i = 0; i < num_types; ++i)
288 (void) __tzstring (&zone_names[types[i].idx]);
290 /* Find the standard and daylight time offsets used by the rule file.
291 We choose the offsets in the types of each flavor that are
292 transitioned to earliest in time. */
293 __tzname[0] = NULL;
294 __tzname[1] = NULL;
295 for (i = num_transitions; i > 0; )
297 int type = type_idxs[--i];
298 int dst = types[type].isdst;
300 if (__tzname[dst] == NULL)
302 int idx = types[type].idx;
304 __tzname[dst] = __tzstring (&zone_names[idx]);
306 if (__tzname[1 - dst] != NULL)
307 break;
310 if (__tzname[0] == NULL)
312 /* This should only happen if there are no transition rules.
313 In this case there should be only one single type. */
314 assert (num_types == 1);
315 __tzname[0] = __tzstring (zone_names);
317 if (__tzname[1] == NULL)
318 __tzname[1] = __tzname[0];
320 compute_tzname_max (chars);
322 if (num_transitions == 0)
323 /* Use the first rule (which should also be the only one). */
324 rule_stdoff = rule_dstoff = types[0].offset;
325 else
327 int stdoff_set = 0, dstoff_set = 0;
328 rule_stdoff = rule_dstoff = 0;
329 i = num_transitions - 1;
332 if (!stdoff_set && !types[type_idxs[i]].isdst)
334 stdoff_set = 1;
335 rule_stdoff = types[type_idxs[i]].offset;
337 else if (!dstoff_set && types[type_idxs[i]].isdst)
339 dstoff_set = 1;
340 rule_dstoff = types[type_idxs[i]].offset;
342 if (stdoff_set && dstoff_set)
343 break;
345 while (i-- > 0);
347 if (!dstoff_set)
348 rule_dstoff = rule_stdoff;
351 __daylight = rule_stdoff != rule_dstoff;
352 __timezone = -rule_stdoff;
354 __use_tzfile = 1;
355 return;
357 lose:
358 fclose (f);
361 /* The user specified a hand-made timezone, but not its DST rules.
362 We will use the names and offsets from the user, and the rules
363 from the TZDEFRULES file. */
365 void
366 __tzfile_default (const char *std, const char *dst,
367 long int stdoff, long int dstoff)
369 size_t stdlen = strlen (std) + 1;
370 size_t dstlen = strlen (dst) + 1;
371 size_t i;
372 int isdst;
373 char *cp;
375 __tzfile_read (TZDEFRULES, stdlen + dstlen, &cp);
376 if (!__use_tzfile)
377 return;
379 if (num_types < 2)
381 __use_tzfile = 0;
382 return;
385 /* Ignore the zone names read from the file and use the given ones
386 instead. */
387 __mempcpy (__mempcpy (cp, std, stdlen), dst, dstlen);
388 zone_names = cp;
390 /* Now there are only two zones, regardless of what the file contained. */
391 num_types = 2;
393 /* Now correct the transition times for the user-specified standard and
394 daylight offsets from GMT. */
395 isdst = 0;
396 for (i = 0; i < num_transitions; ++i)
398 struct ttinfo *trans_type = &types[type_idxs[i]];
400 /* We will use only types 0 (standard) and 1 (daylight).
401 Fix up this transition to point to whichever matches
402 the flavor of its original type. */
403 type_idxs[i] = trans_type->isdst;
405 if (trans_type->isgmt)
406 /* The transition time is in GMT. No correction to apply. */ ;
407 else if (isdst && !trans_type->isstd)
408 /* The type says this transition is in "local wall clock time", and
409 wall clock time as of the previous transition was DST. Correct
410 for the difference between the rule's DST offset and the user's
411 DST offset. */
412 transitions[i] += dstoff - rule_dstoff;
413 else
414 /* This transition is in "local wall clock time", and wall clock
415 time as of this iteration is non-DST. Correct for the
416 difference between the rule's standard offset and the user's
417 standard offset. */
418 transitions[i] += stdoff - rule_stdoff;
420 /* The DST state of "local wall clock time" for the next iteration is
421 as specified by this transition. */
422 isdst = trans_type->isdst;
425 /* Reset types 0 and 1 to describe the user's settings. */
426 types[0].idx = 0;
427 types[0].offset = stdoff;
428 types[0].isdst = 0;
429 types[1].idx = stdlen;
430 types[1].offset = dstoff;
431 types[1].isdst = 1;
433 /* Reset the zone names to point to the user's names. */
434 __tzname[0] = (char *) std;
435 __tzname[1] = (char *) dst;
437 /* Set the timezone. */
438 __timezone = -types[0].offset;
440 compute_tzname_max (stdlen + dstlen);
443 static struct ttinfo *
444 internal_function
445 find_transition (time_t timer)
447 size_t i;
449 if (num_transitions == 0 || timer < transitions[0])
451 /* TIMER is before any transition (or there are no transitions).
452 Choose the first non-DST type
453 (or the first if they're all DST types). */
454 i = 0;
455 while (i < num_types && types[i].isdst)
456 ++i;
457 if (i == num_types)
458 i = 0;
460 else
462 /* Find the first transition after TIMER, and
463 then pick the type of the transition before it. */
464 for (i = 1; i < num_transitions; ++i)
465 if (timer < transitions[i])
466 break;
467 i = type_idxs[i - 1];
470 return &types[i];
473 void
474 __tzfile_compute (time_t timer, int use_localtime,
475 long int *leap_correct, int *leap_hit,
476 struct tm *tp)
478 register size_t i;
480 if (use_localtime)
482 struct ttinfo *info = find_transition (timer);
483 __daylight = rule_stdoff != rule_dstoff;
484 __timezone = -rule_stdoff;
485 __tzname[0] = NULL;
486 __tzname[1] = NULL;
487 for (i = num_transitions; i > 0; )
489 int type = type_idxs[--i];
490 int dst = types[type].isdst;
491 int idx = types[type].idx;
493 if (__tzname[dst] == NULL)
495 __tzname[dst] = __tzstring (&zone_names[idx]);
497 if (__tzname[1 - dst] != NULL)
498 break;
501 if (__tzname[0] == NULL)
503 /* This should only happen if there are no transition rules.
504 In this case there should be only one single type. */
505 assert (num_types == 1);
506 __tzname[0] = __tzstring (zone_names);
508 if (__tzname[1] == NULL)
509 /* There is no daylight saving time. */
510 __tzname[1] = __tzname[0];
511 tp->tm_isdst = info->isdst;
512 tp->tm_zone = __tzstring (&zone_names[info->idx]);
513 tp->tm_gmtoff = info->offset;
516 *leap_correct = 0L;
517 *leap_hit = 0;
519 /* Find the last leap second correction transition time before TIMER. */
520 i = num_leaps;
522 if (i-- == 0)
523 return;
524 while (timer < leaps[i].transition);
526 /* Apply its correction. */
527 *leap_correct = leaps[i].change;
529 if (timer == leaps[i].transition && /* Exactly at the transition time. */
530 ((i == 0 && leaps[i].change > 0) ||
531 leaps[i].change > leaps[i - 1].change))
533 *leap_hit = 1;
534 while (i > 0
535 && leaps[i].transition == leaps[i - 1].transition + 1
536 && leaps[i].change == leaps[i - 1].change + 1)
538 ++*leap_hit;
539 --i;
544 static void
545 internal_function
546 compute_tzname_max (size_t chars)
548 const char *p;
550 p = zone_names;
553 const char *start = p;
554 while (*p != '\0')
555 ++p;
556 if ((size_t) (p - start) > __tzname_cur_max)
557 __tzname_cur_max = p - start;
559 while (++p < &zone_names[chars]);