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