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