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