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