2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
6 static char elsieid
[] = "@(#)zic.c 8.17";
12 #define ZIC_VERSION '2'
14 typedef int_fast64_t zic_t
;
16 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
17 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
18 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
24 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
26 #define MKDIR_UMASK 0755
30 ** On some ancient hosts, predicates like `isspace(C)' are defined
31 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
32 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
33 ** Neither the C Standard nor Posix require that `isascii' exist.
34 ** For portability, we check both ancient and modern requirements.
35 ** If isascii is not defined, the isascii check succeeds trivially.
42 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
43 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
45 #define end(cp) (strchr((cp), '\0'))
48 const char * r_filename
;
52 int r_loyear
; /* for example, 1986 */
53 int r_hiyear
; /* for example, 1986 */
54 const char * r_yrtype
;
58 int r_month
; /* 0..11 */
60 int r_dycode
; /* see below */
64 long r_tod
; /* time from midnight */
65 int r_todisstd
; /* above is standard time if TRUE */
66 /* or wall clock time if FALSE */
67 int r_todisgmt
; /* above is GMT if TRUE */
68 /* or local time if FALSE */
69 long r_stdoff
; /* offset from standard time */
70 const char * r_abbrvar
; /* variable part of abbreviation */
72 int r_todo
; /* a rule to do (used in outzone) */
73 zic_t r_temp
; /* used in outzone */
77 ** r_dycode r_dayofmonth r_wday
80 #define DC_DOM 0 /* 1..31 */ /* unused */
81 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
82 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
85 const char * z_filename
;
91 const char * z_format
;
95 struct rule
* z_rules
;
98 struct rule z_untilrule
;
102 extern int getopt(int argc
, char * const argv
[],
103 const char * options
);
104 extern int link(const char * fromname
, const char * toname
);
105 extern char * optarg
;
108 static void addtt(zic_t starttime
, int type
);
109 static int addtype(long gmtoff
, const char * abbr
, int isdst
,
110 int ttisstd
, int ttisgmt
);
111 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
112 static void adjleap(void);
113 static void associate(void);
114 static int ciequal(const char * ap
, const char * bp
);
115 static void convert(long val
, char * buf
);
116 static void convert64(zic_t val
, char * buf
);
117 static void dolink(const char * fromfield
, const char * tofield
);
118 static void doabbr(char * abbr
, const char * format
,
119 const char * letters
, int isdst
, int doquotes
);
120 static void eat(const char * name
, int num
);
121 static void eats(const char * name
, int num
,
122 const char * rname
, int rnum
);
123 static long eitol(int i
);
124 static void error(const char * message
);
125 static char ** getfields(char * buf
);
126 static long gethms(const char * string
, const char * errstrng
,
128 static void infile(const char * filename
);
129 static void inleap(char ** fields
, int nfields
);
130 static void inlink(char ** fields
, int nfields
);
131 static void inrule(char ** fields
, int nfields
);
132 static int inzcont(char ** fields
, int nfields
);
133 static int inzone(char ** fields
, int nfields
);
134 static int inzsub(char ** fields
, int nfields
, int iscont
);
135 static int is32(zic_t x
);
136 static int itsabbr(const char * abbr
, const char * word
);
137 static int itsdir(const char * name
);
138 static int lowerit(int c
);
139 static char * memcheck(char * tocheck
);
140 static int mkdirs(char * filename
);
141 static void newabbr(const char * abbr
);
142 static long oadd(long t1
, long t2
);
143 static void outzone(const struct zone
* zp
, int ntzones
);
144 static void puttzcode(long code
, FILE * fp
);
145 static void puttzcode64(zic_t code
, FILE * fp
);
146 static int rcomp(const void * leftp
, const void * rightp
);
147 static zic_t
rpytime(const struct rule
* rp
, int wantedy
);
148 static void rulesub(struct rule
* rp
,
149 const char * loyearp
, const char * hiyearp
,
150 const char * typep
, const char * monthp
,
151 const char * dayp
, const char * timep
);
152 static int stringoffset(char * result
, long offset
);
153 static int stringrule(char * result
, const struct rule
* rp
,
154 long dstoff
, long gmtoff
);
155 static void stringzone(char * result
,
156 const struct zone
* zp
, int ntzones
);
157 static void setboundaries(void);
158 static zic_t
tadd(zic_t t1
, long t2
);
159 static void usage(void);
160 static void writezone(const char * name
, const char * string
);
161 static int yearistype(int year
, const char * type
);
165 static const char * filename
;
168 static int leapminyear
;
169 static int leapmaxyear
;
171 static int max_abbrvar_len
;
172 static int max_format_len
;
173 static zic_t max_time
;
175 static zic_t min_time
;
178 static const char * rfilename
;
180 static const char * progname
;
194 ** Which fields are which on a Zone line.
202 #define ZF_TILMONTH 6
205 #define ZONE_MINFIELDS 5
206 #define ZONE_MAXFIELDS 9
209 ** Which fields are which on a Zone continuation line.
215 #define ZFC_TILYEAR 3
216 #define ZFC_TILMONTH 4
218 #define ZFC_TILTIME 6
219 #define ZONEC_MINFIELDS 3
220 #define ZONEC_MAXFIELDS 7
223 ** Which files are which on a Rule line.
235 #define RULE_FIELDS 10
238 ** Which fields are which on a Link line.
243 #define LINK_FIELDS 3
246 ** Which fields are which on a Leap line.
255 #define LEAP_FIELDS 7
265 static struct rule
* rules
;
266 static int nrules
; /* number of rules */
268 static struct zone
* zones
;
269 static int nzones
; /* number of zones */
272 const char * l_filename
;
278 static struct link
* links
;
286 static struct lookup
const * byword(const char * string
,
287 const struct lookup
* lp
);
289 static struct lookup
const line_codes
[] = {
297 static struct lookup
const mon_names
[] = {
298 { "January", TM_JANUARY
},
299 { "February", TM_FEBRUARY
},
300 { "March", TM_MARCH
},
301 { "April", TM_APRIL
},
305 { "August", TM_AUGUST
},
306 { "September", TM_SEPTEMBER
},
307 { "October", TM_OCTOBER
},
308 { "November", TM_NOVEMBER
},
309 { "December", TM_DECEMBER
},
313 static struct lookup
const wday_names
[] = {
314 { "Sunday", TM_SUNDAY
},
315 { "Monday", TM_MONDAY
},
316 { "Tuesday", TM_TUESDAY
},
317 { "Wednesday", TM_WEDNESDAY
},
318 { "Thursday", TM_THURSDAY
},
319 { "Friday", TM_FRIDAY
},
320 { "Saturday", TM_SATURDAY
},
324 static struct lookup
const lasts
[] = {
325 { "last-Sunday", TM_SUNDAY
},
326 { "last-Monday", TM_MONDAY
},
327 { "last-Tuesday", TM_TUESDAY
},
328 { "last-Wednesday", TM_WEDNESDAY
},
329 { "last-Thursday", TM_THURSDAY
},
330 { "last-Friday", TM_FRIDAY
},
331 { "last-Saturday", TM_SATURDAY
},
335 static struct lookup
const begin_years
[] = {
336 { "minimum", YR_MINIMUM
},
337 { "maximum", YR_MAXIMUM
},
341 static struct lookup
const end_years
[] = {
342 { "minimum", YR_MINIMUM
},
343 { "maximum", YR_MAXIMUM
},
348 static struct lookup
const leap_types
[] = {
350 { "Stationary", FALSE
},
354 static const int len_months
[2][MONSPERYEAR
] = {
355 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
356 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
359 static const int len_years
[2] = {
360 DAYSPERNYEAR
, DAYSPERLYEAR
363 static struct attype
{
366 } attypes
[TZ_MAX_TIMES
];
367 static long gmtoffs
[TZ_MAX_TYPES
];
368 static char isdsts
[TZ_MAX_TYPES
];
369 static unsigned char abbrinds
[TZ_MAX_TYPES
];
370 static char ttisstds
[TZ_MAX_TYPES
];
371 static char ttisgmts
[TZ_MAX_TYPES
];
372 static char chars
[TZ_MAX_CHARS
];
373 static zic_t trans
[TZ_MAX_LEAPS
];
374 static long corr
[TZ_MAX_LEAPS
];
375 static char roll
[TZ_MAX_LEAPS
];
378 ** Memory allocation.
386 const char *e
= strerror(errno
);
388 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
395 #define emalloc(size) memcheck(imalloc(size))
396 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
397 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
398 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
405 eats(name
, num
, rname
, rnum
)
406 const char * const name
;
408 const char * const rname
;
419 const char * const name
;
422 eats(name
, num
, (char *) NULL
, -1);
427 const char * const string
;
430 ** Match the format of "cc" to allow sh users to
431 ** zic ... 2>&1 | error -t "*" -v
434 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
435 filename
, linenum
, string
);
436 if (rfilename
!= NULL
)
437 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
438 rfilename
, rlinenum
);
439 (void) fprintf(stderr
, "\n");
445 const char * const string
;
449 cp
= ecpyalloc(_("warning: "));
450 cp
= ecatalloc(cp
, string
);
459 (void) fprintf(stderr
, _("%s: usage is %s \
460 [ --version ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
461 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
466 static const char * psxrules
;
467 static const char * lcltime
;
468 static const char * directory
;
469 static const char * leapsec
;
470 static const char * yitcommand
;
482 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
483 #endif /* defined unix */
485 (void) setlocale(LC_ALL
, "");
487 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
488 #endif /* defined TEXTDOMAINDIR */
489 (void) textdomain(TZ_DOMAIN
);
490 #endif /* HAVE_GETTEXT */
492 if (TYPE_BIT(zic_t
) < 64) {
493 (void) fprintf(stderr
, "%s: %s\n", progname
,
494 _("wild compilation-time specification of zic_t"));
497 for (i
= 1; i
< argc
; ++i
)
498 if (strcmp(argv
[i
], "--version") == 0) {
499 (void) printf("%s\n", elsieid
);
502 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
507 if (directory
== NULL
)
510 (void) fprintf(stderr
,
511 _("%s: More than one -d option specified\n"),
520 (void) fprintf(stderr
,
521 _("%s: More than one -l option specified\n"),
527 if (psxrules
== NULL
)
530 (void) fprintf(stderr
,
531 _("%s: More than one -p option specified\n"),
537 if (yitcommand
== NULL
)
540 (void) fprintf(stderr
,
541 _("%s: More than one -y option specified\n"),
550 (void) fprintf(stderr
,
551 _("%s: More than one -L option specified\n"),
560 (void) printf("%s: -s ignored\n", progname
);
563 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
564 usage(); /* usage message by request */
565 if (directory
== NULL
)
567 if (yitcommand
== NULL
)
568 yitcommand
= "yearistype";
572 if (optind
< argc
&& leapsec
!= NULL
) {
577 for (i
= optind
; i
< argc
; ++i
)
582 for (i
= 0; i
< nzones
; i
= j
) {
584 ** Find the next non-continuation zone entry.
586 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
588 outzone(&zones
[i
], j
- i
);
593 for (i
= 0; i
< nlinks
; ++i
) {
594 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
595 dolink(links
[i
].l_from
, links
[i
].l_to
);
597 for (j
= 0; j
< nlinks
; ++j
)
598 if (strcmp(links
[i
].l_to
,
599 links
[j
].l_from
) == 0)
600 warning(_("link to link"));
602 if (lcltime
!= NULL
) {
603 eat("command line", 1);
604 dolink(lcltime
, TZDEFAULT
);
606 if (psxrules
!= NULL
) {
607 eat("command line", 1);
608 dolink(psxrules
, TZDEFRULES
);
610 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
614 dolink(fromfield
, tofield
)
615 const char * const fromfield
;
616 const char * const tofield
;
618 register char * fromname
;
619 register char * toname
;
621 if (fromfield
[0] == '/')
622 fromname
= ecpyalloc(fromfield
);
624 fromname
= ecpyalloc(directory
);
625 fromname
= ecatalloc(fromname
, "/");
626 fromname
= ecatalloc(fromname
, fromfield
);
628 if (tofield
[0] == '/')
629 toname
= ecpyalloc(tofield
);
631 toname
= ecpyalloc(directory
);
632 toname
= ecatalloc(toname
, "/");
633 toname
= ecatalloc(toname
, tofield
);
636 ** We get to be careful here since
637 ** there's a fair chance of root running us.
640 (void) remove(toname
);
641 if (link(fromname
, toname
) != 0) {
644 if (mkdirs(toname
) != 0)
647 result
= link(fromname
, toname
);
650 access(fromname
, F_OK
) == 0 &&
652 const char *s
= tofield
;
653 register char * symlinkcontents
= NULL
;
655 while ((s
= strchr(s
+1, '/')) != NULL
)
657 ecatalloc(symlinkcontents
,
660 ecatalloc(symlinkcontents
,
662 result
= symlink(symlinkcontents
,
665 warning(_("hard link failed, symbolic link used"));
666 ifree(symlinkcontents
);
668 #endif /* HAVE_SYMLINK */
670 const char *e
= strerror(errno
);
672 (void) fprintf(stderr
,
673 _("%s: Can't link from %s to %s: %s\n"),
674 progname
, fromname
, toname
, e
);
682 #define TIME_T_BITS_IN_FILE 64
690 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
692 max_time
= -(min_time
+ 1);
697 const char * const name
;
699 register char * myname
;
702 myname
= ecpyalloc(name
);
703 myname
= ecatalloc(myname
, "/.");
704 accres
= access(myname
, F_OK
);
710 ** Associate sets of rules with zones.
714 ** Sort by rule name.
722 return strcmp(((const struct rule
*) cp1
)->r_name
,
723 ((const struct rule
*) cp2
)->r_name
);
729 register struct zone
* zp
;
730 register struct rule
* rp
;
731 register int base
, out
;
735 (void) qsort((void *) rules
, (size_t) nrules
,
736 (size_t) sizeof *rules
, rcomp
);
737 for (i
= 0; i
< nrules
- 1; ++i
) {
738 if (strcmp(rules
[i
].r_name
,
739 rules
[i
+ 1].r_name
) != 0)
741 if (strcmp(rules
[i
].r_filename
,
742 rules
[i
+ 1].r_filename
) == 0)
744 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
745 warning(_("same rule name in multiple files"));
746 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
747 warning(_("same rule name in multiple files"));
748 for (j
= i
+ 2; j
< nrules
; ++j
) {
749 if (strcmp(rules
[i
].r_name
,
750 rules
[j
].r_name
) != 0)
752 if (strcmp(rules
[i
].r_filename
,
753 rules
[j
].r_filename
) == 0)
755 if (strcmp(rules
[i
+ 1].r_filename
,
756 rules
[j
].r_filename
) == 0)
763 for (i
= 0; i
< nzones
; ++i
) {
768 for (base
= 0; base
< nrules
; base
= out
) {
770 for (out
= base
+ 1; out
< nrules
; ++out
)
771 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
773 for (i
= 0; i
< nzones
; ++i
) {
775 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
778 zp
->z_nrules
= out
- base
;
781 for (i
= 0; i
< nzones
; ++i
) {
783 if (zp
->z_nrules
== 0) {
785 ** Maybe we have a local standard time offset.
787 eat(zp
->z_filename
, zp
->z_linenum
);
788 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
791 ** Note, though, that if there's no rule,
792 ** a '%s' in the format is a bad thing.
794 if (strchr(zp
->z_format
, '%') != 0)
795 error(_("%s in ruleless zone"));
807 register char ** fields
;
809 register const struct lookup
* lp
;
810 register int nfields
;
811 register int wantcont
;
815 if (strcmp(name
, "-") == 0) {
816 name
= _("standard input");
818 } else if ((fp
= fopen(name
, "r")) == NULL
) {
819 const char *e
= strerror(errno
);
821 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
826 for (num
= 1; ; ++num
) {
828 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
830 cp
= strchr(buf
, '\n');
832 error(_("line too long"));
836 fields
= getfields(buf
);
838 while (fields
[nfields
] != NULL
) {
841 if (strcmp(fields
[nfields
], "-") == 0)
842 fields
[nfields
] = &nada
;
847 } else if (wantcont
) {
848 wantcont
= inzcont(fields
, nfields
);
850 lp
= byword(fields
[0], line_codes
);
852 error(_("input line of unknown type"));
853 else switch ((int) (lp
->l_value
)) {
855 inrule(fields
, nfields
);
859 wantcont
= inzone(fields
, nfields
);
862 inlink(fields
, nfields
);
867 (void) fprintf(stderr
,
868 _("%s: Leap line in non leap seconds file %s\n"),
870 else inleap(fields
, nfields
);
873 default: /* "cannot happen" */
874 (void) fprintf(stderr
,
875 _("%s: panic: Invalid l_value %d\n"),
876 progname
, lp
->l_value
);
880 ifree((char *) fields
);
883 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
887 if (fp
!= stdin
&& fclose(fp
)) {
888 const char *e
= strerror(errno
);
890 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
891 progname
, filename
, e
);
895 error(_("expected continuation line not found"));
899 ** Convert a string of one of the forms
900 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
901 ** into a number of seconds.
902 ** A null string maps to zero.
903 ** Call error with errstring and return zero on errors.
907 gethms(string
, errstring
, signable
)
909 const char * const errstring
;
915 if (string
== NULL
|| *string
== '\0')
919 else if (*string
== '-') {
923 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
925 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
927 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
928 &hh
, &mm
, &ss
) != 3) {
933 mm
< 0 || mm
>= MINSPERHOUR
||
934 ss
< 0 || ss
> SECSPERMIN
) {
938 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
939 error(_("time overflow"));
942 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
943 warning(_("24:00 not handled by pre-1998 versions of zic"));
944 if (noise
&& (hh
> HOURSPERDAY
||
945 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
946 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
947 return oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
948 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
)));
952 inrule(fields
, nfields
)
953 register char ** const fields
;
956 static struct rule r
;
958 if (nfields
!= RULE_FIELDS
) {
959 error(_("wrong number of fields on Rule line"));
962 if (*fields
[RF_NAME
] == '\0') {
963 error(_("nameless rule"));
966 r
.r_filename
= filename
;
967 r
.r_linenum
= linenum
;
968 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
969 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
970 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
971 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
972 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
973 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
974 max_abbrvar_len
= strlen(r
.r_abbrvar
);
975 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
976 (int) ((nrules
+ 1) * sizeof *rules
));
981 inzone(fields
, nfields
)
982 register char ** const fields
;
988 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
989 error(_("wrong number of fields on Zone line"));
992 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
993 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
995 _("\"Zone %s\" line and -l option are mutually exclusive"),
1000 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1001 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1003 _("\"Zone %s\" line and -p option are mutually exclusive"),
1008 for (i
= 0; i
< nzones
; ++i
)
1009 if (zones
[i
].z_name
!= NULL
&&
1010 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1011 buf
= erealloc(buf
, (int) (132 +
1012 strlen(fields
[ZF_NAME
]) +
1013 strlen(zones
[i
].z_filename
)));
1015 _("duplicate zone name %s (file \"%s\", line %d)"),
1017 zones
[i
].z_filename
,
1018 zones
[i
].z_linenum
);
1022 return inzsub(fields
, nfields
, FALSE
);
1026 inzcont(fields
, nfields
)
1027 register char ** const fields
;
1030 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1031 error(_("wrong number of fields on Zone continuation line"));
1034 return inzsub(fields
, nfields
, TRUE
);
1038 inzsub(fields
, nfields
, iscont
)
1039 register char ** const fields
;
1044 static struct zone z
;
1045 register int i_gmtoff
, i_rule
, i_format
;
1046 register int i_untilyear
, i_untilmonth
;
1047 register int i_untilday
, i_untiltime
;
1048 register int hasuntil
;
1051 i_gmtoff
= ZFC_GMTOFF
;
1053 i_format
= ZFC_FORMAT
;
1054 i_untilyear
= ZFC_TILYEAR
;
1055 i_untilmonth
= ZFC_TILMONTH
;
1056 i_untilday
= ZFC_TILDAY
;
1057 i_untiltime
= ZFC_TILTIME
;
1060 i_gmtoff
= ZF_GMTOFF
;
1062 i_format
= ZF_FORMAT
;
1063 i_untilyear
= ZF_TILYEAR
;
1064 i_untilmonth
= ZF_TILMONTH
;
1065 i_untilday
= ZF_TILDAY
;
1066 i_untiltime
= ZF_TILTIME
;
1067 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1069 z
.z_filename
= filename
;
1070 z
.z_linenum
= linenum
;
1071 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1072 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1073 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1074 error(_("invalid abbreviation format"));
1078 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1079 z
.z_format
= ecpyalloc(fields
[i_format
]);
1080 if (max_format_len
< strlen(z
.z_format
))
1081 max_format_len
= strlen(z
.z_format
);
1082 hasuntil
= nfields
> i_untilyear
;
1084 z
.z_untilrule
.r_filename
= filename
;
1085 z
.z_untilrule
.r_linenum
= linenum
;
1086 rulesub(&z
.z_untilrule
,
1087 fields
[i_untilyear
],
1090 (nfields
> i_untilmonth
) ?
1091 fields
[i_untilmonth
] : "Jan",
1092 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1093 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1094 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1095 z
.z_untilrule
.r_loyear
);
1096 if (iscont
&& nzones
> 0 &&
1097 z
.z_untiltime
> min_time
&&
1098 z
.z_untiltime
< max_time
&&
1099 zones
[nzones
- 1].z_untiltime
> min_time
&&
1100 zones
[nzones
- 1].z_untiltime
< max_time
&&
1101 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1103 "Zone continuation line end time is not after end time of previous line"
1108 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1109 (int) ((nzones
+ 1) * sizeof *zones
));
1110 zones
[nzones
++] = z
;
1112 ** If there was an UNTIL field on this line,
1113 ** there's more information about the zone on the next line.
1119 inleap(fields
, nfields
)
1120 register char ** const fields
;
1123 register const char * cp
;
1124 register const struct lookup
* lp
;
1126 int year
, month
, day
;
1130 if (nfields
!= LEAP_FIELDS
) {
1131 error(_("wrong number of fields on Leap line"));
1135 cp
= fields
[LP_YEAR
];
1136 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1140 error(_("invalid leaping year"));
1143 if (!leapseen
|| leapmaxyear
< year
)
1145 if (!leapseen
|| leapminyear
> year
)
1151 i
= len_years
[isleap(j
)];
1155 i
= -len_years
[isleap(j
)];
1157 dayoff
= oadd(dayoff
, eitol(i
));
1159 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1160 error(_("invalid month name"));
1163 month
= lp
->l_value
;
1165 while (j
!= month
) {
1166 i
= len_months
[isleap(year
)][j
];
1167 dayoff
= oadd(dayoff
, eitol(i
));
1170 cp
= fields
[LP_DAY
];
1171 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1172 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1173 error(_("invalid day of month"));
1176 dayoff
= oadd(dayoff
, eitol(day
- 1));
1177 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1178 error(_("time before zero"));
1181 if (dayoff
< min_time
/ SECSPERDAY
) {
1182 error(_("time too small"));
1185 if (dayoff
> max_time
/ SECSPERDAY
) {
1186 error(_("time too large"));
1189 t
= (zic_t
) dayoff
* SECSPERDAY
;
1190 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1191 cp
= fields
[LP_CORR
];
1193 register int positive
;
1196 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1199 } else if (strcmp(cp
, "--") == 0) {
1202 } else if (strcmp(cp
, "+") == 0) {
1205 } else if (strcmp(cp
, "++") == 0) {
1209 error(_("illegal CORRECTION field on Leap line"));
1212 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1214 "illegal Rolling/Stationary field on Leap line"
1218 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1223 inlink(fields
, nfields
)
1224 register char ** const fields
;
1229 if (nfields
!= LINK_FIELDS
) {
1230 error(_("wrong number of fields on Link line"));
1233 if (*fields
[LF_FROM
] == '\0') {
1234 error(_("blank FROM field on Link line"));
1237 if (*fields
[LF_TO
] == '\0') {
1238 error(_("blank TO field on Link line"));
1241 l
.l_filename
= filename
;
1242 l
.l_linenum
= linenum
;
1243 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1244 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1245 links
= (struct link
*) (void *) erealloc((char *) links
,
1246 (int) ((nlinks
+ 1) * sizeof *links
));
1247 links
[nlinks
++] = l
;
1251 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1252 register struct rule
* const rp
;
1253 const char * const loyearp
;
1254 const char * const hiyearp
;
1255 const char * const typep
;
1256 const char * const monthp
;
1257 const char * const dayp
;
1258 const char * const timep
;
1260 register const struct lookup
* lp
;
1261 register const char * cp
;
1265 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1266 error(_("invalid month name"));
1269 rp
->r_month
= lp
->l_value
;
1270 rp
->r_todisstd
= FALSE
;
1271 rp
->r_todisgmt
= FALSE
;
1272 dp
= ecpyalloc(timep
);
1274 ep
= dp
+ strlen(dp
) - 1;
1275 switch (lowerit(*ep
)) {
1276 case 's': /* Standard */
1277 rp
->r_todisstd
= TRUE
;
1278 rp
->r_todisgmt
= FALSE
;
1281 case 'w': /* Wall */
1282 rp
->r_todisstd
= FALSE
;
1283 rp
->r_todisgmt
= FALSE
;
1286 case 'g': /* Greenwich */
1287 case 'u': /* Universal */
1288 case 'z': /* Zulu */
1289 rp
->r_todisstd
= TRUE
;
1290 rp
->r_todisgmt
= TRUE
;
1295 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1301 lp
= byword(cp
, begin_years
);
1302 rp
->r_lowasnum
= lp
== NULL
;
1303 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1305 rp
->r_loyear
= INT_MIN
;
1308 rp
->r_loyear
= INT_MAX
;
1310 default: /* "cannot happen" */
1311 (void) fprintf(stderr
,
1312 _("%s: panic: Invalid l_value %d\n"),
1313 progname
, lp
->l_value
);
1315 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1316 error(_("invalid starting year"));
1320 lp
= byword(cp
, end_years
);
1321 rp
->r_hiwasnum
= lp
== NULL
;
1322 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1324 rp
->r_hiyear
= INT_MIN
;
1327 rp
->r_hiyear
= INT_MAX
;
1330 rp
->r_hiyear
= rp
->r_loyear
;
1332 default: /* "cannot happen" */
1333 (void) fprintf(stderr
,
1334 _("%s: panic: Invalid l_value %d\n"),
1335 progname
, lp
->l_value
);
1337 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1338 error(_("invalid ending year"));
1341 if (rp
->r_loyear
> rp
->r_hiyear
) {
1342 error(_("starting year greater than ending year"));
1346 rp
->r_yrtype
= NULL
;
1348 if (rp
->r_loyear
== rp
->r_hiyear
) {
1349 error(_("typed single year"));
1352 rp
->r_yrtype
= ecpyalloc(typep
);
1356 ** Accept things such as:
1362 dp
= ecpyalloc(dayp
);
1363 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1364 rp
->r_dycode
= DC_DOWLEQ
;
1365 rp
->r_wday
= lp
->l_value
;
1366 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1368 if ((ep
= strchr(dp
, '<')) != 0)
1369 rp
->r_dycode
= DC_DOWLEQ
;
1370 else if ((ep
= strchr(dp
, '>')) != 0)
1371 rp
->r_dycode
= DC_DOWGEQ
;
1374 rp
->r_dycode
= DC_DOM
;
1376 if (rp
->r_dycode
!= DC_DOM
) {
1379 error(_("invalid day of month"));
1383 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1384 error(_("invalid weekday name"));
1388 rp
->r_wday
= lp
->l_value
;
1390 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1391 rp
->r_dayofmonth
<= 0 ||
1392 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1393 error(_("invalid day of month"));
1409 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1410 buf
[i
] = val
>> shift
;
1421 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1422 buf
[i
] = val
>> shift
;
1433 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1437 puttzcode64(val
, fp
)
1443 convert64(val
, buf
);
1444 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1452 const zic_t a
= ((const struct attype
*) avp
)->at
;
1453 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1455 return (a
< b
) ? -1 : (a
> b
);
1462 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1466 writezone(name
, string
)
1467 const char * const name
;
1468 const char * const string
;
1472 register int leapcnt32
, leapi32
;
1473 register int timecnt32
, timei32
;
1475 static char * fullname
;
1476 static const struct tzhead tzh0
;
1477 static struct tzhead tzh
;
1478 zic_t ats
[TZ_MAX_TIMES
];
1479 unsigned char types
[TZ_MAX_TIMES
];
1485 (void) qsort((void *) attypes
, (size_t) timecnt
,
1486 (size_t) sizeof *attypes
, atcomp
);
1496 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1499 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1500 ++fromi
; /* handled by default rule */
1501 for ( ; fromi
< timecnt
; ++fromi
) {
1502 if (toi
!= 0 && ((attypes
[fromi
].at
+
1503 gmtoffs
[attypes
[toi
- 1].type
]) <=
1504 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1505 : attypes
[toi
- 2].type
]))) {
1506 attypes
[toi
- 1].type
=
1507 attypes
[fromi
].type
;
1511 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1512 attypes
[toi
++] = attypes
[fromi
];
1519 for (i
= 0; i
< timecnt
; ++i
) {
1520 ats
[i
] = attypes
[i
].at
;
1521 types
[i
] = attypes
[i
].type
;
1524 ** Correct for leap seconds.
1526 for (i
= 0; i
< timecnt
; ++i
) {
1529 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1530 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1535 ** Figure out 32-bit-limited starts and counts.
1537 timecnt32
= timecnt
;
1539 leapcnt32
= leapcnt
;
1541 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1543 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1547 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1549 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1553 fullname
= erealloc(fullname
,
1554 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1555 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1557 ** Remove old file, if any, to snap links.
1559 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1560 const char *e
= strerror(errno
);
1562 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1563 progname
, fullname
, e
);
1566 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1567 if (mkdirs(fullname
) != 0)
1569 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1570 const char *e
= strerror(errno
);
1572 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1573 progname
, fullname
, e
);
1577 for (pass
= 1; pass
<= 2; ++pass
) {
1578 register int thistimei
, thistimecnt
;
1579 register int thisleapi
, thisleapcnt
;
1580 register int thistimelim
, thisleaplim
;
1581 int writetype
[TZ_MAX_TIMES
];
1582 int typemap
[TZ_MAX_TYPES
];
1583 register int thistypecnt
;
1584 char thischars
[TZ_MAX_CHARS
];
1586 int indmap
[TZ_MAX_CHARS
];
1589 thistimei
= timei32
;
1590 thistimecnt
= timecnt32
;
1591 thisleapi
= leapi32
;
1592 thisleapcnt
= leapcnt32
;
1595 thistimecnt
= timecnt
;
1597 thisleapcnt
= leapcnt
;
1599 thistimelim
= thistimei
+ thistimecnt
;
1600 thisleaplim
= thisleapi
+ thisleapcnt
;
1601 for (i
= 0; i
< typecnt
; ++i
)
1602 writetype
[i
] = thistimecnt
== timecnt
;
1603 if (thistimecnt
== 0) {
1605 ** No transition times fall in the current
1606 ** (32- or 64-bit) window.
1609 writetype
[typecnt
- 1] = TRUE
;
1611 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1613 writetype
[types
[i
]] = TRUE
;
1615 ** For America/Godthab and Antarctica/Palmer
1618 writetype
[0] = TRUE
;
1621 for (i
= 0; i
< typecnt
; ++i
)
1622 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : -1;
1623 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1626 for (i
= 0; i
< typecnt
; ++i
) {
1627 register char * thisabbr
;
1631 if (indmap
[abbrinds
[i
]] >= 0)
1633 thisabbr
= &chars
[abbrinds
[i
]];
1634 for (j
= 0; j
< thischarcnt
; ++j
)
1635 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1637 if (j
== thischarcnt
) {
1638 (void) strcpy(&thischars
[(int) thischarcnt
],
1640 thischarcnt
+= strlen(thisabbr
) + 1;
1642 indmap
[abbrinds
[i
]] = j
;
1644 #define DO(field) (void) fwrite((void *) tzh.field, \
1645 (size_t) sizeof tzh.field, (size_t) 1, fp)
1647 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1648 tzh
.tzh_version
[0] = ZIC_VERSION
;
1649 convert(eitol(thistypecnt
), tzh
.tzh_ttisgmtcnt
);
1650 convert(eitol(thistypecnt
), tzh
.tzh_ttisstdcnt
);
1651 convert(eitol(thisleapcnt
), tzh
.tzh_leapcnt
);
1652 convert(eitol(thistimecnt
), tzh
.tzh_timecnt
);
1653 convert(eitol(thistypecnt
), tzh
.tzh_typecnt
);
1654 convert(eitol(thischarcnt
), tzh
.tzh_charcnt
);
1665 for (i
= thistimei
; i
< thistimelim
; ++i
)
1667 puttzcode((long) ats
[i
], fp
);
1668 else puttzcode64(ats
[i
], fp
);
1669 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1672 uc
= typemap
[types
[i
]];
1673 (void) fwrite((void *) &uc
,
1678 for (i
= 0; i
< typecnt
; ++i
)
1680 puttzcode(gmtoffs
[i
], fp
);
1681 (void) putc(isdsts
[i
], fp
);
1682 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1684 if (thischarcnt
!= 0)
1685 (void) fwrite((void *) thischars
,
1686 (size_t) sizeof thischars
[0],
1687 (size_t) thischarcnt
, fp
);
1688 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1689 register zic_t todo
;
1692 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1695 if (++j
>= typecnt
) {
1701 while (j
< timecnt
&&
1706 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1707 } else todo
= trans
[i
];
1709 puttzcode((long) todo
, fp
);
1710 else puttzcode64(todo
, fp
);
1711 puttzcode(corr
[i
], fp
);
1713 for (i
= 0; i
< typecnt
; ++i
)
1715 (void) putc(ttisstds
[i
], fp
);
1716 for (i
= 0; i
< typecnt
; ++i
)
1718 (void) putc(ttisgmts
[i
], fp
);
1720 (void) fprintf(fp
, "\n%s\n", string
);
1721 if (ferror(fp
) || fclose(fp
)) {
1722 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1723 progname
, fullname
);
1729 doabbr(abbr
, format
, letters
, isdst
, doquotes
)
1731 const char * const format
;
1732 const char * const letters
;
1737 register char * slashp
;
1740 slashp
= strchr(format
, '/');
1741 if (slashp
== NULL
) {
1742 if (letters
== NULL
)
1743 (void) strcpy(abbr
, format
);
1744 else (void) sprintf(abbr
, format
, letters
);
1746 (void) strcpy(abbr
, slashp
+ 1);
1748 if (slashp
> format
)
1749 (void) strncpy(abbr
, format
,
1750 (unsigned) (slashp
- format
));
1751 abbr
[slashp
- format
] = '\0';
1755 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1756 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1757 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1760 if (len
> 0 && *cp
== '\0')
1762 abbr
[len
+ 2] = '\0';
1763 abbr
[len
+ 1] = '>';
1764 for ( ; len
> 0; --len
)
1765 abbr
[len
] = abbr
[len
- 1];
1780 stringoffset(result
, offset
)
1785 register int minutes
;
1786 register int seconds
;
1790 (void) strcpy(result
, "-");
1793 seconds
= offset
% SECSPERMIN
;
1794 offset
/= SECSPERMIN
;
1795 minutes
= offset
% MINSPERHOUR
;
1796 offset
/= MINSPERHOUR
;
1798 if (hours
>= HOURSPERDAY
) {
1802 (void) sprintf(end(result
), "%d", hours
);
1803 if (minutes
!= 0 || seconds
!= 0) {
1804 (void) sprintf(end(result
), ":%02d", minutes
);
1806 (void) sprintf(end(result
), ":%02d", seconds
);
1812 stringrule(result
, rp
, dstoff
, gmtoff
)
1814 const struct rule
* const rp
;
1820 result
= end(result
);
1821 if (rp
->r_dycode
== DC_DOM
) {
1822 register int month
, total
;
1824 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1827 for (month
= 0; month
< rp
->r_month
; ++month
)
1828 total
+= len_months
[0][month
];
1829 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1833 if (rp
->r_dycode
== DC_DOWGEQ
) {
1834 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1835 if ((week
- 1) * DAYSPERWEEK
+ 1 != rp
->r_dayofmonth
)
1837 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1838 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1841 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1842 if (week
* DAYSPERWEEK
- 1 != rp
->r_dayofmonth
)
1845 } else return -1; /* "cannot happen" */
1846 (void) sprintf(result
, "M%d.%d.%d",
1847 rp
->r_month
+ 1, week
, rp
->r_wday
);
1852 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1858 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1859 (void) strcat(result
, "/");
1860 if (stringoffset(end(result
), tod
) != 0)
1867 stringzone(result
, zpfirst
, zonecount
)
1869 const struct zone
* const zpfirst
;
1870 const int zonecount
;
1872 register const struct zone
* zp
;
1873 register struct rule
* rp
;
1874 register struct rule
* stdrp
;
1875 register struct rule
* dstrp
;
1877 register const char * abbrvar
;
1880 zp
= zpfirst
+ zonecount
- 1;
1881 stdrp
= dstrp
= NULL
;
1882 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1883 rp
= &zp
->z_rules
[i
];
1884 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= INT_MAX
)
1886 if (rp
->r_yrtype
!= NULL
)
1888 if (rp
->r_stdoff
== 0) {
1898 if (stdrp
== NULL
&& dstrp
== NULL
) {
1900 ** There are no rules running through "max".
1901 ** Let's find the latest rule.
1903 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1904 rp
= &zp
->z_rules
[i
];
1905 if (stdrp
== NULL
|| rp
->r_hiyear
> stdrp
->r_hiyear
||
1906 (rp
->r_hiyear
== stdrp
->r_hiyear
&&
1907 rp
->r_month
> stdrp
->r_month
))
1910 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0)
1911 return; /* We end up in DST (a POSIX no-no). */
1913 ** Horrid special case: if year is 2037,
1914 ** presume this is a zone handled on a year-by-year basis;
1915 ** do not try to apply a rule to the zone.
1917 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1920 if (stdrp
== NULL
&& zp
->z_nrules
!= 0)
1922 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1923 doabbr(result
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1924 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
1930 doabbr(end(result
), zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
1931 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
1932 if (stringoffset(end(result
),
1933 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
1937 (void) strcat(result
, ",");
1938 if (stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1942 (void) strcat(result
, ",");
1943 if (stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1950 outzone(zpfirst
, zonecount
)
1951 const struct zone
* const zpfirst
;
1952 const int zonecount
;
1954 register const struct zone
* zp
;
1955 register struct rule
* rp
;
1957 register int usestart
, useuntil
;
1958 register zic_t starttime
, untiltime
;
1959 register long gmtoff
;
1960 register long stdoff
;
1962 register long startoff
;
1963 register int startttisstd
;
1964 register int startttisgmt
;
1966 register char * startbuf
;
1968 register char * envvar
;
1969 register int max_abbr_len
;
1970 register int max_envvar_len
;
1972 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
1973 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
1974 startbuf
= emalloc(max_abbr_len
+ 1);
1975 ab
= emalloc(max_abbr_len
+ 1);
1976 envvar
= emalloc(max_envvar_len
+ 1);
1977 INITIALIZE(untiltime
);
1978 INITIALIZE(starttime
);
1980 ** Now. . .finally. . .generate some useful data!
1986 ** Thanks to Earl Chew
1987 ** for noting the need to unconditionally initialize startttisstd.
1989 startttisstd
= FALSE
;
1990 startttisgmt
= FALSE
;
1991 min_year
= max_year
= EPOCH_YEAR
;
1993 updateminmax(leapminyear
);
1994 updateminmax(leapmaxyear
);
1996 for (i
= 0; i
< zonecount
; ++i
) {
1998 if (i
< zonecount
- 1)
1999 updateminmax(zp
->z_untilrule
.r_loyear
);
2000 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2001 rp
= &zp
->z_rules
[j
];
2003 updateminmax(rp
->r_loyear
);
2005 updateminmax(rp
->r_hiyear
);
2009 ** Generate lots of data if a rule can't cover all future times.
2011 stringzone(envvar
, zpfirst
, zonecount
);
2012 if (noise
&& envvar
[0] == '\0') {
2015 wp
= ecpyalloc(_("no POSIX environment variable for zone"));
2016 wp
= ecatalloc(wp
, " ");
2017 wp
= ecatalloc(wp
, zpfirst
->z_name
);
2021 if (envvar
[0] == '\0') {
2022 if (min_year
>= INT_MIN
+ YEARSPERREPEAT
)
2023 min_year
-= YEARSPERREPEAT
;
2024 else min_year
= INT_MIN
;
2025 if (max_year
<= INT_MAX
- YEARSPERREPEAT
)
2026 max_year
+= YEARSPERREPEAT
;
2027 else max_year
= INT_MAX
;
2030 ** For the benefit of older systems,
2031 ** generate data from 1900 through 2037.
2033 if (min_year
> 1900)
2035 if (max_year
< 2037)
2037 for (i
= 0; i
< zonecount
; ++i
) {
2039 ** A guess that may well be corrected later.
2043 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2044 useuntil
= i
< (zonecount
- 1);
2045 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2047 gmtoff
= zp
->z_gmtoff
;
2048 eat(zp
->z_filename
, zp
->z_linenum
);
2050 startoff
= zp
->z_gmtoff
;
2051 if (zp
->z_nrules
== 0) {
2052 stdoff
= zp
->z_stdoff
;
2053 doabbr(startbuf
, zp
->z_format
,
2054 (char *) NULL
, stdoff
!= 0, FALSE
);
2055 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2056 startbuf
, stdoff
!= 0, startttisstd
,
2059 addtt(starttime
, type
);
2061 } else if (stdoff
!= 0)
2062 addtt(min_time
, type
);
2063 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2064 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2067 ** Mark which rules to do in the current year.
2068 ** For those to do, calculate rpytime(rp, year);
2070 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2071 rp
= &zp
->z_rules
[j
];
2072 eats(zp
->z_filename
, zp
->z_linenum
,
2073 rp
->r_filename
, rp
->r_linenum
);
2074 rp
->r_todo
= year
>= rp
->r_loyear
&&
2075 year
<= rp
->r_hiyear
&&
2076 yearistype(year
, rp
->r_yrtype
);
2078 rp
->r_temp
= rpytime(rp
, year
);
2082 register zic_t jtime
, ktime
;
2083 register long offset
;
2088 ** Turn untiltime into UTC
2089 ** assuming the current gmtoff and
2092 untiltime
= zp
->z_untiltime
;
2093 if (!zp
->z_untilrule
.r_todisgmt
)
2094 untiltime
= tadd(untiltime
,
2096 if (!zp
->z_untilrule
.r_todisstd
)
2097 untiltime
= tadd(untiltime
,
2101 ** Find the rule (of those to do, if any)
2102 ** that takes effect earliest in the year.
2105 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2106 rp
= &zp
->z_rules
[j
];
2109 eats(zp
->z_filename
, zp
->z_linenum
,
2110 rp
->r_filename
, rp
->r_linenum
);
2111 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2112 if (!rp
->r_todisstd
)
2113 offset
= oadd(offset
, stdoff
);
2115 if (jtime
== min_time
||
2118 jtime
= tadd(jtime
, -offset
);
2119 if (k
< 0 || jtime
< ktime
) {
2125 break; /* go on to next year */
2126 rp
= &zp
->z_rules
[k
];
2128 if (useuntil
&& ktime
>= untiltime
)
2130 stdoff
= rp
->r_stdoff
;
2131 if (usestart
&& ktime
== starttime
)
2134 if (ktime
< starttime
) {
2135 startoff
= oadd(zp
->z_gmtoff
,
2137 doabbr(startbuf
, zp
->z_format
,
2143 if (*startbuf
== '\0' &&
2144 startoff
== oadd(zp
->z_gmtoff
,
2154 eats(zp
->z_filename
, zp
->z_linenum
,
2155 rp
->r_filename
, rp
->r_linenum
);
2156 doabbr(ab
, zp
->z_format
, rp
->r_abbrvar
,
2157 rp
->r_stdoff
!= 0, FALSE
);
2158 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2159 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2160 rp
->r_todisstd
, rp
->r_todisgmt
);
2165 if (*startbuf
== '\0' &&
2166 zp
->z_format
!= NULL
&&
2167 strchr(zp
->z_format
, '%') == NULL
&&
2168 strchr(zp
->z_format
, '/') == NULL
)
2169 (void) strcpy(startbuf
, zp
->z_format
);
2170 eat(zp
->z_filename
, zp
->z_linenum
);
2171 if (*startbuf
== '\0')
2172 error(_("can't determine time zone abbreviation to use just after until time"));
2173 else addtt(starttime
,
2174 addtype(startoff
, startbuf
,
2175 startoff
!= zp
->z_gmtoff
,
2180 ** Now we may get to set starttime for the next zone line.
2183 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2184 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2185 starttime
= zp
->z_untiltime
;
2187 starttime
= tadd(starttime
, -stdoff
);
2189 starttime
= tadd(starttime
, -gmtoff
);
2192 writezone(zpfirst
->z_name
, envvar
);
2199 addtt(starttime
, type
)
2200 const zic_t starttime
;
2203 if (starttime
<= min_time
||
2204 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2205 gmtoffs
[0] = gmtoffs
[type
];
2206 isdsts
[0] = isdsts
[type
];
2207 ttisstds
[0] = ttisstds
[type
];
2208 ttisgmts
[0] = ttisgmts
[type
];
2209 if (abbrinds
[type
] != 0)
2210 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2212 charcnt
= strlen(chars
) + 1;
2217 if (timecnt
>= TZ_MAX_TIMES
) {
2218 error(_("too many transitions?!"));
2221 attypes
[timecnt
].at
= starttime
;
2222 attypes
[timecnt
].type
= type
;
2227 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
2229 const char * const abbr
;
2236 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2237 error(_("internal error - addtype called with bad isdst"));
2240 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2241 error(_("internal error - addtype called with bad ttisstd"));
2244 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2245 error(_("internal error - addtype called with bad ttisgmt"));
2249 ** See if there's already an entry for this zone type.
2250 ** If so, just return its index.
2252 for (i
= 0; i
< typecnt
; ++i
) {
2253 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2254 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2255 ttisstd
== ttisstds
[i
] &&
2256 ttisgmt
== ttisgmts
[i
])
2260 ** There isn't one; add a new one, unless there are already too
2263 if (typecnt
>= TZ_MAX_TYPES
) {
2264 error(_("too many local time types"));
2267 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2268 error(_("UTC offset out of range"));
2271 gmtoffs
[i
] = gmtoff
;
2273 ttisstds
[i
] = ttisstd
;
2274 ttisgmts
[i
] = ttisgmt
;
2276 for (j
= 0; j
< charcnt
; ++j
)
2277 if (strcmp(&chars
[j
], abbr
) == 0)
2287 leapadd(t
, positive
, rolling
, count
)
2295 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2296 error(_("too many leap seconds"));
2299 for (i
= 0; i
< leapcnt
; ++i
)
2300 if (t
<= trans
[i
]) {
2301 if (t
== trans
[i
]) {
2302 error(_("repeated leap second moment"));
2308 for (j
= leapcnt
; j
> i
; --j
) {
2309 trans
[j
] = trans
[j
- 1];
2310 corr
[j
] = corr
[j
- 1];
2311 roll
[j
] = roll
[j
- 1];
2314 corr
[i
] = positive
? 1L : eitol(-count
);
2317 } while (positive
&& --count
!= 0);
2324 register long last
= 0;
2327 ** propagate leap seconds forward
2329 for (i
= 0; i
< leapcnt
; ++i
) {
2330 trans
[i
] = tadd(trans
[i
], last
);
2331 last
= corr
[i
] += last
;
2336 yearistype(year
, type
)
2338 const char * const type
;
2343 if (type
== NULL
|| *type
== '\0')
2345 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
2346 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
2347 result
= system(buf
);
2348 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2354 error(_("Wild result from command execution"));
2355 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2356 progname
, buf
, result
);
2365 a
= (unsigned char) a
;
2366 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2370 ciequal(ap
, bp
) /* case-insensitive equality */
2371 register const char * ap
;
2372 register const char * bp
;
2374 while (lowerit(*ap
) == lowerit(*bp
++))
2382 register const char * abbr
;
2383 register const char * word
;
2385 if (lowerit(*abbr
) != lowerit(*word
))
2388 while (*++abbr
!= '\0')
2392 } while (lowerit(*word
++) != lowerit(*abbr
));
2396 static const struct lookup
*
2398 register const char * const word
;
2399 register const struct lookup
* const table
;
2401 register const struct lookup
* foundlp
;
2402 register const struct lookup
* lp
;
2404 if (word
== NULL
|| table
== NULL
)
2407 ** Look for exact match.
2409 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2410 if (ciequal(word
, lp
->l_word
))
2413 ** Look for inexact match.
2416 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2417 if (itsabbr(word
, lp
->l_word
)) {
2418 if (foundlp
== NULL
)
2420 else return NULL
; /* multiple inexact matches */
2430 register char ** array
;
2435 array
= (char **) (void *)
2436 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2439 while (isascii((unsigned char) *cp
) &&
2440 isspace((unsigned char) *cp
))
2442 if (*cp
== '\0' || *cp
== '#')
2444 array
[nsubs
++] = dp
= cp
;
2446 if ((*dp
= *cp
++) != '"')
2448 else while ((*dp
= *cp
++) != '"')
2453 "Odd number of quotation marks"
2457 } while (*cp
!= '\0' && *cp
!= '#' &&
2458 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2459 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2463 array
[nsubs
] = NULL
;
2475 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2476 error(_("time overflow"));
2489 if (t1
== max_time
&& t2
> 0)
2491 if (t1
== min_time
&& t2
< 0)
2494 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2495 error(_("time overflow"));
2502 ** Given a rule, and a year, compute the date - in seconds since January 1,
2503 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2507 rpytime(rp
, wantedy
)
2508 register const struct rule
* const rp
;
2509 register const int wantedy
;
2511 register int y
, m
, i
;
2512 register long dayoff
; /* with a nod to Margaret O. */
2515 if (wantedy
== INT_MIN
)
2517 if (wantedy
== INT_MAX
)
2522 while (wantedy
!= y
) {
2524 i
= len_years
[isleap(y
)];
2528 i
= -len_years
[isleap(y
)];
2530 dayoff
= oadd(dayoff
, eitol(i
));
2532 while (m
!= rp
->r_month
) {
2533 i
= len_months
[isleap(y
)][m
];
2534 dayoff
= oadd(dayoff
, eitol(i
));
2537 i
= rp
->r_dayofmonth
;
2538 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2539 if (rp
->r_dycode
== DC_DOWLEQ
)
2542 error(_("use of 2/29 in non leap-year"));
2547 dayoff
= oadd(dayoff
, eitol(i
));
2548 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2551 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2552 wday
= eitol(EPOCH_WDAY
);
2554 ** Don't trust mod of negative numbers.
2557 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2559 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2561 wday
+= LDAYSPERWEEK
;
2563 while (wday
!= eitol(rp
->r_wday
))
2564 if (rp
->r_dycode
== DC_DOWGEQ
) {
2565 dayoff
= oadd(dayoff
, (long) 1);
2566 if (++wday
>= LDAYSPERWEEK
)
2570 dayoff
= oadd(dayoff
, (long) -1);
2572 wday
= LDAYSPERWEEK
- 1;
2575 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2577 warning(_("rule goes past start/end of month--\
2578 will not work with pre-2004 versions of zic"));
2581 if (dayoff
< min_time
/ SECSPERDAY
)
2583 if (dayoff
> max_time
/ SECSPERDAY
)
2585 t
= (zic_t
) dayoff
* SECSPERDAY
;
2586 return tadd(t
, rp
->r_tod
);
2591 const char * const string
;
2595 if (strcmp(string
, GRANDPARENTED
) != 0) {
2596 register const char * cp
;
2600 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2601 ** optionally followed by a + or - and a number from 1 to 14.
2605 while (isascii((unsigned char) *cp
) &&
2606 isalpha((unsigned char) *cp
))
2608 if (cp
- string
== 0)
2609 wp
= _("time zone abbreviation lacks alphabetic at start");
2610 if (noise
&& cp
- string
> 3)
2611 wp
= _("time zone abbreviation has more than 3 alphabetics");
2612 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2613 wp
= _("time zone abbreviation has too many alphabetics");
2614 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2616 if (isascii((unsigned char) *cp
) &&
2617 isdigit((unsigned char) *cp
))
2619 *cp
>= '0' && *cp
<= '4')
2623 wp
= _("time zone abbreviation differs from POSIX standard");
2626 wp
= ecatalloc(wp
, " (");
2627 wp
= ecatalloc(wp
, string
);
2628 wp
= ecatalloc(wp
, ")");
2633 i
= strlen(string
) + 1;
2634 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2635 error(_("too many, or too long, time zone abbreviations"));
2638 (void) strcpy(&chars
[charcnt
], string
);
2639 charcnt
+= eitol(i
);
2646 register char * name
;
2649 if (argname
== NULL
|| *argname
== '\0')
2651 cp
= name
= ecpyalloc(argname
);
2652 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2656 ** DOS drive specifier?
2658 if (isalpha((unsigned char) name
[0]) &&
2659 name
[1] == ':' && name
[2] == '\0') {
2663 #endif /* !defined unix */
2664 if (!itsdir(name
)) {
2666 ** It doesn't seem to exist, so we try to create it.
2667 ** Creation may fail because of the directory being
2668 ** created by some other multiprocessor, so we get
2669 ** to do extra checking.
2671 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2672 const char *e
= strerror(errno
);
2674 if (errno
!= EEXIST
|| !itsdir(name
)) {
2675 (void) fprintf(stderr
,
2676 _("%s: Can't create directory %s: %s\n"),
2696 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2697 (void) fprintf(stderr
,
2698 _("%s: %d did not sign extend correctly\n"),
2706 ** UNIX was a registered trademark of The Open Group in 2003.