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