1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License, Version 1.0 only 6 * (the "License"). You may not use this file except in compliance 7 * with the License. 8 * 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10 * or http://www.opensolaris.org/os/licensing. 11 * See the License for the specific language governing permissions 12 * and limitations under the License. 13 * 14 * When distributing Covered Code, include this CDDL HEADER in each 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16 * If applicable, add the following below this CDDL HEADER, with the 17 * fields enclosed by brackets "[]" replaced with your own identifying 18 * information: Portions Copyright [yyyy] [name of copyright owner] 19 * 20 * CDDL HEADER END 21 */ 22 /* 23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 /* Copyright (c) 1988 AT&T */ 30 /* All Rights Reserved */ 31 32 33 /* 34 * A part of this file comes from public domain source, so 35 * clarified as of June 5, 1996 by Arthur David Olson 36 * (arthur_david_olson@nih.gov). 37 */ 38 39 /* 40 * localtime.c 41 * 42 * This file contains routines to convert struct tm to time_t and 43 * back as well as adjust time values based on their timezone, which 44 * is a local offset from GMT (Greenwich Mean Time). 45 * 46 * Many timezones actually consist of more than one offset from GMT. 47 * The GMT offset that is considered the normal offset is referred 48 * to as standard time. The other offset is referred to as alternate 49 * time, but is better known as daylight savings time or summer time. 50 * 51 * The current timezone for an application is derived from the TZ 52 * environment variable either as defined in the environment or in 53 * /etc/default/init. As defined by IEEE 1003.1-1990 (POSIX), the 54 * TZ variable can either be: 55 * :<characters> 56 * or 57 * <std><offset1>[<dst>[<offset2>]][,<start>[/<time>],<end>[/<time>] 58 * 59 * <characters> is an implementation-defined string that somehow describes 60 * a timezone. The implementation-defined description of a timezone used 61 * in Solaris is based on the public domain zoneinfo code available from 62 * elsie.nci.nih.gov and a timezone that is specified in this way is 63 * referred to as a zoneinfo timezone. An example of this is ":US/Pacific". 64 * 65 * The precise definition of the second format can be found in POSIX, 66 * but, basically, <std> is the abbreviation for the timezone in standard 67 * (not daylight savings time), <offset1> is the standard offset from GMT, 68 * <dst> is the abbreviation for the timezone in daylight savings time and 69 * <offset2> is the daylight savings time offset from GMT. The remainder 70 * specifies when daylight savings time begins and ends. A timezone 71 * specified in this way is referred to as a POSIX timezone. An example 72 * of this is "PST7PDT". 73 * 74 * In Solaris, there is an extension to this. If the timezone is not 75 * preceded by a ":" and it does not parse as a POSIX timezone, then it 76 * will be treated as a zoneinfo timezone. Much usage of zoneinfo 77 * timezones in Solaris is done without the leading ":". 78 * 79 * A zoneinfo timezone is a reference to a file that contains a set of 80 * rules that describe the timezone. In Solaris, the file is in 81 * /usr/share/lib/zoneinfo. The file is generated by zic(1M), based 82 * on zoneinfo rules "source" files. This is all described on the zic(1M) 83 * man page. 84 */ 85 86 /* 87 * Functions that are common to ctime(3C) and cftime(3C) 88 */ 89 90 #pragma weak tzset = _tzset 91 #pragma weak localtime_r = _localtime_r 92 #pragma weak gmtime_r = _gmtime_r 93 94 #include "synonyms.h" 95 #include "libc.h" 96 #include "tsd.h" 97 #include <stdarg.h> 98 #include <mtlib.h> 99 #include <sys/types.h> 100 #include <ctype.h> 101 #include <stdio.h> 102 #include <limits.h> 103 #include <sys/param.h> 104 #include <time.h> 105 #include <unistd.h> 106 #include <stdlib.h> 107 #include <string.h> 108 #include <tzfile.h> 109 #include <thread.h> 110 #include <synch.h> 111 #include <fcntl.h> 112 #include <errno.h> 113 #include <sys/stat.h> 114 115 #define JAN_01_1902 (long long)0x8017E880 116 #define LEN_TZDIR (sizeof (TZDIR) - 1) 117 #define TIMEZONE "/etc/default/init" 118 #define TZSTRING "TZ=" 119 #define HASHTABLE 109 120 121 #define LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400) 122 123 /* Days since 1/1/70 to 12/31/(1900 + Y - 1) */ 124 #define DAYS_SINCE_70(Y) (YR((Y)-1L) - YR(70-1)) 125 #define YR(X) /* Calc # days since 0 A.D. X = curr. yr - 1900 */ \ 126 ((1900L + (X)) * 365L + (1900L + (X)) / 4L - \ 127 (1900L + (X)) / 100L + ((1900L + (X)) - 1600L) / 400L) 128 129 130 /* 131 * The following macros are replacements for detzcode(), which has 132 * been in the public domain versions of the localtime.c code for 133 * a long time. The primatives supporting the CVTZCODE macro are 134 * implemented differently for different endianness (ie. little 135 * vs. big endian) out of necessity, to account for the different 136 * byte ordering of the quantities being fetched. Both versions 137 * are substantially faster than the detzcode() macro. The big 138 * endian version is approx. 6.8x faster than detzcode(), the 139 * little endian version is approximately 3x faster, due to the 140 * extra shifting requiring to change byte order. The micro 141 * benchmarks used to compare were based on the SUNWSpro SC6.1 142 * (and later) compilers. 143 */ 144 145 #if defined(__sparc) || defined(__sparcv9) /* big endian */ 146 147 #define GET_LONG(p) \ 148 *(uint_t *)(p) 149 150 #define GET_SHORTS(p) \ 151 *(ushort_t *)(p) << 16 |\ 152 *(ushort_t *)((p) + 2) 153 154 #define GET_CHARS(p) \ 155 *(uchar_t *)(p) << 24 |\ 156 *(uchar_t *)((p) + 1) << 16 |\ 157 *(uchar_t *)((p) + 2) << 8 |\ 158 *(uchar_t *)((p) + 3) 159 160 #else /* little endian */ 161 162 #define GET_BYTE(x) \ 163 ((x) & 0xff) 164 165 #define SWAP_BYTES(x) ((\ 166 GET_BYTE(x) << 8) |\ 167 GET_BYTE((x) >> 8)) 168 169 #define SWAP_WORDS(x) ((\ 170 SWAP_BYTES(x) << 16) |\ 171 SWAP_BYTES((x) >> 16)) 172 173 #define GET_LONG(p) \ 174 SWAP_WORDS(*(uint_t *)(p)) 175 176 #define GET_SHORTS(p) \ 177 SWAP_BYTES(*(ushort_t *)(p)) << 16 |\ 178 SWAP_BYTES(*(ushort_t *)((p) + 2)) 179 180 #define GET_CHARS(p) \ 181 GET_BYTE(*(uchar_t *)(p)) << 24 |\ 182 GET_BYTE(*(uchar_t *)((p) + 1)) << 16 |\ 183 GET_BYTE(*(uchar_t *)((p) + 2)) << 8 |\ 184 GET_BYTE(*(uchar_t *)((p) + 3)) 185 186 #endif 187 188 189 #define IF_ALIGNED(ptr, byte_alignment) \ 190 !((uintptr_t)(ptr) & (byte_alignment - 1)) 191 192 #define CVTZCODE(p) (int)(\ 193 IF_ALIGNED(p, 4) ? GET_LONG(p) :\ 194 IF_ALIGNED(p, 2) ? GET_SHORTS(p) : GET_CHARS(p));\ 195 p += 4; 196 197 #ifndef FALSE 198 #define FALSE (0) 199 #endif 200 201 #ifndef TRUE 202 #define TRUE (1) 203 #endif 204 205 extern mutex_t _time_lock; 206 207 extern const int __lyday_to_month[]; 208 extern const int __yday_to_month[]; 209 extern const int __mon_lengths[2][MONS_PER_YEAR]; 210 extern const int __year_lengths[2]; 211 212 const char _tz_gmt[4] = "GMT"; /* "GMT" */ 213 const char _tz_spaces[4] = " "; /* " " */ 214 static const char _posix_gmt0[5] = "GMT0"; /* "GMT0" */ 215 216 typedef struct ttinfo { /* Time type information */ 217 long tt_gmtoff; /* GMT offset in seconds */ 218 int tt_isdst; /* used to set tm_isdst */ 219 int tt_abbrind; /* abbreviation list index */ 220 int tt_ttisstd; /* TRUE if trans is std time */ 221 int tt_ttisgmt; /* TRUE if transition is GMT */ 222 } ttinfo_t; 223 224 typedef struct lsinfo { /* Leap second information */ 225 time_t ls_trans; /* transition time */ 226 long ls_corr; /* correction to apply */ 227 } lsinfo_t; 228 229 typedef struct previnfo { /* Info about *prev* trans */ 230 ttinfo_t *std; /* Most recent std type */ 231 ttinfo_t *alt; /* Most recent alt type */ 232 } prev_t; 233 234 typedef enum { 235 MON_WEEK_DOW, /* Mm.n.d - month, week, day of week */ 236 JULIAN_DAY, /* Jn - Julian day */ 237 DAY_OF_YEAR /* n - day of year */ 238 } posrule_type_t; 239 240 typedef struct { 241 posrule_type_t r_type; /* type of rule */ 242 int r_day; /* day number of rule */ 243 int r_week; /* week number of rule */ 244 int r_mon; /* month number of rule */ 245 long r_time; /* transition time of rule */ 246 } rule_t; 247 248 typedef struct { 249 rule_t *rules[2]; 250 long offset[2]; 251 long long rtime[2]; 252 } posix_daylight_t; 253 254 /* 255 * Note: ZONERULES_INVALID used for global curr_zonerules variable, but not 256 * for zonerules field of state_t. 257 */ 258 typedef enum { 259 ZONERULES_INVALID, POSIX, POSIX_USA, ZONEINFO 260 } zone_rules_t; 261 262 /* 263 * The following members are allocated from the libc-internal malloc: 264 * 265 * zonename 266 * chars 267 */ 268 typedef struct state { 269 const char *zonename; /* Timezone */ 270 struct state *next; /* next state */ 271 zone_rules_t zonerules; /* Type of zone */ 272 int daylight; /* daylight global */ 273 long default_timezone; /* Def. timezone val */ 274 long default_altzone; /* Def. altzone val */ 275 const char *default_tzname0; /* Def tz..[0] val */ 276 const char *default_tzname1; /* Def tz..[1] val */ 277 int leapcnt; /* # leap sec trans */ 278 int timecnt; /* # transitions */ 279 int typecnt; /* # zone types */ 280 int charcnt; /* # zone abbv. chars */ 281 char *chars; /* Zone abbv. chars */ 282 size_t charsbuf_size; /* malloc'ed buflen */ 283 prev_t prev[TZ_MAX_TIMES]; /* Pv. trans info */ 284 time_t ats[TZ_MAX_TIMES]; /* Trans. times */ 285 uchar_t types[TZ_MAX_TIMES]; /* Type indices */ 286 ttinfo_t ttis[TZ_MAX_TYPES]; /* Zone types */ 287 lsinfo_t lsis[TZ_MAX_LEAPS]; /* Leap sec trans */ 288 rule_t start_rule; /* For POSIX w/rules */ 289 rule_t end_rule; /* For POSIX w/rules */ 290 } state_t; 291 292 typedef struct systemtz { 293 const char *tz; 294 state_t *entry; 295 int flag; 296 } systemtz_t; 297 298 static const char *namecache; 299 300 static state_t *tzcache[HASHTABLE]; 301 302 static state_t *lclzonep; 303 304 static struct tm tm; /* For non-reentrant use */ 305 static int is_in_dst; /* Set if t is in DST */ 306 static zone_rules_t curr_zonerules = ZONERULES_INVALID; 307 static int cached_year; /* mktime() perf. enhancement */ 308 static long long cached_secs_since_1970; /* mktime() perf. */ 309 static int year_is_cached = FALSE; /* mktime() perf. */ 310 311 312 #define _2AM (2 * SECS_PER_HOUR) 313 #define FIRSTWEEK 1 314 #define LASTWEEK 5 315 316 enum wks { 317 _1st_week = 1, 318 _2nd_week, 319 _3rd_week, 320 _4th_week, 321 _Last_week 322 }; 323 324 enum dwk { 325 Sun, 326 Mon, 327 Tue, 328 Wed, 329 Thu, 330 Fri, 331 Sat 332 }; 333 334 enum mth { 335 Jan = 1, 336 Feb, 337 Mar, 338 Apr, 339 May, 340 Jun, 341 Jul, 342 Aug, 343 Sep, 344 Oct, 345 Nov, 346 Dec 347 }; 348 349 /* 350 * The following table defines standard USA DST transitions 351 * as they have been declared throughout history, disregarding 352 * the legally sanctioned local variants. 353 * 354 * Note: At some point, this table may be supplanted by 355 * more popular 'posixrules' logic. 356 */ 357 typedef struct { 358 int s_year; 359 int e_year; 360 rule_t start; 361 rule_t end; 362 } __usa_rules_t; 363 364 static const __usa_rules_t __usa_rules[] = { 365 { 366 1987, 2037, 367 { MON_WEEK_DOW, Sun, _1st_week, Apr, _2AM }, 368 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM }, 369 }, 370 { 371 1976, 1986, 372 { MON_WEEK_DOW, Sun, _Last_week, Apr, _2AM }, 373 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM }, 374 }, 375 { 376 1975, 1975, 377 { MON_WEEK_DOW, Sun, _Last_week, Feb, _2AM }, 378 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM }, 379 }, 380 381 { 382 1974, 1974, 383 { MON_WEEK_DOW, Sun, _1st_week, Jan, _2AM }, 384 { MON_WEEK_DOW, Sun, _Last_week, Nov, _2AM }, 385 }, 386 /* 387 * The entry below combines two previously separate entries for 388 * 1969-1973 and 1902-1968 389 */ 390 { 391 1902, 1973, 392 { MON_WEEK_DOW, Sun, _Last_week, Apr, _2AM }, 393 { MON_WEEK_DOW, Sun, _Last_week, Oct, _2AM }, 394 } 395 }; 396 #define MAX_RULE_TABLE (sizeof (__usa_rules) / sizeof (__usa_rules_t) - 1) 397 398 /* 399 * Prototypes for static functions. 400 */ 401 static systemtz_t *getsystemTZ(systemtz_t *); 402 static const char *getzname(const char *, int); 403 static const char *getnum(const char *, int *, int, int); 404 static const char *getsecs(const char *, long *); 405 static const char *getoffset(const char *, long *); 406 static const char *getrule(const char *, rule_t *, int); 407 static int load_posixinfo(const char *, state_t *); 408 static int load_zoneinfo(const char *, state_t *); 409 static void ltzset_u(time_t, systemtz_t *); 410 static struct tm *offtime_u(time_t, long, struct tm *); 411 static int posix_check_dst(long long, state_t *); 412 static int posix_daylight(long long *, int, posix_daylight_t *); 413 static void set_zone_context(time_t); 414 415 /* 416 * definition of difftime 417 * 418 * This code assumes time_t is type long. Note the difference of two 419 * longs in absolute value is representable as an unsigned long. So, 420 * compute the absolute value of the difference, cast the result to 421 * double and attach the sign back on. 422 * 423 * Note this code assumes 2's complement arithmetic. The subtraction 424 * operation may overflow when using signed operands, but when the 425 * result is cast to unsigned long, it yields the desired value 426 * (ie, the absolute value of the difference). The cast to unsigned 427 * long is done using pointers to avoid undefined behavior if casting 428 * a negative value to unsigned. 429 */ 430 double 431 difftime(time_t time1, time_t time0) 432 { 433 if (time1 < time0) { 434 time0 -= time1; 435 return (-(double)*(unsigned long *) &time0); 436 } else { 437 time1 -= time0; 438 return ((double)*(unsigned long *) &time1); 439 } 440 } 441 442 /* 443 * Accepts a time_t, returns a tm struct based on it, with 444 * no local timezone adjustment. 445 * 446 * This routine is the thread-safe variant of gmtime(), and 447 * requires that the call provide the address of their own tm 448 * struct. 449 * 450 * Locking is not done here because set_zone_context() 451 * is not called, thus timezone, altzone, and tzname[] are not 452 * accessed, no memory is allocated, and no common dynamic 453 * data is accessed. 454 * 455 * See ctime(3C) 456 */ 457 struct tm * 458 _gmtime_r(const time_t *timep, struct tm *p_tm) 459 { 460 return (offtime_u((time_t)*timep, 0L, p_tm)); 461 } 462 463 /* 464 * Accepts a time_t, returns a tm struct based on it, with 465 * no local timezone adjustment. 466 * 467 * This function is explicitly NOT THREAD-SAFE. The standards 468 * indicate it should provide its results in its own statically 469 * allocated tm struct that gets overwritten. The thread-safe 470 * variant is gmtime_r(). We make it mostly thread-safe by 471 * allocating its buffer in thread-specific data. 472 * 473 * See ctime(3C) 474 */ 475 struct tm * 476 gmtime(const time_t *timep) 477 { 478 struct tm *p_tm = tsdalloc(_T_STRUCT_TM, sizeof (struct tm), NULL); 479 480 if (p_tm == NULL) /* memory allocation failure */ 481 p_tm = &tm; /* use static buffer and hope for the best */ 482 return (_gmtime_r(timep, p_tm)); 483 } 484 485 /* 486 * This is the hashing function, based on the input timezone name. 487 */ 488 static int 489 get_hashid(const char *id) 490 { 491 const unsigned char *s = (const unsigned char *)id; 492 unsigned char c; 493 unsigned int h; 494 495 h = *s++; 496 while ((c = *s++) != '\0') { 497 h = (h << 5) - h + c; 498 } 499 return ((int)(h % HASHTABLE)); 500 } 501 502 /* 503 * find_zone() gets the hashid for zonename, then uses the hashid 504 * to search the hash table for the appropriate timezone entry. If 505 * the entry for zonename is found in the hash table, return a pointer 506 * to the entry. Otherwise, update the input link_prev and link_next 507 * to the addresses of pointers for the caller to update to add the new 508 * entry to the hash table. 509 */ 510 static state_t * 511 find_zone(const char *zonename, state_t ***link_prev, state_t **link_next) 512 { 513 int hashid; 514 state_t *cur, *prv; 515 516 hashid = get_hashid(zonename); 517 cur = tzcache[hashid]; 518 prv = NULL; 519 while (cur) { 520 int res; 521 res = strcmp(cur->zonename, zonename); 522 if (res == 0) { 523 return (cur); 524 } else if (res > 0) { 525 break; 526 } 527 prv = cur; 528 cur = cur->next; 529 } 530 if (prv) { 531 *link_prev = &prv->next; 532 *link_next = cur; 533 } else { 534 *link_prev = &tzcache[hashid]; 535 *link_next = NULL; 536 } 537 return (NULL); 538 } 539 540 541 /* 542 * Returns tm struct based on input time_t argument, correcting 543 * for the local timezone, producing documented side-effects 544 * to extern global state, timezone, altzone, daylight and tzname[]. 545 * 546 * localtime_r() is the thread-safe variant of localtime(). 547 * 548 * IMPLEMENTATION NOTE: 549 * 550 * Locking slows multithreaded access and is probably ultimately 551 * unnecessary here. The POSIX specification is a bit vague 552 * as to whether the extern variables set by tzset() need to 553 * set as a result of a call to localtime_r() 554 * 555 * Currently, the spec only mentions that tzname[] doesn't 556 * need to be set. As soon as it becomes unequivocal 557 * that the external zone state doesn't need to be asserted 558 * for this call, and it really doesn't make much sense 559 * to set common state from multi-threaded calls made to this 560 * function, locking can be dispensed with here. 561 * 562 * local zone state would still need to be aquired for the 563 * time in question in order for calculations elicited here 564 * to be correct, but that state wouldn't need to be shared, 565 * thus no multi-threaded synchronization would be required. 566 * 567 * It would be nice if POSIX would approve an ltzset_r() 568 * function, but if not, it wouldn't stop us from making one 569 * privately. 570 * 571 * localtime_r() can now return NULL if overflow is detected. 572 * offtime_u() is the function that detects overflow, and sets 573 * errno appropriately. We unlock before the call to offtime_u(), 574 * so that lmutex_unlock() does not reassign errno. The function 575 * offtime_u() is MT-safe and does not have to be locked. Use 576 * my_is_in_dst to reference local copy of is_in_dst outside locks. 577 * 578 * See ctime(3C) 579 */ 580 struct tm * 581 _localtime_r(const time_t *timep, struct tm *p_tm) 582 { 583 long offset; 584 struct tm *rt; 585 int my_is_in_dst; 586 systemtz_t stz; 587 systemtz_t *tzp; 588 589 tzp = getsystemTZ(&stz); 590 591 lmutex_lock(&_time_lock); 592 ltzset_u(*timep, tzp); 593 if (lclzonep == NULL) { 594 lmutex_unlock(&_time_lock); 595 if (tzp->flag) 596 free(tzp->entry); 597 return (offtime_u(*timep, 0L, p_tm)); 598 } 599 my_is_in_dst = is_in_dst; 600 offset = (my_is_in_dst) ? -altzone : -timezone; 601 lmutex_unlock(&_time_lock); 602 rt = offtime_u(*timep, offset, p_tm); 603 p_tm->tm_isdst = my_is_in_dst; 604 if (tzp->flag) 605 free(tzp->entry); 606 return (rt); 607 } 608 609 /* 610 * Accepts a time_t, returns a tm struct based on it, correcting 611 * for the local timezone. Produces documented side-effects to 612 * extern global timezone state data. 613 * 614 * This function is explicitly NOT THREAD-SAFE. The standards 615 * indicate it should provide its results in its own statically 616 * allocated tm struct that gets overwritten. The thread-safe 617 * variant is localtime_r(). We make it mostly thread-safe by 618 * allocating its buffer in thread-specific data. 619 * 620 * localtime() can now return NULL if overflow is detected. 621 * offtime_u() is the function that detects overflow, and sets 622 * errno appropriately. 623 * 624 * See ctime(3C) 625 */ 626 struct tm * 627 localtime(const time_t *timep) 628 { 629 struct tm *p_tm = tsdalloc(_T_STRUCT_TM, sizeof (struct tm), NULL); 630 631 if (p_tm == NULL) /* memory allocation failure */ 632 p_tm = &tm; /* use static buffer and hope for the best */ 633 return (_localtime_r(timep, p_tm)); 634 } 635 636 /* 637 * This function takes a pointer to a tm struct and returns a 638 * normalized time_t, also inducing documented side-effects in 639 * extern global zone state variables. (See mktime(3C)). 640 */ 641 time_t 642 mktime(struct tm *tmptr) 643 { 644 struct tm _tm; 645 long long t; /* must hold more than 32-bit time_t */ 646 int temp; 647 int mketimerrno; 648 int overflow; 649 systemtz_t stz; 650 systemtz_t *tzp; 651 652 mketimerrno = errno; 653 654 tzp = getsystemTZ(&stz); 655 656 /* mktime leaves errno unchanged if no error is encountered */ 657 658 lmutex_lock(&_time_lock); 659 660 /* Calculate time_t from tm arg. tm may need to be normalized. */ 661 t = tmptr->tm_sec + SECSPERMIN * tmptr->tm_min + 662 SECSPERHOUR * tmptr->tm_hour + 663 SECSPERDAY * (tmptr->tm_mday - 1); 664 665 if (tmptr->tm_mon >= 12) { 666 tmptr->tm_year += tmptr->tm_mon / 12; 667 tmptr->tm_mon %= 12; 668 } else if (tmptr->tm_mon < 0) { 669 temp = -tmptr->tm_mon; 670 tmptr->tm_mon = 0; /* If tm_mon divides by 12. */ 671 tmptr->tm_year -= (temp / 12); 672 if (temp %= 12) { /* Remainder... */ 673 tmptr->tm_year--; 674 tmptr->tm_mon = 12 - temp; 675 } 676 } 677 678 /* Avoid numerous calculations embedded in macro if possible */ 679 if (!year_is_cached || (cached_year != tmptr->tm_year)) { 680 cached_year = tmptr->tm_year; 681 year_is_cached = TRUE; 682 /* For boundry values of tm_year, typecasting required */ 683 cached_secs_since_1970 = 684 (long long)SECSPERDAY * DAYS_SINCE_70(cached_year); 685 } 686 t += cached_secs_since_1970; 687 688 if (isleap(tmptr->tm_year + TM_YEAR_BASE)) 689 t += SECSPERDAY * __lyday_to_month[tmptr->tm_mon]; 690 else 691 t += SECSPERDAY * __yday_to_month[tmptr->tm_mon]; 692 693 ltzset_u((time_t)t, tzp); 694 /* Attempt to convert time to GMT based on tm_isdst setting */ 695 t += (tmptr->tm_isdst > 0) ? altzone : timezone; 696 697 #ifdef _ILP32 698 overflow = t > LONG_MAX || t < LONG_MIN || 699 tmptr->tm_year < 1 || tmptr->tm_year > 138; 700 #else 701 overflow = t > LONG_MAX || t < LONG_MIN; 702 #endif 703 set_zone_context((time_t)t); 704 if (tmptr->tm_isdst < 0) { 705 long dst_delta = timezone - altzone; 706 switch (curr_zonerules) { 707 case ZONEINFO: 708 if (is_in_dst) { 709 t -= dst_delta; 710 set_zone_context((time_t)t); 711 if (is_in_dst) { 712 (void) offtime_u((time_t)t, 713 -altzone, &_tm); 714 _tm.tm_isdst = 1; 715 } else { 716 (void) offtime_u((time_t)t, 717 -timezone, &_tm); 718 } 719 } else { 720 (void) offtime_u((time_t)t, -timezone, &_tm); 721 } 722 break; 723 case POSIX_USA: 724 case POSIX: 725 if (is_in_dst) { 726 t -= dst_delta; 727 set_zone_context((time_t)t); 728 if (is_in_dst) { 729 (void) offtime_u((time_t)t, 730 -altzone, &_tm); 731 _tm.tm_isdst = 1; 732 } else { 733 (void) offtime_u((time_t)t, 734 -timezone, &_tm); 735 } 736 } else { /* check for ambiguous 'fallback' transition */ 737 set_zone_context((time_t)t - dst_delta); 738 if (is_in_dst) { /* In fallback, force DST */ 739 t -= dst_delta; 740 (void) offtime_u((time_t)t, 741 -altzone, &_tm); 742 _tm.tm_isdst = 1; 743 } else { 744 (void) offtime_u((time_t)t, 745 -timezone, &_tm); 746 } 747 } 748 break; 749 750 case ZONERULES_INVALID: 751 (void) offtime_u((time_t)t, 0L, &_tm); 752 break; 753 754 } 755 } else if (is_in_dst) { 756 (void) offtime_u((time_t)t, -altzone, &_tm); 757 _tm.tm_isdst = 1; 758 } else { 759 (void) offtime_u((time_t)t, -timezone, &_tm); 760 } 761 762 if (overflow || t > LONG_MAX || t < LONG_MIN) { 763 mketimerrno = EOVERFLOW; 764 t = -1; 765 } else { 766 *tmptr = _tm; 767 } 768 769 lmutex_unlock(&_time_lock); 770 771 if (tzp->flag) 772 free(tzp->entry); 773 errno = mketimerrno; 774 return ((time_t)t); 775 } 776 777 /* 778 * Sets extern global zone state variables based on the current 779 * time. Specifically, tzname[], timezone, altzone, and daylight 780 * are updated. See ctime(3C) manpage. 781 */ 782 void 783 _tzset(void) 784 { 785 systemtz_t stz; 786 systemtz_t *tzp; 787 788 tzp = getsystemTZ(&stz); 789 790 lmutex_lock(&_time_lock); 791 ltzset_u(time(NULL), tzp); 792 lmutex_unlock(&_time_lock); 793 if (tzp->flag) 794 free(tzp->entry); 795 } 796 797 void 798 _ltzset(time_t tim) 799 { 800 systemtz_t stz; 801 systemtz_t *tzp; 802 803 tzp = getsystemTZ(&stz); 804 805 lmutex_lock(&_time_lock); 806 ltzset_u(tim, tzp); 807 lmutex_unlock(&_time_lock); 808 if (tzp->flag) 809 free(tzp->entry); 810 } 811 812 /* 813 * Loads local zone information if TZ changed since last time zone 814 * information was loaded, or if this is the first time thru. 815 * We already hold _time_lock; no further locking is required. 816 */ 817 static void 818 ltzset_u(time_t t, systemtz_t *tzp) 819 { 820 const char *zonename = tzp->tz; 821 state_t *entry, **p, *q; 822 823 if (zonename == NULL || *zonename == '\0') 824 zonename = _posix_gmt0; 825 826 if (curr_zonerules != ZONERULES_INVALID && 827 strcmp(namecache, zonename) == 0) { 828 set_zone_context(t); 829 return; 830 } 831 832 entry = find_zone(zonename, &p, &q); 833 if (entry == NULL) { 834 /* 835 * No timezone entry found in hash table, so load it, 836 * and create a new timezone entry. 837 */ 838 char *newzonename, *charsbuf; 839 840 /* Invalidate the current timezone */ 841 curr_zonerules = ZONERULES_INVALID; 842 843 newzonename = libc_strdup(zonename); 844 daylight = 0; 845 entry = tzp->entry; 846 847 if (entry == NULL || newzonename == NULL) { 848 /* something wrong happened. */ 849 if (newzonename != NULL) 850 libc_free(newzonename); 851 timezone = altzone = 0; 852 is_in_dst = 0; 853 tzname[0] = (char *)_tz_gmt; 854 tzname[1] = (char *)_tz_spaces; 855 return; 856 } 857 858 /* 859 * Builds transition cache and sets up zone state data for zone 860 * specified in TZ, which can be specified as a POSIX zone or an 861 * Olson zoneinfo file reference. 862 * 863 * If local data cannot be parsed or loaded, the local zone 864 * tables are set up for GMT. 865 * 866 * Unless a leading ':' is prepended to TZ, TZ is initially 867 * parsed as a POSIX zone; failing that, it reverts to 868 * a zoneinfo check. 869 * However, if a ':' is prepended, the zone will *only* be 870 * parsed as zoneinfo. If any failure occurs parsing or 871 * loading a zoneinfo TZ, GMT data is loaded for the local zone. 872 * 873 * Example: There is a zoneinfo file in the standard 874 * distribution called 'PST8PDT'. The only way the user can 875 * specify that file under Solaris is to set TZ to ":PST8PDT". 876 * Otherwise the initial parse of PST8PDT as a POSIX zone will 877 * succeed and be used. 878 */ 879 if ((charsbuf = libc_malloc(TZ_MAX_CHARS)) == NULL) { 880 libc_free(newzonename); 881 882 timezone = altzone = 0; 883 is_in_dst = 0; 884 tzname[0] = (char *)_tz_gmt; 885 tzname[1] = (char *)_tz_spaces; 886 return; 887 } 888 entry->charsbuf_size = TZ_MAX_CHARS; 889 entry->chars = charsbuf; 890 entry->default_tzname0 = _tz_gmt; 891 entry->default_tzname1 = _tz_spaces; 892 entry->zonename = newzonename; 893 894 if (*zonename == ':') { 895 if (load_zoneinfo(zonename + 1, entry) != 0) { 896 (void) load_posixinfo(_posix_gmt0, entry); 897 } 898 } else if (load_posixinfo(zonename, entry) != 0) { 899 if (load_zoneinfo(zonename, entry) != 0) { 900 (void) load_posixinfo(_posix_gmt0, entry); 901 } 902 } 903 /* 904 * The pre-allocated buffer is used; reset the free flag 905 * so the buffer won't be freed. 906 */ 907 tzp->flag = 0; 908 entry->next = q; 909 *p = entry; 910 } 911 912 curr_zonerules = entry->zonerules; 913 namecache = entry->zonename; 914 daylight = entry->daylight; 915 lclzonep = entry; 916 917 set_zone_context(t); 918 } 919 920 /* 921 * Sets timezone, altzone, tzname[], extern globals, to represent 922 * disposition of t with respect to TZ; See ctime(3C). is_in_dst, 923 * internal global is also set. daylight is set at zone load time. 924 * 925 * Issues: 926 * 927 * In this function, any time_t not located in the cache is handled 928 * as a miss. To build/update transition cache, load_zoneinfo() 929 * must be called prior to this routine. 930 * 931 * If POSIX zone, cache miss penalty is slightly degraded 932 * performance. For zoneinfo, penalty is decreased is_in_dst 933 * accuracy. 934 * 935 * POSIX, despite its chicken/egg problem, ie. not knowing DST 936 * until time known, and not knowing time until DST known, at 937 * least uses the same algorithm for 64-bit time as 32-bit. 938 * 939 * The fact that zoneinfo files only contain transistions for 32-bit 940 * time space is a well known problem, as yet unresolved. 941 * Without an official standard for coping with out-of-range 942 * zoneinfo times, assumptions must be made. For now 943 * the assumption is: If t exceeds 32-bit boundries and local zone 944 * is zoneinfo type, is_in_dst is set to to 0 for negative values 945 * of t, and set to the same DST state as the highest ordered 946 * transition in cache for positive values of t. 947 */ 948 static void 949 set_zone_context(time_t t) 950 { 951 prev_t *prevp; 952 int lo, hi, tidx; 953 ttinfo_t *ttisp, *std, *alt; 954 955 /* If state data not loaded or TZ busted, just use GMT */ 956 if (lclzonep == NULL || curr_zonerules == ZONERULES_INVALID) { 957 timezone = altzone = 0; 958 daylight = is_in_dst = 0; 959 tzname[0] = (char *)_tz_gmt; 960 tzname[1] = (char *)_tz_spaces; 961 return; 962 } 963 964 /* Retrieve suitable defaults for this zone */ 965 altzone = lclzonep->default_altzone; 966 timezone = lclzonep->default_timezone; 967 tzname[0] = (char *)lclzonep->default_tzname0; 968 tzname[1] = (char *)lclzonep->default_tzname1; 969 is_in_dst = 0; 970 971 if (lclzonep->timecnt <= 0 || lclzonep->typecnt < 2) 972 /* Loaded zone incapable of transitioning. */ 973 return; 974 975 /* 976 * At least one alt. zone and one transistion exist. Locate 977 * state for 't' quickly as possible. Use defaults as necessary. 978 */ 979 lo = 0; 980 hi = lclzonep->timecnt - 1; 981 982 if (t < lclzonep->ats[0] || t >= lclzonep->ats[hi]) { 983 984 /* CACHE MISS. Calculate DST as best as possible */ 985 if (lclzonep->zonerules == POSIX_USA || 986 lclzonep->zonerules == POSIX) { 987 /* Must nvoke calculations to determine DST */ 988 is_in_dst = (daylight) ? 989 posix_check_dst(t, lclzonep) : 0; 990 return; 991 } else if (t < lclzonep->ats[0]) { /* zoneinfo... */ 992 /* t precedes 1st transition. Use defaults */ 993 return; 994 } else { /* zoneinfo */ 995 /* t follows final transistion. Use final */ 996 tidx = hi; 997 } 998 999 } else { 1000 1001 /* CACHE HIT. Locate transition using binary search. */ 1002 1003 while (lo <= hi) { 1004 tidx = (lo + hi) / 2; 1005 if (t == lclzonep->ats[tidx]) 1006 break; 1007 else if (t < lclzonep->ats[tidx]) 1008 hi = tidx - 1; 1009 else 1010 lo = tidx + 1; 1011 } 1012 if (lo > hi) 1013 tidx = hi; 1014 } 1015 1016 /* 1017 * Set extern globals based on located transition and summary of 1018 * its previous state, which were cached when zone was loaded 1019 */ 1020 ttisp = &lclzonep->ttis[lclzonep->types[tidx]]; 1021 prevp = &lclzonep->prev[tidx]; 1022 1023 if ((is_in_dst = ttisp->tt_isdst) == 0) { /* std. time */ 1024 timezone = -ttisp->tt_gmtoff; 1025 tzname[0] = &lclzonep->chars[ttisp->tt_abbrind]; 1026 if ((alt = prevp->alt) != NULL) { 1027 altzone = -alt->tt_gmtoff; 1028 tzname[1] = &lclzonep->chars[alt->tt_abbrind]; 1029 } 1030 } else { /* alt. time */ 1031 altzone = -ttisp->tt_gmtoff; 1032 tzname[1] = &lclzonep->chars[ttisp->tt_abbrind]; 1033 if ((std = prevp->std) != NULL) { 1034 timezone = -std->tt_gmtoff; 1035 tzname[0] = &lclzonep->chars[std->tt_abbrind]; 1036 } 1037 } 1038 } 1039 1040 /* 1041 * This function takes a time_t and gmt offset and produces a 1042 * tm struct based on specified time. 1043 * 1044 * The the following fields are calculated, based entirely 1045 * on the offset-adjusted value of t: 1046 * 1047 * tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec 1048 * tm_yday. tm_wday. (tm_isdst is ALWAYS set to 0). 1049 */ 1050 1051 static struct tm * 1052 offtime_u(time_t t, long offset, struct tm *tmptr) 1053 { 1054 long days; 1055 long rem; 1056 long y; 1057 int yleap; 1058 const int *ip; 1059 1060 days = t / SECSPERDAY; 1061 rem = t % SECSPERDAY; 1062 rem += offset; 1063 while (rem < 0) { 1064 rem += SECSPERDAY; 1065 --days; 1066 } 1067 while (rem >= SECSPERDAY) { 1068 rem -= SECSPERDAY; 1069 ++days; 1070 } 1071 tmptr->tm_hour = (int)(rem / SECSPERHOUR); 1072 rem = rem % SECSPERHOUR; 1073 tmptr->tm_min = (int)(rem / SECSPERMIN); 1074 tmptr->tm_sec = (int)(rem % SECSPERMIN); 1075 1076 tmptr->tm_wday = (int)((EPOCH_WDAY + days) % DAYSPERWEEK); 1077 if (tmptr->tm_wday < 0) 1078 tmptr->tm_wday += DAYSPERWEEK; 1079 y = EPOCH_YEAR; 1080 while (days < 0 || days >= (long)__year_lengths[yleap = isleap(y)]) { 1081 long newy; 1082 1083 newy = y + days / DAYSPERNYEAR; 1084 if (days < 0) 1085 --newy; 1086 days -= ((long)newy - (long)y) * DAYSPERNYEAR + 1087 LEAPS_THRU_END_OF(newy > 0 ? newy - 1L : newy) - 1088 LEAPS_THRU_END_OF(y > 0 ? y - 1L : y); 1089 y = newy; 1090 } 1091 tmptr->tm_year = (int)(y - TM_YEAR_BASE); 1092 tmptr->tm_yday = (int)days; 1093 ip = __mon_lengths[yleap]; 1094 for (tmptr->tm_mon = 0; days >= 1095 (long)ip[tmptr->tm_mon]; ++(tmptr->tm_mon)) 1096 days = days - (long)ip[tmptr->tm_mon]; 1097 tmptr->tm_mday = (int)(days + 1); 1098 tmptr->tm_isdst = 0; 1099 1100 #ifdef _LP64 1101 /* do as much as possible before checking for error. */ 1102 if ((y > (long)INT_MAX + TM_YEAR_BASE) || 1103 (y < (long)INT_MIN + TM_YEAR_BASE)) { 1104 errno = EOVERFLOW; 1105 return (NULL); 1106 } 1107 #endif 1108 return (tmptr); 1109 } 1110 1111 /* 1112 * Check whether DST is set for time in question. Only applies to 1113 * POSIX timezones. If explicit POSIX transition rules were provided 1114 * for the current zone, use those, otherwise use default USA POSIX 1115 * transitions. 1116 */ 1117 static int 1118 posix_check_dst(long long t, state_t *sp) 1119 { 1120 struct tm gmttm; 1121 long long jan01; 1122 int year, i, idx, ridx; 1123 posix_daylight_t pdaylight; 1124 1125 (void) offtime_u(t, 0L, &gmttm); 1126 1127 year = gmttm.tm_year + 1900; 1128 jan01 = t - ((gmttm.tm_yday * SECSPERDAY) + 1129 (gmttm.tm_hour * SECSPERHOUR) + 1130 (gmttm.tm_min * SECSPERMIN) + gmttm.tm_sec); 1131 /* 1132 * If transition rules were provided for this zone, 1133 * use them, otherwise, default to USA daylight rules, 1134 * which are historically correct for the continental USA, 1135 * excluding local provisions. (This logic may be replaced 1136 * at some point in the future with "posixrules" to offer 1137 * more flexibility to the system administrator). 1138 */ 1139 if (sp->zonerules == POSIX) { /* POSIX rules */ 1140 pdaylight.rules[0] = &sp->start_rule; 1141 pdaylight.rules[1] = &sp->end_rule; 1142 } else { /* POSIX_USA: USA */ 1143 i = 0; 1144 while (year < __usa_rules[i].s_year && i < MAX_RULE_TABLE) { 1145 i++; 1146 } 1147 pdaylight.rules[0] = (rule_t *)&__usa_rules[i].start; 1148 pdaylight.rules[1] = (rule_t *)&__usa_rules[i].end; 1149 } 1150 pdaylight.offset[0] = timezone; 1151 pdaylight.offset[1] = altzone; 1152 1153 idx = posix_daylight(&jan01, year, &pdaylight); 1154 ridx = !idx; 1155 1156 /* 1157 * Note: t, rtime[0], and rtime[1] are all bounded within 'year' 1158 * beginning on 'jan01' 1159 */ 1160 if (t >= pdaylight.rtime[idx] && t < pdaylight.rtime[ridx]) { 1161 return (ridx); 1162 } else { 1163 return (idx); 1164 } 1165 } 1166 1167 /* 1168 * Given January 1, 00:00:00 GMT for a year as an Epoch-relative time, 1169 * along with the integer year #, a posix_daylight_t that is composed 1170 * of two rules, and two GMT offsets (timezone and altzone), calculate 1171 * the two Epoch-relative times the two rules take effect, and return 1172 * them in the two rtime fields of the posix_daylight_t structure. 1173 * Also update janfirst by a year, by adding the appropriate number of 1174 * seconds depending on whether the year is a leap year or not. (We take 1175 * advantage that this routine knows the leap year status.) 1176 */ 1177 static int 1178 posix_daylight(long long *janfirst, int year, posix_daylight_t *pdaylightp) 1179 { 1180 rule_t *rulep; 1181 long offset; 1182 int idx; 1183 int i, d, m1, yy0, yy1, yy2, dow; 1184 long leapyear; 1185 long long value; 1186 1187 static const int __secs_year_lengths[2] = { 1188 DAYS_PER_NYEAR * SECSPERDAY, 1189 DAYS_PER_LYEAR * SECSPERDAY 1190 }; 1191 1192 leapyear = isleap(year); 1193 1194 for (idx = 0; idx < 2; idx++) { 1195 rulep = pdaylightp->rules[idx]; 1196 offset = pdaylightp->offset[idx]; 1197 1198 switch (rulep->r_type) { 1199 1200 case MON_WEEK_DOW: 1201 /* 1202 * Mm.n.d - nth "dth day" of month m. 1203 */ 1204 value = *janfirst; 1205 for (i = 0; i < rulep->r_mon - 1; ++i) 1206 value += __mon_lengths[leapyear][i] * 1207 SECSPERDAY; 1208 1209 /* 1210 * Use Zeller's Congruence to get day-of-week of first 1211 * day of month. 1212 */ 1213 m1 = (rulep->r_mon + 9) % 12 + 1; 1214 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year; 1215 yy1 = yy0 / 100; 1216 yy2 = yy0 % 100; 1217 dow = ((26 * m1 - 2) / 10 + 1218 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7; 1219 1220 if (dow < 0) 1221 dow += DAYSPERWEEK; 1222 1223 /* 1224 * Following heuristic increases accuracy of USA rules 1225 * for negative years. 1226 */ 1227 if (year < 1 && leapyear) 1228 ++dow; 1229 /* 1230 * "dow" is the day-of-week of the first day of the 1231 * month. Get the day-of-month, zero-origin, of the 1232 * first "dow" day of the month. 1233 */ 1234 d = rulep->r_day - dow; 1235 if (d < 0) 1236 d += DAYSPERWEEK; 1237 for (i = 1; i < rulep->r_week; ++i) { 1238 if (d + DAYSPERWEEK >= 1239 __mon_lengths[leapyear][rulep->r_mon - 1]) 1240 break; 1241 d += DAYSPERWEEK; 1242 } 1243 /* 1244 * "d" is the day-of-month, zero-origin, of the day 1245 * we want. 1246 */ 1247 value += d * SECSPERDAY; 1248 break; 1249 1250 case JULIAN_DAY: 1251 /* 1252 * Jn - Julian day, 1 == Jan 1, 60 == March 1 even 1253 * in leap yrs. 1254 */ 1255 value = *janfirst + (rulep->r_day - 1) * SECSPERDAY; 1256 if (leapyear && rulep->r_day >= 60) 1257 value += SECSPERDAY; 1258 break; 1259 1260 case DAY_OF_YEAR: 1261 /* 1262 * n - day of year. 1263 */ 1264 value = *janfirst + rulep->r_day * SECSPERDAY; 1265 break; 1266 } 1267 pdaylightp->rtime[idx] = value + rulep->r_time + offset; 1268 } 1269 *janfirst += __secs_year_lengths[leapyear]; 1270 1271 return ((pdaylightp->rtime[0] > pdaylightp->rtime[1]) ? 1 : 0); 1272 } 1273 1274 /* 1275 * Try to load zoneinfo file into internal transition tables using name 1276 * indicated in TZ, and do validity checks. The format of zic(1M) 1277 * compiled zoneinfo files isdescribed in tzfile.h 1278 */ 1279 static int 1280 load_zoneinfo(const char *name, state_t *sp) 1281 { 1282 char *cp; 1283 char *cp2; 1284 int i; 1285 long cnt; 1286 int fid; 1287 int ttisstdcnt; 1288 int ttisgmtcnt; 1289 char *fullname; 1290 size_t namelen; 1291 char *bufp; 1292 size_t flen; 1293 prev_t *prevp; 1294 /* LINTED */ 1295 struct tzhead *tzhp; 1296 struct stat64 stbuf; 1297 ttinfo_t *most_recent_alt = NULL; 1298 ttinfo_t *most_recent_std = NULL; 1299 ttinfo_t *ttisp; 1300 1301 1302 if (name == NULL && (name = TZDEFAULT) == NULL) 1303 return (-1); 1304 1305 if ((name[0] == '/') || strstr(name, "../")) 1306 return (-1); 1307 1308 /* 1309 * We allocate fullname this way to avoid having 1310 * a PATH_MAX size buffer in our stack frame. 1311 */ 1312 namelen = LEN_TZDIR + 1 + strlen(name) + 1; 1313 if ((fullname = lmalloc(namelen)) == NULL) 1314 return (-1); 1315 (void) strcpy(fullname, TZDIR "/"); 1316 (void) strcpy(fullname + LEN_TZDIR + 1, name); 1317 if ((fid = open(fullname, O_RDONLY)) == -1) { 1318 lfree(fullname, namelen); 1319 return (-1); 1320 } 1321 lfree(fullname, namelen); 1322 1323 if (fstat64(fid, &stbuf) == -1) { 1324 (void) close(fid); 1325 return (-1); 1326 } 1327 1328 flen = (size_t)stbuf.st_size; 1329 if (flen < sizeof (struct tzhead)) { 1330 (void) close(fid); 1331 return (-1); 1332 } 1333 1334 /* 1335 * It would be nice to use alloca() to allocate bufp but, 1336 * as above, we wish to avoid allocating a big buffer in 1337 * our stack frame, and also because alloca() gives us no 1338 * opportunity to fail gracefully on allocation failure. 1339 */ 1340 cp = bufp = lmalloc(flen); 1341 if (bufp == NULL) { 1342 (void) close(fid); 1343 return (-1); 1344 } 1345 1346 if ((cnt = read(fid, bufp, flen)) != flen) { 1347 lfree(bufp, flen); 1348 (void) close(fid); 1349 return (-1); 1350 } 1351 1352 if (close(fid) != 0) { 1353 lfree(bufp, flen); 1354 return (-1); 1355 } 1356 1357 cp += (sizeof (tzhp->tzh_magic)) + (sizeof (tzhp->tzh_reserved)); 1358 1359 /* LINTED: alignment */ 1360 ttisstdcnt = CVTZCODE(cp); 1361 /* LINTED: alignment */ 1362 ttisgmtcnt = CVTZCODE(cp); 1363 /* LINTED: alignment */ 1364 sp->leapcnt = CVTZCODE(cp); 1365 /* LINTED: alignment */ 1366 sp->timecnt = CVTZCODE(cp); 1367 /* LINTED: alignment */ 1368 sp->typecnt = CVTZCODE(cp); 1369 /* LINTED: alignment */ 1370 sp->charcnt = CVTZCODE(cp); 1371 1372 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS || 1373 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES || 1374 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES || 1375 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS || 1376 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) || 1377 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0)) { 1378 lfree(bufp, flen); 1379 return (-1); 1380 } 1381 1382 if (cnt - (cp - bufp) < (long)(sp->timecnt * 4 + /* ats */ 1383 sp->timecnt + /* types */ 1384 sp->typecnt * (4 + 2) + /* ttinfos */ 1385 sp->charcnt + /* chars */ 1386 sp->leapcnt * (4 + 4) + /* lsinfos */ 1387 ttisstdcnt + /* ttisstds */ 1388 ttisgmtcnt)) { /* ttisgmts */ 1389 lfree(bufp, flen); 1390 return (-1); 1391 } 1392 1393 1394 for (i = 0; i < sp->timecnt; ++i) { 1395 /* LINTED: alignment */ 1396 sp->ats[i] = CVTZCODE(cp); 1397 } 1398 1399 /* 1400 * Skip over types[] for now and load ttis[] so that when 1401 * types[] are loaded we can check for transitions to STD & DST. 1402 * This allows us to shave cycles in ltzset_u(), including 1403 * eliminating the need to check set 'daylight' later. 1404 */ 1405 1406 cp2 = (char *)((uintptr_t)cp + sp->timecnt); 1407 1408 for (i = 0; i < sp->typecnt; ++i) { 1409 ttisp = &sp->ttis[i]; 1410 /* LINTED: alignment */ 1411 ttisp->tt_gmtoff = CVTZCODE(cp2); 1412 ttisp->tt_isdst = (uchar_t)*cp2++; 1413 1414 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1) { 1415 lfree(bufp, flen); 1416 return (-1); 1417 } 1418 1419 ttisp->tt_abbrind = (uchar_t)*cp2++; 1420 if (ttisp->tt_abbrind < 0 || 1421 ttisp->tt_abbrind > sp->charcnt) { 1422 lfree(bufp, flen); 1423 return (-1); 1424 } 1425 } 1426 1427 /* 1428 * Since ttis were loaded ahead of types, it is possible to 1429 * detect whether daylight is ever set for this zone now, and 1430 * also preload other information to avoid repeated lookups later. 1431 * This logic facilitates keeping a running tab on the state of 1432 * std zone and alternate zone transitions such that timezone, 1433 * altzone and tzname[] can be determined quickly via an 1434 * index to any transition. 1435 * 1436 * For transition #0 there are no previous transitions, 1437 * so prev->std and prev->alt will be null, but that's OK, 1438 * because null prev->std/prev->alt effectively 1439 * indicates none existed prior. 1440 */ 1441 1442 prevp = &sp->prev[0]; 1443 1444 for (i = 0; i < sp->timecnt; ++i) { 1445 1446 sp->types[i] = (uchar_t)*cp++; 1447 ttisp = &sp->ttis[sp->types[i]]; 1448 1449 prevp->std = most_recent_std; 1450 prevp->alt = most_recent_alt; 1451 1452 if (ttisp->tt_isdst == 1) { 1453 most_recent_alt = ttisp; 1454 } else { 1455 most_recent_std = ttisp; 1456 } 1457 1458 if ((int)sp->types[i] >= sp->typecnt) { 1459 lfree(bufp, flen); 1460 return (-1); 1461 } 1462 1463 ++prevp; 1464 } 1465 if (most_recent_alt == NULL) 1466 sp->daylight = 0; 1467 else 1468 sp->daylight = 1; 1469 1470 /* 1471 * Set pointer ahead to where it would have been if we 1472 * had read types[] and ttis[] in the same order they 1473 * occurred in the file. 1474 */ 1475 cp = cp2; 1476 for (i = 0; i < sp->charcnt; ++i) 1477 sp->chars[i] = *cp++; 1478 1479 sp->chars[i] = '\0'; /* ensure '\0' at end */ 1480 1481 for (i = 0; i < sp->leapcnt; ++i) { 1482 struct lsinfo *lsisp; 1483 1484 lsisp = &sp->lsis[i]; 1485 /* LINTED: alignment */ 1486 lsisp->ls_trans = CVTZCODE(cp); 1487 /* LINTED: alignment */ 1488 lsisp->ls_corr = CVTZCODE(cp); 1489 } 1490 1491 for (i = 0; i < sp->typecnt; ++i) { 1492 ttisp = &sp->ttis[i]; 1493 if (ttisstdcnt == 0) { 1494 ttisp->tt_ttisstd = FALSE; 1495 } else { 1496 ttisp->tt_ttisstd = *cp++; 1497 if (ttisp->tt_ttisstd != TRUE && 1498 ttisp->tt_ttisstd != FALSE) { 1499 lfree(bufp, flen); 1500 return (-1); 1501 } 1502 } 1503 } 1504 1505 for (i = 0; i < sp->typecnt; ++i) { 1506 ttisp = &sp->ttis[i]; 1507 if (ttisgmtcnt == 0) { 1508 ttisp->tt_ttisgmt = FALSE; 1509 } else { 1510 ttisp->tt_ttisgmt = *cp++; 1511 if (ttisp->tt_ttisgmt != TRUE && 1512 ttisp->tt_ttisgmt != FALSE) { 1513 lfree(bufp, flen); 1514 return (-1); 1515 } 1516 } 1517 } 1518 1519 /* 1520 * Other defaults set at beginning of this routine 1521 * to cover case where zoneinfo file cannot be loaded 1522 */ 1523 sp->default_timezone = -sp->ttis[0].tt_gmtoff; 1524 sp->default_altzone = 0; 1525 sp->default_tzname0 = &sp->chars[0]; 1526 sp->default_tzname1 = _tz_spaces; 1527 1528 lfree(bufp, flen); 1529 1530 sp->zonerules = ZONEINFO; 1531 1532 return (0); 1533 } 1534 1535 /* 1536 * Given a POSIX section 8-style TZ string, fill in transition tables. 1537 * 1538 * Examples: 1539 * 1540 * TZ = PST8 or GMT0 1541 * Timecnt set to 0 and typecnt set to 1, reflecting std time only. 1542 * 1543 * TZ = PST8PDT or PST8PDT7 1544 * Create transition times by applying USA transitions from 1545 * Jan 1 of each year covering 1902-2038. POSIX offsets 1546 * as specified in the TZ are used to calculate the tt_gmtoff 1547 * for each of the two zones. If ommitted, DST defaults to 1548 * std. time minus one hour. 1549 * 1550 * TZ = <PST8>8PDT or <PST8>8<PDT9> 1551 * Quoted transition. The values in angled brackets are treated 1552 * as zone name text, not parsed as offsets. The offsets 1553 * occuring following the zonename section. In this way, 1554 * instead of PST being displayed for standard time, it could 1555 * be displayed as PST8 to give an indication of the offset 1556 * of that zone to GMT. 1557 * 1558 * TZ = GMT0BST, M3.5.0/1, M10.5.0/2 or GMT0BST, J23953, J23989 1559 * Create transition times based on the application new-year 1560 * relative POSIX transitions, parsed from TZ, from Jan 1 1561 * for each year covering 1902-2038. POSIX offsets specified 1562 * in TZ are used to calculate tt_gmtoff for each of the two 1563 * zones. 1564 * 1565 */ 1566 static int 1567 load_posixinfo(const char *name, state_t *sp) 1568 { 1569 const char *stdname; 1570 const char *dstname = 0; 1571 size_t stdlen; 1572 size_t dstlen; 1573 long stdoff = 0; 1574 long dstoff = 0; 1575 time_t *tranp; 1576 uchar_t *typep; 1577 prev_t *prevp; 1578 char *cp; 1579 int year; 1580 int i; 1581 long long janfirst; 1582 ttinfo_t *dst; 1583 ttinfo_t *std; 1584 int quoted; 1585 zone_rules_t zonetype; 1586 posix_daylight_t pdaylight; 1587 1588 zonetype = POSIX_USA; 1589 stdname = name; 1590 1591 if ((quoted = (*stdname == '<')) != 0) 1592 ++stdname; 1593 1594 /* Parse/extract STD zone name, len and GMT offset */ 1595 if (*name != '\0') { 1596 if ((name = getzname(name, quoted)) == NULL) 1597 return (-1); 1598 stdlen = name - stdname; 1599 if (*name == '>') 1600 ++name; 1601 if (*name == '\0' || stdlen < 1) { 1602 return (-1); 1603 } else { 1604 if ((name = getoffset(name, &stdoff)) == NULL) 1605 return (-1); 1606 } 1607 } 1608 1609 /* If DST specified in TZ, extract DST zone details */ 1610 if (*name != '\0') { 1611 1612 dstname = name; 1613 if ((quoted = (*dstname == '<')) != 0) 1614 ++dstname; 1615 if ((name = getzname(name, quoted)) == NULL) 1616 return (-1); 1617 dstlen = name - dstname; 1618 if (dstlen < 1) 1619 return (-1); 1620 if (*name == '>') 1621 ++name; 1622 if (*name != '\0' && *name != ',' && *name != ';') { 1623 if ((name = getoffset(name, &dstoff)) == NULL) 1624 return (-1); 1625 } else { 1626 dstoff = stdoff - SECSPERHOUR; 1627 } 1628 1629 /* If any present, extract POSIX transitions from TZ */ 1630 if (*name == ',' || *name == ';') { 1631 /* Backward compatibility using ';' separator */ 1632 int compat_flag = (*name == ';'); 1633 ++name; 1634 if ((name = getrule(name, &sp->start_rule, compat_flag)) 1635 == NULL) 1636 return (-1); 1637 if (*name++ != ',') 1638 return (-1); 1639 if ((name = getrule(name, &sp->end_rule, compat_flag)) 1640 == NULL) 1641 return (-1); 1642 if (*name != '\0') 1643 return (-1); 1644 zonetype = POSIX; 1645 } 1646 1647 /* 1648 * We know STD and DST zones are specified with this timezone 1649 * therefore the cache will be set up with 2 transitions per 1650 * year transitioning to their respective std and dst zones. 1651 */ 1652 sp->daylight = 1; 1653 sp->typecnt = 2; 1654 sp->timecnt = 272; 1655 1656 /* 1657 * Insert zone data from POSIX TZ into state table 1658 * The Olson public domain POSIX code sets up ttis[0] to be DST, 1659 * as we are doing here. It seems to be the correct behavior. 1660 * The US/Pacific zoneinfo file also lists DST as first type. 1661 */ 1662 dst = &sp->ttis[0]; 1663 dst->tt_gmtoff = -dstoff; 1664 dst->tt_isdst = 1; 1665 1666 std = &sp->ttis[1]; 1667 std->tt_gmtoff = -stdoff; 1668 std->tt_isdst = 0; 1669 1670 sp->prev[0].std = NULL; 1671 sp->prev[0].alt = NULL; 1672 1673 /* Create transition data based on POSIX TZ */ 1674 tranp = sp->ats; 1675 prevp = &sp->prev[1]; 1676 typep = sp->types; 1677 1678 /* 1679 * We only cache from 1902 to 2037 to avoid transistions 1680 * that wrap at the 32-bit boundries, since 1901 and 2038 1681 * are not full years in 32-bit time. The rough edges 1682 * will be handled as transition cache misses. 1683 */ 1684 1685 janfirst = JAN_01_1902; 1686 1687 pdaylight.rules[0] = &sp->start_rule; 1688 pdaylight.rules[1] = &sp->end_rule; 1689 pdaylight.offset[0] = stdoff; 1690 pdaylight.offset[1] = dstoff; 1691 1692 for (i = MAX_RULE_TABLE; i >= 0; i--) { 1693 if (zonetype == POSIX_USA) { 1694 pdaylight.rules[0] = 1695 (rule_t *)&__usa_rules[i].start; 1696 pdaylight.rules[1] = 1697 (rule_t *)&__usa_rules[i].end; 1698 } 1699 for (year = __usa_rules[i].s_year; 1700 year <= __usa_rules[i].e_year; 1701 year++) { 1702 int idx, ridx; 1703 idx = 1704 posix_daylight(&janfirst, year, &pdaylight); 1705 ridx = !idx; 1706 1707 /* 1708 * Two transitions per year. Since there are 1709 * only two zone types for this POSIX zone, 1710 * previous std and alt are always set to 1711 * &ttis[0] and &ttis[1]. 1712 */ 1713 *tranp++ = (time_t)pdaylight.rtime[idx]; 1714 *typep++ = idx; 1715 prevp->std = std; 1716 prevp->alt = dst; 1717 ++prevp; 1718 1719 *tranp++ = (time_t)pdaylight.rtime[ridx]; 1720 *typep++ = ridx; 1721 prevp->std = std; 1722 prevp->alt = dst; 1723 ++prevp; 1724 } 1725 } 1726 } else { /* DST wasn't specified in POSIX TZ */ 1727 1728 /* Since we only have STD time, there are no transitions */ 1729 dstlen = 0; 1730 sp->daylight = 0; 1731 sp->typecnt = 1; 1732 sp->timecnt = 0; 1733 std = &sp->ttis[0]; 1734 std->tt_gmtoff = -stdoff; 1735 std->tt_isdst = 0; 1736 1737 } 1738 1739 /* Setup zone name character data for state table */ 1740 sp->charcnt = (int)(stdlen + 1); 1741 if (dstlen != 0) 1742 sp->charcnt += dstlen + 1; 1743 1744 /* If bigger than zone name abbv. buffer, grow it */ 1745 if ((size_t)sp->charcnt > sp->charsbuf_size) { 1746 if ((cp = libc_realloc(sp->chars, sp->charcnt)) == NULL) 1747 return (-1); 1748 sp->chars = cp; 1749 sp->charsbuf_size = sp->charcnt; 1750 } 1751 1752 /* 1753 * Copy zone name text null-terminatedly into state table. 1754 * By doing the copy once during zone loading, setting 1755 * tzname[] subsequently merely involves setting pointer 1756 * 1757 * If either or both std. or alt. zone name < 3 chars, 1758 * space pad the deficient name(s) to right. 1759 */ 1760 1761 std->tt_abbrind = 0; 1762 cp = sp->chars; 1763 (void) strncpy(cp, stdname, stdlen); 1764 while (stdlen < 3) 1765 cp[stdlen++] = ' '; 1766 cp[stdlen] = '\0'; 1767 1768 i = (int)(stdlen + 1); 1769 if (dstlen != 0) { 1770 dst->tt_abbrind = i; 1771 cp += i; 1772 (void) strncpy(cp, dstname, dstlen); 1773 while (dstlen < 3) 1774 cp[dstlen++] = ' '; 1775 cp[dstlen] = '\0'; 1776 } 1777 1778 /* Save default values */ 1779 if (sp->typecnt == 1) { 1780 sp->default_timezone = stdoff; 1781 sp->default_altzone = stdoff; 1782 sp->default_tzname0 = &sp->chars[0]; 1783 sp->default_tzname1 = _tz_spaces; 1784 } else { 1785 sp->default_timezone = -std->tt_gmtoff; 1786 sp->default_altzone = -dst->tt_gmtoff; 1787 sp->default_tzname0 = &sp->chars[std->tt_abbrind]; 1788 sp->default_tzname1 = &sp->chars[dst->tt_abbrind]; 1789 } 1790 1791 sp->zonerules = zonetype; 1792 1793 return (0); 1794 } 1795 1796 1797 /* 1798 * Given a pointer into a time zone string, scan until a character that is not 1799 * a valid character in a zone name is found. Return ptr to that character. 1800 * Return NULL if error (ie. non-printable character located in name) 1801 */ 1802 static const char * 1803 getzname(const char *strp, int quoted) 1804 { 1805 char c; 1806 1807 if (quoted) { 1808 while ((c = *strp) != '\0' && c != '>' && 1809 isgraph((unsigned char)c)) 1810 ++strp; 1811 } else { 1812 while ((c = *strp) != '\0' && isgraph((unsigned char)c) && 1813 !isdigit((unsigned char)c) && c != ',' && c != '-' && 1814 c != '+') 1815 ++strp; 1816 } 1817 1818 /* Found an excessively invalid character. Discredit whole name */ 1819 if (c != '\0' && !isgraph((unsigned char)c)) 1820 return (NULL); 1821 1822 return (strp); 1823 } 1824 1825 /* 1826 * Given pointer into time zone string, extract first 1827 * number pointed to. Validate number within range specified, 1828 * Return ptr to first char following valid numeric sequence. 1829 */ 1830 static const char * 1831 getnum(const char *strp, int *nump, int min, int max) 1832 { 1833 char c; 1834 int num; 1835 1836 if (strp == NULL || !isdigit((unsigned char)(c = *strp))) 1837 return (NULL); 1838 num = 0; 1839 do { 1840 num = num * 10 + (c - '0'); 1841 if (num > max) 1842 return (NULL); /* illegal value */ 1843 c = *++strp; 1844 } while (isdigit((unsigned char)c)); 1845 if (num < min) 1846 return (NULL); /* illegal value */ 1847 *nump = num; 1848 return (strp); 1849 } 1850 1851 /* 1852 * Given a pointer into a time zone string, extract a number of seconds, 1853 * in hh[:mm[:ss]] form, from the string. If an error occurs, return NULL, 1854 * otherwise, return a pointer to the first character not part of the number 1855 * of seconds. 1856 */ 1857 static const char * 1858 getsecs(const char *strp, long *secsp) 1859 { 1860 int num; 1861 1862 /* 1863 * `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like 1864 * "M10.4.6/26", which does not conform to Posix, 1865 * but which specifies the equivalent of 1866 * ``02:00 on the first Sunday on or after 23 Oct''. 1867 */ 1868 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1); 1869 if (strp == NULL) 1870 return (NULL); 1871 *secsp = num * (long)SECSPERHOUR; 1872 if (*strp == ':') { 1873 ++strp; 1874 strp = getnum(strp, &num, 0, MINSPERHOUR - 1); 1875 if (strp == NULL) 1876 return (NULL); 1877 *secsp += num * SECSPERMIN; 1878 if (*strp == ':') { 1879 ++strp; 1880 /* `SECSPERMIN' allows for leap seconds. */ 1881 strp = getnum(strp, &num, 0, SECSPERMIN); 1882 if (strp == NULL) 1883 return (NULL); 1884 *secsp += num; 1885 } 1886 } 1887 return (strp); 1888 } 1889 1890 /* 1891 * Given a pointer into a time zone string, extract an offset, in 1892 * [+-]hh[:mm[:ss]] form, from the string. 1893 * If any error occurs, return NULL. 1894 * Otherwise, return a pointer to the first character not part of the time. 1895 */ 1896 static const char * 1897 getoffset(const char *strp, long *offsetp) 1898 { 1899 int neg = 0; 1900 1901 if (*strp == '-') { 1902 neg = 1; 1903 ++strp; 1904 } else if (*strp == '+') { 1905 ++strp; 1906 } 1907 strp = getsecs(strp, offsetp); 1908 if (strp == NULL) 1909 return (NULL); /* illegal time */ 1910 if (neg) 1911 *offsetp = -*offsetp; 1912 return (strp); 1913 } 1914 1915 /* 1916 * Given a pointer into a time zone string, extract a rule in the form 1917 * date[/time]. See POSIX section 8 for the format of "date" and "time". 1918 * If a valid rule is not found, return NULL. 1919 * Otherwise, return a pointer to the first character not part of the rule. 1920 * 1921 * If compat_flag is set, support old 1-based day of year values. 1922 */ 1923 static const char * 1924 getrule(const char *strp, rule_t *rulep, int compat_flag) 1925 { 1926 if (compat_flag == 0 && *strp == 'M') { 1927 /* 1928 * Month, week, day. 1929 */ 1930 rulep->r_type = MON_WEEK_DOW; 1931 ++strp; 1932 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR); 1933 if (strp == NULL) 1934 return (NULL); 1935 if (*strp++ != '.') 1936 return (NULL); 1937 strp = getnum(strp, &rulep->r_week, 1, 5); 1938 if (strp == NULL) 1939 return (NULL); 1940 if (*strp++ != '.') 1941 return (NULL); 1942 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1); 1943 } else if (compat_flag == 0 && *strp == 'J') { 1944 /* 1945 * Julian day. 1946 */ 1947 rulep->r_type = JULIAN_DAY; 1948 ++strp; 1949 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR); 1950 1951 } else if (isdigit((unsigned char)*strp)) { 1952 /* 1953 * Day of year. 1954 */ 1955 rulep->r_type = DAY_OF_YEAR; 1956 if (compat_flag == 0) { 1957 /* zero-based day of year */ 1958 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1); 1959 } else { 1960 /* one-based day of year */ 1961 strp = getnum(strp, &rulep->r_day, 1, DAYSPERLYEAR); 1962 rulep->r_day--; 1963 } 1964 } else { 1965 return (NULL); /* ZONERULES_INVALID format */ 1966 } 1967 if (strp == NULL) 1968 return (NULL); 1969 if (*strp == '/') { 1970 /* 1971 * Time specified. 1972 */ 1973 ++strp; 1974 strp = getsecs(strp, &rulep->r_time); 1975 } else { 1976 rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */ 1977 } 1978 return (strp); 1979 } 1980 1981 /* 1982 * Returns default value for TZ as specified in /etc/default/init file, if 1983 * a default value for TZ is provided there. 1984 * 1985 * To strip quotes: if a '"', or '\''is found, then transfer the following 1986 * bytes forward, and overwrite the double or single quote. The tzS 1987 * pointer is used to keep track of the location in the buffer, to which bytes 1988 * pointed to by tzq, will be transferred. Then, the <value> string 1989 * should be NULL terminated. If no '"' or '\'' characters were encountered, 1990 * tzS will be NULL, so tzq should already be NULL-terminated. 1991 * However, if '"' or '\'' characters were encountered, then tzS will 1992 * be non-NULL, and *tzS should be set to the NULL character. 1993 */ 1994 static char * 1995 get_default_tz(void) 1996 { 1997 char *tz; 1998 int in; 1999 2000 tz = NULL; 2001 in = open(TIMEZONE, O_RDONLY); 2002 if (in != -1) { 2003 int eof = 0; 2004 char tzFilebuf[BUFSIZ+1]; 2005 char *p, *q; 2006 size_t bufsize; 2007 2008 p = q = tzFilebuf; 2009 bufsize = BUFSIZ; 2010 for (;;) { 2011 char *lineE, *nextp; 2012 size_t mlen; 2013 int r; 2014 2015 if (eof == 0) { 2016 r = read(in, q, bufsize); 2017 if (r <= 0) 2018 break; 2019 if (r < bufsize) 2020 eof = 1; 2021 *(q + r) = '\0'; 2022 } 2023 2024 if ((lineE = strchr(p, '\n')) == NULL) { 2025 /* line too long */ 2026 break; 2027 } 2028 *lineE = '\0'; 2029 2030 if (strncmp(TZSTRING, p, sizeof (TZSTRING) - 1) == 0) { 2031 unsigned char *tzp, *tzq, *tzS; 2032 2033 tzp = (unsigned char *)p + 2034 sizeof (TZSTRING) - 1; 2035 while (isspace(*tzp)) 2036 tzp++; 2037 tzq = tzp; 2038 tzS = NULL; 2039 while (isspace(*tzq) == 0 && 2040 *tzq != ';' && 2041 *tzq != '#' && 2042 *tzq != '\0') { 2043 if (*tzq == '"' || 2044 *tzq == '\'') { 2045 if (tzS == NULL) { 2046 tzS = tzq; 2047 } 2048 } else { 2049 if (tzS != NULL) { 2050 *tzS = *tzq; 2051 tzS++; 2052 } 2053 } 2054 tzq++; 2055 } 2056 if (tzS != NULL) 2057 *tzS = '\0'; 2058 else 2059 *tzq = '\0'; 2060 tz = strdup((char *)tzp); 2061 break; 2062 } 2063 nextp = lineE + 1; 2064 if (eof == 0) { 2065 mlen = (q + r) - nextp; 2066 (void) memmove(p, nextp, mlen); 2067 q = p + mlen; 2068 bufsize = BUFSIZ - mlen; 2069 } else { 2070 p = nextp; 2071 } 2072 } 2073 (void) close(in); 2074 } 2075 return (tz); 2076 } 2077 2078 static state_t * 2079 get_zone(systemtz_t *tzp) 2080 { 2081 int hashid; 2082 state_t *m, *p; 2083 const char *zonename = tzp->tz; 2084 2085 hashid = get_hashid(zonename); 2086 m = tzcache[hashid]; 2087 while (m) { 2088 int r; 2089 r = strcmp(m->zonename, zonename); 2090 if (r == 0) { 2091 /* matched */ 2092 return (NULL); 2093 } else if (r > 0) { 2094 break; 2095 } 2096 m = m->next; 2097 } 2098 /* malloc() return value is also checked for NULL in ltzset_u() */ 2099 p = malloc(sizeof (state_t)); 2100 2101 /* ltzset_u() resets the free flag to 0 if it uses the p buffer */ 2102 if (p != NULL) 2103 tzp->flag = 1; 2104 return (p); 2105 } 2106 2107 /* 2108 * getsystemTZ() returns the TZ value if it is set in the environment, or 2109 * it returns the system TZ; if the systemTZ has not yet been set, 2110 * get_default_tz() is called to read the /etc/default/init file to get 2111 * the value. 2112 * 2113 * getsystemTZ() also calls get_zone() to do an initial check to see if the 2114 * timezone is the current timezone, or one that is already loaded in the 2115 * hash table. If get_zone() determines the timezone has not yet been loaded, 2116 * it pre-allocates a buffer for a state_t struct, which ltzset_u() can use 2117 * later to load the timezone and add to the hash table. 2118 * 2119 * The large state_t buffer is allocated here to avoid calls to malloc() 2120 * within mutex_locks. 2121 */ 2122 static systemtz_t * 2123 getsystemTZ(systemtz_t *stzp) 2124 { 2125 static const char *systemTZ = NULL; 2126 char *tz; 2127 2128 assert_no_libc_locks_held(); 2129 2130 stzp->flag = 0; 2131 2132 tz = getenv("TZ"); 2133 if (tz != NULL && *tz != '\0') { 2134 stzp->tz = (const char *)tz; 2135 goto get_entry; 2136 } 2137 2138 if (systemTZ != NULL) { 2139 stzp->tz = systemTZ; 2140 goto get_entry; 2141 } 2142 2143 tz = get_default_tz(); 2144 lmutex_lock(&_time_lock); 2145 if (systemTZ == NULL) { 2146 if ((systemTZ = tz) != NULL) /* found TZ entry in the file */ 2147 tz = NULL; 2148 else 2149 systemTZ = _posix_gmt0; /* no TZ entry in the file */ 2150 } 2151 lmutex_unlock(&_time_lock); 2152 2153 if (tz != NULL) /* someone beat us to it; free our entry */ 2154 free(tz); 2155 2156 stzp->tz = systemTZ; 2157 2158 get_entry: 2159 /* 2160 * The object referred to by the 1st 'namecache' 2161 * may be different from the one by the 2nd 'namecache' below. 2162 * But, it does not matter. The bottomline is at this point 2163 * 'namecache' points to non-NULL and whether the string pointed 2164 * to by 'namecache' is equivalent to stzp->tz or not. 2165 */ 2166 if (namecache != NULL && strcmp(namecache, stzp->tz) == 0) { 2167 /* 2168 * At this point, we found the entry having the same 2169 * zonename as stzp->tz exists. Later we will find 2170 * the exact one, so we don't need to allocate 2171 * the memory here. 2172 */ 2173 stzp->entry = NULL; 2174 } else { 2175 /* 2176 * At this point, we could not get the evidence that this 2177 * zonename had been cached. We will look into the cache 2178 * further. 2179 */ 2180 stzp->entry = get_zone(stzp); 2181 } 2182 return (stzp); 2183 } 2184