xref: /titanic_41/usr/src/cmd/zic/zic.c (revision 5f0cdc3a8bd2b7fbf5fdfc47052d70a7e3cd12e8)
17c478bd9Sstevel@tonic-gate /*
2a68d064cSrobbin  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
380868c53Srobbin  * Use is subject to license terms.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
6a68d064cSrobbin static char    elsieid[] = "@(#)zic.c  7.128.1";
77c478bd9Sstevel@tonic-gate 
87c478bd9Sstevel@tonic-gate /*
97c478bd9Sstevel@tonic-gate  * #define	LEAPSECOND_SUPPORT
107c478bd9Sstevel@tonic-gate  */
117c478bd9Sstevel@tonic-gate 
1280868c53Srobbin /*
1380868c53Srobbin  * Regardless of the type of time_t, we do our work using this type.
1480868c53Srobbin  */
1580868c53Srobbin 
1680868c53Srobbin typedef int	zic_t;
1780868c53Srobbin 
187c478bd9Sstevel@tonic-gate #include "private.h"
197c478bd9Sstevel@tonic-gate #include <tzfile.h>			/* this is in system headers at Sun */
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #include <sys/stat.h>			/* for umask manifest constants */
227c478bd9Sstevel@tonic-gate #include <ctype.h>
237c478bd9Sstevel@tonic-gate #include <locale.h>
247c478bd9Sstevel@tonic-gate #include <stdlib.h>			/* for getopt */
257c478bd9Sstevel@tonic-gate 
2680868c53Srobbin #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
2780868c53Srobbin #define	ZIC_MAX_ABBR_LEN_WO_WARN	6
2880868c53Srobbin #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
2980868c53Srobbin 
3080868c53Srobbin #ifdef S_IRUSR
3180868c53Srobbin #define	MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
3280868c53Srobbin #else
3380868c53Srobbin #define	MKDIR_UMASK 0755
3480868c53Srobbin #endif
3580868c53Srobbin 
367c478bd9Sstevel@tonic-gate struct rule {
377c478bd9Sstevel@tonic-gate 	const char	*r_filename;
387c478bd9Sstevel@tonic-gate 	int		r_linenum;
397c478bd9Sstevel@tonic-gate 	const char	*r_name;
407c478bd9Sstevel@tonic-gate 
417c478bd9Sstevel@tonic-gate 	int		r_loyear;	/* for example, 1986 */
427c478bd9Sstevel@tonic-gate 	int		r_hiyear;	/* for example, 1986 */
437c478bd9Sstevel@tonic-gate 	const char	*r_yrtype;
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate 	int		r_month;	/* 0..11 */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 	int		r_dycode;	/* see below */
487c478bd9Sstevel@tonic-gate 	int		r_dayofmonth;
497c478bd9Sstevel@tonic-gate 	int		r_wday;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	long		r_tod;		/* time from midnight */
527c478bd9Sstevel@tonic-gate 	int		r_todisstd;	/* above is standard time if TRUE */
537c478bd9Sstevel@tonic-gate 					/* or wall clock time if FALSE */
547c478bd9Sstevel@tonic-gate 	int		r_todisgmt;	/* above is GMT if TRUE */
557c478bd9Sstevel@tonic-gate 					/* or local time if FALSE */
567c478bd9Sstevel@tonic-gate 	long		r_stdoff;	/* offset from standard time */
577c478bd9Sstevel@tonic-gate 	const char	*r_abbrvar;	/* variable part of abbreviation */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	int		r_todo;		/* a rule to do (used in outzone) */
6080868c53Srobbin 	zic_t		r_temp;		/* used in outzone */
617c478bd9Sstevel@tonic-gate };
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate  *	r_dycode		r_dayofmonth	r_wday
657c478bd9Sstevel@tonic-gate  */
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate #define	DC_DOM		0	/* 1..31 */	/* unused */
687c478bd9Sstevel@tonic-gate #define	DC_DOWGEQ	1	/* 1..31 */	/* 0..6 (Sun..Sat) */
697c478bd9Sstevel@tonic-gate #define	DC_DOWLEQ	2	/* 1..31 */	/* 0..6 (Sun..Sat) */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate struct zone {
727c478bd9Sstevel@tonic-gate 	const char	*z_filename;
737c478bd9Sstevel@tonic-gate 	int		z_linenum;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	const char	*z_name;
767c478bd9Sstevel@tonic-gate 	long		z_gmtoff;
777c478bd9Sstevel@tonic-gate 	const char	*z_rule;
787c478bd9Sstevel@tonic-gate 	const char	*z_format;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate 	long		z_stdoff;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	struct rule	*z_rules;
837c478bd9Sstevel@tonic-gate 	int		z_nrules;
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate 	struct rule	z_untilrule;
8680868c53Srobbin 	zic_t		z_untiltime;
877c478bd9Sstevel@tonic-gate };
887c478bd9Sstevel@tonic-gate 
8980868c53Srobbin static void	addtt(zic_t starttime, int type);
907c478bd9Sstevel@tonic-gate static int	addtype(long gmtoff, const char *abbr, int isdst,
917c478bd9Sstevel@tonic-gate 				int ttisstd, int ttisgmt);
927c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
9380868c53Srobbin static void	leapadd(zic_t t, int positive, int rolling, int count);
947c478bd9Sstevel@tonic-gate static void	adjleap(void);
957c478bd9Sstevel@tonic-gate #endif
967c478bd9Sstevel@tonic-gate static void	associate(void);
977c478bd9Sstevel@tonic-gate static int	ciequal(const char *ap, const char *bp);
987c478bd9Sstevel@tonic-gate static void	convert(long val, char *buf);
99a68d064cSrobbin static void	dolink(const char *fromfield, const char *tofield);
1007c478bd9Sstevel@tonic-gate static void	doabbr(char *abbr, const char *format,
1017c478bd9Sstevel@tonic-gate 			const char *letters, int isdst);
1027c478bd9Sstevel@tonic-gate static void	eat(const char *name, int num);
1037c478bd9Sstevel@tonic-gate static void	eats(const char *name, int num,
1047c478bd9Sstevel@tonic-gate 			const char *rname, int rnum);
1057c478bd9Sstevel@tonic-gate static long	eitol(int i);
1067c478bd9Sstevel@tonic-gate static void	error(const char *message);
1077c478bd9Sstevel@tonic-gate static char	**getfields(char *buf);
1087c478bd9Sstevel@tonic-gate static long	gethms(const char *string, const char *errstrng, int signable);
1097c478bd9Sstevel@tonic-gate static void	infile(const char *filename);
1107c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
1117c478bd9Sstevel@tonic-gate static void	inleap(char **fields, int nfields);
1127c478bd9Sstevel@tonic-gate #endif
1137c478bd9Sstevel@tonic-gate static void	inlink(char **fields, int nfields);
1147c478bd9Sstevel@tonic-gate static void	inrule(char **fields, int nfields);
1157c478bd9Sstevel@tonic-gate static int	inzcont(char **fields, int nfields);
1167c478bd9Sstevel@tonic-gate static int	inzone(char **fields, int nfields);
1177c478bd9Sstevel@tonic-gate static int	inzsub(char **fields, int nfields, int iscont);
1187c478bd9Sstevel@tonic-gate static int	itsabbr(const char *abbr, const char *word);
1197c478bd9Sstevel@tonic-gate static int	itsdir(const char *name);
1207c478bd9Sstevel@tonic-gate static int	lowerit(int c);
1217c478bd9Sstevel@tonic-gate static char	*memcheck(char *tocheck);
1227c478bd9Sstevel@tonic-gate static int	mkdirs(char *filename);
1237c478bd9Sstevel@tonic-gate static void	newabbr(const char *abbr);
1247c478bd9Sstevel@tonic-gate static long	oadd(long t1, long t2);
1257c478bd9Sstevel@tonic-gate static void	outzone(const struct zone *zp, int ntzones);
1267c478bd9Sstevel@tonic-gate static void	puttzcode(long code, FILE *fp);
1277c478bd9Sstevel@tonic-gate static int	rcomp(const void *leftp, const void *rightp);
12880868c53Srobbin static zic_t	rpytime(const struct rule *rp, int wantedy);
1297c478bd9Sstevel@tonic-gate static void	rulesub(struct rule *rp,
1307c478bd9Sstevel@tonic-gate 			const char *loyearp, const char *hiyearp,
1317c478bd9Sstevel@tonic-gate 			const char *typep, const char *monthp,
1327c478bd9Sstevel@tonic-gate 			const char *dayp, const char *timep);
1337c478bd9Sstevel@tonic-gate static void	setboundaries(void);
13480868c53Srobbin static zic_t	tadd(zic_t t1, long t2);
1357c478bd9Sstevel@tonic-gate static void	usage(void);
1367c478bd9Sstevel@tonic-gate static void	writezone(const char *name);
1377c478bd9Sstevel@tonic-gate static int	yearistype(int year, const char *type);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static int		charcnt;
1407c478bd9Sstevel@tonic-gate static int		errors;
1417c478bd9Sstevel@tonic-gate static const char	*filename;
1427c478bd9Sstevel@tonic-gate static int		leapcnt;
1437c478bd9Sstevel@tonic-gate static int		linenum;
14480868c53Srobbin static zic_t		max_time;
1457c478bd9Sstevel@tonic-gate static int		max_year;
1467c478bd9Sstevel@tonic-gate static int		max_year_representable;
14780868c53Srobbin static zic_t		min_time;
1487c478bd9Sstevel@tonic-gate static int		min_year;
1497c478bd9Sstevel@tonic-gate static int		min_year_representable;
1507c478bd9Sstevel@tonic-gate static int		noise;
1517c478bd9Sstevel@tonic-gate static const char	*rfilename;
1527c478bd9Sstevel@tonic-gate static int		rlinenum;
1537c478bd9Sstevel@tonic-gate static const char	*progname;
1547c478bd9Sstevel@tonic-gate static int		timecnt;
1557c478bd9Sstevel@tonic-gate static int		typecnt;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate /*
1587c478bd9Sstevel@tonic-gate  * Line codes.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate #define	LC_RULE		0
1627c478bd9Sstevel@tonic-gate #define	LC_ZONE		1
1637c478bd9Sstevel@tonic-gate #define	LC_LINK		2
1647c478bd9Sstevel@tonic-gate #define	LC_LEAP		3
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate /*
1677c478bd9Sstevel@tonic-gate  * Which fields are which on a Zone line.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #define	ZF_NAME		1
1717c478bd9Sstevel@tonic-gate #define	ZF_GMTOFF	2
1727c478bd9Sstevel@tonic-gate #define	ZF_RULE		3
1737c478bd9Sstevel@tonic-gate #define	ZF_FORMAT	4
1747c478bd9Sstevel@tonic-gate #define	ZF_TILYEAR	5
1757c478bd9Sstevel@tonic-gate #define	ZF_TILMONTH	6
1767c478bd9Sstevel@tonic-gate #define	ZF_TILDAY	7
1777c478bd9Sstevel@tonic-gate #define	ZF_TILTIME	8
1787c478bd9Sstevel@tonic-gate #define	ZONE_MINFIELDS	5
1797c478bd9Sstevel@tonic-gate #define	ZONE_MAXFIELDS	9
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Which fields are which on a Zone continuation line.
1837c478bd9Sstevel@tonic-gate  */
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate #define	ZFC_GMTOFF	0
1867c478bd9Sstevel@tonic-gate #define	ZFC_RULE	1
1877c478bd9Sstevel@tonic-gate #define	ZFC_FORMAT	2
1887c478bd9Sstevel@tonic-gate #define	ZFC_TILYEAR	3
1897c478bd9Sstevel@tonic-gate #define	ZFC_TILMONTH	4
1907c478bd9Sstevel@tonic-gate #define	ZFC_TILDAY	5
1917c478bd9Sstevel@tonic-gate #define	ZFC_TILTIME	6
1927c478bd9Sstevel@tonic-gate #define	ZONEC_MINFIELDS	3
1937c478bd9Sstevel@tonic-gate #define	ZONEC_MAXFIELDS	7
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * Which files are which on a Rule line.
1977c478bd9Sstevel@tonic-gate  */
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate #define	RF_NAME		1
2007c478bd9Sstevel@tonic-gate #define	RF_LOYEAR	2
2017c478bd9Sstevel@tonic-gate #define	RF_HIYEAR	3
2027c478bd9Sstevel@tonic-gate #define	RF_COMMAND	4
2037c478bd9Sstevel@tonic-gate #define	RF_MONTH	5
2047c478bd9Sstevel@tonic-gate #define	RF_DAY		6
2057c478bd9Sstevel@tonic-gate #define	RF_TOD		7
2067c478bd9Sstevel@tonic-gate #define	RF_STDOFF	8
2077c478bd9Sstevel@tonic-gate #define	RF_ABBRVAR	9
2087c478bd9Sstevel@tonic-gate #define	RULE_FIELDS	10
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate /*
2117c478bd9Sstevel@tonic-gate  * Which fields are which on a Link line.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate 
2147c478bd9Sstevel@tonic-gate #define	LF_FROM		1
2157c478bd9Sstevel@tonic-gate #define	LF_TO		2
2167c478bd9Sstevel@tonic-gate #define	LINK_FIELDS	3
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate /*
2197c478bd9Sstevel@tonic-gate  * Which fields are which on a Leap line.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #define	LP_YEAR		1
2237c478bd9Sstevel@tonic-gate #define	LP_MONTH	2
2247c478bd9Sstevel@tonic-gate #define	LP_DAY		3
2257c478bd9Sstevel@tonic-gate #define	LP_TIME		4
2267c478bd9Sstevel@tonic-gate #define	LP_CORR		5
2277c478bd9Sstevel@tonic-gate #define	LP_ROLL		6
2287c478bd9Sstevel@tonic-gate #define	LEAP_FIELDS	7
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate /*
2317c478bd9Sstevel@tonic-gate  * Year synonyms.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate #define	YR_MINIMUM	0
2357c478bd9Sstevel@tonic-gate #define	YR_MAXIMUM	1
2367c478bd9Sstevel@tonic-gate #define	YR_ONLY		2
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static struct rule	*rules;
2397c478bd9Sstevel@tonic-gate static int		nrules;	/* number of rules */
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate static struct zone	*zones;
2427c478bd9Sstevel@tonic-gate static int		nzones;	/* number of zones */
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate struct link {
2457c478bd9Sstevel@tonic-gate 	const char	*l_filename;
2467c478bd9Sstevel@tonic-gate 	int		l_linenum;
2477c478bd9Sstevel@tonic-gate 	const char	*l_from;
2487c478bd9Sstevel@tonic-gate 	const char	*l_to;
2497c478bd9Sstevel@tonic-gate };
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate static struct link	*links;
2527c478bd9Sstevel@tonic-gate static int		nlinks;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate struct lookup {
2557c478bd9Sstevel@tonic-gate 	const char	*l_word;
2567c478bd9Sstevel@tonic-gate 	const int	l_value;
2577c478bd9Sstevel@tonic-gate };
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate static struct lookup const *byword(const char *string,
2607c478bd9Sstevel@tonic-gate     const struct lookup *lp);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate static struct lookup const	line_codes[] = {
2637c478bd9Sstevel@tonic-gate 	{ "Rule",	LC_RULE },
2647c478bd9Sstevel@tonic-gate 	{ "Zone",	LC_ZONE },
2657c478bd9Sstevel@tonic-gate 	{ "Link",	LC_LINK },
2667c478bd9Sstevel@tonic-gate 	{ "Leap",	LC_LEAP },
2677c478bd9Sstevel@tonic-gate 	{ NULL,		0}
2687c478bd9Sstevel@tonic-gate };
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate static struct lookup const	mon_names[] = {
2717c478bd9Sstevel@tonic-gate 	{ "January",	TM_JANUARY },
2727c478bd9Sstevel@tonic-gate 	{ "February",	TM_FEBRUARY },
2737c478bd9Sstevel@tonic-gate 	{ "March",	TM_MARCH },
2747c478bd9Sstevel@tonic-gate 	{ "April",	TM_APRIL },
2757c478bd9Sstevel@tonic-gate 	{ "May",	TM_MAY },
2767c478bd9Sstevel@tonic-gate 	{ "June",	TM_JUNE },
2777c478bd9Sstevel@tonic-gate 	{ "July",	TM_JULY },
2787c478bd9Sstevel@tonic-gate 	{ "August",	TM_AUGUST },
2797c478bd9Sstevel@tonic-gate 	{ "September",	TM_SEPTEMBER },
2807c478bd9Sstevel@tonic-gate 	{ "October",	TM_OCTOBER },
2817c478bd9Sstevel@tonic-gate 	{ "November",	TM_NOVEMBER },
2827c478bd9Sstevel@tonic-gate 	{ "December",	TM_DECEMBER },
2837c478bd9Sstevel@tonic-gate 	{ NULL,		0 }
2847c478bd9Sstevel@tonic-gate };
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate static struct lookup const	wday_names[] = {
2877c478bd9Sstevel@tonic-gate 	{ "Sunday",	TM_SUNDAY },
2887c478bd9Sstevel@tonic-gate 	{ "Monday",	TM_MONDAY },
2897c478bd9Sstevel@tonic-gate 	{ "Tuesday",	TM_TUESDAY },
2907c478bd9Sstevel@tonic-gate 	{ "Wednesday",	TM_WEDNESDAY },
2917c478bd9Sstevel@tonic-gate 	{ "Thursday",	TM_THURSDAY },
2927c478bd9Sstevel@tonic-gate 	{ "Friday",	TM_FRIDAY },
2937c478bd9Sstevel@tonic-gate 	{ "Saturday",	TM_SATURDAY },
2947c478bd9Sstevel@tonic-gate 	{ NULL,		0 }
2957c478bd9Sstevel@tonic-gate };
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate static struct lookup const	lasts[] = {
2987c478bd9Sstevel@tonic-gate 	{ "last-Sunday",	TM_SUNDAY },
2997c478bd9Sstevel@tonic-gate 	{ "last-Monday",	TM_MONDAY },
3007c478bd9Sstevel@tonic-gate 	{ "last-Tuesday",	TM_TUESDAY },
3017c478bd9Sstevel@tonic-gate 	{ "last-Wednesday",	TM_WEDNESDAY },
3027c478bd9Sstevel@tonic-gate 	{ "last-Thursday",	TM_THURSDAY },
3037c478bd9Sstevel@tonic-gate 	{ "last-Friday",	TM_FRIDAY },
3047c478bd9Sstevel@tonic-gate 	{ "last-Saturday",	TM_SATURDAY },
3057c478bd9Sstevel@tonic-gate 	{ NULL,			0 }
3067c478bd9Sstevel@tonic-gate };
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static struct lookup const	begin_years[] = {
3097c478bd9Sstevel@tonic-gate 	{ "minimum",	YR_MINIMUM },
3107c478bd9Sstevel@tonic-gate 	{ "maximum",	YR_MAXIMUM },
3117c478bd9Sstevel@tonic-gate 	{ NULL,		0 }
3127c478bd9Sstevel@tonic-gate };
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate static struct lookup const	end_years[] = {
3157c478bd9Sstevel@tonic-gate 	{ "minimum",	YR_MINIMUM },
3167c478bd9Sstevel@tonic-gate 	{ "maximum",	YR_MAXIMUM },
3177c478bd9Sstevel@tonic-gate 	{ "only",	YR_ONLY },
3187c478bd9Sstevel@tonic-gate 	{ NULL,		0 }
3197c478bd9Sstevel@tonic-gate };
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate static struct lookup const	leap_types[] = {
3227c478bd9Sstevel@tonic-gate 	{ "Rolling",	TRUE },
3237c478bd9Sstevel@tonic-gate 	{ "Stationary",	FALSE },
3247c478bd9Sstevel@tonic-gate 	{ NULL,		0 }
3257c478bd9Sstevel@tonic-gate };
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static const int	len_months[2][MONSPERYEAR] = {
3287c478bd9Sstevel@tonic-gate 	{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
3297c478bd9Sstevel@tonic-gate 	{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
3307c478bd9Sstevel@tonic-gate };
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate static const int	len_years[2] = {
3337c478bd9Sstevel@tonic-gate 	DAYSPERNYEAR, DAYSPERLYEAR
3347c478bd9Sstevel@tonic-gate };
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate static struct attype {
33780868c53Srobbin 	zic_t		at;
3387c478bd9Sstevel@tonic-gate 	unsigned char	type;
3397c478bd9Sstevel@tonic-gate }			attypes[TZ_MAX_TIMES];
3407c478bd9Sstevel@tonic-gate static long		gmtoffs[TZ_MAX_TYPES];
3417c478bd9Sstevel@tonic-gate static char		isdsts[TZ_MAX_TYPES];
3427c478bd9Sstevel@tonic-gate static unsigned char	abbrinds[TZ_MAX_TYPES];
3437c478bd9Sstevel@tonic-gate static char		ttisstds[TZ_MAX_TYPES];
3447c478bd9Sstevel@tonic-gate static char		ttisgmts[TZ_MAX_TYPES];
3457c478bd9Sstevel@tonic-gate static char		chars[TZ_MAX_CHARS];
34680868c53Srobbin static zic_t		trans[TZ_MAX_LEAPS];
3477c478bd9Sstevel@tonic-gate static long		corr[TZ_MAX_LEAPS];
3487c478bd9Sstevel@tonic-gate static char		roll[TZ_MAX_LEAPS];
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Memory allocation.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate static char *
memcheck(ptr)3557c478bd9Sstevel@tonic-gate memcheck(ptr)
3567c478bd9Sstevel@tonic-gate char * const	ptr;
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
3597c478bd9Sstevel@tonic-gate 		const char *e = strerror(errno);
3607c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Memory exhausted: %s\n"),
3617c478bd9Sstevel@tonic-gate 			progname, e);
36280868c53Srobbin 		exit(EXIT_FAILURE);
3637c478bd9Sstevel@tonic-gate 	}
3647c478bd9Sstevel@tonic-gate 	return (ptr);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate #define	emalloc(size)		memcheck(imalloc(size))
3687c478bd9Sstevel@tonic-gate #define	erealloc(ptr, size)	memcheck(irealloc((ptr), (size)))
3697c478bd9Sstevel@tonic-gate #define	ecpyalloc(ptr)		memcheck(icpyalloc(ptr))
3707c478bd9Sstevel@tonic-gate #define	ecatalloc(oldp, newp)	memcheck(icatalloc((oldp), (newp)))
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate /*
3737c478bd9Sstevel@tonic-gate  * Error handling.
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate static void
eats(name,num,rname,rnum)3777c478bd9Sstevel@tonic-gate eats(name, num, rname, rnum)
3787c478bd9Sstevel@tonic-gate const char * const	name;
3797c478bd9Sstevel@tonic-gate const int		num;
3807c478bd9Sstevel@tonic-gate const char * const	rname;
3817c478bd9Sstevel@tonic-gate const int		rnum;
3827c478bd9Sstevel@tonic-gate {
3837c478bd9Sstevel@tonic-gate 	filename = name;
3847c478bd9Sstevel@tonic-gate 	linenum = num;
3857c478bd9Sstevel@tonic-gate 	rfilename = rname;
3867c478bd9Sstevel@tonic-gate 	rlinenum = rnum;
3877c478bd9Sstevel@tonic-gate }
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate static void
eat(name,num)3907c478bd9Sstevel@tonic-gate eat(name, num)
3917c478bd9Sstevel@tonic-gate const char * const	name;
3927c478bd9Sstevel@tonic-gate const int		num;
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	eats(name, num, (char *)NULL, -1);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate static void
error(string)3987c478bd9Sstevel@tonic-gate error(string)
3997c478bd9Sstevel@tonic-gate const char * const	string;
4007c478bd9Sstevel@tonic-gate {
4017c478bd9Sstevel@tonic-gate 	/*
4027c478bd9Sstevel@tonic-gate 	 * Match the format of "cc" to allow sh users to
4037c478bd9Sstevel@tonic-gate 	 *	zic ... 2>&1 | error -t "*" -v
4047c478bd9Sstevel@tonic-gate 	 * on BSD systems.
4057c478bd9Sstevel@tonic-gate 	 */
4067c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("\"%s\", line %d: %s"),
4077c478bd9Sstevel@tonic-gate 		filename, linenum, string);
4087c478bd9Sstevel@tonic-gate 	if (rfilename != NULL)
4097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(" (rule from \"%s\", line %d)"),
4107c478bd9Sstevel@tonic-gate 			rfilename, rlinenum);
4117c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n");
4127c478bd9Sstevel@tonic-gate 	++errors;
4137c478bd9Sstevel@tonic-gate }
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate static void
warning(string)4167c478bd9Sstevel@tonic-gate warning(string)
4177c478bd9Sstevel@tonic-gate const char * const	string;
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	char *cp;
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	cp = ecpyalloc(gettext("warning: "));
4227c478bd9Sstevel@tonic-gate 	cp = ecatalloc(cp, string);
4237c478bd9Sstevel@tonic-gate 	error(cp);
4247c478bd9Sstevel@tonic-gate 	ifree(cp);
4257c478bd9Sstevel@tonic-gate 	--errors;
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate static void
usage(void)4297c478bd9Sstevel@tonic-gate usage(void)
4307c478bd9Sstevel@tonic-gate {
4317c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
43280868c53Srobbin 	(void) fprintf(stderr, gettext("%s: usage is %s "
43380868c53Srobbin 	    "[ --version ] [ -s ] [ -v ] [ -l localtime ] "
43480868c53Srobbin 	    "\n\t[ -p posixrules ] [ -d directory ] [ -L leapseconds ] "
43580868c53Srobbin 	    "[ -y yearistype ] [ filename ... ]\n"), progname, progname);
4367c478bd9Sstevel@tonic-gate #else /* ! LEAPSECOND_SUPPORT */
43780868c53Srobbin 	(void) fprintf(stderr, gettext("%s: usage is %s "
43880868c53Srobbin 	    "[ --version ] [ -s ] [ -v ] [ -l localtime ]"
43980868c53Srobbin 	    "\n\t[ -p posixrules ] [ -d directory ] [ -y yearistype ] "
44080868c53Srobbin 	    "[ filename ... ]\n"), progname, progname);
4417c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate static const char	*psxrules;
4457c478bd9Sstevel@tonic-gate static const char	*lcltime;
4467c478bd9Sstevel@tonic-gate static const char	*directory;
4477c478bd9Sstevel@tonic-gate static const char	*leapsec;
4487c478bd9Sstevel@tonic-gate static const char	*yitcommand;
4497c478bd9Sstevel@tonic-gate static int		sflag = FALSE;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate int
main(argc,argv)4527c478bd9Sstevel@tonic-gate main(argc, argv)
4537c478bd9Sstevel@tonic-gate int	argc;
4547c478bd9Sstevel@tonic-gate char	*argv[];
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate 	register int	i;
4577c478bd9Sstevel@tonic-gate 	register int	j;
4587c478bd9Sstevel@tonic-gate 	register int	c;
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	(void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
4637c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
4647c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN	"SYS_TEST"
4657c478bd9Sstevel@tonic-gate #endif
4667c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	progname = argv[0];
46980868c53Srobbin 	for (i = 1; i < argc; ++i)
47080868c53Srobbin 		if (strcmp(argv[i], "--version") == 0) {
47180868c53Srobbin 			(void) printf("%s\n", elsieid);
47280868c53Srobbin 			exit(EXIT_SUCCESS);
47380868c53Srobbin 		}
47480868c53Srobbin 
4757c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
4767c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF)
4777c478bd9Sstevel@tonic-gate #else
4787c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "d:l:p:vsy:")) != EOF)
4797c478bd9Sstevel@tonic-gate #endif
4807c478bd9Sstevel@tonic-gate 		switch (c) {
4817c478bd9Sstevel@tonic-gate 			default:
4827c478bd9Sstevel@tonic-gate 				usage();
4837c478bd9Sstevel@tonic-gate 			case 'd':
4847c478bd9Sstevel@tonic-gate 				if (directory == NULL)
4857c478bd9Sstevel@tonic-gate 					directory = optarg;
4867c478bd9Sstevel@tonic-gate 				else {
4877c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4887c478bd9Sstevel@tonic-gate "%s: More than one -d option specified\n"),
4897c478bd9Sstevel@tonic-gate 						progname);
49080868c53Srobbin 					exit(EXIT_FAILURE);
4917c478bd9Sstevel@tonic-gate 				}
4927c478bd9Sstevel@tonic-gate 				break;
4937c478bd9Sstevel@tonic-gate 			case 'l':
4947c478bd9Sstevel@tonic-gate 				if (lcltime == NULL)
4957c478bd9Sstevel@tonic-gate 					lcltime = optarg;
4967c478bd9Sstevel@tonic-gate 				else {
4977c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
4987c478bd9Sstevel@tonic-gate "%s: More than one -l option specified\n"),
4997c478bd9Sstevel@tonic-gate 						progname);
50080868c53Srobbin 					exit(EXIT_FAILURE);
5017c478bd9Sstevel@tonic-gate 				}
5027c478bd9Sstevel@tonic-gate 				break;
5037c478bd9Sstevel@tonic-gate 			case 'p':
5047c478bd9Sstevel@tonic-gate 				if (psxrules == NULL)
5057c478bd9Sstevel@tonic-gate 					psxrules = optarg;
5067c478bd9Sstevel@tonic-gate 				else {
5077c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5087c478bd9Sstevel@tonic-gate "%s: More than one -p option specified\n"),
5097c478bd9Sstevel@tonic-gate 						progname);
51080868c53Srobbin 					exit(EXIT_FAILURE);
5117c478bd9Sstevel@tonic-gate 				}
5127c478bd9Sstevel@tonic-gate 				break;
5137c478bd9Sstevel@tonic-gate 			case 'y':
5147c478bd9Sstevel@tonic-gate 				if (yitcommand == NULL)
5157c478bd9Sstevel@tonic-gate 					yitcommand = optarg;
5167c478bd9Sstevel@tonic-gate 				else {
5177c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5187c478bd9Sstevel@tonic-gate "%s: More than one -y option specified\n"),
5197c478bd9Sstevel@tonic-gate 						progname);
52080868c53Srobbin 					exit(EXIT_FAILURE);
5217c478bd9Sstevel@tonic-gate 				}
5227c478bd9Sstevel@tonic-gate 				break;
5237c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
5247c478bd9Sstevel@tonic-gate 			case 'L':
5257c478bd9Sstevel@tonic-gate 				if (leapsec == NULL)
5267c478bd9Sstevel@tonic-gate 					leapsec = optarg;
5277c478bd9Sstevel@tonic-gate 				else {
5287c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
5297c478bd9Sstevel@tonic-gate "%s: More than one -L option specified\n"),
5307c478bd9Sstevel@tonic-gate 						progname);
53180868c53Srobbin 					exit(EXIT_FAILURE);
5327c478bd9Sstevel@tonic-gate 				}
5337c478bd9Sstevel@tonic-gate 				break;
5347c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
5357c478bd9Sstevel@tonic-gate 			case 'v':
5367c478bd9Sstevel@tonic-gate 				noise = TRUE;
5377c478bd9Sstevel@tonic-gate 				break;
5387c478bd9Sstevel@tonic-gate 			case 's':
5397c478bd9Sstevel@tonic-gate 				sflag = TRUE;
5407c478bd9Sstevel@tonic-gate 				break;
5417c478bd9Sstevel@tonic-gate 		}
5427c478bd9Sstevel@tonic-gate 	if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
5437c478bd9Sstevel@tonic-gate 		usage();	/* usage message by request */
5447c478bd9Sstevel@tonic-gate 	if (directory == NULL)
5457c478bd9Sstevel@tonic-gate 		directory = TZDIR;
5467c478bd9Sstevel@tonic-gate 	if (yitcommand == NULL)
5477c478bd9Sstevel@tonic-gate 		yitcommand = "yearistype";
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 	setboundaries();
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
5527c478bd9Sstevel@tonic-gate 	if (optind < argc && leapsec != NULL) {
5537c478bd9Sstevel@tonic-gate 		infile(leapsec);
5547c478bd9Sstevel@tonic-gate 		adjleap();
5557c478bd9Sstevel@tonic-gate 	}
5567c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	for (i = optind; i < argc; ++i)
5597c478bd9Sstevel@tonic-gate 		infile(argv[i]);
5607c478bd9Sstevel@tonic-gate 	if (errors)
56180868c53Srobbin 		exit(EXIT_FAILURE);
5627c478bd9Sstevel@tonic-gate 	associate();
5637c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzones; i = j) {
5647c478bd9Sstevel@tonic-gate 		/*
5657c478bd9Sstevel@tonic-gate 		 * Find the next non-continuation zone entry.
5667c478bd9Sstevel@tonic-gate 		 */
5677c478bd9Sstevel@tonic-gate 		for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
5687c478bd9Sstevel@tonic-gate 			continue;
5697c478bd9Sstevel@tonic-gate 		outzone(&zones[i], j - i);
5707c478bd9Sstevel@tonic-gate 	}
5717c478bd9Sstevel@tonic-gate 	/*
5727c478bd9Sstevel@tonic-gate 	 * Make links.
5737c478bd9Sstevel@tonic-gate 	 */
5747c478bd9Sstevel@tonic-gate 	for (i = 0; i < nlinks; ++i) {
5757c478bd9Sstevel@tonic-gate 		eat(links[i].l_filename, links[i].l_linenum);
5767c478bd9Sstevel@tonic-gate 		dolink(links[i].l_from, links[i].l_to);
57780868c53Srobbin 		if (noise)
57880868c53Srobbin 			for (j = 0; j < nlinks; ++j)
57980868c53Srobbin 				if (strcmp(links[i].l_to, links[j].l_from) == 0)
58080868c53Srobbin 					warning(gettext("link to link"));
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	if (lcltime != NULL) {
5837c478bd9Sstevel@tonic-gate 		eat("command line", 1);
5847c478bd9Sstevel@tonic-gate 		dolink(lcltime, TZDEFAULT);
5857c478bd9Sstevel@tonic-gate 	}
5867c478bd9Sstevel@tonic-gate 	if (psxrules != NULL) {
5877c478bd9Sstevel@tonic-gate 		eat("command line", 1);
5887c478bd9Sstevel@tonic-gate 		dolink(psxrules, TZDEFRULES);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 	return ((errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE);
5917c478bd9Sstevel@tonic-gate }
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate static void
dolink(fromfield,tofield)594a68d064cSrobbin dolink(fromfield, tofield)
595a68d064cSrobbin const char * const	fromfield;
596a68d064cSrobbin const char * const	tofield;
5977c478bd9Sstevel@tonic-gate {
5987c478bd9Sstevel@tonic-gate 	register char *fromname;
5997c478bd9Sstevel@tonic-gate 	register char *toname;
6007c478bd9Sstevel@tonic-gate 
601a68d064cSrobbin 	if (fromfield[0] == '/')
602a68d064cSrobbin 		fromname = ecpyalloc(fromfield);
6037c478bd9Sstevel@tonic-gate 	else {
6047c478bd9Sstevel@tonic-gate 		fromname = ecpyalloc(directory);
6057c478bd9Sstevel@tonic-gate 		fromname = ecatalloc(fromname, "/");
606a68d064cSrobbin 		fromname = ecatalloc(fromname, fromfield);
6077c478bd9Sstevel@tonic-gate 	}
608a68d064cSrobbin 	if (tofield[0] == '/')
609a68d064cSrobbin 		toname = ecpyalloc(tofield);
6107c478bd9Sstevel@tonic-gate 	else {
6117c478bd9Sstevel@tonic-gate 		toname = ecpyalloc(directory);
6127c478bd9Sstevel@tonic-gate 		toname = ecatalloc(toname, "/");
613a68d064cSrobbin 		toname = ecatalloc(toname, tofield);
6147c478bd9Sstevel@tonic-gate 	}
6157c478bd9Sstevel@tonic-gate 	/*
6167c478bd9Sstevel@tonic-gate 	 * We get to be careful here since
6177c478bd9Sstevel@tonic-gate 	 * there's a fair chance of root running us.
6187c478bd9Sstevel@tonic-gate 	 */
6197c478bd9Sstevel@tonic-gate 	if (!itsdir(toname))
6207c478bd9Sstevel@tonic-gate 		(void) remove(toname);
6217c478bd9Sstevel@tonic-gate 	if (link(fromname, toname) != 0) {
6227c478bd9Sstevel@tonic-gate 		int	result;
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		if (mkdirs(toname) != 0)
62580868c53Srobbin 			exit(EXIT_FAILURE);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 		result = link(fromname, toname);
6287c478bd9Sstevel@tonic-gate 
62980868c53Srobbin 		if (result != 0 && access(fromname, F_OK) == 0 &&
63080868c53Srobbin 		    !itsdir(fromname)) {
631a68d064cSrobbin 			const char *s = tofield;
6327c478bd9Sstevel@tonic-gate 			register char *symlinkcontents = NULL;
63380868c53Srobbin 
6347c478bd9Sstevel@tonic-gate 			while ((s = strchr(s+1, '/')) != NULL)
6357c478bd9Sstevel@tonic-gate 				symlinkcontents = ecatalloc(symlinkcontents,
6367c478bd9Sstevel@tonic-gate 					"../");
637a68d064cSrobbin 			symlinkcontents = ecatalloc(symlinkcontents, fromname);
6387c478bd9Sstevel@tonic-gate 			result = symlink(symlinkcontents, toname);
6397c478bd9Sstevel@tonic-gate 			if (result == 0)
6407c478bd9Sstevel@tonic-gate 				warning(gettext(
6417c478bd9Sstevel@tonic-gate 				    "hard link failed, symbolic link used"));
6427c478bd9Sstevel@tonic-gate 			ifree(symlinkcontents);
6437c478bd9Sstevel@tonic-gate 		}
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		if (result != 0) {
6467c478bd9Sstevel@tonic-gate 			const char *e = strerror(errno);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
6497c478bd9Sstevel@tonic-gate 				"%s: Can't link from %s to %s: %s\n"),
6507c478bd9Sstevel@tonic-gate 				progname, fromname, toname, e);
65180868c53Srobbin 			exit(EXIT_FAILURE);
6527c478bd9Sstevel@tonic-gate 		}
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	ifree(fromname);
6557c478bd9Sstevel@tonic-gate 	ifree(toname);
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate #ifndef INT_MAX
6597c478bd9Sstevel@tonic-gate #define	INT_MAX		((int)(((unsigned)~0)>>1))
6607c478bd9Sstevel@tonic-gate #endif /* !defined INT_MAX */
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate #ifndef INT_MIN
6637c478bd9Sstevel@tonic-gate #define	INT_MIN		((int)~(((unsigned)~0)>>1))
6647c478bd9Sstevel@tonic-gate #endif /* !defined INT_MIN */
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * The tz file format currently allows at most 32-bit quantities.
6687c478bd9Sstevel@tonic-gate  * This restriction should be removed before signed 32-bit values
6697c478bd9Sstevel@tonic-gate  * wrap around in 2038, but unfortunately this will require a
6707c478bd9Sstevel@tonic-gate  * change to the tz file format.
6717c478bd9Sstevel@tonic-gate  */
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate #define	MAX_BITS_IN_FILE	32
6747c478bd9Sstevel@tonic-gate /* CSTYLED */
67580868c53Srobbin #define	TIME_T_BITS_IN_FILE	((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
67680868c53Srobbin 				    TYPE_BIT(zic_t): MAX_BITS_IN_FILE)
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate static void
setboundaries(void)6797c478bd9Sstevel@tonic-gate setboundaries(void)
6807c478bd9Sstevel@tonic-gate {
68180868c53Srobbin 	register int	i;
68280868c53Srobbin 
68380868c53Srobbin 	if (TYPE_SIGNED(zic_t)) {
68480868c53Srobbin 		min_time = -1;
68580868c53Srobbin 		for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
68680868c53Srobbin 			min_time *= 2;
68780868c53Srobbin 		max_time = -(min_time + 1);
6887c478bd9Sstevel@tonic-gate 		if (sflag)
6897c478bd9Sstevel@tonic-gate 			min_time = 0;
6907c478bd9Sstevel@tonic-gate 	} else {
6917c478bd9Sstevel@tonic-gate 		min_time = 0;
6927c478bd9Sstevel@tonic-gate 		max_time = 2 - sflag;
69380868c53Srobbin 		for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
69480868c53Srobbin 			max_time *= 2;
6957c478bd9Sstevel@tonic-gate 		--max_time;
6967c478bd9Sstevel@tonic-gate 	}
69780868c53Srobbin 	{
69880868c53Srobbin 		time_t	t;
69980868c53Srobbin 
70080868c53Srobbin 		t = (time_t)min_time;
70180868c53Srobbin 		min_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
70280868c53Srobbin 		t = (time_t)max_time;
70380868c53Srobbin 		max_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
70480868c53Srobbin 	}
7057c478bd9Sstevel@tonic-gate 	min_year_representable = min_year;
7067c478bd9Sstevel@tonic-gate 	max_year_representable = max_year;
7077c478bd9Sstevel@tonic-gate }
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate static int
itsdir(name)7107c478bd9Sstevel@tonic-gate itsdir(name)
7117c478bd9Sstevel@tonic-gate const char * const	name;
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 	register char	*myname;
7147c478bd9Sstevel@tonic-gate 	register int	accres;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	myname = ecpyalloc(name);
7177c478bd9Sstevel@tonic-gate 	myname = ecatalloc(myname, "/.");
7187c478bd9Sstevel@tonic-gate 	accres = access(myname, F_OK);
7197c478bd9Sstevel@tonic-gate 	ifree(myname);
7207c478bd9Sstevel@tonic-gate 	return (accres == 0);
7217c478bd9Sstevel@tonic-gate }
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate /*
7247c478bd9Sstevel@tonic-gate  * Associate sets of rules with zones.
7257c478bd9Sstevel@tonic-gate  */
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate /*
7287c478bd9Sstevel@tonic-gate  * Sort by rule name.
7297c478bd9Sstevel@tonic-gate  */
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate static int
rcomp(cp1,cp2)7327c478bd9Sstevel@tonic-gate rcomp(cp1, cp2)
7337c478bd9Sstevel@tonic-gate const void *	cp1;
7347c478bd9Sstevel@tonic-gate const void *	cp2;
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 	return (strcmp(((const struct rule *) cp1)->r_name,
7377c478bd9Sstevel@tonic-gate 		((const struct rule *) cp2)->r_name));
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate static void
associate(void)7417c478bd9Sstevel@tonic-gate associate(void)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 	register struct zone	*zp;
7447c478bd9Sstevel@tonic-gate 	register struct rule	*rp;
7457c478bd9Sstevel@tonic-gate 	register int		base, out;
7467c478bd9Sstevel@tonic-gate 	register int		i, j;
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	if (nrules != 0) {
7497c478bd9Sstevel@tonic-gate 		(void) qsort((void *)rules, (size_t)nrules,
7507c478bd9Sstevel@tonic-gate 			(size_t)sizeof (*rules), rcomp);
7517c478bd9Sstevel@tonic-gate 		for (i = 0; i < nrules - 1; ++i) {
7527c478bd9Sstevel@tonic-gate 			if (strcmp(rules[i].r_name,
7537c478bd9Sstevel@tonic-gate 				rules[i + 1].r_name) != 0)
7547c478bd9Sstevel@tonic-gate 					continue;
7557c478bd9Sstevel@tonic-gate 			if (strcmp(rules[i].r_filename,
7567c478bd9Sstevel@tonic-gate 				rules[i + 1].r_filename) == 0)
7577c478bd9Sstevel@tonic-gate 					continue;
7587c478bd9Sstevel@tonic-gate 			eat(rules[i].r_filename, rules[i].r_linenum);
7597c478bd9Sstevel@tonic-gate 			warning(gettext("same rule name in multiple files"));
7607c478bd9Sstevel@tonic-gate 			eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
7617c478bd9Sstevel@tonic-gate 			warning(gettext("same rule name in multiple files"));
7627c478bd9Sstevel@tonic-gate 			for (j = i + 2; j < nrules; ++j) {
7637c478bd9Sstevel@tonic-gate 				if (strcmp(rules[i].r_name,
7647c478bd9Sstevel@tonic-gate 					rules[j].r_name) != 0)
7657c478bd9Sstevel@tonic-gate 						break;
7667c478bd9Sstevel@tonic-gate 				if (strcmp(rules[i].r_filename,
7677c478bd9Sstevel@tonic-gate 					rules[j].r_filename) == 0)
7687c478bd9Sstevel@tonic-gate 						continue;
7697c478bd9Sstevel@tonic-gate 				if (strcmp(rules[i + 1].r_filename,
7707c478bd9Sstevel@tonic-gate 					rules[j].r_filename) == 0)
7717c478bd9Sstevel@tonic-gate 						continue;
7727c478bd9Sstevel@tonic-gate 				break;
7737c478bd9Sstevel@tonic-gate 			}
7747c478bd9Sstevel@tonic-gate 			i = j - 1;
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzones; ++i) {
7787c478bd9Sstevel@tonic-gate 		zp = &zones[i];
7797c478bd9Sstevel@tonic-gate 		zp->z_rules = NULL;
7807c478bd9Sstevel@tonic-gate 		zp->z_nrules = 0;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 	for (base = 0; base < nrules; base = out) {
7837c478bd9Sstevel@tonic-gate 		rp = &rules[base];
7847c478bd9Sstevel@tonic-gate 		for (out = base + 1; out < nrules; ++out)
7857c478bd9Sstevel@tonic-gate 			if (strcmp(rp->r_name, rules[out].r_name) != 0)
7867c478bd9Sstevel@tonic-gate 				break;
7877c478bd9Sstevel@tonic-gate 		for (i = 0; i < nzones; ++i) {
7887c478bd9Sstevel@tonic-gate 			zp = &zones[i];
7897c478bd9Sstevel@tonic-gate 			if (strcmp(zp->z_rule, rp->r_name) != 0)
7907c478bd9Sstevel@tonic-gate 				continue;
7917c478bd9Sstevel@tonic-gate 			zp->z_rules = rp;
7927c478bd9Sstevel@tonic-gate 			zp->z_nrules = out - base;
7937c478bd9Sstevel@tonic-gate 		}
7947c478bd9Sstevel@tonic-gate 	}
7957c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzones; ++i) {
7967c478bd9Sstevel@tonic-gate 		zp = &zones[i];
7977c478bd9Sstevel@tonic-gate 		if (zp->z_nrules == 0) {
7987c478bd9Sstevel@tonic-gate 			/*
7997c478bd9Sstevel@tonic-gate 			 * Maybe we have a local standard time offset.
8007c478bd9Sstevel@tonic-gate 			 */
8017c478bd9Sstevel@tonic-gate 			eat(zp->z_filename, zp->z_linenum);
8027c478bd9Sstevel@tonic-gate 			zp->z_stdoff = gethms(zp->z_rule,
8037c478bd9Sstevel@tonic-gate 				gettext("unruly zone"), TRUE);
8047c478bd9Sstevel@tonic-gate 			/*
8057c478bd9Sstevel@tonic-gate 			 * Note, though, that if there's no rule,
8067c478bd9Sstevel@tonic-gate 			 * a '%s' in the format is a bad thing.
8077c478bd9Sstevel@tonic-gate 			 */
8087c478bd9Sstevel@tonic-gate 			if (strchr(zp->z_format, '%') != 0)
8097c478bd9Sstevel@tonic-gate 				error(gettext("%s in ruleless zone"));
8107c478bd9Sstevel@tonic-gate 		}
8117c478bd9Sstevel@tonic-gate 	}
8127c478bd9Sstevel@tonic-gate 	if (errors)
81380868c53Srobbin 		exit(EXIT_FAILURE);
8147c478bd9Sstevel@tonic-gate }
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate static void
infile(name)8177c478bd9Sstevel@tonic-gate infile(name)
8187c478bd9Sstevel@tonic-gate const char *name;
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	register FILE			*fp;
8217c478bd9Sstevel@tonic-gate 	register char			**fields;
8227c478bd9Sstevel@tonic-gate 	register char			*cp;
8237c478bd9Sstevel@tonic-gate 	register const struct lookup	*lp;
8247c478bd9Sstevel@tonic-gate 	register int			nfields;
8257c478bd9Sstevel@tonic-gate 	register int			wantcont;
8267c478bd9Sstevel@tonic-gate 	register int			num;
8277c478bd9Sstevel@tonic-gate 	char				buf[BUFSIZ];
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (strcmp(name, "-") == 0) {
8307c478bd9Sstevel@tonic-gate 		name = gettext("standard input");
8317c478bd9Sstevel@tonic-gate 		fp = stdin;
8327c478bd9Sstevel@tonic-gate 	} else if ((fp = fopen(name, "r")) == NULL) {
8337c478bd9Sstevel@tonic-gate 		const char *e = strerror(errno);
8347c478bd9Sstevel@tonic-gate 
8357c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Can't open %s: %s\n"),
8367c478bd9Sstevel@tonic-gate 			progname, name, e);
83780868c53Srobbin 		exit(EXIT_FAILURE);
8387c478bd9Sstevel@tonic-gate 	}
8397c478bd9Sstevel@tonic-gate 	wantcont = FALSE;
8407c478bd9Sstevel@tonic-gate 	for (num = 1; ; ++num) {
8417c478bd9Sstevel@tonic-gate 		eat(name, num);
8427c478bd9Sstevel@tonic-gate 		if (fgets(buf, (int)sizeof (buf), fp) != buf)
8437c478bd9Sstevel@tonic-gate 			break;
8447c478bd9Sstevel@tonic-gate 		cp = strchr(buf, '\n');
8457c478bd9Sstevel@tonic-gate 		if (cp == NULL) {
8467c478bd9Sstevel@tonic-gate 			error(gettext("line too long"));
84780868c53Srobbin 			exit(EXIT_FAILURE);
8487c478bd9Sstevel@tonic-gate 		}
8497c478bd9Sstevel@tonic-gate 		*cp = '\0';
8507c478bd9Sstevel@tonic-gate 		fields = getfields(buf);
8517c478bd9Sstevel@tonic-gate 		nfields = 0;
8527c478bd9Sstevel@tonic-gate 		while (fields[nfields] != NULL) {
8537c478bd9Sstevel@tonic-gate 			static char	nada;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 			if (strcmp(fields[nfields], "-") == 0)
8567c478bd9Sstevel@tonic-gate 				fields[nfields] = &nada;
8577c478bd9Sstevel@tonic-gate 			++nfields;
8587c478bd9Sstevel@tonic-gate 		}
8597c478bd9Sstevel@tonic-gate 		if (nfields == 0) {
8607c478bd9Sstevel@tonic-gate 			/* nothing to do */
8617c478bd9Sstevel@tonic-gate 		} else if (wantcont) {
8627c478bd9Sstevel@tonic-gate 			wantcont = inzcont(fields, nfields);
8637c478bd9Sstevel@tonic-gate 		} else {
8647c478bd9Sstevel@tonic-gate 			lp = byword(fields[0], line_codes);
8657c478bd9Sstevel@tonic-gate 			if (lp == NULL)
8667c478bd9Sstevel@tonic-gate 				error(gettext("input line of unknown type"));
8677c478bd9Sstevel@tonic-gate 			else switch ((int)(lp->l_value)) {
8687c478bd9Sstevel@tonic-gate 				case LC_RULE:
8697c478bd9Sstevel@tonic-gate 					inrule(fields, nfields);
8707c478bd9Sstevel@tonic-gate 					wantcont = FALSE;
8717c478bd9Sstevel@tonic-gate 					break;
8727c478bd9Sstevel@tonic-gate 				case LC_ZONE:
8737c478bd9Sstevel@tonic-gate 					wantcont = inzone(fields, nfields);
8747c478bd9Sstevel@tonic-gate 					break;
8757c478bd9Sstevel@tonic-gate 				case LC_LINK:
8767c478bd9Sstevel@tonic-gate 					inlink(fields, nfields);
8777c478bd9Sstevel@tonic-gate 					wantcont = FALSE;
8787c478bd9Sstevel@tonic-gate 					break;
8797c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
8807c478bd9Sstevel@tonic-gate 				case LC_LEAP:
8817c478bd9Sstevel@tonic-gate 					if (name != leapsec)
8827c478bd9Sstevel@tonic-gate 						(void) fprintf(stderr, gettext(
8837c478bd9Sstevel@tonic-gate "%s: Leap line in non leap seconds file %s\n"),
8847c478bd9Sstevel@tonic-gate 							progname, name);
8857c478bd9Sstevel@tonic-gate 					else	inleap(fields, nfields);
8867c478bd9Sstevel@tonic-gate 					wantcont = FALSE;
8877c478bd9Sstevel@tonic-gate 					break;
8887c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
8897c478bd9Sstevel@tonic-gate 				default:	/* "cannot happen" */
8907c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
8917c478bd9Sstevel@tonic-gate "%s: panic: Invalid l_value %d\n"),
8927c478bd9Sstevel@tonic-gate 						progname, lp->l_value);
89380868c53Srobbin 					exit(EXIT_FAILURE);
8947c478bd9Sstevel@tonic-gate 			}
8957c478bd9Sstevel@tonic-gate 		}
8967c478bd9Sstevel@tonic-gate 		ifree((char *)fields);
8977c478bd9Sstevel@tonic-gate 	}
8987c478bd9Sstevel@tonic-gate 	if (ferror(fp)) {
8997c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Error reading %s\n"),
9007c478bd9Sstevel@tonic-gate 			progname, filename);
90180868c53Srobbin 		exit(EXIT_FAILURE);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate 	if (fp != stdin && fclose(fp)) {
9047c478bd9Sstevel@tonic-gate 		const char *e = strerror(errno);
9057c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Error closing %s: %s\n"),
9067c478bd9Sstevel@tonic-gate 			progname, filename, e);
90780868c53Srobbin 		exit(EXIT_FAILURE);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 	if (wantcont)
9107c478bd9Sstevel@tonic-gate 		error(gettext("expected continuation line not found"));
9117c478bd9Sstevel@tonic-gate }
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate /*
9147c478bd9Sstevel@tonic-gate  * Convert a string of one of the forms
9157c478bd9Sstevel@tonic-gate  *	h	-h	hh:mm	-hh:mm	hh:mm:ss	-hh:mm:ss
9167c478bd9Sstevel@tonic-gate  * into a number of seconds.
9177c478bd9Sstevel@tonic-gate  * A null string maps to zero.
9187c478bd9Sstevel@tonic-gate  * Call error with errstring and return zero on errors.
9197c478bd9Sstevel@tonic-gate  */
9207c478bd9Sstevel@tonic-gate 
9217c478bd9Sstevel@tonic-gate static long
gethms(string,errstring,signable)9227c478bd9Sstevel@tonic-gate gethms(string, errstring, signable)
9237c478bd9Sstevel@tonic-gate const char		*string;
9247c478bd9Sstevel@tonic-gate const char * const	errstring;
9257c478bd9Sstevel@tonic-gate const int		signable;
9267c478bd9Sstevel@tonic-gate {
927a68d064cSrobbin 	long	hh;
928a68d064cSrobbin 	int	mm, ss, sign;
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate 	if (string == NULL || *string == '\0')
9317c478bd9Sstevel@tonic-gate 		return (0);
9327c478bd9Sstevel@tonic-gate 	if (!signable)
9337c478bd9Sstevel@tonic-gate 		sign = 1;
9347c478bd9Sstevel@tonic-gate 	else if (*string == '-') {
9357c478bd9Sstevel@tonic-gate 		sign = -1;
9367c478bd9Sstevel@tonic-gate 		++string;
9377c478bd9Sstevel@tonic-gate 	} else	sign = 1;
938a68d064cSrobbin 	if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
9397c478bd9Sstevel@tonic-gate 		mm = ss = 0;
940a68d064cSrobbin 	else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
9417c478bd9Sstevel@tonic-gate 		ss = 0;
942a68d064cSrobbin 	else if (sscanf(string, scheck(string, "%ld:%d:%d"),
9437c478bd9Sstevel@tonic-gate 		&hh, &mm, &ss) != 3) {
9447c478bd9Sstevel@tonic-gate 			error(errstring);
9457c478bd9Sstevel@tonic-gate 			return (0);
9467c478bd9Sstevel@tonic-gate 	}
947a68d064cSrobbin 	if (hh < 0 ||
9487c478bd9Sstevel@tonic-gate 		mm < 0 || mm >= MINSPERHOUR ||
949a68d064cSrobbin 		ss < 0 || ss > SECSPERMIN) {
9507c478bd9Sstevel@tonic-gate 			error(errstring);
9517c478bd9Sstevel@tonic-gate 			return (0);
9527c478bd9Sstevel@tonic-gate 	}
953a68d064cSrobbin 	if (LONG_MAX / SECSPERHOUR < hh) {
954a68d064cSrobbin 		error(gettext("time overflow"));
955a68d064cSrobbin 		return (0);
956a68d064cSrobbin 	}
957a68d064cSrobbin 	if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
95880868c53Srobbin 		warning(
95980868c53Srobbin 		    gettext("24:00 not handled by pre-1998 versions of zic"));
960a68d064cSrobbin 	if (noise && (hh > HOURSPERDAY ||
961a68d064cSrobbin 		(hh == HOURSPERDAY && (mm != 0 || ss != 0))))
962a68d064cSrobbin 		warning(gettext("values over 24 hours not handled by "
963a68d064cSrobbin 			    "pre-2007 versions of zic"));
964a68d064cSrobbin 
965a68d064cSrobbin 	return (oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
966a68d064cSrobbin 		eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss))));
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate static void
inrule(fields,nfields)9707c478bd9Sstevel@tonic-gate inrule(fields, nfields)
9717c478bd9Sstevel@tonic-gate register char ** const	fields;
9727c478bd9Sstevel@tonic-gate const int		nfields;
9737c478bd9Sstevel@tonic-gate {
9747c478bd9Sstevel@tonic-gate 	static struct rule	r;
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	if (nfields != RULE_FIELDS) {
9777c478bd9Sstevel@tonic-gate 		error(gettext("wrong number of fields on Rule line"));
9787c478bd9Sstevel@tonic-gate 		return;
9797c478bd9Sstevel@tonic-gate 	}
9807c478bd9Sstevel@tonic-gate 	if (*fields[RF_NAME] == '\0') {
9817c478bd9Sstevel@tonic-gate 		error(gettext("nameless rule"));
9827c478bd9Sstevel@tonic-gate 		return;
9837c478bd9Sstevel@tonic-gate 	}
9847c478bd9Sstevel@tonic-gate 	r.r_filename = filename;
9857c478bd9Sstevel@tonic-gate 	r.r_linenum = linenum;
9867c478bd9Sstevel@tonic-gate 	r.r_stdoff = gethms(fields[RF_STDOFF], gettext("invalid saved time"),
9877c478bd9Sstevel@tonic-gate 		TRUE);
9887c478bd9Sstevel@tonic-gate 	rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
9897c478bd9Sstevel@tonic-gate 		fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
9907c478bd9Sstevel@tonic-gate 	r.r_name = ecpyalloc(fields[RF_NAME]);
9917c478bd9Sstevel@tonic-gate 	r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
9927c478bd9Sstevel@tonic-gate 	rules = (struct rule *)(void *)erealloc((char *)rules,
9937c478bd9Sstevel@tonic-gate 		(int)((nrules + 1) * sizeof (*rules)));
9947c478bd9Sstevel@tonic-gate 	rules[nrules++] = r;
9957c478bd9Sstevel@tonic-gate }
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate static int
inzone(fields,nfields)9987c478bd9Sstevel@tonic-gate inzone(fields, nfields)
9997c478bd9Sstevel@tonic-gate register char ** const	fields;
10007c478bd9Sstevel@tonic-gate const int		nfields;
10017c478bd9Sstevel@tonic-gate {
10027c478bd9Sstevel@tonic-gate 	register int	i;
10037c478bd9Sstevel@tonic-gate 	static char	*buf;
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
10067c478bd9Sstevel@tonic-gate 		error(gettext("wrong number of fields on Zone line"));
10077c478bd9Sstevel@tonic-gate 		return (FALSE);
10087c478bd9Sstevel@tonic-gate 	}
10097c478bd9Sstevel@tonic-gate 	if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
10107c478bd9Sstevel@tonic-gate 		buf = erealloc(buf, (int)(132 + strlen(TZDEFAULT)));
10117c478bd9Sstevel@tonic-gate 		(void) sprintf(buf,
10127c478bd9Sstevel@tonic-gate gettext("\"Zone %s\" line and -l option are mutually exclusive"),
10137c478bd9Sstevel@tonic-gate 			TZDEFAULT);
10147c478bd9Sstevel@tonic-gate 		error(buf);
10157c478bd9Sstevel@tonic-gate 		return (FALSE);
10167c478bd9Sstevel@tonic-gate 	}
10177c478bd9Sstevel@tonic-gate 	if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
10187c478bd9Sstevel@tonic-gate 		buf = erealloc(buf, (int)(132 + strlen(TZDEFRULES)));
10197c478bd9Sstevel@tonic-gate 		(void) sprintf(buf,
10207c478bd9Sstevel@tonic-gate gettext("\"Zone %s\" line and -p option are mutually exclusive"),
10217c478bd9Sstevel@tonic-gate 			TZDEFRULES);
10227c478bd9Sstevel@tonic-gate 		error(buf);
10237c478bd9Sstevel@tonic-gate 		return (FALSE);
10247c478bd9Sstevel@tonic-gate 	}
10257c478bd9Sstevel@tonic-gate 	for (i = 0; i < nzones; ++i)
10267c478bd9Sstevel@tonic-gate 		if (zones[i].z_name != NULL &&
10277c478bd9Sstevel@tonic-gate 			strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
10287c478bd9Sstevel@tonic-gate 				buf = erealloc(buf, (int)(132 +
10297c478bd9Sstevel@tonic-gate 					strlen(fields[ZF_NAME]) +
10307c478bd9Sstevel@tonic-gate 					strlen(zones[i].z_filename)));
10317c478bd9Sstevel@tonic-gate 				(void) sprintf(buf,
10327c478bd9Sstevel@tonic-gate gettext("duplicate zone name %s (file \"%s\", line %d)"),
10337c478bd9Sstevel@tonic-gate 					fields[ZF_NAME],
10347c478bd9Sstevel@tonic-gate 					zones[i].z_filename,
10357c478bd9Sstevel@tonic-gate 					zones[i].z_linenum);
10367c478bd9Sstevel@tonic-gate 				error(buf);
10377c478bd9Sstevel@tonic-gate 				return (FALSE);
10387c478bd9Sstevel@tonic-gate 		}
10397c478bd9Sstevel@tonic-gate 	return (inzsub(fields, nfields, FALSE));
10407c478bd9Sstevel@tonic-gate }
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate static int
inzcont(fields,nfields)10437c478bd9Sstevel@tonic-gate inzcont(fields, nfields)
10447c478bd9Sstevel@tonic-gate register char ** const	fields;
10457c478bd9Sstevel@tonic-gate const int		nfields;
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
10487c478bd9Sstevel@tonic-gate 		error(gettext(
10497c478bd9Sstevel@tonic-gate 		    "wrong number of fields on Zone continuation line"));
10507c478bd9Sstevel@tonic-gate 		return (FALSE);
10517c478bd9Sstevel@tonic-gate 	}
10527c478bd9Sstevel@tonic-gate 	return (inzsub(fields, nfields, TRUE));
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate static int
inzsub(fields,nfields,iscont)10567c478bd9Sstevel@tonic-gate inzsub(fields, nfields, iscont)
10577c478bd9Sstevel@tonic-gate register char ** const	fields;
10587c478bd9Sstevel@tonic-gate const int		nfields;
10597c478bd9Sstevel@tonic-gate const int		iscont;
10607c478bd9Sstevel@tonic-gate {
10617c478bd9Sstevel@tonic-gate 	register char		*cp;
10627c478bd9Sstevel@tonic-gate 	static struct zone	z;
10637c478bd9Sstevel@tonic-gate 	register int		i_gmtoff, i_rule, i_format;
10647c478bd9Sstevel@tonic-gate 	register int		i_untilyear, i_untilmonth;
10657c478bd9Sstevel@tonic-gate 	register int		i_untilday, i_untiltime;
10667c478bd9Sstevel@tonic-gate 	register int		hasuntil;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 	if (iscont) {
10697c478bd9Sstevel@tonic-gate 		i_gmtoff = ZFC_GMTOFF;
10707c478bd9Sstevel@tonic-gate 		i_rule = ZFC_RULE;
10717c478bd9Sstevel@tonic-gate 		i_format = ZFC_FORMAT;
10727c478bd9Sstevel@tonic-gate 		i_untilyear = ZFC_TILYEAR;
10737c478bd9Sstevel@tonic-gate 		i_untilmonth = ZFC_TILMONTH;
10747c478bd9Sstevel@tonic-gate 		i_untilday = ZFC_TILDAY;
10757c478bd9Sstevel@tonic-gate 		i_untiltime = ZFC_TILTIME;
10767c478bd9Sstevel@tonic-gate 		z.z_name = NULL;
10777c478bd9Sstevel@tonic-gate 	} else {
10787c478bd9Sstevel@tonic-gate 		i_gmtoff = ZF_GMTOFF;
10797c478bd9Sstevel@tonic-gate 		i_rule = ZF_RULE;
10807c478bd9Sstevel@tonic-gate 		i_format = ZF_FORMAT;
10817c478bd9Sstevel@tonic-gate 		i_untilyear = ZF_TILYEAR;
10827c478bd9Sstevel@tonic-gate 		i_untilmonth = ZF_TILMONTH;
10837c478bd9Sstevel@tonic-gate 		i_untilday = ZF_TILDAY;
10847c478bd9Sstevel@tonic-gate 		i_untiltime = ZF_TILTIME;
10857c478bd9Sstevel@tonic-gate 		z.z_name = ecpyalloc(fields[ZF_NAME]);
10867c478bd9Sstevel@tonic-gate 	}
10877c478bd9Sstevel@tonic-gate 	z.z_filename = filename;
10887c478bd9Sstevel@tonic-gate 	z.z_linenum = linenum;
10897c478bd9Sstevel@tonic-gate 	z.z_gmtoff = gethms(fields[i_gmtoff], gettext("invalid UTC offset"),
10907c478bd9Sstevel@tonic-gate 		TRUE);
10917c478bd9Sstevel@tonic-gate 	if ((cp = strchr(fields[i_format], '%')) != 0) {
10927c478bd9Sstevel@tonic-gate 		if (*++cp != 's' || strchr(cp, '%') != 0) {
10937c478bd9Sstevel@tonic-gate 			error(gettext("invalid abbreviation format"));
10947c478bd9Sstevel@tonic-gate 			return (FALSE);
10957c478bd9Sstevel@tonic-gate 		}
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 	z.z_rule = ecpyalloc(fields[i_rule]);
10987c478bd9Sstevel@tonic-gate 	z.z_format = ecpyalloc(fields[i_format]);
10997c478bd9Sstevel@tonic-gate 	hasuntil = nfields > i_untilyear;
11007c478bd9Sstevel@tonic-gate 	if (hasuntil) {
11017c478bd9Sstevel@tonic-gate 		z.z_untilrule.r_filename = filename;
11027c478bd9Sstevel@tonic-gate 		z.z_untilrule.r_linenum = linenum;
11037c478bd9Sstevel@tonic-gate 		rulesub(&z.z_untilrule,
11047c478bd9Sstevel@tonic-gate 			fields[i_untilyear],
11057c478bd9Sstevel@tonic-gate 			"only",
11067c478bd9Sstevel@tonic-gate 			"",
11077c478bd9Sstevel@tonic-gate 			(nfields > i_untilmonth) ?
11087c478bd9Sstevel@tonic-gate 			fields[i_untilmonth] : "Jan",
11097c478bd9Sstevel@tonic-gate 			(nfields > i_untilday) ? fields[i_untilday] : "1",
11107c478bd9Sstevel@tonic-gate 			(nfields > i_untiltime) ? fields[i_untiltime] : "0");
11117c478bd9Sstevel@tonic-gate 		z.z_untiltime = rpytime(&z.z_untilrule,
11127c478bd9Sstevel@tonic-gate 			z.z_untilrule.r_loyear);
11137c478bd9Sstevel@tonic-gate 		if (iscont && nzones > 0 &&
11147c478bd9Sstevel@tonic-gate 			z.z_untiltime > min_time &&
11157c478bd9Sstevel@tonic-gate 			z.z_untiltime < max_time &&
11167c478bd9Sstevel@tonic-gate 			zones[nzones - 1].z_untiltime > min_time &&
11177c478bd9Sstevel@tonic-gate 			zones[nzones - 1].z_untiltime < max_time &&
11187c478bd9Sstevel@tonic-gate 			zones[nzones - 1].z_untiltime >= z.z_untiltime) {
11197c478bd9Sstevel@tonic-gate 				error(gettext(
11207c478bd9Sstevel@tonic-gate "Zone continuation line end time is not after end time of previous line"));
11217c478bd9Sstevel@tonic-gate 				return (FALSE);
11227c478bd9Sstevel@tonic-gate 		}
11237c478bd9Sstevel@tonic-gate 	}
11247c478bd9Sstevel@tonic-gate 	zones = (struct zone *)(void *)erealloc((char *)zones,
11257c478bd9Sstevel@tonic-gate 		(int)((nzones + 1) * sizeof (*zones)));
11267c478bd9Sstevel@tonic-gate 	zones[nzones++] = z;
11277c478bd9Sstevel@tonic-gate 	/*
11287c478bd9Sstevel@tonic-gate 	 * If there was an UNTIL field on this line,
11297c478bd9Sstevel@tonic-gate 	 * there's more information about the zone on the next line.
11307c478bd9Sstevel@tonic-gate 	 */
11317c478bd9Sstevel@tonic-gate 	return (hasuntil);
11327c478bd9Sstevel@tonic-gate }
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
11357c478bd9Sstevel@tonic-gate static void
inleap(fields,nfields)11367c478bd9Sstevel@tonic-gate inleap(fields, nfields)
11377c478bd9Sstevel@tonic-gate register char ** const	fields;
11387c478bd9Sstevel@tonic-gate const int		nfields;
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	register const char 		*cp;
11417c478bd9Sstevel@tonic-gate 	register const struct lookup 	*lp;
11427c478bd9Sstevel@tonic-gate 	register int			i, j;
11437c478bd9Sstevel@tonic-gate 	int				year, month, day;
11447c478bd9Sstevel@tonic-gate 	long				dayoff, tod;
114580868c53Srobbin 	zic_t				t;
11467c478bd9Sstevel@tonic-gate 
11477c478bd9Sstevel@tonic-gate 	if (nfields != LEAP_FIELDS) {
11487c478bd9Sstevel@tonic-gate 		error(gettext("wrong number of fields on Leap line"));
11497c478bd9Sstevel@tonic-gate 		return;
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate 	dayoff = 0;
11527c478bd9Sstevel@tonic-gate 	cp = fields[LP_YEAR];
11537c478bd9Sstevel@tonic-gate 	if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
11547c478bd9Sstevel@tonic-gate 		/*
11557c478bd9Sstevel@tonic-gate 		 * Leapin' Lizards!
11567c478bd9Sstevel@tonic-gate 		 */
11577c478bd9Sstevel@tonic-gate 		error(gettext("invalid leaping year"));
11587c478bd9Sstevel@tonic-gate 		return;
11597c478bd9Sstevel@tonic-gate 	}
11607c478bd9Sstevel@tonic-gate 	j = EPOCH_YEAR;
11617c478bd9Sstevel@tonic-gate 	while (j != year) {
11627c478bd9Sstevel@tonic-gate 		if (year > j) {
11637c478bd9Sstevel@tonic-gate 			i = len_years[isleap(j)];
11647c478bd9Sstevel@tonic-gate 			++j;
11657c478bd9Sstevel@tonic-gate 		} else {
11667c478bd9Sstevel@tonic-gate 			--j;
11677c478bd9Sstevel@tonic-gate 			i = -len_years[isleap(j)];
11687c478bd9Sstevel@tonic-gate 		}
11697c478bd9Sstevel@tonic-gate 		dayoff = oadd(dayoff, eitol(i));
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
11727c478bd9Sstevel@tonic-gate 		error(gettext("invalid month name"));
11737c478bd9Sstevel@tonic-gate 		return;
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 	month = lp->l_value;
11767c478bd9Sstevel@tonic-gate 	j = TM_JANUARY;
11777c478bd9Sstevel@tonic-gate 	while (j != month) {
11787c478bd9Sstevel@tonic-gate 		i = len_months[isleap(year)][j];
11797c478bd9Sstevel@tonic-gate 		dayoff = oadd(dayoff, eitol(i));
11807c478bd9Sstevel@tonic-gate 		++j;
11817c478bd9Sstevel@tonic-gate 	}
11827c478bd9Sstevel@tonic-gate 	cp = fields[LP_DAY];
11837c478bd9Sstevel@tonic-gate 	if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
11847c478bd9Sstevel@tonic-gate 		day <= 0 || day > len_months[isleap(year)][month]) {
11857c478bd9Sstevel@tonic-gate 			error(gettext("invalid day of month"));
11867c478bd9Sstevel@tonic-gate 			return;
11877c478bd9Sstevel@tonic-gate 	}
11887c478bd9Sstevel@tonic-gate 	dayoff = oadd(dayoff, eitol(day - 1));
118980868c53Srobbin 	if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
11907c478bd9Sstevel@tonic-gate 		error(gettext("time before zero"));
11917c478bd9Sstevel@tonic-gate 		return;
11927c478bd9Sstevel@tonic-gate 	}
119380868c53Srobbin 	if (dayoff < min_time / SECSPERDAY) {
119480868c53Srobbin 		error(gettext("time too small"));
11957c478bd9Sstevel@tonic-gate 		return;
11967c478bd9Sstevel@tonic-gate 	}
119780868c53Srobbin 	if (dayoff > max_time / SECSPERDAY) {
119880868c53Srobbin 		error(gettext("time too large"));
119980868c53Srobbin 		return;
120080868c53Srobbin 	}
120180868c53Srobbin 	t = (zic_t)dayoff * SECSPERDAY;
12027c478bd9Sstevel@tonic-gate 	tod = gethms(fields[LP_TIME], gettext("invalid time of day"), FALSE);
12037c478bd9Sstevel@tonic-gate 	cp = fields[LP_CORR];
12047c478bd9Sstevel@tonic-gate 	{
12057c478bd9Sstevel@tonic-gate 		register int	positive;
12067c478bd9Sstevel@tonic-gate 		int		count;
12077c478bd9Sstevel@tonic-gate 
12087c478bd9Sstevel@tonic-gate 		if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
12097c478bd9Sstevel@tonic-gate 			positive = FALSE;
12107c478bd9Sstevel@tonic-gate 			count = 1;
12117c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp, "--") == 0) {
12127c478bd9Sstevel@tonic-gate 			positive = FALSE;
12137c478bd9Sstevel@tonic-gate 			count = 2;
12147c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp, "+") == 0) {
12157c478bd9Sstevel@tonic-gate 			positive = TRUE;
12167c478bd9Sstevel@tonic-gate 			count = 1;
12177c478bd9Sstevel@tonic-gate 		} else if (strcmp(cp, "++") == 0) {
12187c478bd9Sstevel@tonic-gate 			positive = TRUE;
12197c478bd9Sstevel@tonic-gate 			count = 2;
12207c478bd9Sstevel@tonic-gate 		} else {
12217c478bd9Sstevel@tonic-gate 			error(gettext("illegal CORRECTION field on Leap line"));
12227c478bd9Sstevel@tonic-gate 			return;
12237c478bd9Sstevel@tonic-gate 		}
12247c478bd9Sstevel@tonic-gate 		if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
12257c478bd9Sstevel@tonic-gate 			error(gettext(
12267c478bd9Sstevel@tonic-gate 			    "illegal Rolling/Stationary field on Leap line"));
12277c478bd9Sstevel@tonic-gate 			return;
12287c478bd9Sstevel@tonic-gate 		}
12297c478bd9Sstevel@tonic-gate 		leapadd(tadd(t, tod), positive, lp->l_value, count);
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate static void
inlink(fields,nfields)12357c478bd9Sstevel@tonic-gate inlink(fields, nfields)
12367c478bd9Sstevel@tonic-gate register char ** const	fields;
12377c478bd9Sstevel@tonic-gate const int		nfields;
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	struct link	l;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	if (nfields != LINK_FIELDS) {
12427c478bd9Sstevel@tonic-gate 		error(gettext("wrong number of fields on Link line"));
12437c478bd9Sstevel@tonic-gate 		return;
12447c478bd9Sstevel@tonic-gate 	}
12457c478bd9Sstevel@tonic-gate 	if (*fields[LF_FROM] == '\0') {
12467c478bd9Sstevel@tonic-gate 		error(gettext("blank FROM field on Link line"));
12477c478bd9Sstevel@tonic-gate 		return;
12487c478bd9Sstevel@tonic-gate 	}
12497c478bd9Sstevel@tonic-gate 	if (*fields[LF_TO] == '\0') {
12507c478bd9Sstevel@tonic-gate 		error(gettext("blank TO field on Link line"));
12517c478bd9Sstevel@tonic-gate 		return;
12527c478bd9Sstevel@tonic-gate 	}
12537c478bd9Sstevel@tonic-gate 	l.l_filename = filename;
12547c478bd9Sstevel@tonic-gate 	l.l_linenum = linenum;
12557c478bd9Sstevel@tonic-gate 	l.l_from = ecpyalloc(fields[LF_FROM]);
12567c478bd9Sstevel@tonic-gate 	l.l_to = ecpyalloc(fields[LF_TO]);
12577c478bd9Sstevel@tonic-gate 	links = (struct link *)(void *)erealloc((char *)links,
12587c478bd9Sstevel@tonic-gate 		(int)((nlinks + 1) * sizeof (*links)));
12597c478bd9Sstevel@tonic-gate 	links[nlinks++] = l;
12607c478bd9Sstevel@tonic-gate }
12617c478bd9Sstevel@tonic-gate 
12627c478bd9Sstevel@tonic-gate static void
rulesub(rp,loyearp,hiyearp,typep,monthp,dayp,timep)12637c478bd9Sstevel@tonic-gate rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
12647c478bd9Sstevel@tonic-gate register struct rule * const	rp;
12657c478bd9Sstevel@tonic-gate const char * const		loyearp;
12667c478bd9Sstevel@tonic-gate const char * const		hiyearp;
12677c478bd9Sstevel@tonic-gate const char * const		typep;
12687c478bd9Sstevel@tonic-gate const char * const		monthp;
12697c478bd9Sstevel@tonic-gate const char * const		dayp;
12707c478bd9Sstevel@tonic-gate const char * const		timep;
12717c478bd9Sstevel@tonic-gate {
12727c478bd9Sstevel@tonic-gate 	register const struct lookup	*lp;
12737c478bd9Sstevel@tonic-gate 	register const char		*cp;
12747c478bd9Sstevel@tonic-gate 	register char			*dp;
12757c478bd9Sstevel@tonic-gate 	register char			*ep;
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate 	if ((lp = byword(monthp, mon_names)) == NULL) {
12787c478bd9Sstevel@tonic-gate 		error(gettext("invalid month name"));
12797c478bd9Sstevel@tonic-gate 		return;
12807c478bd9Sstevel@tonic-gate 	}
12817c478bd9Sstevel@tonic-gate 	rp->r_month = lp->l_value;
12827c478bd9Sstevel@tonic-gate 	rp->r_todisstd = FALSE;
12837c478bd9Sstevel@tonic-gate 	rp->r_todisgmt = FALSE;
12847c478bd9Sstevel@tonic-gate 	dp = ecpyalloc(timep);
12857c478bd9Sstevel@tonic-gate 	if (*dp != '\0') {
12867c478bd9Sstevel@tonic-gate 		ep = dp + strlen(dp) - 1;
12877c478bd9Sstevel@tonic-gate 		switch (lowerit(*ep)) {
12887c478bd9Sstevel@tonic-gate 			case 's':	/* Standard */
12897c478bd9Sstevel@tonic-gate 				rp->r_todisstd = TRUE;
12907c478bd9Sstevel@tonic-gate 				rp->r_todisgmt = FALSE;
12917c478bd9Sstevel@tonic-gate 				*ep = '\0';
12927c478bd9Sstevel@tonic-gate 				break;
12937c478bd9Sstevel@tonic-gate 			case 'w':	/* Wall */
12947c478bd9Sstevel@tonic-gate 				rp->r_todisstd = FALSE;
12957c478bd9Sstevel@tonic-gate 				rp->r_todisgmt = FALSE;
12967c478bd9Sstevel@tonic-gate 				*ep = '\0';
12977c478bd9Sstevel@tonic-gate 				break;
12987c478bd9Sstevel@tonic-gate 			case 'g':	/* Greenwich */
12997c478bd9Sstevel@tonic-gate 			case 'u':	/* Universal */
13007c478bd9Sstevel@tonic-gate 			case 'z':	/* Zulu */
13017c478bd9Sstevel@tonic-gate 				rp->r_todisstd = TRUE;
13027c478bd9Sstevel@tonic-gate 				rp->r_todisgmt = TRUE;
13037c478bd9Sstevel@tonic-gate 				*ep = '\0';
13047c478bd9Sstevel@tonic-gate 				break;
13057c478bd9Sstevel@tonic-gate 		}
13067c478bd9Sstevel@tonic-gate 	}
13077c478bd9Sstevel@tonic-gate 	rp->r_tod = gethms(dp, gettext("invalid time of day"), FALSE);
13087c478bd9Sstevel@tonic-gate 	ifree(dp);
13097c478bd9Sstevel@tonic-gate 	/*
13107c478bd9Sstevel@tonic-gate 	 * Year work.
13117c478bd9Sstevel@tonic-gate 	 */
13127c478bd9Sstevel@tonic-gate 	cp = loyearp;
13137c478bd9Sstevel@tonic-gate 	lp = byword(cp, begin_years);
13147c478bd9Sstevel@tonic-gate 	if (lp != NULL) {
13157c478bd9Sstevel@tonic-gate 	    switch ((int)lp->l_value) {
13167c478bd9Sstevel@tonic-gate 		case YR_MINIMUM:
13177c478bd9Sstevel@tonic-gate 			rp->r_loyear = INT_MIN;
13187c478bd9Sstevel@tonic-gate 			break;
13197c478bd9Sstevel@tonic-gate 		case YR_MAXIMUM:
13207c478bd9Sstevel@tonic-gate 			rp->r_loyear = INT_MAX;
13217c478bd9Sstevel@tonic-gate 			break;
13227c478bd9Sstevel@tonic-gate 		default:	/* "cannot happen" */
13237c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13247c478bd9Sstevel@tonic-gate 				gettext("%s: panic: Invalid l_value %d\n"),
13257c478bd9Sstevel@tonic-gate 				progname, lp->l_value);
132680868c53Srobbin 			exit(EXIT_FAILURE);
13277c478bd9Sstevel@tonic-gate 	    }
13287c478bd9Sstevel@tonic-gate 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
13297c478bd9Sstevel@tonic-gate 		error(gettext("invalid starting year"));
13307c478bd9Sstevel@tonic-gate 		return;
13317c478bd9Sstevel@tonic-gate 	} else if (noise) {
13327c478bd9Sstevel@tonic-gate 		if (rp->r_loyear < min_year_representable)
13337c478bd9Sstevel@tonic-gate 			warning(gettext(
13347c478bd9Sstevel@tonic-gate 			    "starting year too low to be represented"));
13357c478bd9Sstevel@tonic-gate 		else if (rp->r_loyear > max_year_representable)
13367c478bd9Sstevel@tonic-gate 			warning(gettext(
13377c478bd9Sstevel@tonic-gate 			    "starting year too high to be represented"));
13387c478bd9Sstevel@tonic-gate 	}
13397c478bd9Sstevel@tonic-gate 	cp = hiyearp;
13407c478bd9Sstevel@tonic-gate 	if ((lp = byword(cp, end_years)) != NULL) {
13417c478bd9Sstevel@tonic-gate 	    switch ((int)lp->l_value) {
13427c478bd9Sstevel@tonic-gate 		case YR_MINIMUM:
13437c478bd9Sstevel@tonic-gate 			rp->r_hiyear = INT_MIN;
13447c478bd9Sstevel@tonic-gate 			break;
13457c478bd9Sstevel@tonic-gate 		case YR_MAXIMUM:
13467c478bd9Sstevel@tonic-gate 			rp->r_hiyear = INT_MAX;
13477c478bd9Sstevel@tonic-gate 			break;
13487c478bd9Sstevel@tonic-gate 		case YR_ONLY:
13497c478bd9Sstevel@tonic-gate 			rp->r_hiyear = rp->r_loyear;
13507c478bd9Sstevel@tonic-gate 			break;
13517c478bd9Sstevel@tonic-gate 		default:	/* "cannot happen" */
13527c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
13537c478bd9Sstevel@tonic-gate 				gettext("%s: panic: Invalid l_value %d\n"),
13547c478bd9Sstevel@tonic-gate 				progname, lp->l_value);
135580868c53Srobbin 			exit(EXIT_FAILURE);
13567c478bd9Sstevel@tonic-gate 	    }
13577c478bd9Sstevel@tonic-gate 	} else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
13587c478bd9Sstevel@tonic-gate 		error(gettext("invalid ending year"));
13597c478bd9Sstevel@tonic-gate 		return;
13607c478bd9Sstevel@tonic-gate 	} else if (noise) {
13617c478bd9Sstevel@tonic-gate 		if (rp->r_loyear < min_year_representable)
13627c478bd9Sstevel@tonic-gate 			warning(gettext(
136380868c53Srobbin 			    "ending year too low to be represented"));
13647c478bd9Sstevel@tonic-gate 		else if (rp->r_loyear > max_year_representable)
13657c478bd9Sstevel@tonic-gate 			warning(gettext(
136680868c53Srobbin 			    "ending year too high to be represented"));
13677c478bd9Sstevel@tonic-gate 	}
13687c478bd9Sstevel@tonic-gate 	if (rp->r_loyear > rp->r_hiyear) {
13697c478bd9Sstevel@tonic-gate 		error(gettext("starting year greater than ending year"));
13707c478bd9Sstevel@tonic-gate 		return;
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 	if (*typep == '\0')
13737c478bd9Sstevel@tonic-gate 		rp->r_yrtype = NULL;
13747c478bd9Sstevel@tonic-gate 	else {
13757c478bd9Sstevel@tonic-gate 		if (rp->r_loyear == rp->r_hiyear) {
13767c478bd9Sstevel@tonic-gate 			error(gettext("typed single year"));
13777c478bd9Sstevel@tonic-gate 			return;
13787c478bd9Sstevel@tonic-gate 		}
13797c478bd9Sstevel@tonic-gate 		rp->r_yrtype = ecpyalloc(typep);
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate 	if (rp->r_loyear < min_year && rp->r_loyear > 0)
13827c478bd9Sstevel@tonic-gate 		min_year = rp->r_loyear;
13837c478bd9Sstevel@tonic-gate 	/*
13847c478bd9Sstevel@tonic-gate 	 * Day work.
13857c478bd9Sstevel@tonic-gate 	 * Accept things such as:
13867c478bd9Sstevel@tonic-gate 	 *	1
13877c478bd9Sstevel@tonic-gate 	 *	last-Sunday
13887c478bd9Sstevel@tonic-gate 	 *	Sun<=20
13897c478bd9Sstevel@tonic-gate 	 *	Sun>=7
13907c478bd9Sstevel@tonic-gate 	 */
13917c478bd9Sstevel@tonic-gate 	dp = ecpyalloc(dayp);
13927c478bd9Sstevel@tonic-gate 	if ((lp = byword(dp, lasts)) != NULL) {
13937c478bd9Sstevel@tonic-gate 		rp->r_dycode = DC_DOWLEQ;
13947c478bd9Sstevel@tonic-gate 		rp->r_wday = lp->l_value;
13957c478bd9Sstevel@tonic-gate 		rp->r_dayofmonth = len_months[1][rp->r_month];
13967c478bd9Sstevel@tonic-gate 	} else {
13977c478bd9Sstevel@tonic-gate 		if ((ep = strchr(dp, '<')) != 0)
13987c478bd9Sstevel@tonic-gate 			rp->r_dycode = DC_DOWLEQ;
13997c478bd9Sstevel@tonic-gate 		else if ((ep = strchr(dp, '>')) != 0)
14007c478bd9Sstevel@tonic-gate 			rp->r_dycode = DC_DOWGEQ;
14017c478bd9Sstevel@tonic-gate 		else {
14027c478bd9Sstevel@tonic-gate 			ep = dp;
14037c478bd9Sstevel@tonic-gate 			rp->r_dycode = DC_DOM;
14047c478bd9Sstevel@tonic-gate 		}
14057c478bd9Sstevel@tonic-gate 		if (rp->r_dycode != DC_DOM) {
14067c478bd9Sstevel@tonic-gate 			*ep++ = 0;
14077c478bd9Sstevel@tonic-gate 			if (*ep++ != '=') {
14087c478bd9Sstevel@tonic-gate 				error(gettext("invalid day of month"));
14097c478bd9Sstevel@tonic-gate 				ifree(dp);
14107c478bd9Sstevel@tonic-gate 				return;
14117c478bd9Sstevel@tonic-gate 			}
14127c478bd9Sstevel@tonic-gate 			if ((lp = byword(dp, wday_names)) == NULL) {
14137c478bd9Sstevel@tonic-gate 				error(gettext("invalid weekday name"));
14147c478bd9Sstevel@tonic-gate 				ifree(dp);
14157c478bd9Sstevel@tonic-gate 				return;
14167c478bd9Sstevel@tonic-gate 			}
14177c478bd9Sstevel@tonic-gate 			rp->r_wday = lp->l_value;
14187c478bd9Sstevel@tonic-gate 		}
14197c478bd9Sstevel@tonic-gate 		if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
14207c478bd9Sstevel@tonic-gate 			rp->r_dayofmonth <= 0 ||
14217c478bd9Sstevel@tonic-gate 			(rp->r_dayofmonth > len_months[1][rp->r_month])) {
14227c478bd9Sstevel@tonic-gate 				error(gettext("invalid day of month"));
14237c478bd9Sstevel@tonic-gate 				ifree(dp);
14247c478bd9Sstevel@tonic-gate 				return;
14257c478bd9Sstevel@tonic-gate 		}
14267c478bd9Sstevel@tonic-gate 	}
14277c478bd9Sstevel@tonic-gate 	ifree(dp);
14287c478bd9Sstevel@tonic-gate }
14297c478bd9Sstevel@tonic-gate 
14307c478bd9Sstevel@tonic-gate static void
convert(val,buf)14317c478bd9Sstevel@tonic-gate convert(val, buf)
14327c478bd9Sstevel@tonic-gate const long	val;
14337c478bd9Sstevel@tonic-gate char * const	buf;
14347c478bd9Sstevel@tonic-gate {
14357c478bd9Sstevel@tonic-gate 	register int	i;
14367c478bd9Sstevel@tonic-gate 	register long	shift;
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
14397c478bd9Sstevel@tonic-gate 		buf[i] = val >> shift;
14407c478bd9Sstevel@tonic-gate }
14417c478bd9Sstevel@tonic-gate 
14427c478bd9Sstevel@tonic-gate static void
puttzcode(val,fp)14437c478bd9Sstevel@tonic-gate puttzcode(val, fp)
14447c478bd9Sstevel@tonic-gate const long	val;
14457c478bd9Sstevel@tonic-gate FILE * const	fp;
14467c478bd9Sstevel@tonic-gate {
14477c478bd9Sstevel@tonic-gate 	char	buf[4];
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	convert(val, buf);
14507c478bd9Sstevel@tonic-gate 	(void) fwrite((void *)buf, (size_t)sizeof (buf), (size_t)1, fp);
14517c478bd9Sstevel@tonic-gate }
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate static int
atcomp(avp,bvp)14547c478bd9Sstevel@tonic-gate atcomp(avp, bvp)
14557c478bd9Sstevel@tonic-gate const void *avp;
14567c478bd9Sstevel@tonic-gate const void *bvp;
14577c478bd9Sstevel@tonic-gate {
14587c478bd9Sstevel@tonic-gate 	if (((struct attype *)avp)->at < ((struct attype *)bvp)->at)
14597c478bd9Sstevel@tonic-gate 		return (-1);
14607c478bd9Sstevel@tonic-gate 	else if (((struct attype *)avp)->at > ((struct attype *)bvp)->at)
14617c478bd9Sstevel@tonic-gate 		return (1);
14627c478bd9Sstevel@tonic-gate 	else	return (0);
14637c478bd9Sstevel@tonic-gate }
14647c478bd9Sstevel@tonic-gate 
14657c478bd9Sstevel@tonic-gate static void
writezone(name)14667c478bd9Sstevel@tonic-gate writezone(name)
14677c478bd9Sstevel@tonic-gate const char * const	name;
14687c478bd9Sstevel@tonic-gate {
14697c478bd9Sstevel@tonic-gate 	register FILE		*fp;
14707c478bd9Sstevel@tonic-gate 	register int		i, j;
14717c478bd9Sstevel@tonic-gate 	static char		*fullname;
14727c478bd9Sstevel@tonic-gate 	static struct tzhead	tzh;
147380868c53Srobbin 	zic_t			ats[TZ_MAX_TIMES];
14747c478bd9Sstevel@tonic-gate 	unsigned char		types[TZ_MAX_TIMES];
14757c478bd9Sstevel@tonic-gate 
14767c478bd9Sstevel@tonic-gate 	/*
14777c478bd9Sstevel@tonic-gate 	 * Sort.
14787c478bd9Sstevel@tonic-gate 	 */
14797c478bd9Sstevel@tonic-gate 	if (timecnt > 1)
14807c478bd9Sstevel@tonic-gate 		(void) qsort((void *)attypes, (size_t)timecnt,
14817c478bd9Sstevel@tonic-gate 			(size_t)sizeof (*attypes), atcomp);
14827c478bd9Sstevel@tonic-gate 	/*
14837c478bd9Sstevel@tonic-gate 	 * Optimize.
14847c478bd9Sstevel@tonic-gate 	 */
14857c478bd9Sstevel@tonic-gate 	{
14867c478bd9Sstevel@tonic-gate 		int	fromi;
14877c478bd9Sstevel@tonic-gate 		int	toi;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 		toi = 0;
14907c478bd9Sstevel@tonic-gate 		fromi = 0;
14917c478bd9Sstevel@tonic-gate 		while (fromi < timecnt && attypes[fromi].at < min_time)
14927c478bd9Sstevel@tonic-gate 			++fromi;
14937c478bd9Sstevel@tonic-gate 		if (isdsts[0] == 0)
14947c478bd9Sstevel@tonic-gate 			while (fromi < timecnt && attypes[fromi].type == 0)
14957c478bd9Sstevel@tonic-gate 				++fromi;	/* handled by default rule */
14967c478bd9Sstevel@tonic-gate 		for (; fromi < timecnt; ++fromi) {
149780868c53Srobbin 			if (toi != 0 && ((attypes[fromi].at +
14987c478bd9Sstevel@tonic-gate 				gmtoffs[attypes[toi - 1].type]) <=
14997c478bd9Sstevel@tonic-gate 				(attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
15007c478bd9Sstevel@tonic-gate 				: attypes[toi - 2].type]))) {
150180868c53Srobbin 					attypes[toi - 1].type =
150280868c53Srobbin 						attypes[fromi].type;
15037c478bd9Sstevel@tonic-gate 					continue;
15047c478bd9Sstevel@tonic-gate 			}
15057c478bd9Sstevel@tonic-gate 			if (toi == 0 ||
15067c478bd9Sstevel@tonic-gate 				attypes[toi - 1].type != attypes[fromi].type)
15077c478bd9Sstevel@tonic-gate 					attypes[toi++] = attypes[fromi];
15087c478bd9Sstevel@tonic-gate 		}
15097c478bd9Sstevel@tonic-gate 		timecnt = toi;
15107c478bd9Sstevel@tonic-gate 	}
15117c478bd9Sstevel@tonic-gate 	/*
15127c478bd9Sstevel@tonic-gate 	 * Transfer.
15137c478bd9Sstevel@tonic-gate 	 */
15147c478bd9Sstevel@tonic-gate 	for (i = 0; i < timecnt; ++i) {
15157c478bd9Sstevel@tonic-gate 		ats[i] = attypes[i].at;
15167c478bd9Sstevel@tonic-gate 		types[i] = attypes[i].type;
15177c478bd9Sstevel@tonic-gate 	}
15187c478bd9Sstevel@tonic-gate 	fullname = erealloc(fullname,
15197c478bd9Sstevel@tonic-gate 		(int)(strlen(directory) + 1 + strlen(name) + 1));
15207c478bd9Sstevel@tonic-gate 	(void) sprintf(fullname, "%s/%s", directory, name);
15217c478bd9Sstevel@tonic-gate 	/*
15227c478bd9Sstevel@tonic-gate 	 * Remove old file, if any, to snap links.
15237c478bd9Sstevel@tonic-gate 	 */
15247c478bd9Sstevel@tonic-gate 	if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
15257c478bd9Sstevel@tonic-gate 		const char *e = strerror(errno);
15267c478bd9Sstevel@tonic-gate 
15277c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Can't remove %s: %s\n"),
15287c478bd9Sstevel@tonic-gate 			progname, fullname, e);
152980868c53Srobbin 		exit(EXIT_FAILURE);
15307c478bd9Sstevel@tonic-gate 	}
15317c478bd9Sstevel@tonic-gate 	if ((fp = fopen(fullname, "wb")) == NULL) {
15327c478bd9Sstevel@tonic-gate 		if (mkdirs(fullname) != 0)
153380868c53Srobbin 			exit(EXIT_FAILURE);
15347c478bd9Sstevel@tonic-gate 		if ((fp = fopen(fullname, "wb")) == NULL) {
15357c478bd9Sstevel@tonic-gate 			const char *e = strerror(errno);
15367c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
15377c478bd9Sstevel@tonic-gate 				"%s: Can't create %s: %s\n"),
15387c478bd9Sstevel@tonic-gate 				progname, fullname, e);
153980868c53Srobbin 			exit(EXIT_FAILURE);
15407c478bd9Sstevel@tonic-gate 		}
15417c478bd9Sstevel@tonic-gate 	}
15427c478bd9Sstevel@tonic-gate 	convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
15437c478bd9Sstevel@tonic-gate 	convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
15447c478bd9Sstevel@tonic-gate 	convert(eitol(leapcnt), tzh.tzh_leapcnt);
15457c478bd9Sstevel@tonic-gate 	convert(eitol(timecnt), tzh.tzh_timecnt);
15467c478bd9Sstevel@tonic-gate 	convert(eitol(typecnt), tzh.tzh_typecnt);
15477c478bd9Sstevel@tonic-gate 	convert(eitol(charcnt), tzh.tzh_charcnt);
15487c478bd9Sstevel@tonic-gate 	(void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof (tzh.tzh_magic));
15497c478bd9Sstevel@tonic-gate #define	DO(field)	(void) fwrite((void *) tzh.field, \
15507c478bd9Sstevel@tonic-gate 			(size_t)sizeof (tzh.field), (size_t)1, fp)
15517c478bd9Sstevel@tonic-gate 	DO(tzh_magic);
15527c478bd9Sstevel@tonic-gate 	DO(tzh_reserved);
15537c478bd9Sstevel@tonic-gate 	DO(tzh_ttisgmtcnt);
15547c478bd9Sstevel@tonic-gate 	DO(tzh_ttisstdcnt);
15557c478bd9Sstevel@tonic-gate 	DO(tzh_leapcnt);
15567c478bd9Sstevel@tonic-gate 	DO(tzh_timecnt);
15577c478bd9Sstevel@tonic-gate 	DO(tzh_typecnt);
15587c478bd9Sstevel@tonic-gate 	DO(tzh_charcnt);
15597c478bd9Sstevel@tonic-gate #undef DO
15607c478bd9Sstevel@tonic-gate 	for (i = 0; i < timecnt; ++i) {
15617c478bd9Sstevel@tonic-gate 		j = leapcnt;
15627c478bd9Sstevel@tonic-gate 		while (--j >= 0)
15637c478bd9Sstevel@tonic-gate 			if (ats[i] >= trans[j]) {
15647c478bd9Sstevel@tonic-gate 				ats[i] = tadd(ats[i], corr[j]);
15657c478bd9Sstevel@tonic-gate 				break;
15667c478bd9Sstevel@tonic-gate 			}
15677c478bd9Sstevel@tonic-gate 		puttzcode((long)ats[i], fp);
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 	if (timecnt > 0)
15707c478bd9Sstevel@tonic-gate 		(void) fwrite((void *)types, (size_t)sizeof (types[0]),
15717c478bd9Sstevel@tonic-gate 			(size_t)timecnt, fp);
15727c478bd9Sstevel@tonic-gate 	for (i = 0; i < typecnt; ++i) {
15737c478bd9Sstevel@tonic-gate 		puttzcode((long)gmtoffs[i], fp);
15747c478bd9Sstevel@tonic-gate 		(void) putc(isdsts[i], fp);
15757c478bd9Sstevel@tonic-gate 		(void) putc(abbrinds[i], fp);
15767c478bd9Sstevel@tonic-gate 	}
15777c478bd9Sstevel@tonic-gate 	if (charcnt != 0)
15787c478bd9Sstevel@tonic-gate 		(void) fwrite((void *)chars, (size_t)sizeof (chars[0]),
15797c478bd9Sstevel@tonic-gate 			(size_t)charcnt, fp);
15807c478bd9Sstevel@tonic-gate 	for (i = 0; i < leapcnt; ++i) {
15817c478bd9Sstevel@tonic-gate 		if (roll[i]) {
15827c478bd9Sstevel@tonic-gate 			if (timecnt == 0 || trans[i] < ats[0]) {
15837c478bd9Sstevel@tonic-gate 				j = 0;
15847c478bd9Sstevel@tonic-gate 				while (isdsts[j])
15857c478bd9Sstevel@tonic-gate 					if (++j >= typecnt) {
15867c478bd9Sstevel@tonic-gate 						j = 0;
15877c478bd9Sstevel@tonic-gate 						break;
15887c478bd9Sstevel@tonic-gate 					}
15897c478bd9Sstevel@tonic-gate 			} else {
15907c478bd9Sstevel@tonic-gate 				j = 1;
15917c478bd9Sstevel@tonic-gate 				while (j < timecnt && trans[i] >= ats[j])
15927c478bd9Sstevel@tonic-gate 					++j;
15937c478bd9Sstevel@tonic-gate 				j = types[j - 1];
15947c478bd9Sstevel@tonic-gate 			}
15957c478bd9Sstevel@tonic-gate 			puttzcode((long)tadd(trans[i], -gmtoffs[j]), fp);
15967c478bd9Sstevel@tonic-gate 		} else	puttzcode((long)trans[i], fp);
15977c478bd9Sstevel@tonic-gate 		puttzcode((long)corr[i], fp);
15987c478bd9Sstevel@tonic-gate 	}
15997c478bd9Sstevel@tonic-gate 	for (i = 0; i < typecnt; ++i)
16007c478bd9Sstevel@tonic-gate 		(void) putc(ttisstds[i], fp);
16017c478bd9Sstevel@tonic-gate 	for (i = 0; i < typecnt; ++i)
16027c478bd9Sstevel@tonic-gate 		(void) putc(ttisgmts[i], fp);
16037c478bd9Sstevel@tonic-gate 	if (ferror(fp) || fclose(fp)) {
16047c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("%s: Error writing %s\n"),
16057c478bd9Sstevel@tonic-gate 			progname, fullname);
160680868c53Srobbin 		exit(EXIT_FAILURE);
16077c478bd9Sstevel@tonic-gate 	}
16087c478bd9Sstevel@tonic-gate }
16097c478bd9Sstevel@tonic-gate 
16107c478bd9Sstevel@tonic-gate static void
doabbr(abbr,format,letters,isdst)16117c478bd9Sstevel@tonic-gate doabbr(abbr, format, letters, isdst)
16127c478bd9Sstevel@tonic-gate char * const		abbr;
16137c478bd9Sstevel@tonic-gate const char * const	format;
16147c478bd9Sstevel@tonic-gate const char * const	letters;
16157c478bd9Sstevel@tonic-gate const int		isdst;
16167c478bd9Sstevel@tonic-gate {
16177c478bd9Sstevel@tonic-gate 	if (strchr(format, '/') == NULL) {
16187c478bd9Sstevel@tonic-gate 		if (letters == NULL)
16197c478bd9Sstevel@tonic-gate 			(void) strcpy(abbr, format);
16207c478bd9Sstevel@tonic-gate 		else
16217c478bd9Sstevel@tonic-gate 			(void) sprintf(abbr, format, letters);
16227c478bd9Sstevel@tonic-gate 	} else if (isdst)
16237c478bd9Sstevel@tonic-gate 		(void) strcpy(abbr, strchr(format, '/') + 1);
16247c478bd9Sstevel@tonic-gate 	else {
16257c478bd9Sstevel@tonic-gate 		(void) strcpy(abbr, format);
16267c478bd9Sstevel@tonic-gate 		*strchr(abbr, '/') = '\0';
16277c478bd9Sstevel@tonic-gate 	}
16287c478bd9Sstevel@tonic-gate }
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate static void
outzone(zpfirst,zonecount)16317c478bd9Sstevel@tonic-gate outzone(zpfirst, zonecount)
16327c478bd9Sstevel@tonic-gate const struct zone * const	zpfirst;
16337c478bd9Sstevel@tonic-gate const int			zonecount;
16347c478bd9Sstevel@tonic-gate {
16357c478bd9Sstevel@tonic-gate 	register const struct zone	*zp;
16367c478bd9Sstevel@tonic-gate 	register struct rule		*rp;
16377c478bd9Sstevel@tonic-gate 	register int			i, j;
16387c478bd9Sstevel@tonic-gate 	register int			usestart, useuntil;
163980868c53Srobbin 	register zic_t			starttime, untiltime;
16407c478bd9Sstevel@tonic-gate 	register long			gmtoff;
16417c478bd9Sstevel@tonic-gate 	register long			stdoff;
16427c478bd9Sstevel@tonic-gate 	register int			year;
16437c478bd9Sstevel@tonic-gate 	register long			startoff;
16447c478bd9Sstevel@tonic-gate 	register int			startttisstd;
16457c478bd9Sstevel@tonic-gate 	register int			startttisgmt;
16467c478bd9Sstevel@tonic-gate 	register int			type;
16477c478bd9Sstevel@tonic-gate 	char				startbuf[BUFSIZ];
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	INITIALIZE(untiltime);
16507c478bd9Sstevel@tonic-gate 	INITIALIZE(starttime);
16517c478bd9Sstevel@tonic-gate 	/*
16527c478bd9Sstevel@tonic-gate 	 * Now. . .finally. . .generate some useful data!
16537c478bd9Sstevel@tonic-gate 	 */
16547c478bd9Sstevel@tonic-gate 	timecnt = 0;
16557c478bd9Sstevel@tonic-gate 	typecnt = 0;
16567c478bd9Sstevel@tonic-gate 	charcnt = 0;
16577c478bd9Sstevel@tonic-gate 	/*
1658f430f59aSrobbin 	 * Thanks to Earl Chew
16597c478bd9Sstevel@tonic-gate 	 * for noting the need to unconditionally initialize startttisstd.
16607c478bd9Sstevel@tonic-gate 	 */
16617c478bd9Sstevel@tonic-gate 	startttisstd = FALSE;
16627c478bd9Sstevel@tonic-gate 	startttisgmt = FALSE;
16637c478bd9Sstevel@tonic-gate 	for (i = 0; i < zonecount; ++i) {
16647c478bd9Sstevel@tonic-gate 		/*
16657c478bd9Sstevel@tonic-gate 		 * A guess that may well be corrected later.
16667c478bd9Sstevel@tonic-gate 		 */
16677c478bd9Sstevel@tonic-gate 		stdoff = 0;
16687c478bd9Sstevel@tonic-gate 		zp = &zpfirst[i];
16697c478bd9Sstevel@tonic-gate 		usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
16707c478bd9Sstevel@tonic-gate 		useuntil = i < (zonecount - 1);
16717c478bd9Sstevel@tonic-gate 		if (useuntil && zp->z_untiltime <= min_time)
16727c478bd9Sstevel@tonic-gate 			continue;
16737c478bd9Sstevel@tonic-gate 		gmtoff = zp->z_gmtoff;
16747c478bd9Sstevel@tonic-gate 		eat(zp->z_filename, zp->z_linenum);
16757c478bd9Sstevel@tonic-gate 		*startbuf = '\0';
16767c478bd9Sstevel@tonic-gate 		startoff = zp->z_gmtoff;
16777c478bd9Sstevel@tonic-gate 		if (zp->z_nrules == 0) {
16787c478bd9Sstevel@tonic-gate 			stdoff = zp->z_stdoff;
16797c478bd9Sstevel@tonic-gate 			doabbr(startbuf, zp->z_format,
16807c478bd9Sstevel@tonic-gate 				(char *)NULL, stdoff != 0);
16817c478bd9Sstevel@tonic-gate 			type = addtype(oadd(zp->z_gmtoff, stdoff),
16827c478bd9Sstevel@tonic-gate 				startbuf, stdoff != 0, startttisstd,
16837c478bd9Sstevel@tonic-gate 				startttisgmt);
16847c478bd9Sstevel@tonic-gate 			if (usestart) {
16857c478bd9Sstevel@tonic-gate 				addtt(starttime, type);
16867c478bd9Sstevel@tonic-gate 				usestart = FALSE;
168780868c53Srobbin 			} else if (stdoff != 0)
16887c478bd9Sstevel@tonic-gate 				addtt(min_time, type);
16897c478bd9Sstevel@tonic-gate 		} else
16907c478bd9Sstevel@tonic-gate 		    for (year = min_year; year <= max_year; ++year) {
16917c478bd9Sstevel@tonic-gate 			if (useuntil && year > zp->z_untilrule.r_hiyear)
16927c478bd9Sstevel@tonic-gate 				break;
16937c478bd9Sstevel@tonic-gate 			/*
16947c478bd9Sstevel@tonic-gate 			 * Mark which rules to do in the current year.
16957c478bd9Sstevel@tonic-gate 			 * For those to do, calculate rpytime(rp, year);
16967c478bd9Sstevel@tonic-gate 			 */
16977c478bd9Sstevel@tonic-gate 			for (j = 0; j < zp->z_nrules; ++j) {
16987c478bd9Sstevel@tonic-gate 				rp = &zp->z_rules[j];
16997c478bd9Sstevel@tonic-gate 				eats(zp->z_filename, zp->z_linenum,
17007c478bd9Sstevel@tonic-gate 					rp->r_filename, rp->r_linenum);
17017c478bd9Sstevel@tonic-gate 				rp->r_todo = year >= rp->r_loyear &&
17027c478bd9Sstevel@tonic-gate 						year <= rp->r_hiyear &&
17037c478bd9Sstevel@tonic-gate 						yearistype(year, rp->r_yrtype);
17047c478bd9Sstevel@tonic-gate 				if (rp->r_todo)
17057c478bd9Sstevel@tonic-gate 					rp->r_temp = rpytime(rp, year);
17067c478bd9Sstevel@tonic-gate 			}
17077c478bd9Sstevel@tonic-gate 			for (;;) {
17087c478bd9Sstevel@tonic-gate 				register int	k;
170980868c53Srobbin 				register zic_t	jtime, ktime;
17107c478bd9Sstevel@tonic-gate 				register long	offset;
17117c478bd9Sstevel@tonic-gate 				char		buf[BUFSIZ];
17127c478bd9Sstevel@tonic-gate 
17137c478bd9Sstevel@tonic-gate 				INITIALIZE(ktime);
17147c478bd9Sstevel@tonic-gate 				if (useuntil) {
17157c478bd9Sstevel@tonic-gate 					/*
171680868c53Srobbin 					 * Turn untiltime into UTC * assuming
171780868c53Srobbin 					 * the current gmtoff and stdoff values.
17187c478bd9Sstevel@tonic-gate 					 */
17197c478bd9Sstevel@tonic-gate 					untiltime = zp->z_untiltime;
17207c478bd9Sstevel@tonic-gate 					if (!zp->z_untilrule.r_todisgmt)
17217c478bd9Sstevel@tonic-gate 						untiltime = tadd(untiltime,
17227c478bd9Sstevel@tonic-gate 							-gmtoff);
17237c478bd9Sstevel@tonic-gate 					if (!zp->z_untilrule.r_todisstd)
17247c478bd9Sstevel@tonic-gate 						untiltime = tadd(untiltime,
17257c478bd9Sstevel@tonic-gate 							-stdoff);
17267c478bd9Sstevel@tonic-gate 				}
17277c478bd9Sstevel@tonic-gate 				/*
17287c478bd9Sstevel@tonic-gate 				 * Find the rule (of those to do, if any)
17297c478bd9Sstevel@tonic-gate 				 * that takes effect earliest in the year.
17307c478bd9Sstevel@tonic-gate 				 */
17317c478bd9Sstevel@tonic-gate 				k = -1;
17327c478bd9Sstevel@tonic-gate 				for (j = 0; j < zp->z_nrules; ++j) {
17337c478bd9Sstevel@tonic-gate 					rp = &zp->z_rules[j];
17347c478bd9Sstevel@tonic-gate 					if (!rp->r_todo)
17357c478bd9Sstevel@tonic-gate 						continue;
17367c478bd9Sstevel@tonic-gate 					eats(zp->z_filename, zp->z_linenum,
17377c478bd9Sstevel@tonic-gate 						rp->r_filename, rp->r_linenum);
17387c478bd9Sstevel@tonic-gate 					offset = rp->r_todisgmt ? 0 : gmtoff;
17397c478bd9Sstevel@tonic-gate 					if (!rp->r_todisstd)
17407c478bd9Sstevel@tonic-gate 						offset = oadd(offset, stdoff);
17417c478bd9Sstevel@tonic-gate 					jtime = rp->r_temp;
17427c478bd9Sstevel@tonic-gate 					if (jtime == min_time ||
17437c478bd9Sstevel@tonic-gate 						jtime == max_time)
17447c478bd9Sstevel@tonic-gate 							continue;
17457c478bd9Sstevel@tonic-gate 					jtime = tadd(jtime, -offset);
17467c478bd9Sstevel@tonic-gate 					if (k < 0 || jtime < ktime) {
17477c478bd9Sstevel@tonic-gate 						k = j;
17487c478bd9Sstevel@tonic-gate 						ktime = jtime;
17497c478bd9Sstevel@tonic-gate 					}
17507c478bd9Sstevel@tonic-gate 				}
17517c478bd9Sstevel@tonic-gate 				if (k < 0)
17527c478bd9Sstevel@tonic-gate 					break;	/* go on to next year */
17537c478bd9Sstevel@tonic-gate 				rp = &zp->z_rules[k];
17547c478bd9Sstevel@tonic-gate 				rp->r_todo = FALSE;
17557c478bd9Sstevel@tonic-gate 				if (useuntil && ktime >= untiltime)
17567c478bd9Sstevel@tonic-gate 					break;
17577c478bd9Sstevel@tonic-gate 				stdoff = rp->r_stdoff;
17587c478bd9Sstevel@tonic-gate 				if (usestart && ktime == starttime)
17597c478bd9Sstevel@tonic-gate 					usestart = FALSE;
17607c478bd9Sstevel@tonic-gate 				if (usestart) {
17617c478bd9Sstevel@tonic-gate 					if (ktime < starttime) {
17627c478bd9Sstevel@tonic-gate 						startoff = oadd(zp->z_gmtoff,
17637c478bd9Sstevel@tonic-gate 							stdoff);
17647c478bd9Sstevel@tonic-gate 						doabbr(startbuf, zp->z_format,
17657c478bd9Sstevel@tonic-gate 							rp->r_abbrvar,
17667c478bd9Sstevel@tonic-gate 							rp->r_stdoff != 0);
17677c478bd9Sstevel@tonic-gate 						continue;
17687c478bd9Sstevel@tonic-gate 					}
17697c478bd9Sstevel@tonic-gate 					if (*startbuf == '\0' &&
17707c478bd9Sstevel@tonic-gate 					    startoff == oadd(zp->z_gmtoff,
17717c478bd9Sstevel@tonic-gate 					    stdoff)) {
17727c478bd9Sstevel@tonic-gate 						doabbr(startbuf, zp->z_format,
17737c478bd9Sstevel@tonic-gate 							rp->r_abbrvar,
17747c478bd9Sstevel@tonic-gate 							rp->r_stdoff != 0);
17757c478bd9Sstevel@tonic-gate 					}
17767c478bd9Sstevel@tonic-gate 				}
17777c478bd9Sstevel@tonic-gate 				eats(zp->z_filename, zp->z_linenum,
17787c478bd9Sstevel@tonic-gate 					rp->r_filename, rp->r_linenum);
17797c478bd9Sstevel@tonic-gate 				doabbr(buf, zp->z_format, rp->r_abbrvar,
17807c478bd9Sstevel@tonic-gate 					rp->r_stdoff != 0);
17817c478bd9Sstevel@tonic-gate 				offset = oadd(zp->z_gmtoff, rp->r_stdoff);
17827c478bd9Sstevel@tonic-gate 				type = addtype(offset, buf, rp->r_stdoff != 0,
17837c478bd9Sstevel@tonic-gate 					rp->r_todisstd, rp->r_todisgmt);
17847c478bd9Sstevel@tonic-gate 				addtt(ktime, type);
17857c478bd9Sstevel@tonic-gate 			}
17867c478bd9Sstevel@tonic-gate 		    }
17877c478bd9Sstevel@tonic-gate 		if (usestart) {
17887c478bd9Sstevel@tonic-gate 			if (*startbuf == '\0' &&
17897c478bd9Sstevel@tonic-gate 				zp->z_format != NULL &&
17907c478bd9Sstevel@tonic-gate 				strchr(zp->z_format, '%') == NULL &&
17917c478bd9Sstevel@tonic-gate 				strchr(zp->z_format, '/') == NULL)
17927c478bd9Sstevel@tonic-gate 					(void) strcpy(startbuf, zp->z_format);
17937c478bd9Sstevel@tonic-gate 			eat(zp->z_filename, zp->z_linenum);
17947c478bd9Sstevel@tonic-gate 			if (*startbuf == '\0')
17957c478bd9Sstevel@tonic-gate 				error(gettext(
17967c478bd9Sstevel@tonic-gate "can't determine time zone abbrevation to use just after until time"));
17977c478bd9Sstevel@tonic-gate 			else	addtt(starttime,
17987c478bd9Sstevel@tonic-gate 					addtype(startoff, startbuf,
17997c478bd9Sstevel@tonic-gate 						startoff != zp->z_gmtoff,
18007c478bd9Sstevel@tonic-gate 						startttisstd,
18017c478bd9Sstevel@tonic-gate 						startttisgmt));
18027c478bd9Sstevel@tonic-gate 		}
18037c478bd9Sstevel@tonic-gate 		/*
18047c478bd9Sstevel@tonic-gate 		 * Now we may get to set starttime for the next zone line.
18057c478bd9Sstevel@tonic-gate 		 */
18067c478bd9Sstevel@tonic-gate 		if (useuntil) {
18077c478bd9Sstevel@tonic-gate 			startttisstd = zp->z_untilrule.r_todisstd;
18087c478bd9Sstevel@tonic-gate 			startttisgmt = zp->z_untilrule.r_todisgmt;
18097c478bd9Sstevel@tonic-gate 			starttime = zp->z_untiltime;
18107c478bd9Sstevel@tonic-gate 			if (!startttisstd)
18117c478bd9Sstevel@tonic-gate 				starttime = tadd(starttime, -stdoff);
18127c478bd9Sstevel@tonic-gate 			if (!startttisgmt)
18137c478bd9Sstevel@tonic-gate 				starttime = tadd(starttime, -gmtoff);
18147c478bd9Sstevel@tonic-gate 		}
18157c478bd9Sstevel@tonic-gate 	}
18167c478bd9Sstevel@tonic-gate 	writezone(zpfirst->z_name);
18177c478bd9Sstevel@tonic-gate }
18187c478bd9Sstevel@tonic-gate 
18197c478bd9Sstevel@tonic-gate static void
addtt(starttime,type)18207c478bd9Sstevel@tonic-gate addtt(starttime, type)
182180868c53Srobbin const zic_t	starttime;
18227c478bd9Sstevel@tonic-gate int		type;
18237c478bd9Sstevel@tonic-gate {
18247c478bd9Sstevel@tonic-gate 	if (starttime <= min_time ||
18257c478bd9Sstevel@tonic-gate 		(timecnt == 1 && attypes[0].at < min_time)) {
18267c478bd9Sstevel@tonic-gate 		gmtoffs[0] = gmtoffs[type];
18277c478bd9Sstevel@tonic-gate 		isdsts[0] = isdsts[type];
18287c478bd9Sstevel@tonic-gate 		ttisstds[0] = ttisstds[type];
18297c478bd9Sstevel@tonic-gate 		ttisgmts[0] = ttisgmts[type];
18307c478bd9Sstevel@tonic-gate 		if (abbrinds[type] != 0)
18317c478bd9Sstevel@tonic-gate 			(void) strcpy(chars, &chars[abbrinds[type]]);
18327c478bd9Sstevel@tonic-gate 		abbrinds[0] = 0;
18337c478bd9Sstevel@tonic-gate 		charcnt = strlen(chars) + 1;
18347c478bd9Sstevel@tonic-gate 		typecnt = 1;
18357c478bd9Sstevel@tonic-gate 		timecnt = 0;
18367c478bd9Sstevel@tonic-gate 		type = 0;
18377c478bd9Sstevel@tonic-gate 	}
18387c478bd9Sstevel@tonic-gate 	if (timecnt >= TZ_MAX_TIMES) {
18397c478bd9Sstevel@tonic-gate 		error(gettext("too many transitions?!"));
184080868c53Srobbin 		exit(EXIT_FAILURE);
18417c478bd9Sstevel@tonic-gate 	}
18427c478bd9Sstevel@tonic-gate 	attypes[timecnt].at = starttime;
18437c478bd9Sstevel@tonic-gate 	attypes[timecnt].type = type;
18447c478bd9Sstevel@tonic-gate 	++timecnt;
18457c478bd9Sstevel@tonic-gate }
18467c478bd9Sstevel@tonic-gate 
18477c478bd9Sstevel@tonic-gate static int
addtype(gmtoff,abbr,isdst,ttisstd,ttisgmt)18487c478bd9Sstevel@tonic-gate addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
18497c478bd9Sstevel@tonic-gate const long		gmtoff;
18507c478bd9Sstevel@tonic-gate const char * const	abbr;
18517c478bd9Sstevel@tonic-gate const int		isdst;
18527c478bd9Sstevel@tonic-gate const int		ttisstd;
18537c478bd9Sstevel@tonic-gate const int		ttisgmt;
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate 	register int	i, j;
18567c478bd9Sstevel@tonic-gate 
18577c478bd9Sstevel@tonic-gate 	if (isdst != TRUE && isdst != FALSE) {
18587c478bd9Sstevel@tonic-gate 		error(gettext(
18597c478bd9Sstevel@tonic-gate 		    "internal error - addtype called with bad isdst"));
186080868c53Srobbin 		exit(EXIT_FAILURE);
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate 	if (ttisstd != TRUE && ttisstd != FALSE) {
18637c478bd9Sstevel@tonic-gate 		error(gettext(
18647c478bd9Sstevel@tonic-gate 		    "internal error - addtype called with bad ttisstd"));
186580868c53Srobbin 		exit(EXIT_FAILURE);
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 	if (ttisgmt != TRUE && ttisgmt != FALSE) {
18687c478bd9Sstevel@tonic-gate 		error(gettext(
18697c478bd9Sstevel@tonic-gate 		    "internal error - addtype called with bad ttisgmt"));
187080868c53Srobbin 		exit(EXIT_FAILURE);
18717c478bd9Sstevel@tonic-gate 	}
18727c478bd9Sstevel@tonic-gate 	/*
18737c478bd9Sstevel@tonic-gate 	 * See if there's already an entry for this zone type.
18747c478bd9Sstevel@tonic-gate 	 * If so, just return its index.
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	for (i = 0; i < typecnt; ++i) {
18777c478bd9Sstevel@tonic-gate 		if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
18787c478bd9Sstevel@tonic-gate 			strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
18797c478bd9Sstevel@tonic-gate 			ttisstd == ttisstds[i] &&
18807c478bd9Sstevel@tonic-gate 			ttisgmt == ttisgmts[i])
18817c478bd9Sstevel@tonic-gate 				return (i);
18827c478bd9Sstevel@tonic-gate 	}
18837c478bd9Sstevel@tonic-gate 	/*
18847c478bd9Sstevel@tonic-gate 	 * There isn't one; add a new one, unless there are already too
18857c478bd9Sstevel@tonic-gate 	 * many.
18867c478bd9Sstevel@tonic-gate 	 */
18877c478bd9Sstevel@tonic-gate 	if (typecnt >= TZ_MAX_TYPES) {
18887c478bd9Sstevel@tonic-gate 		error(gettext("too many local time types"));
188980868c53Srobbin 		exit(EXIT_FAILURE);
18907c478bd9Sstevel@tonic-gate 	}
1891a68d064cSrobbin 	if (!(-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
1892a68d064cSrobbin 		error(gettext("UTC offset out of range"));
1893a68d064cSrobbin 		exit(EXIT_FAILURE);
1894a68d064cSrobbin 	}
18957c478bd9Sstevel@tonic-gate 	gmtoffs[i] = gmtoff;
18967c478bd9Sstevel@tonic-gate 	isdsts[i] = isdst;
18977c478bd9Sstevel@tonic-gate 	ttisstds[i] = ttisstd;
18987c478bd9Sstevel@tonic-gate 	ttisgmts[i] = ttisgmt;
18997c478bd9Sstevel@tonic-gate 
19007c478bd9Sstevel@tonic-gate 	for (j = 0; j < charcnt; ++j)
19017c478bd9Sstevel@tonic-gate 		if (strcmp(&chars[j], abbr) == 0)
19027c478bd9Sstevel@tonic-gate 			break;
19037c478bd9Sstevel@tonic-gate 	if (j == charcnt)
19047c478bd9Sstevel@tonic-gate 		newabbr(abbr);
19057c478bd9Sstevel@tonic-gate 	abbrinds[i] = j;
19067c478bd9Sstevel@tonic-gate 	++typecnt;
19077c478bd9Sstevel@tonic-gate 	return (i);
19087c478bd9Sstevel@tonic-gate }
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
19117c478bd9Sstevel@tonic-gate static void
leapadd(t,positive,rolling,count)19127c478bd9Sstevel@tonic-gate leapadd(t, positive, rolling, count)
191380868c53Srobbin const zic_t	t;
19147c478bd9Sstevel@tonic-gate const int	positive;
19157c478bd9Sstevel@tonic-gate const int	rolling;
19167c478bd9Sstevel@tonic-gate int		count;
19177c478bd9Sstevel@tonic-gate {
19187c478bd9Sstevel@tonic-gate 	register int	i, j;
19197c478bd9Sstevel@tonic-gate 
19207c478bd9Sstevel@tonic-gate 	if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
19217c478bd9Sstevel@tonic-gate 		error(gettext("too many leap seconds"));
192280868c53Srobbin 		exit(EXIT_FAILURE);
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate 	for (i = 0; i < leapcnt; ++i)
19257c478bd9Sstevel@tonic-gate 		if (t <= trans[i]) {
19267c478bd9Sstevel@tonic-gate 			if (t == trans[i]) {
19277c478bd9Sstevel@tonic-gate 				error(gettext("repeated leap second moment"));
192880868c53Srobbin 				exit(EXIT_FAILURE);
19297c478bd9Sstevel@tonic-gate 			}
19307c478bd9Sstevel@tonic-gate 			break;
19317c478bd9Sstevel@tonic-gate 		}
19327c478bd9Sstevel@tonic-gate 	do {
19337c478bd9Sstevel@tonic-gate 		for (j = leapcnt; j > i; --j) {
19347c478bd9Sstevel@tonic-gate 			trans[j] = trans[j - 1];
19357c478bd9Sstevel@tonic-gate 			corr[j] = corr[j - 1];
19367c478bd9Sstevel@tonic-gate 			roll[j] = roll[j - 1];
19377c478bd9Sstevel@tonic-gate 		}
19387c478bd9Sstevel@tonic-gate 		trans[i] = t;
19397c478bd9Sstevel@tonic-gate 		corr[i] = positive ? 1L : eitol(-count);
19407c478bd9Sstevel@tonic-gate 		roll[i] = rolling;
19417c478bd9Sstevel@tonic-gate 		++leapcnt;
19427c478bd9Sstevel@tonic-gate 	} while (positive && --count != 0);
19437c478bd9Sstevel@tonic-gate }
19447c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate #ifdef LEAPSECOND_SUPPORT
19477c478bd9Sstevel@tonic-gate static void
adjleap(void)19487c478bd9Sstevel@tonic-gate adjleap(void)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	register int	i;
19517c478bd9Sstevel@tonic-gate 	register long	last = 0;
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * propagate leap seconds forward
19557c478bd9Sstevel@tonic-gate 	 */
19567c478bd9Sstevel@tonic-gate 	for (i = 0; i < leapcnt; ++i) {
19577c478bd9Sstevel@tonic-gate 		trans[i] = tadd(trans[i], last);
19587c478bd9Sstevel@tonic-gate 		last = corr[i] += last;
19597c478bd9Sstevel@tonic-gate 	}
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate #endif /* LEAPSECOND_SUPPORT */
19627c478bd9Sstevel@tonic-gate 
19637c478bd9Sstevel@tonic-gate static int
yearistype(year,type)19647c478bd9Sstevel@tonic-gate yearistype(year, type)
19657c478bd9Sstevel@tonic-gate const int		year;
19667c478bd9Sstevel@tonic-gate const char * const	type;
19677c478bd9Sstevel@tonic-gate {
19687c478bd9Sstevel@tonic-gate 	static char	*buf;
19697c478bd9Sstevel@tonic-gate 	int		result;
19707c478bd9Sstevel@tonic-gate 
19717c478bd9Sstevel@tonic-gate 	if (type == NULL || *type == '\0')
19727c478bd9Sstevel@tonic-gate 		return (TRUE);
19737c478bd9Sstevel@tonic-gate #if defined(sun)
19747c478bd9Sstevel@tonic-gate 	if (strcmp(type, "uspres") == 0)
19757c478bd9Sstevel@tonic-gate 		return ((year % 4) == 0);
19767c478bd9Sstevel@tonic-gate 	if (strcmp(type, "nonpres") == 0)
19777c478bd9Sstevel@tonic-gate 		return ((year % 4) != 0);
19787c478bd9Sstevel@tonic-gate 	if (strcmp(type, "even") == 0)
19797c478bd9Sstevel@tonic-gate 		return ((year % 2) == 0);
19807c478bd9Sstevel@tonic-gate 	if (strcmp(type, "odd") == 0)
19817c478bd9Sstevel@tonic-gate 		return ((year % 2) != 0);
19827c478bd9Sstevel@tonic-gate #endif /* defined(sun) */
19837c478bd9Sstevel@tonic-gate 
19847c478bd9Sstevel@tonic-gate 	buf = erealloc(buf, (int)(132 + strlen(yitcommand) + strlen(type)));
19857c478bd9Sstevel@tonic-gate 	(void) sprintf(buf, "%s %d %s", yitcommand, year, type);
19867c478bd9Sstevel@tonic-gate 	result = system(buf);
198780868c53Srobbin 	if (WIFEXITED(result)) {
198880868c53Srobbin 		switch (WEXITSTATUS(result)) {
198980868c53Srobbin 		case 0:
19907c478bd9Sstevel@tonic-gate 			return (TRUE);
199180868c53Srobbin 		case 1:
19927c478bd9Sstevel@tonic-gate 			return (FALSE);
199380868c53Srobbin 		}
199480868c53Srobbin 	}
19957c478bd9Sstevel@tonic-gate 	error(gettext("Wild result from command execution"));
19967c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("%s: command was '%s', result was %d\n"),
19977c478bd9Sstevel@tonic-gate 		progname, buf, result);
19987c478bd9Sstevel@tonic-gate 	for (;;)
199980868c53Srobbin 		exit(EXIT_FAILURE);
20007c478bd9Sstevel@tonic-gate }
20017c478bd9Sstevel@tonic-gate 
20027c478bd9Sstevel@tonic-gate static int
lowerit(a)20037c478bd9Sstevel@tonic-gate lowerit(a)
20047c478bd9Sstevel@tonic-gate int	a;
20057c478bd9Sstevel@tonic-gate {
20067c478bd9Sstevel@tonic-gate 	a = (unsigned char) a;
20077c478bd9Sstevel@tonic-gate 	return ((isascii(a) && isupper(a)) ? tolower(a) : a);
20087c478bd9Sstevel@tonic-gate }
20097c478bd9Sstevel@tonic-gate 
20107c478bd9Sstevel@tonic-gate static int
ciequal(ap,bp)20117c478bd9Sstevel@tonic-gate ciequal(ap, bp)		/* case-insensitive equality */
20127c478bd9Sstevel@tonic-gate register const char *ap;
20137c478bd9Sstevel@tonic-gate register const char *bp;
20147c478bd9Sstevel@tonic-gate {
20157c478bd9Sstevel@tonic-gate 	while (lowerit(*ap) == lowerit(*bp++))
20167c478bd9Sstevel@tonic-gate 		if (*ap++ == '\0')
20177c478bd9Sstevel@tonic-gate 			return (TRUE);
20187c478bd9Sstevel@tonic-gate 	return (FALSE);
20197c478bd9Sstevel@tonic-gate }
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate static int
itsabbr(abbr,word)20227c478bd9Sstevel@tonic-gate itsabbr(abbr, word)
20237c478bd9Sstevel@tonic-gate register const char *abbr;
20247c478bd9Sstevel@tonic-gate register const char *word;
20257c478bd9Sstevel@tonic-gate {
20267c478bd9Sstevel@tonic-gate 	if (lowerit(*abbr) != lowerit(*word))
20277c478bd9Sstevel@tonic-gate 		return (FALSE);
20287c478bd9Sstevel@tonic-gate 	++word;
20297c478bd9Sstevel@tonic-gate 	while (*++abbr != '\0')
20307c478bd9Sstevel@tonic-gate 		do {
20317c478bd9Sstevel@tonic-gate 			if (*word == '\0')
20327c478bd9Sstevel@tonic-gate 				return (FALSE);
20337c478bd9Sstevel@tonic-gate 		} while (lowerit(*word++) != lowerit(*abbr));
20347c478bd9Sstevel@tonic-gate 	return (TRUE);
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate static const struct lookup *
byword(word,table)20387c478bd9Sstevel@tonic-gate byword(word, table)
20397c478bd9Sstevel@tonic-gate register const char * const		word;
20407c478bd9Sstevel@tonic-gate register const struct lookup * const	table;
20417c478bd9Sstevel@tonic-gate {
20427c478bd9Sstevel@tonic-gate 	register const struct lookup *foundlp;
20437c478bd9Sstevel@tonic-gate 	register const struct lookup *lp;
20447c478bd9Sstevel@tonic-gate 
20457c478bd9Sstevel@tonic-gate 	if (word == NULL || table == NULL)
20467c478bd9Sstevel@tonic-gate 		return (NULL);
20477c478bd9Sstevel@tonic-gate 	/*
20487c478bd9Sstevel@tonic-gate 	 * Look for exact match.
20497c478bd9Sstevel@tonic-gate 	 */
20507c478bd9Sstevel@tonic-gate 	for (lp = table; lp->l_word != NULL; ++lp)
20517c478bd9Sstevel@tonic-gate 		if (ciequal(word, lp->l_word))
20527c478bd9Sstevel@tonic-gate 			return (lp);
20537c478bd9Sstevel@tonic-gate 	/*
20547c478bd9Sstevel@tonic-gate 	 * Look for inexact match.
20557c478bd9Sstevel@tonic-gate 	 */
20567c478bd9Sstevel@tonic-gate 	foundlp = NULL;
20577c478bd9Sstevel@tonic-gate 	for (lp = table; lp->l_word != NULL; ++lp)
20587c478bd9Sstevel@tonic-gate 		if (itsabbr(word, lp->l_word)) {
20597c478bd9Sstevel@tonic-gate 			if (foundlp == NULL)
20607c478bd9Sstevel@tonic-gate 				foundlp = lp;
20617c478bd9Sstevel@tonic-gate 			else	return (NULL);	/* multiple inexact matches */
20627c478bd9Sstevel@tonic-gate 		}
20637c478bd9Sstevel@tonic-gate 	return (foundlp);
20647c478bd9Sstevel@tonic-gate }
20657c478bd9Sstevel@tonic-gate 
20667c478bd9Sstevel@tonic-gate static char **
getfields(cp)20677c478bd9Sstevel@tonic-gate getfields(cp)
20687c478bd9Sstevel@tonic-gate register char *cp;
20697c478bd9Sstevel@tonic-gate {
20707c478bd9Sstevel@tonic-gate 	register char 	*dp;
20717c478bd9Sstevel@tonic-gate 	register char	**array;
20727c478bd9Sstevel@tonic-gate 	register int	nsubs;
20737c478bd9Sstevel@tonic-gate 
20747c478bd9Sstevel@tonic-gate 	if (cp == NULL)
20757c478bd9Sstevel@tonic-gate 		return (NULL);
20767c478bd9Sstevel@tonic-gate 	array = (char **)(void *)
20777c478bd9Sstevel@tonic-gate 		emalloc((int)((strlen(cp) + 1) * sizeof (*array)));
20787c478bd9Sstevel@tonic-gate 	nsubs = 0;
20797c478bd9Sstevel@tonic-gate 	for (;;) {
20807c478bd9Sstevel@tonic-gate 		while (isascii(*cp) && isspace((unsigned char) *cp))
20817c478bd9Sstevel@tonic-gate 			++cp;
20827c478bd9Sstevel@tonic-gate 		if (*cp == '\0' || *cp == '#')
20837c478bd9Sstevel@tonic-gate 			break;
20847c478bd9Sstevel@tonic-gate 		array[nsubs++] = dp = cp;
20857c478bd9Sstevel@tonic-gate 		do {
20867c478bd9Sstevel@tonic-gate 			if ((*dp = *cp++) != '"')
20877c478bd9Sstevel@tonic-gate 				++dp;
20887c478bd9Sstevel@tonic-gate 			else while ((*dp = *cp++) != '"')
20897c478bd9Sstevel@tonic-gate 				if (*dp != '\0')
20907c478bd9Sstevel@tonic-gate 					++dp;
2091a68d064cSrobbin 				else {
20927c478bd9Sstevel@tonic-gate 					error(gettext(
20937c478bd9Sstevel@tonic-gate 					    "Odd number of quotation marks"));
2094a68d064cSrobbin 					exit(1);
2095a68d064cSrobbin 				}
20967c478bd9Sstevel@tonic-gate 		} while (*cp != '\0' && *cp != '#' &&
20977c478bd9Sstevel@tonic-gate 			(!isascii(*cp) || !isspace((unsigned char) *cp)));
20987c478bd9Sstevel@tonic-gate 		if (isascii(*cp) && isspace((unsigned char) *cp))
20997c478bd9Sstevel@tonic-gate 			++cp;
21007c478bd9Sstevel@tonic-gate 		*dp = '\0';
21017c478bd9Sstevel@tonic-gate 	}
21027c478bd9Sstevel@tonic-gate 	array[nsubs] = NULL;
21037c478bd9Sstevel@tonic-gate 	return (array);
21047c478bd9Sstevel@tonic-gate }
21057c478bd9Sstevel@tonic-gate 
21067c478bd9Sstevel@tonic-gate static long
oadd(t1,t2)21077c478bd9Sstevel@tonic-gate oadd(t1, t2)
21087c478bd9Sstevel@tonic-gate const long	t1;
21097c478bd9Sstevel@tonic-gate const long	t2;
21107c478bd9Sstevel@tonic-gate {
21117c478bd9Sstevel@tonic-gate 	register long	t;
21127c478bd9Sstevel@tonic-gate 
21137c478bd9Sstevel@tonic-gate 	t = t1 + t2;
21147c478bd9Sstevel@tonic-gate 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
21157c478bd9Sstevel@tonic-gate 		error(gettext("time overflow"));
211680868c53Srobbin 		exit(EXIT_FAILURE);
21177c478bd9Sstevel@tonic-gate 	}
21187c478bd9Sstevel@tonic-gate 	return (t);
21197c478bd9Sstevel@tonic-gate }
21207c478bd9Sstevel@tonic-gate 
212180868c53Srobbin static zic_t
tadd(t1,t2)21227c478bd9Sstevel@tonic-gate tadd(t1, t2)
212380868c53Srobbin const zic_t	t1;
21247c478bd9Sstevel@tonic-gate const long	t2;
21257c478bd9Sstevel@tonic-gate {
212680868c53Srobbin 	register zic_t	t;
21277c478bd9Sstevel@tonic-gate 
21287c478bd9Sstevel@tonic-gate 	if (t1 == max_time && t2 > 0)
21297c478bd9Sstevel@tonic-gate 		return (max_time);
21307c478bd9Sstevel@tonic-gate 	if (t1 == min_time && t2 < 0)
21317c478bd9Sstevel@tonic-gate 		return (min_time);
21327c478bd9Sstevel@tonic-gate 	t = t1 + t2;
21337c478bd9Sstevel@tonic-gate 	if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
21347c478bd9Sstevel@tonic-gate 		error(gettext("time overflow"));
213580868c53Srobbin 		exit(EXIT_FAILURE);
21367c478bd9Sstevel@tonic-gate 	}
21377c478bd9Sstevel@tonic-gate 	return (t);
21387c478bd9Sstevel@tonic-gate }
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate /*
21417c478bd9Sstevel@tonic-gate  * Given a rule, and a year, compute the date - in seconds since January 1,
21427c478bd9Sstevel@tonic-gate  * 1970, 00:00 LOCAL time - in that year that the rule refers to.
21437c478bd9Sstevel@tonic-gate  */
21447c478bd9Sstevel@tonic-gate 
214580868c53Srobbin static zic_t
rpytime(rp,wantedy)21467c478bd9Sstevel@tonic-gate rpytime(rp, wantedy)
21477c478bd9Sstevel@tonic-gate register const struct rule * const	rp;
21487c478bd9Sstevel@tonic-gate register const int			wantedy;
21497c478bd9Sstevel@tonic-gate {
21507c478bd9Sstevel@tonic-gate 	register int	y, m, i;
21517c478bd9Sstevel@tonic-gate 	register long	dayoff;			/* with a nod to Margaret O. */
215280868c53Srobbin 	register zic_t	t;
21537c478bd9Sstevel@tonic-gate 
21547c478bd9Sstevel@tonic-gate 	if (wantedy == INT_MIN)
21557c478bd9Sstevel@tonic-gate 		return (min_time);
21567c478bd9Sstevel@tonic-gate 	if (wantedy == INT_MAX)
21577c478bd9Sstevel@tonic-gate 		return (max_time);
21587c478bd9Sstevel@tonic-gate 	dayoff = 0;
21597c478bd9Sstevel@tonic-gate 	m = TM_JANUARY;
21607c478bd9Sstevel@tonic-gate 	y = EPOCH_YEAR;
21617c478bd9Sstevel@tonic-gate 	while (wantedy != y) {
21627c478bd9Sstevel@tonic-gate 		if (wantedy > y) {
21637c478bd9Sstevel@tonic-gate 			i = len_years[isleap(y)];
21647c478bd9Sstevel@tonic-gate 			++y;
21657c478bd9Sstevel@tonic-gate 		} else {
21667c478bd9Sstevel@tonic-gate 			--y;
21677c478bd9Sstevel@tonic-gate 			i = -len_years[isleap(y)];
21687c478bd9Sstevel@tonic-gate 		}
21697c478bd9Sstevel@tonic-gate 		dayoff = oadd(dayoff, eitol(i));
21707c478bd9Sstevel@tonic-gate 	}
21717c478bd9Sstevel@tonic-gate 	while (m != rp->r_month) {
21727c478bd9Sstevel@tonic-gate 		i = len_months[isleap(y)][m];
21737c478bd9Sstevel@tonic-gate 		dayoff = oadd(dayoff, eitol(i));
21747c478bd9Sstevel@tonic-gate 		++m;
21757c478bd9Sstevel@tonic-gate 	}
21767c478bd9Sstevel@tonic-gate 	i = rp->r_dayofmonth;
21777c478bd9Sstevel@tonic-gate 	if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
21787c478bd9Sstevel@tonic-gate 		if (rp->r_dycode == DC_DOWLEQ)
21797c478bd9Sstevel@tonic-gate 			--i;
21807c478bd9Sstevel@tonic-gate 		else {
21817c478bd9Sstevel@tonic-gate 			error(gettext("use of 2/29 in non leap-year"));
218280868c53Srobbin 			exit(EXIT_FAILURE);
21837c478bd9Sstevel@tonic-gate 		}
21847c478bd9Sstevel@tonic-gate 	}
21857c478bd9Sstevel@tonic-gate 	--i;
21867c478bd9Sstevel@tonic-gate 	dayoff = oadd(dayoff, eitol(i));
21877c478bd9Sstevel@tonic-gate 	if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
21887c478bd9Sstevel@tonic-gate 		register long	wday;
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate #define	LDAYSPERWEEK	((long)DAYSPERWEEK)
21917c478bd9Sstevel@tonic-gate 		wday = eitol(EPOCH_WDAY);
21927c478bd9Sstevel@tonic-gate 		/*
21937c478bd9Sstevel@tonic-gate 		 * Don't trust mod of negative numbers.
21947c478bd9Sstevel@tonic-gate 		 */
21957c478bd9Sstevel@tonic-gate 		if (dayoff >= 0)
21967c478bd9Sstevel@tonic-gate 			wday = (wday + dayoff) % LDAYSPERWEEK;
21977c478bd9Sstevel@tonic-gate 		else {
21987c478bd9Sstevel@tonic-gate 			wday -= ((-dayoff) % LDAYSPERWEEK);
21997c478bd9Sstevel@tonic-gate 			if (wday < 0)
22007c478bd9Sstevel@tonic-gate 				wday += LDAYSPERWEEK;
22017c478bd9Sstevel@tonic-gate 		}
22027c478bd9Sstevel@tonic-gate 		while (wday != eitol(rp->r_wday))
22037c478bd9Sstevel@tonic-gate 			if (rp->r_dycode == DC_DOWGEQ) {
22047c478bd9Sstevel@tonic-gate 				dayoff = oadd(dayoff, (long)1);
22057c478bd9Sstevel@tonic-gate 				if (++wday >= LDAYSPERWEEK)
22067c478bd9Sstevel@tonic-gate 					wday = 0;
22077c478bd9Sstevel@tonic-gate 				++i;
22087c478bd9Sstevel@tonic-gate 			} else {
22097c478bd9Sstevel@tonic-gate 				dayoff = oadd(dayoff, (long)-1);
22107c478bd9Sstevel@tonic-gate 				if (--wday < 0)
22117c478bd9Sstevel@tonic-gate 					wday = LDAYSPERWEEK - 1;
22127c478bd9Sstevel@tonic-gate 				--i;
22137c478bd9Sstevel@tonic-gate 			}
22147c478bd9Sstevel@tonic-gate 		if (i < 0 || i >= len_months[isleap(y)][m]) {
221580868c53Srobbin 			if (noise)
221680868c53Srobbin 				warning(gettext("rule goes past start/end of "
221780868c53Srobbin 				    "month--will not work with pre-2004 "
221880868c53Srobbin 				    "versions of zic"));
22197c478bd9Sstevel@tonic-gate 		}
22207c478bd9Sstevel@tonic-gate 	}
222180868c53Srobbin 	if (dayoff < 0 && !TYPE_SIGNED(zic_t))
22227c478bd9Sstevel@tonic-gate 		return (min_time);
222380868c53Srobbin 	if (dayoff < min_time / SECSPERDAY)
222480868c53Srobbin 		return (min_time);
222580868c53Srobbin 	if (dayoff > max_time / SECSPERDAY)
222680868c53Srobbin 		return (max_time);
222780868c53Srobbin 	t = (zic_t)dayoff * SECSPERDAY;
22287c478bd9Sstevel@tonic-gate 	return (tadd(t, rp->r_tod));
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate static void
newabbr(const char * const string)2232*5f0cdc3aSDan McDonald newabbr(const char * const string)
22337c478bd9Sstevel@tonic-gate {
22347c478bd9Sstevel@tonic-gate 	register int	i;
22357c478bd9Sstevel@tonic-gate 
223680868c53Srobbin 	if (strcmp(string, GRANDPARENTED) != 0) {
223780868c53Srobbin 		register const char *cp;
223880868c53Srobbin 		register char *wp;
223980868c53Srobbin 
224080868c53Srobbin 		cp = string;
224180868c53Srobbin 		wp = NULL;
2242*5f0cdc3aSDan McDonald 		while (isalpha(*cp) || ('0' <= *cp && *cp <= '9') ||
2243*5f0cdc3aSDan McDonald 		    *cp == '-' || *cp == '+') {
224480868c53Srobbin 			++cp;
2245*5f0cdc3aSDan McDonald 		}
2246*5f0cdc3aSDan McDonald 		if (noise && cp - string < 3)
2247*5f0cdc3aSDan McDonald 			wp = gettext(("time zone abbreviation has less than 3 "
224880868c53Srobbin 			    "alphabetics"));
2249*5f0cdc3aSDan McDonald 		if (wp == NULL && cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
225080868c53Srobbin 			wp = gettext(("time zone abbreviation has too many "
2251*5f0cdc3aSDan McDonald 			    "characters"));
225280868c53Srobbin 		if (wp == NULL && (*cp == '+' || *cp == '-')) {
225380868c53Srobbin 			++cp;
225480868c53Srobbin 			if (isascii(*cp) && isdigit(*cp))
225580868c53Srobbin 				if (*cp++ == '1' && *cp >= '0' && *cp <= '4')
225680868c53Srobbin 					++cp;
225780868c53Srobbin 		}
2258*5f0cdc3aSDan McDonald 		if (wp == NULL && *cp != '\0')
225980868c53Srobbin 			wp = gettext("time zone abbreviation differs from "
226080868c53Srobbin 			    "POSIX standard");
226180868c53Srobbin 		if (wp != NULL) {
226280868c53Srobbin 			wp = ecpyalloc(wp);
226380868c53Srobbin 			wp = ecatalloc(wp, " (");
226480868c53Srobbin 			wp = ecatalloc(wp, string);
226580868c53Srobbin 			wp = ecatalloc(wp, ")");
226680868c53Srobbin 			warning(wp);
226780868c53Srobbin 			ifree(wp);
226880868c53Srobbin 		}
226980868c53Srobbin 	}
22707c478bd9Sstevel@tonic-gate 	i = strlen(string) + 1;
22717c478bd9Sstevel@tonic-gate 	if (charcnt + i > TZ_MAX_CHARS) {
227280868c53Srobbin 		error(gettext("too many, or too long, time zone "
227380868c53Srobbin 		    "abbreviations"));
227480868c53Srobbin 		exit(EXIT_FAILURE);
22757c478bd9Sstevel@tonic-gate 	}
22767c478bd9Sstevel@tonic-gate 	(void) strcpy(&chars[charcnt], string);
22777c478bd9Sstevel@tonic-gate 	charcnt += eitol(i);
22787c478bd9Sstevel@tonic-gate }
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate static int
mkdirs(argname)22817c478bd9Sstevel@tonic-gate mkdirs(argname)
2282a68d064cSrobbin char *argname;
22837c478bd9Sstevel@tonic-gate {
22847c478bd9Sstevel@tonic-gate 	register char *name;
22857c478bd9Sstevel@tonic-gate 	register char *cp;
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 	if (argname == NULL || *argname == '\0')
22887c478bd9Sstevel@tonic-gate 		return (0);
22897c478bd9Sstevel@tonic-gate 	cp = name = ecpyalloc(argname);
22907c478bd9Sstevel@tonic-gate 	while ((cp = strchr(cp + 1, '/')) != 0) {
22917c478bd9Sstevel@tonic-gate 		*cp = '\0';
22927c478bd9Sstevel@tonic-gate 		if (!itsdir(name)) {
22937c478bd9Sstevel@tonic-gate 			/*
22947c478bd9Sstevel@tonic-gate 			 * It doesn't seem to exist, so we try to create it.
22957c478bd9Sstevel@tonic-gate 			 * Creation may fail because of the directory being
22967c478bd9Sstevel@tonic-gate 			 * created by some other multiprocessor, so we get
22977c478bd9Sstevel@tonic-gate 			 * to do extra checking.
22987c478bd9Sstevel@tonic-gate 			 */
229980868c53Srobbin 			if (mkdir(name, MKDIR_UMASK) != 0) {
23007c478bd9Sstevel@tonic-gate 				const char *e = strerror(errno);
23017c478bd9Sstevel@tonic-gate 
23027c478bd9Sstevel@tonic-gate 				if (errno != EEXIST || !itsdir(name)) {
23037c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext(
23047c478bd9Sstevel@tonic-gate 				    "%s: Can't create directory %s: %s\n"),
23057c478bd9Sstevel@tonic-gate 						progname, name, e);
23067c478bd9Sstevel@tonic-gate 					ifree(name);
23077c478bd9Sstevel@tonic-gate 					return (-1);
23087c478bd9Sstevel@tonic-gate 				}
23097c478bd9Sstevel@tonic-gate 			}
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 		*cp = '/';
23127c478bd9Sstevel@tonic-gate 	}
23137c478bd9Sstevel@tonic-gate 	ifree(name);
23147c478bd9Sstevel@tonic-gate 	return (0);
23157c478bd9Sstevel@tonic-gate }
23167c478bd9Sstevel@tonic-gate 
23177c478bd9Sstevel@tonic-gate static long
eitol(i)23187c478bd9Sstevel@tonic-gate eitol(i)
23197c478bd9Sstevel@tonic-gate const int	i;
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	long	l;
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	l = i;
23247c478bd9Sstevel@tonic-gate 	if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
23257c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
23267c478bd9Sstevel@tonic-gate 			gettext("%s: %d did not sign extend correctly\n"),
23277c478bd9Sstevel@tonic-gate 			progname, i);
232880868c53Srobbin 		exit(EXIT_FAILURE);
23297c478bd9Sstevel@tonic-gate 	}
23307c478bd9Sstevel@tonic-gate 	return (l);
23317c478bd9Sstevel@tonic-gate }
23327c478bd9Sstevel@tonic-gate 
23337c478bd9Sstevel@tonic-gate /*
233480868c53Srobbin  * UNIX was a registered trademark of The Open Group in 2003.
23357c478bd9Sstevel@tonic-gate  */
2336