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.19";
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(FILE *stream
, int status
);
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
);
457 usage(FILE *stream
, int status
)
459 (void) fprintf(stream
, _("%s: usage is %s \
460 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
461 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
463 Report bugs to tz@elsie.nci.nih.gov.\n"),
468 static const char * psxrules
;
469 static const char * lcltime
;
470 static const char * directory
;
471 static const char * leapsec
;
472 static const char * yitcommand
;
484 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
485 #endif /* defined unix */
487 (void) setlocale(LC_ALL
, "");
489 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
490 #endif /* defined TEXTDOMAINDIR */
491 (void) textdomain(TZ_DOMAIN
);
492 #endif /* HAVE_GETTEXT */
494 if (TYPE_BIT(zic_t
) < 64) {
495 (void) fprintf(stderr
, "%s: %s\n", progname
,
496 _("wild compilation-time specification of zic_t"));
499 for (i
= 1; i
< argc
; ++i
)
500 if (strcmp(argv
[i
], "--version") == 0) {
501 (void) printf("%s\n", elsieid
);
503 } else if (strcmp(argv
[i
], "--help") == 0) {
504 usage(stdout
, EXIT_SUCCESS
);
506 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
509 usage(stderr
, EXIT_FAILURE
);
511 if (directory
== NULL
)
514 (void) fprintf(stderr
,
515 _("%s: More than one -d option specified\n"),
524 (void) fprintf(stderr
,
525 _("%s: More than one -l option specified\n"),
531 if (psxrules
== NULL
)
534 (void) fprintf(stderr
,
535 _("%s: More than one -p option specified\n"),
541 if (yitcommand
== NULL
)
544 (void) fprintf(stderr
,
545 _("%s: More than one -y option specified\n"),
554 (void) fprintf(stderr
,
555 _("%s: More than one -L option specified\n"),
564 (void) printf("%s: -s ignored\n", progname
);
567 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
568 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
569 if (directory
== NULL
)
571 if (yitcommand
== NULL
)
572 yitcommand
= "yearistype";
576 if (optind
< argc
&& leapsec
!= NULL
) {
581 for (i
= optind
; i
< argc
; ++i
)
586 for (i
= 0; i
< nzones
; i
= j
) {
588 ** Find the next non-continuation zone entry.
590 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
592 outzone(&zones
[i
], j
- i
);
597 for (i
= 0; i
< nlinks
; ++i
) {
598 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
599 dolink(links
[i
].l_from
, links
[i
].l_to
);
601 for (j
= 0; j
< nlinks
; ++j
)
602 if (strcmp(links
[i
].l_to
,
603 links
[j
].l_from
) == 0)
604 warning(_("link to link"));
606 if (lcltime
!= NULL
) {
607 eat("command line", 1);
608 dolink(lcltime
, TZDEFAULT
);
610 if (psxrules
!= NULL
) {
611 eat("command line", 1);
612 dolink(psxrules
, TZDEFRULES
);
614 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
618 dolink(fromfield
, tofield
)
619 const char * const fromfield
;
620 const char * const tofield
;
622 register char * fromname
;
623 register char * toname
;
625 if (fromfield
[0] == '/')
626 fromname
= ecpyalloc(fromfield
);
628 fromname
= ecpyalloc(directory
);
629 fromname
= ecatalloc(fromname
, "/");
630 fromname
= ecatalloc(fromname
, fromfield
);
632 if (tofield
[0] == '/')
633 toname
= ecpyalloc(tofield
);
635 toname
= ecpyalloc(directory
);
636 toname
= ecatalloc(toname
, "/");
637 toname
= ecatalloc(toname
, tofield
);
640 ** We get to be careful here since
641 ** there's a fair chance of root running us.
644 (void) remove(toname
);
645 if (link(fromname
, toname
) != 0) {
648 if (mkdirs(toname
) != 0)
651 result
= link(fromname
, toname
);
654 access(fromname
, F_OK
) == 0 &&
656 const char *s
= tofield
;
657 register char * symlinkcontents
= NULL
;
659 while ((s
= strchr(s
+1, '/')) != NULL
)
661 ecatalloc(symlinkcontents
,
664 ecatalloc(symlinkcontents
,
666 result
= symlink(symlinkcontents
,
669 warning(_("hard link failed, symbolic link used"));
670 ifree(symlinkcontents
);
672 #endif /* HAVE_SYMLINK */
674 const char *e
= strerror(errno
);
676 (void) fprintf(stderr
,
677 _("%s: Can't link from %s to %s: %s\n"),
678 progname
, fromname
, toname
, e
);
686 #define TIME_T_BITS_IN_FILE 64
694 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
696 max_time
= -(min_time
+ 1);
701 const char * const name
;
703 register char * myname
;
706 myname
= ecpyalloc(name
);
707 myname
= ecatalloc(myname
, "/.");
708 accres
= access(myname
, F_OK
);
714 ** Associate sets of rules with zones.
718 ** Sort by rule name.
726 return strcmp(((const struct rule
*) cp1
)->r_name
,
727 ((const struct rule
*) cp2
)->r_name
);
733 register struct zone
* zp
;
734 register struct rule
* rp
;
735 register int base
, out
;
739 (void) qsort((void *) rules
, (size_t) nrules
,
740 (size_t) sizeof *rules
, rcomp
);
741 for (i
= 0; i
< nrules
- 1; ++i
) {
742 if (strcmp(rules
[i
].r_name
,
743 rules
[i
+ 1].r_name
) != 0)
745 if (strcmp(rules
[i
].r_filename
,
746 rules
[i
+ 1].r_filename
) == 0)
748 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
749 warning(_("same rule name in multiple files"));
750 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
751 warning(_("same rule name in multiple files"));
752 for (j
= i
+ 2; j
< nrules
; ++j
) {
753 if (strcmp(rules
[i
].r_name
,
754 rules
[j
].r_name
) != 0)
756 if (strcmp(rules
[i
].r_filename
,
757 rules
[j
].r_filename
) == 0)
759 if (strcmp(rules
[i
+ 1].r_filename
,
760 rules
[j
].r_filename
) == 0)
767 for (i
= 0; i
< nzones
; ++i
) {
772 for (base
= 0; base
< nrules
; base
= out
) {
774 for (out
= base
+ 1; out
< nrules
; ++out
)
775 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
777 for (i
= 0; i
< nzones
; ++i
) {
779 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
782 zp
->z_nrules
= out
- base
;
785 for (i
= 0; i
< nzones
; ++i
) {
787 if (zp
->z_nrules
== 0) {
789 ** Maybe we have a local standard time offset.
791 eat(zp
->z_filename
, zp
->z_linenum
);
792 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
795 ** Note, though, that if there's no rule,
796 ** a '%s' in the format is a bad thing.
798 if (strchr(zp
->z_format
, '%') != 0)
799 error(_("%s in ruleless zone"));
811 register char ** fields
;
813 register const struct lookup
* lp
;
814 register int nfields
;
815 register int wantcont
;
819 if (strcmp(name
, "-") == 0) {
820 name
= _("standard input");
822 } else if ((fp
= fopen(name
, "r")) == NULL
) {
823 const char *e
= strerror(errno
);
825 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
830 for (num
= 1; ; ++num
) {
832 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
834 cp
= strchr(buf
, '\n');
836 error(_("line too long"));
840 fields
= getfields(buf
);
842 while (fields
[nfields
] != NULL
) {
845 if (strcmp(fields
[nfields
], "-") == 0)
846 fields
[nfields
] = &nada
;
851 } else if (wantcont
) {
852 wantcont
= inzcont(fields
, nfields
);
854 lp
= byword(fields
[0], line_codes
);
856 error(_("input line of unknown type"));
857 else switch ((int) (lp
->l_value
)) {
859 inrule(fields
, nfields
);
863 wantcont
= inzone(fields
, nfields
);
866 inlink(fields
, nfields
);
871 (void) fprintf(stderr
,
872 _("%s: Leap line in non leap seconds file %s\n"),
874 else inleap(fields
, nfields
);
877 default: /* "cannot happen" */
878 (void) fprintf(stderr
,
879 _("%s: panic: Invalid l_value %d\n"),
880 progname
, lp
->l_value
);
884 ifree((char *) fields
);
887 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
891 if (fp
!= stdin
&& fclose(fp
)) {
892 const char *e
= strerror(errno
);
894 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
895 progname
, filename
, e
);
899 error(_("expected continuation line not found"));
903 ** Convert a string of one of the forms
904 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
905 ** into a number of seconds.
906 ** A null string maps to zero.
907 ** Call error with errstring and return zero on errors.
911 gethms(string
, errstring
, signable
)
913 const char * const errstring
;
919 if (string
== NULL
|| *string
== '\0')
923 else if (*string
== '-') {
927 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
929 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
931 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
932 &hh
, &mm
, &ss
) != 3) {
937 mm
< 0 || mm
>= MINSPERHOUR
||
938 ss
< 0 || ss
> SECSPERMIN
) {
942 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
943 error(_("time overflow"));
946 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
947 warning(_("24:00 not handled by pre-1998 versions of zic"));
948 if (noise
&& (hh
> HOURSPERDAY
||
949 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
950 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
951 return oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
952 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
)));
956 inrule(fields
, nfields
)
957 register char ** const fields
;
960 static struct rule r
;
962 if (nfields
!= RULE_FIELDS
) {
963 error(_("wrong number of fields on Rule line"));
966 if (*fields
[RF_NAME
] == '\0') {
967 error(_("nameless rule"));
970 r
.r_filename
= filename
;
971 r
.r_linenum
= linenum
;
972 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
973 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
974 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
975 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
976 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
977 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
978 max_abbrvar_len
= strlen(r
.r_abbrvar
);
979 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
980 (int) ((nrules
+ 1) * sizeof *rules
));
985 inzone(fields
, nfields
)
986 register char ** const fields
;
992 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
993 error(_("wrong number of fields on Zone line"));
996 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
997 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
999 _("\"Zone %s\" line and -l option are mutually exclusive"),
1004 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1005 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1007 _("\"Zone %s\" line and -p option are mutually exclusive"),
1012 for (i
= 0; i
< nzones
; ++i
)
1013 if (zones
[i
].z_name
!= NULL
&&
1014 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1015 buf
= erealloc(buf
, (int) (132 +
1016 strlen(fields
[ZF_NAME
]) +
1017 strlen(zones
[i
].z_filename
)));
1019 _("duplicate zone name %s (file \"%s\", line %d)"),
1021 zones
[i
].z_filename
,
1022 zones
[i
].z_linenum
);
1026 return inzsub(fields
, nfields
, FALSE
);
1030 inzcont(fields
, nfields
)
1031 register char ** const fields
;
1034 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1035 error(_("wrong number of fields on Zone continuation line"));
1038 return inzsub(fields
, nfields
, TRUE
);
1042 inzsub(fields
, nfields
, iscont
)
1043 register char ** const fields
;
1048 static struct zone z
;
1049 register int i_gmtoff
, i_rule
, i_format
;
1050 register int i_untilyear
, i_untilmonth
;
1051 register int i_untilday
, i_untiltime
;
1052 register int hasuntil
;
1055 i_gmtoff
= ZFC_GMTOFF
;
1057 i_format
= ZFC_FORMAT
;
1058 i_untilyear
= ZFC_TILYEAR
;
1059 i_untilmonth
= ZFC_TILMONTH
;
1060 i_untilday
= ZFC_TILDAY
;
1061 i_untiltime
= ZFC_TILTIME
;
1064 i_gmtoff
= ZF_GMTOFF
;
1066 i_format
= ZF_FORMAT
;
1067 i_untilyear
= ZF_TILYEAR
;
1068 i_untilmonth
= ZF_TILMONTH
;
1069 i_untilday
= ZF_TILDAY
;
1070 i_untiltime
= ZF_TILTIME
;
1071 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1073 z
.z_filename
= filename
;
1074 z
.z_linenum
= linenum
;
1075 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1076 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1077 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1078 error(_("invalid abbreviation format"));
1082 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1083 z
.z_format
= ecpyalloc(fields
[i_format
]);
1084 if (max_format_len
< strlen(z
.z_format
))
1085 max_format_len
= strlen(z
.z_format
);
1086 hasuntil
= nfields
> i_untilyear
;
1088 z
.z_untilrule
.r_filename
= filename
;
1089 z
.z_untilrule
.r_linenum
= linenum
;
1090 rulesub(&z
.z_untilrule
,
1091 fields
[i_untilyear
],
1094 (nfields
> i_untilmonth
) ?
1095 fields
[i_untilmonth
] : "Jan",
1096 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1097 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1098 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1099 z
.z_untilrule
.r_loyear
);
1100 if (iscont
&& nzones
> 0 &&
1101 z
.z_untiltime
> min_time
&&
1102 z
.z_untiltime
< max_time
&&
1103 zones
[nzones
- 1].z_untiltime
> min_time
&&
1104 zones
[nzones
- 1].z_untiltime
< max_time
&&
1105 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1107 "Zone continuation line end time is not after end time of previous line"
1112 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1113 (int) ((nzones
+ 1) * sizeof *zones
));
1114 zones
[nzones
++] = z
;
1116 ** If there was an UNTIL field on this line,
1117 ** there's more information about the zone on the next line.
1123 inleap(fields
, nfields
)
1124 register char ** const fields
;
1127 register const char * cp
;
1128 register const struct lookup
* lp
;
1130 int year
, month
, day
;
1134 if (nfields
!= LEAP_FIELDS
) {
1135 error(_("wrong number of fields on Leap line"));
1139 cp
= fields
[LP_YEAR
];
1140 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1144 error(_("invalid leaping year"));
1147 if (!leapseen
|| leapmaxyear
< year
)
1149 if (!leapseen
|| leapminyear
> year
)
1155 i
= len_years
[isleap(j
)];
1159 i
= -len_years
[isleap(j
)];
1161 dayoff
= oadd(dayoff
, eitol(i
));
1163 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1164 error(_("invalid month name"));
1167 month
= lp
->l_value
;
1169 while (j
!= month
) {
1170 i
= len_months
[isleap(year
)][j
];
1171 dayoff
= oadd(dayoff
, eitol(i
));
1174 cp
= fields
[LP_DAY
];
1175 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1176 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1177 error(_("invalid day of month"));
1180 dayoff
= oadd(dayoff
, eitol(day
- 1));
1181 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1182 error(_("time before zero"));
1185 if (dayoff
< min_time
/ SECSPERDAY
) {
1186 error(_("time too small"));
1189 if (dayoff
> max_time
/ SECSPERDAY
) {
1190 error(_("time too large"));
1193 t
= (zic_t
) dayoff
* SECSPERDAY
;
1194 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1195 cp
= fields
[LP_CORR
];
1197 register int positive
;
1200 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1203 } else if (strcmp(cp
, "--") == 0) {
1206 } else if (strcmp(cp
, "+") == 0) {
1209 } else if (strcmp(cp
, "++") == 0) {
1213 error(_("illegal CORRECTION field on Leap line"));
1216 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1218 "illegal Rolling/Stationary field on Leap line"
1222 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1227 inlink(fields
, nfields
)
1228 register char ** const fields
;
1233 if (nfields
!= LINK_FIELDS
) {
1234 error(_("wrong number of fields on Link line"));
1237 if (*fields
[LF_FROM
] == '\0') {
1238 error(_("blank FROM field on Link line"));
1241 if (*fields
[LF_TO
] == '\0') {
1242 error(_("blank TO field on Link line"));
1245 l
.l_filename
= filename
;
1246 l
.l_linenum
= linenum
;
1247 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1248 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1249 links
= (struct link
*) (void *) erealloc((char *) links
,
1250 (int) ((nlinks
+ 1) * sizeof *links
));
1251 links
[nlinks
++] = l
;
1255 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1256 register struct rule
* const rp
;
1257 const char * const loyearp
;
1258 const char * const hiyearp
;
1259 const char * const typep
;
1260 const char * const monthp
;
1261 const char * const dayp
;
1262 const char * const timep
;
1264 register const struct lookup
* lp
;
1265 register const char * cp
;
1269 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1270 error(_("invalid month name"));
1273 rp
->r_month
= lp
->l_value
;
1274 rp
->r_todisstd
= FALSE
;
1275 rp
->r_todisgmt
= FALSE
;
1276 dp
= ecpyalloc(timep
);
1278 ep
= dp
+ strlen(dp
) - 1;
1279 switch (lowerit(*ep
)) {
1280 case 's': /* Standard */
1281 rp
->r_todisstd
= TRUE
;
1282 rp
->r_todisgmt
= FALSE
;
1285 case 'w': /* Wall */
1286 rp
->r_todisstd
= FALSE
;
1287 rp
->r_todisgmt
= FALSE
;
1290 case 'g': /* Greenwich */
1291 case 'u': /* Universal */
1292 case 'z': /* Zulu */
1293 rp
->r_todisstd
= TRUE
;
1294 rp
->r_todisgmt
= TRUE
;
1299 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1305 lp
= byword(cp
, begin_years
);
1306 rp
->r_lowasnum
= lp
== NULL
;
1307 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1309 rp
->r_loyear
= INT_MIN
;
1312 rp
->r_loyear
= INT_MAX
;
1314 default: /* "cannot happen" */
1315 (void) fprintf(stderr
,
1316 _("%s: panic: Invalid l_value %d\n"),
1317 progname
, lp
->l_value
);
1319 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1320 error(_("invalid starting year"));
1324 lp
= byword(cp
, end_years
);
1325 rp
->r_hiwasnum
= lp
== NULL
;
1326 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1328 rp
->r_hiyear
= INT_MIN
;
1331 rp
->r_hiyear
= INT_MAX
;
1334 rp
->r_hiyear
= rp
->r_loyear
;
1336 default: /* "cannot happen" */
1337 (void) fprintf(stderr
,
1338 _("%s: panic: Invalid l_value %d\n"),
1339 progname
, lp
->l_value
);
1341 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1342 error(_("invalid ending year"));
1345 if (rp
->r_loyear
> rp
->r_hiyear
) {
1346 error(_("starting year greater than ending year"));
1350 rp
->r_yrtype
= NULL
;
1352 if (rp
->r_loyear
== rp
->r_hiyear
) {
1353 error(_("typed single year"));
1356 rp
->r_yrtype
= ecpyalloc(typep
);
1360 ** Accept things such as:
1366 dp
= ecpyalloc(dayp
);
1367 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1368 rp
->r_dycode
= DC_DOWLEQ
;
1369 rp
->r_wday
= lp
->l_value
;
1370 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1372 if ((ep
= strchr(dp
, '<')) != 0)
1373 rp
->r_dycode
= DC_DOWLEQ
;
1374 else if ((ep
= strchr(dp
, '>')) != 0)
1375 rp
->r_dycode
= DC_DOWGEQ
;
1378 rp
->r_dycode
= DC_DOM
;
1380 if (rp
->r_dycode
!= DC_DOM
) {
1383 error(_("invalid day of month"));
1387 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1388 error(_("invalid weekday name"));
1392 rp
->r_wday
= lp
->l_value
;
1394 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1395 rp
->r_dayofmonth
<= 0 ||
1396 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1397 error(_("invalid day of month"));
1413 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1414 buf
[i
] = val
>> shift
;
1425 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1426 buf
[i
] = val
>> shift
;
1437 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1441 puttzcode64(val
, fp
)
1447 convert64(val
, buf
);
1448 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1456 const zic_t a
= ((const struct attype
*) avp
)->at
;
1457 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1459 return (a
< b
) ? -1 : (a
> b
);
1466 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1470 writezone(name
, string
)
1471 const char * const name
;
1472 const char * const string
;
1476 register int leapcnt32
, leapi32
;
1477 register int timecnt32
, timei32
;
1479 static char * fullname
;
1480 static const struct tzhead tzh0
;
1481 static struct tzhead tzh
;
1482 zic_t ats
[TZ_MAX_TIMES
];
1483 unsigned char types
[TZ_MAX_TIMES
];
1489 (void) qsort((void *) attypes
, (size_t) timecnt
,
1490 (size_t) sizeof *attypes
, atcomp
);
1500 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1503 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1504 ++fromi
; /* handled by default rule */
1505 for ( ; fromi
< timecnt
; ++fromi
) {
1506 if (toi
!= 0 && ((attypes
[fromi
].at
+
1507 gmtoffs
[attypes
[toi
- 1].type
]) <=
1508 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1509 : attypes
[toi
- 2].type
]))) {
1510 attypes
[toi
- 1].type
=
1511 attypes
[fromi
].type
;
1515 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1516 attypes
[toi
++] = attypes
[fromi
];
1523 for (i
= 0; i
< timecnt
; ++i
) {
1524 ats
[i
] = attypes
[i
].at
;
1525 types
[i
] = attypes
[i
].type
;
1528 ** Correct for leap seconds.
1530 for (i
= 0; i
< timecnt
; ++i
) {
1533 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1534 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1539 ** Figure out 32-bit-limited starts and counts.
1541 timecnt32
= timecnt
;
1543 leapcnt32
= leapcnt
;
1545 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1547 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1551 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1553 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1557 fullname
= erealloc(fullname
,
1558 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1559 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1561 ** Remove old file, if any, to snap links.
1563 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1564 const char *e
= strerror(errno
);
1566 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1567 progname
, fullname
, e
);
1570 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1571 if (mkdirs(fullname
) != 0)
1573 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1574 const char *e
= strerror(errno
);
1576 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1577 progname
, fullname
, e
);
1581 for (pass
= 1; pass
<= 2; ++pass
) {
1582 register int thistimei
, thistimecnt
;
1583 register int thisleapi
, thisleapcnt
;
1584 register int thistimelim
, thisleaplim
;
1585 int writetype
[TZ_MAX_TIMES
];
1586 int typemap
[TZ_MAX_TYPES
];
1587 register int thistypecnt
;
1588 char thischars
[TZ_MAX_CHARS
];
1590 int indmap
[TZ_MAX_CHARS
];
1593 thistimei
= timei32
;
1594 thistimecnt
= timecnt32
;
1595 thisleapi
= leapi32
;
1596 thisleapcnt
= leapcnt32
;
1599 thistimecnt
= timecnt
;
1601 thisleapcnt
= leapcnt
;
1603 thistimelim
= thistimei
+ thistimecnt
;
1604 thisleaplim
= thisleapi
+ thisleapcnt
;
1605 for (i
= 0; i
< typecnt
; ++i
)
1606 writetype
[i
] = thistimecnt
== timecnt
;
1607 if (thistimecnt
== 0) {
1609 ** No transition times fall in the current
1610 ** (32- or 64-bit) window.
1613 writetype
[typecnt
- 1] = TRUE
;
1615 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1617 writetype
[types
[i
]] = TRUE
;
1619 ** For America/Godthab and Antarctica/Palmer
1622 writetype
[0] = TRUE
;
1625 for (i
= 0; i
< typecnt
; ++i
)
1626 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : -1;
1627 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1630 for (i
= 0; i
< typecnt
; ++i
) {
1631 register char * thisabbr
;
1635 if (indmap
[abbrinds
[i
]] >= 0)
1637 thisabbr
= &chars
[abbrinds
[i
]];
1638 for (j
= 0; j
< thischarcnt
; ++j
)
1639 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1641 if (j
== thischarcnt
) {
1642 (void) strcpy(&thischars
[(int) thischarcnt
],
1644 thischarcnt
+= strlen(thisabbr
) + 1;
1646 indmap
[abbrinds
[i
]] = j
;
1648 #define DO(field) (void) fwrite((void *) tzh.field, \
1649 (size_t) sizeof tzh.field, (size_t) 1, fp)
1651 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1652 tzh
.tzh_version
[0] = ZIC_VERSION
;
1653 convert(eitol(thistypecnt
), tzh
.tzh_ttisgmtcnt
);
1654 convert(eitol(thistypecnt
), tzh
.tzh_ttisstdcnt
);
1655 convert(eitol(thisleapcnt
), tzh
.tzh_leapcnt
);
1656 convert(eitol(thistimecnt
), tzh
.tzh_timecnt
);
1657 convert(eitol(thistypecnt
), tzh
.tzh_typecnt
);
1658 convert(eitol(thischarcnt
), tzh
.tzh_charcnt
);
1669 for (i
= thistimei
; i
< thistimelim
; ++i
)
1671 puttzcode((long) ats
[i
], fp
);
1672 else puttzcode64(ats
[i
], fp
);
1673 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1676 uc
= typemap
[types
[i
]];
1677 (void) fwrite((void *) &uc
,
1682 for (i
= 0; i
< typecnt
; ++i
)
1684 puttzcode(gmtoffs
[i
], fp
);
1685 (void) putc(isdsts
[i
], fp
);
1686 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1688 if (thischarcnt
!= 0)
1689 (void) fwrite((void *) thischars
,
1690 (size_t) sizeof thischars
[0],
1691 (size_t) thischarcnt
, fp
);
1692 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1693 register zic_t todo
;
1696 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1699 if (++j
>= typecnt
) {
1705 while (j
< timecnt
&&
1710 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1711 } else todo
= trans
[i
];
1713 puttzcode((long) todo
, fp
);
1714 else puttzcode64(todo
, fp
);
1715 puttzcode(corr
[i
], fp
);
1717 for (i
= 0; i
< typecnt
; ++i
)
1719 (void) putc(ttisstds
[i
], fp
);
1720 for (i
= 0; i
< typecnt
; ++i
)
1722 (void) putc(ttisgmts
[i
], fp
);
1724 (void) fprintf(fp
, "\n%s\n", string
);
1725 if (ferror(fp
) || fclose(fp
)) {
1726 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1727 progname
, fullname
);
1733 doabbr(abbr
, format
, letters
, isdst
, doquotes
)
1735 const char * const format
;
1736 const char * const letters
;
1741 register char * slashp
;
1744 slashp
= strchr(format
, '/');
1745 if (slashp
== NULL
) {
1746 if (letters
== NULL
)
1747 (void) strcpy(abbr
, format
);
1748 else (void) sprintf(abbr
, format
, letters
);
1750 (void) strcpy(abbr
, slashp
+ 1);
1752 if (slashp
> format
)
1753 (void) strncpy(abbr
, format
,
1754 (unsigned) (slashp
- format
));
1755 abbr
[slashp
- format
] = '\0';
1759 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1760 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1761 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1764 if (len
> 0 && *cp
== '\0')
1766 abbr
[len
+ 2] = '\0';
1767 abbr
[len
+ 1] = '>';
1768 for ( ; len
> 0; --len
)
1769 abbr
[len
] = abbr
[len
- 1];
1784 stringoffset(result
, offset
)
1789 register int minutes
;
1790 register int seconds
;
1794 (void) strcpy(result
, "-");
1797 seconds
= offset
% SECSPERMIN
;
1798 offset
/= SECSPERMIN
;
1799 minutes
= offset
% MINSPERHOUR
;
1800 offset
/= MINSPERHOUR
;
1802 if (hours
>= HOURSPERDAY
) {
1806 (void) sprintf(end(result
), "%d", hours
);
1807 if (minutes
!= 0 || seconds
!= 0) {
1808 (void) sprintf(end(result
), ":%02d", minutes
);
1810 (void) sprintf(end(result
), ":%02d", seconds
);
1816 stringrule(result
, rp
, dstoff
, gmtoff
)
1818 const struct rule
* const rp
;
1824 result
= end(result
);
1825 if (rp
->r_dycode
== DC_DOM
) {
1826 register int month
, total
;
1828 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1831 for (month
= 0; month
< rp
->r_month
; ++month
)
1832 total
+= len_months
[0][month
];
1833 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1837 if (rp
->r_dycode
== DC_DOWGEQ
) {
1838 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1839 if ((week
- 1) * DAYSPERWEEK
+ 1 != rp
->r_dayofmonth
)
1841 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1842 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1845 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1846 if (week
* DAYSPERWEEK
- 1 != rp
->r_dayofmonth
)
1849 } else return -1; /* "cannot happen" */
1850 (void) sprintf(result
, "M%d.%d.%d",
1851 rp
->r_month
+ 1, week
, rp
->r_wday
);
1856 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1862 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1863 (void) strcat(result
, "/");
1864 if (stringoffset(end(result
), tod
) != 0)
1871 stringzone(result
, zpfirst
, zonecount
)
1873 const struct zone
* const zpfirst
;
1874 const int zonecount
;
1876 register const struct zone
* zp
;
1877 register struct rule
* rp
;
1878 register struct rule
* stdrp
;
1879 register struct rule
* dstrp
;
1881 register const char * abbrvar
;
1884 zp
= zpfirst
+ zonecount
- 1;
1885 stdrp
= dstrp
= NULL
;
1886 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1887 rp
= &zp
->z_rules
[i
];
1888 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= INT_MAX
)
1890 if (rp
->r_yrtype
!= NULL
)
1892 if (rp
->r_stdoff
== 0) {
1902 if (stdrp
== NULL
&& dstrp
== NULL
) {
1904 ** There are no rules running through "max".
1905 ** Let's find the latest rule.
1907 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1908 rp
= &zp
->z_rules
[i
];
1909 if (stdrp
== NULL
|| rp
->r_hiyear
> stdrp
->r_hiyear
||
1910 (rp
->r_hiyear
== stdrp
->r_hiyear
&&
1911 rp
->r_month
> stdrp
->r_month
))
1914 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0)
1915 return; /* We end up in DST (a POSIX no-no). */
1917 ** Horrid special case: if year is 2037,
1918 ** presume this is a zone handled on a year-by-year basis;
1919 ** do not try to apply a rule to the zone.
1921 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1924 if (stdrp
== NULL
&& zp
->z_nrules
!= 0)
1926 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1927 doabbr(result
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1928 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
1934 doabbr(end(result
), zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
1935 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
1936 if (stringoffset(end(result
),
1937 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
1941 (void) strcat(result
, ",");
1942 if (stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1946 (void) strcat(result
, ",");
1947 if (stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1954 outzone(zpfirst
, zonecount
)
1955 const struct zone
* const zpfirst
;
1956 const int zonecount
;
1958 register const struct zone
* zp
;
1959 register struct rule
* rp
;
1961 register int usestart
, useuntil
;
1962 register zic_t starttime
, untiltime
;
1963 register long gmtoff
;
1964 register long stdoff
;
1966 register long startoff
;
1967 register int startttisstd
;
1968 register int startttisgmt
;
1970 register char * startbuf
;
1972 register char * envvar
;
1973 register int max_abbr_len
;
1974 register int max_envvar_len
;
1976 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
1977 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
1978 startbuf
= emalloc(max_abbr_len
+ 1);
1979 ab
= emalloc(max_abbr_len
+ 1);
1980 envvar
= emalloc(max_envvar_len
+ 1);
1981 INITIALIZE(untiltime
);
1982 INITIALIZE(starttime
);
1984 ** Now. . .finally. . .generate some useful data!
1990 ** Thanks to Earl Chew
1991 ** for noting the need to unconditionally initialize startttisstd.
1993 startttisstd
= FALSE
;
1994 startttisgmt
= FALSE
;
1995 min_year
= max_year
= EPOCH_YEAR
;
1997 updateminmax(leapminyear
);
1998 updateminmax(leapmaxyear
+ (leapmaxyear
< INT_MAX
));
2000 for (i
= 0; i
< zonecount
; ++i
) {
2002 if (i
< zonecount
- 1)
2003 updateminmax(zp
->z_untilrule
.r_loyear
);
2004 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2005 rp
= &zp
->z_rules
[j
];
2007 updateminmax(rp
->r_loyear
);
2009 updateminmax(rp
->r_hiyear
);
2013 ** Generate lots of data if a rule can't cover all future times.
2015 stringzone(envvar
, zpfirst
, zonecount
);
2016 if (noise
&& envvar
[0] == '\0') {
2019 wp
= ecpyalloc(_("no POSIX environment variable for zone"));
2020 wp
= ecatalloc(wp
, " ");
2021 wp
= ecatalloc(wp
, zpfirst
->z_name
);
2025 if (envvar
[0] == '\0') {
2026 if (min_year
>= INT_MIN
+ YEARSPERREPEAT
)
2027 min_year
-= YEARSPERREPEAT
;
2028 else min_year
= INT_MIN
;
2029 if (max_year
<= INT_MAX
- YEARSPERREPEAT
)
2030 max_year
+= YEARSPERREPEAT
;
2031 else max_year
= INT_MAX
;
2034 ** For the benefit of older systems,
2035 ** generate data from 1900 through 2037.
2037 if (min_year
> 1900)
2039 if (max_year
< 2037)
2041 for (i
= 0; i
< zonecount
; ++i
) {
2043 ** A guess that may well be corrected later.
2047 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2048 useuntil
= i
< (zonecount
- 1);
2049 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2051 gmtoff
= zp
->z_gmtoff
;
2052 eat(zp
->z_filename
, zp
->z_linenum
);
2054 startoff
= zp
->z_gmtoff
;
2055 if (zp
->z_nrules
== 0) {
2056 stdoff
= zp
->z_stdoff
;
2057 doabbr(startbuf
, zp
->z_format
,
2058 (char *) NULL
, stdoff
!= 0, FALSE
);
2059 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2060 startbuf
, stdoff
!= 0, startttisstd
,
2063 addtt(starttime
, type
);
2065 } else if (stdoff
!= 0)
2066 addtt(min_time
, type
);
2067 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2068 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2071 ** Mark which rules to do in the current year.
2072 ** For those to do, calculate rpytime(rp, year);
2074 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2075 rp
= &zp
->z_rules
[j
];
2076 eats(zp
->z_filename
, zp
->z_linenum
,
2077 rp
->r_filename
, rp
->r_linenum
);
2078 rp
->r_todo
= year
>= rp
->r_loyear
&&
2079 year
<= rp
->r_hiyear
&&
2080 yearistype(year
, rp
->r_yrtype
);
2082 rp
->r_temp
= rpytime(rp
, year
);
2086 register zic_t jtime
, ktime
;
2087 register long offset
;
2092 ** Turn untiltime into UTC
2093 ** assuming the current gmtoff and
2096 untiltime
= zp
->z_untiltime
;
2097 if (!zp
->z_untilrule
.r_todisgmt
)
2098 untiltime
= tadd(untiltime
,
2100 if (!zp
->z_untilrule
.r_todisstd
)
2101 untiltime
= tadd(untiltime
,
2105 ** Find the rule (of those to do, if any)
2106 ** that takes effect earliest in the year.
2109 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2110 rp
= &zp
->z_rules
[j
];
2113 eats(zp
->z_filename
, zp
->z_linenum
,
2114 rp
->r_filename
, rp
->r_linenum
);
2115 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2116 if (!rp
->r_todisstd
)
2117 offset
= oadd(offset
, stdoff
);
2119 if (jtime
== min_time
||
2122 jtime
= tadd(jtime
, -offset
);
2123 if (k
< 0 || jtime
< ktime
) {
2129 break; /* go on to next year */
2130 rp
= &zp
->z_rules
[k
];
2132 if (useuntil
&& ktime
>= untiltime
)
2134 stdoff
= rp
->r_stdoff
;
2135 if (usestart
&& ktime
== starttime
)
2138 if (ktime
< starttime
) {
2139 startoff
= oadd(zp
->z_gmtoff
,
2141 doabbr(startbuf
, zp
->z_format
,
2147 if (*startbuf
== '\0' &&
2148 startoff
== oadd(zp
->z_gmtoff
,
2158 eats(zp
->z_filename
, zp
->z_linenum
,
2159 rp
->r_filename
, rp
->r_linenum
);
2160 doabbr(ab
, zp
->z_format
, rp
->r_abbrvar
,
2161 rp
->r_stdoff
!= 0, FALSE
);
2162 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2163 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2164 rp
->r_todisstd
, rp
->r_todisgmt
);
2169 if (*startbuf
== '\0' &&
2170 zp
->z_format
!= NULL
&&
2171 strchr(zp
->z_format
, '%') == NULL
&&
2172 strchr(zp
->z_format
, '/') == NULL
)
2173 (void) strcpy(startbuf
, zp
->z_format
);
2174 eat(zp
->z_filename
, zp
->z_linenum
);
2175 if (*startbuf
== '\0')
2176 error(_("can't determine time zone abbreviation to use just after until time"));
2177 else addtt(starttime
,
2178 addtype(startoff
, startbuf
,
2179 startoff
!= zp
->z_gmtoff
,
2184 ** Now we may get to set starttime for the next zone line.
2187 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2188 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2189 starttime
= zp
->z_untiltime
;
2191 starttime
= tadd(starttime
, -stdoff
);
2193 starttime
= tadd(starttime
, -gmtoff
);
2196 writezone(zpfirst
->z_name
, envvar
);
2203 addtt(starttime
, type
)
2204 const zic_t starttime
;
2207 if (starttime
<= min_time
||
2208 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2209 gmtoffs
[0] = gmtoffs
[type
];
2210 isdsts
[0] = isdsts
[type
];
2211 ttisstds
[0] = ttisstds
[type
];
2212 ttisgmts
[0] = ttisgmts
[type
];
2213 if (abbrinds
[type
] != 0)
2214 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2216 charcnt
= strlen(chars
) + 1;
2221 if (timecnt
>= TZ_MAX_TIMES
) {
2222 error(_("too many transitions?!"));
2225 attypes
[timecnt
].at
= starttime
;
2226 attypes
[timecnt
].type
= type
;
2231 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
2233 const char * const abbr
;
2240 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2241 error(_("internal error - addtype called with bad isdst"));
2244 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2245 error(_("internal error - addtype called with bad ttisstd"));
2248 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2249 error(_("internal error - addtype called with bad ttisgmt"));
2253 ** See if there's already an entry for this zone type.
2254 ** If so, just return its index.
2256 for (i
= 0; i
< typecnt
; ++i
) {
2257 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2258 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2259 ttisstd
== ttisstds
[i
] &&
2260 ttisgmt
== ttisgmts
[i
])
2264 ** There isn't one; add a new one, unless there are already too
2267 if (typecnt
>= TZ_MAX_TYPES
) {
2268 error(_("too many local time types"));
2271 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2272 error(_("UTC offset out of range"));
2275 gmtoffs
[i
] = gmtoff
;
2277 ttisstds
[i
] = ttisstd
;
2278 ttisgmts
[i
] = ttisgmt
;
2280 for (j
= 0; j
< charcnt
; ++j
)
2281 if (strcmp(&chars
[j
], abbr
) == 0)
2291 leapadd(t
, positive
, rolling
, count
)
2299 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2300 error(_("too many leap seconds"));
2303 for (i
= 0; i
< leapcnt
; ++i
)
2304 if (t
<= trans
[i
]) {
2305 if (t
== trans
[i
]) {
2306 error(_("repeated leap second moment"));
2312 for (j
= leapcnt
; j
> i
; --j
) {
2313 trans
[j
] = trans
[j
- 1];
2314 corr
[j
] = corr
[j
- 1];
2315 roll
[j
] = roll
[j
- 1];
2318 corr
[i
] = positive
? 1L : eitol(-count
);
2321 } while (positive
&& --count
!= 0);
2328 register long last
= 0;
2331 ** propagate leap seconds forward
2333 for (i
= 0; i
< leapcnt
; ++i
) {
2334 trans
[i
] = tadd(trans
[i
], last
);
2335 last
= corr
[i
] += last
;
2340 yearistype(year
, type
)
2342 const char * const type
;
2347 if (type
== NULL
|| *type
== '\0')
2349 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
2350 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
2351 result
= system(buf
);
2352 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2358 error(_("Wild result from command execution"));
2359 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2360 progname
, buf
, result
);
2369 a
= (unsigned char) a
;
2370 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2374 ciequal(ap
, bp
) /* case-insensitive equality */
2375 register const char * ap
;
2376 register const char * bp
;
2378 while (lowerit(*ap
) == lowerit(*bp
++))
2386 register const char * abbr
;
2387 register const char * word
;
2389 if (lowerit(*abbr
) != lowerit(*word
))
2392 while (*++abbr
!= '\0')
2396 } while (lowerit(*word
++) != lowerit(*abbr
));
2400 static const struct lookup
*
2402 register const char * const word
;
2403 register const struct lookup
* const table
;
2405 register const struct lookup
* foundlp
;
2406 register const struct lookup
* lp
;
2408 if (word
== NULL
|| table
== NULL
)
2411 ** Look for exact match.
2413 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2414 if (ciequal(word
, lp
->l_word
))
2417 ** Look for inexact match.
2420 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2421 if (itsabbr(word
, lp
->l_word
)) {
2422 if (foundlp
== NULL
)
2424 else return NULL
; /* multiple inexact matches */
2434 register char ** array
;
2439 array
= (char **) (void *)
2440 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2443 while (isascii((unsigned char) *cp
) &&
2444 isspace((unsigned char) *cp
))
2446 if (*cp
== '\0' || *cp
== '#')
2448 array
[nsubs
++] = dp
= cp
;
2450 if ((*dp
= *cp
++) != '"')
2452 else while ((*dp
= *cp
++) != '"')
2457 "Odd number of quotation marks"
2461 } while (*cp
!= '\0' && *cp
!= '#' &&
2462 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2463 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2467 array
[nsubs
] = NULL
;
2479 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2480 error(_("time overflow"));
2493 if (t1
== max_time
&& t2
> 0)
2495 if (t1
== min_time
&& t2
< 0)
2498 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2499 error(_("time overflow"));
2506 ** Given a rule, and a year, compute the date - in seconds since January 1,
2507 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2511 rpytime(rp
, wantedy
)
2512 register const struct rule
* const rp
;
2513 register const int wantedy
;
2515 register int y
, m
, i
;
2516 register long dayoff
; /* with a nod to Margaret O. */
2519 if (wantedy
== INT_MIN
)
2521 if (wantedy
== INT_MAX
)
2526 while (wantedy
!= y
) {
2528 i
= len_years
[isleap(y
)];
2532 i
= -len_years
[isleap(y
)];
2534 dayoff
= oadd(dayoff
, eitol(i
));
2536 while (m
!= rp
->r_month
) {
2537 i
= len_months
[isleap(y
)][m
];
2538 dayoff
= oadd(dayoff
, eitol(i
));
2541 i
= rp
->r_dayofmonth
;
2542 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2543 if (rp
->r_dycode
== DC_DOWLEQ
)
2546 error(_("use of 2/29 in non leap-year"));
2551 dayoff
= oadd(dayoff
, eitol(i
));
2552 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2555 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2556 wday
= eitol(EPOCH_WDAY
);
2558 ** Don't trust mod of negative numbers.
2561 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2563 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2565 wday
+= LDAYSPERWEEK
;
2567 while (wday
!= eitol(rp
->r_wday
))
2568 if (rp
->r_dycode
== DC_DOWGEQ
) {
2569 dayoff
= oadd(dayoff
, (long) 1);
2570 if (++wday
>= LDAYSPERWEEK
)
2574 dayoff
= oadd(dayoff
, (long) -1);
2576 wday
= LDAYSPERWEEK
- 1;
2579 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2581 warning(_("rule goes past start/end of month--\
2582 will not work with pre-2004 versions of zic"));
2585 if (dayoff
< min_time
/ SECSPERDAY
)
2587 if (dayoff
> max_time
/ SECSPERDAY
)
2589 t
= (zic_t
) dayoff
* SECSPERDAY
;
2590 return tadd(t
, rp
->r_tod
);
2595 const char * const string
;
2599 if (strcmp(string
, GRANDPARENTED
) != 0) {
2600 register const char * cp
;
2604 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2605 ** optionally followed by a + or - and a number from 1 to 14.
2609 while (isascii((unsigned char) *cp
) &&
2610 isalpha((unsigned char) *cp
))
2612 if (cp
- string
== 0)
2613 wp
= _("time zone abbreviation lacks alphabetic at start");
2614 if (noise
&& cp
- string
> 3)
2615 wp
= _("time zone abbreviation has more than 3 alphabetics");
2616 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2617 wp
= _("time zone abbreviation has too many alphabetics");
2618 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2620 if (isascii((unsigned char) *cp
) &&
2621 isdigit((unsigned char) *cp
))
2623 *cp
>= '0' && *cp
<= '4')
2627 wp
= _("time zone abbreviation differs from POSIX standard");
2630 wp
= ecatalloc(wp
, " (");
2631 wp
= ecatalloc(wp
, string
);
2632 wp
= ecatalloc(wp
, ")");
2637 i
= strlen(string
) + 1;
2638 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2639 error(_("too many, or too long, time zone abbreviations"));
2642 (void) strcpy(&chars
[charcnt
], string
);
2643 charcnt
+= eitol(i
);
2650 register char * name
;
2653 if (argname
== NULL
|| *argname
== '\0')
2655 cp
= name
= ecpyalloc(argname
);
2656 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2660 ** DOS drive specifier?
2662 if (isalpha((unsigned char) name
[0]) &&
2663 name
[1] == ':' && name
[2] == '\0') {
2667 #endif /* !defined unix */
2668 if (!itsdir(name
)) {
2670 ** It doesn't seem to exist, so we try to create it.
2671 ** Creation may fail because of the directory being
2672 ** created by some other multiprocessor, so we get
2673 ** to do extra checking.
2675 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2676 const char *e
= strerror(errno
);
2678 if (errno
!= EEXIST
|| !itsdir(name
)) {
2679 (void) fprintf(stderr
,
2680 _("%s: Can't create directory %s: %s\n"),
2700 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2701 (void) fprintf(stderr
,
2702 _("%s: %d did not sign extend correctly\n"),
2710 ** UNIX was a registered trademark of The Open Group in 2003.