]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/tzcode/zic.c
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
6 static char elsieid
[] = "@(#)zic.c 8.18";
12 #define ZIC_VERSION '2'
14 typedef int_fast64_t zic_t
;
16 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
17 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
18 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
24 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
26 #define MKDIR_UMASK 0755
29 /* Enable extensions and modifications for ICU. */
32 /* Continue executing after link failure. Even if ICU is undefined
33 * (for vanilla zic behavior), ICU_LINKS should be defined, since zic
34 * appears to fail on the 2003 data the first time through during the
35 * linking phase. Running zic twice, with ICU_LINKS defined, causes
36 * links to be handled correctly. */
44 ** On some ancient hosts, predicates like `isspace(C)' are defined
45 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
46 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
47 ** Neither the C Standard nor Posix require that `isascii' exist.
48 ** For portability, we check both ancient and modern requirements.
49 ** If isascii is not defined, the isascii check succeeds trivially.
56 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
57 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
59 #define end(cp) (strchr((cp), '\0'))
62 const char * r_filename
;
66 int r_loyear
; /* for example, 1986 */
67 int r_hiyear
; /* for example, 1986 */
68 const char * r_yrtype
;
72 int r_month
; /* 0..11 */
74 int r_dycode
; /* see below */
78 long r_tod
; /* time from midnight */
79 int r_todisstd
; /* above is standard time if TRUE */
80 /* or wall clock time if FALSE */
81 int r_todisgmt
; /* above is GMT if TRUE */
82 /* or local time if FALSE */
83 long r_stdoff
; /* offset from standard time */
84 const char * r_abbrvar
; /* variable part of abbreviation */
86 int r_todo
; /* a rule to do (used in outzone) */
87 zic_t r_temp
; /* used in outzone */
91 ** r_dycode r_dayofmonth r_wday
94 #define DC_DOM 0 /* 1..31 */ /* unused */
95 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
96 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
99 const char * z_filename
;
105 const char * z_format
;
109 struct rule
* z_rules
;
112 struct rule z_untilrule
;
116 extern int getopt(int argc
, char * const argv
[],
117 const char * options
);
118 extern int link(const char * fromname
, const char * toname
);
119 extern char * optarg
;
122 static void addtt(zic_t starttime
, int type
);
124 static int addtype(long gmtoff
, long rawoff
, long dstoff
,
125 const char * abbr
, int isdst
,
126 int ttisstd
, int ttisgmt
);
128 static int addtype(long gmtoff
, const char * abbr
, int isdst
,
129 int ttisstd
, int ttisgmt
);
131 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
132 static void adjleap(void);
133 static void associate(void);
134 static int ciequal(const char * ap
, const char * bp
);
135 static void convert(long val
, char * buf
);
136 static void convert64(zic_t val
, char * buf
);
137 static void dolink(const char * fromfield
, const char * tofield
);
138 static void doabbr(char * abbr
, const char * format
,
139 const char * letters
, int isdst
, int doquotes
);
140 static void eat(const char * name
, int num
);
141 static void eats(const char * name
, int num
,
142 const char * rname
, int rnum
);
143 static long eitol(int i
);
144 static void error(const char * message
);
145 static char ** getfields(char * buf
);
146 static long gethms(const char * string
, const char * errstrng
,
148 static void infile(const char * filename
);
149 static void inleap(char ** fields
, int nfields
);
150 static void inlink(char ** fields
, int nfields
);
151 static void inrule(char ** fields
, int nfields
);
152 static int inzcont(char ** fields
, int nfields
);
153 static int inzone(char ** fields
, int nfields
);
154 static int inzsub(char ** fields
, int nfields
, int iscont
);
155 static int is32(zic_t x
);
156 static int itsabbr(const char * abbr
, const char * word
);
157 static int itsdir(const char * name
);
158 static int lowerit(int c
);
159 static char * memcheck(char * tocheck
);
160 static int mkdirs(char * filename
);
161 static void newabbr(const char * abbr
);
162 static long oadd(long t1
, long t2
);
163 static void outzone(const struct zone
* zp
, int ntzones
);
164 static void puttzcode(long code
, FILE * fp
);
165 static void puttzcode64(zic_t code
, FILE * fp
);
166 static int rcomp(const void * leftp
, const void * rightp
);
167 static zic_t
rpytime(const struct rule
* rp
, int wantedy
);
168 static void rulesub(struct rule
* rp
,
169 const char * loyearp
, const char * hiyearp
,
170 const char * typep
, const char * monthp
,
171 const char * dayp
, const char * timep
);
172 static int stringoffset(char * result
, long offset
);
173 static int stringrule(char * result
, const struct rule
* rp
,
174 long dstoff
, long gmtoff
);
175 static void stringzone(char * result
,
176 const struct zone
* zp
, int ntzones
);
177 static void setboundaries(void);
178 static zic_t
tadd(zic_t t1
, long t2
);
179 static void usage(FILE *stream
, int status
);
180 static void writezone(const char * name
, const char * string
);
181 static int yearistype(int year
, const char * type
);
183 static void emit_icu_zone(FILE* f
, const char* zoneName
, int zoneOffset
,
184 const struct rule
* rule
,
185 int ruleIndex
, int startYear
);
186 static void emit_icu_link(FILE* f
, const char* from
, const char* to
);
187 static void emit_icu_rule(FILE* f
, const struct rule
* r
, int ruleIndex
);
188 static int add_icu_final_rules(const struct rule
* r1
, const struct rule
* r2
);
193 static const char * filename
;
196 static int leapminyear
;
197 static int leapmaxyear
;
199 static int max_abbrvar_len
;
200 static int max_format_len
;
201 static zic_t max_time
;
203 static zic_t min_time
;
206 static const char * rfilename
;
208 static const char * progname
;
222 ** Which fields are which on a Zone line.
230 #define ZF_TILMONTH 6
233 #define ZONE_MINFIELDS 5
234 #define ZONE_MAXFIELDS 9
237 ** Which fields are which on a Zone continuation line.
243 #define ZFC_TILYEAR 3
244 #define ZFC_TILMONTH 4
246 #define ZFC_TILTIME 6
247 #define ZONEC_MINFIELDS 3
248 #define ZONEC_MAXFIELDS 7
251 ** Which files are which on a Rule line.
263 #define RULE_FIELDS 10
266 ** Which fields are which on a Link line.
271 #define LINK_FIELDS 3
274 ** Which fields are which on a Leap line.
283 #define LEAP_FIELDS 7
293 static struct rule
* rules
;
294 static int nrules
; /* number of rules */
296 static struct zone
* zones
;
297 static int nzones
; /* number of zones */
300 const char * l_filename
;
306 static struct link
* links
;
315 /* Indices into rules[] for final rules. They will occur in pairs,
316 * with finalRules[i] occurring before finalRules[i+1] in the year.
317 * Each zone need only store a start year, a standard offset, and an
318 * index into finalRules[]. FinalRules[] are aliases into rules[]. */
319 static const struct rule
** finalRules
;
320 static int finalRulesCount
;
323 static struct lookup
const * byword(const char * string
,
324 const struct lookup
* lp
);
326 static struct lookup
const line_codes
[] = {
334 static struct lookup
const mon_names
[] = {
335 { "January", TM_JANUARY
},
336 { "February", TM_FEBRUARY
},
337 { "March", TM_MARCH
},
338 { "April", TM_APRIL
},
342 { "August", TM_AUGUST
},
343 { "September", TM_SEPTEMBER
},
344 { "October", TM_OCTOBER
},
345 { "November", TM_NOVEMBER
},
346 { "December", TM_DECEMBER
},
350 static struct lookup
const wday_names
[] = {
351 { "Sunday", TM_SUNDAY
},
352 { "Monday", TM_MONDAY
},
353 { "Tuesday", TM_TUESDAY
},
354 { "Wednesday", TM_WEDNESDAY
},
355 { "Thursday", TM_THURSDAY
},
356 { "Friday", TM_FRIDAY
},
357 { "Saturday", TM_SATURDAY
},
361 static struct lookup
const lasts
[] = {
362 { "last-Sunday", TM_SUNDAY
},
363 { "last-Monday", TM_MONDAY
},
364 { "last-Tuesday", TM_TUESDAY
},
365 { "last-Wednesday", TM_WEDNESDAY
},
366 { "last-Thursday", TM_THURSDAY
},
367 { "last-Friday", TM_FRIDAY
},
368 { "last-Saturday", TM_SATURDAY
},
372 static struct lookup
const begin_years
[] = {
373 { "minimum", YR_MINIMUM
},
374 { "maximum", YR_MAXIMUM
},
378 static struct lookup
const end_years
[] = {
379 { "minimum", YR_MINIMUM
},
380 { "maximum", YR_MAXIMUM
},
385 static struct lookup
const leap_types
[] = {
387 { "Stationary", FALSE
},
391 static const int len_months
[2][MONSPERYEAR
] = {
392 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
393 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
396 static const int len_years
[2] = {
397 DAYSPERNYEAR
, DAYSPERLYEAR
400 static struct attype
{
403 } attypes
[TZ_MAX_TIMES
];
404 static long gmtoffs
[TZ_MAX_TYPES
];
406 /* gmtoffs[i] = rawoffs[i] + dstoffs[i] */
407 static long rawoffs
[TZ_MAX_TYPES
];
408 static long dstoffs
[TZ_MAX_TYPES
];
410 static char isdsts
[TZ_MAX_TYPES
];
411 static unsigned char abbrinds
[TZ_MAX_TYPES
];
412 static char ttisstds
[TZ_MAX_TYPES
];
413 static char ttisgmts
[TZ_MAX_TYPES
];
414 static char chars
[TZ_MAX_CHARS
];
415 static zic_t trans
[TZ_MAX_LEAPS
];
416 static long corr
[TZ_MAX_LEAPS
];
417 static char roll
[TZ_MAX_LEAPS
];
420 ** Memory allocation.
428 const char *e
= strerror(errno
);
430 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
437 #define emalloc(size) memcheck(imalloc(size))
438 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
439 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
440 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
447 eats(name
, num
, rname
, rnum
)
448 const char * const name
;
450 const char * const rname
;
461 const char * const name
;
464 eats(name
, num
, (char *) NULL
, -1);
469 const char * const string
;
472 ** Match the format of "cc" to allow sh users to
473 ** zic ... 2>&1 | error -t "*" -v
476 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
477 filename
, linenum
, string
);
478 if (rfilename
!= NULL
)
479 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
480 rfilename
, rlinenum
);
481 (void) fprintf(stderr
, "\n");
487 const char * const string
;
491 cp
= ecpyalloc(_("warning: "));
492 cp
= ecatalloc(cp
, string
);
499 usage(FILE *stream
, int status
)
501 (void) fprintf(stream
, _("%s: usage is %s \
502 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
503 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
505 Report bugs to tz@elsie.nci.nih.gov.\n"),
511 /* File into which we will write supplemental ICU data. */
512 static FILE * icuFile
;
515 emit_icu_zone(FILE* f
, const char* zoneName
, int zoneOffset
,
516 const struct rule
* rule
,
517 int ruleIndex
, int startYear
) {
518 /* machine-readable section */
519 fprintf(f
, "zone %s %d %d %s", zoneName
, zoneOffset
, startYear
, rule
->r_name
);
521 /* human-readable section */
522 fprintf(f
, " # zone %s, offset %d, year >= %d, rule %s (%d)\n",
523 zoneName
, zoneOffset
, startYear
,
524 rule
->r_name
, ruleIndex
);
528 emit_icu_link(FILE* f
, const char* from
, const char* to
) {
529 /* machine-readable section */
530 fprintf(f
, "link %s %s\n", from
, to
);
533 static const char* DYCODE
[] = {"DOM", "DOWGEQ", "DOWLEQ"};
536 emit_icu_rule(FILE* f
, const struct rule
* r
, int ruleIndex
) {
537 if (r
->r_yrtype
!= NULL
) {
538 warning("year types not supported by ICU");
539 fprintf(stderr
, "rule %s, file %s, line %d\n",
540 r
->r_name
, r
->r_filename
, r
->r_linenum
);
543 /* machine-readable section */
544 fprintf(f
, "rule %s %s %d %d %d %ld %d %d %ld",
545 r
->r_name
, DYCODE
[r
->r_dycode
],
546 r
->r_month
, r
->r_dayofmonth
,
547 (r
->r_dycode
== DC_DOM
? -1 : r
->r_wday
),
548 r
->r_tod
, r
->r_todisstd
, r
->r_todisgmt
, r
->r_stdoff
551 /* human-readable section */
552 fprintf(f
, " # %d: %s, file %s, line %d",
553 ruleIndex
, r
->r_name
, r
->r_filename
, r
->r_linenum
);
554 fprintf(f
, ", mode %s", DYCODE
[r
->r_dycode
]);
555 fprintf(f
, ", %s, dom %d", mon_names
[r
->r_month
].l_word
, r
->r_dayofmonth
);
556 if (r
->r_dycode
!= DC_DOM
) {
557 fprintf(f
, ", %s", wday_names
[r
->r_wday
].l_word
);
559 fprintf(f
, ", time %ld", r
->r_tod
);
560 fprintf(f
, ", isstd %d", r
->r_todisstd
);
561 fprintf(f
, ", isgmt %d", r
->r_todisgmt
);
562 fprintf(f
, ", offset %ld", r
->r_stdoff
);
567 add_icu_final_rules(const struct rule
* r1
, const struct rule
* r2
) {
570 for (i
=0; i
<finalRulesCount
; ++i
) { /* i+=2 should work too */
571 if (r1
==finalRules
[i
]) return i
; /* [sic] pointer comparison */
574 finalRules
= (const struct rule
**) (void*) erealloc((char *) finalRules
,
575 (finalRulesCount
+ 2) * sizeof(*finalRules
));
576 finalRules
[finalRulesCount
++] = r1
;
577 finalRules
[finalRulesCount
++] = r2
;
578 return finalRulesCount
- 2;
582 static const char * psxrules
;
583 static const char * lcltime
;
584 static const char * directory
;
585 static const char * leapsec
;
586 static const char * yitcommand
;
598 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
599 #endif /* defined unix */
601 (void) setlocale(LC_ALL
, "");
603 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
604 #endif /* defined TEXTDOMAINDIR */
605 (void) textdomain(TZ_DOMAIN
);
606 #endif /* HAVE_GETTEXT */
608 if (TYPE_BIT(zic_t
) < 64) {
609 (void) fprintf(stderr
, "%s: %s\n", progname
,
610 _("wild compilation-time specification of zic_t"));
613 for (i
= 1; i
< argc
; ++i
)
614 if (strcmp(argv
[i
], "--version") == 0) {
615 (void) printf("%s\n", elsieid
);
617 } else if (strcmp(argv
[i
], "--help") == 0) {
618 usage(stdout
, EXIT_SUCCESS
);
620 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
623 usage(stderr
, EXIT_FAILURE
);
625 if (directory
== NULL
)
628 (void) fprintf(stderr
,
629 _("%s: More than one -d option specified\n"),
638 (void) fprintf(stderr
,
639 _("%s: More than one -l option specified\n"),
645 if (psxrules
== NULL
)
648 (void) fprintf(stderr
,
649 _("%s: More than one -p option specified\n"),
655 if (yitcommand
== NULL
)
658 (void) fprintf(stderr
,
659 _("%s: More than one -y option specified\n"),
668 (void) fprintf(stderr
,
669 _("%s: More than one -L option specified\n"),
678 (void) printf("%s: -s ignored\n", progname
);
681 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
682 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
683 if (directory
== NULL
)
685 if (yitcommand
== NULL
)
686 yitcommand
= "yearistype";
690 if (optind
< argc
&& leapsec
!= NULL
) {
696 if ((icuFile
= fopen(ICU_ZONE_FILE
, "w")) == NULL
) {
697 const char *e
= strerror(errno
);
698 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
699 progname
, ICU_ZONE_FILE
, e
);
700 (void) exit(EXIT_FAILURE
);
703 for (i
= optind
; i
< argc
; ++i
)
708 for (i
= 0; i
< nzones
; i
= j
) {
710 ** Find the next non-continuation zone entry.
712 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
714 outzone(&zones
[i
], j
- i
);
719 for (i
= 0; i
< nlinks
; ++i
) {
720 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
721 dolink(links
[i
].l_from
, links
[i
].l_to
);
723 emit_icu_link(icuFile
, links
[i
].l_from
, links
[i
].l_to
);
726 for (j
= 0; j
< nlinks
; ++j
)
727 if (strcmp(links
[i
].l_to
,
728 links
[j
].l_from
) == 0)
729 warning(_("link to link"));
731 if (lcltime
!= NULL
) {
732 eat("command line", 1);
733 dolink(lcltime
, TZDEFAULT
);
735 if (psxrules
!= NULL
) {
736 eat("command line", 1);
737 dolink(psxrules
, TZDEFRULES
);
740 for (i
=0; i
<finalRulesCount
; ++i
) {
741 emit_icu_rule(icuFile
, finalRules
[i
], i
);
744 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
748 dolink(fromfield
, tofield
)
749 const char * const fromfield
;
750 const char * const tofield
;
752 register char * fromname
;
753 register char * toname
;
755 if (fromfield
[0] == '/')
756 fromname
= ecpyalloc(fromfield
);
758 fromname
= ecpyalloc(directory
);
759 fromname
= ecatalloc(fromname
, "/");
760 fromname
= ecatalloc(fromname
, fromfield
);
762 if (tofield
[0] == '/')
763 toname
= ecpyalloc(tofield
);
765 toname
= ecpyalloc(directory
);
766 toname
= ecatalloc(toname
, "/");
767 toname
= ecatalloc(toname
, tofield
);
770 ** We get to be careful here since
771 ** there's a fair chance of root running us.
774 (void) remove(toname
);
775 if (link(fromname
, toname
) != 0) {
778 if (mkdirs(toname
) != 0)
781 result
= link(fromname
, toname
);
784 access(fromname
, F_OK
) == 0 &&
786 const char *s
= tofield
;
787 register char * symlinkcontents
= NULL
;
789 while ((s
= strchr(s
+1, '/')) != NULL
)
791 ecatalloc(symlinkcontents
,
794 ecatalloc(symlinkcontents
,
796 result
= symlink(symlinkcontents
,
799 warning(_("hard link failed, symbolic link used"));
800 ifree(symlinkcontents
);
802 #endif /* HAVE_SYMLINK */
804 const char *e
= strerror(errno
);
806 (void) fprintf(stderr
,
807 _("%s: Can't link from %s to %s: %s\n"),
808 progname
, fromname
, toname
, e
);
818 #define TIME_T_BITS_IN_FILE 64
826 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
828 max_time
= -(min_time
+ 1);
833 const char * const name
;
835 register char * myname
;
838 myname
= ecpyalloc(name
);
839 myname
= ecatalloc(myname
, "/.");
840 accres
= access(myname
, F_OK
);
846 ** Associate sets of rules with zones.
850 ** Sort by rule name.
858 return strcmp(((const struct rule
*) cp1
)->r_name
,
859 ((const struct rule
*) cp2
)->r_name
);
865 register struct zone
* zp
;
866 register struct rule
* rp
;
867 register int base
, out
;
871 (void) qsort((void *) rules
, (size_t) nrules
,
872 (size_t) sizeof *rules
, rcomp
);
873 for (i
= 0; i
< nrules
- 1; ++i
) {
874 if (strcmp(rules
[i
].r_name
,
875 rules
[i
+ 1].r_name
) != 0)
877 if (strcmp(rules
[i
].r_filename
,
878 rules
[i
+ 1].r_filename
) == 0)
880 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
881 warning(_("same rule name in multiple files"));
882 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
883 warning(_("same rule name in multiple files"));
884 for (j
= i
+ 2; j
< nrules
; ++j
) {
885 if (strcmp(rules
[i
].r_name
,
886 rules
[j
].r_name
) != 0)
888 if (strcmp(rules
[i
].r_filename
,
889 rules
[j
].r_filename
) == 0)
891 if (strcmp(rules
[i
+ 1].r_filename
,
892 rules
[j
].r_filename
) == 0)
899 for (i
= 0; i
< nzones
; ++i
) {
904 for (base
= 0; base
< nrules
; base
= out
) {
906 for (out
= base
+ 1; out
< nrules
; ++out
)
907 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
909 for (i
= 0; i
< nzones
; ++i
) {
911 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
914 zp
->z_nrules
= out
- base
;
917 for (i
= 0; i
< nzones
; ++i
) {
919 if (zp
->z_nrules
== 0) {
921 ** Maybe we have a local standard time offset.
923 eat(zp
->z_filename
, zp
->z_linenum
);
924 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
927 ** Note, though, that if there's no rule,
928 ** a '%s' in the format is a bad thing.
930 if (strchr(zp
->z_format
, '%') != 0)
931 error(_("%s in ruleless zone"));
943 register char ** fields
;
945 register const struct lookup
* lp
;
946 register int nfields
;
947 register int wantcont
;
951 if (strcmp(name
, "-") == 0) {
952 name
= _("standard input");
954 } else if ((fp
= fopen(name
, "r")) == NULL
) {
955 const char *e
= strerror(errno
);
957 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
962 for (num
= 1; ; ++num
) {
964 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
966 cp
= strchr(buf
, '\n');
968 error(_("line too long"));
972 fields
= getfields(buf
);
974 while (fields
[nfields
] != NULL
) {
977 if (strcmp(fields
[nfields
], "-") == 0)
978 fields
[nfields
] = &nada
;
983 } else if (wantcont
) {
984 wantcont
= inzcont(fields
, nfields
);
986 lp
= byword(fields
[0], line_codes
);
988 error(_("input line of unknown type"));
989 else switch ((int) (lp
->l_value
)) {
991 inrule(fields
, nfields
);
995 wantcont
= inzone(fields
, nfields
);
998 inlink(fields
, nfields
);
1002 if (name
!= leapsec
)
1003 (void) fprintf(stderr
,
1004 _("%s: Leap line in non leap seconds file %s\n"),
1006 else inleap(fields
, nfields
);
1009 default: /* "cannot happen" */
1010 (void) fprintf(stderr
,
1011 _("%s: panic: Invalid l_value %d\n"),
1012 progname
, lp
->l_value
);
1016 ifree((char *) fields
);
1019 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
1020 progname
, filename
);
1023 if (fp
!= stdin
&& fclose(fp
)) {
1024 const char *e
= strerror(errno
);
1026 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
1027 progname
, filename
, e
);
1031 error(_("expected continuation line not found"));
1035 ** Convert a string of one of the forms
1036 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
1037 ** into a number of seconds.
1038 ** A null string maps to zero.
1039 ** Call error with errstring and return zero on errors.
1043 gethms(string
, errstring
, signable
)
1044 const char * string
;
1045 const char * const errstring
;
1051 if (string
== NULL
|| *string
== '\0')
1055 else if (*string
== '-') {
1059 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
1061 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
1063 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
1064 &hh
, &mm
, &ss
) != 3) {
1069 mm
< 0 || mm
>= MINSPERHOUR
||
1070 ss
< 0 || ss
> SECSPERMIN
) {
1074 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
1075 error(_("time overflow"));
1078 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
1079 warning(_("24:00 not handled by pre-1998 versions of zic"));
1080 if (noise
&& (hh
> HOURSPERDAY
||
1081 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
1082 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
1083 return oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
1084 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
)));
1088 inrule(fields
, nfields
)
1089 register char ** const fields
;
1092 static struct rule r
;
1094 if (nfields
!= RULE_FIELDS
) {
1095 error(_("wrong number of fields on Rule line"));
1098 if (*fields
[RF_NAME
] == '\0') {
1099 error(_("nameless rule"));
1102 r
.r_filename
= filename
;
1103 r
.r_linenum
= linenum
;
1104 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
1105 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
1106 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
1107 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
1108 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
1109 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
1110 max_abbrvar_len
= strlen(r
.r_abbrvar
);
1111 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
1112 (int) ((nrules
+ 1) * sizeof *rules
));
1113 rules
[nrules
++] = r
;
1117 inzone(fields
, nfields
)
1118 register char ** const fields
;
1124 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1125 error(_("wrong number of fields on Zone line"));
1128 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1129 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
1131 _("\"Zone %s\" line and -l option are mutually exclusive"),
1136 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1137 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1139 _("\"Zone %s\" line and -p option are mutually exclusive"),
1144 for (i
= 0; i
< nzones
; ++i
)
1145 if (zones
[i
].z_name
!= NULL
&&
1146 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1147 buf
= erealloc(buf
, (int) (132 +
1148 strlen(fields
[ZF_NAME
]) +
1149 strlen(zones
[i
].z_filename
)));
1151 _("duplicate zone name %s (file \"%s\", line %d)"),
1153 zones
[i
].z_filename
,
1154 zones
[i
].z_linenum
);
1158 return inzsub(fields
, nfields
, FALSE
);
1162 inzcont(fields
, nfields
)
1163 register char ** const fields
;
1166 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1167 error(_("wrong number of fields on Zone continuation line"));
1170 return inzsub(fields
, nfields
, TRUE
);
1174 inzsub(fields
, nfields
, iscont
)
1175 register char ** const fields
;
1180 static struct zone z
;
1181 register int i_gmtoff
, i_rule
, i_format
;
1182 register int i_untilyear
, i_untilmonth
;
1183 register int i_untilday
, i_untiltime
;
1184 register int hasuntil
;
1187 i_gmtoff
= ZFC_GMTOFF
;
1189 i_format
= ZFC_FORMAT
;
1190 i_untilyear
= ZFC_TILYEAR
;
1191 i_untilmonth
= ZFC_TILMONTH
;
1192 i_untilday
= ZFC_TILDAY
;
1193 i_untiltime
= ZFC_TILTIME
;
1196 i_gmtoff
= ZF_GMTOFF
;
1198 i_format
= ZF_FORMAT
;
1199 i_untilyear
= ZF_TILYEAR
;
1200 i_untilmonth
= ZF_TILMONTH
;
1201 i_untilday
= ZF_TILDAY
;
1202 i_untiltime
= ZF_TILTIME
;
1203 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1205 z
.z_filename
= filename
;
1206 z
.z_linenum
= linenum
;
1207 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1208 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1209 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1210 error(_("invalid abbreviation format"));
1214 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1215 z
.z_format
= ecpyalloc(fields
[i_format
]);
1216 if (max_format_len
< strlen(z
.z_format
))
1217 max_format_len
= strlen(z
.z_format
);
1218 hasuntil
= nfields
> i_untilyear
;
1220 z
.z_untilrule
.r_filename
= filename
;
1221 z
.z_untilrule
.r_linenum
= linenum
;
1222 rulesub(&z
.z_untilrule
,
1223 fields
[i_untilyear
],
1226 (nfields
> i_untilmonth
) ?
1227 fields
[i_untilmonth
] : "Jan",
1228 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1229 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1230 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1231 z
.z_untilrule
.r_loyear
);
1232 if (iscont
&& nzones
> 0 &&
1233 z
.z_untiltime
> min_time
&&
1234 z
.z_untiltime
< max_time
&&
1235 zones
[nzones
- 1].z_untiltime
> min_time
&&
1236 zones
[nzones
- 1].z_untiltime
< max_time
&&
1237 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1239 "Zone continuation line end time is not after end time of previous line"
1244 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1245 (int) ((nzones
+ 1) * sizeof *zones
));
1246 zones
[nzones
++] = z
;
1248 ** If there was an UNTIL field on this line,
1249 ** there's more information about the zone on the next line.
1255 inleap(fields
, nfields
)
1256 register char ** const fields
;
1259 register const char * cp
;
1260 register const struct lookup
* lp
;
1262 int year
, month
, day
;
1266 if (nfields
!= LEAP_FIELDS
) {
1267 error(_("wrong number of fields on Leap line"));
1271 cp
= fields
[LP_YEAR
];
1272 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1276 error(_("invalid leaping year"));
1279 if (!leapseen
|| leapmaxyear
< year
)
1281 if (!leapseen
|| leapminyear
> year
)
1287 i
= len_years
[isleap(j
)];
1291 i
= -len_years
[isleap(j
)];
1293 dayoff
= oadd(dayoff
, eitol(i
));
1295 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1296 error(_("invalid month name"));
1299 month
= lp
->l_value
;
1301 while (j
!= month
) {
1302 i
= len_months
[isleap(year
)][j
];
1303 dayoff
= oadd(dayoff
, eitol(i
));
1306 cp
= fields
[LP_DAY
];
1307 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1308 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1309 error(_("invalid day of month"));
1312 dayoff
= oadd(dayoff
, eitol(day
- 1));
1313 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1314 error(_("time before zero"));
1317 if (dayoff
< min_time
/ SECSPERDAY
) {
1318 error(_("time too small"));
1321 if (dayoff
> max_time
/ SECSPERDAY
) {
1322 error(_("time too large"));
1325 t
= (zic_t
) dayoff
* SECSPERDAY
;
1326 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1327 cp
= fields
[LP_CORR
];
1329 register int positive
;
1332 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1335 } else if (strcmp(cp
, "--") == 0) {
1338 } else if (strcmp(cp
, "+") == 0) {
1341 } else if (strcmp(cp
, "++") == 0) {
1345 error(_("illegal CORRECTION field on Leap line"));
1348 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1350 "illegal Rolling/Stationary field on Leap line"
1354 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1359 inlink(fields
, nfields
)
1360 register char ** const fields
;
1365 if (nfields
!= LINK_FIELDS
) {
1366 error(_("wrong number of fields on Link line"));
1369 if (*fields
[LF_FROM
] == '\0') {
1370 error(_("blank FROM field on Link line"));
1373 if (*fields
[LF_TO
] == '\0') {
1374 error(_("blank TO field on Link line"));
1377 l
.l_filename
= filename
;
1378 l
.l_linenum
= linenum
;
1379 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1380 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1381 links
= (struct link
*) (void *) erealloc((char *) links
,
1382 (int) ((nlinks
+ 1) * sizeof *links
));
1383 links
[nlinks
++] = l
;
1387 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1388 register struct rule
* const rp
;
1389 const char * const loyearp
;
1390 const char * const hiyearp
;
1391 const char * const typep
;
1392 const char * const monthp
;
1393 const char * const dayp
;
1394 const char * const timep
;
1396 register const struct lookup
* lp
;
1397 register const char * cp
;
1401 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1402 error(_("invalid month name"));
1405 rp
->r_month
= lp
->l_value
;
1406 rp
->r_todisstd
= FALSE
;
1407 rp
->r_todisgmt
= FALSE
;
1408 dp
= ecpyalloc(timep
);
1410 ep
= dp
+ strlen(dp
) - 1;
1411 switch (lowerit(*ep
)) {
1412 case 's': /* Standard */
1413 rp
->r_todisstd
= TRUE
;
1414 rp
->r_todisgmt
= FALSE
;
1417 case 'w': /* Wall */
1418 rp
->r_todisstd
= FALSE
;
1419 rp
->r_todisgmt
= FALSE
;
1422 case 'g': /* Greenwich */
1423 case 'u': /* Universal */
1424 case 'z': /* Zulu */
1425 rp
->r_todisstd
= TRUE
;
1426 rp
->r_todisgmt
= TRUE
;
1431 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1437 lp
= byword(cp
, begin_years
);
1438 rp
->r_lowasnum
= lp
== NULL
;
1439 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1441 rp
->r_loyear
= INT_MIN
;
1444 rp
->r_loyear
= INT_MAX
;
1446 default: /* "cannot happen" */
1447 (void) fprintf(stderr
,
1448 _("%s: panic: Invalid l_value %d\n"),
1449 progname
, lp
->l_value
);
1451 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1452 error(_("invalid starting year"));
1456 lp
= byword(cp
, end_years
);
1457 rp
->r_hiwasnum
= lp
== NULL
;
1458 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1460 rp
->r_hiyear
= INT_MIN
;
1463 rp
->r_hiyear
= INT_MAX
;
1466 rp
->r_hiyear
= rp
->r_loyear
;
1468 default: /* "cannot happen" */
1469 (void) fprintf(stderr
,
1470 _("%s: panic: Invalid l_value %d\n"),
1471 progname
, lp
->l_value
);
1473 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1474 error(_("invalid ending year"));
1477 if (rp
->r_loyear
> rp
->r_hiyear
) {
1478 error(_("starting year greater than ending year"));
1482 rp
->r_yrtype
= NULL
;
1484 if (rp
->r_loyear
== rp
->r_hiyear
) {
1485 error(_("typed single year"));
1488 rp
->r_yrtype
= ecpyalloc(typep
);
1492 ** Accept things such as:
1498 dp
= ecpyalloc(dayp
);
1499 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1500 rp
->r_dycode
= DC_DOWLEQ
;
1501 rp
->r_wday
= lp
->l_value
;
1502 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1504 if ((ep
= strchr(dp
, '<')) != 0)
1505 rp
->r_dycode
= DC_DOWLEQ
;
1506 else if ((ep
= strchr(dp
, '>')) != 0)
1507 rp
->r_dycode
= DC_DOWGEQ
;
1510 rp
->r_dycode
= DC_DOM
;
1512 if (rp
->r_dycode
!= DC_DOM
) {
1515 error(_("invalid day of month"));
1519 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1520 error(_("invalid weekday name"));
1524 rp
->r_wday
= lp
->l_value
;
1526 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1527 rp
->r_dayofmonth
<= 0 ||
1528 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1529 error(_("invalid day of month"));
1545 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1546 buf
[i
] = val
>> shift
;
1557 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1558 buf
[i
] = val
>> shift
;
1569 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1573 puttzcode64(val
, fp
)
1579 convert64(val
, buf
);
1580 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1588 const zic_t a
= ((const struct attype
*) avp
)->at
;
1589 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1591 return (a
< b
) ? -1 : (a
> b
);
1598 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1602 writezone(name
, string
)
1603 const char * const name
;
1604 const char * const string
;
1608 register int leapcnt32
, leapi32
;
1609 register int timecnt32
, timei32
;
1611 static char * fullname
;
1612 static const struct tzhead tzh0
;
1613 static struct tzhead tzh
;
1614 zic_t ats
[TZ_MAX_TIMES
];
1615 unsigned char types
[TZ_MAX_TIMES
];
1621 (void) qsort((void *) attypes
, (size_t) timecnt
,
1622 (size_t) sizeof *attypes
, atcomp
);
1632 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1635 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1636 ++fromi
; /* handled by default rule */
1637 for ( ; fromi
< timecnt
; ++fromi
) {
1638 if (toi
!= 0 && ((attypes
[fromi
].at
+
1639 gmtoffs
[attypes
[toi
- 1].type
]) <=
1640 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1641 : attypes
[toi
- 2].type
]))) {
1642 attypes
[toi
- 1].type
=
1643 attypes
[fromi
].type
;
1647 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1648 attypes
[toi
++] = attypes
[fromi
];
1655 for (i
= 0; i
< timecnt
; ++i
) {
1656 ats
[i
] = attypes
[i
].at
;
1657 types
[i
] = attypes
[i
].type
;
1660 ** Correct for leap seconds.
1662 for (i
= 0; i
< timecnt
; ++i
) {
1665 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1666 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1671 ** Figure out 32-bit-limited starts and counts.
1673 timecnt32
= timecnt
;
1675 leapcnt32
= leapcnt
;
1677 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1679 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1683 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1685 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1689 fullname
= erealloc(fullname
,
1690 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1691 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1693 ** Remove old file, if any, to snap links.
1695 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1696 const char *e
= strerror(errno
);
1698 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1699 progname
, fullname
, e
);
1702 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1703 if (mkdirs(fullname
) != 0)
1705 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1706 const char *e
= strerror(errno
);
1708 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1709 progname
, fullname
, e
);
1713 for (pass
= 1; pass
<= 2; ++pass
) {
1714 register int thistimei
, thistimecnt
;
1715 register int thisleapi
, thisleapcnt
;
1716 register int thistimelim
, thisleaplim
;
1717 int writetype
[TZ_MAX_TIMES
];
1718 int typemap
[TZ_MAX_TYPES
];
1719 register int thistypecnt
;
1720 char thischars
[TZ_MAX_CHARS
];
1722 int indmap
[TZ_MAX_CHARS
];
1725 thistimei
= timei32
;
1726 thistimecnt
= timecnt32
;
1727 thisleapi
= leapi32
;
1728 thisleapcnt
= leapcnt32
;
1731 thistimecnt
= timecnt
;
1733 thisleapcnt
= leapcnt
;
1735 thistimelim
= thistimei
+ thistimecnt
;
1736 thisleaplim
= thisleapi
+ thisleapcnt
;
1737 for (i
= 0; i
< typecnt
; ++i
)
1738 writetype
[i
] = thistimecnt
== timecnt
;
1739 if (thistimecnt
== 0) {
1741 ** No transition times fall in the current
1742 ** (32- or 64-bit) window.
1745 writetype
[typecnt
- 1] = TRUE
;
1747 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1749 writetype
[types
[i
]] = TRUE
;
1751 ** For America/Godthab and Antarctica/Palmer
1754 writetype
[0] = TRUE
;
1757 for (i
= 0; i
< typecnt
; ++i
)
1758 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : -1;
1759 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1762 for (i
= 0; i
< typecnt
; ++i
) {
1763 register char * thisabbr
;
1767 if (indmap
[abbrinds
[i
]] >= 0)
1769 thisabbr
= &chars
[abbrinds
[i
]];
1770 for (j
= 0; j
< thischarcnt
; ++j
)
1771 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1773 if (j
== thischarcnt
) {
1774 (void) strcpy(&thischars
[(int) thischarcnt
],
1776 thischarcnt
+= strlen(thisabbr
) + 1;
1778 indmap
[abbrinds
[i
]] = j
;
1780 #define DO(field) (void) fwrite((void *) tzh.field, \
1781 (size_t) sizeof tzh.field, (size_t) 1, fp)
1784 * (ICUZoneinfoVersion
*) &tzh
.tzh_reserved
= TZ_ICU_VERSION
;
1785 (void) strncpy(tzh
.tzh_magic
, TZ_ICU_MAGIC
, sizeof tzh
.tzh_magic
);
1787 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1789 tzh
.tzh_version
[0] = ZIC_VERSION
;
1790 convert(eitol(thistypecnt
), tzh
.tzh_ttisgmtcnt
);
1791 convert(eitol(thistypecnt
), tzh
.tzh_ttisstdcnt
);
1792 convert(eitol(thisleapcnt
), tzh
.tzh_leapcnt
);
1793 convert(eitol(thistimecnt
), tzh
.tzh_timecnt
);
1794 convert(eitol(thistypecnt
), tzh
.tzh_typecnt
);
1795 convert(eitol(thischarcnt
), tzh
.tzh_charcnt
);
1806 for (i
= thistimei
; i
< thistimelim
; ++i
)
1808 puttzcode((long) ats
[i
], fp
);
1809 else puttzcode64(ats
[i
], fp
);
1810 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1813 uc
= typemap
[types
[i
]];
1814 (void) fwrite((void *) &uc
,
1819 for (i
= 0; i
< typecnt
; ++i
)
1822 puttzcode((long) rawoffs
[i
], fp
);
1823 puttzcode((long) dstoffs
[i
], fp
);
1825 puttzcode(gmtoffs
[i
], fp
);
1827 (void) putc(isdsts
[i
], fp
);
1828 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1830 if (thischarcnt
!= 0)
1831 (void) fwrite((void *) thischars
,
1832 (size_t) sizeof thischars
[0],
1833 (size_t) thischarcnt
, fp
);
1834 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1835 register zic_t todo
;
1838 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1841 if (++j
>= typecnt
) {
1847 while (j
< timecnt
&&
1852 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1853 } else todo
= trans
[i
];
1855 puttzcode((long) todo
, fp
);
1856 else puttzcode64(todo
, fp
);
1857 puttzcode(corr
[i
], fp
);
1859 for (i
= 0; i
< typecnt
; ++i
)
1861 (void) putc(ttisstds
[i
], fp
);
1862 for (i
= 0; i
< typecnt
; ++i
)
1864 (void) putc(ttisgmts
[i
], fp
);
1866 (void) fprintf(fp
, "\n%s\n", string
);
1867 if (ferror(fp
) || fclose(fp
)) {
1868 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1869 progname
, fullname
);
1875 doabbr(abbr
, format
, letters
, isdst
, doquotes
)
1877 const char * const format
;
1878 const char * const letters
;
1883 register char * slashp
;
1886 slashp
= strchr(format
, '/');
1887 if (slashp
== NULL
) {
1888 if (letters
== NULL
)
1889 (void) strcpy(abbr
, format
);
1890 else (void) sprintf(abbr
, format
, letters
);
1892 (void) strcpy(abbr
, slashp
+ 1);
1894 if (slashp
> format
)
1895 (void) strncpy(abbr
, format
,
1896 (unsigned) (slashp
- format
));
1897 abbr
[slashp
- format
] = '\0';
1901 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1902 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1903 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1906 if (len
> 0 && *cp
== '\0')
1908 abbr
[len
+ 2] = '\0';
1909 abbr
[len
+ 1] = '>';
1910 for ( ; len
> 0; --len
)
1911 abbr
[len
] = abbr
[len
- 1];
1926 stringoffset(result
, offset
)
1931 register int minutes
;
1932 register int seconds
;
1936 (void) strcpy(result
, "-");
1939 seconds
= offset
% SECSPERMIN
;
1940 offset
/= SECSPERMIN
;
1941 minutes
= offset
% MINSPERHOUR
;
1942 offset
/= MINSPERHOUR
;
1944 if (hours
>= HOURSPERDAY
) {
1948 (void) sprintf(end(result
), "%d", hours
);
1949 if (minutes
!= 0 || seconds
!= 0) {
1950 (void) sprintf(end(result
), ":%02d", minutes
);
1952 (void) sprintf(end(result
), ":%02d", seconds
);
1958 stringrule(result
, rp
, dstoff
, gmtoff
)
1960 const struct rule
* const rp
;
1966 result
= end(result
);
1967 if (rp
->r_dycode
== DC_DOM
) {
1968 register int month
, total
;
1970 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1973 for (month
= 0; month
< rp
->r_month
; ++month
)
1974 total
+= len_months
[0][month
];
1975 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1979 if (rp
->r_dycode
== DC_DOWGEQ
) {
1980 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1981 if ((week
- 1) * DAYSPERWEEK
+ 1 != rp
->r_dayofmonth
)
1983 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1984 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1987 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1988 if (week
* DAYSPERWEEK
- 1 != rp
->r_dayofmonth
)
1991 } else return -1; /* "cannot happen" */
1992 (void) sprintf(result
, "M%d.%d.%d",
1993 rp
->r_month
+ 1, week
, rp
->r_wday
);
1998 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
2004 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
2005 (void) strcat(result
, "/");
2006 if (stringoffset(end(result
), tod
) != 0)
2013 stringzone(result
, zpfirst
, zonecount
)
2015 const struct zone
* const zpfirst
;
2016 const int zonecount
;
2018 register const struct zone
* zp
;
2019 register struct rule
* rp
;
2020 register struct rule
* stdrp
;
2021 register struct rule
* dstrp
;
2023 register const char * abbrvar
;
2026 zp
= zpfirst
+ zonecount
- 1;
2027 stdrp
= dstrp
= NULL
;
2028 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
2029 rp
= &zp
->z_rules
[i
];
2030 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= INT_MAX
)
2032 if (rp
->r_yrtype
!= NULL
)
2034 if (rp
->r_stdoff
== 0) {
2044 if (stdrp
== NULL
&& dstrp
== NULL
) {
2046 ** There are no rules running through "max".
2047 ** Let's find the latest rule.
2049 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
2050 rp
= &zp
->z_rules
[i
];
2051 if (stdrp
== NULL
|| rp
->r_hiyear
> stdrp
->r_hiyear
||
2052 (rp
->r_hiyear
== stdrp
->r_hiyear
&&
2053 rp
->r_month
> stdrp
->r_month
))
2056 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0)
2057 return; /* We end up in DST (a POSIX no-no). */
2059 ** Horrid special case: if year is 2037,
2060 ** presume this is a zone handled on a year-by-year basis;
2061 ** do not try to apply a rule to the zone.
2063 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
2066 if (stdrp
== NULL
&& zp
->z_nrules
!= 0)
2068 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
2069 doabbr(result
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
2070 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
2076 doabbr(end(result
), zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
2077 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
2078 if (stringoffset(end(result
),
2079 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
2083 (void) strcat(result
, ",");
2084 if (stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
2088 (void) strcat(result
, ",");
2089 if (stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
2096 outzone(zpfirst
, zonecount
)
2097 const struct zone
* const zpfirst
;
2098 const int zonecount
;
2100 register const struct zone
* zp
;
2101 register struct rule
* rp
;
2103 register int usestart
, useuntil
;
2104 register zic_t starttime
, untiltime
;
2105 register long gmtoff
;
2106 register long stdoff
;
2108 register long startoff
;
2109 register int startttisstd
;
2110 register int startttisgmt
;
2112 register char * startbuf
;
2114 register char * envvar
;
2115 register int max_abbr_len
;
2116 register int max_envvar_len
;
2118 int finalRuleYear
, finalRuleIndex
;
2119 const struct rule
* finalRule1
;
2120 const struct rule
* finalRule2
;
2123 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
2124 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
2125 startbuf
= emalloc(max_abbr_len
+ 1);
2126 ab
= emalloc(max_abbr_len
+ 1);
2127 envvar
= emalloc(max_envvar_len
+ 1);
2128 INITIALIZE(untiltime
);
2129 INITIALIZE(starttime
);
2131 ** Now. . .finally. . .generate some useful data!
2137 ** Thanks to Earl Chew
2138 ** for noting the need to unconditionally initialize startttisstd.
2140 startttisstd
= FALSE
;
2141 startttisgmt
= FALSE
;
2142 min_year
= max_year
= EPOCH_YEAR
;
2144 updateminmax(leapminyear
);
2145 updateminmax(leapmaxyear
);
2147 for (i
= 0; i
< zonecount
; ++i
) {
2149 if (i
< zonecount
- 1)
2150 updateminmax(zp
->z_untilrule
.r_loyear
);
2151 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2152 rp
= &zp
->z_rules
[j
];
2154 updateminmax(rp
->r_loyear
);
2156 updateminmax(rp
->r_hiyear
);
2160 ** Generate lots of data if a rule can't cover all future times.
2162 stringzone(envvar
, zpfirst
, zonecount
);
2163 if (noise
&& envvar
[0] == '\0') {
2166 wp
= ecpyalloc(_("no POSIX environment variable for zone"));
2167 wp
= ecatalloc(wp
, " ");
2168 wp
= ecatalloc(wp
, zpfirst
->z_name
);
2172 if (envvar
[0] == '\0') {
2173 if (min_year
>= INT_MIN
+ YEARSPERREPEAT
)
2174 min_year
-= YEARSPERREPEAT
;
2175 else min_year
= INT_MIN
;
2176 if (max_year
<= INT_MAX
- YEARSPERREPEAT
)
2177 max_year
+= YEARSPERREPEAT
;
2178 else max_year
= INT_MAX
;
2181 ** For the benefit of older systems,
2182 ** generate data from 1900 through 2037.
2184 if (min_year
> 1900)
2186 if (max_year
< 2037)
2188 for (i
= 0; i
< zonecount
; ++i
) {
2190 ** A guess that may well be corrected later.
2194 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2195 useuntil
= i
< (zonecount
- 1);
2196 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2198 gmtoff
= zp
->z_gmtoff
;
2199 eat(zp
->z_filename
, zp
->z_linenum
);
2201 startoff
= zp
->z_gmtoff
;
2203 finalRuleYear
= finalRuleIndex
= -1;
2204 finalRule1
= finalRule2
= NULL
;
2205 if (i
== (zonecount
- 1)) { /* !useuntil */
2206 /* Look for exactly 2 rules that end at 'max' and
2207 * note them. Determine max(r_loyear) for the 2 of
2209 for (j
=0; j
<zp
->z_nrules
; ++j
) {
2210 rp
= &zp
->z_rules
[j
];
2211 if (rp
->r_hiyear
== INT_MAX
) {
2212 if (finalRule1
== NULL
) {
2214 finalRuleYear
= rp
->r_loyear
;
2215 } else if (finalRule2
== NULL
) {
2217 if (rp
->r_loyear
> finalRuleYear
) {
2218 finalRuleYear
= rp
->r_loyear
;
2221 error("more than two max rules found (ICU)");
2226 if (finalRule1
!= NULL
&& finalRule2
== NULL
) {
2227 error("only one max rule found (ICU)");
2230 if (finalRule1
!= NULL
) {
2231 if (finalRule1
->r_stdoff
== finalRule2
->r_stdoff
) {
2232 /* America/Resolute in 2009a uses a pair of rules
2233 * which does not change the offset. ICU ignores
2234 * such rules without actual time transitions. */
2235 finalRuleYear
= finalRuleIndex
= -1;
2236 finalRule1
= finalRule2
= NULL
;
2238 /* Swap if necessary so finalRule1 occurs before
2240 if (finalRule1
->r_month
> finalRule2
->r_month
) {
2241 const struct rule
* t
= finalRule1
;
2242 finalRule1
= finalRule2
;
2245 /* Add final rule to our list */
2246 finalRuleIndex
= add_icu_final_rules(finalRule1
, finalRule2
);
2252 if (zp
->z_nrules
== 0) {
2253 stdoff
= zp
->z_stdoff
;
2254 doabbr(startbuf
, zp
->z_format
,
2255 (char *) NULL
, stdoff
!= 0, FALSE
);
2256 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2258 zp
->z_gmtoff
, stdoff
,
2260 startbuf
, stdoff
!= 0, startttisstd
,
2263 addtt(starttime
, type
);
2265 } else if (stdoff
!= 0)
2266 addtt(min_time
, type
);
2267 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2268 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2271 ** Mark which rules to do in the current year.
2272 ** For those to do, calculate rpytime(rp, year);
2274 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2275 rp
= &zp
->z_rules
[j
];
2276 eats(zp
->z_filename
, zp
->z_linenum
,
2277 rp
->r_filename
, rp
->r_linenum
);
2278 rp
->r_todo
= year
>= rp
->r_loyear
&&
2279 year
<= rp
->r_hiyear
&&
2280 yearistype(year
, rp
->r_yrtype
);
2282 rp
->r_temp
= rpytime(rp
, year
);
2286 register zic_t jtime
, ktime
;
2287 register long offset
;
2292 ** Turn untiltime into UTC
2293 ** assuming the current gmtoff and
2296 untiltime
= zp
->z_untiltime
;
2297 if (!zp
->z_untilrule
.r_todisgmt
)
2298 untiltime
= tadd(untiltime
,
2300 if (!zp
->z_untilrule
.r_todisstd
)
2301 untiltime
= tadd(untiltime
,
2305 ** Find the rule (of those to do, if any)
2306 ** that takes effect earliest in the year.
2309 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2310 rp
= &zp
->z_rules
[j
];
2313 eats(zp
->z_filename
, zp
->z_linenum
,
2314 rp
->r_filename
, rp
->r_linenum
);
2315 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2316 if (!rp
->r_todisstd
)
2317 offset
= oadd(offset
, stdoff
);
2319 if (jtime
== min_time
||
2322 jtime
= tadd(jtime
, -offset
);
2323 if (k
< 0 || jtime
< ktime
) {
2329 break; /* go on to next year */
2330 rp
= &zp
->z_rules
[k
];
2332 if (useuntil
&& ktime
>= untiltime
)
2334 stdoff
= rp
->r_stdoff
;
2335 if (usestart
&& ktime
== starttime
)
2338 if (ktime
< starttime
) {
2339 startoff
= oadd(zp
->z_gmtoff
,
2341 doabbr(startbuf
, zp
->z_format
,
2347 if (*startbuf
== '\0' &&
2348 startoff
== oadd(zp
->z_gmtoff
,
2359 if (year
>= finalRuleYear
&& rp
== finalRule1
) {
2360 /* We want to shift final year 1 year after
2361 * the actual final rule takes effect (year + 1),
2362 * because the previous type is valid until the first
2363 * transition defined by the final rule. Otherwise
2364 * we may see unexpected offset shift at the
2365 * begining of the year when the final rule takes
2368 /* ICU currently can support signed int32 transition
2369 * times. Thus, the transitions in year 2038 may be
2370 * truncated. At this moment (tzdata2008g), only
2371 * Rule Brazil is impacted by this limitation, because
2372 * the final set of rules are starting in 2038. Although
2373 * this code put the first couple of transitions populated
2374 * by the final rules, they might be dropped off when
2375 * collecting transition times by tz2icu. */
2376 emit_icu_zone(icuFile
,
2377 zpfirst
->z_name
, zp
->z_gmtoff
,
2378 rp
, finalRuleIndex
, year
+ 1);
2379 /* only emit this for the first year */
2383 eats(zp
->z_filename
, zp
->z_linenum
,
2384 rp
->r_filename
, rp
->r_linenum
);
2385 doabbr(ab
, zp
->z_format
, rp
->r_abbrvar
,
2386 rp
->r_stdoff
!= 0, FALSE
);
2387 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2389 type
= addtype(offset
, zp
->z_gmtoff
, rp
->r_stdoff
,
2390 ab
, rp
->r_stdoff
!= 0,
2391 rp
->r_todisstd
, rp
->r_todisgmt
);
2393 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2394 rp
->r_todisstd
, rp
->r_todisgmt
);
2400 if (*startbuf
== '\0' &&
2401 zp
->z_format
!= NULL
&&
2402 strchr(zp
->z_format
, '%') == NULL
&&
2403 strchr(zp
->z_format
, '/') == NULL
)
2404 (void) strcpy(startbuf
, zp
->z_format
);
2405 eat(zp
->z_filename
, zp
->z_linenum
);
2406 if (*startbuf
== '\0')
2407 error(_("can't determine time zone abbreviation to use just after until time"));
2408 else addtt(starttime
,
2411 zp
->z_gmtoff
, startoff
- zp
->z_gmtoff
,
2413 startoff
!= zp
->z_gmtoff
,
2417 addtype(startoff
, startbuf
,
2418 startoff
!= zp
->z_gmtoff
,
2424 ** Now we may get to set starttime for the next zone line.
2427 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2428 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2429 starttime
= zp
->z_untiltime
;
2431 starttime
= tadd(starttime
, -stdoff
);
2433 starttime
= tadd(starttime
, -gmtoff
);
2436 writezone(zpfirst
->z_name
, envvar
);
2443 addtt(starttime
, type
)
2444 const zic_t starttime
;
2447 if (starttime
<= min_time
||
2448 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2449 gmtoffs
[0] = gmtoffs
[type
];
2451 rawoffs
[0] = rawoffs
[type
];
2452 dstoffs
[0] = dstoffs
[type
];
2454 isdsts
[0] = isdsts
[type
];
2455 ttisstds
[0] = ttisstds
[type
];
2456 ttisgmts
[0] = ttisgmts
[type
];
2457 if (abbrinds
[type
] != 0)
2458 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2460 charcnt
= strlen(chars
) + 1;
2465 if (timecnt
>= TZ_MAX_TIMES
) {
2466 error(_("too many transitions?!"));
2469 attypes
[timecnt
].at
= starttime
;
2470 attypes
[timecnt
].type
= type
;
2476 addtype(gmtoff
, rawoff
, dstoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
2481 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
2484 const char * const abbr
;
2491 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2492 error(_("internal error - addtype called with bad isdst"));
2495 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2496 error(_("internal error - addtype called with bad ttisstd"));
2499 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2500 error(_("internal error - addtype called with bad ttisgmt"));
2504 if (isdst
!= (dstoff
!= 0)) {
2505 error(_("internal error - addtype called with bad isdst/dstoff"));
2506 (void) exit(EXIT_FAILURE
);
2508 if (gmtoff
!= (rawoff
+ dstoff
)) {
2509 error(_("internal error - addtype called with bad gmt/raw/dstoff"));
2510 (void) exit(EXIT_FAILURE
);
2514 ** See if there's already an entry for this zone type.
2515 ** If so, just return its index.
2517 for (i
= 0; i
< typecnt
; ++i
) {
2518 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2520 rawoff
== rawoffs
[i
] && dstoff
== dstoffs
[i
] &&
2522 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2523 ttisstd
== ttisstds
[i
] &&
2524 ttisgmt
== ttisgmts
[i
])
2528 ** There isn't one; add a new one, unless there are already too
2531 if (typecnt
>= TZ_MAX_TYPES
) {
2532 error(_("too many local time types"));
2535 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2536 error(_("UTC offset out of range"));
2539 gmtoffs
[i
] = gmtoff
;
2541 rawoffs
[i
] = rawoff
;
2542 dstoffs
[i
] = dstoff
;
2545 ttisstds
[i
] = ttisstd
;
2546 ttisgmts
[i
] = ttisgmt
;
2548 for (j
= 0; j
< charcnt
; ++j
)
2549 if (strcmp(&chars
[j
], abbr
) == 0)
2559 leapadd(t
, positive
, rolling
, count
)
2567 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2568 error(_("too many leap seconds"));
2571 for (i
= 0; i
< leapcnt
; ++i
)
2572 if (t
<= trans
[i
]) {
2573 if (t
== trans
[i
]) {
2574 error(_("repeated leap second moment"));
2580 for (j
= leapcnt
; j
> i
; --j
) {
2581 trans
[j
] = trans
[j
- 1];
2582 corr
[j
] = corr
[j
- 1];
2583 roll
[j
] = roll
[j
- 1];
2586 corr
[i
] = positive
? 1L : eitol(-count
);
2589 } while (positive
&& --count
!= 0);
2596 register long last
= 0;
2599 ** propagate leap seconds forward
2601 for (i
= 0; i
< leapcnt
; ++i
) {
2602 trans
[i
] = tadd(trans
[i
], last
);
2603 last
= corr
[i
] += last
;
2608 yearistype(year
, type
)
2610 const char * const type
;
2615 if (type
== NULL
|| *type
== '\0')
2617 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
2618 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
2619 result
= system(buf
);
2620 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2626 error(_("Wild result from command execution"));
2627 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2628 progname
, buf
, result
);
2637 a
= (unsigned char) a
;
2638 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2642 ciequal(ap
, bp
) /* case-insensitive equality */
2643 register const char * ap
;
2644 register const char * bp
;
2646 while (lowerit(*ap
) == lowerit(*bp
++))
2654 register const char * abbr
;
2655 register const char * word
;
2657 if (lowerit(*abbr
) != lowerit(*word
))
2660 while (*++abbr
!= '\0')
2664 } while (lowerit(*word
++) != lowerit(*abbr
));
2668 static const struct lookup
*
2670 register const char * const word
;
2671 register const struct lookup
* const table
;
2673 register const struct lookup
* foundlp
;
2674 register const struct lookup
* lp
;
2676 if (word
== NULL
|| table
== NULL
)
2679 ** Look for exact match.
2681 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2682 if (ciequal(word
, lp
->l_word
))
2685 ** Look for inexact match.
2688 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2689 if (itsabbr(word
, lp
->l_word
)) {
2690 if (foundlp
== NULL
)
2692 else return NULL
; /* multiple inexact matches */
2702 register char ** array
;
2707 array
= (char **) (void *)
2708 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2711 while (isascii((unsigned char) *cp
) &&
2712 isspace((unsigned char) *cp
))
2714 if (*cp
== '\0' || *cp
== '#')
2716 array
[nsubs
++] = dp
= cp
;
2718 if ((*dp
= *cp
++) != '"')
2720 else while ((*dp
= *cp
++) != '"')
2725 "Odd number of quotation marks"
2729 } while (*cp
!= '\0' && *cp
!= '#' &&
2730 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2731 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2735 array
[nsubs
] = NULL
;
2747 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2748 error(_("time overflow"));
2761 if (t1
== max_time
&& t2
> 0)
2763 if (t1
== min_time
&& t2
< 0)
2766 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2767 error(_("time overflow"));
2774 ** Given a rule, and a year, compute the date - in seconds since January 1,
2775 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2779 rpytime(rp
, wantedy
)
2780 register const struct rule
* const rp
;
2781 register const int wantedy
;
2783 register int y
, m
, i
;
2784 register long dayoff
; /* with a nod to Margaret O. */
2787 if (wantedy
== INT_MIN
)
2789 if (wantedy
== INT_MAX
)
2794 while (wantedy
!= y
) {
2796 i
= len_years
[isleap(y
)];
2800 i
= -len_years
[isleap(y
)];
2802 dayoff
= oadd(dayoff
, eitol(i
));
2804 while (m
!= rp
->r_month
) {
2805 i
= len_months
[isleap(y
)][m
];
2806 dayoff
= oadd(dayoff
, eitol(i
));
2809 i
= rp
->r_dayofmonth
;
2810 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2811 if (rp
->r_dycode
== DC_DOWLEQ
)
2814 error(_("use of 2/29 in non leap-year"));
2819 dayoff
= oadd(dayoff
, eitol(i
));
2820 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2823 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2824 wday
= eitol(EPOCH_WDAY
);
2826 ** Don't trust mod of negative numbers.
2829 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2831 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2833 wday
+= LDAYSPERWEEK
;
2835 while (wday
!= eitol(rp
->r_wday
))
2836 if (rp
->r_dycode
== DC_DOWGEQ
) {
2837 dayoff
= oadd(dayoff
, (long) 1);
2838 if (++wday
>= LDAYSPERWEEK
)
2842 dayoff
= oadd(dayoff
, (long) -1);
2844 wday
= LDAYSPERWEEK
- 1;
2847 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2849 warning(_("rule goes past start/end of month--\
2850 will not work with pre-2004 versions of zic"));
2853 if (dayoff
< min_time
/ SECSPERDAY
)
2855 if (dayoff
> max_time
/ SECSPERDAY
)
2857 t
= (zic_t
) dayoff
* SECSPERDAY
;
2858 return tadd(t
, rp
->r_tod
);
2863 const char * const string
;
2867 if (strcmp(string
, GRANDPARENTED
) != 0) {
2868 register const char * cp
;
2872 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2873 ** optionally followed by a + or - and a number from 1 to 14.
2877 while (isascii((unsigned char) *cp
) &&
2878 isalpha((unsigned char) *cp
))
2880 if (cp
- string
== 0)
2881 wp
= _("time zone abbreviation lacks alphabetic at start");
2882 if (noise
&& cp
- string
> 3)
2883 wp
= _("time zone abbreviation has more than 3 alphabetics");
2884 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2885 wp
= _("time zone abbreviation has too many alphabetics");
2886 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2888 if (isascii((unsigned char) *cp
) &&
2889 isdigit((unsigned char) *cp
))
2891 *cp
>= '0' && *cp
<= '4')
2895 wp
= _("time zone abbreviation differs from POSIX standard");
2898 wp
= ecatalloc(wp
, " (");
2899 wp
= ecatalloc(wp
, string
);
2900 wp
= ecatalloc(wp
, ")");
2905 i
= strlen(string
) + 1;
2906 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2907 error(_("too many, or too long, time zone abbreviations"));
2910 (void) strcpy(&chars
[charcnt
], string
);
2911 charcnt
+= eitol(i
);
2918 register char * name
;
2921 if (argname
== NULL
|| *argname
== '\0')
2923 cp
= name
= ecpyalloc(argname
);
2924 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2928 ** DOS drive specifier?
2930 if (isalpha((unsigned char) name
[0]) &&
2931 name
[1] == ':' && name
[2] == '\0') {
2935 #endif /* !defined unix */
2936 if (!itsdir(name
)) {
2938 ** It doesn't seem to exist, so we try to create it.
2939 ** Creation may fail because of the directory being
2940 ** created by some other multiprocessor, so we get
2941 ** to do extra checking.
2943 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2944 const char *e
= strerror(errno
);
2946 if (errno
!= EEXIST
|| !itsdir(name
)) {
2947 (void) fprintf(stderr
,
2948 _("%s: Can't create directory %s: %s\n"),
2968 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2969 (void) fprintf(stderr
,
2970 _("%s: %d did not sign extend correctly\n"),
2978 ** UNIX was a registered trademark of The Open Group in 2003.