3 static char elsieid[] = "@(#)zic.c 7.50";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
10 #include "sys/stat.h" /* for umask manifest constants */
11 #endif /* defined unix */
14 const char * r_filename;
18 int r_loyear; /* for example, 1986 */
19 int r_hiyear; /* for example, 1986 */
20 const char * r_yrtype;
22 int r_month; /* 0..11 */
24 int r_dycode; /* see below */
28 long r_tod; /* time from midnight */
29 int r_todisstd; /* above is standard time if TRUE */
30 /* or wall clock time if FALSE */
31 int r_todisgmt; /* above is GMT if TRUE */
32 /* or local time if FALSE */
33 long r_stdoff; /* offset from standard time */
34 const char * r_abbrvar; /* variable part of abbreviation */
36 int r_todo; /* a rule to do (used in outzone) */
37 time_t r_temp; /* used in outzone */
41 ** r_dycode r_dayofmonth r_wday
44 #define DC_DOM 0 /* 1..31 */ /* unused */
45 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
46 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
49 const char * z_filename;
55 const char * z_format;
59 struct rule * z_rules;
62 struct rule z_untilrule;
66 extern int getopt P((int argc, char * const argv[],
67 const char * options));
68 extern char * icatalloc P((char * old, const char * new));
69 extern char * icpyalloc P((const char * string));
70 extern void ifree P((char * p));
71 extern char * imalloc P((int n));
72 extern void * irealloc P((void * old, int n));
73 extern int link P((const char * fromname, const char * toname));
76 extern char * scheck P((const char * string, const char * format));
78 static void addtt P((time_t starttime, int type));
79 static int addtype P((long gmtoff, const char * abbr, int isdst,
80 int ttisstd, int ttisgmt));
81 static void leapadd P((time_t t, int positive, int rolling, int count));
82 static void adjleap P((void));
83 static void associate P((void));
84 static int ciequal P((const char * ap, const char * bp));
85 static void convert P((long val, char * buf));
86 static void dolink P((const char * fromfile, const char * tofile));
87 static void doabbr P((char * abbr, const char * format,
88 const char * letters, int isdst));
89 static void eat P((const char * name, int num));
90 static void eats P((const char * name, int num,
91 const char * rname, int rnum));
92 static long eitol P((int i));
93 static void error P((const char * message));
94 static char ** getfields P((char * buf));
95 static long gethms P((const char * string, const char * errstrng,
97 static void infile P((const char * filename));
98 static void inleap P((char ** fields, int nfields));
99 static void inlink P((char ** fields, int nfields));
100 static void inrule P((char ** fields, int nfields));
101 static int inzcont P((char ** fields, int nfields));
102 static int inzone P((char ** fields, int nfields));
103 static int inzsub P((char ** fields, int nfields, int iscont));
104 static int itsabbr P((const char * abbr, const char * word));
105 static int itsdir P((const char * name));
106 static int lowerit P((int c));
107 static char * memcheck P((char * tocheck));
108 static int mkdirs P((char * filename));
109 static void newabbr P((const char * abbr));
110 static long oadd P((long t1, long t2));
111 static void outzone P((const struct zone * zp, int ntzones));
112 static void puttzcode P((long code, FILE * fp));
113 static int rcomp P((const void * leftp, const void * rightp));
114 static time_t rpytime P((const struct rule * rp, int wantedy));
115 static void rulesub P((struct rule * rp,
116 const char * loyearp, const char * hiyearp,
117 const char * typep, const char * monthp,
118 const char * dayp, const char * timep));
119 static void setboundaries P((void));
120 static time_t tadd P((time_t t1, long t2));
121 static void usage P((void));
122 static void writezone P((const char * name));
123 static int yearistype P((int year, const char * type));
127 static const char * filename;
131 static time_t max_time;
134 static time_t min_time;
137 static const char * rfilename;
139 static const char * progname;
142 static int tt_signed;
154 ** Which fields are which on a Zone line.
162 #define ZF_TILMONTH 6
165 #define ZONE_MINFIELDS 5
166 #define ZONE_MAXFIELDS 9
169 ** Which fields are which on a Zone continuation line.
175 #define ZFC_TILYEAR 3
176 #define ZFC_TILMONTH 4
178 #define ZFC_TILTIME 6
179 #define ZONEC_MINFIELDS 3
180 #define ZONEC_MAXFIELDS 7
183 ** Which files are which on a Rule line.
195 #define RULE_FIELDS 10
198 ** Which fields are which on a Link line.
203 #define LINK_FIELDS 3
206 ** Which fields are which on a Leap line.
215 #define LEAP_FIELDS 7
225 static struct rule * rules;
226 static int nrules; /* number of rules */
228 static struct zone * zones;
229 static int nzones; /* number of zones */
232 const char * l_filename;
238 static struct link * links;
246 static struct lookup const * byword P((const char * string,
247 const struct lookup * lp));
249 static struct lookup const line_codes[] = {
257 static struct lookup const mon_names[] = {
258 { "January", TM_JANUARY },
259 { "February", TM_FEBRUARY },
260 { "March", TM_MARCH },
261 { "April", TM_APRIL },
265 { "August", TM_AUGUST },
266 { "September", TM_SEPTEMBER },
267 { "October", TM_OCTOBER },
268 { "November", TM_NOVEMBER },
269 { "December", TM_DECEMBER },
273 static struct lookup const wday_names[] = {
274 { "Sunday", TM_SUNDAY },
275 { "Monday", TM_MONDAY },
276 { "Tuesday", TM_TUESDAY },
277 { "Wednesday", TM_WEDNESDAY },
278 { "Thursday", TM_THURSDAY },
279 { "Friday", TM_FRIDAY },
280 { "Saturday", TM_SATURDAY },
284 static struct lookup const lasts[] = {
285 { "last-Sunday", TM_SUNDAY },
286 { "last-Monday", TM_MONDAY },
287 { "last-Tuesday", TM_TUESDAY },
288 { "last-Wednesday", TM_WEDNESDAY },
289 { "last-Thursday", TM_THURSDAY },
290 { "last-Friday", TM_FRIDAY },
291 { "last-Saturday", TM_SATURDAY },
295 static struct lookup const begin_years[] = {
296 { "minimum", YR_MINIMUM },
297 { "maximum", YR_MAXIMUM },
301 static struct lookup const end_years[] = {
302 { "minimum", YR_MINIMUM },
303 { "maximum", YR_MAXIMUM },
308 static struct lookup const leap_types[] = {
310 { "Stationary", FALSE },
314 static const int len_months[2][MONSPERYEAR] = {
315 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
316 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
319 static const int len_years[2] = {
320 DAYSPERNYEAR, DAYSPERLYEAR
323 static time_t ats[TZ_MAX_TIMES];
324 static unsigned char types[TZ_MAX_TIMES];
325 static long gmtoffs[TZ_MAX_TYPES];
326 static char isdsts[TZ_MAX_TYPES];
327 static unsigned char abbrinds[TZ_MAX_TYPES];
328 static char ttisstds[TZ_MAX_TYPES];
329 static char ttisgmts[TZ_MAX_TYPES];
330 static char chars[TZ_MAX_CHARS];
331 static time_t trans[TZ_MAX_LEAPS];
332 static long corr[TZ_MAX_LEAPS];
333 static char roll[TZ_MAX_LEAPS];
336 ** Memory allocation.
344 (void) perror(progname);
345 (void) exit(EXIT_FAILURE);
350 #define emalloc(size) memcheck(imalloc(size))
351 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
352 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
353 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
360 eats(name, num, rname, rnum)
361 const char * const name;
363 const char * const rname;
374 const char * const name;
377 eats(name, num, (char *) NULL, -1);
382 const char * const string;
385 ** Match the format of "cc" to allow sh users to
386 ** zic ... 2>&1 | error -t "*" -v
389 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
390 filename, linenum, string);
391 if (rfilename != NULL)
392 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
393 rfilename, rlinenum);
394 (void) fprintf(stderr, "\n");
401 (void) fprintf(stderr, _("%s: usage is %s \
402 [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\
403 \t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
405 (void) exit(EXIT_FAILURE);
408 static const char * psxrules;
409 static const char * lcltime;
410 static const char * directory;
411 static const char * leapsec;
412 static const char * yitcommand;
413 static int sflag = FALSE;
425 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
426 #endif /* defined unix */
428 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF)
433 if (directory == NULL)
436 (void) fprintf(stderr,
437 _("%s: More than one -d option specified\n"),
439 (void) exit(EXIT_FAILURE);
446 (void) fprintf(stderr,
447 _("%s: More than one -l option specified\n"),
449 (void) exit(EXIT_FAILURE);
453 if (psxrules == NULL)
456 (void) fprintf(stderr,
457 _("%s: More than one -p option specified\n"),
459 (void) exit(EXIT_FAILURE);
463 if (yitcommand == NULL)
466 (void) fprintf(stderr,
467 _("%s: More than one -y option specified\n"),
469 (void) exit(EXIT_FAILURE);
476 (void) fprintf(stderr,
477 _("%s: More than one -L option specified\n"),
479 (void) exit(EXIT_FAILURE);
489 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
490 usage(); /* usage message by request */
491 if (directory == NULL)
493 if (yitcommand == NULL)
494 yitcommand = "yearistype";
498 if (optind < argc && leapsec != NULL) {
503 for (i = optind; i < argc; ++i)
506 (void) exit(EXIT_FAILURE);
508 for (i = 0; i < nzones; i = j) {
510 ** Find the next non-continuation zone entry.
512 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
514 outzone(&zones[i], j - i);
519 for (i = 0; i < nlinks; ++i)
520 dolink(links[i].l_from, links[i].l_to);
522 dolink(lcltime, TZDEFAULT);
523 if (psxrules != NULL)
524 dolink(psxrules, TZDEFRULES);
525 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
529 dolink(fromfile, tofile)
530 const char * const fromfile;
531 const char * const tofile;
533 register char * fromname;
534 register char * toname;
536 if (fromfile[0] == '/')
537 fromname = ecpyalloc(fromfile);
539 fromname = ecpyalloc(directory);
540 fromname = ecatalloc(fromname, "/");
541 fromname = ecatalloc(fromname, fromfile);
543 if (tofile[0] == '/')
544 toname = ecpyalloc(tofile);
546 toname = ecpyalloc(directory);
547 toname = ecatalloc(toname, "/");
548 toname = ecatalloc(toname, tofile);
551 ** We get to be careful here since
552 ** there's a fair chance of root running us.
555 (void) remove(toname);
556 if (link(fromname, toname) != 0) {
557 if (mkdirs(toname) != 0)
558 (void) exit(EXIT_FAILURE);
559 if (link(fromname, toname) != 0) {
560 (void) fprintf(stderr, _("%s: Can't link from %s to "),
562 (void) perror(toname);
563 (void) exit(EXIT_FAILURE);
571 setboundaries P((void))
576 for (bit = 1; bit > 0; bit <<= 1)
578 if (bit == 0) { /* time_t is an unsigned type */
581 max_time = ~(time_t) 0;
589 max_time = -max_time;
593 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
594 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
596 for (bii = 1; bii > 0; bii <<= 1)
604 const char * const name;
606 register char * myname;
609 myname = ecpyalloc(name);
610 myname = ecatalloc(myname, "/.");
611 accres = access(myname, F_OK);
617 ** Associate sets of rules with zones.
621 ** Sort by rule name.
629 return strcmp(((const struct rule *) cp1)->r_name,
630 ((const struct rule *) cp2)->r_name);
636 register struct zone * zp;
637 register struct rule * rp;
638 register int base, out;
642 (void) qsort((void *) rules, (size_t) nrules,
643 (size_t) sizeof *rules, rcomp);
644 for (i = 0; i < nzones; ++i) {
649 for (base = 0; base < nrules; base = out) {
651 for (out = base + 1; out < nrules; ++out)
652 if (strcmp(rp->r_name, rules[out].r_name) != 0)
654 for (i = 0; i < nzones; ++i) {
656 if (strcmp(zp->z_rule, rp->r_name) != 0)
659 zp->z_nrules = out - base;
662 for (i = 0; i < nzones; ++i) {
664 if (zp->z_nrules == 0) {
666 ** Maybe we have a local standard time offset.
668 eat(zp->z_filename, zp->z_linenum);
669 zp->z_stdoff = gethms(zp->z_rule, "unruly zone", TRUE);
671 ** Note, though, that if there's no rule,
672 ** a '%s' in the format is a bad thing.
674 if (strchr(zp->z_format, '%') != 0)
675 error(_("%s in ruleless zone"));
679 (void) exit(EXIT_FAILURE);
687 register char ** fields;
689 register const struct lookup * lp;
690 register int nfields;
691 register int wantcont;
695 if (strcmp(name, "-") == 0) {
696 name = _("standard input");
698 } else if ((fp = fopen(name, "r")) == NULL) {
699 (void) fprintf(stderr, _("%s: Can't open "), progname);
701 (void) exit(EXIT_FAILURE);
704 for (num = 1; ; ++num) {
706 if (fgets(buf, (int) sizeof buf, fp) != buf)
708 cp = strchr(buf, '\n');
710 error(_("line too long"));
711 (void) exit(EXIT_FAILURE);
714 fields = getfields(buf);
716 while (fields[nfields] != NULL) {
719 if (ciequal(fields[nfields], "-"))
720 fields[nfields] = &nada;
725 } else if (wantcont) {
726 wantcont = inzcont(fields, nfields);
728 lp = byword(fields[0], line_codes);
730 error(_("input line of unknown type"));
731 else switch ((int) (lp->l_value)) {
733 inrule(fields, nfields);
737 wantcont = inzone(fields, nfields);
740 inlink(fields, nfields);
745 (void) fprintf(stderr,
746 _("%s: Leap line in non leap seconds file %s\n"),
748 else inleap(fields, nfields);
751 default: /* "cannot happen" */
752 (void) fprintf(stderr,
753 _("%s: panic: Invalid l_value %d\n"),
754 progname, lp->l_value);
755 (void) exit(EXIT_FAILURE);
758 ifree((char *) fields);
761 (void) fprintf(stderr, _("%s: Error reading "), progname);
762 (void) perror(filename);
763 (void) exit(EXIT_FAILURE);
765 if (fp != stdin && fclose(fp)) {
766 (void) fprintf(stderr, _("%s: Error closing "), progname);
767 (void) perror(filename);
768 (void) exit(EXIT_FAILURE);
771 error(_("expected continuation line not found"));
775 ** Convert a string of one of the forms
776 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
777 ** into a number of seconds.
778 ** A null string maps to zero.
779 ** Call error with errstring and return zero on errors.
783 gethms(string, errstring, signable)
785 const char * const errstring;
788 int hh, mm, ss, sign;
790 if (string == NULL || *string == '\0')
794 else if (*string == '-') {
798 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
800 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
802 else if (sscanf(string, scheck(string, "%d:%d:%d"),
803 &hh, &mm, &ss) != 3) {
807 if (hh < 0 || hh >= HOURSPERDAY ||
808 mm < 0 || mm >= MINSPERHOUR ||
809 ss < 0 || ss > SECSPERMIN) {
814 (eitol(hh * MINSPERHOUR + mm) *
815 eitol(SECSPERMIN) + eitol(ss));
819 inrule(fields, nfields)
820 register char ** const fields;
823 static struct rule r;
825 if (nfields != RULE_FIELDS) {
826 error(_("wrong number of fields on Rule line"));
829 if (*fields[RF_NAME] == '\0') {
830 error(_("nameless rule"));
833 r.r_filename = filename;
834 r.r_linenum = linenum;
835 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
836 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
837 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
838 r.r_name = ecpyalloc(fields[RF_NAME]);
839 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
840 rules = (struct rule *) (void *) erealloc((char *) rules,
841 (int) ((nrules + 1) * sizeof *rules));
846 inzone(fields, nfields)
847 register char ** const fields;
853 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
854 error(_("wrong number of fields on Zone line"));
857 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
858 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
860 _("\"Zone %s\" line and -l option are mutually exclusive"),
865 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
866 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
868 _("\"Zone %s\" line and -p option are mutually exclusive"),
873 for (i = 0; i < nzones; ++i)
874 if (zones[i].z_name != NULL &&
875 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
876 buf = erealloc(buf, (int) (132 +
877 strlen(fields[ZF_NAME]) +
878 strlen(zones[i].z_filename)));
880 _("duplicate zone name %s (file \"%s\", line %d)"),
887 return inzsub(fields, nfields, FALSE);
891 inzcont(fields, nfields)
892 register char ** const fields;
895 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
896 error(_("wrong number of fields on Zone continuation line"));
899 return inzsub(fields, nfields, TRUE);
903 inzsub(fields, nfields, iscont)
904 register char ** const fields;
909 static struct zone z;
910 register int i_gmtoff, i_rule, i_format;
911 register int i_untilyear, i_untilmonth;
912 register int i_untilday, i_untiltime;
913 register int hasuntil;
916 i_gmtoff = ZFC_GMTOFF;
918 i_format = ZFC_FORMAT;
919 i_untilyear = ZFC_TILYEAR;
920 i_untilmonth = ZFC_TILMONTH;
921 i_untilday = ZFC_TILDAY;
922 i_untiltime = ZFC_TILTIME;
925 i_gmtoff = ZF_GMTOFF;
927 i_format = ZF_FORMAT;
928 i_untilyear = ZF_TILYEAR;
929 i_untilmonth = ZF_TILMONTH;
930 i_untilday = ZF_TILDAY;
931 i_untiltime = ZF_TILTIME;
932 z.z_name = ecpyalloc(fields[ZF_NAME]);
934 z.z_filename = filename;
935 z.z_linenum = linenum;
936 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid GMT offset"), TRUE);
937 if ((cp = strchr(fields[i_format], '%')) != 0) {
938 if (*++cp != 's' || strchr(cp, '%') != 0) {
939 error(_("invalid abbreviation format"));
943 z.z_rule = ecpyalloc(fields[i_rule]);
944 z.z_format = ecpyalloc(fields[i_format]);
945 hasuntil = nfields > i_untilyear;
947 z.z_untilrule.r_filename = filename;
948 z.z_untilrule.r_linenum = linenum;
949 rulesub(&z.z_untilrule,
953 (nfields > i_untilmonth) ?
954 fields[i_untilmonth] : "Jan",
955 (nfields > i_untilday) ? fields[i_untilday] : "1",
956 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
957 z.z_untiltime = rpytime(&z.z_untilrule,
958 z.z_untilrule.r_loyear);
959 if (iscont && nzones > 0 &&
960 z.z_untiltime > min_time &&
961 z.z_untiltime < max_time &&
962 zones[nzones - 1].z_untiltime > min_time &&
963 zones[nzones - 1].z_untiltime < max_time &&
964 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
965 error(_("Zone continuation line end time is \
966 not after end time of previous line"));
970 zones = (struct zone *) (void *) erealloc((char *) zones,
971 (int) ((nzones + 1) * sizeof *zones));
974 ** If there was an UNTIL field on this line,
975 ** there's more information about the zone on the next line.
981 inleap(fields, nfields)
982 register char ** const fields;
985 register const char * cp;
986 register const struct lookup * lp;
988 int year, month, day;
992 if (nfields != LEAP_FIELDS) {
993 error(_("wrong number of fields on Leap line"));
997 cp = fields[LP_YEAR];
998 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1002 error(_("invalid leaping year"));
1008 i = len_years[isleap(j)];
1012 i = -len_years[isleap(j)];
1014 dayoff = oadd(dayoff, eitol(i));
1016 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1017 error(_("invalid month name"));
1020 month = lp->l_value;
1022 while (j != month) {
1023 i = len_months[isleap(year)][j];
1024 dayoff = oadd(dayoff, eitol(i));
1027 cp = fields[LP_DAY];
1028 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1029 day <= 0 || day > len_months[isleap(year)][month]) {
1030 error(_("invalid day of month"));
1033 dayoff = oadd(dayoff, eitol(day - 1));
1034 if (dayoff < 0 && !tt_signed) {
1035 error(_("time before zero"));
1038 t = (time_t) dayoff * SECSPERDAY;
1040 ** Cheap overflow check.
1042 if (t / SECSPERDAY != dayoff) {
1043 error(_("time overflow"));
1046 tod = gethms(fields[LP_TIME], "invalid time of day", FALSE);
1047 cp = fields[LP_CORR];
1049 register int positive;
1052 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1055 } else if (strcmp(cp, "--") == 0) {
1058 } else if (strcmp(cp, "+") == 0) {
1061 } else if (strcmp(cp, "++") == 0) {
1065 error(_("illegal CORRECTION field on Leap line"));
1068 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1069 error(_("illegal Rolling/Stationary field on Leap line"));
1072 leapadd(tadd(t, tod), positive, lp->l_value, count);
1077 inlink(fields, nfields)
1078 register char ** const fields;
1083 if (nfields != LINK_FIELDS) {
1084 error(_("wrong number of fields on Link line"));
1087 if (*fields[LF_FROM] == '\0') {
1088 error(_("blank FROM field on Link line"));
1091 if (*fields[LF_TO] == '\0') {
1092 error(_("blank TO field on Link line"));
1095 l.l_filename = filename;
1096 l.l_linenum = linenum;
1097 l.l_from = ecpyalloc(fields[LF_FROM]);
1098 l.l_to = ecpyalloc(fields[LF_TO]);
1099 links = (struct link *) (void *) erealloc((char *) links,
1100 (int) ((nlinks + 1) * sizeof *links));
1101 links[nlinks++] = l;
1105 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1106 register struct rule * const rp;
1107 const char * const loyearp;
1108 const char * const hiyearp;
1109 const char * const typep;
1110 const char * const monthp;
1111 const char * const dayp;
1112 const char * const timep;
1114 register const struct lookup * lp;
1115 register const char * cp;
1119 if ((lp = byword(monthp, mon_names)) == NULL) {
1120 error(_("invalid month name"));
1123 rp->r_month = lp->l_value;
1124 rp->r_todisstd = FALSE;
1125 rp->r_todisgmt = FALSE;
1126 dp = ecpyalloc(timep);
1128 ep = dp + strlen(dp) - 1;
1129 switch (lowerit(*ep)) {
1130 case 's': /* Standard */
1131 rp->r_todisstd = TRUE;
1132 rp->r_todisgmt = FALSE;
1135 case 'w': /* Wall */
1136 rp->r_todisstd = FALSE;
1137 rp->r_todisgmt = FALSE;
1139 case 'g': /* Greenwich */
1140 case 'u': /* Universal */
1141 case 'z': /* Zulu */
1142 rp->r_todisstd = TRUE;
1143 rp->r_todisgmt = TRUE;
1148 rp->r_tod = gethms(dp, "invalid time of day", FALSE);
1154 lp = byword(cp, begin_years);
1155 if (lp != NULL) switch ((int) lp->l_value) {
1157 rp->r_loyear = min_int;
1160 rp->r_loyear = max_int;
1162 default: /* "cannot happen" */
1163 (void) fprintf(stderr,
1164 _("%s: panic: Invalid l_value %d\n"),
1165 progname, lp->l_value);
1166 (void) exit(EXIT_FAILURE);
1167 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1168 error(_("invalid starting year"));
1172 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1174 rp->r_hiyear = min_int;
1177 rp->r_hiyear = max_int;
1180 rp->r_hiyear = rp->r_loyear;
1182 default: /* "cannot happen" */
1183 (void) fprintf(stderr,
1184 _("%s: panic: Invalid l_value %d\n"),
1185 progname, lp->l_value);
1186 (void) exit(EXIT_FAILURE);
1187 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1188 error(_("invalid ending year"));
1191 if (rp->r_loyear > rp->r_hiyear) {
1192 error(_("starting year greater than ending year"));
1196 rp->r_yrtype = NULL;
1198 if (rp->r_loyear == rp->r_hiyear) {
1199 error(_("typed single year"));
1202 rp->r_yrtype = ecpyalloc(typep);
1206 ** Accept things such as:
1212 dp = ecpyalloc(dayp);
1213 if ((lp = byword(dp, lasts)) != NULL) {
1214 rp->r_dycode = DC_DOWLEQ;
1215 rp->r_wday = lp->l_value;
1216 rp->r_dayofmonth = len_months[1][rp->r_month];
1218 if ((ep = strchr(dp, '<')) != 0)
1219 rp->r_dycode = DC_DOWLEQ;
1220 else if ((ep = strchr(dp, '>')) != 0)
1221 rp->r_dycode = DC_DOWGEQ;
1224 rp->r_dycode = DC_DOM;
1226 if (rp->r_dycode != DC_DOM) {
1229 error(_("invalid day of month"));
1233 if ((lp = byword(dp, wday_names)) == NULL) {
1234 error(_("invalid weekday name"));
1238 rp->r_wday = lp->l_value;
1240 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1241 rp->r_dayofmonth <= 0 ||
1242 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1243 error(_("invalid day of month"));
1257 register long shift;
1259 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1260 buf[i] = val >> shift;
1271 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1276 const char * const name;
1280 static char * fullname;
1281 static struct tzhead tzh;
1283 fullname = erealloc(fullname,
1284 (int) (strlen(directory) + 1 + strlen(name) + 1));
1285 (void) sprintf(fullname, "%s/%s", directory, name);
1286 if ((fp = fopen(fullname, "wb")) == NULL) {
1287 if (mkdirs(fullname) != 0)
1288 (void) exit(EXIT_FAILURE);
1289 if ((fp = fopen(fullname, "wb")) == NULL) {
1290 (void) fprintf(stderr, _("%s: Can't create "),
1292 (void) perror(fullname);
1293 (void) exit(EXIT_FAILURE);
1296 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1297 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1298 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1299 convert(eitol(timecnt), tzh.tzh_timecnt);
1300 convert(eitol(typecnt), tzh.tzh_typecnt);
1301 convert(eitol(charcnt), tzh.tzh_charcnt);
1302 #define DO(field) (void) fwrite((void *) tzh.field, \
1303 (size_t) sizeof tzh.field, (size_t) 1, fp)
1312 for (i = 0; i < timecnt; ++i) {
1315 if (ats[i] >= trans[j]) {
1316 ats[i] = tadd(ats[i], corr[j]);
1319 puttzcode((long) ats[i], fp);
1322 (void) fwrite((void *) types, (size_t) sizeof types[0],
1323 (size_t) timecnt, fp);
1324 for (i = 0; i < typecnt; ++i) {
1325 puttzcode((long) gmtoffs[i], fp);
1326 (void) putc(isdsts[i], fp);
1327 (void) putc(abbrinds[i], fp);
1330 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1331 (size_t) charcnt, fp);
1332 for (i = 0; i < leapcnt; ++i) {
1334 if (timecnt == 0 || trans[i] < ats[0]) {
1337 if (++j >= typecnt) {
1343 while (j < timecnt && trans[i] >= ats[j])
1347 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1348 } else puttzcode((long) trans[i], fp);
1349 puttzcode((long) corr[i], fp);
1351 for (i = 0; i < typecnt; ++i)
1352 (void) putc(ttisstds[i], fp);
1353 for (i = 0; i < typecnt; ++i)
1354 (void) putc(ttisgmts[i], fp);
1355 if (ferror(fp) || fclose(fp)) {
1356 (void) fprintf(stderr, _("%s: Write error on "), progname);
1357 (void) perror(fullname);
1358 (void) exit(EXIT_FAILURE);
1363 doabbr(abbr, format, letters, isdst)
1365 const char * const format;
1366 const char * const letters;
1369 if (strchr(format, '/') == NULL) {
1370 if (letters == NULL)
1371 (void) strcpy(abbr, format);
1372 else (void) sprintf(abbr, format, letters);
1374 (void) strcpy(abbr, strchr(format, '/') + 1);
1376 (void) strcpy(abbr, format);
1377 *strchr(abbr, '/') = '\0';
1382 outzone(zpfirst, zonecount)
1383 const struct zone * const zpfirst;
1384 const int zonecount;
1386 register const struct zone * zp;
1387 register struct rule * rp;
1389 register int usestart, useuntil;
1390 register time_t starttime, untiltime;
1391 register long gmtoff;
1392 register long stdoff;
1394 register long startoff;
1395 register int startisdst;
1396 register int startttisstd;
1397 register int startttisgmt;
1399 char startbuf[BUFSIZ];
1401 INITIALIZE(untiltime);
1402 INITIALIZE(starttime);
1403 INITIALIZE(startoff);
1405 ** Now. . .finally. . .generate some useful data!
1411 ** A guess that may well be corrected later.
1415 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1416 ** for noting the need to unconditionally initialize startttisstd.
1418 startttisstd = FALSE;
1419 startttisgmt = FALSE;
1420 for (i = 0; i < zonecount; ++i) {
1422 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1423 useuntil = i < (zonecount - 1);
1424 if (useuntil && zp->z_untiltime <= min_time)
1426 gmtoff = zp->z_gmtoff;
1427 eat(zp->z_filename, zp->z_linenum);
1429 if (zp->z_nrules == 0) {
1430 stdoff = zp->z_stdoff;
1431 doabbr(startbuf, zp->z_format,
1432 (char *) NULL, stdoff != 0);
1433 type = addtype(oadd(zp->z_gmtoff, stdoff),
1434 startbuf, stdoff != 0, startttisstd,
1437 addtt(starttime, type);
1438 else if (stdoff != 0)
1439 addtt(min_time, type);
1440 } else for (year = min_year; year <= max_year; ++year) {
1441 if (useuntil && year > zp->z_untilrule.r_hiyear)
1444 ** Mark which rules to do in the current year.
1445 ** For those to do, calculate rpytime(rp, year);
1447 for (j = 0; j < zp->z_nrules; ++j) {
1448 rp = &zp->z_rules[j];
1449 eats(zp->z_filename, zp->z_linenum,
1450 rp->r_filename, rp->r_linenum);
1451 rp->r_todo = year >= rp->r_loyear &&
1452 year <= rp->r_hiyear &&
1453 yearistype(year, rp->r_yrtype);
1455 rp->r_temp = rpytime(rp, year);
1459 register time_t jtime, ktime;
1460 register long offset;
1466 ** Turn untiltime into GMT
1467 ** assuming the current gmtoff and
1470 untiltime = zp->z_untiltime;
1471 if (!zp->z_untilrule.r_todisgmt)
1472 untiltime = tadd(untiltime,
1474 if (!zp->z_untilrule.r_todisstd)
1475 untiltime = tadd(untiltime,
1479 ** Find the rule (of those to do, if any)
1480 ** that takes effect earliest in the year.
1483 for (j = 0; j < zp->z_nrules; ++j) {
1484 rp = &zp->z_rules[j];
1487 eats(zp->z_filename, zp->z_linenum,
1488 rp->r_filename, rp->r_linenum);
1489 offset = rp->r_todisgmt ? 0 : gmtoff;
1490 if (!rp->r_todisstd)
1491 offset = oadd(offset, stdoff);
1493 if (jtime == min_time ||
1496 jtime = tadd(jtime, -offset);
1497 if (k < 0 || jtime < ktime) {
1503 break; /* go on to next year */
1504 rp = &zp->z_rules[k];
1506 if (useuntil && ktime >= untiltime)
1509 if (ktime < starttime) {
1510 stdoff = rp->r_stdoff;
1511 startoff = oadd(zp->z_gmtoff,
1513 doabbr(startbuf, zp->z_format,
1516 startisdst = rp->r_stdoff != 0;
1520 if (ktime != starttime) {
1521 if (startisdst < 0 &&
1523 (zp - 1)->z_gmtoff) {
1524 type = (timecnt == 0) ? 0 :
1526 startoff = oadd(gmtoffs[type],
1527 -(zp - 1)->z_gmtoff);
1528 startisdst = startoff != 0;
1529 startoff = oadd(startoff,
1531 (void) strcpy(startbuf,
1532 &chars[abbrinds[type]]);
1534 if (startisdst >= 0)
1535 addtt(starttime, addtype(startoff, startbuf, startisdst, startttisstd,
1539 eats(zp->z_filename, zp->z_linenum,
1540 rp->r_filename, rp->r_linenum);
1541 doabbr(buf, zp->z_format, rp->r_abbrvar,
1543 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1544 type = addtype(offset, buf, rp->r_stdoff != 0,
1545 rp->r_todisstd, rp->r_todisgmt);
1547 stdoff = rp->r_stdoff;
1551 ** Now we may get to set starttime for the next zone line.
1554 starttime = tadd(zp->z_untiltime, -gmtoff);
1555 startttisstd = zp->z_untilrule.r_todisstd;
1556 startttisgmt = zp->z_untilrule.r_todisgmt;
1558 starttime = tadd(starttime, -stdoff);
1561 writezone(zpfirst->z_name);
1565 addtt(starttime, type)
1566 const time_t starttime;
1569 if (timecnt != 0 && type == types[timecnt - 1])
1570 return; /* easy enough! */
1571 if (timecnt == 0 && type == 0 && isdsts[0] == 0)
1572 return; /* handled by default rule */
1573 if (timecnt >= TZ_MAX_TIMES) {
1574 error(_("too many transitions?!"));
1575 (void) exit(EXIT_FAILURE);
1577 ats[timecnt] = starttime;
1578 types[timecnt] = type;
1583 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1585 const char * const abbr;
1593 ** See if there's already an entry for this zone type.
1594 ** If so, just return its index.
1596 for (i = 0; i < typecnt; ++i) {
1597 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1598 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1599 ttisstd == ttisstds[i] &&
1600 ttisgmt == ttisgmts[i])
1604 ** There isn't one; add a new one, unless there are already too
1607 if (typecnt >= TZ_MAX_TYPES) {
1608 error(_("too many local time types"));
1609 (void) exit(EXIT_FAILURE);
1611 gmtoffs[i] = gmtoff;
1613 ttisstds[i] = ttisstd;
1614 ttisgmts[i] = ttisgmt;
1616 for (j = 0; j < charcnt; ++j)
1617 if (strcmp(&chars[j], abbr) == 0)
1627 leapadd(t, positive, rolling, count)
1635 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1636 error(_("too many leap seconds"));
1637 (void) exit(EXIT_FAILURE);
1639 for (i = 0; i < leapcnt; ++i)
1640 if (t <= trans[i]) {
1641 if (t == trans[i]) {
1642 error(_("repeated leap second moment"));
1643 (void) exit(EXIT_FAILURE);
1648 for (j = leapcnt; j > i; --j) {
1649 trans[j] = trans[j - 1];
1650 corr[j] = corr[j - 1];
1651 roll[j] = roll[j - 1];
1654 corr[i] = positive ? 1L : eitol(-count);
1657 } while (positive && --count != 0);
1664 register long last = 0;
1667 ** propagate leap seconds forward
1669 for (i = 0; i < leapcnt; ++i) {
1670 trans[i] = tadd(trans[i], last);
1671 last = corr[i] += last;
1676 yearistype(year, type)
1678 const char * const type;
1683 if (type == NULL || *type == '\0')
1685 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1686 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1687 result = system(buf);
1690 if (result == (1 << 8))
1692 error(_("Wild result from command execution"));
1693 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1694 progname, buf, result);
1696 (void) exit(EXIT_FAILURE);
1703 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1707 ciequal(ap, bp) /* case-insensitive equality */
1708 register const char * ap;
1709 register const char * bp;
1711 while (lowerit(*ap) == lowerit(*bp++))
1719 register const char * abbr;
1720 register const char * word;
1722 if (lowerit(*abbr) != lowerit(*word))
1725 while (*++abbr != '\0')
1726 do if (*word == '\0')
1728 while (lowerit(*word++) != lowerit(*abbr));
1732 static const struct lookup *
1734 register const char * const word;
1735 register const struct lookup * const table;
1737 register const struct lookup * foundlp;
1738 register const struct lookup * lp;
1740 if (word == NULL || table == NULL)
1743 ** Look for exact match.
1745 for (lp = table; lp->l_word != NULL; ++lp)
1746 if (ciequal(word, lp->l_word))
1749 ** Look for inexact match.
1752 for (lp = table; lp->l_word != NULL; ++lp)
1753 if (itsabbr(word, lp->l_word))
1754 if (foundlp == NULL)
1756 else return NULL; /* multiple inexact matches */
1765 register char ** array;
1770 array = (char **) (void *)
1771 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
1774 while (isascii(*cp) && isspace(*cp))
1776 if (*cp == '\0' || *cp == '#')
1778 array[nsubs++] = dp = cp;
1780 if ((*dp = *cp++) != '"')
1782 else while ((*dp = *cp++) != '"')
1785 else error(_("Odd number of quotation marks"));
1786 } while (*cp != '\0' && *cp != '#' &&
1787 (!isascii(*cp) || !isspace(*cp)));
1788 if (isascii(*cp) && isspace(*cp))
1792 array[nsubs] = NULL;
1804 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1805 error(_("time overflow"));
1806 (void) exit(EXIT_FAILURE);
1818 if (t1 == max_time && t2 > 0)
1820 if (t1 == min_time && t2 < 0)
1823 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1824 error(_("time overflow"));
1825 (void) exit(EXIT_FAILURE);
1831 ** Given a rule, and a year, compute the date - in seconds since January 1,
1832 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1836 rpytime(rp, wantedy)
1837 register const struct rule * const rp;
1838 register const int wantedy;
1840 register int y, m, i;
1841 register long dayoff; /* with a nod to Margaret O. */
1844 if (wantedy == min_int)
1846 if (wantedy == max_int)
1851 while (wantedy != y) {
1853 i = len_years[isleap(y)];
1857 i = -len_years[isleap(y)];
1859 dayoff = oadd(dayoff, eitol(i));
1861 while (m != rp->r_month) {
1862 i = len_months[isleap(y)][m];
1863 dayoff = oadd(dayoff, eitol(i));
1866 i = rp->r_dayofmonth;
1867 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
1868 if (rp->r_dycode == DC_DOWLEQ)
1871 error(_("use of 2/29 in non leap-year"));
1872 (void) exit(EXIT_FAILURE);
1876 dayoff = oadd(dayoff, eitol(i));
1877 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
1880 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
1881 wday = eitol(EPOCH_WDAY);
1883 ** Don't trust mod of negative numbers.
1886 wday = (wday + dayoff) % LDAYSPERWEEK;
1888 wday -= ((-dayoff) % LDAYSPERWEEK);
1890 wday += LDAYSPERWEEK;
1892 while (wday != eitol(rp->r_wday))
1893 if (rp->r_dycode == DC_DOWGEQ) {
1894 dayoff = oadd(dayoff, (long) 1);
1895 if (++wday >= LDAYSPERWEEK)
1899 dayoff = oadd(dayoff, (long) -1);
1901 wday = LDAYSPERWEEK - 1;
1904 if (i < 0 || i >= len_months[isleap(y)][m]) {
1905 error(_("no day in month matches rule"));
1906 (void) exit(EXIT_FAILURE);
1909 if (dayoff < 0 && !tt_signed)
1911 t = (time_t) dayoff * SECSPERDAY;
1913 ** Cheap overflow check.
1915 if (t / SECSPERDAY != dayoff)
1916 return (dayoff > 0) ? max_time : min_time;
1917 return tadd(t, rp->r_tod);
1922 const char * const string;
1926 i = strlen(string) + 1;
1927 if (charcnt + i > TZ_MAX_CHARS) {
1928 error(_("too many, or too long, time zone abbreviations"));
1929 (void) exit(EXIT_FAILURE);
1931 (void) strcpy(&chars[charcnt], string);
1932 charcnt += eitol(i);
1937 char * const argname;
1939 register char * name;
1942 if (argname == NULL || *argname == '\0')
1944 cp = name = ecpyalloc(argname);
1945 while ((cp = strchr(cp + 1, '/')) != 0) {
1949 ** DOS drive specifier?
1951 if (strlen(name) == 2 && isascii(name[0]) &&
1952 isalpha(name[0]) && name[1] == ':') {
1956 #endif /* !defined unix */
1957 if (!itsdir(name)) {
1959 ** It doesn't seem to exist, so we try to create it.
1961 if (mkdir(name, 0755) != 0) {
1962 (void) fprintf(stderr,
1963 _("%s: Can't create directory "),
1965 (void) perror(name);
1983 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
1984 (void) fprintf(stderr,
1985 _("%s: %d did not sign extend correctly\n"),
1987 (void) exit(EXIT_FAILURE);
1993 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.