3 static char elsieid[] = "@(#)zic.c 7.19";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
11 const char * r_filename;
15 int r_loyear; /* for example, 1986 */
16 int r_hiyear; /* for example, 1986 */
17 const char * r_yrtype;
19 int r_month; /* 0..11 */
21 int r_dycode; /* see below */
25 long r_tod; /* time from midnight */
26 int r_todisstd; /* above is standard time if TRUE */
27 /* or wall clock time if FALSE */
28 long r_stdoff; /* offset from standard time */
29 const char * r_abbrvar; /* variable part of abbreviation */
31 int r_todo; /* a rule to do (used in outzone) */
32 time_t r_temp; /* used in outzone */
36 ** r_dycode r_dayofmonth r_wday
39 #define DC_DOM 0 /* 1..31 */ /* unused */
40 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
41 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
44 const char * z_filename;
50 const char * z_format;
54 struct rule * z_rules;
57 struct rule z_untilrule;
61 extern int emkdir P((const char * name, int mode));
62 extern int getopt P((int argc, char * argv[], const char * options));
63 extern char * icatalloc P((char * old, const char * new));
64 extern char * icpyalloc P((const char * string));
65 extern void ifree P((char * p));
66 extern char * imalloc P((int n));
67 extern char * irealloc P((char * old, int n));
68 extern int link P((const char * fromname, const char * toname));
71 extern char * scheck P((const char * string, const char * format));
73 static void addtt P((time_t starttime, int type));
74 static int addtype P((long gmtoff, const char * abbr, int isdst,
76 static void leapadd P((time_t t, int positive, int rolling, int count));
77 static void adjleap P((void));
78 static void associate P((void));
79 static int ciequal P((const char * ap, const char * bp));
80 static void convert P((long val, char * buf));
81 static void dolink P((const char * fromfile, const char * tofile));
82 static void eat P((const char * name, int num));
83 static void eats P((const char * name, int num,
84 const char * rname, int rnum));
85 static long eitol P((int i));
86 static void error P((const char * message));
87 static char ** getfields P((char * buf));
88 static long gethms P((const char * string, const char * errstrng,
90 static void infile P((const char * filename));
91 static void inleap P((char ** fields, int nfields));
92 static void inlink P((char ** fields, int nfields));
93 static void inrule P((char ** fields, int nfields));
94 static int inzcont P((char ** fields, int nfields));
95 static int inzone P((char ** fields, int nfields));
96 static int inzsub P((char ** fields, int nfields, int iscont));
97 static int itsabbr P((const char * abbr, const char * word));
98 static int itsdir P((const char * name));
99 static int lowerit P((int c));
100 static char * memcheck P((char * tocheck));
101 static int mkdirs P((char * filename));
102 static void newabbr P((const char * abbr));
103 static long oadd P((long t1, long t2));
104 static void outzone P((const struct zone * zp, int ntzones));
105 static void puttzcode P((long code, FILE * fp));
106 static int rcomp P((const genericptr_t leftp, const genericptr_t rightp));
107 static time_t rpytime P((const struct rule * rp, int wantedy));
108 static void rulesub P((struct rule * rp,
109 char * loyearp, char * hiyearp,
110 char * typep, char * monthp,
111 char * dayp, char * timep));
112 static void setboundaries P((void));
113 static time_t tadd P((time_t t1, long t2));
114 static void usage P((void));
115 static void writezone P((const char * name));
116 static int yearistype P((int year, const char * type));
120 static const char * filename;
123 static time_t max_time;
125 static time_t min_time;
128 static const char * rfilename;
130 static const char * progname;
133 static int tt_signed;
145 ** Which fields are which on a Zone line.
153 #define ZF_TILMONTH 6
156 #define ZONE_MINFIELDS 5
157 #define ZONE_MAXFIELDS 9
160 ** Which fields are which on a Zone continuation line.
166 #define ZFC_TILYEAR 3
167 #define ZFC_TILMONTH 4
169 #define ZFC_TILTIME 6
170 #define ZONEC_MINFIELDS 3
171 #define ZONEC_MAXFIELDS 7
174 ** Which files are which on a Rule line.
186 #define RULE_FIELDS 10
189 ** Which fields are which on a Link line.
194 #define LINK_FIELDS 3
197 ** Which fields are which on a Leap line.
206 #define LEAP_FIELDS 7
216 static struct rule * rules;
217 static int nrules; /* number of rules */
219 static struct zone * zones;
220 static int nzones; /* number of zones */
223 const char * l_filename;
229 static struct link * links;
237 static struct lookup const * byword P((const char * string,
238 const struct lookup * lp));
240 static struct lookup const line_codes[] = {
248 static struct lookup const mon_names[] = {
249 { "January", TM_JANUARY },
250 { "February", TM_FEBRUARY },
251 { "March", TM_MARCH },
252 { "April", TM_APRIL },
256 { "August", TM_AUGUST },
257 { "September", TM_SEPTEMBER },
258 { "October", TM_OCTOBER },
259 { "November", TM_NOVEMBER },
260 { "December", TM_DECEMBER },
264 static struct lookup const wday_names[] = {
265 { "Sunday", TM_SUNDAY },
266 { "Monday", TM_MONDAY },
267 { "Tuesday", TM_TUESDAY },
268 { "Wednesday", TM_WEDNESDAY },
269 { "Thursday", TM_THURSDAY },
270 { "Friday", TM_FRIDAY },
271 { "Saturday", TM_SATURDAY },
275 static struct lookup const lasts[] = {
276 { "last-Sunday", TM_SUNDAY },
277 { "last-Monday", TM_MONDAY },
278 { "last-Tuesday", TM_TUESDAY },
279 { "last-Wednesday", TM_WEDNESDAY },
280 { "last-Thursday", TM_THURSDAY },
281 { "last-Friday", TM_FRIDAY },
282 { "last-Saturday", TM_SATURDAY },
286 static struct lookup const begin_years[] = {
287 { "minimum", YR_MINIMUM },
288 { "maximum", YR_MAXIMUM },
292 static struct lookup const end_years[] = {
293 { "minimum", YR_MINIMUM },
294 { "maximum", YR_MAXIMUM },
299 static struct lookup const leap_types[] = {
301 { "Stationary", FALSE },
305 static const int len_months[2][MONSPERYEAR] = {
306 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
307 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
310 static const int len_years[2] = {
311 DAYSPERNYEAR, DAYSPERLYEAR
314 static time_t ats[TZ_MAX_TIMES];
315 static unsigned char types[TZ_MAX_TIMES];
316 static long gmtoffs[TZ_MAX_TYPES];
317 static char isdsts[TZ_MAX_TYPES];
318 static unsigned char abbrinds[TZ_MAX_TYPES];
319 static char ttisstds[TZ_MAX_TYPES];
320 static char chars[TZ_MAX_CHARS];
321 static time_t trans[TZ_MAX_LEAPS];
322 static long corr[TZ_MAX_LEAPS];
323 static char roll[TZ_MAX_LEAPS];
326 ** Memory allocation.
334 (void) perror(progname);
335 (void) exit(EXIT_FAILURE);
340 #define emalloc(size) memcheck(imalloc(size))
341 #define erealloc(ptr, size) memcheck(irealloc(ptr, size))
342 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
343 #define ecatalloc(oldp, newp) memcheck(icatalloc(oldp, newp))
350 eats(name, num, rname, rnum)
351 const char * const name;
353 const char * const rname;
364 const char * const name;
367 eats(name, num, (char *) NULL, -1);
372 const char * const string;
375 ** Match the format of "cc" to allow sh users to
376 ** zic ... 2>&1 | error -t "*" -v
379 (void) fprintf(stderr, "\"%s\", line %d: %s",
380 filename, linenum, string);
381 if (rfilename != NULL)
382 (void) fprintf(stderr, " (rule from \"%s\", line %d)",
383 rfilename, rlinenum);
384 (void) fprintf(stderr, "\n");
391 (void) fprintf(stderr,
392 "%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ] \n\
393 \t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n",
395 (void) exit(EXIT_FAILURE);
398 static const char * psxrules;
399 static const char * lcltime;
400 static const char * directory;
401 static const char * leapsec;
402 static const char * yitcommand;
403 static int sflag = FALSE;
414 (void) umask(umask(022) | 022);
415 #endif /* defined unix */
417 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF)
422 if (directory == NULL)
425 (void) fprintf(stderr,
426 "%s: More than one -d option specified\n",
428 (void) exit(EXIT_FAILURE);
435 (void) fprintf(stderr,
436 "%s: More than one -l option specified\n",
438 (void) exit(EXIT_FAILURE);
442 if (psxrules == NULL)
445 (void) fprintf(stderr,
446 "%s: More than one -p option specified\n",
448 (void) exit(EXIT_FAILURE);
452 if (yitcommand == NULL)
455 (void) fprintf(stderr,
456 "%s: More than one -y option specified\n",
458 (void) exit(EXIT_FAILURE);
465 (void) fprintf(stderr,
466 "%s: More than one -L option specified\n",
468 (void) exit(EXIT_FAILURE);
478 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
479 usage(); /* usage message by request */
480 if (directory == NULL)
482 if (yitcommand == NULL)
483 yitcommand = "yearistype";
487 if (optind < argc && leapsec != NULL) {
492 zones = (struct zone *) emalloc(0);
493 rules = (struct rule *) emalloc(0);
494 links = (struct link *) emalloc(0);
495 for (i = optind; i < argc; ++i)
498 (void) exit(EXIT_FAILURE);
500 for (i = 0; i < nzones; i = j) {
502 ** Find the next non-continuation zone entry.
504 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
506 outzone(&zones[i], j - i);
511 for (i = 0; i < nlinks; ++i)
512 dolink(links[i].l_from, links[i].l_to);
514 dolink(lcltime, TZDEFAULT);
515 if (psxrules != NULL)
516 dolink(psxrules, TZDEFRULES);
517 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
521 dolink(fromfile, tofile)
522 const char * const fromfile;
523 const char * const tofile;
525 register char * fromname;
526 register char * toname;
528 if (fromfile[0] == '/')
531 fromname = ecpyalloc(directory);
532 fromname = ecatalloc(fromname, "/");
533 fromname = ecatalloc(fromname, fromfile);
535 if (tofile[0] == '/')
538 toname = ecpyalloc(directory);
539 toname = ecatalloc(toname, "/");
540 toname = ecatalloc(toname, tofile);
543 ** We get to be careful here since
544 ** there's a fair chance of root running us.
547 (void) remove(toname);
548 if (link(fromname, toname) != 0) {
549 if (mkdirs(toname) != 0)
550 (void) exit(EXIT_FAILURE);
551 if (link(fromname, toname) != 0) {
552 (void) fprintf(stderr, "%s: Can't link from %s to ",
554 (void) perror(toname);
555 (void) exit(EXIT_FAILURE);
558 if (fromname != fromfile)
560 if (toname != tofile)
569 for (bit = 1; bit > 0; bit <<= 1)
571 if (bit == 0) { /* time_t is an unsigned type */
574 max_time = ~(time_t) 0;
582 max_time = -max_time;
586 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
587 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
592 const char * const name;
594 register char * myname;
597 myname = ecpyalloc(name);
598 myname = ecatalloc(myname, "/.");
599 accres = access(myname, 0);
605 ** Associate sets of rules with zones.
609 ** Sort by rule name.
614 const genericptr_t cp1;
615 const genericptr_t cp2;
617 return strcmp(((struct rule *) cp1)->r_name,
618 ((struct rule *) cp2)->r_name);
624 register struct zone * zp;
625 register struct rule * rp;
626 register int base, out;
630 (void) qsort((genericptr_t) rules,
631 (qsort_size_t) nrules,
632 (qsort_size_t) sizeof *rules, rcomp);
633 for (i = 0; i < nzones; ++i) {
638 for (base = 0; base < nrules; base = out) {
640 for (out = base + 1; out < nrules; ++out)
641 if (strcmp(rp->r_name, rules[out].r_name) != 0)
643 for (i = 0; i < nzones; ++i) {
645 if (strcmp(zp->z_rule, rp->r_name) != 0)
648 zp->z_nrules = out - base;
651 for (i = 0; i < nzones; ++i) {
653 if (zp->z_nrules == 0) {
655 ** Maybe we have a local standard time offset.
657 eat(zp->z_filename, zp->z_linenum);
658 zp->z_stdoff = gethms(zp->z_rule, "unruly zone", TRUE);
660 ** Note, though, that if there's no rule,
661 ** a '%s' in the format is a bad thing.
663 if (strchr(zp->z_format, '%') != 0)
664 error("%s in ruleless zone");
668 (void) exit(EXIT_FAILURE);
676 register char ** fields;
678 register const struct lookup * lp;
679 register int nfields;
680 register int wantcont;
684 if (strcmp(name, "-") == 0) {
685 name = "standard input";
687 } else if ((fp = fopen(name, "r")) == NULL) {
688 (void) fprintf(stderr, "%s: Can't open ", progname);
690 (void) exit(EXIT_FAILURE);
693 for (num = 1; ; ++num) {
695 if (fgets(buf, (int) sizeof buf, fp) != buf)
697 cp = strchr(buf, '\n');
699 error("line too long");
700 (void) exit(EXIT_FAILURE);
703 fields = getfields(buf);
705 while (fields[nfields] != NULL) {
708 if (ciequal(fields[nfields], "-"))
709 fields[nfields] = nada;
714 } else if (wantcont) {
715 wantcont = inzcont(fields, nfields);
717 lp = byword(fields[0], line_codes);
719 error("input line of unknown type");
720 else switch ((int) (lp->l_value)) {
722 inrule(fields, nfields);
726 wantcont = inzone(fields, nfields);
729 inlink(fields, nfields);
734 (void) fprintf(stderr,
735 "%s: Leap line in non leap seconds file %s\n",
737 else inleap(fields, nfields);
740 default: /* "cannot happen" */
741 (void) fprintf(stderr,
742 "%s: panic: Invalid l_value %d\n",
743 progname, lp->l_value);
744 (void) exit(EXIT_FAILURE);
747 ifree((char *) fields);
750 (void) fprintf(stderr, "%s: Error reading ", progname);
751 (void) perror(filename);
752 (void) exit(EXIT_FAILURE);
754 if (fp != stdin && fclose(fp)) {
755 (void) fprintf(stderr, "%s: Error closing ", progname);
756 (void) perror(filename);
757 (void) exit(EXIT_FAILURE);
760 error("expected continuation line not found");
764 ** Convert a string of one of the forms
765 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
766 ** into a number of seconds.
767 ** A null string maps to zero.
768 ** Call error with errstring and return zero on errors.
772 gethms(string, errstring, signable)
774 const char * const errstring;
777 int hh, mm, ss, sign;
779 if (string == NULL || *string == '\0')
783 else if (*string == '-') {
787 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
789 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
791 else if (sscanf(string, scheck(string, "%d:%d:%d"),
792 &hh, &mm, &ss) != 3) {
796 if (hh < 0 || hh >= HOURSPERDAY ||
797 mm < 0 || mm >= MINSPERHOUR ||
798 ss < 0 || ss > SECSPERMIN) {
803 (eitol(hh * MINSPERHOUR + mm) *
804 eitol(SECSPERMIN) + eitol(ss));
808 inrule(fields, nfields)
809 register char ** const fields;
812 static struct rule r;
814 if (nfields != RULE_FIELDS) {
815 error("wrong number of fields on Rule line");
818 if (*fields[RF_NAME] == '\0') {
819 error("nameless rule");
822 r.r_filename = filename;
823 r.r_linenum = linenum;
824 r.r_stdoff = gethms(fields[RF_STDOFF], "invalid saved time", TRUE);
825 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
826 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
827 r.r_name = ecpyalloc(fields[RF_NAME]);
828 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
829 rules = (struct rule *) erealloc((char *) rules,
830 (int) ((nrules + 1) * sizeof *rules));
835 inzone(fields, nfields)
836 register char ** const fields;
842 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
843 error("wrong number of fields on Zone line");
846 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
847 buf = erealloc(buf, 132 + strlen(TZDEFAULT));
849 "\"Zone %s\" line and -l option are mutually exclusive",
854 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
855 buf = erealloc(buf, 132 + strlen(TZDEFRULES));
857 "\"Zone %s\" line and -p option are mutually exclusive",
862 for (i = 0; i < nzones; ++i)
863 if (zones[i].z_name != NULL &&
864 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
865 buf = erealloc(buf, 132 +
866 strlen(fields[ZF_NAME]) +
867 strlen(zones[i].z_filename));
869 "duplicate zone name %s (file \"%s\", line %d)",
876 return inzsub(fields, nfields, FALSE);
880 inzcont(fields, nfields)
881 register char ** const fields;
884 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
885 error("wrong number of fields on Zone continuation line");
888 return inzsub(fields, nfields, TRUE);
892 inzsub(fields, nfields, iscont)
893 register char ** const fields;
898 static struct zone z;
899 register int i_gmtoff, i_rule, i_format;
900 register int i_untilyear, i_untilmonth;
901 register int i_untilday, i_untiltime;
902 register int hasuntil;
905 i_gmtoff = ZFC_GMTOFF;
907 i_format = ZFC_FORMAT;
908 i_untilyear = ZFC_TILYEAR;
909 i_untilmonth = ZFC_TILMONTH;
910 i_untilday = ZFC_TILDAY;
911 i_untiltime = ZFC_TILTIME;
914 i_gmtoff = ZF_GMTOFF;
916 i_format = ZF_FORMAT;
917 i_untilyear = ZF_TILYEAR;
918 i_untilmonth = ZF_TILMONTH;
919 i_untilday = ZF_TILDAY;
920 i_untiltime = ZF_TILTIME;
921 z.z_name = ecpyalloc(fields[ZF_NAME]);
923 z.z_filename = filename;
924 z.z_linenum = linenum;
925 z.z_gmtoff = gethms(fields[i_gmtoff], "invalid GMT offset", TRUE);
926 if ((cp = strchr(fields[i_format], '%')) != 0) {
927 if (*++cp != 's' || strchr(cp, '%') != 0) {
928 error("invalid abbreviation format");
932 z.z_rule = ecpyalloc(fields[i_rule]);
933 z.z_format = ecpyalloc(fields[i_format]);
934 hasuntil = nfields > i_untilyear;
936 z.z_untilrule.r_filename = filename;
937 z.z_untilrule.r_linenum = linenum;
938 rulesub(&z.z_untilrule,
942 (nfields > i_untilmonth) ?
943 fields[i_untilmonth] : "Jan",
944 (nfields > i_untilday) ? fields[i_untilday] : "1",
945 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
946 z.z_untiltime = rpytime(&z.z_untilrule,
947 z.z_untilrule.r_loyear);
948 if (iscont && nzones > 0 &&
949 z.z_untiltime > min_time &&
950 z.z_untiltime < max_time &&
951 zones[nzones - 1].z_untiltime > min_time &&
952 zones[nzones - 1].z_untiltime < max_time &&
953 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
954 error("Zone continuation line end time is not after end time of previous line");
958 zones = (struct zone *) erealloc((char *) zones,
959 (int) ((nzones + 1) * sizeof *zones));
962 ** If there was an UNTIL field on this line,
963 ** there's more information about the zone on the next line.
969 inleap(fields, nfields)
970 register char ** const fields;
973 register const char * cp;
974 register const struct lookup * lp;
976 int year, month, day;
980 if (nfields != LEAP_FIELDS) {
981 error("wrong number of fields on Leap line");
985 cp = fields[LP_YEAR];
986 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
990 error("invalid leaping year");
996 i = len_years[isleap(j)];
1000 i = -len_years[isleap(j)];
1002 dayoff = oadd(dayoff, eitol(i));
1004 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1005 error("invalid month name");
1008 month = lp->l_value;
1010 while (j != month) {
1011 i = len_months[isleap(year)][j];
1012 dayoff = oadd(dayoff, eitol(i));
1015 cp = fields[LP_DAY];
1016 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1017 day <= 0 || day > len_months[isleap(year)][month]) {
1018 error("invalid day of month");
1021 dayoff = oadd(dayoff, eitol(day - 1));
1022 if (dayoff < 0 && !tt_signed) {
1023 error("time before zero");
1026 t = (time_t) dayoff * SECSPERDAY;
1028 ** Cheap overflow check.
1030 if (t / SECSPERDAY != dayoff) {
1031 error("time overflow");
1034 tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
1035 cp = fields[LP_CORR];
1037 register int positive;
1040 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1043 } else if (strcmp(cp, "--") == 0) {
1046 } else if (strcmp(cp, "+") == 0) {
1049 } else if (strcmp(cp, "++") == 0) {
1053 error("illegal CORRECTION field on Leap line");
1056 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1057 error("illegal Rolling/Stationary field on Leap line");
1060 leapadd(tadd(t, tod), positive, lp->l_value, count);
1065 inlink(fields, nfields)
1066 register char ** const fields;
1071 if (nfields != LINK_FIELDS) {
1072 error("wrong number of fields on Link line");
1075 if (*fields[LF_FROM] == '\0') {
1076 error("blank FROM field on Link line");
1079 if (*fields[LF_TO] == '\0') {
1080 error("blank TO field on Link line");
1083 l.l_filename = filename;
1084 l.l_linenum = linenum;
1085 l.l_from = ecpyalloc(fields[LF_FROM]);
1086 l.l_to = ecpyalloc(fields[LF_TO]);
1087 links = (struct link *) erealloc((char *) links,
1088 (int) ((nlinks + 1) * sizeof *links));
1089 links[nlinks++] = l;
1093 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1094 register struct rule * const rp;
1095 char * const loyearp;
1096 char * const hiyearp;
1098 char * const monthp;
1102 register struct lookup const * lp;
1105 if ((lp = byword(monthp, mon_names)) == NULL) {
1106 error("invalid month name");
1109 rp->r_month = lp->l_value;
1110 rp->r_todisstd = FALSE;
1113 cp += strlen(cp) - 1;
1114 switch (lowerit(*cp)) {
1116 rp->r_todisstd = TRUE;
1120 rp->r_todisstd = FALSE;
1125 rp->r_tod = gethms(timep, "invalid time of day", FALSE);
1130 if ((lp = byword(cp, begin_years)) != NULL) switch ((int) lp->l_value) {
1132 rp->r_loyear = min_year;
1135 rp->r_loyear = max_year;
1137 default: /* "cannot happen" */
1138 (void) fprintf(stderr,
1139 "%s: panic: Invalid l_value %d\n",
1140 progname, lp->l_value);
1141 (void) exit(EXIT_FAILURE);
1142 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1143 error("invalid starting year");
1147 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1149 rp->r_hiyear = min_year;
1152 rp->r_hiyear = max_year;
1155 rp->r_hiyear = rp->r_loyear;
1157 default: /* "cannot happen" */
1158 (void) fprintf(stderr,
1159 "%s: panic: Invalid l_value %d\n",
1160 progname, lp->l_value);
1161 (void) exit(EXIT_FAILURE);
1162 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1163 error("invalid ending year");
1166 if (rp->r_loyear > rp->r_hiyear) {
1167 error("starting year greater than ending year");
1171 rp->r_yrtype = NULL;
1173 if (rp->r_loyear == rp->r_hiyear) {
1174 error("typed single year");
1177 rp->r_yrtype = ecpyalloc(typep);
1181 ** Accept things such as:
1187 if ((lp = byword(dayp, lasts)) != NULL) {
1188 rp->r_dycode = DC_DOWLEQ;
1189 rp->r_wday = lp->l_value;
1190 rp->r_dayofmonth = len_months[1][rp->r_month];
1192 if ((cp = strchr(dayp, '<')) != 0)
1193 rp->r_dycode = DC_DOWLEQ;
1194 else if ((cp = strchr(dayp, '>')) != 0)
1195 rp->r_dycode = DC_DOWGEQ;
1198 rp->r_dycode = DC_DOM;
1200 if (rp->r_dycode != DC_DOM) {
1203 error("invalid day of month");
1206 if ((lp = byword(dayp, wday_names)) == NULL) {
1207 error("invalid weekday name");
1210 rp->r_wday = lp->l_value;
1212 if (sscanf(cp, scheck(cp, "%d"), &rp->r_dayofmonth) != 1 ||
1213 rp->r_dayofmonth <= 0 ||
1214 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1215 error("invalid day of month");
1227 register long shift;
1229 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1230 buf[i] = val >> shift;
1241 (void) fwrite((genericptr_t) buf,
1242 (fwrite_size_t) sizeof buf,
1243 (fwrite_size_t) 1, fp);
1248 const char * const name;
1252 static char * fullname;
1253 static struct tzhead tzh;
1255 fullname = erealloc(fullname,
1256 strlen(directory) + 1 + strlen(name) + 1);
1257 (void) sprintf(fullname, "%s/%s", directory, name);
1258 if ((fp = fopen(fullname, "wb")) == NULL) {
1259 if (mkdirs(fullname) != 0)
1260 (void) exit(EXIT_FAILURE);
1261 if ((fp = fopen(fullname, "wb")) == NULL) {
1262 (void) fprintf(stderr, "%s: Can't create ", progname);
1263 (void) perror(fullname);
1264 (void) exit(EXIT_FAILURE);
1267 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1268 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1269 convert(eitol(timecnt), tzh.tzh_timecnt);
1270 convert(eitol(typecnt), tzh.tzh_typecnt);
1271 convert(eitol(charcnt), tzh.tzh_charcnt);
1272 (void) fwrite((genericptr_t) &tzh,
1273 (fwrite_size_t) sizeof tzh,
1274 (fwrite_size_t) 1, fp);
1275 for (i = 0; i < timecnt; ++i) {
1278 if (ats[i] >= trans[j]) {
1279 ats[i] = tadd(ats[i], corr[j]);
1282 puttzcode((long) ats[i], fp);
1285 (void) fwrite((genericptr_t) types,
1286 (fwrite_size_t) sizeof types[0],
1287 (fwrite_size_t) timecnt, fp);
1288 for (i = 0; i < typecnt; ++i) {
1289 puttzcode((long) gmtoffs[i], fp);
1290 (void) putc(isdsts[i], fp);
1291 (void) putc(abbrinds[i], fp);
1294 (void) fwrite((genericptr_t) chars,
1295 (fwrite_size_t) sizeof chars[0],
1296 (fwrite_size_t) charcnt, fp);
1297 for (i = 0; i < leapcnt; ++i) {
1299 if (timecnt == 0 || trans[i] < ats[0]) {
1302 if (++j >= typecnt) {
1308 while (j < timecnt && trans[i] >= ats[j])
1312 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1313 } else puttzcode((long) trans[i], fp);
1314 puttzcode((long) corr[i], fp);
1316 for (i = 0; i < typecnt; ++i)
1317 (void) putc(ttisstds[i], fp);
1318 if (ferror(fp) || fclose(fp)) {
1319 (void) fprintf(stderr, "%s: Write error on ", progname);
1320 (void) perror(fullname);
1321 (void) exit(EXIT_FAILURE);
1326 outzone(zpfirst, zonecount)
1327 const struct zone * const zpfirst;
1328 const int zonecount;
1330 register const struct zone * zp;
1331 register struct rule * rp;
1333 register int usestart, useuntil;
1334 register time_t starttime, untiltime;
1335 register long gmtoff;
1336 register long stdoff;
1338 register long startoff;
1339 register int startisdst;
1340 register int startttisstd;
1342 char startbuf[BUFSIZ];
1345 ** Now. . .finally. . .generate some useful data!
1351 ** A guess that may well be corrected later.
1355 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1356 ** for noting the need to unconditionally initialize startttisstd.
1358 startttisstd = FALSE;
1361 #endif /* defined lint */
1362 for (i = 0; i < zonecount; ++i) {
1364 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1365 useuntil = i < (zonecount - 1);
1366 if (useuntil && zp->z_untiltime <= min_time)
1368 gmtoff = zp->z_gmtoff;
1369 eat(zp->z_filename, zp->z_linenum);
1371 if (zp->z_nrules == 0) {
1372 stdoff = zp->z_stdoff;
1373 (void) strcpy(startbuf, zp->z_format);
1374 type = addtype(oadd(zp->z_gmtoff, stdoff),
1375 startbuf, stdoff != 0, startttisstd);
1377 addtt(starttime, type);
1378 else if (stdoff != 0)
1379 addtt(min_time, type);
1380 } else for (year = min_year; year <= max_year; ++year) {
1381 if (useuntil && year > zp->z_untilrule.r_hiyear)
1384 ** Mark which rules to do in the current year.
1385 ** For those to do, calculate rpytime(rp, year);
1387 for (j = 0; j < zp->z_nrules; ++j) {
1388 rp = &zp->z_rules[j];
1389 eats(zp->z_filename, zp->z_linenum,
1390 rp->r_filename, rp->r_linenum);
1391 rp->r_todo = year >= rp->r_loyear &&
1392 year <= rp->r_hiyear &&
1393 yearistype(year, rp->r_yrtype);
1395 rp->r_temp = rpytime(rp, year);
1399 register time_t jtime, ktime;
1400 register long offset;
1405 ** Turn untiltime into GMT
1406 ** assuming the current gmtoff and
1409 untiltime = tadd(zp->z_untiltime,
1411 if (!zp->z_untilrule.r_todisstd)
1412 untiltime = tadd(untiltime,
1416 ** Find the rule (of those to do, if any)
1417 ** that takes effect earliest in the year.
1422 #endif /* defined lint */
1423 for (j = 0; j < zp->z_nrules; ++j) {
1424 rp = &zp->z_rules[j];
1427 eats(zp->z_filename, zp->z_linenum,
1428 rp->r_filename, rp->r_linenum);
1430 if (!rp->r_todisstd)
1431 offset = oadd(offset, stdoff);
1433 if (jtime == min_time ||
1436 jtime = tadd(jtime, -offset);
1437 if (k < 0 || jtime < ktime) {
1443 break; /* go on to next year */
1444 rp = &zp->z_rules[k];
1446 if (useuntil && ktime >= untiltime)
1449 if (ktime < starttime) {
1450 stdoff = rp->r_stdoff;
1451 startoff = oadd(zp->z_gmtoff,
1453 (void) sprintf(startbuf, zp->z_format,
1455 startisdst = rp->r_stdoff != 0;
1459 if (ktime != starttime) {
1460 if (startisdst < 0 &&
1462 (zp - 1)->z_gmtoff) {
1463 type = (timecnt == 0) ? 0 :
1465 startoff = oadd(gmtoffs[type],
1466 -(zp - 1)->z_gmtoff);
1467 startisdst = startoff != 0;
1468 startoff = oadd(startoff,
1470 (void) strcpy(startbuf,
1471 &chars[abbrinds[type]]);
1473 if (startisdst >= 0)
1474 addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd));
1477 eats(zp->z_filename, zp->z_linenum,
1478 rp->r_filename, rp->r_linenum);
1479 (void) sprintf(buf, zp->z_format,
1481 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1482 type = addtype(offset, buf, rp->r_stdoff != 0,
1485 stdoff = rp->r_stdoff;
1489 ** Now we may get to set starttime for the next zone line.
1492 starttime = tadd(zp->z_untiltime, -gmtoff);
1493 startttisstd = zp->z_untilrule.r_todisstd;
1495 starttime = tadd(starttime, -stdoff);
1498 writezone(zpfirst->z_name);
1502 addtt(starttime, type)
1503 const time_t starttime;
1506 if (timecnt != 0 && type == types[timecnt - 1])
1507 return; /* easy enough! */
1508 if (timecnt == 0 && type == 0 && isdsts[0] == 0)
1509 return; /* handled by default rule */
1510 if (timecnt >= TZ_MAX_TIMES) {
1511 error("too many transitions?!");
1512 (void) exit(EXIT_FAILURE);
1514 ats[timecnt] = starttime;
1515 types[timecnt] = type;
1520 addtype(gmtoff, abbr, isdst, ttisstd)
1522 const char * const abbr;
1529 ** See if there's already an entry for this zone type.
1530 ** If so, just return its index.
1532 for (i = 0; i < typecnt; ++i) {
1533 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1534 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1535 ttisstd == ttisstds[i])
1539 ** There isn't one; add a new one, unless there are already too
1542 if (typecnt >= TZ_MAX_TYPES) {
1543 error("too many local time types");
1544 (void) exit(EXIT_FAILURE);
1546 gmtoffs[i] = gmtoff;
1548 ttisstds[i] = ttisstd;
1550 for (j = 0; j < charcnt; ++j)
1551 if (strcmp(&chars[j], abbr) == 0)
1561 leapadd(t, positive, rolling, count)
1569 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1570 error("too many leap seconds");
1571 (void) exit(EXIT_FAILURE);
1573 for (i = 0; i < leapcnt; ++i)
1574 if (t <= trans[i]) {
1575 if (t == trans[i]) {
1576 error("repeated leap second moment");
1577 (void) exit(EXIT_FAILURE);
1582 for (j = leapcnt; j > i; --j) {
1583 trans[j] = trans[j - 1];
1584 corr[j] = corr[j - 1];
1585 roll[j] = roll[j - 1];
1588 corr[i] = positive ? 1L : eitol(-count);
1591 } while (positive && --count != 0);
1598 register long last = 0;
1601 ** propagate leap seconds forward
1603 for (i = 0; i < leapcnt; ++i) {
1604 trans[i] = tadd(trans[i], last);
1605 last = corr[i] += last;
1610 yearistype(year, type)
1612 const char * const type;
1617 if (type == NULL || *type == '\0')
1619 if (strcmp(type, "uspres") == 0)
1620 return (year % 4) == 0;
1621 if (strcmp(type, "nonpres") == 0)
1622 return (year % 4) != 0;
1623 buf = erealloc(buf, 132 + strlen(yitcommand) + strlen(type));
1624 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1625 result = system(buf);
1628 if (result == (1 << 8))
1630 error("Wild result from command execution");
1631 (void) fprintf(stderr, "%s: command was '%s', result was %d\n",
1632 progname, buf, result);
1634 (void) exit(EXIT_FAILURE);
1641 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1645 ciequal(ap, bp) /* case-insensitive equality */
1646 register const char * ap;
1647 register const char * bp;
1649 while (lowerit(*ap) == lowerit(*bp++))
1657 register const char * abbr;
1658 register const char * word;
1660 if (lowerit(*abbr) != lowerit(*word))
1663 while (*++abbr != '\0')
1664 do if (*word == '\0')
1666 while (lowerit(*word++) != lowerit(*abbr));
1670 static const struct lookup *
1672 register const char * const word;
1673 register const struct lookup * const table;
1675 register const struct lookup * foundlp;
1676 register const struct lookup * lp;
1678 if (word == NULL || table == NULL)
1681 ** Look for exact match.
1683 for (lp = table; lp->l_word != NULL; ++lp)
1684 if (ciequal(word, lp->l_word))
1687 ** Look for inexact match.
1690 for (lp = table; lp->l_word != NULL; ++lp)
1691 if (itsabbr(word, lp->l_word))
1692 if (foundlp == NULL)
1694 else return NULL; /* multiple inexact matches */
1703 register char ** array;
1708 array = (char **) emalloc((int) ((strlen(cp) + 1) * sizeof *array));
1711 while (isascii(*cp) && isspace(*cp))
1713 if (*cp == '\0' || *cp == '#')
1715 array[nsubs++] = dp = cp;
1717 if ((*dp = *cp++) != '"')
1719 else while ((*dp = *cp++) != '"')
1722 else error("Odd number of quotation marks");
1723 } while (*cp != '\0' && *cp != '#' &&
1724 (!isascii(*cp) || !isspace(*cp)));
1725 if (isascii(*cp) && isspace(*cp))
1729 array[nsubs] = NULL;
1741 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1742 error("time overflow");
1743 (void) exit(EXIT_FAILURE);
1755 if (t1 == max_time && t2 > 0)
1757 if (t1 == min_time && t2 < 0)
1760 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1761 error("time overflow");
1762 (void) exit(EXIT_FAILURE);
1768 ** Given a rule, and a year, compute the date - in seconds since January 1,
1769 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1773 rpytime(rp, wantedy)
1774 register const struct rule * const rp;
1775 register const int wantedy;
1777 register int y, m, i;
1778 register long dayoff; /* with a nod to Margaret O. */
1784 while (wantedy != y) {
1786 i = len_years[isleap(y)];
1790 i = -len_years[isleap(y)];
1792 dayoff = oadd(dayoff, eitol(i));
1794 while (m != rp->r_month) {
1795 i = len_months[isleap(y)][m];
1796 dayoff = oadd(dayoff, eitol(i));
1799 i = rp->r_dayofmonth;
1800 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
1801 if (rp->r_dycode == DC_DOWLEQ)
1804 error("use of 2/29 in non leap-year");
1805 (void) exit(EXIT_FAILURE);
1809 dayoff = oadd(dayoff, eitol(i));
1810 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
1813 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
1814 wday = eitol(EPOCH_WDAY);
1816 ** Don't trust mod of negative numbers.
1819 wday = (wday + dayoff) % LDAYSPERWEEK;
1821 wday -= ((-dayoff) % LDAYSPERWEEK);
1823 wday += LDAYSPERWEEK;
1825 while (wday != eitol(rp->r_wday))
1826 if (rp->r_dycode == DC_DOWGEQ) {
1827 dayoff = oadd(dayoff, (long) 1);
1828 if (++wday >= LDAYSPERWEEK)
1832 dayoff = oadd(dayoff, (long) -1);
1834 wday = LDAYSPERWEEK - 1;
1837 if (i < 0 || i >= len_months[isleap(y)][m]) {
1838 error("no day in month matches rule");
1839 (void) exit(EXIT_FAILURE);
1842 if (dayoff < 0 && !tt_signed)
1844 t = (time_t) dayoff * SECSPERDAY;
1846 ** Cheap overflow check.
1848 if (t / SECSPERDAY != dayoff)
1849 return (dayoff > 0) ? max_time : min_time;
1850 return tadd(t, rp->r_tod);
1855 const char * const string;
1859 i = strlen(string) + 1;
1860 if (charcnt + i > TZ_MAX_CHARS) {
1861 error("too many, or too long, time zone abbreviations");
1862 (void) exit(EXIT_FAILURE);
1864 (void) strcpy(&chars[charcnt], string);
1865 charcnt += eitol(i);
1874 if ((cp = name) == NULL || *cp == '\0')
1876 while ((cp = strchr(cp + 1, '/')) != 0) {
1880 ** MS-DOS drive specifier?
1882 if (strlen(name) == 2 && isascii(name[0]) &&
1883 isalpha(name[0]) && name[1] == ':') {
1887 #endif /* !defined unix */
1888 if (!itsdir(name)) {
1890 ** It doesn't seem to exist, so we try to create it.
1892 if (emkdir(name, 0755) != 0) {
1893 (void) fprintf(stderr,
1894 "%s: Can't create directory ",
1896 (void) perror(name);
1912 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
1913 (void) fprintf(stderr,
1914 "%s: %d did not sign extend correctly\n",
1916 (void) exit(EXIT_FAILURE);
1922 ** UNIX is a registered trademark of AT&T.