2 * This file is in the public domain, so clarified as of
3 * 2006-07-17 by Arthur David Olson.
9 #include "postgres_fe.h"
25 #define ZIC_VERSION '2'
29 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
30 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
31 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
33 #ifdef HAVE_SYS_STAT_H
39 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
41 #define MKDIR_UMASK 0755
45 static char elsieid[] = "@(#)zic.c 8.20";
48 * On some ancient hosts, predicates like `isspace(C)' are defined
49 * only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
50 * which says they are defined only if C == ((unsigned char) C) || C == EOF.
51 * Neither the C Standard nor Posix require that `isascii' exist.
52 * For portability, we check both ancient and modern requirements.
53 * If isascii is not defined, the isascii check succeeds trivially.
60 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
61 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
63 #define end(cp) (strchr((cp), '\0'))
67 const char *r_filename;
71 int r_loyear; /* for example, 1986 */
72 int r_hiyear; /* for example, 1986 */
77 int r_month; /* 0..11 */
79 int r_dycode; /* see below */
83 long r_tod; /* time from midnight */
84 int r_todisstd; /* above is standard time if TRUE */
85 /* or wall clock time if FALSE */
86 int r_todisgmt; /* above is GMT if TRUE */
87 /* or local time if FALSE */
88 long r_stdoff; /* offset from standard time */
89 const char *r_abbrvar; /* variable part of abbreviation */
91 int r_todo; /* a rule to do (used in outzone) */
92 zic_t r_temp; /* used in outzone */
96 * r_dycode r_dayofmonth r_wday
99 #define DC_DOM 0 /* 1..31 */ /* unused */
100 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
101 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
105 const char *z_filename;
111 const char *z_format;
115 struct rule *z_rules;
118 struct rule z_untilrule;
122 extern int link(const char *fromname, const char *toname);
123 static void addtt(const pg_time_t starttime, int type);
124 static int addtype(long gmtoff, const char *abbr, int isdst,
125 int ttisstd, int ttisgmt);
126 static void leapadd(const pg_time_t t, int positive, int rolling, int count);
127 static void adjleap(void);
128 static void associate(void);
129 static int ciequal(const char *ap, const char *bp);
130 static void convert(long val, char *buf);
131 static void dolink(const char *fromfile, const char *tofile);
132 static void doabbr(char *abbr, const char *format,
133 const char *letters, int isdst, int doquotes);
134 static void eat(const char *name, int num);
135 static void eats(const char *name, int num,
136 const char *rname, int rnum);
137 static long eitol(int i);
138 static void error(const char *message);
139 static char **getfields(char *buf);
140 static long gethms(const char *string, const char *errstrng,
142 static void infile(const char *filename);
143 static void inleap(char **fields, int nfields);
144 static void inlink(char **fields, int nfields);
145 static void inrule(char **fields, int nfields);
146 static int inzcont(char **fields, int nfields);
147 static int inzone(char **fields, int nfields);
148 static int inzsub(char **fields, int nfields, int iscont);
149 static int itsabbr(const char *abbr, const char *word);
150 static int itsdir(const char *name);
151 static int lowerit(int c);
152 static char *memcheck(char *tocheck);
153 static int mkdirs(char *filename);
154 static void newabbr(const char *abbr);
155 static long oadd(long t1, long t2);
156 static void outzone(const struct zone * zp, int ntzones);
157 static void puttzcode(long code, FILE *fp);
158 static int rcomp(const void *leftp, const void *rightp);
159 static pg_time_t rpytime(const struct rule * rp, int wantedy);
160 static void rulesub(struct rule * rp,
161 const char *loyearp, const char *hiyearp,
162 const char *typep, const char *monthp,
163 const char *dayp, const char *timep);
164 static void setboundaries(void);
165 static pg_time_t tadd(const pg_time_t t1, long t2);
166 static void usage(FILE *stream, int status);
167 static void writezone(const char *name, const char *string);
168 static int yearistype(int year, const char *type);
172 static const char *filename;
175 static int leapminyear;
176 static int leapmaxyear;
178 static int max_abbrvar_len;
179 static int max_format_len;
180 static zic_t max_time;
182 static zic_t min_time;
185 static int print_abbrevs;
186 static zic_t print_cutoff;
187 static const char *rfilename;
189 static const char *progname;
203 * Which fields are which on a Zone line.
211 #define ZF_TILMONTH 6
214 #define ZONE_MINFIELDS 5
215 #define ZONE_MAXFIELDS 9
218 * Which fields are which on a Zone continuation line.
224 #define ZFC_TILYEAR 3
225 #define ZFC_TILMONTH 4
227 #define ZFC_TILTIME 6
228 #define ZONEC_MINFIELDS 3
229 #define ZONEC_MAXFIELDS 7
232 * Which files are which on a Rule line.
244 #define RULE_FIELDS 10
247 * Which fields are which on a Link line.
252 #define LINK_FIELDS 3
255 * Which fields are which on a Leap line.
264 #define LEAP_FIELDS 7
274 static struct rule *rules;
275 static int nrules; /* number of rules */
277 static struct zone *zones;
278 static int nzones; /* number of zones */
282 const char *l_filename;
288 static struct link *links;
297 static struct lookup const *byword(const char *string,
298 const struct lookup * lp);
300 static struct lookup const line_codes[] = {
308 static struct lookup const mon_names[] = {
309 {"January", TM_JANUARY},
310 {"February", TM_FEBRUARY},
316 {"August", TM_AUGUST},
317 {"September", TM_SEPTEMBER},
318 {"October", TM_OCTOBER},
319 {"November", TM_NOVEMBER},
320 {"December", TM_DECEMBER},
324 static struct lookup const wday_names[] = {
325 {"Sunday", TM_SUNDAY},
326 {"Monday", TM_MONDAY},
327 {"Tuesday", TM_TUESDAY},
328 {"Wednesday", TM_WEDNESDAY},
329 {"Thursday", TM_THURSDAY},
330 {"Friday", TM_FRIDAY},
331 {"Saturday", TM_SATURDAY},
335 static struct lookup const lasts[] = {
336 {"last-Sunday", TM_SUNDAY},
337 {"last-Monday", TM_MONDAY},
338 {"last-Tuesday", TM_TUESDAY},
339 {"last-Wednesday", TM_WEDNESDAY},
340 {"last-Thursday", TM_THURSDAY},
341 {"last-Friday", TM_FRIDAY},
342 {"last-Saturday", TM_SATURDAY},
346 static struct lookup const begin_years[] = {
347 {"minimum", YR_MINIMUM},
348 {"maximum", YR_MAXIMUM},
352 static struct lookup const end_years[] = {
353 {"minimum", YR_MINIMUM},
354 {"maximum", YR_MAXIMUM},
359 static struct lookup const leap_types[] = {
361 {"Stationary", FALSE},
365 static const int len_months[2][MONSPERYEAR] = {
366 {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
367 {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
370 static const int len_years[2] = {
371 DAYSPERNYEAR, DAYSPERLYEAR
378 } attypes[TZ_MAX_TIMES];
379 static long gmtoffs[TZ_MAX_TYPES];
380 static char isdsts[TZ_MAX_TYPES];
381 static unsigned char abbrinds[TZ_MAX_TYPES];
382 static char ttisstds[TZ_MAX_TYPES];
383 static char ttisgmts[TZ_MAX_TYPES];
384 static char chars[TZ_MAX_CHARS];
385 static zic_t trans[TZ_MAX_LEAPS];
386 static long corr[TZ_MAX_LEAPS];
387 static char roll[TZ_MAX_LEAPS];
398 const char *e = strerror(errno);
400 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
407 #define emalloc(size) memcheck(imalloc(size))
408 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
409 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
410 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
417 eats(const char *name, int num, const char *rname, int rnum)
426 eat(const char *name, int num)
428 eats(name, num, (char *) NULL, -1);
432 error(const char *string)
435 * Match the format of "cc" to allow sh users to zic ... 2>&1 | error -t
436 * "*" -v on BSD systems.
438 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
439 filename, linenum, string);
440 if (rfilename != NULL)
441 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
442 rfilename, rlinenum);
443 (void) fprintf(stderr, "\n");
448 warning(const char *string)
452 cp = ecpyalloc(_("warning: "));
453 cp = ecatalloc(cp, string);
460 usage(FILE *stream, int status)
462 (void) fprintf(stream, _("%s: usage is %s \
463 [ --version ] [ --help ] [ -v ] [ -P ] [ -l localtime ] [ -p posixrules ] \\\n\
464 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
466 Report bugs to tz@elsie.nci.nih.gov.\n"),
471 static const char *psxrules;
472 static const char *lcltime;
473 static const char *directory;
474 static const char *leapsec;
475 static const char *yitcommand;
478 main(int argc, char *argv[])
485 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
488 if (TYPE_BIT(zic_t) < 64)
490 (void) fprintf(stderr, "%s: %s\n", progname,
491 _("wild compilation-time specification of zic_t"));
494 for (i = 1; i < argc; ++i)
495 if (strcmp(argv[i], "--version") == 0)
497 (void) printf("%s\n", elsieid);
500 else if (strcmp(argv[i], "--help") == 0)
502 usage(stdout, EXIT_SUCCESS);
504 while ((c = getopt(argc, argv, "d:l:p:L:vPsy:")) != EOF && c != -1)
508 usage(stderr, EXIT_FAILURE);
510 if (directory == NULL)
511 directory = strdup(optarg);
514 (void) fprintf(stderr,
515 _("%s: More than one -d option specified\n"),
522 lcltime = strdup(optarg);
525 (void) fprintf(stderr,
526 _("%s: More than one -l option specified\n"),
532 if (psxrules == NULL)
533 psxrules = strdup(optarg);
536 (void) fprintf(stderr,
537 _("%s: More than one -p option specified\n"),
543 if (yitcommand == NULL)
544 yitcommand = strdup(optarg);
547 (void) fprintf(stderr,
548 _("%s: More than one -y option specified\n"),
555 leapsec = strdup(optarg);
558 (void) fprintf(stderr,
559 _("%s: More than one -L option specified\n"),
568 print_abbrevs = TRUE;
569 print_cutoff = time(NULL);
572 (void) printf("%s: -s ignored\n", progname);
575 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
576 usage(stderr, EXIT_FAILURE); /* usage message by request */
577 if (directory == NULL)
579 if (yitcommand == NULL)
580 yitcommand = "yearistype";
584 if (optind < argc && leapsec != NULL)
590 for (i = optind; i < argc; ++i)
595 for (i = 0; i < nzones; i = j)
598 * Find the next non-continuation zone entry.
600 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
602 outzone(&zones[i], j - i);
608 for (i = 0; i < nlinks; ++i)
610 eat(links[i].l_filename, links[i].l_linenum);
611 dolink(links[i].l_from, links[i].l_to);
613 for (j = 0; j < nlinks; ++j)
614 if (strcmp(links[i].l_to,
615 links[j].l_from) == 0)
616 warning(_("link to link"));
620 eat("command line", 1);
621 dolink(lcltime, TZDEFAULT);
623 if (psxrules != NULL)
625 eat("command line", 1);
626 dolink(psxrules, TZDEFRULES);
628 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
632 dolink(const char *fromfield, const char *tofield)
637 if (fromfield[0] == '/')
638 fromname = ecpyalloc(fromfield);
641 fromname = ecpyalloc(directory);
642 fromname = ecatalloc(fromname, "/");
643 fromname = ecatalloc(fromname, fromfield);
645 if (tofield[0] == '/')
646 toname = ecpyalloc(tofield);
649 toname = ecpyalloc(directory);
650 toname = ecatalloc(toname, "/");
651 toname = ecatalloc(toname, tofield);
655 * We get to be careful here since there's a fair chance of root running
659 (void) remove(toname);
660 if (link(fromname, toname) != 0)
664 if (mkdirs(toname) != 0)
667 result = link(fromname, toname);
670 access(fromname, F_OK) == 0 &&
673 const char *s = tofield;
674 char *symlinkcontents = NULL;
676 while ((s = strchr(s + 1, '/')) != NULL)
677 symlinkcontents = ecatalloc(symlinkcontents, "../");
678 symlinkcontents = ecatalloc(symlinkcontents, fromfield);
680 result = symlink(symlinkcontents, toname);
682 warning(_("hard link failed, symbolic link used"));
683 ifree(symlinkcontents);
688 const char *e = strerror(errno);
690 (void) fprintf(stderr,
691 _("%s: Cannot link from %s to %s: %s\n"),
692 progname, fromname, toname, e);
700 #define TIME_T_BITS_IN_FILE 64
708 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
710 max_time = -(min_time + 1);
714 itsdir(const char *name)
719 myname = ecpyalloc(name);
720 myname = ecatalloc(myname, "/.");
721 accres = access(myname, F_OK);
727 * Associate sets of rules with zones.
735 rcomp(const void *cp1, const void *cp2)
737 return strcmp(((const struct rule *) cp1)->r_name,
738 ((const struct rule *) cp2)->r_name);
753 (void) qsort((void *) rules, (size_t) nrules,
754 (size_t) sizeof *rules, rcomp);
755 for (i = 0; i < nrules - 1; ++i)
757 if (strcmp(rules[i].r_name,
758 rules[i + 1].r_name) != 0)
760 if (strcmp(rules[i].r_filename,
761 rules[i + 1].r_filename) == 0)
763 eat(rules[i].r_filename, rules[i].r_linenum);
764 warning(_("same rule name in multiple files"));
765 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
766 warning(_("same rule name in multiple files"));
767 for (j = i + 2; j < nrules; ++j)
769 if (strcmp(rules[i].r_name,
770 rules[j].r_name) != 0)
772 if (strcmp(rules[i].r_filename,
773 rules[j].r_filename) == 0)
775 if (strcmp(rules[i + 1].r_filename,
776 rules[j].r_filename) == 0)
783 for (i = 0; i < nzones; ++i)
789 for (base = 0; base < nrules; base = out)
792 for (out = base + 1; out < nrules; ++out)
793 if (strcmp(rp->r_name, rules[out].r_name) != 0)
795 for (i = 0; i < nzones; ++i)
798 if (strcmp(zp->z_rule, rp->r_name) != 0)
801 zp->z_nrules = out - base;
804 for (i = 0; i < nzones; ++i)
807 if (zp->z_nrules == 0)
810 * Maybe we have a local standard time offset.
812 eat(zp->z_filename, zp->z_linenum);
813 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
817 * Note, though, that if there's no rule, a '%s' in the format is
820 if (strchr(zp->z_format, '%') != NULL)
821 error(_("%s in ruleless zone"));
829 infile(const char *name)
834 const struct lookup *lp;
840 if (strcmp(name, "-") == 0)
842 name = _("standard input");
845 else if ((fp = fopen(name, "r")) == NULL)
847 const char *e = strerror(errno);
849 (void) fprintf(stderr, _("%s: Cannot open %s: %s\n"),
854 for (num = 1;; ++num)
857 if (fgets(buf, (int) sizeof buf, fp) != buf)
859 cp = strchr(buf, '\n');
862 error(_("line too long"));
866 fields = getfields(buf);
868 while (fields[nfields] != NULL)
872 if (strcmp(fields[nfields], "-") == 0)
873 fields[nfields] = &nada;
881 wantcont = inzcont(fields, nfields);
884 lp = byword(fields[0], line_codes);
886 error(_("input line of unknown type"));
888 switch ((int) (lp->l_value))
891 inrule(fields, nfields);
895 wantcont = inzone(fields, nfields);
898 inlink(fields, nfields);
903 (void) fprintf(stderr,
904 _("%s: Leap line in non leap seconds file %s\n"),
907 inleap(fields, nfields);
910 default: /* "cannot happen" */
911 (void) fprintf(stderr,
912 _("%s: panic: Invalid l_value %d\n"),
913 progname, lp->l_value);
917 ifree((char *) fields);
921 (void) fprintf(stderr, _("%s: Error reading %s\n"),
925 if (fp != stdin && fclose(fp))
927 const char *e = strerror(errno);
929 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
930 progname, filename, e);
934 error(_("expected continuation line not found"));
938 * Convert a string of one of the forms
939 * h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
940 * into a number of seconds.
941 * A null string maps to zero.
942 * Call error with errstring and return zero on errors.
946 gethms(const char *string, const char *errstring, int signable)
953 if (string == NULL || *string == '\0')
957 else if (*string == '-')
964 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
966 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
968 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
975 mm < 0 || mm >= MINSPERHOUR ||
976 ss < 0 || ss > SECSPERMIN)
981 if (LONG_MAX / SECSPERHOUR < hh)
983 error(_("time overflow"));
986 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
987 warning(_("24:00 not handled by pre-1998 versions of zic"));
988 if (noise && (hh > HOURSPERDAY ||
989 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
990 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
991 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
992 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
996 inrule(char **fields, int nfields)
998 static struct rule r;
1000 if (nfields != RULE_FIELDS)
1002 error(_("wrong number of fields on Rule line"));
1005 if (*fields[RF_NAME] == '\0')
1007 error(_("nameless rule"));
1010 r.r_filename = filename;
1011 r.r_linenum = linenum;
1012 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
1013 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
1014 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
1015 r.r_name = ecpyalloc(fields[RF_NAME]);
1016 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
1017 if (max_abbrvar_len < strlen(r.r_abbrvar))
1018 max_abbrvar_len = strlen(r.r_abbrvar);
1019 rules = (struct rule *) (void *) erealloc((char *) rules,
1020 (int) ((nrules + 1) * sizeof *rules));
1021 rules[nrules++] = r;
1025 inzone(char **fields, int nfields)
1030 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
1032 error(_("wrong number of fields on Zone line"));
1035 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL)
1037 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1039 _("\"Zone %s\" line and -l option are mutually exclusive"),
1044 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
1046 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1048 _("\"Zone %s\" line and -p option are mutually exclusive"),
1053 for (i = 0; i < nzones; ++i)
1054 if (zones[i].z_name != NULL &&
1055 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
1057 buf = erealloc(buf, (int) (132 +
1058 strlen(fields[ZF_NAME]) +
1059 strlen(zones[i].z_filename)));
1061 _("duplicate zone name %s (file \"%s\", line %d)"),
1063 zones[i].z_filename,
1064 zones[i].z_linenum);
1068 return inzsub(fields, nfields, FALSE);
1072 inzcont(char **fields, int nfields)
1074 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
1076 error(_("wrong number of fields on Zone continuation line"));
1079 return inzsub(fields, nfields, TRUE);
1083 inzsub(char **fields, int nfields, int iscont)
1086 static struct zone z;
1098 i_gmtoff = ZFC_GMTOFF;
1100 i_format = ZFC_FORMAT;
1101 i_untilyear = ZFC_TILYEAR;
1102 i_untilmonth = ZFC_TILMONTH;
1103 i_untilday = ZFC_TILDAY;
1104 i_untiltime = ZFC_TILTIME;
1109 i_gmtoff = ZF_GMTOFF;
1111 i_format = ZF_FORMAT;
1112 i_untilyear = ZF_TILYEAR;
1113 i_untilmonth = ZF_TILMONTH;
1114 i_untilday = ZF_TILDAY;
1115 i_untiltime = ZF_TILTIME;
1116 z.z_name = ecpyalloc(fields[ZF_NAME]);
1118 z.z_filename = filename;
1119 z.z_linenum = linenum;
1120 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1121 if ((cp = strchr(fields[i_format], '%')) != NULL)
1123 if (*++cp != 's' || strchr(cp, '%') != NULL)
1125 error(_("invalid abbreviation format"));
1129 z.z_rule = ecpyalloc(fields[i_rule]);
1130 z.z_format = ecpyalloc(fields[i_format]);
1131 if (max_format_len < strlen(z.z_format))
1132 max_format_len = strlen(z.z_format);
1133 hasuntil = nfields > i_untilyear;
1136 z.z_untilrule.r_filename = filename;
1137 z.z_untilrule.r_linenum = linenum;
1138 rulesub(&z.z_untilrule,
1139 fields[i_untilyear],
1142 (nfields > i_untilmonth) ?
1143 fields[i_untilmonth] : "Jan",
1144 (nfields > i_untilday) ? fields[i_untilday] : "1",
1145 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1146 z.z_untiltime = rpytime(&z.z_untilrule,
1147 z.z_untilrule.r_loyear);
1148 if (iscont && nzones > 0 &&
1149 z.z_untiltime > min_time &&
1150 z.z_untiltime < max_time &&
1151 zones[nzones - 1].z_untiltime > min_time &&
1152 zones[nzones - 1].z_untiltime < max_time &&
1153 zones[nzones - 1].z_untiltime >= z.z_untiltime)
1155 error(_("Zone continuation line end time is not after end time of previous line"));
1159 zones = (struct zone *) (void *) erealloc((char *) zones,
1160 (int) ((nzones + 1) * sizeof *zones));
1161 zones[nzones++] = z;
1164 * If there was an UNTIL field on this line, there's more information
1165 * about the zone on the next line.
1171 inleap(char **fields, int nfields)
1174 const struct lookup *lp;
1184 if (nfields != LEAP_FIELDS)
1186 error(_("wrong number of fields on Leap line"));
1190 cp = fields[LP_YEAR];
1191 if (sscanf(cp, scheck(cp, "%d"), &year) != 1)
1196 error(_("invalid leaping year"));
1199 if (!leapseen || leapmaxyear < year)
1201 if (!leapseen || leapminyear > year)
1209 i = len_years[isleap(j)];
1215 i = -len_years[isleap(j)];
1217 dayoff = oadd(dayoff, eitol(i));
1219 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
1221 error(_("invalid month name"));
1224 month = lp->l_value;
1228 i = len_months[isleap(year)][j];
1229 dayoff = oadd(dayoff, eitol(i));
1232 cp = fields[LP_DAY];
1233 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1234 day <= 0 || day > len_months[isleap(year)][month])
1236 error(_("invalid day of month"));
1239 dayoff = oadd(dayoff, eitol(day - 1));
1240 if (dayoff < min_time / SECSPERDAY)
1242 error(_("time too small"));
1245 if (dayoff > max_time / SECSPERDAY)
1247 error(_("time too large"));
1250 t = (zic_t) dayoff *SECSPERDAY;
1252 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1253 cp = fields[LP_CORR];
1258 if (strcmp(cp, "") == 0)
1259 { /* infile() turns "-" into "" */
1263 else if (strcmp(cp, "--") == 0)
1268 else if (strcmp(cp, "+") == 0)
1273 else if (strcmp(cp, "++") == 0)
1280 error(_("illegal CORRECTION field on Leap line"));
1283 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL)
1285 error(_("illegal Rolling/Stationary field on Leap line"));
1288 leapadd(tadd(t, tod), positive, lp->l_value, count);
1293 inlink(char **fields, int nfields)
1297 if (nfields != LINK_FIELDS)
1299 error(_("wrong number of fields on Link line"));
1302 if (*fields[LF_FROM] == '\0')
1304 error(_("blank FROM field on Link line"));
1307 if (*fields[LF_TO] == '\0')
1309 error(_("blank TO field on Link line"));
1312 l.l_filename = filename;
1313 l.l_linenum = linenum;
1314 l.l_from = ecpyalloc(fields[LF_FROM]);
1315 l.l_to = ecpyalloc(fields[LF_TO]);
1316 links = (struct link *) (void *) erealloc((char *) links,
1317 (int) ((nlinks + 1) * sizeof *links));
1318 links[nlinks++] = l;
1322 rulesub(struct rule * rp, const char *loyearp, const char *hiyearp,
1323 const char *typep, const char *monthp, const char *dayp,
1326 const struct lookup *lp;
1331 if ((lp = byword(monthp, mon_names)) == NULL)
1333 error(_("invalid month name"));
1336 rp->r_month = lp->l_value;
1337 rp->r_todisstd = FALSE;
1338 rp->r_todisgmt = FALSE;
1339 dp = ecpyalloc(timep);
1342 ep = dp + strlen(dp) - 1;
1343 switch (lowerit(*ep))
1345 case 's': /* Standard */
1346 rp->r_todisstd = TRUE;
1347 rp->r_todisgmt = FALSE;
1350 case 'w': /* Wall */
1351 rp->r_todisstd = FALSE;
1352 rp->r_todisgmt = FALSE;
1355 case 'g': /* Greenwich */
1356 case 'u': /* Universal */
1357 case 'z': /* Zulu */
1358 rp->r_todisstd = TRUE;
1359 rp->r_todisgmt = TRUE;
1364 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1371 lp = byword(cp, begin_years);
1372 rp->r_lowasnum = lp == NULL;
1373 if (!rp->r_lowasnum)
1374 switch ((int) lp->l_value)
1377 rp->r_loyear = INT_MIN;
1380 rp->r_loyear = INT_MAX;
1382 default: /* "cannot happen" */
1383 (void) fprintf(stderr,
1384 _("%s: panic: Invalid l_value %d\n"),
1385 progname, lp->l_value);
1388 else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1)
1390 error(_("invalid starting year"));
1394 lp = byword(cp, end_years);
1395 rp->r_hiwasnum = lp == NULL;
1396 if (!rp->r_hiwasnum)
1397 switch ((int) lp->l_value)
1400 rp->r_hiyear = INT_MIN;
1403 rp->r_hiyear = INT_MAX;
1406 rp->r_hiyear = rp->r_loyear;
1408 default: /* "cannot happen" */
1409 (void) fprintf(stderr,
1410 _("%s: panic: Invalid l_value %d\n"),
1411 progname, lp->l_value);
1414 else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1)
1416 error(_("invalid ending year"));
1419 if (rp->r_loyear > rp->r_hiyear)
1421 error(_("starting year greater than ending year"));
1425 rp->r_yrtype = NULL;
1428 if (rp->r_loyear == rp->r_hiyear)
1430 error(_("typed single year"));
1433 rp->r_yrtype = ecpyalloc(typep);
1437 * Day work. Accept things such as: 1 last-Sunday Sun<=20 Sun>=7
1439 dp = ecpyalloc(dayp);
1440 if ((lp = byword(dp, lasts)) != NULL)
1442 rp->r_dycode = DC_DOWLEQ;
1443 rp->r_wday = lp->l_value;
1444 rp->r_dayofmonth = len_months[1][rp->r_month];
1448 if ((ep = strchr(dp, '<')) != NULL)
1449 rp->r_dycode = DC_DOWLEQ;
1450 else if ((ep = strchr(dp, '>')) != NULL)
1451 rp->r_dycode = DC_DOWGEQ;
1455 rp->r_dycode = DC_DOM;
1457 if (rp->r_dycode != DC_DOM)
1462 error(_("invalid day of month"));
1466 if ((lp = byword(dp, wday_names)) == NULL)
1468 error(_("invalid weekday name"));
1472 rp->r_wday = lp->l_value;
1474 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1475 rp->r_dayofmonth <= 0 ||
1476 (rp->r_dayofmonth > len_months[1][rp->r_month]))
1478 error(_("invalid day of month"));
1487 convert(long val, char *buf)
1492 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1493 buf[i] = val >> shift;
1497 convert64(zic_t val, char *buf)
1502 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1503 buf[i] = val >> shift;
1507 puttzcode(long val, FILE *fp)
1512 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1516 puttzcode64(zic_t val, FILE *fp)
1520 convert64(val, buf);
1521 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1525 atcomp(const void *avp, const void *bvp)
1527 const zic_t a = ((const struct attype *) avp)->at;
1528 const zic_t b = ((const struct attype *) bvp)->at;
1530 return (a < b) ? -1 : (a > b);
1536 return x == ((zic_t) ((int32) x));
1540 writezone(const char *name, const char *string)
1550 static char *fullname;
1551 static const struct tzhead tzh0;
1552 static struct tzhead tzh;
1553 zic_t ats[TZ_MAX_TIMES];
1554 unsigned char types[TZ_MAX_TIMES];
1560 (void) qsort((void *) attypes, (size_t) timecnt,
1561 (size_t) sizeof *attypes, atcomp);
1572 while (fromi < timecnt && attypes[fromi].at < min_time)
1575 while (fromi < timecnt && attypes[fromi].type == 0)
1576 ++fromi; /* handled by default rule */
1577 for (; fromi < timecnt; ++fromi)
1580 && ((attypes[fromi].at
1581 + gmtoffs[attypes[toi - 1].type])
1582 <= (attypes[toi - 1].at
1583 + gmtoffs[toi == 1 ? 0
1584 : attypes[toi - 2].type])))
1586 attypes[toi - 1].type = attypes[fromi].type;
1590 attypes[toi - 1].type != attypes[fromi].type)
1591 attypes[toi++] = attypes[fromi];
1599 for (i = 0; i < timecnt; ++i)
1601 ats[i] = attypes[i].at;
1602 types[i] = attypes[i].type;
1606 * Correct for leap seconds.
1608 for (i = 0; i < timecnt; ++i)
1612 if (ats[i] > trans[j] - corr[j])
1614 ats[i] = tadd(ats[i], corr[j]);
1620 * Figure out 32-bit-limited starts and counts.
1622 timecnt32 = timecnt;
1624 leapcnt32 = leapcnt;
1626 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1628 while (timecnt32 > 0 && !is32(ats[timei32]))
1633 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1635 while (leapcnt32 > 0 && !is32(trans[leapi32]))
1640 fullname = erealloc(fullname,
1641 (int) (strlen(directory) + 1 + strlen(name) + 1));
1642 (void) sprintf(fullname, "%s/%s", directory, name);
1645 * Remove old file, if any, to snap links.
1647 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1649 const char *e = strerror(errno);
1651 (void) fprintf(stderr, _("%s: Cannot remove %s: %s\n"),
1652 progname, fullname, e);
1655 if ((fp = fopen(fullname, "wb")) == NULL)
1657 if (mkdirs(fullname) != 0)
1658 (void) exit(EXIT_FAILURE);
1659 if ((fp = fopen(fullname, "wb")) == NULL)
1661 const char *e = strerror(errno);
1663 (void) fprintf(stderr, _("%s: Cannot create %s: %s\n"),
1664 progname, fullname, e);
1668 for (pass = 1; pass <= 2; ++pass)
1670 register int thistimei,
1672 register int thisleapi,
1674 register int thistimelim,
1676 int writetype[TZ_MAX_TIMES];
1677 int typemap[TZ_MAX_TYPES];
1678 register int thistypecnt;
1679 char thischars[TZ_MAX_CHARS];
1681 int indmap[TZ_MAX_CHARS];
1685 thistimei = timei32;
1686 thistimecnt = timecnt32;
1687 thisleapi = leapi32;
1688 thisleapcnt = leapcnt32;
1693 thistimecnt = timecnt;
1695 thisleapcnt = leapcnt;
1697 thistimelim = thistimei + thistimecnt;
1698 thisleaplim = thisleapi + thisleapcnt;
1699 for (i = 0; i < typecnt; ++i)
1700 writetype[i] = thistimecnt == timecnt;
1701 if (thistimecnt == 0)
1704 * * No transition times fall in the current * (32- or 64-bit)
1708 writetype[typecnt - 1] = TRUE;
1712 for (i = thistimei - 1; i < thistimelim; ++i)
1714 writetype[types[i]] = TRUE;
1717 * * For America/Godthab and Antarctica/Palmer
1720 writetype[0] = TRUE;
1723 for (i = 0; i < typecnt; ++i)
1724 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1725 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1728 for (i = 0; i < typecnt; ++i)
1730 register char *thisabbr;
1734 if (indmap[abbrinds[i]] >= 0)
1736 thisabbr = &chars[abbrinds[i]];
1737 for (j = 0; j < thischarcnt; ++j)
1738 if (strcmp(&thischars[j], thisabbr) == 0)
1740 if (j == thischarcnt)
1742 (void) strcpy(&thischars[(int) thischarcnt],
1744 thischarcnt += strlen(thisabbr) + 1;
1746 indmap[abbrinds[i]] = j;
1748 #define DO(field) (void) fwrite((void *) tzh.field, \
1749 (size_t) sizeof tzh.field, (size_t) 1, fp)
1751 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1752 tzh.tzh_version[0] = ZIC_VERSION;
1753 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1754 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1755 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1756 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1757 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1758 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1769 for (i = thistimei; i < thistimelim; ++i)
1771 puttzcode((long) ats[i], fp);
1773 puttzcode64(ats[i], fp);
1774 for (i = thistimei; i < thistimelim; ++i)
1778 uc = typemap[types[i]];
1779 (void) fwrite((void *) &uc,
1784 for (i = 0; i < typecnt; ++i)
1787 puttzcode(gmtoffs[i], fp);
1788 (void) putc(isdsts[i], fp);
1789 (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1791 /* Print current timezone abbreviations if requested */
1792 if (print_abbrevs && pass == 2 &&
1793 (ats[i] >= print_cutoff || i == typecnt - 1))
1795 char *thisabbrev = &thischars[indmap[abbrinds[i]]];
1797 /* filter out assorted junk entries */
1798 if (strcmp(thisabbrev, GRANDPARENTED) != 0 &&
1799 strcmp(thisabbrev, "zzz") != 0)
1800 fprintf(stdout, "%s\t%ld%s\n",
1803 isdsts[i] ? "\tD" : "");
1806 if (thischarcnt != 0)
1807 (void) fwrite((void *) thischars,
1808 (size_t) sizeof thischars[0],
1809 (size_t) thischarcnt, fp);
1810 for (i = thisleapi; i < thisleaplim; ++i)
1812 register zic_t todo;
1816 if (timecnt == 0 || trans[i] < ats[0])
1829 while (j < timecnt &&
1834 todo = tadd(trans[i], -gmtoffs[j]);
1839 puttzcode((long) todo, fp);
1841 puttzcode64(todo, fp);
1842 puttzcode(corr[i], fp);
1844 for (i = 0; i < typecnt; ++i)
1846 (void) putc(ttisstds[i], fp);
1847 for (i = 0; i < typecnt; ++i)
1849 (void) putc(ttisgmts[i], fp);
1851 (void) fprintf(fp, "\n%s\n", string);
1852 if (ferror(fp) || fclose(fp))
1854 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1855 progname, fullname);
1861 doabbr(char *abbr, const char *format, const char *letters, int isdst,
1868 slashp = strchr(format, '/');
1871 if (letters == NULL)
1872 (void) strcpy(abbr, format);
1874 (void) sprintf(abbr, format, letters);
1877 (void) strcpy(abbr, slashp + 1);
1880 if (slashp > format)
1881 (void) strncpy(abbr, format,
1882 (unsigned) (slashp - format));
1883 abbr[slashp - format] = '\0';
1887 for (cp = abbr; *cp != '\0'; ++cp)
1888 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1889 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1892 if (len > 0 && *cp == '\0')
1894 abbr[len + 2] = '\0';
1895 abbr[len + 1] = '>';
1896 for (; len > 0; --len)
1897 abbr[len] = abbr[len - 1];
1911 stringoffset(char *result, long offset)
1920 (void) strcpy(result, "-");
1923 seconds = offset % SECSPERMIN;
1924 offset /= SECSPERMIN;
1925 minutes = offset % MINSPERHOUR;
1926 offset /= MINSPERHOUR;
1928 if (hours >= HOURSPERDAY)
1933 (void) sprintf(end(result), "%d", hours);
1934 if (minutes != 0 || seconds != 0)
1936 (void) sprintf(end(result), ":%02d", minutes);
1938 (void) sprintf(end(result), ":%02d", seconds);
1944 stringrule(char *result, const struct rule * rp, long dstoff, long gmtoff)
1948 result = end(result);
1949 if (rp->r_dycode == DC_DOM)
1954 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1957 for (month = 0; month < rp->r_month; ++month)
1958 total += len_months[0][month];
1959 (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1965 if (rp->r_dycode == DC_DOWGEQ)
1967 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1968 if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1971 else if (rp->r_dycode == DC_DOWLEQ)
1973 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1977 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1978 if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1983 return -1; /* "cannot happen" */
1984 (void) sprintf(result, "M%d.%d.%d",
1985 rp->r_month + 1, week, rp->r_wday);
1990 if (rp->r_todisstd && rp->r_stdoff == 0)
1997 if (tod != 2 * SECSPERMIN * MINSPERHOUR)
1999 (void) strcat(result, "/");
2000 if (stringoffset(end(result), tod) != 0)
2007 stringzone(char *result, const struct zone * zpfirst, int zonecount)
2009 const struct zone *zp;
2014 const char *abbrvar;
2017 zp = zpfirst + zonecount - 1;
2018 stdrp = dstrp = NULL;
2019 for (i = 0; i < zp->z_nrules; ++i)
2021 rp = &zp->z_rules[i];
2022 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
2024 if (rp->r_yrtype != NULL)
2026 if (rp->r_stdoff == 0)
2041 if (stdrp == NULL && dstrp == NULL)
2044 * There are no rules running through "max". Let's find the latest
2047 for (i = 0; i < zp->z_nrules; ++i)
2049 rp = &zp->z_rules[i];
2050 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
2051 (rp->r_hiyear == stdrp->r_hiyear &&
2052 rp->r_month > stdrp->r_month))
2055 if (stdrp != NULL && stdrp->r_stdoff != 0)
2056 return; /* We end up in DST (a POSIX no-no). */
2059 * Horrid special case: if year is 2037, presume this is a zone
2060 * handled on a year-by-year basis; do not try to apply a rule to the
2063 if (stdrp != NULL && stdrp->r_hiyear == 2037)
2066 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
2068 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
2069 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
2070 if (stringoffset(end(result), -zp->z_gmtoff) != 0)
2077 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
2078 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
2079 if (stringoffset(end(result),
2080 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0)
2085 (void) strcat(result, ",");
2086 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0)
2091 (void) strcat(result, ",");
2092 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0)
2100 outzone(const struct zone * zpfirst, int zonecount)
2102 const struct zone *zp;
2108 zic_t starttime = 0;
2109 zic_t untiltime = 0;
2123 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2124 max_envvar_len = 2 * max_abbr_len + 5 * 9;
2125 startbuf = emalloc(max_abbr_len + 1);
2126 ab = emalloc(max_abbr_len + 1);
2127 envvar = emalloc(max_envvar_len + 1);
2130 * Now. . .finally. . .generate some useful data!
2137 * Thanks to Earl Chew for noting the need to unconditionally initialize
2140 startttisstd = FALSE;
2141 startttisgmt = FALSE;
2142 min_year = max_year = EPOCH_YEAR;
2145 updateminmax(leapminyear);
2146 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
2148 for (i = 0; i < zonecount; ++i)
2151 if (i < zonecount - 1)
2152 updateminmax(zp->z_untilrule.r_loyear);
2153 for (j = 0; j < zp->z_nrules; ++j)
2155 rp = &zp->z_rules[j];
2157 updateminmax(rp->r_loyear);
2159 updateminmax(rp->r_hiyear);
2164 * Generate lots of data if a rule can't cover all future times.
2166 stringzone(envvar, zpfirst, zonecount);
2167 if (noise && envvar[0] == '\0')
2171 wp = ecpyalloc(_("no POSIX environment variable for zone"));
2172 wp = ecatalloc(wp, " ");
2173 wp = ecatalloc(wp, zpfirst->z_name);
2177 if (envvar[0] == '\0')
2179 if (min_year >= INT_MIN + YEARSPERREPEAT)
2180 min_year -= YEARSPERREPEAT;
2183 if (max_year <= INT_MAX - YEARSPERREPEAT)
2184 max_year += YEARSPERREPEAT;
2190 * For the benefit of older systems, generate data from 1900 through 2037.
2192 if (min_year > 1900)
2194 if (max_year < 2037)
2196 for (i = 0; i < zonecount; ++i)
2199 * A guess that may well be corrected later.
2203 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2204 useuntil = i < (zonecount - 1);
2205 if (useuntil && zp->z_untiltime <= min_time)
2207 gmtoff = zp->z_gmtoff;
2208 eat(zp->z_filename, zp->z_linenum);
2210 startoff = zp->z_gmtoff;
2211 if (zp->z_nrules == 0)
2213 stdoff = zp->z_stdoff;
2214 doabbr(startbuf, zp->z_format,
2215 (char *) NULL, stdoff != 0, FALSE);
2216 type = addtype(oadd(zp->z_gmtoff, stdoff),
2217 startbuf, stdoff != 0, startttisstd,
2221 addtt(starttime, type);
2224 else if (stdoff != 0)
2225 addtt(min_time, type);
2228 for (year = min_year; year <= max_year; ++year)
2230 if (useuntil && year > zp->z_untilrule.r_hiyear)
2234 * Mark which rules to do in the current year. For those to
2235 * do, calculate rpytime(rp, year);
2237 for (j = 0; j < zp->z_nrules; ++j)
2239 rp = &zp->z_rules[j];
2240 eats(zp->z_filename, zp->z_linenum,
2241 rp->r_filename, rp->r_linenum);
2242 rp->r_todo = year >= rp->r_loyear &&
2243 year <= rp->r_hiyear &&
2244 yearistype(year, rp->r_yrtype);
2246 rp->r_temp = rpytime(rp, year);
2258 * Turn untiltime into UTC assuming the current gmtoff
2259 * and stdoff values.
2261 untiltime = zp->z_untiltime;
2262 if (!zp->z_untilrule.r_todisgmt)
2263 untiltime = tadd(untiltime,
2265 if (!zp->z_untilrule.r_todisstd)
2266 untiltime = tadd(untiltime,
2271 * Find the rule (of those to do, if any) that takes
2272 * effect earliest in the year.
2275 for (j = 0; j < zp->z_nrules; ++j)
2277 rp = &zp->z_rules[j];
2280 eats(zp->z_filename, zp->z_linenum,
2281 rp->r_filename, rp->r_linenum);
2282 offset = rp->r_todisgmt ? 0 : gmtoff;
2283 if (!rp->r_todisstd)
2284 offset = oadd(offset, stdoff);
2286 if (jtime == min_time ||
2289 jtime = tadd(jtime, -offset);
2290 if (k < 0 || jtime < ktime)
2297 break; /* go on to next year */
2298 rp = &zp->z_rules[k];
2300 if (useuntil && ktime >= untiltime)
2302 stdoff = rp->r_stdoff;
2303 if (usestart && ktime == starttime)
2307 if (ktime < starttime)
2309 startoff = oadd(zp->z_gmtoff,
2311 doabbr(startbuf, zp->z_format,
2317 if (*startbuf == '\0' &&
2318 startoff == oadd(zp->z_gmtoff, stdoff))
2328 eats(zp->z_filename, zp->z_linenum,
2329 rp->r_filename, rp->r_linenum);
2330 doabbr(ab, zp->z_format, rp->r_abbrvar,
2331 rp->r_stdoff != 0, FALSE);
2332 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2333 type = addtype(offset, ab, rp->r_stdoff != 0,
2334 rp->r_todisstd, rp->r_todisgmt);
2340 if (*startbuf == '\0' &&
2341 zp->z_format != NULL &&
2342 strchr(zp->z_format, '%') == NULL &&
2343 strchr(zp->z_format, '/') == NULL)
2344 (void) strcpy(startbuf, zp->z_format);
2345 eat(zp->z_filename, zp->z_linenum);
2346 if (*startbuf == '\0')
2347 error(_("cannot determine time zone abbreviation to use just after until time"));
2350 addtype(startoff, startbuf,
2351 startoff != zp->z_gmtoff,
2357 * Now we may get to set starttime for the next zone line.
2361 startttisstd = zp->z_untilrule.r_todisstd;
2362 startttisgmt = zp->z_untilrule.r_todisgmt;
2363 starttime = zp->z_untiltime;
2365 starttime = tadd(starttime, -stdoff);
2367 starttime = tadd(starttime, -gmtoff);
2370 writezone(zpfirst->z_name, envvar);
2377 addtt(const zic_t starttime, int type)
2379 if (starttime <= min_time ||
2380 (timecnt == 1 && attypes[0].at < min_time))
2382 gmtoffs[0] = gmtoffs[type];
2383 isdsts[0] = isdsts[type];
2384 ttisstds[0] = ttisstds[type];
2385 ttisgmts[0] = ttisgmts[type];
2386 if (abbrinds[type] != 0)
2387 (void) strcpy(chars, &chars[abbrinds[type]]);
2389 charcnt = strlen(chars) + 1;
2394 if (timecnt >= TZ_MAX_TIMES)
2396 error(_("too many transitions?!"));
2399 attypes[timecnt].at = starttime;
2400 attypes[timecnt].type = type;
2405 addtype(long gmtoff, const char *abbr, int isdst,
2406 int ttisstd, int ttisgmt)
2411 if (isdst != TRUE && isdst != FALSE)
2413 error(_("internal error - addtype called with bad isdst"));
2416 if (ttisstd != TRUE && ttisstd != FALSE)
2418 error(_("internal error - addtype called with bad ttisstd"));
2421 if (ttisgmt != TRUE && ttisgmt != FALSE)
2423 error(_("internal error - addtype called with bad ttisgmt"));
2428 * See if there's already an entry for this zone type. If so, just return
2431 for (i = 0; i < typecnt; ++i)
2433 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2434 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2435 ttisstd == ttisstds[i] &&
2436 ttisgmt == ttisgmts[i])
2441 * There isn't one; add a new one, unless there are already too many.
2443 if (typecnt >= TZ_MAX_TYPES)
2445 error(_("too many local time types"));
2448 if (!(-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L))
2450 error(_("UTC offset out of range"));
2453 gmtoffs[i] = gmtoff;
2455 ttisstds[i] = ttisstd;
2456 ttisgmts[i] = ttisgmt;
2458 for (j = 0; j < charcnt; ++j)
2459 if (strcmp(&chars[j], abbr) == 0)
2469 leapadd(const zic_t t, int positive, int rolling, int count)
2474 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS)
2476 error(_("too many leap seconds"));
2479 for (i = 0; i < leapcnt; ++i)
2484 error(_("repeated leap second moment"));
2491 for (j = leapcnt; j > i; --j)
2493 trans[j] = trans[j - 1];
2494 corr[j] = corr[j - 1];
2495 roll[j] = roll[j - 1];
2498 corr[i] = positive ? 1L : eitol(-count);
2501 } while (positive && --count != 0);
2511 * propagate leap seconds forward
2513 for (i = 0; i < leapcnt; ++i)
2515 trans[i] = tadd(trans[i], last);
2516 last = corr[i] += last;
2521 yearistype(int year, const char *type)
2526 if (type == NULL || *type == '\0')
2528 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2529 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
2530 result = system(buf);
2531 if (WIFEXITED(result))
2532 switch (WEXITSTATUS(result))
2539 error(_("Wild result from command execution"));
2540 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2541 progname, buf, result);
2549 a = (unsigned char) a;
2550 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2554 ciequal(const char *ap, const char *bp)
2556 while (lowerit(*ap) == lowerit(*bp++))
2563 itsabbr(const char *abbr, const char *word)
2565 if (lowerit(*abbr) != lowerit(*word))
2568 while (*++abbr != '\0')
2573 } while (lowerit(*word++) != lowerit(*abbr));
2577 static const struct lookup *
2578 byword(const char *word, const struct lookup * table)
2580 const struct lookup *foundlp;
2581 const struct lookup *lp;
2583 if (word == NULL || table == NULL)
2587 * Look for exact match.
2589 for (lp = table; lp->l_word != NULL; ++lp)
2590 if (ciequal(word, lp->l_word))
2594 * Look for inexact match.
2597 for (lp = table; lp->l_word != NULL; ++lp)
2598 if (itsabbr(word, lp->l_word))
2600 if (foundlp == NULL)
2603 return NULL; /* multiple inexact matches */
2617 array = (char **) (void *)
2618 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2622 while (isascii((unsigned char) *cp) &&
2623 isspace((unsigned char) *cp))
2625 if (*cp == '\0' || *cp == '#')
2627 array[nsubs++] = dp = cp;
2630 if ((*dp = *cp++) != '"')
2633 while ((*dp = *cp++) != '"')
2638 error(_("Odd number of quotation marks"));
2641 } while (*cp != '\0' && *cp != '#' &&
2642 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2643 if (isascii(*cp) && isspace((unsigned char) *cp))
2647 array[nsubs] = NULL;
2652 oadd(long t1, long t2)
2657 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
2659 error(_("time overflow"));
2666 tadd(const zic_t t1, long t2)
2670 if (t1 == max_time && t2 > 0)
2672 if (t1 == min_time && t2 < 0)
2675 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
2677 error(_("time overflow"));
2684 * Given a rule, and a year, compute the date - in seconds since January 1,
2685 * 1970, 00:00 LOCAL time - in that year that the rule refers to.
2689 rpytime(const struct rule * rp, int wantedy)
2694 long dayoff; /* with a nod to Margaret O. */
2697 if (wantedy == INT_MIN)
2699 if (wantedy == INT_MAX)
2704 while (wantedy != y)
2708 i = len_years[isleap(y)];
2714 i = -len_years[isleap(y)];
2716 dayoff = oadd(dayoff, eitol(i));
2718 while (m != rp->r_month)
2720 i = len_months[isleap(y)][m];
2721 dayoff = oadd(dayoff, eitol(i));
2724 i = rp->r_dayofmonth;
2725 if (m == TM_FEBRUARY && i == 29 && !isleap(y))
2727 if (rp->r_dycode == DC_DOWLEQ)
2731 error(_("use of 2/29 in non leap-year"));
2736 dayoff = oadd(dayoff, eitol(i));
2737 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
2741 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2742 wday = eitol(EPOCH_WDAY);
2745 * Don't trust mod of negative numbers.
2748 wday = (wday + dayoff) % LDAYSPERWEEK;
2751 wday -= ((-dayoff) % LDAYSPERWEEK);
2753 wday += LDAYSPERWEEK;
2755 while (wday != eitol(rp->r_wday))
2756 if (rp->r_dycode == DC_DOWGEQ)
2758 dayoff = oadd(dayoff, (long) 1);
2759 if (++wday >= LDAYSPERWEEK)
2765 dayoff = oadd(dayoff, (long) -1);
2767 wday = LDAYSPERWEEK - 1;
2770 if (i < 0 || i >= len_months[isleap(y)][m])
2773 warning(_("rule goes past start/end of month--\
2774 will not work with pre-2004 versions of zic"));
2777 if (dayoff < min_time / SECSPERDAY)
2779 if (dayoff > max_time / SECSPERDAY)
2781 t = (zic_t) dayoff *SECSPERDAY;
2783 return tadd(t, rp->r_tod);
2787 newabbr(const char *string)
2791 if (strcmp(string, GRANDPARENTED) != 0)
2797 * Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics optionally
2798 * followed by a + or - and a number from 1 to 14.
2802 while (isascii((unsigned char) *cp) &&
2803 isalpha((unsigned char) *cp))
2805 if (cp - string == 0)
2806 wp = _("time zone abbreviation lacks alphabetic at start");
2807 if (noise && cp - string > 3)
2808 wp = _("time zone abbreviation has more than 3 alphabetics");
2809 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2810 wp = _("time zone abbreviation has too many alphabetics");
2811 if (wp == NULL && (*cp == '+' || *cp == '-'))
2814 if (isascii((unsigned char) *cp) &&
2815 isdigit((unsigned char) *cp))
2817 *cp >= '0' && *cp <= '4')
2821 wp = _("time zone abbreviation differs from POSIX standard");
2825 wp = ecatalloc(wp, " (");
2826 wp = ecatalloc(wp, string);
2827 wp = ecatalloc(wp, ")");
2832 i = strlen(string) + 1;
2833 if (charcnt + i > TZ_MAX_CHARS)
2835 error(_("too many, or too long, time zone abbreviations"));
2838 (void) strcpy(&chars[charcnt], string);
2839 charcnt += eitol(i);
2843 mkdirs(char *argname)
2848 if (argname == NULL || *argname == '\0')
2850 cp = name = ecpyalloc(argname);
2851 while ((cp = strchr(cp + 1, '/')) != NULL)
2857 * DOS drive specifier?
2859 if (isalpha((unsigned char) name[0]) &&
2860 name[1] == ':' && name[2] == '\0')
2869 * It doesn't seem to exist, so we try to create it. Creation may
2870 * fail because of the directory being created by some other
2871 * multiprocessor, so we get to do extra checking.
2873 if (mkdir(name, MKDIR_UMASK) != 0)
2875 const char *e = strerror(errno);
2877 if (errno != EEXIST || !itsdir(name))
2879 (void) fprintf(stderr,
2880 _("%s: Cannot create directory %s: %s\n"),
2899 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2901 (void) fprintf(stderr,
2902 _("%s: %d did not sign extend correctly\n"),
2910 * UNIX was a registered trademark of The Open Group in 2003.
2919 link(const char *oldpath, const char *newpath)
2921 if (!CopyFile(oldpath, newpath, FALSE))
2928 * This allows zic to compile by just returning a dummy value.
2929 * localtime.c references it, but no one uses it from zic.
2932 pg_open_tzfile(const char *name, char *canonname)