Regenerate sparc ULPs.
[glibc.git] / timezone / zic.c
blobe7b0081193299155d7a5a36534cc3980b8acf1c6
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
4 */
6 #include "version.h"
7 #include "private.h"
8 #include "locale.h"
9 #include "tzfile.h"
11 #include <stdarg.h>
13 #define ZIC_VERSION_PRE_2013 '2'
14 #define ZIC_VERSION '3'
16 typedef int_fast64_t zic_t;
17 #define ZIC_MIN INT_FAST64_MIN
18 #define ZIC_MAX INT_FAST64_MAX
19 #define SCNdZIC SCNdFAST64
21 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
22 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
23 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
25 #if HAVE_SYS_STAT_H
26 #include "sys/stat.h"
27 #endif
28 #ifdef S_IRUSR
29 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
30 #else
31 #define MKDIR_UMASK 0755
32 #endif
35 ** On some ancient hosts, predicates like `isspace(C)' are defined
36 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
37 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
38 ** Neither the C Standard nor Posix require that `isascii' exist.
39 ** For portability, we check both ancient and modern requirements.
40 ** If isascii is not defined, the isascii check succeeds trivially.
42 #include "ctype.h"
43 #ifndef isascii
44 #define isascii(x) 1
45 #endif
47 #define end(cp) (strchr((cp), '\0'))
49 struct rule {
50 const char * r_filename;
51 int r_linenum;
52 const char * r_name;
54 zic_t r_loyear; /* for example, 1986 */
55 zic_t r_hiyear; /* for example, 1986 */
56 const char * r_yrtype;
57 int r_lowasnum;
58 int r_hiwasnum;
60 int r_month; /* 0..11 */
62 int r_dycode; /* see below */
63 int r_dayofmonth;
64 int r_wday;
66 zic_t r_tod; /* time from midnight */
67 int r_todisstd; /* above is standard time if TRUE */
68 /* or wall clock time if FALSE */
69 int r_todisgmt; /* above is GMT if TRUE */
70 /* or local time if FALSE */
71 zic_t r_stdoff; /* offset from standard time */
72 const char * r_abbrvar; /* variable part of abbreviation */
74 int r_todo; /* a rule to do (used in outzone) */
75 zic_t r_temp; /* used in outzone */
79 ** r_dycode r_dayofmonth r_wday
82 #define DC_DOM 0 /* 1..31 */ /* unused */
83 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
84 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
86 struct zone {
87 const char * z_filename;
88 int z_linenum;
90 const char * z_name;
91 zic_t z_gmtoff;
92 const char * z_rule;
93 const char * z_format;
95 zic_t z_stdoff;
97 struct rule * z_rules;
98 int z_nrules;
100 struct rule z_untilrule;
101 zic_t z_untiltime;
104 extern int getopt(int argc, char * const argv[],
105 const char * options);
106 extern int link(const char * fromname, const char * toname);
107 extern char * optarg;
108 extern int optind;
110 #if ! HAVE_LINK
111 # define link(from, to) (-1)
112 #endif
113 #if ! HAVE_SYMLINK
114 # define symlink(from, to) (-1)
115 #endif
117 static void addtt(zic_t starttime, int type);
118 static int addtype(zic_t gmtoff, const char * abbr, int isdst,
119 int ttisstd, int ttisgmt);
120 static void leapadd(zic_t t, int positive, int rolling, int count);
121 static void adjleap(void);
122 static void associate(void);
123 static void dolink(const char * fromfield, const char * tofield);
124 static char ** getfields(char * buf);
125 static zic_t gethms(const char * string, const char * errstrng,
126 int signable);
127 static void infile(const char * filename);
128 static void inleap(char ** fields, int nfields);
129 static void inlink(char ** fields, int nfields);
130 static void inrule(char ** fields, int nfields);
131 static int inzcont(char ** fields, int nfields);
132 static int inzone(char ** fields, int nfields);
133 static int inzsub(char ** fields, int nfields, int iscont);
134 static int itsdir(const char * name);
135 static int lowerit(int c);
136 static int mkdirs(char * filename);
137 static void newabbr(const char * abbr);
138 static zic_t oadd(zic_t t1, zic_t t2);
139 static void outzone(const struct zone * zp, int ntzones);
140 static zic_t rpytime(const struct rule * rp, zic_t wantedy);
141 static void rulesub(struct rule * rp,
142 const char * loyearp, const char * hiyearp,
143 const char * typep, const char * monthp,
144 const char * dayp, const char * timep);
145 static zic_t tadd(zic_t t1, zic_t t2);
146 static int yearistype(int year, const char * type);
148 static int charcnt;
149 static int errors;
150 static const char * filename;
151 static int leapcnt;
152 static int leapseen;
153 static zic_t leapminyear;
154 static zic_t leapmaxyear;
155 static int linenum;
156 static int max_abbrvar_len;
157 static int max_format_len;
158 static zic_t max_year;
159 static zic_t min_year;
160 static int noise;
161 static const char * rfilename;
162 static int rlinenum;
163 static const char * progname;
164 static int timecnt;
165 static int typecnt;
168 ** Line codes.
171 #define LC_RULE 0
172 #define LC_ZONE 1
173 #define LC_LINK 2
174 #define LC_LEAP 3
177 ** Which fields are which on a Zone line.
180 #define ZF_NAME 1
181 #define ZF_GMTOFF 2
182 #define ZF_RULE 3
183 #define ZF_FORMAT 4
184 #define ZF_TILYEAR 5
185 #define ZF_TILMONTH 6
186 #define ZF_TILDAY 7
187 #define ZF_TILTIME 8
188 #define ZONE_MINFIELDS 5
189 #define ZONE_MAXFIELDS 9
192 ** Which fields are which on a Zone continuation line.
195 #define ZFC_GMTOFF 0
196 #define ZFC_RULE 1
197 #define ZFC_FORMAT 2
198 #define ZFC_TILYEAR 3
199 #define ZFC_TILMONTH 4
200 #define ZFC_TILDAY 5
201 #define ZFC_TILTIME 6
202 #define ZONEC_MINFIELDS 3
203 #define ZONEC_MAXFIELDS 7
206 ** Which files are which on a Rule line.
209 #define RF_NAME 1
210 #define RF_LOYEAR 2
211 #define RF_HIYEAR 3
212 #define RF_COMMAND 4
213 #define RF_MONTH 5
214 #define RF_DAY 6
215 #define RF_TOD 7
216 #define RF_STDOFF 8
217 #define RF_ABBRVAR 9
218 #define RULE_FIELDS 10
221 ** Which fields are which on a Link line.
224 #define LF_FROM 1
225 #define LF_TO 2
226 #define LINK_FIELDS 3
229 ** Which fields are which on a Leap line.
232 #define LP_YEAR 1
233 #define LP_MONTH 2
234 #define LP_DAY 3
235 #define LP_TIME 4
236 #define LP_CORR 5
237 #define LP_ROLL 6
238 #define LEAP_FIELDS 7
241 ** Year synonyms.
244 #define YR_MINIMUM 0
245 #define YR_MAXIMUM 1
246 #define YR_ONLY 2
248 static struct rule * rules;
249 static int nrules; /* number of rules */
251 static struct zone * zones;
252 static int nzones; /* number of zones */
254 struct link {
255 const char * l_filename;
256 int l_linenum;
257 const char * l_from;
258 const char * l_to;
261 static struct link * links;
262 static int nlinks;
264 struct lookup {
265 const char * l_word;
266 const int l_value;
269 static struct lookup const * byword(const char * string,
270 const struct lookup * lp);
272 static struct lookup const line_codes[] = {
273 { "Rule", LC_RULE },
274 { "Zone", LC_ZONE },
275 { "Link", LC_LINK },
276 { "Leap", LC_LEAP },
277 { NULL, 0}
280 static struct lookup const mon_names[] = {
281 { "January", TM_JANUARY },
282 { "February", TM_FEBRUARY },
283 { "March", TM_MARCH },
284 { "April", TM_APRIL },
285 { "May", TM_MAY },
286 { "June", TM_JUNE },
287 { "July", TM_JULY },
288 { "August", TM_AUGUST },
289 { "September", TM_SEPTEMBER },
290 { "October", TM_OCTOBER },
291 { "November", TM_NOVEMBER },
292 { "December", TM_DECEMBER },
293 { NULL, 0 }
296 static struct lookup const wday_names[] = {
297 { "Sunday", TM_SUNDAY },
298 { "Monday", TM_MONDAY },
299 { "Tuesday", TM_TUESDAY },
300 { "Wednesday", TM_WEDNESDAY },
301 { "Thursday", TM_THURSDAY },
302 { "Friday", TM_FRIDAY },
303 { "Saturday", TM_SATURDAY },
304 { NULL, 0 }
307 static struct lookup const lasts[] = {
308 { "last-Sunday", TM_SUNDAY },
309 { "last-Monday", TM_MONDAY },
310 { "last-Tuesday", TM_TUESDAY },
311 { "last-Wednesday", TM_WEDNESDAY },
312 { "last-Thursday", TM_THURSDAY },
313 { "last-Friday", TM_FRIDAY },
314 { "last-Saturday", TM_SATURDAY },
315 { NULL, 0 }
318 static struct lookup const begin_years[] = {
319 { "minimum", YR_MINIMUM },
320 { "maximum", YR_MAXIMUM },
321 { NULL, 0 }
324 static struct lookup const end_years[] = {
325 { "minimum", YR_MINIMUM },
326 { "maximum", YR_MAXIMUM },
327 { "only", YR_ONLY },
328 { NULL, 0 }
331 static struct lookup const leap_types[] = {
332 { "Rolling", TRUE },
333 { "Stationary", FALSE },
334 { NULL, 0 }
337 static const int len_months[2][MONSPERYEAR] = {
338 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
339 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
342 static const int len_years[2] = {
343 DAYSPERNYEAR, DAYSPERLYEAR
346 static struct attype {
347 zic_t at;
348 unsigned char type;
349 } attypes[TZ_MAX_TIMES];
350 static zic_t gmtoffs[TZ_MAX_TYPES];
351 static char isdsts[TZ_MAX_TYPES];
352 static unsigned char abbrinds[TZ_MAX_TYPES];
353 static char ttisstds[TZ_MAX_TYPES];
354 static char ttisgmts[TZ_MAX_TYPES];
355 static char chars[TZ_MAX_CHARS];
356 static zic_t trans[TZ_MAX_LEAPS];
357 static zic_t corr[TZ_MAX_LEAPS];
358 static char roll[TZ_MAX_LEAPS];
361 ** Memory allocation.
364 static ATTRIBUTE_PURE void *
365 memcheck(void *const ptr)
367 if (ptr == NULL) {
368 const char *e = strerror(errno);
370 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
371 progname, e);
372 exit(EXIT_FAILURE);
374 return ptr;
377 #define emalloc(size) memcheck(malloc(size))
378 #define erealloc(ptr, size) memcheck(realloc(ptr, size))
379 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
380 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
383 ** Error handling.
386 static void
387 eats(const char *const name, const int num, const char *const rname,
388 const int rnum)
390 filename = name;
391 linenum = num;
392 rfilename = rname;
393 rlinenum = rnum;
396 static void
397 eat(const char *const name, const int num)
399 eats(name, num, NULL, -1);
402 static void ATTRIBUTE_FORMAT((printf, 1, 0))
403 verror(const char *const string, va_list args)
406 ** Match the format of "cc" to allow sh users to
407 ** zic ... 2>&1 | error -t "*" -v
408 ** on BSD systems.
410 fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
411 vfprintf(stderr, string, args);
412 if (rfilename != NULL)
413 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
414 rfilename, rlinenum);
415 (void) fprintf(stderr, "\n");
416 ++errors;
419 static void ATTRIBUTE_FORMAT((printf, 1, 2))
420 error(const char *const string, ...)
422 va_list args;
423 va_start(args, string);
424 verror(string, args);
425 va_end(args);
428 static void ATTRIBUTE_FORMAT((printf, 1, 2))
429 warning(const char *const string, ...)
431 va_list args;
432 fprintf(stderr, _("warning: "));
433 va_start(args, string);
434 verror(string, args);
435 va_end(args);
436 --errors;
439 static _Noreturn void
440 usage(FILE *stream, int status)
442 (void) fprintf(stream, _("%s: usage is %s \
443 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
444 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
446 Report bugs to %s.\n"),
447 progname, progname, REPORT_BUGS_TO);
448 exit(status);
451 static const char * psxrules;
452 static const char * lcltime;
453 static const char * directory;
454 static const char * leapsec;
455 static const char * yitcommand;
458 main(int argc, char **argv)
460 register int i;
461 register int j;
462 register int c;
464 #ifdef S_IWGRP
465 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
466 #endif
467 #if HAVE_GETTEXT
468 (void) setlocale(LC_ALL, "");
469 #ifdef TZ_DOMAINDIR
470 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
471 #endif /* defined TEXTDOMAINDIR */
472 (void) textdomain(TZ_DOMAIN);
473 #endif /* HAVE_GETTEXT */
474 progname = argv[0];
475 if (TYPE_BIT(zic_t) < 64) {
476 (void) fprintf(stderr, "%s: %s\n", progname,
477 _("wild compilation-time specification of zic_t"));
478 exit(EXIT_FAILURE);
480 for (i = 1; i < argc; ++i)
481 if (strcmp(argv[i], "--version") == 0) {
482 (void) printf("zic %s%s\n", PKGVERSION, TZVERSION);
483 exit(EXIT_SUCCESS);
484 } else if (strcmp(argv[i], "--help") == 0) {
485 usage(stdout, EXIT_SUCCESS);
487 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
488 switch (c) {
489 default:
490 usage(stderr, EXIT_FAILURE);
491 case 'd':
492 if (directory == NULL)
493 directory = optarg;
494 else {
495 (void) fprintf(stderr,
496 _("%s: More than one -d option specified\n"),
497 progname);
498 exit(EXIT_FAILURE);
500 break;
501 case 'l':
502 if (lcltime == NULL)
503 lcltime = optarg;
504 else {
505 (void) fprintf(stderr,
506 _("%s: More than one -l option specified\n"),
507 progname);
508 exit(EXIT_FAILURE);
510 break;
511 case 'p':
512 if (psxrules == NULL)
513 psxrules = optarg;
514 else {
515 (void) fprintf(stderr,
516 _("%s: More than one -p option specified\n"),
517 progname);
518 exit(EXIT_FAILURE);
520 break;
521 case 'y':
522 if (yitcommand == NULL)
523 yitcommand = optarg;
524 else {
525 (void) fprintf(stderr,
526 _("%s: More than one -y option specified\n"),
527 progname);
528 exit(EXIT_FAILURE);
530 break;
531 case 'L':
532 if (leapsec == NULL)
533 leapsec = optarg;
534 else {
535 (void) fprintf(stderr,
536 _("%s: More than one -L option specified\n"),
537 progname);
538 exit(EXIT_FAILURE);
540 break;
541 case 'v':
542 noise = TRUE;
543 break;
544 case 's':
545 (void) printf("%s: -s ignored\n", progname);
546 break;
548 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
549 usage(stderr, EXIT_FAILURE); /* usage message by request */
550 if (directory == NULL)
551 directory = TZDIR;
552 if (yitcommand == NULL)
553 yitcommand = "yearistype";
555 if (optind < argc && leapsec != NULL) {
556 infile(leapsec);
557 adjleap();
560 for (i = optind; i < argc; ++i)
561 infile(argv[i]);
562 if (errors)
563 exit(EXIT_FAILURE);
564 associate();
565 for (i = 0; i < nzones; i = j) {
567 ** Find the next non-continuation zone entry.
569 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
570 continue;
571 outzone(&zones[i], j - i);
574 ** Make links.
576 for (i = 0; i < nlinks; ++i) {
577 eat(links[i].l_filename, links[i].l_linenum);
578 dolink(links[i].l_from, links[i].l_to);
579 if (noise)
580 for (j = 0; j < nlinks; ++j)
581 if (strcmp(links[i].l_to,
582 links[j].l_from) == 0)
583 warning(_("link to link"));
585 if (lcltime != NULL) {
586 eat("command line", 1);
587 dolink(lcltime, TZDEFAULT);
589 if (psxrules != NULL) {
590 eat("command line", 1);
591 dolink(psxrules, TZDEFRULES);
593 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
596 static void
597 dolink(const char *const fromfield, const char *const tofield)
599 register char * fromname;
600 register char * toname;
602 if (fromfield[0] == '/')
603 fromname = ecpyalloc(fromfield);
604 else {
605 fromname = ecpyalloc(directory);
606 fromname = ecatalloc(fromname, "/");
607 fromname = ecatalloc(fromname, fromfield);
609 if (tofield[0] == '/')
610 toname = ecpyalloc(tofield);
611 else {
612 toname = ecpyalloc(directory);
613 toname = ecatalloc(toname, "/");
614 toname = ecatalloc(toname, tofield);
617 ** We get to be careful here since
618 ** there's a fair chance of root running us.
620 if (!itsdir(toname))
621 (void) remove(toname);
622 if (link(fromname, toname) != 0
623 && access(fromname, F_OK) == 0 && !itsdir(fromname)) {
624 int result;
626 if (mkdirs(toname) != 0)
627 exit(EXIT_FAILURE);
629 result = link(fromname, toname);
630 if (result != 0) {
631 const char *s = fromfield;
632 const char *t;
633 register char * symlinkcontents = NULL;
636 t = s;
637 while ((s = strchr(s, '/'))
638 && ! strncmp (fromfield, tofield,
639 ++s - fromfield));
641 for (s = tofield + (t - fromfield);
642 (s = strchr(s, '/'));
643 s++)
644 symlinkcontents =
645 ecatalloc(symlinkcontents,
646 "../");
647 symlinkcontents = ecatalloc(symlinkcontents, t);
648 result = symlink(symlinkcontents, toname);
649 if (result == 0)
650 warning(_("hard link failed, symbolic link used"));
651 free(symlinkcontents);
653 if (result != 0) {
654 FILE *fp, *tp;
655 int c;
656 fp = fopen(fromname, "rb");
657 if (!fp) {
658 const char *e = strerror(errno);
659 (void) fprintf(stderr,
660 _("%s: Can't read %s: %s\n"),
661 progname, fromname, e);
662 exit(EXIT_FAILURE);
664 tp = fopen(toname, "wb");
665 if (!tp) {
666 const char *e = strerror(errno);
667 (void) fprintf(stderr,
668 _("%s: Can't create %s: %s\n"),
669 progname, toname, e);
670 exit(EXIT_FAILURE);
672 while ((c = getc(fp)) != EOF)
673 putc(c, tp);
674 if (ferror(fp) || fclose(fp)) {
675 (void) fprintf(stderr,
676 _("%s: Error reading %s\n"),
677 progname, fromname);
678 exit(EXIT_FAILURE);
680 if (ferror(tp) || fclose(tp)) {
681 (void) fprintf(stderr,
682 _("%s: Error writing %s\n"),
683 progname, toname);
684 exit(EXIT_FAILURE);
686 warning(_("link failed, copy used"));
689 free(fromname);
690 free(toname);
693 #define TIME_T_BITS_IN_FILE 64
695 static const zic_t min_time = (zic_t) -1 << (TIME_T_BITS_IN_FILE - 1);
696 static const zic_t max_time = -1 - ((zic_t) -1 << (TIME_T_BITS_IN_FILE - 1));
698 static int
699 itsdir(const char *const name)
701 register char * myname;
702 register int accres;
704 myname = ecpyalloc(name);
705 myname = ecatalloc(myname, "/.");
706 accres = access(myname, F_OK);
707 free(myname);
708 return accres == 0;
712 ** Associate sets of rules with zones.
716 ** Sort by rule name.
719 static int
720 rcomp(const void *cp1, const void *cp2)
722 return strcmp(((const struct rule *) cp1)->r_name,
723 ((const struct rule *) cp2)->r_name);
726 static void
727 associate(void)
729 register struct zone * zp;
730 register struct rule * rp;
731 register int base, out;
732 register int i, j;
734 if (nrules != 0) {
735 (void) qsort(rules, nrules, sizeof *rules, rcomp);
736 for (i = 0; i < nrules - 1; ++i) {
737 if (strcmp(rules[i].r_name,
738 rules[i + 1].r_name) != 0)
739 continue;
740 if (strcmp(rules[i].r_filename,
741 rules[i + 1].r_filename) == 0)
742 continue;
743 eat(rules[i].r_filename, rules[i].r_linenum);
744 warning(_("same rule name in multiple files"));
745 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
746 warning(_("same rule name in multiple files"));
747 for (j = i + 2; j < nrules; ++j) {
748 if (strcmp(rules[i].r_name,
749 rules[j].r_name) != 0)
750 break;
751 if (strcmp(rules[i].r_filename,
752 rules[j].r_filename) == 0)
753 continue;
754 if (strcmp(rules[i + 1].r_filename,
755 rules[j].r_filename) == 0)
756 continue;
757 break;
759 i = j - 1;
762 for (i = 0; i < nzones; ++i) {
763 zp = &zones[i];
764 zp->z_rules = NULL;
765 zp->z_nrules = 0;
767 for (base = 0; base < nrules; base = out) {
768 rp = &rules[base];
769 for (out = base + 1; out < nrules; ++out)
770 if (strcmp(rp->r_name, rules[out].r_name) != 0)
771 break;
772 for (i = 0; i < nzones; ++i) {
773 zp = &zones[i];
774 if (strcmp(zp->z_rule, rp->r_name) != 0)
775 continue;
776 zp->z_rules = rp;
777 zp->z_nrules = out - base;
780 for (i = 0; i < nzones; ++i) {
781 zp = &zones[i];
782 if (zp->z_nrules == 0) {
784 ** Maybe we have a local standard time offset.
786 eat(zp->z_filename, zp->z_linenum);
787 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
788 TRUE);
790 ** Note, though, that if there's no rule,
791 ** a '%s' in the format is a bad thing.
793 if (strchr(zp->z_format, '%') != 0)
794 error("%s", _("%s in ruleless zone"));
797 if (errors)
798 exit(EXIT_FAILURE);
801 static void
802 infile(const char *name)
804 register FILE * fp;
805 register char ** fields;
806 register char * cp;
807 register const struct lookup * lp;
808 register int nfields;
809 register int wantcont;
810 register int num;
811 char buf[BUFSIZ];
813 if (strcmp(name, "-") == 0) {
814 name = _("standard input");
815 fp = stdin;
816 } else if ((fp = fopen(name, "r")) == NULL) {
817 const char *e = strerror(errno);
819 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
820 progname, name, e);
821 exit(EXIT_FAILURE);
823 wantcont = FALSE;
824 for (num = 1; ; ++num) {
825 eat(name, num);
826 if (fgets(buf, sizeof buf, fp) != buf)
827 break;
828 cp = strchr(buf, '\n');
829 if (cp == NULL) {
830 error(_("line too long"));
831 exit(EXIT_FAILURE);
833 *cp = '\0';
834 fields = getfields(buf);
835 nfields = 0;
836 while (fields[nfields] != NULL) {
837 static char nada;
839 if (strcmp(fields[nfields], "-") == 0)
840 fields[nfields] = &nada;
841 ++nfields;
843 if (nfields == 0) {
844 /* nothing to do */
845 } else if (wantcont) {
846 wantcont = inzcont(fields, nfields);
847 } else {
848 lp = byword(fields[0], line_codes);
849 if (lp == NULL)
850 error(_("input line of unknown type"));
851 else switch ((int) (lp->l_value)) {
852 case LC_RULE:
853 inrule(fields, nfields);
854 wantcont = FALSE;
855 break;
856 case LC_ZONE:
857 wantcont = inzone(fields, nfields);
858 break;
859 case LC_LINK:
860 inlink(fields, nfields);
861 wantcont = FALSE;
862 break;
863 case LC_LEAP:
864 if (name != leapsec)
865 (void) fprintf(stderr,
866 _("%s: Leap line in non leap seconds file %s\n"),
867 progname, name);
868 else inleap(fields, nfields);
869 wantcont = FALSE;
870 break;
871 default: /* "cannot happen" */
872 (void) fprintf(stderr,
873 _("%s: panic: Invalid l_value %d\n"),
874 progname, lp->l_value);
875 exit(EXIT_FAILURE);
878 free(fields);
880 if (ferror(fp)) {
881 (void) fprintf(stderr, _("%s: Error reading %s\n"),
882 progname, filename);
883 exit(EXIT_FAILURE);
885 if (fp != stdin && fclose(fp)) {
886 const char *e = strerror(errno);
888 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
889 progname, filename, e);
890 exit(EXIT_FAILURE);
892 if (wantcont)
893 error(_("expected continuation line not found"));
897 ** Convert a string of one of the forms
898 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
899 ** into a number of seconds.
900 ** A null string maps to zero.
901 ** Call error with errstring and return zero on errors.
904 static zic_t
905 gethms(const char *string, const char *const errstring, const int signable)
907 zic_t hh;
908 int mm, ss, sign;
910 if (string == NULL || *string == '\0')
911 return 0;
912 if (!signable)
913 sign = 1;
914 else if (*string == '-') {
915 sign = -1;
916 ++string;
917 } else sign = 1;
918 if (sscanf(string, scheck(string, "%"SCNdZIC), &hh) == 1)
919 mm = ss = 0;
920 else if (sscanf(string, scheck(string, "%"SCNdZIC":%d"), &hh, &mm) == 2)
921 ss = 0;
922 else if (sscanf(string, scheck(string, "%"SCNdZIC":%d:%d"),
923 &hh, &mm, &ss) != 3) {
924 error("%s", errstring);
925 return 0;
927 if (hh < 0 ||
928 mm < 0 || mm >= MINSPERHOUR ||
929 ss < 0 || ss > SECSPERMIN) {
930 error("%s", errstring);
931 return 0;
933 if (ZIC_MAX / SECSPERHOUR < hh) {
934 error(_("time overflow"));
935 return 0;
937 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
938 warning(_("24:00 not handled by pre-1998 versions of zic"));
939 if (noise && (hh > HOURSPERDAY ||
940 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
941 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
942 return oadd(sign * hh * SECSPERHOUR,
943 sign * (mm * SECSPERMIN + ss));
946 static void
947 inrule(register char **const fields, const int nfields)
949 static struct rule r;
951 if (nfields != RULE_FIELDS) {
952 error(_("wrong number of fields on Rule line"));
953 return;
955 if (*fields[RF_NAME] == '\0') {
956 error(_("nameless rule"));
957 return;
959 r.r_filename = filename;
960 r.r_linenum = linenum;
961 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
962 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
963 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
964 r.r_name = ecpyalloc(fields[RF_NAME]);
965 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
966 if (max_abbrvar_len < strlen(r.r_abbrvar))
967 max_abbrvar_len = strlen(r.r_abbrvar);
968 rules = erealloc(rules, (nrules + 1) * sizeof *rules);
969 rules[nrules++] = r;
972 static int
973 inzone(register char **const fields, const int nfields)
975 register int i;
977 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
978 error(_("wrong number of fields on Zone line"));
979 return FALSE;
981 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
982 error(
983 _("\"Zone %s\" line and -l option are mutually exclusive"),
984 TZDEFAULT);
985 return FALSE;
987 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
988 error(
989 _("\"Zone %s\" line and -p option are mutually exclusive"),
990 TZDEFRULES);
991 return FALSE;
993 for (i = 0; i < nzones; ++i)
994 if (zones[i].z_name != NULL &&
995 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
996 error(
997 _("duplicate zone name %s (file \"%s\", line %d)"),
998 fields[ZF_NAME],
999 zones[i].z_filename,
1000 zones[i].z_linenum);
1001 return FALSE;
1003 return inzsub(fields, nfields, FALSE);
1006 static int
1007 inzcont(register char **const fields, const int nfields)
1009 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1010 error(_("wrong number of fields on Zone continuation line"));
1011 return FALSE;
1013 return inzsub(fields, nfields, TRUE);
1016 static int
1017 inzsub(register char **const fields, const int nfields, const int iscont)
1019 register char * cp;
1020 static struct zone z;
1021 register int i_gmtoff, i_rule, i_format;
1022 register int i_untilyear, i_untilmonth;
1023 register int i_untilday, i_untiltime;
1024 register int hasuntil;
1026 if (iscont) {
1027 i_gmtoff = ZFC_GMTOFF;
1028 i_rule = ZFC_RULE;
1029 i_format = ZFC_FORMAT;
1030 i_untilyear = ZFC_TILYEAR;
1031 i_untilmonth = ZFC_TILMONTH;
1032 i_untilday = ZFC_TILDAY;
1033 i_untiltime = ZFC_TILTIME;
1034 z.z_name = NULL;
1035 } else {
1036 i_gmtoff = ZF_GMTOFF;
1037 i_rule = ZF_RULE;
1038 i_format = ZF_FORMAT;
1039 i_untilyear = ZF_TILYEAR;
1040 i_untilmonth = ZF_TILMONTH;
1041 i_untilday = ZF_TILDAY;
1042 i_untiltime = ZF_TILTIME;
1043 z.z_name = ecpyalloc(fields[ZF_NAME]);
1045 z.z_filename = filename;
1046 z.z_linenum = linenum;
1047 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"), TRUE);
1048 if ((cp = strchr(fields[i_format], '%')) != 0) {
1049 if (*++cp != 's' || strchr(cp, '%') != 0) {
1050 error(_("invalid abbreviation format"));
1051 return FALSE;
1054 z.z_rule = ecpyalloc(fields[i_rule]);
1055 z.z_format = ecpyalloc(fields[i_format]);
1056 if (max_format_len < strlen(z.z_format))
1057 max_format_len = strlen(z.z_format);
1058 hasuntil = nfields > i_untilyear;
1059 if (hasuntil) {
1060 z.z_untilrule.r_filename = filename;
1061 z.z_untilrule.r_linenum = linenum;
1062 rulesub(&z.z_untilrule,
1063 fields[i_untilyear],
1064 "only",
1066 (nfields > i_untilmonth) ?
1067 fields[i_untilmonth] : "Jan",
1068 (nfields > i_untilday) ? fields[i_untilday] : "1",
1069 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1070 z.z_untiltime = rpytime(&z.z_untilrule,
1071 z.z_untilrule.r_loyear);
1072 if (iscont && nzones > 0 &&
1073 z.z_untiltime > min_time &&
1074 z.z_untiltime < max_time &&
1075 zones[nzones - 1].z_untiltime > min_time &&
1076 zones[nzones - 1].z_untiltime < max_time &&
1077 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1078 error(_(
1079 "Zone continuation line end time is not after end time of previous line"
1081 return FALSE;
1084 zones = erealloc(zones, (nzones + 1) * sizeof *zones);
1085 zones[nzones++] = z;
1087 ** If there was an UNTIL field on this line,
1088 ** there's more information about the zone on the next line.
1090 return hasuntil;
1093 static void
1094 inleap(register char ** const fields, const int nfields)
1096 register const char * cp;
1097 register const struct lookup * lp;
1098 register int i, j;
1099 zic_t year;
1100 int month, day;
1101 zic_t dayoff, tod;
1102 zic_t t;
1104 if (nfields != LEAP_FIELDS) {
1105 error(_("wrong number of fields on Leap line"));
1106 return;
1108 dayoff = 0;
1109 cp = fields[LP_YEAR];
1110 if (sscanf(cp, scheck(cp, "%"SCNdZIC), &year) != 1) {
1112 ** Leapin' Lizards!
1114 error(_("invalid leaping year"));
1115 return;
1117 if (!leapseen || leapmaxyear < year)
1118 leapmaxyear = year;
1119 if (!leapseen || leapminyear > year)
1120 leapminyear = year;
1121 leapseen = TRUE;
1122 j = EPOCH_YEAR;
1123 while (j != year) {
1124 if (year > j) {
1125 i = len_years[isleap(j)];
1126 ++j;
1127 } else {
1128 --j;
1129 i = -len_years[isleap(j)];
1131 dayoff = oadd(dayoff, i);
1133 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1134 error(_("invalid month name"));
1135 return;
1137 month = lp->l_value;
1138 j = TM_JANUARY;
1139 while (j != month) {
1140 i = len_months[isleap(year)][j];
1141 dayoff = oadd(dayoff, i);
1142 ++j;
1144 cp = fields[LP_DAY];
1145 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1146 day <= 0 || day > len_months[isleap(year)][month]) {
1147 error(_("invalid day of month"));
1148 return;
1150 dayoff = oadd(dayoff, day - 1);
1151 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1152 error(_("time before zero"));
1153 return;
1155 if (dayoff < min_time / SECSPERDAY) {
1156 error(_("time too small"));
1157 return;
1159 if (dayoff > max_time / SECSPERDAY) {
1160 error(_("time too large"));
1161 return;
1163 t = (zic_t) dayoff * SECSPERDAY;
1164 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1165 cp = fields[LP_CORR];
1167 register int positive;
1168 int count;
1170 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1171 positive = FALSE;
1172 count = 1;
1173 } else if (strcmp(cp, "--") == 0) {
1174 positive = FALSE;
1175 count = 2;
1176 } else if (strcmp(cp, "+") == 0) {
1177 positive = TRUE;
1178 count = 1;
1179 } else if (strcmp(cp, "++") == 0) {
1180 positive = TRUE;
1181 count = 2;
1182 } else {
1183 error(_("illegal CORRECTION field on Leap line"));
1184 return;
1186 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1187 error(_(
1188 "illegal Rolling/Stationary field on Leap line"
1190 return;
1192 leapadd(tadd(t, tod), positive, lp->l_value, count);
1196 static void
1197 inlink(register char **const fields, const int nfields)
1199 struct link l;
1201 if (nfields != LINK_FIELDS) {
1202 error(_("wrong number of fields on Link line"));
1203 return;
1205 if (*fields[LF_FROM] == '\0') {
1206 error(_("blank FROM field on Link line"));
1207 return;
1209 if (*fields[LF_TO] == '\0') {
1210 error(_("blank TO field on Link line"));
1211 return;
1213 l.l_filename = filename;
1214 l.l_linenum = linenum;
1215 l.l_from = ecpyalloc(fields[LF_FROM]);
1216 l.l_to = ecpyalloc(fields[LF_TO]);
1217 links = erealloc(links, (nlinks + 1) * sizeof *links);
1218 links[nlinks++] = l;
1221 static void
1222 rulesub(register struct rule *const rp,
1223 const char *const loyearp,
1224 const char *const hiyearp,
1225 const char *const typep,
1226 const char *const monthp,
1227 const char *const dayp,
1228 const char *const timep)
1230 register const struct lookup * lp;
1231 register const char * cp;
1232 register char * dp;
1233 register char * ep;
1235 if ((lp = byword(monthp, mon_names)) == NULL) {
1236 error(_("invalid month name"));
1237 return;
1239 rp->r_month = lp->l_value;
1240 rp->r_todisstd = FALSE;
1241 rp->r_todisgmt = FALSE;
1242 dp = ecpyalloc(timep);
1243 if (*dp != '\0') {
1244 ep = dp + strlen(dp) - 1;
1245 switch (lowerit(*ep)) {
1246 case 's': /* Standard */
1247 rp->r_todisstd = TRUE;
1248 rp->r_todisgmt = FALSE;
1249 *ep = '\0';
1250 break;
1251 case 'w': /* Wall */
1252 rp->r_todisstd = FALSE;
1253 rp->r_todisgmt = FALSE;
1254 *ep = '\0';
1255 break;
1256 case 'g': /* Greenwich */
1257 case 'u': /* Universal */
1258 case 'z': /* Zulu */
1259 rp->r_todisstd = TRUE;
1260 rp->r_todisgmt = TRUE;
1261 *ep = '\0';
1262 break;
1265 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1266 free(dp);
1268 ** Year work.
1270 cp = loyearp;
1271 lp = byword(cp, begin_years);
1272 rp->r_lowasnum = lp == NULL;
1273 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1274 case YR_MINIMUM:
1275 rp->r_loyear = ZIC_MIN;
1276 break;
1277 case YR_MAXIMUM:
1278 rp->r_loyear = ZIC_MAX;
1279 break;
1280 default: /* "cannot happen" */
1281 (void) fprintf(stderr,
1282 _("%s: panic: Invalid l_value %d\n"),
1283 progname, lp->l_value);
1284 exit(EXIT_FAILURE);
1285 } else if (sscanf(cp, scheck(cp, "%"SCNdZIC), &rp->r_loyear) != 1) {
1286 error(_("invalid starting year"));
1287 return;
1289 cp = hiyearp;
1290 lp = byword(cp, end_years);
1291 rp->r_hiwasnum = lp == NULL;
1292 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1293 case YR_MINIMUM:
1294 rp->r_hiyear = ZIC_MIN;
1295 break;
1296 case YR_MAXIMUM:
1297 rp->r_hiyear = ZIC_MAX;
1298 break;
1299 case YR_ONLY:
1300 rp->r_hiyear = rp->r_loyear;
1301 break;
1302 default: /* "cannot happen" */
1303 (void) fprintf(stderr,
1304 _("%s: panic: Invalid l_value %d\n"),
1305 progname, lp->l_value);
1306 exit(EXIT_FAILURE);
1307 } else if (sscanf(cp, scheck(cp, "%"SCNdZIC), &rp->r_hiyear) != 1) {
1308 error(_("invalid ending year"));
1309 return;
1311 if (rp->r_loyear > rp->r_hiyear) {
1312 error(_("starting year greater than ending year"));
1313 return;
1315 if (*typep == '\0')
1316 rp->r_yrtype = NULL;
1317 else {
1318 if (rp->r_loyear == rp->r_hiyear) {
1319 error(_("typed single year"));
1320 return;
1322 rp->r_yrtype = ecpyalloc(typep);
1325 ** Day work.
1326 ** Accept things such as:
1327 ** 1
1328 ** last-Sunday
1329 ** Sun<=20
1330 ** Sun>=7
1332 dp = ecpyalloc(dayp);
1333 if ((lp = byword(dp, lasts)) != NULL) {
1334 rp->r_dycode = DC_DOWLEQ;
1335 rp->r_wday = lp->l_value;
1336 rp->r_dayofmonth = len_months[1][rp->r_month];
1337 } else {
1338 if ((ep = strchr(dp, '<')) != 0)
1339 rp->r_dycode = DC_DOWLEQ;
1340 else if ((ep = strchr(dp, '>')) != 0)
1341 rp->r_dycode = DC_DOWGEQ;
1342 else {
1343 ep = dp;
1344 rp->r_dycode = DC_DOM;
1346 if (rp->r_dycode != DC_DOM) {
1347 *ep++ = 0;
1348 if (*ep++ != '=') {
1349 error(_("invalid day of month"));
1350 free(dp);
1351 return;
1353 if ((lp = byword(dp, wday_names)) == NULL) {
1354 error(_("invalid weekday name"));
1355 free(dp);
1356 return;
1358 rp->r_wday = lp->l_value;
1360 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1361 rp->r_dayofmonth <= 0 ||
1362 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1363 error(_("invalid day of month"));
1364 free(dp);
1365 return;
1368 free(dp);
1371 static void
1372 convert(const int_fast32_t val, char *const buf)
1374 register int i;
1375 register int shift;
1376 unsigned char *const b = (unsigned char *) buf;
1378 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1379 b[i] = val >> shift;
1382 static void
1383 convert64(const zic_t val, char *const buf)
1385 register int i;
1386 register int shift;
1387 unsigned char *const b = (unsigned char *) buf;
1389 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1390 b[i] = val >> shift;
1393 static void
1394 puttzcode(const int_fast32_t val, FILE *const fp)
1396 char buf[4];
1398 convert(val, buf);
1399 (void) fwrite(buf, sizeof buf, 1, fp);
1402 static void
1403 puttzcode64(const zic_t val, FILE *const fp)
1405 char buf[8];
1407 convert64(val, buf);
1408 (void) fwrite(buf, sizeof buf, 1, fp);
1411 static int
1412 atcomp(const void *avp, const void *bvp)
1414 const zic_t a = ((const struct attype *) avp)->at;
1415 const zic_t b = ((const struct attype *) bvp)->at;
1417 return (a < b) ? -1 : (a > b);
1420 static int
1421 is32(const zic_t x)
1423 return INT32_MIN <= x && x <= INT32_MAX;
1426 static void
1427 writezone(const char *const name, const char *const string, char version)
1429 register FILE * fp;
1430 register int i, j;
1431 register int leapcnt32, leapi32;
1432 register int timecnt32, timei32;
1433 register int pass;
1434 static char * fullname;
1435 static const struct tzhead tzh0;
1436 static struct tzhead tzh;
1437 zic_t ats[TZ_MAX_TIMES];
1438 unsigned char types[TZ_MAX_TIMES];
1441 ** Sort.
1443 if (timecnt > 1)
1444 (void) qsort(attypes, timecnt, sizeof *attypes, atcomp);
1446 ** Optimize.
1449 int fromi;
1450 int toi;
1452 toi = 0;
1453 fromi = 0;
1454 while (fromi < timecnt && attypes[fromi].at < min_time)
1455 ++fromi;
1457 ** Remember that type 0 is reserved.
1459 if (isdsts[1] == 0)
1460 while (fromi < timecnt && attypes[fromi].type == 1)
1461 ++fromi; /* handled by default rule */
1462 for ( ; fromi < timecnt; ++fromi) {
1463 if (toi != 0 && ((attypes[fromi].at +
1464 gmtoffs[attypes[toi - 1].type]) <=
1465 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1466 : attypes[toi - 2].type]))) {
1467 attypes[toi - 1].type =
1468 attypes[fromi].type;
1469 continue;
1471 if (toi == 0 ||
1472 attypes[toi - 1].type != attypes[fromi].type)
1473 attypes[toi++] = attypes[fromi];
1475 timecnt = toi;
1478 ** Transfer.
1480 for (i = 0; i < timecnt; ++i) {
1481 ats[i] = attypes[i].at;
1482 types[i] = attypes[i].type;
1485 ** Correct for leap seconds.
1487 for (i = 0; i < timecnt; ++i) {
1488 j = leapcnt;
1489 while (--j >= 0)
1490 if (ats[i] > trans[j] - corr[j]) {
1491 ats[i] = tadd(ats[i], corr[j]);
1492 break;
1496 ** Figure out 32-bit-limited starts and counts.
1498 timecnt32 = timecnt;
1499 timei32 = 0;
1500 leapcnt32 = leapcnt;
1501 leapi32 = 0;
1502 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1503 --timecnt32;
1504 while (timecnt32 > 0 && !is32(ats[timei32])) {
1505 --timecnt32;
1506 ++timei32;
1508 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1509 --leapcnt32;
1510 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1511 --leapcnt32;
1512 ++leapi32;
1514 fullname = erealloc(fullname,
1515 strlen(directory) + 1 + strlen(name) + 1);
1516 (void) sprintf(fullname, "%s/%s", directory, name);
1518 ** Remove old file, if any, to snap links.
1520 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1521 const char *e = strerror(errno);
1523 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1524 progname, fullname, e);
1525 exit(EXIT_FAILURE);
1527 if ((fp = fopen(fullname, "wb")) == NULL) {
1528 if (mkdirs(fullname) != 0)
1529 exit(EXIT_FAILURE);
1530 if ((fp = fopen(fullname, "wb")) == NULL) {
1531 const char *e = strerror(errno);
1533 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1534 progname, fullname, e);
1535 exit(EXIT_FAILURE);
1538 for (pass = 1; pass <= 2; ++pass) {
1539 register int thistimei, thistimecnt;
1540 register int thisleapi, thisleapcnt;
1541 register int thistimelim, thisleaplim;
1542 int writetype[TZ_MAX_TIMES];
1543 int typemap[TZ_MAX_TYPES];
1544 register int thistypecnt;
1545 char thischars[TZ_MAX_CHARS];
1546 char thischarcnt;
1547 int indmap[TZ_MAX_CHARS];
1549 if (pass == 1) {
1550 thistimei = timei32;
1551 thistimecnt = timecnt32;
1552 thisleapi = leapi32;
1553 thisleapcnt = leapcnt32;
1554 } else {
1555 thistimei = 0;
1556 thistimecnt = timecnt;
1557 thisleapi = 0;
1558 thisleapcnt = leapcnt;
1560 thistimelim = thistimei + thistimecnt;
1561 thisleaplim = thisleapi + thisleapcnt;
1563 ** Remember that type 0 is reserved.
1565 writetype[0] = FALSE;
1566 for (i = 1; i < typecnt; ++i)
1567 writetype[i] = thistimecnt == timecnt;
1568 if (thistimecnt == 0) {
1570 ** No transition times fall in the current
1571 ** (32- or 64-bit) window.
1573 if (typecnt != 0)
1574 writetype[typecnt - 1] = TRUE;
1575 } else {
1576 for (i = thistimei - 1; i < thistimelim; ++i)
1577 if (i >= 0)
1578 writetype[types[i]] = TRUE;
1580 ** For America/Godthab and Antarctica/Palmer
1583 ** Remember that type 0 is reserved.
1585 if (thistimei == 0)
1586 writetype[1] = TRUE;
1588 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1590 ** For some pre-2011 systems: if the last-to-be-written
1591 ** standard (or daylight) type has an offset different from the
1592 ** most recently used offset,
1593 ** append an (unused) copy of the most recently used type
1594 ** (to help get global "altzone" and "timezone" variables
1595 ** set correctly).
1598 register int mrudst, mrustd, hidst, histd, type;
1600 hidst = histd = mrudst = mrustd = -1;
1601 for (i = thistimei; i < thistimelim; ++i)
1602 if (isdsts[types[i]])
1603 mrudst = types[i];
1604 else mrustd = types[i];
1605 for (i = 0; i < typecnt; ++i)
1606 if (writetype[i]) {
1607 if (isdsts[i])
1608 hidst = i;
1609 else histd = i;
1611 if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1612 gmtoffs[hidst] != gmtoffs[mrudst]) {
1613 isdsts[mrudst] = -1;
1614 type = addtype(gmtoffs[mrudst],
1615 &chars[abbrinds[mrudst]],
1616 TRUE,
1617 ttisstds[mrudst],
1618 ttisgmts[mrudst]);
1619 isdsts[mrudst] = TRUE;
1620 writetype[type] = TRUE;
1622 if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1623 gmtoffs[histd] != gmtoffs[mrustd]) {
1624 isdsts[mrustd] = -1;
1625 type = addtype(gmtoffs[mrustd],
1626 &chars[abbrinds[mrustd]],
1627 FALSE,
1628 ttisstds[mrustd],
1629 ttisgmts[mrustd]);
1630 isdsts[mrustd] = FALSE;
1631 writetype[type] = TRUE;
1634 #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1635 thistypecnt = 0;
1637 ** Potentially, set type 0 to that of lowest-valued time.
1639 if (thistimei > 0) {
1640 for (i = 1; i < typecnt; ++i)
1641 if (writetype[i] && !isdsts[i])
1642 break;
1643 if (i != types[thistimei - 1]) {
1644 i = types[thistimei - 1];
1645 gmtoffs[0] = gmtoffs[i];
1646 isdsts[0] = isdsts[i];
1647 ttisstds[0] = ttisstds[i];
1648 ttisgmts[0] = ttisgmts[i];
1649 abbrinds[0] = abbrinds[i];
1650 writetype[0] = TRUE;
1651 writetype[i] = FALSE;
1654 for (i = 0; i < typecnt; ++i)
1655 typemap[i] = writetype[i] ? thistypecnt++ : 0;
1656 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1657 indmap[i] = -1;
1658 thischarcnt = 0;
1659 for (i = 0; i < typecnt; ++i) {
1660 register char * thisabbr;
1662 if (!writetype[i])
1663 continue;
1664 if (indmap[abbrinds[i]] >= 0)
1665 continue;
1666 thisabbr = &chars[abbrinds[i]];
1667 for (j = 0; j < thischarcnt; ++j)
1668 if (strcmp(&thischars[j], thisabbr) == 0)
1669 break;
1670 if (j == thischarcnt) {
1671 (void) strcpy(&thischars[(int) thischarcnt],
1672 thisabbr);
1673 thischarcnt += strlen(thisabbr) + 1;
1675 indmap[abbrinds[i]] = j;
1677 #define DO(field) ((void) fwrite(tzh.field, sizeof tzh.field, 1, fp))
1678 tzh = tzh0;
1679 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1680 tzh.tzh_version[0] = version;
1681 convert(thistypecnt, tzh.tzh_ttisgmtcnt);
1682 convert(thistypecnt, tzh.tzh_ttisstdcnt);
1683 convert(thisleapcnt, tzh.tzh_leapcnt);
1684 convert(thistimecnt, tzh.tzh_timecnt);
1685 convert(thistypecnt, tzh.tzh_typecnt);
1686 convert(thischarcnt, tzh.tzh_charcnt);
1687 DO(tzh_magic);
1688 DO(tzh_version);
1689 DO(tzh_reserved);
1690 DO(tzh_ttisgmtcnt);
1691 DO(tzh_ttisstdcnt);
1692 DO(tzh_leapcnt);
1693 DO(tzh_timecnt);
1694 DO(tzh_typecnt);
1695 DO(tzh_charcnt);
1696 #undef DO
1697 for (i = thistimei; i < thistimelim; ++i)
1698 if (pass == 1)
1699 puttzcode(ats[i], fp);
1700 else puttzcode64(ats[i], fp);
1701 for (i = thistimei; i < thistimelim; ++i) {
1702 unsigned char uc;
1704 uc = typemap[types[i]];
1705 (void) fwrite(&uc, sizeof uc, 1, fp);
1707 for (i = 0; i < typecnt; ++i)
1708 if (writetype[i]) {
1709 puttzcode(gmtoffs[i], fp);
1710 (void) putc(isdsts[i], fp);
1711 (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1713 if (thischarcnt != 0)
1714 (void) fwrite(thischars, sizeof thischars[0],
1715 thischarcnt, fp);
1716 for (i = thisleapi; i < thisleaplim; ++i) {
1717 register zic_t todo;
1719 if (roll[i]) {
1720 if (timecnt == 0 || trans[i] < ats[0]) {
1721 j = 0;
1722 while (isdsts[j])
1723 if (++j >= typecnt) {
1724 j = 0;
1725 break;
1727 } else {
1728 j = 1;
1729 while (j < timecnt &&
1730 trans[i] >= ats[j])
1731 ++j;
1732 j = types[j - 1];
1734 todo = tadd(trans[i], -gmtoffs[j]);
1735 } else todo = trans[i];
1736 if (pass == 1)
1737 puttzcode(todo, fp);
1738 else puttzcode64(todo, fp);
1739 puttzcode(corr[i], fp);
1741 for (i = 0; i < typecnt; ++i)
1742 if (writetype[i])
1743 (void) putc(ttisstds[i], fp);
1744 for (i = 0; i < typecnt; ++i)
1745 if (writetype[i])
1746 (void) putc(ttisgmts[i], fp);
1748 (void) fprintf(fp, "\n%s\n", string);
1749 if (ferror(fp) || fclose(fp)) {
1750 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1751 progname, fullname);
1752 exit(EXIT_FAILURE);
1756 static void
1757 doabbr(char *const abbr, const char *const format, const char *const letters,
1758 const int isdst, const int doquotes)
1760 register char * cp;
1761 register char * slashp;
1762 register int len;
1764 slashp = strchr(format, '/');
1765 if (slashp == NULL) {
1766 if (letters == NULL)
1767 (void) strcpy(abbr, format);
1768 else (void) sprintf(abbr, format, letters);
1769 } else if (isdst) {
1770 (void) strcpy(abbr, slashp + 1);
1771 } else {
1772 if (slashp > format)
1773 (void) strncpy(abbr, format, slashp - format);
1774 abbr[slashp - format] = '\0';
1776 if (!doquotes)
1777 return;
1778 for (cp = abbr; *cp != '\0'; ++cp)
1779 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1780 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1781 break;
1782 len = strlen(abbr);
1783 if (len > 0 && *cp == '\0')
1784 return;
1785 abbr[len + 2] = '\0';
1786 abbr[len + 1] = '>';
1787 for ( ; len > 0; --len)
1788 abbr[len] = abbr[len - 1];
1789 abbr[0] = '<';
1792 static void
1793 updateminmax(const zic_t x)
1795 if (min_year > x)
1796 min_year = x;
1797 if (max_year < x)
1798 max_year = x;
1801 static int
1802 stringoffset(char *result, zic_t offset)
1804 register int hours;
1805 register int minutes;
1806 register int seconds;
1808 result[0] = '\0';
1809 if (offset < 0) {
1810 (void) strcpy(result, "-");
1811 offset = -offset;
1813 seconds = offset % SECSPERMIN;
1814 offset /= SECSPERMIN;
1815 minutes = offset % MINSPERHOUR;
1816 offset /= MINSPERHOUR;
1817 hours = offset;
1818 if (hours >= HOURSPERDAY * DAYSPERWEEK) {
1819 result[0] = '\0';
1820 return -1;
1822 (void) sprintf(end(result), "%d", hours);
1823 if (minutes != 0 || seconds != 0) {
1824 (void) sprintf(end(result), ":%02d", minutes);
1825 if (seconds != 0)
1826 (void) sprintf(end(result), ":%02d", seconds);
1828 return 0;
1831 static int
1832 stringrule(char *result, const struct rule *const rp, const zic_t dstoff,
1833 const zic_t gmtoff)
1835 register zic_t tod = rp->r_tod;
1836 register int compat = 0;
1838 result = end(result);
1839 if (rp->r_dycode == DC_DOM) {
1840 register int month, total;
1842 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1843 return -1;
1844 total = 0;
1845 for (month = 0; month < rp->r_month; ++month)
1846 total += len_months[0][month];
1847 /* Omit the "J" in Jan and Feb, as that's shorter. */
1848 if (rp->r_month <= 1)
1849 (void) sprintf(result, "%d", total + rp->r_dayofmonth - 1);
1850 else
1851 (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1852 } else {
1853 register int week;
1854 register int wday = rp->r_wday;
1855 register int wdayoff;
1857 if (rp->r_dycode == DC_DOWGEQ) {
1858 wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
1859 if (wdayoff)
1860 compat = 2013;
1861 wday -= wdayoff;
1862 tod += wdayoff * SECSPERDAY;
1863 week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
1864 } else if (rp->r_dycode == DC_DOWLEQ) {
1865 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1866 week = 5;
1867 else {
1868 wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
1869 if (wdayoff)
1870 compat = 2013;
1871 wday -= wdayoff;
1872 tod += wdayoff * SECSPERDAY;
1873 week = rp->r_dayofmonth / DAYSPERWEEK;
1875 } else return -1; /* "cannot happen" */
1876 if (wday < 0)
1877 wday += DAYSPERWEEK;
1878 (void) sprintf(result, "M%d.%d.%d",
1879 rp->r_month + 1, week, wday);
1881 if (rp->r_todisgmt)
1882 tod += gmtoff;
1883 if (rp->r_todisstd && rp->r_stdoff == 0)
1884 tod += dstoff;
1885 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1886 (void) strcat(result, "/");
1887 if (stringoffset(end(result), tod) != 0)
1888 return -1;
1889 if (tod < 0) {
1890 if (compat < 2013)
1891 compat = 2013;
1892 } else if (SECSPERDAY <= tod) {
1893 if (compat < 1994)
1894 compat = 1994;
1897 return compat;
1900 static int
1901 rule_cmp(struct rule const *a, struct rule const *b)
1903 if (!a)
1904 return -!!b;
1905 if (!b)
1906 return 1;
1907 if (a->r_hiyear != b->r_hiyear)
1908 return a->r_hiyear < b->r_hiyear ? -1 : 1;
1909 if (a->r_month - b->r_month != 0)
1910 return a->r_month - b->r_month;
1911 return a->r_dayofmonth - b->r_dayofmonth;
1914 enum { YEAR_BY_YEAR_ZONE = 1 };
1916 static int
1917 stringzone(char *result, const struct zone *const zpfirst, const int zonecount)
1919 register const struct zone * zp;
1920 register struct rule * rp;
1921 register struct rule * stdrp;
1922 register struct rule * dstrp;
1923 register int i;
1924 register const char * abbrvar;
1925 register int compat = 0;
1926 register int c;
1927 struct rule stdr, dstr;
1929 result[0] = '\0';
1930 zp = zpfirst + zonecount - 1;
1931 stdrp = dstrp = NULL;
1932 for (i = 0; i < zp->z_nrules; ++i) {
1933 rp = &zp->z_rules[i];
1934 if (rp->r_hiwasnum || rp->r_hiyear != ZIC_MAX)
1935 continue;
1936 if (rp->r_yrtype != NULL)
1937 continue;
1938 if (rp->r_stdoff == 0) {
1939 if (stdrp == NULL)
1940 stdrp = rp;
1941 else return -1;
1942 } else {
1943 if (dstrp == NULL)
1944 dstrp = rp;
1945 else return -1;
1948 if (stdrp == NULL && dstrp == NULL) {
1950 ** There are no rules running through "max".
1951 ** Find the latest std rule in stdabbrrp
1952 ** and latest rule of any type in stdrp.
1954 register struct rule *stdabbrrp = NULL;
1955 for (i = 0; i < zp->z_nrules; ++i) {
1956 rp = &zp->z_rules[i];
1957 if (rp->r_stdoff == 0 && rule_cmp(stdabbrrp, rp) < 0)
1958 stdabbrrp = rp;
1959 if (rule_cmp(stdrp, rp) < 0)
1960 stdrp = rp;
1963 ** Horrid special case: if year is 2037,
1964 ** presume this is a zone handled on a year-by-year basis;
1965 ** do not try to apply a rule to the zone.
1967 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1968 return YEAR_BY_YEAR_ZONE;
1970 if (stdrp != NULL && stdrp->r_stdoff != 0) {
1971 /* Perpetual DST. */
1972 dstr.r_month = TM_JANUARY;
1973 dstr.r_dycode = DC_DOM;
1974 dstr.r_dayofmonth = 1;
1975 dstr.r_tod = 0;
1976 dstr.r_todisstd = dstr.r_todisgmt = FALSE;
1977 dstr.r_stdoff = stdrp->r_stdoff;
1978 dstr.r_abbrvar = stdrp->r_abbrvar;
1979 stdr.r_month = TM_DECEMBER;
1980 stdr.r_dycode = DC_DOM;
1981 stdr.r_dayofmonth = 31;
1982 stdr.r_tod = SECSPERDAY + stdrp->r_stdoff;
1983 stdr.r_todisstd = stdr.r_todisgmt = FALSE;
1984 stdr.r_stdoff = 0;
1985 stdr.r_abbrvar
1986 = (stdabbrrp ? stdabbrrp->r_abbrvar : "");
1987 dstrp = &dstr;
1988 stdrp = &stdr;
1991 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1992 return -1;
1993 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1994 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1995 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1996 result[0] = '\0';
1997 return -1;
1999 if (dstrp == NULL)
2000 return compat;
2001 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
2002 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
2003 if (stringoffset(end(result),
2004 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
2005 result[0] = '\0';
2006 return -1;
2008 (void) strcat(result, ",");
2009 c = stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff);
2010 if (c < 0) {
2011 result[0] = '\0';
2012 return -1;
2014 if (compat < c)
2015 compat = c;
2016 (void) strcat(result, ",");
2017 c = stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff);
2018 if (c < 0) {
2019 result[0] = '\0';
2020 return -1;
2022 if (compat < c)
2023 compat = c;
2024 return compat;
2027 static void
2028 outzone(const struct zone * const zpfirst, const int zonecount)
2030 register const struct zone * zp;
2031 register struct rule * rp;
2032 register int i, j;
2033 register int usestart, useuntil;
2034 register zic_t starttime, untiltime;
2035 register zic_t gmtoff;
2036 register zic_t stdoff;
2037 register zic_t year;
2038 register zic_t startoff;
2039 register int startttisstd;
2040 register int startttisgmt;
2041 register int type;
2042 register char * startbuf;
2043 register char * ab;
2044 register char * envvar;
2045 register int max_abbr_len;
2046 register int max_envvar_len;
2047 register int prodstic; /* all rules are min to max */
2048 register int compat;
2049 register int do_extend;
2050 register char version;
2052 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2053 max_envvar_len = 2 * max_abbr_len + 5 * 9;
2054 startbuf = emalloc(max_abbr_len + 1);
2055 ab = emalloc(max_abbr_len + 1);
2056 envvar = emalloc(max_envvar_len + 1);
2057 INITIALIZE(untiltime);
2058 INITIALIZE(starttime);
2060 ** Now. . .finally. . .generate some useful data!
2062 timecnt = 0;
2063 typecnt = 0;
2064 charcnt = 0;
2065 prodstic = zonecount == 1;
2067 ** Thanks to Earl Chew
2068 ** for noting the need to unconditionally initialize startttisstd.
2070 startttisstd = FALSE;
2071 startttisgmt = FALSE;
2072 min_year = max_year = EPOCH_YEAR;
2073 if (leapseen) {
2074 updateminmax(leapminyear);
2075 updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
2078 ** Reserve type 0.
2080 gmtoffs[0] = isdsts[0] = ttisstds[0] = ttisgmts[0] = abbrinds[0] = -1;
2081 typecnt = 1;
2082 for (i = 0; i < zonecount; ++i) {
2083 zp = &zpfirst[i];
2084 if (i < zonecount - 1)
2085 updateminmax(zp->z_untilrule.r_loyear);
2086 for (j = 0; j < zp->z_nrules; ++j) {
2087 rp = &zp->z_rules[j];
2088 if (rp->r_lowasnum)
2089 updateminmax(rp->r_loyear);
2090 if (rp->r_hiwasnum)
2091 updateminmax(rp->r_hiyear);
2092 if (rp->r_lowasnum || rp->r_hiwasnum)
2093 prodstic = FALSE;
2097 ** Generate lots of data if a rule can't cover all future times.
2099 compat = stringzone(envvar, zpfirst, zonecount);
2100 version = compat < 2013 ? ZIC_VERSION_PRE_2013 : ZIC_VERSION;
2101 do_extend = compat < 0 || compat == YEAR_BY_YEAR_ZONE;
2102 if (noise) {
2103 if (!*envvar)
2104 warning("%s %s",
2105 _("no POSIX environment variable for zone"),
2106 zpfirst->z_name);
2107 else if (compat != 0 && compat != YEAR_BY_YEAR_ZONE) {
2108 /* Circa-COMPAT clients, and earlier clients, might
2109 not work for this zone when given dates before
2110 1970 or after 2038. */
2111 warning(_("%s: pre-%d clients may mishandle"
2112 " distant timestamps"),
2113 zpfirst->z_name, compat);
2116 if (do_extend) {
2118 ** Search through a couple of extra years past the obvious
2119 ** 400, to avoid edge cases. For example, suppose a non-POSIX
2120 ** rule applies from 2012 onwards and has transitions in March
2121 ** and September, plus some one-off transitions in November
2122 ** 2013. If zic looked only at the last 400 years, it would
2123 ** set max_year=2413, with the intent that the 400 years 2014
2124 ** through 2413 will be repeated. The last transition listed
2125 ** in the tzfile would be in 2413-09, less than 400 years
2126 ** after the last one-off transition in 2013-11. Two years
2127 ** might be overkill, but with the kind of edge cases
2128 ** available we're not sure that one year would suffice.
2130 enum { years_of_observations = YEARSPERREPEAT + 2 };
2132 if (min_year >= ZIC_MIN + years_of_observations)
2133 min_year -= years_of_observations;
2134 else min_year = ZIC_MIN;
2135 if (max_year <= ZIC_MAX - years_of_observations)
2136 max_year += years_of_observations;
2137 else max_year = ZIC_MAX;
2139 ** Regardless of any of the above,
2140 ** for a "proDSTic" zone which specifies that its rules
2141 ** always have and always will be in effect,
2142 ** we only need one cycle to define the zone.
2144 if (prodstic) {
2145 min_year = 1900;
2146 max_year = min_year + years_of_observations;
2150 ** For the benefit of older systems,
2151 ** generate data from 1900 through 2037.
2153 if (min_year > 1900)
2154 min_year = 1900;
2155 if (max_year < 2037)
2156 max_year = 2037;
2157 for (i = 0; i < zonecount; ++i) {
2159 ** A guess that may well be corrected later.
2161 stdoff = 0;
2162 zp = &zpfirst[i];
2163 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2164 useuntil = i < (zonecount - 1);
2165 if (useuntil && zp->z_untiltime <= min_time)
2166 continue;
2167 gmtoff = zp->z_gmtoff;
2168 eat(zp->z_filename, zp->z_linenum);
2169 *startbuf = '\0';
2170 startoff = zp->z_gmtoff;
2171 if (zp->z_nrules == 0) {
2172 stdoff = zp->z_stdoff;
2173 doabbr(startbuf, zp->z_format,
2174 NULL, stdoff != 0, FALSE);
2175 type = addtype(oadd(zp->z_gmtoff, stdoff),
2176 startbuf, stdoff != 0, startttisstd,
2177 startttisgmt);
2178 if (usestart) {
2179 addtt(starttime, type);
2180 usestart = FALSE;
2181 } else if (stdoff != 0)
2182 addtt(min_time, type);
2183 } else for (year = min_year; year <= max_year; ++year) {
2184 if (useuntil && year > zp->z_untilrule.r_hiyear)
2185 break;
2187 ** Mark which rules to do in the current year.
2188 ** For those to do, calculate rpytime(rp, year);
2190 for (j = 0; j < zp->z_nrules; ++j) {
2191 rp = &zp->z_rules[j];
2192 eats(zp->z_filename, zp->z_linenum,
2193 rp->r_filename, rp->r_linenum);
2194 rp->r_todo = year >= rp->r_loyear &&
2195 year <= rp->r_hiyear &&
2196 yearistype(year, rp->r_yrtype);
2197 if (rp->r_todo)
2198 rp->r_temp = rpytime(rp, year);
2200 for ( ; ; ) {
2201 register int k;
2202 register zic_t jtime, ktime;
2203 register zic_t offset;
2205 INITIALIZE(ktime);
2206 if (useuntil) {
2208 ** Turn untiltime into UT
2209 ** assuming the current gmtoff and
2210 ** stdoff values.
2212 untiltime = zp->z_untiltime;
2213 if (!zp->z_untilrule.r_todisgmt)
2214 untiltime = tadd(untiltime,
2215 -gmtoff);
2216 if (!zp->z_untilrule.r_todisstd)
2217 untiltime = tadd(untiltime,
2218 -stdoff);
2221 ** Find the rule (of those to do, if any)
2222 ** that takes effect earliest in the year.
2224 k = -1;
2225 for (j = 0; j < zp->z_nrules; ++j) {
2226 rp = &zp->z_rules[j];
2227 if (!rp->r_todo)
2228 continue;
2229 eats(zp->z_filename, zp->z_linenum,
2230 rp->r_filename, rp->r_linenum);
2231 offset = rp->r_todisgmt ? 0 : gmtoff;
2232 if (!rp->r_todisstd)
2233 offset = oadd(offset, stdoff);
2234 jtime = rp->r_temp;
2235 if (jtime == min_time ||
2236 jtime == max_time)
2237 continue;
2238 jtime = tadd(jtime, -offset);
2239 if (k < 0 || jtime < ktime) {
2240 k = j;
2241 ktime = jtime;
2244 if (k < 0)
2245 break; /* go on to next year */
2246 rp = &zp->z_rules[k];
2247 rp->r_todo = FALSE;
2248 if (useuntil && ktime >= untiltime)
2249 break;
2250 stdoff = rp->r_stdoff;
2251 if (usestart && ktime == starttime)
2252 usestart = FALSE;
2253 if (usestart) {
2254 if (ktime < starttime) {
2255 startoff = oadd(zp->z_gmtoff,
2256 stdoff);
2257 doabbr(startbuf, zp->z_format,
2258 rp->r_abbrvar,
2259 rp->r_stdoff != 0,
2260 FALSE);
2261 continue;
2263 if (*startbuf == '\0' &&
2264 startoff == oadd(zp->z_gmtoff,
2265 stdoff)) {
2266 doabbr(startbuf,
2267 zp->z_format,
2268 rp->r_abbrvar,
2269 rp->r_stdoff !=
2271 FALSE);
2274 eats(zp->z_filename, zp->z_linenum,
2275 rp->r_filename, rp->r_linenum);
2276 doabbr(ab, zp->z_format, rp->r_abbrvar,
2277 rp->r_stdoff != 0, FALSE);
2278 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2279 type = addtype(offset, ab, rp->r_stdoff != 0,
2280 rp->r_todisstd, rp->r_todisgmt);
2281 addtt(ktime, type);
2284 if (usestart) {
2285 if (*startbuf == '\0' &&
2286 zp->z_format != NULL &&
2287 strchr(zp->z_format, '%') == NULL &&
2288 strchr(zp->z_format, '/') == NULL)
2289 (void) strcpy(startbuf, zp->z_format);
2290 eat(zp->z_filename, zp->z_linenum);
2291 if (*startbuf == '\0')
2292 error(_("can't determine time zone abbreviation to use just after until time"));
2293 else addtt(starttime,
2294 addtype(startoff, startbuf,
2295 startoff != zp->z_gmtoff,
2296 startttisstd,
2297 startttisgmt));
2300 ** Now we may get to set starttime for the next zone line.
2302 if (useuntil) {
2303 startttisstd = zp->z_untilrule.r_todisstd;
2304 startttisgmt = zp->z_untilrule.r_todisgmt;
2305 starttime = zp->z_untiltime;
2306 if (!startttisstd)
2307 starttime = tadd(starttime, -stdoff);
2308 if (!startttisgmt)
2309 starttime = tadd(starttime, -gmtoff);
2312 if (do_extend) {
2314 ** If we're extending the explicitly listed observations
2315 ** for 400 years because we can't fill the POSIX-TZ field,
2316 ** check whether we actually ended up explicitly listing
2317 ** observations through that period. If there aren't any
2318 ** near the end of the 400-year period, add a redundant
2319 ** one at the end of the final year, to make it clear
2320 ** that we are claiming to have definite knowledge of
2321 ** the lack of transitions up to that point.
2323 struct rule xr;
2324 struct attype *lastat;
2325 xr.r_month = TM_JANUARY;
2326 xr.r_dycode = DC_DOM;
2327 xr.r_dayofmonth = 1;
2328 xr.r_tod = 0;
2329 for (lastat = &attypes[0], i = 1; i < timecnt; i++)
2330 if (attypes[i].at > lastat->at)
2331 lastat = &attypes[i];
2332 if (lastat->at < rpytime(&xr, max_year - 1)) {
2334 ** Create new type code for the redundant entry,
2335 ** to prevent it being optimised away.
2337 if (typecnt >= TZ_MAX_TYPES) {
2338 error(_("too many local time types"));
2339 exit(EXIT_FAILURE);
2341 gmtoffs[typecnt] = gmtoffs[lastat->type];
2342 isdsts[typecnt] = isdsts[lastat->type];
2343 ttisstds[typecnt] = ttisstds[lastat->type];
2344 ttisgmts[typecnt] = ttisgmts[lastat->type];
2345 abbrinds[typecnt] = abbrinds[lastat->type];
2346 ++typecnt;
2347 addtt(rpytime(&xr, max_year + 1), typecnt-1);
2350 writezone(zpfirst->z_name, envvar, version);
2351 free(startbuf);
2352 free(ab);
2353 free(envvar);
2356 static void
2357 addtt(const zic_t starttime, int type)
2359 if (starttime <= min_time ||
2360 (timecnt == 1 && attypes[0].at < min_time)) {
2361 gmtoffs[0] = gmtoffs[type];
2362 isdsts[0] = isdsts[type];
2363 ttisstds[0] = ttisstds[type];
2364 ttisgmts[0] = ttisgmts[type];
2365 if (abbrinds[type] != 0)
2366 (void) strcpy(chars, &chars[abbrinds[type]]);
2367 abbrinds[0] = 0;
2368 charcnt = strlen(chars) + 1;
2369 typecnt = 1;
2370 timecnt = 0;
2371 type = 0;
2373 if (timecnt >= TZ_MAX_TIMES) {
2374 error(_("too many transitions?!"));
2375 exit(EXIT_FAILURE);
2377 attypes[timecnt].at = starttime;
2378 attypes[timecnt].type = type;
2379 ++timecnt;
2382 static int
2383 addtype(const zic_t gmtoff, const char *const abbr, const int isdst,
2384 const int ttisstd, const int ttisgmt)
2386 register int i, j;
2388 if (isdst != TRUE && isdst != FALSE) {
2389 error(_("internal error - addtype called with bad isdst"));
2390 exit(EXIT_FAILURE);
2392 if (ttisstd != TRUE && ttisstd != FALSE) {
2393 error(_("internal error - addtype called with bad ttisstd"));
2394 exit(EXIT_FAILURE);
2396 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2397 error(_("internal error - addtype called with bad ttisgmt"));
2398 exit(EXIT_FAILURE);
2401 ** See if there's already an entry for this zone type.
2402 ** If so, just return its index.
2404 for (i = 0; i < typecnt; ++i) {
2405 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2406 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2407 ttisstd == ttisstds[i] &&
2408 ttisgmt == ttisgmts[i])
2409 return i;
2412 ** There isn't one; add a new one, unless there are already too
2413 ** many.
2415 if (typecnt >= TZ_MAX_TYPES) {
2416 error(_("too many local time types"));
2417 exit(EXIT_FAILURE);
2419 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2420 error(_("UT offset out of range"));
2421 exit(EXIT_FAILURE);
2423 gmtoffs[i] = gmtoff;
2424 isdsts[i] = isdst;
2425 ttisstds[i] = ttisstd;
2426 ttisgmts[i] = ttisgmt;
2428 for (j = 0; j < charcnt; ++j)
2429 if (strcmp(&chars[j], abbr) == 0)
2430 break;
2431 if (j == charcnt)
2432 newabbr(abbr);
2433 abbrinds[i] = j;
2434 ++typecnt;
2435 return i;
2438 static void
2439 leapadd(const zic_t t, const int positive, const int rolling, int count)
2441 register int i, j;
2443 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2444 error(_("too many leap seconds"));
2445 exit(EXIT_FAILURE);
2447 for (i = 0; i < leapcnt; ++i)
2448 if (t <= trans[i]) {
2449 if (t == trans[i]) {
2450 error(_("repeated leap second moment"));
2451 exit(EXIT_FAILURE);
2453 break;
2455 do {
2456 for (j = leapcnt; j > i; --j) {
2457 trans[j] = trans[j - 1];
2458 corr[j] = corr[j - 1];
2459 roll[j] = roll[j - 1];
2461 trans[i] = t;
2462 corr[i] = positive ? 1 : -count;
2463 roll[i] = rolling;
2464 ++leapcnt;
2465 } while (positive && --count != 0);
2468 static void
2469 adjleap(void)
2471 register int i;
2472 register zic_t last = 0;
2475 ** propagate leap seconds forward
2477 for (i = 0; i < leapcnt; ++i) {
2478 trans[i] = tadd(trans[i], last);
2479 last = corr[i] += last;
2483 static int
2484 yearistype(const int year, const char *const type)
2486 static char * buf;
2487 int result;
2489 if (type == NULL || *type == '\0')
2490 return TRUE;
2491 buf = erealloc(buf, 132 + strlen(yitcommand) + strlen(type));
2492 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
2493 result = system(buf);
2494 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2495 case 0:
2496 return TRUE;
2497 case 1:
2498 return FALSE;
2500 error(_("Wild result from command execution"));
2501 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2502 progname, buf, result);
2503 for ( ; ; )
2504 exit(EXIT_FAILURE);
2507 static int
2508 lowerit(int a)
2510 a = (unsigned char) a;
2511 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2514 /* case-insensitive equality */
2515 static ATTRIBUTE_PURE int
2516 ciequal(register const char *ap, register const char *bp)
2518 while (lowerit(*ap) == lowerit(*bp++))
2519 if (*ap++ == '\0')
2520 return TRUE;
2521 return FALSE;
2524 static ATTRIBUTE_PURE int
2525 itsabbr(register const char *abbr, register const char *word)
2527 if (lowerit(*abbr) != lowerit(*word))
2528 return FALSE;
2529 ++word;
2530 while (*++abbr != '\0')
2531 do {
2532 if (*word == '\0')
2533 return FALSE;
2534 } while (lowerit(*word++) != lowerit(*abbr));
2535 return TRUE;
2538 static ATTRIBUTE_PURE const struct lookup *
2539 byword(register const char *const word,
2540 register const struct lookup *const table)
2542 register const struct lookup * foundlp;
2543 register const struct lookup * lp;
2545 if (word == NULL || table == NULL)
2546 return NULL;
2548 ** Look for exact match.
2550 for (lp = table; lp->l_word != NULL; ++lp)
2551 if (ciequal(word, lp->l_word))
2552 return lp;
2554 ** Look for inexact match.
2556 foundlp = NULL;
2557 for (lp = table; lp->l_word != NULL; ++lp)
2558 if (itsabbr(word, lp->l_word)) {
2559 if (foundlp == NULL)
2560 foundlp = lp;
2561 else return NULL; /* multiple inexact matches */
2563 return foundlp;
2566 static char **
2567 getfields(register char *cp)
2569 register char * dp;
2570 register char ** array;
2571 register int nsubs;
2573 if (cp == NULL)
2574 return NULL;
2575 array = emalloc((strlen(cp) + 1) * sizeof *array);
2576 nsubs = 0;
2577 for ( ; ; ) {
2578 while (isascii((unsigned char) *cp) &&
2579 isspace((unsigned char) *cp))
2580 ++cp;
2581 if (*cp == '\0' || *cp == '#')
2582 break;
2583 array[nsubs++] = dp = cp;
2584 do {
2585 if ((*dp = *cp++) != '"')
2586 ++dp;
2587 else while ((*dp = *cp++) != '"')
2588 if (*dp != '\0')
2589 ++dp;
2590 else {
2591 error(_(
2592 "Odd number of quotation marks"
2594 exit(1);
2596 } while (*cp != '\0' && *cp != '#' &&
2597 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2598 if (isascii(*cp) && isspace((unsigned char) *cp))
2599 ++cp;
2600 *dp = '\0';
2602 array[nsubs] = NULL;
2603 return array;
2606 static ATTRIBUTE_PURE zic_t
2607 oadd(const zic_t t1, const zic_t t2)
2609 if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2) {
2610 error(_("time overflow"));
2611 exit(EXIT_FAILURE);
2613 return t1 + t2;
2616 static ATTRIBUTE_PURE zic_t
2617 tadd(const zic_t t1, const zic_t t2)
2619 if (t1 == max_time && t2 > 0)
2620 return max_time;
2621 if (t1 == min_time && t2 < 0)
2622 return min_time;
2623 if (t1 < 0 ? t2 < min_time - t1 : max_time - t1 < t2) {
2624 error(_("time overflow"));
2625 exit(EXIT_FAILURE);
2627 return t1 + t2;
2631 ** Given a rule, and a year, compute the date - in seconds since January 1,
2632 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2635 static zic_t
2636 rpytime(register const struct rule *const rp, register const zic_t wantedy)
2638 register int m, i;
2639 register zic_t dayoff; /* with a nod to Margaret O. */
2640 register zic_t t, y;
2642 if (wantedy == ZIC_MIN)
2643 return min_time;
2644 if (wantedy == ZIC_MAX)
2645 return max_time;
2646 dayoff = 0;
2647 m = TM_JANUARY;
2648 y = EPOCH_YEAR;
2649 while (wantedy != y) {
2650 if (wantedy > y) {
2651 i = len_years[isleap(y)];
2652 ++y;
2653 } else {
2654 --y;
2655 i = -len_years[isleap(y)];
2657 dayoff = oadd(dayoff, i);
2659 while (m != rp->r_month) {
2660 i = len_months[isleap(y)][m];
2661 dayoff = oadd(dayoff, i);
2662 ++m;
2664 i = rp->r_dayofmonth;
2665 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2666 if (rp->r_dycode == DC_DOWLEQ)
2667 --i;
2668 else {
2669 error(_("use of 2/29 in non leap-year"));
2670 exit(EXIT_FAILURE);
2673 --i;
2674 dayoff = oadd(dayoff, i);
2675 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2676 register zic_t wday;
2678 #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
2679 wday = EPOCH_WDAY;
2681 ** Don't trust mod of negative numbers.
2683 if (dayoff >= 0)
2684 wday = (wday + dayoff) % LDAYSPERWEEK;
2685 else {
2686 wday -= ((-dayoff) % LDAYSPERWEEK);
2687 if (wday < 0)
2688 wday += LDAYSPERWEEK;
2690 while (wday != rp->r_wday)
2691 if (rp->r_dycode == DC_DOWGEQ) {
2692 dayoff = oadd(dayoff, 1);
2693 if (++wday >= LDAYSPERWEEK)
2694 wday = 0;
2695 ++i;
2696 } else {
2697 dayoff = oadd(dayoff, -1);
2698 if (--wday < 0)
2699 wday = LDAYSPERWEEK - 1;
2700 --i;
2702 if (i < 0 || i >= len_months[isleap(y)][m]) {
2703 if (noise)
2704 warning(_("rule goes past start/end of month--\
2705 will not work with pre-2004 versions of zic"));
2708 if (dayoff < min_time / SECSPERDAY)
2709 return min_time;
2710 if (dayoff > max_time / SECSPERDAY)
2711 return max_time;
2712 t = (zic_t) dayoff * SECSPERDAY;
2713 return tadd(t, rp->r_tod);
2716 static void
2717 newabbr(const char *const string)
2719 register int i;
2721 if (strcmp(string, GRANDPARENTED) != 0) {
2722 register const char * cp;
2723 const char * mp;
2726 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2727 ** optionally followed by a + or - and a number from 1 to 14.
2729 cp = string;
2730 mp = NULL;
2731 while (isascii((unsigned char) *cp) &&
2732 isalpha((unsigned char) *cp))
2733 ++cp;
2734 if (cp - string == 0)
2735 mp = _("time zone abbreviation lacks alphabetic at start");
2736 if (noise && cp - string < 3)
2737 mp = _("time zone abbreviation has fewer than 3 alphabetics");
2738 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2739 mp = _("time zone abbreviation has too many alphabetics");
2740 if (mp == NULL && (*cp == '+' || *cp == '-')) {
2741 ++cp;
2742 if (isascii((unsigned char) *cp) &&
2743 isdigit((unsigned char) *cp))
2744 if (*cp++ == '1' &&
2745 *cp >= '0' && *cp <= '4')
2746 ++cp;
2748 if (*cp != '\0')
2749 mp = _("time zone abbreviation differs from POSIX standard");
2750 if (mp != NULL)
2751 warning("%s (%s)", mp, string);
2753 i = strlen(string) + 1;
2754 if (charcnt + i > TZ_MAX_CHARS) {
2755 error(_("too many, or too long, time zone abbreviations"));
2756 exit(EXIT_FAILURE);
2758 (void) strcpy(&chars[charcnt], string);
2759 charcnt += i;
2762 static int
2763 mkdirs(char *argname)
2765 register char * name;
2766 register char * cp;
2768 if (argname == NULL || *argname == '\0')
2769 return 0;
2770 cp = name = ecpyalloc(argname);
2771 while ((cp = strchr(cp + 1, '/')) != 0) {
2772 *cp = '\0';
2773 #ifdef HAVE_DOS_FILE_NAMES
2775 ** DOS drive specifier?
2777 if (isalpha((unsigned char) name[0]) &&
2778 name[1] == ':' && name[2] == '\0') {
2779 *cp = '/';
2780 continue;
2782 #endif
2783 if (!itsdir(name)) {
2785 ** It doesn't seem to exist, so we try to create it.
2786 ** Creation may fail because of the directory being
2787 ** created by some other multiprocessor, so we get
2788 ** to do extra checking.
2790 if (mkdir(name, MKDIR_UMASK) != 0) {
2791 const char *e = strerror(errno);
2793 if (errno != EEXIST || !itsdir(name)) {
2794 (void) fprintf(stderr,
2795 _("%s: Can't create directory %s: %s\n"),
2796 progname, name, e);
2797 free(name);
2798 return -1;
2802 *cp = '/';
2804 free(name);
2805 return 0;
2809 ** UNIX was a registered trademark of The Open Group in 2003.