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