2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
6 static char elsieid[] = "@(#)zic.c 8.25";
12 #define ZIC_VERSION '2'
14 typedef int_fast64_t zic_t;
16 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
17 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
18 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
24 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
26 #define MKDIR_UMASK 0755
30 ** On some ancient hosts, predicates like `isspace(C)' are defined
31 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
32 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
33 ** Neither the C Standard nor Posix require that `isascii' exist.
34 ** For portability, we check both ancient and modern requirements.
35 ** If isascii is not defined, the isascii check succeeds trivially.
42 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
43 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
45 #define end(cp) (strchr((cp), '\0'))
48 const char * r_filename;
52 int r_loyear; /* for example, 1986 */
53 int r_hiyear; /* for example, 1986 */
54 const char * r_yrtype;
58 int r_month; /* 0..11 */
60 int r_dycode; /* see below */
64 long r_tod; /* time from midnight */
65 int r_todisstd; /* above is standard time if TRUE */
66 /* or wall clock time if FALSE */
67 int r_todisgmt; /* above is GMT if TRUE */
68 /* or local time if FALSE */
69 long r_stdoff; /* offset from standard time */
70 const char * r_abbrvar; /* variable part of abbreviation */
72 int r_todo; /* a rule to do (used in outzone) */
73 zic_t r_temp; /* used in outzone */
77 ** r_dycode r_dayofmonth r_wday
80 #define DC_DOM 0 /* 1..31 */ /* unused */
81 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
82 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
85 const char * z_filename;
91 const char * z_format;
95 struct rule * z_rules;
98 struct rule z_untilrule;
102 extern int getopt(int argc, char * const argv[],
103 const char * options);
104 extern int link(const char * fromname, const char * toname);
105 extern char * optarg;
108 static void addtt(zic_t starttime, int type);
109 static int addtype(long gmtoff, const char * abbr, int isdst,
110 int ttisstd, int ttisgmt);
111 static void leapadd(zic_t t, int positive, int rolling, int count);
112 static void adjleap(void);
113 static void associate(void);
114 static int ciequal(const char * ap, const char * bp);
115 static void convert(long val, char * buf);
116 static void convert64(zic_t val, char * buf);
117 static void dolink(const char * fromfield, const char * tofield);
118 static void doabbr(char * abbr, const char * format,
119 const char * letters, int isdst, int doquotes);
120 static void eat(const char * name, int num);
121 static void eats(const char * name, int num,
122 const char * rname, int rnum);
123 static long eitol(int i);
124 static void error(const char * message);
125 static char ** getfields(char * buf);
126 static long gethms(const char * string, const char * errstrng,
128 static void infile(const char * filename);
129 static void inleap(char ** fields, int nfields);
130 static void inlink(char ** fields, int nfields);
131 static void inrule(char ** fields, int nfields);
132 static int inzcont(char ** fields, int nfields);
133 static int inzone(char ** fields, int nfields);
134 static int inzsub(char ** fields, int nfields, int iscont);
135 static int is32(zic_t x);
136 static int itsabbr(const char * abbr, const char * word);
137 static int itsdir(const char * name);
138 static int lowerit(int c);
139 static char * memcheck(char * tocheck);
140 static int mkdirs(char * filename);
141 static void newabbr(const char * abbr);
142 static long oadd(long t1, long t2);
143 static void outzone(const struct zone * zp, int ntzones);
144 static void puttzcode(long code, FILE * fp);
145 static void puttzcode64(zic_t code, FILE * fp);
146 static int rcomp(const void * leftp, const void * rightp);
147 static zic_t rpytime(const struct rule * rp, int wantedy);
148 static void rulesub(struct rule * rp,
149 const char * loyearp, const char * hiyearp,
150 const char * typep, const char * monthp,
151 const char * dayp, const char * timep);
152 static int stringoffset(char * result, long offset);
153 static int stringrule(char * result, const struct rule * rp,
154 long dstoff, long gmtoff);
155 static void stringzone(char * result,
156 const struct zone * zp, int ntzones);
157 static void setboundaries(void);
158 static zic_t tadd(zic_t t1, long t2);
159 static void usage(FILE *stream, int status);
160 static void writezone(const char * name, const char * string);
161 static int yearistype(int year, const char * type);
165 static const char * filename;
168 static int leapminyear;
169 static int leapmaxyear;
171 static int max_abbrvar_len;
172 static int max_format_len;
173 static zic_t max_time;
175 static zic_t min_time;
178 static const char * rfilename;
180 static const char * progname;
194 ** Which fields are which on a Zone line.
202 #define ZF_TILMONTH 6
205 #define ZONE_MINFIELDS 5
206 #define ZONE_MAXFIELDS 9
209 ** Which fields are which on a Zone continuation line.
215 #define ZFC_TILYEAR 3
216 #define ZFC_TILMONTH 4
218 #define ZFC_TILTIME 6
219 #define ZONEC_MINFIELDS 3
220 #define ZONEC_MAXFIELDS 7
223 ** Which files are which on a Rule line.
235 #define RULE_FIELDS 10
238 ** Which fields are which on a Link line.
243 #define LINK_FIELDS 3
246 ** Which fields are which on a Leap line.
255 #define LEAP_FIELDS 7
265 static struct rule * rules;
266 static int nrules; /* number of rules */
268 static struct zone * zones;
269 static int nzones; /* number of zones */
272 const char * l_filename;
278 static struct link * links;
286 static struct lookup const * byword(const char * string,
287 const struct lookup * lp);
289 static struct lookup const line_codes[] = {
297 static struct lookup const mon_names[] = {
298 { "January", TM_JANUARY },
299 { "February", TM_FEBRUARY },
300 { "March", TM_MARCH },
301 { "April", TM_APRIL },
305 { "August", TM_AUGUST },
306 { "September", TM_SEPTEMBER },
307 { "October", TM_OCTOBER },
308 { "November", TM_NOVEMBER },
309 { "December", TM_DECEMBER },
313 static struct lookup const wday_names[] = {
314 { "Sunday", TM_SUNDAY },
315 { "Monday", TM_MONDAY },
316 { "Tuesday", TM_TUESDAY },
317 { "Wednesday", TM_WEDNESDAY },
318 { "Thursday", TM_THURSDAY },
319 { "Friday", TM_FRIDAY },
320 { "Saturday", TM_SATURDAY },
324 static struct lookup const lasts[] = {
325 { "last-Sunday", TM_SUNDAY },
326 { "last-Monday", TM_MONDAY },
327 { "last-Tuesday", TM_TUESDAY },
328 { "last-Wednesday", TM_WEDNESDAY },
329 { "last-Thursday", TM_THURSDAY },
330 { "last-Friday", TM_FRIDAY },
331 { "last-Saturday", TM_SATURDAY },
335 static struct lookup const begin_years[] = {
336 { "minimum", YR_MINIMUM },
337 { "maximum", YR_MAXIMUM },
341 static struct lookup const end_years[] = {
342 { "minimum", YR_MINIMUM },
343 { "maximum", YR_MAXIMUM },
348 static struct lookup const leap_types[] = {
350 { "Stationary", FALSE },
354 static const int len_months[2][MONSPERYEAR] = {
355 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
356 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
359 static const int len_years[2] = {
360 DAYSPERNYEAR, DAYSPERLYEAR
363 static struct attype {
366 } attypes[TZ_MAX_TIMES];
367 static long gmtoffs[TZ_MAX_TYPES];
368 static char isdsts[TZ_MAX_TYPES];
369 static unsigned char abbrinds[TZ_MAX_TYPES];
370 static char ttisstds[TZ_MAX_TYPES];
371 static char ttisgmts[TZ_MAX_TYPES];
372 static char chars[TZ_MAX_CHARS];
373 static zic_t trans[TZ_MAX_LEAPS];
374 static long corr[TZ_MAX_LEAPS];
375 static char roll[TZ_MAX_LEAPS];
378 ** Memory allocation.
386 const char *e = strerror(errno);
388 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
395 #define emalloc(size) memcheck(imalloc(size))
396 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
397 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
398 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
405 eats(name, num, rname, rnum)
406 const char * const name;
408 const char * const rname;
419 const char * const name;
422 eats(name, num, (char *) NULL, -1);
427 const char * const string;
430 ** Match the format of "cc" to allow sh users to
431 ** zic ... 2>&1 | error -t "*" -v
434 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
435 filename, linenum, string);
436 if (rfilename != NULL)
437 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
438 rfilename, rlinenum);
439 (void) fprintf(stderr, "\n");
445 const char * const string;
449 cp = ecpyalloc(_("warning: "));
450 cp = ecatalloc(cp, string);
457 usage(FILE *stream, int status)
459 (void) fprintf(stream, _("%s: usage is %s \
460 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
461 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
463 Report bugs to tz@elsie.nci.nih.gov.\n"),
468 static const char * psxrules;
469 static const char * lcltime;
470 static const char * directory;
471 static const char * leapsec;
472 static const char * yitcommand;
484 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
485 #endif /* defined unix */
487 (void) setlocale(LC_ALL, "");
489 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
490 #endif /* defined TEXTDOMAINDIR */
491 (void) textdomain(TZ_DOMAIN);
492 #endif /* HAVE_GETTEXT */
494 if (TYPE_BIT(zic_t) < 64) {
495 (void) fprintf(stderr, "%s: %s\n", progname,
496 _("wild compilation-time specification of zic_t"));
499 for (i = 1; i < argc; ++i)
500 if (strcmp(argv[i], "--version") == 0) {
501 (void) printf("%s\n", elsieid);
503 } else if (strcmp(argv[i], "--help") == 0) {
504 usage(stdout, EXIT_SUCCESS);
506 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
509 usage(stderr, EXIT_FAILURE);
511 if (directory == NULL)
514 (void) fprintf(stderr,
515 _("%s: More than one -d option specified\n"),
524 (void) fprintf(stderr,
525 _("%s: More than one -l option specified\n"),
531 if (psxrules == NULL)
534 (void) fprintf(stderr,
535 _("%s: More than one -p option specified\n"),
541 if (yitcommand == NULL)
544 (void) fprintf(stderr,
545 _("%s: More than one -y option specified\n"),
554 (void) fprintf(stderr,
555 _("%s: More than one -L option specified\n"),
564 (void) printf("%s: -s ignored\n", progname);
567 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
568 usage(stderr, EXIT_FAILURE); /* usage message by request */
569 if (directory == NULL)
571 if (yitcommand == NULL)
572 yitcommand = "yearistype";
576 if (optind < argc && leapsec != NULL) {
581 for (i = optind; i < argc; ++i)
586 for (i = 0; i < nzones; i = j) {
588 ** Find the next non-continuation zone entry.
590 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
592 outzone(&zones[i], j - i);
597 for (i = 0; i < nlinks; ++i) {
598 eat(links[i].l_filename, links[i].l_linenum);
599 dolink(links[i].l_from, links[i].l_to);
601 for (j = 0; j < nlinks; ++j)
602 if (strcmp(links[i].l_to,
603 links[j].l_from) == 0)
604 warning(_("link to link"));
606 if (lcltime != NULL) {
607 eat("command line", 1);
608 dolink(lcltime, TZDEFAULT);
610 if (psxrules != NULL) {
611 eat("command line", 1);
612 dolink(psxrules, TZDEFRULES);
614 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
618 dolink(fromfield, tofield)
619 const char * const fromfield;
620 const char * const tofield;
622 register char * fromname;
623 register char * toname;
625 if (fromfield[0] == '/')
626 fromname = ecpyalloc(fromfield);
628 fromname = ecpyalloc(directory);
629 fromname = ecatalloc(fromname, "/");
630 fromname = ecatalloc(fromname, fromfield);
632 if (tofield[0] == '/')
633 toname = ecpyalloc(tofield);
635 toname = ecpyalloc(directory);
636 toname = ecatalloc(toname, "/");
637 toname = ecatalloc(toname, tofield);
640 ** We get to be careful here since
641 ** there's a fair chance of root running us.
644 (void) remove(toname);
645 if (link(fromname, toname) != 0) {
648 if (mkdirs(toname) != 0)
651 result = link(fromname, toname);
654 access(fromname, F_OK) == 0 &&
656 const char *s = tofield;
657 register char * symlinkcontents = NULL;
659 while ((s = strchr(s+1, '/')) != NULL)
661 ecatalloc(symlinkcontents,
664 ecatalloc(symlinkcontents,
666 result = symlink(symlinkcontents,
669 warning(_("hard link failed, symbolic link used"));
670 ifree(symlinkcontents);
672 #endif /* HAVE_SYMLINK */
674 const char *e = strerror(errno);
676 (void) fprintf(stderr,
677 _("%s: Can't link from %s to %s: %s\n"),
678 progname, fromname, toname, e);
686 #define TIME_T_BITS_IN_FILE 64
694 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
696 max_time = -(min_time + 1);
701 const char * const name;
703 register char * myname;
706 myname = ecpyalloc(name);
707 myname = ecatalloc(myname, "/.");
708 accres = access(myname, F_OK);
714 ** Associate sets of rules with zones.
718 ** Sort by rule name.
726 return strcmp(((const struct rule *) cp1)->r_name,
727 ((const struct rule *) cp2)->r_name);
733 register struct zone * zp;
734 register struct rule * rp;
735 register int base, out;
739 (void) qsort((void *) rules, (size_t) nrules,
740 (size_t) sizeof *rules, rcomp);
741 for (i = 0; i < nrules - 1; ++i) {
742 if (strcmp(rules[i].r_name,
743 rules[i + 1].r_name) != 0)
745 if (strcmp(rules[i].r_filename,
746 rules[i + 1].r_filename) == 0)
748 eat(rules[i].r_filename, rules[i].r_linenum);
749 warning(_("same rule name in multiple files"));
750 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
751 warning(_("same rule name in multiple files"));
752 for (j = i + 2; j < nrules; ++j) {
753 if (strcmp(rules[i].r_name,
754 rules[j].r_name) != 0)
756 if (strcmp(rules[i].r_filename,
757 rules[j].r_filename) == 0)
759 if (strcmp(rules[i + 1].r_filename,
760 rules[j].r_filename) == 0)
767 for (i = 0; i < nzones; ++i) {
772 for (base = 0; base < nrules; base = out) {
774 for (out = base + 1; out < nrules; ++out)
775 if (strcmp(rp->r_name, rules[out].r_name) != 0)
777 for (i = 0; i < nzones; ++i) {
779 if (strcmp(zp->z_rule, rp->r_name) != 0)
782 zp->z_nrules = out - base;
785 for (i = 0; i < nzones; ++i) {
787 if (zp->z_nrules == 0) {
789 ** Maybe we have a local standard time offset.
791 eat(zp->z_filename, zp->z_linenum);
792 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
795 ** Note, though, that if there's no rule,
796 ** a '%s' in the format is a bad thing.
798 if (strchr(zp->z_format, '%') != 0)
799 error(_("%s in ruleless zone"));
811 register char ** fields;
813 register const struct lookup * lp;
814 register int nfields;
815 register int wantcont;
819 if (strcmp(name, "-") == 0) {
820 name = _("standard input");
822 } else if ((fp = fopen(name, "r")) == NULL) {
823 const char *e = strerror(errno);
825 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
830 for (num = 1; ; ++num) {
832 if (fgets(buf, (int) sizeof buf, fp) != buf)
834 cp = strchr(buf, '\n');
836 error(_("line too long"));
840 fields = getfields(buf);
842 while (fields[nfields] != NULL) {
845 if (strcmp(fields[nfields], "-") == 0)
846 fields[nfields] = &nada;
851 } else if (wantcont) {
852 wantcont = inzcont(fields, nfields);
854 lp = byword(fields[0], line_codes);
856 error(_("input line of unknown type"));
857 else switch ((int) (lp->l_value)) {
859 inrule(fields, nfields);
863 wantcont = inzone(fields, nfields);
866 inlink(fields, nfields);
871 (void) fprintf(stderr,
872 _("%s: Leap line in non leap seconds file %s\n"),
874 else inleap(fields, nfields);
877 default: /* "cannot happen" */
878 (void) fprintf(stderr,
879 _("%s: panic: Invalid l_value %d\n"),
880 progname, lp->l_value);
884 ifree((char *) fields);
887 (void) fprintf(stderr, _("%s: Error reading %s\n"),
891 if (fp != stdin && fclose(fp)) {
892 const char *e = strerror(errno);
894 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
895 progname, filename, e);
899 error(_("expected continuation line not found"));
903 ** Convert a string of one of the forms
904 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
905 ** into a number of seconds.
906 ** A null string maps to zero.
907 ** Call error with errstring and return zero on errors.
911 gethms(string, errstring, signable)
913 const char * const errstring;
919 if (string == NULL || *string == '\0')
923 else if (*string == '-') {
927 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
929 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
931 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
932 &hh, &mm, &ss) != 3) {
937 mm < 0 || mm >= MINSPERHOUR ||
938 ss < 0 || ss > SECSPERMIN) {
942 if (LONG_MAX / SECSPERHOUR < hh) {
943 error(_("time overflow"));
946 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
947 warning(_("24:00 not handled by pre-1998 versions of zic"));
948 if (noise && (hh > HOURSPERDAY ||
949 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
950 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
951 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
952 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
956 inrule(fields, nfields)
957 register char ** const fields;
960 static struct rule r;
962 if (nfields != RULE_FIELDS) {
963 error(_("wrong number of fields on Rule line"));
966 if (*fields[RF_NAME] == '\0') {
967 error(_("nameless rule"));
970 r.r_filename = filename;
971 r.r_linenum = linenum;
972 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
973 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
974 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
975 r.r_name = ecpyalloc(fields[RF_NAME]);
976 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
977 if (max_abbrvar_len < strlen(r.r_abbrvar))
978 max_abbrvar_len = strlen(r.r_abbrvar);
979 rules = (struct rule *) (void *) erealloc((char *) rules,
980 (int) ((nrules + 1) * sizeof *rules));
985 inzone(fields, nfields)
986 register char ** const fields;
992 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
993 error(_("wrong number of fields on Zone line"));
996 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
997 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
999 _("\"Zone %s\" line and -l option are mutually exclusive"),
1004 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1005 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1007 _("\"Zone %s\" line and -p option are mutually exclusive"),
1012 for (i = 0; i < nzones; ++i)
1013 if (zones[i].z_name != NULL &&
1014 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1015 buf = erealloc(buf, (int) (132 +
1016 strlen(fields[ZF_NAME]) +
1017 strlen(zones[i].z_filename)));
1019 _("duplicate zone name %s (file \"%s\", line %d)"),
1021 zones[i].z_filename,
1022 zones[i].z_linenum);
1026 return inzsub(fields, nfields, FALSE);
1030 inzcont(fields, nfields)
1031 register char ** const fields;
1034 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1035 error(_("wrong number of fields on Zone continuation line"));
1038 return inzsub(fields, nfields, TRUE);
1042 inzsub(fields, nfields, iscont)
1043 register char ** const fields;
1048 static struct zone z;
1049 register int i_gmtoff, i_rule, i_format;
1050 register int i_untilyear, i_untilmonth;
1051 register int i_untilday, i_untiltime;
1052 register int hasuntil;
1055 i_gmtoff = ZFC_GMTOFF;
1057 i_format = ZFC_FORMAT;
1058 i_untilyear = ZFC_TILYEAR;
1059 i_untilmonth = ZFC_TILMONTH;
1060 i_untilday = ZFC_TILDAY;
1061 i_untiltime = ZFC_TILTIME;
1064 i_gmtoff = ZF_GMTOFF;
1066 i_format = ZF_FORMAT;
1067 i_untilyear = ZF_TILYEAR;
1068 i_untilmonth = ZF_TILMONTH;
1069 i_untilday = ZF_TILDAY;
1070 i_untiltime = ZF_TILTIME;
1071 z.z_name = ecpyalloc(fields[ZF_NAME]);
1073 z.z_filename = filename;
1074 z.z_linenum = linenum;
1075 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1076 if ((cp = strchr(fields[i_format], '%')) != 0) {
1077 if (*++cp != 's' || strchr(cp, '%') != 0) {
1078 error(_("invalid abbreviation format"));
1082 z.z_rule = ecpyalloc(fields[i_rule]);
1083 z.z_format = ecpyalloc(fields[i_format]);
1084 if (max_format_len < strlen(z.z_format))
1085 max_format_len = strlen(z.z_format);
1086 hasuntil = nfields > i_untilyear;
1088 z.z_untilrule.r_filename = filename;
1089 z.z_untilrule.r_linenum = linenum;
1090 rulesub(&z.z_untilrule,
1091 fields[i_untilyear],
1094 (nfields > i_untilmonth) ?
1095 fields[i_untilmonth] : "Jan",
1096 (nfields > i_untilday) ? fields[i_untilday] : "1",
1097 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1098 z.z_untiltime = rpytime(&z.z_untilrule,
1099 z.z_untilrule.r_loyear);
1100 if (iscont && nzones > 0 &&
1101 z.z_untiltime > min_time &&
1102 z.z_untiltime < max_time &&
1103 zones[nzones - 1].z_untiltime > min_time &&
1104 zones[nzones - 1].z_untiltime < max_time &&
1105 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1107 "Zone continuation line end time is not after end time of previous line"
1112 zones = (struct zone *) (void *) erealloc((char *) zones,
1113 (int) ((nzones + 1) * sizeof *zones));
1114 zones[nzones++] = z;
1116 ** If there was an UNTIL field on this line,
1117 ** there's more information about the zone on the next line.
1123 inleap(fields, nfields)
1124 register char ** const fields;
1127 register const char * cp;
1128 register const struct lookup * lp;
1130 int year, month, day;
1134 if (nfields != LEAP_FIELDS) {
1135 error(_("wrong number of fields on Leap line"));
1139 cp = fields[LP_YEAR];
1140 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1144 error(_("invalid leaping year"));
1147 if (!leapseen || leapmaxyear < year)
1149 if (!leapseen || leapminyear > year)
1155 i = len_years[isleap(j)];
1159 i = -len_years[isleap(j)];
1161 dayoff = oadd(dayoff, eitol(i));
1163 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1164 error(_("invalid month name"));
1167 month = lp->l_value;
1169 while (j != month) {
1170 i = len_months[isleap(year)][j];
1171 dayoff = oadd(dayoff, eitol(i));
1174 cp = fields[LP_DAY];
1175 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1176 day <= 0 || day > len_months[isleap(year)][month]) {
1177 error(_("invalid day of month"));
1180 dayoff = oadd(dayoff, eitol(day - 1));
1181 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1182 error(_("time before zero"));
1185 if (dayoff < min_time / SECSPERDAY) {
1186 error(_("time too small"));
1189 if (dayoff > max_time / SECSPERDAY) {
1190 error(_("time too large"));
1193 t = (zic_t) dayoff * SECSPERDAY;
1194 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1195 cp = fields[LP_CORR];
1197 register int positive;
1200 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1203 } else if (strcmp(cp, "--") == 0) {
1206 } else if (strcmp(cp, "+") == 0) {
1209 } else if (strcmp(cp, "++") == 0) {
1213 error(_("illegal CORRECTION field on Leap line"));
1216 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1218 "illegal Rolling/Stationary field on Leap line"
1222 leapadd(tadd(t, tod), positive, lp->l_value, count);
1227 inlink(fields, nfields)
1228 register char ** const fields;
1233 if (nfields != LINK_FIELDS) {
1234 error(_("wrong number of fields on Link line"));
1237 if (*fields[LF_FROM] == '\0') {
1238 error(_("blank FROM field on Link line"));
1241 if (*fields[LF_TO] == '\0') {
1242 error(_("blank TO field on Link line"));
1245 l.l_filename = filename;
1246 l.l_linenum = linenum;
1247 l.l_from = ecpyalloc(fields[LF_FROM]);
1248 l.l_to = ecpyalloc(fields[LF_TO]);
1249 links = (struct link *) (void *) erealloc((char *) links,
1250 (int) ((nlinks + 1) * sizeof *links));
1251 links[nlinks++] = l;
1255 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1256 register struct rule * const rp;
1257 const char * const loyearp;
1258 const char * const hiyearp;
1259 const char * const typep;
1260 const char * const monthp;
1261 const char * const dayp;
1262 const char * const timep;
1264 register const struct lookup * lp;
1265 register const char * cp;
1269 if ((lp = byword(monthp, mon_names)) == NULL) {
1270 error(_("invalid month name"));
1273 rp->r_month = lp->l_value;
1274 rp->r_todisstd = FALSE;
1275 rp->r_todisgmt = FALSE;
1276 dp = ecpyalloc(timep);
1278 ep = dp + strlen(dp) - 1;
1279 switch (lowerit(*ep)) {
1280 case 's': /* Standard */
1281 rp->r_todisstd = TRUE;
1282 rp->r_todisgmt = FALSE;
1285 case 'w': /* Wall */
1286 rp->r_todisstd = FALSE;
1287 rp->r_todisgmt = FALSE;
1290 case 'g': /* Greenwich */
1291 case 'u': /* Universal */
1292 case 'z': /* Zulu */
1293 rp->r_todisstd = TRUE;
1294 rp->r_todisgmt = TRUE;
1299 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1305 lp = byword(cp, begin_years);
1306 rp->r_lowasnum = lp == NULL;
1307 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1309 rp->r_loyear = INT_MIN;
1312 rp->r_loyear = INT_MAX;
1314 default: /* "cannot happen" */
1315 (void) fprintf(stderr,
1316 _("%s: panic: Invalid l_value %d\n"),
1317 progname, lp->l_value);
1319 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1320 error(_("invalid starting year"));
1324 lp = byword(cp, end_years);
1325 rp->r_hiwasnum = lp == NULL;
1326 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1328 rp->r_hiyear = INT_MIN;
1331 rp->r_hiyear = INT_MAX;
1334 rp->r_hiyear = rp->r_loyear;
1336 default: /* "cannot happen" */
1337 (void) fprintf(stderr,
1338 _("%s: panic: Invalid l_value %d\n"),
1339 progname, lp->l_value);
1341 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1342 error(_("invalid ending year"));
1345 if (rp->r_loyear > rp->r_hiyear) {
1346 error(_("starting year greater than ending year"));
1350 rp->r_yrtype = NULL;
1352 if (rp->r_loyear == rp->r_hiyear) {
1353 error(_("typed single year"));
1356 rp->r_yrtype = ecpyalloc(typep);
1360 ** Accept things such as:
1366 dp = ecpyalloc(dayp);
1367 if ((lp = byword(dp, lasts)) != NULL) {
1368 rp->r_dycode = DC_DOWLEQ;
1369 rp->r_wday = lp->l_value;
1370 rp->r_dayofmonth = len_months[1][rp->r_month];
1372 if ((ep = strchr(dp, '<')) != 0)
1373 rp->r_dycode = DC_DOWLEQ;
1374 else if ((ep = strchr(dp, '>')) != 0)
1375 rp->r_dycode = DC_DOWGEQ;
1378 rp->r_dycode = DC_DOM;
1380 if (rp->r_dycode != DC_DOM) {
1383 error(_("invalid day of month"));
1387 if ((lp = byword(dp, wday_names)) == NULL) {
1388 error(_("invalid weekday name"));
1392 rp->r_wday = lp->l_value;
1394 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1395 rp->r_dayofmonth <= 0 ||
1396 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1397 error(_("invalid day of month"));
1413 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1414 buf[i] = val >> shift;
1425 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1426 buf[i] = val >> shift;
1437 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1441 puttzcode64(val, fp)
1447 convert64(val, buf);
1448 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1456 const zic_t a = ((const struct attype *) avp)->at;
1457 const zic_t b = ((const struct attype *) bvp)->at;
1459 return (a < b) ? -1 : (a > b);
1466 return INT32_MIN <= x && x <= INT32_MAX;
1470 writezone(name, string)
1471 const char * const name;
1472 const char * const string;
1476 register int leapcnt32, leapi32;
1477 register int timecnt32, timei32;
1479 static char * fullname;
1480 static const struct tzhead tzh0;
1481 static struct tzhead tzh;
1482 zic_t ats[TZ_MAX_TIMES];
1483 unsigned char types[TZ_MAX_TIMES];
1489 (void) qsort((void *) attypes, (size_t) timecnt,
1490 (size_t) sizeof *attypes, atcomp);
1500 while (fromi < timecnt && attypes[fromi].at < min_time)
1503 while (fromi < timecnt && attypes[fromi].type == 0)
1504 ++fromi; /* handled by default rule */
1505 for ( ; fromi < timecnt; ++fromi) {
1506 if (toi != 0 && ((attypes[fromi].at +
1507 gmtoffs[attypes[toi - 1].type]) <=
1508 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1509 : attypes[toi - 2].type]))) {
1510 attypes[toi - 1].type =
1511 attypes[fromi].type;
1515 attypes[toi - 1].type != attypes[fromi].type)
1516 attypes[toi++] = attypes[fromi];
1523 for (i = 0; i < timecnt; ++i) {
1524 ats[i] = attypes[i].at;
1525 types[i] = attypes[i].type;
1528 ** Correct for leap seconds.
1530 for (i = 0; i < timecnt; ++i) {
1533 if (ats[i] > trans[j] - corr[j]) {
1534 ats[i] = tadd(ats[i], corr[j]);
1539 ** Figure out 32-bit-limited starts and counts.
1541 timecnt32 = timecnt;
1543 leapcnt32 = leapcnt;
1545 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1547 while (timecnt32 > 0 && !is32(ats[timei32])) {
1551 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1553 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1557 fullname = erealloc(fullname,
1558 (int) (strlen(directory) + 1 + strlen(name) + 1));
1559 (void) sprintf(fullname, "%s/%s", directory, name);
1561 ** Remove old file, if any, to snap links.
1563 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1564 const char *e = strerror(errno);
1566 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1567 progname, fullname, e);
1570 if ((fp = fopen(fullname, "wb")) == NULL) {
1571 if (mkdirs(fullname) != 0)
1573 if ((fp = fopen(fullname, "wb")) == NULL) {
1574 const char *e = strerror(errno);
1576 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1577 progname, fullname, e);
1581 for (pass = 1; pass <= 2; ++pass) {
1582 register int thistimei, thistimecnt;
1583 register int thisleapi, thisleapcnt;
1584 register int thistimelim, thisleaplim;
1585 int writetype[TZ_MAX_TIMES];
1586 int typemap[TZ_MAX_TYPES];
1587 register int thistypecnt;
1588 char thischars[TZ_MAX_CHARS];
1590 int indmap[TZ_MAX_CHARS];
1593 thistimei = timei32;
1594 thistimecnt = timecnt32;
1595 thisleapi = leapi32;
1596 thisleapcnt = leapcnt32;
1599 thistimecnt = timecnt;
1601 thisleapcnt = leapcnt;
1603 thistimelim = thistimei + thistimecnt;
1604 thisleaplim = thisleapi + thisleapcnt;
1605 for (i = 0; i < typecnt; ++i)
1606 writetype[i] = thistimecnt == timecnt;
1607 if (thistimecnt == 0) {
1609 ** No transition times fall in the current
1610 ** (32- or 64-bit) window.
1613 writetype[typecnt - 1] = TRUE;
1615 for (i = thistimei - 1; i < thistimelim; ++i)
1617 writetype[types[i]] = TRUE;
1619 ** For America/Godthab and Antarctica/Palmer
1622 writetype[0] = TRUE;
1624 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1626 ** For some pre-2011 systems: if the last-to-be-written
1627 ** standard (or daylight) type has an offset different from the
1628 ** most recently used offset,
1629 ** append an (unused) copy of the most recently used type
1630 ** (to help get global "altzone" and "timezone" variables
1634 register int mrudst, mrustd, hidst, histd, type;
1636 hidst = histd = mrudst = mrustd = -1;
1637 for (i = thistimei; i < thistimelim; ++i)
1638 if (isdsts[types[i]])
1640 else mrustd = types[i];
1641 for (i = 0; i < typecnt; ++i)
1647 if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1648 gmtoffs[hidst] != gmtoffs[mrudst]) {
1649 isdsts[mrudst] = -1;
1650 type = addtype(gmtoffs[mrudst],
1651 &chars[abbrinds[mrudst]],
1655 isdsts[mrudst] = TRUE;
1656 writetype[type] = TRUE;
1658 if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1659 gmtoffs[histd] != gmtoffs[mrustd]) {
1660 isdsts[mrustd] = -1;
1661 type = addtype(gmtoffs[mrustd],
1662 &chars[abbrinds[mrustd]],
1666 isdsts[mrustd] = FALSE;
1667 writetype[type] = TRUE;
1670 #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1672 for (i = 0; i < typecnt; ++i)
1673 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1674 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1677 for (i = 0; i < typecnt; ++i) {
1678 register char * thisabbr;
1682 if (indmap[abbrinds[i]] >= 0)
1684 thisabbr = &chars[abbrinds[i]];
1685 for (j = 0; j < thischarcnt; ++j)
1686 if (strcmp(&thischars[j], thisabbr) == 0)
1688 if (j == thischarcnt) {
1689 (void) strcpy(&thischars[(int) thischarcnt],
1691 thischarcnt += strlen(thisabbr) + 1;
1693 indmap[abbrinds[i]] = j;
1695 #define DO(field) (void) fwrite((void *) tzh.field, \
1696 (size_t) sizeof tzh.field, (size_t) 1, fp)
1698 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1699 tzh.tzh_version[0] = ZIC_VERSION;
1700 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1701 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1702 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1703 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1704 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1705 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1716 for (i = thistimei; i < thistimelim; ++i)
1718 puttzcode((long) ats[i], fp);
1719 else puttzcode64(ats[i], fp);
1720 for (i = thistimei; i < thistimelim; ++i) {
1723 uc = typemap[types[i]];
1724 (void) fwrite((void *) &uc,
1729 for (i = 0; i < typecnt; ++i)
1731 puttzcode(gmtoffs[i], fp);
1732 (void) putc(isdsts[i], fp);
1733 (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1735 if (thischarcnt != 0)
1736 (void) fwrite((void *) thischars,
1737 (size_t) sizeof thischars[0],
1738 (size_t) thischarcnt, fp);
1739 for (i = thisleapi; i < thisleaplim; ++i) {
1740 register zic_t todo;
1743 if (timecnt == 0 || trans[i] < ats[0]) {
1746 if (++j >= typecnt) {
1752 while (j < timecnt &&
1757 todo = tadd(trans[i], -gmtoffs[j]);
1758 } else todo = trans[i];
1760 puttzcode((long) todo, fp);
1761 else puttzcode64(todo, fp);
1762 puttzcode(corr[i], fp);
1764 for (i = 0; i < typecnt; ++i)
1766 (void) putc(ttisstds[i], fp);
1767 for (i = 0; i < typecnt; ++i)
1769 (void) putc(ttisgmts[i], fp);
1771 (void) fprintf(fp, "\n%s\n", string);
1772 if (ferror(fp) || fclose(fp)) {
1773 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1774 progname, fullname);
1780 doabbr(abbr, format, letters, isdst, doquotes)
1782 const char * const format;
1783 const char * const letters;
1788 register char * slashp;
1791 slashp = strchr(format, '/');
1792 if (slashp == NULL) {
1793 if (letters == NULL)
1794 (void) strcpy(abbr, format);
1795 else (void) sprintf(abbr, format, letters);
1797 (void) strcpy(abbr, slashp + 1);
1799 if (slashp > format)
1800 (void) strncpy(abbr, format,
1801 (unsigned) (slashp - format));
1802 abbr[slashp - format] = '\0';
1806 for (cp = abbr; *cp != '\0'; ++cp)
1807 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1808 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1811 if (len > 0 && *cp == '\0')
1813 abbr[len + 2] = '\0';
1814 abbr[len + 1] = '>';
1815 for ( ; len > 0; --len)
1816 abbr[len] = abbr[len - 1];
1831 stringoffset(result, offset)
1836 register int minutes;
1837 register int seconds;
1841 (void) strcpy(result, "-");
1844 seconds = offset % SECSPERMIN;
1845 offset /= SECSPERMIN;
1846 minutes = offset % MINSPERHOUR;
1847 offset /= MINSPERHOUR;
1849 if (hours >= HOURSPERDAY) {
1853 (void) sprintf(end(result), "%d", hours);
1854 if (minutes != 0 || seconds != 0) {
1855 (void) sprintf(end(result), ":%02d", minutes);
1857 (void) sprintf(end(result), ":%02d", seconds);
1863 stringrule(result, rp, dstoff, gmtoff)
1865 const struct rule * const rp;
1871 result = end(result);
1872 if (rp->r_dycode == DC_DOM) {
1873 register int month, total;
1875 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1878 for (month = 0; month < rp->r_month; ++month)
1879 total += len_months[0][month];
1880 (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1884 if (rp->r_dycode == DC_DOWGEQ) {
1885 if ((rp->r_dayofmonth % DAYSPERWEEK) != 1)
1887 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1888 } else if (rp->r_dycode == DC_DOWLEQ) {
1889 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1892 if ((rp->r_dayofmonth % DAYSPERWEEK) != 0)
1894 week = rp->r_dayofmonth / DAYSPERWEEK;
1896 } else return -1; /* "cannot happen" */
1897 (void) sprintf(result, "M%d.%d.%d",
1898 rp->r_month + 1, week, rp->r_wday);
1903 if (rp->r_todisstd && rp->r_stdoff == 0)
1909 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1910 (void) strcat(result, "/");
1911 if (stringoffset(end(result), tod) != 0)
1918 stringzone(result, zpfirst, zonecount)
1920 const struct zone * const zpfirst;
1921 const int zonecount;
1923 register const struct zone * zp;
1924 register struct rule * rp;
1925 register struct rule * stdrp;
1926 register struct rule * dstrp;
1928 register const char * abbrvar;
1931 zp = zpfirst + zonecount - 1;
1932 stdrp = dstrp = NULL;
1933 for (i = 0; i < zp->z_nrules; ++i) {
1934 rp = &zp->z_rules[i];
1935 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1937 if (rp->r_yrtype != NULL)
1939 if (rp->r_stdoff == 0) {
1949 if (stdrp == NULL && dstrp == NULL) {
1951 ** There are no rules running through "max".
1952 ** Let's find the latest rule.
1954 for (i = 0; i < zp->z_nrules; ++i) {
1955 rp = &zp->z_rules[i];
1956 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1957 (rp->r_hiyear == stdrp->r_hiyear &&
1958 rp->r_month > stdrp->r_month))
1961 if (stdrp != NULL && stdrp->r_stdoff != 0)
1962 return; /* We end up in DST (a POSIX no-no). */
1964 ** Horrid special case: if year is 2037,
1965 ** presume this is a zone handled on a year-by-year basis;
1966 ** do not try to apply a rule to the zone.
1968 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1971 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1973 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1974 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1975 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1981 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1982 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1983 if (stringoffset(end(result),
1984 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1988 (void) strcat(result, ",");
1989 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1993 (void) strcat(result, ",");
1994 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
2001 outzone(zpfirst, zonecount)
2002 const struct zone * const zpfirst;
2003 const int zonecount;
2005 register const struct zone * zp;
2006 register struct rule * rp;
2008 register int usestart, useuntil;
2009 register zic_t starttime, untiltime;
2010 register long gmtoff;
2011 register long stdoff;
2013 register long startoff;
2014 register int startttisstd;
2015 register int startttisgmt;
2017 register char * startbuf;
2019 register char * envvar;
2020 register int max_abbr_len;
2021 register int max_envvar_len;
2022 register int prodstic; /* all rules are min to max */
2024 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2025 max_envvar_len = 2 * max_abbr_len + 5 * 9;
2026 startbuf = emalloc(max_abbr_len + 1);
2027 ab = emalloc(max_abbr_len + 1);
2028 envvar = emalloc(max_envvar_len + 1);
2029 INITIALIZE(untiltime);
2030 INITIALIZE(starttime);
2032 ** Now. . .finally. . .generate some useful data!
2037 prodstic = zonecount == 1;
2039 ** Thanks to Earl Chew
2040 ** for noting the need to unconditionally initialize startttisstd.
2042 startttisstd = FALSE;
2043 startttisgmt = FALSE;
2044 min_year = max_year = EPOCH_YEAR;
2046 updateminmax(leapminyear);
2047 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
2049 for (i = 0; i < zonecount; ++i) {
2051 if (i < zonecount - 1)
2052 updateminmax(zp->z_untilrule.r_loyear);
2053 for (j = 0; j < zp->z_nrules; ++j) {
2054 rp = &zp->z_rules[j];
2056 updateminmax(rp->r_loyear);
2058 updateminmax(rp->r_hiyear);
2059 if (rp->r_lowasnum || rp->r_hiwasnum)
2064 ** Generate lots of data if a rule can't cover all future times.
2066 stringzone(envvar, zpfirst, zonecount);
2067 if (noise && envvar[0] == '\0') {
2070 wp = ecpyalloc(_("no POSIX environment variable for zone"));
2071 wp = ecatalloc(wp, " ");
2072 wp = ecatalloc(wp, zpfirst->z_name);
2076 if (envvar[0] == '\0') {
2077 if (min_year >= INT_MIN + YEARSPERREPEAT)
2078 min_year -= YEARSPERREPEAT;
2079 else min_year = INT_MIN;
2080 if (max_year <= INT_MAX - YEARSPERREPEAT)
2081 max_year += YEARSPERREPEAT;
2082 else max_year = INT_MAX;
2084 ** Regardless of any of the above,
2085 ** for a "proDSTic" zone which specifies that its rules
2086 ** always have and always will be in effect,
2087 ** we only need one cycle to define the zone.
2091 max_year = min_year + YEARSPERREPEAT;
2095 ** For the benefit of older systems,
2096 ** generate data from 1900 through 2037.
2098 if (min_year > 1900)
2100 if (max_year < 2037)
2102 for (i = 0; i < zonecount; ++i) {
2104 ** A guess that may well be corrected later.
2108 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2109 useuntil = i < (zonecount - 1);
2110 if (useuntil && zp->z_untiltime <= min_time)
2112 gmtoff = zp->z_gmtoff;
2113 eat(zp->z_filename, zp->z_linenum);
2115 startoff = zp->z_gmtoff;
2116 if (zp->z_nrules == 0) {
2117 stdoff = zp->z_stdoff;
2118 doabbr(startbuf, zp->z_format,
2119 (char *) NULL, stdoff != 0, FALSE);
2120 type = addtype(oadd(zp->z_gmtoff, stdoff),
2121 startbuf, stdoff != 0, startttisstd,
2124 addtt(starttime, type);
2126 } else if (stdoff != 0)
2127 addtt(min_time, type);
2128 } else for (year = min_year; year <= max_year; ++year) {
2129 if (useuntil && year > zp->z_untilrule.r_hiyear)
2132 ** Mark which rules to do in the current year.
2133 ** For those to do, calculate rpytime(rp, year);
2135 for (j = 0; j < zp->z_nrules; ++j) {
2136 rp = &zp->z_rules[j];
2137 eats(zp->z_filename, zp->z_linenum,
2138 rp->r_filename, rp->r_linenum);
2139 rp->r_todo = year >= rp->r_loyear &&
2140 year <= rp->r_hiyear &&
2141 yearistype(year, rp->r_yrtype);
2143 rp->r_temp = rpytime(rp, year);
2147 register zic_t jtime, ktime;
2148 register long offset;
2153 ** Turn untiltime into UTC
2154 ** assuming the current gmtoff and
2157 untiltime = zp->z_untiltime;
2158 if (!zp->z_untilrule.r_todisgmt)
2159 untiltime = tadd(untiltime,
2161 if (!zp->z_untilrule.r_todisstd)
2162 untiltime = tadd(untiltime,
2166 ** Find the rule (of those to do, if any)
2167 ** that takes effect earliest in the year.
2170 for (j = 0; j < zp->z_nrules; ++j) {
2171 rp = &zp->z_rules[j];
2174 eats(zp->z_filename, zp->z_linenum,
2175 rp->r_filename, rp->r_linenum);
2176 offset = rp->r_todisgmt ? 0 : gmtoff;
2177 if (!rp->r_todisstd)
2178 offset = oadd(offset, stdoff);
2180 if (jtime == min_time ||
2183 jtime = tadd(jtime, -offset);
2184 if (k < 0 || jtime < ktime) {
2190 break; /* go on to next year */
2191 rp = &zp->z_rules[k];
2193 if (useuntil && ktime >= untiltime)
2195 stdoff = rp->r_stdoff;
2196 if (usestart && ktime == starttime)
2199 if (ktime < starttime) {
2200 startoff = oadd(zp->z_gmtoff,
2202 doabbr(startbuf, zp->z_format,
2208 if (*startbuf == '\0' &&
2209 startoff == oadd(zp->z_gmtoff,
2219 eats(zp->z_filename, zp->z_linenum,
2220 rp->r_filename, rp->r_linenum);
2221 doabbr(ab, zp->z_format, rp->r_abbrvar,
2222 rp->r_stdoff != 0, FALSE);
2223 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2224 type = addtype(offset, ab, rp->r_stdoff != 0,
2225 rp->r_todisstd, rp->r_todisgmt);
2230 if (*startbuf == '\0' &&
2231 zp->z_format != NULL &&
2232 strchr(zp->z_format, '%') == NULL &&
2233 strchr(zp->z_format, '/') == NULL)
2234 (void) strcpy(startbuf, zp->z_format);
2235 eat(zp->z_filename, zp->z_linenum);
2236 if (*startbuf == '\0')
2237 error(_("can't determine time zone abbreviation to use just after until time"));
2238 else addtt(starttime,
2239 addtype(startoff, startbuf,
2240 startoff != zp->z_gmtoff,
2245 ** Now we may get to set starttime for the next zone line.
2248 startttisstd = zp->z_untilrule.r_todisstd;
2249 startttisgmt = zp->z_untilrule.r_todisgmt;
2250 starttime = zp->z_untiltime;
2252 starttime = tadd(starttime, -stdoff);
2254 starttime = tadd(starttime, -gmtoff);
2257 writezone(zpfirst->z_name, envvar);
2264 addtt(starttime, type)
2265 const zic_t starttime;
2268 if (starttime <= min_time ||
2269 (timecnt == 1 && attypes[0].at < min_time)) {
2270 gmtoffs[0] = gmtoffs[type];
2271 isdsts[0] = isdsts[type];
2272 ttisstds[0] = ttisstds[type];
2273 ttisgmts[0] = ttisgmts[type];
2274 if (abbrinds[type] != 0)
2275 (void) strcpy(chars, &chars[abbrinds[type]]);
2277 charcnt = strlen(chars) + 1;
2282 if (timecnt >= TZ_MAX_TIMES) {
2283 error(_("too many transitions?!"));
2286 attypes[timecnt].at = starttime;
2287 attypes[timecnt].type = type;
2292 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
2294 const char * const abbr;
2301 if (isdst != TRUE && isdst != FALSE) {
2302 error(_("internal error - addtype called with bad isdst"));
2305 if (ttisstd != TRUE && ttisstd != FALSE) {
2306 error(_("internal error - addtype called with bad ttisstd"));
2309 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2310 error(_("internal error - addtype called with bad ttisgmt"));
2314 ** See if there's already an entry for this zone type.
2315 ** If so, just return its index.
2317 for (i = 0; i < typecnt; ++i) {
2318 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2319 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2320 ttisstd == ttisstds[i] &&
2321 ttisgmt == ttisgmts[i])
2325 ** There isn't one; add a new one, unless there are already too
2328 if (typecnt >= TZ_MAX_TYPES) {
2329 error(_("too many local time types"));
2332 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2333 error(_("UTC offset out of range"));
2336 gmtoffs[i] = gmtoff;
2338 ttisstds[i] = ttisstd;
2339 ttisgmts[i] = ttisgmt;
2341 for (j = 0; j < charcnt; ++j)
2342 if (strcmp(&chars[j], abbr) == 0)
2352 leapadd(t, positive, rolling, count)
2360 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2361 error(_("too many leap seconds"));
2364 for (i = 0; i < leapcnt; ++i)
2365 if (t <= trans[i]) {
2366 if (t == trans[i]) {
2367 error(_("repeated leap second moment"));
2373 for (j = leapcnt; j > i; --j) {
2374 trans[j] = trans[j - 1];
2375 corr[j] = corr[j - 1];
2376 roll[j] = roll[j - 1];
2379 corr[i] = positive ? 1L : eitol(-count);
2382 } while (positive && --count != 0);
2389 register long last = 0;
2392 ** propagate leap seconds forward
2394 for (i = 0; i < leapcnt; ++i) {
2395 trans[i] = tadd(trans[i], last);
2396 last = corr[i] += last;
2401 yearistype(year, type)
2403 const char * const type;
2408 if (type == NULL || *type == '\0')
2410 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2411 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
2412 result = system(buf);
2413 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2419 error(_("Wild result from command execution"));
2420 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2421 progname, buf, result);
2430 a = (unsigned char) a;
2431 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2435 ciequal(ap, bp) /* case-insensitive equality */
2436 register const char * ap;
2437 register const char * bp;
2439 while (lowerit(*ap) == lowerit(*bp++))
2447 register const char * abbr;
2448 register const char * word;
2450 if (lowerit(*abbr) != lowerit(*word))
2453 while (*++abbr != '\0')
2457 } while (lowerit(*word++) != lowerit(*abbr));
2461 static const struct lookup *
2463 register const char * const word;
2464 register const struct lookup * const table;
2466 register const struct lookup * foundlp;
2467 register const struct lookup * lp;
2469 if (word == NULL || table == NULL)
2472 ** Look for exact match.
2474 for (lp = table; lp->l_word != NULL; ++lp)
2475 if (ciequal(word, lp->l_word))
2478 ** Look for inexact match.
2481 for (lp = table; lp->l_word != NULL; ++lp)
2482 if (itsabbr(word, lp->l_word)) {
2483 if (foundlp == NULL)
2485 else return NULL; /* multiple inexact matches */
2495 register char ** array;
2500 array = (char **) (void *)
2501 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2504 while (isascii((unsigned char) *cp) &&
2505 isspace((unsigned char) *cp))
2507 if (*cp == '\0' || *cp == '#')
2509 array[nsubs++] = dp = cp;
2511 if ((*dp = *cp++) != '"')
2513 else while ((*dp = *cp++) != '"')
2518 "Odd number of quotation marks"
2522 } while (*cp != '\0' && *cp != '#' &&
2523 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2524 if (isascii(*cp) && isspace((unsigned char) *cp))
2528 array[nsubs] = NULL;
2540 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2541 error(_("time overflow"));
2554 if (t1 == max_time && t2 > 0)
2556 if (t1 == min_time && t2 < 0)
2559 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2560 error(_("time overflow"));
2567 ** Given a rule, and a year, compute the date - in seconds since January 1,
2568 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2572 rpytime(rp, wantedy)
2573 register const struct rule * const rp;
2574 register const int wantedy;
2576 register int y, m, i;
2577 register long dayoff; /* with a nod to Margaret O. */
2580 if (wantedy == INT_MIN)
2582 if (wantedy == INT_MAX)
2587 while (wantedy != y) {
2589 i = len_years[isleap(y)];
2593 i = -len_years[isleap(y)];
2595 dayoff = oadd(dayoff, eitol(i));
2597 while (m != rp->r_month) {
2598 i = len_months[isleap(y)][m];
2599 dayoff = oadd(dayoff, eitol(i));
2602 i = rp->r_dayofmonth;
2603 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2604 if (rp->r_dycode == DC_DOWLEQ)
2607 error(_("use of 2/29 in non leap-year"));
2612 dayoff = oadd(dayoff, eitol(i));
2613 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2616 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2617 wday = eitol(EPOCH_WDAY);
2619 ** Don't trust mod of negative numbers.
2622 wday = (wday + dayoff) % LDAYSPERWEEK;
2624 wday -= ((-dayoff) % LDAYSPERWEEK);
2626 wday += LDAYSPERWEEK;
2628 while (wday != eitol(rp->r_wday))
2629 if (rp->r_dycode == DC_DOWGEQ) {
2630 dayoff = oadd(dayoff, (long) 1);
2631 if (++wday >= LDAYSPERWEEK)
2635 dayoff = oadd(dayoff, (long) -1);
2637 wday = LDAYSPERWEEK - 1;
2640 if (i < 0 || i >= len_months[isleap(y)][m]) {
2642 warning(_("rule goes past start/end of month--\
2643 will not work with pre-2004 versions of zic"));
2646 if (dayoff < min_time / SECSPERDAY)
2648 if (dayoff > max_time / SECSPERDAY)
2650 t = (zic_t) dayoff * SECSPERDAY;
2651 return tadd(t, rp->r_tod);
2656 const char * const string;
2660 if (strcmp(string, GRANDPARENTED) != 0) {
2661 register const char * cp;
2665 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2666 ** optionally followed by a + or - and a number from 1 to 14.
2670 while (isascii((unsigned char) *cp) &&
2671 isalpha((unsigned char) *cp))
2673 if (cp - string == 0)
2674 wp = _("time zone abbreviation lacks alphabetic at start");
2675 if (noise && cp - string > 3)
2676 wp = _("time zone abbreviation has more than 3 alphabetics");
2677 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2678 wp = _("time zone abbreviation has too many alphabetics");
2679 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2681 if (isascii((unsigned char) *cp) &&
2682 isdigit((unsigned char) *cp))
2684 *cp >= '0' && *cp <= '4')
2688 wp = _("time zone abbreviation differs from POSIX standard");
2691 wp = ecatalloc(wp, " (");
2692 wp = ecatalloc(wp, string);
2693 wp = ecatalloc(wp, ")");
2698 i = strlen(string) + 1;
2699 if (charcnt + i > TZ_MAX_CHARS) {
2700 error(_("too many, or too long, time zone abbreviations"));
2703 (void) strcpy(&chars[charcnt], string);
2704 charcnt += eitol(i);
2711 register char * name;
2714 if (argname == NULL || *argname == '\0')
2716 cp = name = ecpyalloc(argname);
2717 while ((cp = strchr(cp + 1, '/')) != 0) {
2721 ** DOS drive specifier?
2723 if (isalpha((unsigned char) name[0]) &&
2724 name[1] == ':' && name[2] == '\0') {
2728 #endif /* !defined unix */
2729 if (!itsdir(name)) {
2731 ** It doesn't seem to exist, so we try to create it.
2732 ** Creation may fail because of the directory being
2733 ** created by some other multiprocessor, so we get
2734 ** to do extra checking.
2736 if (mkdir(name, MKDIR_UMASK) != 0) {
2737 const char *e = strerror(errno);
2739 if (errno != EEXIST || !itsdir(name)) {
2740 (void) fprintf(stderr,
2741 _("%s: Can't create directory %s: %s\n"),
2761 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2762 (void) fprintf(stderr,
2763 _("%s: %d did not sign extend correctly\n"),
2771 ** UNIX was a registered trademark of The Open Group in 2003.