Home | History | Annotate | Line # | Download | only in time
localtime.c revision 1.67.2.3
      1  1.67.2.2       tls /*	$NetBSD: localtime.c,v 1.67.2.3 2014/08/20 00:02:16 tls Exp $	*/
      2       1.7       jtc 
      3       1.7       jtc /*
      4       1.7       jtc ** This file is in the public domain, so clarified as of
      5      1.45   mlelstv ** 1996-06-05 by Arthur David Olson.
      6       1.7       jtc */
      7       1.2       jtc 
      8      1.11  christos #include <sys/cdefs.h>
      9      1.24   msaitoh #if defined(LIBC_SCCS) && !defined(lint)
     10      1.11  christos #if 0
     11      1.58  christos static char	elsieid[] = "@(#)localtime.c	8.17";
     12      1.11  christos #else
     13  1.67.2.2       tls __RCSID("$NetBSD: localtime.c,v 1.67.2.3 2014/08/20 00:02:16 tls Exp $");
     14      1.11  christos #endif
     15      1.24   msaitoh #endif /* LIBC_SCCS and not lint */
     16       1.1       jtc 
     17       1.1       jtc /*
     18      1.45   mlelstv ** Leap second handling from Bradley White.
     19      1.45   mlelstv ** POSIX-style TZ environment variable handling from Guy Harris.
     20       1.1       jtc */
     21       1.1       jtc 
     22       1.1       jtc /*LINTLIBRARY*/
     23       1.1       jtc 
     24      1.12       jtc #include "namespace.h"
     25  1.67.2.3       tls #include <assert.h>
     26       1.1       jtc #include "private.h"
     27       1.1       jtc #include "tzfile.h"
     28       1.1       jtc #include "fcntl.h"
     29      1.19    kleink #include "reentrant.h"
     30      1.12       jtc 
     31      1.42  christos #if defined(__weak_alias)
     32      1.25    kleink __weak_alias(daylight,_daylight)
     33      1.23   mycroft __weak_alias(tzname,_tzname)
     34      1.12       jtc #endif
     35       1.1       jtc 
     36      1.45   mlelstv #ifndef TZ_ABBR_MAX_LEN
     37      1.45   mlelstv #define TZ_ABBR_MAX_LEN	16
     38      1.45   mlelstv #endif /* !defined TZ_ABBR_MAX_LEN */
     39      1.45   mlelstv 
     40      1.45   mlelstv #ifndef TZ_ABBR_CHAR_SET
     41      1.45   mlelstv #define TZ_ABBR_CHAR_SET \
     42      1.45   mlelstv 	"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 :+-._"
     43      1.45   mlelstv #endif /* !defined TZ_ABBR_CHAR_SET */
     44      1.45   mlelstv 
     45      1.45   mlelstv #ifndef TZ_ABBR_ERR_CHAR
     46      1.45   mlelstv #define TZ_ABBR_ERR_CHAR	'_'
     47      1.45   mlelstv #endif /* !defined TZ_ABBR_ERR_CHAR */
     48      1.45   mlelstv 
     49  1.67.2.3       tls /* The minimum and maximum finite time values.  */
     50  1.67.2.3       tls static time_t const time_t_min =
     51  1.67.2.3       tls   (TYPE_SIGNED(time_t)
     52  1.67.2.3       tls    ? (time_t) -1 << (int)(CHAR_BIT * sizeof (time_t) - 1)
     53  1.67.2.3       tls    : 0);
     54  1.67.2.3       tls static time_t const time_t_max =
     55  1.67.2.3       tls   (TYPE_SIGNED(time_t)
     56  1.67.2.3       tls    ? - (~ 0 < 0) - ((time_t) -1 << (int)(CHAR_BIT * sizeof (time_t) - 1))
     57  1.67.2.3       tls    : -1);
     58  1.67.2.3       tls 
     59       1.1       jtc /*
     60       1.1       jtc ** SunOS 4.1.1 headers lack O_BINARY.
     61       1.1       jtc */
     62       1.1       jtc 
     63       1.1       jtc #ifdef O_BINARY
     64       1.1       jtc #define OPEN_MODE	(O_RDONLY | O_BINARY)
     65       1.1       jtc #endif /* defined O_BINARY */
     66       1.1       jtc #ifndef O_BINARY
     67       1.1       jtc #define OPEN_MODE	O_RDONLY
     68       1.1       jtc #endif /* !defined O_BINARY */
     69       1.1       jtc 
     70       1.1       jtc #ifndef WILDABBR
     71       1.1       jtc /*
     72       1.1       jtc ** Someone might make incorrect use of a time zone abbreviation:
     73       1.1       jtc **	1.	They might reference tzname[0] before calling tzset (explicitly
     74       1.1       jtc **		or implicitly).
     75       1.1       jtc **	2.	They might reference tzname[1] before calling tzset (explicitly
     76       1.1       jtc **		or implicitly).
     77       1.1       jtc **	3.	They might reference tzname[1] after setting to a time zone
     78       1.1       jtc **		in which Daylight Saving Time is never observed.
     79       1.1       jtc **	4.	They might reference tzname[0] after setting to a time zone
     80       1.1       jtc **		in which Standard Time is never observed.
     81       1.1       jtc **	5.	They might reference tm.TM_ZONE after calling offtime.
     82       1.1       jtc ** What's best to do in the above cases is open to debate;
     83       1.1       jtc ** for now, we just set things up so that in any of the five cases
     84      1.45   mlelstv ** WILDABBR is used. Another possibility: initialize tzname[0] to the
     85       1.1       jtc ** string "tzname[0] used before set", and similarly for the other cases.
     86      1.45   mlelstv ** And another: initialize tzname[0] to "ERA", with an explanation in the
     87       1.1       jtc ** manual page of what this "time zone abbreviation" means (doing this so
     88       1.1       jtc ** that tzname[0] has the "normal" length of three characters).
     89       1.1       jtc */
     90       1.1       jtc #define WILDABBR	"   "
     91       1.1       jtc #endif /* !defined WILDABBR */
     92       1.1       jtc 
     93      1.45   mlelstv static const char	wildabbr[] = WILDABBR;
     94       1.1       jtc 
     95  1.67.2.1       tls static const char	gmt[] = "GMT";
     96       1.1       jtc 
     97      1.22    kleink /*
     98      1.22    kleink ** The DST rules to use if TZ has no rules and we can't load TZDEFRULES.
     99      1.22    kleink ** We default to US rules as of 1999-08-17.
    100      1.22    kleink ** POSIX 1003.1 section 8.1.1 says that the default DST rules are
    101      1.22    kleink ** implementation dependent; for historical reasons, US rules are a
    102      1.22    kleink ** common default.
    103      1.22    kleink */
    104      1.22    kleink #ifndef TZDEFRULESTRING
    105      1.22    kleink #define TZDEFRULESTRING ",M4.1.0,M10.5.0"
    106      1.22    kleink #endif /* !defined TZDEFDST */
    107      1.22    kleink 
    108       1.1       jtc struct ttinfo {				/* time type information */
    109  1.67.2.3       tls 	int_fast32_t	tt_gmtoff;	/* UT offset in seconds */
    110       1.1       jtc 	int		tt_isdst;	/* used to set tm_isdst */
    111       1.1       jtc 	int		tt_abbrind;	/* abbreviation list index */
    112       1.1       jtc 	int		tt_ttisstd;	/* TRUE if transition is std time */
    113  1.67.2.3       tls 	int		tt_ttisgmt;	/* TRUE if transition is UT */
    114       1.1       jtc };
    115       1.1       jtc 
    116       1.1       jtc struct lsinfo {				/* leap second information */
    117       1.1       jtc 	time_t		ls_trans;	/* transition time */
    118  1.67.2.3       tls 	int_fast64_t	ls_corr;	/* correction to apply */
    119       1.1       jtc };
    120       1.1       jtc 
    121       1.1       jtc #define BIGGEST(a, b)	(((a) > (b)) ? (a) : (b))
    122       1.1       jtc 
    123       1.1       jtc #ifdef TZNAME_MAX
    124       1.1       jtc #define MY_TZNAME_MAX	TZNAME_MAX
    125       1.1       jtc #endif /* defined TZNAME_MAX */
    126       1.1       jtc #ifndef TZNAME_MAX
    127       1.1       jtc #define MY_TZNAME_MAX	255
    128       1.1       jtc #endif /* !defined TZNAME_MAX */
    129       1.1       jtc 
    130      1.49  christos struct __state {
    131       1.1       jtc 	int		leapcnt;
    132       1.1       jtc 	int		timecnt;
    133       1.1       jtc 	int		typecnt;
    134       1.1       jtc 	int		charcnt;
    135      1.45   mlelstv 	int		goback;
    136      1.45   mlelstv 	int		goahead;
    137      1.45   mlelstv 	time_t		ats[TZ_MAX_TIMES];
    138       1.1       jtc 	unsigned char	types[TZ_MAX_TIMES];
    139       1.1       jtc 	struct ttinfo	ttis[TZ_MAX_TYPES];
    140  1.67.2.1       tls 	char		chars[/*CONSTCOND*/BIGGEST(BIGGEST(TZ_MAX_CHARS + 1,
    141  1.67.2.1       tls 				sizeof gmt), (2 * (MY_TZNAME_MAX + 1)))];
    142       1.1       jtc 	struct lsinfo	lsis[TZ_MAX_LEAPS];
    143  1.67.2.3       tls 	int		defaulttype; /* for early times or if no transitions */
    144       1.1       jtc };
    145       1.1       jtc 
    146       1.1       jtc struct rule {
    147       1.1       jtc 	int		r_type;		/* type of rule--see below */
    148       1.1       jtc 	int		r_day;		/* day number of rule */
    149       1.1       jtc 	int		r_week;		/* week number of rule */
    150       1.1       jtc 	int		r_mon;		/* month number of rule */
    151  1.67.2.3       tls 	int_fast32_t	r_time;		/* transition time of rule */
    152       1.1       jtc };
    153       1.1       jtc 
    154       1.1       jtc #define JULIAN_DAY		0	/* Jn - Julian day */
    155       1.1       jtc #define DAY_OF_YEAR		1	/* n - day of year */
    156       1.1       jtc #define MONTH_NTH_DAY_OF_WEEK	2	/* Mm.n.d - month, week, day of week */
    157       1.1       jtc 
    158      1.49  christos typedef struct tm *(*subfun_t)(const timezone_t sp, const time_t *timep,
    159  1.67.2.3       tls 			       const int_fast32_t offset, struct tm *tmp);
    160      1.49  christos 
    161       1.1       jtc /*
    162       1.1       jtc ** Prototypes for static functions.
    163       1.1       jtc */
    164       1.1       jtc 
    165  1.67.2.3       tls static int_fast32_t	detzcode(const char * codep);
    166  1.67.2.3       tls static int_fast64_t	detzcode64(const char * codep);
    167      1.45   mlelstv static int		differ_by_repeat(time_t t1, time_t t0);
    168  1.67.2.3       tls static const char *	getzname(const char * strp) ATTRIBUTE_PURE;
    169  1.67.2.3       tls static const char *	getqzname(const char * strp, const int delim) ATTRIBUTE_PURE;
    170      1.45   mlelstv static const char *	getnum(const char * strp, int * nump, int min,
    171      1.45   mlelstv 				int max);
    172  1.67.2.3       tls static const char *	getsecs(const char * strp, int_fast32_t * secsp);
    173  1.67.2.3       tls static const char *	getoffset(const char * strp, int_fast32_t * offsetp);
    174      1.45   mlelstv static const char *	getrule(const char * strp, struct rule * rulep);
    175      1.49  christos static void		gmtload(timezone_t sp);
    176      1.49  christos static struct tm *	gmtsub(const timezone_t sp, const time_t *timep,
    177  1.67.2.3       tls 				const int_fast32_t offset, struct tm * tmp);
    178      1.49  christos static struct tm *	localsub(const timezone_t sp, const time_t *timep,
    179  1.67.2.3       tls 				const int_fast32_t offset, struct tm *tmp);
    180      1.45   mlelstv static int		increment_overflow(int * number, int delta);
    181  1.67.2.3       tls static int		increment_overflow32(int_fast32_t * number, int delta);
    182  1.67.2.3       tls static int		increment_overflow_time(time_t *t, int_fast32_t delta);
    183  1.67.2.3       tls static int		leaps_thru_end_of(int y) ATTRIBUTE_PURE;
    184      1.45   mlelstv static int		normalize_overflow(int * tensptr, int * unitsptr,
    185      1.45   mlelstv 				int base);
    186  1.67.2.3       tls static int		normalize_overflow32(int_fast32_t * tensptr,
    187  1.67.2.3       tls 				int * unitsptr, int base);
    188      1.45   mlelstv static void		settzname(void);
    189      1.49  christos static time_t		time1(const timezone_t sp, struct tm * const tmp,
    190  1.67.2.3       tls 				subfun_t funcp, const int_fast32_t offset);
    191      1.49  christos static time_t		time2(const timezone_t sp, struct tm * const tmp,
    192      1.49  christos 				subfun_t funcp,
    193  1.67.2.3       tls 				const int_fast32_t offset, int *const okayp);
    194  1.67.2.1       tls static time_t		time2sub(const timezone_t sp, struct tm * const tmp,
    195  1.67.2.3       tls 				subfun_t funcp, const int_fast32_t offset,
    196      1.49  christos 				int *const okayp, const int do_norm_secs);
    197      1.49  christos static struct tm *	timesub(const timezone_t sp, const time_t * timep,
    198  1.67.2.3       tls 				const int_fast32_t offset, struct tm * tmp);
    199      1.45   mlelstv static int		tmcomp(const struct tm * atmp,
    200      1.45   mlelstv 				const struct tm * btmp);
    201  1.67.2.3       tls static int_fast32_t	transtime(int year, const struct rule * rulep,
    202  1.67.2.3       tls 				  int_fast32_t offset)
    203  1.67.2.3       tls   ATTRIBUTE_PURE;
    204      1.49  christos static int		typesequiv(const timezone_t sp, int a, int b);
    205      1.49  christos static int		tzload(timezone_t sp, const char * name,
    206      1.45   mlelstv 				int doextend);
    207      1.49  christos static int		tzparse(timezone_t sp, const char * name,
    208      1.45   mlelstv 				int lastditch);
    209      1.45   mlelstv static void		tzset_unlocked(void);
    210      1.45   mlelstv static void		tzsetwall_unlocked(void);
    211  1.67.2.3       tls static int_fast64_t	leapcorr(const timezone_t sp, time_t * timep);
    212       1.1       jtc 
    213      1.49  christos static timezone_t lclptr;
    214      1.49  christos static timezone_t gmtptr;
    215       1.1       jtc 
    216       1.1       jtc #ifndef TZ_STRLEN_MAX
    217       1.1       jtc #define TZ_STRLEN_MAX 255
    218       1.1       jtc #endif /* !defined TZ_STRLEN_MAX */
    219       1.1       jtc 
    220      1.45   mlelstv static char		lcl_TZname[TZ_STRLEN_MAX + 1];
    221      1.45   mlelstv static int		lcl_is_set;
    222      1.45   mlelstv static int		gmt_is_set;
    223      1.42  christos 
    224      1.42  christos #if !defined(__LIBC12_SOURCE__)
    225       1.1       jtc 
    226      1.16   mycroft __aconst char *		tzname[2] = {
    227      1.37  christos 	(__aconst char *)__UNCONST(wildabbr),
    228      1.37  christos 	(__aconst char *)__UNCONST(wildabbr)
    229       1.1       jtc };
    230       1.1       jtc 
    231      1.42  christos #else
    232      1.42  christos 
    233      1.42  christos extern __aconst char *	tzname[2];
    234      1.42  christos 
    235      1.42  christos #endif
    236      1.42  christos 
    237      1.33  christos #ifdef _REENTRANT
    238      1.45   mlelstv static rwlock_t lcl_lock = RWLOCK_INITIALIZER;
    239      1.19    kleink #endif
    240      1.19    kleink 
    241       1.1       jtc /*
    242       1.1       jtc ** Section 4.12.3 of X3.159-1989 requires that
    243       1.1       jtc **	Except for the strftime function, these functions [asctime,
    244       1.1       jtc **	ctime, gmtime, localtime] return values in one of two static
    245       1.1       jtc **	objects: a broken-down time structure and an array of char.
    246      1.45   mlelstv ** Thanks to Paul Eggert for noting this.
    247       1.1       jtc */
    248       1.1       jtc 
    249       1.1       jtc static struct tm	tm;
    250       1.1       jtc 
    251       1.1       jtc #ifdef USG_COMPAT
    252      1.42  christos #if !defined(__LIBC12_SOURCE__)
    253      1.42  christos long 			timezone = 0;
    254       1.1       jtc int			daylight = 0;
    255      1.42  christos #else
    256      1.42  christos extern int		daylight;
    257      1.42  christos extern long		timezone __RENAME(__timezone13);
    258      1.42  christos #endif
    259       1.1       jtc #endif /* defined USG_COMPAT */
    260       1.1       jtc 
    261       1.1       jtc #ifdef ALTZONE
    262  1.67.2.3       tls long			altzone = 0;
    263       1.1       jtc #endif /* defined ALTZONE */
    264       1.1       jtc 
    265  1.67.2.3       tls static int_fast32_t
    266      1.49  christos detzcode(const char *const codep)
    267       1.1       jtc {
    268  1.67.2.3       tls 	int_fast32_t	result;
    269      1.49  christos 	int	i;
    270      1.45   mlelstv 
    271  1.67.2.3       tls 	result = (codep[0] & 0x80) ? -1 : 0;
    272      1.45   mlelstv 	for (i = 0; i < 4; ++i)
    273      1.45   mlelstv 		result = (result << 8) | (codep[i] & 0xff);
    274      1.45   mlelstv 	return result;
    275      1.45   mlelstv }
    276      1.45   mlelstv 
    277  1.67.2.3       tls static int_fast64_t
    278      1.49  christos detzcode64(const char *const codep)
    279      1.45   mlelstv {
    280  1.67.2.3       tls 	int_fast64_t	result;
    281      1.49  christos 	int	i;
    282       1.1       jtc 
    283  1.67.2.3       tls 	result = (codep[0] & 0x80) ? -1 : 0;
    284      1.45   mlelstv 	for (i = 0; i < 8; ++i)
    285  1.67.2.3       tls 		result = (result << 8) | (codep[i] & 0xff);
    286       1.1       jtc 	return result;
    287       1.1       jtc }
    288       1.1       jtc 
    289      1.49  christos const char *
    290      1.49  christos tzgetname(const timezone_t sp, int isdst)
    291      1.49  christos {
    292      1.49  christos 	int i;
    293      1.49  christos 	for (i = 0; i < sp->timecnt; ++i) {
    294      1.49  christos 		const struct ttinfo *const ttisp = &sp->ttis[sp->types[i]];
    295      1.49  christos 
    296      1.49  christos 		if (ttisp->tt_isdst == isdst)
    297      1.49  christos 			return &sp->chars[ttisp->tt_abbrind];
    298      1.49  christos 	}
    299      1.49  christos 	return NULL;
    300      1.49  christos }
    301      1.49  christos 
    302      1.49  christos static void
    303      1.49  christos settzname_z(timezone_t sp)
    304      1.49  christos {
    305      1.49  christos 	int			i;
    306      1.49  christos 
    307      1.49  christos 	/*
    308      1.49  christos 	** Scrub the abbreviations.
    309      1.49  christos 	** First, replace bogus characters.
    310      1.49  christos 	*/
    311      1.49  christos 	for (i = 0; i < sp->charcnt; ++i)
    312      1.49  christos 		if (strchr(TZ_ABBR_CHAR_SET, sp->chars[i]) == NULL)
    313      1.49  christos 			sp->chars[i] = TZ_ABBR_ERR_CHAR;
    314      1.49  christos 	/*
    315      1.49  christos 	** Second, truncate long abbreviations.
    316      1.49  christos 	*/
    317      1.49  christos 	for (i = 0; i < sp->typecnt; ++i) {
    318      1.49  christos 		const struct ttinfo * const	ttisp = &sp->ttis[i];
    319      1.49  christos 		char *				cp = &sp->chars[ttisp->tt_abbrind];
    320      1.49  christos 
    321      1.49  christos 		if (strlen(cp) > TZ_ABBR_MAX_LEN &&
    322      1.49  christos 			strcmp(cp, GRANDPARENTED) != 0)
    323      1.49  christos 				*(cp + TZ_ABBR_MAX_LEN) = '\0';
    324      1.49  christos 	}
    325      1.49  christos }
    326      1.49  christos 
    327      1.45   mlelstv static void
    328      1.45   mlelstv settzname(void)
    329       1.1       jtc {
    330      1.49  christos 	timezone_t const	sp = lclptr;
    331      1.49  christos 	int			i;
    332       1.1       jtc 
    333      1.37  christos 	tzname[0] = (__aconst char *)__UNCONST(wildabbr);
    334      1.37  christos 	tzname[1] = (__aconst char *)__UNCONST(wildabbr);
    335       1.1       jtc #ifdef USG_COMPAT
    336       1.1       jtc 	daylight = 0;
    337       1.1       jtc 	timezone = 0;
    338       1.1       jtc #endif /* defined USG_COMPAT */
    339       1.1       jtc #ifdef ALTZONE
    340       1.1       jtc 	altzone = 0;
    341       1.1       jtc #endif /* defined ALTZONE */
    342       1.1       jtc 	if (sp == NULL) {
    343      1.37  christos 		tzname[0] = tzname[1] = (__aconst char *)__UNCONST(gmt);
    344       1.1       jtc 		return;
    345       1.1       jtc 	}
    346      1.58  christos 	/*
    347      1.58  christos 	** And to get the latest zone names into tzname. . .
    348      1.58  christos 	*/
    349       1.1       jtc 	for (i = 0; i < sp->typecnt; ++i) {
    350      1.49  christos 		const struct ttinfo * const	ttisp = &sp->ttis[i];
    351       1.1       jtc 
    352  1.67.2.2       tls 		tzname[ttisp->tt_isdst] = &sp->chars[ttisp->tt_abbrind];
    353      1.45   mlelstv #ifdef USG_COMPAT
    354      1.45   mlelstv 		if (ttisp->tt_isdst)
    355      1.45   mlelstv 			daylight = 1;
    356      1.58  christos 		if (!ttisp->tt_isdst)
    357      1.45   mlelstv 			timezone = -(ttisp->tt_gmtoff);
    358      1.45   mlelstv #endif /* defined USG_COMPAT */
    359      1.45   mlelstv #ifdef ALTZONE
    360  1.67.2.1       tls 		if (ttisp->tt_isdst)
    361      1.45   mlelstv 			altzone = -(ttisp->tt_gmtoff);
    362      1.45   mlelstv #endif /* defined ALTZONE */
    363       1.1       jtc 	}
    364      1.49  christos 	settzname_z(sp);
    365       1.1       jtc }
    366       1.1       jtc 
    367      1.45   mlelstv static int
    368      1.49  christos differ_by_repeat(const time_t t1, const time_t t0)
    369      1.45   mlelstv {
    370  1.67.2.3       tls 	if (TYPE_BIT(time_t) - TYPE_SIGNED(time_t) < SECSPERREPEAT_BITS)
    371  1.67.2.3       tls 		return 0;
    372      1.45   mlelstv 	return (int_fast64_t)t1 - (int_fast64_t)t0 == SECSPERREPEAT;
    373      1.45   mlelstv }
    374      1.45   mlelstv 
    375      1.45   mlelstv static int
    376      1.49  christos tzload(timezone_t sp, const char *name, const int doextend)
    377      1.49  christos {
    378      1.49  christos 	const char *		p;
    379      1.49  christos 	int			i;
    380      1.49  christos 	int			fid;
    381      1.49  christos 	int			stored;
    382      1.66  christos 	ssize_t			nread;
    383      1.58  christos 	typedef union {
    384      1.45   mlelstv 		struct tzhead	tzhead;
    385      1.45   mlelstv 		char		buf[2 * sizeof(struct tzhead) +
    386      1.45   mlelstv 					2 * sizeof *sp +
    387      1.45   mlelstv 					4 * TZ_MAX_TIMES];
    388      1.58  christos 	} u_t;
    389      1.58  christos 	u_t *			up;
    390      1.58  christos 
    391  1.67.2.3       tls 	up = malloc(sizeof *up);
    392      1.58  christos 	if (up == NULL)
    393      1.58  christos 		return -1;
    394       1.1       jtc 
    395      1.47  christos 	sp->goback = sp->goahead = FALSE;
    396       1.1       jtc 	if (name == NULL && (name = TZDEFAULT) == NULL)
    397      1.58  christos 		goto oops;
    398       1.1       jtc 	{
    399      1.49  christos 		int	doaccess;
    400       1.1       jtc 		/*
    401       1.1       jtc 		** Section 4.9.1 of the C standard says that
    402       1.1       jtc 		** "FILENAME_MAX expands to an integral constant expression
    403      1.10       jtc 		** that is the size needed for an array of char large enough
    404       1.1       jtc 		** to hold the longest file name string that the implementation
    405       1.1       jtc 		** guarantees can be opened."
    406       1.1       jtc 		*/
    407       1.1       jtc 		char		fullname[FILENAME_MAX + 1];
    408       1.1       jtc 
    409       1.1       jtc 		if (name[0] == ':')
    410       1.1       jtc 			++name;
    411       1.1       jtc 		doaccess = name[0] == '/';
    412       1.1       jtc 		if (!doaccess) {
    413       1.1       jtc 			if ((p = TZDIR) == NULL)
    414      1.58  christos 				goto oops;
    415       1.1       jtc 			if ((strlen(p) + strlen(name) + 1) >= sizeof fullname)
    416      1.58  christos 				goto oops;
    417       1.8       mrg 			(void) strcpy(fullname, p);	/* XXX strcpy is safe */
    418       1.8       mrg 			(void) strcat(fullname, "/");	/* XXX strcat is safe */
    419       1.8       mrg 			(void) strcat(fullname, name);	/* XXX strcat is safe */
    420       1.1       jtc 			/*
    421       1.1       jtc 			** Set doaccess if '.' (as in "../") shows up in name.
    422       1.1       jtc 			*/
    423       1.1       jtc 			if (strchr(name, '.') != NULL)
    424       1.1       jtc 				doaccess = TRUE;
    425       1.1       jtc 			name = fullname;
    426       1.1       jtc 		}
    427       1.1       jtc 		if (doaccess && access(name, R_OK) != 0)
    428      1.58  christos 			goto oops;
    429       1.9       mrg 		/*
    430       1.9       mrg 		 * XXX potential security problem here if user of a set-id
    431       1.9       mrg 		 * program has set TZ (which is passed in as name) here,
    432       1.9       mrg 		 * and uses a race condition trick to defeat the access(2)
    433       1.9       mrg 		 * above.
    434       1.9       mrg 		 */
    435       1.1       jtc 		if ((fid = open(name, OPEN_MODE)) == -1)
    436      1.58  christos 			goto oops;
    437       1.1       jtc 	}
    438      1.58  christos 	nread = read(fid, up->buf, sizeof up->buf);
    439      1.45   mlelstv 	if (close(fid) < 0 || nread <= 0)
    440      1.58  christos 		goto oops;
    441      1.45   mlelstv 	for (stored = 4; stored <= 8; stored *= 2) {
    442       1.1       jtc 		int		ttisstdcnt;
    443       1.1       jtc 		int		ttisgmtcnt;
    444  1.67.2.3       tls 		int		timecnt;
    445       1.1       jtc 
    446      1.58  christos 		ttisstdcnt = (int) detzcode(up->tzhead.tzh_ttisstdcnt);
    447      1.58  christos 		ttisgmtcnt = (int) detzcode(up->tzhead.tzh_ttisgmtcnt);
    448      1.58  christos 		sp->leapcnt = (int) detzcode(up->tzhead.tzh_leapcnt);
    449      1.58  christos 		sp->timecnt = (int) detzcode(up->tzhead.tzh_timecnt);
    450      1.58  christos 		sp->typecnt = (int) detzcode(up->tzhead.tzh_typecnt);
    451      1.58  christos 		sp->charcnt = (int) detzcode(up->tzhead.tzh_charcnt);
    452      1.58  christos 		p = up->tzhead.tzh_charcnt + sizeof up->tzhead.tzh_charcnt;
    453       1.1       jtc 		if (sp->leapcnt < 0 || sp->leapcnt > TZ_MAX_LEAPS ||
    454       1.1       jtc 			sp->typecnt <= 0 || sp->typecnt > TZ_MAX_TYPES ||
    455       1.1       jtc 			sp->timecnt < 0 || sp->timecnt > TZ_MAX_TIMES ||
    456       1.1       jtc 			sp->charcnt < 0 || sp->charcnt > TZ_MAX_CHARS ||
    457       1.1       jtc 			(ttisstdcnt != sp->typecnt && ttisstdcnt != 0) ||
    458       1.1       jtc 			(ttisgmtcnt != sp->typecnt && ttisgmtcnt != 0))
    459      1.58  christos 				goto oops;
    460      1.58  christos 		if (nread - (p - up->buf) <
    461      1.45   mlelstv 			sp->timecnt * stored +		/* ats */
    462       1.1       jtc 			sp->timecnt +			/* types */
    463      1.45   mlelstv 			sp->typecnt * 6 +		/* ttinfos */
    464       1.1       jtc 			sp->charcnt +			/* chars */
    465      1.45   mlelstv 			sp->leapcnt * (stored + 4) +	/* lsinfos */
    466       1.1       jtc 			ttisstdcnt +			/* ttisstds */
    467       1.1       jtc 			ttisgmtcnt)			/* ttisgmts */
    468      1.58  christos 				goto oops;
    469  1.67.2.3       tls 		timecnt = 0;
    470       1.1       jtc 		for (i = 0; i < sp->timecnt; ++i) {
    471  1.67.2.3       tls 			int_fast64_t at
    472  1.67.2.3       tls 			  = stored == 4 ? detzcode(p) : detzcode64(p);
    473  1.67.2.3       tls 			sp->types[i] = ((TYPE_SIGNED(time_t)
    474  1.67.2.3       tls 					 ? time_t_min <= at
    475  1.67.2.3       tls 					 : 0 <= at)
    476  1.67.2.3       tls 					&& at <= time_t_max);
    477  1.67.2.3       tls 			if (sp->types[i]) {
    478  1.67.2.3       tls 				if (i && !timecnt && at != time_t_min) {
    479  1.67.2.3       tls 					/*
    480  1.67.2.3       tls 					** Keep the earlier record, but tweak
    481  1.67.2.3       tls 					** it so that it starts with the
    482  1.67.2.3       tls 					** minimum time_t value.
    483  1.67.2.3       tls 					*/
    484  1.67.2.3       tls 					sp->types[i - 1] = 1;
    485  1.67.2.3       tls 					sp->ats[timecnt++] = time_t_min;
    486  1.67.2.3       tls 				}
    487  1.67.2.3       tls 				_DIAGASSERT(__type_fit(time_t, at));
    488  1.67.2.3       tls 				sp->ats[timecnt++] = (time_t)at;
    489  1.67.2.3       tls 			}
    490      1.45   mlelstv 			p += stored;
    491       1.1       jtc 		}
    492  1.67.2.3       tls 		timecnt = 0;
    493       1.1       jtc 		for (i = 0; i < sp->timecnt; ++i) {
    494  1.67.2.3       tls 			unsigned char typ = *p++;
    495  1.67.2.3       tls 			if (sp->typecnt <= typ)
    496      1.58  christos 				goto oops;
    497  1.67.2.3       tls 			if (sp->types[i])
    498  1.67.2.3       tls 				sp->types[timecnt++] = typ;
    499       1.1       jtc 		}
    500       1.1       jtc 		for (i = 0; i < sp->typecnt; ++i) {
    501      1.49  christos 			struct ttinfo *	ttisp;
    502       1.1       jtc 
    503       1.1       jtc 			ttisp = &sp->ttis[i];
    504       1.1       jtc 			ttisp->tt_gmtoff = detzcode(p);
    505       1.1       jtc 			p += 4;
    506       1.1       jtc 			ttisp->tt_isdst = (unsigned char) *p++;
    507       1.1       jtc 			if (ttisp->tt_isdst != 0 && ttisp->tt_isdst != 1)
    508      1.58  christos 				goto oops;
    509       1.1       jtc 			ttisp->tt_abbrind = (unsigned char) *p++;
    510       1.1       jtc 			if (ttisp->tt_abbrind < 0 ||
    511       1.1       jtc 				ttisp->tt_abbrind > sp->charcnt)
    512      1.58  christos 					goto oops;
    513       1.1       jtc 		}
    514       1.1       jtc 		for (i = 0; i < sp->charcnt; ++i)
    515       1.1       jtc 			sp->chars[i] = *p++;
    516       1.1       jtc 		sp->chars[i] = '\0';	/* ensure '\0' at end */
    517       1.1       jtc 		for (i = 0; i < sp->leapcnt; ++i) {
    518      1.49  christos 			struct lsinfo *	lsisp;
    519       1.1       jtc 
    520       1.1       jtc 			lsisp = &sp->lsis[i];
    521      1.66  christos 			lsisp->ls_trans = (time_t)((stored == 4) ?
    522      1.66  christos 			    detzcode(p) : detzcode64(p));
    523      1.45   mlelstv 			p += stored;
    524       1.1       jtc 			lsisp->ls_corr = detzcode(p);
    525       1.1       jtc 			p += 4;
    526       1.1       jtc 		}
    527       1.1       jtc 		for (i = 0; i < sp->typecnt; ++i) {
    528      1.49  christos 			struct ttinfo *	ttisp;
    529       1.1       jtc 
    530       1.1       jtc 			ttisp = &sp->ttis[i];
    531       1.1       jtc 			if (ttisstdcnt == 0)
    532       1.1       jtc 				ttisp->tt_ttisstd = FALSE;
    533       1.1       jtc 			else {
    534       1.1       jtc 				ttisp->tt_ttisstd = *p++;
    535       1.1       jtc 				if (ttisp->tt_ttisstd != TRUE &&
    536       1.1       jtc 					ttisp->tt_ttisstd != FALSE)
    537      1.58  christos 						goto oops;
    538       1.1       jtc 			}
    539       1.1       jtc 		}
    540       1.1       jtc 		for (i = 0; i < sp->typecnt; ++i) {
    541      1.49  christos 			struct ttinfo *	ttisp;
    542       1.1       jtc 
    543       1.1       jtc 			ttisp = &sp->ttis[i];
    544       1.1       jtc 			if (ttisgmtcnt == 0)
    545       1.1       jtc 				ttisp->tt_ttisgmt = FALSE;
    546       1.1       jtc 			else {
    547       1.1       jtc 				ttisp->tt_ttisgmt = *p++;
    548       1.1       jtc 				if (ttisp->tt_ttisgmt != TRUE &&
    549       1.1       jtc 					ttisp->tt_ttisgmt != FALSE)
    550      1.58  christos 						goto oops;
    551       1.1       jtc 			}
    552       1.1       jtc 		}
    553      1.45   mlelstv 		/*
    554      1.45   mlelstv 		** If this is an old file, we're done.
    555      1.45   mlelstv 		*/
    556      1.58  christos 		if (up->tzhead.tzh_version[0] == '\0')
    557      1.45   mlelstv 			break;
    558      1.58  christos 		nread -= p - up->buf;
    559      1.45   mlelstv 		for (i = 0; i < nread; ++i)
    560      1.58  christos 			up->buf[i] = p[i];
    561      1.45   mlelstv 		/*
    562  1.67.2.3       tls 		** If this is a signed narrow time_t system, we're done.
    563      1.45   mlelstv 		*/
    564  1.67.2.3       tls 		if (TYPE_SIGNED(time_t) && stored >= (int) sizeof(time_t))
    565      1.45   mlelstv 			break;
    566      1.45   mlelstv 	}
    567      1.45   mlelstv 	if (doextend && nread > 2 &&
    568      1.58  christos 		up->buf[0] == '\n' && up->buf[nread - 1] == '\n' &&
    569      1.45   mlelstv 		sp->typecnt + 2 <= TZ_MAX_TYPES) {
    570      1.49  christos 			struct __state ts;
    571      1.49  christos 			int	result;
    572      1.45   mlelstv 
    573      1.58  christos 			up->buf[nread - 1] = '\0';
    574      1.58  christos 			result = tzparse(&ts, &up->buf[1], FALSE);
    575      1.45   mlelstv 			if (result == 0 && ts.typecnt == 2 &&
    576      1.45   mlelstv 				sp->charcnt + ts.charcnt <= TZ_MAX_CHARS) {
    577      1.45   mlelstv 					for (i = 0; i < 2; ++i)
    578      1.45   mlelstv 						ts.ttis[i].tt_abbrind +=
    579      1.45   mlelstv 							sp->charcnt;
    580      1.45   mlelstv 					for (i = 0; i < ts.charcnt; ++i)
    581      1.45   mlelstv 						sp->chars[sp->charcnt++] =
    582      1.45   mlelstv 							ts.chars[i];
    583      1.45   mlelstv 					i = 0;
    584      1.45   mlelstv 					while (i < ts.timecnt &&
    585      1.45   mlelstv 						ts.ats[i] <=
    586      1.45   mlelstv 						sp->ats[sp->timecnt - 1])
    587      1.45   mlelstv 							++i;
    588      1.45   mlelstv 					while (i < ts.timecnt &&
    589      1.45   mlelstv 					    sp->timecnt < TZ_MAX_TIMES) {
    590      1.45   mlelstv 						sp->ats[sp->timecnt] =
    591      1.45   mlelstv 							ts.ats[i];
    592      1.45   mlelstv 						sp->types[sp->timecnt] =
    593      1.45   mlelstv 							sp->typecnt +
    594      1.45   mlelstv 							ts.types[i];
    595      1.45   mlelstv 						++sp->timecnt;
    596      1.45   mlelstv 						++i;
    597      1.45   mlelstv 					}
    598      1.45   mlelstv 					sp->ttis[sp->typecnt++] = ts.ttis[0];
    599      1.45   mlelstv 					sp->ttis[sp->typecnt++] = ts.ttis[1];
    600      1.45   mlelstv 			}
    601      1.45   mlelstv 	}
    602      1.45   mlelstv 	if (sp->timecnt > 1) {
    603      1.45   mlelstv 		for (i = 1; i < sp->timecnt; ++i)
    604      1.45   mlelstv 			if (typesequiv(sp, sp->types[i], sp->types[0]) &&
    605      1.45   mlelstv 				differ_by_repeat(sp->ats[i], sp->ats[0])) {
    606      1.45   mlelstv 					sp->goback = TRUE;
    607      1.45   mlelstv 					break;
    608      1.45   mlelstv 				}
    609      1.45   mlelstv 		for (i = sp->timecnt - 2; i >= 0; --i)
    610      1.45   mlelstv 			if (typesequiv(sp, sp->types[sp->timecnt - 1],
    611      1.45   mlelstv 				sp->types[i]) &&
    612      1.45   mlelstv 				differ_by_repeat(sp->ats[sp->timecnt - 1],
    613      1.45   mlelstv 				sp->ats[i])) {
    614      1.45   mlelstv 					sp->goahead = TRUE;
    615      1.45   mlelstv 					break;
    616      1.45   mlelstv 		}
    617       1.1       jtc 	}
    618  1.67.2.3       tls 	/*
    619  1.67.2.3       tls 	** If type 0 is is unused in transitions,
    620  1.67.2.3       tls 	** it's the type to use for early times.
    621  1.67.2.3       tls 	*/
    622  1.67.2.3       tls 	for (i = 0; i < sp->typecnt; ++i)
    623  1.67.2.3       tls 		if (sp->types[i] == 0)
    624  1.67.2.3       tls 			break;
    625  1.67.2.3       tls 	i = (i >= sp->typecnt) ? 0 : -1;
    626  1.67.2.3       tls 	/*
    627  1.67.2.3       tls 	** Absent the above,
    628  1.67.2.3       tls 	** if there are transition times
    629  1.67.2.3       tls 	** and the first transition is to a daylight time
    630  1.67.2.3       tls 	** find the standard type less than and closest to
    631  1.67.2.3       tls 	** the type of the first transition.
    632  1.67.2.3       tls 	*/
    633  1.67.2.3       tls 	if (i < 0 && sp->timecnt > 0 && sp->ttis[sp->types[0]].tt_isdst) {
    634  1.67.2.3       tls 		i = sp->types[0];
    635  1.67.2.3       tls 		while (--i >= 0)
    636  1.67.2.3       tls 			if (!sp->ttis[i].tt_isdst)
    637  1.67.2.3       tls 				break;
    638  1.67.2.3       tls 	}
    639  1.67.2.3       tls 	/*
    640  1.67.2.3       tls 	** If no result yet, find the first standard type.
    641  1.67.2.3       tls 	** If there is none, punt to type zero.
    642  1.67.2.3       tls 	*/
    643  1.67.2.3       tls 	if (i < 0) {
    644  1.67.2.3       tls 		i = 0;
    645  1.67.2.3       tls 		while (sp->ttis[i].tt_isdst)
    646  1.67.2.3       tls 			if (++i >= sp->typecnt) {
    647  1.67.2.3       tls 				i = 0;
    648  1.67.2.3       tls 				break;
    649  1.67.2.3       tls 			}
    650  1.67.2.3       tls 	}
    651  1.67.2.3       tls 	sp->defaulttype = i;
    652      1.58  christos 	free(up);
    653       1.1       jtc 	return 0;
    654      1.58  christos oops:
    655      1.58  christos 	free(up);
    656      1.58  christos 	return -1;
    657       1.1       jtc }
    658       1.1       jtc 
    659      1.45   mlelstv static int
    660      1.49  christos typesequiv(const timezone_t sp, const int a, const int b)
    661      1.45   mlelstv {
    662      1.49  christos 	int	result;
    663      1.45   mlelstv 
    664      1.45   mlelstv 	if (sp == NULL ||
    665      1.45   mlelstv 		a < 0 || a >= sp->typecnt ||
    666      1.45   mlelstv 		b < 0 || b >= sp->typecnt)
    667      1.45   mlelstv 			result = FALSE;
    668      1.45   mlelstv 	else {
    669      1.49  christos 		const struct ttinfo *	ap = &sp->ttis[a];
    670      1.49  christos 		const struct ttinfo *	bp = &sp->ttis[b];
    671      1.45   mlelstv 		result = ap->tt_gmtoff == bp->tt_gmtoff &&
    672      1.45   mlelstv 			ap->tt_isdst == bp->tt_isdst &&
    673      1.45   mlelstv 			ap->tt_ttisstd == bp->tt_ttisstd &&
    674      1.45   mlelstv 			ap->tt_ttisgmt == bp->tt_ttisgmt &&
    675      1.45   mlelstv 			strcmp(&sp->chars[ap->tt_abbrind],
    676      1.45   mlelstv 			&sp->chars[bp->tt_abbrind]) == 0;
    677      1.45   mlelstv 	}
    678      1.45   mlelstv 	return result;
    679      1.45   mlelstv }
    680      1.45   mlelstv 
    681       1.1       jtc static const int	mon_lengths[2][MONSPERYEAR] = {
    682       1.1       jtc 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    683       1.1       jtc 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    684       1.1       jtc };
    685       1.1       jtc 
    686       1.1       jtc static const int	year_lengths[2] = {
    687       1.1       jtc 	DAYSPERNYEAR, DAYSPERLYEAR
    688       1.1       jtc };
    689       1.1       jtc 
    690       1.1       jtc /*
    691       1.1       jtc ** Given a pointer into a time zone string, scan until a character that is not
    692      1.45   mlelstv ** a valid character in a zone name is found. Return a pointer to that
    693       1.1       jtc ** character.
    694       1.1       jtc */
    695       1.1       jtc 
    696       1.1       jtc static const char *
    697      1.67      matt getzname(const char *strp)
    698       1.1       jtc {
    699      1.49  christos 	char	c;
    700       1.1       jtc 
    701       1.5       jtc 	while ((c = *strp) != '\0' && !is_digit(c) && c != ',' && c != '-' &&
    702       1.1       jtc 		c != '+')
    703       1.1       jtc 			++strp;
    704       1.1       jtc 	return strp;
    705       1.1       jtc }
    706       1.1       jtc 
    707       1.1       jtc /*
    708      1.45   mlelstv ** Given a pointer into an extended time zone string, scan until the ending
    709      1.45   mlelstv ** delimiter of the zone name is located. Return a pointer to the delimiter.
    710      1.45   mlelstv **
    711      1.45   mlelstv ** As with getzname above, the legal character set is actually quite
    712      1.45   mlelstv ** restricted, with other characters producing undefined results.
    713      1.45   mlelstv ** We don't do any checking here; checking is done later in common-case code.
    714      1.45   mlelstv */
    715      1.45   mlelstv 
    716      1.45   mlelstv static const char *
    717      1.49  christos getqzname(const char *strp, const int delim)
    718      1.45   mlelstv {
    719      1.49  christos 	int	c;
    720      1.45   mlelstv 
    721      1.45   mlelstv 	while ((c = *strp) != '\0' && c != delim)
    722      1.45   mlelstv 		++strp;
    723      1.45   mlelstv 	return strp;
    724      1.45   mlelstv }
    725      1.45   mlelstv 
    726      1.45   mlelstv /*
    727       1.1       jtc ** Given a pointer into a time zone string, extract a number from that string.
    728       1.1       jtc ** Check that the number is within a specified range; if it is not, return
    729       1.1       jtc ** NULL.
    730       1.1       jtc ** Otherwise, return a pointer to the first character not part of the number.
    731       1.1       jtc */
    732       1.1       jtc 
    733       1.1       jtc static const char *
    734  1.67.2.1       tls getnum(const char *strp, int *const nump, const int min, const int max)
    735       1.1       jtc {
    736      1.49  christos 	char	c;
    737      1.49  christos 	int	num;
    738       1.1       jtc 
    739      1.46  christos 	if (strp == NULL || !is_digit(c = *strp)) {
    740      1.46  christos 		errno = EINVAL;
    741       1.1       jtc 		return NULL;
    742      1.46  christos 	}
    743       1.1       jtc 	num = 0;
    744       1.5       jtc 	do {
    745       1.1       jtc 		num = num * 10 + (c - '0');
    746      1.46  christos 		if (num > max) {
    747      1.46  christos 			errno = EOVERFLOW;
    748       1.1       jtc 			return NULL;	/* illegal value */
    749      1.46  christos 		}
    750       1.5       jtc 		c = *++strp;
    751       1.5       jtc 	} while (is_digit(c));
    752      1.46  christos 	if (num < min) {
    753      1.46  christos 		errno = EINVAL;
    754       1.1       jtc 		return NULL;		/* illegal value */
    755      1.46  christos 	}
    756       1.1       jtc 	*nump = num;
    757       1.1       jtc 	return strp;
    758       1.1       jtc }
    759       1.1       jtc 
    760       1.1       jtc /*
    761       1.1       jtc ** Given a pointer into a time zone string, extract a number of seconds,
    762       1.1       jtc ** in hh[:mm[:ss]] form, from the string.
    763       1.1       jtc ** If any error occurs, return NULL.
    764       1.1       jtc ** Otherwise, return a pointer to the first character not part of the number
    765       1.1       jtc ** of seconds.
    766       1.1       jtc */
    767       1.1       jtc 
    768       1.1       jtc static const char *
    769  1.67.2.3       tls getsecs(const char *strp, int_fast32_t *const secsp)
    770       1.1       jtc {
    771       1.1       jtc 	int	num;
    772       1.1       jtc 
    773       1.1       jtc 	/*
    774       1.1       jtc 	** `HOURSPERDAY * DAYSPERWEEK - 1' allows quasi-Posix rules like
    775       1.1       jtc 	** "M10.4.6/26", which does not conform to Posix,
    776       1.1       jtc 	** but which specifies the equivalent of
    777       1.1       jtc 	** ``02:00 on the first Sunday on or after 23 Oct''.
    778       1.1       jtc 	*/
    779       1.1       jtc 	strp = getnum(strp, &num, 0, HOURSPERDAY * DAYSPERWEEK - 1);
    780       1.1       jtc 	if (strp == NULL)
    781       1.1       jtc 		return NULL;
    782  1.67.2.3       tls 	*secsp = num * (int_fast32_t) SECSPERHOUR;
    783       1.1       jtc 	if (*strp == ':') {
    784       1.1       jtc 		++strp;
    785       1.1       jtc 		strp = getnum(strp, &num, 0, MINSPERHOUR - 1);
    786       1.1       jtc 		if (strp == NULL)
    787       1.1       jtc 			return NULL;
    788       1.1       jtc 		*secsp += num * SECSPERMIN;
    789       1.1       jtc 		if (*strp == ':') {
    790       1.1       jtc 			++strp;
    791      1.45   mlelstv 			/* `SECSPERMIN' allows for leap seconds. */
    792       1.1       jtc 			strp = getnum(strp, &num, 0, SECSPERMIN);
    793       1.1       jtc 			if (strp == NULL)
    794       1.1       jtc 				return NULL;
    795       1.1       jtc 			*secsp += num;
    796       1.1       jtc 		}
    797       1.1       jtc 	}
    798       1.1       jtc 	return strp;
    799       1.1       jtc }
    800       1.1       jtc 
    801       1.1       jtc /*
    802       1.1       jtc ** Given a pointer into a time zone string, extract an offset, in
    803       1.1       jtc ** [+-]hh[:mm[:ss]] form, from the string.
    804       1.1       jtc ** If any error occurs, return NULL.
    805       1.1       jtc ** Otherwise, return a pointer to the first character not part of the time.
    806       1.1       jtc */
    807       1.1       jtc 
    808       1.1       jtc static const char *
    809  1.67.2.3       tls getoffset(const char *strp, int_fast32_t *const offsetp)
    810       1.1       jtc {
    811      1.49  christos 	int	neg = 0;
    812       1.1       jtc 
    813       1.1       jtc 	if (*strp == '-') {
    814       1.1       jtc 		neg = 1;
    815       1.1       jtc 		++strp;
    816       1.5       jtc 	} else if (*strp == '+')
    817       1.5       jtc 		++strp;
    818       1.1       jtc 	strp = getsecs(strp, offsetp);
    819       1.1       jtc 	if (strp == NULL)
    820       1.1       jtc 		return NULL;		/* illegal time */
    821       1.1       jtc 	if (neg)
    822       1.1       jtc 		*offsetp = -*offsetp;
    823       1.1       jtc 	return strp;
    824       1.1       jtc }
    825       1.1       jtc 
    826       1.1       jtc /*
    827       1.1       jtc ** Given a pointer into a time zone string, extract a rule in the form
    828      1.45   mlelstv ** date[/time]. See POSIX section 8 for the format of "date" and "time".
    829       1.1       jtc ** If a valid rule is not found, return NULL.
    830       1.1       jtc ** Otherwise, return a pointer to the first character not part of the rule.
    831       1.1       jtc */
    832       1.1       jtc 
    833       1.1       jtc static const char *
    834      1.49  christos getrule(const char *strp, struct rule *const rulep)
    835       1.1       jtc {
    836       1.1       jtc 	if (*strp == 'J') {
    837       1.1       jtc 		/*
    838       1.1       jtc 		** Julian day.
    839       1.1       jtc 		*/
    840       1.1       jtc 		rulep->r_type = JULIAN_DAY;
    841       1.1       jtc 		++strp;
    842       1.1       jtc 		strp = getnum(strp, &rulep->r_day, 1, DAYSPERNYEAR);
    843       1.1       jtc 	} else if (*strp == 'M') {
    844       1.1       jtc 		/*
    845       1.1       jtc 		** Month, week, day.
    846       1.1       jtc 		*/
    847       1.1       jtc 		rulep->r_type = MONTH_NTH_DAY_OF_WEEK;
    848       1.1       jtc 		++strp;
    849       1.1       jtc 		strp = getnum(strp, &rulep->r_mon, 1, MONSPERYEAR);
    850       1.1       jtc 		if (strp == NULL)
    851       1.1       jtc 			return NULL;
    852       1.1       jtc 		if (*strp++ != '.')
    853       1.1       jtc 			return NULL;
    854       1.1       jtc 		strp = getnum(strp, &rulep->r_week, 1, 5);
    855       1.1       jtc 		if (strp == NULL)
    856       1.1       jtc 			return NULL;
    857       1.1       jtc 		if (*strp++ != '.')
    858       1.1       jtc 			return NULL;
    859       1.1       jtc 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERWEEK - 1);
    860       1.5       jtc 	} else if (is_digit(*strp)) {
    861       1.1       jtc 		/*
    862       1.1       jtc 		** Day of year.
    863       1.1       jtc 		*/
    864       1.1       jtc 		rulep->r_type = DAY_OF_YEAR;
    865       1.1       jtc 		strp = getnum(strp, &rulep->r_day, 0, DAYSPERLYEAR - 1);
    866       1.1       jtc 	} else	return NULL;		/* invalid format */
    867       1.1       jtc 	if (strp == NULL)
    868       1.1       jtc 		return NULL;
    869       1.1       jtc 	if (*strp == '/') {
    870       1.1       jtc 		/*
    871       1.1       jtc 		** Time specified.
    872       1.1       jtc 		*/
    873       1.1       jtc 		++strp;
    874  1.67.2.3       tls 		strp = getoffset(strp, &rulep->r_time);
    875       1.1       jtc 	} else	rulep->r_time = 2 * SECSPERHOUR;	/* default = 2:00:00 */
    876       1.1       jtc 	return strp;
    877       1.1       jtc }
    878       1.1       jtc 
    879       1.1       jtc /*
    880  1.67.2.3       tls ** Given a year, a rule, and the offset from UT at the time that rule takes
    881  1.67.2.3       tls ** effect, calculate the year-relative time that rule takes effect.
    882       1.1       jtc */
    883       1.1       jtc 
    884  1.67.2.3       tls static int_fast32_t
    885  1.67.2.3       tls transtime(const int year, const struct rule *const rulep,
    886  1.67.2.3       tls 	  const int_fast32_t offset)
    887      1.49  christos {
    888      1.49  christos 	int	leapyear;
    889  1.67.2.3       tls 	int_fast32_t	value;
    890      1.49  christos 	int	i;
    891       1.1       jtc 	int		d, m1, yy0, yy1, yy2, dow;
    892       1.1       jtc 
    893       1.1       jtc 	INITIALIZE(value);
    894       1.1       jtc 	leapyear = isleap(year);
    895       1.1       jtc 	switch (rulep->r_type) {
    896       1.1       jtc 
    897       1.1       jtc 	case JULIAN_DAY:
    898       1.1       jtc 		/*
    899       1.1       jtc 		** Jn - Julian day, 1 == January 1, 60 == March 1 even in leap
    900       1.1       jtc 		** years.
    901       1.1       jtc 		** In non-leap years, or if the day number is 59 or less, just
    902       1.1       jtc 		** add SECSPERDAY times the day number-1 to the time of
    903       1.1       jtc 		** January 1, midnight, to get the day.
    904       1.1       jtc 		*/
    905  1.67.2.3       tls 		value = (rulep->r_day - 1) * SECSPERDAY;
    906       1.1       jtc 		if (leapyear && rulep->r_day >= 60)
    907       1.1       jtc 			value += SECSPERDAY;
    908       1.1       jtc 		break;
    909       1.1       jtc 
    910       1.1       jtc 	case DAY_OF_YEAR:
    911       1.1       jtc 		/*
    912       1.1       jtc 		** n - day of year.
    913       1.1       jtc 		** Just add SECSPERDAY times the day number to the time of
    914       1.1       jtc 		** January 1, midnight, to get the day.
    915       1.1       jtc 		*/
    916  1.67.2.3       tls 		value = rulep->r_day * SECSPERDAY;
    917       1.1       jtc 		break;
    918       1.1       jtc 
    919       1.1       jtc 	case MONTH_NTH_DAY_OF_WEEK:
    920       1.1       jtc 		/*
    921       1.1       jtc 		** Mm.n.d - nth "dth day" of month m.
    922       1.1       jtc 		*/
    923       1.1       jtc 
    924       1.1       jtc 		/*
    925       1.1       jtc 		** Use Zeller's Congruence to get day-of-week of first day of
    926       1.1       jtc 		** month.
    927       1.1       jtc 		*/
    928       1.1       jtc 		m1 = (rulep->r_mon + 9) % 12 + 1;
    929       1.1       jtc 		yy0 = (rulep->r_mon <= 2) ? (year - 1) : year;
    930       1.1       jtc 		yy1 = yy0 / 100;
    931       1.1       jtc 		yy2 = yy0 % 100;
    932       1.1       jtc 		dow = ((26 * m1 - 2) / 10 +
    933       1.1       jtc 			1 + yy2 + yy2 / 4 + yy1 / 4 - 2 * yy1) % 7;
    934       1.1       jtc 		if (dow < 0)
    935       1.1       jtc 			dow += DAYSPERWEEK;
    936       1.1       jtc 
    937       1.1       jtc 		/*
    938      1.45   mlelstv 		** "dow" is the day-of-week of the first day of the month. Get
    939       1.1       jtc 		** the day-of-month (zero-origin) of the first "dow" day of the
    940       1.1       jtc 		** month.
    941       1.1       jtc 		*/
    942       1.1       jtc 		d = rulep->r_day - dow;
    943       1.1       jtc 		if (d < 0)
    944       1.1       jtc 			d += DAYSPERWEEK;
    945       1.1       jtc 		for (i = 1; i < rulep->r_week; ++i) {
    946       1.1       jtc 			if (d + DAYSPERWEEK >=
    947       1.1       jtc 				mon_lengths[leapyear][rulep->r_mon - 1])
    948       1.1       jtc 					break;
    949       1.1       jtc 			d += DAYSPERWEEK;
    950       1.1       jtc 		}
    951       1.1       jtc 
    952       1.1       jtc 		/*
    953       1.1       jtc 		** "d" is the day-of-month (zero-origin) of the day we want.
    954       1.1       jtc 		*/
    955  1.67.2.3       tls 		value = d * SECSPERDAY;
    956  1.67.2.3       tls 		for (i = 0; i < rulep->r_mon - 1; ++i)
    957  1.67.2.3       tls 			value += mon_lengths[leapyear][i] * SECSPERDAY;
    958       1.1       jtc 		break;
    959       1.1       jtc 	}
    960       1.1       jtc 
    961       1.1       jtc 	/*
    962  1.67.2.3       tls 	** "value" is the year-relative time of 00:00:00 UT on the day in
    963  1.67.2.3       tls 	** question. To get the year-relative time of the specified local
    964       1.1       jtc 	** time on that day, add the transition time and the current offset
    965  1.67.2.3       tls 	** from UT.
    966       1.1       jtc 	*/
    967  1.67.2.3       tls 	return value + rulep->r_time + offset;
    968       1.1       jtc }
    969       1.1       jtc 
    970       1.1       jtc /*
    971       1.1       jtc ** Given a POSIX section 8-style TZ string, fill in the rule tables as
    972       1.1       jtc ** appropriate.
    973       1.1       jtc */
    974       1.1       jtc 
    975       1.1       jtc static int
    976      1.49  christos tzparse(timezone_t sp, const char *name, const int lastditch)
    977       1.1       jtc {
    978  1.67.2.3       tls 	const char *		stdname;
    979  1.67.2.3       tls 	const char *		dstname;
    980  1.67.2.3       tls 	size_t			stdlen;
    981  1.67.2.3       tls 	size_t			dstlen;
    982  1.67.2.3       tls 	int_fast32_t		stdoffset;
    983  1.67.2.3       tls 	int_fast32_t		dstoffset;
    984      1.49  christos 	char *			cp;
    985      1.49  christos 	int			load_result;
    986       1.1       jtc 
    987       1.1       jtc 	INITIALIZE(dstname);
    988       1.1       jtc 	stdname = name;
    989       1.1       jtc 	if (lastditch) {
    990       1.1       jtc 		stdlen = strlen(name);	/* length of standard zone name */
    991       1.1       jtc 		name += stdlen;
    992       1.1       jtc 		if (stdlen >= sizeof sp->chars)
    993       1.1       jtc 			stdlen = (sizeof sp->chars) - 1;
    994      1.10       jtc 		stdoffset = 0;
    995       1.1       jtc 	} else {
    996      1.45   mlelstv 		if (*name == '<') {
    997      1.45   mlelstv 			name++;
    998      1.45   mlelstv 			stdname = name;
    999      1.45   mlelstv 			name = getqzname(name, '>');
   1000      1.45   mlelstv 			if (*name != '>')
   1001      1.45   mlelstv 				return (-1);
   1002      1.45   mlelstv 			stdlen = name - stdname;
   1003      1.45   mlelstv 			name++;
   1004      1.45   mlelstv 		} else {
   1005      1.45   mlelstv 			name = getzname(name);
   1006      1.45   mlelstv 			stdlen = name - stdname;
   1007      1.45   mlelstv 		}
   1008      1.10       jtc 		if (*name == '\0')
   1009      1.10       jtc 			return -1;
   1010      1.45   mlelstv 		name = getoffset(name, &stdoffset);
   1011       1.1       jtc 		if (name == NULL)
   1012       1.1       jtc 			return -1;
   1013       1.1       jtc 	}
   1014      1.49  christos 	load_result = tzload(sp, TZDEFRULES, FALSE);
   1015       1.1       jtc 	if (load_result != 0)
   1016       1.1       jtc 		sp->leapcnt = 0;		/* so, we're off a little */
   1017       1.1       jtc 	if (*name != '\0') {
   1018      1.45   mlelstv 		if (*name == '<') {
   1019      1.45   mlelstv 			dstname = ++name;
   1020      1.45   mlelstv 			name = getqzname(name, '>');
   1021      1.45   mlelstv 			if (*name != '>')
   1022      1.45   mlelstv 				return -1;
   1023      1.45   mlelstv 			dstlen = name - dstname;
   1024      1.45   mlelstv 			name++;
   1025      1.45   mlelstv 		} else {
   1026      1.45   mlelstv 			dstname = name;
   1027      1.45   mlelstv 			name = getzname(name);
   1028      1.45   mlelstv 			dstlen = name - dstname; /* length of DST zone name */
   1029      1.45   mlelstv 		}
   1030       1.1       jtc 		if (*name != '\0' && *name != ',' && *name != ';') {
   1031      1.45   mlelstv 			name = getoffset(name, &dstoffset);
   1032       1.1       jtc 			if (name == NULL)
   1033       1.1       jtc 				return -1;
   1034       1.1       jtc 		} else	dstoffset = stdoffset - SECSPERHOUR;
   1035      1.22    kleink 		if (*name == '\0' && load_result != 0)
   1036      1.22    kleink 			name = TZDEFRULESTRING;
   1037       1.1       jtc 		if (*name == ',' || *name == ';') {
   1038       1.1       jtc 			struct rule	start;
   1039       1.1       jtc 			struct rule	end;
   1040  1.67.2.3       tls 			int		year;
   1041  1.67.2.3       tls 			int		yearlim;
   1042  1.67.2.3       tls 			int		timecnt;
   1043  1.67.2.3       tls 			time_t		janfirst;
   1044       1.1       jtc 
   1045       1.1       jtc 			++name;
   1046      1.45   mlelstv 			if ((name = getrule(name, &start)) == NULL)
   1047       1.1       jtc 				return -1;
   1048       1.1       jtc 			if (*name++ != ',')
   1049       1.1       jtc 				return -1;
   1050      1.45   mlelstv 			if ((name = getrule(name, &end)) == NULL)
   1051       1.1       jtc 				return -1;
   1052       1.1       jtc 			if (*name != '\0')
   1053       1.1       jtc 				return -1;
   1054       1.1       jtc 			sp->typecnt = 2;	/* standard time and DST */
   1055       1.1       jtc 			/*
   1056      1.45   mlelstv 			** Two transitions per year, from EPOCH_YEAR forward.
   1057       1.1       jtc 			*/
   1058      1.58  christos 			memset(sp->ttis, 0, sizeof(sp->ttis));
   1059       1.1       jtc 			sp->ttis[0].tt_gmtoff = -dstoffset;
   1060       1.1       jtc 			sp->ttis[0].tt_isdst = 1;
   1061      1.66  christos 			sp->ttis[0].tt_abbrind = (int)(stdlen + 1);
   1062       1.1       jtc 			sp->ttis[1].tt_gmtoff = -stdoffset;
   1063       1.1       jtc 			sp->ttis[1].tt_isdst = 0;
   1064       1.1       jtc 			sp->ttis[1].tt_abbrind = 0;
   1065  1.67.2.3       tls 			sp->defaulttype = 0;
   1066  1.67.2.3       tls 			timecnt = 0;
   1067       1.1       jtc 			janfirst = 0;
   1068      1.45   mlelstv 			sp->timecnt = 0;
   1069  1.67.2.3       tls 			yearlim = EPOCH_YEAR + YEARSPERREPEAT;
   1070  1.67.2.3       tls 			for (year = EPOCH_YEAR; year < yearlim; year++) {
   1071  1.67.2.3       tls 				int_fast32_t
   1072  1.67.2.3       tls 				  starttime = transtime(year, &start, stdoffset),
   1073  1.67.2.3       tls 				  endtime = transtime(year, &end, dstoffset);
   1074  1.67.2.3       tls 				int_fast32_t
   1075  1.67.2.3       tls 				  yearsecs = (year_lengths[isleap(year)]
   1076  1.67.2.3       tls 					      * SECSPERDAY);
   1077  1.67.2.3       tls 				int reversed = endtime < starttime;
   1078  1.67.2.3       tls 				if (reversed) {
   1079  1.67.2.3       tls 					int_fast32_t swap = starttime;
   1080  1.67.2.3       tls 					starttime = endtime;
   1081  1.67.2.3       tls 					endtime = swap;
   1082  1.67.2.3       tls 				}
   1083  1.67.2.3       tls 				if (reversed
   1084  1.67.2.3       tls 				    || (starttime < endtime
   1085  1.67.2.3       tls 					&& (endtime - starttime
   1086  1.67.2.3       tls 					    < (yearsecs
   1087  1.67.2.3       tls 					       + (stdoffset - dstoffset))))) {
   1088  1.67.2.3       tls 					if (TZ_MAX_TIMES - 2 < timecnt)
   1089  1.67.2.3       tls 						break;
   1090  1.67.2.3       tls 					yearlim = year + YEARSPERREPEAT + 1;
   1091  1.67.2.3       tls 					sp->ats[timecnt] = janfirst;
   1092  1.67.2.3       tls 					if (increment_overflow_time
   1093  1.67.2.3       tls 					    (&sp->ats[timecnt], starttime))
   1094  1.67.2.3       tls 						break;
   1095  1.67.2.3       tls 					sp->types[timecnt++] = reversed;
   1096  1.67.2.3       tls 					sp->ats[timecnt] = janfirst;
   1097  1.67.2.3       tls 					if (increment_overflow_time
   1098  1.67.2.3       tls 					    (&sp->ats[timecnt], endtime))
   1099  1.67.2.3       tls 						break;
   1100  1.67.2.3       tls 					sp->types[timecnt++] = !reversed;
   1101       1.1       jtc 				}
   1102  1.67.2.3       tls 				if (increment_overflow_time(&janfirst, yearsecs))
   1103      1.45   mlelstv 					break;
   1104       1.1       jtc 			}
   1105  1.67.2.3       tls 			sp->timecnt = timecnt;
   1106  1.67.2.3       tls 			if (!timecnt)
   1107  1.67.2.3       tls 				sp->typecnt = 1;	/* Perpetual DST.  */
   1108       1.1       jtc 		} else {
   1109  1.67.2.3       tls 			int_fast32_t	theirstdoffset;
   1110  1.67.2.3       tls 			int_fast32_t	theirdstoffset;
   1111  1.67.2.3       tls 			int_fast32_t	theiroffset;
   1112  1.67.2.3       tls 			int		isdst;
   1113  1.67.2.3       tls 			int		i;
   1114  1.67.2.3       tls 			int		j;
   1115       1.1       jtc 
   1116       1.1       jtc 			if (*name != '\0')
   1117       1.1       jtc 				return -1;
   1118       1.1       jtc 			/*
   1119  1.67.2.1       tls 			** Initial values of theirstdoffset and theirdstoffset.
   1120       1.1       jtc 			*/
   1121       1.1       jtc 			theirstdoffset = 0;
   1122       1.1       jtc 			for (i = 0; i < sp->timecnt; ++i) {
   1123       1.1       jtc 				j = sp->types[i];
   1124       1.1       jtc 				if (!sp->ttis[j].tt_isdst) {
   1125       1.5       jtc 					theirstdoffset =
   1126       1.5       jtc 						-sp->ttis[j].tt_gmtoff;
   1127       1.1       jtc 					break;
   1128       1.1       jtc 				}
   1129       1.1       jtc 			}
   1130      1.45   mlelstv 			theirdstoffset = 0;
   1131      1.45   mlelstv 			for (i = 0; i < sp->timecnt; ++i) {
   1132      1.45   mlelstv 				j = sp->types[i];
   1133      1.45   mlelstv 				if (sp->ttis[j].tt_isdst) {
   1134      1.45   mlelstv 					theirdstoffset =
   1135      1.45   mlelstv 						-sp->ttis[j].tt_gmtoff;
   1136      1.45   mlelstv 					break;
   1137      1.45   mlelstv 				}
   1138      1.45   mlelstv 			}
   1139       1.1       jtc 			/*
   1140       1.1       jtc 			** Initially we're assumed to be in standard time.
   1141       1.1       jtc 			*/
   1142      1.45   mlelstv 			isdst = FALSE;
   1143       1.1       jtc 			theiroffset = theirstdoffset;
   1144       1.1       jtc 			/*
   1145       1.1       jtc 			** Now juggle transition times and types
   1146       1.1       jtc 			** tracking offsets as you do.
   1147       1.1       jtc 			*/
   1148       1.1       jtc 			for (i = 0; i < sp->timecnt; ++i) {
   1149       1.1       jtc 				j = sp->types[i];
   1150       1.1       jtc 				sp->types[i] = sp->ttis[j].tt_isdst;
   1151       1.1       jtc 				if (sp->ttis[j].tt_ttisgmt) {
   1152       1.1       jtc 					/* No adjustment to transition time */
   1153       1.1       jtc 				} else {
   1154       1.1       jtc 					/*
   1155       1.1       jtc 					** If summer time is in effect, and the
   1156       1.1       jtc 					** transition time was not specified as
   1157       1.1       jtc 					** standard time, add the summer time
   1158       1.1       jtc 					** offset to the transition time;
   1159       1.1       jtc 					** otherwise, add the standard time
   1160       1.1       jtc 					** offset to the transition time.
   1161       1.1       jtc 					*/
   1162       1.1       jtc 					/*
   1163       1.1       jtc 					** Transitions from DST to DDST
   1164       1.1       jtc 					** will effectively disappear since
   1165       1.1       jtc 					** POSIX provides for only one DST
   1166       1.1       jtc 					** offset.
   1167       1.1       jtc 					*/
   1168      1.45   mlelstv 					if (isdst && !sp->ttis[j].tt_ttisstd) {
   1169      1.66  christos 						sp->ats[i] += (time_t)
   1170      1.66  christos 						    (dstoffset - theirdstoffset);
   1171      1.45   mlelstv 					} else {
   1172      1.66  christos 						sp->ats[i] += (time_t)
   1173      1.66  christos 						    (stdoffset - theirstdoffset);
   1174      1.45   mlelstv 					}
   1175       1.1       jtc 				}
   1176       1.1       jtc 				theiroffset = -sp->ttis[j].tt_gmtoff;
   1177      1.39  christos 				if (!sp->ttis[j].tt_isdst)
   1178      1.39  christos 					theirstdoffset = theiroffset;
   1179      1.45   mlelstv 				else	theirdstoffset = theiroffset;
   1180       1.1       jtc 			}
   1181       1.1       jtc 			/*
   1182       1.1       jtc 			** Finally, fill in ttis.
   1183  1.67.2.1       tls 			** ttisstd and ttisgmt need not be handled
   1184       1.1       jtc 			*/
   1185      1.58  christos 			memset(sp->ttis, 0, sizeof(sp->ttis));
   1186       1.1       jtc 			sp->ttis[0].tt_gmtoff = -stdoffset;
   1187       1.1       jtc 			sp->ttis[0].tt_isdst = FALSE;
   1188       1.1       jtc 			sp->ttis[0].tt_abbrind = 0;
   1189       1.1       jtc 			sp->ttis[1].tt_gmtoff = -dstoffset;
   1190       1.1       jtc 			sp->ttis[1].tt_isdst = TRUE;
   1191      1.66  christos 			sp->ttis[1].tt_abbrind = (int)(stdlen + 1);
   1192       1.7       jtc 			sp->typecnt = 2;
   1193  1.67.2.3       tls 			sp->defaulttype = 0;
   1194       1.1       jtc 		}
   1195       1.1       jtc 	} else {
   1196       1.1       jtc 		dstlen = 0;
   1197       1.1       jtc 		sp->typecnt = 1;		/* only standard time */
   1198       1.1       jtc 		sp->timecnt = 0;
   1199      1.58  christos 		memset(sp->ttis, 0, sizeof(sp->ttis));
   1200       1.1       jtc 		sp->ttis[0].tt_gmtoff = -stdoffset;
   1201       1.1       jtc 		sp->ttis[0].tt_isdst = 0;
   1202       1.1       jtc 		sp->ttis[0].tt_abbrind = 0;
   1203  1.67.2.3       tls 		sp->defaulttype = 0;
   1204       1.1       jtc 	}
   1205      1.66  christos 	sp->charcnt = (int)(stdlen + 1);
   1206       1.1       jtc 	if (dstlen != 0)
   1207      1.66  christos 		sp->charcnt += (int)(dstlen + 1);
   1208      1.10       jtc 	if ((size_t) sp->charcnt > sizeof sp->chars)
   1209       1.1       jtc 		return -1;
   1210       1.1       jtc 	cp = sp->chars;
   1211       1.1       jtc 	(void) strncpy(cp, stdname, stdlen);
   1212       1.1       jtc 	cp += stdlen;
   1213       1.1       jtc 	*cp++ = '\0';
   1214       1.1       jtc 	if (dstlen != 0) {
   1215       1.1       jtc 		(void) strncpy(cp, dstname, dstlen);
   1216       1.1       jtc 		*(cp + dstlen) = '\0';
   1217       1.1       jtc 	}
   1218       1.1       jtc 	return 0;
   1219       1.1       jtc }
   1220       1.1       jtc 
   1221       1.1       jtc static void
   1222      1.49  christos gmtload(timezone_t sp)
   1223      1.49  christos {
   1224      1.49  christos 	if (tzload(sp, gmt, TRUE) != 0)
   1225      1.49  christos 		(void) tzparse(sp, gmt, TRUE);
   1226      1.49  christos }
   1227      1.49  christos 
   1228      1.49  christos timezone_t
   1229      1.49  christos tzalloc(const char *name)
   1230      1.49  christos {
   1231  1.67.2.3       tls 	timezone_t sp = malloc(sizeof *sp);
   1232      1.49  christos 	if (sp == NULL)
   1233      1.49  christos 		return NULL;
   1234      1.49  christos 	if (tzload(sp, name, TRUE) != 0) {
   1235      1.49  christos 		free(sp);
   1236      1.49  christos 		return NULL;
   1237      1.49  christos 	}
   1238      1.49  christos 	settzname_z(sp);
   1239      1.49  christos 	return sp;
   1240      1.49  christos }
   1241      1.49  christos 
   1242      1.49  christos void
   1243      1.49  christos tzfree(const timezone_t sp)
   1244       1.1       jtc {
   1245      1.49  christos 	free(sp);
   1246       1.1       jtc }
   1247       1.1       jtc 
   1248      1.19    kleink static void
   1249      1.45   mlelstv tzsetwall_unlocked(void)
   1250       1.1       jtc {
   1251      1.45   mlelstv 	if (lcl_is_set < 0)
   1252       1.1       jtc 		return;
   1253      1.45   mlelstv 	lcl_is_set = -1;
   1254       1.1       jtc 
   1255       1.1       jtc 	if (lclptr == NULL) {
   1256      1.41  christos 		int saveerrno = errno;
   1257  1.67.2.3       tls 		lclptr = malloc(sizeof *lclptr);
   1258      1.41  christos 		errno = saveerrno;
   1259       1.1       jtc 		if (lclptr == NULL) {
   1260      1.45   mlelstv 			settzname();	/* all we can do */
   1261       1.1       jtc 			return;
   1262       1.1       jtc 		}
   1263       1.1       jtc 	}
   1264      1.49  christos 	if (tzload(lclptr, NULL, TRUE) != 0)
   1265      1.45   mlelstv 		gmtload(lclptr);
   1266      1.45   mlelstv 	settzname();
   1267       1.1       jtc }
   1268       1.1       jtc 
   1269      1.19    kleink #ifndef STD_INSPIRED
   1270      1.19    kleink /*
   1271      1.19    kleink ** A non-static declaration of tzsetwall in a system header file
   1272      1.19    kleink ** may cause a warning about this upcoming static declaration...
   1273      1.19    kleink */
   1274      1.19    kleink static
   1275      1.19    kleink #endif /* !defined STD_INSPIRED */
   1276       1.1       jtc void
   1277      1.45   mlelstv tzsetwall(void)
   1278      1.19    kleink {
   1279      1.45   mlelstv 	rwlock_wrlock(&lcl_lock);
   1280      1.45   mlelstv 	tzsetwall_unlocked();
   1281      1.45   mlelstv 	rwlock_unlock(&lcl_lock);
   1282      1.19    kleink }
   1283      1.19    kleink 
   1284      1.45   mlelstv #ifndef STD_INSPIRED
   1285      1.45   mlelstv /*
   1286      1.45   mlelstv ** A non-static declaration of tzsetwall in a system header file
   1287      1.45   mlelstv ** may cause a warning about this upcoming static declaration...
   1288      1.45   mlelstv */
   1289      1.45   mlelstv static
   1290      1.45   mlelstv #endif /* !defined STD_INSPIRED */
   1291      1.45   mlelstv void
   1292      1.45   mlelstv tzset_unlocked(void)
   1293       1.1       jtc {
   1294      1.49  christos 	const char *	name;
   1295       1.1       jtc 
   1296       1.1       jtc 	name = getenv("TZ");
   1297       1.1       jtc 	if (name == NULL) {
   1298      1.45   mlelstv 		tzsetwall_unlocked();
   1299       1.1       jtc 		return;
   1300       1.1       jtc 	}
   1301       1.1       jtc 
   1302      1.45   mlelstv 	if (lcl_is_set > 0 && strcmp(lcl_TZname, name) == 0)
   1303       1.1       jtc 		return;
   1304      1.45   mlelstv 	lcl_is_set = strlen(name) < sizeof lcl_TZname;
   1305      1.45   mlelstv 	if (lcl_is_set)
   1306      1.45   mlelstv 		(void)strlcpy(lcl_TZname, name, sizeof(lcl_TZname));
   1307       1.1       jtc 
   1308       1.1       jtc 	if (lclptr == NULL) {
   1309  1.67.2.1       tls 		int saveerrno = errno;
   1310  1.67.2.3       tls 		lclptr = malloc(sizeof *lclptr);
   1311      1.41  christos 		errno = saveerrno;
   1312       1.1       jtc 		if (lclptr == NULL) {
   1313      1.45   mlelstv 			settzname();	/* all we can do */
   1314       1.1       jtc 			return;
   1315       1.1       jtc 		}
   1316       1.1       jtc 	}
   1317       1.1       jtc 	if (*name == '\0') {
   1318       1.1       jtc 		/*
   1319       1.1       jtc 		** User wants it fast rather than right.
   1320       1.1       jtc 		*/
   1321       1.1       jtc 		lclptr->leapcnt = 0;		/* so, we're off a little */
   1322       1.1       jtc 		lclptr->timecnt = 0;
   1323      1.27    atatat 		lclptr->typecnt = 0;
   1324      1.27    atatat 		lclptr->ttis[0].tt_isdst = 0;
   1325       1.1       jtc 		lclptr->ttis[0].tt_gmtoff = 0;
   1326       1.1       jtc 		lclptr->ttis[0].tt_abbrind = 0;
   1327      1.45   mlelstv 		(void) strlcpy(lclptr->chars, gmt, sizeof(lclptr->chars));
   1328      1.49  christos 	} else if (tzload(lclptr, name, TRUE) != 0)
   1329      1.49  christos 		if (name[0] == ':' || tzparse(lclptr, name, FALSE) != 0)
   1330      1.45   mlelstv 			(void) gmtload(lclptr);
   1331      1.45   mlelstv 	settzname();
   1332       1.1       jtc }
   1333       1.1       jtc 
   1334      1.19    kleink void
   1335      1.45   mlelstv tzset(void)
   1336      1.19    kleink {
   1337      1.45   mlelstv 	rwlock_wrlock(&lcl_lock);
   1338      1.45   mlelstv 	tzset_unlocked();
   1339      1.45   mlelstv 	rwlock_unlock(&lcl_lock);
   1340      1.19    kleink }
   1341      1.19    kleink 
   1342       1.1       jtc /*
   1343       1.1       jtc ** The easy way to behave "as if no library function calls" localtime
   1344       1.1       jtc ** is to not call it--so we drop its guts into "localsub", which can be
   1345      1.45   mlelstv ** freely called. (And no, the PANS doesn't require the above behavior--
   1346       1.1       jtc ** but it *is* desirable.)
   1347       1.1       jtc **
   1348       1.1       jtc ** The unused offset argument is for the benefit of mktime variants.
   1349       1.1       jtc */
   1350       1.1       jtc 
   1351       1.1       jtc /*ARGSUSED*/
   1352      1.45   mlelstv static struct tm *
   1353  1.67.2.3       tls localsub(const timezone_t sp, const time_t * const timep, const int_fast32_t offset,
   1354      1.49  christos     struct tm *const tmp)
   1355      1.49  christos {
   1356      1.49  christos 	const struct ttinfo *	ttisp;
   1357      1.49  christos 	int			i;
   1358      1.49  christos 	struct tm *		result;
   1359       1.1       jtc 	const time_t			t = *timep;
   1360       1.1       jtc 
   1361      1.45   mlelstv 	if ((sp->goback && t < sp->ats[0]) ||
   1362      1.45   mlelstv 		(sp->goahead && t > sp->ats[sp->timecnt - 1])) {
   1363      1.45   mlelstv 			time_t			newt = t;
   1364      1.49  christos 			time_t		seconds;
   1365  1.67.2.3       tls 			time_t		years;
   1366      1.45   mlelstv 
   1367      1.45   mlelstv 			if (t < sp->ats[0])
   1368      1.45   mlelstv 				seconds = sp->ats[0] - t;
   1369      1.45   mlelstv 			else	seconds = t - sp->ats[sp->timecnt - 1];
   1370      1.45   mlelstv 			--seconds;
   1371  1.67.2.3       tls 			years = (time_t)((seconds / SECSPERREPEAT + 1) * YEARSPERREPEAT);
   1372  1.67.2.3       tls 			seconds = (time_t)(years * AVGSECSPERYEAR);
   1373      1.45   mlelstv 			if (t < sp->ats[0])
   1374      1.45   mlelstv 				newt += seconds;
   1375      1.45   mlelstv 			else	newt -= seconds;
   1376      1.45   mlelstv 			if (newt < sp->ats[0] ||
   1377      1.51  christos 				newt > sp->ats[sp->timecnt - 1])
   1378      1.45   mlelstv 					return NULL;	/* "cannot happen" */
   1379      1.49  christos 			result = localsub(sp, &newt, offset, tmp);
   1380      1.45   mlelstv 			if (result == tmp) {
   1381      1.49  christos 				time_t	newy;
   1382      1.45   mlelstv 
   1383      1.45   mlelstv 				newy = tmp->tm_year;
   1384      1.45   mlelstv 				if (t < sp->ats[0])
   1385  1.67.2.3       tls 					newy -= years;
   1386  1.67.2.3       tls 				else	newy += years;
   1387      1.45   mlelstv 				tmp->tm_year = (int)newy;
   1388      1.51  christos 				if (tmp->tm_year != newy)
   1389      1.45   mlelstv 					return NULL;
   1390      1.45   mlelstv 			}
   1391      1.45   mlelstv 			return result;
   1392       1.1       jtc 	}
   1393       1.1       jtc 	if (sp->timecnt == 0 || t < sp->ats[0]) {
   1394  1.67.2.3       tls 		i = sp->defaulttype;
   1395       1.1       jtc 	} else {
   1396      1.49  christos 		int	lo = 1;
   1397      1.49  christos 		int	hi = sp->timecnt;
   1398      1.45   mlelstv 
   1399      1.45   mlelstv 		while (lo < hi) {
   1400      1.49  christos 			int	mid = (lo + hi) / 2;
   1401      1.45   mlelstv 
   1402      1.45   mlelstv 			if (t < sp->ats[mid])
   1403      1.45   mlelstv 				hi = mid;
   1404      1.45   mlelstv 			else	lo = mid + 1;
   1405      1.45   mlelstv 		}
   1406      1.45   mlelstv 		i = (int) sp->types[lo - 1];
   1407       1.1       jtc 	}
   1408       1.1       jtc 	ttisp = &sp->ttis[i];
   1409       1.1       jtc 	/*
   1410       1.1       jtc 	** To get (wrong) behavior that's compatible with System V Release 2.0
   1411       1.1       jtc 	** you'd replace the statement below with
   1412       1.1       jtc 	**	t += ttisp->tt_gmtoff;
   1413       1.1       jtc 	**	timesub(&t, 0L, sp, tmp);
   1414       1.1       jtc 	*/
   1415      1.49  christos 	result = timesub(sp, &t, ttisp->tt_gmtoff, tmp);
   1416       1.1       jtc 	tmp->tm_isdst = ttisp->tt_isdst;
   1417      1.57  christos 	if (sp == lclptr)
   1418      1.57  christos 		tzname[tmp->tm_isdst] = &sp->chars[ttisp->tt_abbrind];
   1419       1.1       jtc #ifdef TM_ZONE
   1420       1.1       jtc 	tmp->TM_ZONE = &sp->chars[ttisp->tt_abbrind];
   1421       1.1       jtc #endif /* defined TM_ZONE */
   1422      1.45   mlelstv 	return result;
   1423       1.1       jtc }
   1424       1.1       jtc 
   1425      1.49  christos /*
   1426      1.49  christos ** Re-entrant version of localtime.
   1427      1.49  christos */
   1428      1.49  christos 
   1429       1.1       jtc struct tm *
   1430      1.49  christos localtime_r(const time_t * __restrict timep, struct tm *tmp)
   1431       1.1       jtc {
   1432      1.49  christos 	rwlock_rdlock(&lcl_lock);
   1433      1.45   mlelstv 	tzset_unlocked();
   1434      1.49  christos 	tmp = localtime_rz(lclptr, timep, tmp);
   1435      1.45   mlelstv 	rwlock_unlock(&lcl_lock);
   1436      1.49  christos 	return tmp;
   1437       1.1       jtc }
   1438       1.1       jtc 
   1439      1.49  christos struct tm *
   1440      1.49  christos localtime(const time_t *const timep)
   1441      1.49  christos {
   1442      1.49  christos 	return localtime_r(timep, &tm);
   1443      1.49  christos }
   1444      1.35    kleink 
   1445      1.18    kleink struct tm *
   1446      1.49  christos localtime_rz(const timezone_t sp, const time_t * __restrict timep, struct tm *tmp)
   1447      1.18    kleink {
   1448      1.49  christos 	if (sp == NULL)
   1449  1.67.2.3       tls 		tmp = gmtsub(NULL, timep, 0, tmp);
   1450      1.49  christos 	else
   1451  1.67.2.3       tls 		tmp = localsub(sp, timep, 0, tmp);
   1452      1.51  christos 	if (tmp == NULL)
   1453      1.51  christos 		errno = EOVERFLOW;
   1454      1.51  christos 	return tmp;
   1455      1.18    kleink }
   1456      1.18    kleink 
   1457      1.18    kleink /*
   1458       1.1       jtc ** gmtsub is to gmtime as localsub is to localtime.
   1459       1.1       jtc */
   1460       1.1       jtc 
   1461      1.45   mlelstv static struct tm *
   1462  1.67.2.3       tls gmtsub(const timezone_t sp, const time_t *const timep,
   1463  1.67.2.3       tls     const int_fast32_t offset, struct tm *const tmp)
   1464       1.1       jtc {
   1465      1.49  christos 	struct tm *	result;
   1466      1.33  christos #ifdef _REENTRANT
   1467      1.19    kleink 	static mutex_t gmt_mutex = MUTEX_INITIALIZER;
   1468      1.19    kleink #endif
   1469      1.19    kleink 
   1470      1.19    kleink 	mutex_lock(&gmt_mutex);
   1471      1.45   mlelstv 	if (!gmt_is_set) {
   1472      1.41  christos 		int saveerrno;
   1473      1.45   mlelstv 		gmt_is_set = TRUE;
   1474      1.41  christos 		saveerrno = errno;
   1475  1.67.2.3       tls 		gmtptr = malloc(sizeof *gmtptr);
   1476      1.41  christos 		errno = saveerrno;
   1477       1.1       jtc 		if (gmtptr != NULL)
   1478      1.45   mlelstv 			gmtload(gmtptr);
   1479       1.1       jtc 	}
   1480      1.19    kleink 	mutex_unlock(&gmt_mutex);
   1481      1.49  christos 	result = timesub(gmtptr, timep, offset, tmp);
   1482       1.1       jtc #ifdef TM_ZONE
   1483       1.1       jtc 	/*
   1484       1.1       jtc 	** Could get fancy here and deliver something such as
   1485  1.67.2.3       tls 	** "UT+xxxx" or "UT-xxxx" if offset is non-zero,
   1486       1.1       jtc 	** but this is no time for a treasure hunt.
   1487       1.1       jtc 	*/
   1488  1.67.2.3       tls 	tmp->TM_ZONE = offset ? __UNCONST(wildabbr) : gmtptr ? gmtptr->chars :
   1489  1.67.2.3       tls 	    __UNCONST(gmt);
   1490       1.1       jtc #endif /* defined TM_ZONE */
   1491      1.45   mlelstv 	return result;
   1492       1.1       jtc }
   1493       1.1       jtc 
   1494       1.1       jtc struct tm *
   1495      1.49  christos gmtime(const time_t *const timep)
   1496       1.1       jtc {
   1497  1.67.2.3       tls 	struct tm *tmp = gmtsub(NULL, timep, 0, &tm);
   1498      1.55  christos 
   1499      1.55  christos 	if (tmp == NULL)
   1500      1.55  christos 		errno = EOVERFLOW;
   1501      1.55  christos 
   1502      1.55  christos 	return tmp;
   1503       1.1       jtc }
   1504       1.1       jtc 
   1505      1.18    kleink /*
   1506      1.35    kleink ** Re-entrant version of gmtime.
   1507      1.35    kleink */
   1508      1.35    kleink 
   1509      1.18    kleink struct tm *
   1510      1.49  christos gmtime_r(const time_t * const timep, struct tm *tmp)
   1511      1.18    kleink {
   1512  1.67.2.3       tls 	tmp = gmtsub(NULL, timep, 0, tmp);
   1513      1.55  christos 
   1514      1.55  christos 	if (tmp == NULL)
   1515      1.55  christos 		errno = EOVERFLOW;
   1516      1.55  christos 
   1517      1.55  christos 	return tmp;
   1518      1.18    kleink }
   1519      1.18    kleink 
   1520       1.1       jtc #ifdef STD_INSPIRED
   1521       1.1       jtc 
   1522       1.1       jtc struct tm *
   1523      1.49  christos offtime(const time_t *const timep, long offset)
   1524       1.1       jtc {
   1525  1.67.2.3       tls 	struct tm *tmp;
   1526  1.67.2.3       tls 
   1527  1.67.2.3       tls 	if ((offset > 0 && offset > INT_FAST32_MAX) ||
   1528  1.67.2.3       tls 	    (offset < 0 && offset < INT_FAST32_MIN)) {
   1529  1.67.2.3       tls 		errno = EOVERFLOW;
   1530  1.67.2.3       tls 		return NULL;
   1531  1.67.2.3       tls 	}
   1532  1.67.2.3       tls 	tmp = gmtsub(NULL, timep, (int_fast32_t)offset, &tm);
   1533      1.55  christos 
   1534      1.55  christos 	if (tmp == NULL)
   1535      1.55  christos 		errno = EOVERFLOW;
   1536      1.55  christos 
   1537      1.55  christos 	return tmp;
   1538      1.49  christos }
   1539      1.49  christos 
   1540      1.49  christos struct tm *
   1541      1.49  christos offtime_r(const time_t *timep, long offset, struct tm *tmp)
   1542      1.49  christos {
   1543  1.67.2.3       tls 	if ((offset > 0 && offset > INT_FAST32_MAX) ||
   1544  1.67.2.3       tls 	    (offset < 0 && offset < INT_FAST32_MIN)) {
   1545  1.67.2.3       tls 		errno = EOVERFLOW;
   1546  1.67.2.3       tls 		return NULL;
   1547  1.67.2.3       tls 	}
   1548  1.67.2.3       tls 	tmp = gmtsub(NULL, timep, (int_fast32_t)offset, tmp);
   1549      1.55  christos 
   1550      1.55  christos 	if (tmp == NULL)
   1551      1.55  christos 		errno = EOVERFLOW;
   1552      1.55  christos 
   1553      1.55  christos 	return tmp;
   1554       1.1       jtc }
   1555       1.1       jtc 
   1556       1.1       jtc #endif /* defined STD_INSPIRED */
   1557       1.1       jtc 
   1558      1.45   mlelstv /*
   1559      1.45   mlelstv ** Return the number of leap years through the end of the given year
   1560      1.45   mlelstv ** where, to make the math easy, the answer for year zero is defined as zero.
   1561      1.45   mlelstv */
   1562      1.45   mlelstv 
   1563      1.45   mlelstv static int
   1564      1.49  christos leaps_thru_end_of(const int y)
   1565      1.45   mlelstv {
   1566      1.45   mlelstv 	return (y >= 0) ? (y / 4 - y / 100 + y / 400) :
   1567      1.45   mlelstv 		-(leaps_thru_end_of(-(y + 1)) + 1);
   1568      1.45   mlelstv }
   1569      1.45   mlelstv 
   1570      1.45   mlelstv static struct tm *
   1571  1.67.2.3       tls timesub(const timezone_t sp, const time_t *const timep,
   1572  1.67.2.3       tls     const int_fast32_t offset, struct tm *const tmp)
   1573      1.49  christos {
   1574      1.49  christos 	const struct lsinfo *	lp;
   1575      1.49  christos 	time_t			tdays;
   1576      1.49  christos 	int			idays;	/* unsigned would be so 2003 */
   1577  1.67.2.3       tls 	int_fast64_t		rem;
   1578      1.49  christos 	int			y;
   1579      1.49  christos 	const int *		ip;
   1580  1.67.2.3       tls 	int_fast64_t		corr;
   1581      1.49  christos 	int			hit;
   1582      1.49  christos 	int			i;
   1583       1.1       jtc 
   1584       1.1       jtc 	corr = 0;
   1585       1.1       jtc 	hit = 0;
   1586       1.1       jtc 	i = (sp == NULL) ? 0 : sp->leapcnt;
   1587       1.1       jtc 	while (--i >= 0) {
   1588       1.1       jtc 		lp = &sp->lsis[i];
   1589       1.1       jtc 		if (*timep >= lp->ls_trans) {
   1590       1.1       jtc 			if (*timep == lp->ls_trans) {
   1591       1.1       jtc 				hit = ((i == 0 && lp->ls_corr > 0) ||
   1592       1.1       jtc 					lp->ls_corr > sp->lsis[i - 1].ls_corr);
   1593       1.1       jtc 				if (hit)
   1594       1.1       jtc 					while (i > 0 &&
   1595       1.1       jtc 						sp->lsis[i].ls_trans ==
   1596       1.1       jtc 						sp->lsis[i - 1].ls_trans + 1 &&
   1597       1.1       jtc 						sp->lsis[i].ls_corr ==
   1598       1.1       jtc 						sp->lsis[i - 1].ls_corr + 1) {
   1599       1.1       jtc 							++hit;
   1600       1.1       jtc 							--i;
   1601       1.1       jtc 					}
   1602       1.1       jtc 			}
   1603       1.1       jtc 			corr = lp->ls_corr;
   1604       1.1       jtc 			break;
   1605       1.1       jtc 		}
   1606       1.1       jtc 	}
   1607      1.45   mlelstv 	y = EPOCH_YEAR;
   1608      1.66  christos 	tdays = (time_t)(*timep / SECSPERDAY);
   1609  1.67.2.3       tls 	rem = (int_fast64_t) (*timep - tdays * SECSPERDAY);
   1610      1.45   mlelstv 	while (tdays < 0 || tdays >= year_lengths[isleap(y)]) {
   1611      1.45   mlelstv 		int		newy;
   1612      1.49  christos 		time_t	tdelta;
   1613      1.49  christos 		int	idelta;
   1614      1.49  christos 		int	leapdays;
   1615      1.45   mlelstv 
   1616      1.45   mlelstv 		tdelta = tdays / DAYSPERLYEAR;
   1617  1.67.2.3       tls 		if (! ((! TYPE_SIGNED(time_t) || INT_MIN <= tdelta)
   1618  1.67.2.3       tls 		       && tdelta <= INT_MAX))
   1619      1.45   mlelstv 			return NULL;
   1620  1.67.2.3       tls 		_DIAGASSERT(__type_fit(int, tdelta));
   1621  1.67.2.3       tls 		idelta = (int)tdelta;
   1622      1.45   mlelstv 		if (idelta == 0)
   1623      1.45   mlelstv 			idelta = (tdays < 0) ? -1 : 1;
   1624      1.45   mlelstv 		newy = y;
   1625      1.51  christos 		if (increment_overflow(&newy, idelta))
   1626      1.45   mlelstv 			return NULL;
   1627      1.45   mlelstv 		leapdays = leaps_thru_end_of(newy - 1) -
   1628      1.45   mlelstv 			leaps_thru_end_of(y - 1);
   1629      1.45   mlelstv 		tdays -= ((time_t) newy - y) * DAYSPERNYEAR;
   1630      1.45   mlelstv 		tdays -= leapdays;
   1631      1.45   mlelstv 		y = newy;
   1632      1.45   mlelstv 	}
   1633      1.45   mlelstv 	{
   1634  1.67.2.3       tls 		int_fast32_t seconds;
   1635      1.45   mlelstv 
   1636  1.67.2.3       tls 		seconds = (int_fast32_t)(tdays * SECSPERDAY);
   1637      1.66  christos 		tdays = (time_t)(seconds / SECSPERDAY);
   1638  1.67.2.3       tls 		rem += (int_fast64_t)(seconds - tdays * SECSPERDAY);
   1639       1.1       jtc 	}
   1640      1.45   mlelstv 	/*
   1641      1.45   mlelstv 	** Given the range, we can now fearlessly cast...
   1642      1.45   mlelstv 	*/
   1643      1.45   mlelstv 	idays = (int) tdays;
   1644      1.45   mlelstv 	rem += offset - corr;
   1645       1.1       jtc 	while (rem < 0) {
   1646       1.1       jtc 		rem += SECSPERDAY;
   1647      1.45   mlelstv 		--idays;
   1648       1.1       jtc 	}
   1649       1.1       jtc 	while (rem >= SECSPERDAY) {
   1650       1.1       jtc 		rem -= SECSPERDAY;
   1651      1.45   mlelstv 		++idays;
   1652      1.45   mlelstv 	}
   1653      1.45   mlelstv 	while (idays < 0) {
   1654      1.51  christos 		if (increment_overflow(&y, -1))
   1655      1.45   mlelstv 			return NULL;
   1656      1.45   mlelstv 		idays += year_lengths[isleap(y)];
   1657       1.1       jtc 	}
   1658      1.45   mlelstv 	while (idays >= year_lengths[isleap(y)]) {
   1659      1.45   mlelstv 		idays -= year_lengths[isleap(y)];
   1660      1.51  christos 		if (increment_overflow(&y, 1))
   1661      1.45   mlelstv 			return NULL;
   1662      1.45   mlelstv 	}
   1663      1.45   mlelstv 	tmp->tm_year = y;
   1664      1.51  christos 	if (increment_overflow(&tmp->tm_year, -TM_YEAR_BASE))
   1665      1.45   mlelstv 		return NULL;
   1666      1.45   mlelstv 	tmp->tm_yday = idays;
   1667      1.45   mlelstv 	/*
   1668      1.45   mlelstv 	** The "extra" mods below avoid overflow problems.
   1669      1.45   mlelstv 	*/
   1670      1.45   mlelstv 	tmp->tm_wday = EPOCH_WDAY +
   1671      1.45   mlelstv 		((y - EPOCH_YEAR) % DAYSPERWEEK) *
   1672      1.45   mlelstv 		(DAYSPERNYEAR % DAYSPERWEEK) +
   1673      1.45   mlelstv 		leaps_thru_end_of(y - 1) -
   1674      1.45   mlelstv 		leaps_thru_end_of(EPOCH_YEAR - 1) +
   1675      1.45   mlelstv 		idays;
   1676      1.45   mlelstv 	tmp->tm_wday %= DAYSPERWEEK;
   1677      1.45   mlelstv 	if (tmp->tm_wday < 0)
   1678      1.45   mlelstv 		tmp->tm_wday += DAYSPERWEEK;
   1679       1.1       jtc 	tmp->tm_hour = (int) (rem / SECSPERHOUR);
   1680      1.45   mlelstv 	rem %= SECSPERHOUR;
   1681       1.1       jtc 	tmp->tm_min = (int) (rem / SECSPERMIN);
   1682       1.6       jtc 	/*
   1683       1.6       jtc 	** A positive leap second requires a special
   1684      1.45   mlelstv 	** representation. This uses "... ??:59:60" et seq.
   1685       1.6       jtc 	*/
   1686       1.6       jtc 	tmp->tm_sec = (int) (rem % SECSPERMIN) + hit;
   1687      1.45   mlelstv 	ip = mon_lengths[isleap(y)];
   1688      1.45   mlelstv 	for (tmp->tm_mon = 0; idays >= ip[tmp->tm_mon]; ++(tmp->tm_mon))
   1689      1.45   mlelstv 		idays -= ip[tmp->tm_mon];
   1690      1.45   mlelstv 	tmp->tm_mday = (int) (idays + 1);
   1691       1.1       jtc 	tmp->tm_isdst = 0;
   1692       1.1       jtc #ifdef TM_GMTOFF
   1693       1.1       jtc 	tmp->TM_GMTOFF = offset;
   1694       1.1       jtc #endif /* defined TM_GMTOFF */
   1695      1.45   mlelstv 	return tmp;
   1696       1.1       jtc }
   1697       1.1       jtc 
   1698       1.1       jtc char *
   1699      1.49  christos ctime(const time_t *const timep)
   1700       1.1       jtc {
   1701       1.1       jtc /*
   1702       1.1       jtc ** Section 4.12.3.2 of X3.159-1989 requires that
   1703      1.18    kleink **	The ctime function converts the calendar time pointed to by timer
   1704      1.45   mlelstv **	to local time in the form of a string. It is equivalent to
   1705       1.1       jtc **		asctime(localtime(timer))
   1706       1.1       jtc */
   1707      1.46  christos 	struct tm *rtm = localtime(timep);
   1708      1.46  christos 	if (rtm == NULL)
   1709      1.46  christos 		return NULL;
   1710      1.46  christos 	return asctime(rtm);
   1711      1.18    kleink }
   1712      1.18    kleink 
   1713      1.18    kleink char *
   1714      1.49  christos ctime_r(const time_t *const timep, char *buf)
   1715      1.18    kleink {
   1716      1.46  christos 	struct tm	mytm, *rtm;
   1717      1.18    kleink 
   1718      1.46  christos 	rtm = localtime_r(timep, &mytm);
   1719      1.46  christos 	if (rtm == NULL)
   1720      1.46  christos 		return NULL;
   1721      1.46  christos 	return asctime_r(rtm, buf);
   1722       1.1       jtc }
   1723       1.1       jtc 
   1724      1.49  christos char *
   1725      1.49  christos ctime_rz(const timezone_t sp, const time_t * timep, char *buf)
   1726      1.49  christos {
   1727      1.49  christos 	struct tm	mytm, *rtm;
   1728      1.49  christos 
   1729      1.49  christos 	rtm = localtime_rz(sp, timep, &mytm);
   1730      1.49  christos 	if (rtm == NULL)
   1731      1.49  christos 		return NULL;
   1732      1.49  christos 	return asctime_r(rtm, buf);
   1733      1.49  christos }
   1734      1.49  christos 
   1735       1.1       jtc /*
   1736       1.1       jtc ** Adapted from code provided by Robert Elz, who writes:
   1737       1.1       jtc **	The "best" way to do mktime I think is based on an idea of Bob
   1738       1.7       jtc **	Kridle's (so its said...) from a long time ago.
   1739      1.45   mlelstv **	It does a binary search of the time_t space. Since time_t's are
   1740       1.1       jtc **	just 32 bits, its a max of 32 iterations (even at 64 bits it
   1741       1.1       jtc **	would still be very reasonable).
   1742       1.1       jtc */
   1743       1.1       jtc 
   1744       1.1       jtc #ifndef WRONG
   1745      1.51  christos #define WRONG	((time_t)-1)
   1746       1.1       jtc #endif /* !defined WRONG */
   1747       1.1       jtc 
   1748       1.1       jtc /*
   1749      1.45   mlelstv ** Simplified normalize logic courtesy Paul Eggert.
   1750       1.1       jtc */
   1751       1.1       jtc 
   1752       1.1       jtc static int
   1753  1.67.2.1       tls increment_overflow(int *const ip, int j)
   1754       1.1       jtc {
   1755      1.58  christos 	int	i = *ip;
   1756       1.1       jtc 
   1757      1.58  christos 	/*
   1758      1.58  christos 	** If i >= 0 there can only be overflow if i + j > INT_MAX
   1759      1.58  christos 	** or if j > INT_MAX - i; given i >= 0, INT_MAX - i cannot overflow.
   1760      1.58  christos 	** If i < 0 there can only be overflow if i + j < INT_MIN
   1761      1.58  christos 	** or if j < INT_MIN - i; given i < 0, INT_MIN - i cannot overflow.
   1762      1.58  christos 	*/
   1763      1.58  christos 	if ((i >= 0) ? (j > INT_MAX - i) : (j < INT_MIN - i))
   1764      1.58  christos 		return TRUE;
   1765      1.58  christos 	*ip += j;
   1766      1.58  christos 	return FALSE;
   1767       1.1       jtc }
   1768       1.1       jtc 
   1769       1.1       jtc static int
   1770  1.67.2.3       tls increment_overflow32(int_fast32_t *const lp, int const m)
   1771      1.45   mlelstv {
   1772  1.67.2.3       tls 	int_fast32_t l = *lp;
   1773      1.45   mlelstv 
   1774  1.67.2.3       tls 	if ((l >= 0) ? (m > INT_FAST32_MAX - l) : (m < INT_FAST32_MIN - l))
   1775      1.58  christos 		return TRUE;
   1776      1.58  christos 	*lp += m;
   1777      1.58  christos 	return FALSE;
   1778      1.45   mlelstv }
   1779      1.45   mlelstv 
   1780      1.45   mlelstv static int
   1781  1.67.2.3       tls increment_overflow_time(time_t *tp, int_fast32_t j)
   1782  1.67.2.3       tls {
   1783  1.67.2.3       tls 	/*
   1784  1.67.2.3       tls 	** This is like
   1785  1.67.2.3       tls 	** 'if (! (time_t_min <= *tp + j && *tp + j <= time_t_max)) ...',
   1786  1.67.2.3       tls 	** except that it does the right thing even if *tp + j would overflow.
   1787  1.67.2.3       tls 	*/
   1788  1.67.2.3       tls 	if (! (j < 0
   1789  1.67.2.3       tls 	       ? (TYPE_SIGNED(time_t) ? time_t_min - j <= *tp : -1 - j < *tp)
   1790  1.67.2.3       tls 	       : *tp <= time_t_max - j))
   1791  1.67.2.3       tls 		return TRUE;
   1792  1.67.2.3       tls 	*tp += j;
   1793  1.67.2.3       tls 	return FALSE;
   1794  1.67.2.3       tls }
   1795  1.67.2.3       tls 
   1796  1.67.2.3       tls static int
   1797      1.49  christos normalize_overflow(int *const tensptr, int *const unitsptr, const int base)
   1798       1.1       jtc {
   1799      1.49  christos 	int	tensdelta;
   1800       1.1       jtc 
   1801       1.1       jtc 	tensdelta = (*unitsptr >= 0) ?
   1802       1.1       jtc 		(*unitsptr / base) :
   1803       1.1       jtc 		(-1 - (-1 - *unitsptr) / base);
   1804       1.1       jtc 	*unitsptr -= tensdelta * base;
   1805       1.1       jtc 	return increment_overflow(tensptr, tensdelta);
   1806       1.1       jtc }
   1807       1.1       jtc 
   1808       1.1       jtc static int
   1809  1.67.2.3       tls normalize_overflow32(int_fast32_t *const tensptr, int *const unitsptr,
   1810      1.49  christos     const int base)
   1811      1.45   mlelstv {
   1812      1.49  christos 	int	tensdelta;
   1813      1.45   mlelstv 
   1814      1.45   mlelstv 	tensdelta = (*unitsptr >= 0) ?
   1815      1.45   mlelstv 		(*unitsptr / base) :
   1816      1.45   mlelstv 		(-1 - (-1 - *unitsptr) / base);
   1817      1.45   mlelstv 	*unitsptr -= tensdelta * base;
   1818  1.67.2.3       tls 	return increment_overflow32(tensptr, tensdelta);
   1819      1.45   mlelstv }
   1820      1.45   mlelstv 
   1821      1.45   mlelstv static int
   1822      1.49  christos tmcomp(const struct tm *const atmp, const struct tm *const btmp)
   1823       1.1       jtc {
   1824      1.49  christos 	int	result;
   1825       1.1       jtc 
   1826  1.67.2.3       tls 	if (atmp->tm_year != btmp->tm_year)
   1827  1.67.2.3       tls 		return atmp->tm_year < btmp->tm_year ? -1 : 1;
   1828  1.67.2.3       tls 	if ((result = (atmp->tm_mon - btmp->tm_mon)) == 0 &&
   1829       1.1       jtc 		(result = (atmp->tm_mday - btmp->tm_mday)) == 0 &&
   1830       1.1       jtc 		(result = (atmp->tm_hour - btmp->tm_hour)) == 0 &&
   1831       1.1       jtc 		(result = (atmp->tm_min - btmp->tm_min)) == 0)
   1832       1.1       jtc 			result = atmp->tm_sec - btmp->tm_sec;
   1833       1.1       jtc 	return result;
   1834       1.1       jtc }
   1835       1.1       jtc 
   1836       1.1       jtc static time_t
   1837      1.49  christos time2sub(const timezone_t sp, struct tm *const tmp, subfun_t funcp,
   1838  1.67.2.3       tls     const int_fast32_t offset, int *const okayp, const int do_norm_secs)
   1839      1.49  christos {
   1840      1.49  christos 	int			dir;
   1841      1.49  christos 	int			i, j;
   1842      1.49  christos 	int			saved_seconds;
   1843  1.67.2.3       tls 	int_fast32_t		li;
   1844      1.49  christos 	time_t			lo;
   1845      1.49  christos 	time_t			hi;
   1846      1.61  christos #ifdef NO_ERROR_IN_DST_GAP
   1847      1.61  christos 	time_t			ilo;
   1848      1.61  christos #endif
   1849  1.67.2.3       tls 	int_fast32_t		y;
   1850  1.67.2.3       tls 	time_t			newt;
   1851  1.67.2.3       tls 	time_t			t;
   1852  1.67.2.3       tls 	struct tm		yourtm, mytm;
   1853       1.1       jtc 
   1854       1.1       jtc 	*okayp = FALSE;
   1855       1.1       jtc 	yourtm = *tmp;
   1856      1.64  christos #ifdef NO_ERROR_IN_DST_GAP
   1857      1.64  christos again:
   1858      1.64  christos #endif
   1859      1.13       jtc 	if (do_norm_secs) {
   1860      1.13       jtc 		if (normalize_overflow(&yourtm.tm_min, &yourtm.tm_sec,
   1861      1.60  christos 		    SECSPERMIN))
   1862      1.60  christos 			goto overflow;
   1863      1.13       jtc 	}
   1864       1.1       jtc 	if (normalize_overflow(&yourtm.tm_hour, &yourtm.tm_min, MINSPERHOUR))
   1865      1.60  christos 		goto overflow;
   1866       1.1       jtc 	if (normalize_overflow(&yourtm.tm_mday, &yourtm.tm_hour, HOURSPERDAY))
   1867      1.60  christos 		goto overflow;
   1868      1.45   mlelstv 	y = yourtm.tm_year;
   1869  1.67.2.3       tls 	if (normalize_overflow32(&y, &yourtm.tm_mon, MONSPERYEAR))
   1870      1.60  christos 		goto overflow;
   1871       1.1       jtc 	/*
   1872      1.45   mlelstv 	** Turn y into an actual year number for now.
   1873       1.1       jtc 	** It is converted back to an offset from TM_YEAR_BASE later.
   1874       1.1       jtc 	*/
   1875  1.67.2.3       tls 	if (increment_overflow32(&y, TM_YEAR_BASE))
   1876      1.60  christos 		goto overflow;
   1877       1.1       jtc 	while (yourtm.tm_mday <= 0) {
   1878  1.67.2.3       tls 		if (increment_overflow32(&y, -1))
   1879      1.60  christos 			goto overflow;
   1880      1.45   mlelstv 		li = y + (1 < yourtm.tm_mon);
   1881      1.45   mlelstv 		yourtm.tm_mday += year_lengths[isleap(li)];
   1882       1.1       jtc 	}
   1883       1.1       jtc 	while (yourtm.tm_mday > DAYSPERLYEAR) {
   1884      1.45   mlelstv 		li = y + (1 < yourtm.tm_mon);
   1885      1.45   mlelstv 		yourtm.tm_mday -= year_lengths[isleap(li)];
   1886  1.67.2.3       tls 		if (increment_overflow32(&y, 1))
   1887      1.60  christos 			goto overflow;
   1888       1.1       jtc 	}
   1889       1.1       jtc 	for ( ; ; ) {
   1890      1.45   mlelstv 		i = mon_lengths[isleap(y)][yourtm.tm_mon];
   1891       1.1       jtc 		if (yourtm.tm_mday <= i)
   1892       1.1       jtc 			break;
   1893       1.1       jtc 		yourtm.tm_mday -= i;
   1894       1.1       jtc 		if (++yourtm.tm_mon >= MONSPERYEAR) {
   1895       1.1       jtc 			yourtm.tm_mon = 0;
   1896  1.67.2.3       tls 			if (increment_overflow32(&y, 1))
   1897      1.60  christos 				goto overflow;
   1898       1.1       jtc 		}
   1899       1.1       jtc 	}
   1900  1.67.2.3       tls 	if (increment_overflow32(&y, -TM_YEAR_BASE))
   1901      1.60  christos 		goto overflow;
   1902      1.66  christos 	yourtm.tm_year = (int)y;
   1903      1.45   mlelstv 	if (yourtm.tm_year != y)
   1904      1.60  christos 		goto overflow;
   1905      1.29    kleink 	if (yourtm.tm_sec >= 0 && yourtm.tm_sec < SECSPERMIN)
   1906      1.29    kleink 		saved_seconds = 0;
   1907      1.45   mlelstv 	else if (y + TM_YEAR_BASE < EPOCH_YEAR) {
   1908       1.1       jtc 		/*
   1909       1.1       jtc 		** We can't set tm_sec to 0, because that might push the
   1910       1.1       jtc 		** time below the minimum representable time.
   1911       1.1       jtc 		** Set tm_sec to 59 instead.
   1912       1.1       jtc 		** This assumes that the minimum representable time is
   1913       1.1       jtc 		** not in the same minute that a leap second was deleted from,
   1914       1.1       jtc 		** which is a safer assumption than using 58 would be.
   1915       1.1       jtc 		*/
   1916       1.1       jtc 		if (increment_overflow(&yourtm.tm_sec, 1 - SECSPERMIN))
   1917      1.60  christos 			goto overflow;
   1918       1.1       jtc 		saved_seconds = yourtm.tm_sec;
   1919       1.1       jtc 		yourtm.tm_sec = SECSPERMIN - 1;
   1920       1.1       jtc 	} else {
   1921       1.1       jtc 		saved_seconds = yourtm.tm_sec;
   1922       1.1       jtc 		yourtm.tm_sec = 0;
   1923       1.1       jtc 	}
   1924       1.1       jtc 	/*
   1925      1.45   mlelstv 	** Do a binary search (this works whatever time_t's type is).
   1926       1.1       jtc 	*/
   1927  1.67.2.1       tls 	/* LINTED const not */
   1928      1.45   mlelstv 	if (!TYPE_SIGNED(time_t)) {
   1929      1.45   mlelstv 		lo = 0;
   1930      1.45   mlelstv 		hi = lo - 1;
   1931      1.45   mlelstv 	} else {
   1932      1.45   mlelstv 		lo = 1;
   1933      1.45   mlelstv 		for (i = 0; i < (int) TYPE_BIT(time_t) - 1; ++i)
   1934      1.45   mlelstv 			lo *= 2;
   1935      1.45   mlelstv 		hi = -(lo + 1);
   1936      1.45   mlelstv 	}
   1937      1.61  christos #ifdef NO_ERROR_IN_DST_GAP
   1938      1.61  christos 	ilo = lo;
   1939      1.61  christos #endif
   1940       1.1       jtc 	for ( ; ; ) {
   1941      1.45   mlelstv 		t = lo / 2 + hi / 2;
   1942      1.45   mlelstv 		if (t < lo)
   1943      1.45   mlelstv 			t = lo;
   1944      1.45   mlelstv 		else if (t > hi)
   1945      1.45   mlelstv 			t = hi;
   1946      1.49  christos 		if ((*funcp)(sp, &t, offset, &mytm) == NULL) {
   1947      1.45   mlelstv 			/*
   1948      1.45   mlelstv 			** Assume that t is too extreme to be represented in
   1949      1.45   mlelstv 			** a struct tm; arrange things so that it is less
   1950      1.45   mlelstv 			** extreme on the next pass.
   1951      1.45   mlelstv 			*/
   1952      1.45   mlelstv 			dir = (t > 0) ? 1 : -1;
   1953      1.45   mlelstv 		} else	dir = tmcomp(&mytm, &yourtm);
   1954       1.1       jtc 		if (dir != 0) {
   1955      1.45   mlelstv 			if (t == lo) {
   1956  1.67.2.3       tls 				if (t == time_t_max)
   1957      1.60  christos 					goto overflow;
   1958  1.67.2.3       tls 				++t;
   1959      1.45   mlelstv 				++lo;
   1960      1.45   mlelstv 			} else if (t == hi) {
   1961  1.67.2.3       tls 				if (t == time_t_min)
   1962      1.60  christos 					goto overflow;
   1963  1.67.2.3       tls 				--t;
   1964      1.45   mlelstv 				--hi;
   1965      1.45   mlelstv 			}
   1966      1.59  christos #ifdef NO_ERROR_IN_DST_GAP
   1967      1.64  christos 			if (ilo != lo && lo - 1 == hi && yourtm.tm_isdst < 0 &&
   1968      1.64  christos 			    do_norm_secs) {
   1969      1.59  christos 				for (i = sp->typecnt - 1; i >= 0; --i) {
   1970      1.59  christos 					for (j = sp->typecnt - 1; j >= 0; --j) {
   1971      1.64  christos 						time_t off;
   1972      1.59  christos 						if (sp->ttis[j].tt_isdst ==
   1973      1.59  christos 						    sp->ttis[i].tt_isdst)
   1974      1.59  christos 							continue;
   1975      1.59  christos 						off = sp->ttis[j].tt_gmtoff -
   1976      1.59  christos 						    sp->ttis[i].tt_gmtoff;
   1977      1.64  christos 						yourtm.tm_sec += off < 0 ?
   1978      1.64  christos 						    -off : off;
   1979      1.64  christos 						goto again;
   1980      1.59  christos 					}
   1981      1.59  christos 				}
   1982      1.59  christos 			}
   1983      1.59  christos #endif
   1984      1.45   mlelstv 			if (lo > hi)
   1985      1.60  christos 				goto invalid;
   1986      1.45   mlelstv 			if (dir > 0)
   1987      1.45   mlelstv 				hi = t;
   1988      1.45   mlelstv 			else	lo = t;
   1989       1.1       jtc 			continue;
   1990       1.1       jtc 		}
   1991       1.1       jtc 		if (yourtm.tm_isdst < 0 || mytm.tm_isdst == yourtm.tm_isdst)
   1992       1.1       jtc 			break;
   1993       1.1       jtc 		/*
   1994       1.1       jtc 		** Right time, wrong type.
   1995       1.1       jtc 		** Hunt for right time, right type.
   1996       1.1       jtc 		** It's okay to guess wrong since the guess
   1997       1.1       jtc 		** gets checked.
   1998       1.1       jtc 		*/
   1999       1.1       jtc 		if (sp == NULL)
   2000      1.60  christos 			goto invalid;
   2001       1.5       jtc 		for (i = sp->typecnt - 1; i >= 0; --i) {
   2002       1.1       jtc 			if (sp->ttis[i].tt_isdst != yourtm.tm_isdst)
   2003       1.1       jtc 				continue;
   2004       1.5       jtc 			for (j = sp->typecnt - 1; j >= 0; --j) {
   2005       1.1       jtc 				if (sp->ttis[j].tt_isdst == yourtm.tm_isdst)
   2006       1.1       jtc 					continue;
   2007      1.66  christos 				newt = (time_t)(t + sp->ttis[j].tt_gmtoff -
   2008      1.66  christos 				    sp->ttis[i].tt_gmtoff);
   2009      1.49  christos 				if ((*funcp)(sp, &newt, offset, &mytm) == NULL)
   2010      1.45   mlelstv 					continue;
   2011       1.1       jtc 				if (tmcomp(&mytm, &yourtm) != 0)
   2012       1.1       jtc 					continue;
   2013       1.1       jtc 				if (mytm.tm_isdst != yourtm.tm_isdst)
   2014       1.1       jtc 					continue;
   2015       1.1       jtc 				/*
   2016       1.1       jtc 				** We have a match.
   2017       1.1       jtc 				*/
   2018       1.1       jtc 				t = newt;
   2019       1.1       jtc 				goto label;
   2020       1.1       jtc 			}
   2021       1.1       jtc 		}
   2022      1.60  christos 		goto invalid;
   2023       1.1       jtc 	}
   2024       1.1       jtc label:
   2025       1.1       jtc 	newt = t + saved_seconds;
   2026       1.1       jtc 	if ((newt < t) != (saved_seconds < 0))
   2027      1.60  christos 		goto overflow;
   2028       1.1       jtc 	t = newt;
   2029      1.51  christos 	if ((*funcp)(sp, &t, offset, tmp)) {
   2030      1.45   mlelstv 		*okayp = TRUE;
   2031      1.51  christos 		return t;
   2032      1.60  christos 	}
   2033      1.60  christos overflow:
   2034      1.60  christos 	errno = EOVERFLOW;
   2035      1.60  christos 	return WRONG;
   2036      1.60  christos invalid:
   2037      1.60  christos 	errno = EINVAL;
   2038      1.60  christos 	return WRONG;
   2039      1.13       jtc }
   2040      1.13       jtc 
   2041      1.13       jtc static time_t
   2042      1.49  christos time2(const timezone_t sp, struct tm *const tmp, subfun_t funcp,
   2043  1.67.2.3       tls     const int_fast32_t offset, int *const okayp)
   2044      1.13       jtc {
   2045      1.13       jtc 	time_t	t;
   2046      1.13       jtc 
   2047      1.13       jtc 	/*
   2048      1.13       jtc 	** First try without normalization of seconds
   2049      1.13       jtc 	** (in case tm_sec contains a value associated with a leap second).
   2050      1.13       jtc 	** If that fails, try with normalization of seconds.
   2051      1.13       jtc 	*/
   2052      1.49  christos 	t = time2sub(sp, tmp, funcp, offset, okayp, FALSE);
   2053      1.49  christos 	return *okayp ? t : time2sub(sp, tmp, funcp, offset, okayp, TRUE);
   2054       1.1       jtc }
   2055       1.1       jtc 
   2056       1.1       jtc static time_t
   2057      1.49  christos time1(const timezone_t sp, struct tm *const tmp, subfun_t funcp,
   2058  1.67.2.3       tls     const int_fast32_t offset)
   2059      1.49  christos {
   2060      1.49  christos 	time_t			t;
   2061      1.49  christos 	int			samei, otheri;
   2062      1.49  christos 	int			sameind, otherind;
   2063      1.49  christos 	int			i;
   2064      1.49  christos 	int			nseen;
   2065      1.35    kleink 	int				seen[TZ_MAX_TYPES];
   2066      1.35    kleink 	int				types[TZ_MAX_TYPES];
   2067       1.1       jtc 	int				okay;
   2068       1.1       jtc 
   2069      1.58  christos 	if (tmp == NULL) {
   2070      1.58  christos 		errno = EINVAL;
   2071      1.58  christos 		return WRONG;
   2072      1.58  christos 	}
   2073       1.1       jtc 	if (tmp->tm_isdst > 1)
   2074       1.1       jtc 		tmp->tm_isdst = 1;
   2075      1.49  christos 	t = time2(sp, tmp, funcp, offset, &okay);
   2076       1.1       jtc 	if (okay)
   2077       1.1       jtc 		return t;
   2078       1.1       jtc 	if (tmp->tm_isdst < 0)
   2079  1.67.2.3       tls #ifdef PCTS
   2080  1.67.2.3       tls 		/*
   2081  1.67.2.3       tls 		** POSIX Conformance Test Suite code courtesy Grant Sullivan.
   2082  1.67.2.3       tls 		*/
   2083       1.1       jtc 		tmp->tm_isdst = 0;	/* reset to std and try again */
   2084  1.67.2.3       tls #else
   2085       1.1       jtc 		return t;
   2086       1.1       jtc #endif /* !defined PCTS */
   2087       1.1       jtc 	/*
   2088       1.1       jtc 	** We're supposed to assume that somebody took a time of one type
   2089       1.1       jtc 	** and did some math on it that yielded a "struct tm" that's bad.
   2090       1.1       jtc 	** We try to divine the type they started from and adjust to the
   2091       1.1       jtc 	** type they need.
   2092       1.1       jtc 	*/
   2093      1.60  christos 	if (sp == NULL) {
   2094      1.60  christos 		errno = EINVAL;
   2095       1.1       jtc 		return WRONG;
   2096      1.60  christos 	}
   2097      1.35    kleink 	for (i = 0; i < sp->typecnt; ++i)
   2098      1.35    kleink 		seen[i] = FALSE;
   2099      1.35    kleink 	nseen = 0;
   2100      1.35    kleink 	for (i = sp->timecnt - 1; i >= 0; --i)
   2101      1.35    kleink 		if (!seen[sp->types[i]]) {
   2102      1.35    kleink 			seen[sp->types[i]] = TRUE;
   2103      1.35    kleink 			types[nseen++] = sp->types[i];
   2104      1.35    kleink 		}
   2105      1.35    kleink 	for (sameind = 0; sameind < nseen; ++sameind) {
   2106      1.35    kleink 		samei = types[sameind];
   2107       1.1       jtc 		if (sp->ttis[samei].tt_isdst != tmp->tm_isdst)
   2108       1.1       jtc 			continue;
   2109      1.35    kleink 		for (otherind = 0; otherind < nseen; ++otherind) {
   2110      1.35    kleink 			otheri = types[otherind];
   2111       1.1       jtc 			if (sp->ttis[otheri].tt_isdst == tmp->tm_isdst)
   2112       1.1       jtc 				continue;
   2113      1.21  christos 			tmp->tm_sec += (int)(sp->ttis[otheri].tt_gmtoff -
   2114      1.21  christos 					sp->ttis[samei].tt_gmtoff);
   2115       1.1       jtc 			tmp->tm_isdst = !tmp->tm_isdst;
   2116      1.49  christos 			t = time2(sp, tmp, funcp, offset, &okay);
   2117       1.1       jtc 			if (okay)
   2118       1.1       jtc 				return t;
   2119      1.21  christos 			tmp->tm_sec -= (int)(sp->ttis[otheri].tt_gmtoff -
   2120      1.21  christos 					sp->ttis[samei].tt_gmtoff);
   2121       1.1       jtc 			tmp->tm_isdst = !tmp->tm_isdst;
   2122       1.1       jtc 		}
   2123       1.1       jtc 	}
   2124      1.60  christos 	errno = EOVERFLOW;
   2125       1.1       jtc 	return WRONG;
   2126       1.1       jtc }
   2127       1.1       jtc 
   2128       1.1       jtc time_t
   2129  1.67.2.1       tls mktime_z(const timezone_t sp, struct tm *const tmp)
   2130      1.49  christos {
   2131      1.51  christos 	time_t t;
   2132      1.49  christos 	if (sp == NULL)
   2133  1.67.2.3       tls 		t = time1(NULL, tmp, gmtsub, 0);
   2134      1.49  christos 	else
   2135  1.67.2.3       tls 		t = time1(sp, tmp, localsub, 0);
   2136      1.51  christos 	return t;
   2137      1.49  christos }
   2138      1.49  christos 
   2139      1.49  christos time_t
   2140  1.67.2.1       tls mktime(struct tm *const tmp)
   2141       1.1       jtc {
   2142      1.19    kleink 	time_t result;
   2143      1.19    kleink 
   2144      1.45   mlelstv 	rwlock_wrlock(&lcl_lock);
   2145      1.45   mlelstv 	tzset_unlocked();
   2146      1.49  christos 	result = mktime_z(lclptr, tmp);
   2147      1.45   mlelstv 	rwlock_unlock(&lcl_lock);
   2148      1.49  christos 	return result;
   2149       1.1       jtc }
   2150       1.1       jtc 
   2151       1.1       jtc #ifdef STD_INSPIRED
   2152       1.1       jtc 
   2153       1.1       jtc time_t
   2154  1.67.2.1       tls timelocal_z(const timezone_t sp, struct tm *const tmp)
   2155      1.49  christos {
   2156      1.49  christos 	if (tmp != NULL)
   2157      1.49  christos 		tmp->tm_isdst = -1;	/* in case it wasn't initialized */
   2158      1.49  christos 	return mktime_z(sp, tmp);
   2159      1.49  christos }
   2160      1.49  christos 
   2161      1.49  christos time_t
   2162      1.49  christos timelocal(struct tm *const tmp)
   2163       1.1       jtc {
   2164      1.58  christos 	if (tmp != NULL)
   2165      1.58  christos 		tmp->tm_isdst = -1;	/* in case it wasn't initialized */
   2166       1.1       jtc 	return mktime(tmp);
   2167       1.1       jtc }
   2168       1.1       jtc 
   2169       1.1       jtc time_t
   2170      1.49  christos timegm(struct tm *const tmp)
   2171       1.1       jtc {
   2172      1.51  christos 	time_t t;
   2173      1.51  christos 
   2174      1.58  christos 	if (tmp != NULL)
   2175      1.58  christos 		tmp->tm_isdst = 0;
   2176  1.67.2.3       tls 	t = time1(gmtptr, tmp, gmtsub, 0);
   2177      1.51  christos 	return t;
   2178       1.1       jtc }
   2179       1.1       jtc 
   2180       1.1       jtc time_t
   2181  1.67.2.3       tls timeoff(struct tm *const tmp, long offset)
   2182       1.1       jtc {
   2183      1.51  christos 	time_t t;
   2184      1.51  christos 
   2185  1.67.2.3       tls 	if ((offset > 0 && offset > INT_FAST32_MAX) ||
   2186  1.67.2.3       tls 	    (offset < 0 && offset < INT_FAST32_MIN)) {
   2187  1.67.2.3       tls 		errno = EOVERFLOW;
   2188  1.67.2.3       tls 		return -1;
   2189  1.67.2.3       tls 	}
   2190      1.58  christos 	if (tmp != NULL)
   2191      1.58  christos 		tmp->tm_isdst = 0;
   2192  1.67.2.3       tls 	t = time1(gmtptr, tmp, gmtsub, (int_fast32_t)offset);
   2193      1.51  christos 	return t;
   2194       1.1       jtc }
   2195       1.1       jtc 
   2196       1.1       jtc #endif /* defined STD_INSPIRED */
   2197       1.1       jtc 
   2198       1.1       jtc #ifdef CMUCS
   2199       1.1       jtc 
   2200       1.1       jtc /*
   2201       1.1       jtc ** The following is supplied for compatibility with
   2202       1.1       jtc ** previous versions of the CMUCS runtime library.
   2203       1.1       jtc */
   2204       1.1       jtc 
   2205       1.1       jtc long
   2206      1.49  christos gtime(struct tm *const tmp)
   2207       1.1       jtc {
   2208      1.49  christos 	const time_t t = mktime(tmp);
   2209       1.1       jtc 
   2210       1.1       jtc 	if (t == WRONG)
   2211       1.1       jtc 		return -1;
   2212       1.1       jtc 	return t;
   2213       1.1       jtc }
   2214       1.1       jtc 
   2215       1.1       jtc #endif /* defined CMUCS */
   2216       1.1       jtc 
   2217       1.1       jtc /*
   2218       1.1       jtc ** XXX--is the below the right way to conditionalize??
   2219       1.1       jtc */
   2220       1.1       jtc 
   2221       1.1       jtc #ifdef STD_INSPIRED
   2222       1.1       jtc 
   2223       1.1       jtc /*
   2224       1.1       jtc ** IEEE Std 1003.1-1988 (POSIX) legislates that 536457599
   2225      1.14       jtc ** shall correspond to "Wed Dec 31 23:59:59 UTC 1986", which
   2226       1.1       jtc ** is not the case if we are accounting for leap seconds.
   2227       1.1       jtc ** So, we provide the following conversion routines for use
   2228       1.1       jtc ** when exchanging timestamps with POSIX conforming systems.
   2229       1.1       jtc */
   2230       1.1       jtc 
   2231  1.67.2.3       tls static int_fast64_t
   2232      1.49  christos leapcorr(const timezone_t sp, time_t *timep)
   2233       1.1       jtc {
   2234      1.49  christos 	struct lsinfo * lp;
   2235      1.49  christos 	int		i;
   2236       1.1       jtc 
   2237       1.1       jtc 	i = sp->leapcnt;
   2238       1.1       jtc 	while (--i >= 0) {
   2239       1.1       jtc 		lp = &sp->lsis[i];
   2240       1.1       jtc 		if (*timep >= lp->ls_trans)
   2241       1.1       jtc 			return lp->ls_corr;
   2242       1.1       jtc 	}
   2243       1.1       jtc 	return 0;
   2244       1.1       jtc }
   2245       1.1       jtc 
   2246       1.1       jtc time_t
   2247      1.49  christos time2posix_z(const timezone_t sp, time_t t)
   2248      1.49  christos {
   2249      1.66  christos 	return (time_t)(t - leapcorr(sp, &t));
   2250      1.49  christos }
   2251      1.49  christos 
   2252      1.49  christos time_t
   2253      1.49  christos time2posix(time_t t)
   2254       1.1       jtc {
   2255      1.19    kleink 	time_t result;
   2256      1.45   mlelstv 	rwlock_wrlock(&lcl_lock);
   2257      1.45   mlelstv 	tzset_unlocked();
   2258      1.66  christos 	result = (time_t)(t - leapcorr(lclptr, &t));
   2259      1.45   mlelstv 	rwlock_unlock(&lcl_lock);
   2260      1.19    kleink 	return (result);
   2261       1.1       jtc }
   2262       1.1       jtc 
   2263       1.1       jtc time_t
   2264      1.49  christos posix2time_z(const timezone_t sp, time_t t)
   2265       1.1       jtc {
   2266       1.1       jtc 	time_t	x;
   2267       1.1       jtc 	time_t	y;
   2268       1.1       jtc 
   2269       1.1       jtc 	/*
   2270       1.1       jtc 	** For a positive leap second hit, the result
   2271      1.45   mlelstv 	** is not unique. For a negative leap second
   2272       1.1       jtc 	** hit, the corresponding time doesn't exist,
   2273       1.1       jtc 	** so we return an adjacent second.
   2274       1.1       jtc 	*/
   2275      1.66  christos 	x = (time_t)(t + leapcorr(sp, &t));
   2276      1.66  christos 	y = (time_t)(x - leapcorr(sp, &x));
   2277       1.1       jtc 	if (y < t) {
   2278       1.1       jtc 		do {
   2279       1.1       jtc 			x++;
   2280      1.66  christos 			y = (time_t)(x - leapcorr(sp, &x));
   2281       1.1       jtc 		} while (y < t);
   2282      1.19    kleink 		if (t != y) {
   2283       1.1       jtc 			return x - 1;
   2284      1.19    kleink 		}
   2285       1.1       jtc 	} else if (y > t) {
   2286       1.1       jtc 		do {
   2287       1.1       jtc 			--x;
   2288      1.66  christos 			y = (time_t)(x - leapcorr(sp, &x));
   2289       1.1       jtc 		} while (y > t);
   2290      1.19    kleink 		if (t != y) {
   2291       1.1       jtc 			return x + 1;
   2292      1.19    kleink 		}
   2293       1.1       jtc 	}
   2294      1.49  christos 	return x;
   2295      1.49  christos }
   2296      1.49  christos 
   2297      1.49  christos 
   2298      1.49  christos 
   2299      1.49  christos time_t
   2300      1.49  christos posix2time(time_t t)
   2301      1.49  christos {
   2302      1.49  christos 	time_t result;
   2303      1.49  christos 
   2304      1.49  christos 	rwlock_wrlock(&lcl_lock);
   2305      1.49  christos 	tzset_unlocked();
   2306      1.49  christos 	result = posix2time_z(lclptr, t);
   2307      1.45   mlelstv 	rwlock_unlock(&lcl_lock);
   2308      1.49  christos 	return result;
   2309       1.1       jtc }
   2310       1.1       jtc 
   2311       1.1       jtc #endif /* defined STD_INSPIRED */
   2312