2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
6 ** $FreeBSD: src/usr.sbin/zic/zic.c,v 1.11 1999/08/28 01:21:20 peter Exp $
11 #include <sys/stat.h> /* for umask manifest constants */
12 #include <sys/types.h>
17 #define ZIC_VERSION '2'
19 typedef int_fast64_t zic_t;
21 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
22 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
23 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
25 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
28 ** On some ancient hosts, predicates like `isspace(C)' are defined
29 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
30 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
31 ** Neither the C Standard nor Posix require that `isascii' exist.
32 ** For portability, we check both ancient and modern requirements.
33 ** If isascii is not defined, the isascii check succeeds trivially.
40 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
41 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
43 #define end(cp) (strchr((cp), '\0'))
46 const char * r_filename;
50 int r_loyear; /* for example, 1986 */
51 int r_hiyear; /* for example, 1986 */
52 const char * r_yrtype;
56 int r_month; /* 0..11 */
58 int r_dycode; /* see below */
62 long r_tod; /* time from midnight */
63 int r_todisstd; /* above is standard time if TRUE */
64 /* or wall clock time if FALSE */
65 int r_todisgmt; /* above is GMT if TRUE */
66 /* or local time if FALSE */
67 long r_stdoff; /* offset from standard time */
68 const char * r_abbrvar; /* variable part of abbreviation */
70 int r_todo; /* a rule to do (used in outzone) */
71 zic_t r_temp; /* used in outzone */
75 ** r_dycode r_dayofmonth r_wday
78 #define DC_DOM 0 /* 1..31 */ /* unused */
79 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
80 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
83 const char * z_filename;
89 const char * z_format;
93 struct rule * z_rules;
96 struct rule z_untilrule;
100 static void addtt(zic_t starttime, int type);
101 static int addtype(long gmtoff, const char *abbr, int isdst,
102 int ttisstd, int ttisgmt);
103 static void leapadd(zic_t t, int positive, int rolling, int count);
104 static void adjleap(void);
105 static void associate(void);
106 static int ciequal(const char *ap, const char *bp);
107 static void convert(long val, char *buf);
108 static void convert64(zic_t val, char *buf);
109 static void dolink(const char *fromfield, const char *tofield);
110 static void doabbr(char *abbr, const char *format,
111 const char *letters, int isdst, int doquotes);
112 static void eat(const char *name, int num);
113 static void eats(const char *name, int num,
114 const char *rname, int rnum);
115 static long eitol(int i);
116 static void error(const char *message);
117 static char ** getfields(char *buf);
118 static long gethms(const char *string, const char *errstrng,
120 static void infile(const char *filename);
121 static void inleap(char **fields, int nfields);
122 static void inlink(char **fields, int nfields);
123 static void inrule(char **fields, int nfields);
124 static int inzcont(char **fields, int nfields);
125 static int inzone(char **fields, int nfields);
126 static int inzsub(char **fields, int nfields, int iscont);
127 static int is32(zic_t x);
128 static int itsabbr(const char *abbr, const char *word);
129 static int itsdir(const char *name);
130 static int lowerit(int c);
131 static char * memcheck(char *tocheck);
132 static int mkdirs(char *filename);
133 static void newabbr(const char *abbr);
134 static long oadd(long t1, long t2);
135 static void outzone(const struct zone *zp, int ntzones);
136 static void puttzcode(long code, FILE *fp);
137 static void puttzcode64(zic_t code, FILE *fp);
138 static int rcomp(const void *leftp, const void *rightp);
139 static zic_t rpytime(const struct rule *rp, int wantedy);
140 static void rulesub(struct rule *rp,
141 const char *loyearp, const char *hiyearp,
142 const char *typep, const char *monthp,
143 const char *dayp, const char *timep);
144 static int stringoffset(char *result, long offset);
145 static int stringrule(char *result, const struct rule *rp,
146 long dstoff, long gmtoff);
147 static void stringzone(char *result,
148 const struct zone *zp, int ntzones);
149 static void setboundaries(void);
150 static void setgroup(gid_t *flag, const char *name);
151 static void setuser(uid_t *flag, const char *name);
152 static zic_t tadd(const zic_t t1, const long t2);
153 static void usage(void);
154 static void writezone(const char *name, const char *string);
155 static int yearistype(int year, const char *type);
159 static const char * filename;
162 static int leapminyear;
163 static int leapmaxyear;
165 static int max_abbrvar_len;
166 static int max_format_len;
167 static zic_t max_time;
169 static zic_t min_time;
172 static const char * rfilename;
187 ** Which fields are which on a Zone line.
195 #define ZF_TILMONTH 6
198 #define ZONE_MINFIELDS 5
199 #define ZONE_MAXFIELDS 9
202 ** Which fields are which on a Zone continuation line.
208 #define ZFC_TILYEAR 3
209 #define ZFC_TILMONTH 4
211 #define ZFC_TILTIME 6
212 #define ZONEC_MINFIELDS 3
213 #define ZONEC_MAXFIELDS 7
216 ** Which files are which on a Rule line.
228 #define RULE_FIELDS 10
231 ** Which fields are which on a Link line.
236 #define LINK_FIELDS 3
239 ** Which fields are which on a Leap line.
248 #define LEAP_FIELDS 7
258 static struct rule * rules;
259 static int nrules; /* number of rules */
261 static struct zone * zones;
262 static int nzones; /* number of zones */
265 const char * l_filename;
271 static struct link * links;
279 static struct lookup const *byword(const char *string,
280 const struct lookup *lp);
282 static struct lookup const line_codes[] = {
290 static struct lookup const mon_names[] = {
291 { "January", TM_JANUARY },
292 { "February", TM_FEBRUARY },
293 { "March", TM_MARCH },
294 { "April", TM_APRIL },
298 { "August", TM_AUGUST },
299 { "September", TM_SEPTEMBER },
300 { "October", TM_OCTOBER },
301 { "November", TM_NOVEMBER },
302 { "December", TM_DECEMBER },
306 static struct lookup const wday_names[] = {
307 { "Sunday", TM_SUNDAY },
308 { "Monday", TM_MONDAY },
309 { "Tuesday", TM_TUESDAY },
310 { "Wednesday", TM_WEDNESDAY },
311 { "Thursday", TM_THURSDAY },
312 { "Friday", TM_FRIDAY },
313 { "Saturday", TM_SATURDAY },
317 static struct lookup const lasts[] = {
318 { "last-Sunday", TM_SUNDAY },
319 { "last-Monday", TM_MONDAY },
320 { "last-Tuesday", TM_TUESDAY },
321 { "last-Wednesday", TM_WEDNESDAY },
322 { "last-Thursday", TM_THURSDAY },
323 { "last-Friday", TM_FRIDAY },
324 { "last-Saturday", TM_SATURDAY },
328 static struct lookup const begin_years[] = {
329 { "minimum", YR_MINIMUM },
330 { "maximum", YR_MAXIMUM },
334 static struct lookup const end_years[] = {
335 { "minimum", YR_MINIMUM },
336 { "maximum", YR_MAXIMUM },
341 static struct lookup const leap_types[] = {
343 { "Stationary", FALSE },
347 static const int len_months[2][MONSPERYEAR] = {
348 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
349 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
352 static const int len_years[2] = {
353 DAYSPERNYEAR, DAYSPERLYEAR
356 static struct attype {
359 } attypes[TZ_MAX_TIMES];
360 static long gmtoffs[TZ_MAX_TYPES];
361 static char isdsts[TZ_MAX_TYPES];
362 static unsigned char abbrinds[TZ_MAX_TYPES];
363 static char ttisstds[TZ_MAX_TYPES];
364 static char ttisgmts[TZ_MAX_TYPES];
365 static char chars[TZ_MAX_CHARS];
366 static zic_t trans[TZ_MAX_LEAPS];
367 static long corr[TZ_MAX_LEAPS];
368 static char roll[TZ_MAX_LEAPS];
371 ** Memory allocation.
375 memcheck(char * const ptr)
378 errx(EXIT_FAILURE, _("memory exhausted"));
382 #define emalloc(size) memcheck(imalloc(size))
383 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
384 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
385 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
392 eats(const char * const name, const int num,
393 const char * const rname, const int rnum)
402 eat(const char * const name, const int num)
404 eats(name, num, NULL, -1);
408 error(const char * const string)
411 ** Match the format of "cc" to allow sh users to
412 ** zic ... 2>&1 | error -t "*" -v
415 fprintf(stderr, _("\"%s\", line %d: %s"),
416 filename, linenum, string);
417 if (rfilename != NULL)
418 fprintf(stderr, _(" (rule from \"%s\", line %d)"),
419 rfilename, rlinenum);
420 fprintf(stderr, "\n");
425 warning(const char * const string)
429 cp = ecpyalloc(_("warning: "));
430 cp = ecatalloc(cp, string);
439 fprintf(stderr, "%s\n%s\n",
440 _("usage: zic [-v] [-l localtime] [-p posixrules] [-d directory]"),
441 _(" [-L leapseconds] [-y yearistype] [filename ...]"));
445 static const char * psxrules;
446 static const char * lcltime;
447 static const char * directory;
448 static const char * leapsec;
449 static const char * yitcommand;
451 static uid_t uflag = (uid_t)-1;
452 static gid_t gflag = (gid_t)-1;
453 static mode_t mflag = (S_IRUSR | S_IRGRP | S_IROTH
457 main(int argc, char *argv[])
464 umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
465 #endif /* defined unix */
466 while ((c = getopt(argc, argv, "Dd:g:l:m:p:L:u:vsy:")) != -1)
474 if (directory == NULL)
478 _("more than one -d option specified"));
481 setgroup(&gflag, optarg);
488 _("more than one -l option specified"));
492 void *set = setmode(optarg);
497 if (psxrules == NULL)
501 _("more than one -p option specified"));
504 setuser(&uflag, optarg);
507 if (yitcommand == NULL)
511 _("more than one -y option specified"));
518 _("more than one -L option specified"));
524 warnx(_("-s ignored\n"));
527 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
528 usage(); /* usage message by request */
529 if (directory == NULL)
531 if (yitcommand == NULL)
532 yitcommand = "yearistype";
536 if (optind < argc && leapsec != NULL) {
541 for (i = optind; i < argc; ++i)
546 for (i = 0; i < nzones; i = j) {
548 ** Find the next non-continuation zone entry.
550 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
552 outzone(&zones[i], j - i);
557 for (i = 0; i < nlinks; ++i) {
558 eat(links[i].l_filename, links[i].l_linenum);
559 dolink(links[i].l_from, links[i].l_to);
561 for (j = 0; j < nlinks; ++j)
562 if (strcmp(links[i].l_to,
563 links[j].l_from) == 0)
564 warning(_("link to link"));
566 if (lcltime != NULL) {
567 eat("command line", 1);
568 dolink(lcltime, TZDEFAULT);
570 if (psxrules != NULL) {
571 eat("command line", 1);
572 dolink(psxrules, TZDEFRULES);
574 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
578 dolink(const char * const fromfield, const char * const tofield)
583 if (fromfield[0] == '/')
584 fromname = ecpyalloc(fromfield);
586 fromname = ecpyalloc(directory);
587 fromname = ecatalloc(fromname, "/");
588 fromname = ecatalloc(fromname, fromfield);
590 if (tofield[0] == '/')
591 toname = ecpyalloc(tofield);
593 toname = ecpyalloc(directory);
594 toname = ecatalloc(toname, "/");
595 toname = ecatalloc(toname, tofield);
598 ** We get to be careful here since
599 ** there's a fair chance of root running us.
603 if (link(fromname, toname) != 0) {
606 if (mkdirs(toname) != 0)
609 result = link(fromname, toname);
611 access(fromname, F_OK) == 0 &&
613 const char *s = tofield;
614 char * symlinkcontents = NULL;
616 while ((s = strchr(s+1, '/')) != NULL)
618 ecatalloc(symlinkcontents,
621 ecatalloc(symlinkcontents,
623 result = symlink(symlinkcontents,
626 warning(_("hard link failed, symbolic link used"));
627 ifree(symlinkcontents);
630 err(EXIT_FAILURE, _("can't link from %s to %s"),
638 #define TIME_T_BITS_IN_FILE 64
646 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
648 max_time = -(min_time + 1);
652 itsdir(const char * const name)
657 myname = ecpyalloc(name);
658 myname = ecatalloc(myname, "/.");
659 accres = access(myname, F_OK);
665 ** Associate sets of rules with zones.
669 ** Sort by rule name.
673 rcomp(const void *cp1, const void *cp2)
675 return strcmp(((const struct rule *) cp1)->r_name,
676 ((const struct rule *) cp2)->r_name);
688 qsort((void *) rules, (size_t) nrules,
689 (size_t) sizeof *rules, rcomp);
690 for (i = 0; i < nrules - 1; ++i) {
691 if (strcmp(rules[i].r_name,
692 rules[i + 1].r_name) != 0)
694 if (strcmp(rules[i].r_filename,
695 rules[i + 1].r_filename) == 0)
697 eat(rules[i].r_filename, rules[i].r_linenum);
698 warning(_("same rule name in multiple files"));
699 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
700 warning(_("same rule name in multiple files"));
701 for (j = i + 2; j < nrules; ++j) {
702 if (strcmp(rules[i].r_name,
703 rules[j].r_name) != 0)
705 if (strcmp(rules[i].r_filename,
706 rules[j].r_filename) == 0)
708 if (strcmp(rules[i + 1].r_filename,
709 rules[j].r_filename) == 0)
716 for (i = 0; i < nzones; ++i) {
721 for (base = 0; base < nrules; base = out) {
723 for (out = base + 1; out < nrules; ++out)
724 if (strcmp(rp->r_name, rules[out].r_name) != 0)
726 for (i = 0; i < nzones; ++i) {
728 if (strcmp(zp->z_rule, rp->r_name) != 0)
731 zp->z_nrules = out - base;
734 for (i = 0; i < nzones; ++i) {
736 if (zp->z_nrules == 0) {
738 ** Maybe we have a local standard time offset.
740 eat(zp->z_filename, zp->z_linenum);
741 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
744 ** Note, though, that if there's no rule,
745 ** a '%s' in the format is a bad thing.
747 if (strchr(zp->z_format, '%') != 0)
748 error(_("%s in ruleless zone"));
756 infile(const char *name)
761 const struct lookup *lp;
767 if (strcmp(name, "-") == 0) {
768 name = _("standard input");
770 } else if ((fp = fopen(name, "r")) == NULL)
771 err(EXIT_FAILURE, _("can't open %s"), name);
773 for (num = 1; ; ++num) {
775 if (fgets(buf, (int) sizeof buf, fp) != buf)
777 cp = strchr(buf, '\n');
779 error(_("line too long"));
783 fields = getfields(buf);
785 while (fields[nfields] != NULL) {
788 if (strcmp(fields[nfields], "-") == 0)
789 fields[nfields] = &nada;
794 } else if (wantcont) {
795 wantcont = inzcont(fields, nfields);
797 lp = byword(fields[0], line_codes);
799 error(_("input line of unknown type"));
800 else switch ((int) (lp->l_value)) {
802 inrule(fields, nfields);
806 wantcont = inzone(fields, nfields);
809 inlink(fields, nfields);
815 _("leap line in non leap seconds file %s"), name);
816 else inleap(fields, nfields);
819 default: /* "cannot happen" */
821 _("panic: invalid l_value %d"), lp->l_value);
824 ifree((char *) fields);
827 errx(EXIT_FAILURE, _("error reading %s"), filename);
828 if (fp != stdin && fclose(fp))
829 err(EXIT_FAILURE, _("error closing %s"), filename);
831 error(_("expected continuation line not found"));
835 ** Convert a string of one of the forms
836 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
837 ** into a number of seconds.
838 ** A null string maps to zero.
839 ** Call error with errstring and return zero on errors.
843 gethms(const char *string, const char * const errstring, const int signable)
848 if (string == NULL || *string == '\0')
852 else if (*string == '-') {
856 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
858 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
860 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
861 &hh, &mm, &ss) != 3) {
866 mm < 0 || mm >= MINSPERHOUR ||
867 ss < 0 || ss > SECSPERMIN) {
871 if (LONG_MAX / SECSPERHOUR < hh) {
872 error(_("time overflow"));
875 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
876 warning(_("24:00 not handled by pre-1998 versions of zic"));
877 if (noise && (hh > HOURSPERDAY ||
878 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
879 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
880 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
881 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
885 inrule(char ** const fields, const int nfields)
887 static struct rule r;
889 if (nfields != RULE_FIELDS) {
890 error(_("wrong number of fields on Rule line"));
893 if (*fields[RF_NAME] == '\0') {
894 error(_("nameless rule"));
897 r.r_filename = filename;
898 r.r_linenum = linenum;
899 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
900 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
901 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
902 r.r_name = ecpyalloc(fields[RF_NAME]);
903 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
904 if (max_abbrvar_len < strlen(r.r_abbrvar))
905 max_abbrvar_len = strlen(r.r_abbrvar);
906 rules = (struct rule *) (void *) erealloc((char *) rules,
907 (int) ((nrules + 1) * sizeof *rules));
912 inzone(char ** const fields, const int nfields)
917 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
918 error(_("wrong number of fields on Zone line"));
921 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
922 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
924 _("\"Zone %s\" line and -l option are mutually exclusive"),
929 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
930 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
932 _("\"Zone %s\" line and -p option are mutually exclusive"),
937 for (i = 0; i < nzones; ++i)
938 if (zones[i].z_name != NULL &&
939 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
940 buf = erealloc(buf, (int) (132 +
941 strlen(fields[ZF_NAME]) +
942 strlen(zones[i].z_filename)));
944 _("duplicate zone name %s (file \"%s\", line %d)"),
951 return inzsub(fields, nfields, FALSE);
955 inzcont(char ** const fields, const int nfields)
957 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
958 error(_("wrong number of fields on Zone continuation line"));
961 return inzsub(fields, nfields, TRUE);
965 inzsub(char ** const fields, const int nfields, const int iscont)
968 static struct zone z;
969 int i_gmtoff, i_rule, i_format;
970 int i_untilyear, i_untilmonth;
971 int i_untilday, i_untiltime;
975 i_gmtoff = ZFC_GMTOFF;
977 i_format = ZFC_FORMAT;
978 i_untilyear = ZFC_TILYEAR;
979 i_untilmonth = ZFC_TILMONTH;
980 i_untilday = ZFC_TILDAY;
981 i_untiltime = ZFC_TILTIME;
984 i_gmtoff = ZF_GMTOFF;
986 i_format = ZF_FORMAT;
987 i_untilyear = ZF_TILYEAR;
988 i_untilmonth = ZF_TILMONTH;
989 i_untilday = ZF_TILDAY;
990 i_untiltime = ZF_TILTIME;
991 z.z_name = ecpyalloc(fields[ZF_NAME]);
993 z.z_filename = filename;
994 z.z_linenum = linenum;
995 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
996 if ((cp = strchr(fields[i_format], '%')) != 0) {
997 if (*++cp != 's' || strchr(cp, '%') != 0) {
998 error(_("invalid abbreviation format"));
1002 z.z_rule = ecpyalloc(fields[i_rule]);
1003 z.z_format = ecpyalloc(fields[i_format]);
1004 if (max_format_len < strlen(z.z_format))
1005 max_format_len = strlen(z.z_format);
1006 hasuntil = nfields > i_untilyear;
1008 z.z_untilrule.r_filename = filename;
1009 z.z_untilrule.r_linenum = linenum;
1010 rulesub(&z.z_untilrule,
1011 fields[i_untilyear],
1014 (nfields > i_untilmonth) ?
1015 fields[i_untilmonth] : "Jan",
1016 (nfields > i_untilday) ? fields[i_untilday] : "1",
1017 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1018 z.z_untiltime = rpytime(&z.z_untilrule,
1019 z.z_untilrule.r_loyear);
1020 if (iscont && nzones > 0 &&
1021 z.z_untiltime > min_time &&
1022 z.z_untiltime < max_time &&
1023 zones[nzones - 1].z_untiltime > min_time &&
1024 zones[nzones - 1].z_untiltime < max_time &&
1025 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1027 "Zone continuation line end time is not after end time of previous line"
1032 zones = (struct zone *) (void *) erealloc((char *) zones,
1033 (int) ((nzones + 1) * sizeof *zones));
1034 zones[nzones++] = z;
1036 ** If there was an UNTIL field on this line,
1037 ** there's more information about the zone on the next line.
1043 inleap(char ** const fields, const int nfields)
1046 const struct lookup *lp;
1048 int year, month, day;
1052 if (nfields != LEAP_FIELDS) {
1053 error(_("wrong number of fields on Leap line"));
1057 cp = fields[LP_YEAR];
1058 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1062 error(_("invalid leaping year"));
1065 if (!leapseen || leapmaxyear < year)
1067 if (!leapseen || leapminyear > year)
1073 i = len_years[isleap(j)];
1077 i = -len_years[isleap(j)];
1079 dayoff = oadd(dayoff, eitol(i));
1081 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1082 error(_("invalid month name"));
1085 month = lp->l_value;
1087 while (j != month) {
1088 i = len_months[isleap(year)][j];
1089 dayoff = oadd(dayoff, eitol(i));
1092 cp = fields[LP_DAY];
1093 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1094 day <= 0 || day > len_months[isleap(year)][month]) {
1095 error(_("invalid day of month"));
1098 dayoff = oadd(dayoff, eitol(day - 1));
1099 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1100 error(_("time before zero"));
1103 if (dayoff < min_time / SECSPERDAY) {
1104 error(_("time too small"));
1107 if (dayoff > max_time / SECSPERDAY) {
1108 error(_("time too large"));
1111 t = (zic_t) dayoff * SECSPERDAY;
1112 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1113 cp = fields[LP_CORR];
1118 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1121 } else if (strcmp(cp, "--") == 0) {
1124 } else if (strcmp(cp, "+") == 0) {
1127 } else if (strcmp(cp, "++") == 0) {
1131 error(_("illegal CORRECTION field on Leap line"));
1134 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1136 "illegal Rolling/Stationary field on Leap line"
1140 leapadd(tadd(t, tod), positive, lp->l_value, count);
1145 inlink(char ** const fields, const int nfields)
1149 if (nfields != LINK_FIELDS) {
1150 error(_("wrong number of fields on Link line"));
1153 if (*fields[LF_FROM] == '\0') {
1154 error(_("blank FROM field on Link line"));
1157 if (*fields[LF_TO] == '\0') {
1158 error(_("blank TO field on Link line"));
1161 l.l_filename = filename;
1162 l.l_linenum = linenum;
1163 l.l_from = ecpyalloc(fields[LF_FROM]);
1164 l.l_to = ecpyalloc(fields[LF_TO]);
1165 links = (struct link *) (void *) erealloc((char *) links,
1166 (int) ((nlinks + 1) * sizeof *links));
1167 links[nlinks++] = l;
1171 rulesub(struct rule * const rp,
1172 const char * const loyearp,
1173 const char * const hiyearp,
1174 const char * const typep,
1175 const char * const monthp,
1176 const char * const dayp,
1177 const char * const timep)
1179 const struct lookup *lp;
1184 if ((lp = byword(monthp, mon_names)) == NULL) {
1185 error(_("invalid month name"));
1188 rp->r_month = lp->l_value;
1189 rp->r_todisstd = FALSE;
1190 rp->r_todisgmt = FALSE;
1191 dp = ecpyalloc(timep);
1193 ep = dp + strlen(dp) - 1;
1194 switch (lowerit(*ep)) {
1195 case 's': /* Standard */
1196 rp->r_todisstd = TRUE;
1197 rp->r_todisgmt = FALSE;
1200 case 'w': /* Wall */
1201 rp->r_todisstd = FALSE;
1202 rp->r_todisgmt = FALSE;
1205 case 'g': /* Greenwich */
1206 case 'u': /* Universal */
1207 case 'z': /* Zulu */
1208 rp->r_todisstd = TRUE;
1209 rp->r_todisgmt = TRUE;
1214 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1220 lp = byword(cp, begin_years);
1221 rp->r_lowasnum = lp == NULL;
1222 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1224 rp->r_loyear = INT_MIN;
1227 rp->r_loyear = INT_MAX;
1229 default: /* "cannot happen" */
1231 _("panic: invalid l_value %d"), lp->l_value);
1232 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1233 error(_("invalid starting year"));
1237 lp = byword(cp, end_years);
1238 rp->r_hiwasnum = lp == NULL;
1239 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1241 rp->r_hiyear = INT_MIN;
1244 rp->r_hiyear = INT_MAX;
1247 rp->r_hiyear = rp->r_loyear;
1249 default: /* "cannot happen" */
1251 _("panic: invalid l_value %d"), lp->l_value);
1252 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1253 error(_("invalid ending year"));
1256 if (rp->r_loyear > rp->r_hiyear) {
1257 error(_("starting year greater than ending year"));
1261 rp->r_yrtype = NULL;
1263 if (rp->r_loyear == rp->r_hiyear) {
1264 error(_("typed single year"));
1267 rp->r_yrtype = ecpyalloc(typep);
1271 ** Accept things such as:
1277 dp = ecpyalloc(dayp);
1278 if ((lp = byword(dp, lasts)) != NULL) {
1279 rp->r_dycode = DC_DOWLEQ;
1280 rp->r_wday = lp->l_value;
1281 rp->r_dayofmonth = len_months[1][rp->r_month];
1283 if ((ep = strchr(dp, '<')) != 0)
1284 rp->r_dycode = DC_DOWLEQ;
1285 else if ((ep = strchr(dp, '>')) != 0)
1286 rp->r_dycode = DC_DOWGEQ;
1289 rp->r_dycode = DC_DOM;
1291 if (rp->r_dycode != DC_DOM) {
1294 error(_("invalid day of month"));
1298 if ((lp = byword(dp, wday_names)) == NULL) {
1299 error(_("invalid weekday name"));
1303 rp->r_wday = lp->l_value;
1305 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1306 rp->r_dayofmonth <= 0 ||
1307 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1308 error(_("invalid day of month"));
1317 convert(const long val, char * const buf)
1322 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1323 buf[i] = val >> shift;
1327 convert64(const zic_t val, char * const buf)
1332 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1333 buf[i] = val >> shift;
1337 puttzcode(const long val, FILE * const fp)
1342 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1346 puttzcode64(const zic_t val, FILE * const fp)
1350 convert64(val, buf);
1351 fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1355 atcomp(const void * avp, const void * bvp)
1357 const zic_t a = ((const struct attype *) avp)->at;
1358 const zic_t b = ((const struct attype *) bvp)->at;
1360 return (a < b) ? -1 : (a > b);
1366 return INT32_MIN <= x && x <= INT32_MAX;
1370 writezone(const char * const name, const char * const string)
1374 int leapcnt32, leapi32;
1375 int timecnt32, timei32;
1377 static char * fullname;
1378 static const struct tzhead tzh0;
1379 static struct tzhead tzh;
1380 zic_t ats[TZ_MAX_TIMES];
1381 unsigned char types[TZ_MAX_TIMES];
1387 qsort((void *) attypes, (size_t) timecnt,
1388 (size_t) sizeof *attypes, atcomp);
1398 while (fromi < timecnt && attypes[fromi].at < min_time)
1401 while (fromi < timecnt && attypes[fromi].type == 0)
1402 ++fromi; /* handled by default rule */
1403 for ( ; fromi < timecnt; ++fromi) {
1404 if (toi != 0 && ((attypes[fromi].at +
1405 gmtoffs[attypes[toi - 1].type]) <=
1406 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1407 : attypes[toi - 2].type]))) {
1408 attypes[toi - 1].type =
1409 attypes[fromi].type;
1413 attypes[toi - 1].type != attypes[fromi].type)
1414 attypes[toi++] = attypes[fromi];
1421 for (i = 0; i < timecnt; ++i) {
1422 ats[i] = attypes[i].at;
1423 types[i] = attypes[i].type;
1426 ** Correct for leap seconds.
1428 for (i = 0; i < timecnt; ++i) {
1431 if (ats[i] > trans[j] - corr[j]) {
1432 ats[i] = tadd(ats[i], corr[j]);
1437 ** Figure out 32-bit-limited starts and counts.
1439 timecnt32 = timecnt;
1441 leapcnt32 = leapcnt;
1443 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1445 while (timecnt32 > 0 && !is32(ats[timei32])) {
1449 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1451 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1455 fullname = erealloc(fullname,
1456 (int) (strlen(directory) + 1 + strlen(name) + 1));
1457 sprintf(fullname, "%s/%s", directory, name);
1459 ** Remove old file, if any, to snap links.
1461 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1462 err(EXIT_FAILURE, _("can't remove %s"), fullname);
1463 if ((fp = fopen(fullname, "wb")) == NULL) {
1464 if (mkdirs(fullname) != 0)
1466 if ((fp = fopen(fullname, "wb")) == NULL)
1467 err(EXIT_FAILURE, _("can't create %s"), fullname);
1469 for (pass = 1; pass <= 2; ++pass) {
1470 int thistimei, thistimecnt;
1471 int thisleapi, thisleapcnt;
1472 int thistimelim, thisleaplim;
1473 int writetype[TZ_MAX_TIMES];
1474 int typemap[TZ_MAX_TYPES];
1476 char thischars[TZ_MAX_CHARS];
1478 int indmap[TZ_MAX_CHARS];
1481 thistimei = timei32;
1482 thistimecnt = timecnt32;
1483 thisleapi = leapi32;
1484 thisleapcnt = leapcnt32;
1487 thistimecnt = timecnt;
1489 thisleapcnt = leapcnt;
1491 thistimelim = thistimei + thistimecnt;
1492 thisleaplim = thisleapi + thisleapcnt;
1493 for (i = 0; i < typecnt; ++i)
1494 writetype[i] = thistimecnt == timecnt;
1495 if (thistimecnt == 0) {
1497 ** No transition times fall in the current
1498 ** (32- or 64-bit) window.
1501 writetype[typecnt - 1] = TRUE;
1503 for (i = thistimei - 1; i < thistimelim; ++i)
1505 writetype[types[i]] = TRUE;
1507 ** For America/Godthab and Antarctica/Palmer
1510 writetype[0] = TRUE;
1513 ** For some pre-2011 systems: if the last-to-be-written
1514 ** standard (or daylight) type has an offset different from the
1515 ** most recently used offset,
1516 ** append an (unused) copy of the most recently used type
1517 ** (to help get global "altzone" and "timezone" variables
1521 int mrudst, mrustd, hidst, histd, type;
1523 hidst = histd = mrudst = mrustd = -1;
1524 for (i = thistimei; i < thistimelim; ++i)
1525 if (isdsts[types[i]])
1527 else mrustd = types[i];
1528 for (i = 0; i < typecnt; ++i)
1534 if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1535 gmtoffs[hidst] != gmtoffs[mrudst]) {
1536 isdsts[mrudst] = -1;
1537 type = addtype(gmtoffs[mrudst],
1538 &chars[abbrinds[mrudst]],
1542 isdsts[mrudst] = TRUE;
1543 writetype[type] = TRUE;
1545 if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1546 gmtoffs[histd] != gmtoffs[mrustd]) {
1547 isdsts[mrustd] = -1;
1548 type = addtype(gmtoffs[mrustd],
1549 &chars[abbrinds[mrustd]],
1553 isdsts[mrustd] = FALSE;
1554 writetype[type] = TRUE;
1558 for (i = 0; i < typecnt; ++i)
1559 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1560 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1563 for (i = 0; i < typecnt; ++i) {
1568 if (indmap[abbrinds[i]] >= 0)
1570 thisabbr = &chars[abbrinds[i]];
1571 for (j = 0; j < thischarcnt; ++j)
1572 if (strcmp(&thischars[j], thisabbr) == 0)
1574 if (j == thischarcnt) {
1575 strcpy(&thischars[(int) thischarcnt],
1577 thischarcnt += strlen(thisabbr) + 1;
1579 indmap[abbrinds[i]] = j;
1581 #define DO(field) fwrite((void *) tzh.field, \
1582 (size_t) sizeof tzh.field, (size_t) 1, fp)
1584 strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1585 tzh.tzh_version[0] = ZIC_VERSION;
1586 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1587 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1588 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1589 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1590 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1591 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1602 for (i = thistimei; i < thistimelim; ++i)
1604 puttzcode((long) ats[i], fp);
1605 else puttzcode64(ats[i], fp);
1606 for (i = thistimei; i < thistimelim; ++i) {
1609 uc = typemap[types[i]];
1610 fwrite((void *) &uc,
1615 for (i = 0; i < typecnt; ++i)
1617 puttzcode(gmtoffs[i], fp);
1618 putc(isdsts[i], fp);
1619 putc((unsigned char) indmap[abbrinds[i]], fp);
1621 if (thischarcnt != 0)
1622 fwrite((void *) thischars,
1623 (size_t) sizeof thischars[0],
1624 (size_t) thischarcnt, fp);
1625 for (i = thisleapi; i < thisleaplim; ++i) {
1626 register zic_t todo;
1629 if (timecnt == 0 || trans[i] < ats[0]) {
1632 if (++j >= typecnt) {
1638 while (j < timecnt &&
1643 todo = tadd(trans[i], -gmtoffs[j]);
1644 } else todo = trans[i];
1646 puttzcode((long) todo, fp);
1647 else puttzcode64(todo, fp);
1648 puttzcode(corr[i], fp);
1650 for (i = 0; i < typecnt; ++i)
1652 putc(ttisstds[i], fp);
1653 for (i = 0; i < typecnt; ++i)
1655 putc(ttisgmts[i], fp);
1657 fprintf(fp, "\n%s\n", string);
1658 if (ferror(fp) || fclose(fp))
1659 errx(EXIT_FAILURE, _("error writing %s"), fullname);
1660 if (chmod(fullname, mflag) < 0)
1661 err(EXIT_FAILURE, _("cannot change mode of %s to %03o"),
1662 fullname, (unsigned)mflag);
1663 if ((uflag != (uid_t)-1 || gflag != (gid_t)-1)
1664 && chown(fullname, uflag, gflag) < 0)
1665 err(EXIT_FAILURE, _("cannot change ownership of %s"),
1670 doabbr(char * const abbr, const char * const format,
1671 const char * const letters, const int isdst, const int doquotes)
1677 slashp = strchr(format, '/');
1678 if (slashp == NULL) {
1679 if (letters == NULL)
1680 strcpy(abbr, format);
1681 else sprintf(abbr, format, letters);
1683 strcpy(abbr, slashp + 1);
1685 if (slashp > format)
1686 strncpy(abbr, format,
1687 (unsigned) (slashp - format));
1688 abbr[slashp - format] = '\0';
1692 for (cp = abbr; *cp != '\0'; ++cp)
1693 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1694 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1697 if (len > 0 && *cp == '\0')
1699 abbr[len + 2] = '\0';
1700 abbr[len + 1] = '>';
1701 for ( ; len > 0; --len)
1702 abbr[len] = abbr[len - 1];
1707 updateminmax(const int x)
1716 stringoffset(char *result, long offset)
1724 strcpy(result, "-");
1727 seconds = offset % SECSPERMIN;
1728 offset /= SECSPERMIN;
1729 minutes = offset % MINSPERHOUR;
1730 offset /= MINSPERHOUR;
1732 if (hours >= HOURSPERDAY) {
1736 sprintf(end(result), "%d", hours);
1737 if (minutes != 0 || seconds != 0) {
1738 sprintf(end(result), ":%02d", minutes);
1740 sprintf(end(result), ":%02d", seconds);
1746 stringrule(char *result, const struct rule * const rp, const long dstoff,
1751 result = end(result);
1752 if (rp->r_dycode == DC_DOM) {
1755 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1758 for (month = 0; month < rp->r_month; ++month)
1759 total += len_months[0][month];
1760 sprintf(result, "J%d", total + rp->r_dayofmonth);
1764 if (rp->r_dycode == DC_DOWGEQ) {
1765 if ((rp->r_dayofmonth % DAYSPERWEEK) != 1)
1767 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1768 } else if (rp->r_dycode == DC_DOWLEQ) {
1769 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1772 if ((rp->r_dayofmonth % DAYSPERWEEK) != 0)
1774 week = rp->r_dayofmonth / DAYSPERWEEK;
1776 } else return -1; /* "cannot happen" */
1777 sprintf(result, "M%d.%d.%d",
1778 rp->r_month + 1, week, rp->r_wday);
1783 if (rp->r_todisstd && rp->r_stdoff == 0)
1789 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1790 strcat(result, "/");
1791 if (stringoffset(end(result), tod) != 0)
1798 stringzone(char *result, const struct zone * const zpfirst,
1799 const int zonecount)
1801 const struct zone * zp;
1803 struct rule * stdrp;
1804 struct rule * dstrp;
1806 const char * abbrvar;
1809 zp = zpfirst + zonecount - 1;
1810 stdrp = dstrp = NULL;
1811 for (i = 0; i < zp->z_nrules; ++i) {
1812 rp = &zp->z_rules[i];
1813 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1815 if (rp->r_yrtype != NULL)
1817 if (rp->r_stdoff == 0) {
1827 if (stdrp == NULL && dstrp == NULL) {
1829 ** There are no rules running through "max".
1830 ** Let's find the latest rule.
1832 for (i = 0; i < zp->z_nrules; ++i) {
1833 rp = &zp->z_rules[i];
1834 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1835 (rp->r_hiyear == stdrp->r_hiyear &&
1836 rp->r_month > stdrp->r_month))
1839 if (stdrp != NULL && stdrp->r_stdoff != 0)
1840 return; /* We end up in DST (a POSIX no-no). */
1842 ** Horrid special case: if year is 2037,
1843 ** presume this is a zone handled on a year-by-year basis;
1844 ** do not try to apply a rule to the zone.
1846 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1849 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1851 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1852 doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1853 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1859 doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1860 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1861 if (stringoffset(end(result),
1862 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1866 strcat(result, ",");
1867 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1871 strcat(result, ",");
1872 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1879 outzone(const struct zone * const zpfirst, const int zonecount)
1881 const struct zone *zp;
1884 int usestart, useuntil;
1885 zic_t starttime, untiltime;
1898 int prodstic; /* all rules are min to max */
1900 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1901 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1902 startbuf = emalloc(max_abbr_len + 1);
1903 ab = emalloc(max_abbr_len + 1);
1904 envvar = emalloc(max_envvar_len + 1);
1905 INITIALIZE(untiltime);
1906 INITIALIZE(starttime);
1908 ** Now. . .finally. . .generate some useful data!
1913 prodstic = zonecount == 1;
1915 ** Thanks to Earl Chew
1916 ** for noting the need to unconditionally initialize startttisstd.
1918 startttisstd = FALSE;
1919 startttisgmt = FALSE;
1920 min_year = max_year = EPOCH_YEAR;
1922 updateminmax(leapminyear);
1923 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
1925 for (i = 0; i < zonecount; ++i) {
1927 if (i < zonecount - 1)
1928 updateminmax(zp->z_untilrule.r_loyear);
1929 for (j = 0; j < zp->z_nrules; ++j) {
1930 rp = &zp->z_rules[j];
1932 updateminmax(rp->r_loyear);
1934 updateminmax(rp->r_hiyear);
1935 if (rp->r_lowasnum || rp->r_hiwasnum)
1940 ** Generate lots of data if a rule can't cover all future times.
1942 stringzone(envvar, zpfirst, zonecount);
1943 if (noise && envvar[0] == '\0') {
1946 wp = ecpyalloc(_("no POSIX environment variable for zone"));
1947 wp = ecatalloc(wp, " ");
1948 wp = ecatalloc(wp, zpfirst->z_name);
1952 if (envvar[0] == '\0') {
1953 if (min_year >= INT_MIN + YEARSPERREPEAT)
1954 min_year -= YEARSPERREPEAT;
1955 else min_year = INT_MIN;
1956 if (max_year <= INT_MAX - YEARSPERREPEAT)
1957 max_year += YEARSPERREPEAT;
1958 else max_year = INT_MAX;
1960 ** Regardless of any of the above,
1961 ** for a "proDSTic" zone which specifies that its rules
1962 ** always have and always will be in effect,
1963 ** we only need one cycle to define the zone.
1967 max_year = min_year + YEARSPERREPEAT;
1971 ** For the benefit of older systems,
1972 ** generate data from 1900 through 2037.
1974 if (min_year > 1900)
1976 if (max_year < 2037)
1978 for (i = 0; i < zonecount; ++i) {
1980 ** A guess that may well be corrected later.
1984 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1985 useuntil = i < (zonecount - 1);
1986 if (useuntil && zp->z_untiltime <= min_time)
1988 gmtoff = zp->z_gmtoff;
1989 eat(zp->z_filename, zp->z_linenum);
1991 startoff = zp->z_gmtoff;
1992 if (zp->z_nrules == 0) {
1993 stdoff = zp->z_stdoff;
1994 doabbr(startbuf, zp->z_format,
1995 NULL, stdoff != 0, FALSE);
1996 type = addtype(oadd(zp->z_gmtoff, stdoff),
1997 startbuf, stdoff != 0, startttisstd,
2000 addtt(starttime, type);
2002 } else if (stdoff != 0)
2003 addtt(min_time, type);
2004 } else for (year = min_year; year <= max_year; ++year) {
2005 if (useuntil && year > zp->z_untilrule.r_hiyear)
2008 ** Mark which rules to do in the current year.
2009 ** For those to do, calculate rpytime(rp, year);
2011 for (j = 0; j < zp->z_nrules; ++j) {
2012 rp = &zp->z_rules[j];
2013 eats(zp->z_filename, zp->z_linenum,
2014 rp->r_filename, rp->r_linenum);
2015 rp->r_todo = year >= rp->r_loyear &&
2016 year <= rp->r_hiyear &&
2017 yearistype(year, rp->r_yrtype);
2019 rp->r_temp = rpytime(rp, year);
2029 ** Turn untiltime into UTC
2030 ** assuming the current gmtoff and
2033 untiltime = zp->z_untiltime;
2034 if (!zp->z_untilrule.r_todisgmt)
2035 untiltime = tadd(untiltime,
2037 if (!zp->z_untilrule.r_todisstd)
2038 untiltime = tadd(untiltime,
2042 ** Find the rule (of those to do, if any)
2043 ** that takes effect earliest in the year.
2046 for (j = 0; j < zp->z_nrules; ++j) {
2047 rp = &zp->z_rules[j];
2050 eats(zp->z_filename, zp->z_linenum,
2051 rp->r_filename, rp->r_linenum);
2052 offset = rp->r_todisgmt ? 0 : gmtoff;
2053 if (!rp->r_todisstd)
2054 offset = oadd(offset, stdoff);
2056 if (jtime == min_time ||
2059 jtime = tadd(jtime, -offset);
2060 if (k < 0 || jtime < ktime) {
2066 break; /* go on to next year */
2067 rp = &zp->z_rules[k];
2069 if (useuntil && ktime >= untiltime)
2071 stdoff = rp->r_stdoff;
2072 if (usestart && ktime == starttime)
2075 if (ktime < starttime) {
2076 startoff = oadd(zp->z_gmtoff,
2078 doabbr(startbuf, zp->z_format,
2084 if (*startbuf == '\0' &&
2085 startoff == oadd(zp->z_gmtoff,
2095 eats(zp->z_filename, zp->z_linenum,
2096 rp->r_filename, rp->r_linenum);
2097 doabbr(ab, zp->z_format, rp->r_abbrvar,
2098 rp->r_stdoff != 0, FALSE);
2099 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2100 type = addtype(offset, ab, rp->r_stdoff != 0,
2101 rp->r_todisstd, rp->r_todisgmt);
2106 if (*startbuf == '\0' &&
2107 zp->z_format != NULL &&
2108 strchr(zp->z_format, '%') == NULL &&
2109 strchr(zp->z_format, '/') == NULL)
2110 strcpy(startbuf, zp->z_format);
2111 eat(zp->z_filename, zp->z_linenum);
2112 if (*startbuf == '\0')
2113 error(_("can't determine time zone abbreviation to use just after until time"));
2114 else addtt(starttime,
2115 addtype(startoff, startbuf,
2116 startoff != zp->z_gmtoff,
2121 ** Now we may get to set starttime for the next zone line.
2124 startttisstd = zp->z_untilrule.r_todisstd;
2125 startttisgmt = zp->z_untilrule.r_todisgmt;
2126 starttime = zp->z_untiltime;
2128 starttime = tadd(starttime, -stdoff);
2130 starttime = tadd(starttime, -gmtoff);
2133 writezone(zpfirst->z_name, envvar);
2140 addtt(const zic_t starttime, int type)
2142 if (starttime <= min_time ||
2143 (timecnt == 1 && attypes[0].at < min_time)) {
2144 gmtoffs[0] = gmtoffs[type];
2145 isdsts[0] = isdsts[type];
2146 ttisstds[0] = ttisstds[type];
2147 ttisgmts[0] = ttisgmts[type];
2148 if (abbrinds[type] != 0)
2149 strcpy(chars, &chars[abbrinds[type]]);
2151 charcnt = strlen(chars) + 1;
2156 if (timecnt >= TZ_MAX_TIMES) {
2157 error(_("too many transitions?!"));
2160 attypes[timecnt].at = starttime;
2161 attypes[timecnt].type = type;
2166 addtype(const long gmtoff, const char * const abbr, const int isdst,
2167 const int ttisstd, const int ttisgmt)
2171 if (isdst != TRUE && isdst != FALSE) {
2172 error(_("internal error - addtype called with bad isdst"));
2175 if (ttisstd != TRUE && ttisstd != FALSE) {
2176 error(_("internal error - addtype called with bad ttisstd"));
2179 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2180 error(_("internal error - addtype called with bad ttisgmt"));
2184 ** See if there's already an entry for this zone type.
2185 ** If so, just return its index.
2187 for (i = 0; i < typecnt; ++i) {
2188 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2189 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2190 ttisstd == ttisstds[i] &&
2191 ttisgmt == ttisgmts[i])
2195 ** There isn't one; add a new one, unless there are already too
2198 if (typecnt >= TZ_MAX_TYPES) {
2199 error(_("too many local time types"));
2202 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2203 error(_("UTC offset out of range"));
2206 gmtoffs[i] = gmtoff;
2208 ttisstds[i] = ttisstd;
2209 ttisgmts[i] = ttisgmt;
2211 for (j = 0; j < charcnt; ++j)
2212 if (strcmp(&chars[j], abbr) == 0)
2222 leapadd(const zic_t t, const int positive, const int rolling, int count)
2226 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2227 error(_("too many leap seconds"));
2230 for (i = 0; i < leapcnt; ++i)
2231 if (t <= trans[i]) {
2232 if (t == trans[i]) {
2233 error(_("repeated leap second moment"));
2239 for (j = leapcnt; j > i; --j) {
2240 trans[j] = trans[j - 1];
2241 corr[j] = corr[j - 1];
2242 roll[j] = roll[j - 1];
2245 corr[i] = positive ? 1L : eitol(-count);
2248 } while (positive && --count != 0);
2258 ** propagate leap seconds forward
2260 for (i = 0; i < leapcnt; ++i) {
2261 trans[i] = tadd(trans[i], last);
2262 last = corr[i] += last;
2267 yearistype(const int year, const char * const type)
2272 if (type == NULL || *type == '\0')
2274 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2275 sprintf(buf, "%s %d %s", yitcommand, year, type);
2276 result = system(buf);
2277 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2283 error(_("Wild result from command execution"));
2284 warnx(_("command was '%s', result was %d"), buf, result);
2292 a = (unsigned char) a;
2293 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2297 ciequal(const char *ap, const char *bp) /* case-insensitive equality */
2299 while (lowerit(*ap) == lowerit(*bp++))
2306 itsabbr(const char *abbr, const char *word)
2308 if (lowerit(*abbr) != lowerit(*word))
2311 while (*++abbr != '\0')
2315 } while (lowerit(*word++) != lowerit(*abbr));
2319 static const struct lookup *
2320 byword(const char * const word, const struct lookup * const table)
2322 const struct lookup *foundlp;
2323 const struct lookup *lp;
2325 if (word == NULL || table == NULL)
2328 ** Look for exact match.
2330 for (lp = table; lp->l_word != NULL; ++lp)
2331 if (ciequal(word, lp->l_word))
2334 ** Look for inexact match.
2337 for (lp = table; lp->l_word != NULL; ++lp)
2338 if (itsabbr(word, lp->l_word)) {
2339 if (foundlp == NULL)
2341 else return NULL; /* multiple inexact matches */
2355 array = (char **) (void *)
2356 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2359 while (isascii((unsigned char) *cp) &&
2360 isspace((unsigned char) *cp))
2362 if (*cp == '\0' || *cp == '#')
2364 array[nsubs++] = dp = cp;
2366 if ((*dp = *cp++) != '"')
2368 else while ((*dp = *cp++) != '"')
2373 "Odd number of quotation marks"
2377 } while (*cp != '\0' && *cp != '#' &&
2378 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2379 if (isascii(*cp) && isspace((unsigned char) *cp))
2383 array[nsubs] = NULL;
2388 oadd(const long t1, const long t2)
2393 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2394 error(_("time overflow"));
2401 tadd(const zic_t t1, const long t2)
2405 if (t1 == max_time && t2 > 0)
2407 if (t1 == min_time && t2 < 0)
2410 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2411 error(_("time overflow"));
2418 ** Given a rule, and a year, compute the date - in seconds since January 1,
2419 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2423 rpytime(const struct rule * const rp, const int wantedy)
2426 long dayoff; /* with a nod to Margaret O. */
2429 if (wantedy == INT_MIN)
2431 if (wantedy == INT_MAX)
2436 while (wantedy != y) {
2438 i = len_years[isleap(y)];
2442 i = -len_years[isleap(y)];
2444 dayoff = oadd(dayoff, eitol(i));
2446 while (m != rp->r_month) {
2447 i = len_months[isleap(y)][m];
2448 dayoff = oadd(dayoff, eitol(i));
2451 i = rp->r_dayofmonth;
2452 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2453 if (rp->r_dycode == DC_DOWLEQ)
2456 error(_("use of 2/29 in non leap-year"));
2461 dayoff = oadd(dayoff, eitol(i));
2462 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2465 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2466 wday = eitol(EPOCH_WDAY);
2468 ** Don't trust mod of negative numbers.
2471 wday = (wday + dayoff) % LDAYSPERWEEK;
2473 wday -= ((-dayoff) % LDAYSPERWEEK);
2475 wday += LDAYSPERWEEK;
2477 while (wday != eitol(rp->r_wday))
2478 if (rp->r_dycode == DC_DOWGEQ) {
2479 dayoff = oadd(dayoff, (long) 1);
2480 if (++wday >= LDAYSPERWEEK)
2484 dayoff = oadd(dayoff, (long) -1);
2486 wday = LDAYSPERWEEK - 1;
2489 if (i < 0 || i >= len_months[isleap(y)][m]) {
2491 warning(_("rule goes past start/end of month--\
2492 will not work with pre-2004 versions of zic"));
2495 if (dayoff < min_time / SECSPERDAY)
2497 if (dayoff > max_time / SECSPERDAY)
2499 t = (zic_t) dayoff * SECSPERDAY;
2500 return tadd(t, rp->r_tod);
2504 newabbr(const char * const string)
2508 if (strcmp(string, GRANDPARENTED) != 0) {
2513 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2514 ** optionally followed by a + or - and a number from 1 to 14.
2518 while (isascii((unsigned char) *cp) &&
2519 isalpha((unsigned char) *cp))
2521 if (cp - string == 0)
2522 wp = _("time zone abbreviation lacks alphabetic at start");
2523 if (noise && cp - string > 3)
2524 wp = _("time zone abbreviation has more than 3 alphabetics");
2525 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2526 wp = _("time zone abbreviation has too many alphabetics");
2527 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2529 if (isascii((unsigned char) *cp) &&
2530 isdigit((unsigned char) *cp))
2532 *cp >= '0' && *cp <= '4')
2536 wp = _("time zone abbreviation differs from POSIX standard");
2539 wp = ecatalloc(wp, " (");
2540 wp = ecatalloc(wp, string);
2541 wp = ecatalloc(wp, ")");
2546 i = strlen(string) + 1;
2547 if (charcnt + i > TZ_MAX_CHARS) {
2548 error(_("too many, or too long, time zone abbreviations"));
2551 strcpy(&chars[charcnt], string);
2552 charcnt += eitol(i);
2556 mkdirs(char *argname)
2561 if (argname == NULL || *argname == '\0' || Dflag)
2563 cp = name = ecpyalloc(argname);
2564 while ((cp = strchr(cp + 1, '/')) != 0) {
2568 ** DOS drive specifier?
2570 if (isalpha((unsigned char) name[0]) &&
2571 name[1] == ':' && name[2] == '\0') {
2575 #endif /* !defined unix */
2576 if (!itsdir(name)) {
2578 ** It doesn't seem to exist, so we try to create it.
2579 ** Creation may fail because of the directory being
2580 ** created by some other multiprocessor, so we get
2581 ** to do extra checking.
2583 if ((mkdir(name, MKDIR_UMASK) != 0) &&
2584 (errno != EEXIST || !itsdir(name))) {
2585 warn(_("can't create directory %s"), name);
2602 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2603 errx(EXIT_FAILURE, _("%d did not sign extend correctly"), i);
2611 setgroup(gid_t *flag, const char *name)
2615 if (*flag != (gid_t)-1)
2616 errx(EXIT_FAILURE, _("multiple -g flags specified"));
2618 gr = getgrnam(name);
2623 ul = strtoul(name, &ep, 10);
2624 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2628 errx(EXIT_FAILURE, _("group `%s' not found"), name);
2634 setuser(uid_t *flag, const char *name)
2638 if (*flag != (gid_t)-1)
2639 errx(EXIT_FAILURE, _("multiple -u flags specified"));
2641 pw = getpwnam(name);
2646 ul = strtoul(name, &ep, 10);
2647 if (ul == (unsigned long)(gid_t)ul && *ep == '\0') {
2651 errx(EXIT_FAILURE, _("user `%s' not found"), name);
2657 ** UNIX was a registered trademark of The Open Group in 2003.