xref: /netbsd-src/lib/libc/time/localtime.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: localtime.c,v 1.35 2003/12/20 00:12:05 kleink Exp $	*/
2 
3 /*
4 ** This file is in the public domain, so clarified as of
5 ** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov).
6 */
7 
8 #include <sys/cdefs.h>
9 #if defined(LIBC_SCCS) && !defined(lint)
10 #if 0
11 static char	elsieid[] = "@(#)localtime.c	7.78";
12 #else
13 __RCSID("$NetBSD: localtime.c,v 1.35 2003/12/20 00:12:05 kleink Exp $");
14 #endif
15 #endif /* LIBC_SCCS and not lint */
16 
17 /*
18 ** Leap second handling from Bradley White (bww@k.gp.cs.cmu.edu).
19 ** POSIX-style TZ environment variable handling from Guy Harris
20 ** (guy@auspex.com).
21 */
22 
23 /*LINTLIBRARY*/
24 
25 #include "namespace.h"
26 #include "private.h"
27 #include "tzfile.h"
28 #include "fcntl.h"
29 #include "reentrant.h"
30 
31 #ifdef __weak_alias
32 __weak_alias(ctime_r,_ctime_r)
33 __weak_alias(daylight,_daylight)
34 __weak_alias(gmtime_r,_gmtime_r)
35 __weak_alias(localtime_r,_localtime_r)
36 __weak_alias(offtime,_offtime)
37 __weak_alias(posix2time,_posix2time)
38 __weak_alias(time2posix,_time2posix)
39 __weak_alias(timegm,_timegm)
40 __weak_alias(timelocal,_timelocal)
41 __weak_alias(timeoff,_timeoff)
42 __weak_alias(tzname,_tzname)
43 __weak_alias(tzset,_tzset)
44 __weak_alias(tzsetwall,_tzsetwall)
45 #endif
46 
47 /*
48 ** SunOS 4.1.1 headers lack O_BINARY.
49 */
50 
51 #ifdef O_BINARY
52 #define OPEN_MODE	(O_RDONLY | O_BINARY)
53 #endif /* defined O_BINARY */
54 #ifndef O_BINARY
55 #define OPEN_MODE	O_RDONLY
56 #endif /* !defined O_BINARY */
57 
58 #ifndef WILDABBR
59 /*
60 ** Someone might make incorrect use of a time zone abbreviation:
61 **	1.	They might reference tzname[0] before calling tzset (explicitly
62 **		or implicitly).
63 **	2.	They might reference tzname[1] before calling tzset (explicitly
64 **		or implicitly).
65 **	3.	They might reference tzname[1] after setting to a time zone
66 **		in which Daylight Saving Time is never observed.
67 **	4.	They might reference tzname[0] after setting to a time zone
68 **		in which Standard Time is never observed.
69 **	5.	They might reference tm.TM_ZONE after calling offtime.
70 ** What's best to do in the above cases is open to debate;
71 ** for now, we just set things up so that in any of the five cases
72 ** WILDABBR is used.  Another possibility:  initialize tzname[0] to the
73 ** string "tzname[0] used before set", and similarly for the other cases.
74 ** And another:  initialize tzname[0] to "ERA", with an explanation in the
75 ** manual page of what this "time zone abbreviation" means (doing this so
76 ** that tzname[0] has the "normal" length of three characters).
77 */
78 #define WILDABBR	"   "
79 #endif /* !defined WILDABBR */
80 
81 static const char	wildabbr[] = "WILDABBR";
82 
83 static const char	gmt[] = "GMT";
84 
85 /*
86 ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
87 ** We default to US rules as of 1999-08-17.
88 ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
89 ** implementation dependent; for historical reasons, US rules are a
90 ** common default.
91 */
92 #ifndef TZDEFRULESTRING
93 #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
94 #endif /* !defined TZDEFDST */
95 
96 struct ttinfo {				/* time type information */
97 	long		tt_gmtoff;	/* UTC offset in seconds */
98 	int		tt_isdst;	/* used to set tm_isdst */
99 	int		tt_abbrind;	/* abbreviation list index */
100 	int		tt_ttisstd;	/* TRUE if transition is std time */
101 	int		tt_ttisgmt;	/* TRUE if transition is UTC */
102 };
103 
104 struct lsinfo {				/* leap second information */
105 	time_t		ls_trans;	/* transition time */
106 	long		ls_corr;	/* correction to apply */
107 };
108 
109 #define BIGGEST(a, b)	(((a) > (b)) ? (a) : (b))
110 
111 #ifdef TZNAME_MAX
112 #define MY_TZNAME_MAX	TZNAME_MAX
113 #endif /* defined TZNAME_MAX */
114 #ifndef TZNAME_MAX
115 #define MY_TZNAME_MAX	255
116 #endif /* !defined TZNAME_MAX */
117 
118 struct state {
119 	int		leapcnt;
120 	int		timecnt;
121 	int		typecnt;
122 	int		charcnt;
123 	time_t		ats[TZ_MAX_TIMES];
124 	unsigned char	types[TZ_MAX_TIMES];
125 	struct ttinfo	ttis[TZ_MAX_TYPES];
126 	char		chars[/* LINTED constant */BIGGEST(BIGGEST(TZ_MAX_CHARS + 1, sizeof gmt),
127 				(2 * (MY_TZNAME_MAX + 1)))];
128 	struct lsinfo	lsis[TZ_MAX_LEAPS];
129 };
130 
131 struct rule {
132 	int		r_type;		/* type of rule--see below */
133 	int		r_day;		/* day number of rule */
134 	int		r_week;		/* week number of rule */
135 	int		r_mon;		/* month number of rule */
136 	long		r_time;		/* transition time of rule */
137 };
138 
139 #define JULIAN_DAY		0	/* Jn - Julian day */
140 #define DAY_OF_YEAR		1	/* n - day of year */
141 #define MONTH_NTH_DAY_OF_WEEK	2	/* Mm.n.d - month, week, day of week */
142 
143 /*
144 ** Prototypes for static functions.
145 */
146 
147 static long		detzcode P((const char * codep));
148 static const char *	getzname P((const char * strp));
149 static const char *	getnum P((const char * strp, int * nump, int min,
150 				int max));
151 static const char *	getsecs P((const char * strp, long * secsp));
152 static const char *	getoffset P((const char * strp, long * offsetp));
153 static const char *	getrule P((const char * strp, struct rule * rulep));
154 static void		gmtload P((struct state * sp));
155 static void		gmtsub P((const time_t * timep, long offset,
156 				struct tm * tmp));
157 static void		localsub P((const time_t * timep, long offset,
158 				struct tm * tmp));
159 static int		increment_overflow P((int * number, int delta));
160 static int		normalize_overflow P((int * tensptr, int * unitsptr,
161 				int base));
162 static void		settzname P((void));
163 static time_t		time1 P((struct tm * tmp,
164 				void(*funcp) P((const time_t *,
165 				long, struct tm *)),
166 				long offset));
167 static time_t		time2 P((struct tm *tmp,
168 				void(*funcp) P((const time_t *,
169 				long, struct tm*)),
170 				long offset, int * okayp));
171 static time_t		time2sub P((struct tm *tmp,
172 				void(*funcp) P((const time_t *,
173 				long, struct tm*)),
174 				long offset, int * okayp, int do_norm_secs));
175 static void		timesub P((const time_t * timep, long offset,
176 				const struct state * sp, struct tm * tmp));
177 static int		tmcomp P((const struct tm * atmp,
178 				const struct tm * btmp));
179 static time_t		transtime P((time_t janfirst, int year,
180 				const struct rule * rulep, long offset));
181 static int		tzload P((const char * name, struct state * sp));
182 static int		tzparse P((const char * name, struct state * sp,
183 				int lastditch));
184 static void		tzset_unlocked P((void));
185 static void		tzsetwall_unlocked P((void));
186 #ifdef STD_INSPIRED
187 static long		leapcorr P((time_t * timep));
188 #endif
189 
190 #ifdef ALL_STATE
191 static struct state *	lclptr;
192 static struct state *	gmtptr;
193 #endif /* defined ALL_STATE */
194 
195 #ifndef ALL_STATE
196 static struct state	lclmem;
197 static struct state	gmtmem;
198 #define lclptr		(&lclmem)
199 #define gmtptr		(&gmtmem)
200 #endif /* State Farm */
201 
202 #ifndef TZ_STRLEN_MAX
203 #define TZ_STRLEN_MAX 255
204 #endif /* !defined TZ_STRLEN_MAX */
205 
206 static char		lcl_TZname[TZ_STRLEN_MAX + 1];
207 static int		lcl_is_set;
208 static int		gmt_is_set;
209 
210 __aconst char *		tzname[2] = {
211 	/* LINTED const castaway */
212 	(__aconst char *)wildabbr,
213 	/* LINTED const castaway */
214 	(__aconst char *)wildabbr
215 };
216 
217 #ifdef _REENTRANT
218 static rwlock_t lcl_lock = RWLOCK_INITIALIZER;
219 #endif
220 
221 /*
222 ** Section 4.12.3 of X3.159-1989 requires that
223 **	Except for the strftime function, these functions [asctime,
224 **	ctime, gmtime, localtime] return values in one of two static
225 **	objects: a broken-down time structure and an array of char.
226 ** Thanks to Paul Eggert (eggert@twinsun.com) for noting this.
227 */
228 
229 static struct tm	tm;
230 
231 #ifdef USG_COMPAT
232 long int		timezone = 0;
233 int			daylight = 0;
234 #endif /* defined USG_COMPAT */
235 
236 #ifdef ALTZONE
237 time_t			altzone = 0;
238 #endif /* defined ALTZONE */
239 
240 static long
241 detzcode(codep)
242 const char * const	codep;
243 {
244 	register long	result;
245 
246 	/*
247         ** The first character must be sign extended on systems with >32bit
248         ** longs.  This was solved differently in the master tzcode sources
249         ** (the fix first appeared in tzcode95c.tar.gz).  But I believe
250 	** that this implementation is superior.
251         */
252 
253 #define SIGN_EXTEND_CHAR(x)	((signed char) x)
254 
255 	result = (SIGN_EXTEND_CHAR(codep[0]) << 24) \
256 	       | (codep[1] & 0xff) << 16 \
257 	       | (codep[2] & 0xff) << 8
258 	       | (codep[3] & 0xff);
259 	return result;
260 }
261 
262 static void
263 settzname P((void))
264 {
265 	register struct state * const	sp = lclptr;
266 	register int			i;
267 
268 	/* LINTED const castaway */
269 	tzname[0] = (__aconst char *)wildabbr;
270 	/* LINTED const castaway */
271 	tzname[1] = (__aconst char *)wildabbr;
272 #ifdef USG_COMPAT
273 	daylight = 0;
274 	timezone = 0;
275 #endif /* defined USG_COMPAT */
276 #ifdef ALTZONE
277 	altzone = 0;
278 #endif /* defined ALTZONE */
279 #ifdef ALL_STATE
280 	if (sp == NULL) {
281 		tzname[0] = tzname[1] = (__aconst char *)gmt;
282 		return;
283 	}
284 #endif /* defined ALL_STATE */
285 	for (i = 0; i < sp->typecnt; ++i) {
286 		register const struct ttinfo * const	ttisp = &sp->ttis[i];
287 
288 		tzname[ttisp->tt_isdst] =
289 			&sp->chars[ttisp->tt_abbrind];
290 #ifdef USG_COMPAT
291 		if (ttisp->tt_isdst)
292 			daylight = 1;
293 		if (i == 0 || !ttisp->tt_isdst)
294 			timezone = -(ttisp->tt_gmtoff);
295 #endif /* defined USG_COMPAT */
296 #ifdef ALTZONE
297 		if (i == 0 || ttisp->tt_isdst)
298 			altzone = -(ttisp->tt_gmtoff);
299 #endif /* defined ALTZONE */
300 	}
301 	/*
302 	** And to get the latest zone names into tzname. . .
303 	*/
304 	for (i = 0; i < sp->timecnt; ++i) {
305 		register const struct ttinfo * const	ttisp =
306 							&sp->ttis[
307 								sp->types[i]];
308 
309 		tzname[ttisp->tt_isdst] =
310 			&sp->chars[ttisp->tt_abbrind];
311 	}
312 }
313 
314 static int
315 tzload(name, sp)
316 register const char *		name;
317 register struct state * const	sp;
318 {
319 	register const char *	p;
320 	register int		i;
321 	register int		fid;
322 
323 	if (name == NULL && (name = TZDEFAULT) == NULL)
324 		return -1;
325 
326 	{
327 		register int	doaccess;
328 		/*
329 		** Section 4.9.1 of the C standard says that
330 		** "FILENAME_MAX expands to an integral constant expression
331 		** that is the size needed for an array of char large enough
332 		** to hold the longest file name string that the implementation
333 		** guarantees can be opened."
334 		*/
335 		char		fullname[FILENAME_MAX + 1];
336 
337 		if (name[0] == ':')
338 			++name;
339 		doaccess = name[0] == '/';
340 		if (!doaccess) {
341 			if ((p = TZDIR) == NULL)
342 				return -1;
343 			if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
344 				return -1;
345 			(void) strcpy(fullname, p);	/* XXX strcpy is safe */
346 			(void) strcat(fullname, "/");	/* XXX strcat is safe */
347 			(void) strcat(fullname, name);	/* XXX strcat is safe */
348 			/*
349 			** Set doaccess if '.' (as in "../") shows up in name.
350 			*/
351 			if (strchr(name, '.') != NULL)
352 				doaccess = TRUE;
353 			name = fullname;
354 		}
355 		if (doaccess && access(name, R_OK) != 0)
356 			return -1;
357 		/*
358 		 * XXX potential security problem here if user of a set-id
359 		 * program has set TZ (which is passed in as name) here,
360 		 * and uses a race condition trick to defeat the access(2)
361 		 * above.
362 		 */
363 		if ((fid = open(name, OPEN_MODE)) == -1)
364 			return -1;
365 	}
366 	{
367 		struct tzhead *	tzhp;
368 		union {
369 			struct tzhead	tzhead;
370 			char		buf[sizeof *sp + sizeof *tzhp];
371 		} u;
372 		int		ttisstdcnt;
373 		int		ttisgmtcnt;
374 
375 		i = read(fid, u.buf, sizeof u.buf);
376 		if (close(fid) != 0)
377 			return -1;
378 		ttisstdcnt = (int) detzcode(u.tzhead.tzh_ttisstdcnt);
379 		ttisgmtcnt = (int) detzcode(u.tzhead.tzh_ttisgmtcnt);
380 		sp->leapcnt = (int) detzcode(u.tzhead.tzh_leapcnt);
381 		sp->timecnt = (int) detzcode(u.tzhead.tzh_timecnt);
382 		sp->typecnt = (int) detzcode(u.tzhead.tzh_typecnt);
383 		sp->charcnt = (int) detzcode(u.tzhead.tzh_charcnt);
384 		p = u.tzhead.tzh_charcnt + sizeof u.tzhead.tzh_charcnt;
385 		if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
386 			sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
387 			sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
388 			sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
389 			(ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
390 			(ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
391 				return -1;
392 		if (i - (p - u.buf) < sp->timecnt * 4 +	/* ats */
393 			sp->timecnt +			/* types */
394 			sp->typecnt * (4 + 2) +		/* ttinfos */
395 			sp->charcnt +			/* chars */
396 			sp->leapcnt * (4 + 4) +		/* lsinfos */
397 			ttisstdcnt +			/* ttisstds */
398 			ttisgmtcnt)			/* ttisgmts */
399 				return -1;
400 		for (i = 0; i < sp->timecnt; ++i) {
401 			sp->ats[i] = detzcode(p);
402 			p += 4;
403 		}
404 		for (i = 0; i < sp->timecnt; ++i) {
405 			sp->types[i] = (unsigned char) *p++;
406 			if (sp->types[i] >= sp->typecnt)
407 				return -1;
408 		}
409 		for (i = 0; i < sp->typecnt; ++i) {
410 			register struct ttinfo *	ttisp;
411 
412 			ttisp = &sp->ttis[i];
413 			ttisp->tt_gmtoff = detzcode(p);
414 			p += 4;
415 			ttisp->tt_isdst = (unsigned char) *p++;
416 			if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
417 				return -1;
418 			ttisp->tt_abbrind = (unsigned char) *p++;
419 			if (ttisp->tt_abbrind < 0 ||
420 				ttisp->tt_abbrind > sp->charcnt)
421 					return -1;
422 		}
423 		for (i = 0; i < sp->charcnt; ++i)
424 			sp->chars[i] = *p++;
425 		sp->chars[i] = '\0';	/* ensure '\0' at end */
426 		for (i = 0; i < sp->leapcnt; ++i) {
427 			register struct lsinfo *	lsisp;
428 
429 			lsisp = &sp->lsis[i];
430 			lsisp->ls_trans = detzcode(p);
431 			p += 4;
432 			lsisp->ls_corr = detzcode(p);
433 			p += 4;
434 		}
435 		for (i = 0; i < sp->typecnt; ++i) {
436 			register struct ttinfo *	ttisp;
437 
438 			ttisp = &sp->ttis[i];
439 			if (ttisstdcnt == 0)
440 				ttisp->tt_ttisstd = FALSE;
441 			else {
442 				ttisp->tt_ttisstd = *p++;
443 				if (ttisp->tt_ttisstd != TRUE &&
444 					ttisp->tt_ttisstd != FALSE)
445 						return -1;
446 			}
447 		}
448 		for (i = 0; i < sp->typecnt; ++i) {
449 			register struct ttinfo *	ttisp;
450 
451 			ttisp = &sp->ttis[i];
452 			if (ttisgmtcnt == 0)
453 				ttisp->tt_ttisgmt = FALSE;
454 			else {
455 				ttisp->tt_ttisgmt = *p++;
456 				if (ttisp->tt_ttisgmt != TRUE &&
457 					ttisp->tt_ttisgmt != FALSE)
458 						return -1;
459 			}
460 		}
461 	}
462 	return 0;
463 }
464 
465 static const int	mon_lengths[2][MONSPERYEAR] = {
466 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
467 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
468 };
469 
470 static const int	year_lengths[2] = {
471 	DAYSPERNYEAR, DAYSPERLYEAR
472 };
473 
474 /*
475 ** Given a pointer into a time zone string, scan until a character that is not
476 ** a valid character in a zone name is found.  Return a pointer to that
477 ** character.
478 */
479 
480 static const char *
481 getzname(strp)
482 register const char *	strp;
483 {
484 	register char	c;
485 
486 	while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
487 		c != '+')
488 			++strp;
489 	return strp;
490 }
491 
492 /*
493 ** Given a pointer into a time zone string, extract a number from that string.
494 ** Check that the number is within a specified range; if it is not, return
495 ** NULL.
496 ** Otherwise, return a pointer to the first character not part of the number.
497 */
498 
499 static const char *
500 getnum(strp, nump, min, max)
501 register const char *	strp;
502 int * const		nump;
503 const int		min;
504 const int		max;
505 {
506 	register char	c;
507 	register int	num;
508 
509 	if (strp == NULL || !is_digit(c = *strp))
510 		return NULL;
511 	num = 0;
512 	do {
513 		num = num * 10 + (c - '0');
514 		if (num > max)
515 			return NULL;	/* illegal value */
516 		c = *++strp;
517 	} while (is_digit(c));
518 	if (num < min)
519 		return NULL;		/* illegal value */
520 	*nump = num;
521 	return strp;
522 }
523 
524 /*
525 ** Given a pointer into a time zone string, extract a number of seconds,
526 ** in hh[:mm[:ss]] form, from the string.
527 ** If any error occurs, return NULL.
528 ** Otherwise, return a pointer to the first character not part of the number
529 ** of seconds.
530 */
531 
532 static const char *
533 getsecs(strp, secsp)
534 register const char *	strp;
535 long * const		secsp;
536 {
537 	int	num;
538 
539 	/*
540 	** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
541 	** "M10.4.6/26", which does not conform to Posix,
542 	** but which specifies the equivalent of
543 	** ``02:00 on the first Sunday on or after 23 Oct''.
544 	*/
545 	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
546 	if (strp == NULL)
547 		return NULL;
548 	*secsp = num * (long) SECSPERHOUR;
549 	if (*strp == ':') {
550 		++strp;
551 		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
552 		if (strp == NULL)
553 			return NULL;
554 		*secsp += num * SECSPERMIN;
555 		if (*strp == ':') {
556 			++strp;
557 			/* `SECSPERMIN' allows for leap seconds.  */
558 			strp = getnum(strp, &num, 0, SECSPERMIN);
559 			if (strp == NULL)
560 				return NULL;
561 			*secsp += num;
562 		}
563 	}
564 	return strp;
565 }
566 
567 /*
568 ** Given a pointer into a time zone string, extract an offset, in
569 ** [+-]hh[:mm[:ss]] form, from the string.
570 ** If any error occurs, return NULL.
571 ** Otherwise, return a pointer to the first character not part of the time.
572 */
573 
574 static const char *
575 getoffset(strp, offsetp)
576 register const char *	strp;
577 long * const		offsetp;
578 {
579 	register int	neg = 0;
580 
581 	if (*strp == '-') {
582 		neg = 1;
583 		++strp;
584 	} else if (*strp == '+')
585 		++strp;
586 	strp = getsecs(strp, offsetp);
587 	if (strp == NULL)
588 		return NULL;		/* illegal time */
589 	if (neg)
590 		*offsetp = -*offsetp;
591 	return strp;
592 }
593 
594 /*
595 ** Given a pointer into a time zone string, extract a rule in the form
596 ** date[/time].  See POSIX section 8 for the format of "date" and "time".
597 ** If a valid rule is not found, return NULL.
598 ** Otherwise, return a pointer to the first character not part of the rule.
599 */
600 
601 static const char *
602 getrule(strp, rulep)
603 const char *			strp;
604 register struct rule * const	rulep;
605 {
606 	if (*strp == 'J') {
607 		/*
608 		** Julian day.
609 		*/
610 		rulep->r_type = JULIAN_DAY;
611 		++strp;
612 		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
613 	} else if (*strp == 'M') {
614 		/*
615 		** Month, week, day.
616 		*/
617 		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
618 		++strp;
619 		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
620 		if (strp == NULL)
621 			return NULL;
622 		if (*strp++ != '.')
623 			return NULL;
624 		strp = getnum(strp, &rulep->r_week, 1, 5);
625 		if (strp == NULL)
626 			return NULL;
627 		if (*strp++ != '.')
628 			return NULL;
629 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
630 	} else if (is_digit(*strp)) {
631 		/*
632 		** Day of year.
633 		*/
634 		rulep->r_type = DAY_OF_YEAR;
635 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
636 	} else	return NULL;		/* invalid format */
637 	if (strp == NULL)
638 		return NULL;
639 	if (*strp == '/') {
640 		/*
641 		** Time specified.
642 		*/
643 		++strp;
644 		strp = getsecs(strp, &rulep->r_time);
645 	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */
646 	return strp;
647 }
648 
649 /*
650 ** Given the Epoch-relative time of January 1, 00:00:00 UTC, in a year, the
651 ** year, a rule, and the offset from UTC at the time that rule takes effect,
652 ** calculate the Epoch-relative time that rule takes effect.
653 */
654 
655 static time_t
656 transtime(janfirst, year, rulep, offset)
657 const time_t				janfirst;
658 const int				year;
659 register const struct rule * const	rulep;
660 const long				offset;
661 {
662 	register int	leapyear;
663 	register time_t	value;
664 	register int	i;
665 	int		d, m1, yy0, yy1, yy2, dow;
666 
667 	INITIALIZE(value);
668 	leapyear = isleap(year);
669 	switch (rulep->r_type) {
670 
671 	case JULIAN_DAY:
672 		/*
673 		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
674 		** years.
675 		** In non-leap years, or if the day number is 59 or less, just
676 		** add SECSPERDAY times the day number-1 to the time of
677 		** January 1, midnight, to get the day.
678 		*/
679 		value = janfirst + (rulep->r_day - 1) * SECSPERDAY;
680 		if (leapyear && rulep->r_day >= 60)
681 			value += SECSPERDAY;
682 		break;
683 
684 	case DAY_OF_YEAR:
685 		/*
686 		** n - day of year.
687 		** Just add SECSPERDAY times the day number to the time of
688 		** January 1, midnight, to get the day.
689 		*/
690 		value = janfirst + rulep->r_day * SECSPERDAY;
691 		break;
692 
693 	case MONTH_NTH_DAY_OF_WEEK:
694 		/*
695 		** Mm.n.d - nth "dth day" of month m.
696 		*/
697 		value = janfirst;
698 		for (i = 0; i < rulep->r_mon - 1; ++i)
699 			value += mon_lengths[leapyear][i] * SECSPERDAY;
700 
701 		/*
702 		** Use Zeller's Congruence to get day-of-week of first day of
703 		** month.
704 		*/
705 		m1 = (rulep->r_mon + 9) % 12 + 1;
706 		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
707 		yy1 = yy0 / 100;
708 		yy2 = yy0 % 100;
709 		dow = ((26 * m1 - 2) / 10 +
710 			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
711 		if (dow < 0)
712 			dow += DAYSPERWEEK;
713 
714 		/*
715 		** "dow" is the day-of-week of the first day of the month.  Get
716 		** the day-of-month (zero-origin) of the first "dow" day of the
717 		** month.
718 		*/
719 		d = rulep->r_day - dow;
720 		if (d < 0)
721 			d += DAYSPERWEEK;
722 		for (i = 1; i < rulep->r_week; ++i) {
723 			if (d + DAYSPERWEEK >=
724 				mon_lengths[leapyear][rulep->r_mon - 1])
725 					break;
726 			d += DAYSPERWEEK;
727 		}
728 
729 		/*
730 		** "d" is the day-of-month (zero-origin) of the day we want.
731 		*/
732 		value += d * SECSPERDAY;
733 		break;
734 	}
735 
736 	/*
737 	** "value" is the Epoch-relative time of 00:00:00 UTC on the day in
738 	** question.  To get the Epoch-relative time of the specified local
739 	** time on that day, add the transition time and the current offset
740 	** from UTC.
741 	*/
742 	return value + rulep->r_time + offset;
743 }
744 
745 /*
746 ** Given a POSIX section 8-style TZ string, fill in the rule tables as
747 ** appropriate.
748 */
749 
750 static int
751 tzparse(name, sp, lastditch)
752 const char *			name;
753 register struct state * const	sp;
754 const int			lastditch;
755 {
756 	const char *			stdname;
757 	const char *			dstname;
758 	size_t				stdlen;
759 	size_t				dstlen;
760 	long				stdoffset;
761 	long				dstoffset;
762 	register time_t *		atp;
763 	register unsigned char *	typep;
764 	register char *			cp;
765 	register int			load_result;
766 
767 	INITIALIZE(dstname);
768 	stdname = name;
769 	if (lastditch) {
770 		stdlen = strlen(name);	/* length of standard zone name */
771 		name += stdlen;
772 		if (stdlen >= sizeof sp->chars)
773 			stdlen = (sizeof sp->chars) - 1;
774 		stdoffset = 0;
775 	} else {
776 		name = getzname(name);
777 		stdlen = name - stdname;
778 		if (stdlen < 3)
779 			return -1;
780 		if (*name == '\0')
781 			return -1;
782 		name = getoffset(name, &stdoffset);
783 		if (name == NULL)
784 			return -1;
785 	}
786 	load_result = tzload(TZDEFRULES, sp);
787 	if (load_result != 0)
788 		sp->leapcnt = 0;		/* so, we're off a little */
789 	if (*name != '\0') {
790 		dstname = name;
791 		name = getzname(name);
792 		dstlen = name - dstname;	/* length of DST zone name */
793 		if (dstlen < 3)
794 			return -1;
795 		if (*name != '\0' && *name != ',' && *name != ';') {
796 			name = getoffset(name, &dstoffset);
797 			if (name == NULL)
798 				return -1;
799 		} else	dstoffset = stdoffset - SECSPERHOUR;
800 		if (*name == '\0' && load_result != 0)
801 			name = TZDEFRULESTRING;
802 		if (*name == ',' || *name == ';') {
803 			struct rule	start;
804 			struct rule	end;
805 			register int	year;
806 			register time_t	janfirst;
807 			time_t		starttime;
808 			time_t		endtime;
809 
810 			++name;
811 			if ((name = getrule(name, &start)) == NULL)
812 				return -1;
813 			if (*name++ != ',')
814 				return -1;
815 			if ((name = getrule(name, &end)) == NULL)
816 				return -1;
817 			if (*name != '\0')
818 				return -1;
819 			sp->typecnt = 2;	/* standard time and DST */
820 			/*
821 			** Two transitions per year, from EPOCH_YEAR to 2037.
822 			*/
823 			sp->timecnt = 2 * (2037 - EPOCH_YEAR + 1);
824 			if (sp->timecnt > TZ_MAX_TIMES)
825 				return -1;
826 			sp->ttis[0].tt_gmtoff = -dstoffset;
827 			sp->ttis[0].tt_isdst = 1;
828 			sp->ttis[0].tt_abbrind = stdlen + 1;
829 			sp->ttis[1].tt_gmtoff = -stdoffset;
830 			sp->ttis[1].tt_isdst = 0;
831 			sp->ttis[1].tt_abbrind = 0;
832 			atp = sp->ats;
833 			typep = sp->types;
834 			janfirst = 0;
835 			for (year = EPOCH_YEAR; year <= 2037; ++year) {
836 				starttime = transtime(janfirst, year, &start,
837 					stdoffset);
838 				endtime = transtime(janfirst, year, &end,
839 					dstoffset);
840 				if (starttime > endtime) {
841 					*atp++ = endtime;
842 					*typep++ = 1;	/* DST ends */
843 					*atp++ = starttime;
844 					*typep++ = 0;	/* DST begins */
845 				} else {
846 					*atp++ = starttime;
847 					*typep++ = 0;	/* DST begins */
848 					*atp++ = endtime;
849 					*typep++ = 1;	/* DST ends */
850 				}
851 				janfirst += year_lengths[isleap(year)] *
852 					SECSPERDAY;
853 			}
854 		} else {
855 			register long	theirstdoffset;
856 			register long	theirdstoffset;
857 			register long	theiroffset;
858 			register int	isdst;
859 			register int	i;
860 			register int	j;
861 
862 			if (*name != '\0')
863 				return -1;
864 			/*
865 			** Initial values of theirstdoffset and theirdstoffset.
866 			*/
867 			theirstdoffset = 0;
868 			for (i = 0; i < sp->timecnt; ++i) {
869 				j = sp->types[i];
870 				if (!sp->ttis[j].tt_isdst) {
871 					theirstdoffset =
872 						-sp->ttis[j].tt_gmtoff;
873 					break;
874 				}
875 			}
876 			theirdstoffset = 0;
877 			for (i = 0; i < sp->timecnt; ++i) {
878 				j = sp->types[i];
879 				if (sp->ttis[j].tt_isdst) {
880 					theirdstoffset =
881 						-sp->ttis[j].tt_gmtoff;
882 					break;
883 				}
884 			}
885 			/*
886 			** Initially we're assumed to be in standard time.
887 			*/
888 			isdst = FALSE;
889 			theiroffset = theirstdoffset;
890 			/*
891 			** Now juggle transition times and types
892 			** tracking offsets as you do.
893 			*/
894 			for (i = 0; i < sp->timecnt; ++i) {
895 				j = sp->types[i];
896 				sp->types[i] = sp->ttis[j].tt_isdst;
897 				if (sp->ttis[j].tt_ttisgmt) {
898 					/* No adjustment to transition time */
899 				} else {
900 					/*
901 					** If summer time is in effect, and the
902 					** transition time was not specified as
903 					** standard time, add the summer time
904 					** offset to the transition time;
905 					** otherwise, add the standard time
906 					** offset to the transition time.
907 					*/
908 					/*
909 					** Transitions from DST to DDST
910 					** will effectively disappear since
911 					** POSIX provides for only one DST
912 					** offset.
913 					*/
914 					if (isdst && !sp->ttis[j].tt_ttisstd) {
915 						sp->ats[i] += dstoffset -
916 							theirdstoffset;
917 					} else {
918 						sp->ats[i] += stdoffset -
919 							theirstdoffset;
920 					}
921 				}
922 				theiroffset = -sp->ttis[j].tt_gmtoff;
923 				if (sp->ttis[j].tt_isdst)
924 					theirdstoffset = theiroffset;
925 				else	theirstdoffset = theiroffset;
926 			}
927 			/*
928 			** Finally, fill in ttis.
929 			** ttisstd and ttisgmt need not be handled.
930 			*/
931 			sp->ttis[0].tt_gmtoff = -stdoffset;
932 			sp->ttis[0].tt_isdst = FALSE;
933 			sp->ttis[0].tt_abbrind = 0;
934 			sp->ttis[1].tt_gmtoff = -dstoffset;
935 			sp->ttis[1].tt_isdst = TRUE;
936 			sp->ttis[1].tt_abbrind = stdlen + 1;
937 			sp->typecnt = 2;
938 		}
939 	} else {
940 		dstlen = 0;
941 		sp->typecnt = 1;		/* only standard time */
942 		sp->timecnt = 0;
943 		sp->ttis[0].tt_gmtoff = -stdoffset;
944 		sp->ttis[0].tt_isdst = 0;
945 		sp->ttis[0].tt_abbrind = 0;
946 	}
947 	sp->charcnt = stdlen + 1;
948 	if (dstlen != 0)
949 		sp->charcnt += dstlen + 1;
950 	if ((size_t) sp->charcnt > sizeof sp->chars)
951 		return -1;
952 	cp = sp->chars;
953 	(void) strncpy(cp, stdname, stdlen);
954 	cp += stdlen;
955 	*cp++ = '\0';
956 	if (dstlen != 0) {
957 		(void) strncpy(cp, dstname, dstlen);
958 		*(cp + dstlen) = '\0';
959 	}
960 	return 0;
961 }
962 
963 static void
964 gmtload(sp)
965 struct state * const	sp;
966 {
967 	if (tzload(gmt, sp) != 0)
968 		(void) tzparse(gmt, sp, TRUE);
969 }
970 
971 static void
972 tzsetwall_unlocked P((void))
973 {
974 	if (lcl_is_set < 0)
975 		return;
976 	lcl_is_set = -1;
977 
978 #ifdef ALL_STATE
979 	if (lclptr == NULL) {
980 		lclptr = (struct state *) malloc(sizeof *lclptr);
981 		if (lclptr == NULL) {
982 			settzname();	/* all we can do */
983 			return;
984 		}
985 	}
986 #endif /* defined ALL_STATE */
987 	if (tzload((char *) NULL, lclptr) != 0)
988 		gmtload(lclptr);
989 	settzname();
990 }
991 
992 #ifndef STD_INSPIRED
993 /*
994 ** A non-static declaration of tzsetwall in a system header file
995 ** may cause a warning about this upcoming static declaration...
996 */
997 static
998 #endif /* !defined STD_INSPIRED */
999 void
1000 tzsetwall P((void))
1001 {
1002 	rwlock_wrlock(&lcl_lock);
1003 	tzsetwall_unlocked();
1004 	rwlock_unlock(&lcl_lock);
1005 }
1006 
1007 static void
1008 tzset_unlocked P((void))
1009 {
1010 	register const char *	name;
1011 
1012 	name = getenv("TZ");
1013 	if (name == NULL) {
1014 		tzsetwall_unlocked();
1015 		return;
1016 	}
1017 
1018 	if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
1019 		return;
1020 	lcl_is_set = strlen(name) < sizeof lcl_TZname;
1021 	if (lcl_is_set)
1022 		(void)strlcpy(lcl_TZname, name, sizeof(lcl_TZname));
1023 
1024 #ifdef ALL_STATE
1025 	if (lclptr == NULL) {
1026 		lclptr = (struct state *) malloc(sizeof *lclptr);
1027 		if (lclptr == NULL) {
1028 			settzname();	/* all we can do */
1029 			return;
1030 		}
1031 	}
1032 #endif /* defined ALL_STATE */
1033 	if (*name == '\0') {
1034 		/*
1035 		** User wants it fast rather than right.
1036 		*/
1037 		lclptr->leapcnt = 0;		/* so, we're off a little */
1038 		lclptr->timecnt = 0;
1039 		lclptr->typecnt = 0;
1040 		lclptr->ttis[0].tt_isdst = 0;
1041 		lclptr->ttis[0].tt_gmtoff = 0;
1042 		lclptr->ttis[0].tt_abbrind = 0;
1043 		(void)strlcpy(lclptr->chars, gmt, sizeof(lclptr->chars));
1044 	} else if (tzload(name, lclptr) != 0)
1045 		if (name[0] == ':' || tzparse(name, lclptr, FALSE) != 0)
1046 			(void) gmtload(lclptr);
1047 	settzname();
1048 }
1049 
1050 void
1051 tzset P((void))
1052 {
1053 	rwlock_wrlock(&lcl_lock);
1054 	tzset_unlocked();
1055 	rwlock_unlock(&lcl_lock);
1056 }
1057 
1058 /*
1059 ** The easy way to behave "as if no library function calls" localtime
1060 ** is to not call it--so we drop its guts into "localsub", which can be
1061 ** freely called.  (And no, the PANS doesn't require the above behavior--
1062 ** but it *is* desirable.)
1063 **
1064 ** The unused offset argument is for the benefit of mktime variants.
1065 */
1066 
1067 /*ARGSUSED*/
1068 static void
1069 localsub(timep, offset, tmp)
1070 const time_t * const	timep;
1071 const long		offset;
1072 struct tm * const	tmp;
1073 {
1074 	register struct state *		sp;
1075 	register const struct ttinfo *	ttisp;
1076 	register int			i;
1077 	const time_t			t = *timep;
1078 
1079 	sp = lclptr;
1080 #ifdef ALL_STATE
1081 	if (sp == NULL) {
1082 		gmtsub(timep, offset, tmp);
1083 		return;
1084 	}
1085 #endif /* defined ALL_STATE */
1086 	if (sp->timecnt == 0 || t < sp->ats[0]) {
1087 		i = 0;
1088 		while (sp->ttis[i].tt_isdst)
1089 			if (++i >= sp->typecnt) {
1090 				i = 0;
1091 				break;
1092 			}
1093 	} else {
1094 		for (i = 1; i < sp->timecnt; ++i)
1095 			if (t < sp->ats[i])
1096 				break;
1097 		i = sp->types[i - 1];
1098 	}
1099 	ttisp = &sp->ttis[i];
1100 	/*
1101 	** To get (wrong) behavior that's compatible with System V Release 2.0
1102 	** you'd replace the statement below with
1103 	**	t += ttisp->tt_gmtoff;
1104 	**	timesub(&t, 0L, sp, tmp);
1105 	*/
1106 	timesub(&t, ttisp->tt_gmtoff, sp, tmp);
1107 	tmp->tm_isdst = ttisp->tt_isdst;
1108 	tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
1109 #ifdef TM_ZONE
1110 	tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
1111 #endif /* defined TM_ZONE */
1112 }
1113 
1114 struct tm *
1115 localtime(timep)
1116 const time_t * const	timep;
1117 {
1118 	rwlock_wrlock(&lcl_lock);
1119 	tzset_unlocked();
1120 	localsub(timep, 0L, &tm);
1121 	rwlock_unlock(&lcl_lock);
1122 	return &tm;
1123 }
1124 
1125 /*
1126 ** Re-entrant version of localtime.
1127 */
1128 
1129 struct tm *
1130 localtime_r(timep, tmp)
1131 const time_t * const	timep;
1132 struct tm *		tmp;
1133 {
1134 	rwlock_rdlock(&lcl_lock);
1135 	localsub(timep, 0L, tmp);
1136 	rwlock_unlock(&lcl_lock);
1137 	return tmp;
1138 }
1139 
1140 /*
1141 ** gmtsub is to gmtime as localsub is to localtime.
1142 */
1143 
1144 static void
1145 gmtsub(timep, offset, tmp)
1146 const time_t * const	timep;
1147 const long		offset;
1148 struct tm * const	tmp;
1149 {
1150 #ifdef _REENTRANT
1151 	static mutex_t gmt_mutex = MUTEX_INITIALIZER;
1152 #endif
1153 
1154 	mutex_lock(&gmt_mutex);
1155 	if (!gmt_is_set) {
1156 		gmt_is_set = TRUE;
1157 #ifdef ALL_STATE
1158 		gmtptr = (struct state *) malloc(sizeof *gmtptr);
1159 		if (gmtptr != NULL)
1160 #endif /* defined ALL_STATE */
1161 			gmtload(gmtptr);
1162 	}
1163 	mutex_unlock(&gmt_mutex);
1164 	timesub(timep, offset, gmtptr, tmp);
1165 #ifdef TM_ZONE
1166 	/*
1167 	** Could get fancy here and deliver something such as
1168 	** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
1169 	** but this is no time for a treasure hunt.
1170 	*/
1171 	if (offset != 0)
1172 		/* LINTED const castaway */
1173 		tmp->TM_ZONE = (__aconst char *)wildabbr;
1174 	else {
1175 #ifdef ALL_STATE
1176 		if (gmtptr == NULL)
1177 			tmp->TM_ZONE = (__aconst char *)gmt;
1178 		else	tmp->TM_ZONE = gmtptr->chars;
1179 #endif /* defined ALL_STATE */
1180 #ifndef ALL_STATE
1181 		tmp->TM_ZONE = gmtptr->chars;
1182 #endif /* State Farm */
1183 	}
1184 #endif /* defined TM_ZONE */
1185 }
1186 
1187 struct tm *
1188 gmtime(timep)
1189 const time_t * const	timep;
1190 {
1191 	gmtsub(timep, 0L, &tm);
1192 	return &tm;
1193 }
1194 
1195 /*
1196 ** Re-entrant version of gmtime.
1197 */
1198 
1199 struct tm *
1200 gmtime_r(timep, tmp)
1201 const time_t * const	timep;
1202 struct tm *		tmp;
1203 {
1204 	gmtsub(timep, 0L, tmp);
1205 	return tmp;
1206 }
1207 
1208 #ifdef STD_INSPIRED
1209 
1210 struct tm *
1211 offtime(timep, offset)
1212 const time_t * const	timep;
1213 const long		offset;
1214 {
1215 	gmtsub(timep, offset, &tm);
1216 	return &tm;
1217 }
1218 
1219 #endif /* defined STD_INSPIRED */
1220 
1221 static void
1222 timesub(timep, offset, sp, tmp)
1223 const time_t * const			timep;
1224 const long				offset;
1225 register const struct state * const	sp;
1226 register struct tm * const		tmp;
1227 {
1228 	register const struct lsinfo *	lp;
1229 	register long			days;
1230 	register long			rem;
1231 	register int			y;
1232 	register int			yleap;
1233 	register const int *		ip;
1234 	register long			corr;
1235 	register int			hit;
1236 	register int			i;
1237 
1238 	corr = 0;
1239 	hit = 0;
1240 #ifdef ALL_STATE
1241 	i = (sp == NULL) ? 0 : sp->leapcnt;
1242 #endif /* defined ALL_STATE */
1243 #ifndef ALL_STATE
1244 	i = sp->leapcnt;
1245 #endif /* State Farm */
1246 	while (--i >= 0) {
1247 		lp = &sp->lsis[i];
1248 		if (*timep >= lp->ls_trans) {
1249 			if (*timep == lp->ls_trans) {
1250 				hit = ((i == 0 && lp->ls_corr > 0) ||
1251 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
1252 				if (hit)
1253 					while (i > 0 &&
1254 						sp->lsis[i].ls_trans ==
1255 						sp->lsis[i - 1].ls_trans + 1 &&
1256 						sp->lsis[i].ls_corr ==
1257 						sp->lsis[i - 1].ls_corr + 1) {
1258 							++hit;
1259 							--i;
1260 					}
1261 			}
1262 			corr = lp->ls_corr;
1263 			break;
1264 		}
1265 	}
1266 	days = *timep / SECSPERDAY;
1267 	rem = *timep % SECSPERDAY;
1268 #ifdef mc68k
1269 	if (*timep == 0x80000000) {
1270 		/*
1271 		** A 3B1 muffs the division on the most negative number.
1272 		*/
1273 		days = -24855;
1274 		rem = -11648;
1275 	}
1276 #endif /* defined mc68k */
1277 	rem += (offset - corr);
1278 	while (rem < 0) {
1279 		rem += SECSPERDAY;
1280 		--days;
1281 	}
1282 	while (rem >= SECSPERDAY) {
1283 		rem -= SECSPERDAY;
1284 		++days;
1285 	}
1286 	tmp->tm_hour = (int) (rem / SECSPERHOUR);
1287 	rem = rem % SECSPERHOUR;
1288 	tmp->tm_min = (int) (rem / SECSPERMIN);
1289 	/*
1290 	** A positive leap second requires a special
1291 	** representation.  This uses "... ??:59:60" et seq.
1292 	*/
1293 	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
1294 	tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
1295 	if (tmp->tm_wday < 0)
1296 		tmp->tm_wday += DAYSPERWEEK;
1297 	y = EPOCH_YEAR;
1298 #define LEAPS_THRU_END_OF(y)	((y) / 4 - (y) / 100 + (y) / 400)
1299 	while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
1300 		register int	newy;
1301 
1302 		newy = (int)(y + days / DAYSPERNYEAR);
1303 		if (days < 0)
1304 			--newy;
1305 		days -= (newy - y) * DAYSPERNYEAR +
1306 			LEAPS_THRU_END_OF(newy - 1) -
1307 			LEAPS_THRU_END_OF(y - 1);
1308 		y = newy;
1309 	}
1310 	tmp->tm_year = y - TM_YEAR_BASE;
1311 	tmp->tm_yday = (int) days;
1312 	ip = mon_lengths[yleap];
1313 	for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
1314 		days = days - (long) ip[tmp->tm_mon];
1315 	tmp->tm_mday = (int) (days + 1);
1316 	tmp->tm_isdst = 0;
1317 #ifdef TM_GMTOFF
1318 	tmp->TM_GMTOFF = offset;
1319 #endif /* defined TM_GMTOFF */
1320 }
1321 
1322 char *
1323 ctime(timep)
1324 const time_t * const	timep;
1325 {
1326 /*
1327 ** Section 4.12.3.2 of X3.159-1989 requires that
1328 **	The ctime function converts the calendar time pointed to by timer
1329 **	to local time in the form of a string.  It is equivalent to
1330 **		asctime(localtime(timer))
1331 */
1332 	return asctime(localtime(timep));
1333 }
1334 
1335 char *
1336 ctime_r(timep, buf)
1337 const time_t * const	timep;
1338 char *			buf;
1339 {
1340 	struct tm	tmp;
1341 
1342 	return asctime_r(localtime_r(timep, &tmp), buf);
1343 }
1344 
1345 /*
1346 ** Adapted from code provided by Robert Elz, who writes:
1347 **	The "best" way to do mktime I think is based on an idea of Bob
1348 **	Kridle's (so its said...) from a long time ago.
1349 **	[kridle@xinet.com as of 1996-01-16.]
1350 **	It does a binary search of the time_t space.  Since time_t's are
1351 **	just 32 bits, its a max of 32 iterations (even at 64 bits it
1352 **	would still be very reasonable).
1353 */
1354 
1355 #ifndef WRONG
1356 #define WRONG	(-1)
1357 #endif /* !defined WRONG */
1358 
1359 /*
1360 ** Simplified normalize logic courtesy Paul Eggert (eggert@twinsun.com).
1361 */
1362 
1363 static int
1364 increment_overflow(number, delta)
1365 int *	number;
1366 int	delta;
1367 {
1368 	int	number0;
1369 
1370 	number0 = *number;
1371 	*number += delta;
1372 	return (*number < number0) != (delta < 0);
1373 }
1374 
1375 static int
1376 normalize_overflow(tensptr, unitsptr, base)
1377 int * const	tensptr;
1378 int * const	unitsptr;
1379 const int	base;
1380 {
1381 	register int	tensdelta;
1382 
1383 	tensdelta = (*unitsptr >= 0) ?
1384 		(*unitsptr / base) :
1385 		(-1 - (-1 - *unitsptr) / base);
1386 	*unitsptr -= tensdelta * base;
1387 	return increment_overflow(tensptr, tensdelta);
1388 }
1389 
1390 static int
1391 tmcomp(atmp, btmp)
1392 register const struct tm * const atmp;
1393 register const struct tm * const btmp;
1394 {
1395 	register int	result;
1396 
1397 	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
1398 		(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
1399 		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
1400 		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
1401 		(result = (atmp->tm_min - btmp->tm_min)) == 0)
1402 			result = atmp->tm_sec - btmp->tm_sec;
1403 	return result;
1404 }
1405 
1406 static time_t
1407 time2sub(tmp, funcp, offset, okayp, do_norm_secs)
1408 struct tm * const	tmp;
1409 void (* const		funcp) P((const time_t*, long, struct tm*));
1410 const long		offset;
1411 int * const		okayp;
1412 const int		do_norm_secs;
1413 {
1414 	register const struct state *	sp;
1415 	register int			dir;
1416 	register int			bits;
1417 	register int			i, j ;
1418 	register int			saved_seconds;
1419 	time_t				newt;
1420 	time_t				t;
1421 	struct tm			yourtm, mytm;
1422 
1423 	*okayp = FALSE;
1424 	yourtm = *tmp;
1425 	if (do_norm_secs) {
1426 		if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
1427 			SECSPERMIN))
1428 				return WRONG;
1429 	}
1430 	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
1431 		return WRONG;
1432 	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
1433 		return WRONG;
1434 	if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
1435 		return WRONG;
1436 	/*
1437 	** Turn yourtm.tm_year into an actual year number for now.
1438 	** It is converted back to an offset from TM_YEAR_BASE later.
1439 	*/
1440 	if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
1441 		return WRONG;
1442 	while (yourtm.tm_mday <= 0) {
1443 		if (increment_overflow(&yourtm.tm_year, -1))
1444 			return WRONG;
1445 		i = yourtm.tm_year + (1 < yourtm.tm_mon);
1446 		yourtm.tm_mday += year_lengths[isleap(i)];
1447 	}
1448 	while (yourtm.tm_mday > DAYSPERLYEAR) {
1449 		i = yourtm.tm_year + (1 < yourtm.tm_mon);
1450 		yourtm.tm_mday -= year_lengths[isleap(i)];
1451 		if (increment_overflow(&yourtm.tm_year, 1))
1452 			return WRONG;
1453 	}
1454 	for ( ; ; ) {
1455 		i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
1456 		if (yourtm.tm_mday <= i)
1457 			break;
1458 		yourtm.tm_mday -= i;
1459 		if (++yourtm.tm_mon >= MONSPERYEAR) {
1460 			yourtm.tm_mon = 0;
1461 			if (increment_overflow(&yourtm.tm_year, 1))
1462 				return WRONG;
1463 		}
1464 	}
1465 	if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
1466 		return WRONG;
1467 	if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
1468 		saved_seconds = 0;
1469 	else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
1470 		/*
1471 		** We can't set tm_sec to 0, because that might push the
1472 		** time below the minimum representable time.
1473 		** Set tm_sec to 59 instead.
1474 		** This assumes that the minimum representable time is
1475 		** not in the same minute that a leap second was deleted from,
1476 		** which is a safer assumption than using 58 would be.
1477 		*/
1478 		if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
1479 			return WRONG;
1480 		saved_seconds = yourtm.tm_sec;
1481 		yourtm.tm_sec = SECSPERMIN - 1;
1482 	} else {
1483 		saved_seconds = yourtm.tm_sec;
1484 		yourtm.tm_sec = 0;
1485 	}
1486 	/*
1487 	** Divide the search space in half
1488 	** (this works whether time_t is signed or unsigned).
1489 	*/
1490 	bits = TYPE_BIT(time_t) - 1;
1491 	/*
1492 	** If time_t is signed, then 0 is just above the median,
1493 	** assuming two's complement arithmetic.
1494 	** If time_t is unsigned, then (1 << bits) is just above the median.
1495 	*/
1496 	/* LINTED constant in conditional context */
1497 	t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
1498 	for ( ; ; ) {
1499 		(*funcp)(&t, offset, &mytm);
1500 		dir = tmcomp(&mytm, &yourtm);
1501 		if (dir != 0) {
1502 			if (bits-- < 0)
1503 				return WRONG;
1504 			if (bits < 0)
1505 				--t; /* may be needed if new t is minimal */
1506 			else if (dir > 0)
1507 				t -= ((time_t) 1) << bits;
1508 			else	t += ((time_t) 1) << bits;
1509 			continue;
1510 		}
1511 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
1512 			break;
1513 		/*
1514 		** Right time, wrong type.
1515 		** Hunt for right time, right type.
1516 		** It's okay to guess wrong since the guess
1517 		** gets checked.
1518 		*/
1519 		/*
1520 		** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1521 		*/
1522 		sp = (const struct state *)
1523 			(((void *) funcp == (void *) localsub) ?
1524 			lclptr : gmtptr);
1525 #ifdef ALL_STATE
1526 		if (sp == NULL)
1527 			return WRONG;
1528 #endif /* defined ALL_STATE */
1529 		for (i = sp->typecnt - 1; i >= 0; --i) {
1530 			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
1531 				continue;
1532 			for (j = sp->typecnt - 1; j >= 0; --j) {
1533 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
1534 					continue;
1535 				newt = t + sp->ttis[j].tt_gmtoff -
1536 					sp->ttis[i].tt_gmtoff;
1537 				(*funcp)(&newt, offset, &mytm);
1538 				if (tmcomp(&mytm, &yourtm) != 0)
1539 					continue;
1540 				if (mytm.tm_isdst != yourtm.tm_isdst)
1541 					continue;
1542 				/*
1543 				** We have a match.
1544 				*/
1545 				t = newt;
1546 				goto label;
1547 			}
1548 		}
1549 		return WRONG;
1550 	}
1551 label:
1552 	newt = t + saved_seconds;
1553 	if ((newt < t) != (saved_seconds < 0))
1554 		return WRONG;
1555 	t = newt;
1556 	(*funcp)(&t, offset, tmp);
1557 	*okayp = TRUE;
1558 	return t;
1559 }
1560 
1561 static time_t
1562 time2(tmp, funcp, offset, okayp)
1563 struct tm * const	tmp;
1564 void (* const		funcp) P((const time_t*, long, struct tm*));
1565 const long		offset;
1566 int * const		okayp;
1567 {
1568 	time_t	t;
1569 
1570 	/*
1571 	** First try without normalization of seconds
1572 	** (in case tm_sec contains a value associated with a leap second).
1573 	** If that fails, try with normalization of seconds.
1574 	*/
1575 	t = time2sub(tmp, funcp, offset, okayp, FALSE);
1576 	return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
1577 }
1578 
1579 static time_t
1580 time1(tmp, funcp, offset)
1581 struct tm * const	tmp;
1582 void (* const		funcp) P((const time_t *, long, struct tm *));
1583 const long		offset;
1584 {
1585 	register time_t			t;
1586 	register const struct state *	sp;
1587 	register int			samei, otheri;
1588 	register int			sameind, otherind;
1589 	register int			i;
1590 	register int			nseen;
1591 	int				seen[TZ_MAX_TYPES];
1592 	int				types[TZ_MAX_TYPES];
1593 	int				okay;
1594 
1595 	if (tmp->tm_isdst > 1)
1596 		tmp->tm_isdst = 1;
1597 	t = time2(tmp, funcp, offset, &okay);
1598 #ifdef PCTS
1599 	/*
1600 	** PCTS code courtesy Grant Sullivan (grant@osf.org).
1601 	*/
1602 	if (okay)
1603 		return t;
1604 	if (tmp->tm_isdst < 0)
1605 		tmp->tm_isdst = 0;	/* reset to std and try again */
1606 #endif /* defined PCTS */
1607 #ifndef PCTS
1608 	if (okay || tmp->tm_isdst < 0)
1609 		return t;
1610 #endif /* !defined PCTS */
1611 	/*
1612 	** We're supposed to assume that somebody took a time of one type
1613 	** and did some math on it that yielded a "struct tm" that's bad.
1614 	** We try to divine the type they started from and adjust to the
1615 	** type they need.
1616 	*/
1617 	/*
1618 	** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
1619 	*/
1620 	sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
1621 		lclptr : gmtptr);
1622 #ifdef ALL_STATE
1623 	if (sp == NULL)
1624 		return WRONG;
1625 #endif /* defined ALL_STATE */
1626 	for (i = 0; i < sp->typecnt; ++i)
1627 		seen[i] = FALSE;
1628 	nseen = 0;
1629 	for (i = sp->timecnt - 1; i >= 0; --i)
1630 		if (!seen[sp->types[i]]) {
1631 			seen[sp->types[i]] = TRUE;
1632 			types[nseen++] = sp->types[i];
1633 		}
1634 	for (sameind = 0; sameind < nseen; ++sameind) {
1635 		samei = types[sameind];
1636 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
1637 			continue;
1638 		for (otherind = 0; otherind < nseen; ++otherind) {
1639 			otheri = types[otherind];
1640 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
1641 				continue;
1642 			tmp->tm_sec += (int)(sp->ttis[otheri].tt_gmtoff -
1643 					sp->ttis[samei].tt_gmtoff);
1644 			tmp->tm_isdst = !tmp->tm_isdst;
1645 			t = time2(tmp, funcp, offset, &okay);
1646 			if (okay)
1647 				return t;
1648 			tmp->tm_sec -= (int)(sp->ttis[otheri].tt_gmtoff -
1649 					sp->ttis[samei].tt_gmtoff);
1650 			tmp->tm_isdst = !tmp->tm_isdst;
1651 		}
1652 	}
1653 	return WRONG;
1654 }
1655 
1656 time_t
1657 mktime(tmp)
1658 struct tm * const	tmp;
1659 {
1660 	time_t result;
1661 
1662 	rwlock_wrlock(&lcl_lock);
1663 	tzset_unlocked();
1664 	result = time1(tmp, localsub, 0L);
1665 	rwlock_unlock(&lcl_lock);
1666 	return (result);
1667 }
1668 
1669 #ifdef STD_INSPIRED
1670 
1671 time_t
1672 timelocal(tmp)
1673 struct tm * const	tmp;
1674 {
1675 	tmp->tm_isdst = -1;	/* in case it wasn't initialized */
1676 	return mktime(tmp);
1677 }
1678 
1679 time_t
1680 timegm(tmp)
1681 struct tm * const	tmp;
1682 {
1683 	tmp->tm_isdst = 0;
1684 	return time1(tmp, gmtsub, 0L);
1685 }
1686 
1687 time_t
1688 timeoff(tmp, offset)
1689 struct tm * const	tmp;
1690 const long		offset;
1691 {
1692 	tmp->tm_isdst = 0;
1693 	return time1(tmp, gmtsub, offset);
1694 }
1695 
1696 #endif /* defined STD_INSPIRED */
1697 
1698 #ifdef CMUCS
1699 
1700 /*
1701 ** The following is supplied for compatibility with
1702 ** previous versions of the CMUCS runtime library.
1703 */
1704 
1705 long
1706 gtime(tmp)
1707 struct tm * const	tmp;
1708 {
1709 	const time_t	t = mktime(tmp);
1710 
1711 	if (t == WRONG)
1712 		return -1;
1713 	return t;
1714 }
1715 
1716 #endif /* defined CMUCS */
1717 
1718 /*
1719 ** XXX--is the below the right way to conditionalize??
1720 */
1721 
1722 #ifdef STD_INSPIRED
1723 
1724 /*
1725 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
1726 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
1727 ** is not the case if we are accounting for leap seconds.
1728 ** So, we provide the following conversion routines for use
1729 ** when exchanging timestamps with POSIX conforming systems.
1730 */
1731 
1732 static long
1733 leapcorr(timep)
1734 time_t *	timep;
1735 {
1736 	register struct state *		sp;
1737 	register struct lsinfo *	lp;
1738 	register int			i;
1739 
1740 	sp = lclptr;
1741 	i = sp->leapcnt;
1742 	while (--i >= 0) {
1743 		lp = &sp->lsis[i];
1744 		if (*timep >= lp->ls_trans)
1745 			return lp->ls_corr;
1746 	}
1747 	return 0;
1748 }
1749 
1750 time_t
1751 time2posix(t)
1752 time_t	t;
1753 {
1754 	time_t result;
1755 
1756 	rwlock_wrlock(&lcl_lock);
1757 	tzset_unlocked();
1758 	result = t - leapcorr(&t);
1759 	rwlock_unlock(&lcl_lock);
1760 	return (result);
1761 }
1762 
1763 time_t
1764 posix2time(t)
1765 time_t	t;
1766 {
1767 	time_t	x;
1768 	time_t	y;
1769 
1770 	rwlock_wrlock(&lcl_lock);
1771 	tzset_unlocked();
1772 	/*
1773 	** For a positive leap second hit, the result
1774 	** is not unique.  For a negative leap second
1775 	** hit, the corresponding time doesn't exist,
1776 	** so we return an adjacent second.
1777 	*/
1778 	x = t + leapcorr(&t);
1779 	y = x - leapcorr(&x);
1780 	if (y < t) {
1781 		do {
1782 			x++;
1783 			y = x - leapcorr(&x);
1784 		} while (y < t);
1785 		if (t != y) {
1786 			rwlock_unlock(&lcl_lock);
1787 			return x - 1;
1788 		}
1789 	} else if (y > t) {
1790 		do {
1791 			--x;
1792 			y = x - leapcorr(&x);
1793 		} while (y > t);
1794 		if (t != y) {
1795 			rwlock_unlock(&lcl_lock);
1796 			return x + 1;
1797 		}
1798 	}
1799 	rwlock_unlock(&lcl_lock);
1800 	return x;
1801 }
1802 
1803 #endif /* defined STD_INSPIRED */
1804