]> git.saurik.com Git - apple/libc.git/blob - stdtime/FreeBSD/localtime.c
Libc-1244.50.9.tar.gz
[apple/libc.git] / stdtime / FreeBSD / localtime.c
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
4 */
5
6 #include <sys/cdefs.h>
7 #ifndef lint
8 #ifndef NOID
9 static char elsieid[] __unused = "@(#)localtime.c 8.14";
10 #endif /* !defined NOID */
11 #endif /* !defined lint */
12 __FBSDID("$FreeBSD: head/contrib/tzcode/stdtime/localtime.c 289027 2015-10-08 11:42:15Z rodrigc $");
13
14 /*
15 ** Leap second handling from Bradley White.
16 ** POSIX-style TZ environment variable handling from Guy Harris.
17 */
18
19 /*LINTLIBRARY*/
20
21 #include "namespace.h"
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <errno.h>
25 #include <time.h>
26 #include <fcntl.h>
27 #include <pthread.h>
28 #ifdef NOTIFY_TZ
29 //#define NOTIFY_TZ_DEBUG
30 //#define NOTIFY_TZ_DEBUG_FILE "/var/log/localtime.debug"
31 //#define NOTIFY_TZ_LOG "/var/log/localtime.log"
32 /* force ALL_STATE if NOTIFY_TZ is set */
33 #ifndef ALL_STATE
34 #define ALL_STATE
35 #endif /* ALL_STATE */
36 #include <mach/mach_init.h>
37 #include <notify.h>
38 #include <alloca.h>
39 #endif /* NOTIFY_TZ */
40 #include "private.h"
41 #include "un-namespace.h"
42
43 #include "tzfile.h"
44 #include "float.h" /* for FLT_MAX and DBL_MAX */
45
46 #ifndef TZ_ABBR_MAX_LEN
47 /* UNIX03 requires this to be the same as sysconf(_SC_TZNAME_MAX) */
48 #define TZ_ABBR_MAX_LEN 255
49 #endif /* !defined TZ_ABBR_MAX_LEN */
50
51 #ifndef TZ_ABBR_CHAR_SET
52 #define TZ_ABBR_CHAR_SET \
53 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
54 #endif /* !defined TZ_ABBR_CHAR_SET */
55
56 #ifndef TZ_ABBR_ERR_CHAR
57 #define TZ_ABBR_ERR_CHAR '_'
58 #endif /* !defined TZ_ABBR_ERR_CHAR */
59
60 #include "libc_private.h"
61
62 #define _MUTEX_LOCK(x) if (__isthreaded) _pthread_mutex_lock(x)
63 #define _MUTEX_UNLOCK(x) if (__isthreaded) _pthread_mutex_unlock(x)
64
65 #define _RWLOCK_RDLOCK(x) \
66 do { \
67 if (__isthreaded) _pthread_rwlock_rdlock(x); \
68 } while (0)
69
70 #define _RWLOCK_WRLOCK(x) \
71 do { \
72 if (__isthreaded) _pthread_rwlock_wrlock(x); \
73 } while (0)
74
75 #define _RWLOCK_UNLOCK(x) \
76 do { \
77 if (__isthreaded) _pthread_rwlock_unlock(x); \
78 } while (0)
79
80 /*
81 ** SunOS 4.1.1 headers lack O_BINARY.
82 */
83
84 #ifdef O_BINARY
85 #define OPEN_MODE (O_RDONLY | O_BINARY)
86 #endif /* defined O_BINARY */
87 #ifndef O_BINARY
88 #define OPEN_MODE O_RDONLY
89 #endif /* !defined O_BINARY */
90
91 #ifndef WILDABBR
92 /*
93 ** Someone might make incorrect use of a time zone abbreviation:
94 ** 1. They might reference tzname[0] before calling tzset (explicitly
95 ** or implicitly).
96 ** 2. They might reference tzname[1] before calling tzset (explicitly
97 ** or implicitly).
98 ** 3. They might reference tzname[1] after setting to a time zone
99 ** in which Daylight Saving Time is never observed.
100 ** 4. They might reference tzname[0] after setting to a time zone
101 ** in which Standard Time is never observed.
102 ** 5. They might reference tm.TM_ZONE after calling offtime.
103 ** What's best to do in the above cases is open to debate;
104 ** for now, we just set things up so that in any of the five cases
105 ** WILDABBR is used. Another possibility: initialize tzname[0] to the
106 ** string "tzname[0] used before set", and similarly for the other cases.
107 ** And another: initialize tzname[0] to "ERA", with an explanation in the
108 ** manual page of what this "time zone abbreviation" means (doing this so
109 ** that tzname[0] has the "normal" length of three characters).
110 */
111 #define WILDABBR " "
112 #endif /* !defined WILDABBR */
113
114 __used static const char wildabbr[] = WILDABBR;
115
116 /*
117 * In June 2004 it was decided UTC was a more appropriate default time
118 * zone than GMT.
119 */
120
121 __used static const char gmt[] = "UTC";
122
123 /*
124 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
125 ** We default to US rules as of 1999-08-17.
126 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
127 ** implementation dependent; for historical reasons, US rules are a
128 ** common default.
129 */
130 #ifndef TZDEFRULESTRING
131 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
132 #endif /* !defined TZDEFDST */
133
134 struct ttinfo { /* time type information */
135 long tt_gmtoff; /* UTC offset in seconds */
136 int tt_isdst; /* used to set tm_isdst */
137 int tt_abbrind; /* abbreviation list index */
138 int tt_ttisstd; /* TRUE if transition is std time */
139 int tt_ttisgmt; /* TRUE if transition is UTC */
140 };
141
142 struct lsinfo { /* leap second information */
143 time_t ls_trans; /* transition time */
144 long ls_corr; /* correction to apply */
145 };
146
147 #define BIGGEST(a, b) (((a) > (b)) ? (a) : (b))
148
149 #ifdef TZNAME_MAX
150 #define MY_TZNAME_MAX TZNAME_MAX
151 #endif /* defined TZNAME_MAX */
152 #ifndef TZNAME_MAX
153 #define MY_TZNAME_MAX 255
154 #endif /* !defined TZNAME_MAX */
155
156 struct state {
157 int leapcnt;
158 int timecnt;
159 int typecnt;
160 int charcnt;
161 int goback;
162 int goahead;
163 time_t ats[TZ_MAX_TIMES];
164 unsigned char types[TZ_MAX_TIMES];
165 struct ttinfo ttis[TZ_MAX_TYPES];
166 char chars[BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
167 (2 * (MY_TZNAME_MAX + 1)))];
168 struct lsinfo lsis[TZ_MAX_LEAPS];
169 };
170
171 struct rule {
172 int r_type; /* type of rule--see below */
173 int r_day; /* day number of rule */
174 int r_week; /* week number of rule */
175 int r_mon; /* month number of rule */
176 long r_time; /* transition time of rule */
177 };
178
179 #define JULIAN_DAY 0 /* Jn - Julian day */
180 #define DAY_OF_YEAR 1 /* n - day of year */
181 #define MONTH_NTH_DAY_OF_WEEK 2 /* Mm.n.d - month, week, day of week */
182
183 #ifdef NOTIFY_TZ
184 typedef struct {
185 int token;
186 int is_set;
187 } notify_tz_t;
188
189 #define NOTIFY_TZ_NAME "com.apple.system.timezone"
190 #endif /* NOTIFY_TZ */
191
192 /*
193 ** Prototypes for static functions.
194 */
195 #define localsub _st_localsub
196 #define time1 _st_time1
197 #define tzset_basic _st_tzset_basic
198 __private_extern__
199 #ifdef __LP64__
200 struct tm * localsub(const time_t * timep, long offset,
201 struct tm * tmp);
202 #else /* !__LP64__ */
203 void localsub(const time_t * timep, long offset,
204 struct tm * tmp);
205 #endif /* __LP64__ */
206 __private_extern__
207 time_t time1(struct tm * tmp,
208 #ifdef __LP64__
209 struct tm *(*funcp) (const time_t *,
210 long, struct tm *),
211 #else /* !__LP64__ */
212 void(*funcp) (const time_t *,
213 long, struct tm *),
214 #endif /* __LP64__ */
215 long offset,
216 int unix03);
217 __private_extern__
218 void tzset_basic(int);
219
220 #if !BUILDING_VARIANT
221 static long detzcode(const char * codep);
222 static time_t detzcode64(const char * codep);
223 static int differ_by_repeat(time_t t1, time_t t0);
224 static const char * getzname(const char * strp, char **name, size_t *len);
225 static const char * getqzname(const char * strp, const int delim)
226 ATTRIBUTE_PURE;
227 static const char * getnum(const char * strp, int * nump, int min,
228 int max);
229 static const char * getsecs(const char * strp, long * secsp);
230 static const char * getoffset(const char * strp, long * offsetp);
231 static const char * getrule(const char * strp, struct rule * rulep);
232 #ifdef NOTIFY_TZ
233 static void gmtload(struct state * sp, char *path);
234 #else /* ! NOTIFY_TZ */
235 static void gmtload(struct state * sp);
236 #endif /* NOTIFY_TZ */
237 #ifdef __LP64__
238 static struct tm * gmtsub(const time_t * timep, long offset,
239 struct tm * tmp);
240 #else /* !__LP64__ */
241 static void gmtsub(const time_t * timep, long offset,
242 struct tm * tmp);
243 #endif /* __LP64__ */
244 static int increment_overflow(int * number, int delta);
245 static int leaps_thru_end_of(int y) ATTRIBUTE_PURE;
246 static int long_increment_overflow(long * number, int delta);
247 static int long_normalize_overflow(long * tensptr,
248 int * unitsptr, int base);
249 static int normalize_overflow(int * tensptr, int * unitsptr,
250 int base);
251 #ifdef NOTIFY_TZ
252 static void notify_check_tz(notify_tz_t *p);
253 static void notify_register_tz(char *file, notify_tz_t *p);
254 #endif /* NOTIFY_TZ */
255 static void settzname(void);
256 static time_t time2(struct tm *tmp,
257 #ifdef __LP64__
258 struct tm *(*funcp) (const time_t *,
259 long, struct tm*),
260 #else /* !__LP64__ */
261 void(*funcp) (const time_t *,
262 long, struct tm*),
263 #endif /* __LP64__ */
264 long offset, int * okayp, int unix03);
265 static time_t time2sub(struct tm *tmp,
266 #ifdef __LP64__
267 struct tm *(*funcp) (const time_t *,
268 long, struct tm*),
269 #else /* !__LP64__ */
270 void(*funcp) (const time_t *,
271 long, struct tm*),
272 #endif /* __LP64__ */
273 long offset, int * okayp, int do_norm_secs,
274 int unix03);
275 #ifdef __LP64__
276 static struct tm * timesub(const time_t * timep, long offset,
277 const struct state * sp, struct tm * tmp);
278 #else /* !__LP64__ */
279 static void timesub(const time_t * timep, long offset,
280 const struct state * sp, struct tm * tmp);
281 #endif /* __LP64__ */
282 static int tmcomp(const struct tm * atmp,
283 const struct tm * btmp);
284 static time_t transtime(time_t janfirst, int year,
285 const struct rule * rulep, long offset)
286 ATTRIBUTE_PURE;
287 static int typesequiv(const struct state * sp, int a, int b);
288 #ifdef NOTIFY_TZ
289 static int tzload(const char * name, struct state * sp, char *path, int doextend);
290 #else /* ! NOTIFY_TZ */
291 static int tzload(const char * name, struct state * sp, int doextend);
292 #endif /* NOTIFY_TZ */
293 static int tzparse(const char * name, struct state * sp,
294 int lastditch);
295
296 #ifdef ALL_STATE
297 static struct state * lclptr;
298 static struct state * gmtptr;
299 #endif /* defined ALL_STATE */
300
301 #ifndef ALL_STATE
302 static struct state lclmem;
303 static struct state gmtmem;
304 #define lclptr (&lclmem)
305 #define gmtptr (&gmtmem)
306 #endif /* State Farm */
307
308 #ifndef TZ_STRLEN_MAX
309 #define TZ_STRLEN_MAX 255
310 #endif /* !defined TZ_STRLEN_MAX */
311
312 static char lcl_TZname[TZ_STRLEN_MAX + 1];
313 #ifdef NOTIFY_TZ
314 #define lcl_is_set (lcl_notify.is_set)
315 #define gmt_is_set (gmt_notify.is_set)
316 #else /* ! NOTIFY_TZ */
317 static int lcl_is_set;
318 #endif /* NOTIFY_TZ */
319 static pthread_once_t gmt_once = PTHREAD_ONCE_INIT;
320 __private_extern__ pthread_rwlock_t lcl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
321 static pthread_once_t gmtime_once = PTHREAD_ONCE_INIT;
322 static pthread_key_t gmtime_key;
323 static int gmtime_key_error;
324 static pthread_once_t localtime_once = PTHREAD_ONCE_INIT;
325 static pthread_key_t localtime_key;
326 static int localtime_key_error;
327
328 char * tzname[2] = {
329 (char *)wildabbr,
330 (char *)wildabbr
331 };
332
333 /*
334 ** Section 4.12.3 of X3.159-1989 requires that
335 ** Except for the strftime function, these functions [asctime,
336 ** ctime, gmtime, localtime] return values in one of two static
337 ** objects: a broken-down time structure and an array of char.
338 ** Thanks to Paul Eggert for noting this.
339 */
340
341 static struct tm tm;
342
343 #define USG_COMPAT
344 #define ALTZONE
345 #ifdef USG_COMPAT
346 int daylight = 0;
347 __private_extern__ void _st_set_timezone(long);
348 #endif /* defined USG_COMPAT */
349
350 #ifdef ALTZONE
351 __private_extern__ long __darwin_altzone = 0;
352 #define altzone __darwin_altzone
353 #endif /* defined ALTZONE */
354
355 #ifdef NOTIFY_TZ
356 #ifdef NOTIFY_TZ_DEBUG
357 #ifdef NOTIFY_TZ_DEBUG_FILE
358 #define NOTIFY_TZ_PRINTF(fmt, args...) \
359 { \
360 FILE *_notify_tz_fp_; \
361 if((_notify_tz_fp_ = fopen(NOTIFY_TZ_DEBUG_FILE, "a")) != NULL) { \
362 fprintf(_notify_tz_fp_, "%d: " fmt, getpid(), ## args); \
363 fclose(_notify_tz_fp_); \
364 } \
365 }
366 #else /* ! NOTIFY_TZ_DEBUG_FILE */
367 #define NOTIFY_TZ_PRINTF(args...) fprintf(stdout, ## args)
368 #endif /* NOTIFY_TZ_DEBUG_FILE */
369 #endif /* NOTIFY_TZ_DEBUG */
370 #ifdef NOTIFY_TZ_LOG
371 #define NOTIFY_LOG(fmt, args...) \
372 { \
373 FILE *_notify_log_fp_; \
374 if((_notify_log_fp_ = fopen(NOTIFY_TZ_LOG, "a")) != NULL) { \
375 fprintf(_notify_log_fp_, "%d: " fmt, getpid(), ## args); \
376 fclose(_notify_log_fp_); \
377 } \
378 }
379 #endif /* NOTIFY_TZ_LOG */
380
381 static notify_tz_t gmt_notify = {-1, 0};
382 static notify_tz_t lcl_notify = {-1, 0};
383 static const char notify_tz_name[] = NOTIFY_TZ_NAME;
384 #endif /* NOTIFY_TZ */
385
386 static long
387 detzcode(const char *const codep)
388 {
389 long result;
390 int i;
391
392 result = (codep[0] & 0x80) ? ~0L : 0;
393 for (i = 0; i < 4; ++i)
394 result = (result << 8) | (codep[i] & 0xff);
395 return result;
396 }
397
398 static time_t
399 detzcode64(const char *const codep)
400 {
401 register time_t result;
402 register int i;
403
404 result = (codep[0] & 0x80) ? (~(int_fast64_t) 0) : 0;
405 for (i = 0; i < 8; ++i)
406 result = result * 256 + (codep[i] & 0xff);
407 return result;
408 }
409
410 static void
411 settzname(void)
412 {
413 struct state * sp = lclptr;
414 int i, need;
415 unsigned char * types;
416 #define NEED_STD 1
417 #define NEED_DST 2
418 #define NEED_DAYLIGHT 4
419 #define NEED_ALL (NEED_STD | NEED_DST | NEED_DAYLIGHT)
420
421 tzname[0] = (char *)wildabbr;
422 tzname[1] = (char *)wildabbr;
423 #ifdef USG_COMPAT
424 daylight = 0;
425 _st_set_timezone(0);
426 #endif /* defined USG_COMPAT */
427 #ifdef ALTZONE
428 altzone = 0;
429 #endif /* defined ALTZONE */
430 #ifdef ALL_STATE
431 if (sp == NULL) {
432 tzname[0] = tzname[1] = (char *)gmt;
433 return;
434 }
435 #endif /* defined ALL_STATE */
436 /*
437 * PR-3765457: The original settzname went sequentially through the ttis
438 * array, rather than correctly indexing via the types array, to get
439 * the real order of the timezone changes. In addition, as a speed up,
440 * we start at the end of the changes, and work back, so that most of
441 * the time, we don't have to look through the entire array.
442 */
443 if (sp->timecnt == 0 && sp->typecnt == 1) {
444 /*
445 * Unfortunately, there is an edge case when typecnt == 1 and
446 * timecnt == 0, which would cause the loop to never run. So
447 * in that case, we fudge things up so that it is as if
448 * timecnt == 1.
449 */
450 i = 0;
451 types = (unsigned char *)""; /* we use the null as index */
452 } else {
453 /* the usual case */
454 i = sp->timecnt - 1;
455 types = sp->types;
456 }
457 need = NEED_ALL;
458 for (; i >= 0 && need; --i) {
459 const struct ttinfo * const ttisp = &sp->ttis[types[i]];
460
461 #ifdef USG_COMPAT
462 if ((need & NEED_DAYLIGHT) && ttisp->tt_isdst) {
463 need &= ~NEED_DAYLIGHT;
464 daylight = 1;
465 }
466 #endif /* defined USG_COMPAT */
467 if (ttisp->tt_isdst) {
468 if (need & NEED_DST) {
469 need &= ~NEED_DST;
470 tzname[1] = &sp->chars[ttisp->tt_abbrind];
471 #ifdef ALTZONE
472 altzone = -(ttisp->tt_gmtoff);
473 #endif /* defined ALTZONE */
474 }
475 } else if (need & NEED_STD) {
476 need &= ~NEED_STD;
477 tzname[0] = &sp->chars[ttisp->tt_abbrind];
478 #ifdef USG_COMPAT
479 _st_set_timezone(-(ttisp->tt_gmtoff));
480 #endif /* defined USG_COMPAT */
481 }
482 #if defined(ALTZONE) || defined(USG_COMPAT)
483 if (i == 0) {
484 #endif /* defined(ALTZONE) || defined(USG_COMPAT) */
485 #ifdef ALTZONE
486 if (need & NEED_DST)
487 altzone = -(ttisp->tt_gmtoff);
488 #endif /* defined ALTZONE */
489 #ifdef USG_COMPAT
490 if (need & NEED_STD)
491 _st_set_timezone(-(ttisp->tt_gmtoff));
492 #endif /* defined USG_COMPAT */
493 #if defined(ALTZONE) || defined(USG_COMPAT)
494 }
495 #endif /* defined(ALTZONE) || defined(USG_COMPAT) */
496 }
497 /*
498 ** Finally, scrub the abbreviations.
499 ** First, replace bogus characters.
500 */
501 for (i = 0; i < sp->charcnt; ++i)
502 if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
503 sp->chars[i] = TZ_ABBR_ERR_CHAR;
504 /*
505 ** Second, truncate long abbreviations.
506 */
507 for (i = 0; i < sp->typecnt; ++i) {
508 register const struct ttinfo * const ttisp = &sp->ttis[i];
509 register char * cp = &sp->chars[ttisp->tt_abbrind];
510
511 if (strlen(cp) > TZ_ABBR_MAX_LEN &&
512 strcmp(cp, GRANDPARENTED) != 0)
513 *(cp + TZ_ABBR_MAX_LEN) = '\0';
514 }
515 }
516
517 #ifdef NOTIFY_TZ
518 static void
519 notify_check_tz(notify_tz_t *p)
520 {
521 unsigned int nstat;
522 int ncheck;
523
524 if (p->token < 0)
525 return;
526 nstat = notify_check(p->token, &ncheck);
527 if (nstat || ncheck) {
528 p->is_set = 0;
529 #ifdef NOTIFY_TZ_DEBUG
530 NOTIFY_TZ_PRINTF("notify_check_tz: %s changed\n", (p == &lcl_notify) ? "lcl" : "gmt");
531 #endif /* NOTIFY_TZ_DEBUG */
532 }
533 #ifdef NOTIFY_TZ_DEBUG
534 NOTIFY_TZ_PRINTF("notify_check_tz: %s unchanged\n", (p == &lcl_notify) ? "lcl" : "gmt");
535 #endif /* NOTIFY_TZ_DEBUG */
536 }
537
538 extern uint32_t notify_monitor_file(int token, char *path, int flags);
539
540 static void
541 notify_register_tz(char *file, notify_tz_t *p)
542 {
543 char *name;
544 unsigned int nstat;
545 int ncheck;
546
547 /*----------------------------------------------------------------
548 * Since we don't record the last time zone filename, just cancel
549 * (which should remove the file monitor) and setup from scratch
550 *----------------------------------------------------------------*/
551 if (p->token >= 0)
552 notify_cancel(p->token);
553 if (!file || *file == 0) {
554 /* no time zone file to monitor */
555 p->token = -1;
556 return;
557 }
558 /*----------------------------------------------------------------
559 * Just use com.apple.system.timezone if the path is /etc/localtime.
560 * Otherwise use com.apple.system.timezone.<fullpath>
561 *----------------------------------------------------------------*/
562 if (TZDEFAULT && strcmp(file, TZDEFAULT) == 0)
563 name = (char *)notify_tz_name;
564 else {
565 name = alloca(sizeof(notify_tz_name) + strlen(file) + 1);
566 if (name == NULL) {
567 p->token = -1;
568 return;
569 }
570 strcpy(name, notify_tz_name);
571 strcat(name, ".");
572 strcat(name, file);
573 }
574 #ifdef NOTIFY_TZ_DEBUG
575 NOTIFY_TZ_PRINTF("notify_register_tz: file=%s name=%s\n", file, name);
576 #endif /* NOTIFY_TZ_DEBUG */
577 nstat = notify_register_check(name, &p->token);
578 if (nstat != 0) {
579 p->token = -1;
580 p->is_set = 0;
581 #ifdef NOTIFY_TZ_DEBUG
582 NOTIFY_TZ_PRINTF("***notify_register_tz: notify_register_check failed: %u\n", nstat);
583 #endif /* NOTIFY_TZ_DEBUG */
584 #ifdef NOTIFY_TZ_LOG
585 NOTIFY_LOG("notify_register_check(%s) failed: %u\n", name, nstat);
586 #endif /* NOTIFY_TZ_LOG */
587 return;
588 }
589 /* don't need to request monitoring /etc/localtime */
590 if (name != notify_tz_name) {
591 #ifdef NOTIFY_TZ_DEBUG
592 NOTIFY_TZ_PRINTF("notify_register_tz: monitor %s\n", file);
593 #endif /* NOTIFY_TZ_DEBUG */
594 nstat = notify_monitor_file(p->token, file, 0);
595 if (nstat != 0) {
596 notify_cancel(p->token);
597 p->token = -1;
598 p->is_set = 0;
599 #ifdef NOTIFY_TZ_DEBUG
600 NOTIFY_TZ_PRINTF("***notify_register_tz: notify_monitor_file failed: %u\n", nstat);
601 #endif /* NOTIFY_TZ_DEBUG */
602 #ifdef NOTIFY_TZ_LOG
603 NOTIFY_LOG("notify_monitor_file(%s) failed: %u\n", file, nstat);
604 #endif /* NOTIFY_TZ_LOG */
605 return;
606 }
607 }
608 notify_check(p->token, &ncheck); /* this always returns true */
609 }
610 #endif /* NOTIFY_TZ */
611
612 static int
613 differ_by_repeat(const time_t t1, const time_t t0)
614 {
615 int_fast64_t _t0 = t0;
616 int_fast64_t _t1 = t1;
617
618 if (TYPE_INTEGRAL(time_t) &&
619 TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
620 return 0;
621 //turn ((int_fast64_t)(t1 - t0) == SECSPERREPEAT);
622 return _t1 - _t0 == SECSPERREPEAT;
623 }
624
625 static int
626 #ifdef NOTIFY_TZ
627 tzload(name, sp, path, doextend)
628 #else /* ! NOTIFY_TZ */
629 tzload(name, sp, doextend)
630 #endif /* NOTIFY_TZ */
631 const char * name;
632 struct state * const sp;
633 #ifdef NOTIFY_TZ
634 char * path; /* copy full path if non-NULL */
635 #endif /* NOTIFY_TZ */
636 register const int doextend;
637 {
638 const char * p;
639 int i;
640 int fid;
641 int stored;
642 int nread;
643 int res;
644 union {
645 struct tzhead tzhead;
646 char buf[2 * sizeof(struct tzhead) +
647 2 * sizeof *sp +
648 4 * TZ_MAX_TIMES];
649 } *u;
650
651 u = NULL;
652 res = -1;
653 sp->goback = sp->goahead = FALSE;
654
655 #ifdef NOTIFY_TZ_DEBUG
656 NOTIFY_TZ_PRINTF("tzload: name=%s\n", name);
657 #endif /* NOTIFY_TZ_DEBUG */
658 /* XXX The following is from OpenBSD, and I'm not sure it is correct */
659 if (name != NULL && issetugid() != 0)
660 if ((name[0] == ':' && name[1] == '/') ||
661 name[0] == '/' || strchr(name, '.'))
662 name = NULL;
663 #ifdef NOTIFY_TZ
664 if (path)
665 *path = 0; /* default to empty string on error */
666 #endif /* NOTIFY_TZ */
667 if (name == NULL && (name = TZDEFAULT) == NULL)
668 return -1;
669 {
670 int doaccess;
671 struct stat stab;
672 /*
673 ** Section 4.9.1 of the C standard says that
674 ** "FILENAME_MAX expands to an integral constant expression
675 ** that is the size needed for an array of char large enough
676 ** to hold the longest file name string that the implementation
677 ** guarantees can be opened."
678 */
679 char *fullname;
680
681 fullname = malloc(FILENAME_MAX + 1);
682 if (fullname == NULL)
683 goto out;
684
685 if (name[0] == ':')
686 ++name;
687 doaccess = name[0] == '/';
688 if (!doaccess) {
689 if ((p = TZDIR) == NULL) {
690 free(fullname);
691 return -1;
692 }
693 if (strlen(p) + 1 + strlen(name) >= FILENAME_MAX) {
694 free(fullname);
695 return -1;
696 }
697 (void) strcpy(fullname, p);
698 (void) strcat(fullname, "/");
699 (void) strcat(fullname, name);
700 /*
701 ** Set doaccess if '.' (as in "../") shows up in name.
702 */
703 if (strchr(name, '.') != NULL)
704 doaccess = TRUE;
705 name = fullname;
706 }
707 #ifdef NOTIFY_TZ
708 if (path) {
709 if (strlen(name) > FILENAME_MAX)
710 return -1;
711 strcpy(path, name);
712 }
713 #endif /* NOTIFY_TZ */
714 if (doaccess && access(name, R_OK) != 0) {
715 free(fullname);
716 return -1;
717 }
718 if ((fid = _open(name, OPEN_MODE)) == -1) {
719 free(fullname);
720 return -1;
721 }
722 if ((_fstat(fid, &stab) < 0) || !S_ISREG(stab.st_mode)) {
723 free(fullname);
724 _close(fid);
725 return -1;
726 }
727 free(fullname);
728 }
729 u = malloc(sizeof(*u));
730 if (u == NULL)
731 goto out;
732 #ifdef NOTIFY_TZ_DEBUG
733 NOTIFY_TZ_PRINTF("tzload: reading %s\n", name);
734 #endif /* NOTIFY_TZ_DEBUG */
735 nread = _read(fid, u->buf, sizeof u->buf);
736 if (_close(fid) < 0 || nread <= 0)
737 goto out;
738 for (stored = 4; stored <= 8; stored *= 2) {
739 int ttisstdcnt;
740 int ttisgmtcnt;
741
742 ttisstdcnt = (int) detzcode(u->tzhead.tzh_ttisstdcnt);
743 ttisgmtcnt = (int) detzcode(u->tzhead.tzh_ttisgmtcnt);
744 sp->leapcnt = (int) detzcode(u->tzhead.tzh_leapcnt);
745 sp->timecnt = (int) detzcode(u->tzhead.tzh_timecnt);
746 sp->typecnt = (int) detzcode(u->tzhead.tzh_typecnt);
747 sp->charcnt = (int) detzcode(u->tzhead.tzh_charcnt);
748 p = u->tzhead.tzh_charcnt + sizeof u->tzhead.tzh_charcnt;
749 if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
750 sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
751 sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
752 sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
753 (ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
754 (ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
755 goto out;
756 if (nread - (p - u->buf) <
757 sp->timecnt * stored + /* ats */
758 sp->timecnt + /* types */
759 sp->typecnt * 6 + /* ttinfos */
760 sp->charcnt + /* chars */
761 sp->leapcnt * (stored + 4) + /* lsinfos */
762 ttisstdcnt + /* ttisstds */
763 ttisgmtcnt) /* ttisgmts */
764 goto out;
765 for (i = 0; i < sp->timecnt; ++i) {
766 sp->ats[i] = (stored == 4) ?
767 detzcode(p) : detzcode64(p);
768 p += stored;
769 }
770 for (i = 0; i < sp->timecnt; ++i) {
771 sp->types[i] = (unsigned char) *p++;
772 if (sp->types[i] >= sp->typecnt)
773 goto out;
774 }
775 for (i = 0; i < sp->typecnt; ++i) {
776 struct ttinfo * ttisp;
777
778 ttisp = &sp->ttis[i];
779 ttisp->tt_gmtoff = detzcode(p);
780 p += 4;
781 ttisp->tt_isdst = (unsigned char) *p++;
782 if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
783 goto out;
784 ttisp->tt_abbrind = (unsigned char) *p++;
785 if (ttisp->tt_abbrind < 0 ||
786 ttisp->tt_abbrind > sp->charcnt)
787 goto out;
788 }
789 for (i = 0; i < sp->charcnt; ++i)
790 sp->chars[i] = *p++;
791 sp->chars[i] = '\0'; /* ensure '\0' at end */
792 for (i = 0; i < sp->leapcnt; ++i) {
793 struct lsinfo * lsisp;
794
795 lsisp = &sp->lsis[i];
796 lsisp->ls_trans = (stored == 4) ?
797 detzcode(p) : detzcode64(p);
798 p += stored;
799 lsisp->ls_corr = detzcode(p);
800 p += 4;
801 }
802 for (i = 0; i < sp->typecnt; ++i) {
803 struct ttinfo * ttisp;
804
805 ttisp = &sp->ttis[i];
806 if (ttisstdcnt == 0)
807 ttisp->tt_ttisstd = FALSE;
808 else {
809 ttisp->tt_ttisstd = *p++;
810 if (ttisp->tt_ttisstd != TRUE &&
811 ttisp->tt_ttisstd != FALSE)
812 goto out;
813 }
814 }
815 for (i = 0; i < sp->typecnt; ++i) {
816 struct ttinfo * ttisp;
817
818 ttisp = &sp->ttis[i];
819 if (ttisgmtcnt == 0)
820 ttisp->tt_ttisgmt = FALSE;
821 else {
822 ttisp->tt_ttisgmt = *p++;
823 if (ttisp->tt_ttisgmt != TRUE &&
824 ttisp->tt_ttisgmt != FALSE)
825 goto out;
826 }
827 }
828 /*
829 ** Out-of-sort ats should mean we're running on a
830 ** signed time_t system but using a data file with
831 ** unsigned values (or vice versa).
832 */
833 for (i = 0; i < sp->timecnt - 2; ++i)
834 if (sp->ats[i] > sp->ats[i + 1]) {
835 ++i;
836 if (TYPE_SIGNED(time_t)) {
837 /*
838 ** Ignore the end (easy).
839 */
840 sp->timecnt = i;
841 } else {
842 /*
843 ** Ignore the beginning (harder).
844 */
845 register int j;
846
847 for (j = 0; j + i < sp->timecnt; ++j) {
848 sp->ats[j] = sp->ats[j + i];
849 sp->types[j] = sp->types[j + i];
850 }
851 sp->timecnt = j;
852 }
853 break;
854 }
855 /*
856 ** If this is an old file, we're done.
857 */
858 if (u->tzhead.tzh_version[0] == '\0')
859 break;
860 nread -= p - u->buf;
861 for (i = 0; i < nread; ++i)
862 u->buf[i] = p[i];
863 /*
864 ** If this is a narrow integer time_t system, we're done.
865 */
866 if (stored >= (int) sizeof(time_t) && TYPE_INTEGRAL(time_t))
867 break;
868 }
869 if (doextend && nread > 2 &&
870 u->buf[0] == '\n' && u->buf[nread - 1] == '\n' &&
871 sp->typecnt + 2 <= TZ_MAX_TYPES) {
872 struct state *ts;
873 register int result;
874
875 ts = malloc(sizeof(*ts));
876 if (ts == NULL)
877 goto out;
878 u->buf[nread - 1] = '\0';
879 result = tzparse(&u->buf[1], ts, FALSE);
880 if (result == 0 && ts->typecnt == 2 &&
881 sp->charcnt + ts->charcnt <= TZ_MAX_CHARS) {
882 for (i = 0; i < 2; ++i)
883 ts->ttis[i].tt_abbrind +=
884 sp->charcnt;
885 for (i = 0; i < ts->charcnt; ++i)
886 sp->chars[sp->charcnt++] =
887 ts->chars[i];
888 i = 0;
889 while (i < ts->timecnt &&
890 ts->ats[i] <=
891 sp->ats[sp->timecnt - 1])
892 ++i;
893 while (i < ts->timecnt &&
894 sp->timecnt < TZ_MAX_TIMES) {
895 sp->ats[sp->timecnt] =
896 ts->ats[i];
897 sp->types[sp->timecnt] =
898 sp->typecnt +
899 ts->types[i];
900 ++sp->timecnt;
901 ++i;
902 }
903 sp->ttis[sp->typecnt++] = ts->ttis[0];
904 sp->ttis[sp->typecnt++] = ts->ttis[1];
905 }
906 free(ts);
907 }
908 if (sp->timecnt > 1) {
909 for (i = 1; i < sp->timecnt; ++i)
910 if (typesequiv(sp, sp->types[i], sp->types[0]) &&
911 differ_by_repeat(sp->ats[i], sp->ats[0])) {
912 sp->goback = TRUE;
913 break;
914 }
915 for (i = sp->timecnt - 2; i >= 0; --i)
916 if (typesequiv(sp, sp->types[sp->timecnt - 1],
917 sp->types[i]) &&
918 differ_by_repeat(sp->ats[sp->timecnt - 1],
919 sp->ats[i])) {
920 sp->goahead = TRUE;
921 break;
922 }
923 }
924 res = 0;
925 out:
926 free(u);
927 return (res);
928 }
929
930 static int
931 typesequiv(sp, a, b)
932 const struct state * const sp;
933 const int a;
934 const int b;
935 {
936 register int result;
937
938 if (sp == NULL ||
939 a < 0 || a >= sp->typecnt ||
940 b < 0 || b >= sp->typecnt)
941 result = FALSE;
942 else {
943 register const struct ttinfo * ap = &sp->ttis[a];
944 register const struct ttinfo * bp = &sp->ttis[b];
945 result = ap->tt_gmtoff == bp->tt_gmtoff &&
946 ap->tt_isdst == bp->tt_isdst &&
947 ap->tt_ttisstd == bp->tt_ttisstd &&
948 ap->tt_ttisgmt == bp->tt_ttisgmt &&
949 strcmp(&sp->chars[ap->tt_abbrind],
950 &sp->chars[bp->tt_abbrind]) == 0;
951 }
952 return result;
953 }
954
955 static const int mon_lengths[2][MONSPERYEAR] = {
956 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
957 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
958 };
959
960 static const int year_lengths[2] = {
961 DAYSPERNYEAR, DAYSPERLYEAR
962 };
963
964 /*
965 ** Given a pointer into a time zone string, scan until a character that is not
966 ** a valid character in a zone name is found. Return a pointer to that
967 ** character.
968 */
969
970 static const char *
971 getzname(strp, name, len)
972 const char * strp;
973 char ** name;
974 size_t * len;
975 {
976 char c;
977 char * ket;
978
979 if (*strp == '<' && (ket = strchr(strp, '>')) != NULL) {
980 *name = (char *)(strp + 1);
981 *len = ket - strp - 1;
982 return ket + 1;
983 }
984 *name = (char *)strp;
985 while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
986 c != '+')
987 ++strp;
988 *len = strp - *name;
989 return strp;
990 }
991
992 /*
993 ** Given a pointer into an extended time zone string, scan until the ending
994 ** delimiter of the zone name is located. Return a pointer to the delimiter.
995 **
996 ** As with getzname above, the legal character set is actually quite
997 ** restricted, with other characters producing undefined results.
998 ** We don't do any checking here; checking is done later in common-case code.
999 */
1000
1001 static const char *
1002 getqzname(register const char *strp, const int delim)
1003 {
1004 register int c;
1005
1006 while ((c = *strp) != '\0' && c != delim)
1007 ++strp;
1008 return strp;
1009 }
1010
1011 /*
1012 ** Given a pointer into a time zone string, extract a number from that string.
1013 ** Check that the number is within a specified range; if it is not, return
1014 ** NULL.
1015 ** Otherwise, return a pointer to the first character not part of the number.
1016 */
1017
1018 static const char *
1019 getnum(strp, nump, min, max)
1020 const char * strp;
1021 int * const nump;
1022 const int min;
1023 const int max;
1024 {
1025 char c;
1026 int num;
1027
1028 if (strp == NULL || !is_digit(c = *strp))
1029 return NULL;
1030 num = 0;
1031 do {
1032 num = num * 10 + (c - '0');
1033 if (num > max)
1034 return NULL; /* illegal value */
1035 c = *++strp;
1036 } while (is_digit(c));
1037 if (num < min)
1038 return NULL; /* illegal value */
1039 *nump = num;
1040 return strp;
1041 }
1042
1043 /*
1044 ** Given a pointer into a time zone string, extract a number of seconds,
1045 ** in hh[:mm[:ss]] form, from the string.
1046 ** If any error occurs, return NULL.
1047 ** Otherwise, return a pointer to the first character not part of the number
1048 ** of seconds.
1049 */
1050
1051 static const char *
1052 getsecs(strp, secsp)
1053 const char * strp;
1054 long * const secsp;
1055 {
1056 int num;
1057
1058 /*
1059 ** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
1060 ** "M10.4.6/26", which does not conform to Posix,
1061 ** but which specifies the equivalent of
1062 ** ``02:00 on the first Sunday on or after 23 Oct''.
1063 */
1064 strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
1065 if (strp == NULL)
1066 return NULL;
1067 *secsp = num * (long) SECSPERHOUR;
1068 if (*strp == ':') {
1069 ++strp;
1070 strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
1071 if (strp == NULL)
1072 return NULL;
1073 *secsp += num * SECSPERMIN;
1074 if (*strp == ':') {
1075 ++strp;
1076 /* `SECSPERMIN' allows for leap seconds. */
1077 strp = getnum(strp, &num, 0, SECSPERMIN);
1078 if (strp == NULL)
1079 return NULL;
1080 *secsp += num;
1081 }
1082 }
1083 return strp;
1084 }
1085
1086 /*
1087 ** Given a pointer into a time zone string, extract an offset, in
1088 ** [+-]hh[:mm[:ss]] form, from the string.
1089 ** If any error occurs, return NULL.
1090 ** Otherwise, return a pointer to the first character not part of the time.
1091 */
1092
1093 static const char *
1094 getoffset(strp, offsetp)
1095 const char * strp;
1096 long * const offsetp;
1097 {
1098 int neg = 0;
1099
1100 if (*strp == '-') {
1101 neg = 1;
1102 ++strp;
1103 } else if (*strp == '+')
1104 ++strp;
1105 strp = getsecs(strp, offsetp);
1106 if (strp == NULL)
1107 return NULL; /* illegal time */
1108 if (neg)
1109 *offsetp = -*offsetp;
1110 return strp;
1111 }
1112
1113 /*
1114 ** Given a pointer into a time zone string, extract a rule in the form
1115 ** date[/time]. See POSIX section 8 for the format of "date" and "time".
1116 ** If a valid rule is not found, return NULL.
1117 ** Otherwise, return a pointer to the first character not part of the rule.
1118 */
1119
1120 static const char *
1121 getrule(strp, rulep)
1122 const char * strp;
1123 struct rule * const rulep;
1124 {
1125 if (*strp == 'J') {
1126 /*
1127 ** Julian day.
1128 */
1129 rulep->r_type = JULIAN_DAY;
1130 ++strp;
1131 strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
1132 } else if (*strp == 'M') {
1133 /*
1134 ** Month, week, day.
1135 */
1136 rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
1137 ++strp;
1138 strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
1139 if (strp == NULL)
1140 return NULL;
1141 if (*strp++ != '.')
1142 return NULL;
1143 strp = getnum(strp, &rulep->r_week, 1, 5);
1144 if (strp == NULL)
1145 return NULL;
1146 if (*strp++ != '.')
1147 return NULL;
1148 strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
1149 } else if (is_digit(*strp)) {
1150 /*
1151 ** Day of year.
1152 */
1153 rulep->r_type = DAY_OF_YEAR;
1154 strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
1155 } else return NULL; /* invalid format */
1156 if (strp == NULL)
1157 return NULL;
1158 if (*strp == '/') {
1159 /*
1160 ** Time specified.
1161 */
1162 ++strp;
1163 strp = getsecs(strp, &rulep->r_time);
1164 } else rulep->r_time = 2 * SECSPERHOUR; /* default = 2:00:00 */
1165 return strp;
1166 }
1167
1168 /*
1169 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
1170 ** year, a rule, and the offset from UTC at the time that rule takes effect,
1171 ** calculate the Epoch-relative time that rule takes effect.
1172 */
1173
1174 static time_t
1175 transtime(janfirst, year, rulep, offset)
1176 const time_t janfirst;
1177 const int year;
1178 const struct rule * const rulep;
1179 const long offset;
1180 {
1181 int leapyear;
1182 time_t value;
1183 int i;
1184 int d, m1, yy0, yy1, yy2, dow;
1185
1186 INITIALIZE(value);
1187 leapyear = isleap(year);
1188 switch (rulep->r_type) {
1189
1190 case JULIAN_DAY:
1191 /*
1192 ** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
1193 ** years.
1194 ** In non-leap years, or if the day number is 59 or less, just
1195 ** add SECSPERDAY times the day number-1 to the time of
1196 ** January 1, midnight, to get the day.
1197 */
1198 value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
1199 if (leapyear && rulep->r_day >= 60)
1200 value += SECSPERDAY;
1201 break;
1202
1203 case DAY_OF_YEAR:
1204 /*
1205 ** n - day of year.
1206 ** Just add SECSPERDAY times the day number to the time of
1207 ** January 1, midnight, to get the day.
1208 */
1209 value = janfirst + rulep->r_day * SECSPERDAY;
1210 break;
1211
1212 case MONTH_NTH_DAY_OF_WEEK:
1213 /*
1214 ** Mm.n.d - nth "dth day" of month m.
1215 */
1216 value = janfirst;
1217 for (i = 0; i < rulep->r_mon - 1; ++i)
1218 value += mon_lengths[leapyear][i] * SECSPERDAY;
1219
1220 /*
1221 ** Use Zeller's Congruence to get day-of-week of first day of
1222 ** month.
1223 */
1224 m1 = (rulep->r_mon + 9) % 12 + 1;
1225 yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
1226 yy1 = yy0 / 100;
1227 yy2 = yy0 % 100;
1228 dow = ((26 * m1 - 2) / 10 +
1229 1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
1230 if (dow < 0)
1231 dow += DAYSPERWEEK;
1232
1233 /*
1234 ** "dow" is the day-of-week of the first day of the month. Get
1235 ** the day-of-month (zero-origin) of the first "dow" day of the
1236 ** month.
1237 */
1238 d = rulep->r_day - dow;
1239 if (d < 0)
1240 d += DAYSPERWEEK;
1241 for (i = 1; i < rulep->r_week; ++i) {
1242 if (d + DAYSPERWEEK >=
1243 mon_lengths[leapyear][rulep->r_mon - 1])
1244 break;
1245 d += DAYSPERWEEK;
1246 }
1247
1248 /*
1249 ** "d" is the day-of-month (zero-origin) of the day we want.
1250 */
1251 value += d * SECSPERDAY;
1252 break;
1253 }
1254
1255 /*
1256 ** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
1257 ** question. To get the Epoch-relative time of the specified local
1258 ** time on that day, add the transition time and the current offset
1259 ** from UTC.
1260 */
1261 return value + rulep->r_time + offset;
1262 }
1263
1264 /*
1265 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
1266 ** appropriate.
1267 */
1268
1269 static int
1270 tzparse(name, sp, lastditch)
1271 const char * name;
1272 struct state * const sp;
1273 const int lastditch;
1274 {
1275 const char * stdname;
1276 const char * dstname;
1277 size_t stdlen;
1278 size_t dstlen;
1279 long stdoffset;
1280 long dstoffset;
1281 time_t * atp;
1282 unsigned char * typep;
1283 char * cp;
1284 int load_result;
1285
1286 INITIALIZE(dstname);
1287 if (lastditch) {
1288 stdname = name;
1289 stdlen = strlen(name); /* length of standard zone name */
1290 name += stdlen;
1291 if (stdlen >= sizeof sp->chars)
1292 stdlen = (sizeof sp->chars) - 1;
1293 stdoffset = 0;
1294 } else {
1295 name = getzname(name, (char **)&stdname, &stdlen);
1296 if (*name == '\0')
1297 return -1; /* was "stdoffset = 0;" */
1298 else {
1299 name = getoffset(name, &stdoffset);
1300 if (name == NULL)
1301 return -1;
1302 }
1303 }
1304 #ifdef NOTIFY_TZ
1305 load_result = tzload(TZDEFRULES, sp, NULL, FALSE);
1306 #else /* !NOTIFY_TZ */
1307 load_result = tzload(TZDEFRULES, sp, FALSE);
1308 #endif /* NOTIFY_TZ */
1309 if (load_result != 0)
1310 sp->leapcnt = 0; /* so, we're off a little */
1311 if (*name != '\0') {
1312 if (*name == '<') {
1313 dstname = ++name;
1314 name = getqzname(name, '>');
1315 if (*name != '>')
1316 return -1;
1317 dstlen = name - dstname;
1318 name++;
1319 } else {
1320 dstname = name;
1321 name = getzname(name, (char **)&dstname, &dstlen);
1322 }
1323 if (*name != '\0' && *name != ',' && *name != ';') {
1324 name = getoffset(name, &dstoffset);
1325 if (name == NULL)
1326 return -1;
1327 } else dstoffset = stdoffset - SECSPERHOUR;
1328 if (*name == '\0' && load_result != 0)
1329 name = TZDEFRULESTRING;
1330 if (*name == ',' || *name == ';') {
1331 struct rule start;
1332 struct rule end;
1333 int year;
1334 time_t janfirst;
1335 time_t starttime;
1336 time_t endtime;
1337
1338 ++name;
1339 if ((name = getrule(name, &start)) == NULL)
1340 return -1;
1341 if (*name++ != ',')
1342 return -1;
1343 if ((name = getrule(name, &end)) == NULL)
1344 return -1;
1345 if (*name != '\0')
1346 return -1;
1347 sp->typecnt = 2; /* standard time and DST */
1348 /*
1349 ** Two transitions per year, from EPOCH_YEAR forward.
1350 */
1351 sp->ttis[0].tt_gmtoff = -dstoffset;
1352 sp->ttis[0].tt_isdst = 1;
1353 sp->ttis[0].tt_abbrind = stdlen + 1;
1354 sp->ttis[1].tt_gmtoff = -stdoffset;
1355 sp->ttis[1].tt_isdst = 0;
1356 sp->ttis[1].tt_abbrind = 0;
1357 atp = sp->ats;
1358 typep = sp->types;
1359 janfirst = 0;
1360 sp->timecnt = 0;
1361 for (year = EPOCH_YEAR;
1362 sp->timecnt + 2 <= TZ_MAX_TIMES;
1363 ++year) {
1364 time_t newfirst;
1365
1366 starttime = transtime(janfirst, year, &start,
1367 stdoffset);
1368 endtime = transtime(janfirst, year, &end,
1369 dstoffset);
1370 if (starttime > endtime) {
1371 *atp++ = endtime;
1372 *typep++ = 1; /* DST ends */
1373 *atp++ = starttime;
1374 *typep++ = 0; /* DST begins */
1375 } else {
1376 *atp++ = starttime;
1377 *typep++ = 0; /* DST begins */
1378 *atp++ = endtime;
1379 *typep++ = 1; /* DST ends */
1380 }
1381 sp->timecnt += 2;
1382 newfirst = janfirst;
1383 newfirst += year_lengths[isleap(year)] *
1384 SECSPERDAY;
1385 if (newfirst <= janfirst)
1386 break;
1387 janfirst = newfirst;
1388 }
1389 } else {
1390 long theirstdoffset;
1391 long theirdstoffset;
1392 long theiroffset;
1393 int isdst;
1394 int i;
1395 int j;
1396
1397 if (*name != '\0')
1398 return -1;
1399 /*
1400 ** Initial values of theirstdoffset and theirdstoffset.
1401 */
1402 theirstdoffset = 0;
1403 for (i = 0; i < sp->timecnt; ++i) {
1404 j = sp->types[i];
1405 if (!sp->ttis[j].tt_isdst) {
1406 theirstdoffset =
1407 -sp->ttis[j].tt_gmtoff;
1408 break;
1409 }
1410 }
1411 theirdstoffset = 0;
1412 for (i = 0; i < sp->timecnt; ++i) {
1413 j = sp->types[i];
1414 if (sp->ttis[j].tt_isdst) {
1415 theirdstoffset =
1416 -sp->ttis[j].tt_gmtoff;
1417 break;
1418 }
1419 }
1420 /*
1421 ** Initially we're assumed to be in standard time.
1422 */
1423 isdst = FALSE;
1424 theiroffset = theirstdoffset;
1425 /*
1426 ** Now juggle transition times and types
1427 ** tracking offsets as you do.
1428 */
1429 for (i = 0; i < sp->timecnt; ++i) {
1430 j = sp->types[i];
1431 sp->types[i] = sp->ttis[j].tt_isdst;
1432 if (sp->ttis[j].tt_ttisgmt) {
1433 /* No adjustment to transition time */
1434 } else {
1435 /*
1436 ** If summer time is in effect, and the
1437 ** transition time was not specified as
1438 ** standard time, add the summer time
1439 ** offset to the transition time;
1440 ** otherwise, add the standard time
1441 ** offset to the transition time.
1442 */
1443 /*
1444 ** Transitions from DST to DDST
1445 ** will effectively disappear since
1446 ** POSIX provides for only one DST
1447 ** offset.
1448 */
1449 if (isdst && !sp->ttis[j].tt_ttisstd) {
1450 sp->ats[i] += dstoffset -
1451 theirdstoffset;
1452 } else {
1453 sp->ats[i] += stdoffset -
1454 theirstdoffset;
1455 }
1456 }
1457 theiroffset = -sp->ttis[j].tt_gmtoff;
1458 if (sp->ttis[j].tt_isdst)
1459 theirdstoffset = theiroffset;
1460 else theirstdoffset = theiroffset;
1461 }
1462 /*
1463 ** Finally, fill in ttis.
1464 ** ttisstd and ttisgmt need not be handled.
1465 */
1466 sp->ttis[0].tt_gmtoff = -stdoffset;
1467 sp->ttis[0].tt_isdst = FALSE;
1468 sp->ttis[0].tt_abbrind = 0;
1469 sp->ttis[1].tt_gmtoff = -dstoffset;
1470 sp->ttis[1].tt_isdst = TRUE;
1471 sp->ttis[1].tt_abbrind = stdlen + 1;
1472 sp->typecnt = 2;
1473 }
1474 } else {
1475 dstlen = 0;
1476 sp->typecnt = 1; /* only standard time */
1477 sp->timecnt = 0;
1478 sp->ttis[0].tt_gmtoff = -stdoffset;
1479 sp->ttis[0].tt_isdst = 0;
1480 sp->ttis[0].tt_abbrind = 0;
1481 }
1482 sp->charcnt = stdlen + 1;
1483 if (dstlen != 0)
1484 sp->charcnt += dstlen + 1;
1485 if ((size_t) sp->charcnt > sizeof sp->chars)
1486 return -1;
1487 cp = sp->chars;
1488 (void) strncpy(cp, stdname, stdlen);
1489 cp += stdlen;
1490 *cp++ = '\0';
1491 if (dstlen != 0) {
1492 (void) strncpy(cp, dstname, dstlen);
1493 *(cp + dstlen) = '\0';
1494 }
1495 return 0;
1496 }
1497
1498 static void
1499 #ifdef NOTIFY_TZ
1500 gmtload(sp, path)
1501 #else /* ! NOTIFY_TZ */
1502 gmtload(sp)
1503 #endif /* NOTIFY_TZ */
1504 struct state * const sp;
1505 #ifdef NOTIFY_TZ
1506 char *path;
1507 #endif /* NOTIFY_TZ */
1508 {
1509 #ifdef NOTIFY_TZ
1510 if (tzload(gmt, sp, path, TRUE) != 0)
1511 #else /* ! NOTIFY_TZ */
1512 if (tzload(gmt, sp, TRUE) != 0)
1513 #endif /* NOTIFY_TZ */
1514 (void) tzparse(gmt, sp, TRUE);
1515 }
1516
1517 static void
1518 tzsetwall_basic(int rdlocked)
1519 {
1520 #ifdef NOTIFY_TZ
1521 notify_check_tz(&lcl_notify);
1522 #else
1523 if (TZDEFAULT) {
1524 static struct timespec last_mtimespec = {0, 0};
1525 struct stat statbuf;
1526
1527 if (lstat(TZDEFAULT, &statbuf) == 0) {
1528 if (statbuf.st_mtimespec.tv_sec > last_mtimespec.tv_sec ||
1529 (statbuf.st_mtimespec.tv_sec == last_mtimespec.tv_sec &&
1530 statbuf.st_mtimespec.tv_nsec > last_mtimespec.tv_nsec)) {
1531 /* Trigger resetting the local TZ */
1532 lcl_is_set = 0;
1533 }
1534 last_mtimespec = statbuf.st_mtimespec;
1535 }
1536 }
1537 #endif /* NOTIFY_TZ */
1538 if (!rdlocked)
1539 _RWLOCK_RDLOCK(&lcl_rwlock);
1540 if (lcl_is_set < 0) {
1541 #ifdef NOTIFY_TZ_DEBUG
1542 NOTIFY_TZ_PRINTF("tzsetwall_basic lcl_is_set < 0\n");
1543 #endif
1544 if (!rdlocked)
1545 _RWLOCK_UNLOCK(&lcl_rwlock);
1546 return;
1547 }
1548 #ifdef NOTIFY_TZ_DEBUG
1549 NOTIFY_TZ_PRINTF("tzsetwall_basic not set\n");
1550 #endif
1551 _RWLOCK_UNLOCK(&lcl_rwlock);
1552
1553 _RWLOCK_WRLOCK(&lcl_rwlock);
1554 lcl_is_set = -1;
1555
1556 #ifdef ALL_STATE
1557 if (lclptr == NULL) {
1558 lclptr = calloc(1, sizeof *lclptr);
1559 if (lclptr == NULL) {
1560 settzname(); /* all we can do */
1561 _RWLOCK_UNLOCK(&lcl_rwlock);
1562 if (rdlocked)
1563 _RWLOCK_RDLOCK(&lcl_rwlock);
1564 return;
1565 }
1566 }
1567 #endif /* defined ALL_STATE */
1568 #ifdef NOTIFY_TZ
1569 {
1570 char fullname[FILENAME_MAX + 1];
1571 if (tzload((char *) NULL, lclptr, fullname, TRUE) != 0)
1572 /*
1573 * If fullname is empty (an error occurred) then
1574 * default to the UTC path
1575 */
1576 gmtload(lclptr, *fullname ? NULL : fullname);
1577 notify_register_tz(fullname, &lcl_notify);
1578 }
1579 #else /* ! NOTIFY_TZ */
1580 if (tzload((char *) NULL, lclptr, TRUE) != 0)
1581 gmtload(lclptr);
1582 #endif /* NOTIFY_TZ */
1583 settzname();
1584 _RWLOCK_UNLOCK(&lcl_rwlock);
1585
1586 if (rdlocked)
1587 _RWLOCK_RDLOCK(&lcl_rwlock);
1588 }
1589
1590 void
1591 tzsetwall(void)
1592 {
1593 #ifdef NOTIFY_TZ_DEBUG
1594 NOTIFY_TZ_PRINTF("tzsetwall called\n");
1595 #endif /* NOTIFY_TZ_DEBUG */
1596 tzsetwall_basic(0);
1597 }
1598
1599 __private_extern__ void
1600 tzset_basic(int rdlocked)
1601 {
1602 const char * name;
1603
1604 name = getenv("TZ");
1605 if (name == NULL) {
1606 tzsetwall_basic(rdlocked);
1607 return;
1608 }
1609
1610 #ifdef NOTIFY_TZ
1611 notify_check_tz(&lcl_notify);
1612 #endif /* NOTIFY_TZ */
1613 if (!rdlocked)
1614 _RWLOCK_RDLOCK(&lcl_rwlock);
1615 if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0) {
1616 if (!rdlocked)
1617 _RWLOCK_UNLOCK(&lcl_rwlock);
1618 #ifdef NOTIFY_TZ_DEBUG
1619 NOTIFY_TZ_PRINTF("tzset_basic matched %s\n", lcl_TZname);
1620 #endif
1621 return;
1622 }
1623 _RWLOCK_UNLOCK(&lcl_rwlock);
1624
1625 _RWLOCK_WRLOCK(&lcl_rwlock);
1626 lcl_is_set = strlen(name) < sizeof lcl_TZname;
1627 if (lcl_is_set)
1628 (void) strcpy(lcl_TZname, name);
1629
1630 #ifdef ALL_STATE
1631 if (lclptr == NULL) {
1632 lclptr = (struct state *) calloc(1, sizeof *lclptr);
1633 if (lclptr == NULL) {
1634 settzname(); /* all we can do */
1635 _RWLOCK_UNLOCK(&lcl_rwlock);
1636 if (rdlocked)
1637 _RWLOCK_RDLOCK(&lcl_rwlock);
1638 return;
1639 }
1640 }
1641 #endif /* defined ALL_STATE */
1642 if (*name == '\0') {
1643 /*
1644 ** User wants it fast rather than right.
1645 */
1646 lclptr->leapcnt = 0; /* so, we're off a little */
1647 lclptr->timecnt = 0;
1648 lclptr->typecnt = 0;
1649 lclptr->ttis[0].tt_isdst = 0;
1650 lclptr->ttis[0].tt_gmtoff = 0;
1651 lclptr->ttis[0].tt_abbrind = 0;
1652 (void) strcpy(lclptr->chars, gmt);
1653 #ifdef NOTIFY_TZ
1654 notify_register_tz(NULL, &lcl_notify);
1655 #endif /* NOTIFY_TZ */
1656 } else
1657 #ifdef NOTIFY_TZ
1658 {
1659 char fullname[FILENAME_MAX + 1];
1660 /*
1661 * parsedOK indicates whether tzparse() was called and
1662 * succeeded. This means that TZ is a time conversion
1663 * specification, so we don't need to register for
1664 * notifications.
1665 */
1666 int parsedOK = FALSE;
1667 if (tzload(name, lclptr, fullname, TRUE) != 0)
1668 if (name[0] == ':' || !(parsedOK = tzparse(name, lclptr, FALSE) == 0))
1669 /*
1670 * If fullname is empty (an error occurred) then
1671 * default to the UTC path
1672 */
1673 (void) gmtload(lclptr, *fullname ? NULL : fullname);
1674 notify_register_tz(parsedOK ? NULL : fullname, &lcl_notify);
1675 }
1676 #else /* ! NOTIFY_TZ */
1677 if (tzload(name, lclptr, TRUE) != 0)
1678 if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1679 (void) gmtload(lclptr);
1680 #endif /* NOTIFY_TZ */
1681 settzname();
1682 _RWLOCK_UNLOCK(&lcl_rwlock);
1683
1684 if (rdlocked)
1685 _RWLOCK_RDLOCK(&lcl_rwlock);
1686 }
1687
1688 void
1689 tzset(void)
1690 {
1691 #ifdef NOTIFY_TZ_DEBUG
1692 NOTIFY_TZ_PRINTF("tzset called TZ=%s\n", getenv("TZ"));
1693 #endif /* NOTIFY_TZ_DEBUG */
1694 tzset_basic(0);
1695 }
1696
1697 /*
1698 ** The easy way to behave "as if no library function calls" localtime
1699 ** is to not call it--so we drop its guts into "localsub", which can be
1700 ** freely called. (And no, the PANS doesn't require the above behavior--
1701 ** but it *is* desirable.)
1702 **
1703 ** The unused offset argument is for the benefit of mktime variants.
1704 */
1705
1706 /*ARGSUSED*/
1707 #ifdef __LP64__
1708 __private_extern__ struct tm *
1709 #else /* !__LP64__ */
1710 __private_extern__ void
1711 #endif /* __LP64__ */
1712 localsub(const time_t *const timep, const long offset, struct tm *const tmp)
1713 {
1714 struct state * sp;
1715 const struct ttinfo * ttisp;
1716 int i;
1717 #ifdef __LP64__
1718 struct tm * result;
1719 #endif /* __LP64__ */
1720 const time_t t = *timep;
1721
1722 #ifdef NOTIFY_TZ_DEBUG
1723 NOTIFY_TZ_PRINTF("localsub called\n");
1724 #endif /* NOTIFY_TZ_DEBUG */
1725 sp = lclptr;
1726 #ifdef ALL_STATE
1727 if (sp == NULL) {
1728 #ifdef __LP64__
1729 return gmtsub(timep, offset, tmp);
1730 #else /* !__LP64__ */
1731 gmtsub(timep, offset, tmp);
1732 return;
1733 #endif /* __LP64__ */
1734 }
1735 #endif /* defined ALL_STATE */
1736 if ((sp->goback && t < sp->ats[0]) ||
1737 (sp->goahead && t > sp->ats[sp->timecnt - 1])) {
1738 time_t newt = t;
1739 register time_t seconds;
1740 register time_t tcycles;
1741 register int_fast64_t icycles;
1742
1743 if (t < sp->ats[0])
1744 seconds = sp->ats[0] - t;
1745 else seconds = t - sp->ats[sp->timecnt - 1];
1746 --seconds;
1747 tcycles = seconds / YEARSPERREPEAT / AVGSECSPERYEAR;
1748 ++tcycles;
1749 icycles = tcycles;
1750 if (tcycles - icycles >= 1 || icycles - tcycles >= 1) {
1751 #ifdef __LP64__
1752 return NULL;
1753 #else /* !__LP64__ */
1754 return;
1755 #endif /* __LP64__ */
1756 }
1757 seconds = icycles;
1758 seconds *= YEARSPERREPEAT;
1759 seconds *= AVGSECSPERYEAR;
1760 if (t < sp->ats[0])
1761 newt += seconds;
1762 else newt -= seconds;
1763 if (newt < sp->ats[0] ||
1764 newt > sp->ats[sp->timecnt - 1])
1765 #ifdef __LP64__
1766 return NULL; /* "cannot happen" */
1767 result = localsub(&newt, offset, tmp);
1768 if (result == tmp) {
1769 #else /* !__LP64__ */
1770 return;
1771 localsub(&newt, offset, tmp);
1772 {
1773 #endif /* __LP64__ */
1774 register time_t newy;
1775
1776 newy = tmp->tm_year;
1777 if (t < sp->ats[0])
1778 newy -= icycles * YEARSPERREPEAT;
1779 else newy += icycles * YEARSPERREPEAT;
1780 tmp->tm_year = newy;
1781 if (tmp->tm_year != newy)
1782 #ifdef __LP64__
1783 return NULL;
1784 }
1785 return result;
1786 #else /* !__LP64__ */
1787 return;
1788 }
1789 return;
1790 #endif /* __LP64__ */
1791 }
1792 if (sp->timecnt == 0 || t < sp->ats[0]) {
1793 i = 0;
1794 while (sp->ttis[i].tt_isdst)
1795 if (++i >= sp->typecnt) {
1796 i = 0;
1797 break;
1798 }
1799 } else {
1800 register int lo = 1;
1801 register int hi = sp->timecnt;
1802
1803 while (lo < hi) {
1804 register int mid = (lo + hi) >> 1;
1805
1806 if (t < sp->ats[mid])
1807 hi = mid;
1808 else lo = mid + 1;
1809 }
1810 i = (int) sp->types[lo - 1];
1811 }
1812 ttisp = &sp->ttis[i];
1813 /*
1814 ** To get (wrong) behavior that's compatible with System V Release 2.0
1815 ** you'd replace the statement below with
1816 ** t += ttisp->tt_gmtoff;
1817 ** timesub(&t, 0L, sp, tmp);
1818 */
1819 #ifdef __LP64__
1820 result = timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1821 if (result == NULL)
1822 return NULL;
1823 #else /* !__LP64__ */
1824 timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1825 #endif /* __LP64__ */
1826 tmp->tm_isdst = ttisp->tt_isdst;
1827 tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1828 #ifdef TM_ZONE
1829 tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1830 #endif /* defined TM_ZONE */
1831 #ifdef __LP64__
1832 return result;
1833 #endif /* __LP64__ */
1834 }
1835
1836 static void
1837 localtime_key_init(void)
1838 {
1839
1840 localtime_key = __LIBC_PTHREAD_KEY_LOCALTIME;
1841 localtime_key_error = pthread_key_init_np(localtime_key, free);
1842 }
1843
1844 struct tm *
1845 localtime(const time_t *const timep)
1846 {
1847 struct tm *p_tm;
1848
1849 if (__isthreaded != 0) {
1850 _pthread_once(&localtime_once, localtime_key_init);
1851 if (localtime_key_error != 0) {
1852 errno = localtime_key_error;
1853 return(NULL);
1854 }
1855 p_tm = _pthread_getspecific(localtime_key);
1856 if (p_tm == NULL) {
1857 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
1858 == NULL)
1859 return(NULL);
1860 _pthread_setspecific(localtime_key, p_tm);
1861 }
1862 _RWLOCK_RDLOCK(&lcl_rwlock);
1863 tzset_basic(1);
1864 #ifdef __LP64__
1865 p_tm = localsub(timep, 0L, p_tm);
1866 #else /* !__LP64__ */
1867 localsub(timep, 0L, p_tm);
1868 #endif /* __LP64__ */
1869 _RWLOCK_UNLOCK(&lcl_rwlock);
1870 return(p_tm);
1871 } else {
1872 tzset_basic(0);
1873 #ifdef __LP64__
1874 return localsub(timep, 0L, &tm);
1875 #else /* !__LP64__ */
1876 localsub(timep, 0L, &tm);
1877 return(&tm);
1878 #endif /* __LP64__ */
1879 }
1880 }
1881
1882 /*
1883 ** Re-entrant version of localtime.
1884 */
1885
1886 struct tm *
1887 localtime_r(const time_t *const __restrict timep, struct tm * __restrict tmp)
1888 {
1889 _RWLOCK_RDLOCK(&lcl_rwlock);
1890 tzset_basic(1);
1891 #ifdef __LP64__
1892 tmp = localsub(timep, 0L, tmp);
1893 #else /* !__LP64__ */
1894 localsub(timep, 0L, tmp);
1895 #endif /* __LP64__ */
1896 _RWLOCK_UNLOCK(&lcl_rwlock);
1897 return tmp;
1898 }
1899
1900 static void
1901 gmt_init(void)
1902 {
1903
1904 #ifdef ALL_STATE
1905 #ifdef NOTIFY_TZ
1906 if (gmtptr == NULL)
1907 #endif /* NOTIFY_TZ */
1908 gmtptr = (struct state *) calloc(1, sizeof *gmtptr);
1909 if (gmtptr != NULL)
1910 #endif /* defined ALL_STATE */
1911 #ifdef NOTIFY_TZ
1912 {
1913 char fullname[FILENAME_MAX + 1];
1914 gmtload(gmtptr, fullname);
1915 notify_register_tz(fullname, &gmt_notify);
1916 }
1917 #else /* ! NOTIFY_TZ */
1918 gmtload(gmtptr);
1919 #endif /* NOTIFY_TZ */
1920 }
1921
1922 /*
1923 ** gmtsub is to gmtime as localsub is to localtime.
1924 */
1925
1926 #ifdef __LP64__
1927 static struct tm *
1928 #else /* !__LP64__ */
1929 static void
1930 #endif /* __LP64__ */
1931 gmtsub(timep, offset, tmp)
1932 const time_t * const timep;
1933 const long offset;
1934 struct tm * const tmp;
1935 {
1936 #ifdef __LP64__
1937 register struct tm * result;
1938 #endif /* __LP64__ */
1939
1940 #ifdef NOTIFY_TZ_DEBUG
1941 NOTIFY_TZ_PRINTF("gmtsub called\n");
1942 #endif /* NOTIFY_TZ_DEBUG */
1943 #ifdef NOTIFY_TZ
1944 notify_check_tz(&gmt_notify);
1945 #endif /* NOTIFY_TZ */
1946 pthread_once(&gmt_once, gmt_init);
1947 #ifdef __LP64__
1948 result = timesub(timep, offset, gmtptr, tmp);
1949 if (result == NULL)
1950 return NULL;
1951 #else /* !__LP64__ */
1952 timesub(timep, offset, gmtptr, tmp);
1953 #endif /* __LP64__ */
1954 #ifdef TM_ZONE
1955 /*
1956 ** Could get fancy here and deliver something such as
1957 ** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1958 ** but this is no time for a treasure hunt.
1959 */
1960 if (offset != 0)
1961 tmp->TM_ZONE = (char*)wildabbr;
1962 else {
1963 #ifdef ALL_STATE
1964 if (gmtptr == NULL)
1965 tmp->TM_ZONE = (char *)gmt;
1966 else tmp->TM_ZONE = gmtptr->chars;
1967 #endif /* defined ALL_STATE */
1968 #ifndef ALL_STATE
1969 tmp->TM_ZONE = gmtptr->chars;
1970 #endif /* State Farm */
1971 }
1972 #endif /* defined TM_ZONE */
1973 #ifdef __LP64__
1974 return result;
1975 #endif /* __LP64__ */
1976 }
1977
1978 static void
1979 gmtime_key_init(void)
1980 {
1981
1982 gmtime_key = __LIBC_PTHREAD_KEY_GMTIME;
1983 gmtime_key_error = pthread_key_init_np(gmtime_key, free);
1984 }
1985
1986 struct tm *
1987 gmtime(const time_t *const timep)
1988 {
1989 struct tm *p_tm;
1990
1991 if (__isthreaded != 0) {
1992 _pthread_once(&gmtime_once, gmtime_key_init);
1993 if (gmtime_key_error != 0) {
1994 errno = gmtime_key_error;
1995 return(NULL);
1996 }
1997 /*
1998 * Changed to follow POSIX.1 threads standard, which
1999 * is what BSD currently has.
2000 */
2001 if ((p_tm = _pthread_getspecific(gmtime_key)) == NULL) {
2002 if ((p_tm = (struct tm *)malloc(sizeof(struct tm)))
2003 == NULL) {
2004 return(NULL);
2005 }
2006 _pthread_setspecific(gmtime_key, p_tm);
2007 }
2008 #ifdef __LP64__
2009 return gmtsub(timep, 0L, p_tm);
2010 #else /* !__LP64__ */
2011 gmtsub(timep, 0L, p_tm);
2012 return(p_tm);
2013 #endif /* __LP64__ */
2014 }
2015 else {
2016 #ifdef __LP64__
2017 return gmtsub(timep, 0L, &tm);
2018 #else /* !__LP64__ */
2019 gmtsub(timep, 0L, &tm);
2020 return(&tm);
2021 #endif /* __LP64__ */
2022 }
2023 }
2024
2025 /*
2026 * Re-entrant version of gmtime.
2027 */
2028
2029 struct tm *
2030 gmtime_r(const time_t *const timep, struct tm *tmp)
2031 {
2032
2033 #ifdef __LP64__
2034 return gmtsub(timep, 0L, tmp);
2035 #else /* !__LP64__ */
2036 gmtsub(timep, 0L, tmp);
2037 return tmp;
2038 #endif /* __LP64__ */
2039 }
2040
2041 #ifdef STD_INSPIRED
2042
2043 struct tm *
2044 offtime(const time_t *const timep, const long offset)
2045 {
2046 #ifdef __LP64__
2047 return gmtsub(timep, offset, &tm);
2048 #else /* !__LP64__ */
2049 gmtsub(timep, offset, &tm);
2050 return &tm;
2051 #endif /* __LP64__ */
2052 }
2053
2054 #endif /* defined STD_INSPIRED */
2055
2056 /*
2057 ** Return the number of leap years through the end of the given year
2058 ** where, to make the math easy, the answer for year zero is defined as zero.
2059 */
2060
2061 __unused static int
2062 leaps_thru_end_of(y)
2063 register const int y;
2064 {
2065 #ifdef __LP64__
2066 return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
2067 -(leaps_thru_end_of(-(y + 1)) + 1);
2068 #else /* !__LP64__ */
2069 return (y / 4 - y / 100 + y / 400);
2070 #endif /* __LP64__ */
2071 }
2072
2073 #ifdef __LP64__
2074 static struct tm *
2075 #else /* !__LP64__ */
2076 static void
2077 #endif /* __LP64__ */
2078 timesub(timep, offset, sp, tmp)
2079 const time_t * const timep;
2080 const long offset;
2081 const struct state * const sp;
2082 struct tm * const tmp;
2083 {
2084 const struct lsinfo * lp;
2085 long days;
2086 long rem;
2087 long y;
2088 int yleap;
2089 const int * ip;
2090 long corr;
2091 int hit;
2092 int i;
2093
2094 corr = 0;
2095 hit = 0;
2096 #ifdef ALL_STATE
2097 i = (sp == NULL) ? 0 : sp->leapcnt;
2098 #endif /* defined ALL_STATE */
2099 #ifndef ALL_STATE
2100 i = sp->leapcnt;
2101 #endif /* State Farm */
2102 while (--i >= 0) {
2103 lp = &sp->lsis[i];
2104 if (*timep >= lp->ls_trans) {
2105 if (*timep == lp->ls_trans) {
2106 hit = ((i == 0 && lp->ls_corr > 0) ||
2107 lp->ls_corr > sp->lsis[i - 1].ls_corr);
2108 if (hit)
2109 while (i > 0 &&
2110 sp->lsis[i].ls_trans ==
2111 sp->lsis[i - 1].ls_trans + 1 &&
2112 sp->lsis[i].ls_corr ==
2113 sp->lsis[i - 1].ls_corr + 1) {
2114 ++hit;
2115 --i;
2116 }
2117 }
2118 corr = lp->ls_corr;
2119 break;
2120 }
2121 }
2122 days = *timep / SECSPERDAY;
2123 rem = *timep % SECSPERDAY;
2124 #ifdef mc68k
2125 if (*timep == 0x80000000) {
2126 /*
2127 ** A 3B1 muffs the division on the most negative number.
2128 */
2129 days = -24855;
2130 rem = -11648;
2131 }
2132 #endif /* defined mc68k */
2133 rem += (offset - corr);
2134 while (rem < 0) {
2135 rem += SECSPERDAY;
2136 --days;
2137 }
2138 while (rem >= SECSPERDAY) {
2139 rem -= SECSPERDAY;
2140 ++days;
2141 }
2142 tmp->tm_hour = (int) (rem / SECSPERHOUR);
2143 rem = rem % SECSPERHOUR;
2144 tmp->tm_min = (int) (rem / SECSPERMIN);
2145 /*
2146 ** A positive leap second requires a special
2147 ** representation. This uses "... ??:59:60" et seq.
2148 */
2149 tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
2150 tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
2151 if (tmp->tm_wday < 0)
2152 tmp->tm_wday += DAYSPERWEEK;
2153 y = EPOCH_YEAR;
2154 #define _LEAPS_THRU_END_OF(y) ((y) / 4 - (y) / 100 + (y) / 400)
2155 #ifdef __LP64__
2156 #define LEAPS_THRU_END_OF(y) ((y) >= 0 ? _LEAPS_THRU_END_OF(y) : _LEAPS_THRU_END_OF((y) + 1) - 1)
2157 #else /* !__LP64__ */
2158 #define LEAPS_THRU_END_OF(y) _LEAPS_THRU_END_OF(y)
2159 #endif /* __LP64__ */
2160 while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
2161 long newy;
2162
2163 newy = y + days / DAYSPERNYEAR;
2164 if (days < 0)
2165 --newy;
2166 days -= (newy - y) * DAYSPERNYEAR +
2167 LEAPS_THRU_END_OF(newy - 1) -
2168 LEAPS_THRU_END_OF(y - 1);
2169 y = newy;
2170 }
2171 #ifdef __LP64__
2172 y -= TM_YEAR_BASE;
2173 if (y < INT_MIN || y > INT_MAX) {
2174 errno = EOVERFLOW;
2175 return NULL;
2176 }
2177 tmp->tm_year = y;
2178 #else /* !__LP64__ */
2179 tmp->tm_year = y - TM_YEAR_BASE;
2180 #endif /* __LP64__ */
2181 tmp->tm_yday = (int) days;
2182 ip = mon_lengths[yleap];
2183 for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
2184 days = days - (long) ip[tmp->tm_mon];
2185 tmp->tm_mday = (int) (days + 1);
2186 tmp->tm_isdst = 0;
2187 #ifdef TM_GMTOFF
2188 tmp->TM_GMTOFF = offset;
2189 #endif /* defined TM_GMTOFF */
2190 #ifdef __LP64__
2191 return tmp;
2192 #endif /* __LP64__ */
2193 }
2194
2195 char *
2196 ctime(const time_t *const timep)
2197 {
2198 /*
2199 ** Section 4.12.3.2 of X3.159-1989 requires that
2200 ** The ctime function converts the calendar time pointed to by timer
2201 ** to local time in the form of a string. It is equivalent to
2202 ** asctime(localtime(timer))
2203 */
2204 #ifdef __LP64__
2205 /*
2206 * In 64-bit, the timep value may produce a time value with a year
2207 * that exceeds 32-bits in size (won't fit in struct tm), so localtime
2208 * will return NULL.
2209 */
2210 struct tm *tm = localtime(timep);
2211
2212 if (tm == NULL)
2213 return NULL;
2214 return asctime(tm);
2215 #else /* !__LP64__ */
2216 return asctime(localtime(timep));
2217 #endif /* __LP64__ */
2218 }
2219
2220 char *
2221 ctime_r(const time_t *const timep, char *buf)
2222 {
2223 struct tm mytm;
2224
2225 #ifdef __LP64__
2226 /*
2227 * In 64-bit, the timep value may produce a time value with a year
2228 * that exceeds 32-bits in size (won't fit in struct tm), so localtime_r
2229 * will return NULL.
2230 */
2231 if (localtime_r(timep, &mytm) == NULL)
2232 return NULL;
2233 return asctime_r(&mytm, buf);
2234 #else /* !__LP64__ */
2235 return asctime_r(localtime_r(timep, &mytm), buf);
2236 #endif /* __LP64__ */
2237 }
2238
2239 /*
2240 ** Adapted from code provided by Robert Elz, who writes:
2241 ** The "best" way to do mktime I think is based on an idea of Bob
2242 ** Kridle's (so its said...) from a long time ago.
2243 ** It does a binary search of the time_t space. Since time_t's are
2244 ** just 32 bits, its a max of 32 iterations (even at 64 bits it
2245 ** would still be very reasonable).
2246 */
2247
2248 #ifndef WRONG
2249 #define WRONG (-1)
2250 #endif /* !defined WRONG */
2251
2252 /*
2253 ** Simplified normalize logic courtesy Paul Eggert.
2254 */
2255
2256 static int
2257 increment_overflow(number, delta)
2258 int * number;
2259 int delta;
2260 {
2261 int number0;
2262
2263 number0 = *number;
2264 *number += delta;
2265 return (*number < number0) != (delta < 0);
2266 }
2267
2268 static int
2269 long_increment_overflow(number, delta)
2270 long * number;
2271 int delta;
2272 {
2273 long number0;
2274
2275 number0 = *number;
2276 *number += delta;
2277 return (*number < number0) != (delta < 0);
2278 }
2279
2280 static int
2281 normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
2282 {
2283 int tensdelta;
2284
2285 tensdelta = (*unitsptr >= 0) ?
2286 (*unitsptr / base) :
2287 (-1 - (-1 - *unitsptr) / base);
2288 *unitsptr -= tensdelta * base;
2289 return increment_overflow(tensptr, tensdelta);
2290 }
2291
2292 static int
2293 long_normalize_overflow(long *const tensptr, int *const unitsptr, const int base)
2294 {
2295 register int tensdelta;
2296
2297 tensdelta = (*unitsptr >= 0) ?
2298 (*unitsptr / base) :
2299 (-1 - (-1 - *unitsptr) / base);
2300 *unitsptr -= tensdelta * base;
2301 return long_increment_overflow(tensptr, tensdelta);
2302 }
2303
2304 static int
2305 tmcomp(atmp, btmp)
2306 const struct tm * const atmp;
2307 const struct tm * const btmp;
2308 {
2309 int result;
2310
2311 /*
2312 * Assume that atmp and btmp point to normalized tm strutures.
2313 * So only arithmetic with tm_year could overflow in 64-bit.
2314 */
2315 if (atmp->tm_year != btmp->tm_year) {
2316 return (atmp->tm_year > btmp->tm_year ? 1 : -1);
2317 }
2318 if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
2319 (result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
2320 (result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
2321 (result = (atmp->tm_min - btmp->tm_min)) == 0)
2322 result = atmp->tm_sec - btmp->tm_sec;
2323 return result;
2324 }
2325
2326 static time_t
2327 time2sub(struct tm *const tmp,
2328 #ifdef __LP64__
2329 struct tm *(*const funcp)(const time_t *, long, struct tm *),
2330 #else /* !__LP64__ */
2331 void(*funcp) (const time_t *, long, struct tm*),
2332 #endif /* __LP64__ */
2333 const long offset,
2334 int *const okayp,
2335 const int do_norm_secs,
2336 int unix03)
2337 {
2338 const struct state * sp;
2339 int dir;
2340 int i, j;
2341 int saved_seconds;
2342 long li;
2343 time_t lo;
2344 time_t hi;
2345 long y;
2346 time_t newt;
2347 time_t t;
2348 struct tm yourtm, mytm;
2349
2350 *okayp = FALSE;
2351 yourtm = *tmp;
2352 if (do_norm_secs) {
2353 if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
2354 SECSPERMIN))
2355 return WRONG;
2356 }
2357 if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
2358 return WRONG;
2359 if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
2360 return WRONG;
2361 y = yourtm.tm_year;
2362 if (long_normalize_overflow(&y, &yourtm.tm_mon, MONSPERYEAR))
2363 return WRONG;
2364 /*
2365 ** Turn y into an actual year number for now.
2366 ** It is converted back to an offset from TM_YEAR_BASE later.
2367 */
2368 if (long_increment_overflow(&y, TM_YEAR_BASE))
2369 return WRONG;
2370 while (yourtm.tm_mday <= 0) {
2371 if (long_increment_overflow(&y, -1))
2372 return WRONG;
2373 li = y + (1 < yourtm.tm_mon);
2374 yourtm.tm_mday += year_lengths[isleap(li)];
2375 }
2376 while (yourtm.tm_mday > DAYSPERLYEAR) {
2377 li = y + (1 < yourtm.tm_mon);
2378 yourtm.tm_mday -= year_lengths[isleap(li)];
2379 if (long_increment_overflow(&y, 1))
2380 return WRONG;
2381 }
2382 for ( ; ; ) {
2383 i = mon_lengths[isleap(y)][yourtm.tm_mon];
2384 if (yourtm.tm_mday <= i)
2385 break;
2386 yourtm.tm_mday -= i;
2387 if (++yourtm.tm_mon >= MONSPERYEAR) {
2388 yourtm.tm_mon = 0;
2389 if (long_increment_overflow(&y, 1))
2390 return WRONG;
2391 }
2392 }
2393 if (long_increment_overflow(&y, -TM_YEAR_BASE))
2394 return WRONG;
2395 yourtm.tm_year = y;
2396 if (yourtm.tm_year != y)
2397 return WRONG;
2398 /* Don't go below 1900 for POLA */
2399 if (yourtm.tm_year < 0)
2400 return WRONG;
2401 if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
2402 saved_seconds = 0;
2403 else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
2404 /*
2405 ** We can't set tm_sec to 0, because that might push the
2406 ** time below the minimum representable time.
2407 ** Set tm_sec to 59 instead.
2408 ** This assumes that the minimum representable time is
2409 ** not in the same minute that a leap second was deleted from,
2410 ** which is a safer assumption than using 58 would be.
2411 */
2412 if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
2413 return WRONG;
2414 saved_seconds = yourtm.tm_sec;
2415 yourtm.tm_sec = SECSPERMIN - 1;
2416 } else {
2417 saved_seconds = yourtm.tm_sec;
2418 yourtm.tm_sec = 0;
2419 }
2420 /*
2421 ** Do a binary search (this works whatever time_t's type is).
2422 */
2423 if (!TYPE_SIGNED(time_t)) {
2424 lo = 0;
2425 hi = lo - 1;
2426 } else if (!TYPE_INTEGRAL(time_t)) {
2427 if (sizeof(time_t) > sizeof(float))
2428 hi = (time_t) DBL_MAX;
2429 else hi = (time_t) FLT_MAX;
2430 lo = -hi;
2431 } else {
2432 lo = 1;
2433 for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
2434 lo *= 2;
2435 hi = -(lo + 1);
2436 }
2437 for ( ; ; ) {
2438 t = lo / 2 + hi / 2;
2439 if (t < lo)
2440 t = lo;
2441 else if (t > hi)
2442 t = hi;
2443 #ifdef __LP64__
2444 if ((*funcp)(&t, offset, &mytm) == NULL) {
2445 /*
2446 ** Assume that t is too extreme to be represented in
2447 ** a struct tm; arrange things so that it is less
2448 ** extreme on the next pass.
2449 */
2450 dir = (t > 0) ? 1 : -1;
2451 } else dir = tmcomp(&mytm, &yourtm);
2452 #else /* !__LP64__ */
2453 (*funcp)(&t, offset, &mytm);
2454 dir = tmcomp(&mytm, &yourtm);
2455 // If we have searched the entire space without a match, exit
2456 if (dir != 0 && t == lo && t == hi)
2457 return WRONG;
2458 #endif /* __LP64__ */
2459 if (dir != 0) {
2460 if (t == lo) {
2461 ++t;
2462 if (t <= lo)
2463 return WRONG;
2464 ++lo;
2465 } else if (t == hi) {
2466 --t;
2467 if (t >= hi)
2468 return WRONG;
2469 --hi;
2470 }
2471 if (lo > hi)
2472 return WRONG;
2473 if (dir > 0)
2474 hi = t;
2475 else lo = t;
2476 continue;
2477 }
2478 sp = (funcp == localsub) ? lclptr : gmtptr;
2479 if (unix03 && sp->typecnt == 1 && yourtm.tm_isdst > 0)
2480 yourtm.tm_isdst = 0; /* alternative time does not apply */
2481 if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
2482 break;
2483 /*
2484 ** Right time, wrong type.
2485 ** Hunt for right time, right type.
2486 ** It's okay to guess wrong since the guess
2487 ** gets checked.
2488 */
2489 #ifdef ALL_STATE
2490 if (sp == NULL)
2491 return WRONG;
2492 #endif /* defined ALL_STATE */
2493 for (i = sp->typecnt - 1; i >= 0; --i) {
2494 if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
2495 continue;
2496 for (j = sp->typecnt - 1; j >= 0; --j) {
2497 if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
2498 continue;
2499 newt = t + sp->ttis[j].tt_gmtoff -
2500 sp->ttis[i].tt_gmtoff;
2501 #ifdef __LP64__
2502 if ((*funcp)(&newt, offset, &mytm) == NULL)
2503 continue;
2504 #else /* !__LP64__ */
2505 (*funcp)(&newt, offset, &mytm);
2506 #endif /* __LP64__ */
2507 if (tmcomp(&mytm, &yourtm) != 0)
2508 continue;
2509 if (mytm.tm_isdst != yourtm.tm_isdst)
2510 continue;
2511 /*
2512 ** We have a match.
2513 */
2514 t = newt;
2515 goto label;
2516 }
2517 }
2518 return WRONG;
2519 }
2520 label:
2521 newt = t + saved_seconds;
2522 if ((newt < t) != (saved_seconds < 0))
2523 return WRONG;
2524 t = newt;
2525 #ifdef __LP64__
2526 if ((*funcp)(&t, offset, tmp) == NULL)
2527 return WRONG;
2528 #else /* !__LP64__ */
2529 (*funcp)(&t, offset, tmp);
2530 #endif /* __LP64__ */
2531 *okayp = TRUE;
2532 return t;
2533 }
2534
2535 static time_t
2536 time2(struct tm * const tmp,
2537 #ifdef __LP64__
2538 struct tm * (*const funcp)(const time_t *, long, struct tm *),
2539 #else /* !__LP64__ */
2540 void (*const funcp)(const time_t *, long, struct tm *),
2541 #endif /* __LP64__ */
2542 const long offset,
2543 int *const okayp,
2544 int unix03)
2545 {
2546 time_t t;
2547
2548 /*
2549 ** First try without normalization of seconds
2550 ** (in case tm_sec contains a value associated with a leap second).
2551 ** If that fails, try with normalization of seconds.
2552 */
2553 t = time2sub(tmp, funcp, offset, okayp, FALSE, unix03);
2554 return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE, unix03);
2555 }
2556
2557 __private_extern__ time_t
2558 time1(tmp, funcp, offset, unix03)
2559 struct tm * const tmp;
2560 #ifdef __LP64__
2561 struct tm * (* const funcp)(const time_t *, long, struct tm *);
2562 #else /* !__LP64__ */
2563 void (* const funcp)(const time_t *, long, struct tm *);
2564 #endif /* __LP64__ */
2565 const long offset;
2566 int unix03;
2567 {
2568 time_t t;
2569 const struct state * sp;
2570 int samei, otheri;
2571 int sameind, otherind;
2572 int i;
2573 int nseen;
2574 int seen[TZ_MAX_TYPES];
2575 int types[TZ_MAX_TYPES];
2576 int okay;
2577
2578 if (tmp == NULL) {
2579 errno = EINVAL;
2580 return WRONG;
2581 }
2582
2583 if (tmp->tm_isdst > 1)
2584 tmp->tm_isdst = 1;
2585 t = time2(tmp, funcp, offset, &okay, unix03);
2586 #ifdef PCTS
2587 /*
2588 ** PCTS code courtesy Grant Sullivan.
2589 */
2590 if (okay)
2591 return t;
2592 if (tmp->tm_isdst < 0)
2593 tmp->tm_isdst = 0; /* reset to std and try again */
2594 #endif /* defined PCTS */
2595 #ifndef PCTS
2596 if (okay || tmp->tm_isdst < 0)
2597 return t;
2598 #endif /* !defined PCTS */
2599 /*
2600 ** We're supposed to assume that somebody took a time of one type
2601 ** and did some math on it that yielded a "struct tm" that's bad.
2602 ** We try to divine the type they started from and adjust to the
2603 ** type they need.
2604 */
2605 sp = (const struct state *) ((funcp == localsub) ? lclptr : gmtptr);
2606 #ifdef ALL_STATE
2607 if (sp == NULL)
2608 return WRONG;
2609 #endif /* defined ALL_STATE */
2610 for (i = 0; i < sp->typecnt; ++i)
2611 seen[i] = FALSE;
2612 nseen = 0;
2613 for (i = sp->timecnt - 1; i >= 0; --i)
2614 if (!seen[sp->types[i]]) {
2615 seen[sp->types[i]] = TRUE;
2616 types[nseen++] = sp->types[i];
2617 }
2618 for (sameind = 0; sameind < nseen; ++sameind) {
2619 samei = types[sameind];
2620 if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
2621 continue;
2622 for (otherind = 0; otherind < nseen; ++otherind) {
2623 otheri = types[otherind];
2624 if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
2625 continue;
2626 tmp->tm_sec += sp->ttis[otheri].tt_gmtoff -
2627 sp->ttis[samei].tt_gmtoff;
2628 tmp->tm_isdst = !tmp->tm_isdst;
2629 t = time2(tmp, funcp, offset, &okay, unix03);
2630 if (okay)
2631 return t;
2632 tmp->tm_sec -= sp->ttis[otheri].tt_gmtoff -
2633 sp->ttis[samei].tt_gmtoff;
2634 tmp->tm_isdst = !tmp->tm_isdst;
2635 }
2636 }
2637 return WRONG;
2638 }
2639 #else /* BUILDING_VARIANT */
2640 extern pthread_rwlock_t lcl_rwlock;
2641 #endif /* BUILDING_VARIANT */
2642
2643 time_t
2644 mktime(struct tm *const tmp)
2645 {
2646 time_t mktime_return_value;
2647 int serrno = errno;
2648 _RWLOCK_RDLOCK(&lcl_rwlock);
2649 tzset_basic(1);
2650 mktime_return_value = time1(tmp, localsub, 0L, __DARWIN_UNIX03);
2651 _RWLOCK_UNLOCK(&lcl_rwlock);
2652 errno = serrno;
2653 return(mktime_return_value);
2654 }
2655
2656 #if !BUILDING_VARIANT
2657 #ifdef STD_INSPIRED
2658
2659 time_t
2660 timelocal(struct tm *const tmp)
2661 {
2662 if (tmp != NULL)
2663 tmp->tm_isdst = -1; /* in case it wasn't initialized */
2664 return mktime(tmp);
2665 }
2666
2667 time_t
2668 timegm(struct tm *const tmp)
2669 {
2670 if (tmp != NULL)
2671 tmp->tm_isdst = 0;
2672 return time1(tmp, gmtsub, 0L, __DARWIN_UNIX03);
2673 }
2674
2675 time_t
2676 timeoff(struct tm *const tmp, const long offset)
2677 {
2678 if (tmp != NULL)
2679 tmp->tm_isdst = 0;
2680 return time1(tmp, gmtsub, offset, __DARWIN_UNIX03);
2681 }
2682
2683 #endif /* defined STD_INSPIRED */
2684
2685 #ifdef CMUCS
2686
2687 /*
2688 ** The following is supplied for compatibility with
2689 ** previous versions of the CMUCS runtime library.
2690 */
2691
2692 long
2693 gtime(struct tm *const tmp)
2694 {
2695 const time_t t = mktime(tmp);
2696
2697 if (t == WRONG)
2698 return -1;
2699 return t;
2700 }
2701
2702 #endif /* defined CMUCS */
2703
2704 /*
2705 ** XXX--is the below the right way to conditionalize??
2706 */
2707
2708 #ifdef STD_INSPIRED
2709
2710 /*
2711 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
2712 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
2713 ** is not the case if we are accounting for leap seconds.
2714 ** So, we provide the following conversion routines for use
2715 ** when exchanging timestamps with POSIX conforming systems.
2716 */
2717
2718 static long
2719 leapcorr(time_t *timep)
2720 {
2721 struct state * sp;
2722 struct lsinfo * lp;
2723 int i;
2724
2725 sp = lclptr;
2726 i = sp->leapcnt;
2727 while (--i >= 0) {
2728 lp = &sp->lsis[i];
2729 if (*timep >= lp->ls_trans)
2730 return lp->ls_corr;
2731 }
2732 return 0;
2733 }
2734
2735 time_t
2736 time2posix(time_t t)
2737 {
2738 tzset();
2739 return t - leapcorr(&t);
2740 }
2741
2742 time_t
2743 posix2time(time_t t)
2744 {
2745 time_t x;
2746 time_t y;
2747
2748 tzset();
2749 /*
2750 ** For a positive leap second hit, the result
2751 ** is not unique. For a negative leap second
2752 ** hit, the corresponding time doesn't exist,
2753 ** so we return an adjacent second.
2754 */
2755 x = t + leapcorr(&t);
2756 y = x - leapcorr(&x);
2757 if (y < t) {
2758 do {
2759 x++;
2760 y = x - leapcorr(&x);
2761 } while (y < t);
2762 if (t != y)
2763 return x - 1;
2764 } else if (y > t) {
2765 do {
2766 --x;
2767 y = x - leapcorr(&x);
2768 } while (y > t);
2769 if (t != y)
2770 return x + 1;
2771 }
2772 return x;
2773 }
2774
2775 #endif /* defined STD_INSPIRED */
2776 #endif /* !BUILDING_VARIANT */