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>
16 #define ZIC_VERSION '2'
18 typedef int_fast64_t zic_t;
20 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
21 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
22 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
24 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
27 ** On some ancient hosts, predicates like `isspace(C)' are defined
28 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
29 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
30 ** Neither the C Standard nor Posix require that `isascii' exist.
31 ** For portability, we check both ancient and modern requirements.
32 ** If isascii is not defined, the isascii check succeeds trivially.
39 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
40 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
42 #define end(cp) (strchr((cp), '\0'))
45 const char * r_filename;
49 int r_loyear; /* for example, 1986 */
50 int r_hiyear; /* for example, 1986 */
51 const char * r_yrtype;
55 int r_month; /* 0..11 */
57 int r_dycode; /* see below */
61 long r_tod; /* time from midnight */
62 int r_todisstd; /* above is standard time if TRUE */
63 /* or wall clock time if FALSE */
64 int r_todisgmt; /* above is GMT if TRUE */
65 /* or local time if FALSE */
66 long r_stdoff; /* offset from standard time */
67 const char * r_abbrvar; /* variable part of abbreviation */
69 int r_todo; /* a rule to do (used in outzone) */
70 zic_t r_temp; /* used in outzone */
74 ** r_dycode r_dayofmonth r_wday
77 #define DC_DOM 0 /* 1..31 */ /* unused */
78 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
79 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
82 const char * z_filename;
88 const char * z_format;
92 struct rule * z_rules;
95 struct rule z_untilrule;
99 static void addtt(zic_t starttime, int type);
100 static int addtype(long gmtoff, const char *abbr, int isdst,
101 int ttisstd, int ttisgmt);
102 static void leapadd(zic_t t, int positive, int rolling, int count);
103 static void adjleap(void);
104 static void associate(void);
105 static int ciequal(const char *ap, const char *bp);
106 static void convert(long val, char *buf);
107 static void convert64(zic_t val, char *buf);
108 static void dolink(const char *fromfield, const char *tofield);
109 static void doabbr(char *abbr, const char *format,
110 const char *letters, int isdst, int doquotes);
111 static void eat(const char *name, int num);
112 static void eats(const char *name, int num,
113 const char *rname, int rnum);
114 static long eitol(int i);
115 static void error(const char *message);
116 static char ** getfields(char *buf);
117 static long gethms(const char *string, const char *errstrng,
119 static void infile(const char *filename);
120 static void inleap(char **fields, int nfields);
121 static void inlink(char **fields, int nfields);
122 static void inrule(char **fields, int nfields);
123 static int inzcont(char **fields, int nfields);
124 static int inzone(char **fields, int nfields);
125 static int inzsub(char **fields, int nfields, int iscont);
126 static int is32(zic_t x);
127 static int itsabbr(const char *abbr, const char *word);
128 static int itsdir(const char *name);
129 static int lowerit(int c);
130 static char * memcheck(char *tocheck);
131 static int mkdirs(char *filename);
132 static void newabbr(const char *abbr);
133 static long oadd(long t1, long t2);
134 static void outzone(const struct zone *zp, int ntzones);
135 static void puttzcode(long code, FILE *fp);
136 static void puttzcode64(zic_t code, FILE *fp);
137 static int rcomp(const void *leftp, const void *rightp);
138 static zic_t rpytime(const struct rule *rp, int wantedy);
139 static void rulesub(struct rule *rp,
140 const char *loyearp, const char *hiyearp,
141 const char *typep, const char *monthp,
142 const char *dayp, const char *timep);
143 static int stringoffset(char *result, long offset);
144 static int stringrule(char *result, const struct rule *rp,
145 long dstoff, long gmtoff);
146 static void stringzone(char *result,
147 const struct zone *zp, int ntzones);
148 static void setboundaries(void);
149 static void setgroup(gid_t *flag, const char *name);
150 static void setuser(uid_t *flag, const char *name);
151 static zic_t tadd(const zic_t t1, const long t2);
152 static void usage(void);
153 static void writezone(const char *name, const char *string);
154 static int yearistype(int year, const char *type);
158 static const char * filename;
161 static int leapminyear;
162 static int leapmaxyear;
164 static int max_abbrvar_len;
165 static int max_format_len;
166 static zic_t max_time;
168 static zic_t min_time;
171 static const char * rfilename;
186 ** Which fields are which on a Zone line.
194 #define ZF_TILMONTH 6
197 #define ZONE_MINFIELDS 5
198 #define ZONE_MAXFIELDS 9
201 ** Which fields are which on a Zone continuation line.
207 #define ZFC_TILYEAR 3
208 #define ZFC_TILMONTH 4
210 #define ZFC_TILTIME 6
211 #define ZONEC_MINFIELDS 3
212 #define ZONEC_MAXFIELDS 7
215 ** Which files are which on a Rule line.
227 #define RULE_FIELDS 10
230 ** Which fields are which on a Link line.
235 #define LINK_FIELDS 3
238 ** Which fields are which on a Leap line.
247 #define LEAP_FIELDS 7
257 static struct rule * rules;
258 static int nrules; /* number of rules */
260 static struct zone * zones;
261 static int nzones; /* number of zones */
264 const char * l_filename;
270 static struct link * links;
278 static struct lookup const *byword(const char *string,
279 const struct lookup *lp);
281 static struct lookup const line_codes[] = {
289 static struct lookup const mon_names[] = {
290 { "January", TM_JANUARY },
291 { "February", TM_FEBRUARY },
292 { "March", TM_MARCH },
293 { "April", TM_APRIL },
297 { "August", TM_AUGUST },
298 { "September", TM_SEPTEMBER },
299 { "October", TM_OCTOBER },
300 { "November", TM_NOVEMBER },
301 { "December", TM_DECEMBER },
305 static struct lookup const wday_names[] = {
306 { "Sunday", TM_SUNDAY },
307 { "Monday", TM_MONDAY },
308 { "Tuesday", TM_TUESDAY },
309 { "Wednesday", TM_WEDNESDAY },
310 { "Thursday", TM_THURSDAY },
311 { "Friday", TM_FRIDAY },
312 { "Saturday", TM_SATURDAY },
316 static struct lookup const lasts[] = {
317 { "last-Sunday", TM_SUNDAY },
318 { "last-Monday", TM_MONDAY },
319 { "last-Tuesday", TM_TUESDAY },
320 { "last-Wednesday", TM_WEDNESDAY },
321 { "last-Thursday", TM_THURSDAY },
322 { "last-Friday", TM_FRIDAY },
323 { "last-Saturday", TM_SATURDAY },
327 static struct lookup const begin_years[] = {
328 { "minimum", YR_MINIMUM },
329 { "maximum", YR_MAXIMUM },
333 static struct lookup const end_years[] = {
334 { "minimum", YR_MINIMUM },
335 { "maximum", YR_MAXIMUM },
340 static struct lookup const leap_types[] = {
342 { "Stationary", FALSE },
346 static const int len_months[2][MONSPERYEAR] = {
347 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
348 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
351 static const int len_years[2] = {
352 DAYSPERNYEAR, DAYSPERLYEAR
355 static struct attype {
358 } attypes[TZ_MAX_TIMES];
359 static long gmtoffs[TZ_MAX_TYPES];
360 static char isdsts[TZ_MAX_TYPES];
361 static unsigned char abbrinds[TZ_MAX_TYPES];
362 static char ttisstds[TZ_MAX_TYPES];
363 static char ttisgmts[TZ_MAX_TYPES];
364 static char chars[TZ_MAX_CHARS];
365 static zic_t trans[TZ_MAX_LEAPS];
366 static long corr[TZ_MAX_LEAPS];
367 static char roll[TZ_MAX_LEAPS];
370 ** Memory allocation.
374 memcheck(char * const ptr)
377 errx(EXIT_FAILURE, _("memory exhausted"));
381 #define emalloc(size) memcheck(imalloc(size))
382 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
383 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
384 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
391 eats(const char * const name, const int num,
392 const char * const rname, const int rnum)
401 eat(const char * const name, const int num)
403 eats(name, num, NULL, -1);
407 error(const char * const string)
410 ** Match the format of "cc" to allow sh users to
411 ** zic ... 2>&1 | error -t "*" -v
414 fprintf(stderr, _("\"%s\", line %d: %s"),
415 filename, linenum, string);
416 if (rfilename != NULL)
417 fprintf(stderr, _(" (rule from \"%s\", line %d)"),
418 rfilename, rlinenum);
419 fprintf(stderr, "\n");
424 warning(const char * const string)
428 cp = ecpyalloc(_("warning: "));
429 cp = ecatalloc(cp, string);
438 fprintf(stderr, "%s\n%s\n",
439 _("usage: zic [-v] [-l localtime] [-p posixrules] [-d directory]"),
440 _(" [-L leapseconds] [-y yearistype] [filename ...]"));
444 static const char * psxrules;
445 static const char * lcltime;
446 static const char * directory;
447 static const char * leapsec;
448 static const char * yitcommand;
450 static uid_t uflag = (uid_t)-1;
451 static gid_t gflag = (gid_t)-1;
452 static mode_t mflag = (S_IRUSR | S_IRGRP | S_IROTH
456 main(int argc, char *argv[])
463 umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
464 #endif /* defined unix */
465 while ((c = getopt(argc, argv, "Dd:g:l:m:p:L:u:vsy:")) != -1)
473 if (directory == NULL)
477 _("more than one -d option specified"));
480 setgroup(&gflag, optarg);
487 _("more than one -l option specified"));
491 void *set = setmode(optarg);
496 if (psxrules == NULL)
500 _("more than one -p option specified"));
503 setuser(&uflag, optarg);
506 if (yitcommand == NULL)
510 _("more than one -y option specified"));
517 _("more than one -L option specified"));
523 warnx(_("-s ignored\n"));
526 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
527 usage(); /* usage message by request */
528 if (directory == NULL)
530 if (yitcommand == NULL)
531 yitcommand = "yearistype";
535 if (optind < argc && leapsec != NULL) {
540 for (i = optind; i < argc; ++i)
545 for (i = 0; i < nzones; i = j) {
547 ** Find the next non-continuation zone entry.
549 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
551 outzone(&zones[i], j - i);
556 for (i = 0; i < nlinks; ++i) {
557 eat(links[i].l_filename, links[i].l_linenum);
558 dolink(links[i].l_from, links[i].l_to);
560 for (j = 0; j < nlinks; ++j)
561 if (strcmp(links[i].l_to,
562 links[j].l_from) == 0)
563 warning(_("link to link"));
565 if (lcltime != NULL) {
566 eat("command line", 1);
567 dolink(lcltime, TZDEFAULT);
569 if (psxrules != NULL) {
570 eat("command line", 1);
571 dolink(psxrules, TZDEFRULES);
573 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
577 dolink(const char * const fromfield, const char * const tofield)
582 if (fromfield[0] == '/')
583 fromname = ecpyalloc(fromfield);
585 fromname = ecpyalloc(directory);
586 fromname = ecatalloc(fromname, "/");
587 fromname = ecatalloc(fromname, fromfield);
589 if (tofield[0] == '/')
590 toname = ecpyalloc(tofield);
592 toname = ecpyalloc(directory);
593 toname = ecatalloc(toname, "/");
594 toname = ecatalloc(toname, tofield);
597 ** We get to be careful here since
598 ** there's a fair chance of root running us.
602 if (link(fromname, toname) != 0) {
605 if (mkdirs(toname) != 0)
608 result = link(fromname, toname);
610 access(fromname, F_OK) == 0 &&
612 const char *s = tofield;
613 char * symlinkcontents = NULL;
615 while ((s = strchr(s+1, '/')) != NULL)
617 ecatalloc(symlinkcontents,
620 ecatalloc(symlinkcontents,
622 result = symlink(symlinkcontents,
625 warning(_("hard link failed, symbolic link used"));
626 ifree(symlinkcontents);
629 err(EXIT_FAILURE, _("can't link from %s to %s"),
637 #define TIME_T_BITS_IN_FILE 64
645 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
647 max_time = -(min_time + 1);
651 itsdir(const char * const name)
656 myname = ecpyalloc(name);
657 myname = ecatalloc(myname, "/.");
658 accres = access(myname, F_OK);
664 ** Associate sets of rules with zones.
668 ** Sort by rule name.
672 rcomp(const void *cp1, const void *cp2)
674 return strcmp(((const struct rule *) cp1)->r_name,
675 ((const struct rule *) cp2)->r_name);
687 qsort((void *) rules, (size_t) nrules,
688 (size_t) sizeof *rules, rcomp);
689 for (i = 0; i < nrules - 1; ++i) {
690 if (strcmp(rules[i].r_name,
691 rules[i + 1].r_name) != 0)
693 if (strcmp(rules[i].r_filename,
694 rules[i + 1].r_filename) == 0)
696 eat(rules[i].r_filename, rules[i].r_linenum);
697 warning(_("same rule name in multiple files"));
698 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
699 warning(_("same rule name in multiple files"));
700 for (j = i + 2; j < nrules; ++j) {
701 if (strcmp(rules[i].r_name,
702 rules[j].r_name) != 0)
704 if (strcmp(rules[i].r_filename,
705 rules[j].r_filename) == 0)
707 if (strcmp(rules[i + 1].r_filename,
708 rules[j].r_filename) == 0)
715 for (i = 0; i < nzones; ++i) {
720 for (base = 0; base < nrules; base = out) {
722 for (out = base + 1; out < nrules; ++out)
723 if (strcmp(rp->r_name, rules[out].r_name) != 0)
725 for (i = 0; i < nzones; ++i) {
727 if (strcmp(zp->z_rule, rp->r_name) != 0)
730 zp->z_nrules = out - base;
733 for (i = 0; i < nzones; ++i) {
735 if (zp->z_nrules == 0) {
737 ** Maybe we have a local standard time offset.
739 eat(zp->z_filename, zp->z_linenum);
740 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
743 ** Note, though, that if there's no rule,
744 ** a '%s' in the format is a bad thing.
746 if (strchr(zp->z_format, '%') != 0)
747 error(_("%s in ruleless zone"));
755 infile(const char *name)
760 const struct lookup *lp;
766 if (strcmp(name, "-") == 0) {
767 name = _("standard input");
769 } else if ((fp = fopen(name, "r")) == NULL)
770 err(EXIT_FAILURE, _("can't open %s"), name);
772 for (num = 1; ; ++num) {
774 if (fgets(buf, (int) sizeof buf, fp) != buf)
776 cp = strchr(buf, '\n');
778 error(_("line too long"));
782 fields = getfields(buf);
784 while (fields[nfields] != NULL) {
787 if (strcmp(fields[nfields], "-") == 0)
788 fields[nfields] = &nada;
793 } else if (wantcont) {
794 wantcont = inzcont(fields, nfields);
796 lp = byword(fields[0], line_codes);
798 error(_("input line of unknown type"));
799 else switch ((int) (lp->l_value)) {
801 inrule(fields, nfields);
805 wantcont = inzone(fields, nfields);
808 inlink(fields, nfields);
814 _("leap line in non leap seconds file %s"), name);
815 else inleap(fields, nfields);
818 default: /* "cannot happen" */
820 _("panic: invalid l_value %d"), lp->l_value);
823 ifree((char *) fields);
826 errx(EXIT_FAILURE, _("error reading %s"), filename);
827 if (fp != stdin && fclose(fp))
828 err(EXIT_FAILURE, _("error closing %s"), filename);
830 error(_("expected continuation line not found"));
834 ** Convert a string of one of the forms
835 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
836 ** into a number of seconds.
837 ** A null string maps to zero.
838 ** Call error with errstring and return zero on errors.
842 gethms(const char *string, const char * const errstring, const int signable)
847 if (string == NULL || *string == '\0')
851 else if (*string == '-') {
855 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
857 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
859 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
860 &hh, &mm, &ss) != 3) {
865 mm < 0 || mm >= MINSPERHOUR ||
866 ss < 0 || ss > SECSPERMIN) {
870 if (LONG_MAX / SECSPERHOUR < hh) {
871 error(_("time overflow"));
874 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
875 warning(_("24:00 not handled by pre-1998 versions of zic"));
876 if (noise && (hh > HOURSPERDAY ||
877 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
878 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
879 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
880 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
884 inrule(char ** const fields, const int nfields)
886 static struct rule r;
888 if (nfields != RULE_FIELDS) {
889 error(_("wrong number of fields on Rule line"));
892 if (*fields[RF_NAME] == '\0') {
893 error(_("nameless rule"));
896 r.r_filename = filename;
897 r.r_linenum = linenum;
898 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
899 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
900 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
901 r.r_name = ecpyalloc(fields[RF_NAME]);
902 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
903 if (max_abbrvar_len < strlen(r.r_abbrvar))
904 max_abbrvar_len = strlen(r.r_abbrvar);
905 rules = (struct rule *) (void *) erealloc((char *) rules,
906 (int) ((nrules + 1) * sizeof *rules));
911 inzone(char ** const fields, const int nfields)
916 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
917 error(_("wrong number of fields on Zone line"));
920 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
921 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
923 _("\"Zone %s\" line and -l option are mutually exclusive"),
928 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
929 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
931 _("\"Zone %s\" line and -p option are mutually exclusive"),
936 for (i = 0; i < nzones; ++i)
937 if (zones[i].z_name != NULL &&
938 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
939 buf = erealloc(buf, (int) (132 +
940 strlen(fields[ZF_NAME]) +
941 strlen(zones[i].z_filename)));
943 _("duplicate zone name %s (file \"%s\", line %d)"),
950 return inzsub(fields, nfields, FALSE);
954 inzcont(char ** const fields, const int nfields)
956 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
957 error(_("wrong number of fields on Zone continuation line"));
960 return inzsub(fields, nfields, TRUE);
964 inzsub(char ** const fields, const int nfields, const int iscont)
967 static struct zone z;
968 int i_gmtoff, i_rule, i_format;
969 int i_untilyear, i_untilmonth;
970 int i_untilday, i_untiltime;
974 i_gmtoff = ZFC_GMTOFF;
976 i_format = ZFC_FORMAT;
977 i_untilyear = ZFC_TILYEAR;
978 i_untilmonth = ZFC_TILMONTH;
979 i_untilday = ZFC_TILDAY;
980 i_untiltime = ZFC_TILTIME;
983 i_gmtoff = ZF_GMTOFF;
985 i_format = ZF_FORMAT;
986 i_untilyear = ZF_TILYEAR;
987 i_untilmonth = ZF_TILMONTH;
988 i_untilday = ZF_TILDAY;
989 i_untiltime = ZF_TILTIME;
990 z.z_name = ecpyalloc(fields[ZF_NAME]);
992 z.z_filename = filename;
993 z.z_linenum = linenum;
994 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
995 if ((cp = strchr(fields[i_format], '%')) != NULL) {
996 if (*++cp != 's' || strchr(cp, '%') != 0) {
997 error(_("invalid abbreviation format"));
1001 z.z_rule = ecpyalloc(fields[i_rule]);
1002 z.z_format = ecpyalloc(fields[i_format]);
1003 if (max_format_len < strlen(z.z_format))
1004 max_format_len = strlen(z.z_format);
1005 hasuntil = nfields > i_untilyear;
1007 z.z_untilrule.r_filename = filename;
1008 z.z_untilrule.r_linenum = linenum;
1009 rulesub(&z.z_untilrule,
1010 fields[i_untilyear],
1013 (nfields > i_untilmonth) ?
1014 fields[i_untilmonth] : "Jan",
1015 (nfields > i_untilday) ? fields[i_untilday] : "1",
1016 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1017 z.z_untiltime = rpytime(&z.z_untilrule,
1018 z.z_untilrule.r_loyear);
1019 if (iscont && nzones > 0 &&
1020 z.z_untiltime > min_time &&
1021 z.z_untiltime < max_time &&
1022 zones[nzones - 1].z_untiltime > min_time &&
1023 zones[nzones - 1].z_untiltime < max_time &&
1024 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1026 "Zone continuation line end time is not after end time of previous line"
1031 zones = (struct zone *) (void *) erealloc((char *) zones,
1032 (int) ((nzones + 1) * sizeof *zones));
1033 zones[nzones++] = z;
1035 ** If there was an UNTIL field on this line,
1036 ** there's more information about the zone on the next line.
1042 inleap(char ** const fields, const int nfields)
1045 const struct lookup *lp;
1047 int year, month, day;
1051 if (nfields != LEAP_FIELDS) {
1052 error(_("wrong number of fields on Leap line"));
1056 cp = fields[LP_YEAR];
1057 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1061 error(_("invalid leaping year"));
1064 if (!leapseen || leapmaxyear < year)
1066 if (!leapseen || leapminyear > year)
1072 i = len_years[isleap(j)];
1076 i = -len_years[isleap(j)];
1078 dayoff = oadd(dayoff, eitol(i));
1080 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1081 error(_("invalid month name"));
1084 month = lp->l_value;
1086 while (j != month) {
1087 i = len_months[isleap(year)][j];
1088 dayoff = oadd(dayoff, eitol(i));
1091 cp = fields[LP_DAY];
1092 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1093 day <= 0 || day > len_months[isleap(year)][month]) {
1094 error(_("invalid day of month"));
1097 dayoff = oadd(dayoff, eitol(day - 1));
1098 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1099 error(_("time before zero"));
1102 if (dayoff < min_time / SECSPERDAY) {
1103 error(_("time too small"));
1106 if (dayoff > max_time / SECSPERDAY) {
1107 error(_("time too large"));
1110 t = (zic_t) dayoff * SECSPERDAY;
1111 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1112 cp = fields[LP_CORR];
1117 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1120 } else if (strcmp(cp, "--") == 0) {
1123 } else if (strcmp(cp, "+") == 0) {
1126 } else if (strcmp(cp, "++") == 0) {
1130 error(_("illegal CORRECTION field on Leap line"));
1133 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1135 "illegal Rolling/Stationary field on Leap line"
1139 leapadd(tadd(t, tod), positive, lp->l_value, count);
1144 inlink(char ** const fields, const int nfields)
1148 if (nfields != LINK_FIELDS) {
1149 error(_("wrong number of fields on Link line"));
1152 if (*fields[LF_FROM] == '\0') {
1153 error(_("blank FROM field on Link line"));
1156 if (*fields[LF_TO] == '\0') {
1157 error(_("blank TO field on Link line"));
1160 l.l_filename = filename;
1161 l.l_linenum = linenum;
1162 l.l_from = ecpyalloc(fields[LF_FROM]);
1163 l.l_to = ecpyalloc(fields[LF_TO]);
1164 links = (struct link *) (void *) erealloc((char *) links,
1165 (int) ((nlinks + 1) * sizeof *links));
1166 links[nlinks++] = l;
1170 rulesub(struct rule * const rp,
1171 const char * const loyearp,
1172 const char * const hiyearp,
1173 const char * const typep,
1174 const char * const monthp,
1175 const char * const dayp,
1176 const char * const timep)
1178 const struct lookup *lp;
1183 if ((lp = byword(monthp, mon_names)) == NULL) {
1184 error(_("invalid month name"));
1187 rp->r_month = lp->l_value;
1188 rp->r_todisstd = FALSE;
1189 rp->r_todisgmt = FALSE;
1190 dp = ecpyalloc(timep);
1192 ep = dp + strlen(dp) - 1;
1193 switch (lowerit(*ep)) {
1194 case 's': /* Standard */
1195 rp->r_todisstd = TRUE;
1196 rp->r_todisgmt = FALSE;
1199 case 'w': /* Wall */
1200 rp->r_todisstd = FALSE;
1201 rp->r_todisgmt = FALSE;
1204 case 'g': /* Greenwich */
1205 case 'u': /* Universal */
1206 case 'z': /* Zulu */
1207 rp->r_todisstd = TRUE;
1208 rp->r_todisgmt = TRUE;
1213 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1219 lp = byword(cp, begin_years);
1220 rp->r_lowasnum = lp == NULL;
1221 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1223 rp->r_loyear = INT_MIN;
1226 rp->r_loyear = INT_MAX;
1228 default: /* "cannot happen" */
1230 _("panic: invalid l_value %d"), lp->l_value);
1231 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1232 error(_("invalid starting year"));
1236 lp = byword(cp, end_years);
1237 rp->r_hiwasnum = lp == NULL;
1238 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1240 rp->r_hiyear = INT_MIN;
1243 rp->r_hiyear = INT_MAX;
1246 rp->r_hiyear = rp->r_loyear;
1248 default: /* "cannot happen" */
1250 _("panic: invalid l_value %d"), lp->l_value);
1251 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1252 error(_("invalid ending year"));
1255 if (rp->r_loyear > rp->r_hiyear) {
1256 error(_("starting year greater than ending year"));
1260 rp->r_yrtype = NULL;
1262 if (rp->r_loyear == rp->r_hiyear) {
1263 error(_("typed single year"));
1266 rp->r_yrtype = ecpyalloc(typep);
1270 ** Accept things such as:
1276 dp = ecpyalloc(dayp);
1277 if ((lp = byword(dp, lasts)) != NULL) {
1278 rp->r_dycode = DC_DOWLEQ;
1279 rp->r_wday = lp->l_value;
1280 rp->r_dayofmonth = len_months[1][rp->r_month];
1282 if ((ep = strchr(dp, '<')) != NULL)
1283 rp->r_dycode = DC_DOWLEQ;
1284 else if ((ep = strchr(dp, '>')) != NULL)
1285 rp->r_dycode = DC_DOWGEQ;
1288 rp->r_dycode = DC_DOM;
1290 if (rp->r_dycode != DC_DOM) {
1293 error(_("invalid day of month"));
1297 if ((lp = byword(dp, wday_names)) == NULL) {
1298 error(_("invalid weekday name"));
1302 rp->r_wday = lp->l_value;
1304 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1305 rp->r_dayofmonth <= 0 ||
1306 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1307 error(_("invalid day of month"));
1316 convert(const long val, char * const buf)
1321 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1322 buf[i] = val >> shift;
1326 convert64(const zic_t val, char * const buf)
1331 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1332 buf[i] = val >> shift;
1336 puttzcode(const long val, FILE * const fp)
1341 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1345 puttzcode64(const zic_t val, FILE * const fp)
1349 convert64(val, buf);
1350 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1354 atcomp(const void * avp, const void * bvp)
1356 const zic_t a = ((const struct attype *) avp)->at;
1357 const zic_t b = ((const struct attype *) bvp)->at;
1359 return (a < b) ? -1 : (a > b);
1365 return INT32_MIN <= x && x <= INT32_MAX;
1369 writezone(const char * const name, const char * const string)
1373 int leapcnt32, leapi32;
1374 int timecnt32, timei32;
1376 static char * fullname;
1377 static const struct tzhead tzh0;
1378 static struct tzhead tzh;
1379 zic_t ats[TZ_MAX_TIMES];
1380 unsigned char types[TZ_MAX_TIMES];
1386 qsort((void *) attypes, (size_t) timecnt,
1387 (size_t) sizeof *attypes, atcomp);
1397 while (fromi < timecnt && attypes[fromi].at < min_time)
1400 while (fromi < timecnt && attypes[fromi].type == 0)
1401 ++fromi; /* handled by default rule */
1402 for ( ; fromi < timecnt; ++fromi) {
1403 if (toi != 0 && ((attypes[fromi].at +
1404 gmtoffs[attypes[toi - 1].type]) <=
1405 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1406 : attypes[toi - 2].type]))) {
1407 attypes[toi - 1].type =
1408 attypes[fromi].type;
1412 attypes[toi - 1].type != attypes[fromi].type)
1413 attypes[toi++] = attypes[fromi];
1420 for (i = 0; i < timecnt; ++i) {
1421 ats[i] = attypes[i].at;
1422 types[i] = attypes[i].type;
1425 ** Correct for leap seconds.
1427 for (i = 0; i < timecnt; ++i) {
1430 if (ats[i] > trans[j] - corr[j]) {
1431 ats[i] = tadd(ats[i], corr[j]);
1436 ** Figure out 32-bit-limited starts and counts.
1438 timecnt32 = timecnt;
1440 leapcnt32 = leapcnt;
1442 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1444 while (timecnt32 > 0 && !is32(ats[timei32])) {
1448 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1450 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1454 fullname = erealloc(fullname,
1455 (int) (strlen(directory) + 1 + strlen(name) + 1));
1456 sprintf(fullname, "%s/%s", directory, name);
1458 ** Remove old file, if any, to snap links.
1460 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1461 err(EXIT_FAILURE, _("can't remove %s"), fullname);
1462 if ((fp = fopen(fullname, "wb")) == NULL) {
1463 if (mkdirs(fullname) != 0)
1465 if ((fp = fopen(fullname, "wb")) == NULL)
1466 err(EXIT_FAILURE, _("can't create %s"), fullname);
1468 for (pass = 1; pass <= 2; ++pass) {
1469 int thistimei, thistimecnt;
1470 int thisleapi, thisleapcnt;
1471 int thistimelim, thisleaplim;
1472 int writetype[TZ_MAX_TIMES];
1473 int typemap[TZ_MAX_TYPES];
1475 char thischars[TZ_MAX_CHARS];
1477 int indmap[TZ_MAX_CHARS];
1480 thistimei = timei32;
1481 thistimecnt = timecnt32;
1482 thisleapi = leapi32;
1483 thisleapcnt = leapcnt32;
1486 thistimecnt = timecnt;
1488 thisleapcnt = leapcnt;
1490 thistimelim = thistimei + thistimecnt;
1491 thisleaplim = thisleapi + thisleapcnt;
1492 for (i = 0; i < typecnt; ++i)
1493 writetype[i] = thistimecnt == timecnt;
1494 if (thistimecnt == 0) {
1496 ** No transition times fall in the current
1497 ** (32- or 64-bit) window.
1500 writetype[typecnt - 1] = TRUE;
1502 for (i = thistimei - 1; i < thistimelim; ++i)
1504 writetype[types[i]] = TRUE;
1506 ** For America/Godthab and Antarctica/Palmer
1509 writetype[0] = TRUE;
1512 ** For some pre-2011 systems: if the last-to-be-written
1513 ** standard (or daylight) type has an offset different from the
1514 ** most recently used offset,
1515 ** append an (unused) copy of the most recently used type
1516 ** (to help get global "altzone" and "timezone" variables
1520 int mrudst, mrustd, hidst, histd, type;
1522 hidst = histd = mrudst = mrustd = -1;
1523 for (i = thistimei; i < thistimelim; ++i)
1524 if (isdsts[types[i]])
1526 else mrustd = types[i];
1527 for (i = 0; i < typecnt; ++i)
1533 if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1534 gmtoffs[hidst] != gmtoffs[mrudst]) {
1535 isdsts[mrudst] = -1;
1536 type = addtype(gmtoffs[mrudst],
1537 &chars[abbrinds[mrudst]],
1541 isdsts[mrudst] = TRUE;
1542 writetype[type] = TRUE;
1544 if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1545 gmtoffs[histd] != gmtoffs[mrustd]) {
1546 isdsts[mrustd] = -1;
1547 type = addtype(gmtoffs[mrustd],
1548 &chars[abbrinds[mrustd]],
1552 isdsts[mrustd] = FALSE;
1553 writetype[type] = TRUE;
1557 for (i = 0; i < typecnt; ++i)
1558 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1559 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1562 for (i = 0; i < typecnt; ++i) {
1567 if (indmap[abbrinds[i]] >= 0)
1569 thisabbr = &chars[abbrinds[i]];
1570 for (j = 0; j < thischarcnt; ++j)
1571 if (strcmp(&thischars[j], thisabbr) == 0)
1573 if (j == thischarcnt) {
1574 strcpy(&thischars[(int) thischarcnt],
1576 thischarcnt += strlen(thisabbr) + 1;
1578 indmap[abbrinds[i]] = j;
1580 #define DO(field) fwrite((void *) tzh.field, \
1581 (size_t) sizeof tzh.field, (size_t) 1, fp)
1583 strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1584 tzh.tzh_version[0] = ZIC_VERSION;
1585 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1586 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1587 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1588 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1589 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1590 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1601 for (i = thistimei; i < thistimelim; ++i)
1603 puttzcode((long) ats[i], fp);
1604 else puttzcode64(ats[i], fp);
1605 for (i = thistimei; i < thistimelim; ++i) {
1608 uc = typemap[types[i]];
1609 fwrite((void *) &uc,
1614 for (i = 0; i < typecnt; ++i)
1616 puttzcode(gmtoffs[i], fp);
1617 putc(isdsts[i], fp);
1618 putc((unsigned char) indmap[abbrinds[i]], fp);
1620 if (thischarcnt != 0)
1621 fwrite((void *) thischars,
1622 (size_t) sizeof thischars[0],
1623 (size_t) thischarcnt, fp);
1624 for (i = thisleapi; i < thisleaplim; ++i) {
1625 register zic_t todo;
1628 if (timecnt == 0 || trans[i] < ats[0]) {
1631 if (++j >= typecnt) {
1637 while (j < timecnt &&
1642 todo = tadd(trans[i], -gmtoffs[j]);
1643 } else todo = trans[i];
1645 puttzcode((long) todo, fp);
1646 else puttzcode64(todo, fp);
1647 puttzcode(corr[i], fp);
1649 for (i = 0; i < typecnt; ++i)
1651 putc(ttisstds[i], fp);
1652 for (i = 0; i < typecnt; ++i)
1654 putc(ttisgmts[i], fp);
1656 fprintf(fp, "\n%s\n", string);
1657 if (ferror(fp) || fclose(fp))
1658 errx(EXIT_FAILURE, _("error writing %s"), fullname);
1659 if (chmod(fullname, mflag) < 0)
1660 err(EXIT_FAILURE, _("cannot change mode of %s to %03o"),
1661 fullname, (unsigned)mflag);
1662 if ((uflag != (uid_t)-1 || gflag != (gid_t)-1)
1663 && chown(fullname, uflag, gflag) < 0)
1664 err(EXIT_FAILURE, _("cannot change ownership of %s"),
1669 doabbr(char * const abbr, const char * const format,
1670 const char * const letters, const int isdst, const int doquotes)
1676 slashp = strchr(format, '/');
1677 if (slashp == NULL) {
1678 if (letters == NULL)
1679 strcpy(abbr, format);
1680 else sprintf(abbr, format, letters);
1682 strcpy(abbr, slashp + 1);
1684 if (slashp > format)
1685 strncpy(abbr, format,
1686 (unsigned) (slashp - format));
1687 abbr[slashp - format] = '\0';
1691 for (cp = abbr; *cp != '\0'; ++cp)
1692 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1693 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1696 if (len > 0 && *cp == '\0')
1698 abbr[len + 2] = '\0';
1699 abbr[len + 1] = '>';
1700 for ( ; len > 0; --len)
1701 abbr[len] = abbr[len - 1];
1706 updateminmax(const int x)
1715 stringoffset(char *result, long offset)
1723 strcpy(result, "-");
1726 seconds = offset % SECSPERMIN;
1727 offset /= SECSPERMIN;
1728 minutes = offset % MINSPERHOUR;
1729 offset /= MINSPERHOUR;
1731 if (hours >= HOURSPERDAY) {
1735 sprintf(end(result), "%d", hours);
1736 if (minutes != 0 || seconds != 0) {
1737 sprintf(end(result), ":%02d", minutes);
1739 sprintf(end(result), ":%02d", seconds);
1745 stringrule(char *result, const struct rule * const rp, const long dstoff,
1750 result = end(result);
1751 if (rp->r_dycode == DC_DOM) {
1754 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1757 for (month = 0; month < rp->r_month; ++month)
1758 total += len_months[0][month];
1759 sprintf(result, "J%d", total + rp->r_dayofmonth);
1763 if (rp->r_dycode == DC_DOWGEQ) {
1764 if ((rp->r_dayofmonth % DAYSPERWEEK) != 1)
1766 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1767 } else if (rp->r_dycode == DC_DOWLEQ) {
1768 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1771 if ((rp->r_dayofmonth % DAYSPERWEEK) != 0)
1773 week = rp->r_dayofmonth / DAYSPERWEEK;
1775 } else return -1; /* "cannot happen" */
1776 sprintf(result, "M%d.%d.%d",
1777 rp->r_month + 1, week, rp->r_wday);
1782 if (rp->r_todisstd && rp->r_stdoff == 0)
1788 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1789 strcat(result, "/");
1790 if (stringoffset(end(result), tod) != 0)
1797 stringzone(char *result, const struct zone * const zpfirst,
1798 const int zonecount)
1800 const struct zone * zp;
1802 struct rule * stdrp;
1803 struct rule * dstrp;
1805 const char * abbrvar;
1808 zp = zpfirst + zonecount - 1;
1809 stdrp = dstrp = NULL;
1810 for (i = 0; i < zp->z_nrules; ++i) {
1811 rp = &zp->z_rules[i];
1812 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1814 if (rp->r_yrtype != NULL)
1816 if (rp->r_stdoff == 0) {
1826 if (stdrp == NULL && dstrp == NULL) {
1828 ** There are no rules running through "max".
1829 ** Let's find the latest rule.
1831 for (i = 0; i < zp->z_nrules; ++i) {
1832 rp = &zp->z_rules[i];
1833 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1834 (rp->r_hiyear == stdrp->r_hiyear &&
1835 rp->r_month > stdrp->r_month))
1838 if (stdrp != NULL && stdrp->r_stdoff != 0)
1839 return; /* We end up in DST (a POSIX no-no). */
1841 ** Horrid special case: if year is 2037,
1842 ** presume this is a zone handled on a year-by-year basis;
1843 ** do not try to apply a rule to the zone.
1845 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1848 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1850 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1851 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1852 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1858 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1859 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1860 if (stringoffset(end(result),
1861 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1865 strcat(result, ",");
1866 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1870 strcat(result, ",");
1871 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1878 outzone(const struct zone * const zpfirst, const int zonecount)
1880 const struct zone *zp;
1883 int usestart, useuntil;
1884 zic_t starttime, untiltime;
1897 int prodstic; /* all rules are min to max */
1899 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1900 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1901 startbuf = emalloc(max_abbr_len + 1);
1902 ab = emalloc(max_abbr_len + 1);
1903 envvar = emalloc(max_envvar_len + 1);
1904 INITIALIZE(untiltime);
1905 INITIALIZE(starttime);
1907 ** Now. . .finally. . .generate some useful data!
1912 prodstic = zonecount == 1;
1914 ** Thanks to Earl Chew
1915 ** for noting the need to unconditionally initialize startttisstd.
1917 startttisstd = FALSE;
1918 startttisgmt = FALSE;
1919 min_year = max_year = EPOCH_YEAR;
1921 updateminmax(leapminyear);
1922 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
1924 for (i = 0; i < zonecount; ++i) {
1926 if (i < zonecount - 1)
1927 updateminmax(zp->z_untilrule.r_loyear);
1928 for (j = 0; j < zp->z_nrules; ++j) {
1929 rp = &zp->z_rules[j];
1931 updateminmax(rp->r_loyear);
1933 updateminmax(rp->r_hiyear);
1934 if (rp->r_lowasnum || rp->r_hiwasnum)
1939 ** Generate lots of data if a rule can't cover all future times.
1941 stringzone(envvar, zpfirst, zonecount);
1942 if (noise && envvar[0] == '\0') {
1945 wp = ecpyalloc(_("no POSIX environment variable for zone"));
1946 wp = ecatalloc(wp, " ");
1947 wp = ecatalloc(wp, zpfirst->z_name);
1951 if (envvar[0] == '\0') {
1952 if (min_year >= INT_MIN + YEARSPERREPEAT)
1953 min_year -= YEARSPERREPEAT;
1954 else min_year = INT_MIN;
1955 if (max_year <= INT_MAX - YEARSPERREPEAT)
1956 max_year += YEARSPERREPEAT;
1957 else max_year = INT_MAX;
1959 ** Regardless of any of the above,
1960 ** for a "proDSTic" zone which specifies that its rules
1961 ** always have and always will be in effect,
1962 ** we only need one cycle to define the zone.
1966 max_year = min_year + YEARSPERREPEAT;
1970 ** For the benefit of older systems,
1971 ** generate data from 1900 through 2037.
1973 if (min_year > 1900)
1975 if (max_year < 2037)
1977 for (i = 0; i < zonecount; ++i) {
1979 ** A guess that may well be corrected later.
1983 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1984 useuntil = i < (zonecount - 1);
1985 if (useuntil && zp->z_untiltime <= min_time)
1987 gmtoff = zp->z_gmtoff;
1988 eat(zp->z_filename, zp->z_linenum);
1990 startoff = zp->z_gmtoff;
1991 if (zp->z_nrules == 0) {
1992 stdoff = zp->z_stdoff;
1993 doabbr(startbuf, zp->z_format,
1994 NULL, stdoff != 0, FALSE);
1995 type = addtype(oadd(zp->z_gmtoff, stdoff),
1996 startbuf, stdoff != 0, startttisstd,
1999 addtt(starttime, type);
2001 } else if (stdoff != 0)
2002 addtt(min_time, type);
2003 } else for (year = min_year; year <= max_year; ++year) {
2004 if (useuntil && year > zp->z_untilrule.r_hiyear)
2007 ** Mark which rules to do in the current year.
2008 ** For those to do, calculate rpytime(rp, year);
2010 for (j = 0; j < zp->z_nrules; ++j) {
2011 rp = &zp->z_rules[j];
2012 eats(zp->z_filename, zp->z_linenum,
2013 rp->r_filename, rp->r_linenum);
2014 rp->r_todo = year >= rp->r_loyear &&
2015 year <= rp->r_hiyear &&
2016 yearistype(year, rp->r_yrtype);
2018 rp->r_temp = rpytime(rp, year);
2028 ** Turn untiltime into UTC
2029 ** assuming the current gmtoff and
2032 untiltime = zp->z_untiltime;
2033 if (!zp->z_untilrule.r_todisgmt)
2034 untiltime = tadd(untiltime,
2036 if (!zp->z_untilrule.r_todisstd)
2037 untiltime = tadd(untiltime,
2041 ** Find the rule (of those to do, if any)
2042 ** that takes effect earliest in the year.
2045 for (j = 0; j < zp->z_nrules; ++j) {
2046 rp = &zp->z_rules[j];
2049 eats(zp->z_filename, zp->z_linenum,
2050 rp->r_filename, rp->r_linenum);
2051 offset = rp->r_todisgmt ? 0 : gmtoff;
2052 if (!rp->r_todisstd)
2053 offset = oadd(offset, stdoff);
2055 if (jtime == min_time ||
2058 jtime = tadd(jtime, -offset);
2059 if (k < 0 || jtime < ktime) {
2065 break; /* go on to next year */
2066 rp = &zp->z_rules[k];
2068 if (useuntil && ktime >= untiltime)
2070 stdoff = rp->r_stdoff;
2071 if (usestart && ktime == starttime)
2074 if (ktime < starttime) {
2075 startoff = oadd(zp->z_gmtoff,
2077 doabbr(startbuf, zp->z_format,
2083 if (*startbuf == '\0' &&
2084 startoff == oadd(zp->z_gmtoff,
2094 eats(zp->z_filename, zp->z_linenum,
2095 rp->r_filename, rp->r_linenum);
2096 doabbr(ab, zp->z_format, rp->r_abbrvar,
2097 rp->r_stdoff != 0, FALSE);
2098 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2099 type = addtype(offset, ab, rp->r_stdoff != 0,
2100 rp->r_todisstd, rp->r_todisgmt);
2105 if (*startbuf == '\0' &&
2106 zp->z_format != NULL &&
2107 strchr(zp->z_format, '%') == NULL &&
2108 strchr(zp->z_format, '/') == NULL)
2109 strcpy(startbuf, zp->z_format);
2110 eat(zp->z_filename, zp->z_linenum);
2111 if (*startbuf == '\0')
2112 error(_("can't determine time zone abbreviation to use just after until time"));
2113 else addtt(starttime,
2114 addtype(startoff, startbuf,
2115 startoff != zp->z_gmtoff,
2120 ** Now we may get to set starttime for the next zone line.
2123 startttisstd = zp->z_untilrule.r_todisstd;
2124 startttisgmt = zp->z_untilrule.r_todisgmt;
2125 starttime = zp->z_untiltime;
2127 starttime = tadd(starttime, -stdoff);
2129 starttime = tadd(starttime, -gmtoff);
2132 writezone(zpfirst->z_name, envvar);
2139 addtt(const zic_t starttime, int type)
2141 if (starttime <= min_time ||
2142 (timecnt == 1 && attypes[0].at < min_time)) {
2143 gmtoffs[0] = gmtoffs[type];
2144 isdsts[0] = isdsts[type];
2145 ttisstds[0] = ttisstds[type];
2146 ttisgmts[0] = ttisgmts[type];
2147 if (abbrinds[type] != 0)
2148 strcpy(chars, &chars[abbrinds[type]]);
2150 charcnt = strlen(chars) + 1;
2155 if (timecnt >= TZ_MAX_TIMES) {
2156 error(_("too many transitions?!"));
2159 attypes[timecnt].at = starttime;
2160 attypes[timecnt].type = type;
2165 addtype(const long gmtoff, const char * const abbr, const int isdst,
2166 const int ttisstd, const int ttisgmt)
2170 if (isdst != TRUE && isdst != FALSE) {
2171 error(_("internal error - addtype called with bad isdst"));
2174 if (ttisstd != TRUE && ttisstd != FALSE) {
2175 error(_("internal error - addtype called with bad ttisstd"));
2178 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2179 error(_("internal error - addtype called with bad ttisgmt"));
2183 ** See if there's already an entry for this zone type.
2184 ** If so, just return its index.
2186 for (i = 0; i < typecnt; ++i) {
2187 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2188 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2189 ttisstd == ttisstds[i] &&
2190 ttisgmt == ttisgmts[i])
2194 ** There isn't one; add a new one, unless there are already too
2197 if (typecnt >= TZ_MAX_TYPES) {
2198 error(_("too many local time types"));
2201 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2202 error(_("UTC offset out of range"));
2205 gmtoffs[i] = gmtoff;
2207 ttisstds[i] = ttisstd;
2208 ttisgmts[i] = ttisgmt;
2210 for (j = 0; j < charcnt; ++j)
2211 if (strcmp(&chars[j], abbr) == 0)
2221 leapadd(const zic_t t, const int positive, const int rolling, int count)
2225 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2226 error(_("too many leap seconds"));
2229 for (i = 0; i < leapcnt; ++i)
2230 if (t <= trans[i]) {
2231 if (t == trans[i]) {
2232 error(_("repeated leap second moment"));
2238 for (j = leapcnt; j > i; --j) {
2239 trans[j] = trans[j - 1];
2240 corr[j] = corr[j - 1];
2241 roll[j] = roll[j - 1];
2244 corr[i] = positive ? 1L : eitol(-count);
2247 } while (positive && --count != 0);
2257 ** propagate leap seconds forward
2259 for (i = 0; i < leapcnt; ++i) {
2260 trans[i] = tadd(trans[i], last);
2261 last = corr[i] += last;
2266 yearistype(const int year, const char * const type)
2271 if (type == NULL || *type == '\0')
2273 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2274 sprintf(buf, "%s %d %s", yitcommand, year, type);
2275 result = system(buf);
2276 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2282 error(_("Wild result from command execution"));
2283 warnx(_("command was '%s', result was %d"), buf, result);
2291 a = (unsigned char) a;
2292 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2296 ciequal(const char *ap, const char *bp) /* case-insensitive equality */
2298 while (lowerit(*ap) == lowerit(*bp++))
2305 itsabbr(const char *abbr, const char *word)
2307 if (lowerit(*abbr) != lowerit(*word))
2310 while (*++abbr != '\0')
2314 } while (lowerit(*word++) != lowerit(*abbr));
2318 static const struct lookup *
2319 byword(const char * const word, const struct lookup * const table)
2321 const struct lookup *foundlp;
2322 const struct lookup *lp;
2324 if (word == NULL || table == NULL)
2327 ** Look for exact match.
2329 for (lp = table; lp->l_word != NULL; ++lp)
2330 if (ciequal(word, lp->l_word))
2333 ** Look for inexact match.
2336 for (lp = table; lp->l_word != NULL; ++lp)
2337 if (itsabbr(word, lp->l_word)) {
2338 if (foundlp == NULL)
2340 else return NULL; /* multiple inexact matches */
2354 array = (char **) (void *)
2355 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2358 while (isascii((unsigned char) *cp) &&
2359 isspace((unsigned char) *cp))
2361 if (*cp == '\0' || *cp == '#')
2363 array[nsubs++] = dp = cp;
2365 if ((*dp = *cp++) != '"')
2367 else while ((*dp = *cp++) != '"')
2372 "Odd number of quotation marks"
2376 } while (*cp != '\0' && *cp != '#' &&
2377 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2378 if (isascii(*cp) && isspace((unsigned char) *cp))
2382 array[nsubs] = NULL;
2387 oadd(const long t1, const long t2)
2392 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2393 error(_("time overflow"));
2400 tadd(const zic_t t1, const long t2)
2404 if (t1 == max_time && t2 > 0)
2406 if (t1 == min_time && t2 < 0)
2409 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2410 error(_("time overflow"));
2417 ** Given a rule, and a year, compute the date - in seconds since January 1,
2418 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2422 rpytime(const struct rule * const rp, const int wantedy)
2425 long dayoff; /* with a nod to Margaret O. */
2428 if (wantedy == INT_MIN)
2430 if (wantedy == INT_MAX)
2435 while (wantedy != y) {
2437 i = len_years[isleap(y)];
2441 i = -len_years[isleap(y)];
2443 dayoff = oadd(dayoff, eitol(i));
2445 while (m != rp->r_month) {
2446 i = len_months[isleap(y)][m];
2447 dayoff = oadd(dayoff, eitol(i));
2450 i = rp->r_dayofmonth;
2451 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2452 if (rp->r_dycode == DC_DOWLEQ)
2455 error(_("use of 2/29 in non leap-year"));
2460 dayoff = oadd(dayoff, eitol(i));
2461 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2464 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2465 wday = eitol(EPOCH_WDAY);
2467 ** Don't trust mod of negative numbers.
2470 wday = (wday + dayoff) % LDAYSPERWEEK;
2472 wday -= ((-dayoff) % LDAYSPERWEEK);
2474 wday += LDAYSPERWEEK;
2476 while (wday != eitol(rp->r_wday))
2477 if (rp->r_dycode == DC_DOWGEQ) {
2478 dayoff = oadd(dayoff, (long) 1);
2479 if (++wday >= LDAYSPERWEEK)
2483 dayoff = oadd(dayoff, (long) -1);
2485 wday = LDAYSPERWEEK - 1;
2488 if (i < 0 || i >= len_months[isleap(y)][m]) {
2490 warning(_("rule goes past start/end of month--\
2491 will not work with pre-2004 versions of zic"));
2494 if (dayoff < min_time / SECSPERDAY)
2496 if (dayoff > max_time / SECSPERDAY)
2498 t = (zic_t) dayoff * SECSPERDAY;
2499 return tadd(t, rp->r_tod);
2503 newabbr(const char * const string)
2507 if (strcmp(string, GRANDPARENTED) != 0) {
2512 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2513 ** optionally followed by a + or - and a number from 1 to 14.
2517 while (isascii((unsigned char) *cp) &&
2518 isalpha((unsigned char) *cp))
2520 if (cp - string == 0)
2521 wp = _("time zone abbreviation lacks alphabetic at start");
2522 if (noise && cp - string > 3)
2523 wp = _("time zone abbreviation has more than 3 alphabetics");
2524 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2525 wp = _("time zone abbreviation has too many alphabetics");
2526 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2528 if (isascii((unsigned char) *cp) &&
2529 isdigit((unsigned char) *cp))
2531 *cp >= '0' && *cp <= '4')
2535 wp = _("time zone abbreviation differs from POSIX standard");
2538 wp = ecatalloc(wp, " (");
2539 wp = ecatalloc(wp, string);
2540 wp = ecatalloc(wp, ")");
2545 i = strlen(string) + 1;
2546 if (charcnt + i > TZ_MAX_CHARS) {
2547 error(_("too many, or too long, time zone abbreviations"));
2550 strcpy(&chars[charcnt], string);
2551 charcnt += eitol(i);
2555 mkdirs(char *argname)
2560 if (argname == NULL || *argname == '\0' || Dflag)
2562 cp = name = ecpyalloc(argname);
2563 while ((cp = strchr(cp + 1, '/')) != NULL) {
2567 ** DOS drive specifier?
2569 if (isalpha((unsigned char) name[0]) &&
2570 name[1] == ':' && name[2] == '\0') {
2574 #endif /* !defined unix */
2575 if (!itsdir(name)) {
2577 ** It doesn't seem to exist, so we try to create it.
2578 ** Creation may fail because of the directory being
2579 ** created by some other multiprocessor, so we get
2580 ** to do extra checking.
2582 if ((mkdir(name, MKDIR_UMASK) != 0) &&
2583 (errno != EEXIST || !itsdir(name))) {
2584 warn(_("can't create directory %s"), name);
2601 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2602 errx(EXIT_FAILURE, _("%d did not sign extend correctly"), i);
2610 setgroup(gid_t *flag, const char *name)
2614 if (*flag != (gid_t)-1)
2615 errx(EXIT_FAILURE, _("multiple -g flags specified"));
2617 gr = getgrnam(name);
2622 ul = strtoul(name, &ep, 10);
2623 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2627 errx(EXIT_FAILURE, _("group `%s' not found"), name);
2633 setuser(uid_t *flag, const char *name)
2637 if (*flag != (gid_t)-1)
2638 errx(EXIT_FAILURE, _("multiple -u flags specified"));
2640 pw = getpwnam(name);
2645 ul = strtoul(name, &ep, 10);
2646 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2650 errx(EXIT_FAILURE, _("user `%s' not found"), name);
2656 ** UNIX was a registered trademark of The Open Group in 2003.