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