Home | History | Annotate | Line # | Download | only in time
zic.c revision 1.4
      1  1.4  jtc /*	$NetBSD: zic.c,v 1.4 1996/01/20 02:31:50 jtc Exp $	*/
      2  1.2  jtc 
      3  1.1  jtc #ifndef lint
      4  1.1  jtc #ifndef NOID
      5  1.4  jtc static char	elsieid[] = "@(#)zic.c	7.59";
      6  1.1  jtc #endif /* !defined NOID */
      7  1.1  jtc #endif /* !defined lint */
      8  1.1  jtc 
      9  1.1  jtc #include "private.h"
     10  1.1  jtc #include "tzfile.h"
     11  1.1  jtc #ifdef unix
     12  1.1  jtc #include "sys/stat.h"			/* for umask manifest constants */
     13  1.1  jtc #endif /* defined unix */
     14  1.1  jtc 
     15  1.3  jtc /*
     16  1.3  jtc ** On some ancient hosts, predicates like `isspace(C)' are defined
     17  1.3  jtc ** only if isascii(C) || C == EOF.  Modern hosts obey the C Standard,
     18  1.3  jtc ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
     19  1.3  jtc ** Neither the C Standard nor Posix require that `isascii' exist.
     20  1.3  jtc ** For portability, we check both ancient and modern requirements.
     21  1.3  jtc ** If isascii is not defined, the isascii check succeeds trivially.
     22  1.3  jtc */
     23  1.3  jtc #include "ctype.h"
     24  1.3  jtc #ifndef isascii
     25  1.3  jtc #define isascii(x) 1
     26  1.3  jtc #endif
     27  1.3  jtc 
     28  1.1  jtc struct rule {
     29  1.1  jtc 	const char *	r_filename;
     30  1.1  jtc 	int		r_linenum;
     31  1.1  jtc 	const char *	r_name;
     32  1.1  jtc 
     33  1.1  jtc 	int		r_loyear;	/* for example, 1986 */
     34  1.1  jtc 	int		r_hiyear;	/* for example, 1986 */
     35  1.1  jtc 	const char *	r_yrtype;
     36  1.1  jtc 
     37  1.1  jtc 	int		r_month;	/* 0..11 */
     38  1.1  jtc 
     39  1.1  jtc 	int		r_dycode;	/* see below */
     40  1.1  jtc 	int		r_dayofmonth;
     41  1.1  jtc 	int		r_wday;
     42  1.1  jtc 
     43  1.1  jtc 	long		r_tod;		/* time from midnight */
     44  1.1  jtc 	int		r_todisstd;	/* above is standard time if TRUE */
     45  1.1  jtc 					/* or wall clock time if FALSE */
     46  1.1  jtc 	int		r_todisgmt;	/* above is GMT if TRUE */
     47  1.1  jtc 					/* or local time if FALSE */
     48  1.1  jtc 	long		r_stdoff;	/* offset from standard time */
     49  1.1  jtc 	const char *	r_abbrvar;	/* variable part of abbreviation */
     50  1.1  jtc 
     51  1.1  jtc 	int		r_todo;		/* a rule to do (used in outzone) */
     52  1.1  jtc 	time_t		r_temp;		/* used in outzone */
     53  1.1  jtc };
     54  1.1  jtc 
     55  1.1  jtc /*
     56  1.1  jtc **	r_dycode		r_dayofmonth	r_wday
     57  1.1  jtc */
     58  1.1  jtc 
     59  1.1  jtc #define DC_DOM		0	/* 1..31 */	/* unused */
     60  1.1  jtc #define DC_DOWGEQ	1	/* 1..31 */	/* 0..6 (Sun..Sat) */
     61  1.1  jtc #define DC_DOWLEQ	2	/* 1..31 */	/* 0..6 (Sun..Sat) */
     62  1.1  jtc 
     63  1.1  jtc struct zone {
     64  1.1  jtc 	const char *	z_filename;
     65  1.1  jtc 	int		z_linenum;
     66  1.1  jtc 
     67  1.1  jtc 	const char *	z_name;
     68  1.1  jtc 	long		z_gmtoff;
     69  1.1  jtc 	const char *	z_rule;
     70  1.1  jtc 	const char *	z_format;
     71  1.1  jtc 
     72  1.1  jtc 	long		z_stdoff;
     73  1.1  jtc 
     74  1.1  jtc 	struct rule *	z_rules;
     75  1.1  jtc 	int		z_nrules;
     76  1.1  jtc 
     77  1.1  jtc 	struct rule	z_untilrule;
     78  1.1  jtc 	time_t		z_untiltime;
     79  1.1  jtc };
     80  1.1  jtc 
     81  1.1  jtc extern int	getopt P((int argc, char * const argv[],
     82  1.1  jtc 			const char * options));
     83  1.1  jtc extern char *	icatalloc P((char * old, const char * new));
     84  1.1  jtc extern char *	icpyalloc P((const char * string));
     85  1.1  jtc extern void	ifree P((char * p));
     86  1.1  jtc extern char *	imalloc P((int n));
     87  1.1  jtc extern void *	irealloc P((void * old, int n));
     88  1.1  jtc extern int	link P((const char * fromname, const char * toname));
     89  1.1  jtc extern char *	optarg;
     90  1.1  jtc extern int	optind;
     91  1.1  jtc extern char *	scheck P((const char * string, const char * format));
     92  1.1  jtc 
     93  1.1  jtc static void	addtt P((time_t starttime, int type));
     94  1.1  jtc static int	addtype P((long gmtoff, const char * abbr, int isdst,
     95  1.1  jtc 				int ttisstd, int ttisgmt));
     96  1.1  jtc static void	leapadd P((time_t t, int positive, int rolling, int count));
     97  1.1  jtc static void	adjleap P((void));
     98  1.1  jtc static void	associate P((void));
     99  1.1  jtc static int	ciequal P((const char * ap, const char * bp));
    100  1.1  jtc static void	convert P((long val, char * buf));
    101  1.1  jtc static void	dolink P((const char * fromfile, const char * tofile));
    102  1.1  jtc static void	doabbr P((char * abbr, const char * format,
    103  1.1  jtc 			const char * letters, int isdst));
    104  1.1  jtc static void	eat P((const char * name, int num));
    105  1.1  jtc static void	eats P((const char * name, int num,
    106  1.1  jtc 			const char * rname, int rnum));
    107  1.1  jtc static long	eitol P((int i));
    108  1.1  jtc static void	error P((const char * message));
    109  1.1  jtc static char **	getfields P((char * buf));
    110  1.1  jtc static long	gethms P((const char * string, const char * errstrng,
    111  1.1  jtc 			int signable));
    112  1.1  jtc static void	infile P((const char * filename));
    113  1.1  jtc static void	inleap P((char ** fields, int nfields));
    114  1.1  jtc static void	inlink P((char ** fields, int nfields));
    115  1.1  jtc static void	inrule P((char ** fields, int nfields));
    116  1.1  jtc static int	inzcont P((char ** fields, int nfields));
    117  1.1  jtc static int	inzone P((char ** fields, int nfields));
    118  1.1  jtc static int	inzsub P((char ** fields, int nfields, int iscont));
    119  1.1  jtc static int	itsabbr P((const char * abbr, const char * word));
    120  1.1  jtc static int	itsdir P((const char * name));
    121  1.1  jtc static int	lowerit P((int c));
    122  1.1  jtc static char *	memcheck P((char * tocheck));
    123  1.1  jtc static int	mkdirs P((char * filename));
    124  1.1  jtc static void	newabbr P((const char * abbr));
    125  1.1  jtc static long	oadd P((long t1, long t2));
    126  1.1  jtc static void	outzone P((const struct zone * zp, int ntzones));
    127  1.1  jtc static void	puttzcode P((long code, FILE * fp));
    128  1.1  jtc static int	rcomp P((const void * leftp, const void * rightp));
    129  1.1  jtc static time_t	rpytime P((const struct rule * rp, int wantedy));
    130  1.1  jtc static void	rulesub P((struct rule * rp,
    131  1.1  jtc 			const char * loyearp, const char * hiyearp,
    132  1.1  jtc 			const char * typep, const char * monthp,
    133  1.1  jtc 			const char * dayp, const char * timep));
    134  1.1  jtc static void	setboundaries P((void));
    135  1.1  jtc static time_t	tadd P((time_t t1, long t2));
    136  1.1  jtc static void	usage P((void));
    137  1.1  jtc static void	writezone P((const char * name));
    138  1.1  jtc static int	yearistype P((int year, const char * type));
    139  1.1  jtc 
    140  1.1  jtc static int		charcnt;
    141  1.1  jtc static int		errors;
    142  1.1  jtc static const char *	filename;
    143  1.1  jtc static int		leapcnt;
    144  1.1  jtc static int		linenum;
    145  1.1  jtc static time_t		max_time;
    146  1.1  jtc static int		max_year;
    147  1.1  jtc static time_t		min_time;
    148  1.1  jtc static int		min_year;
    149  1.1  jtc static int		noise;
    150  1.1  jtc static const char *	rfilename;
    151  1.1  jtc static int		rlinenum;
    152  1.1  jtc static const char *	progname;
    153  1.1  jtc static int		timecnt;
    154  1.1  jtc static int		typecnt;
    155  1.1  jtc 
    156  1.1  jtc /*
    157  1.1  jtc ** Line codes.
    158  1.1  jtc */
    159  1.1  jtc 
    160  1.1  jtc #define LC_RULE		0
    161  1.1  jtc #define LC_ZONE		1
    162  1.1  jtc #define LC_LINK		2
    163  1.1  jtc #define LC_LEAP		3
    164  1.1  jtc 
    165  1.1  jtc /*
    166  1.1  jtc ** Which fields are which on a Zone line.
    167  1.1  jtc */
    168  1.1  jtc 
    169  1.1  jtc #define ZF_NAME		1
    170  1.1  jtc #define ZF_GMTOFF	2
    171  1.1  jtc #define ZF_RULE		3
    172  1.1  jtc #define ZF_FORMAT	4
    173  1.1  jtc #define ZF_TILYEAR	5
    174  1.1  jtc #define ZF_TILMONTH	6
    175  1.1  jtc #define ZF_TILDAY	7
    176  1.1  jtc #define ZF_TILTIME	8
    177  1.1  jtc #define ZONE_MINFIELDS	5
    178  1.1  jtc #define ZONE_MAXFIELDS	9
    179  1.1  jtc 
    180  1.1  jtc /*
    181  1.1  jtc ** Which fields are which on a Zone continuation line.
    182  1.1  jtc */
    183  1.1  jtc 
    184  1.1  jtc #define ZFC_GMTOFF	0
    185  1.1  jtc #define ZFC_RULE	1
    186  1.1  jtc #define ZFC_FORMAT	2
    187  1.1  jtc #define ZFC_TILYEAR	3
    188  1.1  jtc #define ZFC_TILMONTH	4
    189  1.1  jtc #define ZFC_TILDAY	5
    190  1.1  jtc #define ZFC_TILTIME	6
    191  1.1  jtc #define ZONEC_MINFIELDS	3
    192  1.1  jtc #define ZONEC_MAXFIELDS	7
    193  1.1  jtc 
    194  1.1  jtc /*
    195  1.1  jtc ** Which files are which on a Rule line.
    196  1.1  jtc */
    197  1.1  jtc 
    198  1.1  jtc #define RF_NAME		1
    199  1.1  jtc #define RF_LOYEAR	2
    200  1.1  jtc #define RF_HIYEAR	3
    201  1.1  jtc #define RF_COMMAND	4
    202  1.1  jtc #define RF_MONTH	5
    203  1.1  jtc #define RF_DAY		6
    204  1.1  jtc #define RF_TOD		7
    205  1.1  jtc #define RF_STDOFF	8
    206  1.1  jtc #define RF_ABBRVAR	9
    207  1.1  jtc #define RULE_FIELDS	10
    208  1.1  jtc 
    209  1.1  jtc /*
    210  1.1  jtc ** Which fields are which on a Link line.
    211  1.1  jtc */
    212  1.1  jtc 
    213  1.1  jtc #define LF_FROM		1
    214  1.1  jtc #define LF_TO		2
    215  1.1  jtc #define LINK_FIELDS	3
    216  1.1  jtc 
    217  1.1  jtc /*
    218  1.1  jtc ** Which fields are which on a Leap line.
    219  1.1  jtc */
    220  1.1  jtc 
    221  1.1  jtc #define LP_YEAR		1
    222  1.1  jtc #define LP_MONTH	2
    223  1.1  jtc #define LP_DAY		3
    224  1.1  jtc #define LP_TIME		4
    225  1.1  jtc #define LP_CORR		5
    226  1.1  jtc #define LP_ROLL		6
    227  1.1  jtc #define LEAP_FIELDS	7
    228  1.1  jtc 
    229  1.1  jtc /*
    230  1.1  jtc ** Year synonyms.
    231  1.1  jtc */
    232  1.1  jtc 
    233  1.1  jtc #define YR_MINIMUM	0
    234  1.1  jtc #define YR_MAXIMUM	1
    235  1.1  jtc #define YR_ONLY		2
    236  1.1  jtc 
    237  1.1  jtc static struct rule *	rules;
    238  1.1  jtc static int		nrules;	/* number of rules */
    239  1.1  jtc 
    240  1.1  jtc static struct zone *	zones;
    241  1.1  jtc static int		nzones;	/* number of zones */
    242  1.1  jtc 
    243  1.1  jtc struct link {
    244  1.1  jtc 	const char *	l_filename;
    245  1.1  jtc 	int		l_linenum;
    246  1.1  jtc 	const char *	l_from;
    247  1.1  jtc 	const char *	l_to;
    248  1.1  jtc };
    249  1.1  jtc 
    250  1.1  jtc static struct link *	links;
    251  1.1  jtc static int		nlinks;
    252  1.1  jtc 
    253  1.1  jtc struct lookup {
    254  1.1  jtc 	const char *	l_word;
    255  1.1  jtc 	const int	l_value;
    256  1.1  jtc };
    257  1.1  jtc 
    258  1.1  jtc static struct lookup const *	byword P((const char * string,
    259  1.1  jtc 					const struct lookup * lp));
    260  1.1  jtc 
    261  1.1  jtc static struct lookup const	line_codes[] = {
    262  1.1  jtc 	{ "Rule",	LC_RULE },
    263  1.1  jtc 	{ "Zone",	LC_ZONE },
    264  1.1  jtc 	{ "Link",	LC_LINK },
    265  1.1  jtc 	{ "Leap",	LC_LEAP },
    266  1.1  jtc 	{ NULL,		0}
    267  1.1  jtc };
    268  1.1  jtc 
    269  1.1  jtc static struct lookup const	mon_names[] = {
    270  1.1  jtc 	{ "January",	TM_JANUARY },
    271  1.1  jtc 	{ "February",	TM_FEBRUARY },
    272  1.1  jtc 	{ "March",	TM_MARCH },
    273  1.1  jtc 	{ "April",	TM_APRIL },
    274  1.1  jtc 	{ "May",	TM_MAY },
    275  1.1  jtc 	{ "June",	TM_JUNE },
    276  1.1  jtc 	{ "July",	TM_JULY },
    277  1.1  jtc 	{ "August",	TM_AUGUST },
    278  1.1  jtc 	{ "September",	TM_SEPTEMBER },
    279  1.1  jtc 	{ "October",	TM_OCTOBER },
    280  1.1  jtc 	{ "November",	TM_NOVEMBER },
    281  1.1  jtc 	{ "December",	TM_DECEMBER },
    282  1.1  jtc 	{ NULL,		0 }
    283  1.1  jtc };
    284  1.1  jtc 
    285  1.1  jtc static struct lookup const	wday_names[] = {
    286  1.1  jtc 	{ "Sunday",	TM_SUNDAY },
    287  1.1  jtc 	{ "Monday",	TM_MONDAY },
    288  1.1  jtc 	{ "Tuesday",	TM_TUESDAY },
    289  1.1  jtc 	{ "Wednesday",	TM_WEDNESDAY },
    290  1.1  jtc 	{ "Thursday",	TM_THURSDAY },
    291  1.1  jtc 	{ "Friday",	TM_FRIDAY },
    292  1.1  jtc 	{ "Saturday",	TM_SATURDAY },
    293  1.1  jtc 	{ NULL,		0 }
    294  1.1  jtc };
    295  1.1  jtc 
    296  1.1  jtc static struct lookup const	lasts[] = {
    297  1.1  jtc 	{ "last-Sunday",	TM_SUNDAY },
    298  1.1  jtc 	{ "last-Monday",	TM_MONDAY },
    299  1.1  jtc 	{ "last-Tuesday",	TM_TUESDAY },
    300  1.1  jtc 	{ "last-Wednesday",	TM_WEDNESDAY },
    301  1.1  jtc 	{ "last-Thursday",	TM_THURSDAY },
    302  1.1  jtc 	{ "last-Friday",	TM_FRIDAY },
    303  1.1  jtc 	{ "last-Saturday",	TM_SATURDAY },
    304  1.1  jtc 	{ NULL,			0 }
    305  1.1  jtc };
    306  1.1  jtc 
    307  1.1  jtc static struct lookup const	begin_years[] = {
    308  1.1  jtc 	{ "minimum",	YR_MINIMUM },
    309  1.1  jtc 	{ "maximum",	YR_MAXIMUM },
    310  1.1  jtc 	{ NULL,		0 }
    311  1.1  jtc };
    312  1.1  jtc 
    313  1.1  jtc static struct lookup const	end_years[] = {
    314  1.1  jtc 	{ "minimum",	YR_MINIMUM },
    315  1.1  jtc 	{ "maximum",	YR_MAXIMUM },
    316  1.1  jtc 	{ "only",	YR_ONLY },
    317  1.1  jtc 	{ NULL,		0 }
    318  1.1  jtc };
    319  1.1  jtc 
    320  1.1  jtc static struct lookup const	leap_types[] = {
    321  1.1  jtc 	{ "Rolling",	TRUE },
    322  1.1  jtc 	{ "Stationary",	FALSE },
    323  1.1  jtc 	{ NULL,		0 }
    324  1.1  jtc };
    325  1.1  jtc 
    326  1.1  jtc static const int	len_months[2][MONSPERYEAR] = {
    327  1.1  jtc 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
    328  1.1  jtc 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
    329  1.1  jtc };
    330  1.1  jtc 
    331  1.1  jtc static const int	len_years[2] = {
    332  1.1  jtc 	DAYSPERNYEAR, DAYSPERLYEAR
    333  1.1  jtc };
    334  1.1  jtc 
    335  1.1  jtc static time_t		ats[TZ_MAX_TIMES];
    336  1.1  jtc static unsigned char	types[TZ_MAX_TIMES];
    337  1.1  jtc static long		gmtoffs[TZ_MAX_TYPES];
    338  1.1  jtc static char		isdsts[TZ_MAX_TYPES];
    339  1.1  jtc static unsigned char	abbrinds[TZ_MAX_TYPES];
    340  1.1  jtc static char		ttisstds[TZ_MAX_TYPES];
    341  1.1  jtc static char		ttisgmts[TZ_MAX_TYPES];
    342  1.1  jtc static char		chars[TZ_MAX_CHARS];
    343  1.1  jtc static time_t		trans[TZ_MAX_LEAPS];
    344  1.1  jtc static long		corr[TZ_MAX_LEAPS];
    345  1.1  jtc static char		roll[TZ_MAX_LEAPS];
    346  1.1  jtc 
    347  1.1  jtc /*
    348  1.1  jtc ** Memory allocation.
    349  1.1  jtc */
    350  1.1  jtc 
    351  1.1  jtc static char *
    352  1.1  jtc memcheck(ptr)
    353  1.1  jtc char * const	ptr;
    354  1.1  jtc {
    355  1.1  jtc 	if (ptr == NULL) {
    356  1.1  jtc 		(void) perror(progname);
    357  1.1  jtc 		(void) exit(EXIT_FAILURE);
    358  1.1  jtc 	}
    359  1.1  jtc 	return ptr;
    360  1.1  jtc }
    361  1.1  jtc 
    362  1.1  jtc #define emalloc(size)		memcheck(imalloc(size))
    363  1.1  jtc #define erealloc(ptr, size)	memcheck(irealloc((ptr), (size)))
    364  1.1  jtc #define ecpyalloc(ptr)		memcheck(icpyalloc(ptr))
    365  1.1  jtc #define ecatalloc(oldp, newp)	memcheck(icatalloc((oldp), (newp)))
    366  1.1  jtc 
    367  1.1  jtc /*
    368  1.1  jtc ** Error handling.
    369  1.1  jtc */
    370  1.1  jtc 
    371  1.1  jtc static void
    372  1.1  jtc eats(name, num, rname, rnum)
    373  1.1  jtc const char * const	name;
    374  1.1  jtc const int		num;
    375  1.1  jtc const char * const	rname;
    376  1.1  jtc const int		rnum;
    377  1.1  jtc {
    378  1.1  jtc 	filename = name;
    379  1.1  jtc 	linenum = num;
    380  1.1  jtc 	rfilename = rname;
    381  1.1  jtc 	rlinenum = rnum;
    382  1.1  jtc }
    383  1.1  jtc 
    384  1.1  jtc static void
    385  1.1  jtc eat(name, num)
    386  1.1  jtc const char * const	name;
    387  1.1  jtc const int		num;
    388  1.1  jtc {
    389  1.1  jtc 	eats(name, num, (char *) NULL, -1);
    390  1.1  jtc }
    391  1.1  jtc 
    392  1.1  jtc static void
    393  1.1  jtc error(string)
    394  1.1  jtc const char * const	string;
    395  1.1  jtc {
    396  1.1  jtc 	/*
    397  1.1  jtc 	** Match the format of "cc" to allow sh users to
    398  1.1  jtc 	**	zic ... 2>&1 | error -t "*" -v
    399  1.1  jtc 	** on BSD systems.
    400  1.1  jtc 	*/
    401  1.1  jtc 	(void) fprintf(stderr, "\"%s\", line %d: %s",
    402  1.1  jtc 		filename, linenum, string);
    403  1.1  jtc 	if (rfilename != NULL)
    404  1.1  jtc 		(void) fprintf(stderr, " (rule from \"%s\", line %d)",
    405  1.1  jtc 			rfilename, rlinenum);
    406  1.1  jtc 	(void) fprintf(stderr, "\n");
    407  1.1  jtc 	++errors;
    408  1.1  jtc }
    409  1.1  jtc 
    410  1.1  jtc static void
    411  1.1  jtc usage P((void))
    412  1.1  jtc {
    413  1.1  jtc 	(void) fprintf(stderr, "%s: usage is %s \
    414  1.1  jtc [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\
    415  1.1  jtc \t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n",
    416  1.1  jtc 		progname, progname);
    417  1.1  jtc 	(void) exit(EXIT_FAILURE);
    418  1.1  jtc }
    419  1.1  jtc 
    420  1.1  jtc static const char *	psxrules;
    421  1.1  jtc static const char *	lcltime;
    422  1.1  jtc static const char *	directory;
    423  1.1  jtc static const char *	leapsec;
    424  1.1  jtc static const char *	yitcommand;
    425  1.1  jtc static int		sflag = FALSE;
    426  1.1  jtc 
    427  1.1  jtc int
    428  1.1  jtc main(argc, argv)
    429  1.1  jtc int	argc;
    430  1.1  jtc char *	argv[];
    431  1.1  jtc {
    432  1.1  jtc 	register int	i;
    433  1.1  jtc 	register int	j;
    434  1.1  jtc 	register int	c;
    435  1.1  jtc 
    436  1.1  jtc #ifdef unix
    437  1.1  jtc 	(void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
    438  1.1  jtc #endif /* defined unix */
    439  1.1  jtc 	progname = argv[0];
    440  1.1  jtc 	while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF)
    441  1.1  jtc 		switch (c) {
    442  1.1  jtc 			default:
    443  1.1  jtc 				usage();
    444  1.1  jtc 			case 'd':
    445  1.1  jtc 				if (directory == NULL)
    446  1.1  jtc 					directory = optarg;
    447  1.1  jtc 				else {
    448  1.1  jtc 					(void) fprintf(stderr,
    449  1.1  jtc "%s: More than one -d option specified\n",
    450  1.1  jtc 						progname);
    451  1.1  jtc 					(void) exit(EXIT_FAILURE);
    452  1.1  jtc 				}
    453  1.1  jtc 				break;
    454  1.1  jtc 			case 'l':
    455  1.1  jtc 				if (lcltime == NULL)
    456  1.1  jtc 					lcltime = optarg;
    457  1.1  jtc 				else {
    458  1.1  jtc 					(void) fprintf(stderr,
    459  1.1  jtc "%s: More than one -l option specified\n",
    460  1.1  jtc 						progname);
    461  1.1  jtc 					(void) exit(EXIT_FAILURE);
    462  1.1  jtc 				}
    463  1.1  jtc 				break;
    464  1.1  jtc 			case 'p':
    465  1.1  jtc 				if (psxrules == NULL)
    466  1.1  jtc 					psxrules = optarg;
    467  1.1  jtc 				else {
    468  1.1  jtc 					(void) fprintf(stderr,
    469  1.1  jtc "%s: More than one -p option specified\n",
    470  1.1  jtc 						progname);
    471  1.1  jtc 					(void) exit(EXIT_FAILURE);
    472  1.1  jtc 				}
    473  1.1  jtc 				break;
    474  1.1  jtc 			case 'y':
    475  1.1  jtc 				if (yitcommand == NULL)
    476  1.1  jtc 					yitcommand = optarg;
    477  1.1  jtc 				else {
    478  1.1  jtc 					(void) fprintf(stderr,
    479  1.1  jtc "%s: More than one -y option specified\n",
    480  1.1  jtc 						progname);
    481  1.1  jtc 					(void) exit(EXIT_FAILURE);
    482  1.1  jtc 				}
    483  1.1  jtc 				break;
    484  1.1  jtc 			case 'L':
    485  1.1  jtc 				if (leapsec == NULL)
    486  1.1  jtc 					leapsec = optarg;
    487  1.1  jtc 				else {
    488  1.1  jtc 					(void) fprintf(stderr,
    489  1.1  jtc "%s: More than one -L option specified\n",
    490  1.1  jtc 						progname);
    491  1.1  jtc 					(void) exit(EXIT_FAILURE);
    492  1.1  jtc 				}
    493  1.1  jtc 				break;
    494  1.1  jtc 			case 'v':
    495  1.1  jtc 				noise = TRUE;
    496  1.1  jtc 				break;
    497  1.1  jtc 			case 's':
    498  1.1  jtc 				sflag = TRUE;
    499  1.1  jtc 				break;
    500  1.1  jtc 		}
    501  1.1  jtc 	if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
    502  1.1  jtc 		usage();	/* usage message by request */
    503  1.1  jtc 	if (directory == NULL)
    504  1.1  jtc 		directory = TZDIR;
    505  1.1  jtc 	if (yitcommand == NULL)
    506  1.1  jtc 		yitcommand = "yearistype";
    507  1.1  jtc 
    508  1.1  jtc 	setboundaries();
    509  1.1  jtc 
    510  1.1  jtc 	if (optind < argc && leapsec != NULL) {
    511  1.1  jtc 		infile(leapsec);
    512  1.1  jtc 		adjleap();
    513  1.1  jtc 	}
    514  1.1  jtc 
    515  1.1  jtc 	for (i = optind; i < argc; ++i)
    516  1.1  jtc 		infile(argv[i]);
    517  1.1  jtc 	if (errors)
    518  1.1  jtc 		(void) exit(EXIT_FAILURE);
    519  1.1  jtc 	associate();
    520  1.1  jtc 	for (i = 0; i < nzones; i = j) {
    521  1.1  jtc 		/*
    522  1.1  jtc 		** Find the next non-continuation zone entry.
    523  1.1  jtc 		*/
    524  1.1  jtc 		for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
    525  1.1  jtc 			continue;
    526  1.1  jtc 		outzone(&zones[i], j - i);
    527  1.1  jtc 	}
    528  1.1  jtc 	/*
    529  1.1  jtc 	** Make links.
    530  1.1  jtc 	*/
    531  1.1  jtc 	for (i = 0; i < nlinks; ++i)
    532  1.1  jtc 		dolink(links[i].l_from, links[i].l_to);
    533  1.1  jtc 	if (lcltime != NULL)
    534  1.1  jtc 		dolink(lcltime, TZDEFAULT);
    535  1.1  jtc 	if (psxrules != NULL)
    536  1.1  jtc 		dolink(psxrules, TZDEFRULES);
    537  1.1  jtc 	return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    538  1.1  jtc }
    539  1.1  jtc 
    540  1.1  jtc static void
    541  1.1  jtc dolink(fromfile, tofile)
    542  1.1  jtc const char * const	fromfile;
    543  1.1  jtc const char * const	tofile;
    544  1.1  jtc {
    545  1.1  jtc 	register char *	fromname;
    546  1.1  jtc 	register char *	toname;
    547  1.1  jtc 
    548  1.1  jtc 	if (fromfile[0] == '/')
    549  1.1  jtc 		fromname = ecpyalloc(fromfile);
    550  1.1  jtc 	else {
    551  1.1  jtc 		fromname = ecpyalloc(directory);
    552  1.1  jtc 		fromname = ecatalloc(fromname, "/");
    553  1.1  jtc 		fromname = ecatalloc(fromname, fromfile);
    554  1.1  jtc 	}
    555  1.1  jtc 	if (tofile[0] == '/')
    556  1.1  jtc 		toname = ecpyalloc(tofile);
    557  1.1  jtc 	else {
    558  1.1  jtc 		toname = ecpyalloc(directory);
    559  1.1  jtc 		toname = ecatalloc(toname, "/");
    560  1.1  jtc 		toname = ecatalloc(toname, tofile);
    561  1.1  jtc 	}
    562  1.1  jtc 	/*
    563  1.1  jtc 	** We get to be careful here since
    564  1.1  jtc 	** there's a fair chance of root running us.
    565  1.1  jtc 	*/
    566  1.1  jtc 	if (!itsdir(toname))
    567  1.1  jtc 		(void) remove(toname);
    568  1.1  jtc 	if (link(fromname, toname) != 0) {
    569  1.1  jtc 		if (mkdirs(toname) != 0)
    570  1.1  jtc 			(void) exit(EXIT_FAILURE);
    571  1.1  jtc 		if (link(fromname, toname) != 0) {
    572  1.1  jtc 			(void) fprintf(stderr, "%s: Can't link from %s to ",
    573  1.1  jtc 				progname, fromname);
    574  1.1  jtc 			(void) perror(toname);
    575  1.1  jtc 			(void) exit(EXIT_FAILURE);
    576  1.1  jtc 		}
    577  1.1  jtc 	}
    578  1.1  jtc 	ifree(fromname);
    579  1.1  jtc 	ifree(toname);
    580  1.1  jtc }
    581  1.1  jtc 
    582  1.3  jtc #ifndef INT_MAX
    583  1.3  jtc #define INT_MAX	((int) (((unsigned)~0)>>1))
    584  1.3  jtc #endif /* !defined INT_MAX */
    585  1.3  jtc 
    586  1.3  jtc #ifndef INT_MIN
    587  1.3  jtc #define INT_MIN	((int) ~(((unsigned)~0)>>1))
    588  1.3  jtc #endif /* !defined INT_MIN */
    589  1.3  jtc 
    590  1.3  jtc /*
    591  1.3  jtc ** The tz file format currently allows at most 32-bit quantities.
    592  1.3  jtc ** This restriction should be removed before signed 32-bit values
    593  1.3  jtc ** wrap around in 2038, but unfortunately this will require a
    594  1.3  jtc ** change to the tz file format.
    595  1.3  jtc */
    596  1.3  jtc 
    597  1.3  jtc #define MAX_BITS_IN_FILE	32
    598  1.4  jtc #define TIME_T_BITS_IN_FILE	((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? \
    599  1.4  jtc 					TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
    600  1.3  jtc 
    601  1.1  jtc static void
    602  1.1  jtc setboundaries P((void))
    603  1.1  jtc {
    604  1.4  jtc 	if (TYPE_SIGNED(time_t)) {
    605  1.3  jtc 		min_time = ~ (time_t) 0;
    606  1.3  jtc 		min_time <<= TIME_T_BITS_IN_FILE - 1;
    607  1.3  jtc 		max_time = ~ (time_t) 0 - min_time;
    608  1.1  jtc 		if (sflag)
    609  1.3  jtc 			min_time = 0;
    610  1.1  jtc 	} else {
    611  1.3  jtc 		min_time = 0;
    612  1.3  jtc 		max_time = 2 - sflag;
    613  1.3  jtc 		max_time <<= TIME_T_BITS_IN_FILE - 1;
    614  1.3  jtc 		--max_time;
    615  1.1  jtc 	}
    616  1.1  jtc 	min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
    617  1.1  jtc 	max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
    618  1.1  jtc }
    619  1.1  jtc 
    620  1.1  jtc static int
    621  1.1  jtc itsdir(name)
    622  1.1  jtc const char * const	name;
    623  1.1  jtc {
    624  1.1  jtc 	register char *	myname;
    625  1.1  jtc 	register int	accres;
    626  1.1  jtc 
    627  1.1  jtc 	myname = ecpyalloc(name);
    628  1.1  jtc 	myname = ecatalloc(myname, "/.");
    629  1.1  jtc 	accres = access(myname, F_OK);
    630  1.1  jtc 	ifree(myname);
    631  1.1  jtc 	return accres == 0;
    632  1.1  jtc }
    633  1.1  jtc 
    634  1.1  jtc /*
    635  1.1  jtc ** Associate sets of rules with zones.
    636  1.1  jtc */
    637  1.1  jtc 
    638  1.1  jtc /*
    639  1.1  jtc ** Sort by rule name.
    640  1.1  jtc */
    641  1.1  jtc 
    642  1.1  jtc static int
    643  1.1  jtc rcomp(cp1, cp2)
    644  1.1  jtc const void *	cp1;
    645  1.1  jtc const void *	cp2;
    646  1.1  jtc {
    647  1.1  jtc 	return strcmp(((const struct rule *) cp1)->r_name,
    648  1.1  jtc 		((const struct rule *) cp2)->r_name);
    649  1.1  jtc }
    650  1.1  jtc 
    651  1.1  jtc static void
    652  1.1  jtc associate P((void))
    653  1.1  jtc {
    654  1.1  jtc 	register struct zone *	zp;
    655  1.1  jtc 	register struct rule *	rp;
    656  1.1  jtc 	register int		base, out;
    657  1.1  jtc 	register int		i;
    658  1.1  jtc 
    659  1.1  jtc 	if (nrules != 0)
    660  1.1  jtc 		(void) qsort((void *) rules, (size_t) nrules,
    661  1.1  jtc 			(size_t) sizeof *rules, rcomp);
    662  1.1  jtc 	for (i = 0; i < nzones; ++i) {
    663  1.1  jtc 		zp = &zones[i];
    664  1.1  jtc 		zp->z_rules = NULL;
    665  1.1  jtc 		zp->z_nrules = 0;
    666  1.1  jtc 	}
    667  1.1  jtc 	for (base = 0; base < nrules; base = out) {
    668  1.1  jtc 		rp = &rules[base];
    669  1.1  jtc 		for (out = base + 1; out < nrules; ++out)
    670  1.1  jtc 			if (strcmp(rp->r_name, rules[out].r_name) != 0)
    671  1.1  jtc 				break;
    672  1.1  jtc 		for (i = 0; i < nzones; ++i) {
    673  1.1  jtc 			zp = &zones[i];
    674  1.1  jtc 			if (strcmp(zp->z_rule, rp->r_name) != 0)
    675  1.1  jtc 				continue;
    676  1.1  jtc 			zp->z_rules = rp;
    677  1.1  jtc 			zp->z_nrules = out - base;
    678  1.1  jtc 		}
    679  1.1  jtc 	}
    680  1.1  jtc 	for (i = 0; i < nzones; ++i) {
    681  1.1  jtc 		zp = &zones[i];
    682  1.1  jtc 		if (zp->z_nrules == 0) {
    683  1.1  jtc 			/*
    684  1.1  jtc 			** Maybe we have a local standard time offset.
    685  1.1  jtc 			*/
    686  1.1  jtc 			eat(zp->z_filename, zp->z_linenum);
    687  1.1  jtc 			zp->z_stdoff = gethms(zp->z_rule, "unruly zone", TRUE);
    688  1.1  jtc 			/*
    689  1.1  jtc 			** Note, though, that if there's no rule,
    690  1.1  jtc 			** a '%s' in the format is a bad thing.
    691  1.1  jtc 			*/
    692  1.1  jtc 			if (strchr(zp->z_format, '%') != 0)
    693  1.1  jtc 				error("%s in ruleless zone");
    694  1.1  jtc 		}
    695  1.1  jtc 	}
    696  1.1  jtc 	if (errors)
    697  1.1  jtc 		(void) exit(EXIT_FAILURE);
    698  1.1  jtc }
    699  1.1  jtc 
    700  1.1  jtc static void
    701  1.1  jtc infile(name)
    702  1.1  jtc const char *	name;
    703  1.1  jtc {
    704  1.1  jtc 	register FILE *			fp;
    705  1.1  jtc 	register char **		fields;
    706  1.1  jtc 	register char *			cp;
    707  1.1  jtc 	register const struct lookup *	lp;
    708  1.1  jtc 	register int			nfields;
    709  1.1  jtc 	register int			wantcont;
    710  1.1  jtc 	register int			num;
    711  1.1  jtc 	char				buf[BUFSIZ];
    712  1.1  jtc 
    713  1.1  jtc 	if (strcmp(name, "-") == 0) {
    714  1.1  jtc 		name = "standard input";
    715  1.1  jtc 		fp = stdin;
    716  1.1  jtc 	} else if ((fp = fopen(name, "r")) == NULL) {
    717  1.1  jtc 		(void) fprintf(stderr, "%s: Can't open ", progname);
    718  1.1  jtc 		(void) perror(name);
    719  1.1  jtc 		(void) exit(EXIT_FAILURE);
    720  1.1  jtc 	}
    721  1.1  jtc 	wantcont = FALSE;
    722  1.1  jtc 	for (num = 1; ; ++num) {
    723  1.1  jtc 		eat(name, num);
    724  1.1  jtc 		if (fgets(buf, (int) sizeof buf, fp) != buf)
    725  1.1  jtc 			break;
    726  1.1  jtc 		cp = strchr(buf, '\n');
    727  1.1  jtc 		if (cp == NULL) {
    728  1.1  jtc 			error("line too long");
    729  1.1  jtc 			(void) exit(EXIT_FAILURE);
    730  1.1  jtc 		}
    731  1.1  jtc 		*cp = '\0';
    732  1.1  jtc 		fields = getfields(buf);
    733  1.1  jtc 		nfields = 0;
    734  1.1  jtc 		while (fields[nfields] != NULL) {
    735  1.1  jtc 			static char	nada;
    736  1.1  jtc 
    737  1.3  jtc 			if (strcmp(fields[nfields], "-") == 0)
    738  1.1  jtc 				fields[nfields] = &nada;
    739  1.1  jtc 			++nfields;
    740  1.1  jtc 		}
    741  1.1  jtc 		if (nfields == 0) {
    742  1.1  jtc 			/* nothing to do */
    743  1.1  jtc 		} else if (wantcont) {
    744  1.1  jtc 			wantcont = inzcont(fields, nfields);
    745  1.1  jtc 		} else {
    746  1.1  jtc 			lp = byword(fields[0], line_codes);
    747  1.1  jtc 			if (lp == NULL)
    748  1.1  jtc 				error("input line of unknown type");
    749  1.1  jtc 			else switch ((int) (lp->l_value)) {
    750  1.1  jtc 				case LC_RULE:
    751  1.1  jtc 					inrule(fields, nfields);
    752  1.1  jtc 					wantcont = FALSE;
    753  1.1  jtc 					break;
    754  1.1  jtc 				case LC_ZONE:
    755  1.1  jtc 					wantcont = inzone(fields, nfields);
    756  1.1  jtc 					break;
    757  1.1  jtc 				case LC_LINK:
    758  1.1  jtc 					inlink(fields, nfields);
    759  1.1  jtc 					wantcont = FALSE;
    760  1.1  jtc 					break;
    761  1.1  jtc 				case LC_LEAP:
    762  1.1  jtc 					if (name != leapsec)
    763  1.1  jtc 						(void) fprintf(stderr,
    764  1.1  jtc "%s: Leap line in non leap seconds file %s\n",
    765  1.1  jtc 							progname, name);
    766  1.1  jtc 					else	inleap(fields, nfields);
    767  1.1  jtc 					wantcont = FALSE;
    768  1.1  jtc 					break;
    769  1.1  jtc 				default:	/* "cannot happen" */
    770  1.1  jtc 					(void) fprintf(stderr,
    771  1.1  jtc "%s: panic: Invalid l_value %d\n",
    772  1.1  jtc 						progname, lp->l_value);
    773  1.1  jtc 					(void) exit(EXIT_FAILURE);
    774  1.1  jtc 			}
    775  1.1  jtc 		}
    776  1.1  jtc 		ifree((char *) fields);
    777  1.1  jtc 	}
    778  1.1  jtc 	if (ferror(fp)) {
    779  1.1  jtc 		(void) fprintf(stderr, "%s: Error reading ", progname);
    780  1.1  jtc 		(void) perror(filename);
    781  1.1  jtc 		(void) exit(EXIT_FAILURE);
    782  1.1  jtc 	}
    783  1.1  jtc 	if (fp != stdin && fclose(fp)) {
    784  1.1  jtc 		(void) fprintf(stderr, "%s: Error closing ", progname);
    785  1.1  jtc 		(void) perror(filename);
    786  1.1  jtc 		(void) exit(EXIT_FAILURE);
    787  1.1  jtc 	}
    788  1.1  jtc 	if (wantcont)
    789  1.1  jtc 		error("expected continuation line not found");
    790  1.1  jtc }
    791  1.1  jtc 
    792  1.1  jtc /*
    793  1.1  jtc ** Convert a string of one of the forms
    794  1.1  jtc **	h	-h	hh:mm	-hh:mm	hh:mm:ss	-hh:mm:ss
    795  1.1  jtc ** into a number of seconds.
    796  1.1  jtc ** A null string maps to zero.
    797  1.1  jtc ** Call error with errstring and return zero on errors.
    798  1.1  jtc */
    799  1.1  jtc 
    800  1.1  jtc static long
    801  1.1  jtc gethms(string, errstring, signable)
    802  1.1  jtc const char *		string;
    803  1.1  jtc const char * const	errstring;
    804  1.1  jtc const int		signable;
    805  1.1  jtc {
    806  1.1  jtc 	int	hh, mm, ss, sign;
    807  1.1  jtc 
    808  1.1  jtc 	if (string == NULL || *string == '\0')
    809  1.1  jtc 		return 0;
    810  1.1  jtc 	if (!signable)
    811  1.1  jtc 		sign = 1;
    812  1.1  jtc 	else if (*string == '-') {
    813  1.1  jtc 		sign = -1;
    814  1.1  jtc 		++string;
    815  1.1  jtc 	} else	sign = 1;
    816  1.1  jtc 	if (sscanf(string, scheck(string, "%d"), &hh) == 1)
    817  1.1  jtc 		mm = ss = 0;
    818  1.1  jtc 	else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
    819  1.1  jtc 		ss = 0;
    820  1.1  jtc 	else if (sscanf(string, scheck(string, "%d:%d:%d"),
    821  1.1  jtc 		&hh, &mm, &ss) != 3) {
    822  1.1  jtc 			error(errstring);
    823  1.1  jtc 			return 0;
    824  1.1  jtc 	}
    825  1.1  jtc 	if (hh < 0 || hh >= HOURSPERDAY ||
    826  1.1  jtc 		mm < 0 || mm >= MINSPERHOUR ||
    827  1.1  jtc 		ss < 0 || ss > SECSPERMIN) {
    828  1.1  jtc 			error(errstring);
    829  1.1  jtc 			return 0;
    830  1.1  jtc 	}
    831  1.1  jtc 	return eitol(sign) *
    832  1.1  jtc 		(eitol(hh * MINSPERHOUR + mm) *
    833  1.1  jtc 		eitol(SECSPERMIN) + eitol(ss));
    834  1.1  jtc }
    835  1.1  jtc 
    836  1.1  jtc static void
    837  1.1  jtc inrule(fields, nfields)
    838  1.1  jtc register char ** const	fields;
    839  1.1  jtc const int		nfields;
    840  1.1  jtc {
    841  1.1  jtc 	static struct rule	r;
    842  1.1  jtc 
    843  1.1  jtc 	if (nfields != RULE_FIELDS) {
    844  1.1  jtc 		error("wrong number of fields on Rule line");
    845  1.1  jtc 		return;
    846  1.1  jtc 	}
    847  1.1  jtc 	if (*fields[RF_NAME] == '\0') {
    848  1.1  jtc 		error("nameless rule");
    849  1.1  jtc 		return;
    850  1.1  jtc 	}
    851  1.1  jtc 	r.r_filename = filename;
    852  1.1  jtc 	r.r_linenum = linenum;
    853  1.1  jtc 	r.r_stdoff = gethms(fields[RF_STDOFF], "invalid saved time", TRUE);
    854  1.1  jtc 	rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
    855  1.1  jtc 		fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
    856  1.1  jtc 	r.r_name = ecpyalloc(fields[RF_NAME]);
    857  1.1  jtc 	r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
    858  1.1  jtc 	rules = (struct rule *) (void *) erealloc((char *) rules,
    859  1.1  jtc 		(int) ((nrules + 1) * sizeof *rules));
    860  1.1  jtc 	rules[nrules++] = r;
    861  1.1  jtc }
    862  1.1  jtc 
    863  1.1  jtc static int
    864  1.1  jtc inzone(fields, nfields)
    865  1.1  jtc register char ** const	fields;
    866  1.1  jtc const int		nfields;
    867  1.1  jtc {
    868  1.1  jtc 	register int	i;
    869  1.1  jtc 	static char *	buf;
    870  1.1  jtc 
    871  1.1  jtc 	if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
    872  1.1  jtc 		error("wrong number of fields on Zone line");
    873  1.1  jtc 		return FALSE;
    874  1.1  jtc 	}
    875  1.1  jtc 	if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
    876  1.1  jtc 		buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
    877  1.1  jtc 		(void) sprintf(buf,
    878  1.1  jtc "\"Zone %s\" line and -l option are mutually exclusive",
    879  1.1  jtc 			TZDEFAULT);
    880  1.1  jtc 		error(buf);
    881  1.1  jtc 		return FALSE;
    882  1.1  jtc 	}
    883  1.1  jtc 	if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
    884  1.1  jtc 		buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
    885  1.1  jtc 		(void) sprintf(buf,
    886  1.1  jtc "\"Zone %s\" line and -p option are mutually exclusive",
    887  1.1  jtc 			TZDEFRULES);
    888  1.1  jtc 		error(buf);
    889  1.1  jtc 		return FALSE;
    890  1.1  jtc 	}
    891  1.1  jtc 	for (i = 0; i < nzones; ++i)
    892  1.1  jtc 		if (zones[i].z_name != NULL &&
    893  1.1  jtc 			strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
    894  1.1  jtc 				buf = erealloc(buf, (int) (132 +
    895  1.1  jtc 					strlen(fields[ZF_NAME]) +
    896  1.1  jtc 					strlen(zones[i].z_filename)));
    897  1.1  jtc 				(void) sprintf(buf,
    898  1.1  jtc "duplicate zone name %s (file \"%s\", line %d)",
    899  1.1  jtc 					fields[ZF_NAME],
    900  1.1  jtc 					zones[i].z_filename,
    901  1.1  jtc 					zones[i].z_linenum);
    902  1.1  jtc 				error(buf);
    903  1.1  jtc 				return FALSE;
    904  1.1  jtc 		}
    905  1.1  jtc 	return inzsub(fields, nfields, FALSE);
    906  1.1  jtc }
    907  1.1  jtc 
    908  1.1  jtc static int
    909  1.1  jtc inzcont(fields, nfields)
    910  1.1  jtc register char ** const	fields;
    911  1.1  jtc const int		nfields;
    912  1.1  jtc {
    913  1.1  jtc 	if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
    914  1.1  jtc 		error("wrong number of fields on Zone continuation line");
    915  1.1  jtc 		return FALSE;
    916  1.1  jtc 	}
    917  1.1  jtc 	return inzsub(fields, nfields, TRUE);
    918  1.1  jtc }
    919  1.1  jtc 
    920  1.1  jtc static int
    921  1.1  jtc inzsub(fields, nfields, iscont)
    922  1.1  jtc register char ** const	fields;
    923  1.1  jtc const int		nfields;
    924  1.1  jtc const int		iscont;
    925  1.1  jtc {
    926  1.1  jtc 	register char *		cp;
    927  1.1  jtc 	static struct zone	z;
    928  1.1  jtc 	register int		i_gmtoff, i_rule, i_format;
    929  1.1  jtc 	register int		i_untilyear, i_untilmonth;
    930  1.1  jtc 	register int		i_untilday, i_untiltime;
    931  1.1  jtc 	register int		hasuntil;
    932  1.1  jtc 
    933  1.1  jtc 	if (iscont) {
    934  1.1  jtc 		i_gmtoff = ZFC_GMTOFF;
    935  1.1  jtc 		i_rule = ZFC_RULE;
    936  1.1  jtc 		i_format = ZFC_FORMAT;
    937  1.1  jtc 		i_untilyear = ZFC_TILYEAR;
    938  1.1  jtc 		i_untilmonth = ZFC_TILMONTH;
    939  1.1  jtc 		i_untilday = ZFC_TILDAY;
    940  1.1  jtc 		i_untiltime = ZFC_TILTIME;
    941  1.1  jtc 		z.z_name = NULL;
    942  1.1  jtc 	} else {
    943  1.1  jtc 		i_gmtoff = ZF_GMTOFF;
    944  1.1  jtc 		i_rule = ZF_RULE;
    945  1.1  jtc 		i_format = ZF_FORMAT;
    946  1.1  jtc 		i_untilyear = ZF_TILYEAR;
    947  1.1  jtc 		i_untilmonth = ZF_TILMONTH;
    948  1.1  jtc 		i_untilday = ZF_TILDAY;
    949  1.1  jtc 		i_untiltime = ZF_TILTIME;
    950  1.1  jtc 		z.z_name = ecpyalloc(fields[ZF_NAME]);
    951  1.1  jtc 	}
    952  1.1  jtc 	z.z_filename = filename;
    953  1.1  jtc 	z.z_linenum = linenum;
    954  1.1  jtc 	z.z_gmtoff = gethms(fields[i_gmtoff], "invalid GMT offset", TRUE);
    955  1.1  jtc 	if ((cp = strchr(fields[i_format], '%')) != 0) {
    956  1.1  jtc 		if (*++cp != 's' || strchr(cp, '%') != 0) {
    957  1.1  jtc 			error("invalid abbreviation format");
    958  1.1  jtc 			return FALSE;
    959  1.1  jtc 		}
    960  1.1  jtc 	}
    961  1.1  jtc 	z.z_rule = ecpyalloc(fields[i_rule]);
    962  1.1  jtc 	z.z_format = ecpyalloc(fields[i_format]);
    963  1.1  jtc 	hasuntil = nfields > i_untilyear;
    964  1.1  jtc 	if (hasuntil) {
    965  1.1  jtc 		z.z_untilrule.r_filename = filename;
    966  1.1  jtc 		z.z_untilrule.r_linenum = linenum;
    967  1.1  jtc 		rulesub(&z.z_untilrule,
    968  1.1  jtc 			fields[i_untilyear],
    969  1.1  jtc 			"only",
    970  1.1  jtc 			"",
    971  1.1  jtc 			(nfields > i_untilmonth) ?
    972  1.1  jtc 			fields[i_untilmonth] : "Jan",
    973  1.1  jtc 			(nfields > i_untilday) ? fields[i_untilday] : "1",
    974  1.1  jtc 			(nfields > i_untiltime) ? fields[i_untiltime] : "0");
    975  1.1  jtc 		z.z_untiltime = rpytime(&z.z_untilrule,
    976  1.1  jtc 			z.z_untilrule.r_loyear);
    977  1.1  jtc 		if (iscont && nzones > 0 &&
    978  1.1  jtc 			z.z_untiltime > min_time &&
    979  1.1  jtc 			z.z_untiltime < max_time &&
    980  1.1  jtc 			zones[nzones - 1].z_untiltime > min_time &&
    981  1.1  jtc 			zones[nzones - 1].z_untiltime < max_time &&
    982  1.1  jtc 			zones[nzones - 1].z_untiltime >= z.z_untiltime) {
    983  1.1  jtc 				error("Zone continuation line end time is not \
    984  1.1  jtc after end time of previous line");
    985  1.1  jtc 				return FALSE;
    986  1.1  jtc 		}
    987  1.1  jtc 	}
    988  1.1  jtc 	zones = (struct zone *) (void *) erealloc((char *) zones,
    989  1.1  jtc 		(int) ((nzones + 1) * sizeof *zones));
    990  1.1  jtc 	zones[nzones++] = z;
    991  1.1  jtc 	/*
    992  1.1  jtc 	** If there was an UNTIL field on this line,
    993  1.1  jtc 	** there's more information about the zone on the next line.
    994  1.1  jtc 	*/
    995  1.1  jtc 	return hasuntil;
    996  1.1  jtc }
    997  1.1  jtc 
    998  1.1  jtc static void
    999  1.1  jtc inleap(fields, nfields)
   1000  1.1  jtc register char ** const	fields;
   1001  1.1  jtc const int		nfields;
   1002  1.1  jtc {
   1003  1.1  jtc 	register const char *		cp;
   1004  1.1  jtc 	register const struct lookup *	lp;
   1005  1.1  jtc 	register int			i, j;
   1006  1.1  jtc 	int				year, month, day;
   1007  1.1  jtc 	long				dayoff, tod;
   1008  1.1  jtc 	time_t				t;
   1009  1.1  jtc 
   1010  1.1  jtc 	if (nfields != LEAP_FIELDS) {
   1011  1.1  jtc 		error("wrong number of fields on Leap line");
   1012  1.1  jtc 		return;
   1013  1.1  jtc 	}
   1014  1.1  jtc 	dayoff = 0;
   1015  1.1  jtc 	cp = fields[LP_YEAR];
   1016  1.1  jtc 	if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
   1017  1.1  jtc 			/*
   1018  1.1  jtc 			 * Leapin' Lizards!
   1019  1.1  jtc 			 */
   1020  1.1  jtc 			error("invalid leaping year");
   1021  1.1  jtc 			return;
   1022  1.1  jtc 	}
   1023  1.1  jtc 	j = EPOCH_YEAR;
   1024  1.1  jtc 	while (j != year) {
   1025  1.1  jtc 		if (year > j) {
   1026  1.1  jtc 			i = len_years[isleap(j)];
   1027  1.1  jtc 			++j;
   1028  1.1  jtc 		} else {
   1029  1.1  jtc 			--j;
   1030  1.1  jtc 			i = -len_years[isleap(j)];
   1031  1.1  jtc 		}
   1032  1.1  jtc 		dayoff = oadd(dayoff, eitol(i));
   1033  1.1  jtc 	}
   1034  1.1  jtc 	if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
   1035  1.1  jtc 		error("invalid month name");
   1036  1.1  jtc 		return;
   1037  1.1  jtc 	}
   1038  1.1  jtc 	month = lp->l_value;
   1039  1.1  jtc 	j = TM_JANUARY;
   1040  1.1  jtc 	while (j != month) {
   1041  1.1  jtc 		i = len_months[isleap(year)][j];
   1042  1.1  jtc 		dayoff = oadd(dayoff, eitol(i));
   1043  1.1  jtc 		++j;
   1044  1.1  jtc 	}
   1045  1.1  jtc 	cp = fields[LP_DAY];
   1046  1.1  jtc 	if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
   1047  1.1  jtc 		day <= 0 || day > len_months[isleap(year)][month]) {
   1048  1.1  jtc 			error("invalid day of month");
   1049  1.1  jtc 			return;
   1050  1.1  jtc 	}
   1051  1.1  jtc 	dayoff = oadd(dayoff, eitol(day - 1));
   1052  1.4  jtc 	if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
   1053  1.1  jtc 		error("time before zero");
   1054  1.1  jtc 		return;
   1055  1.1  jtc 	}
   1056  1.1  jtc 	t = (time_t) dayoff * SECSPERDAY;
   1057  1.1  jtc 	/*
   1058  1.1  jtc 	** Cheap overflow check.
   1059  1.1  jtc 	*/
   1060  1.1  jtc 	if (t / SECSPERDAY != dayoff) {
   1061  1.1  jtc 		error("time overflow");
   1062  1.1  jtc 		return;
   1063  1.1  jtc 	}
   1064  1.1  jtc 	tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
   1065  1.1  jtc 	cp = fields[LP_CORR];
   1066  1.1  jtc 	{
   1067  1.1  jtc 		register int	positive;
   1068  1.1  jtc 		int		count;
   1069  1.1  jtc 
   1070  1.1  jtc 		if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
   1071  1.1  jtc 			positive = FALSE;
   1072  1.1  jtc 			count = 1;
   1073  1.1  jtc 		} else if (strcmp(cp, "--") == 0) {
   1074  1.1  jtc 			positive = FALSE;
   1075  1.1  jtc 			count = 2;
   1076  1.1  jtc 		} else if (strcmp(cp, "+") == 0) {
   1077  1.1  jtc 			positive = TRUE;
   1078  1.1  jtc 			count = 1;
   1079  1.1  jtc 		} else if (strcmp(cp, "++") == 0) {
   1080  1.1  jtc 			positive = TRUE;
   1081  1.1  jtc 			count = 2;
   1082  1.1  jtc 		} else {
   1083  1.1  jtc 			error("illegal CORRECTION field on Leap line");
   1084  1.1  jtc 			return;
   1085  1.1  jtc 		}
   1086  1.1  jtc 		if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
   1087  1.1  jtc 			error("illegal Rolling/Stationary field on Leap line");
   1088  1.1  jtc 			return;
   1089  1.1  jtc 		}
   1090  1.1  jtc 		leapadd(tadd(t, tod), positive, lp->l_value, count);
   1091  1.1  jtc 	}
   1092  1.1  jtc }
   1093  1.1  jtc 
   1094  1.1  jtc static void
   1095  1.1  jtc inlink(fields, nfields)
   1096  1.1  jtc register char ** const	fields;
   1097  1.1  jtc const int		nfields;
   1098  1.1  jtc {
   1099  1.1  jtc 	struct link	l;
   1100  1.1  jtc 
   1101  1.1  jtc 	if (nfields != LINK_FIELDS) {
   1102  1.1  jtc 		error("wrong number of fields on Link line");
   1103  1.1  jtc 		return;
   1104  1.1  jtc 	}
   1105  1.1  jtc 	if (*fields[LF_FROM] == '\0') {
   1106  1.1  jtc 		error("blank FROM field on Link line");
   1107  1.1  jtc 		return;
   1108  1.1  jtc 	}
   1109  1.1  jtc 	if (*fields[LF_TO] == '\0') {
   1110  1.1  jtc 		error("blank TO field on Link line");
   1111  1.1  jtc 		return;
   1112  1.1  jtc 	}
   1113  1.1  jtc 	l.l_filename = filename;
   1114  1.1  jtc 	l.l_linenum = linenum;
   1115  1.1  jtc 	l.l_from = ecpyalloc(fields[LF_FROM]);
   1116  1.1  jtc 	l.l_to = ecpyalloc(fields[LF_TO]);
   1117  1.1  jtc 	links = (struct link *) (void *) erealloc((char *) links,
   1118  1.1  jtc 		(int) ((nlinks + 1) * sizeof *links));
   1119  1.1  jtc 	links[nlinks++] = l;
   1120  1.1  jtc }
   1121  1.1  jtc 
   1122  1.1  jtc static void
   1123  1.1  jtc rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
   1124  1.1  jtc register struct rule * const	rp;
   1125  1.1  jtc const char * const		loyearp;
   1126  1.1  jtc const char * const		hiyearp;
   1127  1.1  jtc const char * const		typep;
   1128  1.1  jtc const char * const		monthp;
   1129  1.1  jtc const char * const		dayp;
   1130  1.1  jtc const char * const		timep;
   1131  1.1  jtc {
   1132  1.1  jtc 	register const struct lookup *	lp;
   1133  1.1  jtc 	register const char *		cp;
   1134  1.1  jtc 	register char *			dp;
   1135  1.1  jtc 	register char *			ep;
   1136  1.1  jtc 
   1137  1.1  jtc 	if ((lp = byword(monthp, mon_names)) == NULL) {
   1138  1.1  jtc 		error("invalid month name");
   1139  1.1  jtc 		return;
   1140  1.1  jtc 	}
   1141  1.1  jtc 	rp->r_month = lp->l_value;
   1142  1.1  jtc 	rp->r_todisstd = FALSE;
   1143  1.1  jtc 	rp->r_todisgmt = FALSE;
   1144  1.1  jtc 	dp = ecpyalloc(timep);
   1145  1.1  jtc 	if (*dp != '\0') {
   1146  1.1  jtc 		ep = dp + strlen(dp) - 1;
   1147  1.1  jtc 		switch (lowerit(*ep)) {
   1148  1.1  jtc 			case 's':	/* Standard */
   1149  1.1  jtc 				rp->r_todisstd = TRUE;
   1150  1.1  jtc 				rp->r_todisgmt = FALSE;
   1151  1.1  jtc 				*ep = '\0';
   1152  1.1  jtc 				break;
   1153  1.1  jtc 			case 'w':	/* Wall */
   1154  1.1  jtc 				rp->r_todisstd = FALSE;
   1155  1.1  jtc 				rp->r_todisgmt = FALSE;
   1156  1.1  jtc 				*ep = '\0';
   1157  1.1  jtc 			case 'g':	/* Greenwich */
   1158  1.1  jtc 			case 'u':	/* Universal */
   1159  1.1  jtc 			case 'z':	/* Zulu */
   1160  1.1  jtc 				rp->r_todisstd = TRUE;
   1161  1.1  jtc 				rp->r_todisgmt = TRUE;
   1162  1.1  jtc 				*ep = '\0';
   1163  1.1  jtc 				break;
   1164  1.1  jtc 		}
   1165  1.1  jtc 	}
   1166  1.1  jtc 	rp->r_tod = gethms(dp, "invalid time of day", FALSE);
   1167  1.1  jtc 	ifree(dp);
   1168  1.1  jtc 	/*
   1169  1.1  jtc 	** Year work.
   1170  1.1  jtc 	*/
   1171  1.1  jtc 	cp = loyearp;
   1172  1.1  jtc 	lp = byword(cp, begin_years);
   1173  1.1  jtc 	if (lp != NULL) switch ((int) lp->l_value) {
   1174  1.1  jtc 		case YR_MINIMUM:
   1175  1.3  jtc 			rp->r_loyear = INT_MIN;
   1176  1.1  jtc 			break;
   1177  1.1  jtc 		case YR_MAXIMUM:
   1178  1.3  jtc 			rp->r_loyear = INT_MAX;
   1179  1.1  jtc 			break;
   1180  1.1  jtc 		default:	/* "cannot happen" */
   1181  1.1  jtc 			(void) fprintf(stderr,
   1182  1.1  jtc 				"%s: panic: Invalid l_value %d\n",
   1183  1.1  jtc 				progname, lp->l_value);
   1184  1.1  jtc 			(void) exit(EXIT_FAILURE);
   1185  1.1  jtc 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
   1186  1.1  jtc 		error("invalid starting year");
   1187  1.1  jtc 		return;
   1188  1.1  jtc 	}
   1189  1.1  jtc 	cp = hiyearp;
   1190  1.1  jtc 	if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
   1191  1.1  jtc 		case YR_MINIMUM:
   1192  1.3  jtc 			rp->r_hiyear = INT_MIN;
   1193  1.1  jtc 			break;
   1194  1.1  jtc 		case YR_MAXIMUM:
   1195  1.3  jtc 			rp->r_hiyear = INT_MAX;
   1196  1.1  jtc 			break;
   1197  1.1  jtc 		case YR_ONLY:
   1198  1.1  jtc 			rp->r_hiyear = rp->r_loyear;
   1199  1.1  jtc 			break;
   1200  1.1  jtc 		default:	/* "cannot happen" */
   1201  1.1  jtc 			(void) fprintf(stderr,
   1202  1.1  jtc 				"%s: panic: Invalid l_value %d\n",
   1203  1.1  jtc 				progname, lp->l_value);
   1204  1.1  jtc 			(void) exit(EXIT_FAILURE);
   1205  1.1  jtc 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
   1206  1.1  jtc 		error("invalid ending year");
   1207  1.1  jtc 		return;
   1208  1.1  jtc 	}
   1209  1.1  jtc 	if (rp->r_loyear > rp->r_hiyear) {
   1210  1.1  jtc 		error("starting year greater than ending year");
   1211  1.1  jtc 		return;
   1212  1.1  jtc 	}
   1213  1.1  jtc 	if (*typep == '\0')
   1214  1.1  jtc 		rp->r_yrtype = NULL;
   1215  1.1  jtc 	else {
   1216  1.1  jtc 		if (rp->r_loyear == rp->r_hiyear) {
   1217  1.1  jtc 			error("typed single year");
   1218  1.1  jtc 			return;
   1219  1.1  jtc 		}
   1220  1.1  jtc 		rp->r_yrtype = ecpyalloc(typep);
   1221  1.1  jtc 	}
   1222  1.1  jtc 	/*
   1223  1.1  jtc 	** Day work.
   1224  1.1  jtc 	** Accept things such as:
   1225  1.1  jtc 	**	1
   1226  1.1  jtc 	**	last-Sunday
   1227  1.1  jtc 	**	Sun<=20
   1228  1.1  jtc 	**	Sun>=7
   1229  1.1  jtc 	*/
   1230  1.1  jtc 	dp = ecpyalloc(dayp);
   1231  1.1  jtc 	if ((lp = byword(dp, lasts)) != NULL) {
   1232  1.1  jtc 		rp->r_dycode = DC_DOWLEQ;
   1233  1.1  jtc 		rp->r_wday = lp->l_value;
   1234  1.1  jtc 		rp->r_dayofmonth = len_months[1][rp->r_month];
   1235  1.1  jtc 	} else {
   1236  1.1  jtc 		if ((ep = strchr(dp, '<')) != 0)
   1237  1.1  jtc 			rp->r_dycode = DC_DOWLEQ;
   1238  1.1  jtc 		else if ((ep = strchr(dp, '>')) != 0)
   1239  1.1  jtc 			rp->r_dycode = DC_DOWGEQ;
   1240  1.1  jtc 		else {
   1241  1.1  jtc 			ep = dp;
   1242  1.1  jtc 			rp->r_dycode = DC_DOM;
   1243  1.1  jtc 		}
   1244  1.1  jtc 		if (rp->r_dycode != DC_DOM) {
   1245  1.1  jtc 			*ep++ = 0;
   1246  1.1  jtc 			if (*ep++ != '=') {
   1247  1.1  jtc 				error("invalid day of month");
   1248  1.1  jtc 				ifree(dp);
   1249  1.1  jtc 				return;
   1250  1.1  jtc 			}
   1251  1.1  jtc 			if ((lp = byword(dp, wday_names)) == NULL) {
   1252  1.1  jtc 				error("invalid weekday name");
   1253  1.1  jtc 				ifree(dp);
   1254  1.1  jtc 				return;
   1255  1.1  jtc 			}
   1256  1.1  jtc 			rp->r_wday = lp->l_value;
   1257  1.1  jtc 		}
   1258  1.1  jtc 		if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
   1259  1.1  jtc 			rp->r_dayofmonth <= 0 ||
   1260  1.1  jtc 			(rp->r_dayofmonth > len_months[1][rp->r_month])) {
   1261  1.1  jtc 				error("invalid day of month");
   1262  1.1  jtc 				ifree(dp);
   1263  1.1  jtc 				return;
   1264  1.1  jtc 		}
   1265  1.1  jtc 	}
   1266  1.1  jtc 	ifree(dp);
   1267  1.1  jtc }
   1268  1.1  jtc 
   1269  1.1  jtc static void
   1270  1.1  jtc convert(val, buf)
   1271  1.1  jtc const long	val;
   1272  1.1  jtc char * const	buf;
   1273  1.1  jtc {
   1274  1.1  jtc 	register int	i;
   1275  1.1  jtc 	register long	shift;
   1276  1.1  jtc 
   1277  1.1  jtc 	for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
   1278  1.1  jtc 		buf[i] = val >> shift;
   1279  1.1  jtc }
   1280  1.1  jtc 
   1281  1.1  jtc static void
   1282  1.1  jtc puttzcode(val, fp)
   1283  1.1  jtc const long	val;
   1284  1.1  jtc FILE * const	fp;
   1285  1.1  jtc {
   1286  1.1  jtc 	char	buf[4];
   1287  1.1  jtc 
   1288  1.1  jtc 	convert(val, buf);
   1289  1.1  jtc 	(void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
   1290  1.1  jtc }
   1291  1.1  jtc 
   1292  1.1  jtc static void
   1293  1.1  jtc writezone(name)
   1294  1.1  jtc const char * const	name;
   1295  1.1  jtc {
   1296  1.1  jtc 	register FILE *		fp;
   1297  1.1  jtc 	register int		i, j;
   1298  1.1  jtc 	static char *		fullname;
   1299  1.1  jtc 	static struct tzhead	tzh;
   1300  1.1  jtc 
   1301  1.1  jtc 	fullname = erealloc(fullname,
   1302  1.1  jtc 		(int) (strlen(directory) + 1 + strlen(name) + 1));
   1303  1.1  jtc 	(void) sprintf(fullname, "%s/%s", directory, name);
   1304  1.1  jtc 	if ((fp = fopen(fullname, "wb")) == NULL) {
   1305  1.1  jtc 		if (mkdirs(fullname) != 0)
   1306  1.1  jtc 			(void) exit(EXIT_FAILURE);
   1307  1.1  jtc 		if ((fp = fopen(fullname, "wb")) == NULL) {
   1308  1.1  jtc 			(void) fprintf(stderr, "%s: Can't create ", progname);
   1309  1.1  jtc 			(void) perror(fullname);
   1310  1.1  jtc 			(void) exit(EXIT_FAILURE);
   1311  1.1  jtc 		}
   1312  1.1  jtc 	}
   1313  1.1  jtc 	convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
   1314  1.1  jtc 	convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
   1315  1.1  jtc 	convert(eitol(leapcnt), tzh.tzh_leapcnt);
   1316  1.1  jtc 	convert(eitol(timecnt), tzh.tzh_timecnt);
   1317  1.1  jtc 	convert(eitol(typecnt), tzh.tzh_typecnt);
   1318  1.1  jtc 	convert(eitol(charcnt), tzh.tzh_charcnt);
   1319  1.1  jtc #define DO(field)	(void) fwrite((void *) tzh.field, \
   1320  1.1  jtc 		(size_t) sizeof tzh.field, (size_t) 1, fp)
   1321  1.1  jtc 	DO(tzh_reserved);
   1322  1.1  jtc 	DO(tzh_ttisgmtcnt);
   1323  1.1  jtc 	DO(tzh_ttisstdcnt);
   1324  1.1  jtc 	DO(tzh_leapcnt);
   1325  1.1  jtc 	DO(tzh_timecnt);
   1326  1.1  jtc 	DO(tzh_typecnt);
   1327  1.1  jtc 	DO(tzh_charcnt);
   1328  1.1  jtc #undef DO
   1329  1.1  jtc 	for (i = 0; i < timecnt; ++i) {
   1330  1.1  jtc 		j = leapcnt;
   1331  1.1  jtc 		while (--j >= 0)
   1332  1.1  jtc 			if (ats[i] >= trans[j]) {
   1333  1.1  jtc 				ats[i] = tadd(ats[i], corr[j]);
   1334  1.1  jtc 				break;
   1335  1.1  jtc 			}
   1336  1.1  jtc 		puttzcode((long) ats[i], fp);
   1337  1.1  jtc 	}
   1338  1.1  jtc 	if (timecnt > 0)
   1339  1.1  jtc 		(void) fwrite((void *) types, (size_t) sizeof types[0],
   1340  1.1  jtc 			(size_t) timecnt, fp);
   1341  1.1  jtc 	for (i = 0; i < typecnt; ++i) {
   1342  1.1  jtc 		puttzcode((long) gmtoffs[i], fp);
   1343  1.1  jtc 		(void) putc(isdsts[i], fp);
   1344  1.1  jtc 		(void) putc(abbrinds[i], fp);
   1345  1.1  jtc 	}
   1346  1.1  jtc 	if (charcnt != 0)
   1347  1.1  jtc 		(void) fwrite((void *) chars, (size_t) sizeof chars[0],
   1348  1.1  jtc 			(size_t) charcnt, fp);
   1349  1.1  jtc 	for (i = 0; i < leapcnt; ++i) {
   1350  1.1  jtc 		if (roll[i]) {
   1351  1.1  jtc 			if (timecnt == 0 || trans[i] < ats[0]) {
   1352  1.1  jtc 				j = 0;
   1353  1.1  jtc 				while (isdsts[j])
   1354  1.1  jtc 					if (++j >= typecnt) {
   1355  1.1  jtc 						j = 0;
   1356  1.1  jtc 						break;
   1357  1.1  jtc 					}
   1358  1.1  jtc 			} else {
   1359  1.1  jtc 				j = 1;
   1360  1.1  jtc 				while (j < timecnt && trans[i] >= ats[j])
   1361  1.1  jtc 					++j;
   1362  1.1  jtc 				j = types[j - 1];
   1363  1.1  jtc 			}
   1364  1.1  jtc 			puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
   1365  1.1  jtc 		} else	puttzcode((long) trans[i], fp);
   1366  1.1  jtc 		puttzcode((long) corr[i], fp);
   1367  1.1  jtc 	}
   1368  1.1  jtc 	for (i = 0; i < typecnt; ++i)
   1369  1.1  jtc 		(void) putc(ttisstds[i], fp);
   1370  1.1  jtc 	for (i = 0; i < typecnt; ++i)
   1371  1.1  jtc 		(void) putc(ttisgmts[i], fp);
   1372  1.1  jtc 	if (ferror(fp) || fclose(fp)) {
   1373  1.1  jtc 		(void) fprintf(stderr, "%s: Write error on ", progname);
   1374  1.1  jtc 		(void) perror(fullname);
   1375  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1376  1.1  jtc 	}
   1377  1.1  jtc }
   1378  1.1  jtc 
   1379  1.1  jtc static void
   1380  1.1  jtc doabbr(abbr, format, letters, isdst)
   1381  1.1  jtc char * const		abbr;
   1382  1.1  jtc const char * const	format;
   1383  1.1  jtc const char * const	letters;
   1384  1.1  jtc const int		isdst;
   1385  1.1  jtc {
   1386  1.1  jtc 	if (strchr(format, '/') == NULL) {
   1387  1.1  jtc 		if (letters == NULL)
   1388  1.1  jtc 			(void) strcpy(abbr, format);
   1389  1.1  jtc 		else	(void) sprintf(abbr, format, letters);
   1390  1.1  jtc 	} else if (isdst)
   1391  1.1  jtc 		(void) strcpy(abbr, strchr(format, '/') + 1);
   1392  1.1  jtc 	else {
   1393  1.1  jtc 		(void) strcpy(abbr, format);
   1394  1.1  jtc 		*strchr(abbr, '/') = '\0';
   1395  1.1  jtc 	}
   1396  1.1  jtc }
   1397  1.1  jtc 
   1398  1.1  jtc static void
   1399  1.1  jtc outzone(zpfirst, zonecount)
   1400  1.1  jtc const struct zone * const	zpfirst;
   1401  1.1  jtc const int			zonecount;
   1402  1.1  jtc {
   1403  1.1  jtc 	register const struct zone *	zp;
   1404  1.1  jtc 	register struct rule *		rp;
   1405  1.1  jtc 	register int			i, j;
   1406  1.1  jtc 	register int			usestart, useuntil;
   1407  1.1  jtc 	register time_t			starttime, untiltime;
   1408  1.1  jtc 	register long			gmtoff;
   1409  1.1  jtc 	register long			stdoff;
   1410  1.1  jtc 	register int			year;
   1411  1.1  jtc 	register long			startoff;
   1412  1.1  jtc 	register int			startisdst;
   1413  1.1  jtc 	register int			startttisstd;
   1414  1.1  jtc 	register int			startttisgmt;
   1415  1.1  jtc 	register int			type;
   1416  1.1  jtc 	char				startbuf[BUFSIZ];
   1417  1.1  jtc 
   1418  1.1  jtc 	INITIALIZE(untiltime);
   1419  1.1  jtc 	INITIALIZE(starttime);
   1420  1.1  jtc 	INITIALIZE(startoff);
   1421  1.1  jtc 	/*
   1422  1.1  jtc 	** Now. . .finally. . .generate some useful data!
   1423  1.1  jtc 	*/
   1424  1.1  jtc 	timecnt = 0;
   1425  1.1  jtc 	typecnt = 0;
   1426  1.1  jtc 	charcnt = 0;
   1427  1.1  jtc 	/*
   1428  1.1  jtc 	** A guess that may well be corrected later.
   1429  1.1  jtc 	*/
   1430  1.1  jtc 	stdoff = 0;
   1431  1.1  jtc 	/*
   1432  1.1  jtc 	** Thanks to Earl Chew (earl (at) dnd.icp.nec.com.au)
   1433  1.1  jtc 	** for noting the need to unconditionally initialize startttisstd.
   1434  1.1  jtc 	*/
   1435  1.1  jtc 	startttisstd = FALSE;
   1436  1.1  jtc 	startttisgmt = FALSE;
   1437  1.1  jtc 	for (i = 0; i < zonecount; ++i) {
   1438  1.1  jtc 		zp = &zpfirst[i];
   1439  1.1  jtc 		usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
   1440  1.1  jtc 		useuntil = i < (zonecount - 1);
   1441  1.1  jtc 		if (useuntil && zp->z_untiltime <= min_time)
   1442  1.1  jtc 			continue;
   1443  1.1  jtc 		gmtoff = zp->z_gmtoff;
   1444  1.1  jtc 		eat(zp->z_filename, zp->z_linenum);
   1445  1.1  jtc 		startisdst = -1;
   1446  1.1  jtc 		if (zp->z_nrules == 0) {
   1447  1.1  jtc 			stdoff = zp->z_stdoff;
   1448  1.1  jtc 			doabbr(startbuf, zp->z_format,
   1449  1.1  jtc 				(char *) NULL, stdoff != 0);
   1450  1.1  jtc 			type = addtype(oadd(zp->z_gmtoff, stdoff),
   1451  1.1  jtc 				startbuf, stdoff != 0, startttisstd,
   1452  1.1  jtc 				startttisgmt);
   1453  1.1  jtc 			if (usestart)
   1454  1.1  jtc 				addtt(starttime, type);
   1455  1.1  jtc 			else if (stdoff != 0)
   1456  1.1  jtc 				addtt(min_time, type);
   1457  1.1  jtc 		} else for (year = min_year; year <= max_year; ++year) {
   1458  1.1  jtc 			if (useuntil && year > zp->z_untilrule.r_hiyear)
   1459  1.1  jtc 				break;
   1460  1.1  jtc 			/*
   1461  1.1  jtc 			** Mark which rules to do in the current year.
   1462  1.1  jtc 			** For those to do, calculate rpytime(rp, year);
   1463  1.1  jtc 			*/
   1464  1.1  jtc 			for (j = 0; j < zp->z_nrules; ++j) {
   1465  1.1  jtc 				rp = &zp->z_rules[j];
   1466  1.1  jtc 				eats(zp->z_filename, zp->z_linenum,
   1467  1.1  jtc 					rp->r_filename, rp->r_linenum);
   1468  1.1  jtc 				rp->r_todo = year >= rp->r_loyear &&
   1469  1.1  jtc 						year <= rp->r_hiyear &&
   1470  1.1  jtc 						yearistype(year, rp->r_yrtype);
   1471  1.1  jtc 				if (rp->r_todo)
   1472  1.1  jtc 					rp->r_temp = rpytime(rp, year);
   1473  1.1  jtc 			}
   1474  1.1  jtc 			for ( ; ; ) {
   1475  1.1  jtc 				register int	k;
   1476  1.1  jtc 				register time_t	jtime, ktime;
   1477  1.1  jtc 				register long	offset;
   1478  1.1  jtc 				char		buf[BUFSIZ];
   1479  1.1  jtc 
   1480  1.1  jtc 				INITIALIZE(ktime);
   1481  1.1  jtc 				if (useuntil) {
   1482  1.1  jtc 					/*
   1483  1.1  jtc 					** Turn untiltime into GMT
   1484  1.1  jtc 					** assuming the current gmtoff and
   1485  1.1  jtc 					** stdoff values.
   1486  1.1  jtc 					*/
   1487  1.1  jtc 					untiltime = zp->z_untiltime;
   1488  1.1  jtc 					if (!zp->z_untilrule.r_todisgmt)
   1489  1.1  jtc 						untiltime = tadd(untiltime,
   1490  1.1  jtc 							-gmtoff);
   1491  1.1  jtc 					if (!zp->z_untilrule.r_todisstd)
   1492  1.1  jtc 						untiltime = tadd(untiltime,
   1493  1.1  jtc 							-stdoff);
   1494  1.1  jtc 				}
   1495  1.1  jtc 				/*
   1496  1.1  jtc 				** Find the rule (of those to do, if any)
   1497  1.1  jtc 				** that takes effect earliest in the year.
   1498  1.1  jtc 				*/
   1499  1.1  jtc 				k = -1;
   1500  1.1  jtc 				for (j = 0; j < zp->z_nrules; ++j) {
   1501  1.1  jtc 					rp = &zp->z_rules[j];
   1502  1.1  jtc 					if (!rp->r_todo)
   1503  1.1  jtc 						continue;
   1504  1.1  jtc 					eats(zp->z_filename, zp->z_linenum,
   1505  1.1  jtc 						rp->r_filename, rp->r_linenum);
   1506  1.1  jtc 					offset = rp->r_todisgmt ? 0 : gmtoff;
   1507  1.1  jtc 					if (!rp->r_todisstd)
   1508  1.1  jtc 						offset = oadd(offset, stdoff);
   1509  1.1  jtc 					jtime = rp->r_temp;
   1510  1.1  jtc 					if (jtime == min_time ||
   1511  1.1  jtc 						jtime == max_time)
   1512  1.1  jtc 							continue;
   1513  1.1  jtc 					jtime = tadd(jtime, -offset);
   1514  1.1  jtc 					if (k < 0 || jtime < ktime) {
   1515  1.1  jtc 						k = j;
   1516  1.1  jtc 						ktime = jtime;
   1517  1.1  jtc 					}
   1518  1.1  jtc 				}
   1519  1.1  jtc 				if (k < 0)
   1520  1.1  jtc 					break;	/* go on to next year */
   1521  1.1  jtc 				rp = &zp->z_rules[k];
   1522  1.1  jtc 				rp->r_todo = FALSE;
   1523  1.1  jtc 				if (useuntil && ktime >= untiltime)
   1524  1.1  jtc 					break;
   1525  1.1  jtc 				if (usestart) {
   1526  1.1  jtc 				    if (ktime < starttime) {
   1527  1.1  jtc 					stdoff = rp->r_stdoff;
   1528  1.1  jtc 					startoff = oadd(zp->z_gmtoff,
   1529  1.1  jtc 						rp->r_stdoff);
   1530  1.1  jtc 					doabbr(startbuf, zp->z_format,
   1531  1.1  jtc 						rp->r_abbrvar,
   1532  1.1  jtc 						rp->r_stdoff != 0);
   1533  1.1  jtc 					startisdst = rp->r_stdoff != 0;
   1534  1.1  jtc 					continue;
   1535  1.1  jtc 				    }
   1536  1.1  jtc 				    usestart = FALSE;
   1537  1.1  jtc 				    if (ktime != starttime) {
   1538  1.1  jtc 					if (startisdst < 0 &&
   1539  1.1  jtc 					    zp->z_gmtoff !=
   1540  1.1  jtc 					    (zp - 1)->z_gmtoff) {
   1541  1.1  jtc 						type = (timecnt == 0) ? 0 :
   1542  1.1  jtc 							types[timecnt - 1];
   1543  1.1  jtc 						startoff = oadd(gmtoffs[type],
   1544  1.1  jtc 							-(zp - 1)->z_gmtoff);
   1545  1.1  jtc 						startisdst = startoff != 0;
   1546  1.1  jtc 						startoff = oadd(startoff,
   1547  1.1  jtc 							zp->z_gmtoff);
   1548  1.1  jtc 						(void) strcpy(startbuf,
   1549  1.1  jtc 						    &chars[abbrinds[type]]);
   1550  1.1  jtc 					}
   1551  1.1  jtc 					if (startisdst >= 0)
   1552  1.1  jtc addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd,
   1553  1.1  jtc 	startttisgmt));
   1554  1.1  jtc 				    }
   1555  1.1  jtc 				}
   1556  1.1  jtc 				eats(zp->z_filename, zp->z_linenum,
   1557  1.1  jtc 					rp->r_filename, rp->r_linenum);
   1558  1.1  jtc 				doabbr(buf, zp->z_format, rp->r_abbrvar,
   1559  1.1  jtc 					rp->r_stdoff != 0);
   1560  1.1  jtc 				offset = oadd(zp->z_gmtoff, rp->r_stdoff);
   1561  1.1  jtc 				type = addtype(offset, buf, rp->r_stdoff != 0,
   1562  1.1  jtc 					rp->r_todisstd, rp->r_todisgmt);
   1563  1.1  jtc 				addtt(ktime, type);
   1564  1.1  jtc 				stdoff = rp->r_stdoff;
   1565  1.1  jtc 			}
   1566  1.1  jtc 		}
   1567  1.1  jtc 		/*
   1568  1.1  jtc 		** Now we may get to set starttime for the next zone line.
   1569  1.1  jtc 		*/
   1570  1.1  jtc 		if (useuntil) {
   1571  1.1  jtc 			starttime = tadd(zp->z_untiltime, -gmtoff);
   1572  1.1  jtc 			startttisstd = zp->z_untilrule.r_todisstd;
   1573  1.1  jtc 			startttisgmt = zp->z_untilrule.r_todisgmt;
   1574  1.1  jtc 			if (!startttisstd)
   1575  1.1  jtc 				starttime = tadd(starttime, -stdoff);
   1576  1.1  jtc 		}
   1577  1.1  jtc 	}
   1578  1.1  jtc 	writezone(zpfirst->z_name);
   1579  1.1  jtc }
   1580  1.1  jtc 
   1581  1.1  jtc static void
   1582  1.1  jtc addtt(starttime, type)
   1583  1.1  jtc const time_t	starttime;
   1584  1.1  jtc const int	type;
   1585  1.1  jtc {
   1586  1.1  jtc 	if (timecnt != 0 && type == types[timecnt - 1])
   1587  1.1  jtc 		return;	/* easy enough! */
   1588  1.1  jtc 	if (timecnt == 0 && type == 0 && isdsts[0] == 0)
   1589  1.1  jtc 		return; /* handled by default rule */
   1590  1.1  jtc 	if (timecnt >= TZ_MAX_TIMES) {
   1591  1.1  jtc 		error("too many transitions?!");
   1592  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1593  1.1  jtc 	}
   1594  1.1  jtc 	ats[timecnt] = starttime;
   1595  1.1  jtc 	types[timecnt] = type;
   1596  1.1  jtc 	++timecnt;
   1597  1.1  jtc }
   1598  1.1  jtc 
   1599  1.1  jtc static int
   1600  1.1  jtc addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
   1601  1.1  jtc const long		gmtoff;
   1602  1.1  jtc const char * const	abbr;
   1603  1.1  jtc const int		isdst;
   1604  1.1  jtc const int		ttisstd;
   1605  1.1  jtc const int		ttisgmt;
   1606  1.1  jtc {
   1607  1.1  jtc 	register int	i, j;
   1608  1.1  jtc 
   1609  1.1  jtc 	/*
   1610  1.1  jtc 	** See if there's already an entry for this zone type.
   1611  1.1  jtc 	** If so, just return its index.
   1612  1.1  jtc 	*/
   1613  1.1  jtc 	for (i = 0; i < typecnt; ++i) {
   1614  1.1  jtc 		if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
   1615  1.1  jtc 			strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
   1616  1.1  jtc 			ttisstd == ttisstds[i] &&
   1617  1.1  jtc 			ttisgmt == ttisgmts[i])
   1618  1.1  jtc 				return i;
   1619  1.1  jtc 	}
   1620  1.1  jtc 	/*
   1621  1.1  jtc 	** There isn't one; add a new one, unless there are already too
   1622  1.1  jtc 	** many.
   1623  1.1  jtc 	*/
   1624  1.1  jtc 	if (typecnt >= TZ_MAX_TYPES) {
   1625  1.1  jtc 		error("too many local time types");
   1626  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1627  1.1  jtc 	}
   1628  1.1  jtc 	gmtoffs[i] = gmtoff;
   1629  1.1  jtc 	isdsts[i] = isdst;
   1630  1.1  jtc 	ttisstds[i] = ttisstd;
   1631  1.1  jtc 	ttisgmts[i] = ttisgmt;
   1632  1.1  jtc 
   1633  1.1  jtc 	for (j = 0; j < charcnt; ++j)
   1634  1.1  jtc 		if (strcmp(&chars[j], abbr) == 0)
   1635  1.1  jtc 			break;
   1636  1.1  jtc 	if (j == charcnt)
   1637  1.1  jtc 		newabbr(abbr);
   1638  1.1  jtc 	abbrinds[i] = j;
   1639  1.1  jtc 	++typecnt;
   1640  1.1  jtc 	return i;
   1641  1.1  jtc }
   1642  1.1  jtc 
   1643  1.1  jtc static void
   1644  1.1  jtc leapadd(t, positive, rolling, count)
   1645  1.1  jtc const time_t	t;
   1646  1.1  jtc const int	positive;
   1647  1.1  jtc const int	rolling;
   1648  1.1  jtc int		count;
   1649  1.1  jtc {
   1650  1.1  jtc 	register int	i, j;
   1651  1.1  jtc 
   1652  1.1  jtc 	if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
   1653  1.1  jtc 		error("too many leap seconds");
   1654  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1655  1.1  jtc 	}
   1656  1.1  jtc 	for (i = 0; i < leapcnt; ++i)
   1657  1.1  jtc 		if (t <= trans[i]) {
   1658  1.1  jtc 			if (t == trans[i]) {
   1659  1.1  jtc 				error("repeated leap second moment");
   1660  1.1  jtc 				(void) exit(EXIT_FAILURE);
   1661  1.1  jtc 			}
   1662  1.1  jtc 			break;
   1663  1.1  jtc 		}
   1664  1.1  jtc 	do {
   1665  1.1  jtc 		for (j = leapcnt; j > i; --j) {
   1666  1.1  jtc 			trans[j] = trans[j - 1];
   1667  1.1  jtc 			corr[j] = corr[j - 1];
   1668  1.1  jtc 			roll[j] = roll[j - 1];
   1669  1.1  jtc 		}
   1670  1.1  jtc 		trans[i] = t;
   1671  1.1  jtc 		corr[i] = positive ? 1L : eitol(-count);
   1672  1.1  jtc 		roll[i] = rolling;
   1673  1.1  jtc 		++leapcnt;
   1674  1.1  jtc 	} while (positive && --count != 0);
   1675  1.1  jtc }
   1676  1.1  jtc 
   1677  1.1  jtc static void
   1678  1.1  jtc adjleap P((void))
   1679  1.1  jtc {
   1680  1.1  jtc 	register int	i;
   1681  1.1  jtc 	register long	last = 0;
   1682  1.1  jtc 
   1683  1.1  jtc 	/*
   1684  1.1  jtc 	** propagate leap seconds forward
   1685  1.1  jtc 	*/
   1686  1.1  jtc 	for (i = 0; i < leapcnt; ++i) {
   1687  1.1  jtc 		trans[i] = tadd(trans[i], last);
   1688  1.1  jtc 		last = corr[i] += last;
   1689  1.1  jtc 	}
   1690  1.1  jtc }
   1691  1.1  jtc 
   1692  1.1  jtc static int
   1693  1.1  jtc yearistype(year, type)
   1694  1.1  jtc const int		year;
   1695  1.1  jtc const char * const	type;
   1696  1.1  jtc {
   1697  1.1  jtc 	static char *	buf;
   1698  1.1  jtc 	int		result;
   1699  1.1  jtc 
   1700  1.1  jtc 	if (type == NULL || *type == '\0')
   1701  1.1  jtc 		return TRUE;
   1702  1.1  jtc 	buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
   1703  1.1  jtc 	(void) sprintf(buf, "%s %d %s", yitcommand, year, type);
   1704  1.1  jtc 	result = system(buf);
   1705  1.1  jtc 	if (result == 0)
   1706  1.1  jtc 		return TRUE;
   1707  1.1  jtc 	if (result == (1 << 8))
   1708  1.1  jtc 		return FALSE;
   1709  1.1  jtc 	error("Wild result from command execution");
   1710  1.1  jtc 	(void) fprintf(stderr, "%s: command was '%s', result was %d\n",
   1711  1.1  jtc 		progname, buf, result);
   1712  1.1  jtc 	for ( ; ; )
   1713  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1714  1.1  jtc }
   1715  1.1  jtc 
   1716  1.1  jtc static int
   1717  1.1  jtc lowerit(a)
   1718  1.3  jtc int	a;
   1719  1.1  jtc {
   1720  1.3  jtc 	a = (unsigned char) a;
   1721  1.1  jtc 	return (isascii(a) && isupper(a)) ? tolower(a) : a;
   1722  1.1  jtc }
   1723  1.1  jtc 
   1724  1.1  jtc static int
   1725  1.1  jtc ciequal(ap, bp)		/* case-insensitive equality */
   1726  1.1  jtc register const char *	ap;
   1727  1.1  jtc register const char *	bp;
   1728  1.1  jtc {
   1729  1.1  jtc 	while (lowerit(*ap) == lowerit(*bp++))
   1730  1.1  jtc 		if (*ap++ == '\0')
   1731  1.1  jtc 			return TRUE;
   1732  1.1  jtc 	return FALSE;
   1733  1.1  jtc }
   1734  1.1  jtc 
   1735  1.1  jtc static int
   1736  1.1  jtc itsabbr(abbr, word)
   1737  1.1  jtc register const char *	abbr;
   1738  1.1  jtc register const char *	word;
   1739  1.1  jtc {
   1740  1.1  jtc 	if (lowerit(*abbr) != lowerit(*word))
   1741  1.1  jtc 		return FALSE;
   1742  1.1  jtc 	++word;
   1743  1.1  jtc 	while (*++abbr != '\0')
   1744  1.3  jtc 		do {
   1745  1.3  jtc 			if (*word == '\0')
   1746  1.3  jtc 				return FALSE;
   1747  1.3  jtc 		} while (lowerit(*word++) != lowerit(*abbr));
   1748  1.1  jtc 	return TRUE;
   1749  1.1  jtc }
   1750  1.1  jtc 
   1751  1.1  jtc static const struct lookup *
   1752  1.1  jtc byword(word, table)
   1753  1.1  jtc register const char * const		word;
   1754  1.1  jtc register const struct lookup * const	table;
   1755  1.1  jtc {
   1756  1.1  jtc 	register const struct lookup *	foundlp;
   1757  1.1  jtc 	register const struct lookup *	lp;
   1758  1.1  jtc 
   1759  1.1  jtc 	if (word == NULL || table == NULL)
   1760  1.1  jtc 		return NULL;
   1761  1.1  jtc 	/*
   1762  1.1  jtc 	** Look for exact match.
   1763  1.1  jtc 	*/
   1764  1.1  jtc 	for (lp = table; lp->l_word != NULL; ++lp)
   1765  1.1  jtc 		if (ciequal(word, lp->l_word))
   1766  1.1  jtc 			return lp;
   1767  1.1  jtc 	/*
   1768  1.1  jtc 	** Look for inexact match.
   1769  1.1  jtc 	*/
   1770  1.1  jtc 	foundlp = NULL;
   1771  1.1  jtc 	for (lp = table; lp->l_word != NULL; ++lp)
   1772  1.1  jtc 		if (itsabbr(word, lp->l_word))
   1773  1.1  jtc 			if (foundlp == NULL)
   1774  1.1  jtc 				foundlp = lp;
   1775  1.1  jtc 			else	return NULL;	/* multiple inexact matches */
   1776  1.1  jtc 	return foundlp;
   1777  1.1  jtc }
   1778  1.1  jtc 
   1779  1.1  jtc static char **
   1780  1.1  jtc getfields(cp)
   1781  1.1  jtc register char *	cp;
   1782  1.1  jtc {
   1783  1.1  jtc 	register char *		dp;
   1784  1.1  jtc 	register char **	array;
   1785  1.1  jtc 	register int		nsubs;
   1786  1.1  jtc 
   1787  1.1  jtc 	if (cp == NULL)
   1788  1.1  jtc 		return NULL;
   1789  1.1  jtc 	array = (char **) (void *)
   1790  1.1  jtc 		emalloc((int) ((strlen(cp) + 1) * sizeof *array));
   1791  1.1  jtc 	nsubs = 0;
   1792  1.1  jtc 	for ( ; ; ) {
   1793  1.3  jtc 		while (isascii(*cp) && isspace((unsigned char) *cp))
   1794  1.1  jtc 			++cp;
   1795  1.1  jtc 		if (*cp == '\0' || *cp == '#')
   1796  1.1  jtc 			break;
   1797  1.1  jtc 		array[nsubs++] = dp = cp;
   1798  1.1  jtc 		do {
   1799  1.1  jtc 			if ((*dp = *cp++) != '"')
   1800  1.1  jtc 				++dp;
   1801  1.1  jtc 			else while ((*dp = *cp++) != '"')
   1802  1.1  jtc 				if (*dp != '\0')
   1803  1.1  jtc 					++dp;
   1804  1.1  jtc 				else	error("Odd number of quotation marks");
   1805  1.1  jtc 		} while (*cp != '\0' && *cp != '#' &&
   1806  1.3  jtc 			(!isascii(*cp) || !isspace((unsigned char) *cp)));
   1807  1.3  jtc 		if (isascii(*cp) && isspace((unsigned char) *cp))
   1808  1.1  jtc 			++cp;
   1809  1.1  jtc 		*dp = '\0';
   1810  1.1  jtc 	}
   1811  1.1  jtc 	array[nsubs] = NULL;
   1812  1.1  jtc 	return array;
   1813  1.1  jtc }
   1814  1.1  jtc 
   1815  1.1  jtc static long
   1816  1.1  jtc oadd(t1, t2)
   1817  1.1  jtc const long	t1;
   1818  1.1  jtc const long	t2;
   1819  1.1  jtc {
   1820  1.1  jtc 	register long	t;
   1821  1.1  jtc 
   1822  1.1  jtc 	t = t1 + t2;
   1823  1.1  jtc 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
   1824  1.1  jtc 		error("time overflow");
   1825  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1826  1.1  jtc 	}
   1827  1.1  jtc 	return t;
   1828  1.1  jtc }
   1829  1.1  jtc 
   1830  1.1  jtc static time_t
   1831  1.1  jtc tadd(t1, t2)
   1832  1.1  jtc const time_t	t1;
   1833  1.1  jtc const long	t2;
   1834  1.1  jtc {
   1835  1.1  jtc 	register time_t	t;
   1836  1.1  jtc 
   1837  1.1  jtc 	if (t1 == max_time && t2 > 0)
   1838  1.1  jtc 		return max_time;
   1839  1.1  jtc 	if (t1 == min_time && t2 < 0)
   1840  1.1  jtc 		return min_time;
   1841  1.1  jtc 	t = t1 + t2;
   1842  1.1  jtc 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
   1843  1.1  jtc 		error("time overflow");
   1844  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1845  1.1  jtc 	}
   1846  1.1  jtc 	return t;
   1847  1.1  jtc }
   1848  1.1  jtc 
   1849  1.1  jtc /*
   1850  1.1  jtc ** Given a rule, and a year, compute the date - in seconds since January 1,
   1851  1.1  jtc ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
   1852  1.1  jtc */
   1853  1.1  jtc 
   1854  1.1  jtc static time_t
   1855  1.1  jtc rpytime(rp, wantedy)
   1856  1.1  jtc register const struct rule * const	rp;
   1857  1.1  jtc register const int			wantedy;
   1858  1.1  jtc {
   1859  1.1  jtc 	register int	y, m, i;
   1860  1.1  jtc 	register long	dayoff;			/* with a nod to Margaret O. */
   1861  1.1  jtc 	register time_t	t;
   1862  1.1  jtc 
   1863  1.3  jtc 	if (wantedy == INT_MIN)
   1864  1.1  jtc 		return min_time;
   1865  1.3  jtc 	if (wantedy == INT_MAX)
   1866  1.1  jtc 		return max_time;
   1867  1.1  jtc 	dayoff = 0;
   1868  1.1  jtc 	m = TM_JANUARY;
   1869  1.1  jtc 	y = EPOCH_YEAR;
   1870  1.1  jtc 	while (wantedy != y) {
   1871  1.1  jtc 		if (wantedy > y) {
   1872  1.1  jtc 			i = len_years[isleap(y)];
   1873  1.1  jtc 			++y;
   1874  1.1  jtc 		} else {
   1875  1.1  jtc 			--y;
   1876  1.1  jtc 			i = -len_years[isleap(y)];
   1877  1.1  jtc 		}
   1878  1.1  jtc 		dayoff = oadd(dayoff, eitol(i));
   1879  1.1  jtc 	}
   1880  1.1  jtc 	while (m != rp->r_month) {
   1881  1.1  jtc 		i = len_months[isleap(y)][m];
   1882  1.1  jtc 		dayoff = oadd(dayoff, eitol(i));
   1883  1.1  jtc 		++m;
   1884  1.1  jtc 	}
   1885  1.1  jtc 	i = rp->r_dayofmonth;
   1886  1.1  jtc 	if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
   1887  1.1  jtc 		if (rp->r_dycode == DC_DOWLEQ)
   1888  1.1  jtc 			--i;
   1889  1.1  jtc 		else {
   1890  1.1  jtc 			error("use of 2/29 in non leap-year");
   1891  1.1  jtc 			(void) exit(EXIT_FAILURE);
   1892  1.1  jtc 		}
   1893  1.1  jtc 	}
   1894  1.1  jtc 	--i;
   1895  1.1  jtc 	dayoff = oadd(dayoff, eitol(i));
   1896  1.1  jtc 	if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
   1897  1.1  jtc 		register long	wday;
   1898  1.1  jtc 
   1899  1.1  jtc #define LDAYSPERWEEK	((long) DAYSPERWEEK)
   1900  1.1  jtc 		wday = eitol(EPOCH_WDAY);
   1901  1.1  jtc 		/*
   1902  1.1  jtc 		** Don't trust mod of negative numbers.
   1903  1.1  jtc 		*/
   1904  1.1  jtc 		if (dayoff >= 0)
   1905  1.1  jtc 			wday = (wday + dayoff) % LDAYSPERWEEK;
   1906  1.1  jtc 		else {
   1907  1.1  jtc 			wday -= ((-dayoff) % LDAYSPERWEEK);
   1908  1.1  jtc 			if (wday < 0)
   1909  1.1  jtc 				wday += LDAYSPERWEEK;
   1910  1.1  jtc 		}
   1911  1.1  jtc 		while (wday != eitol(rp->r_wday))
   1912  1.1  jtc 			if (rp->r_dycode == DC_DOWGEQ) {
   1913  1.1  jtc 				dayoff = oadd(dayoff, (long) 1);
   1914  1.1  jtc 				if (++wday >= LDAYSPERWEEK)
   1915  1.1  jtc 					wday = 0;
   1916  1.1  jtc 				++i;
   1917  1.1  jtc 			} else {
   1918  1.1  jtc 				dayoff = oadd(dayoff, (long) -1);
   1919  1.1  jtc 				if (--wday < 0)
   1920  1.1  jtc 					wday = LDAYSPERWEEK - 1;
   1921  1.1  jtc 				--i;
   1922  1.1  jtc 			}
   1923  1.1  jtc 		if (i < 0 || i >= len_months[isleap(y)][m]) {
   1924  1.1  jtc 			error("no day in month matches rule");
   1925  1.1  jtc 			(void) exit(EXIT_FAILURE);
   1926  1.1  jtc 		}
   1927  1.1  jtc 	}
   1928  1.4  jtc 	if (dayoff < 0 && !TYPE_SIGNED(time_t))
   1929  1.1  jtc 		return min_time;
   1930  1.1  jtc 	t = (time_t) dayoff * SECSPERDAY;
   1931  1.1  jtc 	/*
   1932  1.1  jtc 	** Cheap overflow check.
   1933  1.1  jtc 	*/
   1934  1.1  jtc 	if (t / SECSPERDAY != dayoff)
   1935  1.1  jtc 		return (dayoff > 0) ? max_time : min_time;
   1936  1.1  jtc 	return tadd(t, rp->r_tod);
   1937  1.1  jtc }
   1938  1.1  jtc 
   1939  1.1  jtc static void
   1940  1.1  jtc newabbr(string)
   1941  1.1  jtc const char * const	string;
   1942  1.1  jtc {
   1943  1.1  jtc 	register int	i;
   1944  1.1  jtc 
   1945  1.1  jtc 	i = strlen(string) + 1;
   1946  1.1  jtc 	if (charcnt + i > TZ_MAX_CHARS) {
   1947  1.1  jtc 		error("too many, or too long, time zone abbreviations");
   1948  1.1  jtc 		(void) exit(EXIT_FAILURE);
   1949  1.1  jtc 	}
   1950  1.1  jtc 	(void) strcpy(&chars[charcnt], string);
   1951  1.1  jtc 	charcnt += eitol(i);
   1952  1.1  jtc }
   1953  1.1  jtc 
   1954  1.1  jtc static int
   1955  1.1  jtc mkdirs(argname)
   1956  1.1  jtc char * const	argname;
   1957  1.1  jtc {
   1958  1.1  jtc 	register char *	name;
   1959  1.1  jtc 	register char *	cp;
   1960  1.1  jtc 
   1961  1.1  jtc 	if (argname == NULL || *argname == '\0')
   1962  1.1  jtc 		return 0;
   1963  1.1  jtc 	cp = name = ecpyalloc(argname);
   1964  1.1  jtc 	while ((cp = strchr(cp + 1, '/')) != 0) {
   1965  1.1  jtc 		*cp = '\0';
   1966  1.1  jtc #ifndef unix
   1967  1.1  jtc 		/*
   1968  1.1  jtc 		** DOS drive specifier?
   1969  1.1  jtc 		*/
   1970  1.3  jtc 		if (isalpha((unsigned char) name[0]) &&
   1971  1.4  jtc 			name[1] == ':' && name[2] == '\0') {
   1972  1.1  jtc 				*cp = '/';
   1973  1.1  jtc 				continue;
   1974  1.1  jtc 		}
   1975  1.1  jtc #endif /* !defined unix */
   1976  1.1  jtc 		if (!itsdir(name)) {
   1977  1.1  jtc 			/*
   1978  1.1  jtc 			** It doesn't seem to exist, so we try to create it.
   1979  1.1  jtc 			*/
   1980  1.1  jtc 			if (mkdir(name, 0755) != 0) {
   1981  1.1  jtc 				(void) fprintf(stderr,
   1982  1.1  jtc 					"%s: Can't create directory ",
   1983  1.1  jtc 					progname);
   1984  1.1  jtc 				(void) perror(name);
   1985  1.1  jtc 				ifree(name);
   1986  1.1  jtc 				return -1;
   1987  1.1  jtc 			}
   1988  1.1  jtc 		}
   1989  1.1  jtc 		*cp = '/';
   1990  1.1  jtc 	}
   1991  1.1  jtc 	ifree(name);
   1992  1.1  jtc 	return 0;
   1993  1.1  jtc }
   1994  1.1  jtc 
   1995  1.1  jtc static long
   1996  1.1  jtc eitol(i)
   1997  1.1  jtc const int	i;
   1998  1.1  jtc {
   1999  1.1  jtc 	long	l;
   2000  1.1  jtc 
   2001  1.1  jtc 	l = i;
   2002  1.1  jtc 	if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
   2003  1.1  jtc 		(void) fprintf(stderr,
   2004  1.1  jtc 			"%s: %d did not sign extend correctly\n",
   2005  1.1  jtc 			progname, i);
   2006  1.1  jtc 		(void) exit(EXIT_FAILURE);
   2007  1.1  jtc 	}
   2008  1.1  jtc 	return l;
   2009  1.1  jtc }
   2010  1.1  jtc 
   2011  1.1  jtc /*
   2012  1.1  jtc ** UNIX was a registered trademark of UNIX System Laboratories in 1993.
   2013  1.1  jtc */
   2014