Home | History | Annotate | Line # | Download | only in time
zic.c revision 1.1.1.15
      1 static char	elsieid[] = "@(#)zic.c	7.116";
      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 	if (noise && hh == HOURSPERDAY)
    935 		warning(_("24:00 not handled by pre-1998 versions of zic"));
    936 	return eitol(sign) *
    937 		(eitol(hh * MINSPERHOUR + mm) *
    938 		eitol(SECSPERMIN) + eitol(ss));
    939 }
    940 
    941 static void
    942 inrule(fields, nfields)
    943 register char ** const	fields;
    944 const int		nfields;
    945 {
    946 	static struct rule	r;
    947 
    948 	if (nfields != RULE_FIELDS) {
    949 		error(_("wrong number of fields on Rule line"));
    950 		return;
    951 	}
    952 	if (*fields[RF_NAME] == '\0') {
    953 		error(_("nameless rule"));
    954 		return;
    955 	}
    956 	r.r_filename = filename;
    957 	r.r_linenum = linenum;
    958 	r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
    959 	rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
    960 		fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
    961 	r.r_name = ecpyalloc(fields[RF_NAME]);
    962 	r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
    963 	rules = (struct rule *) (void *) erealloc((char *) rules,
    964 		(int) ((nrules + 1) * sizeof *rules));
    965 	rules[nrules++] = r;
    966 }
    967 
    968 static int
    969 inzone(fields, nfields)
    970 register char ** const	fields;
    971 const int		nfields;
    972 {
    973 	register int	i;
    974 	static char *	buf;
    975 
    976 	if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
    977 		error(_("wrong number of fields on Zone line"));
    978 		return FALSE;
    979 	}
    980 	if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
    981 		buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
    982 		(void) sprintf(buf,
    983 _("\"Zone %s\" line and -l option are mutually exclusive"),
    984 			TZDEFAULT);
    985 		error(buf);
    986 		return FALSE;
    987 	}
    988 	if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
    989 		buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
    990 		(void) sprintf(buf,
    991 _("\"Zone %s\" line and -p option are mutually exclusive"),
    992 			TZDEFRULES);
    993 		error(buf);
    994 		return FALSE;
    995 	}
    996 	for (i = 0; i < nzones; ++i)
    997 		if (zones[i].z_name != NULL &&
    998 			strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
    999 				buf = erealloc(buf, (int) (132 +
   1000 					strlen(fields[ZF_NAME]) +
   1001 					strlen(zones[i].z_filename)));
   1002 				(void) sprintf(buf,
   1003 _("duplicate zone name %s (file \"%s\", line %d)"),
   1004 					fields[ZF_NAME],
   1005 					zones[i].z_filename,
   1006 					zones[i].z_linenum);
   1007 				error(buf);
   1008 				return FALSE;
   1009 		}
   1010 	return inzsub(fields, nfields, FALSE);
   1011 }
   1012 
   1013 static int
   1014 inzcont(fields, nfields)
   1015 register char ** const	fields;
   1016 const int		nfields;
   1017 {
   1018 	if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
   1019 		error(_("wrong number of fields on Zone continuation line"));
   1020 		return FALSE;
   1021 	}
   1022 	return inzsub(fields, nfields, TRUE);
   1023 }
   1024 
   1025 static int
   1026 inzsub(fields, nfields, iscont)
   1027 register char ** const	fields;
   1028 const int		nfields;
   1029 const int		iscont;
   1030 {
   1031 	register char *		cp;
   1032 	static struct zone	z;
   1033 	register int		i_gmtoff, i_rule, i_format;
   1034 	register int		i_untilyear, i_untilmonth;
   1035 	register int		i_untilday, i_untiltime;
   1036 	register int		hasuntil;
   1037 
   1038 	if (iscont) {
   1039 		i_gmtoff = ZFC_GMTOFF;
   1040 		i_rule = ZFC_RULE;
   1041 		i_format = ZFC_FORMAT;
   1042 		i_untilyear = ZFC_TILYEAR;
   1043 		i_untilmonth = ZFC_TILMONTH;
   1044 		i_untilday = ZFC_TILDAY;
   1045 		i_untiltime = ZFC_TILTIME;
   1046 		z.z_name = NULL;
   1047 	} else {
   1048 		i_gmtoff = ZF_GMTOFF;
   1049 		i_rule = ZF_RULE;
   1050 		i_format = ZF_FORMAT;
   1051 		i_untilyear = ZF_TILYEAR;
   1052 		i_untilmonth = ZF_TILMONTH;
   1053 		i_untilday = ZF_TILDAY;
   1054 		i_untiltime = ZF_TILTIME;
   1055 		z.z_name = ecpyalloc(fields[ZF_NAME]);
   1056 	}
   1057 	z.z_filename = filename;
   1058 	z.z_linenum = linenum;
   1059 	z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
   1060 	if ((cp = strchr(fields[i_format], '%')) != 0) {
   1061 		if (*++cp != 's' || strchr(cp, '%') != 0) {
   1062 			error(_("invalid abbreviation format"));
   1063 			return FALSE;
   1064 		}
   1065 	}
   1066 	z.z_rule = ecpyalloc(fields[i_rule]);
   1067 	z.z_format = ecpyalloc(fields[i_format]);
   1068 	hasuntil = nfields > i_untilyear;
   1069 	if (hasuntil) {
   1070 		z.z_untilrule.r_filename = filename;
   1071 		z.z_untilrule.r_linenum = linenum;
   1072 		rulesub(&z.z_untilrule,
   1073 			fields[i_untilyear],
   1074 			"only",
   1075 			"",
   1076 			(nfields > i_untilmonth) ?
   1077 			fields[i_untilmonth] : "Jan",
   1078 			(nfields > i_untilday) ? fields[i_untilday] : "1",
   1079 			(nfields > i_untiltime) ? fields[i_untiltime] : "0");
   1080 		z.z_untiltime = rpytime(&z.z_untilrule,
   1081 			z.z_untilrule.r_loyear);
   1082 		if (iscont && nzones > 0 &&
   1083 			z.z_untiltime > min_time &&
   1084 			z.z_untiltime < max_time &&
   1085 			zones[nzones - 1].z_untiltime > min_time &&
   1086 			zones[nzones - 1].z_untiltime < max_time &&
   1087 			zones[nzones - 1].z_untiltime >= z.z_untiltime) {
   1088 				error(_("Zone continuation line end time is not after end time of previous line"));
   1089 				return FALSE;
   1090 		}
   1091 	}
   1092 	zones = (struct zone *) (void *) erealloc((char *) zones,
   1093 		(int) ((nzones + 1) * sizeof *zones));
   1094 	zones[nzones++] = z;
   1095 	/*
   1096 	** If there was an UNTIL field on this line,
   1097 	** there's more information about the zone on the next line.
   1098 	*/
   1099 	return hasuntil;
   1100 }
   1101 
   1102 static void
   1103 inleap(fields, nfields)
   1104 register char ** const	fields;
   1105 const int		nfields;
   1106 {
   1107 	register const char *		cp;
   1108 	register const struct lookup *	lp;
   1109 	register int			i, j;
   1110 	int				year, month, day;
   1111 	long				dayoff, tod;
   1112 	time_t				t;
   1113 
   1114 	if (nfields != LEAP_FIELDS) {
   1115 		error(_("wrong number of fields on Leap line"));
   1116 		return;
   1117 	}
   1118 	dayoff = 0;
   1119 	cp = fields[LP_YEAR];
   1120 	if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
   1121 			/*
   1122 			 * Leapin' Lizards!
   1123 			 */
   1124 			error(_("invalid leaping year"));
   1125 			return;
   1126 	}
   1127 	j = EPOCH_YEAR;
   1128 	while (j != year) {
   1129 		if (year > j) {
   1130 			i = len_years[isleap(j)];
   1131 			++j;
   1132 		} else {
   1133 			--j;
   1134 			i = -len_years[isleap(j)];
   1135 		}
   1136 		dayoff = oadd(dayoff, eitol(i));
   1137 	}
   1138 	if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
   1139 		error(_("invalid month name"));
   1140 		return;
   1141 	}
   1142 	month = lp->l_value;
   1143 	j = TM_JANUARY;
   1144 	while (j != month) {
   1145 		i = len_months[isleap(year)][j];
   1146 		dayoff = oadd(dayoff, eitol(i));
   1147 		++j;
   1148 	}
   1149 	cp = fields[LP_DAY];
   1150 	if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
   1151 		day <= 0 || day > len_months[isleap(year)][month]) {
   1152 			error(_("invalid day of month"));
   1153 			return;
   1154 	}
   1155 	dayoff = oadd(dayoff, eitol(day - 1));
   1156 	if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
   1157 		error(_("time before zero"));
   1158 		return;
   1159 	}
   1160 	if (dayoff < min_time / SECSPERDAY) {
   1161 		error(_("time too small"));
   1162 		return;
   1163 	}
   1164 	if (dayoff > max_time / SECSPERDAY) {
   1165 		error(_("time too large"));
   1166 		return;
   1167 	}
   1168 	t = (time_t) dayoff * SECSPERDAY;
   1169 	tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
   1170 	cp = fields[LP_CORR];
   1171 	{
   1172 		register int	positive;
   1173 		int		count;
   1174 
   1175 		if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
   1176 			positive = FALSE;
   1177 			count = 1;
   1178 		} else if (strcmp(cp, "--") == 0) {
   1179 			positive = FALSE;
   1180 			count = 2;
   1181 		} else if (strcmp(cp, "+") == 0) {
   1182 			positive = TRUE;
   1183 			count = 1;
   1184 		} else if (strcmp(cp, "++") == 0) {
   1185 			positive = TRUE;
   1186 			count = 2;
   1187 		} else {
   1188 			error(_("illegal CORRECTION field on Leap line"));
   1189 			return;
   1190 		}
   1191 		if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
   1192 			error(_("illegal Rolling/Stationary field on Leap line"));
   1193 			return;
   1194 		}
   1195 		leapadd(tadd(t, tod), positive, lp->l_value, count);
   1196 	}
   1197 }
   1198 
   1199 static void
   1200 inlink(fields, nfields)
   1201 register char ** const	fields;
   1202 const int		nfields;
   1203 {
   1204 	struct link	l;
   1205 
   1206 	if (nfields != LINK_FIELDS) {
   1207 		error(_("wrong number of fields on Link line"));
   1208 		return;
   1209 	}
   1210 	if (*fields[LF_FROM] == '\0') {
   1211 		error(_("blank FROM field on Link line"));
   1212 		return;
   1213 	}
   1214 	if (*fields[LF_TO] == '\0') {
   1215 		error(_("blank TO field on Link line"));
   1216 		return;
   1217 	}
   1218 	l.l_filename = filename;
   1219 	l.l_linenum = linenum;
   1220 	l.l_from = ecpyalloc(fields[LF_FROM]);
   1221 	l.l_to = ecpyalloc(fields[LF_TO]);
   1222 	links = (struct link *) (void *) erealloc((char *) links,
   1223 		(int) ((nlinks + 1) * sizeof *links));
   1224 	links[nlinks++] = l;
   1225 }
   1226 
   1227 static void
   1228 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
   1229 register struct rule * const	rp;
   1230 const char * const		loyearp;
   1231 const char * const		hiyearp;
   1232 const char * const		typep;
   1233 const char * const		monthp;
   1234 const char * const		dayp;
   1235 const char * const		timep;
   1236 {
   1237 	register const struct lookup *	lp;
   1238 	register const char *		cp;
   1239 	register char *			dp;
   1240 	register char *			ep;
   1241 
   1242 	if ((lp = byword(monthp, mon_names)) == NULL) {
   1243 		error(_("invalid month name"));
   1244 		return;
   1245 	}
   1246 	rp->r_month = lp->l_value;
   1247 	rp->r_todisstd = FALSE;
   1248 	rp->r_todisgmt = FALSE;
   1249 	dp = ecpyalloc(timep);
   1250 	if (*dp != '\0') {
   1251 		ep = dp + strlen(dp) - 1;
   1252 		switch (lowerit(*ep)) {
   1253 			case 's':	/* Standard */
   1254 				rp->r_todisstd = TRUE;
   1255 				rp->r_todisgmt = FALSE;
   1256 				*ep = '\0';
   1257 				break;
   1258 			case 'w':	/* Wall */
   1259 				rp->r_todisstd = FALSE;
   1260 				rp->r_todisgmt = FALSE;
   1261 				*ep = '\0';
   1262 				break;
   1263 			case 'g':	/* Greenwich */
   1264 			case 'u':	/* Universal */
   1265 			case 'z':	/* Zulu */
   1266 				rp->r_todisstd = TRUE;
   1267 				rp->r_todisgmt = TRUE;
   1268 				*ep = '\0';
   1269 				break;
   1270 		}
   1271 	}
   1272 	rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
   1273 	ifree(dp);
   1274 	/*
   1275 	** Year work.
   1276 	*/
   1277 	cp = loyearp;
   1278 	lp = byword(cp, begin_years);
   1279 	if (lp != NULL) switch ((int) lp->l_value) {
   1280 		case YR_MINIMUM:
   1281 			rp->r_loyear = INT_MIN;
   1282 			break;
   1283 		case YR_MAXIMUM:
   1284 			rp->r_loyear = INT_MAX;
   1285 			break;
   1286 		default:	/* "cannot happen" */
   1287 			(void) fprintf(stderr,
   1288 				_("%s: panic: Invalid l_value %d\n"),
   1289 				progname, lp->l_value);
   1290 			(void) exit(EXIT_FAILURE);
   1291 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
   1292 		error(_("invalid starting year"));
   1293 		return;
   1294 	} else if (noise) {
   1295 		if (rp->r_loyear < min_year_representable)
   1296 			warning(_("starting year too low to be represented"));
   1297 		else if (rp->r_loyear > max_year_representable)
   1298 			warning(_("starting year too high to be represented"));
   1299 	}
   1300 	cp = hiyearp;
   1301 	if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
   1302 		case YR_MINIMUM:
   1303 			rp->r_hiyear = INT_MIN;
   1304 			break;
   1305 		case YR_MAXIMUM:
   1306 			rp->r_hiyear = INT_MAX;
   1307 			break;
   1308 		case YR_ONLY:
   1309 			rp->r_hiyear = rp->r_loyear;
   1310 			break;
   1311 		default:	/* "cannot happen" */
   1312 			(void) fprintf(stderr,
   1313 				_("%s: panic: Invalid l_value %d\n"),
   1314 				progname, lp->l_value);
   1315 			(void) exit(EXIT_FAILURE);
   1316 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
   1317 		error(_("invalid ending year"));
   1318 		return;
   1319 	} else if (noise) {
   1320 		if (rp->r_loyear < min_year_representable)
   1321 			warning(_("ending year too low to be represented"));
   1322 		else if (rp->r_loyear > max_year_representable)
   1323 			warning(_("ending year too high to be represented"));
   1324 	}
   1325 	if (rp->r_loyear > rp->r_hiyear) {
   1326 		error(_("starting year greater than ending year"));
   1327 		return;
   1328 	}
   1329 	if (*typep == '\0')
   1330 		rp->r_yrtype = NULL;
   1331 	else {
   1332 		if (rp->r_loyear == rp->r_hiyear) {
   1333 			error(_("typed single year"));
   1334 			return;
   1335 		}
   1336 		rp->r_yrtype = ecpyalloc(typep);
   1337 	}
   1338 	if (rp->r_loyear < min_year && rp->r_loyear > 0)
   1339 		min_year = rp->r_loyear;
   1340 	/*
   1341 	** Day work.
   1342 	** Accept things such as:
   1343 	**	1
   1344 	**	last-Sunday
   1345 	**	Sun<=20
   1346 	**	Sun>=7
   1347 	*/
   1348 	dp = ecpyalloc(dayp);
   1349 	if ((lp = byword(dp, lasts)) != NULL) {
   1350 		rp->r_dycode = DC_DOWLEQ;
   1351 		rp->r_wday = lp->l_value;
   1352 		rp->r_dayofmonth = len_months[1][rp->r_month];
   1353 	} else {
   1354 		if ((ep = strchr(dp, '<')) != 0)
   1355 			rp->r_dycode = DC_DOWLEQ;
   1356 		else if ((ep = strchr(dp, '>')) != 0)
   1357 			rp->r_dycode = DC_DOWGEQ;
   1358 		else {
   1359 			ep = dp;
   1360 			rp->r_dycode = DC_DOM;
   1361 		}
   1362 		if (rp->r_dycode != DC_DOM) {
   1363 			*ep++ = 0;
   1364 			if (*ep++ != '=') {
   1365 				error(_("invalid day of month"));
   1366 				ifree(dp);
   1367 				return;
   1368 			}
   1369 			if ((lp = byword(dp, wday_names)) == NULL) {
   1370 				error(_("invalid weekday name"));
   1371 				ifree(dp);
   1372 				return;
   1373 			}
   1374 			rp->r_wday = lp->l_value;
   1375 		}
   1376 		if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
   1377 			rp->r_dayofmonth <= 0 ||
   1378 			(rp->r_dayofmonth > len_months[1][rp->r_month])) {
   1379 				error(_("invalid day of month"));
   1380 				ifree(dp);
   1381 				return;
   1382 		}
   1383 	}
   1384 	ifree(dp);
   1385 }
   1386 
   1387 static void
   1388 convert(val, buf)
   1389 const long	val;
   1390 char * const	buf;
   1391 {
   1392 	register int	i;
   1393 	register long	shift;
   1394 
   1395 	for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
   1396 		buf[i] = val >> shift;
   1397 }
   1398 
   1399 static void
   1400 puttzcode(val, fp)
   1401 const long	val;
   1402 FILE * const	fp;
   1403 {
   1404 	char	buf[4];
   1405 
   1406 	convert(val, buf);
   1407 	(void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
   1408 }
   1409 
   1410 static int
   1411 atcomp(avp, bvp)
   1412 void *	avp;
   1413 void *	bvp;
   1414 {
   1415 	if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
   1416 		return -1;
   1417 	else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
   1418 		return 1;
   1419 	else	return 0;
   1420 }
   1421 
   1422 static void
   1423 writezone(name)
   1424 const char * const	name;
   1425 {
   1426 	register FILE *		fp;
   1427 	register int		i, j;
   1428 	static char *		fullname;
   1429 	static struct tzhead	tzh;
   1430 	time_t			ats[TZ_MAX_TIMES];
   1431 	unsigned char		types[TZ_MAX_TIMES];
   1432 
   1433 	/*
   1434 	** Sort.
   1435 	*/
   1436 	if (timecnt > 1)
   1437 		(void) qsort((void *) attypes, (size_t) timecnt,
   1438 			(size_t) sizeof *attypes, atcomp);
   1439 	/*
   1440 	** Optimize.
   1441 	*/
   1442 	{
   1443 		int	fromi;
   1444 		int	toi;
   1445 
   1446 		toi = 0;
   1447 		fromi = 0;
   1448 		while (fromi < timecnt && attypes[fromi].at < min_time)
   1449 			++fromi;
   1450 		if (isdsts[0] == 0)
   1451 			while (fromi < timecnt && attypes[fromi].type == 0)
   1452 				++fromi;	/* handled by default rule */
   1453 		for ( ; fromi < timecnt; ++fromi) {
   1454 			if (toi != 0
   1455 			    && ((attypes[fromi].at
   1456 				 + gmtoffs[attypes[toi - 1].type])
   1457 				<= (attypes[toi - 1].at
   1458 				    + gmtoffs[toi == 1 ? 0
   1459 					      : attypes[toi - 2].type]))) {
   1460 				attypes[toi - 1].type = attypes[fromi].type;
   1461 				continue;
   1462 			}
   1463 			if (toi == 0 ||
   1464 				attypes[toi - 1].type != attypes[fromi].type)
   1465 					attypes[toi++] = attypes[fromi];
   1466 		}
   1467 		timecnt = toi;
   1468 	}
   1469 	/*
   1470 	** Transfer.
   1471 	*/
   1472 	for (i = 0; i < timecnt; ++i) {
   1473 		ats[i] = attypes[i].at;
   1474 		types[i] = attypes[i].type;
   1475 	}
   1476 	fullname = erealloc(fullname,
   1477 		(int) (strlen(directory) + 1 + strlen(name) + 1));
   1478 	(void) sprintf(fullname, "%s/%s", directory, name);
   1479 	/*
   1480 	** Remove old file, if any, to snap links.
   1481 	*/
   1482 	if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
   1483 		const char *e = strerror(errno);
   1484 
   1485 		(void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
   1486 			progname, fullname, e);
   1487 		(void) exit(EXIT_FAILURE);
   1488 	}
   1489 	if ((fp = fopen(fullname, "wb")) == NULL) {
   1490 		if (mkdirs(fullname) != 0)
   1491 			(void) exit(EXIT_FAILURE);
   1492 		if ((fp = fopen(fullname, "wb")) == NULL) {
   1493 			const char *e = strerror(errno);
   1494 
   1495 			(void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
   1496 				progname, fullname, e);
   1497 			(void) exit(EXIT_FAILURE);
   1498 		}
   1499 	}
   1500 	convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
   1501 	convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
   1502 	convert(eitol(leapcnt), tzh.tzh_leapcnt);
   1503 	convert(eitol(timecnt), tzh.tzh_timecnt);
   1504 	convert(eitol(typecnt), tzh.tzh_typecnt);
   1505 	convert(eitol(charcnt), tzh.tzh_charcnt);
   1506 	(void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
   1507 #define DO(field)	(void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
   1508 	DO(tzh_magic);
   1509 	DO(tzh_reserved);
   1510 	DO(tzh_ttisgmtcnt);
   1511 	DO(tzh_ttisstdcnt);
   1512 	DO(tzh_leapcnt);
   1513 	DO(tzh_timecnt);
   1514 	DO(tzh_typecnt);
   1515 	DO(tzh_charcnt);
   1516 #undef DO
   1517 	for (i = 0; i < timecnt; ++i) {
   1518 		j = leapcnt;
   1519 		while (--j >= 0)
   1520 			if (ats[i] >= trans[j]) {
   1521 				ats[i] = tadd(ats[i], corr[j]);
   1522 				break;
   1523 			}
   1524 		puttzcode((long) ats[i], fp);
   1525 	}
   1526 	if (timecnt > 0)
   1527 		(void) fwrite((void *) types, (size_t) sizeof types[0],
   1528 			(size_t) timecnt, fp);
   1529 	for (i = 0; i < typecnt; ++i) {
   1530 		puttzcode((long) gmtoffs[i], fp);
   1531 		(void) putc(isdsts[i], fp);
   1532 		(void) putc(abbrinds[i], fp);
   1533 	}
   1534 	if (charcnt != 0)
   1535 		(void) fwrite((void *) chars, (size_t) sizeof chars[0],
   1536 			(size_t) charcnt, fp);
   1537 	for (i = 0; i < leapcnt; ++i) {
   1538 		if (roll[i]) {
   1539 			if (timecnt == 0 || trans[i] < ats[0]) {
   1540 				j = 0;
   1541 				while (isdsts[j])
   1542 					if (++j >= typecnt) {
   1543 						j = 0;
   1544 						break;
   1545 					}
   1546 			} else {
   1547 				j = 1;
   1548 				while (j < timecnt && trans[i] >= ats[j])
   1549 					++j;
   1550 				j = types[j - 1];
   1551 			}
   1552 			puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
   1553 		} else	puttzcode((long) trans[i], fp);
   1554 		puttzcode((long) corr[i], fp);
   1555 	}
   1556 	for (i = 0; i < typecnt; ++i)
   1557 		(void) putc(ttisstds[i], fp);
   1558 	for (i = 0; i < typecnt; ++i)
   1559 		(void) putc(ttisgmts[i], fp);
   1560 	if (ferror(fp) || fclose(fp)) {
   1561 		(void) fprintf(stderr, _("%s: Error writing %s\n"),
   1562 			progname, fullname);
   1563 		(void) exit(EXIT_FAILURE);
   1564 	}
   1565 }
   1566 
   1567 static void
   1568 doabbr(abbr, format, letters, isdst)
   1569 char * const		abbr;
   1570 const char * const	format;
   1571 const char * const	letters;
   1572 const int		isdst;
   1573 {
   1574 	if (strchr(format, '/') == NULL) {
   1575 		if (letters == NULL)
   1576 			(void) strcpy(abbr, format);
   1577 		else	(void) sprintf(abbr, format, letters);
   1578 	} else if (isdst)
   1579 		(void) strcpy(abbr, strchr(format, '/') + 1);
   1580 	else {
   1581 		(void) strcpy(abbr, format);
   1582 		*strchr(abbr, '/') = '\0';
   1583 	}
   1584 }
   1585 
   1586 static void
   1587 outzone(zpfirst, zonecount)
   1588 const struct zone * const	zpfirst;
   1589 const int			zonecount;
   1590 {
   1591 	register const struct zone *	zp;
   1592 	register struct rule *		rp;
   1593 	register int			i, j;
   1594 	register int			usestart, useuntil;
   1595 	register time_t			starttime, untiltime;
   1596 	register long			gmtoff;
   1597 	register long			stdoff;
   1598 	register int			year;
   1599 	register long			startoff;
   1600 	register int			startttisstd;
   1601 	register int			startttisgmt;
   1602 	register int			type;
   1603 	char				startbuf[BUFSIZ];
   1604 
   1605 	INITIALIZE(untiltime);
   1606 	INITIALIZE(starttime);
   1607 	/*
   1608 	** Now. . .finally. . .generate some useful data!
   1609 	*/
   1610 	timecnt = 0;
   1611 	typecnt = 0;
   1612 	charcnt = 0;
   1613 	/*
   1614 	** Thanks to Earl Chew (earl (at) dnd.icp.nec.com.au)
   1615 	** for noting the need to unconditionally initialize startttisstd.
   1616 	*/
   1617 	startttisstd = FALSE;
   1618 	startttisgmt = FALSE;
   1619 	for (i = 0; i < zonecount; ++i) {
   1620 		/*
   1621 		** A guess that may well be corrected later.
   1622 		*/
   1623 		stdoff = 0;
   1624 		zp = &zpfirst[i];
   1625 		usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
   1626 		useuntil = i < (zonecount - 1);
   1627 		if (useuntil && zp->z_untiltime <= min_time)
   1628 			continue;
   1629 		gmtoff = zp->z_gmtoff;
   1630 		eat(zp->z_filename, zp->z_linenum);
   1631 		*startbuf = '\0';
   1632 		startoff = zp->z_gmtoff;
   1633 		if (zp->z_nrules == 0) {
   1634 			stdoff = zp->z_stdoff;
   1635 			doabbr(startbuf, zp->z_format,
   1636 				(char *) NULL, stdoff != 0);
   1637 			type = addtype(oadd(zp->z_gmtoff, stdoff),
   1638 				startbuf, stdoff != 0, startttisstd,
   1639 				startttisgmt);
   1640 			if (usestart) {
   1641 				addtt(starttime, type);
   1642 				usestart = FALSE;
   1643 			} else if (stdoff != 0)
   1644 				addtt(min_time, type);
   1645 		} else for (year = min_year; year <= max_year; ++year) {
   1646 			if (useuntil && year > zp->z_untilrule.r_hiyear)
   1647 				break;
   1648 			/*
   1649 			** Mark which rules to do in the current year.
   1650 			** For those to do, calculate rpytime(rp, year);
   1651 			*/
   1652 			for (j = 0; j < zp->z_nrules; ++j) {
   1653 				rp = &zp->z_rules[j];
   1654 				eats(zp->z_filename, zp->z_linenum,
   1655 					rp->r_filename, rp->r_linenum);
   1656 				rp->r_todo = year >= rp->r_loyear &&
   1657 						year <= rp->r_hiyear &&
   1658 						yearistype(year, rp->r_yrtype);
   1659 				if (rp->r_todo)
   1660 					rp->r_temp = rpytime(rp, year);
   1661 			}
   1662 			for ( ; ; ) {
   1663 				register int	k;
   1664 				register time_t	jtime, ktime;
   1665 				register long	offset;
   1666 				char		buf[BUFSIZ];
   1667 
   1668 				INITIALIZE(ktime);
   1669 				if (useuntil) {
   1670 					/*
   1671 					** Turn untiltime into UTC
   1672 					** assuming the current gmtoff and
   1673 					** stdoff values.
   1674 					*/
   1675 					untiltime = zp->z_untiltime;
   1676 					if (!zp->z_untilrule.r_todisgmt)
   1677 						untiltime = tadd(untiltime,
   1678 							-gmtoff);
   1679 					if (!zp->z_untilrule.r_todisstd)
   1680 						untiltime = tadd(untiltime,
   1681 							-stdoff);
   1682 				}
   1683 				/*
   1684 				** Find the rule (of those to do, if any)
   1685 				** that takes effect earliest in the year.
   1686 				*/
   1687 				k = -1;
   1688 				for (j = 0; j < zp->z_nrules; ++j) {
   1689 					rp = &zp->z_rules[j];
   1690 					if (!rp->r_todo)
   1691 						continue;
   1692 					eats(zp->z_filename, zp->z_linenum,
   1693 						rp->r_filename, rp->r_linenum);
   1694 					offset = rp->r_todisgmt ? 0 : gmtoff;
   1695 					if (!rp->r_todisstd)
   1696 						offset = oadd(offset, stdoff);
   1697 					jtime = rp->r_temp;
   1698 					if (jtime == min_time ||
   1699 						jtime == max_time)
   1700 							continue;
   1701 					jtime = tadd(jtime, -offset);
   1702 					if (k < 0 || jtime < ktime) {
   1703 						k = j;
   1704 						ktime = jtime;
   1705 					}
   1706 				}
   1707 				if (k < 0)
   1708 					break;	/* go on to next year */
   1709 				rp = &zp->z_rules[k];
   1710 				rp->r_todo = FALSE;
   1711 				if (useuntil && ktime >= untiltime)
   1712 					break;
   1713 				stdoff = rp->r_stdoff;
   1714 				if (usestart && ktime == starttime)
   1715 					usestart = FALSE;
   1716 				if (usestart) {
   1717 					if (ktime < starttime) {
   1718 						startoff = oadd(zp->z_gmtoff,
   1719 							stdoff);
   1720 						doabbr(startbuf, zp->z_format,
   1721 							rp->r_abbrvar,
   1722 							rp->r_stdoff != 0);
   1723 						continue;
   1724 					}
   1725 					if (*startbuf == '\0' &&
   1726 					    startoff == oadd(zp->z_gmtoff,
   1727 					    stdoff)) {
   1728 						doabbr(startbuf, zp->z_format,
   1729 							rp->r_abbrvar,
   1730 							rp->r_stdoff != 0);
   1731 					}
   1732 				}
   1733 				eats(zp->z_filename, zp->z_linenum,
   1734 					rp->r_filename, rp->r_linenum);
   1735 				doabbr(buf, zp->z_format, rp->r_abbrvar,
   1736 					rp->r_stdoff != 0);
   1737 				offset = oadd(zp->z_gmtoff, rp->r_stdoff);
   1738 				type = addtype(offset, buf, rp->r_stdoff != 0,
   1739 					rp->r_todisstd, rp->r_todisgmt);
   1740 				addtt(ktime, type);
   1741 			}
   1742 		}
   1743 		if (usestart) {
   1744 			if (*startbuf == '\0' &&
   1745 				zp->z_format != NULL &&
   1746 				strchr(zp->z_format, '%') == NULL &&
   1747 				strchr(zp->z_format, '/') == NULL)
   1748 					(void) strcpy(startbuf, zp->z_format);
   1749 			eat(zp->z_filename, zp->z_linenum);
   1750 			if (*startbuf == '\0')
   1751 error(_("can't determine time zone abbreviation to use just after until time"));
   1752 			else	addtt(starttime,
   1753 					addtype(startoff, startbuf,
   1754 						startoff != zp->z_gmtoff,
   1755 						startttisstd,
   1756 						startttisgmt));
   1757 		}
   1758 		/*
   1759 		** Now we may get to set starttime for the next zone line.
   1760 		*/
   1761 		if (useuntil) {
   1762 			startttisstd = zp->z_untilrule.r_todisstd;
   1763 			startttisgmt = zp->z_untilrule.r_todisgmt;
   1764 			starttime = zp->z_untiltime;
   1765 			if (!startttisstd)
   1766 				starttime = tadd(starttime, -stdoff);
   1767 			if (!startttisgmt)
   1768 				starttime = tadd(starttime, -gmtoff);
   1769 		}
   1770 	}
   1771 	writezone(zpfirst->z_name);
   1772 }
   1773 
   1774 static void
   1775 addtt(starttime, type)
   1776 const time_t	starttime;
   1777 int		type;
   1778 {
   1779 	if (starttime <= min_time ||
   1780 		(timecnt == 1 && attypes[0].at < min_time)) {
   1781 		gmtoffs[0] = gmtoffs[type];
   1782 		isdsts[0] = isdsts[type];
   1783 		ttisstds[0] = ttisstds[type];
   1784 		ttisgmts[0] = ttisgmts[type];
   1785 		if (abbrinds[type] != 0)
   1786 			(void) strcpy(chars, &chars[abbrinds[type]]);
   1787 		abbrinds[0] = 0;
   1788 		charcnt = strlen(chars) + 1;
   1789 		typecnt = 1;
   1790 		timecnt = 0;
   1791 		type = 0;
   1792 	}
   1793 	if (timecnt >= TZ_MAX_TIMES) {
   1794 		error(_("too many transitions?!"));
   1795 		(void) exit(EXIT_FAILURE);
   1796 	}
   1797 	attypes[timecnt].at = starttime;
   1798 	attypes[timecnt].type = type;
   1799 	++timecnt;
   1800 }
   1801 
   1802 static int
   1803 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
   1804 const long		gmtoff;
   1805 const char * const	abbr;
   1806 const int		isdst;
   1807 const int		ttisstd;
   1808 const int		ttisgmt;
   1809 {
   1810 	register int	i, j;
   1811 
   1812 	if (isdst != TRUE && isdst != FALSE) {
   1813 		error(_("internal error - addtype called with bad isdst"));
   1814 		(void) exit(EXIT_FAILURE);
   1815 	}
   1816 	if (ttisstd != TRUE && ttisstd != FALSE) {
   1817 		error(_("internal error - addtype called with bad ttisstd"));
   1818 		(void) exit(EXIT_FAILURE);
   1819 	}
   1820 	if (ttisgmt != TRUE && ttisgmt != FALSE) {
   1821 		error(_("internal error - addtype called with bad ttisgmt"));
   1822 		(void) exit(EXIT_FAILURE);
   1823 	}
   1824 	/*
   1825 	** See if there's already an entry for this zone type.
   1826 	** If so, just return its index.
   1827 	*/
   1828 	for (i = 0; i < typecnt; ++i) {
   1829 		if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
   1830 			strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
   1831 			ttisstd == ttisstds[i] &&
   1832 			ttisgmt == ttisgmts[i])
   1833 				return i;
   1834 	}
   1835 	/*
   1836 	** There isn't one; add a new one, unless there are already too
   1837 	** many.
   1838 	*/
   1839 	if (typecnt >= TZ_MAX_TYPES) {
   1840 		error(_("too many local time types"));
   1841 		(void) exit(EXIT_FAILURE);
   1842 	}
   1843 	gmtoffs[i] = gmtoff;
   1844 	isdsts[i] = isdst;
   1845 	ttisstds[i] = ttisstd;
   1846 	ttisgmts[i] = ttisgmt;
   1847 
   1848 	for (j = 0; j < charcnt; ++j)
   1849 		if (strcmp(&chars[j], abbr) == 0)
   1850 			break;
   1851 	if (j == charcnt)
   1852 		newabbr(abbr);
   1853 	abbrinds[i] = j;
   1854 	++typecnt;
   1855 	return i;
   1856 }
   1857 
   1858 static void
   1859 leapadd(t, positive, rolling, count)
   1860 const time_t	t;
   1861 const int	positive;
   1862 const int	rolling;
   1863 int		count;
   1864 {
   1865 	register int	i, j;
   1866 
   1867 	if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
   1868 		error(_("too many leap seconds"));
   1869 		(void) exit(EXIT_FAILURE);
   1870 	}
   1871 	for (i = 0; i < leapcnt; ++i)
   1872 		if (t <= trans[i]) {
   1873 			if (t == trans[i]) {
   1874 				error(_("repeated leap second moment"));
   1875 				(void) exit(EXIT_FAILURE);
   1876 			}
   1877 			break;
   1878 		}
   1879 	do {
   1880 		for (j = leapcnt; j > i; --j) {
   1881 			trans[j] = trans[j - 1];
   1882 			corr[j] = corr[j - 1];
   1883 			roll[j] = roll[j - 1];
   1884 		}
   1885 		trans[i] = t;
   1886 		corr[i] = positive ? 1L : eitol(-count);
   1887 		roll[i] = rolling;
   1888 		++leapcnt;
   1889 	} while (positive && --count != 0);
   1890 }
   1891 
   1892 static void
   1893 adjleap P((void))
   1894 {
   1895 	register int	i;
   1896 	register long	last = 0;
   1897 
   1898 	/*
   1899 	** propagate leap seconds forward
   1900 	*/
   1901 	for (i = 0; i < leapcnt; ++i) {
   1902 		trans[i] = tadd(trans[i], last);
   1903 		last = corr[i] += last;
   1904 	}
   1905 }
   1906 
   1907 static int
   1908 yearistype(year, type)
   1909 const int		year;
   1910 const char * const	type;
   1911 {
   1912 	static char *	buf;
   1913 	int		result;
   1914 
   1915 	if (type == NULL || *type == '\0')
   1916 		return TRUE;
   1917 	buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
   1918 	(void) sprintf(buf, "%s %d %s", yitcommand, year, type);
   1919 	result = system(buf);
   1920 	if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
   1921 		case 0:
   1922 			return TRUE;
   1923 		case 1:
   1924 			return FALSE;
   1925 	}
   1926 	error(_("Wild result from command execution"));
   1927 	(void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
   1928 		progname, buf, result);
   1929 	for ( ; ; )
   1930 		(void) exit(EXIT_FAILURE);
   1931 }
   1932 
   1933 static int
   1934 lowerit(a)
   1935 int	a;
   1936 {
   1937 	a = (unsigned char) a;
   1938 	return (isascii(a) && isupper(a)) ? tolower(a) : a;
   1939 }
   1940 
   1941 static int
   1942 ciequal(ap, bp)		/* case-insensitive equality */
   1943 register const char *	ap;
   1944 register const char *	bp;
   1945 {
   1946 	while (lowerit(*ap) == lowerit(*bp++))
   1947 		if (*ap++ == '\0')
   1948 			return TRUE;
   1949 	return FALSE;
   1950 }
   1951 
   1952 static int
   1953 itsabbr(abbr, word)
   1954 register const char *	abbr;
   1955 register const char *	word;
   1956 {
   1957 	if (lowerit(*abbr) != lowerit(*word))
   1958 		return FALSE;
   1959 	++word;
   1960 	while (*++abbr != '\0')
   1961 		do {
   1962 			if (*word == '\0')
   1963 				return FALSE;
   1964 		} while (lowerit(*word++) != lowerit(*abbr));
   1965 	return TRUE;
   1966 }
   1967 
   1968 static const struct lookup *
   1969 byword(word, table)
   1970 register const char * const		word;
   1971 register const struct lookup * const	table;
   1972 {
   1973 	register const struct lookup *	foundlp;
   1974 	register const struct lookup *	lp;
   1975 
   1976 	if (word == NULL || table == NULL)
   1977 		return NULL;
   1978 	/*
   1979 	** Look for exact match.
   1980 	*/
   1981 	for (lp = table; lp->l_word != NULL; ++lp)
   1982 		if (ciequal(word, lp->l_word))
   1983 			return lp;
   1984 	/*
   1985 	** Look for inexact match.
   1986 	*/
   1987 	foundlp = NULL;
   1988 	for (lp = table; lp->l_word != NULL; ++lp)
   1989 		if (itsabbr(word, lp->l_word)) {
   1990 			if (foundlp == NULL)
   1991 				foundlp = lp;
   1992 			else	return NULL;	/* multiple inexact matches */
   1993 		}
   1994 	return foundlp;
   1995 }
   1996 
   1997 static char **
   1998 getfields(cp)
   1999 register char *	cp;
   2000 {
   2001 	register char *		dp;
   2002 	register char **	array;
   2003 	register int		nsubs;
   2004 
   2005 	if (cp == NULL)
   2006 		return NULL;
   2007 	array = (char **) (void *)
   2008 		emalloc((int) ((strlen(cp) + 1) * sizeof *array));
   2009 	nsubs = 0;
   2010 	for ( ; ; ) {
   2011 		while (isascii(*cp) && isspace((unsigned char) *cp))
   2012 			++cp;
   2013 		if (*cp == '\0' || *cp == '#')
   2014 			break;
   2015 		array[nsubs++] = dp = cp;
   2016 		do {
   2017 			if ((*dp = *cp++) != '"')
   2018 				++dp;
   2019 			else while ((*dp = *cp++) != '"')
   2020 				if (*dp != '\0')
   2021 					++dp;
   2022 				else	error(_("Odd number of quotation marks"));
   2023 		} while (*cp != '\0' && *cp != '#' &&
   2024 			(!isascii(*cp) || !isspace((unsigned char) *cp)));
   2025 		if (isascii(*cp) && isspace((unsigned char) *cp))
   2026 			++cp;
   2027 		*dp = '\0';
   2028 	}
   2029 	array[nsubs] = NULL;
   2030 	return array;
   2031 }
   2032 
   2033 static long
   2034 oadd(t1, t2)
   2035 const long	t1;
   2036 const long	t2;
   2037 {
   2038 	register long	t;
   2039 
   2040 	t = t1 + t2;
   2041 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
   2042 		error(_("time overflow"));
   2043 		(void) exit(EXIT_FAILURE);
   2044 	}
   2045 	return t;
   2046 }
   2047 
   2048 static time_t
   2049 tadd(t1, t2)
   2050 const time_t	t1;
   2051 const long	t2;
   2052 {
   2053 	register time_t	t;
   2054 
   2055 	if (t1 == max_time && t2 > 0)
   2056 		return max_time;
   2057 	if (t1 == min_time && t2 < 0)
   2058 		return min_time;
   2059 	t = t1 + t2;
   2060 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
   2061 		error(_("time overflow"));
   2062 		(void) exit(EXIT_FAILURE);
   2063 	}
   2064 	return t;
   2065 }
   2066 
   2067 /*
   2068 ** Given a rule, and a year, compute the date - in seconds since January 1,
   2069 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
   2070 */
   2071 
   2072 static time_t
   2073 rpytime(rp, wantedy)
   2074 register const struct rule * const	rp;
   2075 register const int			wantedy;
   2076 {
   2077 	register int	y, m, i;
   2078 	register long	dayoff;			/* with a nod to Margaret O. */
   2079 	register time_t	t;
   2080 
   2081 	if (wantedy == INT_MIN)
   2082 		return min_time;
   2083 	if (wantedy == INT_MAX)
   2084 		return max_time;
   2085 	dayoff = 0;
   2086 	m = TM_JANUARY;
   2087 	y = EPOCH_YEAR;
   2088 	while (wantedy != y) {
   2089 		if (wantedy > y) {
   2090 			i = len_years[isleap(y)];
   2091 			++y;
   2092 		} else {
   2093 			--y;
   2094 			i = -len_years[isleap(y)];
   2095 		}
   2096 		dayoff = oadd(dayoff, eitol(i));
   2097 	}
   2098 	while (m != rp->r_month) {
   2099 		i = len_months[isleap(y)][m];
   2100 		dayoff = oadd(dayoff, eitol(i));
   2101 		++m;
   2102 	}
   2103 	i = rp->r_dayofmonth;
   2104 	if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
   2105 		if (rp->r_dycode == DC_DOWLEQ)
   2106 			--i;
   2107 		else {
   2108 			error(_("use of 2/29 in non leap-year"));
   2109 			(void) exit(EXIT_FAILURE);
   2110 		}
   2111 	}
   2112 	--i;
   2113 	dayoff = oadd(dayoff, eitol(i));
   2114 	if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
   2115 		register long	wday;
   2116 
   2117 #define LDAYSPERWEEK	((long) DAYSPERWEEK)
   2118 		wday = eitol(EPOCH_WDAY);
   2119 		/*
   2120 		** Don't trust mod of negative numbers.
   2121 		*/
   2122 		if (dayoff >= 0)
   2123 			wday = (wday + dayoff) % LDAYSPERWEEK;
   2124 		else {
   2125 			wday -= ((-dayoff) % LDAYSPERWEEK);
   2126 			if (wday < 0)
   2127 				wday += LDAYSPERWEEK;
   2128 		}
   2129 		while (wday != eitol(rp->r_wday))
   2130 			if (rp->r_dycode == DC_DOWGEQ) {
   2131 				dayoff = oadd(dayoff, (long) 1);
   2132 				if (++wday >= LDAYSPERWEEK)
   2133 					wday = 0;
   2134 				++i;
   2135 			} else {
   2136 				dayoff = oadd(dayoff, (long) -1);
   2137 				if (--wday < 0)
   2138 					wday = LDAYSPERWEEK - 1;
   2139 				--i;
   2140 			}
   2141 		if (i < 0 || i >= len_months[isleap(y)][m]) {
   2142 			if (noise)
   2143 				warning(_("rule goes past start/end of month--will not work with pre-2004 versions of zic"));
   2144 		}
   2145 	}
   2146 	if (dayoff < 0 && !TYPE_SIGNED(time_t))
   2147 		return min_time;
   2148 	if (dayoff < min_time / SECSPERDAY)
   2149 		return min_time;
   2150 	if (dayoff > max_time / SECSPERDAY)
   2151 		return max_time;
   2152 	t = (time_t) dayoff * SECSPERDAY;
   2153 	return tadd(t, rp->r_tod);
   2154 }
   2155 
   2156 static void
   2157 newabbr(string)
   2158 const char * const	string;
   2159 {
   2160 	register int	i;
   2161 
   2162 	i = strlen(string) + 1;
   2163 	if (charcnt + i > TZ_MAX_CHARS) {
   2164 		error(_("too many, or too long, time zone abbreviations"));
   2165 		(void) exit(EXIT_FAILURE);
   2166 	}
   2167 	(void) strcpy(&chars[charcnt], string);
   2168 	charcnt += eitol(i);
   2169 }
   2170 
   2171 static int
   2172 mkdirs(argname)
   2173 char * const	argname;
   2174 {
   2175 	register char *	name;
   2176 	register char *	cp;
   2177 
   2178 	if (argname == NULL || *argname == '\0')
   2179 		return 0;
   2180 	cp = name = ecpyalloc(argname);
   2181 	while ((cp = strchr(cp + 1, '/')) != 0) {
   2182 		*cp = '\0';
   2183 #ifndef unix
   2184 		/*
   2185 		** DOS drive specifier?
   2186 		*/
   2187 		if (isalpha((unsigned char) name[0]) &&
   2188 			name[1] == ':' && name[2] == '\0') {
   2189 				*cp = '/';
   2190 				continue;
   2191 		}
   2192 #endif /* !defined unix */
   2193 		if (!itsdir(name)) {
   2194 			/*
   2195 			** It doesn't seem to exist, so we try to create it.
   2196 			** Creation may fail because of the directory being
   2197 			** created by some other multiprocessor, so we get
   2198 			** to do extra checking.
   2199 			*/
   2200 			if (mkdir(name, MKDIR_UMASK) != 0) {
   2201 				const char *e = strerror(errno);
   2202 
   2203 				if (errno != EEXIST || !itsdir(name)) {
   2204 					(void) fprintf(stderr,
   2205 _("%s: Can't create directory %s: %s\n"),
   2206 						progname, name, e);
   2207 					ifree(name);
   2208 					return -1;
   2209 				}
   2210 			}
   2211 		}
   2212 		*cp = '/';
   2213 	}
   2214 	ifree(name);
   2215 	return 0;
   2216 }
   2217 
   2218 static long
   2219 eitol(i)
   2220 const int	i;
   2221 {
   2222 	long	l;
   2223 
   2224 	l = i;
   2225 	if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
   2226 		(void) fprintf(stderr,
   2227 			_("%s: %d did not sign extend correctly\n"),
   2228 			progname, i);
   2229 		(void) exit(EXIT_FAILURE);
   2230 	}
   2231 	return l;
   2232 }
   2233 
   2234 /*
   2235 ** UNIX was a registered trademark of The Open Group in 2003.
   2236 */
   2237