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