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