1 static char elsieid[] = "@(#)zic.c 7.128";
4 ** Regardless of the type of time_t, we do our work using this type.
13 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
14 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
15 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
21 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
23 #define MKDIR_UMASK 0755
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.
40 const char * r_filename;
44 int r_loyear; /* for example, 1986 */
45 int r_hiyear; /* for example, 1986 */
46 const char * r_yrtype;
48 int r_month; /* 0..11 */
50 int r_dycode; /* see below */
54 long r_tod; /* time from midnight */
55 int r_todisstd; /* above is standard time if TRUE */
56 /* or wall clock time if FALSE */
57 int r_todisgmt; /* above is GMT if TRUE */
58 /* or local time if FALSE */
59 long r_stdoff; /* offset from standard time */
60 const char * r_abbrvar; /* variable part of abbreviation */
62 int r_todo; /* a rule to do (used in outzone) */
63 zic_t r_temp; /* used in outzone */
67 ** r_dycode r_dayofmonth r_wday
70 #define DC_DOM 0 /* 1..31 */ /* unused */
71 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
72 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
75 const char * z_filename;
81 const char * z_format;
85 struct rule * z_rules;
88 struct rule z_untilrule;
92 extern int getopt P((int argc, char * const argv[],
93 const char * options));
94 extern int link P((const char * fromname, const char * toname));
98 static void addtt P((zic_t starttime, int type));
99 static int addtype P((long gmtoff, const char * abbr, int isdst,
100 int ttisstd, int ttisgmt));
101 static void leapadd P((zic_t t, int positive, int rolling, int count));
102 static void adjleap P((void));
103 static void associate P((void));
104 static int ciequal P((const char * ap, const char * bp));
105 static void convert P((long val, char * buf));
106 static void dolink P((const char * fromfile, const char * tofile));
107 static void doabbr P((char * abbr, const char * format,
108 const char * letters, int isdst));
109 static void eat P((const char * name, int num));
110 static void eats P((const char * name, int num,
111 const char * rname, int rnum));
112 static long eitol P((int i));
113 static void error P((const char * message));
114 static char ** getfields P((char * buf));
115 static long gethms P((const char * string, const char * errstrng,
117 static void infile P((const char * filename));
118 static void inleap P((char ** fields, int nfields));
119 static void inlink P((char ** fields, int nfields));
120 static void inrule P((char ** fields, int nfields));
121 static int inzcont P((char ** fields, int nfields));
122 static int inzone P((char ** fields, int nfields));
123 static int inzsub P((char ** fields, int nfields, int iscont));
124 static int itsabbr P((const char * abbr, const char * word));
125 static int itsdir P((const char * name));
126 static int lowerit P((int c));
127 static char * memcheck P((char * tocheck));
128 static int mkdirs P((char * filename));
129 static void newabbr P((const char * abbr));
130 static long oadd P((long t1, long t2));
131 static void outzone P((const struct zone * zp, int ntzones));
132 static void puttzcode P((long code, FILE * fp));
133 static int rcomp P((const void * leftp, const void * rightp));
134 static zic_t rpytime P((const struct rule * rp, int wantedy));
135 static void rulesub P((struct rule * rp,
136 const char * loyearp, const char * hiyearp,
137 const char * typep, const char * monthp,
138 const char * dayp, const char * timep));
139 static void setboundaries P((void));
140 static zic_t tadd P((zic_t t1, long t2));
141 static void usage P((void));
142 static void writezone P((const char * name));
143 static int yearistype P((int year, const char * type));
146 static char * strerror P((int));
147 #endif /* !HAVE_STRERROR */
151 static const char * filename;
154 static zic_t max_time;
156 static int max_year_representable;
157 static zic_t min_time;
159 static int min_year_representable;
161 static const char * rfilename;
163 static const char * progname;
177 ** Which fields are which on a Zone line.
185 #define ZF_TILMONTH 6
188 #define ZONE_MINFIELDS 5
189 #define ZONE_MAXFIELDS 9
192 ** Which fields are which on a Zone continuation line.
198 #define ZFC_TILYEAR 3
199 #define ZFC_TILMONTH 4
201 #define ZFC_TILTIME 6
202 #define ZONEC_MINFIELDS 3
203 #define ZONEC_MAXFIELDS 7
206 ** Which files are which on a Rule line.
218 #define RULE_FIELDS 10
221 ** Which fields are which on a Link line.
226 #define LINK_FIELDS 3
229 ** Which fields are which on a Leap line.
238 #define LEAP_FIELDS 7
248 static struct rule * rules;
249 static int nrules; /* number of rules */
251 static struct zone * zones;
252 static int nzones; /* number of zones */
255 const char * l_filename;
261 static struct link * links;
269 static struct lookup const * byword P((const char * string,
270 const struct lookup * lp));
272 static struct lookup const line_codes[] = {
280 static struct lookup const mon_names[] = {
281 { "January", TM_JANUARY },
282 { "February", TM_FEBRUARY },
283 { "March", TM_MARCH },
284 { "April", TM_APRIL },
288 { "August", TM_AUGUST },
289 { "September", TM_SEPTEMBER },
290 { "October", TM_OCTOBER },
291 { "November", TM_NOVEMBER },
292 { "December", TM_DECEMBER },
296 static struct lookup const wday_names[] = {
297 { "Sunday", TM_SUNDAY },
298 { "Monday", TM_MONDAY },
299 { "Tuesday", TM_TUESDAY },
300 { "Wednesday", TM_WEDNESDAY },
301 { "Thursday", TM_THURSDAY },
302 { "Friday", TM_FRIDAY },
303 { "Saturday", TM_SATURDAY },
307 static struct lookup const lasts[] = {
308 { "last-Sunday", TM_SUNDAY },
309 { "last-Monday", TM_MONDAY },
310 { "last-Tuesday", TM_TUESDAY },
311 { "last-Wednesday", TM_WEDNESDAY },
312 { "last-Thursday", TM_THURSDAY },
313 { "last-Friday", TM_FRIDAY },
314 { "last-Saturday", TM_SATURDAY },
318 static struct lookup const begin_years[] = {
319 { "minimum", YR_MINIMUM },
320 { "maximum", YR_MAXIMUM },
324 static struct lookup const end_years[] = {
325 { "minimum", YR_MINIMUM },
326 { "maximum", YR_MAXIMUM },
331 static struct lookup const leap_types[] = {
333 { "Stationary", FALSE },
337 static const int len_months[2][MONSPERYEAR] = {
338 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
339 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
342 static const int len_years[2] = {
343 DAYSPERNYEAR, DAYSPERLYEAR
346 static struct attype {
349 } attypes[TZ_MAX_TIMES];
350 static long gmtoffs[TZ_MAX_TYPES];
351 static char isdsts[TZ_MAX_TYPES];
352 static unsigned char abbrinds[TZ_MAX_TYPES];
353 static char ttisstds[TZ_MAX_TYPES];
354 static char ttisgmts[TZ_MAX_TYPES];
355 static char chars[TZ_MAX_CHARS];
356 static zic_t trans[TZ_MAX_LEAPS];
357 static long corr[TZ_MAX_LEAPS];
358 static char roll[TZ_MAX_LEAPS];
361 ** Memory allocation.
369 const char *e = strerror(errno);
371 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
378 #define emalloc(size) memcheck(imalloc(size))
379 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
380 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
381 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
392 extern char * sys_errlist[];
395 return (errnum > 0 && errnum <= sys_nerr) ?
396 sys_errlist[errnum] : _("Unknown system error");
398 #endif /* !HAVE_STRERROR */
401 eats(name, num, rname, rnum)
402 const char * const name;
404 const char * const rname;
415 const char * const name;
418 eats(name, num, (char *) NULL, -1);
423 const char * const string;
426 ** Match the format of "cc" to allow sh users to
427 ** zic ... 2>&1 | error -t "*" -v
430 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
431 filename, linenum, string);
432 if (rfilename != NULL)
433 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
434 rfilename, rlinenum);
435 (void) fprintf(stderr, "\n");
441 const char * const string;
445 cp = ecpyalloc(_("warning: "));
446 cp = ecatalloc(cp, string);
455 (void) fprintf(stderr, _("%s: usage is %s \
456 [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
457 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
462 static const char * psxrules;
463 static const char * lcltime;
464 static const char * directory;
465 static const char * leapsec;
466 static const char * yitcommand;
467 static int sflag = FALSE;
479 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
480 #endif /* defined unix */
482 (void) setlocale(LC_ALL, "");
484 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
485 #endif /* defined TEXTDOMAINDIR */
486 (void) textdomain(TZ_DOMAIN);
487 #endif /* HAVE_GETTEXT */
489 for (i = 1; i < argc; ++i)
490 if (strcmp(argv[i], "--version") == 0) {
491 (void) printf("%s\n", elsieid);
494 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
499 if (directory == NULL)
502 (void) fprintf(stderr,
503 _("%s: More than one -d option specified\n"),
512 (void) fprintf(stderr,
513 _("%s: More than one -l option specified\n"),
519 if (psxrules == NULL)
522 (void) fprintf(stderr,
523 _("%s: More than one -p option specified\n"),
529 if (yitcommand == NULL)
532 (void) fprintf(stderr,
533 _("%s: More than one -y option specified\n"),
542 (void) fprintf(stderr,
543 _("%s: More than one -L option specified\n"),
555 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
556 usage(); /* usage message by request */
557 if (directory == NULL)
559 if (yitcommand == NULL)
560 yitcommand = "yearistype";
564 if (optind < argc && leapsec != NULL) {
569 for (i = optind; i < argc; ++i)
574 for (i = 0; i < nzones; i = j) {
576 ** Find the next non-continuation zone entry.
578 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
580 outzone(&zones[i], j - i);
585 for (i = 0; i < nlinks; ++i) {
586 eat(links[i].l_filename, links[i].l_linenum);
587 dolink(links[i].l_from, links[i].l_to);
589 for (j = 0; j < nlinks; ++j)
590 if (strcmp(links[i].l_to,
591 links[j].l_from) == 0)
592 warning(_("link to link"));
594 if (lcltime != NULL) {
595 eat("command line", 1);
596 dolink(lcltime, TZDEFAULT);
598 if (psxrules != NULL) {
599 eat("command line", 1);
600 dolink(psxrules, TZDEFRULES);
602 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
606 dolink(fromfile, tofile)
607 const char * const fromfile;
608 const char * const tofile;
610 register char * fromname;
611 register char * toname;
613 if (fromfile[0] == '/')
614 fromname = ecpyalloc(fromfile);
616 fromname = ecpyalloc(directory);
617 fromname = ecatalloc(fromname, "/");
618 fromname = ecatalloc(fromname, fromfile);
620 if (tofile[0] == '/')
621 toname = ecpyalloc(tofile);
623 toname = ecpyalloc(directory);
624 toname = ecatalloc(toname, "/");
625 toname = ecatalloc(toname, tofile);
628 ** We get to be careful here since
629 ** there's a fair chance of root running us.
632 (void) remove(toname);
633 if (link(fromname, toname) != 0) {
636 if (mkdirs(toname) != 0)
639 result = link(fromname, toname);
642 access(fromname, F_OK) == 0 &&
644 const char *s = tofile;
645 register char * symlinkcontents = NULL;
647 while ((s = strchr(s+1, '/')) != NULL)
649 ecatalloc(symlinkcontents,
652 ecatalloc(symlinkcontents,
654 result = symlink(symlinkcontents,
657 warning(_("hard link failed, symbolic link used"));
658 ifree(symlinkcontents);
660 #endif /* HAVE_SYMLINK */
662 const char *e = strerror(errno);
664 (void) fprintf(stderr,
665 _("%s: Can't link from %s to %s: %s\n"),
666 progname, fromname, toname, e);
675 #define INT_MAX ((int) (((unsigned)~0)>>1))
676 #endif /* !defined INT_MAX */
679 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
680 #endif /* !defined INT_MIN */
683 ** The tz file format currently allows at most 32-bit quantities.
684 ** This restriction should be removed before signed 32-bit values
685 ** wrap around in 2038, but unfortunately this will require a
686 ** change to the tz file format.
689 #define MAX_BITS_IN_FILE 32
690 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
691 TYPE_BIT(zic_t) : MAX_BITS_IN_FILE)
694 setboundaries P((void))
698 if (TYPE_SIGNED(zic_t)) {
700 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
702 max_time = -(min_time + 1);
707 max_time = 2 - sflag;
708 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
715 t = (time_t) min_time;
716 min_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
717 t = (time_t) max_time;
718 max_year = TM_YEAR_BASE + gmtime(&t)->tm_year;
720 min_year_representable = min_year;
721 max_year_representable = max_year;
726 const char * const name;
728 register char * myname;
731 myname = ecpyalloc(name);
732 myname = ecatalloc(myname, "/.");
733 accres = access(myname, F_OK);
739 ** Associate sets of rules with zones.
743 ** Sort by rule name.
751 return strcmp(((const struct rule *) cp1)->r_name,
752 ((const struct rule *) cp2)->r_name);
758 register struct zone * zp;
759 register struct rule * rp;
760 register int base, out;
764 (void) qsort((void *) rules, (size_t) nrules,
765 (size_t) sizeof *rules, rcomp);
766 for (i = 0; i < nrules - 1; ++i) {
767 if (strcmp(rules[i].r_name,
768 rules[i + 1].r_name) != 0)
770 if (strcmp(rules[i].r_filename,
771 rules[i + 1].r_filename) == 0)
773 eat(rules[i].r_filename, rules[i].r_linenum);
774 warning(_("same rule name in multiple files"));
775 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
776 warning(_("same rule name in multiple files"));
777 for (j = i + 2; j < nrules; ++j) {
778 if (strcmp(rules[i].r_name,
779 rules[j].r_name) != 0)
781 if (strcmp(rules[i].r_filename,
782 rules[j].r_filename) == 0)
784 if (strcmp(rules[i + 1].r_filename,
785 rules[j].r_filename) == 0)
792 for (i = 0; i < nzones; ++i) {
797 for (base = 0; base < nrules; base = out) {
799 for (out = base + 1; out < nrules; ++out)
800 if (strcmp(rp->r_name, rules[out].r_name) != 0)
802 for (i = 0; i < nzones; ++i) {
804 if (strcmp(zp->z_rule, rp->r_name) != 0)
807 zp->z_nrules = out - base;
810 for (i = 0; i < nzones; ++i) {
812 if (zp->z_nrules == 0) {
814 ** Maybe we have a local standard time offset.
816 eat(zp->z_filename, zp->z_linenum);
817 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
820 ** Note, though, that if there's no rule,
821 ** a '%s' in the format is a bad thing.
823 if (strchr(zp->z_format, '%') != 0)
824 error(_("%s in ruleless zone"));
836 register char ** fields;
838 register const struct lookup * lp;
839 register int nfields;
840 register int wantcont;
844 if (strcmp(name, "-") == 0) {
845 name = _("standard input");
847 } else if ((fp = fopen(name, "r")) == NULL) {
848 const char *e = strerror(errno);
850 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
855 for (num = 1; ; ++num) {
857 if (fgets(buf, (int) sizeof buf, fp) != buf)
859 cp = strchr(buf, '\n');
861 error(_("line too long"));
865 fields = getfields(buf);
867 while (fields[nfields] != NULL) {
870 if (strcmp(fields[nfields], "-") == 0)
871 fields[nfields] = &nada;
876 } else if (wantcont) {
877 wantcont = inzcont(fields, nfields);
879 lp = byword(fields[0], line_codes);
881 error(_("input line of unknown type"));
882 else switch ((int) (lp->l_value)) {
884 inrule(fields, nfields);
888 wantcont = inzone(fields, nfields);
891 inlink(fields, nfields);
896 (void) fprintf(stderr,
897 _("%s: Leap line in non leap seconds file %s\n"),
899 else inleap(fields, nfields);
902 default: /* "cannot happen" */
903 (void) fprintf(stderr,
904 _("%s: panic: Invalid l_value %d\n"),
905 progname, lp->l_value);
909 ifree((char *) fields);
912 (void) fprintf(stderr, _("%s: Error reading %s\n"),
916 if (fp != stdin && fclose(fp)) {
917 const char *e = strerror(errno);
919 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
920 progname, filename, e);
924 error(_("expected continuation line not found"));
928 ** Convert a string of one of the forms
929 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
930 ** into a number of seconds.
931 ** A null string maps to zero.
932 ** Call error with errstring and return zero on errors.
936 gethms(string, errstring, signable)
938 const char * const errstring;
941 int hh, mm, ss, sign;
943 if (string == NULL || *string == '\0')
947 else if (*string == '-') {
951 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
953 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
955 else if (sscanf(string, scheck(string, "%d:%d:%d"),
956 &hh, &mm, &ss) != 3) {
960 if ((hh < 0 || hh >= HOURSPERDAY ||
961 mm < 0 || mm >= MINSPERHOUR ||
962 ss < 0 || ss > SECSPERMIN) &&
963 !(hh == HOURSPERDAY && mm == 0 && ss == 0)) {
967 if (noise && hh == HOURSPERDAY)
968 warning(_("24:00 not handled by pre-1998 versions of zic"));
970 (eitol(hh * MINSPERHOUR + mm) *
971 eitol(SECSPERMIN) + eitol(ss));
975 inrule(fields, nfields)
976 register char ** const fields;
979 static struct rule r;
981 if (nfields != RULE_FIELDS) {
982 error(_("wrong number of fields on Rule line"));
985 if (*fields[RF_NAME] == '\0') {
986 error(_("nameless rule"));
989 r.r_filename = filename;
990 r.r_linenum = linenum;
991 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
992 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
993 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
994 r.r_name = ecpyalloc(fields[RF_NAME]);
995 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
996 rules = (struct rule *) (void *) erealloc((char *) rules,
997 (int) ((nrules + 1) * sizeof *rules));
1002 inzone(fields, nfields)
1003 register char ** const fields;
1009 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1010 error(_("wrong number of fields on Zone line"));
1013 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
1014 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1016 _("\"Zone %s\" line and -l option are mutually exclusive"),
1021 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1022 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1024 _("\"Zone %s\" line and -p option are mutually exclusive"),
1029 for (i = 0; i < nzones; ++i)
1030 if (zones[i].z_name != NULL &&
1031 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1032 buf = erealloc(buf, (int) (132 +
1033 strlen(fields[ZF_NAME]) +
1034 strlen(zones[i].z_filename)));
1036 _("duplicate zone name %s (file \"%s\", line %d)"),
1038 zones[i].z_filename,
1039 zones[i].z_linenum);
1043 return inzsub(fields, nfields, FALSE);
1047 inzcont(fields, nfields)
1048 register char ** const fields;
1051 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1052 error(_("wrong number of fields on Zone continuation line"));
1055 return inzsub(fields, nfields, TRUE);
1059 inzsub(fields, nfields, iscont)
1060 register char ** const fields;
1065 static struct zone z;
1066 register int i_gmtoff, i_rule, i_format;
1067 register int i_untilyear, i_untilmonth;
1068 register int i_untilday, i_untiltime;
1069 register int hasuntil;
1072 i_gmtoff = ZFC_GMTOFF;
1074 i_format = ZFC_FORMAT;
1075 i_untilyear = ZFC_TILYEAR;
1076 i_untilmonth = ZFC_TILMONTH;
1077 i_untilday = ZFC_TILDAY;
1078 i_untiltime = ZFC_TILTIME;
1081 i_gmtoff = ZF_GMTOFF;
1083 i_format = ZF_FORMAT;
1084 i_untilyear = ZF_TILYEAR;
1085 i_untilmonth = ZF_TILMONTH;
1086 i_untilday = ZF_TILDAY;
1087 i_untiltime = ZF_TILTIME;
1088 z.z_name = ecpyalloc(fields[ZF_NAME]);
1090 z.z_filename = filename;
1091 z.z_linenum = linenum;
1092 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1093 if ((cp = strchr(fields[i_format], '%')) != 0) {
1094 if (*++cp != 's' || strchr(cp, '%') != 0) {
1095 error(_("invalid abbreviation format"));
1099 z.z_rule = ecpyalloc(fields[i_rule]);
1100 z.z_format = ecpyalloc(fields[i_format]);
1101 hasuntil = nfields > i_untilyear;
1103 z.z_untilrule.r_filename = filename;
1104 z.z_untilrule.r_linenum = linenum;
1105 rulesub(&z.z_untilrule,
1106 fields[i_untilyear],
1109 (nfields > i_untilmonth) ?
1110 fields[i_untilmonth] : "Jan",
1111 (nfields > i_untilday) ? fields[i_untilday] : "1",
1112 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1113 z.z_untiltime = rpytime(&z.z_untilrule,
1114 z.z_untilrule.r_loyear);
1115 if (iscont && nzones > 0 &&
1116 z.z_untiltime > min_time &&
1117 z.z_untiltime < max_time &&
1118 zones[nzones - 1].z_untiltime > min_time &&
1119 zones[nzones - 1].z_untiltime < max_time &&
1120 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1122 "Zone continuation line end time is not after end time of previous line"
1127 zones = (struct zone *) (void *) erealloc((char *) zones,
1128 (int) ((nzones + 1) * sizeof *zones));
1129 zones[nzones++] = z;
1131 ** If there was an UNTIL field on this line,
1132 ** there's more information about the zone on the next line.
1138 inleap(fields, nfields)
1139 register char ** const fields;
1142 register const char * cp;
1143 register const struct lookup * lp;
1145 int year, month, day;
1149 if (nfields != LEAP_FIELDS) {
1150 error(_("wrong number of fields on Leap line"));
1154 cp = fields[LP_YEAR];
1155 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1159 error(_("invalid leaping year"));
1165 i = len_years[isleap(j)];
1169 i = -len_years[isleap(j)];
1171 dayoff = oadd(dayoff, eitol(i));
1173 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1174 error(_("invalid month name"));
1177 month = lp->l_value;
1179 while (j != month) {
1180 i = len_months[isleap(year)][j];
1181 dayoff = oadd(dayoff, eitol(i));
1184 cp = fields[LP_DAY];
1185 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1186 day <= 0 || day > len_months[isleap(year)][month]) {
1187 error(_("invalid day of month"));
1190 dayoff = oadd(dayoff, eitol(day - 1));
1191 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1192 error(_("time before zero"));
1195 if (dayoff < min_time / SECSPERDAY) {
1196 error(_("time too small"));
1199 if (dayoff > max_time / SECSPERDAY) {
1200 error(_("time too large"));
1203 t = (zic_t) dayoff * SECSPERDAY;
1204 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1205 cp = fields[LP_CORR];
1207 register int positive;
1210 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1213 } else if (strcmp(cp, "--") == 0) {
1216 } else if (strcmp(cp, "+") == 0) {
1219 } else if (strcmp(cp, "++") == 0) {
1223 error(_("illegal CORRECTION field on Leap line"));
1226 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1228 "illegal Rolling/Stationary field on Leap line"
1232 leapadd(tadd(t, tod), positive, lp->l_value, count);
1237 inlink(fields, nfields)
1238 register char ** const fields;
1243 if (nfields != LINK_FIELDS) {
1244 error(_("wrong number of fields on Link line"));
1247 if (*fields[LF_FROM] == '\0') {
1248 error(_("blank FROM field on Link line"));
1251 if (*fields[LF_TO] == '\0') {
1252 error(_("blank TO field on Link line"));
1255 l.l_filename = filename;
1256 l.l_linenum = linenum;
1257 l.l_from = ecpyalloc(fields[LF_FROM]);
1258 l.l_to = ecpyalloc(fields[LF_TO]);
1259 links = (struct link *) (void *) erealloc((char *) links,
1260 (int) ((nlinks + 1) * sizeof *links));
1261 links[nlinks++] = l;
1265 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1266 register struct rule * const rp;
1267 const char * const loyearp;
1268 const char * const hiyearp;
1269 const char * const typep;
1270 const char * const monthp;
1271 const char * const dayp;
1272 const char * const timep;
1274 register const struct lookup * lp;
1275 register const char * cp;
1279 if ((lp = byword(monthp, mon_names)) == NULL) {
1280 error(_("invalid month name"));
1283 rp->r_month = lp->l_value;
1284 rp->r_todisstd = FALSE;
1285 rp->r_todisgmt = FALSE;
1286 dp = ecpyalloc(timep);
1288 ep = dp + strlen(dp) - 1;
1289 switch (lowerit(*ep)) {
1290 case 's': /* Standard */
1291 rp->r_todisstd = TRUE;
1292 rp->r_todisgmt = FALSE;
1295 case 'w': /* Wall */
1296 rp->r_todisstd = FALSE;
1297 rp->r_todisgmt = FALSE;
1300 case 'g': /* Greenwich */
1301 case 'u': /* Universal */
1302 case 'z': /* Zulu */
1303 rp->r_todisstd = TRUE;
1304 rp->r_todisgmt = TRUE;
1309 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1315 lp = byword(cp, begin_years);
1316 if (lp != NULL) switch ((int) lp->l_value) {
1318 rp->r_loyear = INT_MIN;
1321 rp->r_loyear = INT_MAX;
1323 default: /* "cannot happen" */
1324 (void) fprintf(stderr,
1325 _("%s: panic: Invalid l_value %d\n"),
1326 progname, lp->l_value);
1328 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1329 error(_("invalid starting year"));
1332 if (rp->r_loyear < min_year_representable)
1333 warning(_("starting year too low to be represented"));
1334 else if (rp->r_loyear > max_year_representable)
1335 warning(_("starting year too high to be represented"));
1338 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1340 rp->r_hiyear = INT_MIN;
1343 rp->r_hiyear = INT_MAX;
1346 rp->r_hiyear = rp->r_loyear;
1348 default: /* "cannot happen" */
1349 (void) fprintf(stderr,
1350 _("%s: panic: Invalid l_value %d\n"),
1351 progname, lp->l_value);
1353 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1354 error(_("invalid ending year"));
1357 if (rp->r_loyear < min_year_representable)
1358 warning(_("ending year too low to be represented"));
1359 else if (rp->r_loyear > max_year_representable)
1360 warning(_("ending year too high to be represented"));
1362 if (rp->r_loyear > rp->r_hiyear) {
1363 error(_("starting year greater than ending year"));
1367 rp->r_yrtype = NULL;
1369 if (rp->r_loyear == rp->r_hiyear) {
1370 error(_("typed single year"));
1373 rp->r_yrtype = ecpyalloc(typep);
1375 if (rp->r_loyear < min_year && rp->r_loyear > 0)
1376 min_year = rp->r_loyear;
1379 ** Accept things such as:
1385 dp = ecpyalloc(dayp);
1386 if ((lp = byword(dp, lasts)) != NULL) {
1387 rp->r_dycode = DC_DOWLEQ;
1388 rp->r_wday = lp->l_value;
1389 rp->r_dayofmonth = len_months[1][rp->r_month];
1391 if ((ep = strchr(dp, '<')) != 0)
1392 rp->r_dycode = DC_DOWLEQ;
1393 else if ((ep = strchr(dp, '>')) != 0)
1394 rp->r_dycode = DC_DOWGEQ;
1397 rp->r_dycode = DC_DOM;
1399 if (rp->r_dycode != DC_DOM) {
1402 error(_("invalid day of month"));
1406 if ((lp = byword(dp, wday_names)) == NULL) {
1407 error(_("invalid weekday name"));
1411 rp->r_wday = lp->l_value;
1413 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1414 rp->r_dayofmonth <= 0 ||
1415 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1416 error(_("invalid day of month"));
1430 register long shift;
1432 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1433 buf[i] = val >> shift;
1444 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1452 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1454 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1461 const char * const name;
1465 static char * fullname;
1466 static struct tzhead tzh;
1467 zic_t ats[TZ_MAX_TIMES];
1468 unsigned char types[TZ_MAX_TIMES];
1474 (void) qsort((void *) attypes, (size_t) timecnt,
1475 (size_t) sizeof *attypes, atcomp);
1485 while (fromi < timecnt && attypes[fromi].at < min_time)
1488 while (fromi < timecnt && attypes[fromi].type == 0)
1489 ++fromi; /* handled by default rule */
1490 for ( ; fromi < timecnt; ++fromi) {
1491 if (toi != 0 && ((attypes[fromi].at +
1492 gmtoffs[attypes[toi - 1].type]) <=
1493 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1494 : attypes[toi - 2].type]))) {
1495 attypes[toi - 1].type =
1496 attypes[fromi].type;
1500 attypes[toi - 1].type != attypes[fromi].type)
1501 attypes[toi++] = attypes[fromi];
1508 for (i = 0; i < timecnt; ++i) {
1509 ats[i] = attypes[i].at;
1510 types[i] = attypes[i].type;
1512 fullname = erealloc(fullname,
1513 (int) (strlen(directory) + 1 + strlen(name) + 1));
1514 (void) sprintf(fullname, "%s/%s", directory, name);
1516 ** Remove old file, if any, to snap links.
1518 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1519 const char *e = strerror(errno);
1521 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1522 progname, fullname, e);
1525 if ((fp = fopen(fullname, "wb")) == NULL) {
1526 if (mkdirs(fullname) != 0)
1528 if ((fp = fopen(fullname, "wb")) == NULL) {
1529 const char *e = strerror(errno);
1531 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1532 progname, fullname, e);
1536 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1537 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1538 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1539 convert(eitol(timecnt), tzh.tzh_timecnt);
1540 convert(eitol(typecnt), tzh.tzh_typecnt);
1541 convert(eitol(charcnt), tzh.tzh_charcnt);
1542 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1543 #define DO(field) (void) fwrite((void *) tzh.field, \
1544 (size_t) sizeof tzh.field, (size_t) 1, fp)
1554 for (i = 0; i < timecnt; ++i) {
1557 if (ats[i] >= trans[j]) {
1558 ats[i] = tadd(ats[i], corr[j]);
1561 puttzcode((long) ats[i], fp);
1564 (void) fwrite((void *) types, (size_t) sizeof types[0],
1565 (size_t) timecnt, fp);
1566 for (i = 0; i < typecnt; ++i) {
1567 puttzcode((long) gmtoffs[i], fp);
1568 (void) putc(isdsts[i], fp);
1569 (void) putc(abbrinds[i], fp);
1572 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1573 (size_t) charcnt, fp);
1574 for (i = 0; i < leapcnt; ++i) {
1576 if (timecnt == 0 || trans[i] < ats[0]) {
1579 if (++j >= typecnt) {
1585 while (j < timecnt && trans[i] >= ats[j])
1589 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1590 } else puttzcode((long) trans[i], fp);
1591 puttzcode((long) corr[i], fp);
1593 for (i = 0; i < typecnt; ++i)
1594 (void) putc(ttisstds[i], fp);
1595 for (i = 0; i < typecnt; ++i)
1596 (void) putc(ttisgmts[i], fp);
1597 if (ferror(fp) || fclose(fp)) {
1598 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1599 progname, fullname);
1605 doabbr(abbr, format, letters, isdst)
1607 const char * const format;
1608 const char * const letters;
1611 if (strchr(format, '/') == NULL) {
1612 if (letters == NULL)
1613 (void) strcpy(abbr, format);
1614 else (void) sprintf(abbr, format, letters);
1616 (void) strcpy(abbr, strchr(format, '/') + 1);
1618 (void) strcpy(abbr, format);
1619 *strchr(abbr, '/') = '\0';
1624 outzone(zpfirst, zonecount)
1625 const struct zone * const zpfirst;
1626 const int zonecount;
1628 register const struct zone * zp;
1629 register struct rule * rp;
1631 register int usestart, useuntil;
1632 register zic_t starttime, untiltime;
1633 register long gmtoff;
1634 register long stdoff;
1636 register long startoff;
1637 register int startttisstd;
1638 register int startttisgmt;
1640 char startbuf[BUFSIZ];
1642 INITIALIZE(untiltime);
1643 INITIALIZE(starttime);
1645 ** Now. . .finally. . .generate some useful data!
1651 ** Thanks to Earl Chew
1652 ** for noting the need to unconditionally initialize startttisstd.
1654 startttisstd = FALSE;
1655 startttisgmt = FALSE;
1656 for (i = 0; i < zonecount; ++i) {
1658 ** A guess that may well be corrected later.
1662 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1663 useuntil = i < (zonecount - 1);
1664 if (useuntil && zp->z_untiltime <= min_time)
1666 gmtoff = zp->z_gmtoff;
1667 eat(zp->z_filename, zp->z_linenum);
1669 startoff = zp->z_gmtoff;
1670 if (zp->z_nrules == 0) {
1671 stdoff = zp->z_stdoff;
1672 doabbr(startbuf, zp->z_format,
1673 (char *) NULL, stdoff != 0);
1674 type = addtype(oadd(zp->z_gmtoff, stdoff),
1675 startbuf, stdoff != 0, startttisstd,
1678 addtt(starttime, type);
1680 } else if (stdoff != 0)
1681 addtt(min_time, type);
1682 } else for (year = min_year; year <= max_year; ++year) {
1683 if (useuntil && year > zp->z_untilrule.r_hiyear)
1686 ** Mark which rules to do in the current year.
1687 ** For those to do, calculate rpytime(rp, year);
1689 for (j = 0; j < zp->z_nrules; ++j) {
1690 rp = &zp->z_rules[j];
1691 eats(zp->z_filename, zp->z_linenum,
1692 rp->r_filename, rp->r_linenum);
1693 rp->r_todo = year >= rp->r_loyear &&
1694 year <= rp->r_hiyear &&
1695 yearistype(year, rp->r_yrtype);
1697 rp->r_temp = rpytime(rp, year);
1701 register zic_t jtime, ktime;
1702 register long offset;
1708 ** Turn untiltime into UTC
1709 ** assuming the current gmtoff and
1712 untiltime = zp->z_untiltime;
1713 if (!zp->z_untilrule.r_todisgmt)
1714 untiltime = tadd(untiltime,
1716 if (!zp->z_untilrule.r_todisstd)
1717 untiltime = tadd(untiltime,
1721 ** Find the rule (of those to do, if any)
1722 ** that takes effect earliest in the year.
1725 for (j = 0; j < zp->z_nrules; ++j) {
1726 rp = &zp->z_rules[j];
1729 eats(zp->z_filename, zp->z_linenum,
1730 rp->r_filename, rp->r_linenum);
1731 offset = rp->r_todisgmt ? 0 : gmtoff;
1732 if (!rp->r_todisstd)
1733 offset = oadd(offset, stdoff);
1735 if (jtime == min_time ||
1738 jtime = tadd(jtime, -offset);
1739 if (k < 0 || jtime < ktime) {
1745 break; /* go on to next year */
1746 rp = &zp->z_rules[k];
1748 if (useuntil && ktime >= untiltime)
1750 stdoff = rp->r_stdoff;
1751 if (usestart && ktime == starttime)
1754 if (ktime < starttime) {
1755 startoff = oadd(zp->z_gmtoff,
1757 doabbr(startbuf, zp->z_format,
1762 if (*startbuf == '\0' &&
1763 startoff == oadd(zp->z_gmtoff,
1771 eats(zp->z_filename, zp->z_linenum,
1772 rp->r_filename, rp->r_linenum);
1773 doabbr(buf, zp->z_format, rp->r_abbrvar,
1775 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1776 type = addtype(offset, buf, rp->r_stdoff != 0,
1777 rp->r_todisstd, rp->r_todisgmt);
1782 if (*startbuf == '\0' &&
1783 zp->z_format != NULL &&
1784 strchr(zp->z_format, '%') == NULL &&
1785 strchr(zp->z_format, '/') == NULL)
1786 (void) strcpy(startbuf, zp->z_format);
1787 eat(zp->z_filename, zp->z_linenum);
1788 if (*startbuf == '\0')
1789 error(_("can't determine time zone abbreviation to use just after until time"));
1790 else addtt(starttime,
1791 addtype(startoff, startbuf,
1792 startoff != zp->z_gmtoff,
1797 ** Now we may get to set starttime for the next zone line.
1800 startttisstd = zp->z_untilrule.r_todisstd;
1801 startttisgmt = zp->z_untilrule.r_todisgmt;
1802 starttime = zp->z_untiltime;
1804 starttime = tadd(starttime, -stdoff);
1806 starttime = tadd(starttime, -gmtoff);
1809 writezone(zpfirst->z_name);
1813 addtt(starttime, type)
1814 const zic_t starttime;
1817 if (starttime <= min_time ||
1818 (timecnt == 1 && attypes[0].at < min_time)) {
1819 gmtoffs[0] = gmtoffs[type];
1820 isdsts[0] = isdsts[type];
1821 ttisstds[0] = ttisstds[type];
1822 ttisgmts[0] = ttisgmts[type];
1823 if (abbrinds[type] != 0)
1824 (void) strcpy(chars, &chars[abbrinds[type]]);
1826 charcnt = strlen(chars) + 1;
1831 if (timecnt >= TZ_MAX_TIMES) {
1832 error(_("too many transitions?!"));
1835 attypes[timecnt].at = starttime;
1836 attypes[timecnt].type = type;
1841 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1843 const char * const abbr;
1850 if (isdst != TRUE && isdst != FALSE) {
1851 error(_("internal error - addtype called with bad isdst"));
1854 if (ttisstd != TRUE && ttisstd != FALSE) {
1855 error(_("internal error - addtype called with bad ttisstd"));
1858 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1859 error(_("internal error - addtype called with bad ttisgmt"));
1863 ** See if there's already an entry for this zone type.
1864 ** If so, just return its index.
1866 for (i = 0; i < typecnt; ++i) {
1867 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1868 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1869 ttisstd == ttisstds[i] &&
1870 ttisgmt == ttisgmts[i])
1874 ** There isn't one; add a new one, unless there are already too
1877 if (typecnt >= TZ_MAX_TYPES) {
1878 error(_("too many local time types"));
1881 gmtoffs[i] = gmtoff;
1883 ttisstds[i] = ttisstd;
1884 ttisgmts[i] = ttisgmt;
1886 for (j = 0; j < charcnt; ++j)
1887 if (strcmp(&chars[j], abbr) == 0)
1897 leapadd(t, positive, rolling, count)
1905 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1906 error(_("too many leap seconds"));
1909 for (i = 0; i < leapcnt; ++i)
1910 if (t <= trans[i]) {
1911 if (t == trans[i]) {
1912 error(_("repeated leap second moment"));
1918 for (j = leapcnt; j > i; --j) {
1919 trans[j] = trans[j - 1];
1920 corr[j] = corr[j - 1];
1921 roll[j] = roll[j - 1];
1924 corr[i] = positive ? 1L : eitol(-count);
1927 } while (positive && --count != 0);
1934 register long last = 0;
1937 ** propagate leap seconds forward
1939 for (i = 0; i < leapcnt; ++i) {
1940 trans[i] = tadd(trans[i], last);
1941 last = corr[i] += last;
1946 yearistype(year, type)
1948 const char * const type;
1953 if (type == NULL || *type == '\0')
1955 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1956 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1957 result = system(buf);
1958 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
1964 error(_("Wild result from command execution"));
1965 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1966 progname, buf, result);
1975 a = (unsigned char) a;
1976 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1980 ciequal(ap, bp) /* case-insensitive equality */
1981 register const char * ap;
1982 register const char * bp;
1984 while (lowerit(*ap) == lowerit(*bp++))
1992 register const char * abbr;
1993 register const char * word;
1995 if (lowerit(*abbr) != lowerit(*word))
1998 while (*++abbr != '\0')
2002 } while (lowerit(*word++) != lowerit(*abbr));
2006 static const struct lookup *
2008 register const char * const word;
2009 register const struct lookup * const table;
2011 register const struct lookup * foundlp;
2012 register const struct lookup * lp;
2014 if (word == NULL || table == NULL)
2017 ** Look for exact match.
2019 for (lp = table; lp->l_word != NULL; ++lp)
2020 if (ciequal(word, lp->l_word))
2023 ** Look for inexact match.
2026 for (lp = table; lp->l_word != NULL; ++lp)
2027 if (itsabbr(word, lp->l_word)) {
2028 if (foundlp == NULL)
2030 else return NULL; /* multiple inexact matches */
2040 register char ** array;
2045 array = (char **) (void *)
2046 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2049 while (isascii((unsigned char) *cp) &&
2050 isspace((unsigned char) *cp))
2052 if (*cp == '\0' || *cp == '#')
2054 array[nsubs++] = dp = cp;
2056 if ((*dp = *cp++) != '"')
2058 else while ((*dp = *cp++) != '"')
2062 "Odd number of quotation marks"
2064 } while (*cp != '\0' && *cp != '#' &&
2065 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2066 if (isascii(*cp) && isspace((unsigned char) *cp))
2070 array[nsubs] = NULL;
2082 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2083 error(_("time overflow"));
2096 if (t1 == max_time && t2 > 0)
2098 if (t1 == min_time && t2 < 0)
2101 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2102 error(_("time overflow"));
2109 ** Given a rule, and a year, compute the date - in seconds since January 1,
2110 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2114 rpytime(rp, wantedy)
2115 register const struct rule * const rp;
2116 register const int wantedy;
2118 register int y, m, i;
2119 register long dayoff; /* with a nod to Margaret O. */
2122 if (wantedy == INT_MIN)
2124 if (wantedy == INT_MAX)
2129 while (wantedy != y) {
2131 i = len_years[isleap(y)];
2135 i = -len_years[isleap(y)];
2137 dayoff = oadd(dayoff, eitol(i));
2139 while (m != rp->r_month) {
2140 i = len_months[isleap(y)][m];
2141 dayoff = oadd(dayoff, eitol(i));
2144 i = rp->r_dayofmonth;
2145 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2146 if (rp->r_dycode == DC_DOWLEQ)
2149 error(_("use of 2/29 in non leap-year"));
2154 dayoff = oadd(dayoff, eitol(i));
2155 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2158 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2159 wday = eitol(EPOCH_WDAY);
2161 ** Don't trust mod of negative numbers.
2164 wday = (wday + dayoff) % LDAYSPERWEEK;
2166 wday -= ((-dayoff) % LDAYSPERWEEK);
2168 wday += LDAYSPERWEEK;
2170 while (wday != eitol(rp->r_wday))
2171 if (rp->r_dycode == DC_DOWGEQ) {
2172 dayoff = oadd(dayoff, (long) 1);
2173 if (++wday >= LDAYSPERWEEK)
2177 dayoff = oadd(dayoff, (long) -1);
2179 wday = LDAYSPERWEEK - 1;
2182 if (i < 0 || i >= len_months[isleap(y)][m]) {
2184 warning(_("rule goes past start/end of month--\
2185 will not work with pre-2004 versions of zic"));
2188 if (dayoff < 0 && !TYPE_SIGNED(zic_t))
2190 if (dayoff < min_time / SECSPERDAY)
2192 if (dayoff > max_time / SECSPERDAY)
2194 t = (zic_t) dayoff * SECSPERDAY;
2195 return tadd(t, rp->r_tod);
2200 const char * const string;
2204 if (strcmp(string, GRANDPARENTED) != 0) {
2205 register const char * cp;
2209 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2210 ** optionally followed by a + or - and a number from 1 to 14.
2214 while (isascii((unsigned char) *cp) &&
2215 isalpha((unsigned char) *cp))
2217 if (cp - string == 0)
2218 wp = _("time zone abbreviation lacks alphabetic at start");
2219 if (noise && cp - string > 3)
2220 wp = _("time zone abbreviation has more than 3 alphabetics");
2221 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2222 wp = _("time zone abbreviation has too many alphabetics");
2223 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2225 if (isascii((unsigned char) *cp) &&
2226 isdigit((unsigned char) *cp))
2228 *cp >= '0' && *cp <= '4')
2232 wp = _("time zone abbreviation differs from POSIX standard");
2235 wp = ecatalloc(wp, " (");
2236 wp = ecatalloc(wp, string);
2237 wp = ecatalloc(wp, ")");
2242 i = strlen(string) + 1;
2243 if (charcnt + i > TZ_MAX_CHARS) {
2244 error(_("too many, or too long, time zone abbreviations"));
2247 (void) strcpy(&chars[charcnt], string);
2248 charcnt += eitol(i);
2253 char * const argname;
2255 register char * name;
2258 if (argname == NULL || *argname == '\0')
2260 cp = name = ecpyalloc(argname);
2261 while ((cp = strchr(cp + 1, '/')) != 0) {
2265 ** DOS drive specifier?
2267 if (isalpha((unsigned char) name[0]) &&
2268 name[1] == ':' && name[2] == '\0') {
2272 #endif /* !defined unix */
2273 if (!itsdir(name)) {
2275 ** It doesn't seem to exist, so we try to create it.
2276 ** Creation may fail because of the directory being
2277 ** created by some other multiprocessor, so we get
2278 ** to do extra checking.
2280 if (mkdir(name, MKDIR_UMASK) != 0) {
2281 const char *e = strerror(errno);
2283 if (errno != EEXIST || !itsdir(name)) {
2284 (void) fprintf(stderr,
2285 _("%s: Can't create directory %s: %s\n"),
2305 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2306 (void) fprintf(stderr,
2307 _("%s: %d did not sign extend correctly\n"),
2315 ** UNIX was a registered trademark of The Open Group in 2003.