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