michael@0: /* michael@0: ** This file is in the public domain, so clarified as of michael@0: ** 2006-07-17 by Arthur David Olson. michael@0: */ michael@0: michael@0: static char elsieid[] = "@(#)zic.c 8.18"; michael@0: michael@0: #include "private.h" michael@0: #include "locale.h" michael@0: #include "tzfile.h" michael@0: michael@0: #define ZIC_VERSION '2' michael@0: michael@0: typedef int_fast64_t zic_t; michael@0: michael@0: #ifndef ZIC_MAX_ABBR_LEN_WO_WARN michael@0: #define ZIC_MAX_ABBR_LEN_WO_WARN 6 michael@0: #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */ michael@0: michael@0: #if HAVE_SYS_STAT_H michael@0: #include "sys/stat.h" michael@0: #endif michael@0: #ifdef S_IRUSR michael@0: #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) michael@0: #else michael@0: #define MKDIR_UMASK 0755 michael@0: #endif michael@0: michael@0: /* Enable extensions and modifications for ICU. */ michael@0: #define ICU michael@0: michael@0: /* Continue executing after link failure. Even if ICU is undefined michael@0: * (for vanilla zic behavior), ICU_LINKS should be defined, since zic michael@0: * appears to fail on the 2003 data the first time through during the michael@0: * linking phase. Running zic twice, with ICU_LINKS defined, causes michael@0: * links to be handled correctly. */ michael@0: #define ICU_LINKS michael@0: michael@0: #ifdef ICU michael@0: #include "tz2icu.h" michael@0: #endif michael@0: michael@0: /* michael@0: ** On some ancient hosts, predicates like `isspace(C)' are defined michael@0: ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard, michael@0: ** which says they are defined only if C == ((unsigned char) C) || C == EOF. michael@0: ** Neither the C Standard nor Posix require that `isascii' exist. michael@0: ** For portability, we check both ancient and modern requirements. michael@0: ** If isascii is not defined, the isascii check succeeds trivially. michael@0: */ michael@0: #include "ctype.h" michael@0: #ifndef isascii michael@0: #define isascii(x) 1 michael@0: #endif michael@0: michael@0: #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long)) michael@0: #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */ michael@0: michael@0: #define end(cp) (strchr((cp), '\0')) michael@0: michael@0: struct rule { michael@0: const char * r_filename; michael@0: int r_linenum; michael@0: const char * r_name; michael@0: michael@0: int r_loyear; /* for example, 1986 */ michael@0: int r_hiyear; /* for example, 1986 */ michael@0: const char * r_yrtype; michael@0: int r_lowasnum; michael@0: int r_hiwasnum; michael@0: michael@0: int r_month; /* 0..11 */ michael@0: michael@0: int r_dycode; /* see below */ michael@0: int r_dayofmonth; michael@0: int r_wday; michael@0: michael@0: long r_tod; /* time from midnight */ michael@0: int r_todisstd; /* above is standard time if TRUE */ michael@0: /* or wall clock time if FALSE */ michael@0: int r_todisgmt; /* above is GMT if TRUE */ michael@0: /* or local time if FALSE */ michael@0: long r_stdoff; /* offset from standard time */ michael@0: const char * r_abbrvar; /* variable part of abbreviation */ michael@0: michael@0: int r_todo; /* a rule to do (used in outzone) */ michael@0: zic_t r_temp; /* used in outzone */ michael@0: }; michael@0: michael@0: /* michael@0: ** r_dycode r_dayofmonth r_wday michael@0: */ michael@0: michael@0: #define DC_DOM 0 /* 1..31 */ /* unused */ michael@0: #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */ michael@0: #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */ michael@0: michael@0: struct zone { michael@0: const char * z_filename; michael@0: int z_linenum; michael@0: michael@0: const char * z_name; michael@0: long z_gmtoff; michael@0: const char * z_rule; michael@0: const char * z_format; michael@0: michael@0: long z_stdoff; michael@0: michael@0: struct rule * z_rules; michael@0: int z_nrules; michael@0: michael@0: struct rule z_untilrule; michael@0: zic_t z_untiltime; michael@0: }; michael@0: michael@0: extern int getopt(int argc, char * const argv[], michael@0: const char * options); michael@0: extern int link(const char * fromname, const char * toname); michael@0: extern char * optarg; michael@0: extern int optind; michael@0: michael@0: static void addtt(zic_t starttime, int type); michael@0: #ifdef ICU michael@0: static int addtype(long gmtoff, long rawoff, long dstoff, michael@0: const char * abbr, int isdst, michael@0: int ttisstd, int ttisgmt); michael@0: #else michael@0: static int addtype(long gmtoff, const char * abbr, int isdst, michael@0: int ttisstd, int ttisgmt); michael@0: #endif michael@0: static void leapadd(zic_t t, int positive, int rolling, int count); michael@0: static void adjleap(void); michael@0: static void associate(void); michael@0: static int ciequal(const char * ap, const char * bp); michael@0: static void convert(long val, char * buf); michael@0: static void convert64(zic_t val, char * buf); michael@0: static void dolink(const char * fromfield, const char * tofield); michael@0: static void doabbr(char * abbr, const char * format, michael@0: const char * letters, int isdst, int doquotes); michael@0: static void eat(const char * name, int num); michael@0: static void eats(const char * name, int num, michael@0: const char * rname, int rnum); michael@0: static long eitol(int i); michael@0: static void error(const char * message); michael@0: static char ** getfields(char * buf); michael@0: static long gethms(const char * string, const char * errstrng, michael@0: int signable); michael@0: static void infile(const char * filename); michael@0: static void inleap(char ** fields, int nfields); michael@0: static void inlink(char ** fields, int nfields); michael@0: static void inrule(char ** fields, int nfields); michael@0: static int inzcont(char ** fields, int nfields); michael@0: static int inzone(char ** fields, int nfields); michael@0: static int inzsub(char ** fields, int nfields, int iscont); michael@0: static int is32(zic_t x); michael@0: static int itsabbr(const char * abbr, const char * word); michael@0: static int itsdir(const char * name); michael@0: static int lowerit(int c); michael@0: static char * memcheck(char * tocheck); michael@0: static int mkdirs(char * filename); michael@0: static void newabbr(const char * abbr); michael@0: static long oadd(long t1, long t2); michael@0: static void outzone(const struct zone * zp, int ntzones); michael@0: static void puttzcode(long code, FILE * fp); michael@0: static void puttzcode64(zic_t code, FILE * fp); michael@0: static int rcomp(const void * leftp, const void * rightp); michael@0: static zic_t rpytime(const struct rule * rp, int wantedy); michael@0: static void rulesub(struct rule * rp, michael@0: const char * loyearp, const char * hiyearp, michael@0: const char * typep, const char * monthp, michael@0: const char * dayp, const char * timep); michael@0: static int stringoffset(char * result, long offset); michael@0: static int stringrule(char * result, const struct rule * rp, michael@0: long dstoff, long gmtoff); michael@0: static void stringzone(char * result, michael@0: const struct zone * zp, int ntzones); michael@0: static void setboundaries(void); michael@0: static zic_t tadd(zic_t t1, long t2); michael@0: static void usage(FILE *stream, int status); michael@0: static void writezone(const char * name, const char * string); michael@0: static int yearistype(int year, const char * type); michael@0: #ifdef ICU michael@0: static void emit_icu_zone(FILE* f, const char* zoneName, int zoneOffset, michael@0: const struct rule* rule, michael@0: int ruleIndex, int startYear); michael@0: static void emit_icu_link(FILE* f, const char* from, const char* to); michael@0: static void emit_icu_rule(FILE* f, const struct rule* r, int ruleIndex); michael@0: static int add_icu_final_rules(const struct rule* r1, const struct rule* r2); michael@0: #endif michael@0: michael@0: static int charcnt; michael@0: static int errors; michael@0: static const char * filename; michael@0: static int leapcnt; michael@0: static int leapseen; michael@0: static int leapminyear; michael@0: static int leapmaxyear; michael@0: static int linenum; michael@0: static int max_abbrvar_len; michael@0: static int max_format_len; michael@0: static zic_t max_time; michael@0: static int max_year; michael@0: static zic_t min_time; michael@0: static int min_year; michael@0: static int noise; michael@0: static const char * rfilename; michael@0: static int rlinenum; michael@0: static const char * progname; michael@0: static int timecnt; michael@0: static int typecnt; michael@0: michael@0: /* michael@0: ** Line codes. michael@0: */ michael@0: michael@0: #define LC_RULE 0 michael@0: #define LC_ZONE 1 michael@0: #define LC_LINK 2 michael@0: #define LC_LEAP 3 michael@0: michael@0: /* michael@0: ** Which fields are which on a Zone line. michael@0: */ michael@0: michael@0: #define ZF_NAME 1 michael@0: #define ZF_GMTOFF 2 michael@0: #define ZF_RULE 3 michael@0: #define ZF_FORMAT 4 michael@0: #define ZF_TILYEAR 5 michael@0: #define ZF_TILMONTH 6 michael@0: #define ZF_TILDAY 7 michael@0: #define ZF_TILTIME 8 michael@0: #define ZONE_MINFIELDS 5 michael@0: #define ZONE_MAXFIELDS 9 michael@0: michael@0: /* michael@0: ** Which fields are which on a Zone continuation line. michael@0: */ michael@0: michael@0: #define ZFC_GMTOFF 0 michael@0: #define ZFC_RULE 1 michael@0: #define ZFC_FORMAT 2 michael@0: #define ZFC_TILYEAR 3 michael@0: #define ZFC_TILMONTH 4 michael@0: #define ZFC_TILDAY 5 michael@0: #define ZFC_TILTIME 6 michael@0: #define ZONEC_MINFIELDS 3 michael@0: #define ZONEC_MAXFIELDS 7 michael@0: michael@0: /* michael@0: ** Which files are which on a Rule line. michael@0: */ michael@0: michael@0: #define RF_NAME 1 michael@0: #define RF_LOYEAR 2 michael@0: #define RF_HIYEAR 3 michael@0: #define RF_COMMAND 4 michael@0: #define RF_MONTH 5 michael@0: #define RF_DAY 6 michael@0: #define RF_TOD 7 michael@0: #define RF_STDOFF 8 michael@0: #define RF_ABBRVAR 9 michael@0: #define RULE_FIELDS 10 michael@0: michael@0: /* michael@0: ** Which fields are which on a Link line. michael@0: */ michael@0: michael@0: #define LF_FROM 1 michael@0: #define LF_TO 2 michael@0: #define LINK_FIELDS 3 michael@0: michael@0: /* michael@0: ** Which fields are which on a Leap line. michael@0: */ michael@0: michael@0: #define LP_YEAR 1 michael@0: #define LP_MONTH 2 michael@0: #define LP_DAY 3 michael@0: #define LP_TIME 4 michael@0: #define LP_CORR 5 michael@0: #define LP_ROLL 6 michael@0: #define LEAP_FIELDS 7 michael@0: michael@0: /* michael@0: ** Year synonyms. michael@0: */ michael@0: michael@0: #define YR_MINIMUM 0 michael@0: #define YR_MAXIMUM 1 michael@0: #define YR_ONLY 2 michael@0: michael@0: static struct rule * rules; michael@0: static int nrules; /* number of rules */ michael@0: michael@0: static struct zone * zones; michael@0: static int nzones; /* number of zones */ michael@0: michael@0: struct link { michael@0: const char * l_filename; michael@0: int l_linenum; michael@0: const char * l_from; michael@0: const char * l_to; michael@0: }; michael@0: michael@0: static struct link * links; michael@0: static int nlinks; michael@0: michael@0: struct lookup { michael@0: const char * l_word; michael@0: const int l_value; michael@0: }; michael@0: michael@0: #ifdef ICU michael@0: /* Indices into rules[] for final rules. They will occur in pairs, michael@0: * with finalRules[i] occurring before finalRules[i+1] in the year. michael@0: * Each zone need only store a start year, a standard offset, and an michael@0: * index into finalRules[]. FinalRules[] are aliases into rules[]. */ michael@0: static const struct rule ** finalRules; michael@0: static int finalRulesCount; michael@0: #endif michael@0: michael@0: static struct lookup const * byword(const char * string, michael@0: const struct lookup * lp); michael@0: michael@0: static struct lookup const line_codes[] = { michael@0: { "Rule", LC_RULE }, michael@0: { "Zone", LC_ZONE }, michael@0: { "Link", LC_LINK }, michael@0: { "Leap", LC_LEAP }, michael@0: { NULL, 0} michael@0: }; michael@0: michael@0: static struct lookup const mon_names[] = { michael@0: { "January", TM_JANUARY }, michael@0: { "February", TM_FEBRUARY }, michael@0: { "March", TM_MARCH }, michael@0: { "April", TM_APRIL }, michael@0: { "May", TM_MAY }, michael@0: { "June", TM_JUNE }, michael@0: { "July", TM_JULY }, michael@0: { "August", TM_AUGUST }, michael@0: { "September", TM_SEPTEMBER }, michael@0: { "October", TM_OCTOBER }, michael@0: { "November", TM_NOVEMBER }, michael@0: { "December", TM_DECEMBER }, michael@0: { NULL, 0 } michael@0: }; michael@0: michael@0: static struct lookup const wday_names[] = { michael@0: { "Sunday", TM_SUNDAY }, michael@0: { "Monday", TM_MONDAY }, michael@0: { "Tuesday", TM_TUESDAY }, michael@0: { "Wednesday", TM_WEDNESDAY }, michael@0: { "Thursday", TM_THURSDAY }, michael@0: { "Friday", TM_FRIDAY }, michael@0: { "Saturday", TM_SATURDAY }, michael@0: { NULL, 0 } michael@0: }; michael@0: michael@0: static struct lookup const lasts[] = { michael@0: { "last-Sunday", TM_SUNDAY }, michael@0: { "last-Monday", TM_MONDAY }, michael@0: { "last-Tuesday", TM_TUESDAY }, michael@0: { "last-Wednesday", TM_WEDNESDAY }, michael@0: { "last-Thursday", TM_THURSDAY }, michael@0: { "last-Friday", TM_FRIDAY }, michael@0: { "last-Saturday", TM_SATURDAY }, michael@0: { NULL, 0 } michael@0: }; michael@0: michael@0: static struct lookup const begin_years[] = { michael@0: { "minimum", YR_MINIMUM }, michael@0: { "maximum", YR_MAXIMUM }, michael@0: { NULL, 0 } michael@0: }; michael@0: michael@0: static struct lookup const end_years[] = { michael@0: { "minimum", YR_MINIMUM }, michael@0: { "maximum", YR_MAXIMUM }, michael@0: { "only", YR_ONLY }, michael@0: { NULL, 0 } michael@0: }; michael@0: michael@0: static struct lookup const leap_types[] = { michael@0: { "Rolling", TRUE }, michael@0: { "Stationary", FALSE }, michael@0: { NULL, 0 } michael@0: }; michael@0: michael@0: static const int len_months[2][MONSPERYEAR] = { michael@0: { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }, michael@0: { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 } michael@0: }; michael@0: michael@0: static const int len_years[2] = { michael@0: DAYSPERNYEAR, DAYSPERLYEAR michael@0: }; michael@0: michael@0: static struct attype { michael@0: zic_t at; michael@0: unsigned char type; michael@0: } attypes[TZ_MAX_TIMES]; michael@0: static long gmtoffs[TZ_MAX_TYPES]; michael@0: #ifdef ICU michael@0: /* gmtoffs[i] = rawoffs[i] + dstoffs[i] */ michael@0: static long rawoffs[TZ_MAX_TYPES]; michael@0: static long dstoffs[TZ_MAX_TYPES]; michael@0: #endif michael@0: static char isdsts[TZ_MAX_TYPES]; michael@0: static unsigned char abbrinds[TZ_MAX_TYPES]; michael@0: static char ttisstds[TZ_MAX_TYPES]; michael@0: static char ttisgmts[TZ_MAX_TYPES]; michael@0: static char chars[TZ_MAX_CHARS]; michael@0: static zic_t trans[TZ_MAX_LEAPS]; michael@0: static long corr[TZ_MAX_LEAPS]; michael@0: static char roll[TZ_MAX_LEAPS]; michael@0: michael@0: /* michael@0: ** Memory allocation. michael@0: */ michael@0: michael@0: static char * michael@0: memcheck(ptr) michael@0: char * const ptr; michael@0: { michael@0: if (ptr == NULL) { michael@0: const char *e = strerror(errno); michael@0: michael@0: (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"), michael@0: progname, e); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: return ptr; michael@0: } michael@0: michael@0: #define emalloc(size) memcheck(imalloc(size)) michael@0: #define erealloc(ptr, size) memcheck(irealloc((ptr), (size))) michael@0: #define ecpyalloc(ptr) memcheck(icpyalloc(ptr)) michael@0: #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp))) michael@0: michael@0: /* michael@0: ** Error handling. michael@0: */ michael@0: michael@0: static void michael@0: eats(name, num, rname, rnum) michael@0: const char * const name; michael@0: const int num; michael@0: const char * const rname; michael@0: const int rnum; michael@0: { michael@0: filename = name; michael@0: linenum = num; michael@0: rfilename = rname; michael@0: rlinenum = rnum; michael@0: } michael@0: michael@0: static void michael@0: eat(name, num) michael@0: const char * const name; michael@0: const int num; michael@0: { michael@0: eats(name, num, (char *) NULL, -1); michael@0: } michael@0: michael@0: static void michael@0: error(string) michael@0: const char * const string; michael@0: { michael@0: /* michael@0: ** Match the format of "cc" to allow sh users to michael@0: ** zic ... 2>&1 | error -t "*" -v michael@0: ** on BSD systems. michael@0: */ michael@0: (void) fprintf(stderr, _("\"%s\", line %d: %s"), michael@0: filename, linenum, string); michael@0: if (rfilename != NULL) michael@0: (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"), michael@0: rfilename, rlinenum); michael@0: (void) fprintf(stderr, "\n"); michael@0: ++errors; michael@0: } michael@0: michael@0: static void michael@0: warning(string) michael@0: const char * const string; michael@0: { michael@0: char * cp; michael@0: michael@0: cp = ecpyalloc(_("warning: ")); michael@0: cp = ecatalloc(cp, string); michael@0: error(cp); michael@0: ifree(cp); michael@0: --errors; michael@0: } michael@0: michael@0: static void michael@0: usage(FILE *stream, int status) michael@0: { michael@0: (void) fprintf(stream, _("%s: usage is %s \ michael@0: [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\ michael@0: \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\ michael@0: \n\ michael@0: Report bugs to tz@elsie.nci.nih.gov.\n"), michael@0: progname, progname); michael@0: exit(status); michael@0: } michael@0: michael@0: #ifdef ICU michael@0: /* File into which we will write supplemental ICU data. */ michael@0: static FILE * icuFile; michael@0: michael@0: static void michael@0: emit_icu_zone(FILE* f, const char* zoneName, int zoneOffset, michael@0: const struct rule* rule, michael@0: int ruleIndex, int startYear) { michael@0: /* machine-readable section */ michael@0: fprintf(f, "zone %s %d %d %s", zoneName, zoneOffset, startYear, rule->r_name); michael@0: michael@0: /* human-readable section */ michael@0: fprintf(f, " # zone %s, offset %d, year >= %d, rule %s (%d)\n", michael@0: zoneName, zoneOffset, startYear, michael@0: rule->r_name, ruleIndex); michael@0: } michael@0: michael@0: static void michael@0: emit_icu_link(FILE* f, const char* from, const char* to) { michael@0: /* machine-readable section */ michael@0: fprintf(f, "link %s %s\n", from, to); michael@0: } michael@0: michael@0: static const char* DYCODE[] = {"DOM", "DOWGEQ", "DOWLEQ"}; michael@0: michael@0: static void michael@0: emit_icu_rule(FILE* f, const struct rule* r, int ruleIndex) { michael@0: if (r->r_yrtype != NULL) { michael@0: warning("year types not supported by ICU"); michael@0: fprintf(stderr, "rule %s, file %s, line %d\n", michael@0: r->r_name, r->r_filename, r->r_linenum); michael@0: } michael@0: michael@0: /* machine-readable section */ michael@0: fprintf(f, "rule %s %s %d %d %d %ld %d %d %ld", michael@0: r->r_name, DYCODE[r->r_dycode], michael@0: r->r_month, r->r_dayofmonth, michael@0: (r->r_dycode == DC_DOM ? -1 : r->r_wday), michael@0: r->r_tod, r->r_todisstd, r->r_todisgmt, r->r_stdoff michael@0: ); michael@0: michael@0: /* human-readable section */ michael@0: fprintf(f, " # %d: %s, file %s, line %d", michael@0: ruleIndex, r->r_name, r->r_filename, r->r_linenum); michael@0: fprintf(f, ", mode %s", DYCODE[r->r_dycode]); michael@0: fprintf(f, ", %s, dom %d", mon_names[r->r_month].l_word, r->r_dayofmonth); michael@0: if (r->r_dycode != DC_DOM) { michael@0: fprintf(f, ", %s", wday_names[r->r_wday].l_word); michael@0: } michael@0: fprintf(f, ", time %ld", r->r_tod); michael@0: fprintf(f, ", isstd %d", r->r_todisstd); michael@0: fprintf(f, ", isgmt %d", r->r_todisgmt); michael@0: fprintf(f, ", offset %ld", r->r_stdoff); michael@0: fprintf(f, "\n"); michael@0: } michael@0: michael@0: static int michael@0: add_icu_final_rules(const struct rule* r1, const struct rule* r2) { michael@0: int i; michael@0: michael@0: for (i=0; ir_name, michael@0: ((const struct rule *) cp2)->r_name); michael@0: } michael@0: michael@0: static void michael@0: associate(void) michael@0: { michael@0: register struct zone * zp; michael@0: register struct rule * rp; michael@0: register int base, out; michael@0: register int i, j; michael@0: michael@0: if (nrules != 0) { michael@0: (void) qsort((void *) rules, (size_t) nrules, michael@0: (size_t) sizeof *rules, rcomp); michael@0: for (i = 0; i < nrules - 1; ++i) { michael@0: if (strcmp(rules[i].r_name, michael@0: rules[i + 1].r_name) != 0) michael@0: continue; michael@0: if (strcmp(rules[i].r_filename, michael@0: rules[i + 1].r_filename) == 0) michael@0: continue; michael@0: eat(rules[i].r_filename, rules[i].r_linenum); michael@0: warning(_("same rule name in multiple files")); michael@0: eat(rules[i + 1].r_filename, rules[i + 1].r_linenum); michael@0: warning(_("same rule name in multiple files")); michael@0: for (j = i + 2; j < nrules; ++j) { michael@0: if (strcmp(rules[i].r_name, michael@0: rules[j].r_name) != 0) michael@0: break; michael@0: if (strcmp(rules[i].r_filename, michael@0: rules[j].r_filename) == 0) michael@0: continue; michael@0: if (strcmp(rules[i + 1].r_filename, michael@0: rules[j].r_filename) == 0) michael@0: continue; michael@0: break; michael@0: } michael@0: i = j - 1; michael@0: } michael@0: } michael@0: for (i = 0; i < nzones; ++i) { michael@0: zp = &zones[i]; michael@0: zp->z_rules = NULL; michael@0: zp->z_nrules = 0; michael@0: } michael@0: for (base = 0; base < nrules; base = out) { michael@0: rp = &rules[base]; michael@0: for (out = base + 1; out < nrules; ++out) michael@0: if (strcmp(rp->r_name, rules[out].r_name) != 0) michael@0: break; michael@0: for (i = 0; i < nzones; ++i) { michael@0: zp = &zones[i]; michael@0: if (strcmp(zp->z_rule, rp->r_name) != 0) michael@0: continue; michael@0: zp->z_rules = rp; michael@0: zp->z_nrules = out - base; michael@0: } michael@0: } michael@0: for (i = 0; i < nzones; ++i) { michael@0: zp = &zones[i]; michael@0: if (zp->z_nrules == 0) { michael@0: /* michael@0: ** Maybe we have a local standard time offset. michael@0: */ michael@0: eat(zp->z_filename, zp->z_linenum); michael@0: zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"), michael@0: TRUE); michael@0: /* michael@0: ** Note, though, that if there's no rule, michael@0: ** a '%s' in the format is a bad thing. michael@0: */ michael@0: if (strchr(zp->z_format, '%') != 0) michael@0: error(_("%s in ruleless zone")); michael@0: } michael@0: } michael@0: if (errors) michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: michael@0: static void michael@0: infile(name) michael@0: const char * name; michael@0: { michael@0: register FILE * fp; michael@0: register char ** fields; michael@0: register char * cp; michael@0: register const struct lookup * lp; michael@0: register int nfields; michael@0: register int wantcont; michael@0: register int num; michael@0: char buf[BUFSIZ]; michael@0: michael@0: if (strcmp(name, "-") == 0) { michael@0: name = _("standard input"); michael@0: fp = stdin; michael@0: } else if ((fp = fopen(name, "r")) == NULL) { michael@0: const char *e = strerror(errno); michael@0: michael@0: (void) fprintf(stderr, _("%s: Can't open %s: %s\n"), michael@0: progname, name, e); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: wantcont = FALSE; michael@0: for (num = 1; ; ++num) { michael@0: eat(name, num); michael@0: if (fgets(buf, (int) sizeof buf, fp) != buf) michael@0: break; michael@0: cp = strchr(buf, '\n'); michael@0: if (cp == NULL) { michael@0: error(_("line too long")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: *cp = '\0'; michael@0: fields = getfields(buf); michael@0: nfields = 0; michael@0: while (fields[nfields] != NULL) { michael@0: static char nada; michael@0: michael@0: if (strcmp(fields[nfields], "-") == 0) michael@0: fields[nfields] = &nada; michael@0: ++nfields; michael@0: } michael@0: if (nfields == 0) { michael@0: /* nothing to do */ michael@0: } else if (wantcont) { michael@0: wantcont = inzcont(fields, nfields); michael@0: } else { michael@0: lp = byword(fields[0], line_codes); michael@0: if (lp == NULL) michael@0: error(_("input line of unknown type")); michael@0: else switch ((int) (lp->l_value)) { michael@0: case LC_RULE: michael@0: inrule(fields, nfields); michael@0: wantcont = FALSE; michael@0: break; michael@0: case LC_ZONE: michael@0: wantcont = inzone(fields, nfields); michael@0: break; michael@0: case LC_LINK: michael@0: inlink(fields, nfields); michael@0: wantcont = FALSE; michael@0: break; michael@0: case LC_LEAP: michael@0: if (name != leapsec) michael@0: (void) fprintf(stderr, michael@0: _("%s: Leap line in non leap seconds file %s\n"), michael@0: progname, name); michael@0: else inleap(fields, nfields); michael@0: wantcont = FALSE; michael@0: break; michael@0: default: /* "cannot happen" */ michael@0: (void) fprintf(stderr, michael@0: _("%s: panic: Invalid l_value %d\n"), michael@0: progname, lp->l_value); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } michael@0: ifree((char *) fields); michael@0: } michael@0: if (ferror(fp)) { michael@0: (void) fprintf(stderr, _("%s: Error reading %s\n"), michael@0: progname, filename); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: if (fp != stdin && fclose(fp)) { michael@0: const char *e = strerror(errno); michael@0: michael@0: (void) fprintf(stderr, _("%s: Error closing %s: %s\n"), michael@0: progname, filename, e); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: if (wantcont) michael@0: error(_("expected continuation line not found")); michael@0: } michael@0: michael@0: /* michael@0: ** Convert a string of one of the forms michael@0: ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss michael@0: ** into a number of seconds. michael@0: ** A null string maps to zero. michael@0: ** Call error with errstring and return zero on errors. michael@0: */ michael@0: michael@0: static long michael@0: gethms(string, errstring, signable) michael@0: const char * string; michael@0: const char * const errstring; michael@0: const int signable; michael@0: { michael@0: long hh; michael@0: int mm, ss, sign; michael@0: michael@0: if (string == NULL || *string == '\0') michael@0: return 0; michael@0: if (!signable) michael@0: sign = 1; michael@0: else if (*string == '-') { michael@0: sign = -1; michael@0: ++string; michael@0: } else sign = 1; michael@0: if (sscanf(string, scheck(string, "%ld"), &hh) == 1) michael@0: mm = ss = 0; michael@0: else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2) michael@0: ss = 0; michael@0: else if (sscanf(string, scheck(string, "%ld:%d:%d"), michael@0: &hh, &mm, &ss) != 3) { michael@0: error(errstring); michael@0: return 0; michael@0: } michael@0: if (hh < 0 || michael@0: mm < 0 || mm >= MINSPERHOUR || michael@0: ss < 0 || ss > SECSPERMIN) { michael@0: error(errstring); michael@0: return 0; michael@0: } michael@0: if (LONG_MAX / SECSPERHOUR < hh) { michael@0: error(_("time overflow")); michael@0: return 0; michael@0: } michael@0: if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0) michael@0: warning(_("24:00 not handled by pre-1998 versions of zic")); michael@0: if (noise && (hh > HOURSPERDAY || michael@0: (hh == HOURSPERDAY && (mm != 0 || ss != 0)))) michael@0: warning(_("values over 24 hours not handled by pre-2007 versions of zic")); michael@0: return oadd(eitol(sign) * hh * eitol(SECSPERHOUR), michael@0: eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss))); michael@0: } michael@0: michael@0: static void michael@0: inrule(fields, nfields) michael@0: register char ** const fields; michael@0: const int nfields; michael@0: { michael@0: static struct rule r; michael@0: michael@0: if (nfields != RULE_FIELDS) { michael@0: error(_("wrong number of fields on Rule line")); michael@0: return; michael@0: } michael@0: if (*fields[RF_NAME] == '\0') { michael@0: error(_("nameless rule")); michael@0: return; michael@0: } michael@0: r.r_filename = filename; michael@0: r.r_linenum = linenum; michael@0: r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE); michael@0: rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND], michael@0: fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]); michael@0: r.r_name = ecpyalloc(fields[RF_NAME]); michael@0: r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]); michael@0: if (max_abbrvar_len < strlen(r.r_abbrvar)) michael@0: max_abbrvar_len = strlen(r.r_abbrvar); michael@0: rules = (struct rule *) (void *) erealloc((char *) rules, michael@0: (int) ((nrules + 1) * sizeof *rules)); michael@0: rules[nrules++] = r; michael@0: } michael@0: michael@0: static int michael@0: inzone(fields, nfields) michael@0: register char ** const fields; michael@0: const int nfields; michael@0: { michael@0: register int i; michael@0: static char * buf; michael@0: michael@0: if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) { michael@0: error(_("wrong number of fields on Zone line")); michael@0: return FALSE; michael@0: } michael@0: if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) { michael@0: buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT))); michael@0: (void) sprintf(buf, michael@0: _("\"Zone %s\" line and -l option are mutually exclusive"), michael@0: TZDEFAULT); michael@0: error(buf); michael@0: return FALSE; michael@0: } michael@0: if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) { michael@0: buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES))); michael@0: (void) sprintf(buf, michael@0: _("\"Zone %s\" line and -p option are mutually exclusive"), michael@0: TZDEFRULES); michael@0: error(buf); michael@0: return FALSE; michael@0: } michael@0: for (i = 0; i < nzones; ++i) michael@0: if (zones[i].z_name != NULL && michael@0: strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) { michael@0: buf = erealloc(buf, (int) (132 + michael@0: strlen(fields[ZF_NAME]) + michael@0: strlen(zones[i].z_filename))); michael@0: (void) sprintf(buf, michael@0: _("duplicate zone name %s (file \"%s\", line %d)"), michael@0: fields[ZF_NAME], michael@0: zones[i].z_filename, michael@0: zones[i].z_linenum); michael@0: error(buf); michael@0: return FALSE; michael@0: } michael@0: return inzsub(fields, nfields, FALSE); michael@0: } michael@0: michael@0: static int michael@0: inzcont(fields, nfields) michael@0: register char ** const fields; michael@0: const int nfields; michael@0: { michael@0: if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) { michael@0: error(_("wrong number of fields on Zone continuation line")); michael@0: return FALSE; michael@0: } michael@0: return inzsub(fields, nfields, TRUE); michael@0: } michael@0: michael@0: static int michael@0: inzsub(fields, nfields, iscont) michael@0: register char ** const fields; michael@0: const int nfields; michael@0: const int iscont; michael@0: { michael@0: register char * cp; michael@0: static struct zone z; michael@0: register int i_gmtoff, i_rule, i_format; michael@0: register int i_untilyear, i_untilmonth; michael@0: register int i_untilday, i_untiltime; michael@0: register int hasuntil; michael@0: michael@0: if (iscont) { michael@0: i_gmtoff = ZFC_GMTOFF; michael@0: i_rule = ZFC_RULE; michael@0: i_format = ZFC_FORMAT; michael@0: i_untilyear = ZFC_TILYEAR; michael@0: i_untilmonth = ZFC_TILMONTH; michael@0: i_untilday = ZFC_TILDAY; michael@0: i_untiltime = ZFC_TILTIME; michael@0: z.z_name = NULL; michael@0: } else { michael@0: i_gmtoff = ZF_GMTOFF; michael@0: i_rule = ZF_RULE; michael@0: i_format = ZF_FORMAT; michael@0: i_untilyear = ZF_TILYEAR; michael@0: i_untilmonth = ZF_TILMONTH; michael@0: i_untilday = ZF_TILDAY; michael@0: i_untiltime = ZF_TILTIME; michael@0: z.z_name = ecpyalloc(fields[ZF_NAME]); michael@0: } michael@0: z.z_filename = filename; michael@0: z.z_linenum = linenum; michael@0: z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE); michael@0: if ((cp = strchr(fields[i_format], '%')) != 0) { michael@0: if (*++cp != 's' || strchr(cp, '%') != 0) { michael@0: error(_("invalid abbreviation format")); michael@0: return FALSE; michael@0: } michael@0: } michael@0: z.z_rule = ecpyalloc(fields[i_rule]); michael@0: z.z_format = ecpyalloc(fields[i_format]); michael@0: if (max_format_len < strlen(z.z_format)) michael@0: max_format_len = strlen(z.z_format); michael@0: hasuntil = nfields > i_untilyear; michael@0: if (hasuntil) { michael@0: z.z_untilrule.r_filename = filename; michael@0: z.z_untilrule.r_linenum = linenum; michael@0: rulesub(&z.z_untilrule, michael@0: fields[i_untilyear], michael@0: "only", michael@0: "", michael@0: (nfields > i_untilmonth) ? michael@0: fields[i_untilmonth] : "Jan", michael@0: (nfields > i_untilday) ? fields[i_untilday] : "1", michael@0: (nfields > i_untiltime) ? fields[i_untiltime] : "0"); michael@0: z.z_untiltime = rpytime(&z.z_untilrule, michael@0: z.z_untilrule.r_loyear); michael@0: if (iscont && nzones > 0 && michael@0: z.z_untiltime > min_time && michael@0: z.z_untiltime < max_time && michael@0: zones[nzones - 1].z_untiltime > min_time && michael@0: zones[nzones - 1].z_untiltime < max_time && michael@0: zones[nzones - 1].z_untiltime >= z.z_untiltime) { michael@0: error(_( michael@0: "Zone continuation line end time is not after end time of previous line" michael@0: )); michael@0: return FALSE; michael@0: } michael@0: } michael@0: zones = (struct zone *) (void *) erealloc((char *) zones, michael@0: (int) ((nzones + 1) * sizeof *zones)); michael@0: zones[nzones++] = z; michael@0: /* michael@0: ** If there was an UNTIL field on this line, michael@0: ** there's more information about the zone on the next line. michael@0: */ michael@0: return hasuntil; michael@0: } michael@0: michael@0: static void michael@0: inleap(fields, nfields) michael@0: register char ** const fields; michael@0: const int nfields; michael@0: { michael@0: register const char * cp; michael@0: register const struct lookup * lp; michael@0: register int i, j; michael@0: int year, month, day; michael@0: long dayoff, tod; michael@0: zic_t t; michael@0: michael@0: if (nfields != LEAP_FIELDS) { michael@0: error(_("wrong number of fields on Leap line")); michael@0: return; michael@0: } michael@0: dayoff = 0; michael@0: cp = fields[LP_YEAR]; michael@0: if (sscanf(cp, scheck(cp, "%d"), &year) != 1) { michael@0: /* michael@0: ** Leapin' Lizards! michael@0: */ michael@0: error(_("invalid leaping year")); michael@0: return; michael@0: } michael@0: if (!leapseen || leapmaxyear < year) michael@0: leapmaxyear = year; michael@0: if (!leapseen || leapminyear > year) michael@0: leapminyear = year; michael@0: leapseen = TRUE; michael@0: j = EPOCH_YEAR; michael@0: while (j != year) { michael@0: if (year > j) { michael@0: i = len_years[isleap(j)]; michael@0: ++j; michael@0: } else { michael@0: --j; michael@0: i = -len_years[isleap(j)]; michael@0: } michael@0: dayoff = oadd(dayoff, eitol(i)); michael@0: } michael@0: if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) { michael@0: error(_("invalid month name")); michael@0: return; michael@0: } michael@0: month = lp->l_value; michael@0: j = TM_JANUARY; michael@0: while (j != month) { michael@0: i = len_months[isleap(year)][j]; michael@0: dayoff = oadd(dayoff, eitol(i)); michael@0: ++j; michael@0: } michael@0: cp = fields[LP_DAY]; michael@0: if (sscanf(cp, scheck(cp, "%d"), &day) != 1 || michael@0: day <= 0 || day > len_months[isleap(year)][month]) { michael@0: error(_("invalid day of month")); michael@0: return; michael@0: } michael@0: dayoff = oadd(dayoff, eitol(day - 1)); michael@0: if (dayoff < 0 && !TYPE_SIGNED(zic_t)) { michael@0: error(_("time before zero")); michael@0: return; michael@0: } michael@0: if (dayoff < min_time / SECSPERDAY) { michael@0: error(_("time too small")); michael@0: return; michael@0: } michael@0: if (dayoff > max_time / SECSPERDAY) { michael@0: error(_("time too large")); michael@0: return; michael@0: } michael@0: t = (zic_t) dayoff * SECSPERDAY; michael@0: tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE); michael@0: cp = fields[LP_CORR]; michael@0: { michael@0: register int positive; michael@0: int count; michael@0: michael@0: if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */ michael@0: positive = FALSE; michael@0: count = 1; michael@0: } else if (strcmp(cp, "--") == 0) { michael@0: positive = FALSE; michael@0: count = 2; michael@0: } else if (strcmp(cp, "+") == 0) { michael@0: positive = TRUE; michael@0: count = 1; michael@0: } else if (strcmp(cp, "++") == 0) { michael@0: positive = TRUE; michael@0: count = 2; michael@0: } else { michael@0: error(_("illegal CORRECTION field on Leap line")); michael@0: return; michael@0: } michael@0: if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) { michael@0: error(_( michael@0: "illegal Rolling/Stationary field on Leap line" michael@0: )); michael@0: return; michael@0: } michael@0: leapadd(tadd(t, tod), positive, lp->l_value, count); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: inlink(fields, nfields) michael@0: register char ** const fields; michael@0: const int nfields; michael@0: { michael@0: struct link l; michael@0: michael@0: if (nfields != LINK_FIELDS) { michael@0: error(_("wrong number of fields on Link line")); michael@0: return; michael@0: } michael@0: if (*fields[LF_FROM] == '\0') { michael@0: error(_("blank FROM field on Link line")); michael@0: return; michael@0: } michael@0: if (*fields[LF_TO] == '\0') { michael@0: error(_("blank TO field on Link line")); michael@0: return; michael@0: } michael@0: l.l_filename = filename; michael@0: l.l_linenum = linenum; michael@0: l.l_from = ecpyalloc(fields[LF_FROM]); michael@0: l.l_to = ecpyalloc(fields[LF_TO]); michael@0: links = (struct link *) (void *) erealloc((char *) links, michael@0: (int) ((nlinks + 1) * sizeof *links)); michael@0: links[nlinks++] = l; michael@0: } michael@0: michael@0: static void michael@0: rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep) michael@0: register struct rule * const rp; michael@0: const char * const loyearp; michael@0: const char * const hiyearp; michael@0: const char * const typep; michael@0: const char * const monthp; michael@0: const char * const dayp; michael@0: const char * const timep; michael@0: { michael@0: register const struct lookup * lp; michael@0: register const char * cp; michael@0: register char * dp; michael@0: register char * ep; michael@0: michael@0: if ((lp = byword(monthp, mon_names)) == NULL) { michael@0: error(_("invalid month name")); michael@0: return; michael@0: } michael@0: rp->r_month = lp->l_value; michael@0: rp->r_todisstd = FALSE; michael@0: rp->r_todisgmt = FALSE; michael@0: dp = ecpyalloc(timep); michael@0: if (*dp != '\0') { michael@0: ep = dp + strlen(dp) - 1; michael@0: switch (lowerit(*ep)) { michael@0: case 's': /* Standard */ michael@0: rp->r_todisstd = TRUE; michael@0: rp->r_todisgmt = FALSE; michael@0: *ep = '\0'; michael@0: break; michael@0: case 'w': /* Wall */ michael@0: rp->r_todisstd = FALSE; michael@0: rp->r_todisgmt = FALSE; michael@0: *ep = '\0'; michael@0: break; michael@0: case 'g': /* Greenwich */ michael@0: case 'u': /* Universal */ michael@0: case 'z': /* Zulu */ michael@0: rp->r_todisstd = TRUE; michael@0: rp->r_todisgmt = TRUE; michael@0: *ep = '\0'; michael@0: break; michael@0: } michael@0: } michael@0: rp->r_tod = gethms(dp, _("invalid time of day"), FALSE); michael@0: ifree(dp); michael@0: /* michael@0: ** Year work. michael@0: */ michael@0: cp = loyearp; michael@0: lp = byword(cp, begin_years); michael@0: rp->r_lowasnum = lp == NULL; michael@0: if (!rp->r_lowasnum) switch ((int) lp->l_value) { michael@0: case YR_MINIMUM: michael@0: rp->r_loyear = INT_MIN; michael@0: break; michael@0: case YR_MAXIMUM: michael@0: rp->r_loyear = INT_MAX; michael@0: break; michael@0: default: /* "cannot happen" */ michael@0: (void) fprintf(stderr, michael@0: _("%s: panic: Invalid l_value %d\n"), michael@0: progname, lp->l_value); michael@0: exit(EXIT_FAILURE); michael@0: } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) { michael@0: error(_("invalid starting year")); michael@0: return; michael@0: } michael@0: cp = hiyearp; michael@0: lp = byword(cp, end_years); michael@0: rp->r_hiwasnum = lp == NULL; michael@0: if (!rp->r_hiwasnum) switch ((int) lp->l_value) { michael@0: case YR_MINIMUM: michael@0: rp->r_hiyear = INT_MIN; michael@0: break; michael@0: case YR_MAXIMUM: michael@0: rp->r_hiyear = INT_MAX; michael@0: break; michael@0: case YR_ONLY: michael@0: rp->r_hiyear = rp->r_loyear; michael@0: break; michael@0: default: /* "cannot happen" */ michael@0: (void) fprintf(stderr, michael@0: _("%s: panic: Invalid l_value %d\n"), michael@0: progname, lp->l_value); michael@0: exit(EXIT_FAILURE); michael@0: } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) { michael@0: error(_("invalid ending year")); michael@0: return; michael@0: } michael@0: if (rp->r_loyear > rp->r_hiyear) { michael@0: error(_("starting year greater than ending year")); michael@0: return; michael@0: } michael@0: if (*typep == '\0') michael@0: rp->r_yrtype = NULL; michael@0: else { michael@0: if (rp->r_loyear == rp->r_hiyear) { michael@0: error(_("typed single year")); michael@0: return; michael@0: } michael@0: rp->r_yrtype = ecpyalloc(typep); michael@0: } michael@0: /* michael@0: ** Day work. michael@0: ** Accept things such as: michael@0: ** 1 michael@0: ** last-Sunday michael@0: ** Sun<=20 michael@0: ** Sun>=7 michael@0: */ michael@0: dp = ecpyalloc(dayp); michael@0: if ((lp = byword(dp, lasts)) != NULL) { michael@0: rp->r_dycode = DC_DOWLEQ; michael@0: rp->r_wday = lp->l_value; michael@0: rp->r_dayofmonth = len_months[1][rp->r_month]; michael@0: } else { michael@0: if ((ep = strchr(dp, '<')) != 0) michael@0: rp->r_dycode = DC_DOWLEQ; michael@0: else if ((ep = strchr(dp, '>')) != 0) michael@0: rp->r_dycode = DC_DOWGEQ; michael@0: else { michael@0: ep = dp; michael@0: rp->r_dycode = DC_DOM; michael@0: } michael@0: if (rp->r_dycode != DC_DOM) { michael@0: *ep++ = 0; michael@0: if (*ep++ != '=') { michael@0: error(_("invalid day of month")); michael@0: ifree(dp); michael@0: return; michael@0: } michael@0: if ((lp = byword(dp, wday_names)) == NULL) { michael@0: error(_("invalid weekday name")); michael@0: ifree(dp); michael@0: return; michael@0: } michael@0: rp->r_wday = lp->l_value; michael@0: } michael@0: if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 || michael@0: rp->r_dayofmonth <= 0 || michael@0: (rp->r_dayofmonth > len_months[1][rp->r_month])) { michael@0: error(_("invalid day of month")); michael@0: ifree(dp); michael@0: return; michael@0: } michael@0: } michael@0: ifree(dp); michael@0: } michael@0: michael@0: static void michael@0: convert(val, buf) michael@0: const long val; michael@0: char * const buf; michael@0: { michael@0: register int i; michael@0: register int shift; michael@0: michael@0: for (i = 0, shift = 24; i < 4; ++i, shift -= 8) michael@0: buf[i] = val >> shift; michael@0: } michael@0: michael@0: static void michael@0: convert64(val, buf) michael@0: const zic_t val; michael@0: char * const buf; michael@0: { michael@0: register int i; michael@0: register int shift; michael@0: michael@0: for (i = 0, shift = 56; i < 8; ++i, shift -= 8) michael@0: buf[i] = val >> shift; michael@0: } michael@0: michael@0: static void michael@0: puttzcode(val, fp) michael@0: const long val; michael@0: FILE * const fp; michael@0: { michael@0: char buf[4]; michael@0: michael@0: convert(val, buf); michael@0: (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp); michael@0: } michael@0: michael@0: static void michael@0: puttzcode64(val, fp) michael@0: const zic_t val; michael@0: FILE * const fp; michael@0: { michael@0: char buf[8]; michael@0: michael@0: convert64(val, buf); michael@0: (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp); michael@0: } michael@0: michael@0: static int michael@0: atcomp(avp, bvp) michael@0: const void * avp; michael@0: const void * bvp; michael@0: { michael@0: const zic_t a = ((const struct attype *) avp)->at; michael@0: const zic_t b = ((const struct attype *) bvp)->at; michael@0: michael@0: return (a < b) ? -1 : (a > b); michael@0: } michael@0: michael@0: static int michael@0: is32(x) michael@0: const zic_t x; michael@0: { michael@0: return INT32_MIN <= x && x <= INT32_MAX; michael@0: } michael@0: michael@0: static void michael@0: writezone(name, string) michael@0: const char * const name; michael@0: const char * const string; michael@0: { michael@0: register FILE * fp; michael@0: register int i, j; michael@0: register int leapcnt32, leapi32; michael@0: register int timecnt32, timei32; michael@0: register int pass; michael@0: static char * fullname; michael@0: static const struct tzhead tzh0; michael@0: static struct tzhead tzh; michael@0: zic_t ats[TZ_MAX_TIMES]; michael@0: unsigned char types[TZ_MAX_TIMES]; michael@0: michael@0: /* michael@0: ** Sort. michael@0: */ michael@0: if (timecnt > 1) michael@0: (void) qsort((void *) attypes, (size_t) timecnt, michael@0: (size_t) sizeof *attypes, atcomp); michael@0: /* michael@0: ** Optimize. michael@0: */ michael@0: { michael@0: int fromi; michael@0: int toi; michael@0: michael@0: toi = 0; michael@0: fromi = 0; michael@0: while (fromi < timecnt && attypes[fromi].at < min_time) michael@0: ++fromi; michael@0: if (isdsts[0] == 0) michael@0: while (fromi < timecnt && attypes[fromi].type == 0) michael@0: ++fromi; /* handled by default rule */ michael@0: for ( ; fromi < timecnt; ++fromi) { michael@0: if (toi != 0 && ((attypes[fromi].at + michael@0: gmtoffs[attypes[toi - 1].type]) <= michael@0: (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0 michael@0: : attypes[toi - 2].type]))) { michael@0: attypes[toi - 1].type = michael@0: attypes[fromi].type; michael@0: continue; michael@0: } michael@0: if (toi == 0 || michael@0: attypes[toi - 1].type != attypes[fromi].type) michael@0: attypes[toi++] = attypes[fromi]; michael@0: } michael@0: timecnt = toi; michael@0: } michael@0: /* michael@0: ** Transfer. michael@0: */ michael@0: for (i = 0; i < timecnt; ++i) { michael@0: ats[i] = attypes[i].at; michael@0: types[i] = attypes[i].type; michael@0: } michael@0: /* michael@0: ** Correct for leap seconds. michael@0: */ michael@0: for (i = 0; i < timecnt; ++i) { michael@0: j = leapcnt; michael@0: while (--j >= 0) michael@0: if (ats[i] > trans[j] - corr[j]) { michael@0: ats[i] = tadd(ats[i], corr[j]); michael@0: break; michael@0: } michael@0: } michael@0: /* michael@0: ** Figure out 32-bit-limited starts and counts. michael@0: */ michael@0: timecnt32 = timecnt; michael@0: timei32 = 0; michael@0: leapcnt32 = leapcnt; michael@0: leapi32 = 0; michael@0: while (timecnt32 > 0 && !is32(ats[timecnt32 - 1])) michael@0: --timecnt32; michael@0: while (timecnt32 > 0 && !is32(ats[timei32])) { michael@0: --timecnt32; michael@0: ++timei32; michael@0: } michael@0: while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1])) michael@0: --leapcnt32; michael@0: while (leapcnt32 > 0 && !is32(trans[leapi32])) { michael@0: --leapcnt32; michael@0: ++leapi32; michael@0: } michael@0: fullname = erealloc(fullname, michael@0: (int) (strlen(directory) + 1 + strlen(name) + 1)); michael@0: (void) sprintf(fullname, "%s/%s", directory, name); michael@0: /* michael@0: ** Remove old file, if any, to snap links. michael@0: */ michael@0: if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) { michael@0: const char *e = strerror(errno); michael@0: michael@0: (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"), michael@0: progname, fullname, e); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: if ((fp = fopen(fullname, "wb")) == NULL) { michael@0: if (mkdirs(fullname) != 0) michael@0: exit(EXIT_FAILURE); michael@0: if ((fp = fopen(fullname, "wb")) == NULL) { michael@0: const char *e = strerror(errno); michael@0: michael@0: (void) fprintf(stderr, _("%s: Can't create %s: %s\n"), michael@0: progname, fullname, e); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } michael@0: for (pass = 1; pass <= 2; ++pass) { michael@0: register int thistimei, thistimecnt; michael@0: register int thisleapi, thisleapcnt; michael@0: register int thistimelim, thisleaplim; michael@0: int writetype[TZ_MAX_TIMES]; michael@0: int typemap[TZ_MAX_TYPES]; michael@0: register int thistypecnt; michael@0: char thischars[TZ_MAX_CHARS]; michael@0: char thischarcnt; michael@0: int indmap[TZ_MAX_CHARS]; michael@0: michael@0: if (pass == 1) { michael@0: thistimei = timei32; michael@0: thistimecnt = timecnt32; michael@0: thisleapi = leapi32; michael@0: thisleapcnt = leapcnt32; michael@0: } else { michael@0: thistimei = 0; michael@0: thistimecnt = timecnt; michael@0: thisleapi = 0; michael@0: thisleapcnt = leapcnt; michael@0: } michael@0: thistimelim = thistimei + thistimecnt; michael@0: thisleaplim = thisleapi + thisleapcnt; michael@0: for (i = 0; i < typecnt; ++i) michael@0: writetype[i] = thistimecnt == timecnt; michael@0: if (thistimecnt == 0) { michael@0: /* michael@0: ** No transition times fall in the current michael@0: ** (32- or 64-bit) window. michael@0: */ michael@0: if (typecnt != 0) michael@0: writetype[typecnt - 1] = TRUE; michael@0: } else { michael@0: for (i = thistimei - 1; i < thistimelim; ++i) michael@0: if (i >= 0) michael@0: writetype[types[i]] = TRUE; michael@0: /* michael@0: ** For America/Godthab and Antarctica/Palmer michael@0: */ michael@0: if (thistimei == 0) michael@0: writetype[0] = TRUE; michael@0: } michael@0: thistypecnt = 0; michael@0: for (i = 0; i < typecnt; ++i) michael@0: typemap[i] = writetype[i] ? thistypecnt++ : -1; michael@0: for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i) michael@0: indmap[i] = -1; michael@0: thischarcnt = 0; michael@0: for (i = 0; i < typecnt; ++i) { michael@0: register char * thisabbr; michael@0: michael@0: if (!writetype[i]) michael@0: continue; michael@0: if (indmap[abbrinds[i]] >= 0) michael@0: continue; michael@0: thisabbr = &chars[abbrinds[i]]; michael@0: for (j = 0; j < thischarcnt; ++j) michael@0: if (strcmp(&thischars[j], thisabbr) == 0) michael@0: break; michael@0: if (j == thischarcnt) { michael@0: (void) strcpy(&thischars[(int) thischarcnt], michael@0: thisabbr); michael@0: thischarcnt += strlen(thisabbr) + 1; michael@0: } michael@0: indmap[abbrinds[i]] = j; michael@0: } michael@0: #define DO(field) (void) fwrite((void *) tzh.field, \ michael@0: (size_t) sizeof tzh.field, (size_t) 1, fp) michael@0: tzh = tzh0; michael@0: #ifdef ICU michael@0: * (ICUZoneinfoVersion*) &tzh.tzh_reserved = TZ_ICU_VERSION; michael@0: (void) strncpy(tzh.tzh_magic, TZ_ICU_MAGIC, sizeof tzh.tzh_magic); michael@0: #else michael@0: (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic); michael@0: #endif michael@0: tzh.tzh_version[0] = ZIC_VERSION; michael@0: convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt); michael@0: convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt); michael@0: convert(eitol(thisleapcnt), tzh.tzh_leapcnt); michael@0: convert(eitol(thistimecnt), tzh.tzh_timecnt); michael@0: convert(eitol(thistypecnt), tzh.tzh_typecnt); michael@0: convert(eitol(thischarcnt), tzh.tzh_charcnt); michael@0: DO(tzh_magic); michael@0: DO(tzh_version); michael@0: DO(tzh_reserved); michael@0: DO(tzh_ttisgmtcnt); michael@0: DO(tzh_ttisstdcnt); michael@0: DO(tzh_leapcnt); michael@0: DO(tzh_timecnt); michael@0: DO(tzh_typecnt); michael@0: DO(tzh_charcnt); michael@0: #undef DO michael@0: for (i = thistimei; i < thistimelim; ++i) michael@0: if (pass == 1) michael@0: puttzcode((long) ats[i], fp); michael@0: else puttzcode64(ats[i], fp); michael@0: for (i = thistimei; i < thistimelim; ++i) { michael@0: unsigned char uc; michael@0: michael@0: uc = typemap[types[i]]; michael@0: (void) fwrite((void *) &uc, michael@0: (size_t) sizeof uc, michael@0: (size_t) 1, michael@0: fp); michael@0: } michael@0: for (i = 0; i < typecnt; ++i) michael@0: if (writetype[i]) { michael@0: #ifdef ICU michael@0: puttzcode((long) rawoffs[i], fp); michael@0: puttzcode((long) dstoffs[i], fp); michael@0: #else michael@0: puttzcode(gmtoffs[i], fp); michael@0: #endif michael@0: (void) putc(isdsts[i], fp); michael@0: (void) putc((unsigned char) indmap[abbrinds[i]], fp); michael@0: } michael@0: if (thischarcnt != 0) michael@0: (void) fwrite((void *) thischars, michael@0: (size_t) sizeof thischars[0], michael@0: (size_t) thischarcnt, fp); michael@0: for (i = thisleapi; i < thisleaplim; ++i) { michael@0: register zic_t todo; michael@0: michael@0: if (roll[i]) { michael@0: if (timecnt == 0 || trans[i] < ats[0]) { michael@0: j = 0; michael@0: while (isdsts[j]) michael@0: if (++j >= typecnt) { michael@0: j = 0; michael@0: break; michael@0: } michael@0: } else { michael@0: j = 1; michael@0: while (j < timecnt && michael@0: trans[i] >= ats[j]) michael@0: ++j; michael@0: j = types[j - 1]; michael@0: } michael@0: todo = tadd(trans[i], -gmtoffs[j]); michael@0: } else todo = trans[i]; michael@0: if (pass == 1) michael@0: puttzcode((long) todo, fp); michael@0: else puttzcode64(todo, fp); michael@0: puttzcode(corr[i], fp); michael@0: } michael@0: for (i = 0; i < typecnt; ++i) michael@0: if (writetype[i]) michael@0: (void) putc(ttisstds[i], fp); michael@0: for (i = 0; i < typecnt; ++i) michael@0: if (writetype[i]) michael@0: (void) putc(ttisgmts[i], fp); michael@0: } michael@0: (void) fprintf(fp, "\n%s\n", string); michael@0: if (ferror(fp) || fclose(fp)) { michael@0: (void) fprintf(stderr, _("%s: Error writing %s\n"), michael@0: progname, fullname); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: doabbr(abbr, format, letters, isdst, doquotes) michael@0: char * const abbr; michael@0: const char * const format; michael@0: const char * const letters; michael@0: const int isdst; michael@0: const int doquotes; michael@0: { michael@0: register char * cp; michael@0: register char * slashp; michael@0: register int len; michael@0: michael@0: slashp = strchr(format, '/'); michael@0: if (slashp == NULL) { michael@0: if (letters == NULL) michael@0: (void) strcpy(abbr, format); michael@0: else (void) sprintf(abbr, format, letters); michael@0: } else if (isdst) { michael@0: (void) strcpy(abbr, slashp + 1); michael@0: } else { michael@0: if (slashp > format) michael@0: (void) strncpy(abbr, format, michael@0: (unsigned) (slashp - format)); michael@0: abbr[slashp - format] = '\0'; michael@0: } michael@0: if (!doquotes) michael@0: return; michael@0: for (cp = abbr; *cp != '\0'; ++cp) michael@0: if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL && michael@0: strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL) michael@0: break; michael@0: len = strlen(abbr); michael@0: if (len > 0 && *cp == '\0') michael@0: return; michael@0: abbr[len + 2] = '\0'; michael@0: abbr[len + 1] = '>'; michael@0: for ( ; len > 0; --len) michael@0: abbr[len] = abbr[len - 1]; michael@0: abbr[0] = '<'; michael@0: } michael@0: michael@0: static void michael@0: updateminmax(x) michael@0: const int x; michael@0: { michael@0: if (min_year > x) michael@0: min_year = x; michael@0: if (max_year < x) michael@0: max_year = x; michael@0: } michael@0: michael@0: static int michael@0: stringoffset(result, offset) michael@0: char * result; michael@0: long offset; michael@0: { michael@0: register int hours; michael@0: register int minutes; michael@0: register int seconds; michael@0: michael@0: result[0] = '\0'; michael@0: if (offset < 0) { michael@0: (void) strcpy(result, "-"); michael@0: offset = -offset; michael@0: } michael@0: seconds = offset % SECSPERMIN; michael@0: offset /= SECSPERMIN; michael@0: minutes = offset % MINSPERHOUR; michael@0: offset /= MINSPERHOUR; michael@0: hours = offset; michael@0: if (hours >= HOURSPERDAY) { michael@0: result[0] = '\0'; michael@0: return -1; michael@0: } michael@0: (void) sprintf(end(result), "%d", hours); michael@0: if (minutes != 0 || seconds != 0) { michael@0: (void) sprintf(end(result), ":%02d", minutes); michael@0: if (seconds != 0) michael@0: (void) sprintf(end(result), ":%02d", seconds); michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static int michael@0: stringrule(result, rp, dstoff, gmtoff) michael@0: char * result; michael@0: const struct rule * const rp; michael@0: const long dstoff; michael@0: const long gmtoff; michael@0: { michael@0: register long tod; michael@0: michael@0: result = end(result); michael@0: if (rp->r_dycode == DC_DOM) { michael@0: register int month, total; michael@0: michael@0: if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY) michael@0: return -1; michael@0: total = 0; michael@0: for (month = 0; month < rp->r_month; ++month) michael@0: total += len_months[0][month]; michael@0: (void) sprintf(result, "J%d", total + rp->r_dayofmonth); michael@0: } else { michael@0: register int week; michael@0: michael@0: if (rp->r_dycode == DC_DOWGEQ) { michael@0: week = 1 + rp->r_dayofmonth / DAYSPERWEEK; michael@0: if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth) michael@0: return -1; michael@0: } else if (rp->r_dycode == DC_DOWLEQ) { michael@0: if (rp->r_dayofmonth == len_months[1][rp->r_month]) michael@0: week = 5; michael@0: else { michael@0: week = 1 + rp->r_dayofmonth / DAYSPERWEEK; michael@0: if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth) michael@0: return -1; michael@0: } michael@0: } else return -1; /* "cannot happen" */ michael@0: (void) sprintf(result, "M%d.%d.%d", michael@0: rp->r_month + 1, week, rp->r_wday); michael@0: } michael@0: tod = rp->r_tod; michael@0: if (rp->r_todisgmt) michael@0: tod += gmtoff; michael@0: if (rp->r_todisstd && rp->r_stdoff == 0) michael@0: tod += dstoff; michael@0: if (tod < 0) { michael@0: result[0] = '\0'; michael@0: return -1; michael@0: } michael@0: if (tod != 2 * SECSPERMIN * MINSPERHOUR) { michael@0: (void) strcat(result, "/"); michael@0: if (stringoffset(end(result), tod) != 0) michael@0: return -1; michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: static void michael@0: stringzone(result, zpfirst, zonecount) michael@0: char * result; michael@0: const struct zone * const zpfirst; michael@0: const int zonecount; michael@0: { michael@0: register const struct zone * zp; michael@0: register struct rule * rp; michael@0: register struct rule * stdrp; michael@0: register struct rule * dstrp; michael@0: register int i; michael@0: register const char * abbrvar; michael@0: michael@0: result[0] = '\0'; michael@0: zp = zpfirst + zonecount - 1; michael@0: stdrp = dstrp = NULL; michael@0: for (i = 0; i < zp->z_nrules; ++i) { michael@0: rp = &zp->z_rules[i]; michael@0: if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX) michael@0: continue; michael@0: if (rp->r_yrtype != NULL) michael@0: continue; michael@0: if (rp->r_stdoff == 0) { michael@0: if (stdrp == NULL) michael@0: stdrp = rp; michael@0: else return; michael@0: } else { michael@0: if (dstrp == NULL) michael@0: dstrp = rp; michael@0: else return; michael@0: } michael@0: } michael@0: if (stdrp == NULL && dstrp == NULL) { michael@0: /* michael@0: ** There are no rules running through "max". michael@0: ** Let's find the latest rule. michael@0: */ michael@0: for (i = 0; i < zp->z_nrules; ++i) { michael@0: rp = &zp->z_rules[i]; michael@0: if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear || michael@0: (rp->r_hiyear == stdrp->r_hiyear && michael@0: rp->r_month > stdrp->r_month)) michael@0: stdrp = rp; michael@0: } michael@0: if (stdrp != NULL && stdrp->r_stdoff != 0) michael@0: return; /* We end up in DST (a POSIX no-no). */ michael@0: /* michael@0: ** Horrid special case: if year is 2037, michael@0: ** presume this is a zone handled on a year-by-year basis; michael@0: ** do not try to apply a rule to the zone. michael@0: */ michael@0: if (stdrp != NULL && stdrp->r_hiyear == 2037) michael@0: return; michael@0: } michael@0: if (stdrp == NULL && zp->z_nrules != 0) michael@0: return; michael@0: abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar; michael@0: doabbr(result, zp->z_format, abbrvar, FALSE, TRUE); michael@0: if (stringoffset(end(result), -zp->z_gmtoff) != 0) { michael@0: result[0] = '\0'; michael@0: return; michael@0: } michael@0: if (dstrp == NULL) michael@0: return; michael@0: doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE); michael@0: if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR) michael@0: if (stringoffset(end(result), michael@0: -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) { michael@0: result[0] = '\0'; michael@0: return; michael@0: } michael@0: (void) strcat(result, ","); michael@0: if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) { michael@0: result[0] = '\0'; michael@0: return; michael@0: } michael@0: (void) strcat(result, ","); michael@0: if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) { michael@0: result[0] = '\0'; michael@0: return; michael@0: } michael@0: } michael@0: michael@0: static void michael@0: outzone(zpfirst, zonecount) michael@0: const struct zone * const zpfirst; michael@0: const int zonecount; michael@0: { michael@0: register const struct zone * zp; michael@0: register struct rule * rp; michael@0: register int i, j; michael@0: register int usestart, useuntil; michael@0: register zic_t starttime, untiltime; michael@0: register long gmtoff; michael@0: register long stdoff; michael@0: register int year; michael@0: register long startoff; michael@0: register int startttisstd; michael@0: register int startttisgmt; michael@0: register int type; michael@0: register char * startbuf; michael@0: register char * ab; michael@0: register char * envvar; michael@0: register int max_abbr_len; michael@0: register int max_envvar_len; michael@0: #ifdef ICU michael@0: int finalRuleYear, finalRuleIndex; michael@0: const struct rule* finalRule1; michael@0: const struct rule* finalRule2; michael@0: #endif michael@0: michael@0: max_abbr_len = 2 + max_format_len + max_abbrvar_len; michael@0: max_envvar_len = 2 * max_abbr_len + 5 * 9; michael@0: startbuf = emalloc(max_abbr_len + 1); michael@0: ab = emalloc(max_abbr_len + 1); michael@0: envvar = emalloc(max_envvar_len + 1); michael@0: INITIALIZE(untiltime); michael@0: INITIALIZE(starttime); michael@0: /* michael@0: ** Now. . .finally. . .generate some useful data! michael@0: */ michael@0: timecnt = 0; michael@0: typecnt = 0; michael@0: charcnt = 0; michael@0: /* michael@0: ** Thanks to Earl Chew michael@0: ** for noting the need to unconditionally initialize startttisstd. michael@0: */ michael@0: startttisstd = FALSE; michael@0: startttisgmt = FALSE; michael@0: min_year = max_year = EPOCH_YEAR; michael@0: if (leapseen) { michael@0: updateminmax(leapminyear); michael@0: updateminmax(leapmaxyear); michael@0: } michael@0: for (i = 0; i < zonecount; ++i) { michael@0: zp = &zpfirst[i]; michael@0: if (i < zonecount - 1) michael@0: updateminmax(zp->z_untilrule.r_loyear); michael@0: for (j = 0; j < zp->z_nrules; ++j) { michael@0: rp = &zp->z_rules[j]; michael@0: if (rp->r_lowasnum) michael@0: updateminmax(rp->r_loyear); michael@0: if (rp->r_hiwasnum) michael@0: updateminmax(rp->r_hiyear); michael@0: } michael@0: } michael@0: /* michael@0: ** Generate lots of data if a rule can't cover all future times. michael@0: */ michael@0: stringzone(envvar, zpfirst, zonecount); michael@0: if (noise && envvar[0] == '\0') { michael@0: register char * wp; michael@0: michael@0: wp = ecpyalloc(_("no POSIX environment variable for zone")); michael@0: wp = ecatalloc(wp, " "); michael@0: wp = ecatalloc(wp, zpfirst->z_name); michael@0: warning(wp); michael@0: ifree(wp); michael@0: } michael@0: if (envvar[0] == '\0') { michael@0: if (min_year >= INT_MIN + YEARSPERREPEAT) michael@0: min_year -= YEARSPERREPEAT; michael@0: else min_year = INT_MIN; michael@0: if (max_year <= INT_MAX - YEARSPERREPEAT) michael@0: max_year += YEARSPERREPEAT; michael@0: else max_year = INT_MAX; michael@0: } michael@0: /* michael@0: ** For the benefit of older systems, michael@0: ** generate data from 1900 through 2037. michael@0: */ michael@0: if (min_year > 1900) michael@0: min_year = 1900; michael@0: if (max_year < 2037) michael@0: max_year = 2037; michael@0: for (i = 0; i < zonecount; ++i) { michael@0: /* michael@0: ** A guess that may well be corrected later. michael@0: */ michael@0: stdoff = 0; michael@0: zp = &zpfirst[i]; michael@0: usestart = i > 0 && (zp - 1)->z_untiltime > min_time; michael@0: useuntil = i < (zonecount - 1); michael@0: if (useuntil && zp->z_untiltime <= min_time) michael@0: continue; michael@0: gmtoff = zp->z_gmtoff; michael@0: eat(zp->z_filename, zp->z_linenum); michael@0: *startbuf = '\0'; michael@0: startoff = zp->z_gmtoff; michael@0: #ifdef ICU michael@0: finalRuleYear = finalRuleIndex = -1; michael@0: finalRule1 = finalRule2 = NULL; michael@0: if (i == (zonecount - 1)) { /* !useuntil */ michael@0: /* Look for exactly 2 rules that end at 'max' and michael@0: * note them. Determine max(r_loyear) for the 2 of michael@0: * them. */ michael@0: for (j=0; jz_nrules; ++j) { michael@0: rp = &zp->z_rules[j]; michael@0: if (rp->r_hiyear == INT_MAX) { michael@0: if (rp->r_loyear > finalRuleYear) { michael@0: finalRuleYear = rp->r_loyear; michael@0: } michael@0: if (finalRule1 == NULL) { michael@0: finalRule1 = rp; michael@0: } else if (finalRule2 == NULL) { michael@0: finalRule2 = rp; michael@0: } else { michael@0: error("more than two max rules found (ICU)"); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } else if (rp->r_hiyear >= finalRuleYear) { michael@0: /* There might be an overriding non-max rule michael@0: * to be applied to a specific year after one of michael@0: * max rule's start year. For example, michael@0: * michael@0: * Rule Foo 2010 max ... michael@0: * Rule Foo 2015 only ... michael@0: * michael@0: * In this case, we need to change the start year of michael@0: * the final (max) rules to the next year. */ michael@0: finalRuleYear = rp->r_hiyear + 1; michael@0: michael@0: /* When above adjustment is done, max_year might need michael@0: * to be adjusted, so the final rule will be properly michael@0: * evaluated and emitted by the later code block. michael@0: * michael@0: * Note: This may push the start year of the final michael@0: * rules ahead by 1 year unnecessarily. For example, michael@0: * If there are two rules, non-max rule and max rule michael@0: * starting in the same year, such as michael@0: * michael@0: * Rule Foo 2010 only .... michael@0: * Rule Foo 2010 max .... michael@0: * michael@0: * In this case, the final (max) rule actually starts michael@0: * in 2010, instead of 2010. We could make this tool michael@0: * more intelligent to detect such situation. But pushing michael@0: * final rule start year to 1 year ahead (in the worst case) michael@0: * will just populate a few extra transitions, and it still michael@0: * works fine. So for now, we're not trying to put additional michael@0: * logic to optimize the case. michael@0: */ michael@0: if (max_year < finalRuleYear) { michael@0: max_year = finalRuleYear; michael@0: } michael@0: } michael@0: } michael@0: if (finalRule1 != NULL) { michael@0: if (finalRule2 == NULL) { michael@0: warning("only one max rule found (ICU)"); michael@0: finalRuleYear = finalRuleIndex = -1; michael@0: finalRule1 = NULL; michael@0: } else { michael@0: if (finalRule1->r_stdoff == finalRule2->r_stdoff) { michael@0: /* America/Resolute in 2009a uses a pair of rules michael@0: * which does not change the offset. ICU ignores michael@0: * such rules without actual time transitions. */ michael@0: finalRuleYear = finalRuleIndex = -1; michael@0: finalRule1 = finalRule2 = NULL; michael@0: } else { michael@0: /* Swap if necessary so finalRule1 occurs before michael@0: * finalRule2 */ michael@0: if (finalRule1->r_month > finalRule2->r_month) { michael@0: const struct rule* t = finalRule1; michael@0: finalRule1 = finalRule2; michael@0: finalRule2 = t; michael@0: } michael@0: /* Add final rule to our list */ michael@0: finalRuleIndex = add_icu_final_rules(finalRule1, finalRule2); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: if (zp->z_nrules == 0) { michael@0: stdoff = zp->z_stdoff; michael@0: doabbr(startbuf, zp->z_format, michael@0: (char *) NULL, stdoff != 0, FALSE); michael@0: type = addtype(oadd(zp->z_gmtoff, stdoff), michael@0: #ifdef ICU michael@0: zp->z_gmtoff, stdoff, michael@0: #endif michael@0: startbuf, stdoff != 0, startttisstd, michael@0: startttisgmt); michael@0: if (usestart) { michael@0: addtt(starttime, type); michael@0: usestart = FALSE; michael@0: } else if (stdoff != 0) michael@0: addtt(min_time, type); michael@0: } else for (year = min_year; year <= max_year; ++year) { michael@0: if (useuntil && year > zp->z_untilrule.r_hiyear) michael@0: break; michael@0: /* michael@0: ** Mark which rules to do in the current year. michael@0: ** For those to do, calculate rpytime(rp, year); michael@0: */ michael@0: for (j = 0; j < zp->z_nrules; ++j) { michael@0: rp = &zp->z_rules[j]; michael@0: eats(zp->z_filename, zp->z_linenum, michael@0: rp->r_filename, rp->r_linenum); michael@0: rp->r_todo = year >= rp->r_loyear && michael@0: year <= rp->r_hiyear && michael@0: yearistype(year, rp->r_yrtype); michael@0: if (rp->r_todo) michael@0: rp->r_temp = rpytime(rp, year); michael@0: } michael@0: for ( ; ; ) { michael@0: register int k; michael@0: register zic_t jtime, ktime; michael@0: register long offset; michael@0: michael@0: INITIALIZE(ktime); michael@0: if (useuntil) { michael@0: /* michael@0: ** Turn untiltime into UTC michael@0: ** assuming the current gmtoff and michael@0: ** stdoff values. michael@0: */ michael@0: untiltime = zp->z_untiltime; michael@0: if (!zp->z_untilrule.r_todisgmt) michael@0: untiltime = tadd(untiltime, michael@0: -gmtoff); michael@0: if (!zp->z_untilrule.r_todisstd) michael@0: untiltime = tadd(untiltime, michael@0: -stdoff); michael@0: } michael@0: /* michael@0: ** Find the rule (of those to do, if any) michael@0: ** that takes effect earliest in the year. michael@0: */ michael@0: k = -1; michael@0: for (j = 0; j < zp->z_nrules; ++j) { michael@0: rp = &zp->z_rules[j]; michael@0: if (!rp->r_todo) michael@0: continue; michael@0: eats(zp->z_filename, zp->z_linenum, michael@0: rp->r_filename, rp->r_linenum); michael@0: offset = rp->r_todisgmt ? 0 : gmtoff; michael@0: if (!rp->r_todisstd) michael@0: offset = oadd(offset, stdoff); michael@0: jtime = rp->r_temp; michael@0: if (jtime == min_time || michael@0: jtime == max_time) michael@0: continue; michael@0: jtime = tadd(jtime, -offset); michael@0: if (k < 0 || jtime < ktime) { michael@0: k = j; michael@0: ktime = jtime; michael@0: } michael@0: } michael@0: if (k < 0) michael@0: break; /* go on to next year */ michael@0: rp = &zp->z_rules[k]; michael@0: rp->r_todo = FALSE; michael@0: if (useuntil && ktime >= untiltime) michael@0: break; michael@0: stdoff = rp->r_stdoff; michael@0: if (usestart && ktime == starttime) michael@0: usestart = FALSE; michael@0: if (usestart) { michael@0: if (ktime < starttime) { michael@0: startoff = oadd(zp->z_gmtoff, michael@0: stdoff); michael@0: doabbr(startbuf, zp->z_format, michael@0: rp->r_abbrvar, michael@0: rp->r_stdoff != 0, michael@0: FALSE); michael@0: continue; michael@0: } michael@0: if (*startbuf == '\0' && michael@0: startoff == oadd(zp->z_gmtoff, michael@0: stdoff)) { michael@0: doabbr(startbuf, michael@0: zp->z_format, michael@0: rp->r_abbrvar, michael@0: rp->r_stdoff != michael@0: 0, michael@0: FALSE); michael@0: } michael@0: } michael@0: #ifdef ICU michael@0: if (year >= finalRuleYear && rp == finalRule1) { michael@0: /* We want to shift final year 1 year after michael@0: * the actual final rule takes effect (year + 1), michael@0: * because the previous type is valid until the first michael@0: * transition defined by the final rule. Otherwise michael@0: * we may see unexpected offset shift at the michael@0: * begining of the year when the final rule takes michael@0: * effect. michael@0: * michael@0: * Note: This may results some 64bit second transitions michael@0: * at the very end (year 2038). ICU 4.2 or older releases michael@0: * cannot handle 64bit second transitions and they are michael@0: * dropped from zoneinfo.txt. */ michael@0: emit_icu_zone(icuFile, michael@0: zpfirst->z_name, zp->z_gmtoff, michael@0: rp, finalRuleIndex, year + 1); michael@0: /* only emit this for the first year */ michael@0: finalRule1 = NULL; michael@0: } michael@0: #endif michael@0: eats(zp->z_filename, zp->z_linenum, michael@0: rp->r_filename, rp->r_linenum); michael@0: doabbr(ab, zp->z_format, rp->r_abbrvar, michael@0: rp->r_stdoff != 0, FALSE); michael@0: offset = oadd(zp->z_gmtoff, rp->r_stdoff); michael@0: #ifdef ICU michael@0: type = addtype(offset, zp->z_gmtoff, rp->r_stdoff, michael@0: ab, rp->r_stdoff != 0, michael@0: rp->r_todisstd, rp->r_todisgmt); michael@0: #else michael@0: type = addtype(offset, ab, rp->r_stdoff != 0, michael@0: rp->r_todisstd, rp->r_todisgmt); michael@0: #endif michael@0: addtt(ktime, type); michael@0: } michael@0: } michael@0: if (usestart) { michael@0: if (*startbuf == '\0' && michael@0: zp->z_format != NULL && michael@0: strchr(zp->z_format, '%') == NULL && michael@0: strchr(zp->z_format, '/') == NULL) michael@0: (void) strcpy(startbuf, zp->z_format); michael@0: eat(zp->z_filename, zp->z_linenum); michael@0: if (*startbuf == '\0') michael@0: error(_("can't determine time zone abbreviation to use just after until time")); michael@0: else addtt(starttime, michael@0: #ifdef ICU michael@0: addtype(startoff, michael@0: zp->z_gmtoff, startoff - zp->z_gmtoff, michael@0: startbuf, michael@0: startoff != zp->z_gmtoff, michael@0: startttisstd, michael@0: startttisgmt)); michael@0: #else michael@0: addtype(startoff, startbuf, michael@0: startoff != zp->z_gmtoff, michael@0: startttisstd, michael@0: startttisgmt)); michael@0: #endif michael@0: } michael@0: /* michael@0: ** Now we may get to set starttime for the next zone line. michael@0: */ michael@0: if (useuntil) { michael@0: startttisstd = zp->z_untilrule.r_todisstd; michael@0: startttisgmt = zp->z_untilrule.r_todisgmt; michael@0: starttime = zp->z_untiltime; michael@0: if (!startttisstd) michael@0: starttime = tadd(starttime, -stdoff); michael@0: if (!startttisgmt) michael@0: starttime = tadd(starttime, -gmtoff); michael@0: } michael@0: } michael@0: writezone(zpfirst->z_name, envvar); michael@0: ifree(startbuf); michael@0: ifree(ab); michael@0: ifree(envvar); michael@0: } michael@0: michael@0: static void michael@0: addtt(starttime, type) michael@0: const zic_t starttime; michael@0: int type; michael@0: { michael@0: if (starttime <= min_time || michael@0: (timecnt == 1 && attypes[0].at < min_time)) { michael@0: gmtoffs[0] = gmtoffs[type]; michael@0: #ifdef ICU michael@0: rawoffs[0] = rawoffs[type]; michael@0: dstoffs[0] = dstoffs[type]; michael@0: #endif michael@0: isdsts[0] = isdsts[type]; michael@0: ttisstds[0] = ttisstds[type]; michael@0: ttisgmts[0] = ttisgmts[type]; michael@0: if (abbrinds[type] != 0) michael@0: (void) strcpy(chars, &chars[abbrinds[type]]); michael@0: abbrinds[0] = 0; michael@0: charcnt = strlen(chars) + 1; michael@0: typecnt = 1; michael@0: timecnt = 0; michael@0: type = 0; michael@0: } michael@0: if (timecnt >= TZ_MAX_TIMES) { michael@0: error(_("too many transitions?!")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: attypes[timecnt].at = starttime; michael@0: attypes[timecnt].type = type; michael@0: ++timecnt; michael@0: } michael@0: michael@0: static int michael@0: #ifdef ICU michael@0: addtype(gmtoff, rawoff, dstoff, abbr, isdst, ttisstd, ttisgmt) michael@0: const long gmtoff; michael@0: const long rawoff; michael@0: const long dstoff; michael@0: #else michael@0: addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt) michael@0: const long gmtoff; michael@0: #endif michael@0: const char * const abbr; michael@0: const int isdst; michael@0: const int ttisstd; michael@0: const int ttisgmt; michael@0: { michael@0: register int i, j; michael@0: michael@0: if (isdst != TRUE && isdst != FALSE) { michael@0: error(_("internal error - addtype called with bad isdst")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: if (ttisstd != TRUE && ttisstd != FALSE) { michael@0: error(_("internal error - addtype called with bad ttisstd")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: if (ttisgmt != TRUE && ttisgmt != FALSE) { michael@0: error(_("internal error - addtype called with bad ttisgmt")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: #ifdef ICU michael@0: if (isdst != (dstoff != 0)) { michael@0: error(_("internal error - addtype called with bad isdst/dstoff")); michael@0: (void) exit(EXIT_FAILURE); michael@0: } michael@0: if (gmtoff != (rawoff + dstoff)) { michael@0: error(_("internal error - addtype called with bad gmt/raw/dstoff")); michael@0: (void) exit(EXIT_FAILURE); michael@0: } michael@0: #endif michael@0: /* michael@0: ** See if there's already an entry for this zone type. michael@0: ** If so, just return its index. michael@0: */ michael@0: for (i = 0; i < typecnt; ++i) { michael@0: if (gmtoff == gmtoffs[i] && isdst == isdsts[i] && michael@0: #ifdef ICU michael@0: rawoff == rawoffs[i] && dstoff == dstoffs[i] && michael@0: #endif michael@0: strcmp(abbr, &chars[abbrinds[i]]) == 0 && michael@0: ttisstd == ttisstds[i] && michael@0: ttisgmt == ttisgmts[i]) michael@0: return i; michael@0: } michael@0: /* michael@0: ** There isn't one; add a new one, unless there are already too michael@0: ** many. michael@0: */ michael@0: if (typecnt >= TZ_MAX_TYPES) { michael@0: error(_("too many local time types")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) { michael@0: error(_("UTC offset out of range")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: gmtoffs[i] = gmtoff; michael@0: #ifdef ICU michael@0: rawoffs[i] = rawoff; michael@0: dstoffs[i] = dstoff; michael@0: #endif michael@0: isdsts[i] = isdst; michael@0: ttisstds[i] = ttisstd; michael@0: ttisgmts[i] = ttisgmt; michael@0: michael@0: for (j = 0; j < charcnt; ++j) michael@0: if (strcmp(&chars[j], abbr) == 0) michael@0: break; michael@0: if (j == charcnt) michael@0: newabbr(abbr); michael@0: abbrinds[i] = j; michael@0: ++typecnt; michael@0: return i; michael@0: } michael@0: michael@0: static void michael@0: leapadd(t, positive, rolling, count) michael@0: const zic_t t; michael@0: const int positive; michael@0: const int rolling; michael@0: int count; michael@0: { michael@0: register int i, j; michael@0: michael@0: if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) { michael@0: error(_("too many leap seconds")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: for (i = 0; i < leapcnt; ++i) michael@0: if (t <= trans[i]) { michael@0: if (t == trans[i]) { michael@0: error(_("repeated leap second moment")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: break; michael@0: } michael@0: do { michael@0: for (j = leapcnt; j > i; --j) { michael@0: trans[j] = trans[j - 1]; michael@0: corr[j] = corr[j - 1]; michael@0: roll[j] = roll[j - 1]; michael@0: } michael@0: trans[i] = t; michael@0: corr[i] = positive ? 1L : eitol(-count); michael@0: roll[i] = rolling; michael@0: ++leapcnt; michael@0: } while (positive && --count != 0); michael@0: } michael@0: michael@0: static void michael@0: adjleap(void) michael@0: { michael@0: register int i; michael@0: register long last = 0; michael@0: michael@0: /* michael@0: ** propagate leap seconds forward michael@0: */ michael@0: for (i = 0; i < leapcnt; ++i) { michael@0: trans[i] = tadd(trans[i], last); michael@0: last = corr[i] += last; michael@0: } michael@0: } michael@0: michael@0: static int michael@0: yearistype(year, type) michael@0: const int year; michael@0: const char * const type; michael@0: { michael@0: static char * buf; michael@0: int result; michael@0: michael@0: if (type == NULL || *type == '\0') michael@0: return TRUE; michael@0: buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type))); michael@0: (void) sprintf(buf, "%s %d %s", yitcommand, year, type); michael@0: result = system(buf); michael@0: if (WIFEXITED(result)) switch (WEXITSTATUS(result)) { michael@0: case 0: michael@0: return TRUE; michael@0: case 1: michael@0: return FALSE; michael@0: } michael@0: error(_("Wild result from command execution")); michael@0: (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"), michael@0: progname, buf, result); michael@0: for ( ; ; ) michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: michael@0: static int michael@0: lowerit(a) michael@0: int a; michael@0: { michael@0: a = (unsigned char) a; michael@0: return (isascii(a) && isupper(a)) ? tolower(a) : a; michael@0: } michael@0: michael@0: static int michael@0: ciequal(ap, bp) /* case-insensitive equality */ michael@0: register const char * ap; michael@0: register const char * bp; michael@0: { michael@0: while (lowerit(*ap) == lowerit(*bp++)) michael@0: if (*ap++ == '\0') michael@0: return TRUE; michael@0: return FALSE; michael@0: } michael@0: michael@0: static int michael@0: itsabbr(abbr, word) michael@0: register const char * abbr; michael@0: register const char * word; michael@0: { michael@0: if (lowerit(*abbr) != lowerit(*word)) michael@0: return FALSE; michael@0: ++word; michael@0: while (*++abbr != '\0') michael@0: do { michael@0: if (*word == '\0') michael@0: return FALSE; michael@0: } while (lowerit(*word++) != lowerit(*abbr)); michael@0: return TRUE; michael@0: } michael@0: michael@0: static const struct lookup * michael@0: byword(word, table) michael@0: register const char * const word; michael@0: register const struct lookup * const table; michael@0: { michael@0: register const struct lookup * foundlp; michael@0: register const struct lookup * lp; michael@0: michael@0: if (word == NULL || table == NULL) michael@0: return NULL; michael@0: /* michael@0: ** Look for exact match. michael@0: */ michael@0: for (lp = table; lp->l_word != NULL; ++lp) michael@0: if (ciequal(word, lp->l_word)) michael@0: return lp; michael@0: /* michael@0: ** Look for inexact match. michael@0: */ michael@0: foundlp = NULL; michael@0: for (lp = table; lp->l_word != NULL; ++lp) michael@0: if (itsabbr(word, lp->l_word)) { michael@0: if (foundlp == NULL) michael@0: foundlp = lp; michael@0: else return NULL; /* multiple inexact matches */ michael@0: } michael@0: return foundlp; michael@0: } michael@0: michael@0: static char ** michael@0: getfields(cp) michael@0: register char * cp; michael@0: { michael@0: register char * dp; michael@0: register char ** array; michael@0: register int nsubs; michael@0: michael@0: if (cp == NULL) michael@0: return NULL; michael@0: array = (char **) (void *) michael@0: emalloc((int) ((strlen(cp) + 1) * sizeof *array)); michael@0: nsubs = 0; michael@0: for ( ; ; ) { michael@0: while (isascii((unsigned char) *cp) && michael@0: isspace((unsigned char) *cp)) michael@0: ++cp; michael@0: if (*cp == '\0' || *cp == '#') michael@0: break; michael@0: array[nsubs++] = dp = cp; michael@0: do { michael@0: if ((*dp = *cp++) != '"') michael@0: ++dp; michael@0: else while ((*dp = *cp++) != '"') michael@0: if (*dp != '\0') michael@0: ++dp; michael@0: else { michael@0: error(_( michael@0: "Odd number of quotation marks" michael@0: )); michael@0: exit(1); michael@0: } michael@0: } while (*cp != '\0' && *cp != '#' && michael@0: (!isascii(*cp) || !isspace((unsigned char) *cp))); michael@0: if (isascii(*cp) && isspace((unsigned char) *cp)) michael@0: ++cp; michael@0: *dp = '\0'; michael@0: } michael@0: array[nsubs] = NULL; michael@0: return array; michael@0: } michael@0: michael@0: static long michael@0: oadd(t1, t2) michael@0: const long t1; michael@0: const long t2; michael@0: { michael@0: register long t; michael@0: michael@0: t = t1 + t2; michael@0: if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) { michael@0: error(_("time overflow")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: return t; michael@0: } michael@0: michael@0: static zic_t michael@0: tadd(t1, t2) michael@0: const zic_t t1; michael@0: const long t2; michael@0: { michael@0: register zic_t t; michael@0: michael@0: if (t1 == max_time && t2 > 0) michael@0: return max_time; michael@0: if (t1 == min_time && t2 < 0) michael@0: return min_time; michael@0: t = t1 + t2; michael@0: if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) { michael@0: error(_("time overflow")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: return t; michael@0: } michael@0: michael@0: /* michael@0: ** Given a rule, and a year, compute the date - in seconds since January 1, michael@0: ** 1970, 00:00 LOCAL time - in that year that the rule refers to. michael@0: */ michael@0: michael@0: static zic_t michael@0: rpytime(rp, wantedy) michael@0: register const struct rule * const rp; michael@0: register const int wantedy; michael@0: { michael@0: register int y, m, i; michael@0: register long dayoff; /* with a nod to Margaret O. */ michael@0: register zic_t t; michael@0: michael@0: if (wantedy == INT_MIN) michael@0: return min_time; michael@0: if (wantedy == INT_MAX) michael@0: return max_time; michael@0: dayoff = 0; michael@0: m = TM_JANUARY; michael@0: y = EPOCH_YEAR; michael@0: while (wantedy != y) { michael@0: if (wantedy > y) { michael@0: i = len_years[isleap(y)]; michael@0: ++y; michael@0: } else { michael@0: --y; michael@0: i = -len_years[isleap(y)]; michael@0: } michael@0: dayoff = oadd(dayoff, eitol(i)); michael@0: } michael@0: while (m != rp->r_month) { michael@0: i = len_months[isleap(y)][m]; michael@0: dayoff = oadd(dayoff, eitol(i)); michael@0: ++m; michael@0: } michael@0: i = rp->r_dayofmonth; michael@0: if (m == TM_FEBRUARY && i == 29 && !isleap(y)) { michael@0: if (rp->r_dycode == DC_DOWLEQ) michael@0: --i; michael@0: else { michael@0: error(_("use of 2/29 in non leap-year")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: } michael@0: --i; michael@0: dayoff = oadd(dayoff, eitol(i)); michael@0: if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) { michael@0: register long wday; michael@0: michael@0: #define LDAYSPERWEEK ((long) DAYSPERWEEK) michael@0: wday = eitol(EPOCH_WDAY); michael@0: /* michael@0: ** Don't trust mod of negative numbers. michael@0: */ michael@0: if (dayoff >= 0) michael@0: wday = (wday + dayoff) % LDAYSPERWEEK; michael@0: else { michael@0: wday -= ((-dayoff) % LDAYSPERWEEK); michael@0: if (wday < 0) michael@0: wday += LDAYSPERWEEK; michael@0: } michael@0: while (wday != eitol(rp->r_wday)) michael@0: if (rp->r_dycode == DC_DOWGEQ) { michael@0: dayoff = oadd(dayoff, (long) 1); michael@0: if (++wday >= LDAYSPERWEEK) michael@0: wday = 0; michael@0: ++i; michael@0: } else { michael@0: dayoff = oadd(dayoff, (long) -1); michael@0: if (--wday < 0) michael@0: wday = LDAYSPERWEEK - 1; michael@0: --i; michael@0: } michael@0: if (i < 0 || i >= len_months[isleap(y)][m]) { michael@0: if (noise) michael@0: warning(_("rule goes past start/end of month--\ michael@0: will not work with pre-2004 versions of zic")); michael@0: } michael@0: } michael@0: if (dayoff < min_time / SECSPERDAY) michael@0: return min_time; michael@0: if (dayoff > max_time / SECSPERDAY) michael@0: return max_time; michael@0: t = (zic_t) dayoff * SECSPERDAY; michael@0: return tadd(t, rp->r_tod); michael@0: } michael@0: michael@0: static void michael@0: newabbr(string) michael@0: const char * const string; michael@0: { michael@0: register int i; michael@0: michael@0: if (strcmp(string, GRANDPARENTED) != 0) { michael@0: register const char * cp; michael@0: register char * wp; michael@0: michael@0: /* michael@0: ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics michael@0: ** optionally followed by a + or - and a number from 1 to 14. michael@0: */ michael@0: cp = string; michael@0: wp = NULL; michael@0: while (isascii((unsigned char) *cp) && michael@0: isalpha((unsigned char) *cp)) michael@0: ++cp; michael@0: if (cp - string == 0) michael@0: wp = _("time zone abbreviation lacks alphabetic at start"); michael@0: if (noise && cp - string > 3) michael@0: wp = _("time zone abbreviation has more than 3 alphabetics"); michael@0: if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN) michael@0: wp = _("time zone abbreviation has too many alphabetics"); michael@0: if (wp == NULL && (*cp == '+' || *cp == '-')) { michael@0: ++cp; michael@0: if (isascii((unsigned char) *cp) && michael@0: isdigit((unsigned char) *cp)) michael@0: if (*cp++ == '1' && michael@0: *cp >= '0' && *cp <= '4') michael@0: ++cp; michael@0: } michael@0: if (*cp != '\0') michael@0: wp = _("time zone abbreviation differs from POSIX standard"); michael@0: if (wp != NULL) { michael@0: wp = ecpyalloc(wp); michael@0: wp = ecatalloc(wp, " ("); michael@0: wp = ecatalloc(wp, string); michael@0: wp = ecatalloc(wp, ")"); michael@0: warning(wp); michael@0: ifree(wp); michael@0: } michael@0: } michael@0: i = strlen(string) + 1; michael@0: if (charcnt + i > TZ_MAX_CHARS) { michael@0: error(_("too many, or too long, time zone abbreviations")); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: (void) strcpy(&chars[charcnt], string); michael@0: charcnt += eitol(i); michael@0: } michael@0: michael@0: static int michael@0: mkdirs(argname) michael@0: char * argname; michael@0: { michael@0: register char * name; michael@0: register char * cp; michael@0: michael@0: if (argname == NULL || *argname == '\0') michael@0: return 0; michael@0: cp = name = ecpyalloc(argname); michael@0: while ((cp = strchr(cp + 1, '/')) != 0) { michael@0: *cp = '\0'; michael@0: #ifndef unix michael@0: /* michael@0: ** DOS drive specifier? michael@0: */ michael@0: if (isalpha((unsigned char) name[0]) && michael@0: name[1] == ':' && name[2] == '\0') { michael@0: *cp = '/'; michael@0: continue; michael@0: } michael@0: #endif /* !defined unix */ michael@0: if (!itsdir(name)) { michael@0: /* michael@0: ** It doesn't seem to exist, so we try to create it. michael@0: ** Creation may fail because of the directory being michael@0: ** created by some other multiprocessor, so we get michael@0: ** to do extra checking. michael@0: */ michael@0: if (mkdir(name, MKDIR_UMASK) != 0) { michael@0: const char *e = strerror(errno); michael@0: michael@0: if (errno != EEXIST || !itsdir(name)) { michael@0: (void) fprintf(stderr, michael@0: _("%s: Can't create directory %s: %s\n"), michael@0: progname, name, e); michael@0: ifree(name); michael@0: return -1; michael@0: } michael@0: } michael@0: } michael@0: *cp = '/'; michael@0: } michael@0: ifree(name); michael@0: return 0; michael@0: } michael@0: michael@0: static long michael@0: eitol(i) michael@0: const int i; michael@0: { michael@0: long l; michael@0: michael@0: l = i; michael@0: if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) { michael@0: (void) fprintf(stderr, michael@0: _("%s: %d did not sign extend correctly\n"), michael@0: progname, i); michael@0: exit(EXIT_FAILURE); michael@0: } michael@0: return l; michael@0: } michael@0: michael@0: /* michael@0: ** UNIX was a registered trademark of The Open Group in 2003. michael@0: */