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