Home | History | Annotate | Line # | Download | only in time
zic.c revision 1.7
      1 /*	$NetBSD: zic.c,v 1.7 1997/06/18 01:13:03 jtc Exp $	*/
      2 
      3 #ifndef lint
      4 #ifndef NOID
      5 static char	elsieid[] = "@(#)zic.c	7.87";
      6 #endif /* !defined NOID */
      7 #endif /* !defined lint */
      8 
      9 #include "private.h"
     10 #include "locale.h"
     11 #include "tzfile.h"
     12 #ifdef unix
     13 #include "sys/stat.h"			/* for umask manifest constants */
     14 #endif /* defined unix */
     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, int abbrlen, 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 [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -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 	while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
    478 		switch (c) {
    479 			default:
    480 				usage();
    481 			case 'd':
    482 				if (directory == NULL)
    483 					directory = optarg;
    484 				else {
    485 					(void) fprintf(stderr,
    486 _("%s: More than one -d option specified\n"),
    487 						progname);
    488 					(void) exit(EXIT_FAILURE);
    489 				}
    490 				break;
    491 			case 'l':
    492 				if (lcltime == NULL)
    493 					lcltime = optarg;
    494 				else {
    495 					(void) fprintf(stderr,
    496 _("%s: More than one -l option specified\n"),
    497 						progname);
    498 					(void) exit(EXIT_FAILURE);
    499 				}
    500 				break;
    501 			case 'p':
    502 				if (psxrules == NULL)
    503 					psxrules = optarg;
    504 				else {
    505 					(void) fprintf(stderr,
    506 _("%s: More than one -p option specified\n"),
    507 						progname);
    508 					(void) exit(EXIT_FAILURE);
    509 				}
    510 				break;
    511 			case 'y':
    512 				if (yitcommand == NULL)
    513 					yitcommand = optarg;
    514 				else {
    515 					(void) fprintf(stderr,
    516 _("%s: More than one -y option specified\n"),
    517 						progname);
    518 					(void) exit(EXIT_FAILURE);
    519 				}
    520 				break;
    521 			case 'L':
    522 				if (leapsec == NULL)
    523 					leapsec = optarg;
    524 				else {
    525 					(void) fprintf(stderr,
    526 _("%s: More than one -L option specified\n"),
    527 						progname);
    528 					(void) exit(EXIT_FAILURE);
    529 				}
    530 				break;
    531 			case 'v':
    532 				noise = TRUE;
    533 				break;
    534 			case 's':
    535 				sflag = TRUE;
    536 				break;
    537 		}
    538 	if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
    539 		usage();	/* usage message by request */
    540 	if (directory == NULL)
    541 		directory = TZDIR;
    542 	if (yitcommand == NULL)
    543 		yitcommand = "yearistype";
    544 
    545 	setboundaries();
    546 
    547 	if (optind < argc && leapsec != NULL) {
    548 		infile(leapsec);
    549 		adjleap();
    550 	}
    551 
    552 	for (i = optind; i < argc; ++i)
    553 		infile(argv[i]);
    554 	if (errors)
    555 		(void) exit(EXIT_FAILURE);
    556 	associate();
    557 	for (i = 0; i < nzones; i = j) {
    558 		/*
    559 		** Find the next non-continuation zone entry.
    560 		*/
    561 		for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
    562 			continue;
    563 		outzone(&zones[i], j - i);
    564 	}
    565 	/*
    566 	** Make links.
    567 	*/
    568 	for (i = 0; i < nlinks; ++i)
    569 		dolink(links[i].l_from, links[i].l_to);
    570 	if (lcltime != NULL)
    571 		dolink(lcltime, TZDEFAULT);
    572 	if (psxrules != NULL)
    573 		dolink(psxrules, TZDEFRULES);
    574 	return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
    575 }
    576 
    577 static void
    578 dolink(fromfile, tofile)
    579 const char * const	fromfile;
    580 const char * const	tofile;
    581 {
    582 	register char *	fromname;
    583 	register char *	toname;
    584 
    585 	if (fromfile[0] == '/')
    586 		fromname = ecpyalloc(fromfile);
    587 	else {
    588 		fromname = ecpyalloc(directory);
    589 		fromname = ecatalloc(fromname, "/");
    590 		fromname = ecatalloc(fromname, fromfile);
    591 	}
    592 	if (tofile[0] == '/')
    593 		toname = ecpyalloc(tofile);
    594 	else {
    595 		toname = ecpyalloc(directory);
    596 		toname = ecatalloc(toname, "/");
    597 		toname = ecatalloc(toname, tofile);
    598 	}
    599 	/*
    600 	** We get to be careful here since
    601 	** there's a fair chance of root running us.
    602 	*/
    603 	if (!itsdir(toname))
    604 		(void) remove(toname);
    605 	if (link(fromname, toname) != 0) {
    606 		if (mkdirs(toname) != 0)
    607 			(void) exit(EXIT_FAILURE);
    608 		if (link(fromname, toname) != 0) {
    609 			const char *e = strerror(errno);
    610 
    611 			(void) fprintf(stderr,
    612 				_("%s: Can't link from %s to %s: %s\n"),
    613 				progname, fromname, toname, e);
    614 			(void) exit(EXIT_FAILURE);
    615 		}
    616 	}
    617 	ifree(fromname);
    618 	ifree(toname);
    619 }
    620 
    621 #ifndef INT_MAX
    622 #define INT_MAX	((int) (((unsigned)~0)>>1))
    623 #endif /* !defined INT_MAX */
    624 
    625 #ifndef INT_MIN
    626 #define INT_MIN	((int) ~(((unsigned)~0)>>1))
    627 #endif /* !defined INT_MIN */
    628 
    629 /*
    630 ** The tz file format currently allows at most 32-bit quantities.
    631 ** This restriction should be removed before signed 32-bit values
    632 ** wrap around in 2038, but unfortunately this will require a
    633 ** change to the tz file format.
    634 */
    635 
    636 #define MAX_BITS_IN_FILE	32
    637 #define TIME_T_BITS_IN_FILE	((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
    638 
    639 static void
    640 setboundaries P((void))
    641 {
    642 	if (TYPE_SIGNED(time_t)) {
    643 		min_time = ~ (time_t) 0;
    644 		min_time <<= TIME_T_BITS_IN_FILE - 1;
    645 		max_time = ~ (time_t) 0 - min_time;
    646 		if (sflag)
    647 			min_time = 0;
    648 	} else {
    649 		min_time = 0;
    650 		max_time = 2 - sflag;
    651 		max_time <<= TIME_T_BITS_IN_FILE - 1;
    652 		--max_time;
    653 	}
    654 	min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
    655 	max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
    656 	min_year_representable = min_year;
    657 	max_year_representable = max_year;
    658 }
    659 
    660 static int
    661 itsdir(name)
    662 const char * const	name;
    663 {
    664 	register char *	myname;
    665 	register int	accres;
    666 
    667 	myname = ecpyalloc(name);
    668 	myname = ecatalloc(myname, "/.");
    669 	accres = access(myname, F_OK);
    670 	ifree(myname);
    671 	return accres == 0;
    672 }
    673 
    674 /*
    675 ** Associate sets of rules with zones.
    676 */
    677 
    678 /*
    679 ** Sort by rule name.
    680 */
    681 
    682 static int
    683 rcomp(cp1, cp2)
    684 const void *	cp1;
    685 const void *	cp2;
    686 {
    687 	return strcmp(((const struct rule *) cp1)->r_name,
    688 		((const struct rule *) cp2)->r_name);
    689 }
    690 
    691 static void
    692 associate P((void))
    693 {
    694 	register struct zone *	zp;
    695 	register struct rule *	rp;
    696 	register int		base, out;
    697 	register int		i, j;
    698 
    699 	if (nrules != 0) {
    700 		(void) qsort((void *) rules, (size_t) nrules,
    701 			(size_t) sizeof *rules, rcomp);
    702 		for (i = 0; i < nrules - 1; ++i) {
    703 			if (strcmp(rules[i].r_name,
    704 				rules[i + 1].r_name) != 0)
    705 					continue;
    706 			if (strcmp(rules[i].r_filename,
    707 				rules[i + 1].r_filename) == 0)
    708 					continue;
    709 			eat(rules[i].r_filename, rules[i].r_linenum);
    710 			warning(_("same rule name in multiple files"));
    711 			eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
    712 			warning(_("same rule name in multiple files"));
    713 			for (j = i + 2; j < nrules; ++j) {
    714 				if (strcmp(rules[i].r_name,
    715 					rules[j].r_name) != 0)
    716 						break;
    717 				if (strcmp(rules[i].r_filename,
    718 					rules[j].r_filename) == 0)
    719 						continue;
    720 				if (strcmp(rules[i + 1].r_filename,
    721 					rules[j].r_filename) == 0)
    722 						continue;
    723 				break;
    724 			}
    725 			i = j - 1;
    726 		}
    727 	}
    728 	for (i = 0; i < nzones; ++i) {
    729 		zp = &zones[i];
    730 		zp->z_rules = NULL;
    731 		zp->z_nrules = 0;
    732 	}
    733 	for (base = 0; base < nrules; base = out) {
    734 		rp = &rules[base];
    735 		for (out = base + 1; out < nrules; ++out)
    736 			if (strcmp(rp->r_name, rules[out].r_name) != 0)
    737 				break;
    738 		for (i = 0; i < nzones; ++i) {
    739 			zp = &zones[i];
    740 			if (strcmp(zp->z_rule, rp->r_name) != 0)
    741 				continue;
    742 			zp->z_rules = rp;
    743 			zp->z_nrules = out - base;
    744 		}
    745 	}
    746 	for (i = 0; i < nzones; ++i) {
    747 		zp = &zones[i];
    748 		if (zp->z_nrules == 0) {
    749 			/*
    750 			** Maybe we have a local standard time offset.
    751 			*/
    752 			eat(zp->z_filename, zp->z_linenum);
    753 			zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
    754 					      TRUE);
    755 			/*
    756 			** Note, though, that if there's no rule,
    757 			** a '%s' in the format is a bad thing.
    758 			*/
    759 			if (strchr(zp->z_format, '%') != 0)
    760 				error(_("%s in ruleless zone"));
    761 		}
    762 	}
    763 	if (errors)
    764 		(void) exit(EXIT_FAILURE);
    765 }
    766 
    767 static void
    768 infile(name)
    769 const char *	name;
    770 {
    771 	register FILE *			fp;
    772 	register char **		fields;
    773 	register char *			cp;
    774 	register const struct lookup *	lp;
    775 	register int			nfields;
    776 	register int			wantcont;
    777 	register int			num;
    778 	char				buf[BUFSIZ];
    779 
    780 	if (strcmp(name, "-") == 0) {
    781 		name = _("standard input");
    782 		fp = stdin;
    783 	} else if ((fp = fopen(name, "r")) == NULL) {
    784 		const char *e = strerror(errno);
    785 
    786 		(void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
    787 			progname, name, e);
    788 		(void) exit(EXIT_FAILURE);
    789 	}
    790 	wantcont = FALSE;
    791 	for (num = 1; ; ++num) {
    792 		eat(name, num);
    793 		if (fgets(buf, (int) sizeof buf, fp) != buf)
    794 			break;
    795 		cp = strchr(buf, '\n');
    796 		if (cp == NULL) {
    797 			error(_("line too long"));
    798 			(void) exit(EXIT_FAILURE);
    799 		}
    800 		*cp = '\0';
    801 		fields = getfields(buf);
    802 		nfields = 0;
    803 		while (fields[nfields] != NULL) {
    804 			static char	nada;
    805 
    806 			if (strcmp(fields[nfields], "-") == 0)
    807 				fields[nfields] = &nada;
    808 			++nfields;
    809 		}
    810 		if (nfields == 0) {
    811 			/* nothing to do */
    812 		} else if (wantcont) {
    813 			wantcont = inzcont(fields, nfields);
    814 		} else {
    815 			lp = byword(fields[0], line_codes);
    816 			if (lp == NULL)
    817 				error(_("input line of unknown type"));
    818 			else switch ((int) (lp->l_value)) {
    819 				case LC_RULE:
    820 					inrule(fields, nfields);
    821 					wantcont = FALSE;
    822 					break;
    823 				case LC_ZONE:
    824 					wantcont = inzone(fields, nfields);
    825 					break;
    826 				case LC_LINK:
    827 					inlink(fields, nfields);
    828 					wantcont = FALSE;
    829 					break;
    830 				case LC_LEAP:
    831 					if (name != leapsec)
    832 						(void) fprintf(stderr,
    833 _("%s: Leap line in non leap seconds file %s\n"),
    834 							progname, name);
    835 					else	inleap(fields, nfields);
    836 					wantcont = FALSE;
    837 					break;
    838 				default:	/* "cannot happen" */
    839 					(void) fprintf(stderr,
    840 _("%s: panic: Invalid l_value %d\n"),
    841 						progname, lp->l_value);
    842 					(void) exit(EXIT_FAILURE);
    843 			}
    844 		}
    845 		ifree((char *) fields);
    846 	}
    847 	if (ferror(fp)) {
    848 		(void) fprintf(stderr, _("%s: Error reading %s\n"),
    849 			progname, filename);
    850 		(void) exit(EXIT_FAILURE);
    851 	}
    852 	if (fp != stdin && fclose(fp)) {
    853 		const char *e = strerror(errno);
    854 
    855 		(void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
    856 			progname, filename, e);
    857 		(void) exit(EXIT_FAILURE);
    858 	}
    859 	if (wantcont)
    860 		error(_("expected continuation line not found"));
    861 }
    862 
    863 /*
    864 ** Convert a string of one of the forms
    865 **	h	-h	hh:mm	-hh:mm	hh:mm:ss	-hh:mm:ss
    866 ** into a number of seconds.
    867 ** A null string maps to zero.
    868 ** Call error with errstring and return zero on errors.
    869 */
    870 
    871 static long
    872 gethms(string, errstring, signable)
    873 const char *		string;
    874 const char * const	errstring;
    875 const int		signable;
    876 {
    877 	int	hh, mm, ss, sign;
    878 
    879 	if (string == NULL || *string == '\0')
    880 		return 0;
    881 	if (!signable)
    882 		sign = 1;
    883 	else if (*string == '-') {
    884 		sign = -1;
    885 		++string;
    886 	} else	sign = 1;
    887 	if (sscanf(string, scheck(string, "%d"), &hh) == 1)
    888 		mm = ss = 0;
    889 	else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
    890 		ss = 0;
    891 	else if (sscanf(string, scheck(string, "%d:%d:%d"),
    892 		&hh, &mm, &ss) != 3) {
    893 			error(errstring);
    894 			return 0;
    895 	}
    896 	if (hh < 0 || hh >= HOURSPERDAY ||
    897 		mm < 0 || mm >= MINSPERHOUR ||
    898 		ss < 0 || ss > SECSPERMIN) {
    899 			error(errstring);
    900 			return 0;
    901 	}
    902 	return eitol(sign) *
    903 		(eitol(hh * MINSPERHOUR + mm) *
    904 		eitol(SECSPERMIN) + eitol(ss));
    905 }
    906 
    907 static void
    908 inrule(fields, nfields)
    909 register char ** const	fields;
    910 const int		nfields;
    911 {
    912 	static struct rule	r;
    913 
    914 	if (nfields != RULE_FIELDS) {
    915 		error(_("wrong number of fields on Rule line"));
    916 		return;
    917 	}
    918 	if (*fields[RF_NAME] == '\0') {
    919 		error(_("nameless rule"));
    920 		return;
    921 	}
    922 	r.r_filename = filename;
    923 	r.r_linenum = linenum;
    924 	r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
    925 	rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
    926 		fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
    927 	r.r_name = ecpyalloc(fields[RF_NAME]);
    928 	r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
    929 	rules = (struct rule *) (void *) erealloc((char *) rules,
    930 		(int) ((nrules + 1) * sizeof *rules));
    931 	rules[nrules++] = r;
    932 }
    933 
    934 static int
    935 inzone(fields, nfields)
    936 register char ** const	fields;
    937 const int		nfields;
    938 {
    939 	register int	i;
    940 	static char *	buf;
    941 
    942 	if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
    943 		error(_("wrong number of fields on Zone line"));
    944 		return FALSE;
    945 	}
    946 	if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
    947 		buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
    948 		(void)sprintf(buf,	/* XXX: sprintf is safe */
    949 _("\"Zone %s\" line and -l option are mutually exclusive"),
    950 			TZDEFAULT);
    951 		error(buf);
    952 		return FALSE;
    953 	}
    954 	if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
    955 		buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
    956 		(void)sprintf(buf,	/* XXX: sprintf is safe */
    957 _("\"Zone %s\" line and -p option are mutually exclusive"),
    958 			TZDEFRULES);
    959 		error(buf);
    960 		return FALSE;
    961 	}
    962 	for (i = 0; i < nzones; ++i)
    963 		if (zones[i].z_name != NULL &&
    964 			strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
    965 				buf = erealloc(buf, (int) (132 +
    966 					strlen(fields[ZF_NAME]) +
    967 					strlen(zones[i].z_filename)));
    968 				(void)sprintf(buf,	/* XXX: sprintf is safe */
    969 _("duplicate zone name %s (file \"%s\", line %d)"),
    970 					fields[ZF_NAME],
    971 					zones[i].z_filename,
    972 					zones[i].z_linenum);
    973 				error(buf);
    974 				return FALSE;
    975 		}
    976 	return inzsub(fields, nfields, FALSE);
    977 }
    978 
    979 static int
    980 inzcont(fields, nfields)
    981 register char ** const	fields;
    982 const int		nfields;
    983 {
    984 	if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
    985 		error(_("wrong number of fields on Zone continuation line"));
    986 		return FALSE;
    987 	}
    988 	return inzsub(fields, nfields, TRUE);
    989 }
    990 
    991 static int
    992 inzsub(fields, nfields, iscont)
    993 register char ** const	fields;
    994 const int		nfields;
    995 const int		iscont;
    996 {
    997 	register char *		cp;
    998 	static struct zone	z;
    999 	register int		i_gmtoff, i_rule, i_format;
   1000 	register int		i_untilyear, i_untilmonth;
   1001 	register int		i_untilday, i_untiltime;
   1002 	register int		hasuntil;
   1003 
   1004 	if (iscont) {
   1005 		i_gmtoff = ZFC_GMTOFF;
   1006 		i_rule = ZFC_RULE;
   1007 		i_format = ZFC_FORMAT;
   1008 		i_untilyear = ZFC_TILYEAR;
   1009 		i_untilmonth = ZFC_TILMONTH;
   1010 		i_untilday = ZFC_TILDAY;
   1011 		i_untiltime = ZFC_TILTIME;
   1012 		z.z_name = NULL;
   1013 	} else {
   1014 		i_gmtoff = ZF_GMTOFF;
   1015 		i_rule = ZF_RULE;
   1016 		i_format = ZF_FORMAT;
   1017 		i_untilyear = ZF_TILYEAR;
   1018 		i_untilmonth = ZF_TILMONTH;
   1019 		i_untilday = ZF_TILDAY;
   1020 		i_untiltime = ZF_TILTIME;
   1021 		z.z_name = ecpyalloc(fields[ZF_NAME]);
   1022 	}
   1023 	z.z_filename = filename;
   1024 	z.z_linenum = linenum;
   1025 	z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid GMT offset"), TRUE);
   1026 	if ((cp = strchr(fields[i_format], '%')) != 0) {
   1027 		if (*++cp != 's' || strchr(cp, '%') != 0) {
   1028 			error(_("invalid abbreviation format"));
   1029 			return FALSE;
   1030 		}
   1031 	}
   1032 	z.z_rule = ecpyalloc(fields[i_rule]);
   1033 	z.z_format = ecpyalloc(fields[i_format]);
   1034 	hasuntil = nfields > i_untilyear;
   1035 	if (hasuntil) {
   1036 		z.z_untilrule.r_filename = filename;
   1037 		z.z_untilrule.r_linenum = linenum;
   1038 		rulesub(&z.z_untilrule,
   1039 			fields[i_untilyear],
   1040 			"only",
   1041 			"",
   1042 			(nfields > i_untilmonth) ?
   1043 			fields[i_untilmonth] : "Jan",
   1044 			(nfields > i_untilday) ? fields[i_untilday] : "1",
   1045 			(nfields > i_untiltime) ? fields[i_untiltime] : "0");
   1046 		z.z_untiltime = rpytime(&z.z_untilrule,
   1047 			z.z_untilrule.r_loyear);
   1048 		if (iscont && nzones > 0 &&
   1049 			z.z_untiltime > min_time &&
   1050 			z.z_untiltime < max_time &&
   1051 			zones[nzones - 1].z_untiltime > min_time &&
   1052 			zones[nzones - 1].z_untiltime < max_time &&
   1053 			zones[nzones - 1].z_untiltime >= z.z_untiltime) {
   1054 				error(_("Zone continuation line end time is not after end time of previous line"));
   1055 				return FALSE;
   1056 		}
   1057 	}
   1058 	zones = (struct zone *) (void *) erealloc((char *) zones,
   1059 		(int) ((nzones + 1) * sizeof *zones));
   1060 	zones[nzones++] = z;
   1061 	/*
   1062 	** If there was an UNTIL field on this line,
   1063 	** there's more information about the zone on the next line.
   1064 	*/
   1065 	return hasuntil;
   1066 }
   1067 
   1068 static void
   1069 inleap(fields, nfields)
   1070 register char ** const	fields;
   1071 const int		nfields;
   1072 {
   1073 	register const char *		cp;
   1074 	register const struct lookup *	lp;
   1075 	register int			i, j;
   1076 	int				year, month, day;
   1077 	long				dayoff, tod;
   1078 	time_t				t;
   1079 
   1080 	if (nfields != LEAP_FIELDS) {
   1081 		error(_("wrong number of fields on Leap line"));
   1082 		return;
   1083 	}
   1084 	dayoff = 0;
   1085 	cp = fields[LP_YEAR];
   1086 	if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
   1087 			/*
   1088 			 * Leapin' Lizards!
   1089 			 */
   1090 			error(_("invalid leaping year"));
   1091 			return;
   1092 	}
   1093 	j = EPOCH_YEAR;
   1094 	while (j != year) {
   1095 		if (year > j) {
   1096 			i = len_years[isleap(j)];
   1097 			++j;
   1098 		} else {
   1099 			--j;
   1100 			i = -len_years[isleap(j)];
   1101 		}
   1102 		dayoff = oadd(dayoff, eitol(i));
   1103 	}
   1104 	if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
   1105 		error(_("invalid month name"));
   1106 		return;
   1107 	}
   1108 	month = lp->l_value;
   1109 	j = TM_JANUARY;
   1110 	while (j != month) {
   1111 		i = len_months[isleap(year)][j];
   1112 		dayoff = oadd(dayoff, eitol(i));
   1113 		++j;
   1114 	}
   1115 	cp = fields[LP_DAY];
   1116 	if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
   1117 		day <= 0 || day > len_months[isleap(year)][month]) {
   1118 			error(_("invalid day of month"));
   1119 			return;
   1120 	}
   1121 	dayoff = oadd(dayoff, eitol(day - 1));
   1122 	if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
   1123 		error(_("time before zero"));
   1124 		return;
   1125 	}
   1126 	t = (time_t) dayoff * SECSPERDAY;
   1127 	/*
   1128 	** Cheap overflow check.
   1129 	*/
   1130 	if (t / SECSPERDAY != dayoff) {
   1131 		error(_("time overflow"));
   1132 		return;
   1133 	}
   1134 	tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
   1135 	cp = fields[LP_CORR];
   1136 	{
   1137 		register int	positive;
   1138 		int		count;
   1139 
   1140 		if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
   1141 			positive = FALSE;
   1142 			count = 1;
   1143 		} else if (strcmp(cp, "--") == 0) {
   1144 			positive = FALSE;
   1145 			count = 2;
   1146 		} else if (strcmp(cp, "+") == 0) {
   1147 			positive = TRUE;
   1148 			count = 1;
   1149 		} else if (strcmp(cp, "++") == 0) {
   1150 			positive = TRUE;
   1151 			count = 2;
   1152 		} else {
   1153 			error(_("illegal CORRECTION field on Leap line"));
   1154 			return;
   1155 		}
   1156 		if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
   1157 			error(_("illegal Rolling/Stationary field on Leap line"));
   1158 			return;
   1159 		}
   1160 		leapadd(tadd(t, tod), positive, lp->l_value, count);
   1161 	}
   1162 }
   1163 
   1164 static void
   1165 inlink(fields, nfields)
   1166 register char ** const	fields;
   1167 const int		nfields;
   1168 {
   1169 	struct link	l;
   1170 
   1171 	if (nfields != LINK_FIELDS) {
   1172 		error(_("wrong number of fields on Link line"));
   1173 		return;
   1174 	}
   1175 	if (*fields[LF_FROM] == '\0') {
   1176 		error(_("blank FROM field on Link line"));
   1177 		return;
   1178 	}
   1179 	if (*fields[LF_TO] == '\0') {
   1180 		error(_("blank TO field on Link line"));
   1181 		return;
   1182 	}
   1183 	l.l_filename = filename;
   1184 	l.l_linenum = linenum;
   1185 	l.l_from = ecpyalloc(fields[LF_FROM]);
   1186 	l.l_to = ecpyalloc(fields[LF_TO]);
   1187 	links = (struct link *) (void *) erealloc((char *) links,
   1188 		(int) ((nlinks + 1) * sizeof *links));
   1189 	links[nlinks++] = l;
   1190 }
   1191 
   1192 static void
   1193 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
   1194 register struct rule * const	rp;
   1195 const char * const		loyearp;
   1196 const char * const		hiyearp;
   1197 const char * const		typep;
   1198 const char * const		monthp;
   1199 const char * const		dayp;
   1200 const char * const		timep;
   1201 {
   1202 	register const struct lookup *	lp;
   1203 	register const char *		cp;
   1204 	register char *			dp;
   1205 	register char *			ep;
   1206 
   1207 	if ((lp = byword(monthp, mon_names)) == NULL) {
   1208 		error(_("invalid month name"));
   1209 		return;
   1210 	}
   1211 	rp->r_month = lp->l_value;
   1212 	rp->r_todisstd = FALSE;
   1213 	rp->r_todisgmt = FALSE;
   1214 	dp = ecpyalloc(timep);
   1215 	if (*dp != '\0') {
   1216 		ep = dp + strlen(dp) - 1;
   1217 		switch (lowerit(*ep)) {
   1218 			case 's':	/* Standard */
   1219 				rp->r_todisstd = TRUE;
   1220 				rp->r_todisgmt = FALSE;
   1221 				*ep = '\0';
   1222 				break;
   1223 			case 'w':	/* Wall */
   1224 				rp->r_todisstd = FALSE;
   1225 				rp->r_todisgmt = FALSE;
   1226 				*ep = '\0';
   1227 				break;
   1228 			case 'g':	/* Greenwich */
   1229 			case 'u':	/* Universal */
   1230 			case 'z':	/* Zulu */
   1231 				rp->r_todisstd = TRUE;
   1232 				rp->r_todisgmt = TRUE;
   1233 				*ep = '\0';
   1234 				break;
   1235 		}
   1236 	}
   1237 	rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
   1238 	ifree(dp);
   1239 	/*
   1240 	** Year work.
   1241 	*/
   1242 	cp = loyearp;
   1243 	lp = byword(cp, begin_years);
   1244 	if (lp != NULL) switch ((int) lp->l_value) {
   1245 		case YR_MINIMUM:
   1246 			rp->r_loyear = INT_MIN;
   1247 			break;
   1248 		case YR_MAXIMUM:
   1249 			rp->r_loyear = INT_MAX;
   1250 			break;
   1251 		default:	/* "cannot happen" */
   1252 			(void) fprintf(stderr,
   1253 				_("%s: panic: Invalid l_value %d\n"),
   1254 				progname, lp->l_value);
   1255 			(void) exit(EXIT_FAILURE);
   1256 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
   1257 		error(_("invalid starting year"));
   1258 		return;
   1259 	} else if (noise)
   1260 		if (rp->r_loyear < min_year_representable)
   1261 			warning(_("starting year too low to be represented"));
   1262 		else if (rp->r_loyear > max_year_representable)
   1263 			warning(_("starting year too high to be represented"));
   1264 	cp = hiyearp;
   1265 	if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
   1266 		case YR_MINIMUM:
   1267 			rp->r_hiyear = INT_MIN;
   1268 			break;
   1269 		case YR_MAXIMUM:
   1270 			rp->r_hiyear = INT_MAX;
   1271 			break;
   1272 		case YR_ONLY:
   1273 			rp->r_hiyear = rp->r_loyear;
   1274 			break;
   1275 		default:	/* "cannot happen" */
   1276 			(void) fprintf(stderr,
   1277 				_("%s: panic: Invalid l_value %d\n"),
   1278 				progname, lp->l_value);
   1279 			(void) exit(EXIT_FAILURE);
   1280 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
   1281 		error(_("invalid ending year"));
   1282 		return;
   1283 	} else if (noise)
   1284 		if (rp->r_loyear < min_year_representable)
   1285 			warning(_("starting year too low to be represented"));
   1286 		else if (rp->r_loyear > max_year_representable)
   1287 			warning(_("starting year too high to be represented"));
   1288 	if (rp->r_loyear > rp->r_hiyear) {
   1289 		error(_("starting year greater than ending year"));
   1290 		return;
   1291 	}
   1292 	if (*typep == '\0')
   1293 		rp->r_yrtype = NULL;
   1294 	else {
   1295 		if (rp->r_loyear == rp->r_hiyear) {
   1296 			error(_("typed single year"));
   1297 			return;
   1298 		}
   1299 		rp->r_yrtype = ecpyalloc(typep);
   1300 	}
   1301 	if (rp->r_loyear < min_year && rp->r_loyear > 0)
   1302 		min_year = rp->r_loyear;
   1303 	/*
   1304 	** Day work.
   1305 	** Accept things such as:
   1306 	**	1
   1307 	**	last-Sunday
   1308 	**	Sun<=20
   1309 	**	Sun>=7
   1310 	*/
   1311 	dp = ecpyalloc(dayp);
   1312 	if ((lp = byword(dp, lasts)) != NULL) {
   1313 		rp->r_dycode = DC_DOWLEQ;
   1314 		rp->r_wday = lp->l_value;
   1315 		rp->r_dayofmonth = len_months[1][rp->r_month];
   1316 	} else {
   1317 		if ((ep = strchr(dp, '<')) != 0)
   1318 			rp->r_dycode = DC_DOWLEQ;
   1319 		else if ((ep = strchr(dp, '>')) != 0)
   1320 			rp->r_dycode = DC_DOWGEQ;
   1321 		else {
   1322 			ep = dp;
   1323 			rp->r_dycode = DC_DOM;
   1324 		}
   1325 		if (rp->r_dycode != DC_DOM) {
   1326 			*ep++ = 0;
   1327 			if (*ep++ != '=') {
   1328 				error(_("invalid day of month"));
   1329 				ifree(dp);
   1330 				return;
   1331 			}
   1332 			if ((lp = byword(dp, wday_names)) == NULL) {
   1333 				error(_("invalid weekday name"));
   1334 				ifree(dp);
   1335 				return;
   1336 			}
   1337 			rp->r_wday = lp->l_value;
   1338 		}
   1339 		if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
   1340 			rp->r_dayofmonth <= 0 ||
   1341 			(rp->r_dayofmonth > len_months[1][rp->r_month])) {
   1342 				error(_("invalid day of month"));
   1343 				ifree(dp);
   1344 				return;
   1345 		}
   1346 	}
   1347 	ifree(dp);
   1348 }
   1349 
   1350 static void
   1351 convert(val, buf)
   1352 const long	val;
   1353 char * const	buf;
   1354 {
   1355 	register int	i;
   1356 	register long	shift;
   1357 
   1358 	for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
   1359 		buf[i] = val >> shift;
   1360 }
   1361 
   1362 static void
   1363 puttzcode(val, fp)
   1364 const long	val;
   1365 FILE * const	fp;
   1366 {
   1367 	char	buf[4];
   1368 
   1369 	convert(val, buf);
   1370 	(void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
   1371 }
   1372 
   1373 static int
   1374 atcomp(avp, bvp)
   1375 void *	avp;
   1376 void *	bvp;
   1377 {
   1378 	if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
   1379 		return -1;
   1380 	else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
   1381 		return 1;
   1382 	else	return 0;
   1383 }
   1384 
   1385 static void
   1386 writezone(name)
   1387 const char * const	name;
   1388 {
   1389 	register FILE *		fp;
   1390 	register int		i, j;
   1391 	static char *		fullname;
   1392 	static struct tzhead	tzh;
   1393 	time_t			ats[TZ_MAX_TIMES];
   1394 	unsigned char		types[TZ_MAX_TIMES];
   1395 
   1396 	/*
   1397 	** Sort.
   1398 	*/
   1399 	if (timecnt > 1)
   1400 		(void) qsort((void *) attypes, (size_t) timecnt,
   1401 			(size_t) sizeof *attypes, atcomp);
   1402 	/*
   1403 	** Optimize.
   1404 	*/
   1405 	{
   1406 		int	fromi;
   1407 		int	toi;
   1408 
   1409 		toi = 0;
   1410 		fromi = 0;
   1411 		while (fromi < timecnt && attypes[fromi].at < min_time)
   1412 			++fromi;
   1413 		if (isdsts[0] == 0)
   1414 			while (fromi < timecnt && attypes[fromi].type == 0)
   1415 				++fromi;	/* handled by default rule */
   1416 		for ( ; fromi < timecnt; ++fromi) {
   1417 			if (toi != 0
   1418 			    && ((attypes[fromi].at
   1419 				 + gmtoffs[attypes[toi - 1].type])
   1420 				<= (attypes[toi - 1].at
   1421 				    + gmtoffs[toi == 1 ? 0
   1422 					      : attypes[toi - 2].type]))) {
   1423 				attypes[toi - 1].type = attypes[fromi].type;
   1424 				continue;
   1425 			}
   1426 			if (toi == 0 ||
   1427 				attypes[toi - 1].type != attypes[fromi].type)
   1428 					attypes[toi++] = attypes[fromi];
   1429 		}
   1430 		timecnt = toi;
   1431 	}
   1432 	/*
   1433 	** Transfer.
   1434 	*/
   1435 	for (i = 0; i < timecnt; ++i) {
   1436 		ats[i] = attypes[i].at;
   1437 		types[i] = attypes[i].type;
   1438 	}
   1439 	fullname = erealloc(fullname,
   1440 		(int) (strlen(directory) + 1 + strlen(name) + 1));
   1441 	(void) sprintf(fullname, "%s/%s", directory, name);	/* XXX: sprintf is safe */
   1442 	/*
   1443 	** Remove old file, if any, to snap links.
   1444 	*/
   1445 	if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
   1446 		const char *e = strerror(errno);
   1447 
   1448 		(void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
   1449 			progname, fullname, e);
   1450 		(void) exit(EXIT_FAILURE);
   1451 	}
   1452 	if ((fp = fopen(fullname, "wb")) == NULL) {
   1453 		if (mkdirs(fullname) != 0)
   1454 			(void) exit(EXIT_FAILURE);
   1455 		if ((fp = fopen(fullname, "wb")) == NULL) {
   1456 			const char *e = strerror(errno);
   1457 
   1458 			(void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
   1459 				progname, fullname, e);
   1460 			(void) exit(EXIT_FAILURE);
   1461 		}
   1462 	}
   1463 	convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
   1464 	convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
   1465 	convert(eitol(leapcnt), tzh.tzh_leapcnt);
   1466 	convert(eitol(timecnt), tzh.tzh_timecnt);
   1467 	convert(eitol(typecnt), tzh.tzh_typecnt);
   1468 	convert(eitol(charcnt), tzh.tzh_charcnt);
   1469 #define DO(field)	(void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
   1470 	DO(tzh_reserved);
   1471 	DO(tzh_ttisgmtcnt);
   1472 	DO(tzh_ttisstdcnt);
   1473 	DO(tzh_leapcnt);
   1474 	DO(tzh_timecnt);
   1475 	DO(tzh_typecnt);
   1476 	DO(tzh_charcnt);
   1477 #undef DO
   1478 	for (i = 0; i < timecnt; ++i) {
   1479 		j = leapcnt;
   1480 		while (--j >= 0)
   1481 			if (ats[i] >= trans[j]) {
   1482 				ats[i] = tadd(ats[i], corr[j]);
   1483 				break;
   1484 			}
   1485 		puttzcode((long) ats[i], fp);
   1486 	}
   1487 	if (timecnt > 0)
   1488 		(void) fwrite((void *) types, (size_t) sizeof types[0],
   1489 			(size_t) timecnt, fp);
   1490 	for (i = 0; i < typecnt; ++i) {
   1491 		puttzcode((long) gmtoffs[i], fp);
   1492 		(void) putc(isdsts[i], fp);
   1493 		(void) putc(abbrinds[i], fp);
   1494 	}
   1495 	if (charcnt != 0)
   1496 		(void) fwrite((void *) chars, (size_t) sizeof chars[0],
   1497 			(size_t) charcnt, fp);
   1498 	for (i = 0; i < leapcnt; ++i) {
   1499 		if (roll[i]) {
   1500 			if (timecnt == 0 || trans[i] < ats[0]) {
   1501 				j = 0;
   1502 				while (isdsts[j])
   1503 					if (++j >= typecnt) {
   1504 						j = 0;
   1505 						break;
   1506 					}
   1507 			} else {
   1508 				j = 1;
   1509 				while (j < timecnt && trans[i] >= ats[j])
   1510 					++j;
   1511 				j = types[j - 1];
   1512 			}
   1513 			puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
   1514 		} else	puttzcode((long) trans[i], fp);
   1515 		puttzcode((long) corr[i], fp);
   1516 	}
   1517 	for (i = 0; i < typecnt; ++i)
   1518 		(void) putc(ttisstds[i], fp);
   1519 	for (i = 0; i < typecnt; ++i)
   1520 		(void) putc(ttisgmts[i], fp);
   1521 	if (ferror(fp) || fclose(fp)) {
   1522 		(void) fprintf(stderr, _("%s: Error writing %s\n"),
   1523 			progname, fullname);
   1524 		(void) exit(EXIT_FAILURE);
   1525 	}
   1526 }
   1527 
   1528 static void
   1529 doabbr(abbr, abbrlen, format, letters, isdst)
   1530 char * const		abbr;
   1531 const int		abbrlen;
   1532 const char * const	format;
   1533 const char * const	letters;
   1534 const int		isdst;
   1535 {
   1536 	if (strchr(format, '/') == NULL) {
   1537 		if (letters == NULL)
   1538 			(void)strncpy(abbr, format, abbrlen - 1);
   1539 		else
   1540 			(void)snprintf(abbr, abbrlen, format, letters);
   1541 	} else if (isdst)
   1542 		(void)strncpy(abbr, strchr(format, '/') + 1, abbrlen - 1);
   1543 	else {
   1544 		(void)strncpy(abbr, format, abbrlen - 1);
   1545 		*strchr(abbr, '/') = '\0';
   1546 	}
   1547 }
   1548 
   1549 static void
   1550 outzone(zpfirst, zonecount)
   1551 const struct zone * const	zpfirst;
   1552 const int			zonecount;
   1553 {
   1554 	register const struct zone *	zp;
   1555 	register struct rule *		rp;
   1556 	register int			i, j;
   1557 	register int			usestart, useuntil;
   1558 	register time_t			starttime, untiltime;
   1559 	register long			gmtoff;
   1560 	register long			stdoff;
   1561 	register int			year;
   1562 	register long			startoff;
   1563 	register int			startttisstd;
   1564 	register int			startttisgmt;
   1565 	register int			type;
   1566 	char				startbuf[BUFSIZ];
   1567 
   1568 	INITIALIZE(untiltime);
   1569 	INITIALIZE(starttime);
   1570 	/*
   1571 	** Now. . .finally. . .generate some useful data!
   1572 	*/
   1573 	timecnt = 0;
   1574 	typecnt = 0;
   1575 	charcnt = 0;
   1576 	/*
   1577 	** A guess that may well be corrected later.
   1578 	*/
   1579 	stdoff = 0;
   1580 	/*
   1581 	** Thanks to Earl Chew (earl (at) dnd.icp.nec.com.au)
   1582 	** for noting the need to unconditionally initialize startttisstd.
   1583 	*/
   1584 	startttisstd = FALSE;
   1585 	startttisgmt = FALSE;
   1586 	for (i = 0; i < zonecount; ++i) {
   1587 		zp = &zpfirst[i];
   1588 		usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
   1589 		useuntil = i < (zonecount - 1);
   1590 		if (useuntil && zp->z_untiltime <= min_time)
   1591 			continue;
   1592 		gmtoff = zp->z_gmtoff;
   1593 		eat(zp->z_filename, zp->z_linenum);
   1594 		*startbuf = '\0';
   1595 		startoff = zp->z_gmtoff;
   1596 		if (zp->z_nrules == 0) {
   1597 			stdoff = zp->z_stdoff;
   1598 			doabbr(startbuf, sizeof startbuf, zp->z_format,
   1599 				(char *) NULL, stdoff != 0);
   1600 			type = addtype(oadd(zp->z_gmtoff, stdoff),
   1601 				startbuf, stdoff != 0, startttisstd,
   1602 				startttisgmt);
   1603 			if (usestart) {
   1604 				addtt(starttime, type);
   1605 				usestart = FALSE;
   1606 			}
   1607 			else if (stdoff != 0)
   1608 				addtt(min_time, type);
   1609 		} else for (year = min_year; year <= max_year; ++year) {
   1610 			if (useuntil && year > zp->z_untilrule.r_hiyear)
   1611 				break;
   1612 			/*
   1613 			** Mark which rules to do in the current year.
   1614 			** For those to do, calculate rpytime(rp, year);
   1615 			*/
   1616 			for (j = 0; j < zp->z_nrules; ++j) {
   1617 				rp = &zp->z_rules[j];
   1618 				eats(zp->z_filename, zp->z_linenum,
   1619 					rp->r_filename, rp->r_linenum);
   1620 				rp->r_todo = year >= rp->r_loyear &&
   1621 						year <= rp->r_hiyear &&
   1622 						yearistype(year, rp->r_yrtype);
   1623 				if (rp->r_todo)
   1624 					rp->r_temp = rpytime(rp, year);
   1625 			}
   1626 			for ( ; ; ) {
   1627 				register int	k;
   1628 				register time_t	jtime, ktime;
   1629 				register long	offset;
   1630 				char		buf[BUFSIZ];
   1631 
   1632 				INITIALIZE(ktime);
   1633 				if (useuntil) {
   1634 					/*
   1635 					** Turn untiltime into GMT
   1636 					** assuming the current gmtoff and
   1637 					** stdoff values.
   1638 					*/
   1639 					untiltime = zp->z_untiltime;
   1640 					if (!zp->z_untilrule.r_todisgmt)
   1641 						untiltime = tadd(untiltime,
   1642 							-gmtoff);
   1643 					if (!zp->z_untilrule.r_todisstd)
   1644 						untiltime = tadd(untiltime,
   1645 							-stdoff);
   1646 				}
   1647 				/*
   1648 				** Find the rule (of those to do, if any)
   1649 				** that takes effect earliest in the year.
   1650 				*/
   1651 				k = -1;
   1652 				for (j = 0; j < zp->z_nrules; ++j) {
   1653 					rp = &zp->z_rules[j];
   1654 					if (!rp->r_todo)
   1655 						continue;
   1656 					eats(zp->z_filename, zp->z_linenum,
   1657 						rp->r_filename, rp->r_linenum);
   1658 					offset = rp->r_todisgmt ? 0 : gmtoff;
   1659 					if (!rp->r_todisstd)
   1660 						offset = oadd(offset, stdoff);
   1661 					jtime = rp->r_temp;
   1662 					if (jtime == min_time ||
   1663 						jtime == max_time)
   1664 							continue;
   1665 					jtime = tadd(jtime, -offset);
   1666 					if (k < 0 || jtime < ktime) {
   1667 						k = j;
   1668 						ktime = jtime;
   1669 					}
   1670 				}
   1671 				if (k < 0)
   1672 					break;	/* go on to next year */
   1673 				rp = &zp->z_rules[k];
   1674 				rp->r_todo = FALSE;
   1675 				if (useuntil && ktime >= untiltime)
   1676 					break;
   1677 				stdoff = rp->r_stdoff;
   1678 				if (usestart && ktime == starttime)
   1679 					usestart = FALSE;
   1680 				if (usestart) {
   1681 					if (ktime < starttime) {
   1682 						startoff = oadd(zp->z_gmtoff,
   1683 							stdoff);
   1684 						doabbr(startbuf,sizeof startbuf,
   1685 							zp->z_format,
   1686 							rp->r_abbrvar,
   1687 							rp->r_stdoff != 0);
   1688 						continue;
   1689 					}
   1690 					if (*startbuf == '\0' &&
   1691 					    startoff == oadd(zp->z_gmtoff,
   1692 					    stdoff)) {
   1693 						doabbr(startbuf,sizeof startbuf,
   1694 							zp->z_format,
   1695 							rp->r_abbrvar,
   1696 							rp->r_stdoff != 0);
   1697 					}
   1698 				}
   1699 				eats(zp->z_filename, zp->z_linenum,
   1700 					rp->r_filename, rp->r_linenum);
   1701 				doabbr(buf, sizeof buf, zp->z_format,
   1702 					rp->r_abbrvar, rp->r_stdoff != 0);
   1703 				offset = oadd(zp->z_gmtoff, rp->r_stdoff);
   1704 				type = addtype(offset, buf, rp->r_stdoff != 0,
   1705 					rp->r_todisstd, rp->r_todisgmt);
   1706 				addtt(ktime, type);
   1707 			}
   1708 		}
   1709 		if (usestart) {
   1710 			if (*startbuf == '\0' &&
   1711 				zp->z_format != NULL &&
   1712 				strchr(zp->z_format, '%') == NULL &&
   1713 				strchr(zp->z_format, '/') == NULL)
   1714 					(void)strncpy(startbuf, zp->z_format,
   1715 					    sizeof(startbuf) - 1);
   1716 			eat(zp->z_filename, zp->z_linenum);
   1717 			if (*startbuf == '\0')
   1718 error(_("can't determine time zone abbreviation to use just after until time"));
   1719 			else	addtt(starttime,
   1720 					addtype(startoff, startbuf,
   1721 						startoff != zp->z_gmtoff,
   1722 						startttisstd,
   1723 						startttisgmt));
   1724 		}
   1725 		/*
   1726 		** Now we may get to set starttime for the next zone line.
   1727 		*/
   1728 		if (useuntil) {
   1729 			startttisstd = zp->z_untilrule.r_todisstd;
   1730 			startttisgmt = zp->z_untilrule.r_todisgmt;
   1731 			starttime = zp->z_untiltime;
   1732 			if (!startttisstd)
   1733 				starttime = tadd(starttime, -stdoff);
   1734 			if (!startttisgmt)
   1735 				starttime = tadd(starttime, -gmtoff);
   1736 		}
   1737 	}
   1738 	writezone(zpfirst->z_name);
   1739 }
   1740 
   1741 static void
   1742 addtt(starttime, type)
   1743 const time_t	starttime;
   1744 int		type;
   1745 {
   1746 	if (starttime <= min_time ||
   1747 		(timecnt == 1 && attypes[0].at < min_time)) {
   1748 		gmtoffs[0] = gmtoffs[type];
   1749 		isdsts[0] = isdsts[type];
   1750 		ttisstds[0] = ttisstds[type];
   1751 		ttisgmts[0] = ttisgmts[type];
   1752 		if (abbrinds[type] != 0)
   1753 			(void) strcpy(chars, &chars[abbrinds[type]]);
   1754 		abbrinds[0] = 0;
   1755 		charcnt = strlen(chars) + 1;
   1756 		typecnt = 1;
   1757 		timecnt = 0;
   1758 		type = 0;
   1759 	}
   1760 	if (timecnt >= TZ_MAX_TIMES) {
   1761 		error(_("too many transitions?!"));
   1762 		(void) exit(EXIT_FAILURE);
   1763 	}
   1764 	attypes[timecnt].at = starttime;
   1765 	attypes[timecnt].type = type;
   1766 	++timecnt;
   1767 }
   1768 
   1769 static int
   1770 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
   1771 const long		gmtoff;
   1772 const char * const	abbr;
   1773 const int		isdst;
   1774 const int		ttisstd;
   1775 const int		ttisgmt;
   1776 {
   1777 	register int	i, j;
   1778 
   1779 	if (isdst != TRUE && isdst != FALSE) {
   1780 		error(_("internal error - addtype called with bad isdst"));
   1781 		(void) exit(EXIT_FAILURE);
   1782 	}
   1783 	if (ttisstd != TRUE && ttisstd != FALSE) {
   1784 		error(_("internal error - addtype called with bad ttisstd"));
   1785 		(void) exit(EXIT_FAILURE);
   1786 	}
   1787 	if (ttisgmt != TRUE && ttisgmt != FALSE) {
   1788 		error(_("internal error - addtype called with bad ttisgmt"));
   1789 		(void) exit(EXIT_FAILURE);
   1790 	}
   1791 	/*
   1792 	** See if there's already an entry for this zone type.
   1793 	** If so, just return its index.
   1794 	*/
   1795 	for (i = 0; i < typecnt; ++i) {
   1796 		if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
   1797 			strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
   1798 			ttisstd == ttisstds[i] &&
   1799 			ttisgmt == ttisgmts[i])
   1800 				return i;
   1801 	}
   1802 	/*
   1803 	** There isn't one; add a new one, unless there are already too
   1804 	** many.
   1805 	*/
   1806 	if (typecnt >= TZ_MAX_TYPES) {
   1807 		error(_("too many local time types"));
   1808 		(void) exit(EXIT_FAILURE);
   1809 	}
   1810 	gmtoffs[i] = gmtoff;
   1811 	isdsts[i] = isdst;
   1812 	ttisstds[i] = ttisstd;
   1813 	ttisgmts[i] = ttisgmt;
   1814 
   1815 	for (j = 0; j < charcnt; ++j)
   1816 		if (strcmp(&chars[j], abbr) == 0)
   1817 			break;
   1818 	if (j == charcnt)
   1819 		newabbr(abbr);
   1820 	abbrinds[i] = j;
   1821 	++typecnt;
   1822 	return i;
   1823 }
   1824 
   1825 static void
   1826 leapadd(t, positive, rolling, count)
   1827 const time_t	t;
   1828 const int	positive;
   1829 const int	rolling;
   1830 int		count;
   1831 {
   1832 	register int	i, j;
   1833 
   1834 	if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
   1835 		error(_("too many leap seconds"));
   1836 		(void) exit(EXIT_FAILURE);
   1837 	}
   1838 	for (i = 0; i < leapcnt; ++i)
   1839 		if (t <= trans[i]) {
   1840 			if (t == trans[i]) {
   1841 				error(_("repeated leap second moment"));
   1842 				(void) exit(EXIT_FAILURE);
   1843 			}
   1844 			break;
   1845 		}
   1846 	do {
   1847 		for (j = leapcnt; j > i; --j) {
   1848 			trans[j] = trans[j - 1];
   1849 			corr[j] = corr[j - 1];
   1850 			roll[j] = roll[j - 1];
   1851 		}
   1852 		trans[i] = t;
   1853 		corr[i] = positive ? 1L : eitol(-count);
   1854 		roll[i] = rolling;
   1855 		++leapcnt;
   1856 	} while (positive && --count != 0);
   1857 }
   1858 
   1859 static void
   1860 adjleap P((void))
   1861 {
   1862 	register int	i;
   1863 	register long	last = 0;
   1864 
   1865 	/*
   1866 	** propagate leap seconds forward
   1867 	*/
   1868 	for (i = 0; i < leapcnt; ++i) {
   1869 		trans[i] = tadd(trans[i], last);
   1870 		last = corr[i] += last;
   1871 	}
   1872 }
   1873 
   1874 static int
   1875 yearistype(year, type)
   1876 const int		year;
   1877 const char * const	type;
   1878 {
   1879 	static char *	buf;
   1880 	int		result;
   1881 
   1882 	if (type == NULL || *type == '\0')
   1883 		return TRUE;
   1884 	buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
   1885 	(void)sprintf(buf, "%s %d %s", yitcommand, year, type); /* XXX: sprintf is safe */
   1886 	result = system(buf);
   1887 	if (result == 0)
   1888 		return TRUE;
   1889 	if (result == (1 << 8))
   1890 		return FALSE;
   1891 	error(_("Wild result from command execution"));
   1892 	(void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
   1893 		progname, buf, result);
   1894 	for ( ; ; )
   1895 		(void) exit(EXIT_FAILURE);
   1896 }
   1897 
   1898 static int
   1899 lowerit(a)
   1900 int	a;
   1901 {
   1902 	a = (unsigned char) a;
   1903 	return (isascii(a) && isupper(a)) ? tolower(a) : a;
   1904 }
   1905 
   1906 static int
   1907 ciequal(ap, bp)		/* case-insensitive equality */
   1908 register const char *	ap;
   1909 register const char *	bp;
   1910 {
   1911 	while (lowerit(*ap) == lowerit(*bp++))
   1912 		if (*ap++ == '\0')
   1913 			return TRUE;
   1914 	return FALSE;
   1915 }
   1916 
   1917 static int
   1918 itsabbr(abbr, word)
   1919 register const char *	abbr;
   1920 register const char *	word;
   1921 {
   1922 	if (lowerit(*abbr) != lowerit(*word))
   1923 		return FALSE;
   1924 	++word;
   1925 	while (*++abbr != '\0')
   1926 		do {
   1927 			if (*word == '\0')
   1928 				return FALSE;
   1929 		} while (lowerit(*word++) != lowerit(*abbr));
   1930 	return TRUE;
   1931 }
   1932 
   1933 static const struct lookup *
   1934 byword(word, table)
   1935 register const char * const		word;
   1936 register const struct lookup * const	table;
   1937 {
   1938 	register const struct lookup *	foundlp;
   1939 	register const struct lookup *	lp;
   1940 
   1941 	if (word == NULL || table == NULL)
   1942 		return NULL;
   1943 	/*
   1944 	** Look for exact match.
   1945 	*/
   1946 	for (lp = table; lp->l_word != NULL; ++lp)
   1947 		if (ciequal(word, lp->l_word))
   1948 			return lp;
   1949 	/*
   1950 	** Look for inexact match.
   1951 	*/
   1952 	foundlp = NULL;
   1953 	for (lp = table; lp->l_word != NULL; ++lp)
   1954 		if (itsabbr(word, lp->l_word))
   1955 			if (foundlp == NULL)
   1956 				foundlp = lp;
   1957 			else	return NULL;	/* multiple inexact matches */
   1958 	return foundlp;
   1959 }
   1960 
   1961 static char **
   1962 getfields(cp)
   1963 register char *	cp;
   1964 {
   1965 	register char *		dp;
   1966 	register char **	array;
   1967 	register int		nsubs;
   1968 
   1969 	if (cp == NULL)
   1970 		return NULL;
   1971 	array = (char **) (void *)
   1972 		emalloc((int) ((strlen(cp) + 1) * sizeof *array));
   1973 	nsubs = 0;
   1974 	for ( ; ; ) {
   1975 		while (isascii(*cp) && isspace((unsigned char) *cp))
   1976 			++cp;
   1977 		if (*cp == '\0' || *cp == '#')
   1978 			break;
   1979 		array[nsubs++] = dp = cp;
   1980 		do {
   1981 			if ((*dp = *cp++) != '"')
   1982 				++dp;
   1983 			else while ((*dp = *cp++) != '"')
   1984 				if (*dp != '\0')
   1985 					++dp;
   1986 				else	error(_("Odd number of quotation marks"));
   1987 		} while (*cp != '\0' && *cp != '#' &&
   1988 			(!isascii(*cp) || !isspace((unsigned char) *cp)));
   1989 		if (isascii(*cp) && isspace((unsigned char) *cp))
   1990 			++cp;
   1991 		*dp = '\0';
   1992 	}
   1993 	array[nsubs] = NULL;
   1994 	return array;
   1995 }
   1996 
   1997 static long
   1998 oadd(t1, t2)
   1999 const long	t1;
   2000 const long	t2;
   2001 {
   2002 	register long	t;
   2003 
   2004 	t = t1 + t2;
   2005 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
   2006 		error(_("time overflow"));
   2007 		(void) exit(EXIT_FAILURE);
   2008 	}
   2009 	return t;
   2010 }
   2011 
   2012 static time_t
   2013 tadd(t1, t2)
   2014 const time_t	t1;
   2015 const long	t2;
   2016 {
   2017 	register time_t	t;
   2018 
   2019 	if (t1 == max_time && t2 > 0)
   2020 		return max_time;
   2021 	if (t1 == min_time && t2 < 0)
   2022 		return min_time;
   2023 	t = t1 + t2;
   2024 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
   2025 		error(_("time overflow"));
   2026 		(void) exit(EXIT_FAILURE);
   2027 	}
   2028 	return t;
   2029 }
   2030 
   2031 /*
   2032 ** Given a rule, and a year, compute the date - in seconds since January 1,
   2033 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
   2034 */
   2035 
   2036 static time_t
   2037 rpytime(rp, wantedy)
   2038 register const struct rule * const	rp;
   2039 register const int			wantedy;
   2040 {
   2041 	register int	y, m, i;
   2042 	register long	dayoff;			/* with a nod to Margaret O. */
   2043 	register time_t	t;
   2044 
   2045 	if (wantedy == INT_MIN)
   2046 		return min_time;
   2047 	if (wantedy == INT_MAX)
   2048 		return max_time;
   2049 	dayoff = 0;
   2050 	m = TM_JANUARY;
   2051 	y = EPOCH_YEAR;
   2052 	while (wantedy != y) {
   2053 		if (wantedy > y) {
   2054 			i = len_years[isleap(y)];
   2055 			++y;
   2056 		} else {
   2057 			--y;
   2058 			i = -len_years[isleap(y)];
   2059 		}
   2060 		dayoff = oadd(dayoff, eitol(i));
   2061 	}
   2062 	while (m != rp->r_month) {
   2063 		i = len_months[isleap(y)][m];
   2064 		dayoff = oadd(dayoff, eitol(i));
   2065 		++m;
   2066 	}
   2067 	i = rp->r_dayofmonth;
   2068 	if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
   2069 		if (rp->r_dycode == DC_DOWLEQ)
   2070 			--i;
   2071 		else {
   2072 			error(_("use of 2/29 in non leap-year"));
   2073 			(void) exit(EXIT_FAILURE);
   2074 		}
   2075 	}
   2076 	--i;
   2077 	dayoff = oadd(dayoff, eitol(i));
   2078 	if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
   2079 		register long	wday;
   2080 
   2081 #define LDAYSPERWEEK	((long) DAYSPERWEEK)
   2082 		wday = eitol(EPOCH_WDAY);
   2083 		/*
   2084 		** Don't trust mod of negative numbers.
   2085 		*/
   2086 		if (dayoff >= 0)
   2087 			wday = (wday + dayoff) % LDAYSPERWEEK;
   2088 		else {
   2089 			wday -= ((-dayoff) % LDAYSPERWEEK);
   2090 			if (wday < 0)
   2091 				wday += LDAYSPERWEEK;
   2092 		}
   2093 		while (wday != eitol(rp->r_wday))
   2094 			if (rp->r_dycode == DC_DOWGEQ) {
   2095 				dayoff = oadd(dayoff, (long) 1);
   2096 				if (++wday >= LDAYSPERWEEK)
   2097 					wday = 0;
   2098 				++i;
   2099 			} else {
   2100 				dayoff = oadd(dayoff, (long) -1);
   2101 				if (--wday < 0)
   2102 					wday = LDAYSPERWEEK - 1;
   2103 				--i;
   2104 			}
   2105 		if (i < 0 || i >= len_months[isleap(y)][m]) {
   2106 			error(_("no day in month matches rule"));
   2107 			(void) exit(EXIT_FAILURE);
   2108 		}
   2109 	}
   2110 	if (dayoff < 0 && !TYPE_SIGNED(time_t))
   2111 		return min_time;
   2112 	t = (time_t) dayoff * SECSPERDAY;
   2113 	/*
   2114 	** Cheap overflow check.
   2115 	*/
   2116 	if (t / SECSPERDAY != dayoff)
   2117 		return (dayoff > 0) ? max_time : min_time;
   2118 	return tadd(t, rp->r_tod);
   2119 }
   2120 
   2121 static void
   2122 newabbr(string)
   2123 const char * const	string;
   2124 {
   2125 	register int	i;
   2126 
   2127 	i = strlen(string) + 1;
   2128 	if (charcnt + i > TZ_MAX_CHARS) {
   2129 		error(_("too many, or too long, time zone abbreviations"));
   2130 		(void) exit(EXIT_FAILURE);
   2131 	}
   2132 	(void)strncpy(&chars[charcnt], string, sizeof(chars) - charcnt - 1);
   2133 	charcnt += eitol(i);
   2134 }
   2135 
   2136 static int
   2137 mkdirs(argname)
   2138 char * const	argname;
   2139 {
   2140 	register char *	name;
   2141 	register char *	cp;
   2142 
   2143 	if (argname == NULL || *argname == '\0')
   2144 		return 0;
   2145 	cp = name = ecpyalloc(argname);
   2146 	while ((cp = strchr(cp + 1, '/')) != 0) {
   2147 		*cp = '\0';
   2148 #ifndef unix
   2149 		/*
   2150 		** DOS drive specifier?
   2151 		*/
   2152 		if (isalpha((unsigned char) name[0]) &&
   2153 			name[1] == ':' && name[2] == '\0') {
   2154 				*cp = '/';
   2155 				continue;
   2156 		}
   2157 #endif /* !defined unix */
   2158 		if (!itsdir(name)) {
   2159 			/*
   2160 			** It doesn't seem to exist, so we try to create it.
   2161 			** Creation may fail because of the directory being
   2162 			** created by some other multiprocessor, so we get
   2163 			** to do extra checking.
   2164 			*/
   2165 			if (mkdir(name, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) != 0) {
   2166 				const char *e = strerror(errno);
   2167 
   2168 				if (errno != EEXIST || !itsdir(name)) {
   2169 					(void) fprintf(stderr,
   2170 _("%s: Can't create directory %s: %s\n"),
   2171 						progname, name, e);
   2172 					ifree(name);
   2173 					return -1;
   2174 				}
   2175 			}
   2176 		}
   2177 		*cp = '/';
   2178 	}
   2179 	ifree(name);
   2180 	return 0;
   2181 }
   2182 
   2183 static long
   2184 eitol(i)
   2185 const int	i;
   2186 {
   2187 	long	l;
   2188 
   2189 	l = i;
   2190 	if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
   2191 		(void) fprintf(stderr,
   2192 			_("%s: %d did not sign extend correctly\n"),
   2193 			progname, i);
   2194 		(void) exit(EXIT_FAILURE);
   2195 	}
   2196 	return l;
   2197 }
   2198 
   2199 /*
   2200 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.
   2201 */
   2202