2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
5 ** $FreeBSD: src/usr.sbin/zic/zic.c,v 1.11 1999/08/28 01:21:20 peter Exp $
10 #include <sys/stat.h> /* for umask manifest constants */
11 #include <sys/types.h>
18 #define ZIC_VERSION_PRE_2013 '2'
19 #define ZIC_VERSION '3'
21 typedef int_fast64_t zic_t;
22 #define ZIC_MIN INT_FAST64_MIN
23 #define ZIC_MAX INT_FAST64_MAX
24 #define SCNdZIC SCNdFAST64
26 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
27 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
28 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
30 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
33 ** On some ancient hosts, predicates like `isspace(C)' are defined
34 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
35 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
36 ** Neither the C Standard nor Posix require that `isascii' exist.
37 ** For portability, we check both ancient and modern requirements.
38 ** If isascii is not defined, the isascii check succeeds trivially.
45 #define end(cp) (strchr((cp), '\0'))
48 const char * r_filename;
52 zic_t r_loyear; /* for example, 1986 */
53 zic_t r_hiyear; /* for example, 1986 */
54 const char * r_yrtype;
58 int r_month; /* 0..11 */
60 int r_dycode; /* see below */
64 zic_t 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 zic_t 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 static void addtt(zic_t starttime, int type);
103 static int addtype(zic_t gmtoff, const char *abbr, int isdst,
104 int ttisstd, int ttisgmt);
105 static void leapadd(zic_t t, int positive, int rolling, int count);
106 static void adjleap(void);
107 static void associate(void);
108 static void dolink(const char *fromfield, const char *tofield);
109 static char ** getfields(char *buf);
110 static zic_t gethms(const char *string, const char *errstrng,
112 static void infile(const char *filename);
113 static void inleap(char **fields, int nfields);
114 static void inlink(char **fields, int nfields);
115 static void inrule(char **fields, int nfields);
116 static int inzcont(char **fields, int nfields);
117 static int inzone(char **fields, int nfields);
118 static int inzsub(char **fields, int nfields, int iscont);
119 static int itsdir(const char *name);
120 static int lowerit(int c);
121 static int mkdirs(char *filename);
122 static void newabbr(const char *abbr);
123 static zic_t oadd(zic_t t1, zic_t t2);
124 static void outzone(const struct zone *zp, int ntzones);
125 static zic_t rpytime(const struct rule *rp, zic_t wantedy);
126 static void rulesub(struct rule *rp,
127 const char *loyearp, const char *hiyearp,
128 const char *typep, const char *monthp,
129 const char *dayp, const char *timep);
130 static void setgroup(gid_t *flag, const char *name);
131 static void setuser(uid_t *flag, const char *name);
132 static zic_t tadd(const zic_t t1, const zic_t t2);
133 static int yearistype(int year, const char *type);
137 static const char * filename;
140 static zic_t leapminyear;
141 static zic_t leapmaxyear;
143 static int max_abbrvar_len;
144 static int max_format_len;
145 static zic_t max_year;
146 static zic_t min_year;
148 static const char * rfilename;
163 ** Which fields are which on a Zone line.
171 #define ZF_TILMONTH 6
174 #define ZONE_MINFIELDS 5
175 #define ZONE_MAXFIELDS 9
178 ** Which fields are which on a Zone continuation line.
184 #define ZFC_TILYEAR 3
185 #define ZFC_TILMONTH 4
187 #define ZFC_TILTIME 6
188 #define ZONEC_MINFIELDS 3
189 #define ZONEC_MAXFIELDS 7
192 ** Which files are which on a Rule line.
204 #define RULE_FIELDS 10
207 ** Which fields are which on a Link line.
212 #define LINK_FIELDS 3
215 ** Which fields are which on a Leap line.
224 #define LEAP_FIELDS 7
234 static struct rule * rules;
235 static int nrules; /* number of rules */
237 static struct zone * zones;
238 static int nzones; /* number of zones */
241 const char * l_filename;
247 static struct link * links;
255 static struct lookup const *byword(const char *string,
256 const struct lookup *lp);
258 static struct lookup const line_codes[] = {
266 static struct lookup const mon_names[] = {
267 { "January", TM_JANUARY },
268 { "February", TM_FEBRUARY },
269 { "March", TM_MARCH },
270 { "April", TM_APRIL },
274 { "August", TM_AUGUST },
275 { "September", TM_SEPTEMBER },
276 { "October", TM_OCTOBER },
277 { "November", TM_NOVEMBER },
278 { "December", TM_DECEMBER },
282 static struct lookup const wday_names[] = {
283 { "Sunday", TM_SUNDAY },
284 { "Monday", TM_MONDAY },
285 { "Tuesday", TM_TUESDAY },
286 { "Wednesday", TM_WEDNESDAY },
287 { "Thursday", TM_THURSDAY },
288 { "Friday", TM_FRIDAY },
289 { "Saturday", TM_SATURDAY },
293 static struct lookup const lasts[] = {
294 { "last-Sunday", TM_SUNDAY },
295 { "last-Monday", TM_MONDAY },
296 { "last-Tuesday", TM_TUESDAY },
297 { "last-Wednesday", TM_WEDNESDAY },
298 { "last-Thursday", TM_THURSDAY },
299 { "last-Friday", TM_FRIDAY },
300 { "last-Saturday", TM_SATURDAY },
304 static struct lookup const begin_years[] = {
305 { "minimum", YR_MINIMUM },
306 { "maximum", YR_MAXIMUM },
310 static struct lookup const end_years[] = {
311 { "minimum", YR_MINIMUM },
312 { "maximum", YR_MAXIMUM },
317 static struct lookup const leap_types[] = {
319 { "Stationary", FALSE },
323 static const int len_months[2][MONSPERYEAR] = {
324 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
325 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
328 static const int len_years[2] = {
329 DAYSPERNYEAR, DAYSPERLYEAR
332 static struct attype {
335 } attypes[TZ_MAX_TIMES];
336 static zic_t gmtoffs[TZ_MAX_TYPES];
337 static char isdsts[TZ_MAX_TYPES];
338 static unsigned char abbrinds[TZ_MAX_TYPES];
339 static char ttisstds[TZ_MAX_TYPES];
340 static char ttisgmts[TZ_MAX_TYPES];
341 static char chars[TZ_MAX_CHARS];
342 static zic_t trans[TZ_MAX_LEAPS];
343 static zic_t corr[TZ_MAX_LEAPS];
344 static char roll[TZ_MAX_LEAPS];
347 ** Memory allocation.
351 memcheck(void* const ptr)
354 errx(EXIT_FAILURE, _("memory exhausted"));
358 #define emalloc(size) memcheck(malloc(size))
359 #define erealloc(ptr, size) memcheck(realloc(ptr, size))
360 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
361 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
368 eats(const char * const name, const int num,
369 const char * const rname, const int rnum)
378 eat(const char * const name, const int num)
380 eats(name, num, NULL, -1);
383 static void __printflike(1, 0)
384 verror(const char * const string, va_list args)
387 ** Match the format of "cc" to allow sh users to
388 ** zic ... 2>&1 | error -t "*" -v
391 fprintf(stderr, _("\"%s\", line %d: "), filename, linenum);
392 vfprintf(stderr, string, args);
393 if (rfilename != NULL)
394 fprintf(stderr, _(" (rule from \"%s\", line %d)"),
395 rfilename, rlinenum);
396 fprintf(stderr, "\n");
400 static void __printflike(1, 2)
401 error(const char * const string, ...)
404 va_start(args, string);
405 verror(string, args);
409 static void __printflike(1, 2)
410 warning(const char * const string, ...)
413 fprintf(stderr, _("warning: "));
414 va_start(args, string);
415 verror(string, args);
420 static _Noreturn void
423 fprintf(stderr, "%s\n%s\n",
424 _("usage: zic [-v] [-l localtime] [-p posixrules] [-d directory]"),
425 _(" [-L leapseconds] [-y yearistype] [filename ...]"));
429 static const char * psxrules;
430 static const char * lcltime;
431 static const char * directory;
432 static const char * leapsec;
433 static const char * yitcommand;
435 static uid_t uflag = (uid_t)-1;
436 static gid_t gflag = (gid_t)-1;
437 static mode_t mflag = (S_IRUSR | S_IRGRP | S_IROTH
441 main(int argc, char *argv[])
447 umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
448 while ((c = getopt(argc, argv, "Dd:g:l:m:p:L:u:vsy:")) != -1)
456 if (directory == NULL)
460 _("more than one -d option specified"));
463 setgroup(&gflag, optarg);
470 _("more than one -l option specified"));
474 void *set = setmode(optarg);
479 if (psxrules == NULL)
483 _("more than one -p option specified"));
486 setuser(&uflag, optarg);
489 if (yitcommand == NULL)
493 _("more than one -y option specified"));
500 _("more than one -L option specified"));
506 warnx(_("-s ignored\n"));
509 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
510 usage(); /* usage message by request */
511 if (directory == NULL)
513 if (yitcommand == NULL)
514 yitcommand = "yearistype";
516 if (optind < argc && leapsec != NULL) {
521 for (i = optind; i < argc; ++i)
526 for (i = 0; i < nzones; i = j) {
528 ** Find the next non-continuation zone entry.
530 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
532 outzone(&zones[i], j - i);
537 for (i = 0; i < nlinks; ++i) {
538 eat(links[i].l_filename, links[i].l_linenum);
539 dolink(links[i].l_from, links[i].l_to);
541 for (j = 0; j < nlinks; ++j)
542 if (strcmp(links[i].l_to,
543 links[j].l_from) == 0)
544 warning(_("link to link"));
546 if (lcltime != NULL) {
547 eat("command line", 1);
548 dolink(lcltime, TZDEFAULT);
550 if (psxrules != NULL) {
551 eat("command line", 1);
552 dolink(psxrules, TZDEFRULES);
554 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
558 dolink(const char * const fromfield, const char * const tofield)
563 if (fromfield[0] == '/')
564 fromname = ecpyalloc(fromfield);
566 fromname = ecpyalloc(directory);
567 fromname = ecatalloc(fromname, "/");
568 fromname = ecatalloc(fromname, fromfield);
570 if (tofield[0] == '/')
571 toname = ecpyalloc(tofield);
573 toname = ecpyalloc(directory);
574 toname = ecatalloc(toname, "/");
575 toname = ecatalloc(toname, tofield);
578 ** We get to be careful here since
579 ** there's a fair chance of root running us.
583 if (link(fromname, toname) != 0
584 && access(fromname, F_OK) == 0 && !itsdir(fromname)) {
587 if (mkdirs(toname) != 0)
590 result = link(fromname, toname);
592 const char *s = fromfield;
594 char * symlinkcontents = NULL;
598 while ((s = strchr(s, '/'))
599 && ! strncmp (fromfield, tofield,
602 for (s = tofield + (t - fromfield);
603 (s = strchr(s, '/'));
606 ecatalloc(symlinkcontents,
608 symlinkcontents = ecatalloc(symlinkcontents, t);
609 result = symlink(symlinkcontents, toname);
611 warning(_("hard link failed, symbolic link used"));
612 free(symlinkcontents);
617 fp = fopen(fromname, "rb");
619 err(EXIT_FAILURE, _("Can't read %s"), fromname);
620 tp = fopen(toname, "wb");
622 err(EXIT_FAILURE, _("Can't create %s"), toname);
623 while ((c = getc(fp)) != EOF)
625 if (ferror(fp) || fclose(fp))
626 err(EXIT_FAILURE, _("Error reading %s"),
628 if (ferror(tp) || fclose(tp))
629 err(EXIT_FAILURE, _("Error writing %s"),
631 warning(_("link failed, copy used"));
638 #define TIME_T_BITS_IN_FILE 64
640 static const zic_t min_time = (zic_t) -1 << (TIME_T_BITS_IN_FILE - 1);
641 static const zic_t max_time = -1 - ((zic_t) -1 << (TIME_T_BITS_IN_FILE - 1));
644 itsdir(const char * const name)
649 myname = ecpyalloc(name);
650 myname = ecatalloc(myname, "/.");
651 accres = access(myname, F_OK);
657 ** Associate sets of rules with zones.
661 ** Sort by rule name.
665 rcomp(const void *cp1, const void *cp2)
667 return strcmp(((const struct rule *) cp1)->r_name,
668 ((const struct rule *) cp2)->r_name);
680 qsort(rules, nrules,sizeof *rules, rcomp);
681 for (i = 0; i < nrules - 1; ++i) {
682 if (strcmp(rules[i].r_name,
683 rules[i + 1].r_name) != 0)
685 if (strcmp(rules[i].r_filename,
686 rules[i + 1].r_filename) == 0)
688 eat(rules[i].r_filename, rules[i].r_linenum);
689 warning(_("same rule name in multiple files"));
690 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
691 warning(_("same rule name in multiple files"));
692 for (j = i + 2; j < nrules; ++j) {
693 if (strcmp(rules[i].r_name,
694 rules[j].r_name) != 0)
696 if (strcmp(rules[i].r_filename,
697 rules[j].r_filename) == 0)
699 if (strcmp(rules[i + 1].r_filename,
700 rules[j].r_filename) == 0)
707 for (i = 0; i < nzones; ++i) {
712 for (base = 0; base < nrules; base = out) {
714 for (out = base + 1; out < nrules; ++out)
715 if (strcmp(rp->r_name, rules[out].r_name) != 0)
717 for (i = 0; i < nzones; ++i) {
719 if (strcmp(zp->z_rule, rp->r_name) != 0)
722 zp->z_nrules = out - base;
725 for (i = 0; i < nzones; ++i) {
727 if (zp->z_nrules == 0) {
729 ** Maybe we have a local standard time offset.
731 eat(zp->z_filename, zp->z_linenum);
732 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
735 ** Note, though, that if there's no rule,
736 ** a '%s' in the format is a bad thing.
738 if (strchr(zp->z_format, '%') != 0)
739 error("%s", _("%s in ruleless zone"));
747 infile(const char *name)
752 const struct lookup *lp;
758 if (strcmp(name, "-") == 0) {
759 name = _("standard input");
761 } else if ((fp = fopen(name, "r")) == NULL)
762 err(EXIT_FAILURE, _("can't open %s"), name);
764 for (num = 1; ; ++num) {
766 if (fgets(buf, sizeof buf, fp) != buf)
768 cp = strchr(buf, '\n');
770 error(_("line too long"));
774 fields = getfields(buf);
776 while (fields[nfields] != NULL) {
779 if (strcmp(fields[nfields], "-") == 0)
780 fields[nfields] = &nada;
785 } else if (wantcont) {
786 wantcont = inzcont(fields, nfields);
788 lp = byword(fields[0], line_codes);
790 error(_("input line of unknown type"));
791 else switch ((int) (lp->l_value)) {
793 inrule(fields, nfields);
797 wantcont = inzone(fields, nfields);
800 inlink(fields, nfields);
806 _("leap line in non leap seconds file %s"), name);
807 else inleap(fields, nfields);
810 default: /* "cannot happen" */
812 _("panic: invalid l_value %d"), lp->l_value);
818 errx(EXIT_FAILURE, _("error reading %s"), filename);
819 if (fp != stdin && fclose(fp))
820 err(EXIT_FAILURE, _("error closing %s"), filename);
822 error(_("expected continuation line not found"));
826 ** Convert a string of one of the forms
827 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
828 ** into a number of seconds.
829 ** A null string maps to zero.
830 ** Call error with errstring and return zero on errors.
834 gethms(const char *string, const char * const errstring, const int signable)
839 if (string == NULL || *string == '\0')
843 else if (*string == '-') {
847 if (sscanf(string, scheck(string, "%"SCNdZIC), &hh) == 1)
849 else if (sscanf(string, scheck(string, "%"SCNdZIC":%d"), &hh, &mm) == 2)
851 else if (sscanf(string, scheck(string, "%"SCNdZIC":%d:%d"),
852 &hh, &mm, &ss) != 3) {
853 error("%s", errstring);
857 mm < 0 || mm >= MINSPERHOUR ||
858 ss < 0 || ss > SECSPERMIN) {
859 error("%s", errstring);
862 if (ZIC_MAX / SECSPERHOUR < hh) {
863 error(_("time overflow"));
866 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
867 warning(_("24:00 not handled by pre-1998 versions of zic"));
868 if (noise && (hh > HOURSPERDAY ||
869 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
870 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
871 return oadd(sign * hh * SECSPERHOUR,
872 sign * (mm * SECSPERMIN + ss));
876 inrule(char ** const fields, const int nfields)
878 static struct rule r;
880 if (nfields != RULE_FIELDS) {
881 error(_("wrong number of fields on Rule line"));
884 if (*fields[RF_NAME] == '\0') {
885 error(_("nameless rule"));
888 r.r_filename = filename;
889 r.r_linenum = linenum;
890 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
891 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
892 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
893 r.r_name = ecpyalloc(fields[RF_NAME]);
894 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
895 if (max_abbrvar_len < strlen(r.r_abbrvar))
896 max_abbrvar_len = strlen(r.r_abbrvar);
897 rules = erealloc(rules, (nrules + 1) * sizeof *rules);
902 inzone(char ** const fields, const int nfields)
906 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
907 error(_("wrong number of fields on Zone line"));
910 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
912 _("\"Zone %s\" line and -l option are mutually exclusive"),
916 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
918 _("\"Zone %s\" line and -p option are mutually exclusive"),
922 for (i = 0; i < nzones; ++i)
923 if (zones[i].z_name != NULL &&
924 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
926 _("duplicate zone name %s (file \"%s\", line %d)"),
932 return inzsub(fields, nfields, FALSE);
936 inzcont(char ** const fields, const int nfields)
938 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
939 error(_("wrong number of fields on Zone continuation line"));
942 return inzsub(fields, nfields, TRUE);
946 inzsub(char ** const fields, const int nfields, const int iscont)
949 static struct zone z;
950 int i_gmtoff, i_rule, i_format;
951 int i_untilyear, i_untilmonth;
952 int i_untilday, i_untiltime;
956 i_gmtoff = ZFC_GMTOFF;
958 i_format = ZFC_FORMAT;
959 i_untilyear = ZFC_TILYEAR;
960 i_untilmonth = ZFC_TILMONTH;
961 i_untilday = ZFC_TILDAY;
962 i_untiltime = ZFC_TILTIME;
965 i_gmtoff = ZF_GMTOFF;
967 i_format = ZF_FORMAT;
968 i_untilyear = ZF_TILYEAR;
969 i_untilmonth = ZF_TILMONTH;
970 i_untilday = ZF_TILDAY;
971 i_untiltime = ZF_TILTIME;
972 z.z_name = ecpyalloc(fields[ZF_NAME]);
974 z.z_filename = filename;
975 z.z_linenum = linenum;
976 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"), TRUE);
977 if ((cp = strchr(fields[i_format], '%')) != NULL) {
978 if (*++cp != 's' || strchr(cp, '%') != 0) {
979 error(_("invalid abbreviation format"));
983 z.z_rule = ecpyalloc(fields[i_rule]);
984 z.z_format = ecpyalloc(fields[i_format]);
985 if (max_format_len < strlen(z.z_format))
986 max_format_len = strlen(z.z_format);
987 hasuntil = nfields > i_untilyear;
989 z.z_untilrule.r_filename = filename;
990 z.z_untilrule.r_linenum = linenum;
991 rulesub(&z.z_untilrule,
995 (nfields > i_untilmonth) ?
996 fields[i_untilmonth] : "Jan",
997 (nfields > i_untilday) ? fields[i_untilday] : "1",
998 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
999 z.z_untiltime = rpytime(&z.z_untilrule,
1000 z.z_untilrule.r_loyear);
1001 if (iscont && nzones > 0 &&
1002 z.z_untiltime > min_time &&
1003 z.z_untiltime < max_time &&
1004 zones[nzones - 1].z_untiltime > min_time &&
1005 zones[nzones - 1].z_untiltime < max_time &&
1006 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1008 "Zone continuation line end time is not after end time of previous line"
1013 zones = erealloc(zones, (nzones + 1) * sizeof *zones);
1014 zones[nzones++] = z;
1016 ** If there was an UNTIL field on this line,
1017 ** there's more information about the zone on the next line.
1023 inleap(char ** const fields, const int nfields)
1026 const struct lookup *lp;
1033 if (nfields != LEAP_FIELDS) {
1034 error(_("wrong number of fields on Leap line"));
1038 cp = fields[LP_YEAR];
1039 if (sscanf(cp, scheck(cp, "%"SCNdZIC), &year) != 1) {
1043 error(_("invalid leaping year"));
1046 if (!leapseen || leapmaxyear < year)
1048 if (!leapseen || leapminyear > year)
1054 i = len_years[isleap(j)];
1058 i = -len_years[isleap(j)];
1060 dayoff = oadd(dayoff, i);
1062 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1063 error(_("invalid month name"));
1066 month = lp->l_value;
1068 while (j != month) {
1069 i = len_months[isleap(year)][j];
1070 dayoff = oadd(dayoff, i);
1073 cp = fields[LP_DAY];
1074 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1075 day <= 0 || day > len_months[isleap(year)][month]) {
1076 error(_("invalid day of month"));
1079 dayoff = oadd(dayoff, day - 1);
1080 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1081 error(_("time before zero"));
1084 if (dayoff < min_time / SECSPERDAY) {
1085 error(_("time too small"));
1088 if (dayoff > max_time / SECSPERDAY) {
1089 error(_("time too large"));
1092 t = (zic_t) dayoff * SECSPERDAY;
1093 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1094 cp = fields[LP_CORR];
1099 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1102 } else if (strcmp(cp, "--") == 0) {
1105 } else if (strcmp(cp, "+") == 0) {
1108 } else if (strcmp(cp, "++") == 0) {
1112 error(_("illegal CORRECTION field on Leap line"));
1115 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1117 "illegal Rolling/Stationary field on Leap line"
1121 leapadd(tadd(t, tod), positive, lp->l_value, count);
1126 inlink(char ** const fields, const int nfields)
1130 if (nfields != LINK_FIELDS) {
1131 error(_("wrong number of fields on Link line"));
1134 if (*fields[LF_FROM] == '\0') {
1135 error(_("blank FROM field on Link line"));
1138 if (*fields[LF_TO] == '\0') {
1139 error(_("blank TO field on Link line"));
1142 l.l_filename = filename;
1143 l.l_linenum = linenum;
1144 l.l_from = ecpyalloc(fields[LF_FROM]);
1145 l.l_to = ecpyalloc(fields[LF_TO]);
1146 links = erealloc(links, (nlinks + 1) * sizeof *links);
1147 links[nlinks++] = l;
1151 rulesub(struct rule * const rp,
1152 const char * const loyearp,
1153 const char * const hiyearp,
1154 const char * const typep,
1155 const char * const monthp,
1156 const char * const dayp,
1157 const char * const timep)
1159 const struct lookup *lp;
1164 if ((lp = byword(monthp, mon_names)) == NULL) {
1165 error(_("invalid month name"));
1168 rp->r_month = lp->l_value;
1169 rp->r_todisstd = FALSE;
1170 rp->r_todisgmt = FALSE;
1171 dp = ecpyalloc(timep);
1173 ep = dp + strlen(dp) - 1;
1174 switch (lowerit(*ep)) {
1175 case 's': /* Standard */
1176 rp->r_todisstd = TRUE;
1177 rp->r_todisgmt = FALSE;
1180 case 'w': /* Wall */
1181 rp->r_todisstd = FALSE;
1182 rp->r_todisgmt = FALSE;
1185 case 'g': /* Greenwich */
1186 case 'u': /* Universal */
1187 case 'z': /* Zulu */
1188 rp->r_todisstd = TRUE;
1189 rp->r_todisgmt = TRUE;
1194 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1200 lp = byword(cp, begin_years);
1201 rp->r_lowasnum = lp == NULL;
1202 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1204 rp->r_loyear = ZIC_MIN;
1207 rp->r_loyear = ZIC_MAX;
1209 default: /* "cannot happen" */
1211 _("panic: invalid l_value %d"), lp->l_value);
1212 } else if (sscanf(cp, scheck(cp, "%"SCNdZIC), &rp->r_loyear) != 1) {
1213 error(_("invalid starting year"));
1217 lp = byword(cp, end_years);
1218 rp->r_hiwasnum = lp == NULL;
1219 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1221 rp->r_hiyear = ZIC_MIN;
1224 rp->r_hiyear = ZIC_MAX;
1227 rp->r_hiyear = rp->r_loyear;
1229 default: /* "cannot happen" */
1231 _("panic: invalid l_value %d"), lp->l_value);
1232 } else if (sscanf(cp, scheck(cp, "%"SCNdZIC), &rp->r_hiyear) != 1) {
1233 error(_("invalid ending year"));
1236 if (rp->r_loyear > rp->r_hiyear) {
1237 error(_("starting year greater than ending year"));
1241 rp->r_yrtype = NULL;
1243 if (rp->r_loyear == rp->r_hiyear) {
1244 error(_("typed single year"));
1247 rp->r_yrtype = ecpyalloc(typep);
1251 ** Accept things such as:
1257 dp = ecpyalloc(dayp);
1258 if ((lp = byword(dp, lasts)) != NULL) {
1259 rp->r_dycode = DC_DOWLEQ;
1260 rp->r_wday = lp->l_value;
1261 rp->r_dayofmonth = len_months[1][rp->r_month];
1263 if ((ep = strchr(dp, '<')) != NULL)
1264 rp->r_dycode = DC_DOWLEQ;
1265 else if ((ep = strchr(dp, '>')) != NULL)
1266 rp->r_dycode = DC_DOWGEQ;
1269 rp->r_dycode = DC_DOM;
1271 if (rp->r_dycode != DC_DOM) {
1274 error(_("invalid day of month"));
1278 if ((lp = byword(dp, wday_names)) == NULL) {
1279 error(_("invalid weekday name"));
1283 rp->r_wday = lp->l_value;
1285 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1286 rp->r_dayofmonth <= 0 ||
1287 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1288 error(_("invalid day of month"));
1297 convert(const int_fast32_t val, char *const buf)
1301 unsigned char * const b = (unsigned char *) buf;
1303 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1304 b[i] = val >> shift;
1308 convert64(const zic_t val, char * const buf)
1312 unsigned char *const b = (unsigned char *) buf;
1314 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1315 b[i] = val >> shift;
1319 puttzcode(const int_fast32_t val, FILE *const fp)
1324 fwrite(buf, sizeof buf, 1, fp);
1328 puttzcode64(const zic_t val, FILE * const fp)
1332 convert64(val, buf);
1333 fwrite(buf, sizeof buf, 1, fp);
1337 atcomp(const void * avp, const void * bvp)
1339 const zic_t a = ((const struct attype *) avp)->at;
1340 const zic_t b = ((const struct attype *) bvp)->at;
1342 return (a < b) ? -1 : (a > b);
1348 return INT32_MIN <= x && x <= INT32_MAX;
1352 writezone(const char * const name, const char * const string, char version)
1356 int leapcnt32, leapi32;
1357 int timecnt32, timei32;
1359 static char * fullname;
1360 static const struct tzhead tzh0;
1361 static struct tzhead tzh;
1362 zic_t ats[TZ_MAX_TIMES];
1363 unsigned char types[TZ_MAX_TIMES];
1369 qsort(attypes, timecnt, sizeof *attypes, atcomp);
1379 while (fromi < timecnt && attypes[fromi].at < min_time)
1382 ** Remember that type 0 is reserved.
1385 while (fromi < timecnt && attypes[fromi].type == 1)
1386 ++fromi; /* handled by default rule */
1387 for ( ; fromi < timecnt; ++fromi) {
1388 if (toi != 0 && ((attypes[fromi].at +
1389 gmtoffs[attypes[toi - 1].type]) <=
1390 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1391 : attypes[toi - 2].type]))) {
1392 attypes[toi - 1].type =
1393 attypes[fromi].type;
1397 attypes[toi - 1].type != attypes[fromi].type)
1398 attypes[toi++] = attypes[fromi];
1405 for (i = 0; i < timecnt; ++i) {
1406 ats[i] = attypes[i].at;
1407 types[i] = attypes[i].type;
1410 ** Correct for leap seconds.
1412 for (i = 0; i < timecnt; ++i) {
1415 if (ats[i] > trans[j] - corr[j]) {
1416 ats[i] = tadd(ats[i], corr[j]);
1421 ** Figure out 32-bit-limited starts and counts.
1423 timecnt32 = timecnt;
1425 leapcnt32 = leapcnt;
1427 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1429 while (timecnt32 > 0 && !is32(ats[timei32])) {
1433 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1435 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1439 fullname = erealloc(fullname,
1440 strlen(directory) + 1 + strlen(name) + 1);
1441 sprintf(fullname, "%s/%s", directory, name);
1443 ** Remove old file, if any, to snap links.
1445 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1446 err(EXIT_FAILURE, _("can't remove %s"), fullname);
1447 if ((fp = fopen(fullname, "wb")) == NULL) {
1448 if (mkdirs(fullname) != 0)
1450 if ((fp = fopen(fullname, "wb")) == NULL)
1451 err(EXIT_FAILURE, _("can't create %s"), fullname);
1453 for (pass = 1; pass <= 2; ++pass) {
1454 int thistimei, thistimecnt;
1455 int thisleapi, thisleapcnt;
1456 int thistimelim, thisleaplim;
1457 int writetype[TZ_MAX_TIMES];
1458 int typemap[TZ_MAX_TYPES];
1460 char thischars[TZ_MAX_CHARS];
1462 int indmap[TZ_MAX_CHARS];
1465 thistimei = timei32;
1466 thistimecnt = timecnt32;
1467 thisleapi = leapi32;
1468 thisleapcnt = leapcnt32;
1471 thistimecnt = timecnt;
1473 thisleapcnt = leapcnt;
1475 thistimelim = thistimei + thistimecnt;
1476 thisleaplim = thisleapi + thisleapcnt;
1478 ** Remember that type 0 is reserved.
1480 writetype[0] = FALSE;
1481 for (i = 1; i < typecnt; ++i)
1482 writetype[i] = thistimecnt == timecnt;
1483 if (thistimecnt == 0) {
1485 ** No transition times fall in the current
1486 ** (32- or 64-bit) window.
1489 writetype[typecnt - 1] = TRUE;
1491 for (i = thistimei - 1; i < thistimelim; ++i)
1493 writetype[types[i]] = TRUE;
1495 ** For America/Godthab and Antarctica/Palmer
1498 ** Remember that type 0 is reserved.
1501 writetype[1] = TRUE;
1504 ** For some pre-2011 systems: if the last-to-be-written
1505 ** standard (or daylight) type has an offset different from the
1506 ** most recently used offset,
1507 ** append an (unused) copy of the most recently used type
1508 ** (to help get global "altzone" and "timezone" variables
1512 int mrudst, mrustd, hidst, histd, type;
1514 hidst = histd = mrudst = mrustd = -1;
1515 for (i = thistimei; i < thistimelim; ++i)
1516 if (isdsts[types[i]])
1518 else mrustd = types[i];
1519 for (i = 0; i < typecnt; ++i)
1525 if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1526 gmtoffs[hidst] != gmtoffs[mrudst]) {
1527 isdsts[mrudst] = -1;
1528 type = addtype(gmtoffs[mrudst],
1529 &chars[abbrinds[mrudst]],
1533 isdsts[mrudst] = TRUE;
1534 writetype[type] = TRUE;
1536 if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1537 gmtoffs[histd] != gmtoffs[mrustd]) {
1538 isdsts[mrustd] = -1;
1539 type = addtype(gmtoffs[mrustd],
1540 &chars[abbrinds[mrustd]],
1544 isdsts[mrustd] = FALSE;
1545 writetype[type] = TRUE;
1550 ** Potentially, set type 0 to that of lowest-valued time.
1552 if (thistimei > 0) {
1553 for (i = 1; i < typecnt; ++i)
1554 if (writetype[i] && !isdsts[i])
1556 if (i != types[thistimei - 1]) {
1557 i = types[thistimei - 1];
1558 gmtoffs[0] = gmtoffs[i];
1559 isdsts[0] = isdsts[i];
1560 ttisstds[0] = ttisstds[i];
1561 ttisgmts[0] = ttisgmts[i];
1562 abbrinds[0] = abbrinds[i];
1563 writetype[0] = TRUE;
1564 writetype[i] = FALSE;
1567 for (i = 0; i < typecnt; ++i)
1568 typemap[i] = writetype[i] ? thistypecnt++ : 0;
1569 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1572 for (i = 0; i < typecnt; ++i) {
1577 if (indmap[abbrinds[i]] >= 0)
1579 thisabbr = &chars[abbrinds[i]];
1580 for (j = 0; j < thischarcnt; ++j)
1581 if (strcmp(&thischars[j], thisabbr) == 0)
1583 if (j == thischarcnt) {
1584 strcpy(&thischars[(int) thischarcnt],
1586 thischarcnt += strlen(thisabbr) + 1;
1588 indmap[abbrinds[i]] = j;
1590 #define DO(field) fwrite(tzh.field, sizeof tzh.field, 1, fp)
1592 strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1593 tzh.tzh_version[0] = version;
1594 convert(thistypecnt, tzh.tzh_ttisgmtcnt);
1595 convert(thistypecnt, tzh.tzh_ttisstdcnt);
1596 convert(thisleapcnt, tzh.tzh_leapcnt);
1597 convert(thistimecnt, tzh.tzh_timecnt);
1598 convert(thistypecnt, tzh.tzh_typecnt);
1599 convert(thischarcnt, tzh.tzh_charcnt);
1610 for (i = thistimei; i < thistimelim; ++i)
1612 puttzcode(ats[i], fp);
1613 else puttzcode64(ats[i], fp);
1614 for (i = thistimei; i < thistimelim; ++i) {
1617 uc = typemap[types[i]];
1618 fwrite(&uc, sizeof uc, 1, fp);
1620 for (i = 0; i < typecnt; ++i)
1622 puttzcode(gmtoffs[i], fp);
1623 putc(isdsts[i], fp);
1624 putc((unsigned char) indmap[abbrinds[i]], fp);
1626 if (thischarcnt != 0)
1627 fwrite(thischars, sizeof thischars[0],
1629 for (i = thisleapi; i < thisleaplim; ++i) {
1633 if (timecnt == 0 || trans[i] < ats[0]) {
1636 if (++j >= typecnt) {
1642 while (j < timecnt &&
1647 todo = tadd(trans[i], -gmtoffs[j]);
1648 } else todo = trans[i];
1650 puttzcode(todo, fp);
1651 else puttzcode64(todo, fp);
1652 puttzcode(corr[i], fp);
1654 for (i = 0; i < typecnt; ++i)
1656 putc(ttisstds[i], fp);
1657 for (i = 0; i < typecnt; ++i)
1659 putc(ttisgmts[i], fp);
1661 fprintf(fp, "\n%s\n", string);
1662 if (ferror(fp) || fclose(fp))
1663 errx(EXIT_FAILURE, _("error writing %s"), fullname);
1664 if (chmod(fullname, mflag) < 0)
1665 err(EXIT_FAILURE, _("cannot change mode of %s to %03o"),
1666 fullname, (unsigned)mflag);
1667 if ((uflag != (uid_t)-1 || gflag != (gid_t)-1)
1668 && chown(fullname, uflag, gflag) < 0)
1669 err(EXIT_FAILURE, _("cannot change ownership of %s"),
1674 doabbr(char * const abbr, const char * const format,
1675 const char * const letters, const int isdst, const int doquotes)
1681 slashp = strchr(format, '/');
1682 if (slashp == NULL) {
1683 if (letters == NULL)
1684 strcpy(abbr, format);
1685 else sprintf(abbr, format, letters);
1687 strcpy(abbr, slashp + 1);
1689 if (slashp > format)
1690 strncpy(abbr, format, slashp - format);
1691 abbr[slashp - format] = '\0';
1695 for (cp = abbr; *cp != '\0'; ++cp)
1696 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1697 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1700 if (len > 0 && *cp == '\0')
1702 abbr[len + 2] = '\0';
1703 abbr[len + 1] = '>';
1704 for ( ; len > 0; --len)
1705 abbr[len] = abbr[len - 1];
1710 updateminmax(const zic_t x)
1719 stringoffset(char *result, zic_t offset)
1727 strcpy(result, "-");
1730 seconds = offset % SECSPERMIN;
1731 offset /= SECSPERMIN;
1732 minutes = offset % MINSPERHOUR;
1733 offset /= MINSPERHOUR;
1735 if (hours >= HOURSPERDAY * DAYSPERWEEK) {
1739 sprintf(end(result), "%d", hours);
1740 if (minutes != 0 || seconds != 0) {
1741 sprintf(end(result), ":%02d", minutes);
1743 sprintf(end(result), ":%02d", seconds);
1749 stringrule(char *result, const struct rule * const rp, const zic_t dstoff,
1752 zic_t tod = rp->r_tod;
1755 result = end(result);
1756 if (rp->r_dycode == DC_DOM) {
1759 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1762 for (month = 0; month < rp->r_month; ++month)
1763 total += len_months[0][month];
1764 /* Omit the "J" in Jan and Feb, as that's shorter. */
1765 if (rp->r_month <= 1)
1766 sprintf(result, "%d", total + rp->r_dayofmonth - 1);
1768 sprintf(result, "J%d", total + rp->r_dayofmonth);
1771 int wday = rp->r_wday;
1774 if (rp->r_dycode == DC_DOWGEQ) {
1775 wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
1779 tod += wdayoff * SECSPERDAY;
1780 week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
1781 } else if (rp->r_dycode == DC_DOWLEQ) {
1782 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1785 wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
1789 tod += wdayoff * SECSPERDAY;
1790 week = rp->r_dayofmonth / DAYSPERWEEK;
1792 } else return -1; /* "cannot happen" */
1794 wday += DAYSPERWEEK;
1795 sprintf(result, "M%d.%d.%d",
1796 rp->r_month + 1, week, wday);
1800 if (rp->r_todisstd && rp->r_stdoff == 0)
1802 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1803 strcat(result, "/");
1804 if (stringoffset(end(result), tod) != 0)
1809 } else if (SECSPERDAY <= tod) {
1818 rule_cmp(struct rule const *a, struct rule const *b)
1824 if (a->r_hiyear != b->r_hiyear)
1825 return a->r_hiyear < b->r_hiyear ? -1 : 1;
1826 if (a->r_month - b->r_month != 0)
1827 return a->r_month - b->r_month;
1828 return a->r_dayofmonth - b->r_dayofmonth;
1831 enum { YEAR_BY_YEAR_ZONE = 1 };
1834 stringzone(char *result, const struct zone * const zpfirst,
1835 const int zonecount)
1837 const struct zone * zp;
1839 struct rule * stdrp;
1840 struct rule * dstrp;
1842 const char * abbrvar;
1845 struct rule stdr, dstr;
1848 zp = zpfirst + zonecount - 1;
1849 stdrp = dstrp = NULL;
1850 for (i = 0; i < zp->z_nrules; ++i) {
1851 rp = &zp->z_rules[i];
1852 if (rp->r_hiwasnum || rp->r_hiyear != ZIC_MAX)
1854 if (rp->r_yrtype != NULL)
1856 if (rp->r_stdoff == 0) {
1866 if (stdrp == NULL && dstrp == NULL) {
1868 ** There are no rules running through "max".
1869 ** Find the latest std rule in stdabbrrp
1870 ** and latest rule of any type in stdrp.
1872 struct rule *stdabbrrp = NULL;
1873 for (i = 0; i < zp->z_nrules; ++i) {
1874 rp = &zp->z_rules[i];
1875 if (rp->r_stdoff == 0 && rule_cmp(stdabbrrp, rp) < 0)
1877 if (rule_cmp(stdrp, rp) < 0)
1881 ** Horrid special case: if year is 2037,
1882 ** presume this is a zone handled on a year-by-year basis;
1883 ** do not try to apply a rule to the zone.
1885 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1886 return YEAR_BY_YEAR_ZONE;
1888 if (stdrp != NULL && stdrp->r_stdoff != 0) {
1889 /* Perpetual DST. */
1890 dstr.r_month = TM_JANUARY;
1891 dstr.r_dycode = DC_DOM;
1892 dstr.r_dayofmonth = 1;
1894 dstr.r_todisstd = dstr.r_todisgmt = FALSE;
1895 dstr.r_stdoff = stdrp->r_stdoff;
1896 dstr.r_abbrvar = stdrp->r_abbrvar;
1897 stdr.r_month = TM_DECEMBER;
1898 stdr.r_dycode = DC_DOM;
1899 stdr.r_dayofmonth = 31;
1900 stdr.r_tod = SECSPERDAY + stdrp->r_stdoff;
1901 stdr.r_todisstd = stdr.r_todisgmt = FALSE;
1904 = (stdabbrrp ? stdabbrrp->r_abbrvar : "");
1909 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1911 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1912 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1913 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1919 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1920 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1921 if (stringoffset(end(result),
1922 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1926 strcat(result, ",");
1927 c = stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff);
1934 strcat(result, ",");
1935 c = stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff);
1946 outzone(const struct zone * const zpfirst, const int zonecount)
1948 const struct zone *zp;
1951 int usestart, useuntil;
1952 zic_t starttime, untiltime;
1965 int prodstic; /* all rules are min to max */
1970 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1971 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1972 startbuf = emalloc(max_abbr_len + 1);
1973 ab = emalloc(max_abbr_len + 1);
1974 envvar = emalloc(max_envvar_len + 1);
1975 INITIALIZE(untiltime);
1976 INITIALIZE(starttime);
1978 ** Now. . .finally. . .generate some useful data!
1983 prodstic = zonecount == 1;
1985 ** Thanks to Earl Chew
1986 ** for noting the need to unconditionally initialize startttisstd.
1988 startttisstd = FALSE;
1989 startttisgmt = FALSE;
1990 min_year = max_year = EPOCH_YEAR;
1992 updateminmax(leapminyear);
1993 updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
1998 gmtoffs[0] = isdsts[0] = ttisstds[0] = ttisgmts[0] = abbrinds[0] = -1;
2000 for (i = 0; i < zonecount; ++i) {
2002 if (i < zonecount - 1)
2003 updateminmax(zp->z_untilrule.r_loyear);
2004 for (j = 0; j < zp->z_nrules; ++j) {
2005 rp = &zp->z_rules[j];
2007 updateminmax(rp->r_loyear);
2009 updateminmax(rp->r_hiyear);
2010 if (rp->r_lowasnum || rp->r_hiwasnum)
2015 ** Generate lots of data if a rule can't cover all future times.
2017 compat = stringzone(envvar, zpfirst, zonecount);
2018 version = compat < 2013 ? ZIC_VERSION_PRE_2013 : ZIC_VERSION;
2019 do_extend = compat < 0 || compat == YEAR_BY_YEAR_ZONE;
2023 _("no POSIX environment variable for zone"),
2025 else if (compat != 0 && compat != YEAR_BY_YEAR_ZONE) {
2026 /* Circa-COMPAT clients, and earlier clients, might
2027 not work for this zone when given dates before
2028 1970 or after 2038. */
2029 warning(_("%s: pre-%d clients may mishandle"
2030 " distant timestamps"),
2031 zpfirst->z_name, compat);
2036 ** Search through a couple of extra years past the obvious
2037 ** 400, to avoid edge cases. For example, suppose a non-POSIX
2038 ** rule applies from 2012 onwards and has transitions in March
2039 ** and September, plus some one-off transitions in November
2040 ** 2013. If zic looked only at the last 400 years, it would
2041 ** set max_year=2413, with the intent that the 400 years 2014
2042 ** through 2413 will be repeated. The last transition listed
2043 ** in the tzfile would be in 2413-09, less than 400 years
2044 ** after the last one-off transition in 2013-11. Two years
2045 ** might be overkill, but with the kind of edge cases
2046 ** available we're not sure that one year would suffice.
2048 enum { years_of_observations = YEARSPERREPEAT + 2 };
2050 if (min_year >= ZIC_MIN + years_of_observations)
2051 min_year -= years_of_observations;
2052 else min_year = ZIC_MIN;
2053 if (max_year <= ZIC_MAX - years_of_observations)
2054 max_year += years_of_observations;
2055 else max_year = ZIC_MAX;
2057 ** Regardless of any of the above,
2058 ** for a "proDSTic" zone which specifies that its rules
2059 ** always have and always will be in effect,
2060 ** we only need one cycle to define the zone.
2064 max_year = min_year + years_of_observations;
2068 ** For the benefit of older systems,
2069 ** generate data from 1900 through 2037.
2071 if (min_year > 1900)
2073 if (max_year < 2037)
2075 for (i = 0; i < zonecount; ++i) {
2077 ** A guess that may well be corrected later.
2081 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2082 useuntil = i < (zonecount - 1);
2083 if (useuntil && zp->z_untiltime <= min_time)
2085 gmtoff = zp->z_gmtoff;
2086 eat(zp->z_filename, zp->z_linenum);
2088 startoff = zp->z_gmtoff;
2089 if (zp->z_nrules == 0) {
2090 stdoff = zp->z_stdoff;
2091 doabbr(startbuf, zp->z_format,
2092 NULL, stdoff != 0, FALSE);
2093 type = addtype(oadd(zp->z_gmtoff, stdoff),
2094 startbuf, stdoff != 0, startttisstd,
2097 addtt(starttime, type);
2099 } else if (stdoff != 0)
2100 addtt(min_time, type);
2101 } else for (year = min_year; year <= max_year; ++year) {
2102 if (useuntil && year > zp->z_untilrule.r_hiyear)
2105 ** Mark which rules to do in the current year.
2106 ** For those to do, calculate rpytime(rp, year);
2108 for (j = 0; j < zp->z_nrules; ++j) {
2109 rp = &zp->z_rules[j];
2110 eats(zp->z_filename, zp->z_linenum,
2111 rp->r_filename, rp->r_linenum);
2112 rp->r_todo = year >= rp->r_loyear &&
2113 year <= rp->r_hiyear &&
2114 yearistype(year, rp->r_yrtype);
2116 rp->r_temp = rpytime(rp, year);
2126 ** Turn untiltime into UT
2127 ** assuming the current gmtoff and
2130 untiltime = zp->z_untiltime;
2131 if (!zp->z_untilrule.r_todisgmt)
2132 untiltime = tadd(untiltime,
2134 if (!zp->z_untilrule.r_todisstd)
2135 untiltime = tadd(untiltime,
2139 ** Find the rule (of those to do, if any)
2140 ** that takes effect earliest in the year.
2143 for (j = 0; j < zp->z_nrules; ++j) {
2144 rp = &zp->z_rules[j];
2147 eats(zp->z_filename, zp->z_linenum,
2148 rp->r_filename, rp->r_linenum);
2149 offset = rp->r_todisgmt ? 0 : gmtoff;
2150 if (!rp->r_todisstd)
2151 offset = oadd(offset, stdoff);
2153 if (jtime == min_time ||
2156 jtime = tadd(jtime, -offset);
2157 if (k < 0 || jtime < ktime) {
2163 break; /* go on to next year */
2164 rp = &zp->z_rules[k];
2166 if (useuntil && ktime >= untiltime)
2168 stdoff = rp->r_stdoff;
2169 if (usestart && ktime == starttime)
2172 if (ktime < starttime) {
2173 startoff = oadd(zp->z_gmtoff,
2175 doabbr(startbuf, zp->z_format,
2181 if (*startbuf == '\0' &&
2182 startoff == oadd(zp->z_gmtoff,
2192 eats(zp->z_filename, zp->z_linenum,
2193 rp->r_filename, rp->r_linenum);
2194 doabbr(ab, zp->z_format, rp->r_abbrvar,
2195 rp->r_stdoff != 0, FALSE);
2196 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2197 type = addtype(offset, ab, rp->r_stdoff != 0,
2198 rp->r_todisstd, rp->r_todisgmt);
2203 if (*startbuf == '\0' &&
2204 zp->z_format != NULL &&
2205 strchr(zp->z_format, '%') == NULL &&
2206 strchr(zp->z_format, '/') == NULL)
2207 strcpy(startbuf, zp->z_format);
2208 eat(zp->z_filename, zp->z_linenum);
2209 if (*startbuf == '\0')
2210 error(_("can't determine time zone abbreviation to use just after until time"));
2211 else addtt(starttime,
2212 addtype(startoff, startbuf,
2213 startoff != zp->z_gmtoff,
2218 ** Now we may get to set starttime for the next zone line.
2221 startttisstd = zp->z_untilrule.r_todisstd;
2222 startttisgmt = zp->z_untilrule.r_todisgmt;
2223 starttime = zp->z_untiltime;
2225 starttime = tadd(starttime, -stdoff);
2227 starttime = tadd(starttime, -gmtoff);
2232 ** If we're extending the explicitly listed observations
2233 ** for 400 years because we can't fill the POSIX-TZ field,
2234 ** check whether we actually ended up explicitly listing
2235 ** observations through that period. If there aren't any
2236 ** near the end of the 400-year period, add a redundant
2237 ** one at the end of the final year, to make it clear
2238 ** that we are claiming to have definite knowledge of
2239 ** the lack of transitions up to that point.
2242 struct attype *lastat;
2243 xr.r_month = TM_JANUARY;
2244 xr.r_dycode = DC_DOM;
2245 xr.r_dayofmonth = 1;
2247 for (lastat = &attypes[0], i = 1; i < timecnt; i++)
2248 if (attypes[i].at > lastat->at)
2249 lastat = &attypes[i];
2250 if (lastat->at < rpytime(&xr, max_year - 1)) {
2252 ** Create new type code for the redundant entry,
2253 ** to prevent it being optimised away.
2255 if (typecnt >= TZ_MAX_TYPES) {
2256 error(_("too many local time types"));
2259 gmtoffs[typecnt] = gmtoffs[lastat->type];
2260 isdsts[typecnt] = isdsts[lastat->type];
2261 ttisstds[typecnt] = ttisstds[lastat->type];
2262 ttisgmts[typecnt] = ttisgmts[lastat->type];
2263 abbrinds[typecnt] = abbrinds[lastat->type];
2265 addtt(rpytime(&xr, max_year + 1), typecnt-1);
2268 writezone(zpfirst->z_name, envvar, version);
2275 addtt(const zic_t starttime, int type)
2277 if (starttime <= min_time ||
2278 (timecnt == 1 && attypes[0].at < min_time)) {
2279 gmtoffs[0] = gmtoffs[type];
2280 isdsts[0] = isdsts[type];
2281 ttisstds[0] = ttisstds[type];
2282 ttisgmts[0] = ttisgmts[type];
2283 if (abbrinds[type] != 0)
2284 strcpy(chars, &chars[abbrinds[type]]);
2286 charcnt = strlen(chars) + 1;
2291 if (timecnt >= TZ_MAX_TIMES) {
2292 error(_("too many transitions?!"));
2295 attypes[timecnt].at = starttime;
2296 attypes[timecnt].type = type;
2301 addtype(const zic_t gmtoff, const char * const abbr, const int isdst,
2302 const int ttisstd, const int ttisgmt)
2306 if (isdst != TRUE && isdst != FALSE) {
2307 error(_("internal error - addtype called with bad isdst"));
2310 if (ttisstd != TRUE && ttisstd != FALSE) {
2311 error(_("internal error - addtype called with bad ttisstd"));
2314 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2315 error(_("internal error - addtype called with bad ttisgmt"));
2319 ** See if there's already an entry for this zone type.
2320 ** If so, just return its index.
2322 for (i = 0; i < typecnt; ++i) {
2323 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2324 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2325 ttisstd == ttisstds[i] &&
2326 ttisgmt == ttisgmts[i])
2330 ** There isn't one; add a new one, unless there are already too
2333 if (typecnt >= TZ_MAX_TYPES) {
2334 error(_("too many local time types"));
2337 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2338 error(_("UT offset out of range"));
2341 gmtoffs[i] = gmtoff;
2343 ttisstds[i] = ttisstd;
2344 ttisgmts[i] = ttisgmt;
2346 for (j = 0; j < charcnt; ++j)
2347 if (strcmp(&chars[j], abbr) == 0)
2357 leapadd(const zic_t t, const int positive, const int rolling, int count)
2361 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2362 error(_("too many leap seconds"));
2365 for (i = 0; i < leapcnt; ++i)
2366 if (t <= trans[i]) {
2367 if (t == trans[i]) {
2368 error(_("repeated leap second moment"));
2374 for (j = leapcnt; j > i; --j) {
2375 trans[j] = trans[j - 1];
2376 corr[j] = corr[j - 1];
2377 roll[j] = roll[j - 1];
2380 corr[i] = positive ? 1 : -count;
2383 } while (positive && --count != 0);
2393 ** propagate leap seconds forward
2395 for (i = 0; i < leapcnt; ++i) {
2396 trans[i] = tadd(trans[i], last);
2397 last = corr[i] += last;
2402 yearistype(const int year, const char * const type)
2407 if (type == NULL || *type == '\0')
2409 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2410 sprintf(buf, "%s %d %s", yitcommand, year, type);
2411 result = system(buf);
2412 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2418 error(_("Wild result from command execution"));
2419 warnx(_("command was '%s', result was %d"), buf, result);
2427 a = (unsigned char) a;
2428 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2432 ciequal(const char *ap, const char *bp) /* case-insensitive equality */
2434 while (lowerit(*ap) == lowerit(*bp++))
2441 itsabbr(const char *abbr, const char *word)
2443 if (lowerit(*abbr) != lowerit(*word))
2446 while (*++abbr != '\0')
2450 } while (lowerit(*word++) != lowerit(*abbr));
2454 static __pure const struct lookup *
2455 byword(const char * const word, const struct lookup * const table)
2457 const struct lookup *foundlp;
2458 const struct lookup *lp;
2460 if (word == NULL || table == NULL)
2463 ** Look for exact match.
2465 for (lp = table; lp->l_word != NULL; ++lp)
2466 if (ciequal(word, lp->l_word))
2469 ** Look for inexact match.
2472 for (lp = table; lp->l_word != NULL; ++lp)
2473 if (itsabbr(word, lp->l_word)) {
2474 if (foundlp == NULL)
2476 else return NULL; /* multiple inexact matches */
2490 array = emalloc((strlen(cp) + 1) * sizeof *array);
2493 while (isascii((unsigned char) *cp) &&
2494 isspace((unsigned char) *cp))
2496 if (*cp == '\0' || *cp == '#')
2498 array[nsubs++] = dp = cp;
2500 if ((*dp = *cp++) != '"')
2502 else while ((*dp = *cp++) != '"')
2507 "Odd number of quotation marks"
2511 } while (*cp != '\0' && *cp != '#' &&
2512 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2513 if (isascii(*cp) && isspace((unsigned char) *cp))
2517 array[nsubs] = NULL;
2522 oadd(const zic_t t1, const zic_t t2)
2524 if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2) {
2525 error(_("time overflow"));
2532 tadd(const zic_t t1, const zic_t t2)
2534 if (t1 == max_time && t2 > 0)
2536 if (t1 == min_time && t2 < 0)
2538 if (t1 < 0 ? t2 < min_time - t1 : max_time - t1 < t2) {
2539 error(_("time overflow"));
2546 ** Given a rule, and a year, compute the date - in seconds since January 1,
2547 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2551 rpytime(const struct rule * const rp, const zic_t wantedy)
2554 zic_t dayoff; /* with a nod to Margaret O. */
2557 if (wantedy == ZIC_MIN)
2559 if (wantedy == ZIC_MAX)
2564 while (wantedy != y) {
2566 i = len_years[isleap(y)];
2570 i = -len_years[isleap(y)];
2572 dayoff = oadd(dayoff, i);
2574 while (m != rp->r_month) {
2575 i = len_months[isleap(y)][m];
2576 dayoff = oadd(dayoff, i);
2579 i = rp->r_dayofmonth;
2580 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2581 if (rp->r_dycode == DC_DOWLEQ)
2584 error(_("use of 2/29 in non leap-year"));
2589 dayoff = oadd(dayoff, i);
2590 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2593 #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
2596 ** Don't trust mod of negative numbers.
2599 wday = (wday + dayoff) % LDAYSPERWEEK;
2601 wday -= ((-dayoff) % LDAYSPERWEEK);
2603 wday += LDAYSPERWEEK;
2605 while (wday != rp->r_wday)
2606 if (rp->r_dycode == DC_DOWGEQ) {
2607 dayoff = oadd(dayoff, 1);
2608 if (++wday >= LDAYSPERWEEK)
2612 dayoff = oadd(dayoff, -1);
2614 wday = LDAYSPERWEEK - 1;
2617 if (i < 0 || i >= len_months[isleap(y)][m]) {
2619 warning(_("rule goes past start/end of month--\
2620 will not work with pre-2004 versions of zic"));
2623 if (dayoff < min_time / SECSPERDAY)
2625 if (dayoff > max_time / SECSPERDAY)
2627 t = (zic_t) dayoff * SECSPERDAY;
2628 return tadd(t, rp->r_tod);
2632 newabbr(const char *string)
2636 if (strcmp(string, GRANDPARENTED) != 0) {
2642 while (isalpha(*cp) || ('0' <= *cp && *cp <= '9')
2643 || *cp == '-' || *cp == '+')
2645 if (noise && cp - string < 3)
2646 mp = _("time zone abbreviation has fewer than 3 characters");
2647 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2648 mp = _("time zone abbreviation has too many characters");
2650 mp = _("time zone abbreviation differs from POSIX standard");
2652 warning("%s (%s)", mp, string);
2654 i = strlen(string) + 1;
2655 if (charcnt + i > TZ_MAX_CHARS) {
2656 error(_("too many, or too long, time zone abbreviations"));
2659 strcpy(&chars[charcnt], string);
2664 mkdirs(char *argname)
2669 if (argname == NULL || *argname == '\0' || Dflag)
2671 cp = name = ecpyalloc(argname);
2672 while ((cp = strchr(cp + 1, '/')) != NULL) {
2674 #ifdef HAVE_DOS_FILE_NAMES
2676 ** DOS drive specifier?
2678 if (isalpha((unsigned char) name[0]) &&
2679 name[1] == ':' && name[2] == '\0') {
2684 if (!itsdir(name)) {
2686 ** It doesn't seem to exist, so we try to create it.
2687 ** Creation may fail because of the directory being
2688 ** created by some other multiprocessor, so we get
2689 ** to do extra checking.
2691 if ((mkdir(name, MKDIR_UMASK) != 0) &&
2692 (errno != EEXIST || !itsdir(name))) {
2693 warn(_("can't create directory %s"), name);
2708 setgroup(gid_t *flag, const char *name)
2712 if (*flag != (gid_t)-1)
2713 errx(EXIT_FAILURE, _("multiple -g flags specified"));
2715 gr = getgrnam(name);
2720 ul = strtoul(name, &ep, 10);
2721 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2725 errx(EXIT_FAILURE, _("group `%s' not found"), name);
2731 setuser(uid_t *flag, const char *name)
2735 if (*flag != (gid_t)-1)
2736 errx(EXIT_FAILURE, _("multiple -u flags specified"));
2738 pw = getpwnam(name);
2743 ul = strtoul(name, &ep, 10);
2744 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2748 errx(EXIT_FAILURE, _("user `%s' not found"), name);
2754 ** UNIX was a registered trademark of The Open Group in 2003.