1 /* $NetBSD: zic.c,v 1.25 2009/12/31 22:49:16 mlelstv Exp $ */
3 ** This file is in the public domain, so clarified as of
4 ** 2006-07-17 by Arthur David Olson.
7 #if HAVE_NBTOOL_CONFIG_H
8 #include "nbtool_config.h"
11 #include <sys/cdefs.h>
13 __RCSID("$NetBSD: zic.c,v 1.25 2009/12/31 22:49:16 mlelstv Exp $");
14 #endif /* !defined lint */
16 static char elsieid
[] = "@(#)zic.c 8.20";
22 #define ZIC_VERSION '2'
24 typedef int_fast64_t zic_t
;
26 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
27 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
28 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
34 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
36 #define MKDIR_UMASK 0755
42 ** On some ancient hosts, predicates like `isspace(C)' are defined
43 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
44 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
45 ** Neither the C Standard nor Posix require that `isascii' exist.
46 ** For portability, we check both ancient and modern requirements.
47 ** If isascii is not defined, the isascii check succeeds trivially.
54 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
55 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
57 #define end(cp) (strchr((cp), '\0'))
60 const char * r_filename
;
64 int r_loyear
; /* for example, 1986 */
65 int r_hiyear
; /* for example, 1986 */
66 const char * r_yrtype
;
70 int r_month
; /* 0..11 */
72 int r_dycode
; /* see below */
76 long r_tod
; /* time from midnight */
77 int r_todisstd
; /* above is standard time if TRUE */
78 /* or wall clock time if FALSE */
79 int r_todisgmt
; /* above is GMT if TRUE */
80 /* or local time if FALSE */
81 long r_stdoff
; /* offset from standard time */
82 const char * r_abbrvar
; /* variable part of abbreviation */
84 int r_todo
; /* a rule to do (used in outzone) */
85 zic_t r_temp
; /* used in outzone */
89 ** r_dycode r_dayofmonth r_wday
92 #define DC_DOM 0 /* 1..31 */ /* unused */
93 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
94 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
97 const char * z_filename
;
103 const char * z_format
;
107 struct rule
* z_rules
;
110 struct rule z_untilrule
;
114 extern int getopt(int argc
, char * const argv
[],
115 const char * options
);
116 extern int link(const char * fromname
, const char * toname
);
117 extern char * optarg
;
120 static void addtt(zic_t starttime
, int type
);
121 static int addtype(long gmtoff
, const char * abbr
, int isdst
,
122 int ttisstd
, int ttisgmt
);
123 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
124 static void adjleap(void);
125 static void associate(void);
126 static int ciequal(const char * ap
, const char * bp
);
127 static void convert(long val
, char * buf
);
128 static void convert64(zic_t val
, char * buf
);
129 static void dolink(const char * fromfield
, const char * tofield
);
130 static void doabbr(char * abbr
, const int, const char * format
,
131 const char * letters
, int isdst
, int doquotes
);
132 static void eat(const char * name
, int num
);
133 static void eats(const char * name
, int num
,
134 const char * rname
, int rnum
);
135 static long eitol(int i
);
136 static void error(const char * message
);
137 static char ** getfields(char * buf
);
138 static long gethms(const char * string
, const char * errstrng
,
140 static void infile(const char * filename
);
141 static void inleap(char ** fields
, int nfields
);
142 static void inlink(char ** fields
, int nfields
);
143 static void inrule(char ** fields
, int nfields
);
144 static int inzcont(char ** fields
, int nfields
);
145 static int inzone(char ** fields
, int nfields
);
146 static int inzsub(char ** fields
, int nfields
, int iscont
);
147 static int is32(zic_t x
);
148 static int itsabbr(const char * abbr
, const char * word
);
149 static int itsdir(const char * name
);
150 static int lowerit(int c
);
151 int main(int, char **);
152 static char * memcheck(char * tocheck
);
153 static int mkdirs(char * filename
);
154 static void newabbr(const char * abbr
);
155 static long oadd(long t1
, long t2
);
156 static void outzone(const struct zone
* zp
, int ntzones
);
157 static void puttzcode(long code
, FILE * fp
);
158 static void puttzcode64(zic_t code
, FILE * fp
);
159 static int rcomp(const void * leftp
, const void * rightp
);
160 static zic_t
rpytime(const struct rule
* rp
, int wantedy
);
161 static void rulesub(struct rule
* rp
,
162 const char * loyearp
, const char * hiyearp
,
163 const char * typep
, const char * monthp
,
164 const char * dayp
, const char * timep
);
165 static int stringoffset(char * result
, long offset
);
166 static int stringrule(char * result
, const struct rule
* rp
,
167 long dstoff
, long gmtoff
);
168 static void stringzone(char * result
, const int,
169 const struct zone
* zp
, int ntzones
);
170 static void setboundaries(void);
171 static zic_t
tadd(zic_t t1
, long t2
);
172 static void usage(FILE *stream
, int status
);
173 static void warning(const char * const);
174 static void writezone(const char * name
, const char * string
);
175 static int yearistype(int year
, const char * type
);
176 static int atcomp(const void *avp
, const void *bvp
);
177 static void updateminmax(int x
);
181 static const char * filename
;
184 static int leapminyear
;
185 static int leapmaxyear
;
187 static int max_abbrvar_len
;
188 static int max_format_len
;
189 static zic_t max_time
;
191 static zic_t min_time
;
194 static const char * rfilename
;
196 static const char * progname
;
210 ** Which fields are which on a Zone line.
218 #define ZF_TILMONTH 6
221 #define ZONE_MINFIELDS 5
222 #define ZONE_MAXFIELDS 9
225 ** Which fields are which on a Zone continuation line.
231 #define ZFC_TILYEAR 3
232 #define ZFC_TILMONTH 4
234 #define ZFC_TILTIME 6
235 #define ZONEC_MINFIELDS 3
236 #define ZONEC_MAXFIELDS 7
239 ** Which files are which on a Rule line.
251 #define RULE_FIELDS 10
254 ** Which fields are which on a Link line.
259 #define LINK_FIELDS 3
262 ** Which fields are which on a Leap line.
271 #define LEAP_FIELDS 7
281 static struct rule
* rules
;
282 static int nrules
; /* number of rules */
284 static struct zone
* zones
;
285 static int nzones
; /* number of zones */
288 const char * l_filename
;
294 static struct link
* links
;
302 static struct lookup
const * byword(const char * string
,
303 const struct lookup
* lp
);
305 static struct lookup
const line_codes
[] = {
313 static struct lookup
const mon_names
[] = {
314 { "January", TM_JANUARY
},
315 { "February", TM_FEBRUARY
},
316 { "March", TM_MARCH
},
317 { "April", TM_APRIL
},
321 { "August", TM_AUGUST
},
322 { "September", TM_SEPTEMBER
},
323 { "October", TM_OCTOBER
},
324 { "November", TM_NOVEMBER
},
325 { "December", TM_DECEMBER
},
329 static struct lookup
const wday_names
[] = {
330 { "Sunday", TM_SUNDAY
},
331 { "Monday", TM_MONDAY
},
332 { "Tuesday", TM_TUESDAY
},
333 { "Wednesday", TM_WEDNESDAY
},
334 { "Thursday", TM_THURSDAY
},
335 { "Friday", TM_FRIDAY
},
336 { "Saturday", TM_SATURDAY
},
340 static struct lookup
const lasts
[] = {
341 { "last-Sunday", TM_SUNDAY
},
342 { "last-Monday", TM_MONDAY
},
343 { "last-Tuesday", TM_TUESDAY
},
344 { "last-Wednesday", TM_WEDNESDAY
},
345 { "last-Thursday", TM_THURSDAY
},
346 { "last-Friday", TM_FRIDAY
},
347 { "last-Saturday", TM_SATURDAY
},
351 static struct lookup
const begin_years
[] = {
352 { "minimum", YR_MINIMUM
},
353 { "maximum", YR_MAXIMUM
},
357 static struct lookup
const end_years
[] = {
358 { "minimum", YR_MINIMUM
},
359 { "maximum", YR_MAXIMUM
},
364 static struct lookup
const leap_types
[] = {
366 { "Stationary", FALSE
},
370 static const int len_months
[2][MONSPERYEAR
] = {
371 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
372 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
375 static const int len_years
[2] = {
376 DAYSPERNYEAR
, DAYSPERLYEAR
379 static struct attype
{
382 } attypes
[TZ_MAX_TIMES
];
383 static long gmtoffs
[TZ_MAX_TYPES
];
384 static char isdsts
[TZ_MAX_TYPES
];
385 static unsigned char abbrinds
[TZ_MAX_TYPES
];
386 static char ttisstds
[TZ_MAX_TYPES
];
387 static char ttisgmts
[TZ_MAX_TYPES
];
388 static char chars
[TZ_MAX_CHARS
];
389 static zic_t trans
[TZ_MAX_LEAPS
];
390 static long corr
[TZ_MAX_LEAPS
];
391 static char roll
[TZ_MAX_LEAPS
];
394 ** Memory allocation.
402 const char *e
= strerror(errno
);
404 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
411 #define emalloc(size) memcheck(imalloc(size))
412 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
413 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
414 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
421 eats(name
, num
, rname
, rnum
)
422 const char * const name
;
424 const char * const rname
;
435 const char * const name
;
438 eats(name
, num
, (char *) NULL
, -1);
443 const char * const string
;
446 ** Match the format of "cc" to allow sh users to
447 ** zic ... 2>&1 | error -t "*" -v
450 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
451 filename
, linenum
, string
);
452 if (rfilename
!= NULL
)
453 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
454 rfilename
, rlinenum
);
455 (void) fprintf(stderr
, "\n");
461 const char * const string
;
465 cp
= ecpyalloc(_("warning: "));
466 cp
= ecatalloc(cp
, string
);
473 usage(FILE *stream
, int status
)
475 (void) fprintf(stream
, _("%s: usage is %s \
476 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
477 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
479 Report bugs to tz@elsie.nci.nih.gov.\n"),
484 static const char * psxrules
;
485 static const char * lcltime
;
486 static const char * directory
;
487 static const char * leapsec
;
488 static const char * yitcommand
;
499 #ifdef _POSIX_VERSION
500 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
501 #endif /* defined _POSIX_VERSION */
503 (void) setlocale(LC_MESSAGES
, "");
505 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
506 #endif /* defined TEXTDOMAINDIR */
507 (void) textdomain(TZ_DOMAIN
);
508 #endif /* HAVE_GETTEXT */
510 if (TYPE_BIT(zic_t
) < 64) {
511 (void) fprintf(stderr
, "%s: %s\n", progname
,
512 _("wild compilation-time specification of zic_t"));
515 for (i
= 1; i
< argc
; ++i
)
516 if (strcmp(argv
[i
], "--version") == 0) {
517 (void) printf("%s\n", elsieid
);
519 } else if (strcmp(argv
[i
], "--help") == 0) {
520 usage(stdout
, EXIT_SUCCESS
);
522 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
525 usage(stderr
, EXIT_FAILURE
);
527 if (directory
== NULL
)
530 (void) fprintf(stderr
,
531 _("%s: More than one -d option specified\n"),
540 (void) fprintf(stderr
,
541 _("%s: More than one -l option specified\n"),
547 if (psxrules
== NULL
)
550 (void) fprintf(stderr
,
551 _("%s: More than one -p option specified\n"),
557 if (yitcommand
== NULL
)
560 (void) fprintf(stderr
,
561 _("%s: More than one -y option specified\n"),
570 (void) fprintf(stderr
,
571 _("%s: More than one -L option specified\n"),
580 (void) printf("%s: -s ignored\n", progname
);
583 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
584 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
585 if (directory
== NULL
)
587 if (yitcommand
== NULL
)
588 yitcommand
= "yearistype";
592 if (optind
< argc
&& leapsec
!= NULL
) {
597 for (i
= optind
; i
< argc
; ++i
)
602 for (i
= 0; i
< nzones
; i
= j
) {
604 ** Find the next non-continuation zone entry.
606 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
608 outzone(&zones
[i
], j
- i
);
613 for (i
= 0; i
< nlinks
; ++i
) {
614 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
615 dolink(links
[i
].l_from
, links
[i
].l_to
);
617 for (j
= 0; j
< nlinks
; ++j
)
618 if (strcmp(links
[i
].l_to
,
619 links
[j
].l_from
) == 0)
620 warning(_("link to link"));
622 if (lcltime
!= NULL
) {
623 eat("command line", 1);
624 dolink(lcltime
, TZDEFAULT
);
626 if (psxrules
!= NULL
) {
627 eat("command line", 1);
628 dolink(psxrules
, TZDEFRULES
);
630 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
634 dolink(fromfield
, tofield
)
635 const char * const fromfield
;
636 const char * const tofield
;
638 register char * fromname
;
639 register char * toname
;
641 if (fromfield
[0] == '/')
642 fromname
= ecpyalloc(fromfield
);
644 fromname
= ecpyalloc(directory
);
645 fromname
= ecatalloc(fromname
, "/");
646 fromname
= ecatalloc(fromname
, fromfield
);
648 if (tofield
[0] == '/')
649 toname
= ecpyalloc(tofield
);
651 toname
= ecpyalloc(directory
);
652 toname
= ecatalloc(toname
, "/");
653 toname
= ecatalloc(toname
, tofield
);
656 ** We get to be careful here since
657 ** there's a fair chance of root running us.
660 (void) remove(toname
);
661 if (link(fromname
, toname
) != 0) {
664 if (mkdirs(toname
) != 0)
667 result
= link(fromname
, toname
);
670 access(fromname
, F_OK
) == 0 &&
672 const char *s
= tofield
;
673 register char * symlinkcontents
= NULL
;
675 while ((s
= strchr(s
+1, '/')) != NULL
)
677 ecatalloc(symlinkcontents
,
680 ecatalloc(symlinkcontents
,
682 result
= symlink(symlinkcontents
,
685 warning(_("hard link failed, symbolic link used"));
686 ifree(symlinkcontents
);
688 #endif /* HAVE_SYMLINK */
690 const char *e
= strerror(errno
);
692 (void) fprintf(stderr
,
693 _("%s: Can't link from %s to %s: %s\n"),
694 progname
, fromname
, toname
, e
);
702 #define TIME_T_BITS_IN_FILE 64
710 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
712 max_time
= -(min_time
+ 1);
717 const char * const name
;
719 register char * myname
;
722 myname
= ecpyalloc(name
);
723 myname
= ecatalloc(myname
, "/.");
724 accres
= access(myname
, F_OK
);
730 ** Associate sets of rules with zones.
734 ** Sort by rule name.
742 return strcmp(((const struct rule
*) cp1
)->r_name
,
743 ((const struct rule
*) cp2
)->r_name
);
749 register struct zone
* zp
;
750 register struct rule
* rp
;
751 register int base
, out
;
755 (void) qsort((void *) rules
, (size_t) nrules
,
756 (size_t) sizeof *rules
, rcomp
);
757 for (i
= 0; i
< nrules
- 1; ++i
) {
758 if (strcmp(rules
[i
].r_name
,
759 rules
[i
+ 1].r_name
) != 0)
761 if (strcmp(rules
[i
].r_filename
,
762 rules
[i
+ 1].r_filename
) == 0)
764 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
765 warning(_("same rule name in multiple files"));
766 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
767 warning(_("same rule name in multiple files"));
768 for (j
= i
+ 2; j
< nrules
; ++j
) {
769 if (strcmp(rules
[i
].r_name
,
770 rules
[j
].r_name
) != 0)
772 if (strcmp(rules
[i
].r_filename
,
773 rules
[j
].r_filename
) == 0)
775 if (strcmp(rules
[i
+ 1].r_filename
,
776 rules
[j
].r_filename
) == 0)
783 for (i
= 0; i
< nzones
; ++i
) {
788 for (base
= 0; base
< nrules
; base
= out
) {
790 for (out
= base
+ 1; out
< nrules
; ++out
)
791 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
793 for (i
= 0; i
< nzones
; ++i
) {
795 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
798 zp
->z_nrules
= out
- base
;
801 for (i
= 0; i
< nzones
; ++i
) {
803 if (zp
->z_nrules
== 0) {
805 ** Maybe we have a local standard time offset.
807 eat(zp
->z_filename
, zp
->z_linenum
);
808 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
811 ** Note, though, that if there's no rule,
812 ** a '%s' in the format is a bad thing.
814 if (strchr(zp
->z_format
, '%') != 0)
815 error(_("%s in ruleless zone"));
827 register char ** fields
;
829 register const struct lookup
* lp
;
830 register int nfields
;
831 register int wantcont
;
835 if (strcmp(name
, "-") == 0) {
836 name
= _("standard input");
838 } else if ((fp
= fopen(name
, "r")) == NULL
) {
839 const char *e
= strerror(errno
);
841 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
846 for (num
= 1; ; ++num
) {
848 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
850 cp
= strchr(buf
, '\n');
852 error(_("line too long"));
856 fields
= getfields(buf
);
858 while (fields
[nfields
] != NULL
) {
861 if (strcmp(fields
[nfields
], "-") == 0)
862 fields
[nfields
] = &nada
;
867 } else if (wantcont
) {
868 wantcont
= inzcont(fields
, nfields
);
870 lp
= byword(fields
[0], line_codes
);
872 error(_("input line of unknown type"));
873 else switch ((int) (lp
->l_value
)) {
875 inrule(fields
, nfields
);
879 wantcont
= inzone(fields
, nfields
);
882 inlink(fields
, nfields
);
887 (void) fprintf(stderr
,
888 _("%s: Leap line in non leap seconds file %s\n"),
890 else inleap(fields
, nfields
);
893 default: /* "cannot happen" */
894 (void) fprintf(stderr
,
895 _("%s: panic: Invalid l_value %d\n"),
896 progname
, lp
->l_value
);
900 ifree((char *) fields
);
903 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
907 if (fp
!= stdin
&& fclose(fp
)) {
908 const char *e
= strerror(errno
);
910 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
911 progname
, filename
, e
);
915 error(_("expected continuation line not found"));
919 ** Convert a string of one of the forms
920 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
921 ** into a number of seconds.
922 ** A null string maps to zero.
923 ** Call error with errstring and return zero on errors.
927 gethms(string
, errstring
, signable
)
929 const char * const errstring
;
935 if (string
== NULL
|| *string
== '\0')
939 else if (*string
== '-') {
943 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
945 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
947 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
948 &hh
, &mm
, &ss
) != 3) {
953 mm
< 0 || mm
>= MINSPERHOUR
||
954 ss
< 0 || ss
> SECSPERMIN
) {
958 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
959 error(_("time overflow"));
962 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
963 warning(_("24:00 not handled by pre-1998 versions of zic"));
964 if (noise
&& (hh
> HOURSPERDAY
||
965 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
966 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
967 return oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
968 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
)));
972 inrule(fields
, nfields
)
973 register char ** const fields
;
976 static struct rule r
;
978 if (nfields
!= RULE_FIELDS
) {
979 error(_("wrong number of fields on Rule line"));
982 if (*fields
[RF_NAME
] == '\0') {
983 error(_("nameless rule"));
986 r
.r_filename
= filename
;
987 r
.r_linenum
= linenum
;
988 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
989 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
990 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
991 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
992 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
993 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
994 max_abbrvar_len
= strlen(r
.r_abbrvar
);
995 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
996 (int) ((nrules
+ 1) * sizeof *rules
));
1001 inzone(fields
, nfields
)
1002 register char ** const fields
;
1008 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1009 error(_("wrong number of fields on Zone line"));
1012 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1013 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
1014 (void)sprintf(buf
, /* XXX: sprintf is safe */
1015 _("\"Zone %s\" line and -l option are mutually exclusive"),
1020 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1021 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1022 (void)sprintf(buf
, /* XXX: sprintf is safe */
1023 _("\"Zone %s\" line and -p option are mutually exclusive"),
1028 for (i
= 0; i
< nzones
; ++i
)
1029 if (zones
[i
].z_name
!= NULL
&&
1030 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1031 buf
= erealloc(buf
, (int) (132 +
1032 strlen(fields
[ZF_NAME
]) +
1033 strlen(zones
[i
].z_filename
)));
1034 (void)sprintf(buf
, /* XXX: sprintf is safe */
1035 _("duplicate zone name %s (file \"%s\", line %d)"),
1037 zones
[i
].z_filename
,
1038 zones
[i
].z_linenum
);
1042 return inzsub(fields
, nfields
, FALSE
);
1046 inzcont(fields
, nfields
)
1047 register char ** const fields
;
1050 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1051 error(_("wrong number of fields on Zone continuation line"));
1054 return inzsub(fields
, nfields
, TRUE
);
1058 inzsub(fields
, nfields
, iscont
)
1059 register char ** const fields
;
1064 static struct zone z
;
1065 register int i_gmtoff
, i_rule
, i_format
;
1066 register int i_untilyear
, i_untilmonth
;
1067 register int i_untilday
, i_untiltime
;
1068 register int hasuntil
;
1071 i_gmtoff
= ZFC_GMTOFF
;
1073 i_format
= ZFC_FORMAT
;
1074 i_untilyear
= ZFC_TILYEAR
;
1075 i_untilmonth
= ZFC_TILMONTH
;
1076 i_untilday
= ZFC_TILDAY
;
1077 i_untiltime
= ZFC_TILTIME
;
1080 i_gmtoff
= ZF_GMTOFF
;
1082 i_format
= ZF_FORMAT
;
1083 i_untilyear
= ZF_TILYEAR
;
1084 i_untilmonth
= ZF_TILMONTH
;
1085 i_untilday
= ZF_TILDAY
;
1086 i_untiltime
= ZF_TILTIME
;
1087 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1089 z
.z_filename
= filename
;
1090 z
.z_linenum
= linenum
;
1091 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1092 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1093 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1094 error(_("invalid abbreviation format"));
1098 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1099 z
.z_format
= ecpyalloc(fields
[i_format
]);
1100 if (max_format_len
< strlen(z
.z_format
))
1101 max_format_len
= strlen(z
.z_format
);
1102 hasuntil
= nfields
> i_untilyear
;
1104 z
.z_untilrule
.r_filename
= filename
;
1105 z
.z_untilrule
.r_linenum
= linenum
;
1106 rulesub(&z
.z_untilrule
,
1107 fields
[i_untilyear
],
1110 (nfields
> i_untilmonth
) ?
1111 fields
[i_untilmonth
] : "Jan",
1112 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1113 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1114 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1115 z
.z_untilrule
.r_loyear
);
1116 if (iscont
&& nzones
> 0 &&
1117 z
.z_untiltime
> min_time
&&
1118 z
.z_untiltime
< max_time
&&
1119 zones
[nzones
- 1].z_untiltime
> min_time
&&
1120 zones
[nzones
- 1].z_untiltime
< max_time
&&
1121 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1123 "Zone continuation line end time is not after end time of previous line"
1128 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1129 (int) ((nzones
+ 1) * sizeof *zones
));
1130 zones
[nzones
++] = z
;
1132 ** If there was an UNTIL field on this line,
1133 ** there's more information about the zone on the next line.
1139 inleap(fields
, nfields
)
1140 register char ** const fields
;
1143 register const char * cp
;
1144 register const struct lookup
* lp
;
1146 int year
, month
, day
;
1150 if (nfields
!= LEAP_FIELDS
) {
1151 error(_("wrong number of fields on Leap line"));
1155 cp
= fields
[LP_YEAR
];
1156 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1160 error(_("invalid leaping year"));
1163 if (!leapseen
|| leapmaxyear
< year
)
1165 if (!leapseen
|| leapminyear
> year
)
1171 i
= len_years
[isleap(j
)];
1175 i
= -len_years
[isleap(j
)];
1177 dayoff
= oadd(dayoff
, eitol(i
));
1179 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1180 error(_("invalid month name"));
1183 month
= lp
->l_value
;
1185 while (j
!= month
) {
1186 i
= len_months
[isleap(year
)][j
];
1187 dayoff
= oadd(dayoff
, eitol(i
));
1190 cp
= fields
[LP_DAY
];
1191 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1192 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1193 error(_("invalid day of month"));
1196 dayoff
= oadd(dayoff
, eitol(day
- 1));
1197 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1198 error(_("time before zero"));
1201 if (dayoff
< min_time
/ SECSPERDAY
) {
1202 error(_("time too small"));
1205 if (dayoff
> max_time
/ SECSPERDAY
) {
1206 error(_("time too large"));
1209 t
= (zic_t
) dayoff
* SECSPERDAY
;
1210 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1211 cp
= fields
[LP_CORR
];
1213 register int positive
;
1216 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1219 } else if (strcmp(cp
, "--") == 0) {
1222 } else if (strcmp(cp
, "+") == 0) {
1225 } else if (strcmp(cp
, "++") == 0) {
1229 error(_("illegal CORRECTION field on Leap line"));
1232 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1234 "illegal Rolling/Stationary field on Leap line"
1238 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1243 inlink(fields
, nfields
)
1244 register char ** const fields
;
1249 if (nfields
!= LINK_FIELDS
) {
1250 error(_("wrong number of fields on Link line"));
1253 if (*fields
[LF_FROM
] == '\0') {
1254 error(_("blank FROM field on Link line"));
1257 if (*fields
[LF_TO
] == '\0') {
1258 error(_("blank TO field on Link line"));
1261 l
.l_filename
= filename
;
1262 l
.l_linenum
= linenum
;
1263 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1264 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1265 links
= (struct link
*) (void *) erealloc((char *) links
,
1266 (int) ((nlinks
+ 1) * sizeof *links
));
1267 links
[nlinks
++] = l
;
1271 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1272 register struct rule
* const rp
;
1273 const char * const loyearp
;
1274 const char * const hiyearp
;
1275 const char * const typep
;
1276 const char * const monthp
;
1277 const char * const dayp
;
1278 const char * const timep
;
1280 register const struct lookup
* lp
;
1281 register const char * cp
;
1285 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1286 error(_("invalid month name"));
1289 rp
->r_month
= lp
->l_value
;
1290 rp
->r_todisstd
= FALSE
;
1291 rp
->r_todisgmt
= FALSE
;
1292 dp
= ecpyalloc(timep
);
1294 ep
= dp
+ strlen(dp
) - 1;
1295 switch (lowerit(*ep
)) {
1296 case 's': /* Standard */
1297 rp
->r_todisstd
= TRUE
;
1298 rp
->r_todisgmt
= FALSE
;
1301 case 'w': /* Wall */
1302 rp
->r_todisstd
= FALSE
;
1303 rp
->r_todisgmt
= FALSE
;
1306 case 'g': /* Greenwich */
1307 case 'u': /* Universal */
1308 case 'z': /* Zulu */
1309 rp
->r_todisstd
= TRUE
;
1310 rp
->r_todisgmt
= TRUE
;
1315 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1321 lp
= byword(cp
, begin_years
);
1322 rp
->r_lowasnum
= lp
== NULL
;
1323 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1325 rp
->r_loyear
= INT_MIN
;
1328 rp
->r_loyear
= INT_MAX
;
1330 default: /* "cannot happen" */
1331 (void) fprintf(stderr
,
1332 _("%s: panic: Invalid l_value %d\n"),
1333 progname
, lp
->l_value
);
1335 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1336 error(_("invalid starting year"));
1340 lp
= byword(cp
, end_years
);
1341 rp
->r_hiwasnum
= lp
== NULL
;
1342 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1344 rp
->r_hiyear
= INT_MIN
;
1347 rp
->r_hiyear
= INT_MAX
;
1350 rp
->r_hiyear
= rp
->r_loyear
;
1352 default: /* "cannot happen" */
1353 (void) fprintf(stderr
,
1354 _("%s: panic: Invalid l_value %d\n"),
1355 progname
, lp
->l_value
);
1357 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1358 error(_("invalid ending year"));
1361 if (rp
->r_loyear
> rp
->r_hiyear
) {
1362 error(_("starting year greater than ending year"));
1366 rp
->r_yrtype
= NULL
;
1368 if (rp
->r_loyear
== rp
->r_hiyear
) {
1369 error(_("typed single year"));
1372 rp
->r_yrtype
= ecpyalloc(typep
);
1376 ** Accept things such as:
1382 dp
= ecpyalloc(dayp
);
1383 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1384 rp
->r_dycode
= DC_DOWLEQ
;
1385 rp
->r_wday
= lp
->l_value
;
1386 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1388 if ((ep
= strchr(dp
, '<')) != 0)
1389 rp
->r_dycode
= DC_DOWLEQ
;
1390 else if ((ep
= strchr(dp
, '>')) != 0)
1391 rp
->r_dycode
= DC_DOWGEQ
;
1394 rp
->r_dycode
= DC_DOM
;
1396 if (rp
->r_dycode
!= DC_DOM
) {
1399 error(_("invalid day of month"));
1403 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1404 error(_("invalid weekday name"));
1408 rp
->r_wday
= lp
->l_value
;
1410 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1411 rp
->r_dayofmonth
<= 0 ||
1412 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1413 error(_("invalid day of month"));
1429 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1430 buf
[i
] = val
>> shift
;
1441 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1442 buf
[i
] = val
>> shift
;
1453 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1457 puttzcode64(val
, fp
)
1463 convert64(val
, buf
);
1464 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1472 const zic_t a
= ((const struct attype
*) avp
)->at
;
1473 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1475 return (a
< b
) ? -1 : (a
> b
);
1482 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1486 writezone(name
, string
)
1487 const char * const name
;
1488 const char * const string
;
1492 register int leapcnt32
, leapi32
;
1493 register int timecnt32
, timei32
;
1495 static char * fullname
;
1496 static const struct tzhead tzh0
;
1497 static struct tzhead tzh
;
1498 zic_t ats
[TZ_MAX_TIMES
];
1499 unsigned char types
[TZ_MAX_TIMES
];
1505 (void) qsort((void *) attypes
, (size_t) timecnt
,
1506 (size_t) sizeof *attypes
, atcomp
);
1516 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1519 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1520 ++fromi
; /* handled by default rule */
1521 for ( ; fromi
< timecnt
; ++fromi
) {
1522 if (toi
!= 0 && ((attypes
[fromi
].at
+
1523 gmtoffs
[attypes
[toi
- 1].type
]) <=
1524 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1525 : attypes
[toi
- 2].type
]))) {
1526 attypes
[toi
- 1].type
=
1527 attypes
[fromi
].type
;
1531 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1532 attypes
[toi
++] = attypes
[fromi
];
1539 for (i
= 0; i
< timecnt
; ++i
) {
1540 ats
[i
] = attypes
[i
].at
;
1541 types
[i
] = attypes
[i
].type
;
1544 ** Correct for leap seconds.
1546 for (i
= 0; i
< timecnt
; ++i
) {
1549 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1550 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1555 ** Figure out 32-bit-limited starts and counts.
1557 timecnt32
= timecnt
;
1559 leapcnt32
= leapcnt
;
1561 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1563 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1567 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1569 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1573 fullname
= erealloc(fullname
,
1574 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1575 (void) sprintf(fullname
, "%s/%s", directory
, name
); /* XXX: sprintf is safe */
1577 ** Remove old file, if any, to snap links.
1579 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1580 const char *e
= strerror(errno
);
1582 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1583 progname
, fullname
, e
);
1586 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1587 if (mkdirs(fullname
) != 0)
1589 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1590 const char *e
= strerror(errno
);
1592 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1593 progname
, fullname
, e
);
1597 for (pass
= 1; pass
<= 2; ++pass
) {
1598 register int thistimei
, thistimecnt
;
1599 register int thisleapi
, thisleapcnt
;
1600 register int thistimelim
, thisleaplim
;
1601 int writetype
[TZ_MAX_TIMES
];
1602 int typemap
[TZ_MAX_TYPES
];
1603 register int thistypecnt
;
1604 char thischars
[TZ_MAX_CHARS
];
1606 int indmap
[TZ_MAX_CHARS
];
1609 thistimei
= timei32
;
1610 thistimecnt
= timecnt32
;
1611 thisleapi
= leapi32
;
1612 thisleapcnt
= leapcnt32
;
1615 thistimecnt
= timecnt
;
1617 thisleapcnt
= leapcnt
;
1619 thistimelim
= thistimei
+ thistimecnt
;
1620 thisleaplim
= thisleapi
+ thisleapcnt
;
1621 for (i
= 0; i
< typecnt
; ++i
)
1622 writetype
[i
] = thistimecnt
== timecnt
;
1623 if (thistimecnt
== 0) {
1625 ** No transition times fall in the current
1626 ** (32- or 64-bit) window.
1629 writetype
[typecnt
- 1] = TRUE
;
1631 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1633 writetype
[types
[i
]] = TRUE
;
1635 ** For America/Godthab and Antarctica/Palmer
1638 writetype
[0] = TRUE
;
1641 for (i
= 0; i
< typecnt
; ++i
)
1642 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : -1;
1643 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1646 for (i
= 0; i
< typecnt
; ++i
) {
1647 register char * thisabbr
;
1651 if (indmap
[abbrinds
[i
]] >= 0)
1653 thisabbr
= &chars
[abbrinds
[i
]];
1654 for (j
= 0; j
< thischarcnt
; ++j
)
1655 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1657 if (j
== thischarcnt
) {
1658 (void) strcpy(&thischars
[(int) thischarcnt
],
1660 thischarcnt
+= strlen(thisabbr
) + 1;
1662 indmap
[abbrinds
[i
]] = j
;
1664 #define DO(field) (void) fwrite((void *) tzh.field, \
1665 (size_t) sizeof tzh.field, (size_t) 1, fp)
1667 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1668 tzh
.tzh_version
[0] = ZIC_VERSION
;
1669 convert(eitol(thistypecnt
), tzh
.tzh_ttisgmtcnt
);
1670 convert(eitol(thistypecnt
), tzh
.tzh_ttisstdcnt
);
1671 convert(eitol(thisleapcnt
), tzh
.tzh_leapcnt
);
1672 convert(eitol(thistimecnt
), tzh
.tzh_timecnt
);
1673 convert(eitol(thistypecnt
), tzh
.tzh_typecnt
);
1674 convert(eitol(thischarcnt
), tzh
.tzh_charcnt
);
1685 for (i
= thistimei
; i
< thistimelim
; ++i
)
1687 puttzcode((long) ats
[i
], fp
);
1688 else puttzcode64(ats
[i
], fp
);
1689 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1692 uc
= typemap
[types
[i
]];
1693 (void) fwrite((void *) &uc
,
1698 for (i
= 0; i
< typecnt
; ++i
)
1700 puttzcode(gmtoffs
[i
], fp
);
1701 (void) putc(isdsts
[i
], fp
);
1702 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1704 if (thischarcnt
!= 0)
1705 (void) fwrite((void *) thischars
,
1706 (size_t) sizeof thischars
[0],
1707 (size_t) thischarcnt
, fp
);
1708 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1709 register zic_t todo
;
1712 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1715 if (++j
>= typecnt
) {
1721 while (j
< timecnt
&&
1726 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1727 } else todo
= trans
[i
];
1729 puttzcode((long) todo
, fp
);
1730 else puttzcode64(todo
, fp
);
1731 puttzcode(corr
[i
], fp
);
1733 for (i
= 0; i
< typecnt
; ++i
)
1735 (void) putc(ttisstds
[i
], fp
);
1736 for (i
= 0; i
< typecnt
; ++i
)
1738 (void) putc(ttisgmts
[i
], fp
);
1740 (void) fprintf(fp
, "\n%s\n", string
);
1741 if (ferror(fp
) || fclose(fp
)) {
1742 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1743 progname
, fullname
);
1749 doabbr(abbr
, abbrlen
, format
, letters
, isdst
, doquotes
)
1752 const char * const format
;
1753 const char * const letters
;
1758 register char * slashp
;
1761 slashp
= strchr(format
, '/');
1762 if (slashp
== NULL
) {
1763 if (letters
== NULL
)
1764 (void) strlcpy(abbr
, format
, abbrlen
);
1765 else (void) snprintf(abbr
, abbrlen
, format
, letters
);
1767 (void) strlcpy(abbr
, slashp
+ 1, abbrlen
);
1769 if (slashp
> format
)
1770 (void) strncpy(abbr
, format
,
1771 (unsigned) (slashp
- format
));
1772 abbr
[slashp
- format
] = '\0';
1776 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1777 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1778 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1781 if (len
> 0 && *cp
== '\0')
1783 abbr
[len
+ 2] = '\0';
1784 abbr
[len
+ 1] = '>';
1785 for ( ; len
> 0; --len
)
1786 abbr
[len
] = abbr
[len
- 1];
1801 stringoffset(result
, offset
)
1806 register int minutes
;
1807 register int seconds
;
1811 (void) strcpy(result
, "-");
1814 seconds
= offset
% SECSPERMIN
;
1815 offset
/= SECSPERMIN
;
1816 minutes
= offset
% MINSPERHOUR
;
1817 offset
/= MINSPERHOUR
;
1819 if (hours
>= HOURSPERDAY
) {
1823 (void) sprintf(end(result
), "%d", hours
);
1824 if (minutes
!= 0 || seconds
!= 0) {
1825 (void) sprintf(end(result
), ":%02d", minutes
);
1827 (void) sprintf(end(result
), ":%02d", seconds
);
1833 stringrule(result
, rp
, dstoff
, gmtoff
)
1835 const struct rule
* const rp
;
1841 result
= end(result
);
1842 if (rp
->r_dycode
== DC_DOM
) {
1843 register int month
, total
;
1845 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1848 for (month
= 0; month
< rp
->r_month
; ++month
)
1849 total
+= len_months
[0][month
];
1850 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1854 if (rp
->r_dycode
== DC_DOWGEQ
) {
1855 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1856 if ((week
- 1) * DAYSPERWEEK
+ 1 != rp
->r_dayofmonth
)
1858 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1859 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1862 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1863 if (week
* DAYSPERWEEK
- 1 != rp
->r_dayofmonth
)
1866 } else return -1; /* "cannot happen" */
1867 (void) sprintf(result
, "M%d.%d.%d",
1868 rp
->r_month
+ 1, week
, rp
->r_wday
);
1873 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1879 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1880 (void) strcat(result
, "/");
1881 if (stringoffset(end(result
), tod
) != 0)
1888 stringzone(result
, resultlen
, zpfirst
, zonecount
)
1890 const int resultlen
;
1891 const struct zone
* const zpfirst
;
1892 const int zonecount
;
1894 register const struct zone
* zp
;
1895 register struct rule
* rp
;
1896 register struct rule
* stdrp
;
1897 register struct rule
* dstrp
;
1899 register const char * abbrvar
;
1902 zp
= zpfirst
+ zonecount
- 1;
1903 stdrp
= dstrp
= NULL
;
1904 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1905 rp
= &zp
->z_rules
[i
];
1906 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= INT_MAX
)
1908 if (rp
->r_yrtype
!= NULL
)
1910 if (rp
->r_stdoff
== 0) {
1920 if (stdrp
== NULL
&& dstrp
== NULL
) {
1922 ** There are no rules running through "max".
1923 ** Let's find the latest rule.
1925 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1926 rp
= &zp
->z_rules
[i
];
1927 if (stdrp
== NULL
|| rp
->r_hiyear
> stdrp
->r_hiyear
||
1928 (rp
->r_hiyear
== stdrp
->r_hiyear
&&
1929 rp
->r_month
> stdrp
->r_month
))
1932 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0)
1933 return; /* We end up in DST (a POSIX no-no). */
1935 ** Horrid special case: if year is 2037,
1936 ** presume this is a zone handled on a year-by-year basis;
1937 ** do not try to apply a rule to the zone.
1939 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1942 if (stdrp
== NULL
&& (zp
->z_nrules
!= 0 || zp
->z_stdoff
!= 0))
1944 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1945 doabbr(result
, resultlen
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1946 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
1952 doabbr(end(result
), resultlen
- strlen(result
),
1953 zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
1954 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
1955 if (stringoffset(end(result
),
1956 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
1960 (void) strcat(result
, ",");
1961 if (stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1965 (void) strcat(result
, ",");
1966 if (stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1973 outzone(zpfirst
, zonecount
)
1974 const struct zone
* const zpfirst
;
1975 const int zonecount
;
1977 register const struct zone
* zp
;
1978 register struct rule
* rp
;
1980 register int usestart
, useuntil
;
1981 register zic_t starttime
, untiltime
;
1982 register long gmtoff
;
1983 register long stdoff
;
1985 register long startoff
;
1986 register int startttisstd
;
1987 register int startttisgmt
;
1989 register char * startbuf
;
1991 register char * envvar
;
1992 register int max_abbr_len
;
1993 register int max_envvar_len
;
1995 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
1996 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
1997 startbuf
= emalloc(max_abbr_len
+ 1);
1998 ab
= emalloc(max_abbr_len
+ 1);
1999 envvar
= emalloc(max_envvar_len
+ 1);
2000 INITIALIZE(untiltime
);
2001 INITIALIZE(starttime
);
2003 ** Now. . .finally. . .generate some useful data!
2009 ** Thanks to Earl Chew
2010 ** for noting the need to unconditionally initialize startttisstd.
2012 startttisstd
= FALSE
;
2013 startttisgmt
= FALSE
;
2014 min_year
= max_year
= EPOCH_YEAR
;
2016 updateminmax(leapminyear
);
2017 updateminmax(leapmaxyear
+ (leapmaxyear
< INT_MAX
));
2019 for (i
= 0; i
< zonecount
; ++i
) {
2021 if (i
< zonecount
- 1)
2022 updateminmax(zp
->z_untilrule
.r_loyear
);
2023 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2024 rp
= &zp
->z_rules
[j
];
2026 updateminmax(rp
->r_loyear
);
2028 updateminmax(rp
->r_hiyear
);
2032 ** Generate lots of data if a rule can't cover all future times.
2034 stringzone(envvar
, max_envvar_len
+1, zpfirst
, zonecount
);
2035 if (noise
&& envvar
[0] == '\0') {
2038 wp
= ecpyalloc(_("no POSIX environment variable for zone"));
2039 wp
= ecatalloc(wp
, " ");
2040 wp
= ecatalloc(wp
, zpfirst
->z_name
);
2044 if (envvar
[0] == '\0') {
2045 if (min_year
>= INT_MIN
+ YEARSPERREPEAT
)
2046 min_year
-= YEARSPERREPEAT
;
2047 else min_year
= INT_MIN
;
2048 if (max_year
<= INT_MAX
- YEARSPERREPEAT
)
2049 max_year
+= YEARSPERREPEAT
;
2050 else max_year
= INT_MAX
;
2053 ** For the benefit of older systems,
2054 ** generate data from 1900 through 2037.
2056 if (min_year
> 1900)
2058 if (max_year
< 2037)
2060 for (i
= 0; i
< zonecount
; ++i
) {
2062 ** A guess that may well be corrected later.
2066 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2067 useuntil
= i
< (zonecount
- 1);
2068 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2070 gmtoff
= zp
->z_gmtoff
;
2071 eat(zp
->z_filename
, zp
->z_linenum
);
2073 startoff
= zp
->z_gmtoff
;
2074 if (zp
->z_nrules
== 0) {
2075 stdoff
= zp
->z_stdoff
;
2076 doabbr(startbuf
, max_abbr_len
+ 1, zp
->z_format
,
2077 (char *) NULL
, stdoff
!= 0, FALSE
);
2078 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2079 startbuf
, stdoff
!= 0, startttisstd
,
2082 addtt(starttime
, type
);
2084 } else if (stdoff
!= 0)
2085 addtt(min_time
, type
);
2086 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2087 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2090 ** Mark which rules to do in the current year.
2091 ** For those to do, calculate rpytime(rp, year);
2093 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2094 rp
= &zp
->z_rules
[j
];
2095 eats(zp
->z_filename
, zp
->z_linenum
,
2096 rp
->r_filename
, rp
->r_linenum
);
2097 rp
->r_todo
= year
>= rp
->r_loyear
&&
2098 year
<= rp
->r_hiyear
&&
2099 yearistype(year
, rp
->r_yrtype
);
2101 rp
->r_temp
= rpytime(rp
, year
);
2105 register zic_t jtime
, ktime
;
2106 register long offset
;
2111 ** Turn untiltime into UTC
2112 ** assuming the current gmtoff and
2115 untiltime
= zp
->z_untiltime
;
2116 if (!zp
->z_untilrule
.r_todisgmt
)
2117 untiltime
= tadd(untiltime
,
2119 if (!zp
->z_untilrule
.r_todisstd
)
2120 untiltime
= tadd(untiltime
,
2124 ** Find the rule (of those to do, if any)
2125 ** that takes effect earliest in the year.
2128 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2129 rp
= &zp
->z_rules
[j
];
2132 eats(zp
->z_filename
, zp
->z_linenum
,
2133 rp
->r_filename
, rp
->r_linenum
);
2134 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2135 if (!rp
->r_todisstd
)
2136 offset
= oadd(offset
, stdoff
);
2138 if (jtime
== min_time
||
2141 jtime
= tadd(jtime
, -offset
);
2142 if (k
< 0 || jtime
< ktime
) {
2148 break; /* go on to next year */
2149 rp
= &zp
->z_rules
[k
];
2151 if (useuntil
&& ktime
>= untiltime
)
2153 stdoff
= rp
->r_stdoff
;
2154 if (usestart
&& ktime
== starttime
)
2157 if (ktime
< starttime
) {
2158 startoff
= oadd(zp
->z_gmtoff
,
2168 if (*startbuf
== '\0' &&
2169 startoff
== oadd(zp
->z_gmtoff
,
2180 eats(zp
->z_filename
, zp
->z_linenum
,
2181 rp
->r_filename
, rp
->r_linenum
);
2182 doabbr(ab
, max_abbr_len
+1, zp
->z_format
, rp
->r_abbrvar
,
2183 rp
->r_stdoff
!= 0, FALSE
);
2184 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2185 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2186 rp
->r_todisstd
, rp
->r_todisgmt
);
2191 if (*startbuf
== '\0' &&
2192 zp
->z_format
!= NULL
&&
2193 strchr(zp
->z_format
, '%') == NULL
&&
2194 strchr(zp
->z_format
, '/') == NULL
)
2195 (void)strncpy(startbuf
, zp
->z_format
,
2196 max_abbr_len
+ 1 - 1);
2197 eat(zp
->z_filename
, zp
->z_linenum
);
2198 if (*startbuf
== '\0')
2199 error(_("can't determine time zone abbreviation to use just after until time"));
2200 else addtt(starttime
,
2201 addtype(startoff
, startbuf
,
2202 startoff
!= zp
->z_gmtoff
,
2207 ** Now we may get to set starttime for the next zone line.
2210 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2211 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2212 starttime
= zp
->z_untiltime
;
2214 starttime
= tadd(starttime
, -stdoff
);
2216 starttime
= tadd(starttime
, -gmtoff
);
2219 writezone(zpfirst
->z_name
, envvar
);
2226 addtt(starttime
, type
)
2227 const zic_t starttime
;
2230 if (starttime
<= min_time
||
2231 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2232 gmtoffs
[0] = gmtoffs
[type
];
2233 isdsts
[0] = isdsts
[type
];
2234 ttisstds
[0] = ttisstds
[type
];
2235 ttisgmts
[0] = ttisgmts
[type
];
2236 if (abbrinds
[type
] != 0)
2237 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2239 charcnt
= strlen(chars
) + 1;
2244 if (timecnt
>= TZ_MAX_TIMES
) {
2245 error(_("too many transitions?!"));
2248 attypes
[timecnt
].at
= starttime
;
2249 attypes
[timecnt
].type
= type
;
2254 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
2256 const char * const abbr
;
2263 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2264 error(_("internal error - addtype called with bad isdst"));
2267 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2268 error(_("internal error - addtype called with bad ttisstd"));
2271 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2272 error(_("internal error - addtype called with bad ttisgmt"));
2276 ** See if there's already an entry for this zone type.
2277 ** If so, just return its index.
2279 for (i
= 0; i
< typecnt
; ++i
) {
2280 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2281 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2282 ttisstd
== ttisstds
[i
] &&
2283 ttisgmt
== ttisgmts
[i
])
2287 ** There isn't one; add a new one, unless there are already too
2290 if (typecnt
>= TZ_MAX_TYPES
) {
2291 error(_("too many local time types"));
2294 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2295 error(_("UTC offset out of range"));
2298 gmtoffs
[i
] = gmtoff
;
2300 ttisstds
[i
] = ttisstd
;
2301 ttisgmts
[i
] = ttisgmt
;
2303 for (j
= 0; j
< charcnt
; ++j
)
2304 if (strcmp(&chars
[j
], abbr
) == 0)
2314 leapadd(t
, positive
, rolling
, count
)
2322 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2323 error(_("too many leap seconds"));
2326 for (i
= 0; i
< leapcnt
; ++i
)
2327 if (t
<= trans
[i
]) {
2328 if (t
== trans
[i
]) {
2329 error(_("repeated leap second moment"));
2335 for (j
= leapcnt
; j
> i
; --j
) {
2336 trans
[j
] = trans
[j
- 1];
2337 corr
[j
] = corr
[j
- 1];
2338 roll
[j
] = roll
[j
- 1];
2341 corr
[i
] = positive
? 1L : eitol(-count
);
2344 } while (positive
&& --count
!= 0);
2351 register long last
= 0;
2354 ** propagate leap seconds forward
2356 for (i
= 0; i
< leapcnt
; ++i
) {
2357 trans
[i
] = tadd(trans
[i
], last
);
2358 last
= corr
[i
] += last
;
2363 yearistype(year
, type
)
2365 const char * const type
;
2370 if (type
== NULL
|| *type
== '\0')
2372 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
2373 (void)sprintf(buf
, "%s %d %s", yitcommand
, year
, type
); /* XXX: sprintf is safe */
2374 result
= system(buf
);
2375 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2381 error(_("Wild result from command execution"));
2382 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2383 progname
, buf
, result
);
2392 a
= (unsigned char) a
;
2393 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2397 ciequal(ap
, bp
) /* case-insensitive equality */
2398 register const char * ap
;
2399 register const char * bp
;
2401 while (lowerit(*ap
) == lowerit(*bp
++))
2409 register const char * abbr
;
2410 register const char * word
;
2412 if (lowerit(*abbr
) != lowerit(*word
))
2415 while (*++abbr
!= '\0')
2419 } while (lowerit(*word
++) != lowerit(*abbr
));
2423 static const struct lookup
*
2425 register const char * const word
;
2426 register const struct lookup
* const table
;
2428 register const struct lookup
* foundlp
;
2429 register const struct lookup
* lp
;
2431 if (word
== NULL
|| table
== NULL
)
2434 ** Look for exact match.
2436 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2437 if (ciequal(word
, lp
->l_word
))
2440 ** Look for inexact match.
2443 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2444 if (itsabbr(word
, lp
->l_word
)) {
2445 if (foundlp
== NULL
)
2447 else return NULL
; /* multiple inexact matches */
2457 register char ** array
;
2462 array
= (char **) (void *)
2463 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2466 while (isascii((unsigned char) *cp
) &&
2467 isspace((unsigned char) *cp
))
2469 if (*cp
== '\0' || *cp
== '#')
2471 array
[nsubs
++] = dp
= cp
;
2473 if ((*dp
= *cp
++) != '"')
2475 else while ((*dp
= *cp
++) != '"')
2480 "Odd number of quotation marks"
2484 } while (*cp
!= '\0' && *cp
!= '#' &&
2485 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2486 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2490 array
[nsubs
] = NULL
;
2502 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2503 error(_("time overflow"));
2516 if (t1
== max_time
&& t2
> 0)
2518 if (t1
== min_time
&& t2
< 0)
2521 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2522 error(_("time overflow"));
2529 ** Given a rule, and a year, compute the date - in seconds since January 1,
2530 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2534 rpytime(rp
, wantedy
)
2535 register const struct rule
* const rp
;
2536 register const int wantedy
;
2538 register int y
, m
, i
;
2539 register long dayoff
; /* with a nod to Margaret O. */
2542 if (wantedy
== INT_MIN
)
2544 if (wantedy
== INT_MAX
)
2549 while (wantedy
!= y
) {
2551 i
= len_years
[isleap(y
)];
2555 i
= -len_years
[isleap(y
)];
2557 dayoff
= oadd(dayoff
, eitol(i
));
2559 while (m
!= rp
->r_month
) {
2560 i
= len_months
[isleap(y
)][m
];
2561 dayoff
= oadd(dayoff
, eitol(i
));
2564 i
= rp
->r_dayofmonth
;
2565 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2566 if (rp
->r_dycode
== DC_DOWLEQ
)
2569 error(_("use of 2/29 in non leap-year"));
2574 dayoff
= oadd(dayoff
, eitol(i
));
2575 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2578 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2579 wday
= eitol(EPOCH_WDAY
);
2581 ** Don't trust mod of negative numbers.
2584 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2586 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2588 wday
+= LDAYSPERWEEK
;
2590 while (wday
!= eitol(rp
->r_wday
))
2591 if (rp
->r_dycode
== DC_DOWGEQ
) {
2592 dayoff
= oadd(dayoff
, (long) 1);
2593 if (++wday
>= LDAYSPERWEEK
)
2597 dayoff
= oadd(dayoff
, (long) -1);
2599 wday
= LDAYSPERWEEK
- 1;
2602 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2604 warning(_("rule goes past start/end of month--\
2605 will not work with pre-2004 versions of zic"));
2608 if (dayoff
< min_time
/ SECSPERDAY
)
2610 if (dayoff
> max_time
/ SECSPERDAY
)
2612 t
= (zic_t
) dayoff
* SECSPERDAY
;
2613 return tadd(t
, rp
->r_tod
);
2618 const char * const string
;
2622 if (strcmp(string
, GRANDPARENTED
) != 0) {
2623 register const char * cp
;
2627 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2628 ** optionally followed by a + or - and a number from 1 to 14.
2632 while (isascii((unsigned char) *cp
) &&
2633 isalpha((unsigned char) *cp
))
2635 if (cp
- string
== 0)
2636 wp
= _("time zone abbreviation lacks alphabetic at start");
2637 if (noise
&& cp
- string
> 3)
2638 wp
= _("time zone abbreviation has more than 3 alphabetics");
2639 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2640 wp
= _("time zone abbreviation has too many alphabetics");
2641 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2643 if (isascii((unsigned char) *cp
) &&
2644 isdigit((unsigned char) *cp
))
2646 *cp
>= '0' && *cp
<= '4')
2650 wp
= _("time zone abbreviation differs from POSIX standard");
2653 wp
= ecatalloc(wp
, " (");
2654 wp
= ecatalloc(wp
, string
);
2655 wp
= ecatalloc(wp
, ")");
2660 i
= strlen(string
) + 1;
2661 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2662 error(_("too many, or too long, time zone abbreviations"));
2665 (void)strncpy(&chars
[charcnt
], string
, sizeof(chars
) - charcnt
- 1);
2666 charcnt
+= eitol(i
);
2673 register char * name
;
2676 if (argname
== NULL
|| *argname
== '\0')
2678 cp
= name
= ecpyalloc(argname
);
2679 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2683 ** DOS drive specifier?
2685 if (isalpha((unsigned char) name
[0]) &&
2686 name
[1] == ':' && name
[2] == '\0') {
2690 #endif /* !defined __NetBSD__ */
2691 if (!itsdir(name
)) {
2693 ** It doesn't seem to exist, so we try to create it.
2694 ** Creation may fail because of the directory being
2695 ** created by some other multiprocessor, so we get
2696 ** to do extra checking.
2698 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2699 const char *e
= strerror(errno
);
2701 if (errno
!= EEXIST
|| !itsdir(name
)) {
2702 (void) fprintf(stderr
,
2703 _("%s: Can't create directory %s: %s\n"),
2723 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2724 (void) fprintf(stderr
,
2725 _("%s: %d did not sign extend correctly\n"),
2733 ** UNIX was a registered trademark of The Open Group in 2003.