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