1*0a6a1f1dSLionel Sambuc /* $NetBSD: zic.c,v 1.56 2015/10/09 17:21:45 christos Exp $ */
22fe8fb19SBen Gras /*
32fe8fb19SBen Gras ** This file is in the public domain, so clarified as of
42fe8fb19SBen Gras ** 2006-07-17 by Arthur David Olson.
52fe8fb19SBen Gras */
62fe8fb19SBen Gras
72fe8fb19SBen Gras #if HAVE_NBTOOL_CONFIG_H
82fe8fb19SBen Gras #include "nbtool_config.h"
92fe8fb19SBen Gras #endif
102fe8fb19SBen Gras
112fe8fb19SBen Gras #include <sys/cdefs.h>
122fe8fb19SBen Gras #ifndef lint
13*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: zic.c,v 1.56 2015/10/09 17:21:45 christos Exp $");
142fe8fb19SBen Gras #endif /* !defined lint */
152fe8fb19SBen Gras
162fe8fb19SBen Gras #include "private.h"
172fe8fb19SBen Gras #include "locale.h"
182fe8fb19SBen Gras #include "tzfile.h"
192fe8fb19SBen Gras
2084d9c625SLionel Sambuc #include <stdarg.h>
2184d9c625SLionel Sambuc #include <unistd.h>
2284d9c625SLionel Sambuc
2384d9c625SLionel Sambuc #define ZIC_VERSION_PRE_2013 '2'
2484d9c625SLionel Sambuc #define ZIC_VERSION '3'
252fe8fb19SBen Gras
262fe8fb19SBen Gras typedef int_fast64_t zic_t;
2784d9c625SLionel Sambuc #define ZIC_MIN INT_FAST64_MIN
2884d9c625SLionel Sambuc #define ZIC_MAX INT_FAST64_MAX
2984d9c625SLionel Sambuc #define SCNdZIC SCNdFAST64
302fe8fb19SBen Gras
312fe8fb19SBen Gras #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
322fe8fb19SBen Gras #define ZIC_MAX_ABBR_LEN_WO_WARN 6
332fe8fb19SBen Gras #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
342fe8fb19SBen Gras
352fe8fb19SBen Gras #if HAVE_SYS_STAT_H
36*0a6a1f1dSLionel Sambuc #include <sys/stat.h>
372fe8fb19SBen Gras #endif
382fe8fb19SBen Gras #ifdef S_IRUSR
392fe8fb19SBen Gras #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
402fe8fb19SBen Gras #else
412fe8fb19SBen Gras #define MKDIR_UMASK 0755
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras struct rule {
452fe8fb19SBen Gras const char * r_filename;
462fe8fb19SBen Gras int r_linenum;
472fe8fb19SBen Gras const char * r_name;
482fe8fb19SBen Gras
4984d9c625SLionel Sambuc zic_t r_loyear; /* for example, 1986 */
5084d9c625SLionel Sambuc zic_t r_hiyear; /* for example, 1986 */
512fe8fb19SBen Gras const char * r_yrtype;
52*0a6a1f1dSLionel Sambuc bool r_lowasnum;
53*0a6a1f1dSLionel Sambuc bool r_hiwasnum;
542fe8fb19SBen Gras
552fe8fb19SBen Gras int r_month; /* 0..11 */
562fe8fb19SBen Gras
572fe8fb19SBen Gras int r_dycode; /* see below */
582fe8fb19SBen Gras int r_dayofmonth;
592fe8fb19SBen Gras int r_wday;
602fe8fb19SBen Gras
6184d9c625SLionel Sambuc zic_t r_tod; /* time from midnight */
62*0a6a1f1dSLionel Sambuc bool r_todisstd; /* above is standard time if 1 */
63*0a6a1f1dSLionel Sambuc /* or wall clock time if 0 */
64*0a6a1f1dSLionel Sambuc bool r_todisgmt; /* above is GMT if 1 */
65*0a6a1f1dSLionel Sambuc /* or local time if 0 */
6684d9c625SLionel Sambuc zic_t r_stdoff; /* offset from standard time */
672fe8fb19SBen Gras const char * r_abbrvar; /* variable part of abbreviation */
682fe8fb19SBen Gras
692fe8fb19SBen Gras int r_todo; /* a rule to do (used in outzone) */
702fe8fb19SBen Gras zic_t r_temp; /* used in outzone */
712fe8fb19SBen Gras };
722fe8fb19SBen Gras
732fe8fb19SBen Gras /*
742fe8fb19SBen Gras ** r_dycode r_dayofmonth r_wday
752fe8fb19SBen Gras */
762fe8fb19SBen Gras
772fe8fb19SBen Gras #define DC_DOM 0 /* 1..31 */ /* unused */
782fe8fb19SBen Gras #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
792fe8fb19SBen Gras #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
802fe8fb19SBen Gras
812fe8fb19SBen Gras struct zone {
822fe8fb19SBen Gras const char * z_filename;
832fe8fb19SBen Gras int z_linenum;
842fe8fb19SBen Gras
852fe8fb19SBen Gras const char * z_name;
8684d9c625SLionel Sambuc zic_t z_gmtoff;
872fe8fb19SBen Gras const char * z_rule;
882fe8fb19SBen Gras const char * z_format;
89*0a6a1f1dSLionel Sambuc char z_format_specifier;
902fe8fb19SBen Gras
9184d9c625SLionel Sambuc zic_t z_stdoff;
922fe8fb19SBen Gras
932fe8fb19SBen Gras struct rule * z_rules;
942fe8fb19SBen Gras int z_nrules;
952fe8fb19SBen Gras
962fe8fb19SBen Gras struct rule z_untilrule;
972fe8fb19SBen Gras zic_t z_untiltime;
982fe8fb19SBen Gras };
992fe8fb19SBen Gras
1002fe8fb19SBen Gras extern int getopt(int argc, char * const argv[],
1012fe8fb19SBen Gras const char * options);
1022fe8fb19SBen Gras extern int link(const char * fromname, const char * toname);
1032fe8fb19SBen Gras extern char * optarg;
1042fe8fb19SBen Gras extern int optind;
1052fe8fb19SBen Gras
106*0a6a1f1dSLionel Sambuc #if ! HAVE_LINK
107*0a6a1f1dSLionel Sambuc # define link(from, to) (-1)
108*0a6a1f1dSLionel Sambuc #endif
109*0a6a1f1dSLionel Sambuc #if ! HAVE_SYMLINK
110*0a6a1f1dSLionel Sambuc # define symlink(from, to) (-1)
111*0a6a1f1dSLionel Sambuc #endif
112*0a6a1f1dSLionel Sambuc
1132fe8fb19SBen Gras static void addtt(zic_t starttime, int type);
114*0a6a1f1dSLionel Sambuc static int addtype(zic_t, char * const, bool, bool, bool);
115*0a6a1f1dSLionel Sambuc static void leapadd(zic_t, bool, int, int);
1162fe8fb19SBen Gras static void adjleap(void);
1172fe8fb19SBen Gras static void associate(void);
1182fe8fb19SBen Gras static void dolink(const char * fromfield, const char * tofield);
1192fe8fb19SBen Gras static char ** getfields(char * buf);
120*0a6a1f1dSLionel Sambuc static zic_t gethms(const char * string, const char * errstring,
121*0a6a1f1dSLionel Sambuc bool);
1222fe8fb19SBen Gras static void infile(const char * filename);
1232fe8fb19SBen Gras static void inleap(char ** fields, int nfields);
1242fe8fb19SBen Gras static void inlink(char ** fields, int nfields);
1252fe8fb19SBen Gras static void inrule(char ** fields, int nfields);
126*0a6a1f1dSLionel Sambuc static bool inzcont(char ** fields, int nfields);
127*0a6a1f1dSLionel Sambuc static bool inzone(char ** fields, int nfields);
128*0a6a1f1dSLionel Sambuc static bool inzsub(char ** fields, int nfields, int iscont);
1292fe8fb19SBen Gras static int itsdir(const char * name);
130*0a6a1f1dSLionel Sambuc static bool is_alpha(char a);
131*0a6a1f1dSLionel Sambuc static char lowerit(char);
132*0a6a1f1dSLionel Sambuc static bool mkdirs(char * filename);
1332fe8fb19SBen Gras static void newabbr(const char * abbr);
13484d9c625SLionel Sambuc static zic_t oadd(zic_t t1, zic_t t2);
1352fe8fb19SBen Gras static void outzone(const struct zone * zp, int ntzones);
1362fe8fb19SBen Gras static int rcomp(const void * leftp, const void * rightp);
13784d9c625SLionel Sambuc static zic_t rpytime(const struct rule * rp, zic_t wantedy);
1382fe8fb19SBen Gras static void rulesub(struct rule * rp,
1392fe8fb19SBen Gras const char * loyearp, const char * hiyearp,
1402fe8fb19SBen Gras const char * typep, const char * monthp,
1412fe8fb19SBen Gras const char * dayp, const char * timep);
14284d9c625SLionel Sambuc static zic_t tadd(zic_t t1, zic_t t2);
143*0a6a1f1dSLionel Sambuc static bool yearistype(int year, const char * type);
1442fe8fb19SBen Gras static int atcomp(const void *avp, const void *bvp);
14584d9c625SLionel Sambuc static void updateminmax(zic_t x);
1462fe8fb19SBen Gras
147*0a6a1f1dSLionel Sambuc /* Bound on length of what %z can expand to. */
148*0a6a1f1dSLionel Sambuc enum { PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1 };
149*0a6a1f1dSLionel Sambuc
1502fe8fb19SBen Gras static int charcnt;
151*0a6a1f1dSLionel Sambuc static bool errors;
152*0a6a1f1dSLionel Sambuc static bool warnings;
1532fe8fb19SBen Gras static const char * filename;
1542fe8fb19SBen Gras static int leapcnt;
155*0a6a1f1dSLionel Sambuc static bool leapseen;
15684d9c625SLionel Sambuc static zic_t leapminyear;
15784d9c625SLionel Sambuc static zic_t leapmaxyear;
1582fe8fb19SBen Gras static int linenum;
159*0a6a1f1dSLionel Sambuc static size_t max_abbrvar_len = PERCENT_Z_LEN_BOUND;
16084d9c625SLionel Sambuc static size_t max_format_len;
16184d9c625SLionel Sambuc static zic_t max_year;
16284d9c625SLionel Sambuc static zic_t min_year;
163*0a6a1f1dSLionel Sambuc static bool noise;
1642fe8fb19SBen Gras static const char * rfilename;
1652fe8fb19SBen Gras static int rlinenum;
1662fe8fb19SBen Gras static const char * progname;
1672fe8fb19SBen Gras static int timecnt;
168*0a6a1f1dSLionel Sambuc static int timecnt_alloc;
1692fe8fb19SBen Gras static int typecnt;
1702fe8fb19SBen Gras
1712fe8fb19SBen Gras /*
1722fe8fb19SBen Gras ** Line codes.
1732fe8fb19SBen Gras */
1742fe8fb19SBen Gras
1752fe8fb19SBen Gras #define LC_RULE 0
1762fe8fb19SBen Gras #define LC_ZONE 1
1772fe8fb19SBen Gras #define LC_LINK 2
1782fe8fb19SBen Gras #define LC_LEAP 3
1792fe8fb19SBen Gras
1802fe8fb19SBen Gras /*
1812fe8fb19SBen Gras ** Which fields are which on a Zone line.
1822fe8fb19SBen Gras */
1832fe8fb19SBen Gras
1842fe8fb19SBen Gras #define ZF_NAME 1
1852fe8fb19SBen Gras #define ZF_GMTOFF 2
1862fe8fb19SBen Gras #define ZF_RULE 3
1872fe8fb19SBen Gras #define ZF_FORMAT 4
1882fe8fb19SBen Gras #define ZF_TILYEAR 5
1892fe8fb19SBen Gras #define ZF_TILMONTH 6
1902fe8fb19SBen Gras #define ZF_TILDAY 7
1912fe8fb19SBen Gras #define ZF_TILTIME 8
1922fe8fb19SBen Gras #define ZONE_MINFIELDS 5
1932fe8fb19SBen Gras #define ZONE_MAXFIELDS 9
1942fe8fb19SBen Gras
1952fe8fb19SBen Gras /*
1962fe8fb19SBen Gras ** Which fields are which on a Zone continuation line.
1972fe8fb19SBen Gras */
1982fe8fb19SBen Gras
1992fe8fb19SBen Gras #define ZFC_GMTOFF 0
2002fe8fb19SBen Gras #define ZFC_RULE 1
2012fe8fb19SBen Gras #define ZFC_FORMAT 2
2022fe8fb19SBen Gras #define ZFC_TILYEAR 3
2032fe8fb19SBen Gras #define ZFC_TILMONTH 4
2042fe8fb19SBen Gras #define ZFC_TILDAY 5
2052fe8fb19SBen Gras #define ZFC_TILTIME 6
2062fe8fb19SBen Gras #define ZONEC_MINFIELDS 3
2072fe8fb19SBen Gras #define ZONEC_MAXFIELDS 7
2082fe8fb19SBen Gras
2092fe8fb19SBen Gras /*
2102fe8fb19SBen Gras ** Which files are which on a Rule line.
2112fe8fb19SBen Gras */
2122fe8fb19SBen Gras
2132fe8fb19SBen Gras #define RF_NAME 1
2142fe8fb19SBen Gras #define RF_LOYEAR 2
2152fe8fb19SBen Gras #define RF_HIYEAR 3
2162fe8fb19SBen Gras #define RF_COMMAND 4
2172fe8fb19SBen Gras #define RF_MONTH 5
2182fe8fb19SBen Gras #define RF_DAY 6
2192fe8fb19SBen Gras #define RF_TOD 7
2202fe8fb19SBen Gras #define RF_STDOFF 8
2212fe8fb19SBen Gras #define RF_ABBRVAR 9
2222fe8fb19SBen Gras #define RULE_FIELDS 10
2232fe8fb19SBen Gras
2242fe8fb19SBen Gras /*
2252fe8fb19SBen Gras ** Which fields are which on a Link line.
2262fe8fb19SBen Gras */
2272fe8fb19SBen Gras
2282fe8fb19SBen Gras #define LF_FROM 1
2292fe8fb19SBen Gras #define LF_TO 2
2302fe8fb19SBen Gras #define LINK_FIELDS 3
2312fe8fb19SBen Gras
2322fe8fb19SBen Gras /*
2332fe8fb19SBen Gras ** Which fields are which on a Leap line.
2342fe8fb19SBen Gras */
2352fe8fb19SBen Gras
2362fe8fb19SBen Gras #define LP_YEAR 1
2372fe8fb19SBen Gras #define LP_MONTH 2
2382fe8fb19SBen Gras #define LP_DAY 3
2392fe8fb19SBen Gras #define LP_TIME 4
2402fe8fb19SBen Gras #define LP_CORR 5
2412fe8fb19SBen Gras #define LP_ROLL 6
2422fe8fb19SBen Gras #define LEAP_FIELDS 7
2432fe8fb19SBen Gras
2442fe8fb19SBen Gras /*
2452fe8fb19SBen Gras ** Year synonyms.
2462fe8fb19SBen Gras */
2472fe8fb19SBen Gras
2482fe8fb19SBen Gras #define YR_MINIMUM 0
2492fe8fb19SBen Gras #define YR_MAXIMUM 1
2502fe8fb19SBen Gras #define YR_ONLY 2
2512fe8fb19SBen Gras
2522fe8fb19SBen Gras static struct rule * rules;
2532fe8fb19SBen Gras static int nrules; /* number of rules */
254*0a6a1f1dSLionel Sambuc static int nrules_alloc;
2552fe8fb19SBen Gras
2562fe8fb19SBen Gras static struct zone * zones;
2572fe8fb19SBen Gras static int nzones; /* number of zones */
258*0a6a1f1dSLionel Sambuc static int nzones_alloc;
2592fe8fb19SBen Gras
2602fe8fb19SBen Gras struct link {
2612fe8fb19SBen Gras const char * l_filename;
2622fe8fb19SBen Gras int l_linenum;
2632fe8fb19SBen Gras const char * l_from;
2642fe8fb19SBen Gras const char * l_to;
2652fe8fb19SBen Gras };
2662fe8fb19SBen Gras
2672fe8fb19SBen Gras static struct link * links;
2682fe8fb19SBen Gras static int nlinks;
269*0a6a1f1dSLionel Sambuc static int nlinks_alloc;
2702fe8fb19SBen Gras
2712fe8fb19SBen Gras struct lookup {
2722fe8fb19SBen Gras const char * l_word;
2732fe8fb19SBen Gras const int l_value;
2742fe8fb19SBen Gras };
2752fe8fb19SBen Gras
2762fe8fb19SBen Gras static struct lookup const * byword(const char * string,
2772fe8fb19SBen Gras const struct lookup * lp);
2782fe8fb19SBen Gras
2792fe8fb19SBen Gras static struct lookup const line_codes[] = {
2802fe8fb19SBen Gras { "Rule", LC_RULE },
2812fe8fb19SBen Gras { "Zone", LC_ZONE },
2822fe8fb19SBen Gras { "Link", LC_LINK },
2832fe8fb19SBen Gras { "Leap", LC_LEAP },
2842fe8fb19SBen Gras { NULL, 0}
2852fe8fb19SBen Gras };
2862fe8fb19SBen Gras
2872fe8fb19SBen Gras static struct lookup const mon_names[] = {
2882fe8fb19SBen Gras { "January", TM_JANUARY },
2892fe8fb19SBen Gras { "February", TM_FEBRUARY },
2902fe8fb19SBen Gras { "March", TM_MARCH },
2912fe8fb19SBen Gras { "April", TM_APRIL },
2922fe8fb19SBen Gras { "May", TM_MAY },
2932fe8fb19SBen Gras { "June", TM_JUNE },
2942fe8fb19SBen Gras { "July", TM_JULY },
2952fe8fb19SBen Gras { "August", TM_AUGUST },
2962fe8fb19SBen Gras { "September", TM_SEPTEMBER },
2972fe8fb19SBen Gras { "October", TM_OCTOBER },
2982fe8fb19SBen Gras { "November", TM_NOVEMBER },
2992fe8fb19SBen Gras { "December", TM_DECEMBER },
3002fe8fb19SBen Gras { NULL, 0 }
3012fe8fb19SBen Gras };
3022fe8fb19SBen Gras
3032fe8fb19SBen Gras static struct lookup const wday_names[] = {
3042fe8fb19SBen Gras { "Sunday", TM_SUNDAY },
3052fe8fb19SBen Gras { "Monday", TM_MONDAY },
3062fe8fb19SBen Gras { "Tuesday", TM_TUESDAY },
3072fe8fb19SBen Gras { "Wednesday", TM_WEDNESDAY },
3082fe8fb19SBen Gras { "Thursday", TM_THURSDAY },
3092fe8fb19SBen Gras { "Friday", TM_FRIDAY },
3102fe8fb19SBen Gras { "Saturday", TM_SATURDAY },
3112fe8fb19SBen Gras { NULL, 0 }
3122fe8fb19SBen Gras };
3132fe8fb19SBen Gras
3142fe8fb19SBen Gras static struct lookup const lasts[] = {
3152fe8fb19SBen Gras { "last-Sunday", TM_SUNDAY },
3162fe8fb19SBen Gras { "last-Monday", TM_MONDAY },
3172fe8fb19SBen Gras { "last-Tuesday", TM_TUESDAY },
3182fe8fb19SBen Gras { "last-Wednesday", TM_WEDNESDAY },
3192fe8fb19SBen Gras { "last-Thursday", TM_THURSDAY },
3202fe8fb19SBen Gras { "last-Friday", TM_FRIDAY },
3212fe8fb19SBen Gras { "last-Saturday", TM_SATURDAY },
3222fe8fb19SBen Gras { NULL, 0 }
3232fe8fb19SBen Gras };
3242fe8fb19SBen Gras
3252fe8fb19SBen Gras static struct lookup const begin_years[] = {
3262fe8fb19SBen Gras { "minimum", YR_MINIMUM },
3272fe8fb19SBen Gras { "maximum", YR_MAXIMUM },
3282fe8fb19SBen Gras { NULL, 0 }
3292fe8fb19SBen Gras };
3302fe8fb19SBen Gras
3312fe8fb19SBen Gras static struct lookup const end_years[] = {
3322fe8fb19SBen Gras { "minimum", YR_MINIMUM },
3332fe8fb19SBen Gras { "maximum", YR_MAXIMUM },
3342fe8fb19SBen Gras { "only", YR_ONLY },
3352fe8fb19SBen Gras { NULL, 0 }
3362fe8fb19SBen Gras };
3372fe8fb19SBen Gras
3382fe8fb19SBen Gras static struct lookup const leap_types[] = {
339*0a6a1f1dSLionel Sambuc { "Rolling", true },
340*0a6a1f1dSLionel Sambuc { "Stationary", false },
3412fe8fb19SBen Gras { NULL, 0 }
3422fe8fb19SBen Gras };
3432fe8fb19SBen Gras
3442fe8fb19SBen Gras static const int len_months[2][MONSPERYEAR] = {
3452fe8fb19SBen Gras { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
3462fe8fb19SBen Gras { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
3472fe8fb19SBen Gras };
3482fe8fb19SBen Gras
3492fe8fb19SBen Gras static const int len_years[2] = {
3502fe8fb19SBen Gras DAYSPERNYEAR, DAYSPERLYEAR
3512fe8fb19SBen Gras };
3522fe8fb19SBen Gras
3532fe8fb19SBen Gras static struct attype {
3542fe8fb19SBen Gras zic_t at;
3552fe8fb19SBen Gras unsigned char type;
356*0a6a1f1dSLionel Sambuc } * attypes;
35784d9c625SLionel Sambuc static zic_t gmtoffs[TZ_MAX_TYPES];
3582fe8fb19SBen Gras static char isdsts[TZ_MAX_TYPES];
3592fe8fb19SBen Gras static unsigned char abbrinds[TZ_MAX_TYPES];
360*0a6a1f1dSLionel Sambuc static bool ttisstds[TZ_MAX_TYPES];
361*0a6a1f1dSLionel Sambuc static bool ttisgmts[TZ_MAX_TYPES];
3622fe8fb19SBen Gras static char chars[TZ_MAX_CHARS];
3632fe8fb19SBen Gras static zic_t trans[TZ_MAX_LEAPS];
36484d9c625SLionel Sambuc static zic_t corr[TZ_MAX_LEAPS];
3652fe8fb19SBen Gras static char roll[TZ_MAX_LEAPS];
3662fe8fb19SBen Gras
3672fe8fb19SBen Gras /*
3682fe8fb19SBen Gras ** Memory allocation.
3692fe8fb19SBen Gras */
3702fe8fb19SBen Gras
371*0a6a1f1dSLionel Sambuc static _Noreturn void
memory_exhausted(const char * msg)372*0a6a1f1dSLionel Sambuc memory_exhausted(const char *msg)
3732fe8fb19SBen Gras {
374*0a6a1f1dSLionel Sambuc fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
3752fe8fb19SBen Gras exit(EXIT_FAILURE);
3762fe8fb19SBen Gras }
377*0a6a1f1dSLionel Sambuc
378*0a6a1f1dSLionel Sambuc static ATTRIBUTE_PURE size_t
size_product(size_t nitems,size_t itemsize)379*0a6a1f1dSLionel Sambuc size_product(size_t nitems, size_t itemsize)
380*0a6a1f1dSLionel Sambuc {
381*0a6a1f1dSLionel Sambuc if (SIZE_MAX / itemsize < nitems)
382*0a6a1f1dSLionel Sambuc memory_exhausted(_("size overflow"));
383*0a6a1f1dSLionel Sambuc return nitems * itemsize;
384*0a6a1f1dSLionel Sambuc }
385*0a6a1f1dSLionel Sambuc
386*0a6a1f1dSLionel Sambuc #if !HAVE_STRDUP
387*0a6a1f1dSLionel Sambuc static char *
strdup(char const * str)388*0a6a1f1dSLionel Sambuc strdup(char const *str)
389*0a6a1f1dSLionel Sambuc {
390*0a6a1f1dSLionel Sambuc char *result = malloc(strlen(str) + 1);
391*0a6a1f1dSLionel Sambuc return result ? strcpy(result, str) : result;
392*0a6a1f1dSLionel Sambuc }
393*0a6a1f1dSLionel Sambuc #endif
394*0a6a1f1dSLionel Sambuc
395*0a6a1f1dSLionel Sambuc static ATTRIBUTE_PURE void *
memcheck(void * ptr)396*0a6a1f1dSLionel Sambuc memcheck(void *ptr)
397*0a6a1f1dSLionel Sambuc {
398*0a6a1f1dSLionel Sambuc if (ptr == NULL)
399*0a6a1f1dSLionel Sambuc memory_exhausted(strerror(errno));
4002fe8fb19SBen Gras return ptr;
4012fe8fb19SBen Gras }
4022fe8fb19SBen Gras
403*0a6a1f1dSLionel Sambuc static void *
zic_malloc(size_t size)404*0a6a1f1dSLionel Sambuc zic_malloc(size_t size)
405*0a6a1f1dSLionel Sambuc {
406*0a6a1f1dSLionel Sambuc return memcheck(malloc(size));
407*0a6a1f1dSLionel Sambuc }
408*0a6a1f1dSLionel Sambuc
409*0a6a1f1dSLionel Sambuc static void *
zic_realloc(void * ptr,size_t size)410*0a6a1f1dSLionel Sambuc zic_realloc(void *ptr, size_t size)
411*0a6a1f1dSLionel Sambuc {
412*0a6a1f1dSLionel Sambuc return memcheck(realloc(ptr, size));
413*0a6a1f1dSLionel Sambuc }
414*0a6a1f1dSLionel Sambuc
415*0a6a1f1dSLionel Sambuc static char *
ecpyalloc(char const * str)416*0a6a1f1dSLionel Sambuc ecpyalloc(char const *str)
417*0a6a1f1dSLionel Sambuc {
418*0a6a1f1dSLionel Sambuc return memcheck(strdup(str));
419*0a6a1f1dSLionel Sambuc }
420*0a6a1f1dSLionel Sambuc
421*0a6a1f1dSLionel Sambuc static void *
growalloc(void * ptr,size_t itemsize,int nitems,int * nitems_alloc)422*0a6a1f1dSLionel Sambuc growalloc(void *ptr, size_t itemsize, int nitems, int *nitems_alloc)
423*0a6a1f1dSLionel Sambuc {
424*0a6a1f1dSLionel Sambuc if (nitems < *nitems_alloc)
425*0a6a1f1dSLionel Sambuc return ptr;
426*0a6a1f1dSLionel Sambuc else {
427*0a6a1f1dSLionel Sambuc int amax = INT_MAX < SIZE_MAX ? INT_MAX : SIZE_MAX;
428*0a6a1f1dSLionel Sambuc if ((amax - 1) / 3 * 2 < *nitems_alloc)
429*0a6a1f1dSLionel Sambuc memory_exhausted(_("int overflow"));
430*0a6a1f1dSLionel Sambuc *nitems_alloc = *nitems_alloc + (*nitems_alloc >> 1) + 1;
431*0a6a1f1dSLionel Sambuc return zic_realloc(ptr, size_product(*nitems_alloc, itemsize));
432*0a6a1f1dSLionel Sambuc }
433*0a6a1f1dSLionel Sambuc }
4342fe8fb19SBen Gras
4352fe8fb19SBen Gras /*
4362fe8fb19SBen Gras ** Error handling.
4372fe8fb19SBen Gras */
4382fe8fb19SBen Gras
4392fe8fb19SBen Gras static void
eats(const char * const name,const int num,const char * const rname,const int rnum)44084d9c625SLionel Sambuc eats(const char *const name, const int num, const char *const rname,
44184d9c625SLionel Sambuc const int rnum)
4422fe8fb19SBen Gras {
4432fe8fb19SBen Gras filename = name;
4442fe8fb19SBen Gras linenum = num;
4452fe8fb19SBen Gras rfilename = rname;
4462fe8fb19SBen Gras rlinenum = rnum;
4472fe8fb19SBen Gras }
4482fe8fb19SBen Gras
4492fe8fb19SBen Gras static void
eat(const char * const name,const int num)45084d9c625SLionel Sambuc eat(const char *const name, const int num)
4512fe8fb19SBen Gras {
45284d9c625SLionel Sambuc eats(name, num, NULL, -1);
4532fe8fb19SBen Gras }
4542fe8fb19SBen Gras
45584d9c625SLionel Sambuc static void ATTRIBUTE_FORMAT((printf, 1, 0))
verror(const char * const string,va_list args)45684d9c625SLionel Sambuc verror(const char *const string, va_list args)
4572fe8fb19SBen Gras {
4582fe8fb19SBen Gras /*
4592fe8fb19SBen Gras ** Match the format of "cc" to allow sh users to
4602fe8fb19SBen Gras ** zic ... 2>&1 | error -t "*" -v
4612fe8fb19SBen Gras ** on BSD systems.
4622fe8fb19SBen Gras */
463*0a6a1f1dSLionel Sambuc if (filename)
46484d9c625SLionel Sambuc fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
46584d9c625SLionel Sambuc vfprintf(stderr, string, args);
4662fe8fb19SBen Gras if (rfilename != NULL)
467*0a6a1f1dSLionel Sambuc fprintf(stderr, _(" (rule from \"%s\", line %d)"),
4682fe8fb19SBen Gras rfilename, rlinenum);
469*0a6a1f1dSLionel Sambuc fprintf(stderr, "\n");
4702fe8fb19SBen Gras }
4712fe8fb19SBen Gras
47284d9c625SLionel Sambuc static void ATTRIBUTE_FORMAT((printf, 1, 2))
error(const char * const string,...)47384d9c625SLionel Sambuc error(const char *const string, ...)
4742fe8fb19SBen Gras {
47584d9c625SLionel Sambuc va_list args;
47684d9c625SLionel Sambuc va_start(args, string);
47784d9c625SLionel Sambuc verror(string, args);
47884d9c625SLionel Sambuc va_end(args);
479*0a6a1f1dSLionel Sambuc errors = true;
48084d9c625SLionel Sambuc }
4812fe8fb19SBen Gras
48284d9c625SLionel Sambuc static void ATTRIBUTE_FORMAT((printf, 1, 2))
warning(const char * const string,...)48384d9c625SLionel Sambuc warning(const char *const string, ...)
48484d9c625SLionel Sambuc {
48584d9c625SLionel Sambuc va_list args;
48684d9c625SLionel Sambuc fprintf(stderr, _("warning: "));
48784d9c625SLionel Sambuc va_start(args, string);
48884d9c625SLionel Sambuc verror(string, args);
48984d9c625SLionel Sambuc va_end(args);
490*0a6a1f1dSLionel Sambuc warnings = true;
491*0a6a1f1dSLionel Sambuc }
492*0a6a1f1dSLionel Sambuc
493*0a6a1f1dSLionel Sambuc static void
close_file(FILE * stream,char const * name)494*0a6a1f1dSLionel Sambuc close_file(FILE *stream, char const *name)
495*0a6a1f1dSLionel Sambuc {
496*0a6a1f1dSLionel Sambuc char const *e = (ferror(stream) ? _("I/O error")
497*0a6a1f1dSLionel Sambuc : fclose(stream) != 0 ? strerror(errno) : NULL);
498*0a6a1f1dSLionel Sambuc if (e) {
499*0a6a1f1dSLionel Sambuc fprintf(stderr, "%s: ", progname);
500*0a6a1f1dSLionel Sambuc if (name)
501*0a6a1f1dSLionel Sambuc fprintf(stderr, "%s: ", name);
502*0a6a1f1dSLionel Sambuc fprintf(stderr, "%s\n", e);
503*0a6a1f1dSLionel Sambuc exit(EXIT_FAILURE);
504*0a6a1f1dSLionel Sambuc }
5052fe8fb19SBen Gras }
5062fe8fb19SBen Gras
50784d9c625SLionel Sambuc static _Noreturn void
usage(FILE * stream,int status)5082fe8fb19SBen Gras usage(FILE *stream, int status)
5092fe8fb19SBen Gras {
510*0a6a1f1dSLionel Sambuc fprintf(stream,
511*0a6a1f1dSLionel Sambuc _("%s: usage is %s [ --version ] [ --help ] [ -v ] \\\n"
512*0a6a1f1dSLionel Sambuc "\t[ -l localtime ] [ -p posixrules ] [ -d directory ] \\\n"
513*0a6a1f1dSLionel Sambuc "\t[ -L leapseconds ] [ filename ... ]\n\n"
514*0a6a1f1dSLionel Sambuc "Report bugs to %s.\n"),
51584d9c625SLionel Sambuc progname, progname, REPORT_BUGS_TO);
516*0a6a1f1dSLionel Sambuc if (status == EXIT_SUCCESS)
517*0a6a1f1dSLionel Sambuc close_file(stream, NULL);
5182fe8fb19SBen Gras exit(status);
5192fe8fb19SBen Gras }
5202fe8fb19SBen Gras
5212fe8fb19SBen Gras static const char * psxrules;
5222fe8fb19SBen Gras static const char * lcltime;
5232fe8fb19SBen Gras static const char * directory;
5242fe8fb19SBen Gras static const char * leapsec;
5252fe8fb19SBen Gras static const char * yitcommand;
5262fe8fb19SBen Gras
5272fe8fb19SBen Gras int
main(int argc,char * argv[])52884d9c625SLionel Sambuc main(int argc, char *argv[])
5292fe8fb19SBen Gras {
53084d9c625SLionel Sambuc int i;
53184d9c625SLionel Sambuc int j;
53284d9c625SLionel Sambuc int c;
5332fe8fb19SBen Gras
53484d9c625SLionel Sambuc #ifdef S_IWGRP
5352fe8fb19SBen Gras (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
53684d9c625SLionel Sambuc #endif
5372fe8fb19SBen Gras #if HAVE_GETTEXT - 0
5382fe8fb19SBen Gras (void) setlocale(LC_MESSAGES, "");
5392fe8fb19SBen Gras #ifdef TZ_DOMAINDIR
5402fe8fb19SBen Gras (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
5412fe8fb19SBen Gras #endif /* defined TEXTDOMAINDIR */
5422fe8fb19SBen Gras (void) textdomain(TZ_DOMAIN);
5432fe8fb19SBen Gras #endif /* HAVE_GETTEXT */
5442fe8fb19SBen Gras progname = argv[0];
5452fe8fb19SBen Gras if (TYPE_BIT(zic_t) < 64) {
5462fe8fb19SBen Gras (void) fprintf(stderr, "%s: %s\n", progname,
5472fe8fb19SBen Gras _("wild compilation-time specification of zic_t"));
548*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
5492fe8fb19SBen Gras }
5502fe8fb19SBen Gras for (i = 1; i < argc; ++i)
5512fe8fb19SBen Gras if (strcmp(argv[i], "--version") == 0) {
55284d9c625SLionel Sambuc (void) printf("zic %s%s\n", PKGVERSION, TZVERSION);
553*0a6a1f1dSLionel Sambuc close_file(stdout, NULL);
554*0a6a1f1dSLionel Sambuc return EXIT_SUCCESS;
5552fe8fb19SBen Gras } else if (strcmp(argv[i], "--help") == 0) {
5562fe8fb19SBen Gras usage(stdout, EXIT_SUCCESS);
5572fe8fb19SBen Gras }
5582fe8fb19SBen Gras while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
5592fe8fb19SBen Gras switch (c) {
5602fe8fb19SBen Gras default:
5612fe8fb19SBen Gras usage(stderr, EXIT_FAILURE);
5622fe8fb19SBen Gras case 'd':
5632fe8fb19SBen Gras if (directory == NULL)
5642fe8fb19SBen Gras directory = optarg;
5652fe8fb19SBen Gras else {
566*0a6a1f1dSLionel Sambuc fprintf(stderr,
5672fe8fb19SBen Gras _("%s: More than one -d option specified\n"),
5682fe8fb19SBen Gras progname);
569*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
5702fe8fb19SBen Gras }
5712fe8fb19SBen Gras break;
5722fe8fb19SBen Gras case 'l':
5732fe8fb19SBen Gras if (lcltime == NULL)
5742fe8fb19SBen Gras lcltime = optarg;
5752fe8fb19SBen Gras else {
576*0a6a1f1dSLionel Sambuc fprintf(stderr,
5772fe8fb19SBen Gras _("%s: More than one -l option specified\n"),
5782fe8fb19SBen Gras progname);
579*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
5802fe8fb19SBen Gras }
5812fe8fb19SBen Gras break;
5822fe8fb19SBen Gras case 'p':
5832fe8fb19SBen Gras if (psxrules == NULL)
5842fe8fb19SBen Gras psxrules = optarg;
5852fe8fb19SBen Gras else {
586*0a6a1f1dSLionel Sambuc fprintf(stderr,
5872fe8fb19SBen Gras _("%s: More than one -p option specified\n"),
5882fe8fb19SBen Gras progname);
589*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
5902fe8fb19SBen Gras }
5912fe8fb19SBen Gras break;
5922fe8fb19SBen Gras case 'y':
5932fe8fb19SBen Gras if (yitcommand == NULL)
5942fe8fb19SBen Gras yitcommand = optarg;
5952fe8fb19SBen Gras else {
596*0a6a1f1dSLionel Sambuc fprintf(stderr,
5972fe8fb19SBen Gras _("%s: More than one -y option specified\n"),
5982fe8fb19SBen Gras progname);
599*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
6002fe8fb19SBen Gras }
6012fe8fb19SBen Gras break;
6022fe8fb19SBen Gras case 'L':
6032fe8fb19SBen Gras if (leapsec == NULL)
6042fe8fb19SBen Gras leapsec = optarg;
6052fe8fb19SBen Gras else {
606*0a6a1f1dSLionel Sambuc fprintf(stderr,
6072fe8fb19SBen Gras _("%s: More than one -L option specified\n"),
6082fe8fb19SBen Gras progname);
609*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
6102fe8fb19SBen Gras }
6112fe8fb19SBen Gras break;
6122fe8fb19SBen Gras case 'v':
613*0a6a1f1dSLionel Sambuc noise = true;
6142fe8fb19SBen Gras break;
6152fe8fb19SBen Gras case 's':
616*0a6a1f1dSLionel Sambuc warning(_("-s ignored"));
6172fe8fb19SBen Gras break;
6182fe8fb19SBen Gras }
6192fe8fb19SBen Gras if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
6202fe8fb19SBen Gras usage(stderr, EXIT_FAILURE); /* usage message by request */
6212fe8fb19SBen Gras if (directory == NULL)
6222fe8fb19SBen Gras directory = TZDIR;
6232fe8fb19SBen Gras if (yitcommand == NULL)
6242fe8fb19SBen Gras yitcommand = "yearistype";
6252fe8fb19SBen Gras
6262fe8fb19SBen Gras if (optind < argc && leapsec != NULL) {
6272fe8fb19SBen Gras infile(leapsec);
6282fe8fb19SBen Gras adjleap();
6292fe8fb19SBen Gras }
6302fe8fb19SBen Gras
6312fe8fb19SBen Gras for (i = optind; i < argc; ++i)
6322fe8fb19SBen Gras infile(argv[i]);
6332fe8fb19SBen Gras if (errors)
634*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
6352fe8fb19SBen Gras associate();
6362fe8fb19SBen Gras for (i = 0; i < nzones; i = j) {
6372fe8fb19SBen Gras /*
6382fe8fb19SBen Gras ** Find the next non-continuation zone entry.
6392fe8fb19SBen Gras */
6402fe8fb19SBen Gras for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
6412fe8fb19SBen Gras continue;
6422fe8fb19SBen Gras outzone(&zones[i], j - i);
6432fe8fb19SBen Gras }
6442fe8fb19SBen Gras /*
6452fe8fb19SBen Gras ** Make links.
6462fe8fb19SBen Gras */
6472fe8fb19SBen Gras for (i = 0; i < nlinks; ++i) {
6482fe8fb19SBen Gras eat(links[i].l_filename, links[i].l_linenum);
6492fe8fb19SBen Gras dolink(links[i].l_from, links[i].l_to);
6502fe8fb19SBen Gras if (noise)
6512fe8fb19SBen Gras for (j = 0; j < nlinks; ++j)
6522fe8fb19SBen Gras if (strcmp(links[i].l_to,
6532fe8fb19SBen Gras links[j].l_from) == 0)
6542fe8fb19SBen Gras warning(_("link to link"));
6552fe8fb19SBen Gras }
6562fe8fb19SBen Gras if (lcltime != NULL) {
657*0a6a1f1dSLionel Sambuc eat(_("command line"), 1);
6582fe8fb19SBen Gras dolink(lcltime, TZDEFAULT);
6592fe8fb19SBen Gras }
6602fe8fb19SBen Gras if (psxrules != NULL) {
661*0a6a1f1dSLionel Sambuc eat(_("command line"), 1);
6622fe8fb19SBen Gras dolink(psxrules, TZDEFRULES);
6632fe8fb19SBen Gras }
664*0a6a1f1dSLionel Sambuc if (warnings && (ferror(stderr) || fclose(stderr) != 0))
665*0a6a1f1dSLionel Sambuc return EXIT_FAILURE;
666*0a6a1f1dSLionel Sambuc return errors ? EXIT_FAILURE : EXIT_SUCCESS;
667*0a6a1f1dSLionel Sambuc }
668*0a6a1f1dSLionel Sambuc
669*0a6a1f1dSLionel Sambuc static bool
componentcheck(char const * name,char const * component,char const * component_end)670*0a6a1f1dSLionel Sambuc componentcheck(char const *name, char const *component,
671*0a6a1f1dSLionel Sambuc char const *component_end)
672*0a6a1f1dSLionel Sambuc {
673*0a6a1f1dSLionel Sambuc enum { component_len_max = 14 };
674*0a6a1f1dSLionel Sambuc size_t component_len = component_end - component;
675*0a6a1f1dSLionel Sambuc if (component_len == 0) {
676*0a6a1f1dSLionel Sambuc if (!*name)
677*0a6a1f1dSLionel Sambuc error (_("empty file name"));
678*0a6a1f1dSLionel Sambuc else
679*0a6a1f1dSLionel Sambuc error (_(component == name
680*0a6a1f1dSLionel Sambuc ? "file name '%s' begins with '/'"
681*0a6a1f1dSLionel Sambuc : *component_end
682*0a6a1f1dSLionel Sambuc ? "file name '%s' contains '//'"
683*0a6a1f1dSLionel Sambuc : "file name '%s' ends with '/'"),
684*0a6a1f1dSLionel Sambuc name);
685*0a6a1f1dSLionel Sambuc return false;
686*0a6a1f1dSLionel Sambuc }
687*0a6a1f1dSLionel Sambuc if (0 < component_len && component_len <= 2
688*0a6a1f1dSLionel Sambuc && component[0] == '.' && component_end[-1] == '.') {
689*0a6a1f1dSLionel Sambuc error(_("file name '%s' contains '%.*s' component"),
690*0a6a1f1dSLionel Sambuc name, (int) component_len, component);
691*0a6a1f1dSLionel Sambuc return false;
692*0a6a1f1dSLionel Sambuc }
693*0a6a1f1dSLionel Sambuc if (noise) {
694*0a6a1f1dSLionel Sambuc if (0 < component_len && component[0] == '-')
695*0a6a1f1dSLionel Sambuc warning(_("file name '%s' component contains leading '-'"),
696*0a6a1f1dSLionel Sambuc name);
697*0a6a1f1dSLionel Sambuc if (component_len_max < component_len)
698*0a6a1f1dSLionel Sambuc warning(_("file name '%s' contains overlength component"
699*0a6a1f1dSLionel Sambuc " '%.*s...'"),
700*0a6a1f1dSLionel Sambuc name, component_len_max, component);
701*0a6a1f1dSLionel Sambuc }
702*0a6a1f1dSLionel Sambuc return true;
703*0a6a1f1dSLionel Sambuc }
704*0a6a1f1dSLionel Sambuc
705*0a6a1f1dSLionel Sambuc static bool
namecheck(const char * name)706*0a6a1f1dSLionel Sambuc namecheck(const char *name)
707*0a6a1f1dSLionel Sambuc {
708*0a6a1f1dSLionel Sambuc char const *cp;
709*0a6a1f1dSLionel Sambuc
710*0a6a1f1dSLionel Sambuc /* Benign characters in a portable file name. */
711*0a6a1f1dSLionel Sambuc static char const benign[] =
712*0a6a1f1dSLionel Sambuc "-/_"
713*0a6a1f1dSLionel Sambuc "abcdefghijklmnopqrstuvwxyz"
714*0a6a1f1dSLionel Sambuc "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
715*0a6a1f1dSLionel Sambuc
716*0a6a1f1dSLionel Sambuc /* Non-control chars in the POSIX portable character set,
717*0a6a1f1dSLionel Sambuc excluding the benign characters. */
718*0a6a1f1dSLionel Sambuc static char const printable_and_not_benign[] =
719*0a6a1f1dSLionel Sambuc " !\"#$%&'()*+,.0123456789:;<=>?@[\\]^`{|}~";
720*0a6a1f1dSLionel Sambuc
721*0a6a1f1dSLionel Sambuc char const *component = name;
722*0a6a1f1dSLionel Sambuc for (cp = name; *cp; cp++) {
723*0a6a1f1dSLionel Sambuc unsigned char c = *cp;
724*0a6a1f1dSLionel Sambuc if (noise && !strchr(benign, c)) {
725*0a6a1f1dSLionel Sambuc warning((strchr(printable_and_not_benign, c)
726*0a6a1f1dSLionel Sambuc ? _("file name '%s' contains byte '%c'")
727*0a6a1f1dSLionel Sambuc : _("file name '%s' contains byte '\\%o'")),
728*0a6a1f1dSLionel Sambuc name, c);
729*0a6a1f1dSLionel Sambuc }
730*0a6a1f1dSLionel Sambuc if (c == '/') {
731*0a6a1f1dSLionel Sambuc if (!componentcheck(name, component, cp))
732*0a6a1f1dSLionel Sambuc return false;
733*0a6a1f1dSLionel Sambuc component = cp + 1;
734*0a6a1f1dSLionel Sambuc }
735*0a6a1f1dSLionel Sambuc }
736*0a6a1f1dSLionel Sambuc return componentcheck(name, component, cp);
737*0a6a1f1dSLionel Sambuc }
738*0a6a1f1dSLionel Sambuc
739*0a6a1f1dSLionel Sambuc static char *
relname(char const * dir,char const * base)740*0a6a1f1dSLionel Sambuc relname(char const *dir, char const *base)
741*0a6a1f1dSLionel Sambuc {
742*0a6a1f1dSLionel Sambuc if (*base == '/')
743*0a6a1f1dSLionel Sambuc return ecpyalloc(base);
744*0a6a1f1dSLionel Sambuc else {
745*0a6a1f1dSLionel Sambuc size_t dir_len = strlen(dir);
746*0a6a1f1dSLionel Sambuc bool needs_slash = dir_len && dir[dir_len - 1] != '/';
747*0a6a1f1dSLionel Sambuc char *result = zic_malloc(dir_len + needs_slash + strlen(base) + 1);
748*0a6a1f1dSLionel Sambuc result[dir_len] = '/';
749*0a6a1f1dSLionel Sambuc strcpy(result + dir_len + needs_slash, base);
750*0a6a1f1dSLionel Sambuc return memcpy(result, dir, dir_len);
751*0a6a1f1dSLionel Sambuc }
7522fe8fb19SBen Gras }
7532fe8fb19SBen Gras
7542fe8fb19SBen Gras static void
dolink(char const * fromfield,char const * tofield)755*0a6a1f1dSLionel Sambuc dolink(char const *fromfield, char const *tofield)
7562fe8fb19SBen Gras {
75784d9c625SLionel Sambuc char * fromname;
75884d9c625SLionel Sambuc char * toname;
759*0a6a1f1dSLionel Sambuc int fromisdir;
7602fe8fb19SBen Gras
761*0a6a1f1dSLionel Sambuc fromname = relname(directory, fromfield);
762*0a6a1f1dSLionel Sambuc toname = relname(directory, tofield);
7632fe8fb19SBen Gras /*
7642fe8fb19SBen Gras ** We get to be careful here since
7652fe8fb19SBen Gras ** there's a fair chance of root running us.
7662fe8fb19SBen Gras */
767*0a6a1f1dSLionel Sambuc fromisdir = itsdir(fromname);
768*0a6a1f1dSLionel Sambuc if (fromisdir) {
769*0a6a1f1dSLionel Sambuc char const *e = strerror(fromisdir < 0 ? errno : EPERM);
770*0a6a1f1dSLionel Sambuc fprintf(stderr, _("%s: link from %s failed: %s"),
771*0a6a1f1dSLionel Sambuc progname, fromname, e);
772*0a6a1f1dSLionel Sambuc exit(EXIT_FAILURE);
773*0a6a1f1dSLionel Sambuc }
774*0a6a1f1dSLionel Sambuc if (itsdir(toname) <= 0)
775*0a6a1f1dSLionel Sambuc remove(toname);
7762fe8fb19SBen Gras if (link(fromname, toname) != 0) {
7772fe8fb19SBen Gras int result;
7782fe8fb19SBen Gras
779*0a6a1f1dSLionel Sambuc if (! mkdirs(toname))
7802fe8fb19SBen Gras exit(EXIT_FAILURE);
7812fe8fb19SBen Gras
7822fe8fb19SBen Gras result = link(fromname, toname);
783*0a6a1f1dSLionel Sambuc if (result != 0) {
784*0a6a1f1dSLionel Sambuc const char *s = fromfield;
785*0a6a1f1dSLionel Sambuc const char *t;
786*0a6a1f1dSLionel Sambuc char *p;
787*0a6a1f1dSLionel Sambuc size_t dotdots = 0;
78884d9c625SLionel Sambuc char * symlinkcontents = NULL;
7892fe8fb19SBen Gras
790*0a6a1f1dSLionel Sambuc do
791*0a6a1f1dSLionel Sambuc t = s;
792*0a6a1f1dSLionel Sambuc while ((s = strchr(s, '/'))
793*0a6a1f1dSLionel Sambuc && ! strncmp (fromfield, tofield,
794*0a6a1f1dSLionel Sambuc ++s - fromfield));
795*0a6a1f1dSLionel Sambuc
796*0a6a1f1dSLionel Sambuc for (s = tofield + (t - fromfield); *s; s++)
797*0a6a1f1dSLionel Sambuc dotdots += *s == '/';
798*0a6a1f1dSLionel Sambuc symlinkcontents
799*0a6a1f1dSLionel Sambuc = zic_malloc(3 * dotdots + strlen(t) + 1);
800*0a6a1f1dSLionel Sambuc for (p = symlinkcontents; dotdots-- != 0; p += 3)
801*0a6a1f1dSLionel Sambuc memcpy(p, "../", 3);
802*0a6a1f1dSLionel Sambuc strcpy(p, t);
803*0a6a1f1dSLionel Sambuc result = symlink(symlinkcontents, toname);
8042fe8fb19SBen Gras if (result == 0)
8052fe8fb19SBen Gras warning(_("hard link failed, symbolic link used"));
80684d9c625SLionel Sambuc free(symlinkcontents);
8072fe8fb19SBen Gras }
8082fe8fb19SBen Gras if (result != 0) {
809*0a6a1f1dSLionel Sambuc FILE *fp, *tp;
810*0a6a1f1dSLionel Sambuc int c;
811*0a6a1f1dSLionel Sambuc fp = fopen(fromname, "rb");
812*0a6a1f1dSLionel Sambuc if (!fp) {
8132fe8fb19SBen Gras const char *e = strerror(errno);
814*0a6a1f1dSLionel Sambuc fprintf(stderr,
815*0a6a1f1dSLionel Sambuc _("%s: Can't read %s: %s\n"),
816*0a6a1f1dSLionel Sambuc progname, fromname, e);
8172fe8fb19SBen Gras exit(EXIT_FAILURE);
8182fe8fb19SBen Gras }
819*0a6a1f1dSLionel Sambuc tp = fopen(toname, "wb");
820*0a6a1f1dSLionel Sambuc if (!tp) {
821*0a6a1f1dSLionel Sambuc const char *e = strerror(errno);
822*0a6a1f1dSLionel Sambuc fprintf(stderr,
823*0a6a1f1dSLionel Sambuc _("%s: Can't create %s: %s\n"),
824*0a6a1f1dSLionel Sambuc progname, toname, e);
825*0a6a1f1dSLionel Sambuc exit(EXIT_FAILURE);
826*0a6a1f1dSLionel Sambuc }
827*0a6a1f1dSLionel Sambuc while ((c = getc(fp)) != EOF)
828*0a6a1f1dSLionel Sambuc putc(c, tp);
829*0a6a1f1dSLionel Sambuc close_file(fp, fromname);
830*0a6a1f1dSLionel Sambuc close_file(tp, toname);
831*0a6a1f1dSLionel Sambuc warning(_("link failed, copy used"));
832*0a6a1f1dSLionel Sambuc }
8332fe8fb19SBen Gras }
83484d9c625SLionel Sambuc free(fromname);
83584d9c625SLionel Sambuc free(toname);
8362fe8fb19SBen Gras }
8372fe8fb19SBen Gras
8382fe8fb19SBen Gras #define TIME_T_BITS_IN_FILE 64
8392fe8fb19SBen Gras
840*0a6a1f1dSLionel Sambuc static zic_t const min_time = MINVAL (zic_t, TIME_T_BITS_IN_FILE);
841*0a6a1f1dSLionel Sambuc static zic_t const max_time = MAXVAL (zic_t, TIME_T_BITS_IN_FILE);
8422fe8fb19SBen Gras
843*0a6a1f1dSLionel Sambuc /* Estimated time of the Big Bang, in seconds since the POSIX epoch.
844*0a6a1f1dSLionel Sambuc rounded downward to the negation of a power of two that is
845*0a6a1f1dSLionel Sambuc comfortably outside the error bounds.
846*0a6a1f1dSLionel Sambuc
847*0a6a1f1dSLionel Sambuc zic does not output time stamps before this, partly because they
848*0a6a1f1dSLionel Sambuc are physically suspect, and partly because GNOME mishandles them; see
849*0a6a1f1dSLionel Sambuc GNOME bug 730332 <https://bugzilla.gnome.org/show_bug.cgi?id=730332>.
850*0a6a1f1dSLionel Sambuc
851*0a6a1f1dSLionel Sambuc For the time of the Big Bang, see:
852*0a6a1f1dSLionel Sambuc
853*0a6a1f1dSLionel Sambuc Ade PAR, Aghanim N, Armitage-Caplan C et al. Planck 2013 results.
854*0a6a1f1dSLionel Sambuc I. Overview of products and scientific results.
855*0a6a1f1dSLionel Sambuc arXiv:1303.5062 2013-03-20 20:10:01 UTC
856*0a6a1f1dSLionel Sambuc <http://arxiv.org/pdf/1303.5062v1> [PDF]
857*0a6a1f1dSLionel Sambuc
858*0a6a1f1dSLionel Sambuc Page 36, Table 9, row Age/Gyr, column Planck+WP+highL+BAO 68% limits
859*0a6a1f1dSLionel Sambuc gives the value 13.798 plus-or-minus 0.037 billion years.
860*0a6a1f1dSLionel Sambuc Multiplying this by 1000000000 and then by 31557600 (the number of
861*0a6a1f1dSLionel Sambuc seconds in an astronomical year) gives a value that is comfortably
862*0a6a1f1dSLionel Sambuc less than 2**59, so BIG_BANG is - 2**59.
863*0a6a1f1dSLionel Sambuc
864*0a6a1f1dSLionel Sambuc BIG_BANG is approximate, and may change in future versions.
865*0a6a1f1dSLionel Sambuc Please do not rely on its exact value. */
866*0a6a1f1dSLionel Sambuc
867*0a6a1f1dSLionel Sambuc #ifndef BIG_BANG
868*0a6a1f1dSLionel Sambuc #define BIG_BANG (- (1LL << 59))
869*0a6a1f1dSLionel Sambuc #endif
870*0a6a1f1dSLionel Sambuc
871*0a6a1f1dSLionel Sambuc static const zic_t big_bang_time = BIG_BANG;
872*0a6a1f1dSLionel Sambuc
873*0a6a1f1dSLionel Sambuc /* Return 1 if NAME is a directory, 0 if it's something else, -1 if trouble. */
8742fe8fb19SBen Gras static int
itsdir(char const * name)875*0a6a1f1dSLionel Sambuc itsdir(char const *name)
8762fe8fb19SBen Gras {
877*0a6a1f1dSLionel Sambuc struct stat st;
878*0a6a1f1dSLionel Sambuc int res = stat(name, &st);
879*0a6a1f1dSLionel Sambuc if (res != 0)
880*0a6a1f1dSLionel Sambuc return res;
881*0a6a1f1dSLionel Sambuc #ifdef S_ISDIR
882*0a6a1f1dSLionel Sambuc return S_ISDIR(st.st_mode) != 0;
883*0a6a1f1dSLionel Sambuc #else
884*0a6a1f1dSLionel Sambuc {
885*0a6a1f1dSLionel Sambuc char *nameslashdot = relname(name, ".");
886*0a6a1f1dSLionel Sambuc res = stat(nameslashdot, &st);
887*0a6a1f1dSLionel Sambuc free(nameslashdot);
888*0a6a1f1dSLionel Sambuc return res == 0;
889*0a6a1f1dSLionel Sambuc }
890*0a6a1f1dSLionel Sambuc #endif
8912fe8fb19SBen Gras }
8922fe8fb19SBen Gras
8932fe8fb19SBen Gras /*
8942fe8fb19SBen Gras ** Associate sets of rules with zones.
8952fe8fb19SBen Gras */
8962fe8fb19SBen Gras
8972fe8fb19SBen Gras /*
8982fe8fb19SBen Gras ** Sort by rule name.
8992fe8fb19SBen Gras */
9002fe8fb19SBen Gras
9012fe8fb19SBen Gras static int
rcomp(const void * cp1,const void * cp2)90284d9c625SLionel Sambuc rcomp(const void *cp1, const void *cp2)
9032fe8fb19SBen Gras {
9042fe8fb19SBen Gras return strcmp(((const struct rule *) cp1)->r_name,
9052fe8fb19SBen Gras ((const struct rule *) cp2)->r_name);
9062fe8fb19SBen Gras }
9072fe8fb19SBen Gras
9082fe8fb19SBen Gras static void
associate(void)9092fe8fb19SBen Gras associate(void)
9102fe8fb19SBen Gras {
91184d9c625SLionel Sambuc struct zone * zp;
91284d9c625SLionel Sambuc struct rule * rp;
91384d9c625SLionel Sambuc int base, out;
91484d9c625SLionel Sambuc int i, j;
9152fe8fb19SBen Gras
9162fe8fb19SBen Gras if (nrules != 0) {
91784d9c625SLionel Sambuc (void) qsort(rules, (size_t)nrules, sizeof *rules, rcomp);
9182fe8fb19SBen Gras for (i = 0; i < nrules - 1; ++i) {
9192fe8fb19SBen Gras if (strcmp(rules[i].r_name,
9202fe8fb19SBen Gras rules[i + 1].r_name) != 0)
9212fe8fb19SBen Gras continue;
9222fe8fb19SBen Gras if (strcmp(rules[i].r_filename,
9232fe8fb19SBen Gras rules[i + 1].r_filename) == 0)
9242fe8fb19SBen Gras continue;
9252fe8fb19SBen Gras eat(rules[i].r_filename, rules[i].r_linenum);
9262fe8fb19SBen Gras warning(_("same rule name in multiple files"));
9272fe8fb19SBen Gras eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
9282fe8fb19SBen Gras warning(_("same rule name in multiple files"));
9292fe8fb19SBen Gras for (j = i + 2; j < nrules; ++j) {
9302fe8fb19SBen Gras if (strcmp(rules[i].r_name,
9312fe8fb19SBen Gras rules[j].r_name) != 0)
9322fe8fb19SBen Gras break;
9332fe8fb19SBen Gras if (strcmp(rules[i].r_filename,
9342fe8fb19SBen Gras rules[j].r_filename) == 0)
9352fe8fb19SBen Gras continue;
9362fe8fb19SBen Gras if (strcmp(rules[i + 1].r_filename,
9372fe8fb19SBen Gras rules[j].r_filename) == 0)
9382fe8fb19SBen Gras continue;
9392fe8fb19SBen Gras break;
9402fe8fb19SBen Gras }
9412fe8fb19SBen Gras i = j - 1;
9422fe8fb19SBen Gras }
9432fe8fb19SBen Gras }
9442fe8fb19SBen Gras for (i = 0; i < nzones; ++i) {
9452fe8fb19SBen Gras zp = &zones[i];
9462fe8fb19SBen Gras zp->z_rules = NULL;
9472fe8fb19SBen Gras zp->z_nrules = 0;
9482fe8fb19SBen Gras }
9492fe8fb19SBen Gras for (base = 0; base < nrules; base = out) {
9502fe8fb19SBen Gras rp = &rules[base];
9512fe8fb19SBen Gras for (out = base + 1; out < nrules; ++out)
9522fe8fb19SBen Gras if (strcmp(rp->r_name, rules[out].r_name) != 0)
9532fe8fb19SBen Gras break;
9542fe8fb19SBen Gras for (i = 0; i < nzones; ++i) {
9552fe8fb19SBen Gras zp = &zones[i];
9562fe8fb19SBen Gras if (strcmp(zp->z_rule, rp->r_name) != 0)
9572fe8fb19SBen Gras continue;
9582fe8fb19SBen Gras zp->z_rules = rp;
9592fe8fb19SBen Gras zp->z_nrules = out - base;
9602fe8fb19SBen Gras }
9612fe8fb19SBen Gras }
9622fe8fb19SBen Gras for (i = 0; i < nzones; ++i) {
9632fe8fb19SBen Gras zp = &zones[i];
9642fe8fb19SBen Gras if (zp->z_nrules == 0) {
9652fe8fb19SBen Gras /*
9662fe8fb19SBen Gras ** Maybe we have a local standard time offset.
9672fe8fb19SBen Gras */
9682fe8fb19SBen Gras eat(zp->z_filename, zp->z_linenum);
9692fe8fb19SBen Gras zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
970*0a6a1f1dSLionel Sambuc true);
9712fe8fb19SBen Gras /*
9722fe8fb19SBen Gras ** Note, though, that if there's no rule,
9732fe8fb19SBen Gras ** a '%s' in the format is a bad thing.
9742fe8fb19SBen Gras */
975*0a6a1f1dSLionel Sambuc if (zp->z_format_specifier == 's')
97684d9c625SLionel Sambuc error("%s", _("%s in ruleless zone"));
9772fe8fb19SBen Gras }
9782fe8fb19SBen Gras }
9792fe8fb19SBen Gras if (errors)
9802fe8fb19SBen Gras exit(EXIT_FAILURE);
9812fe8fb19SBen Gras }
9822fe8fb19SBen Gras
9832fe8fb19SBen Gras static void
infile(const char * name)98484d9c625SLionel Sambuc infile(const char *name)
9852fe8fb19SBen Gras {
98684d9c625SLionel Sambuc FILE * fp;
98784d9c625SLionel Sambuc char ** fields;
98884d9c625SLionel Sambuc char * cp;
98984d9c625SLionel Sambuc const struct lookup * lp;
99084d9c625SLionel Sambuc int nfields;
991*0a6a1f1dSLionel Sambuc bool wantcont;
99284d9c625SLionel Sambuc int num;
9932fe8fb19SBen Gras char buf[BUFSIZ];
9942fe8fb19SBen Gras
9952fe8fb19SBen Gras if (strcmp(name, "-") == 0) {
9962fe8fb19SBen Gras name = _("standard input");
9972fe8fb19SBen Gras fp = stdin;
9982fe8fb19SBen Gras } else if ((fp = fopen(name, "r")) == NULL) {
9992fe8fb19SBen Gras const char *e = strerror(errno);
10002fe8fb19SBen Gras
1001*0a6a1f1dSLionel Sambuc fprintf(stderr, _("%s: Can't open %s: %s\n"),
10022fe8fb19SBen Gras progname, name, e);
10032fe8fb19SBen Gras exit(EXIT_FAILURE);
10042fe8fb19SBen Gras }
1005*0a6a1f1dSLionel Sambuc wantcont = false;
10062fe8fb19SBen Gras for (num = 1; ; ++num) {
10072fe8fb19SBen Gras eat(name, num);
10082fe8fb19SBen Gras if (fgets(buf, (int) sizeof buf, fp) != buf)
10092fe8fb19SBen Gras break;
10102fe8fb19SBen Gras cp = strchr(buf, '\n');
10112fe8fb19SBen Gras if (cp == NULL) {
10122fe8fb19SBen Gras error(_("line too long"));
10132fe8fb19SBen Gras exit(EXIT_FAILURE);
10142fe8fb19SBen Gras }
10152fe8fb19SBen Gras *cp = '\0';
10162fe8fb19SBen Gras fields = getfields(buf);
10172fe8fb19SBen Gras nfields = 0;
10182fe8fb19SBen Gras while (fields[nfields] != NULL) {
10192fe8fb19SBen Gras static char nada;
10202fe8fb19SBen Gras
10212fe8fb19SBen Gras if (strcmp(fields[nfields], "-") == 0)
10222fe8fb19SBen Gras fields[nfields] = &nada;
10232fe8fb19SBen Gras ++nfields;
10242fe8fb19SBen Gras }
10252fe8fb19SBen Gras if (nfields == 0) {
10262fe8fb19SBen Gras /* nothing to do */
10272fe8fb19SBen Gras } else if (wantcont) {
10282fe8fb19SBen Gras wantcont = inzcont(fields, nfields);
10292fe8fb19SBen Gras } else {
10302fe8fb19SBen Gras lp = byword(fields[0], line_codes);
10312fe8fb19SBen Gras if (lp == NULL)
10322fe8fb19SBen Gras error(_("input line of unknown type"));
10332fe8fb19SBen Gras else switch ((int) (lp->l_value)) {
10342fe8fb19SBen Gras case LC_RULE:
10352fe8fb19SBen Gras inrule(fields, nfields);
1036*0a6a1f1dSLionel Sambuc wantcont = false;
10372fe8fb19SBen Gras break;
10382fe8fb19SBen Gras case LC_ZONE:
10392fe8fb19SBen Gras wantcont = inzone(fields, nfields);
10402fe8fb19SBen Gras break;
10412fe8fb19SBen Gras case LC_LINK:
10422fe8fb19SBen Gras inlink(fields, nfields);
1043*0a6a1f1dSLionel Sambuc wantcont = false;
10442fe8fb19SBen Gras break;
10452fe8fb19SBen Gras case LC_LEAP:
10462fe8fb19SBen Gras if (name != leapsec)
1047*0a6a1f1dSLionel Sambuc warning(_("%s: Leap line in non leap"
1048*0a6a1f1dSLionel Sambuc " seconds file %s"),
10492fe8fb19SBen Gras progname, name);
10502fe8fb19SBen Gras else inleap(fields, nfields);
1051*0a6a1f1dSLionel Sambuc wantcont = false;
10522fe8fb19SBen Gras break;
10532fe8fb19SBen Gras default: /* "cannot happen" */
1054*0a6a1f1dSLionel Sambuc fprintf(stderr,
10552fe8fb19SBen Gras _("%s: panic: Invalid l_value %d\n"),
10562fe8fb19SBen Gras progname, lp->l_value);
10572fe8fb19SBen Gras exit(EXIT_FAILURE);
10582fe8fb19SBen Gras }
10592fe8fb19SBen Gras }
106084d9c625SLionel Sambuc free(fields);
10612fe8fb19SBen Gras }
1062*0a6a1f1dSLionel Sambuc close_file(fp, filename);
10632fe8fb19SBen Gras if (wantcont)
10642fe8fb19SBen Gras error(_("expected continuation line not found"));
10652fe8fb19SBen Gras }
10662fe8fb19SBen Gras
10672fe8fb19SBen Gras /*
10682fe8fb19SBen Gras ** Convert a string of one of the forms
10692fe8fb19SBen Gras ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
10702fe8fb19SBen Gras ** into a number of seconds.
10712fe8fb19SBen Gras ** A null string maps to zero.
10722fe8fb19SBen Gras ** Call error with errstring and return zero on errors.
10732fe8fb19SBen Gras */
10742fe8fb19SBen Gras
107584d9c625SLionel Sambuc static zic_t
gethms(char const * string,char const * errstring,bool signable)1076*0a6a1f1dSLionel Sambuc gethms(char const *string, char const *errstring, bool signable)
10772fe8fb19SBen Gras {
107884d9c625SLionel Sambuc zic_t hh;
10792fe8fb19SBen Gras int mm, ss, sign;
1080*0a6a1f1dSLionel Sambuc char xs;
10812fe8fb19SBen Gras
10822fe8fb19SBen Gras if (string == NULL || *string == '\0')
10832fe8fb19SBen Gras return 0;
10842fe8fb19SBen Gras if (!signable)
10852fe8fb19SBen Gras sign = 1;
10862fe8fb19SBen Gras else if (*string == '-') {
10872fe8fb19SBen Gras sign = -1;
10882fe8fb19SBen Gras ++string;
10892fe8fb19SBen Gras } else sign = 1;
1090*0a6a1f1dSLionel Sambuc if (sscanf(string, "%"SCNdZIC"%c", &hh, &xs) == 1)
10912fe8fb19SBen Gras mm = ss = 0;
1092*0a6a1f1dSLionel Sambuc else if (sscanf(string, "%"SCNdZIC":%d%c", &hh, &mm, &xs) == 2)
10932fe8fb19SBen Gras ss = 0;
1094*0a6a1f1dSLionel Sambuc else if (sscanf(string, "%"SCNdZIC":%d:%d%c", &hh, &mm, &ss, &xs)
1095*0a6a1f1dSLionel Sambuc != 3) {
109684d9c625SLionel Sambuc error("%s", errstring);
10972fe8fb19SBen Gras return 0;
10982fe8fb19SBen Gras }
10992fe8fb19SBen Gras if (hh < 0 ||
11002fe8fb19SBen Gras mm < 0 || mm >= MINSPERHOUR ||
11012fe8fb19SBen Gras ss < 0 || ss > SECSPERMIN) {
110284d9c625SLionel Sambuc error("%s", errstring);
11032fe8fb19SBen Gras return 0;
11042fe8fb19SBen Gras }
110584d9c625SLionel Sambuc if (ZIC_MAX / SECSPERHOUR < hh) {
11062fe8fb19SBen Gras error(_("time overflow"));
11072fe8fb19SBen Gras return 0;
11082fe8fb19SBen Gras }
11092fe8fb19SBen Gras if (noise && (hh > HOURSPERDAY ||
11102fe8fb19SBen Gras (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
11112fe8fb19SBen Gras warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
111284d9c625SLionel Sambuc return oadd(sign * hh * SECSPERHOUR,
111384d9c625SLionel Sambuc sign * (mm * SECSPERMIN + ss));
11142fe8fb19SBen Gras }
11152fe8fb19SBen Gras
11162fe8fb19SBen Gras static void
inrule(char ** fields,int nfields)1117*0a6a1f1dSLionel Sambuc inrule(char **fields, int nfields)
11182fe8fb19SBen Gras {
11192fe8fb19SBen Gras static struct rule r;
11202fe8fb19SBen Gras
11212fe8fb19SBen Gras if (nfields != RULE_FIELDS) {
11222fe8fb19SBen Gras error(_("wrong number of fields on Rule line"));
11232fe8fb19SBen Gras return;
11242fe8fb19SBen Gras }
11252fe8fb19SBen Gras if (*fields[RF_NAME] == '\0') {
11262fe8fb19SBen Gras error(_("nameless rule"));
11272fe8fb19SBen Gras return;
11282fe8fb19SBen Gras }
11292fe8fb19SBen Gras r.r_filename = filename;
11302fe8fb19SBen Gras r.r_linenum = linenum;
1131*0a6a1f1dSLionel Sambuc r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), true);
11322fe8fb19SBen Gras rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
11332fe8fb19SBen Gras fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
11342fe8fb19SBen Gras r.r_name = ecpyalloc(fields[RF_NAME]);
11352fe8fb19SBen Gras r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
11362fe8fb19SBen Gras if (max_abbrvar_len < strlen(r.r_abbrvar))
11372fe8fb19SBen Gras max_abbrvar_len = strlen(r.r_abbrvar);
1138*0a6a1f1dSLionel Sambuc rules = growalloc(rules, sizeof *rules, nrules, &nrules_alloc);
11392fe8fb19SBen Gras rules[nrules++] = r;
11402fe8fb19SBen Gras }
11412fe8fb19SBen Gras
1142*0a6a1f1dSLionel Sambuc static bool
inzone(char ** fields,int nfields)1143*0a6a1f1dSLionel Sambuc inzone(char **fields, int nfields)
11442fe8fb19SBen Gras {
114584d9c625SLionel Sambuc int i;
11462fe8fb19SBen Gras
11472fe8fb19SBen Gras if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
11482fe8fb19SBen Gras error(_("wrong number of fields on Zone line"));
1149*0a6a1f1dSLionel Sambuc return false;
11502fe8fb19SBen Gras }
11512fe8fb19SBen Gras if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
115284d9c625SLionel Sambuc error(
11532fe8fb19SBen Gras _("\"Zone %s\" line and -l option are mutually exclusive"),
11542fe8fb19SBen Gras TZDEFAULT);
1155*0a6a1f1dSLionel Sambuc return false;
11562fe8fb19SBen Gras }
11572fe8fb19SBen Gras if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
115884d9c625SLionel Sambuc error(
11592fe8fb19SBen Gras _("\"Zone %s\" line and -p option are mutually exclusive"),
11602fe8fb19SBen Gras TZDEFRULES);
1161*0a6a1f1dSLionel Sambuc return false;
11622fe8fb19SBen Gras }
11632fe8fb19SBen Gras for (i = 0; i < nzones; ++i)
11642fe8fb19SBen Gras if (zones[i].z_name != NULL &&
11652fe8fb19SBen Gras strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
116684d9c625SLionel Sambuc error(
11672fe8fb19SBen Gras _("duplicate zone name %s (file \"%s\", line %d)"),
11682fe8fb19SBen Gras fields[ZF_NAME],
11692fe8fb19SBen Gras zones[i].z_filename,
11702fe8fb19SBen Gras zones[i].z_linenum);
1171*0a6a1f1dSLionel Sambuc return false;
11722fe8fb19SBen Gras }
1173*0a6a1f1dSLionel Sambuc return inzsub(fields, nfields, false);
11742fe8fb19SBen Gras }
11752fe8fb19SBen Gras
1176*0a6a1f1dSLionel Sambuc static bool
inzcont(char ** fields,int nfields)1177*0a6a1f1dSLionel Sambuc inzcont(char **fields, int nfields)
11782fe8fb19SBen Gras {
11792fe8fb19SBen Gras if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
11802fe8fb19SBen Gras error(_("wrong number of fields on Zone continuation line"));
1181*0a6a1f1dSLionel Sambuc return false;
11822fe8fb19SBen Gras }
1183*0a6a1f1dSLionel Sambuc return inzsub(fields, nfields, true);
11842fe8fb19SBen Gras }
11852fe8fb19SBen Gras
1186*0a6a1f1dSLionel Sambuc static bool
inzsub(char ** const fields,const int nfields,const int iscont)118784d9c625SLionel Sambuc inzsub(char **const fields, const int nfields, const int iscont)
11882fe8fb19SBen Gras {
118984d9c625SLionel Sambuc char * cp;
1190*0a6a1f1dSLionel Sambuc char * cp1;
11912fe8fb19SBen Gras static struct zone z;
119284d9c625SLionel Sambuc int i_gmtoff, i_rule, i_format;
119384d9c625SLionel Sambuc int i_untilyear, i_untilmonth;
119484d9c625SLionel Sambuc int i_untilday, i_untiltime;
1195*0a6a1f1dSLionel Sambuc bool hasuntil;
11962fe8fb19SBen Gras
11972fe8fb19SBen Gras if (iscont) {
11982fe8fb19SBen Gras i_gmtoff = ZFC_GMTOFF;
11992fe8fb19SBen Gras i_rule = ZFC_RULE;
12002fe8fb19SBen Gras i_format = ZFC_FORMAT;
12012fe8fb19SBen Gras i_untilyear = ZFC_TILYEAR;
12022fe8fb19SBen Gras i_untilmonth = ZFC_TILMONTH;
12032fe8fb19SBen Gras i_untilday = ZFC_TILDAY;
12042fe8fb19SBen Gras i_untiltime = ZFC_TILTIME;
12052fe8fb19SBen Gras z.z_name = NULL;
1206*0a6a1f1dSLionel Sambuc } else if (!namecheck(fields[ZF_NAME]))
1207*0a6a1f1dSLionel Sambuc return false;
1208*0a6a1f1dSLionel Sambuc else {
12092fe8fb19SBen Gras i_gmtoff = ZF_GMTOFF;
12102fe8fb19SBen Gras i_rule = ZF_RULE;
12112fe8fb19SBen Gras i_format = ZF_FORMAT;
12122fe8fb19SBen Gras i_untilyear = ZF_TILYEAR;
12132fe8fb19SBen Gras i_untilmonth = ZF_TILMONTH;
12142fe8fb19SBen Gras i_untilday = ZF_TILDAY;
12152fe8fb19SBen Gras i_untiltime = ZF_TILTIME;
12162fe8fb19SBen Gras z.z_name = ecpyalloc(fields[ZF_NAME]);
12172fe8fb19SBen Gras }
12182fe8fb19SBen Gras z.z_filename = filename;
12192fe8fb19SBen Gras z.z_linenum = linenum;
1220*0a6a1f1dSLionel Sambuc z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"), true);
12212fe8fb19SBen Gras if ((cp = strchr(fields[i_format], '%')) != 0) {
1222*0a6a1f1dSLionel Sambuc if ((*++cp != 's' && *cp != 'z') || strchr(cp, '%')
1223*0a6a1f1dSLionel Sambuc || strchr(fields[i_format], '/')) {
12242fe8fb19SBen Gras error(_("invalid abbreviation format"));
1225*0a6a1f1dSLionel Sambuc return false;
12262fe8fb19SBen Gras }
12272fe8fb19SBen Gras }
12282fe8fb19SBen Gras z.z_rule = ecpyalloc(fields[i_rule]);
1229*0a6a1f1dSLionel Sambuc z.z_format = cp1 = ecpyalloc(fields[i_format]);
1230*0a6a1f1dSLionel Sambuc z.z_format_specifier = cp ? *cp : '\0';
1231*0a6a1f1dSLionel Sambuc if (z.z_format_specifier == 'z') {
1232*0a6a1f1dSLionel Sambuc if (noise)
1233*0a6a1f1dSLionel Sambuc warning(_("format '%s' not handled by pre-2015 versions of zic"),
1234*0a6a1f1dSLionel Sambuc z.z_format);
1235*0a6a1f1dSLionel Sambuc cp1[cp - fields[i_format]] = 's';
1236*0a6a1f1dSLionel Sambuc }
12372fe8fb19SBen Gras if (max_format_len < strlen(z.z_format))
12382fe8fb19SBen Gras max_format_len = strlen(z.z_format);
12392fe8fb19SBen Gras hasuntil = nfields > i_untilyear;
12402fe8fb19SBen Gras if (hasuntil) {
12412fe8fb19SBen Gras z.z_untilrule.r_filename = filename;
12422fe8fb19SBen Gras z.z_untilrule.r_linenum = linenum;
12432fe8fb19SBen Gras rulesub(&z.z_untilrule,
12442fe8fb19SBen Gras fields[i_untilyear],
12452fe8fb19SBen Gras "only",
12462fe8fb19SBen Gras "",
12472fe8fb19SBen Gras (nfields > i_untilmonth) ?
12482fe8fb19SBen Gras fields[i_untilmonth] : "Jan",
12492fe8fb19SBen Gras (nfields > i_untilday) ? fields[i_untilday] : "1",
12502fe8fb19SBen Gras (nfields > i_untiltime) ? fields[i_untiltime] : "0");
12512fe8fb19SBen Gras z.z_untiltime = rpytime(&z.z_untilrule,
12522fe8fb19SBen Gras z.z_untilrule.r_loyear);
12532fe8fb19SBen Gras if (iscont && nzones > 0 &&
12542fe8fb19SBen Gras z.z_untiltime > min_time &&
12552fe8fb19SBen Gras z.z_untiltime < max_time &&
12562fe8fb19SBen Gras zones[nzones - 1].z_untiltime > min_time &&
12572fe8fb19SBen Gras zones[nzones - 1].z_untiltime < max_time &&
12582fe8fb19SBen Gras zones[nzones - 1].z_untiltime >= z.z_untiltime) {
12592fe8fb19SBen Gras error(_(
12602fe8fb19SBen Gras "Zone continuation line end time is not after end time of previous line"
12612fe8fb19SBen Gras ));
1262*0a6a1f1dSLionel Sambuc return false;
12632fe8fb19SBen Gras }
12642fe8fb19SBen Gras }
1265*0a6a1f1dSLionel Sambuc zones = growalloc(zones, sizeof *zones, nzones, &nzones_alloc);
12662fe8fb19SBen Gras zones[nzones++] = z;
12672fe8fb19SBen Gras /*
12682fe8fb19SBen Gras ** If there was an UNTIL field on this line,
12692fe8fb19SBen Gras ** there's more information about the zone on the next line.
12702fe8fb19SBen Gras */
12712fe8fb19SBen Gras return hasuntil;
12722fe8fb19SBen Gras }
12732fe8fb19SBen Gras
12742fe8fb19SBen Gras static void
inleap(char ** fields,int nfields)1275*0a6a1f1dSLionel Sambuc inleap(char **fields, int nfields)
12762fe8fb19SBen Gras {
127784d9c625SLionel Sambuc const char * cp;
127884d9c625SLionel Sambuc const struct lookup * lp;
127984d9c625SLionel Sambuc int i, j;
128084d9c625SLionel Sambuc zic_t year;
128184d9c625SLionel Sambuc int month, day;
128284d9c625SLionel Sambuc zic_t dayoff, tod;
12832fe8fb19SBen Gras zic_t t;
1284*0a6a1f1dSLionel Sambuc char xs;
12852fe8fb19SBen Gras
12862fe8fb19SBen Gras if (nfields != LEAP_FIELDS) {
12872fe8fb19SBen Gras error(_("wrong number of fields on Leap line"));
12882fe8fb19SBen Gras return;
12892fe8fb19SBen Gras }
12902fe8fb19SBen Gras dayoff = 0;
12912fe8fb19SBen Gras cp = fields[LP_YEAR];
1292*0a6a1f1dSLionel Sambuc if (sscanf(cp, "%"SCNdZIC"%c", &year, &xs) != 1) {
12932fe8fb19SBen Gras /*
12942fe8fb19SBen Gras ** Leapin' Lizards!
12952fe8fb19SBen Gras */
12962fe8fb19SBen Gras error(_("invalid leaping year"));
12972fe8fb19SBen Gras return;
12982fe8fb19SBen Gras }
12992fe8fb19SBen Gras if (!leapseen || leapmaxyear < year)
13002fe8fb19SBen Gras leapmaxyear = year;
13012fe8fb19SBen Gras if (!leapseen || leapminyear > year)
13022fe8fb19SBen Gras leapminyear = year;
1303*0a6a1f1dSLionel Sambuc leapseen = true;
13042fe8fb19SBen Gras j = EPOCH_YEAR;
13052fe8fb19SBen Gras while (j != year) {
13062fe8fb19SBen Gras if (year > j) {
13072fe8fb19SBen Gras i = len_years[isleap(j)];
13082fe8fb19SBen Gras ++j;
13092fe8fb19SBen Gras } else {
13102fe8fb19SBen Gras --j;
13112fe8fb19SBen Gras i = -len_years[isleap(j)];
13122fe8fb19SBen Gras }
131384d9c625SLionel Sambuc dayoff = oadd(dayoff, i);
13142fe8fb19SBen Gras }
13152fe8fb19SBen Gras if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
13162fe8fb19SBen Gras error(_("invalid month name"));
13172fe8fb19SBen Gras return;
13182fe8fb19SBen Gras }
13192fe8fb19SBen Gras month = lp->l_value;
13202fe8fb19SBen Gras j = TM_JANUARY;
13212fe8fb19SBen Gras while (j != month) {
13222fe8fb19SBen Gras i = len_months[isleap(year)][j];
132384d9c625SLionel Sambuc dayoff = oadd(dayoff, i);
13242fe8fb19SBen Gras ++j;
13252fe8fb19SBen Gras }
13262fe8fb19SBen Gras cp = fields[LP_DAY];
1327*0a6a1f1dSLionel Sambuc if (sscanf(cp, "%d%c", &day, &xs) != 1 ||
13282fe8fb19SBen Gras day <= 0 || day > len_months[isleap(year)][month]) {
13292fe8fb19SBen Gras error(_("invalid day of month"));
13302fe8fb19SBen Gras return;
13312fe8fb19SBen Gras }
133284d9c625SLionel Sambuc dayoff = oadd(dayoff, day - 1);
13332fe8fb19SBen Gras if (dayoff < min_time / SECSPERDAY) {
13342fe8fb19SBen Gras error(_("time too small"));
13352fe8fb19SBen Gras return;
13362fe8fb19SBen Gras }
13372fe8fb19SBen Gras if (dayoff > max_time / SECSPERDAY) {
13382fe8fb19SBen Gras error(_("time too large"));
13392fe8fb19SBen Gras return;
13402fe8fb19SBen Gras }
1341*0a6a1f1dSLionel Sambuc t = dayoff * SECSPERDAY;
1342*0a6a1f1dSLionel Sambuc tod = gethms(fields[LP_TIME], _("invalid time of day"), false);
13432fe8fb19SBen Gras cp = fields[LP_CORR];
13442fe8fb19SBen Gras {
1345*0a6a1f1dSLionel Sambuc bool positive;
13462fe8fb19SBen Gras int count;
13472fe8fb19SBen Gras
13482fe8fb19SBen Gras if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1349*0a6a1f1dSLionel Sambuc positive = false;
13502fe8fb19SBen Gras count = 1;
13512fe8fb19SBen Gras } else if (strcmp(cp, "--") == 0) {
1352*0a6a1f1dSLionel Sambuc positive = false;
13532fe8fb19SBen Gras count = 2;
13542fe8fb19SBen Gras } else if (strcmp(cp, "+") == 0) {
1355*0a6a1f1dSLionel Sambuc positive = true;
13562fe8fb19SBen Gras count = 1;
13572fe8fb19SBen Gras } else if (strcmp(cp, "++") == 0) {
1358*0a6a1f1dSLionel Sambuc positive = true;
13592fe8fb19SBen Gras count = 2;
13602fe8fb19SBen Gras } else {
13612fe8fb19SBen Gras error(_("illegal CORRECTION field on Leap line"));
13622fe8fb19SBen Gras return;
13632fe8fb19SBen Gras }
13642fe8fb19SBen Gras if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
13652fe8fb19SBen Gras error(_(
13662fe8fb19SBen Gras "illegal Rolling/Stationary field on Leap line"
13672fe8fb19SBen Gras ));
13682fe8fb19SBen Gras return;
13692fe8fb19SBen Gras }
1370*0a6a1f1dSLionel Sambuc t = tadd(t, tod);
1371*0a6a1f1dSLionel Sambuc if (t < big_bang_time) {
1372*0a6a1f1dSLionel Sambuc error(_("leap second precedes Big Bang"));
1373*0a6a1f1dSLionel Sambuc return;
1374*0a6a1f1dSLionel Sambuc }
1375*0a6a1f1dSLionel Sambuc leapadd(t, positive, lp->l_value, count);
13762fe8fb19SBen Gras }
13772fe8fb19SBen Gras }
13782fe8fb19SBen Gras
13792fe8fb19SBen Gras static void
inlink(char ** const fields,const int nfields)138084d9c625SLionel Sambuc inlink(char **const fields, const int nfields)
13812fe8fb19SBen Gras {
13822fe8fb19SBen Gras struct link l;
13832fe8fb19SBen Gras
13842fe8fb19SBen Gras if (nfields != LINK_FIELDS) {
13852fe8fb19SBen Gras error(_("wrong number of fields on Link line"));
13862fe8fb19SBen Gras return;
13872fe8fb19SBen Gras }
13882fe8fb19SBen Gras if (*fields[LF_FROM] == '\0') {
13892fe8fb19SBen Gras error(_("blank FROM field on Link line"));
13902fe8fb19SBen Gras return;
13912fe8fb19SBen Gras }
1392*0a6a1f1dSLionel Sambuc if (! namecheck(fields[LF_TO]))
13932fe8fb19SBen Gras return;
13942fe8fb19SBen Gras l.l_filename = filename;
13952fe8fb19SBen Gras l.l_linenum = linenum;
13962fe8fb19SBen Gras l.l_from = ecpyalloc(fields[LF_FROM]);
13972fe8fb19SBen Gras l.l_to = ecpyalloc(fields[LF_TO]);
1398*0a6a1f1dSLionel Sambuc links = growalloc(links, sizeof *links, nlinks, &nlinks_alloc);
13992fe8fb19SBen Gras links[nlinks++] = l;
14002fe8fb19SBen Gras }
14012fe8fb19SBen Gras
14022fe8fb19SBen Gras static void
rulesub(struct rule * rp,const char * loyearp,const char * hiyearp,const char * typep,const char * monthp,const char * dayp,const char * timep)1403*0a6a1f1dSLionel Sambuc rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
1404*0a6a1f1dSLionel Sambuc const char *typep, const char *monthp, const char *dayp,
1405*0a6a1f1dSLionel Sambuc const char *timep)
14062fe8fb19SBen Gras {
140784d9c625SLionel Sambuc const struct lookup * lp;
140884d9c625SLionel Sambuc const char * cp;
140984d9c625SLionel Sambuc char * dp;
141084d9c625SLionel Sambuc char * ep;
1411*0a6a1f1dSLionel Sambuc char xs;
14122fe8fb19SBen Gras
14132fe8fb19SBen Gras if ((lp = byword(monthp, mon_names)) == NULL) {
14142fe8fb19SBen Gras error(_("invalid month name"));
14152fe8fb19SBen Gras return;
14162fe8fb19SBen Gras }
14172fe8fb19SBen Gras rp->r_month = lp->l_value;
1418*0a6a1f1dSLionel Sambuc rp->r_todisstd = false;
1419*0a6a1f1dSLionel Sambuc rp->r_todisgmt = false;
14202fe8fb19SBen Gras dp = ecpyalloc(timep);
14212fe8fb19SBen Gras if (*dp != '\0') {
14222fe8fb19SBen Gras ep = dp + strlen(dp) - 1;
14232fe8fb19SBen Gras switch (lowerit(*ep)) {
14242fe8fb19SBen Gras case 's': /* Standard */
1425*0a6a1f1dSLionel Sambuc rp->r_todisstd = true;
1426*0a6a1f1dSLionel Sambuc rp->r_todisgmt = false;
14272fe8fb19SBen Gras *ep = '\0';
14282fe8fb19SBen Gras break;
14292fe8fb19SBen Gras case 'w': /* Wall */
1430*0a6a1f1dSLionel Sambuc rp->r_todisstd = false;
1431*0a6a1f1dSLionel Sambuc rp->r_todisgmt = false;
14322fe8fb19SBen Gras *ep = '\0';
14332fe8fb19SBen Gras break;
14342fe8fb19SBen Gras case 'g': /* Greenwich */
14352fe8fb19SBen Gras case 'u': /* Universal */
14362fe8fb19SBen Gras case 'z': /* Zulu */
1437*0a6a1f1dSLionel Sambuc rp->r_todisstd = true;
1438*0a6a1f1dSLionel Sambuc rp->r_todisgmt = true;
14392fe8fb19SBen Gras *ep = '\0';
14402fe8fb19SBen Gras break;
14412fe8fb19SBen Gras }
14422fe8fb19SBen Gras }
1443*0a6a1f1dSLionel Sambuc rp->r_tod = gethms(dp, _("invalid time of day"), false);
144484d9c625SLionel Sambuc free(dp);
14452fe8fb19SBen Gras /*
14462fe8fb19SBen Gras ** Year work.
14472fe8fb19SBen Gras */
14482fe8fb19SBen Gras cp = loyearp;
14492fe8fb19SBen Gras lp = byword(cp, begin_years);
14502fe8fb19SBen Gras rp->r_lowasnum = lp == NULL;
14512fe8fb19SBen Gras if (!rp->r_lowasnum) switch ((int) lp->l_value) {
14522fe8fb19SBen Gras case YR_MINIMUM:
145384d9c625SLionel Sambuc rp->r_loyear = ZIC_MIN;
14542fe8fb19SBen Gras break;
14552fe8fb19SBen Gras case YR_MAXIMUM:
145684d9c625SLionel Sambuc rp->r_loyear = ZIC_MAX;
14572fe8fb19SBen Gras break;
14582fe8fb19SBen Gras default: /* "cannot happen" */
1459*0a6a1f1dSLionel Sambuc fprintf(stderr,
14602fe8fb19SBen Gras _("%s: panic: Invalid l_value %d\n"),
14612fe8fb19SBen Gras progname, lp->l_value);
14622fe8fb19SBen Gras exit(EXIT_FAILURE);
1463*0a6a1f1dSLionel Sambuc } else if (sscanf(cp, "%"SCNdZIC"%c", &rp->r_loyear, &xs) != 1) {
14642fe8fb19SBen Gras error(_("invalid starting year"));
14652fe8fb19SBen Gras return;
14662fe8fb19SBen Gras }
14672fe8fb19SBen Gras cp = hiyearp;
14682fe8fb19SBen Gras lp = byword(cp, end_years);
14692fe8fb19SBen Gras rp->r_hiwasnum = lp == NULL;
14702fe8fb19SBen Gras if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
14712fe8fb19SBen Gras case YR_MINIMUM:
147284d9c625SLionel Sambuc rp->r_hiyear = ZIC_MIN;
14732fe8fb19SBen Gras break;
14742fe8fb19SBen Gras case YR_MAXIMUM:
147584d9c625SLionel Sambuc rp->r_hiyear = ZIC_MAX;
14762fe8fb19SBen Gras break;
14772fe8fb19SBen Gras case YR_ONLY:
14782fe8fb19SBen Gras rp->r_hiyear = rp->r_loyear;
14792fe8fb19SBen Gras break;
14802fe8fb19SBen Gras default: /* "cannot happen" */
1481*0a6a1f1dSLionel Sambuc fprintf(stderr,
14822fe8fb19SBen Gras _("%s: panic: Invalid l_value %d\n"),
14832fe8fb19SBen Gras progname, lp->l_value);
14842fe8fb19SBen Gras exit(EXIT_FAILURE);
1485*0a6a1f1dSLionel Sambuc } else if (sscanf(cp, "%"SCNdZIC"%c", &rp->r_hiyear, &xs) != 1) {
14862fe8fb19SBen Gras error(_("invalid ending year"));
14872fe8fb19SBen Gras return;
14882fe8fb19SBen Gras }
14892fe8fb19SBen Gras if (rp->r_loyear > rp->r_hiyear) {
14902fe8fb19SBen Gras error(_("starting year greater than ending year"));
14912fe8fb19SBen Gras return;
14922fe8fb19SBen Gras }
14932fe8fb19SBen Gras if (*typep == '\0')
14942fe8fb19SBen Gras rp->r_yrtype = NULL;
14952fe8fb19SBen Gras else {
14962fe8fb19SBen Gras if (rp->r_loyear == rp->r_hiyear) {
14972fe8fb19SBen Gras error(_("typed single year"));
14982fe8fb19SBen Gras return;
14992fe8fb19SBen Gras }
15002fe8fb19SBen Gras rp->r_yrtype = ecpyalloc(typep);
15012fe8fb19SBen Gras }
15022fe8fb19SBen Gras /*
15032fe8fb19SBen Gras ** Day work.
15042fe8fb19SBen Gras ** Accept things such as:
15052fe8fb19SBen Gras ** 1
15062fe8fb19SBen Gras ** last-Sunday
15072fe8fb19SBen Gras ** Sun<=20
15082fe8fb19SBen Gras ** Sun>=7
15092fe8fb19SBen Gras */
15102fe8fb19SBen Gras dp = ecpyalloc(dayp);
15112fe8fb19SBen Gras if ((lp = byword(dp, lasts)) != NULL) {
15122fe8fb19SBen Gras rp->r_dycode = DC_DOWLEQ;
15132fe8fb19SBen Gras rp->r_wday = lp->l_value;
15142fe8fb19SBen Gras rp->r_dayofmonth = len_months[1][rp->r_month];
15152fe8fb19SBen Gras } else {
15162fe8fb19SBen Gras if ((ep = strchr(dp, '<')) != 0)
15172fe8fb19SBen Gras rp->r_dycode = DC_DOWLEQ;
15182fe8fb19SBen Gras else if ((ep = strchr(dp, '>')) != 0)
15192fe8fb19SBen Gras rp->r_dycode = DC_DOWGEQ;
15202fe8fb19SBen Gras else {
15212fe8fb19SBen Gras ep = dp;
15222fe8fb19SBen Gras rp->r_dycode = DC_DOM;
15232fe8fb19SBen Gras }
15242fe8fb19SBen Gras if (rp->r_dycode != DC_DOM) {
15252fe8fb19SBen Gras *ep++ = 0;
15262fe8fb19SBen Gras if (*ep++ != '=') {
15272fe8fb19SBen Gras error(_("invalid day of month"));
152884d9c625SLionel Sambuc free(dp);
15292fe8fb19SBen Gras return;
15302fe8fb19SBen Gras }
15312fe8fb19SBen Gras if ((lp = byword(dp, wday_names)) == NULL) {
15322fe8fb19SBen Gras error(_("invalid weekday name"));
153384d9c625SLionel Sambuc free(dp);
15342fe8fb19SBen Gras return;
15352fe8fb19SBen Gras }
15362fe8fb19SBen Gras rp->r_wday = lp->l_value;
15372fe8fb19SBen Gras }
1538*0a6a1f1dSLionel Sambuc if (sscanf(ep, "%d%c", &rp->r_dayofmonth, &xs) != 1 ||
15392fe8fb19SBen Gras rp->r_dayofmonth <= 0 ||
15402fe8fb19SBen Gras (rp->r_dayofmonth > len_months[1][rp->r_month])) {
15412fe8fb19SBen Gras error(_("invalid day of month"));
154284d9c625SLionel Sambuc free(dp);
15432fe8fb19SBen Gras return;
15442fe8fb19SBen Gras }
15452fe8fb19SBen Gras }
154684d9c625SLionel Sambuc free(dp);
15472fe8fb19SBen Gras }
15482fe8fb19SBen Gras
15492fe8fb19SBen Gras static void
convert(const zic_t val,char * const buf)155084d9c625SLionel Sambuc convert(const zic_t val, char *const buf)
15512fe8fb19SBen Gras {
155284d9c625SLionel Sambuc int i;
155384d9c625SLionel Sambuc int shift;
155484d9c625SLionel Sambuc unsigned char *const b = (unsigned char *) buf;
15552fe8fb19SBen Gras
15562fe8fb19SBen Gras for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
155784d9c625SLionel Sambuc b[i] = val >> shift;
15582fe8fb19SBen Gras }
15592fe8fb19SBen Gras
15602fe8fb19SBen Gras static void
convert64(const zic_t val,char * const buf)156184d9c625SLionel Sambuc convert64(const zic_t val, char *const buf)
15622fe8fb19SBen Gras {
156384d9c625SLionel Sambuc int i;
156484d9c625SLionel Sambuc int shift;
156584d9c625SLionel Sambuc unsigned char *const b = (unsigned char *) buf;
15662fe8fb19SBen Gras
15672fe8fb19SBen Gras for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
156884d9c625SLionel Sambuc b[i] = val >> shift;
15692fe8fb19SBen Gras }
15702fe8fb19SBen Gras
15712fe8fb19SBen Gras static void
puttzcode(const zic_t val,FILE * const fp)157284d9c625SLionel Sambuc puttzcode(const zic_t val, FILE *const fp)
15732fe8fb19SBen Gras {
15742fe8fb19SBen Gras char buf[4];
15752fe8fb19SBen Gras
15762fe8fb19SBen Gras convert(val, buf);
157784d9c625SLionel Sambuc (void) fwrite(buf, sizeof buf, (size_t) 1, fp);
15782fe8fb19SBen Gras }
15792fe8fb19SBen Gras
15802fe8fb19SBen Gras static void
puttzcode64(const zic_t val,FILE * const fp)158184d9c625SLionel Sambuc puttzcode64(const zic_t val, FILE *const fp)
15822fe8fb19SBen Gras {
15832fe8fb19SBen Gras char buf[8];
15842fe8fb19SBen Gras
15852fe8fb19SBen Gras convert64(val, buf);
158684d9c625SLionel Sambuc (void) fwrite(buf, sizeof buf, (size_t) 1, fp);
15872fe8fb19SBen Gras }
15882fe8fb19SBen Gras
15892fe8fb19SBen Gras static int
atcomp(const void * avp,const void * bvp)159084d9c625SLionel Sambuc atcomp(const void *avp, const void *bvp)
15912fe8fb19SBen Gras {
15922fe8fb19SBen Gras const zic_t a = ((const struct attype *) avp)->at;
15932fe8fb19SBen Gras const zic_t b = ((const struct attype *) bvp)->at;
15942fe8fb19SBen Gras
15952fe8fb19SBen Gras return (a < b) ? -1 : (a > b);
15962fe8fb19SBen Gras }
15972fe8fb19SBen Gras
1598*0a6a1f1dSLionel Sambuc static bool
is32(const zic_t x)159984d9c625SLionel Sambuc is32(const zic_t x)
16002fe8fb19SBen Gras {
16012fe8fb19SBen Gras return INT32_MIN <= x && x <= INT32_MAX;
16022fe8fb19SBen Gras }
16032fe8fb19SBen Gras
16042fe8fb19SBen Gras static void
writezone(const char * const name,const char * const string,char version)160584d9c625SLionel Sambuc writezone(const char *const name, const char *const string, char version)
16062fe8fb19SBen Gras {
160784d9c625SLionel Sambuc FILE * fp;
160884d9c625SLionel Sambuc int i, j;
160984d9c625SLionel Sambuc int leapcnt32, leapi32;
161084d9c625SLionel Sambuc int timecnt32, timei32;
161184d9c625SLionel Sambuc int pass;
1612*0a6a1f1dSLionel Sambuc char * fullname;
16132fe8fb19SBen Gras static const struct tzhead tzh0;
16142fe8fb19SBen Gras static struct tzhead tzh;
1615*0a6a1f1dSLionel Sambuc zic_t *ats = zic_malloc(size_product(timecnt, sizeof *ats + 1));
1616*0a6a1f1dSLionel Sambuc void *typesptr = ats + timecnt;
1617*0a6a1f1dSLionel Sambuc unsigned char *types = typesptr;
16182fe8fb19SBen Gras
16192fe8fb19SBen Gras /*
16202fe8fb19SBen Gras ** Sort.
16212fe8fb19SBen Gras */
16222fe8fb19SBen Gras if (timecnt > 1)
162384d9c625SLionel Sambuc (void) qsort(attypes, (size_t) timecnt, sizeof *attypes,
162484d9c625SLionel Sambuc atcomp);
16252fe8fb19SBen Gras /*
16262fe8fb19SBen Gras ** Optimize.
16272fe8fb19SBen Gras */
16282fe8fb19SBen Gras {
16292fe8fb19SBen Gras int fromi;
16302fe8fb19SBen Gras int toi;
16312fe8fb19SBen Gras
16322fe8fb19SBen Gras toi = 0;
16332fe8fb19SBen Gras fromi = 0;
1634*0a6a1f1dSLionel Sambuc while (fromi < timecnt && attypes[fromi].at < big_bang_time)
16352fe8fb19SBen Gras ++fromi;
16362fe8fb19SBen Gras for ( ; fromi < timecnt; ++fromi) {
1637*0a6a1f1dSLionel Sambuc if (toi > 1 && ((attypes[fromi].at +
16382fe8fb19SBen Gras gmtoffs[attypes[toi - 1].type]) <=
1639*0a6a1f1dSLionel Sambuc (attypes[toi - 1].at +
1640*0a6a1f1dSLionel Sambuc gmtoffs[attypes[toi - 2].type]))) {
16412fe8fb19SBen Gras attypes[toi - 1].type =
16422fe8fb19SBen Gras attypes[fromi].type;
16432fe8fb19SBen Gras continue;
16442fe8fb19SBen Gras }
16452fe8fb19SBen Gras if (toi == 0 ||
16462fe8fb19SBen Gras attypes[toi - 1].type != attypes[fromi].type)
16472fe8fb19SBen Gras attypes[toi++] = attypes[fromi];
16482fe8fb19SBen Gras }
16492fe8fb19SBen Gras timecnt = toi;
16502fe8fb19SBen Gras }
1651*0a6a1f1dSLionel Sambuc if (noise && timecnt > 1200)
1652*0a6a1f1dSLionel Sambuc warning(_("pre-2014 clients may mishandle"
1653*0a6a1f1dSLionel Sambuc " more than 1200 transition times"));
16542fe8fb19SBen Gras /*
16552fe8fb19SBen Gras ** Transfer.
16562fe8fb19SBen Gras */
16572fe8fb19SBen Gras for (i = 0; i < timecnt; ++i) {
16582fe8fb19SBen Gras ats[i] = attypes[i].at;
16592fe8fb19SBen Gras types[i] = attypes[i].type;
16602fe8fb19SBen Gras }
16612fe8fb19SBen Gras /*
16622fe8fb19SBen Gras ** Correct for leap seconds.
16632fe8fb19SBen Gras */
16642fe8fb19SBen Gras for (i = 0; i < timecnt; ++i) {
16652fe8fb19SBen Gras j = leapcnt;
16662fe8fb19SBen Gras while (--j >= 0)
16672fe8fb19SBen Gras if (ats[i] > trans[j] - corr[j]) {
16682fe8fb19SBen Gras ats[i] = tadd(ats[i], corr[j]);
16692fe8fb19SBen Gras break;
16702fe8fb19SBen Gras }
16712fe8fb19SBen Gras }
16722fe8fb19SBen Gras /*
16732fe8fb19SBen Gras ** Figure out 32-bit-limited starts and counts.
16742fe8fb19SBen Gras */
16752fe8fb19SBen Gras timecnt32 = timecnt;
16762fe8fb19SBen Gras timei32 = 0;
16772fe8fb19SBen Gras leapcnt32 = leapcnt;
16782fe8fb19SBen Gras leapi32 = 0;
16792fe8fb19SBen Gras while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
16802fe8fb19SBen Gras --timecnt32;
16812fe8fb19SBen Gras while (timecnt32 > 0 && !is32(ats[timei32])) {
16822fe8fb19SBen Gras --timecnt32;
16832fe8fb19SBen Gras ++timei32;
16842fe8fb19SBen Gras }
1685*0a6a1f1dSLionel Sambuc /*
1686*0a6a1f1dSLionel Sambuc ** Output an INT32_MIN "transition" if appropriate; see below.
1687*0a6a1f1dSLionel Sambuc */
1688*0a6a1f1dSLionel Sambuc if (timei32 > 0 && ats[timei32] > INT32_MIN) {
1689*0a6a1f1dSLionel Sambuc --timei32;
1690*0a6a1f1dSLionel Sambuc ++timecnt32;
1691*0a6a1f1dSLionel Sambuc }
16922fe8fb19SBen Gras while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
16932fe8fb19SBen Gras --leapcnt32;
16942fe8fb19SBen Gras while (leapcnt32 > 0 && !is32(trans[leapi32])) {
16952fe8fb19SBen Gras --leapcnt32;
16962fe8fb19SBen Gras ++leapi32;
16972fe8fb19SBen Gras }
1698*0a6a1f1dSLionel Sambuc fullname = relname(directory, name);
16992fe8fb19SBen Gras /*
17002fe8fb19SBen Gras ** Remove old file, if any, to snap links.
17012fe8fb19SBen Gras */
1702*0a6a1f1dSLionel Sambuc if (itsdir(fullname) <= 0 && remove(fullname) != 0 && errno != ENOENT) {
17032fe8fb19SBen Gras const char *e = strerror(errno);
17042fe8fb19SBen Gras
17052fe8fb19SBen Gras (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
17062fe8fb19SBen Gras progname, fullname, e);
17072fe8fb19SBen Gras exit(EXIT_FAILURE);
17082fe8fb19SBen Gras }
17092fe8fb19SBen Gras if ((fp = fopen(fullname, "wb")) == NULL) {
1710*0a6a1f1dSLionel Sambuc if (! mkdirs(fullname))
17112fe8fb19SBen Gras exit(EXIT_FAILURE);
17122fe8fb19SBen Gras if ((fp = fopen(fullname, "wb")) == NULL) {
17132fe8fb19SBen Gras const char *e = strerror(errno);
17142fe8fb19SBen Gras
17152fe8fb19SBen Gras (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
17162fe8fb19SBen Gras progname, fullname, e);
17172fe8fb19SBen Gras exit(EXIT_FAILURE);
17182fe8fb19SBen Gras }
17192fe8fb19SBen Gras }
17202fe8fb19SBen Gras for (pass = 1; pass <= 2; ++pass) {
172184d9c625SLionel Sambuc int thistimei, thistimecnt;
172284d9c625SLionel Sambuc int thisleapi, thisleapcnt;
172384d9c625SLionel Sambuc int thistimelim, thisleaplim;
1724*0a6a1f1dSLionel Sambuc int writetype[TZ_MAX_TYPES];
17252fe8fb19SBen Gras int typemap[TZ_MAX_TYPES];
172684d9c625SLionel Sambuc int thistypecnt;
17272fe8fb19SBen Gras char thischars[TZ_MAX_CHARS];
17282fe8fb19SBen Gras char thischarcnt;
17292fe8fb19SBen Gras int indmap[TZ_MAX_CHARS];
17302fe8fb19SBen Gras
17312fe8fb19SBen Gras if (pass == 1) {
17322fe8fb19SBen Gras thistimei = timei32;
17332fe8fb19SBen Gras thistimecnt = timecnt32;
17342fe8fb19SBen Gras thisleapi = leapi32;
17352fe8fb19SBen Gras thisleapcnt = leapcnt32;
17362fe8fb19SBen Gras } else {
17372fe8fb19SBen Gras thistimei = 0;
17382fe8fb19SBen Gras thistimecnt = timecnt;
17392fe8fb19SBen Gras thisleapi = 0;
17402fe8fb19SBen Gras thisleapcnt = leapcnt;
17412fe8fb19SBen Gras }
17422fe8fb19SBen Gras thistimelim = thistimei + thistimecnt;
17432fe8fb19SBen Gras thisleaplim = thisleapi + thisleapcnt;
1744*0a6a1f1dSLionel Sambuc for (i = 0; i < typecnt; ++i)
17452fe8fb19SBen Gras writetype[i] = thistimecnt == timecnt;
17462fe8fb19SBen Gras if (thistimecnt == 0) {
17472fe8fb19SBen Gras /*
17482fe8fb19SBen Gras ** No transition times fall in the current
17492fe8fb19SBen Gras ** (32- or 64-bit) window.
17502fe8fb19SBen Gras */
17512fe8fb19SBen Gras if (typecnt != 0)
1752*0a6a1f1dSLionel Sambuc writetype[typecnt - 1] = true;
17532fe8fb19SBen Gras } else {
17542fe8fb19SBen Gras for (i = thistimei - 1; i < thistimelim; ++i)
17552fe8fb19SBen Gras if (i >= 0)
1756*0a6a1f1dSLionel Sambuc writetype[types[i]] = true;
17572fe8fb19SBen Gras /*
17582fe8fb19SBen Gras ** For America/Godthab and Antarctica/Palmer
17592fe8fb19SBen Gras */
17602fe8fb19SBen Gras if (thistimei == 0)
1761*0a6a1f1dSLionel Sambuc writetype[0] = true;
17622fe8fb19SBen Gras }
1763f14fb602SLionel Sambuc #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1764f14fb602SLionel Sambuc /*
1765f14fb602SLionel Sambuc ** For some pre-2011 systems: if the last-to-be-written
1766f14fb602SLionel Sambuc ** standard (or daylight) type has an offset different from the
1767f14fb602SLionel Sambuc ** most recently used offset,
1768f14fb602SLionel Sambuc ** append an (unused) copy of the most recently used type
1769f14fb602SLionel Sambuc ** (to help get global "altzone" and "timezone" variables
1770f14fb602SLionel Sambuc ** set correctly).
1771f14fb602SLionel Sambuc */
1772f14fb602SLionel Sambuc {
177384d9c625SLionel Sambuc int mrudst, mrustd, hidst, histd, type;
1774f14fb602SLionel Sambuc
1775f14fb602SLionel Sambuc hidst = histd = mrudst = mrustd = -1;
177684d9c625SLionel Sambuc for (i = thistimei; i < thistimelim; ++i) {
177784d9c625SLionel Sambuc if (i < 0)
177884d9c625SLionel Sambuc continue;
1779f14fb602SLionel Sambuc if (isdsts[types[i]])
1780f14fb602SLionel Sambuc mrudst = types[i];
1781f14fb602SLionel Sambuc else mrustd = types[i];
178284d9c625SLionel Sambuc }
1783f14fb602SLionel Sambuc for (i = 0; i < typecnt; ++i)
1784f14fb602SLionel Sambuc if (writetype[i]) {
1785f14fb602SLionel Sambuc if (isdsts[i])
1786f14fb602SLionel Sambuc hidst = i;
1787f14fb602SLionel Sambuc else histd = i;
1788f14fb602SLionel Sambuc }
1789f14fb602SLionel Sambuc if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1790f14fb602SLionel Sambuc gmtoffs[hidst] != gmtoffs[mrudst]) {
1791f14fb602SLionel Sambuc isdsts[mrudst] = -1;
1792f14fb602SLionel Sambuc type = addtype(gmtoffs[mrudst],
1793f14fb602SLionel Sambuc &chars[abbrinds[mrudst]],
1794*0a6a1f1dSLionel Sambuc true,
1795f14fb602SLionel Sambuc ttisstds[mrudst],
1796f14fb602SLionel Sambuc ttisgmts[mrudst]);
1797*0a6a1f1dSLionel Sambuc isdsts[mrudst] = 1;
1798*0a6a1f1dSLionel Sambuc writetype[type] = true;
1799f14fb602SLionel Sambuc }
1800f14fb602SLionel Sambuc if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1801f14fb602SLionel Sambuc gmtoffs[histd] != gmtoffs[mrustd]) {
1802f14fb602SLionel Sambuc isdsts[mrustd] = -1;
1803f14fb602SLionel Sambuc type = addtype(gmtoffs[mrustd],
1804f14fb602SLionel Sambuc &chars[abbrinds[mrustd]],
1805*0a6a1f1dSLionel Sambuc false,
1806f14fb602SLionel Sambuc ttisstds[mrustd],
1807f14fb602SLionel Sambuc ttisgmts[mrustd]);
1808*0a6a1f1dSLionel Sambuc isdsts[mrustd] = 0;
1809*0a6a1f1dSLionel Sambuc writetype[type] = true;
1810f14fb602SLionel Sambuc }
1811f14fb602SLionel Sambuc }
1812f14fb602SLionel Sambuc #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
18132fe8fb19SBen Gras thistypecnt = 0;
18142fe8fb19SBen Gras for (i = 0; i < typecnt; ++i)
1815*0a6a1f1dSLionel Sambuc typemap[i] = writetype[i] ? thistypecnt++ : -1;
181684d9c625SLionel Sambuc for (i = 0; i < (int)(sizeof indmap / sizeof indmap[0]); ++i)
18172fe8fb19SBen Gras indmap[i] = -1;
18182fe8fb19SBen Gras thischarcnt = 0;
18192fe8fb19SBen Gras for (i = 0; i < typecnt; ++i) {
182084d9c625SLionel Sambuc char * thisabbr;
18212fe8fb19SBen Gras
18222fe8fb19SBen Gras if (!writetype[i])
18232fe8fb19SBen Gras continue;
18242fe8fb19SBen Gras if (indmap[abbrinds[i]] >= 0)
18252fe8fb19SBen Gras continue;
18262fe8fb19SBen Gras thisabbr = &chars[abbrinds[i]];
18272fe8fb19SBen Gras for (j = 0; j < thischarcnt; ++j)
18282fe8fb19SBen Gras if (strcmp(&thischars[j], thisabbr) == 0)
18292fe8fb19SBen Gras break;
18302fe8fb19SBen Gras if (j == thischarcnt) {
18312fe8fb19SBen Gras (void) strcpy(&thischars[(int) thischarcnt],
18322fe8fb19SBen Gras thisabbr);
18332fe8fb19SBen Gras thischarcnt += strlen(thisabbr) + 1;
18342fe8fb19SBen Gras }
18352fe8fb19SBen Gras indmap[abbrinds[i]] = j;
18362fe8fb19SBen Gras }
183784d9c625SLionel Sambuc #define DO(field) (void) fwrite(tzh.field, \
183884d9c625SLionel Sambuc sizeof tzh.field, (size_t) 1, fp)
18392fe8fb19SBen Gras tzh = tzh0;
18402fe8fb19SBen Gras (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
184184d9c625SLionel Sambuc tzh.tzh_version[0] = version;
184284d9c625SLionel Sambuc convert(thistypecnt, tzh.tzh_ttisgmtcnt);
184384d9c625SLionel Sambuc convert(thistypecnt, tzh.tzh_ttisstdcnt);
184484d9c625SLionel Sambuc convert(thisleapcnt, tzh.tzh_leapcnt);
184584d9c625SLionel Sambuc convert(thistimecnt, tzh.tzh_timecnt);
184684d9c625SLionel Sambuc convert(thistypecnt, tzh.tzh_typecnt);
184784d9c625SLionel Sambuc convert(thischarcnt, tzh.tzh_charcnt);
18482fe8fb19SBen Gras DO(tzh_magic);
18492fe8fb19SBen Gras DO(tzh_version);
18502fe8fb19SBen Gras DO(tzh_reserved);
18512fe8fb19SBen Gras DO(tzh_ttisgmtcnt);
18522fe8fb19SBen Gras DO(tzh_ttisstdcnt);
18532fe8fb19SBen Gras DO(tzh_leapcnt);
18542fe8fb19SBen Gras DO(tzh_timecnt);
18552fe8fb19SBen Gras DO(tzh_typecnt);
18562fe8fb19SBen Gras DO(tzh_charcnt);
18572fe8fb19SBen Gras #undef DO
18582fe8fb19SBen Gras for (i = thistimei; i < thistimelim; ++i)
18592fe8fb19SBen Gras if (pass == 1)
1860*0a6a1f1dSLionel Sambuc /*
1861*0a6a1f1dSLionel Sambuc ** Output an INT32_MIN "transition"
1862*0a6a1f1dSLionel Sambuc ** if appropriate; see above.
1863*0a6a1f1dSLionel Sambuc */
1864*0a6a1f1dSLionel Sambuc puttzcode(((ats[i] < INT32_MIN) ?
1865*0a6a1f1dSLionel Sambuc INT32_MIN : ats[i]), fp);
18662fe8fb19SBen Gras else puttzcode64(ats[i], fp);
18672fe8fb19SBen Gras for (i = thistimei; i < thistimelim; ++i) {
18682fe8fb19SBen Gras unsigned char uc;
18692fe8fb19SBen Gras
18702fe8fb19SBen Gras uc = typemap[types[i]];
187184d9c625SLionel Sambuc (void) fwrite(&uc, sizeof uc, (size_t) 1, fp);
18722fe8fb19SBen Gras }
18732fe8fb19SBen Gras for (i = 0; i < typecnt; ++i)
18742fe8fb19SBen Gras if (writetype[i]) {
18752fe8fb19SBen Gras puttzcode(gmtoffs[i], fp);
18762fe8fb19SBen Gras (void) putc(isdsts[i], fp);
18772fe8fb19SBen Gras (void) putc((unsigned char) indmap[abbrinds[i]], fp);
18782fe8fb19SBen Gras }
18792fe8fb19SBen Gras if (thischarcnt != 0)
188084d9c625SLionel Sambuc (void) fwrite(thischars, sizeof thischars[0],
18812fe8fb19SBen Gras (size_t) thischarcnt, fp);
18822fe8fb19SBen Gras for (i = thisleapi; i < thisleaplim; ++i) {
188384d9c625SLionel Sambuc zic_t todo;
18842fe8fb19SBen Gras
18852fe8fb19SBen Gras if (roll[i]) {
18862fe8fb19SBen Gras if (timecnt == 0 || trans[i] < ats[0]) {
18872fe8fb19SBen Gras j = 0;
18882fe8fb19SBen Gras while (isdsts[j])
18892fe8fb19SBen Gras if (++j >= typecnt) {
18902fe8fb19SBen Gras j = 0;
18912fe8fb19SBen Gras break;
18922fe8fb19SBen Gras }
18932fe8fb19SBen Gras } else {
18942fe8fb19SBen Gras j = 1;
18952fe8fb19SBen Gras while (j < timecnt &&
18962fe8fb19SBen Gras trans[i] >= ats[j])
18972fe8fb19SBen Gras ++j;
18982fe8fb19SBen Gras j = types[j - 1];
18992fe8fb19SBen Gras }
19002fe8fb19SBen Gras todo = tadd(trans[i], -gmtoffs[j]);
19012fe8fb19SBen Gras } else todo = trans[i];
19022fe8fb19SBen Gras if (pass == 1)
190384d9c625SLionel Sambuc puttzcode(todo, fp);
19042fe8fb19SBen Gras else puttzcode64(todo, fp);
19052fe8fb19SBen Gras puttzcode(corr[i], fp);
19062fe8fb19SBen Gras }
19072fe8fb19SBen Gras for (i = 0; i < typecnt; ++i)
19082fe8fb19SBen Gras if (writetype[i])
19092fe8fb19SBen Gras (void) putc(ttisstds[i], fp);
19102fe8fb19SBen Gras for (i = 0; i < typecnt; ++i)
19112fe8fb19SBen Gras if (writetype[i])
19122fe8fb19SBen Gras (void) putc(ttisgmts[i], fp);
19132fe8fb19SBen Gras }
19142fe8fb19SBen Gras (void) fprintf(fp, "\n%s\n", string);
1915*0a6a1f1dSLionel Sambuc close_file(fp, fullname);
1916*0a6a1f1dSLionel Sambuc free(ats);
1917*0a6a1f1dSLionel Sambuc free(fullname);
1918*0a6a1f1dSLionel Sambuc }
1919*0a6a1f1dSLionel Sambuc
1920*0a6a1f1dSLionel Sambuc static char const *
abbroffset(char * buf,zic_t offset)1921*0a6a1f1dSLionel Sambuc abbroffset(char *buf, zic_t offset)
1922*0a6a1f1dSLionel Sambuc {
1923*0a6a1f1dSLionel Sambuc char sign = '+';
1924*0a6a1f1dSLionel Sambuc int seconds, minutes;
1925*0a6a1f1dSLionel Sambuc
1926*0a6a1f1dSLionel Sambuc if (offset < 0) {
1927*0a6a1f1dSLionel Sambuc offset = -offset;
1928*0a6a1f1dSLionel Sambuc sign = '-';
1929*0a6a1f1dSLionel Sambuc }
1930*0a6a1f1dSLionel Sambuc
1931*0a6a1f1dSLionel Sambuc seconds = offset % SECSPERMIN;
1932*0a6a1f1dSLionel Sambuc offset /= SECSPERMIN;
1933*0a6a1f1dSLionel Sambuc minutes = offset % MINSPERHOUR;
1934*0a6a1f1dSLionel Sambuc offset /= MINSPERHOUR;
1935*0a6a1f1dSLionel Sambuc if (100 <= offset) {
1936*0a6a1f1dSLionel Sambuc error(_("%%z UTC offset magnitude exceeds 99:59:59"));
1937*0a6a1f1dSLionel Sambuc return "%z";
1938*0a6a1f1dSLionel Sambuc } else {
1939*0a6a1f1dSLionel Sambuc char *p = buf;
1940*0a6a1f1dSLionel Sambuc *p++ = sign;
1941*0a6a1f1dSLionel Sambuc *p++ = '0' + offset / 10;
1942*0a6a1f1dSLionel Sambuc *p++ = '0' + offset % 10;
1943*0a6a1f1dSLionel Sambuc if (minutes | seconds) {
1944*0a6a1f1dSLionel Sambuc *p++ = '0' + minutes / 10;
1945*0a6a1f1dSLionel Sambuc *p++ = '0' + minutes % 10;
1946*0a6a1f1dSLionel Sambuc if (seconds) {
1947*0a6a1f1dSLionel Sambuc *p++ = '0' + seconds / 10;
1948*0a6a1f1dSLionel Sambuc *p++ = '0' + seconds % 10;
1949*0a6a1f1dSLionel Sambuc }
1950*0a6a1f1dSLionel Sambuc }
1951*0a6a1f1dSLionel Sambuc *p = '\0';
1952*0a6a1f1dSLionel Sambuc return buf;
19532fe8fb19SBen Gras }
19542fe8fb19SBen Gras }
19552fe8fb19SBen Gras
1956*0a6a1f1dSLionel Sambuc static size_t
doabbr(char * abbr,int abbrlen,struct zone const * zp,const char * letters,zic_t stdoff,bool doquotes)1957*0a6a1f1dSLionel Sambuc doabbr(char *abbr, int abbrlen, struct zone const *zp, const char *letters,
1958*0a6a1f1dSLionel Sambuc zic_t stdoff, bool doquotes)
19592fe8fb19SBen Gras {
196084d9c625SLionel Sambuc char * cp;
196184d9c625SLionel Sambuc char * slashp;
1962*0a6a1f1dSLionel Sambuc size_t len;
1963*0a6a1f1dSLionel Sambuc char const *format = zp->z_format;
19642fe8fb19SBen Gras
19652fe8fb19SBen Gras slashp = strchr(format, '/');
19662fe8fb19SBen Gras if (slashp == NULL) {
1967*0a6a1f1dSLionel Sambuc char letterbuf[PERCENT_Z_LEN_BOUND + 1];
1968*0a6a1f1dSLionel Sambuc if (zp->z_format_specifier == 'z')
1969*0a6a1f1dSLionel Sambuc letters = abbroffset(letterbuf, -zp->z_gmtoff + stdoff);
1970*0a6a1f1dSLionel Sambuc else if (!letters)
1971*0a6a1f1dSLionel Sambuc letters = "%s";
1972*0a6a1f1dSLionel Sambuc (void) snprintf(abbr, abbrlen, format, letters);
1973*0a6a1f1dSLionel Sambuc } else if (stdoff != 0) {
19742fe8fb19SBen Gras (void) strlcpy(abbr, slashp + 1, abbrlen);
19752fe8fb19SBen Gras } else {
1976*0a6a1f1dSLionel Sambuc (void) memcpy(abbr, format, slashp - format);
19772fe8fb19SBen Gras abbr[slashp - format] = '\0';
19782fe8fb19SBen Gras }
19792fe8fb19SBen Gras len = strlen(abbr);
1980*0a6a1f1dSLionel Sambuc if (!doquotes)
1981*0a6a1f1dSLionel Sambuc return len;
1982*0a6a1f1dSLionel Sambuc for (cp = abbr; is_alpha(*cp); cp++)
1983*0a6a1f1dSLionel Sambuc continue;
19842fe8fb19SBen Gras if (len > 0 && *cp == '\0')
1985*0a6a1f1dSLionel Sambuc return len;
19862fe8fb19SBen Gras abbr[len + 2] = '\0';
19872fe8fb19SBen Gras abbr[len + 1] = '>';
1988*0a6a1f1dSLionel Sambuc memmove(abbr + 1, abbr, len);
19892fe8fb19SBen Gras abbr[0] = '<';
1990*0a6a1f1dSLionel Sambuc return len + 2;
19912fe8fb19SBen Gras }
19922fe8fb19SBen Gras
19932fe8fb19SBen Gras static void
updateminmax(const zic_t x)199484d9c625SLionel Sambuc updateminmax(const zic_t x)
19952fe8fb19SBen Gras {
19962fe8fb19SBen Gras if (min_year > x)
19972fe8fb19SBen Gras min_year = x;
19982fe8fb19SBen Gras if (max_year < x)
19992fe8fb19SBen Gras max_year = x;
20002fe8fb19SBen Gras }
20012fe8fb19SBen Gras
20022fe8fb19SBen Gras static int
stringoffset(char * result,zic_t offset)200384d9c625SLionel Sambuc stringoffset(char *result, zic_t offset)
20042fe8fb19SBen Gras {
200584d9c625SLionel Sambuc int hours;
200684d9c625SLionel Sambuc int minutes;
200784d9c625SLionel Sambuc int seconds;
2008*0a6a1f1dSLionel Sambuc bool negative = offset < 0;
2009*0a6a1f1dSLionel Sambuc int len = negative;
20102fe8fb19SBen Gras
2011*0a6a1f1dSLionel Sambuc if (negative) {
20122fe8fb19SBen Gras offset = -offset;
2013*0a6a1f1dSLionel Sambuc result[0] = '-';
20142fe8fb19SBen Gras }
20152fe8fb19SBen Gras seconds = offset % SECSPERMIN;
20162fe8fb19SBen Gras offset /= SECSPERMIN;
20172fe8fb19SBen Gras minutes = offset % MINSPERHOUR;
20182fe8fb19SBen Gras offset /= MINSPERHOUR;
20192fe8fb19SBen Gras hours = offset;
202084d9c625SLionel Sambuc if (hours >= HOURSPERDAY * DAYSPERWEEK) {
20212fe8fb19SBen Gras result[0] = '\0';
20222fe8fb19SBen Gras return 0;
20232fe8fb19SBen Gras }
2024*0a6a1f1dSLionel Sambuc len += sprintf(result + len, "%d", hours);
2025*0a6a1f1dSLionel Sambuc if (minutes != 0 || seconds != 0) {
2026*0a6a1f1dSLionel Sambuc len += sprintf(result + len, ":%02d", minutes);
2027*0a6a1f1dSLionel Sambuc if (seconds != 0)
2028*0a6a1f1dSLionel Sambuc len += sprintf(result + len, ":%02d", seconds);
2029*0a6a1f1dSLionel Sambuc }
2030*0a6a1f1dSLionel Sambuc return len;
2031*0a6a1f1dSLionel Sambuc }
20322fe8fb19SBen Gras
20332fe8fb19SBen Gras static int
stringrule(char * result,const struct rule * const rp,const zic_t dstoff,const zic_t gmtoff)203484d9c625SLionel Sambuc stringrule(char *result, const struct rule *const rp, const zic_t dstoff,
203584d9c625SLionel Sambuc const zic_t gmtoff)
20362fe8fb19SBen Gras {
203784d9c625SLionel Sambuc zic_t tod = rp->r_tod;
203884d9c625SLionel Sambuc int compat = 0;
20392fe8fb19SBen Gras
20402fe8fb19SBen Gras if (rp->r_dycode == DC_DOM) {
204184d9c625SLionel Sambuc int month, total;
20422fe8fb19SBen Gras
20432fe8fb19SBen Gras if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
20442fe8fb19SBen Gras return -1;
20452fe8fb19SBen Gras total = 0;
20462fe8fb19SBen Gras for (month = 0; month < rp->r_month; ++month)
20472fe8fb19SBen Gras total += len_months[0][month];
204884d9c625SLionel Sambuc /* Omit the "J" in Jan and Feb, as that's shorter. */
204984d9c625SLionel Sambuc if (rp->r_month <= 1)
2050*0a6a1f1dSLionel Sambuc result += sprintf(result, "%d", total + rp->r_dayofmonth - 1);
205184d9c625SLionel Sambuc else
2052*0a6a1f1dSLionel Sambuc result += sprintf(result, "J%d", total + rp->r_dayofmonth);
20532fe8fb19SBen Gras } else {
205484d9c625SLionel Sambuc int week;
205584d9c625SLionel Sambuc int wday = rp->r_wday;
205684d9c625SLionel Sambuc int wdayoff;
20572fe8fb19SBen Gras
20582fe8fb19SBen Gras if (rp->r_dycode == DC_DOWGEQ) {
205984d9c625SLionel Sambuc wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
206084d9c625SLionel Sambuc if (wdayoff)
206184d9c625SLionel Sambuc compat = 2013;
206284d9c625SLionel Sambuc wday -= wdayoff;
206384d9c625SLionel Sambuc tod += wdayoff * SECSPERDAY;
206484d9c625SLionel Sambuc week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
20652fe8fb19SBen Gras } else if (rp->r_dycode == DC_DOWLEQ) {
20662fe8fb19SBen Gras if (rp->r_dayofmonth == len_months[1][rp->r_month])
20672fe8fb19SBen Gras week = 5;
20682fe8fb19SBen Gras else {
206984d9c625SLionel Sambuc wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
207084d9c625SLionel Sambuc if (wdayoff)
207184d9c625SLionel Sambuc compat = 2013;
207284d9c625SLionel Sambuc wday -= wdayoff;
207384d9c625SLionel Sambuc tod += wdayoff * SECSPERDAY;
2074f14fb602SLionel Sambuc week = rp->r_dayofmonth / DAYSPERWEEK;
20752fe8fb19SBen Gras }
20762fe8fb19SBen Gras } else return -1; /* "cannot happen" */
207784d9c625SLionel Sambuc if (wday < 0)
207884d9c625SLionel Sambuc wday += DAYSPERWEEK;
2079*0a6a1f1dSLionel Sambuc result += sprintf(result, "M%d.%d.%d",
208084d9c625SLionel Sambuc rp->r_month + 1, week, wday);
20812fe8fb19SBen Gras }
20822fe8fb19SBen Gras if (rp->r_todisgmt)
20832fe8fb19SBen Gras tod += gmtoff;
20842fe8fb19SBen Gras if (rp->r_todisstd && rp->r_stdoff == 0)
20852fe8fb19SBen Gras tod += dstoff;
20862fe8fb19SBen Gras if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
2087*0a6a1f1dSLionel Sambuc *result++ = '/';
2088*0a6a1f1dSLionel Sambuc if (! stringoffset(result, tod))
20892fe8fb19SBen Gras return -1;
209084d9c625SLionel Sambuc if (tod < 0) {
209184d9c625SLionel Sambuc if (compat < 2013)
209284d9c625SLionel Sambuc compat = 2013;
209384d9c625SLionel Sambuc } else if (SECSPERDAY <= tod) {
209484d9c625SLionel Sambuc if (compat < 1994)
209584d9c625SLionel Sambuc compat = 1994;
20962fe8fb19SBen Gras }
209784d9c625SLionel Sambuc }
209884d9c625SLionel Sambuc return compat;
20992fe8fb19SBen Gras }
21002fe8fb19SBen Gras
210184d9c625SLionel Sambuc static int
rule_cmp(struct rule const * a,struct rule const * b)210284d9c625SLionel Sambuc rule_cmp(struct rule const *a, struct rule const *b)
21032fe8fb19SBen Gras {
210484d9c625SLionel Sambuc if (!a)
210584d9c625SLionel Sambuc return -!!b;
210684d9c625SLionel Sambuc if (!b)
210784d9c625SLionel Sambuc return 1;
210884d9c625SLionel Sambuc if (a->r_hiyear != b->r_hiyear)
210984d9c625SLionel Sambuc return a->r_hiyear < b->r_hiyear ? -1 : 1;
211084d9c625SLionel Sambuc if (a->r_month - b->r_month != 0)
211184d9c625SLionel Sambuc return a->r_month - b->r_month;
211284d9c625SLionel Sambuc return a->r_dayofmonth - b->r_dayofmonth;
211384d9c625SLionel Sambuc }
211484d9c625SLionel Sambuc
211584d9c625SLionel Sambuc enum { YEAR_BY_YEAR_ZONE = 1 };
211684d9c625SLionel Sambuc
211784d9c625SLionel Sambuc static int
stringzone(char * result,const int resultlen,const struct zone * const zpfirst,const int zonecount)211884d9c625SLionel Sambuc stringzone(char *result, const int resultlen, const struct zone *const zpfirst,
211984d9c625SLionel Sambuc const int zonecount)
212084d9c625SLionel Sambuc {
212184d9c625SLionel Sambuc const struct zone * zp;
212284d9c625SLionel Sambuc struct rule * rp;
212384d9c625SLionel Sambuc struct rule * stdrp;
212484d9c625SLionel Sambuc struct rule * dstrp;
212584d9c625SLionel Sambuc int i;
212684d9c625SLionel Sambuc const char * abbrvar;
212784d9c625SLionel Sambuc int compat = 0;
212884d9c625SLionel Sambuc int c;
2129*0a6a1f1dSLionel Sambuc size_t len;
2130*0a6a1f1dSLionel Sambuc int offsetlen;
213184d9c625SLionel Sambuc struct rule stdr, dstr;
21322fe8fb19SBen Gras
21332fe8fb19SBen Gras result[0] = '\0';
21342fe8fb19SBen Gras zp = zpfirst + zonecount - 1;
21352fe8fb19SBen Gras stdrp = dstrp = NULL;
21362fe8fb19SBen Gras for (i = 0; i < zp->z_nrules; ++i) {
21372fe8fb19SBen Gras rp = &zp->z_rules[i];
213884d9c625SLionel Sambuc if (rp->r_hiwasnum || rp->r_hiyear != ZIC_MAX)
21392fe8fb19SBen Gras continue;
21402fe8fb19SBen Gras if (rp->r_yrtype != NULL)
21412fe8fb19SBen Gras continue;
21422fe8fb19SBen Gras if (rp->r_stdoff == 0) {
21432fe8fb19SBen Gras if (stdrp == NULL)
21442fe8fb19SBen Gras stdrp = rp;
214584d9c625SLionel Sambuc else return -1;
21462fe8fb19SBen Gras } else {
21472fe8fb19SBen Gras if (dstrp == NULL)
21482fe8fb19SBen Gras dstrp = rp;
214984d9c625SLionel Sambuc else return -1;
21502fe8fb19SBen Gras }
21512fe8fb19SBen Gras }
21522fe8fb19SBen Gras if (stdrp == NULL && dstrp == NULL) {
21532fe8fb19SBen Gras /*
21542fe8fb19SBen Gras ** There are no rules running through "max".
215584d9c625SLionel Sambuc ** Find the latest std rule in stdabbrrp
215684d9c625SLionel Sambuc ** and latest rule of any type in stdrp.
21572fe8fb19SBen Gras */
215884d9c625SLionel Sambuc struct rule *stdabbrrp = NULL;
21592fe8fb19SBen Gras for (i = 0; i < zp->z_nrules; ++i) {
21602fe8fb19SBen Gras rp = &zp->z_rules[i];
216184d9c625SLionel Sambuc if (rp->r_stdoff == 0 && rule_cmp(stdabbrrp, rp) < 0)
216284d9c625SLionel Sambuc stdabbrrp = rp;
216384d9c625SLionel Sambuc if (rule_cmp(stdrp, rp) < 0)
21642fe8fb19SBen Gras stdrp = rp;
21652fe8fb19SBen Gras }
21662fe8fb19SBen Gras /*
21672fe8fb19SBen Gras ** Horrid special case: if year is 2037,
21682fe8fb19SBen Gras ** presume this is a zone handled on a year-by-year basis;
21692fe8fb19SBen Gras ** do not try to apply a rule to the zone.
21702fe8fb19SBen Gras */
21712fe8fb19SBen Gras if (stdrp != NULL && stdrp->r_hiyear == 2037)
217284d9c625SLionel Sambuc return YEAR_BY_YEAR_ZONE;
217384d9c625SLionel Sambuc
217484d9c625SLionel Sambuc if (stdrp != NULL && stdrp->r_stdoff != 0) {
217584d9c625SLionel Sambuc /* Perpetual DST. */
217684d9c625SLionel Sambuc dstr.r_month = TM_JANUARY;
217784d9c625SLionel Sambuc dstr.r_dycode = DC_DOM;
217884d9c625SLionel Sambuc dstr.r_dayofmonth = 1;
217984d9c625SLionel Sambuc dstr.r_tod = 0;
2180*0a6a1f1dSLionel Sambuc dstr.r_todisstd = dstr.r_todisgmt = false;
218184d9c625SLionel Sambuc dstr.r_stdoff = stdrp->r_stdoff;
218284d9c625SLionel Sambuc dstr.r_abbrvar = stdrp->r_abbrvar;
218384d9c625SLionel Sambuc stdr.r_month = TM_DECEMBER;
218484d9c625SLionel Sambuc stdr.r_dycode = DC_DOM;
218584d9c625SLionel Sambuc stdr.r_dayofmonth = 31;
218684d9c625SLionel Sambuc stdr.r_tod = SECSPERDAY + stdrp->r_stdoff;
2187*0a6a1f1dSLionel Sambuc stdr.r_todisstd = stdr.r_todisgmt = false;
218884d9c625SLionel Sambuc stdr.r_stdoff = 0;
218984d9c625SLionel Sambuc stdr.r_abbrvar
219084d9c625SLionel Sambuc = (stdabbrrp ? stdabbrrp->r_abbrvar : "");
219184d9c625SLionel Sambuc dstrp = &dstr;
219284d9c625SLionel Sambuc stdrp = &stdr;
219384d9c625SLionel Sambuc }
21942fe8fb19SBen Gras }
21952fe8fb19SBen Gras if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
219684d9c625SLionel Sambuc return -1;
21972fe8fb19SBen Gras abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
2198*0a6a1f1dSLionel Sambuc len = doabbr(result, resultlen, zp, abbrvar, 0, true);
2199*0a6a1f1dSLionel Sambuc offsetlen = stringoffset(result + len, -zp->z_gmtoff);
2200*0a6a1f1dSLionel Sambuc if (! offsetlen) {
22012fe8fb19SBen Gras result[0] = '\0';
220284d9c625SLionel Sambuc return -1;
22032fe8fb19SBen Gras }
2204*0a6a1f1dSLionel Sambuc len += offsetlen;
22052fe8fb19SBen Gras if (dstrp == NULL)
220684d9c625SLionel Sambuc return compat;
2207*0a6a1f1dSLionel Sambuc len += doabbr(result + len, resultlen - len, zp, dstrp->r_abbrvar, dstrp->r_stdoff, true);
2208*0a6a1f1dSLionel Sambuc if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR) {
2209*0a6a1f1dSLionel Sambuc offsetlen = stringoffset(result + len,
2210*0a6a1f1dSLionel Sambuc -(zp->z_gmtoff + dstrp->r_stdoff));
2211*0a6a1f1dSLionel Sambuc if (! offsetlen) {
22122fe8fb19SBen Gras result[0] = '\0';
221384d9c625SLionel Sambuc return -1;
22142fe8fb19SBen Gras }
2215*0a6a1f1dSLionel Sambuc len += offsetlen;
2216*0a6a1f1dSLionel Sambuc }
2217*0a6a1f1dSLionel Sambuc result[len++] = ',';
2218*0a6a1f1dSLionel Sambuc c = stringrule(result + len, dstrp, dstrp->r_stdoff, zp->z_gmtoff);
221984d9c625SLionel Sambuc if (c < 0) {
22202fe8fb19SBen Gras result[0] = '\0';
222184d9c625SLionel Sambuc return -1;
22222fe8fb19SBen Gras }
222384d9c625SLionel Sambuc if (compat < c)
222484d9c625SLionel Sambuc compat = c;
2225*0a6a1f1dSLionel Sambuc len += strlen(result + len);
2226*0a6a1f1dSLionel Sambuc result[len++] = ',';
2227*0a6a1f1dSLionel Sambuc c = stringrule(result + len, stdrp, dstrp->r_stdoff, zp->z_gmtoff);
222884d9c625SLionel Sambuc if (c < 0) {
22292fe8fb19SBen Gras result[0] = '\0';
223084d9c625SLionel Sambuc return -1;
22312fe8fb19SBen Gras }
223284d9c625SLionel Sambuc if (compat < c)
223384d9c625SLionel Sambuc compat = c;
223484d9c625SLionel Sambuc return compat;
22352fe8fb19SBen Gras }
22362fe8fb19SBen Gras
22372fe8fb19SBen Gras static void
outzone(const struct zone * zpfirst,int zonecount)2238*0a6a1f1dSLionel Sambuc outzone(const struct zone *zpfirst, int zonecount)
22392fe8fb19SBen Gras {
224084d9c625SLionel Sambuc const struct zone * zp;
224184d9c625SLionel Sambuc struct rule * rp;
224284d9c625SLionel Sambuc int i, j;
2243*0a6a1f1dSLionel Sambuc bool usestart, useuntil;
224484d9c625SLionel Sambuc zic_t starttime, untiltime;
224584d9c625SLionel Sambuc zic_t gmtoff;
224684d9c625SLionel Sambuc zic_t stdoff;
224784d9c625SLionel Sambuc zic_t year;
224884d9c625SLionel Sambuc zic_t startoff;
2249*0a6a1f1dSLionel Sambuc bool startttisstd;
2250*0a6a1f1dSLionel Sambuc bool startttisgmt;
225184d9c625SLionel Sambuc int type;
225284d9c625SLionel Sambuc char * startbuf;
225384d9c625SLionel Sambuc char * ab;
225484d9c625SLionel Sambuc char * envvar;
225584d9c625SLionel Sambuc size_t max_abbr_len;
225684d9c625SLionel Sambuc size_t max_envvar_len;
2257*0a6a1f1dSLionel Sambuc bool prodstic; /* all rules are min to max */
225884d9c625SLionel Sambuc int compat;
2259*0a6a1f1dSLionel Sambuc bool do_extend;
226084d9c625SLionel Sambuc int version;
22612fe8fb19SBen Gras
22622fe8fb19SBen Gras max_abbr_len = 2 + max_format_len + max_abbrvar_len;
22632fe8fb19SBen Gras max_envvar_len = 2 * max_abbr_len + 5 * 9;
2264*0a6a1f1dSLionel Sambuc startbuf = zic_malloc(max_abbr_len + 1);
2265*0a6a1f1dSLionel Sambuc ab = zic_malloc(max_abbr_len + 1);
2266*0a6a1f1dSLionel Sambuc envvar = zic_malloc(max_envvar_len + 1);
22672fe8fb19SBen Gras INITIALIZE(untiltime);
22682fe8fb19SBen Gras INITIALIZE(starttime);
22692fe8fb19SBen Gras /*
22702fe8fb19SBen Gras ** Now. . .finally. . .generate some useful data!
22712fe8fb19SBen Gras */
22722fe8fb19SBen Gras timecnt = 0;
22732fe8fb19SBen Gras typecnt = 0;
22742fe8fb19SBen Gras charcnt = 0;
2275f14fb602SLionel Sambuc prodstic = zonecount == 1;
22762fe8fb19SBen Gras /*
22772fe8fb19SBen Gras ** Thanks to Earl Chew
22782fe8fb19SBen Gras ** for noting the need to unconditionally initialize startttisstd.
22792fe8fb19SBen Gras */
2280*0a6a1f1dSLionel Sambuc startttisstd = false;
2281*0a6a1f1dSLionel Sambuc startttisgmt = false;
22822fe8fb19SBen Gras min_year = max_year = EPOCH_YEAR;
22832fe8fb19SBen Gras if (leapseen) {
22842fe8fb19SBen Gras updateminmax(leapminyear);
228584d9c625SLionel Sambuc updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
22862fe8fb19SBen Gras }
22872fe8fb19SBen Gras for (i = 0; i < zonecount; ++i) {
22882fe8fb19SBen Gras zp = &zpfirst[i];
22892fe8fb19SBen Gras if (i < zonecount - 1)
22902fe8fb19SBen Gras updateminmax(zp->z_untilrule.r_loyear);
22912fe8fb19SBen Gras for (j = 0; j < zp->z_nrules; ++j) {
22922fe8fb19SBen Gras rp = &zp->z_rules[j];
22932fe8fb19SBen Gras if (rp->r_lowasnum)
22942fe8fb19SBen Gras updateminmax(rp->r_loyear);
22952fe8fb19SBen Gras if (rp->r_hiwasnum)
22962fe8fb19SBen Gras updateminmax(rp->r_hiyear);
229784d9c625SLionel Sambuc if (rp->r_lowasnum || rp->r_hiwasnum)
2298*0a6a1f1dSLionel Sambuc prodstic = false;
22992fe8fb19SBen Gras }
23002fe8fb19SBen Gras }
23012fe8fb19SBen Gras /*
23022fe8fb19SBen Gras ** Generate lots of data if a rule can't cover all future times.
23032fe8fb19SBen Gras */
230484d9c625SLionel Sambuc compat = stringzone(envvar, max_envvar_len + 1, zpfirst, zonecount);
230584d9c625SLionel Sambuc version = compat < 2013 ? ZIC_VERSION_PRE_2013 : ZIC_VERSION;
230684d9c625SLionel Sambuc do_extend = compat < 0 || compat == YEAR_BY_YEAR_ZONE;
2307*0a6a1f1dSLionel Sambuc if (noise) {
2308*0a6a1f1dSLionel Sambuc if (!*envvar)
230984d9c625SLionel Sambuc warning("%s %s",
231084d9c625SLionel Sambuc _("no POSIX environment variable for zone"),
231184d9c625SLionel Sambuc zpfirst->z_name);
2312*0a6a1f1dSLionel Sambuc else if (compat != 0 && compat != YEAR_BY_YEAR_ZONE) {
231384d9c625SLionel Sambuc /* Circa-COMPAT clients, and earlier clients, might
231484d9c625SLionel Sambuc not work for this zone when given dates before
231584d9c625SLionel Sambuc 1970 or after 2038. */
231684d9c625SLionel Sambuc warning(_("%s: pre-%d clients may mishandle"
231784d9c625SLionel Sambuc " distant timestamps"),
231884d9c625SLionel Sambuc zpfirst->z_name, compat);
23192fe8fb19SBen Gras }
232084d9c625SLionel Sambuc }
232184d9c625SLionel Sambuc if (do_extend) {
232284d9c625SLionel Sambuc /*
232384d9c625SLionel Sambuc ** Search through a couple of extra years past the obvious
232484d9c625SLionel Sambuc ** 400, to avoid edge cases. For example, suppose a non-POSIX
232584d9c625SLionel Sambuc ** rule applies from 2012 onwards and has transitions in March
232684d9c625SLionel Sambuc ** and September, plus some one-off transitions in November
232784d9c625SLionel Sambuc ** 2013. If zic looked only at the last 400 years, it would
232884d9c625SLionel Sambuc ** set max_year=2413, with the intent that the 400 years 2014
232984d9c625SLionel Sambuc ** through 2413 will be repeated. The last transition listed
233084d9c625SLionel Sambuc ** in the tzfile would be in 2413-09, less than 400 years
233184d9c625SLionel Sambuc ** after the last one-off transition in 2013-11. Two years
233284d9c625SLionel Sambuc ** might be overkill, but with the kind of edge cases
233384d9c625SLionel Sambuc ** available we're not sure that one year would suffice.
233484d9c625SLionel Sambuc */
233584d9c625SLionel Sambuc enum { years_of_observations = YEARSPERREPEAT + 2 };
233684d9c625SLionel Sambuc
233784d9c625SLionel Sambuc if (min_year >= ZIC_MIN + years_of_observations)
233884d9c625SLionel Sambuc min_year -= years_of_observations;
233984d9c625SLionel Sambuc else min_year = ZIC_MIN;
234084d9c625SLionel Sambuc if (max_year <= ZIC_MAX - years_of_observations)
234184d9c625SLionel Sambuc max_year += years_of_observations;
234284d9c625SLionel Sambuc else max_year = ZIC_MAX;
2343f14fb602SLionel Sambuc /*
2344f14fb602SLionel Sambuc ** Regardless of any of the above,
2345f14fb602SLionel Sambuc ** for a "proDSTic" zone which specifies that its rules
2346f14fb602SLionel Sambuc ** always have and always will be in effect,
2347f14fb602SLionel Sambuc ** we only need one cycle to define the zone.
2348f14fb602SLionel Sambuc */
2349f14fb602SLionel Sambuc if (prodstic) {
2350f14fb602SLionel Sambuc min_year = 1900;
235184d9c625SLionel Sambuc max_year = min_year + years_of_observations;
2352f14fb602SLionel Sambuc }
23532fe8fb19SBen Gras }
23542fe8fb19SBen Gras /*
23552fe8fb19SBen Gras ** For the benefit of older systems,
23562fe8fb19SBen Gras ** generate data from 1900 through 2037.
23572fe8fb19SBen Gras */
23582fe8fb19SBen Gras if (min_year > 1900)
23592fe8fb19SBen Gras min_year = 1900;
23602fe8fb19SBen Gras if (max_year < 2037)
23612fe8fb19SBen Gras max_year = 2037;
23622fe8fb19SBen Gras for (i = 0; i < zonecount; ++i) {
23632fe8fb19SBen Gras /*
23642fe8fb19SBen Gras ** A guess that may well be corrected later.
23652fe8fb19SBen Gras */
23662fe8fb19SBen Gras stdoff = 0;
23672fe8fb19SBen Gras zp = &zpfirst[i];
2368*0a6a1f1dSLionel Sambuc usestart = i > 0 && (zp - 1)->z_untiltime > big_bang_time;
23692fe8fb19SBen Gras useuntil = i < (zonecount - 1);
2370*0a6a1f1dSLionel Sambuc if (useuntil && zp->z_untiltime <= big_bang_time)
23712fe8fb19SBen Gras continue;
23722fe8fb19SBen Gras gmtoff = zp->z_gmtoff;
23732fe8fb19SBen Gras eat(zp->z_filename, zp->z_linenum);
23742fe8fb19SBen Gras *startbuf = '\0';
23752fe8fb19SBen Gras startoff = zp->z_gmtoff;
23762fe8fb19SBen Gras if (zp->z_nrules == 0) {
23772fe8fb19SBen Gras stdoff = zp->z_stdoff;
2378*0a6a1f1dSLionel Sambuc doabbr(startbuf, max_abbr_len + 1, zp,
2379*0a6a1f1dSLionel Sambuc NULL, stdoff, false);
23802fe8fb19SBen Gras type = addtype(oadd(zp->z_gmtoff, stdoff),
23812fe8fb19SBen Gras startbuf, stdoff != 0, startttisstd,
23822fe8fb19SBen Gras startttisgmt);
23832fe8fb19SBen Gras if (usestart) {
23842fe8fb19SBen Gras addtt(starttime, type);
2385*0a6a1f1dSLionel Sambuc usestart = false;
2386*0a6a1f1dSLionel Sambuc } else addtt(big_bang_time, type);
23872fe8fb19SBen Gras } else for (year = min_year; year <= max_year; ++year) {
23882fe8fb19SBen Gras if (useuntil && year > zp->z_untilrule.r_hiyear)
23892fe8fb19SBen Gras break;
23902fe8fb19SBen Gras /*
23912fe8fb19SBen Gras ** Mark which rules to do in the current year.
23922fe8fb19SBen Gras ** For those to do, calculate rpytime(rp, year);
23932fe8fb19SBen Gras */
23942fe8fb19SBen Gras for (j = 0; j < zp->z_nrules; ++j) {
23952fe8fb19SBen Gras rp = &zp->z_rules[j];
23962fe8fb19SBen Gras eats(zp->z_filename, zp->z_linenum,
23972fe8fb19SBen Gras rp->r_filename, rp->r_linenum);
23982fe8fb19SBen Gras rp->r_todo = year >= rp->r_loyear &&
23992fe8fb19SBen Gras year <= rp->r_hiyear &&
24002fe8fb19SBen Gras yearistype(year, rp->r_yrtype);
24012fe8fb19SBen Gras if (rp->r_todo)
24022fe8fb19SBen Gras rp->r_temp = rpytime(rp, year);
24032fe8fb19SBen Gras }
24042fe8fb19SBen Gras for ( ; ; ) {
240584d9c625SLionel Sambuc int k;
240684d9c625SLionel Sambuc zic_t jtime, ktime;
240784d9c625SLionel Sambuc zic_t offset;
24082fe8fb19SBen Gras
24092fe8fb19SBen Gras INITIALIZE(ktime);
24102fe8fb19SBen Gras if (useuntil) {
24112fe8fb19SBen Gras /*
241284d9c625SLionel Sambuc ** Turn untiltime into UT
24132fe8fb19SBen Gras ** assuming the current gmtoff and
24142fe8fb19SBen Gras ** stdoff values.
24152fe8fb19SBen Gras */
24162fe8fb19SBen Gras untiltime = zp->z_untiltime;
24172fe8fb19SBen Gras if (!zp->z_untilrule.r_todisgmt)
24182fe8fb19SBen Gras untiltime = tadd(untiltime,
24192fe8fb19SBen Gras -gmtoff);
24202fe8fb19SBen Gras if (!zp->z_untilrule.r_todisstd)
24212fe8fb19SBen Gras untiltime = tadd(untiltime,
24222fe8fb19SBen Gras -stdoff);
24232fe8fb19SBen Gras }
24242fe8fb19SBen Gras /*
24252fe8fb19SBen Gras ** Find the rule (of those to do, if any)
24262fe8fb19SBen Gras ** that takes effect earliest in the year.
24272fe8fb19SBen Gras */
24282fe8fb19SBen Gras k = -1;
24292fe8fb19SBen Gras for (j = 0; j < zp->z_nrules; ++j) {
24302fe8fb19SBen Gras rp = &zp->z_rules[j];
24312fe8fb19SBen Gras if (!rp->r_todo)
24322fe8fb19SBen Gras continue;
24332fe8fb19SBen Gras eats(zp->z_filename, zp->z_linenum,
24342fe8fb19SBen Gras rp->r_filename, rp->r_linenum);
24352fe8fb19SBen Gras offset = rp->r_todisgmt ? 0 : gmtoff;
24362fe8fb19SBen Gras if (!rp->r_todisstd)
24372fe8fb19SBen Gras offset = oadd(offset, stdoff);
24382fe8fb19SBen Gras jtime = rp->r_temp;
24392fe8fb19SBen Gras if (jtime == min_time ||
24402fe8fb19SBen Gras jtime == max_time)
24412fe8fb19SBen Gras continue;
24422fe8fb19SBen Gras jtime = tadd(jtime, -offset);
24432fe8fb19SBen Gras if (k < 0 || jtime < ktime) {
24442fe8fb19SBen Gras k = j;
24452fe8fb19SBen Gras ktime = jtime;
2446*0a6a1f1dSLionel Sambuc } else if (jtime == ktime) {
2447*0a6a1f1dSLionel Sambuc char const *dup_rules_msg =
2448*0a6a1f1dSLionel Sambuc _("two rules for same instant");
2449*0a6a1f1dSLionel Sambuc eats(zp->z_filename, zp->z_linenum,
2450*0a6a1f1dSLionel Sambuc rp->r_filename, rp->r_linenum);
2451*0a6a1f1dSLionel Sambuc warning("%s", dup_rules_msg);
2452*0a6a1f1dSLionel Sambuc rp = &zp->z_rules[k];
2453*0a6a1f1dSLionel Sambuc eats(zp->z_filename, zp->z_linenum,
2454*0a6a1f1dSLionel Sambuc rp->r_filename, rp->r_linenum);
2455*0a6a1f1dSLionel Sambuc error("%s", dup_rules_msg);
24562fe8fb19SBen Gras }
24572fe8fb19SBen Gras }
24582fe8fb19SBen Gras if (k < 0)
24592fe8fb19SBen Gras break; /* go on to next year */
24602fe8fb19SBen Gras rp = &zp->z_rules[k];
2461*0a6a1f1dSLionel Sambuc rp->r_todo = false;
24622fe8fb19SBen Gras if (useuntil && ktime >= untiltime)
24632fe8fb19SBen Gras break;
24642fe8fb19SBen Gras stdoff = rp->r_stdoff;
24652fe8fb19SBen Gras if (usestart && ktime == starttime)
2466*0a6a1f1dSLionel Sambuc usestart = false;
24672fe8fb19SBen Gras if (usestart) {
24682fe8fb19SBen Gras if (ktime < starttime) {
24692fe8fb19SBen Gras startoff = oadd(zp->z_gmtoff,
24702fe8fb19SBen Gras stdoff);
24712fe8fb19SBen Gras doabbr(startbuf,
24722fe8fb19SBen Gras max_abbr_len + 1,
2473*0a6a1f1dSLionel Sambuc zp,
24742fe8fb19SBen Gras rp->r_abbrvar,
2475*0a6a1f1dSLionel Sambuc rp->r_stdoff,
2476*0a6a1f1dSLionel Sambuc false);
24772fe8fb19SBen Gras continue;
24782fe8fb19SBen Gras }
24792fe8fb19SBen Gras if (*startbuf == '\0' &&
24802fe8fb19SBen Gras startoff == oadd(zp->z_gmtoff,
24812fe8fb19SBen Gras stdoff)) {
24822fe8fb19SBen Gras doabbr(startbuf,
24832fe8fb19SBen Gras max_abbr_len + 1,
2484*0a6a1f1dSLionel Sambuc zp,
24852fe8fb19SBen Gras rp->r_abbrvar,
2486*0a6a1f1dSLionel Sambuc rp->r_stdoff,
2487*0a6a1f1dSLionel Sambuc false);
24882fe8fb19SBen Gras }
24892fe8fb19SBen Gras }
24902fe8fb19SBen Gras eats(zp->z_filename, zp->z_linenum,
24912fe8fb19SBen Gras rp->r_filename, rp->r_linenum);
2492*0a6a1f1dSLionel Sambuc doabbr(ab, max_abbr_len + 1, zp, rp->r_abbrvar,
2493*0a6a1f1dSLionel Sambuc rp->r_stdoff, false);
24942fe8fb19SBen Gras offset = oadd(zp->z_gmtoff, rp->r_stdoff);
24952fe8fb19SBen Gras type = addtype(offset, ab, rp->r_stdoff != 0,
24962fe8fb19SBen Gras rp->r_todisstd, rp->r_todisgmt);
24972fe8fb19SBen Gras addtt(ktime, type);
24982fe8fb19SBen Gras }
24992fe8fb19SBen Gras }
25002fe8fb19SBen Gras if (usestart) {
25012fe8fb19SBen Gras if (*startbuf == '\0' &&
25022fe8fb19SBen Gras zp->z_format != NULL &&
25032fe8fb19SBen Gras strchr(zp->z_format, '%') == NULL &&
25042fe8fb19SBen Gras strchr(zp->z_format, '/') == NULL)
25052fe8fb19SBen Gras (void)strncpy(startbuf, zp->z_format,
25062fe8fb19SBen Gras max_abbr_len + 1 - 1);
25072fe8fb19SBen Gras eat(zp->z_filename, zp->z_linenum);
25082fe8fb19SBen Gras if (*startbuf == '\0')
25092fe8fb19SBen Gras error(_("can't determine time zone abbreviation to use just after until time"));
25102fe8fb19SBen Gras else addtt(starttime,
25112fe8fb19SBen Gras addtype(startoff, startbuf,
25122fe8fb19SBen Gras startoff != zp->z_gmtoff,
25132fe8fb19SBen Gras startttisstd,
25142fe8fb19SBen Gras startttisgmt));
25152fe8fb19SBen Gras }
25162fe8fb19SBen Gras /*
25172fe8fb19SBen Gras ** Now we may get to set starttime for the next zone line.
25182fe8fb19SBen Gras */
25192fe8fb19SBen Gras if (useuntil) {
25202fe8fb19SBen Gras startttisstd = zp->z_untilrule.r_todisstd;
25212fe8fb19SBen Gras startttisgmt = zp->z_untilrule.r_todisgmt;
25222fe8fb19SBen Gras starttime = zp->z_untiltime;
25232fe8fb19SBen Gras if (!startttisstd)
25242fe8fb19SBen Gras starttime = tadd(starttime, -stdoff);
25252fe8fb19SBen Gras if (!startttisgmt)
25262fe8fb19SBen Gras starttime = tadd(starttime, -gmtoff);
25272fe8fb19SBen Gras }
25282fe8fb19SBen Gras }
252984d9c625SLionel Sambuc if (do_extend) {
253084d9c625SLionel Sambuc /*
253184d9c625SLionel Sambuc ** If we're extending the explicitly listed observations
253284d9c625SLionel Sambuc ** for 400 years because we can't fill the POSIX-TZ field,
253384d9c625SLionel Sambuc ** check whether we actually ended up explicitly listing
253484d9c625SLionel Sambuc ** observations through that period. If there aren't any
253584d9c625SLionel Sambuc ** near the end of the 400-year period, add a redundant
253684d9c625SLionel Sambuc ** one at the end of the final year, to make it clear
253784d9c625SLionel Sambuc ** that we are claiming to have definite knowledge of
253884d9c625SLionel Sambuc ** the lack of transitions up to that point.
253984d9c625SLionel Sambuc */
254084d9c625SLionel Sambuc struct rule xr;
254184d9c625SLionel Sambuc struct attype *lastat;
254284d9c625SLionel Sambuc xr.r_month = TM_JANUARY;
254384d9c625SLionel Sambuc xr.r_dycode = DC_DOM;
254484d9c625SLionel Sambuc xr.r_dayofmonth = 1;
254584d9c625SLionel Sambuc xr.r_tod = 0;
254684d9c625SLionel Sambuc for (lastat = &attypes[0], i = 1; i < timecnt; i++)
254784d9c625SLionel Sambuc if (attypes[i].at > lastat->at)
254884d9c625SLionel Sambuc lastat = &attypes[i];
254984d9c625SLionel Sambuc if (lastat->at < rpytime(&xr, max_year - 1)) {
255084d9c625SLionel Sambuc /*
255184d9c625SLionel Sambuc ** Create new type code for the redundant entry,
2552*0a6a1f1dSLionel Sambuc ** to prevent it being optimized away.
255384d9c625SLionel Sambuc */
255484d9c625SLionel Sambuc if (typecnt >= TZ_MAX_TYPES) {
255584d9c625SLionel Sambuc error(_("too many local time types"));
255684d9c625SLionel Sambuc exit(EXIT_FAILURE);
255784d9c625SLionel Sambuc }
255884d9c625SLionel Sambuc gmtoffs[typecnt] = gmtoffs[lastat->type];
255984d9c625SLionel Sambuc isdsts[typecnt] = isdsts[lastat->type];
256084d9c625SLionel Sambuc ttisstds[typecnt] = ttisstds[lastat->type];
256184d9c625SLionel Sambuc ttisgmts[typecnt] = ttisgmts[lastat->type];
256284d9c625SLionel Sambuc abbrinds[typecnt] = abbrinds[lastat->type];
256384d9c625SLionel Sambuc ++typecnt;
256484d9c625SLionel Sambuc addtt(rpytime(&xr, max_year + 1), typecnt-1);
256584d9c625SLionel Sambuc }
256684d9c625SLionel Sambuc }
256784d9c625SLionel Sambuc writezone(zpfirst->z_name, envvar, version);
256884d9c625SLionel Sambuc free(startbuf);
256984d9c625SLionel Sambuc free(ab);
257084d9c625SLionel Sambuc free(envvar);
25712fe8fb19SBen Gras }
25722fe8fb19SBen Gras
25732fe8fb19SBen Gras static void
addtt(zic_t starttime,int type)2574*0a6a1f1dSLionel Sambuc addtt(zic_t starttime, int type)
25752fe8fb19SBen Gras {
2576*0a6a1f1dSLionel Sambuc if (starttime <= big_bang_time ||
2577*0a6a1f1dSLionel Sambuc (timecnt == 1 && attypes[0].at < big_bang_time)) {
25782fe8fb19SBen Gras gmtoffs[0] = gmtoffs[type];
25792fe8fb19SBen Gras isdsts[0] = isdsts[type];
25802fe8fb19SBen Gras ttisstds[0] = ttisstds[type];
25812fe8fb19SBen Gras ttisgmts[0] = ttisgmts[type];
25822fe8fb19SBen Gras if (abbrinds[type] != 0)
2583*0a6a1f1dSLionel Sambuc strcpy(chars, &chars[abbrinds[type]]);
25842fe8fb19SBen Gras abbrinds[0] = 0;
25852fe8fb19SBen Gras charcnt = strlen(chars) + 1;
25862fe8fb19SBen Gras typecnt = 1;
25872fe8fb19SBen Gras timecnt = 0;
25882fe8fb19SBen Gras type = 0;
25892fe8fb19SBen Gras }
2590*0a6a1f1dSLionel Sambuc attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
25912fe8fb19SBen Gras attypes[timecnt].at = starttime;
25922fe8fb19SBen Gras attypes[timecnt].type = type;
25932fe8fb19SBen Gras ++timecnt;
25942fe8fb19SBen Gras }
25952fe8fb19SBen Gras
25962fe8fb19SBen Gras static int
addtype(zic_t gmtoff,char * const abbr,bool isdst,bool ttisstd,bool ttisgmt)2597*0a6a1f1dSLionel Sambuc addtype(zic_t gmtoff, char *const abbr, bool isdst, bool ttisstd, bool ttisgmt)
25982fe8fb19SBen Gras {
259984d9c625SLionel Sambuc int i, j;
26002fe8fb19SBen Gras
26012fe8fb19SBen Gras /*
26022fe8fb19SBen Gras ** See if there's already an entry for this zone type.
26032fe8fb19SBen Gras ** If so, just return its index.
26042fe8fb19SBen Gras */
26052fe8fb19SBen Gras for (i = 0; i < typecnt; ++i) {
26062fe8fb19SBen Gras if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
26072fe8fb19SBen Gras strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
26082fe8fb19SBen Gras ttisstd == ttisstds[i] &&
26092fe8fb19SBen Gras ttisgmt == ttisgmts[i])
26102fe8fb19SBen Gras return i;
26112fe8fb19SBen Gras }
26122fe8fb19SBen Gras /*
26132fe8fb19SBen Gras ** There isn't one; add a new one, unless there are already too
26142fe8fb19SBen Gras ** many.
26152fe8fb19SBen Gras */
26162fe8fb19SBen Gras if (typecnt >= TZ_MAX_TYPES) {
26172fe8fb19SBen Gras error(_("too many local time types"));
26182fe8fb19SBen Gras exit(EXIT_FAILURE);
26192fe8fb19SBen Gras }
26202fe8fb19SBen Gras if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
262184d9c625SLionel Sambuc error(_("UT offset out of range"));
26222fe8fb19SBen Gras exit(EXIT_FAILURE);
26232fe8fb19SBen Gras }
26242fe8fb19SBen Gras gmtoffs[i] = gmtoff;
26252fe8fb19SBen Gras isdsts[i] = isdst;
26262fe8fb19SBen Gras ttisstds[i] = ttisstd;
26272fe8fb19SBen Gras ttisgmts[i] = ttisgmt;
26282fe8fb19SBen Gras
26292fe8fb19SBen Gras for (j = 0; j < charcnt; ++j)
26302fe8fb19SBen Gras if (strcmp(&chars[j], abbr) == 0)
26312fe8fb19SBen Gras break;
26322fe8fb19SBen Gras if (j == charcnt)
26332fe8fb19SBen Gras newabbr(abbr);
26342fe8fb19SBen Gras abbrinds[i] = j;
26352fe8fb19SBen Gras ++typecnt;
26362fe8fb19SBen Gras return i;
26372fe8fb19SBen Gras }
26382fe8fb19SBen Gras
26392fe8fb19SBen Gras static void
leapadd(zic_t t,bool positive,int rolling,int count)2640*0a6a1f1dSLionel Sambuc leapadd(zic_t t, bool positive, int rolling, int count)
26412fe8fb19SBen Gras {
264284d9c625SLionel Sambuc int i, j;
26432fe8fb19SBen Gras
26442fe8fb19SBen Gras if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
26452fe8fb19SBen Gras error(_("too many leap seconds"));
26462fe8fb19SBen Gras exit(EXIT_FAILURE);
26472fe8fb19SBen Gras }
26482fe8fb19SBen Gras for (i = 0; i < leapcnt; ++i)
26492fe8fb19SBen Gras if (t <= trans[i]) {
26502fe8fb19SBen Gras if (t == trans[i]) {
26512fe8fb19SBen Gras error(_("repeated leap second moment"));
26522fe8fb19SBen Gras exit(EXIT_FAILURE);
26532fe8fb19SBen Gras }
26542fe8fb19SBen Gras break;
26552fe8fb19SBen Gras }
26562fe8fb19SBen Gras do {
26572fe8fb19SBen Gras for (j = leapcnt; j > i; --j) {
26582fe8fb19SBen Gras trans[j] = trans[j - 1];
26592fe8fb19SBen Gras corr[j] = corr[j - 1];
26602fe8fb19SBen Gras roll[j] = roll[j - 1];
26612fe8fb19SBen Gras }
26622fe8fb19SBen Gras trans[i] = t;
266384d9c625SLionel Sambuc corr[i] = positive ? 1 : -count;
26642fe8fb19SBen Gras roll[i] = rolling;
26652fe8fb19SBen Gras ++leapcnt;
26662fe8fb19SBen Gras } while (positive && --count != 0);
26672fe8fb19SBen Gras }
26682fe8fb19SBen Gras
26692fe8fb19SBen Gras static void
adjleap(void)26702fe8fb19SBen Gras adjleap(void)
26712fe8fb19SBen Gras {
267284d9c625SLionel Sambuc int i;
267384d9c625SLionel Sambuc zic_t last = 0;
26742fe8fb19SBen Gras
26752fe8fb19SBen Gras /*
26762fe8fb19SBen Gras ** propagate leap seconds forward
26772fe8fb19SBen Gras */
26782fe8fb19SBen Gras for (i = 0; i < leapcnt; ++i) {
26792fe8fb19SBen Gras trans[i] = tadd(trans[i], last);
26802fe8fb19SBen Gras last = corr[i] += last;
26812fe8fb19SBen Gras }
26822fe8fb19SBen Gras }
26832fe8fb19SBen Gras
2684*0a6a1f1dSLionel Sambuc static bool
yearistype(int year,const char * type)2685*0a6a1f1dSLionel Sambuc yearistype(int year, const char *type)
26862fe8fb19SBen Gras {
26872fe8fb19SBen Gras static char * buf;
26882fe8fb19SBen Gras int result;
26892fe8fb19SBen Gras
26902fe8fb19SBen Gras if (type == NULL || *type == '\0')
2691*0a6a1f1dSLionel Sambuc return true;
2692*0a6a1f1dSLionel Sambuc buf = zic_realloc(buf, 132 + strlen(yitcommand) + strlen(type));
26932fe8fb19SBen Gras (void)sprintf(buf, "%s %d %s", yitcommand, year, type); /* XXX: sprintf is safe */
26942fe8fb19SBen Gras result = system(buf);
26952fe8fb19SBen Gras if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
26962fe8fb19SBen Gras case 0:
2697*0a6a1f1dSLionel Sambuc return true;
26982fe8fb19SBen Gras case 1:
2699*0a6a1f1dSLionel Sambuc return false;
27002fe8fb19SBen Gras }
27012fe8fb19SBen Gras error(_("Wild result from command execution"));
27022fe8fb19SBen Gras (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
27032fe8fb19SBen Gras progname, buf, result);
27042fe8fb19SBen Gras for ( ; ; )
27052fe8fb19SBen Gras exit(EXIT_FAILURE);
27062fe8fb19SBen Gras }
27072fe8fb19SBen Gras
2708*0a6a1f1dSLionel Sambuc /* Is A a space character in the C locale? */
2709*0a6a1f1dSLionel Sambuc static bool
is_space(char a)2710*0a6a1f1dSLionel Sambuc is_space(char a)
27112fe8fb19SBen Gras {
2712*0a6a1f1dSLionel Sambuc switch (a) {
2713*0a6a1f1dSLionel Sambuc default:
2714*0a6a1f1dSLionel Sambuc return false;
2715*0a6a1f1dSLionel Sambuc case ' ': case '\f': case '\n': case '\r': case '\t': case '\v':
2716*0a6a1f1dSLionel Sambuc return true;
2717*0a6a1f1dSLionel Sambuc }
2718*0a6a1f1dSLionel Sambuc }
2719*0a6a1f1dSLionel Sambuc
2720*0a6a1f1dSLionel Sambuc /* Is A an alphabetic character in the C locale? */
2721*0a6a1f1dSLionel Sambuc static bool
is_alpha(char a)2722*0a6a1f1dSLionel Sambuc is_alpha(char a)
2723*0a6a1f1dSLionel Sambuc {
2724*0a6a1f1dSLionel Sambuc switch (a) {
2725*0a6a1f1dSLionel Sambuc default:
2726*0a6a1f1dSLionel Sambuc return 0;
2727*0a6a1f1dSLionel Sambuc case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
2728*0a6a1f1dSLionel Sambuc case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
2729*0a6a1f1dSLionel Sambuc case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
2730*0a6a1f1dSLionel Sambuc case 'V': case 'W': case 'X': case 'Y': case 'Z':
2731*0a6a1f1dSLionel Sambuc case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2732*0a6a1f1dSLionel Sambuc case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
2733*0a6a1f1dSLionel Sambuc case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
2734*0a6a1f1dSLionel Sambuc case 'v': case 'w': case 'x': case 'y': case 'z':
2735*0a6a1f1dSLionel Sambuc return true;
2736*0a6a1f1dSLionel Sambuc }
2737*0a6a1f1dSLionel Sambuc }
2738*0a6a1f1dSLionel Sambuc
2739*0a6a1f1dSLionel Sambuc /* If A is an uppercase character in the C locale, return its lowercase
2740*0a6a1f1dSLionel Sambuc counterpart. Otherwise, return A. */
2741*0a6a1f1dSLionel Sambuc static char
lowerit(char a)2742*0a6a1f1dSLionel Sambuc lowerit(char a)
2743*0a6a1f1dSLionel Sambuc {
2744*0a6a1f1dSLionel Sambuc switch (a) {
2745*0a6a1f1dSLionel Sambuc default: return a;
2746*0a6a1f1dSLionel Sambuc case 'A': return 'a'; case 'B': return 'b'; case 'C': return 'c';
2747*0a6a1f1dSLionel Sambuc case 'D': return 'd'; case 'E': return 'e'; case 'F': return 'f';
2748*0a6a1f1dSLionel Sambuc case 'G': return 'g'; case 'H': return 'h'; case 'I': return 'i';
2749*0a6a1f1dSLionel Sambuc case 'J': return 'j'; case 'K': return 'k'; case 'L': return 'l';
2750*0a6a1f1dSLionel Sambuc case 'M': return 'm'; case 'N': return 'n'; case 'O': return 'o';
2751*0a6a1f1dSLionel Sambuc case 'P': return 'p'; case 'Q': return 'q'; case 'R': return 'r';
2752*0a6a1f1dSLionel Sambuc case 'S': return 's'; case 'T': return 't'; case 'U': return 'u';
2753*0a6a1f1dSLionel Sambuc case 'V': return 'v'; case 'W': return 'w'; case 'X': return 'x';
2754*0a6a1f1dSLionel Sambuc case 'Y': return 'y'; case 'Z': return 'z';
2755*0a6a1f1dSLionel Sambuc }
27562fe8fb19SBen Gras }
27572fe8fb19SBen Gras
275884d9c625SLionel Sambuc /* case-insensitive equality */
2759*0a6a1f1dSLionel Sambuc static ATTRIBUTE_PURE bool
ciequal(const char * ap,const char * bp)276084d9c625SLionel Sambuc ciequal(const char *ap, const char *bp)
27612fe8fb19SBen Gras {
27622fe8fb19SBen Gras while (lowerit(*ap) == lowerit(*bp++))
27632fe8fb19SBen Gras if (*ap++ == '\0')
2764*0a6a1f1dSLionel Sambuc return true;
2765*0a6a1f1dSLionel Sambuc return false;
27662fe8fb19SBen Gras }
27672fe8fb19SBen Gras
2768*0a6a1f1dSLionel Sambuc static ATTRIBUTE_PURE bool
itsabbr(const char * abbr,const char * word)276984d9c625SLionel Sambuc itsabbr(const char *abbr, const char *word)
27702fe8fb19SBen Gras {
27712fe8fb19SBen Gras if (lowerit(*abbr) != lowerit(*word))
2772*0a6a1f1dSLionel Sambuc return false;
27732fe8fb19SBen Gras ++word;
27742fe8fb19SBen Gras while (*++abbr != '\0')
27752fe8fb19SBen Gras do {
27762fe8fb19SBen Gras if (*word == '\0')
2777*0a6a1f1dSLionel Sambuc return false;
27782fe8fb19SBen Gras } while (lowerit(*word++) != lowerit(*abbr));
2779*0a6a1f1dSLionel Sambuc return true;
27802fe8fb19SBen Gras }
27812fe8fb19SBen Gras
278284d9c625SLionel Sambuc static ATTRIBUTE_PURE const struct lookup *
byword(const char * word,const struct lookup * table)2783*0a6a1f1dSLionel Sambuc byword(const char *word, const struct lookup *table)
27842fe8fb19SBen Gras {
278584d9c625SLionel Sambuc const struct lookup * foundlp;
278684d9c625SLionel Sambuc const struct lookup * lp;
27872fe8fb19SBen Gras
27882fe8fb19SBen Gras if (word == NULL || table == NULL)
27892fe8fb19SBen Gras return NULL;
27902fe8fb19SBen Gras /*
27912fe8fb19SBen Gras ** Look for exact match.
27922fe8fb19SBen Gras */
27932fe8fb19SBen Gras for (lp = table; lp->l_word != NULL; ++lp)
27942fe8fb19SBen Gras if (ciequal(word, lp->l_word))
27952fe8fb19SBen Gras return lp;
27962fe8fb19SBen Gras /*
27972fe8fb19SBen Gras ** Look for inexact match.
27982fe8fb19SBen Gras */
27992fe8fb19SBen Gras foundlp = NULL;
28002fe8fb19SBen Gras for (lp = table; lp->l_word != NULL; ++lp)
28012fe8fb19SBen Gras if (itsabbr(word, lp->l_word)) {
28022fe8fb19SBen Gras if (foundlp == NULL)
28032fe8fb19SBen Gras foundlp = lp;
28042fe8fb19SBen Gras else return NULL; /* multiple inexact matches */
28052fe8fb19SBen Gras }
28062fe8fb19SBen Gras return foundlp;
28072fe8fb19SBen Gras }
28082fe8fb19SBen Gras
28092fe8fb19SBen Gras static char **
getfields(char * cp)281084d9c625SLionel Sambuc getfields(char *cp)
28112fe8fb19SBen Gras {
281284d9c625SLionel Sambuc char * dp;
281384d9c625SLionel Sambuc char ** array;
281484d9c625SLionel Sambuc int nsubs;
28152fe8fb19SBen Gras
28162fe8fb19SBen Gras if (cp == NULL)
28172fe8fb19SBen Gras return NULL;
2818*0a6a1f1dSLionel Sambuc array = zic_malloc(size_product(strlen(cp) + 1, sizeof *array));
28192fe8fb19SBen Gras nsubs = 0;
28202fe8fb19SBen Gras for ( ; ; ) {
2821*0a6a1f1dSLionel Sambuc while (is_space(*cp))
28222fe8fb19SBen Gras ++cp;
28232fe8fb19SBen Gras if (*cp == '\0' || *cp == '#')
28242fe8fb19SBen Gras break;
28252fe8fb19SBen Gras array[nsubs++] = dp = cp;
28262fe8fb19SBen Gras do {
28272fe8fb19SBen Gras if ((*dp = *cp++) != '"')
28282fe8fb19SBen Gras ++dp;
28292fe8fb19SBen Gras else while ((*dp = *cp++) != '"')
28302fe8fb19SBen Gras if (*dp != '\0')
28312fe8fb19SBen Gras ++dp;
28322fe8fb19SBen Gras else {
28332fe8fb19SBen Gras error(_(
28342fe8fb19SBen Gras "Odd number of quotation marks"
28352fe8fb19SBen Gras ));
28362fe8fb19SBen Gras exit(1);
28372fe8fb19SBen Gras }
2838*0a6a1f1dSLionel Sambuc } while (*cp && *cp != '#' && !is_space(*cp));
2839*0a6a1f1dSLionel Sambuc if (is_space(*cp))
28402fe8fb19SBen Gras ++cp;
28412fe8fb19SBen Gras *dp = '\0';
28422fe8fb19SBen Gras }
28432fe8fb19SBen Gras array[nsubs] = NULL;
28442fe8fb19SBen Gras return array;
28452fe8fb19SBen Gras }
28462fe8fb19SBen Gras
2847*0a6a1f1dSLionel Sambuc static _Noreturn void
time_overflow(void)2848*0a6a1f1dSLionel Sambuc time_overflow(void)
28492fe8fb19SBen Gras {
28502fe8fb19SBen Gras error(_("time overflow"));
28512fe8fb19SBen Gras exit(EXIT_FAILURE);
28522fe8fb19SBen Gras }
2853*0a6a1f1dSLionel Sambuc
2854*0a6a1f1dSLionel Sambuc static ATTRIBUTE_PURE zic_t
oadd(zic_t t1,zic_t t2)2855*0a6a1f1dSLionel Sambuc oadd(zic_t t1, zic_t t2)
2856*0a6a1f1dSLionel Sambuc {
2857*0a6a1f1dSLionel Sambuc if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2)
2858*0a6a1f1dSLionel Sambuc time_overflow();
285984d9c625SLionel Sambuc return t1 + t2;
28602fe8fb19SBen Gras }
28612fe8fb19SBen Gras
286284d9c625SLionel Sambuc static ATTRIBUTE_PURE zic_t
tadd(zic_t t1,zic_t t2)2863*0a6a1f1dSLionel Sambuc tadd(zic_t t1, zic_t t2)
28642fe8fb19SBen Gras {
2865*0a6a1f1dSLionel Sambuc if (t1 < 0) {
2866*0a6a1f1dSLionel Sambuc if (t2 < min_time - t1) {
2867*0a6a1f1dSLionel Sambuc if (t1 != min_time)
2868*0a6a1f1dSLionel Sambuc time_overflow();
28692fe8fb19SBen Gras return min_time;
2870*0a6a1f1dSLionel Sambuc }
2871*0a6a1f1dSLionel Sambuc } else {
2872*0a6a1f1dSLionel Sambuc if (max_time - t1 < t2) {
2873*0a6a1f1dSLionel Sambuc if (t1 != max_time)
2874*0a6a1f1dSLionel Sambuc time_overflow();
2875*0a6a1f1dSLionel Sambuc return max_time;
2876*0a6a1f1dSLionel Sambuc }
28772fe8fb19SBen Gras }
287884d9c625SLionel Sambuc return t1 + t2;
28792fe8fb19SBen Gras }
28802fe8fb19SBen Gras
28812fe8fb19SBen Gras /*
2882*0a6a1f1dSLionel Sambuc ** Given a rule, and a year, compute the date (in seconds since January 1,
2883*0a6a1f1dSLionel Sambuc ** 1970, 00:00 LOCAL time) in that year that the rule refers to.
28842fe8fb19SBen Gras */
28852fe8fb19SBen Gras
28862fe8fb19SBen Gras static zic_t
rpytime(const struct rule * rp,zic_t wantedy)2887*0a6a1f1dSLionel Sambuc rpytime(const struct rule *rp, zic_t wantedy)
28882fe8fb19SBen Gras {
288984d9c625SLionel Sambuc int m, i;
289084d9c625SLionel Sambuc zic_t dayoff; /* with a nod to Margaret O. */
289184d9c625SLionel Sambuc zic_t t, y;
28922fe8fb19SBen Gras
289384d9c625SLionel Sambuc if (wantedy == ZIC_MIN)
28942fe8fb19SBen Gras return min_time;
289584d9c625SLionel Sambuc if (wantedy == ZIC_MAX)
28962fe8fb19SBen Gras return max_time;
28972fe8fb19SBen Gras dayoff = 0;
28982fe8fb19SBen Gras m = TM_JANUARY;
28992fe8fb19SBen Gras y = EPOCH_YEAR;
29002fe8fb19SBen Gras while (wantedy != y) {
29012fe8fb19SBen Gras if (wantedy > y) {
29022fe8fb19SBen Gras i = len_years[isleap(y)];
29032fe8fb19SBen Gras ++y;
29042fe8fb19SBen Gras } else {
29052fe8fb19SBen Gras --y;
29062fe8fb19SBen Gras i = -len_years[isleap(y)];
29072fe8fb19SBen Gras }
290884d9c625SLionel Sambuc dayoff = oadd(dayoff, i);
29092fe8fb19SBen Gras }
29102fe8fb19SBen Gras while (m != rp->r_month) {
29112fe8fb19SBen Gras i = len_months[isleap(y)][m];
291284d9c625SLionel Sambuc dayoff = oadd(dayoff, i);
29132fe8fb19SBen Gras ++m;
29142fe8fb19SBen Gras }
29152fe8fb19SBen Gras i = rp->r_dayofmonth;
29162fe8fb19SBen Gras if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
29172fe8fb19SBen Gras if (rp->r_dycode == DC_DOWLEQ)
29182fe8fb19SBen Gras --i;
29192fe8fb19SBen Gras else {
29202fe8fb19SBen Gras error(_("use of 2/29 in non leap-year"));
29212fe8fb19SBen Gras exit(EXIT_FAILURE);
29222fe8fb19SBen Gras }
29232fe8fb19SBen Gras }
29242fe8fb19SBen Gras --i;
292584d9c625SLionel Sambuc dayoff = oadd(dayoff, i);
29262fe8fb19SBen Gras if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
292784d9c625SLionel Sambuc zic_t wday;
29282fe8fb19SBen Gras
292984d9c625SLionel Sambuc #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
293084d9c625SLionel Sambuc wday = EPOCH_WDAY;
29312fe8fb19SBen Gras /*
29322fe8fb19SBen Gras ** Don't trust mod of negative numbers.
29332fe8fb19SBen Gras */
29342fe8fb19SBen Gras if (dayoff >= 0)
29352fe8fb19SBen Gras wday = (wday + dayoff) % LDAYSPERWEEK;
29362fe8fb19SBen Gras else {
29372fe8fb19SBen Gras wday -= ((-dayoff) % LDAYSPERWEEK);
29382fe8fb19SBen Gras if (wday < 0)
29392fe8fb19SBen Gras wday += LDAYSPERWEEK;
29402fe8fb19SBen Gras }
294184d9c625SLionel Sambuc while (wday != rp->r_wday)
29422fe8fb19SBen Gras if (rp->r_dycode == DC_DOWGEQ) {
294384d9c625SLionel Sambuc dayoff = oadd(dayoff, (zic_t) 1);
29442fe8fb19SBen Gras if (++wday >= LDAYSPERWEEK)
29452fe8fb19SBen Gras wday = 0;
29462fe8fb19SBen Gras ++i;
29472fe8fb19SBen Gras } else {
294884d9c625SLionel Sambuc dayoff = oadd(dayoff, (zic_t) -1);
29492fe8fb19SBen Gras if (--wday < 0)
29502fe8fb19SBen Gras wday = LDAYSPERWEEK - 1;
29512fe8fb19SBen Gras --i;
29522fe8fb19SBen Gras }
29532fe8fb19SBen Gras if (i < 0 || i >= len_months[isleap(y)][m]) {
29542fe8fb19SBen Gras if (noise)
2955*0a6a1f1dSLionel Sambuc warning(_("rule goes past start/end of month; \
29562fe8fb19SBen Gras will not work with pre-2004 versions of zic"));
29572fe8fb19SBen Gras }
29582fe8fb19SBen Gras }
29592fe8fb19SBen Gras if (dayoff < min_time / SECSPERDAY)
29602fe8fb19SBen Gras return min_time;
29612fe8fb19SBen Gras if (dayoff > max_time / SECSPERDAY)
29622fe8fb19SBen Gras return max_time;
29632fe8fb19SBen Gras t = (zic_t) dayoff * SECSPERDAY;
29642fe8fb19SBen Gras return tadd(t, rp->r_tod);
29652fe8fb19SBen Gras }
29662fe8fb19SBen Gras
29672fe8fb19SBen Gras static void
newabbr(const char * string)2968*0a6a1f1dSLionel Sambuc newabbr(const char *string)
29692fe8fb19SBen Gras {
297084d9c625SLionel Sambuc int i;
29712fe8fb19SBen Gras
29722fe8fb19SBen Gras if (strcmp(string, GRANDPARENTED) != 0) {
297384d9c625SLionel Sambuc const char * cp;
297484d9c625SLionel Sambuc const char * mp;
29752fe8fb19SBen Gras
29762fe8fb19SBen Gras cp = string;
297784d9c625SLionel Sambuc mp = NULL;
2978*0a6a1f1dSLionel Sambuc while (is_alpha(*cp) || ('0' <= *cp && *cp <= '9')
2979*0a6a1f1dSLionel Sambuc || *cp == '-' || *cp == '+')
29802fe8fb19SBen Gras ++cp;
298184d9c625SLionel Sambuc if (noise && cp - string < 3)
2982*0a6a1f1dSLionel Sambuc mp = _("time zone abbreviation has fewer than 3 characters");
29832fe8fb19SBen Gras if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2984*0a6a1f1dSLionel Sambuc mp = _("time zone abbreviation has too many characters");
29852fe8fb19SBen Gras if (*cp != '\0')
298684d9c625SLionel Sambuc mp = _("time zone abbreviation differs from POSIX standard");
298784d9c625SLionel Sambuc if (mp != NULL)
298884d9c625SLionel Sambuc warning("%s (%s)", mp, string);
29892fe8fb19SBen Gras }
29902fe8fb19SBen Gras i = strlen(string) + 1;
29912fe8fb19SBen Gras if (charcnt + i > TZ_MAX_CHARS) {
29922fe8fb19SBen Gras error(_("too many, or too long, time zone abbreviations"));
29932fe8fb19SBen Gras exit(EXIT_FAILURE);
29942fe8fb19SBen Gras }
29952fe8fb19SBen Gras (void)strncpy(&chars[charcnt], string, sizeof(chars) - charcnt - 1);
299684d9c625SLionel Sambuc charcnt += i;
29972fe8fb19SBen Gras }
29982fe8fb19SBen Gras
2999*0a6a1f1dSLionel Sambuc static bool
mkdirs(char * argname)300084d9c625SLionel Sambuc mkdirs(char *argname)
30012fe8fb19SBen Gras {
300284d9c625SLionel Sambuc char * name;
300384d9c625SLionel Sambuc char * cp;
30042fe8fb19SBen Gras
30052fe8fb19SBen Gras if (argname == NULL || *argname == '\0')
3006*0a6a1f1dSLionel Sambuc return true;
30072fe8fb19SBen Gras cp = name = ecpyalloc(argname);
30082fe8fb19SBen Gras while ((cp = strchr(cp + 1, '/')) != 0) {
30092fe8fb19SBen Gras *cp = '\0';
301084d9c625SLionel Sambuc #ifdef HAVE_DOS_FILE_NAMES
30112fe8fb19SBen Gras /*
30122fe8fb19SBen Gras ** DOS drive specifier?
30132fe8fb19SBen Gras */
3014*0a6a1f1dSLionel Sambuc if (is_alpha(name[0]) && name[1] == ':' && name[2] == '\0') {
30152fe8fb19SBen Gras *cp = '/';
30162fe8fb19SBen Gras continue;
30172fe8fb19SBen Gras }
301884d9c625SLionel Sambuc #endif
30192fe8fb19SBen Gras /*
3020*0a6a1f1dSLionel Sambuc ** Try to create it. It's OK if creation fails because
3021*0a6a1f1dSLionel Sambuc ** the directory already exists, perhaps because some
3022*0a6a1f1dSLionel Sambuc ** other process just created it.
30232fe8fb19SBen Gras */
30242fe8fb19SBen Gras if (mkdir(name, MKDIR_UMASK) != 0) {
3025*0a6a1f1dSLionel Sambuc int err = errno;
3026*0a6a1f1dSLionel Sambuc if (itsdir(name) <= 0) {
3027*0a6a1f1dSLionel Sambuc char const *e = strerror(err);
3028*0a6a1f1dSLionel Sambuc warning(_("%s: Can't create directory"
3029*0a6a1f1dSLionel Sambuc " %s: %s"),
30302fe8fb19SBen Gras progname, name, e);
303184d9c625SLionel Sambuc free(name);
3032*0a6a1f1dSLionel Sambuc return false;
30332fe8fb19SBen Gras }
30342fe8fb19SBen Gras }
30352fe8fb19SBen Gras *cp = '/';
30362fe8fb19SBen Gras }
303784d9c625SLionel Sambuc free(name);
3038*0a6a1f1dSLionel Sambuc return true;
30392fe8fb19SBen Gras }
3040