Home | History | Annotate | Line # | Download | only in time
localtime.c revision 1.36
      1 /*	$NetBSD: localtime.c,v 1.36 2004/11/16 04:15:28 christos 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 (at) 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.36 2004/11/16 04:15:28 christos Exp $");
     14 #endif
     15 #endif /* LIBC_SCCS and not lint */
     16 
     17 /*
     18 ** Leap second handling from Bradley White (bww (at) k.gp.cs.cmu.edu).
     19 ** POSIX-style TZ environment variable handling from Guy Harris
     20 ** (guy (at) 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 (at) 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 	tzset_unlocked();
   1136 	localsub(timep, 0L, tmp);
   1137 	rwlock_unlock(&lcl_lock);
   1138 	return tmp;
   1139 }
   1140 
   1141 /*
   1142 ** gmtsub is to gmtime as localsub is to localtime.
   1143 */
   1144 
   1145 static void
   1146 gmtsub(timep, offset, tmp)
   1147 const time_t * const	timep;
   1148 const long		offset;
   1149 struct tm * const	tmp;
   1150 {
   1151 #ifdef _REENTRANT
   1152 	static mutex_t gmt_mutex = MUTEX_INITIALIZER;
   1153 #endif
   1154 
   1155 	mutex_lock(&gmt_mutex);
   1156 	if (!gmt_is_set) {
   1157 		gmt_is_set = TRUE;
   1158 #ifdef ALL_STATE
   1159 		gmtptr = (struct state *) malloc(sizeof *gmtptr);
   1160 		if (gmtptr != NULL)
   1161 #endif /* defined ALL_STATE */
   1162 			gmtload(gmtptr);
   1163 	}
   1164 	mutex_unlock(&gmt_mutex);
   1165 	timesub(timep, offset, gmtptr, tmp);
   1166 #ifdef TM_ZONE
   1167 	/*
   1168 	** Could get fancy here and deliver something such as
   1169 	** "UTC+xxxx" or "UTC-xxxx" if offset is non-zero,
   1170 	** but this is no time for a treasure hunt.
   1171 	*/
   1172 	if (offset != 0)
   1173 		/* LINTED const castaway */
   1174 		tmp->TM_ZONE = (__aconst char *)wildabbr;
   1175 	else {
   1176 #ifdef ALL_STATE
   1177 		if (gmtptr == NULL)
   1178 			tmp->TM_ZONE = (__aconst char *)gmt;
   1179 		else	tmp->TM_ZONE = gmtptr->chars;
   1180 #endif /* defined ALL_STATE */
   1181 #ifndef ALL_STATE
   1182 		tmp->TM_ZONE = gmtptr->chars;
   1183 #endif /* State Farm */
   1184 	}
   1185 #endif /* defined TM_ZONE */
   1186 }
   1187 
   1188 struct tm *
   1189 gmtime(timep)
   1190 const time_t * const	timep;
   1191 {
   1192 	gmtsub(timep, 0L, &tm);
   1193 	return &tm;
   1194 }
   1195 
   1196 /*
   1197 ** Re-entrant version of gmtime.
   1198 */
   1199 
   1200 struct tm *
   1201 gmtime_r(timep, tmp)
   1202 const time_t * const	timep;
   1203 struct tm *		tmp;
   1204 {
   1205 	gmtsub(timep, 0L, tmp);
   1206 	return tmp;
   1207 }
   1208 
   1209 #ifdef STD_INSPIRED
   1210 
   1211 struct tm *
   1212 offtime(timep, offset)
   1213 const time_t * const	timep;
   1214 const long		offset;
   1215 {
   1216 	gmtsub(timep, offset, &tm);
   1217 	return &tm;
   1218 }
   1219 
   1220 #endif /* defined STD_INSPIRED */
   1221 
   1222 static void
   1223 timesub(timep, offset, sp, tmp)
   1224 const time_t * const			timep;
   1225 const long				offset;
   1226 register const struct state * const	sp;
   1227 register struct tm * const		tmp;
   1228 {
   1229 	register const struct lsinfo *	lp;
   1230 	register long			days;
   1231 	register long			rem;
   1232 	register int			y;
   1233 	register int			yleap;
   1234 	register const int *		ip;
   1235 	register long			corr;
   1236 	register int			hit;
   1237 	register int			i;
   1238 
   1239 	corr = 0;
   1240 	hit = 0;
   1241 #ifdef ALL_STATE
   1242 	i = (sp == NULL) ? 0 : sp->leapcnt;
   1243 #endif /* defined ALL_STATE */
   1244 #ifndef ALL_STATE
   1245 	i = sp->leapcnt;
   1246 #endif /* State Farm */
   1247 	while (--i >= 0) {
   1248 		lp = &sp->lsis[i];
   1249 		if (*timep >= lp->ls_trans) {
   1250 			if (*timep == lp->ls_trans) {
   1251 				hit = ((i == 0 && lp->ls_corr > 0) ||
   1252 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
   1253 				if (hit)
   1254 					while (i > 0 &&
   1255 						sp->lsis[i].ls_trans ==
   1256 						sp->lsis[i - 1].ls_trans + 1 &&
   1257 						sp->lsis[i].ls_corr ==
   1258 						sp->lsis[i - 1].ls_corr + 1) {
   1259 							++hit;
   1260 							--i;
   1261 					}
   1262 			}
   1263 			corr = lp->ls_corr;
   1264 			break;
   1265 		}
   1266 	}
   1267 	days = *timep / SECSPERDAY;
   1268 	rem = *timep % SECSPERDAY;
   1269 #ifdef mc68k
   1270 	if (*timep == 0x80000000) {
   1271 		/*
   1272 		** A 3B1 muffs the division on the most negative number.
   1273 		*/
   1274 		days = -24855;
   1275 		rem = -11648;
   1276 	}
   1277 #endif /* defined mc68k */
   1278 	rem += (offset - corr);
   1279 	while (rem < 0) {
   1280 		rem += SECSPERDAY;
   1281 		--days;
   1282 	}
   1283 	while (rem >= SECSPERDAY) {
   1284 		rem -= SECSPERDAY;
   1285 		++days;
   1286 	}
   1287 	tmp->tm_hour = (int) (rem / SECSPERHOUR);
   1288 	rem = rem % SECSPERHOUR;
   1289 	tmp->tm_min = (int) (rem / SECSPERMIN);
   1290 	/*
   1291 	** A positive leap second requires a special
   1292 	** representation.  This uses "... ??:59:60" et seq.
   1293 	*/
   1294 	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
   1295 	tmp->tm_wday = (int) ((EPOCH_WDAY + days) % DAYSPERWEEK);
   1296 	if (tmp->tm_wday < 0)
   1297 		tmp->tm_wday += DAYSPERWEEK;
   1298 	y = EPOCH_YEAR;
   1299 #define LEAPS_THRU_END_OF(y)	((y) / 4 - (y) / 100 + (y) / 400)
   1300 	while (days < 0 || days >= (long) year_lengths[yleap = isleap(y)]) {
   1301 		register int	newy;
   1302 
   1303 		newy = (int)(y + days / DAYSPERNYEAR);
   1304 		if (days < 0)
   1305 			--newy;
   1306 		days -= (newy - y) * DAYSPERNYEAR +
   1307 			LEAPS_THRU_END_OF(newy - 1) -
   1308 			LEAPS_THRU_END_OF(y - 1);
   1309 		y = newy;
   1310 	}
   1311 	tmp->tm_year = y - TM_YEAR_BASE;
   1312 	tmp->tm_yday = (int) days;
   1313 	ip = mon_lengths[yleap];
   1314 	for (tmp->tm_mon = 0; days >= (long) ip[tmp->tm_mon]; ++(tmp->tm_mon))
   1315 		days = days - (long) ip[tmp->tm_mon];
   1316 	tmp->tm_mday = (int) (days + 1);
   1317 	tmp->tm_isdst = 0;
   1318 #ifdef TM_GMTOFF
   1319 	tmp->TM_GMTOFF = offset;
   1320 #endif /* defined TM_GMTOFF */
   1321 }
   1322 
   1323 char *
   1324 ctime(timep)
   1325 const time_t * const	timep;
   1326 {
   1327 /*
   1328 ** Section 4.12.3.2 of X3.159-1989 requires that
   1329 **	The ctime function converts the calendar time pointed to by timer
   1330 **	to local time in the form of a string.  It is equivalent to
   1331 **		asctime(localtime(timer))
   1332 */
   1333 	return asctime(localtime(timep));
   1334 }
   1335 
   1336 char *
   1337 ctime_r(timep, buf)
   1338 const time_t * const	timep;
   1339 char *			buf;
   1340 {
   1341 	struct tm	tmp;
   1342 
   1343 	return asctime_r(localtime_r(timep, &tmp), buf);
   1344 }
   1345 
   1346 /*
   1347 ** Adapted from code provided by Robert Elz, who writes:
   1348 **	The "best" way to do mktime I think is based on an idea of Bob
   1349 **	Kridle's (so its said...) from a long time ago.
   1350 **	[kridle (at) xinet.com as of 1996-01-16.]
   1351 **	It does a binary search of the time_t space.  Since time_t's are
   1352 **	just 32 bits, its a max of 32 iterations (even at 64 bits it
   1353 **	would still be very reasonable).
   1354 */
   1355 
   1356 #ifndef WRONG
   1357 #define WRONG	(-1)
   1358 #endif /* !defined WRONG */
   1359 
   1360 /*
   1361 ** Simplified normalize logic courtesy Paul Eggert (eggert (at) twinsun.com).
   1362 */
   1363 
   1364 static int
   1365 increment_overflow(number, delta)
   1366 int *	number;
   1367 int	delta;
   1368 {
   1369 	int	number0;
   1370 
   1371 	number0 = *number;
   1372 	*number += delta;
   1373 	return (*number < number0) != (delta < 0);
   1374 }
   1375 
   1376 static int
   1377 normalize_overflow(tensptr, unitsptr, base)
   1378 int * const	tensptr;
   1379 int * const	unitsptr;
   1380 const int	base;
   1381 {
   1382 	register int	tensdelta;
   1383 
   1384 	tensdelta = (*unitsptr >= 0) ?
   1385 		(*unitsptr / base) :
   1386 		(-1 - (-1 - *unitsptr) / base);
   1387 	*unitsptr -= tensdelta * base;
   1388 	return increment_overflow(tensptr, tensdelta);
   1389 }
   1390 
   1391 static int
   1392 tmcomp(atmp, btmp)
   1393 register const struct tm * const atmp;
   1394 register const struct tm * const btmp;
   1395 {
   1396 	register int	result;
   1397 
   1398 	if ((result = (atmp->tm_year - btmp->tm_year)) == 0 &&
   1399 		(result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
   1400 		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
   1401 		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
   1402 		(result = (atmp->tm_min - btmp->tm_min)) == 0)
   1403 			result = atmp->tm_sec - btmp->tm_sec;
   1404 	return result;
   1405 }
   1406 
   1407 static time_t
   1408 time2sub(tmp, funcp, offset, okayp, do_norm_secs)
   1409 struct tm * const	tmp;
   1410 void (* const		funcp) P((const time_t*, long, struct tm*));
   1411 const long		offset;
   1412 int * const		okayp;
   1413 const int		do_norm_secs;
   1414 {
   1415 	register const struct state *	sp;
   1416 	register int			dir;
   1417 	register int			bits;
   1418 	register int			i, j ;
   1419 	register int			saved_seconds;
   1420 	time_t				newt;
   1421 	time_t				t;
   1422 	struct tm			yourtm, mytm;
   1423 
   1424 	*okayp = FALSE;
   1425 	yourtm = *tmp;
   1426 	if (do_norm_secs) {
   1427 		if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
   1428 			SECSPERMIN))
   1429 				return WRONG;
   1430 	}
   1431 	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
   1432 		return WRONG;
   1433 	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
   1434 		return WRONG;
   1435 	if (normalize_overflow(&yourtm.tm_year, &yourtm.tm_mon, MONSPERYEAR))
   1436 		return WRONG;
   1437 	/*
   1438 	** Turn yourtm.tm_year into an actual year number for now.
   1439 	** It is converted back to an offset from TM_YEAR_BASE later.
   1440 	*/
   1441 	if (increment_overflow(&yourtm.tm_year, TM_YEAR_BASE))
   1442 		return WRONG;
   1443 	while (yourtm.tm_mday <= 0) {
   1444 		if (increment_overflow(&yourtm.tm_year, -1))
   1445 			return WRONG;
   1446 		i = yourtm.tm_year + (1 < yourtm.tm_mon);
   1447 		yourtm.tm_mday += year_lengths[isleap(i)];
   1448 	}
   1449 	while (yourtm.tm_mday > DAYSPERLYEAR) {
   1450 		i = yourtm.tm_year + (1 < yourtm.tm_mon);
   1451 		yourtm.tm_mday -= year_lengths[isleap(i)];
   1452 		if (increment_overflow(&yourtm.tm_year, 1))
   1453 			return WRONG;
   1454 	}
   1455 	for ( ; ; ) {
   1456 		i = mon_lengths[isleap(yourtm.tm_year)][yourtm.tm_mon];
   1457 		if (yourtm.tm_mday <= i)
   1458 			break;
   1459 		yourtm.tm_mday -= i;
   1460 		if (++yourtm.tm_mon >= MONSPERYEAR) {
   1461 			yourtm.tm_mon = 0;
   1462 			if (increment_overflow(&yourtm.tm_year, 1))
   1463 				return WRONG;
   1464 		}
   1465 	}
   1466 	if (increment_overflow(&yourtm.tm_year, -TM_YEAR_BASE))
   1467 		return WRONG;
   1468 	if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
   1469 		saved_seconds = 0;
   1470 	else if (yourtm.tm_year + TM_YEAR_BASE < EPOCH_YEAR) {
   1471 		/*
   1472 		** We can't set tm_sec to 0, because that might push the
   1473 		** time below the minimum representable time.
   1474 		** Set tm_sec to 59 instead.
   1475 		** This assumes that the minimum representable time is
   1476 		** not in the same minute that a leap second was deleted from,
   1477 		** which is a safer assumption than using 58 would be.
   1478 		*/
   1479 		if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
   1480 			return WRONG;
   1481 		saved_seconds = yourtm.tm_sec;
   1482 		yourtm.tm_sec = SECSPERMIN - 1;
   1483 	} else {
   1484 		saved_seconds = yourtm.tm_sec;
   1485 		yourtm.tm_sec = 0;
   1486 	}
   1487 	/*
   1488 	** Divide the search space in half
   1489 	** (this works whether time_t is signed or unsigned).
   1490 	*/
   1491 	bits = TYPE_BIT(time_t) - 1;
   1492 	/*
   1493 	** If time_t is signed, then 0 is just above the median,
   1494 	** assuming two's complement arithmetic.
   1495 	** If time_t is unsigned, then (1 << bits) is just above the median.
   1496 	*/
   1497 	/* LINTED constant in conditional context */
   1498 	t = TYPE_SIGNED(time_t) ? 0 : (((time_t) 1) << bits);
   1499 	for ( ; ; ) {
   1500 		(*funcp)(&t, offset, &mytm);
   1501 		dir = tmcomp(&mytm, &yourtm);
   1502 		if (dir != 0) {
   1503 			if (bits-- < 0)
   1504 				return WRONG;
   1505 			if (bits < 0)
   1506 				--t; /* may be needed if new t is minimal */
   1507 			else if (dir > 0)
   1508 				t -= ((time_t) 1) << bits;
   1509 			else	t += ((time_t) 1) << bits;
   1510 			continue;
   1511 		}
   1512 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
   1513 			break;
   1514 		/*
   1515 		** Right time, wrong type.
   1516 		** Hunt for right time, right type.
   1517 		** It's okay to guess wrong since the guess
   1518 		** gets checked.
   1519 		*/
   1520 		/*
   1521 		** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
   1522 		*/
   1523 		sp = (const struct state *)
   1524 			(((void *) funcp == (void *) localsub) ?
   1525 			lclptr : gmtptr);
   1526 #ifdef ALL_STATE
   1527 		if (sp == NULL)
   1528 			return WRONG;
   1529 #endif /* defined ALL_STATE */
   1530 		for (i = sp->typecnt - 1; i >= 0; --i) {
   1531 			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
   1532 				continue;
   1533 			for (j = sp->typecnt - 1; j >= 0; --j) {
   1534 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
   1535 					continue;
   1536 				newt = t + sp->ttis[j].tt_gmtoff -
   1537 					sp->ttis[i].tt_gmtoff;
   1538 				(*funcp)(&newt, offset, &mytm);
   1539 				if (tmcomp(&mytm, &yourtm) != 0)
   1540 					continue;
   1541 				if (mytm.tm_isdst != yourtm.tm_isdst)
   1542 					continue;
   1543 				/*
   1544 				** We have a match.
   1545 				*/
   1546 				t = newt;
   1547 				goto label;
   1548 			}
   1549 		}
   1550 		return WRONG;
   1551 	}
   1552 label:
   1553 	newt = t + saved_seconds;
   1554 	if ((newt < t) != (saved_seconds < 0))
   1555 		return WRONG;
   1556 	t = newt;
   1557 	(*funcp)(&t, offset, tmp);
   1558 	*okayp = TRUE;
   1559 	return t;
   1560 }
   1561 
   1562 static time_t
   1563 time2(tmp, funcp, offset, okayp)
   1564 struct tm * const	tmp;
   1565 void (* const		funcp) P((const time_t*, long, struct tm*));
   1566 const long		offset;
   1567 int * const		okayp;
   1568 {
   1569 	time_t	t;
   1570 
   1571 	/*
   1572 	** First try without normalization of seconds
   1573 	** (in case tm_sec contains a value associated with a leap second).
   1574 	** If that fails, try with normalization of seconds.
   1575 	*/
   1576 	t = time2sub(tmp, funcp, offset, okayp, FALSE);
   1577 	return *okayp ? t : time2sub(tmp, funcp, offset, okayp, TRUE);
   1578 }
   1579 
   1580 static time_t
   1581 time1(tmp, funcp, offset)
   1582 struct tm * const	tmp;
   1583 void (* const		funcp) P((const time_t *, long, struct tm *));
   1584 const long		offset;
   1585 {
   1586 	register time_t			t;
   1587 	register const struct state *	sp;
   1588 	register int			samei, otheri;
   1589 	register int			sameind, otherind;
   1590 	register int			i;
   1591 	register int			nseen;
   1592 	int				seen[TZ_MAX_TYPES];
   1593 	int				types[TZ_MAX_TYPES];
   1594 	int				okay;
   1595 
   1596 	if (tmp->tm_isdst > 1)
   1597 		tmp->tm_isdst = 1;
   1598 	t = time2(tmp, funcp, offset, &okay);
   1599 #ifdef PCTS
   1600 	/*
   1601 	** PCTS code courtesy Grant Sullivan (grant (at) osf.org).
   1602 	*/
   1603 	if (okay)
   1604 		return t;
   1605 	if (tmp->tm_isdst < 0)
   1606 		tmp->tm_isdst = 0;	/* reset to std and try again */
   1607 #endif /* defined PCTS */
   1608 #ifndef PCTS
   1609 	if (okay || tmp->tm_isdst < 0)
   1610 		return t;
   1611 #endif /* !defined PCTS */
   1612 	/*
   1613 	** We're supposed to assume that somebody took a time of one type
   1614 	** and did some math on it that yielded a "struct tm" that's bad.
   1615 	** We try to divine the type they started from and adjust to the
   1616 	** type they need.
   1617 	*/
   1618 	/*
   1619 	** The (void *) casts are the benefit of SunOS 3.3 on Sun 2's.
   1620 	*/
   1621 	sp = (const struct state *) (((void *) funcp == (void *) localsub) ?
   1622 		lclptr : gmtptr);
   1623 #ifdef ALL_STATE
   1624 	if (sp == NULL)
   1625 		return WRONG;
   1626 #endif /* defined ALL_STATE */
   1627 	for (i = 0; i < sp->typecnt; ++i)
   1628 		seen[i] = FALSE;
   1629 	nseen = 0;
   1630 	for (i = sp->timecnt - 1; i >= 0; --i)
   1631 		if (!seen[sp->types[i]]) {
   1632 			seen[sp->types[i]] = TRUE;
   1633 			types[nseen++] = sp->types[i];
   1634 		}
   1635 	for (sameind = 0; sameind < nseen; ++sameind) {
   1636 		samei = types[sameind];
   1637 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
   1638 			continue;
   1639 		for (otherind = 0; otherind < nseen; ++otherind) {
   1640 			otheri = types[otherind];
   1641 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
   1642 				continue;
   1643 			tmp->tm_sec += (int)(sp->ttis[otheri].tt_gmtoff -
   1644 					sp->ttis[samei].tt_gmtoff);
   1645 			tmp->tm_isdst = !tmp->tm_isdst;
   1646 			t = time2(tmp, funcp, offset, &okay);
   1647 			if (okay)
   1648 				return t;
   1649 			tmp->tm_sec -= (int)(sp->ttis[otheri].tt_gmtoff -
   1650 					sp->ttis[samei].tt_gmtoff);
   1651 			tmp->tm_isdst = !tmp->tm_isdst;
   1652 		}
   1653 	}
   1654 	return WRONG;
   1655 }
   1656 
   1657 time_t
   1658 mktime(tmp)
   1659 struct tm * const	tmp;
   1660 {
   1661 	time_t result;
   1662 
   1663 	rwlock_wrlock(&lcl_lock);
   1664 	tzset_unlocked();
   1665 	result = time1(tmp, localsub, 0L);
   1666 	rwlock_unlock(&lcl_lock);
   1667 	return (result);
   1668 }
   1669 
   1670 #ifdef STD_INSPIRED
   1671 
   1672 time_t
   1673 timelocal(tmp)
   1674 struct tm * const	tmp;
   1675 {
   1676 	tmp->tm_isdst = -1;	/* in case it wasn't initialized */
   1677 	return mktime(tmp);
   1678 }
   1679 
   1680 time_t
   1681 timegm(tmp)
   1682 struct tm * const	tmp;
   1683 {
   1684 	tmp->tm_isdst = 0;
   1685 	return time1(tmp, gmtsub, 0L);
   1686 }
   1687 
   1688 time_t
   1689 timeoff(tmp, offset)
   1690 struct tm * const	tmp;
   1691 const long		offset;
   1692 {
   1693 	tmp->tm_isdst = 0;
   1694 	return time1(tmp, gmtsub, offset);
   1695 }
   1696 
   1697 #endif /* defined STD_INSPIRED */
   1698 
   1699 #ifdef CMUCS
   1700 
   1701 /*
   1702 ** The following is supplied for compatibility with
   1703 ** previous versions of the CMUCS runtime library.
   1704 */
   1705 
   1706 long
   1707 gtime(tmp)
   1708 struct tm * const	tmp;
   1709 {
   1710 	const time_t	t = mktime(tmp);
   1711 
   1712 	if (t == WRONG)
   1713 		return -1;
   1714 	return t;
   1715 }
   1716 
   1717 #endif /* defined CMUCS */
   1718 
   1719 /*
   1720 ** XXX--is the below the right way to conditionalize??
   1721 */
   1722 
   1723 #ifdef STD_INSPIRED
   1724 
   1725 /*
   1726 ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
   1727 ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
   1728 ** is not the case if we are accounting for leap seconds.
   1729 ** So, we provide the following conversion routines for use
   1730 ** when exchanging timestamps with POSIX conforming systems.
   1731 */
   1732 
   1733 static long
   1734 leapcorr(timep)
   1735 time_t *	timep;
   1736 {
   1737 	register struct state *		sp;
   1738 	register struct lsinfo *	lp;
   1739 	register int			i;
   1740 
   1741 	sp = lclptr;
   1742 	i = sp->leapcnt;
   1743 	while (--i >= 0) {
   1744 		lp = &sp->lsis[i];
   1745 		if (*timep >= lp->ls_trans)
   1746 			return lp->ls_corr;
   1747 	}
   1748 	return 0;
   1749 }
   1750 
   1751 time_t
   1752 time2posix(t)
   1753 time_t	t;
   1754 {
   1755 	time_t result;
   1756 
   1757 	rwlock_wrlock(&lcl_lock);
   1758 	tzset_unlocked();
   1759 	result = t - leapcorr(&t);
   1760 	rwlock_unlock(&lcl_lock);
   1761 	return (result);
   1762 }
   1763 
   1764 time_t
   1765 posix2time(t)
   1766 time_t	t;
   1767 {
   1768 	time_t	x;
   1769 	time_t	y;
   1770 
   1771 	rwlock_wrlock(&lcl_lock);
   1772 	tzset_unlocked();
   1773 	/*
   1774 	** For a positive leap second hit, the result
   1775 	** is not unique.  For a negative leap second
   1776 	** hit, the corresponding time doesn't exist,
   1777 	** so we return an adjacent second.
   1778 	*/
   1779 	x = t + leapcorr(&t);
   1780 	y = x - leapcorr(&x);
   1781 	if (y < t) {
   1782 		do {
   1783 			x++;
   1784 			y = x - leapcorr(&x);
   1785 		} while (y < t);
   1786 		if (t != y) {
   1787 			rwlock_unlock(&lcl_lock);
   1788 			return x - 1;
   1789 		}
   1790 	} else if (y > t) {
   1791 		do {
   1792 			--x;
   1793 			y = x - leapcorr(&x);
   1794 		} while (y > t);
   1795 		if (t != y) {
   1796 			rwlock_unlock(&lcl_lock);
   1797 			return x + 1;
   1798 		}
   1799 	}
   1800 	rwlock_unlock(&lcl_lock);
   1801 	return x;
   1802 }
   1803 
   1804 #endif /* defined STD_INSPIRED */
   1805