1 /* $NetBSD: zic.c,v 1.30 2012/08/09 12:38:26 christos 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.30 2012/08/09 12:38:26 christos Exp $");
14 #endif /* !defined lint */
21 #define ZIC_VERSION '2'
23 typedef int_fast64_t zic_t
;
25 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
26 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
27 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
33 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
35 #define MKDIR_UMASK 0755
41 ** On some ancient hosts, predicates like `isspace(C)' are defined
42 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
43 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
44 ** Neither the C Standard nor Posix require that `isascii' exist.
45 ** For portability, we check both ancient and modern requirements.
46 ** If isascii is not defined, the isascii check succeeds trivially.
53 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
54 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
56 #define end(cp) (strchr((cp), '\0'))
59 const char * r_filename
;
63 int r_loyear
; /* for example, 1986 */
64 int r_hiyear
; /* for example, 1986 */
65 const char * r_yrtype
;
69 int r_month
; /* 0..11 */
71 int r_dycode
; /* see below */
75 long r_tod
; /* time from midnight */
76 int r_todisstd
; /* above is standard time if TRUE */
77 /* or wall clock time if FALSE */
78 int r_todisgmt
; /* above is GMT if TRUE */
79 /* or local time if FALSE */
80 long r_stdoff
; /* offset from standard time */
81 const char * r_abbrvar
; /* variable part of abbreviation */
83 int r_todo
; /* a rule to do (used in outzone) */
84 zic_t r_temp
; /* used in outzone */
88 ** r_dycode r_dayofmonth r_wday
91 #define DC_DOM 0 /* 1..31 */ /* unused */
92 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
93 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
96 const char * z_filename
;
102 const char * z_format
;
106 struct rule
* z_rules
;
109 struct rule z_untilrule
;
113 extern int getopt(int argc
, char * const argv
[],
114 const char * options
);
115 extern int link(const char * fromname
, const char * toname
);
116 extern char * optarg
;
119 static void addtt(zic_t starttime
, int type
);
120 static int addtype(long gmtoff
, const char * abbr
, int isdst
,
121 int ttisstd
, int ttisgmt
);
122 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
123 static void adjleap(void);
124 static void associate(void);
125 static int ciequal(const char * ap
, const char * bp
);
126 static void convert(long val
, char * buf
);
127 static void convert64(zic_t val
, char * buf
);
128 static void dolink(const char * fromfield
, const char * tofield
);
129 static void doabbr(char * abbr
, const int, const char * format
,
130 const char * letters
, int isdst
, int doquotes
);
131 static void eat(const char * name
, int num
);
132 static void eats(const char * name
, int num
,
133 const char * rname
, int rnum
);
134 static long eitol(int i
);
135 static void error(const char * message
);
136 static char ** getfields(char * buf
);
137 static long gethms(const char * string
, const char * errstrng
,
139 static void infile(const char * filename
);
140 static void inleap(char ** fields
, int nfields
);
141 static void inlink(char ** fields
, int nfields
);
142 static void inrule(char ** fields
, int nfields
);
143 static int inzcont(char ** fields
, int nfields
);
144 static int inzone(char ** fields
, int nfields
);
145 static int inzsub(char ** fields
, int nfields
, int iscont
);
146 static int is32(zic_t x
);
147 static int itsabbr(const char * abbr
, const char * word
);
148 static int itsdir(const char * name
);
149 static int lowerit(int c
);
150 int main(int, char **);
151 static char * memcheck(char * tocheck
);
152 static int mkdirs(char * filename
);
153 static void newabbr(const char * abbr
);
154 static long oadd(long t1
, long t2
);
155 static void outzone(const struct zone
* zp
, int ntzones
);
156 static void puttzcode(long code
, FILE * fp
);
157 static void puttzcode64(zic_t code
, FILE * fp
);
158 static int rcomp(const void * leftp
, const void * rightp
);
159 static zic_t
rpytime(const struct rule
* rp
, int wantedy
);
160 static void rulesub(struct rule
* rp
,
161 const char * loyearp
, const char * hiyearp
,
162 const char * typep
, const char * monthp
,
163 const char * dayp
, const char * timep
);
164 static int stringoffset(char * result
, long offset
);
165 static int stringrule(char * result
, const struct rule
* rp
,
166 long dstoff
, long gmtoff
);
167 static void stringzone(char * result
, const int,
168 const struct zone
* zp
, int ntzones
);
169 static void setboundaries(void);
170 static zic_t
tadd(zic_t t1
, long t2
);
171 static void usage(FILE *stream
, int status
);
172 static void warning(const char * const);
173 static void writezone(const char * name
, const char * string
);
174 static int yearistype(int year
, const char * type
);
175 static int atcomp(const void *avp
, const void *bvp
);
176 static void updateminmax(int x
);
180 static const char * filename
;
183 static int leapminyear
;
184 static int leapmaxyear
;
186 static int max_abbrvar_len
;
187 static int max_format_len
;
188 static zic_t max_time
;
190 static zic_t min_time
;
193 static const char * rfilename
;
195 static const char * progname
;
209 ** Which fields are which on a Zone line.
217 #define ZF_TILMONTH 6
220 #define ZONE_MINFIELDS 5
221 #define ZONE_MAXFIELDS 9
224 ** Which fields are which on a Zone continuation line.
230 #define ZFC_TILYEAR 3
231 #define ZFC_TILMONTH 4
233 #define ZFC_TILTIME 6
234 #define ZONEC_MINFIELDS 3
235 #define ZONEC_MAXFIELDS 7
238 ** Which files are which on a Rule line.
250 #define RULE_FIELDS 10
253 ** Which fields are which on a Link line.
258 #define LINK_FIELDS 3
261 ** Which fields are which on a Leap line.
270 #define LEAP_FIELDS 7
280 static struct rule
* rules
;
281 static int nrules
; /* number of rules */
283 static struct zone
* zones
;
284 static int nzones
; /* number of zones */
287 const char * l_filename
;
293 static struct link
* links
;
301 static struct lookup
const * byword(const char * string
,
302 const struct lookup
* lp
);
304 static struct lookup
const line_codes
[] = {
312 static struct lookup
const mon_names
[] = {
313 { "January", TM_JANUARY
},
314 { "February", TM_FEBRUARY
},
315 { "March", TM_MARCH
},
316 { "April", TM_APRIL
},
320 { "August", TM_AUGUST
},
321 { "September", TM_SEPTEMBER
},
322 { "October", TM_OCTOBER
},
323 { "November", TM_NOVEMBER
},
324 { "December", TM_DECEMBER
},
328 static struct lookup
const wday_names
[] = {
329 { "Sunday", TM_SUNDAY
},
330 { "Monday", TM_MONDAY
},
331 { "Tuesday", TM_TUESDAY
},
332 { "Wednesday", TM_WEDNESDAY
},
333 { "Thursday", TM_THURSDAY
},
334 { "Friday", TM_FRIDAY
},
335 { "Saturday", TM_SATURDAY
},
339 static struct lookup
const lasts
[] = {
340 { "last-Sunday", TM_SUNDAY
},
341 { "last-Monday", TM_MONDAY
},
342 { "last-Tuesday", TM_TUESDAY
},
343 { "last-Wednesday", TM_WEDNESDAY
},
344 { "last-Thursday", TM_THURSDAY
},
345 { "last-Friday", TM_FRIDAY
},
346 { "last-Saturday", TM_SATURDAY
},
350 static struct lookup
const begin_years
[] = {
351 { "minimum", YR_MINIMUM
},
352 { "maximum", YR_MAXIMUM
},
356 static struct lookup
const end_years
[] = {
357 { "minimum", YR_MINIMUM
},
358 { "maximum", YR_MAXIMUM
},
363 static struct lookup
const leap_types
[] = {
365 { "Stationary", FALSE
},
369 static const int len_months
[2][MONSPERYEAR
] = {
370 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
371 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
374 static const int len_years
[2] = {
375 DAYSPERNYEAR
, DAYSPERLYEAR
378 static struct attype
{
381 } attypes
[TZ_MAX_TIMES
];
382 static long gmtoffs
[TZ_MAX_TYPES
];
383 static char isdsts
[TZ_MAX_TYPES
];
384 static unsigned char abbrinds
[TZ_MAX_TYPES
];
385 static char ttisstds
[TZ_MAX_TYPES
];
386 static char ttisgmts
[TZ_MAX_TYPES
];
387 static char chars
[TZ_MAX_CHARS
];
388 static zic_t trans
[TZ_MAX_LEAPS
];
389 static long corr
[TZ_MAX_LEAPS
];
390 static char roll
[TZ_MAX_LEAPS
];
393 ** Memory allocation.
401 const char *e
= strerror(errno
);
403 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
410 #define emalloc(size) memcheck(imalloc(size))
411 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
412 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
413 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
420 eats(name
, num
, rname
, rnum
)
421 const char * const name
;
423 const char * const rname
;
434 const char * const name
;
437 eats(name
, num
, (char *) NULL
, -1);
442 const char * const string
;
445 ** Match the format of "cc" to allow sh users to
446 ** zic ... 2>&1 | error -t "*" -v
449 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
450 filename
, linenum
, string
);
451 if (rfilename
!= NULL
)
452 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
453 rfilename
, rlinenum
);
454 (void) fprintf(stderr
, "\n");
460 const char * const string
;
464 cp
= ecpyalloc(_("warning: "));
465 cp
= ecatalloc(cp
, string
);
472 usage(FILE *stream
, int status
)
474 (void) fprintf(stream
, _("%s: usage is %s \
475 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
476 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
478 Report bugs to tz@elsie.nci.nih.gov.\n"),
483 static const char * psxrules
;
484 static const char * lcltime
;
485 static const char * directory
;
486 static const char * leapsec
;
487 static const char * yitcommand
;
498 #ifdef _POSIX_VERSION
499 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
500 #endif /* defined _POSIX_VERSION */
502 (void) setlocale(LC_MESSAGES
, "");
504 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
505 #endif /* defined TEXTDOMAINDIR */
506 (void) textdomain(TZ_DOMAIN
);
507 #endif /* HAVE_GETTEXT */
509 if (TYPE_BIT(zic_t
) < 64) {
510 (void) fprintf(stderr
, "%s: %s\n", progname
,
511 _("wild compilation-time specification of zic_t"));
514 for (i
= 1; i
< argc
; ++i
)
515 if (strcmp(argv
[i
], "--version") == 0) {
516 (void) printf("%s\n", TZVERSION
);
518 } else if (strcmp(argv
[i
], "--help") == 0) {
519 usage(stdout
, EXIT_SUCCESS
);
521 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
524 usage(stderr
, EXIT_FAILURE
);
526 if (directory
== NULL
)
529 (void) fprintf(stderr
,
530 _("%s: More than one -d option specified\n"),
539 (void) fprintf(stderr
,
540 _("%s: More than one -l option specified\n"),
546 if (psxrules
== NULL
)
549 (void) fprintf(stderr
,
550 _("%s: More than one -p option specified\n"),
556 if (yitcommand
== NULL
)
559 (void) fprintf(stderr
,
560 _("%s: More than one -y option specified\n"),
569 (void) fprintf(stderr
,
570 _("%s: More than one -L option specified\n"),
579 (void) printf("%s: -s ignored\n", progname
);
582 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
583 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
584 if (directory
== NULL
)
586 if (yitcommand
== NULL
)
587 yitcommand
= "yearistype";
591 if (optind
< argc
&& leapsec
!= NULL
) {
596 for (i
= optind
; i
< argc
; ++i
)
601 for (i
= 0; i
< nzones
; i
= j
) {
603 ** Find the next non-continuation zone entry.
605 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
607 outzone(&zones
[i
], j
- i
);
612 for (i
= 0; i
< nlinks
; ++i
) {
613 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
614 dolink(links
[i
].l_from
, links
[i
].l_to
);
616 for (j
= 0; j
< nlinks
; ++j
)
617 if (strcmp(links
[i
].l_to
,
618 links
[j
].l_from
) == 0)
619 warning(_("link to link"));
621 if (lcltime
!= NULL
) {
622 eat("command line", 1);
623 dolink(lcltime
, TZDEFAULT
);
625 if (psxrules
!= NULL
) {
626 eat("command line", 1);
627 dolink(psxrules
, TZDEFRULES
);
629 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
633 dolink(fromfield
, tofield
)
634 const char * const fromfield
;
635 const char * const tofield
;
637 register char * fromname
;
638 register char * toname
;
640 if (fromfield
[0] == '/')
641 fromname
= ecpyalloc(fromfield
);
643 fromname
= ecpyalloc(directory
);
644 fromname
= ecatalloc(fromname
, "/");
645 fromname
= ecatalloc(fromname
, fromfield
);
647 if (tofield
[0] == '/')
648 toname
= ecpyalloc(tofield
);
650 toname
= ecpyalloc(directory
);
651 toname
= ecatalloc(toname
, "/");
652 toname
= ecatalloc(toname
, tofield
);
655 ** We get to be careful here since
656 ** there's a fair chance of root running us.
659 (void) remove(toname
);
660 if (link(fromname
, toname
) != 0) {
663 if (mkdirs(toname
) != 0)
666 result
= link(fromname
, toname
);
669 access(fromname
, F_OK
) == 0 &&
671 const char *s
= tofield
;
672 register char * symlinkcontents
= NULL
;
674 while ((s
= strchr(s
+1, '/')) != NULL
)
676 ecatalloc(symlinkcontents
,
679 ecatalloc(symlinkcontents
,
681 result
= symlink(symlinkcontents
,
684 warning(_("hard link failed, symbolic link used"));
685 ifree(symlinkcontents
);
687 #endif /* HAVE_SYMLINK */
689 const char *e
= strerror(errno
);
691 (void) fprintf(stderr
,
692 _("%s: Can't link from %s to %s: %s\n"),
693 progname
, fromname
, toname
, e
);
701 #define TIME_T_BITS_IN_FILE 64
709 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
711 max_time
= -(min_time
+ 1);
716 const char * const name
;
718 register char * myname
;
721 myname
= ecpyalloc(name
);
722 myname
= ecatalloc(myname
, "/.");
723 accres
= access(myname
, F_OK
);
729 ** Associate sets of rules with zones.
733 ** Sort by rule name.
741 return strcmp(((const struct rule
*) cp1
)->r_name
,
742 ((const struct rule
*) cp2
)->r_name
);
748 register struct zone
* zp
;
749 register struct rule
* rp
;
750 register int base
, out
;
754 (void) qsort((void *) rules
, (size_t) nrules
,
755 (size_t) sizeof *rules
, rcomp
);
756 for (i
= 0; i
< nrules
- 1; ++i
) {
757 if (strcmp(rules
[i
].r_name
,
758 rules
[i
+ 1].r_name
) != 0)
760 if (strcmp(rules
[i
].r_filename
,
761 rules
[i
+ 1].r_filename
) == 0)
763 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
764 warning(_("same rule name in multiple files"));
765 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
766 warning(_("same rule name in multiple files"));
767 for (j
= i
+ 2; j
< nrules
; ++j
) {
768 if (strcmp(rules
[i
].r_name
,
769 rules
[j
].r_name
) != 0)
771 if (strcmp(rules
[i
].r_filename
,
772 rules
[j
].r_filename
) == 0)
774 if (strcmp(rules
[i
+ 1].r_filename
,
775 rules
[j
].r_filename
) == 0)
782 for (i
= 0; i
< nzones
; ++i
) {
787 for (base
= 0; base
< nrules
; base
= out
) {
789 for (out
= base
+ 1; out
< nrules
; ++out
)
790 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
792 for (i
= 0; i
< nzones
; ++i
) {
794 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
797 zp
->z_nrules
= out
- base
;
800 for (i
= 0; i
< nzones
; ++i
) {
802 if (zp
->z_nrules
== 0) {
804 ** Maybe we have a local standard time offset.
806 eat(zp
->z_filename
, zp
->z_linenum
);
807 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
810 ** Note, though, that if there's no rule,
811 ** a '%s' in the format is a bad thing.
813 if (strchr(zp
->z_format
, '%') != 0)
814 error(_("%s in ruleless zone"));
826 register char ** fields
;
828 register const struct lookup
* lp
;
829 register int nfields
;
830 register int wantcont
;
834 if (strcmp(name
, "-") == 0) {
835 name
= _("standard input");
837 } else if ((fp
= fopen(name
, "r")) == NULL
) {
838 const char *e
= strerror(errno
);
840 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
845 for (num
= 1; ; ++num
) {
847 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
849 cp
= strchr(buf
, '\n');
851 error(_("line too long"));
855 fields
= getfields(buf
);
857 while (fields
[nfields
] != NULL
) {
860 if (strcmp(fields
[nfields
], "-") == 0)
861 fields
[nfields
] = &nada
;
866 } else if (wantcont
) {
867 wantcont
= inzcont(fields
, nfields
);
869 lp
= byword(fields
[0], line_codes
);
871 error(_("input line of unknown type"));
872 else switch ((int) (lp
->l_value
)) {
874 inrule(fields
, nfields
);
878 wantcont
= inzone(fields
, nfields
);
881 inlink(fields
, nfields
);
886 (void) fprintf(stderr
,
887 _("%s: Leap line in non leap seconds file %s\n"),
889 else inleap(fields
, nfields
);
892 default: /* "cannot happen" */
893 (void) fprintf(stderr
,
894 _("%s: panic: Invalid l_value %d\n"),
895 progname
, lp
->l_value
);
899 ifree((char *) fields
);
902 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
906 if (fp
!= stdin
&& fclose(fp
)) {
907 const char *e
= strerror(errno
);
909 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
910 progname
, filename
, e
);
914 error(_("expected continuation line not found"));
918 ** Convert a string of one of the forms
919 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
920 ** into a number of seconds.
921 ** A null string maps to zero.
922 ** Call error with errstring and return zero on errors.
926 gethms(string
, errstring
, signable
)
928 const char * const errstring
;
934 if (string
== NULL
|| *string
== '\0')
938 else if (*string
== '-') {
942 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
944 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
946 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
947 &hh
, &mm
, &ss
) != 3) {
952 mm
< 0 || mm
>= MINSPERHOUR
||
953 ss
< 0 || ss
> SECSPERMIN
) {
957 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
958 error(_("time overflow"));
961 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
962 warning(_("24:00 not handled by pre-1998 versions of zic"));
963 if (noise
&& (hh
> HOURSPERDAY
||
964 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
965 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
966 return oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
967 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
)));
971 inrule(fields
, nfields
)
972 register char ** const fields
;
975 static struct rule r
;
977 if (nfields
!= RULE_FIELDS
) {
978 error(_("wrong number of fields on Rule line"));
981 if (*fields
[RF_NAME
] == '\0') {
982 error(_("nameless rule"));
985 r
.r_filename
= filename
;
986 r
.r_linenum
= linenum
;
987 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
988 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
989 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
990 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
991 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
992 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
993 max_abbrvar_len
= strlen(r
.r_abbrvar
);
994 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
995 (int) ((nrules
+ 1) * sizeof *rules
));
1000 inzone(fields
, nfields
)
1001 register char ** const fields
;
1007 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1008 error(_("wrong number of fields on Zone line"));
1011 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1012 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
1013 (void)sprintf(buf
, /* XXX: sprintf is safe */
1014 _("\"Zone %s\" line and -l option are mutually exclusive"),
1019 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1020 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1021 (void)sprintf(buf
, /* XXX: sprintf is safe */
1022 _("\"Zone %s\" line and -p option are mutually exclusive"),
1027 for (i
= 0; i
< nzones
; ++i
)
1028 if (zones
[i
].z_name
!= NULL
&&
1029 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1030 buf
= erealloc(buf
, (int) (132 +
1031 strlen(fields
[ZF_NAME
]) +
1032 strlen(zones
[i
].z_filename
)));
1033 (void)sprintf(buf
, /* XXX: sprintf is safe */
1034 _("duplicate zone name %s (file \"%s\", line %d)"),
1036 zones
[i
].z_filename
,
1037 zones
[i
].z_linenum
);
1041 return inzsub(fields
, nfields
, FALSE
);
1045 inzcont(fields
, nfields
)
1046 register char ** const fields
;
1049 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1050 error(_("wrong number of fields on Zone continuation line"));
1053 return inzsub(fields
, nfields
, TRUE
);
1057 inzsub(fields
, nfields
, iscont
)
1058 register char ** const fields
;
1063 static struct zone z
;
1064 register int i_gmtoff
, i_rule
, i_format
;
1065 register int i_untilyear
, i_untilmonth
;
1066 register int i_untilday
, i_untiltime
;
1067 register int hasuntil
;
1070 i_gmtoff
= ZFC_GMTOFF
;
1072 i_format
= ZFC_FORMAT
;
1073 i_untilyear
= ZFC_TILYEAR
;
1074 i_untilmonth
= ZFC_TILMONTH
;
1075 i_untilday
= ZFC_TILDAY
;
1076 i_untiltime
= ZFC_TILTIME
;
1079 i_gmtoff
= ZF_GMTOFF
;
1081 i_format
= ZF_FORMAT
;
1082 i_untilyear
= ZF_TILYEAR
;
1083 i_untilmonth
= ZF_TILMONTH
;
1084 i_untilday
= ZF_TILDAY
;
1085 i_untiltime
= ZF_TILTIME
;
1086 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1088 z
.z_filename
= filename
;
1089 z
.z_linenum
= linenum
;
1090 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1091 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1092 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1093 error(_("invalid abbreviation format"));
1097 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1098 z
.z_format
= ecpyalloc(fields
[i_format
]);
1099 if (max_format_len
< strlen(z
.z_format
))
1100 max_format_len
= strlen(z
.z_format
);
1101 hasuntil
= nfields
> i_untilyear
;
1103 z
.z_untilrule
.r_filename
= filename
;
1104 z
.z_untilrule
.r_linenum
= linenum
;
1105 rulesub(&z
.z_untilrule
,
1106 fields
[i_untilyear
],
1109 (nfields
> i_untilmonth
) ?
1110 fields
[i_untilmonth
] : "Jan",
1111 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1112 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1113 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1114 z
.z_untilrule
.r_loyear
);
1115 if (iscont
&& nzones
> 0 &&
1116 z
.z_untiltime
> min_time
&&
1117 z
.z_untiltime
< max_time
&&
1118 zones
[nzones
- 1].z_untiltime
> min_time
&&
1119 zones
[nzones
- 1].z_untiltime
< max_time
&&
1120 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1122 "Zone continuation line end time is not after end time of previous line"
1127 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1128 (int) ((nzones
+ 1) * sizeof *zones
));
1129 zones
[nzones
++] = z
;
1131 ** If there was an UNTIL field on this line,
1132 ** there's more information about the zone on the next line.
1138 inleap(fields
, nfields
)
1139 register char ** const fields
;
1142 register const char * cp
;
1143 register const struct lookup
* lp
;
1145 int year
, month
, day
;
1149 if (nfields
!= LEAP_FIELDS
) {
1150 error(_("wrong number of fields on Leap line"));
1154 cp
= fields
[LP_YEAR
];
1155 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1159 error(_("invalid leaping year"));
1162 if (!leapseen
|| leapmaxyear
< year
)
1164 if (!leapseen
|| leapminyear
> year
)
1170 i
= len_years
[isleap(j
)];
1174 i
= -len_years
[isleap(j
)];
1176 dayoff
= oadd(dayoff
, eitol(i
));
1178 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1179 error(_("invalid month name"));
1182 month
= lp
->l_value
;
1184 while (j
!= month
) {
1185 i
= len_months
[isleap(year
)][j
];
1186 dayoff
= oadd(dayoff
, eitol(i
));
1189 cp
= fields
[LP_DAY
];
1190 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1191 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1192 error(_("invalid day of month"));
1195 dayoff
= oadd(dayoff
, eitol(day
- 1));
1196 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1197 error(_("time before zero"));
1200 if (dayoff
< min_time
/ SECSPERDAY
) {
1201 error(_("time too small"));
1204 if (dayoff
> max_time
/ SECSPERDAY
) {
1205 error(_("time too large"));
1208 t
= (zic_t
) dayoff
* SECSPERDAY
;
1209 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1210 cp
= fields
[LP_CORR
];
1212 register int positive
;
1215 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1218 } else if (strcmp(cp
, "--") == 0) {
1221 } else if (strcmp(cp
, "+") == 0) {
1224 } else if (strcmp(cp
, "++") == 0) {
1228 error(_("illegal CORRECTION field on Leap line"));
1231 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1233 "illegal Rolling/Stationary field on Leap line"
1237 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1242 inlink(fields
, nfields
)
1243 register char ** const fields
;
1248 if (nfields
!= LINK_FIELDS
) {
1249 error(_("wrong number of fields on Link line"));
1252 if (*fields
[LF_FROM
] == '\0') {
1253 error(_("blank FROM field on Link line"));
1256 if (*fields
[LF_TO
] == '\0') {
1257 error(_("blank TO field on Link line"));
1260 l
.l_filename
= filename
;
1261 l
.l_linenum
= linenum
;
1262 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1263 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1264 links
= (struct link
*) (void *) erealloc((char *) links
,
1265 (int) ((nlinks
+ 1) * sizeof *links
));
1266 links
[nlinks
++] = l
;
1270 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1271 register struct rule
* const rp
;
1272 const char * const loyearp
;
1273 const char * const hiyearp
;
1274 const char * const typep
;
1275 const char * const monthp
;
1276 const char * const dayp
;
1277 const char * const timep
;
1279 register const struct lookup
* lp
;
1280 register const char * cp
;
1284 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1285 error(_("invalid month name"));
1288 rp
->r_month
= lp
->l_value
;
1289 rp
->r_todisstd
= FALSE
;
1290 rp
->r_todisgmt
= FALSE
;
1291 dp
= ecpyalloc(timep
);
1293 ep
= dp
+ strlen(dp
) - 1;
1294 switch (lowerit(*ep
)) {
1295 case 's': /* Standard */
1296 rp
->r_todisstd
= TRUE
;
1297 rp
->r_todisgmt
= FALSE
;
1300 case 'w': /* Wall */
1301 rp
->r_todisstd
= FALSE
;
1302 rp
->r_todisgmt
= FALSE
;
1305 case 'g': /* Greenwich */
1306 case 'u': /* Universal */
1307 case 'z': /* Zulu */
1308 rp
->r_todisstd
= TRUE
;
1309 rp
->r_todisgmt
= TRUE
;
1314 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1320 lp
= byword(cp
, begin_years
);
1321 rp
->r_lowasnum
= lp
== NULL
;
1322 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1324 rp
->r_loyear
= INT_MIN
;
1327 rp
->r_loyear
= INT_MAX
;
1329 default: /* "cannot happen" */
1330 (void) fprintf(stderr
,
1331 _("%s: panic: Invalid l_value %d\n"),
1332 progname
, lp
->l_value
);
1334 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1335 error(_("invalid starting year"));
1339 lp
= byword(cp
, end_years
);
1340 rp
->r_hiwasnum
= lp
== NULL
;
1341 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1343 rp
->r_hiyear
= INT_MIN
;
1346 rp
->r_hiyear
= INT_MAX
;
1349 rp
->r_hiyear
= rp
->r_loyear
;
1351 default: /* "cannot happen" */
1352 (void) fprintf(stderr
,
1353 _("%s: panic: Invalid l_value %d\n"),
1354 progname
, lp
->l_value
);
1356 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1357 error(_("invalid ending year"));
1360 if (rp
->r_loyear
> rp
->r_hiyear
) {
1361 error(_("starting year greater than ending year"));
1365 rp
->r_yrtype
= NULL
;
1367 if (rp
->r_loyear
== rp
->r_hiyear
) {
1368 error(_("typed single year"));
1371 rp
->r_yrtype
= ecpyalloc(typep
);
1375 ** Accept things such as:
1381 dp
= ecpyalloc(dayp
);
1382 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1383 rp
->r_dycode
= DC_DOWLEQ
;
1384 rp
->r_wday
= lp
->l_value
;
1385 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1387 if ((ep
= strchr(dp
, '<')) != 0)
1388 rp
->r_dycode
= DC_DOWLEQ
;
1389 else if ((ep
= strchr(dp
, '>')) != 0)
1390 rp
->r_dycode
= DC_DOWGEQ
;
1393 rp
->r_dycode
= DC_DOM
;
1395 if (rp
->r_dycode
!= DC_DOM
) {
1398 error(_("invalid day of month"));
1402 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1403 error(_("invalid weekday name"));
1407 rp
->r_wday
= lp
->l_value
;
1409 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1410 rp
->r_dayofmonth
<= 0 ||
1411 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1412 error(_("invalid day of month"));
1428 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1429 buf
[i
] = val
>> shift
;
1440 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1441 buf
[i
] = val
>> shift
;
1452 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1456 puttzcode64(val
, fp
)
1462 convert64(val
, buf
);
1463 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1471 const zic_t a
= ((const struct attype
*) avp
)->at
;
1472 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1474 return (a
< b
) ? -1 : (a
> b
);
1481 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1485 writezone(name
, string
)
1486 const char * const name
;
1487 const char * const string
;
1491 register int leapcnt32
, leapi32
;
1492 register int timecnt32
, timei32
;
1494 static char * fullname
;
1495 static const struct tzhead tzh0
;
1496 static struct tzhead tzh
;
1497 zic_t ats
[TZ_MAX_TIMES
];
1498 unsigned char types
[TZ_MAX_TIMES
];
1504 (void) qsort((void *) attypes
, (size_t) timecnt
,
1505 (size_t) sizeof *attypes
, atcomp
);
1515 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1518 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1519 ++fromi
; /* handled by default rule */
1520 for ( ; fromi
< timecnt
; ++fromi
) {
1521 if (toi
!= 0 && ((attypes
[fromi
].at
+
1522 gmtoffs
[attypes
[toi
- 1].type
]) <=
1523 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1524 : attypes
[toi
- 2].type
]))) {
1525 attypes
[toi
- 1].type
=
1526 attypes
[fromi
].type
;
1530 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1531 attypes
[toi
++] = attypes
[fromi
];
1538 for (i
= 0; i
< timecnt
; ++i
) {
1539 ats
[i
] = attypes
[i
].at
;
1540 types
[i
] = attypes
[i
].type
;
1543 ** Correct for leap seconds.
1545 for (i
= 0; i
< timecnt
; ++i
) {
1548 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1549 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1554 ** Figure out 32-bit-limited starts and counts.
1556 timecnt32
= timecnt
;
1558 leapcnt32
= leapcnt
;
1560 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1562 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1566 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1568 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1572 fullname
= erealloc(fullname
,
1573 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1574 (void) sprintf(fullname
, "%s/%s", directory
, name
); /* XXX: sprintf is safe */
1576 ** Remove old file, if any, to snap links.
1578 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1579 const char *e
= strerror(errno
);
1581 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1582 progname
, fullname
, e
);
1585 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1586 if (mkdirs(fullname
) != 0)
1588 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1589 const char *e
= strerror(errno
);
1591 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1592 progname
, fullname
, e
);
1596 for (pass
= 1; pass
<= 2; ++pass
) {
1597 register int thistimei
, thistimecnt
;
1598 register int thisleapi
, thisleapcnt
;
1599 register int thistimelim
, thisleaplim
;
1600 int writetype
[TZ_MAX_TIMES
];
1601 int typemap
[TZ_MAX_TYPES
];
1602 register int thistypecnt
;
1603 char thischars
[TZ_MAX_CHARS
];
1605 int indmap
[TZ_MAX_CHARS
];
1608 thistimei
= timei32
;
1609 thistimecnt
= timecnt32
;
1610 thisleapi
= leapi32
;
1611 thisleapcnt
= leapcnt32
;
1614 thistimecnt
= timecnt
;
1616 thisleapcnt
= leapcnt
;
1618 thistimelim
= thistimei
+ thistimecnt
;
1619 thisleaplim
= thisleapi
+ thisleapcnt
;
1620 for (i
= 0; i
< typecnt
; ++i
)
1621 writetype
[i
] = thistimecnt
== timecnt
;
1622 if (thistimecnt
== 0) {
1624 ** No transition times fall in the current
1625 ** (32- or 64-bit) window.
1628 writetype
[typecnt
- 1] = TRUE
;
1630 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1632 writetype
[types
[i
]] = TRUE
;
1634 ** For America/Godthab and Antarctica/Palmer
1637 writetype
[0] = TRUE
;
1639 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1641 ** For some pre-2011 systems: if the last-to-be-written
1642 ** standard (or daylight) type has an offset different from the
1643 ** most recently used offset,
1644 ** append an (unused) copy of the most recently used type
1645 ** (to help get global "altzone" and "timezone" variables
1649 register int mrudst
, mrustd
, hidst
, histd
, type
;
1651 hidst
= histd
= mrudst
= mrustd
= -1;
1652 for (i
= thistimei
; i
< thistimelim
; ++i
)
1653 if (isdsts
[types
[i
]])
1655 else mrustd
= types
[i
];
1656 for (i
= 0; i
< typecnt
; ++i
)
1662 if (hidst
>= 0 && mrudst
>= 0 && hidst
!= mrudst
&&
1663 gmtoffs
[hidst
] != gmtoffs
[mrudst
]) {
1664 isdsts
[mrudst
] = -1;
1665 type
= addtype(gmtoffs
[mrudst
],
1666 &chars
[abbrinds
[mrudst
]],
1670 isdsts
[mrudst
] = TRUE
;
1671 writetype
[type
] = TRUE
;
1673 if (histd
>= 0 && mrustd
>= 0 && histd
!= mrustd
&&
1674 gmtoffs
[histd
] != gmtoffs
[mrustd
]) {
1675 isdsts
[mrustd
] = -1;
1676 type
= addtype(gmtoffs
[mrustd
],
1677 &chars
[abbrinds
[mrustd
]],
1681 isdsts
[mrustd
] = FALSE
;
1682 writetype
[type
] = TRUE
;
1685 #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1687 for (i
= 0; i
< typecnt
; ++i
)
1688 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : -1;
1689 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1692 for (i
= 0; i
< typecnt
; ++i
) {
1693 register char * thisabbr
;
1697 if (indmap
[abbrinds
[i
]] >= 0)
1699 thisabbr
= &chars
[abbrinds
[i
]];
1700 for (j
= 0; j
< thischarcnt
; ++j
)
1701 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1703 if (j
== thischarcnt
) {
1704 (void) strcpy(&thischars
[(int) thischarcnt
],
1706 thischarcnt
+= strlen(thisabbr
) + 1;
1708 indmap
[abbrinds
[i
]] = j
;
1710 #define DO(field) (void) fwrite((void *) tzh.field, \
1711 (size_t) sizeof tzh.field, (size_t) 1, fp)
1713 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1714 tzh
.tzh_version
[0] = ZIC_VERSION
;
1715 convert(eitol(thistypecnt
), tzh
.tzh_ttisgmtcnt
);
1716 convert(eitol(thistypecnt
), tzh
.tzh_ttisstdcnt
);
1717 convert(eitol(thisleapcnt
), tzh
.tzh_leapcnt
);
1718 convert(eitol(thistimecnt
), tzh
.tzh_timecnt
);
1719 convert(eitol(thistypecnt
), tzh
.tzh_typecnt
);
1720 convert(eitol(thischarcnt
), tzh
.tzh_charcnt
);
1731 for (i
= thistimei
; i
< thistimelim
; ++i
)
1733 puttzcode((long) ats
[i
], fp
);
1734 else puttzcode64(ats
[i
], fp
);
1735 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1738 uc
= typemap
[types
[i
]];
1739 (void) fwrite((void *) &uc
,
1744 for (i
= 0; i
< typecnt
; ++i
)
1746 puttzcode(gmtoffs
[i
], fp
);
1747 (void) putc(isdsts
[i
], fp
);
1748 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1750 if (thischarcnt
!= 0)
1751 (void) fwrite((void *) thischars
,
1752 (size_t) sizeof thischars
[0],
1753 (size_t) thischarcnt
, fp
);
1754 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1755 register zic_t todo
;
1758 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1761 if (++j
>= typecnt
) {
1767 while (j
< timecnt
&&
1772 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1773 } else todo
= trans
[i
];
1775 puttzcode((long) todo
, fp
);
1776 else puttzcode64(todo
, fp
);
1777 puttzcode(corr
[i
], fp
);
1779 for (i
= 0; i
< typecnt
; ++i
)
1781 (void) putc(ttisstds
[i
], fp
);
1782 for (i
= 0; i
< typecnt
; ++i
)
1784 (void) putc(ttisgmts
[i
], fp
);
1786 (void) fprintf(fp
, "\n%s\n", string
);
1787 if (ferror(fp
) || fclose(fp
)) {
1788 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1789 progname
, fullname
);
1795 doabbr(abbr
, abbrlen
, format
, letters
, isdst
, doquotes
)
1798 const char * const format
;
1799 const char * const letters
;
1804 register char * slashp
;
1807 slashp
= strchr(format
, '/');
1808 if (slashp
== NULL
) {
1809 if (letters
== NULL
)
1810 (void) strlcpy(abbr
, format
, abbrlen
);
1811 else (void) snprintf(abbr
, abbrlen
, format
, letters
);
1813 (void) strlcpy(abbr
, slashp
+ 1, abbrlen
);
1815 if (slashp
> format
)
1816 (void) strncpy(abbr
, format
,
1817 (unsigned) (slashp
- format
));
1818 abbr
[slashp
- format
] = '\0';
1822 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1823 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1824 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1827 if (len
> 0 && *cp
== '\0')
1829 abbr
[len
+ 2] = '\0';
1830 abbr
[len
+ 1] = '>';
1831 for ( ; len
> 0; --len
)
1832 abbr
[len
] = abbr
[len
- 1];
1847 stringoffset(result
, offset
)
1852 register int minutes
;
1853 register int seconds
;
1857 (void) strcpy(result
, "-");
1860 seconds
= offset
% SECSPERMIN
;
1861 offset
/= SECSPERMIN
;
1862 minutes
= offset
% MINSPERHOUR
;
1863 offset
/= MINSPERHOUR
;
1865 if (hours
>= HOURSPERDAY
) {
1869 (void) sprintf(end(result
), "%d", hours
);
1870 if (minutes
!= 0 || seconds
!= 0) {
1871 (void) sprintf(end(result
), ":%02d", minutes
);
1873 (void) sprintf(end(result
), ":%02d", seconds
);
1879 stringrule(result
, rp
, dstoff
, gmtoff
)
1881 const struct rule
* const rp
;
1887 result
= end(result
);
1888 if (rp
->r_dycode
== DC_DOM
) {
1889 register int month
, total
;
1891 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1894 for (month
= 0; month
< rp
->r_month
; ++month
)
1895 total
+= len_months
[0][month
];
1896 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1900 if (rp
->r_dycode
== DC_DOWGEQ
) {
1901 if ((rp
->r_dayofmonth
% DAYSPERWEEK
) != 1)
1903 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1904 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1905 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1908 if ((rp
->r_dayofmonth
% DAYSPERWEEK
) != 0)
1910 week
= rp
->r_dayofmonth
/ DAYSPERWEEK
;
1912 } else return -1; /* "cannot happen" */
1913 (void) sprintf(result
, "M%d.%d.%d",
1914 rp
->r_month
+ 1, week
, rp
->r_wday
);
1919 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1925 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1926 (void) strcat(result
, "/");
1927 if (stringoffset(end(result
), tod
) != 0)
1934 stringzone(result
, resultlen
, zpfirst
, zonecount
)
1936 const int resultlen
;
1937 const struct zone
* const zpfirst
;
1938 const int zonecount
;
1940 register const struct zone
* zp
;
1941 register struct rule
* rp
;
1942 register struct rule
* stdrp
;
1943 register struct rule
* dstrp
;
1945 register const char * abbrvar
;
1948 zp
= zpfirst
+ zonecount
- 1;
1949 stdrp
= dstrp
= NULL
;
1950 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1951 rp
= &zp
->z_rules
[i
];
1952 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= INT_MAX
)
1954 if (rp
->r_yrtype
!= NULL
)
1956 if (rp
->r_stdoff
== 0) {
1966 if (stdrp
== NULL
&& dstrp
== NULL
) {
1968 ** There are no rules running through "max".
1969 ** Let's find the latest rule.
1971 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1972 rp
= &zp
->z_rules
[i
];
1973 if (stdrp
== NULL
|| rp
->r_hiyear
> stdrp
->r_hiyear
||
1974 (rp
->r_hiyear
== stdrp
->r_hiyear
&&
1975 rp
->r_month
> stdrp
->r_month
))
1978 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0)
1979 return; /* We end up in DST (a POSIX no-no). */
1981 ** Horrid special case: if year is 2037,
1982 ** presume this is a zone handled on a year-by-year basis;
1983 ** do not try to apply a rule to the zone.
1985 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1988 if (stdrp
== NULL
&& (zp
->z_nrules
!= 0 || zp
->z_stdoff
!= 0))
1990 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1991 doabbr(result
, resultlen
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1992 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
1998 doabbr(end(result
), resultlen
- strlen(result
),
1999 zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
2000 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
2001 if (stringoffset(end(result
),
2002 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
2006 (void) strcat(result
, ",");
2007 if (stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
2011 (void) strcat(result
, ",");
2012 if (stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
2019 outzone(zpfirst
, zonecount
)
2020 const struct zone
* const zpfirst
;
2021 const int zonecount
;
2023 register const struct zone
* zp
;
2024 register struct rule
* rp
;
2026 register int usestart
, useuntil
;
2027 register zic_t starttime
, untiltime
;
2028 register long gmtoff
;
2029 register long stdoff
;
2031 register long startoff
;
2032 register int startttisstd
;
2033 register int startttisgmt
;
2035 register char * startbuf
;
2037 register char * envvar
;
2038 register int max_abbr_len
;
2039 register int max_envvar_len
;
2040 register int prodstic
; /* all rules are min to max */
2042 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
2043 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
2044 startbuf
= emalloc(max_abbr_len
+ 1);
2045 ab
= emalloc(max_abbr_len
+ 1);
2046 envvar
= emalloc(max_envvar_len
+ 1);
2047 INITIALIZE(untiltime
);
2048 INITIALIZE(starttime
);
2050 ** Now. . .finally. . .generate some useful data!
2055 prodstic
= zonecount
== 1;
2057 ** Thanks to Earl Chew
2058 ** for noting the need to unconditionally initialize startttisstd.
2060 startttisstd
= FALSE
;
2061 startttisgmt
= FALSE
;
2062 min_year
= max_year
= EPOCH_YEAR
;
2064 updateminmax(leapminyear
);
2065 updateminmax(leapmaxyear
+ (leapmaxyear
< INT_MAX
));
2067 for (i
= 0; i
< zonecount
; ++i
) {
2069 if (i
< zonecount
- 1)
2070 updateminmax(zp
->z_untilrule
.r_loyear
);
2071 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2072 rp
= &zp
->z_rules
[j
];
2074 updateminmax(rp
->r_loyear
);
2076 updateminmax(rp
->r_hiyear
);
2080 ** Generate lots of data if a rule can't cover all future times.
2082 stringzone(envvar
, max_envvar_len
+1, zpfirst
, zonecount
);
2083 if (noise
&& envvar
[0] == '\0') {
2086 wp
= ecpyalloc(_("no POSIX environment variable for zone"));
2087 wp
= ecatalloc(wp
, " ");
2088 wp
= ecatalloc(wp
, zpfirst
->z_name
);
2092 if (envvar
[0] == '\0') {
2093 if (min_year
>= INT_MIN
+ YEARSPERREPEAT
)
2094 min_year
-= YEARSPERREPEAT
;
2095 else min_year
= INT_MIN
;
2096 if (max_year
<= INT_MAX
- YEARSPERREPEAT
)
2097 max_year
+= YEARSPERREPEAT
;
2098 else max_year
= INT_MAX
;
2100 ** Regardless of any of the above,
2101 ** for a "proDSTic" zone which specifies that its rules
2102 ** always have and always will be in effect,
2103 ** we only need one cycle to define the zone.
2107 max_year
= min_year
+ YEARSPERREPEAT
;
2111 ** For the benefit of older systems,
2112 ** generate data from 1900 through 2037.
2114 if (min_year
> 1900)
2116 if (max_year
< 2037)
2118 for (i
= 0; i
< zonecount
; ++i
) {
2120 ** A guess that may well be corrected later.
2124 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2125 useuntil
= i
< (zonecount
- 1);
2126 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2128 gmtoff
= zp
->z_gmtoff
;
2129 eat(zp
->z_filename
, zp
->z_linenum
);
2131 startoff
= zp
->z_gmtoff
;
2132 if (zp
->z_nrules
== 0) {
2133 stdoff
= zp
->z_stdoff
;
2134 doabbr(startbuf
, max_abbr_len
+ 1, zp
->z_format
,
2135 (char *) NULL
, stdoff
!= 0, FALSE
);
2136 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2137 startbuf
, stdoff
!= 0, startttisstd
,
2140 addtt(starttime
, type
);
2142 } else if (stdoff
!= 0)
2143 addtt(min_time
, type
);
2144 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2145 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2148 ** Mark which rules to do in the current year.
2149 ** For those to do, calculate rpytime(rp, year);
2151 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2152 rp
= &zp
->z_rules
[j
];
2153 eats(zp
->z_filename
, zp
->z_linenum
,
2154 rp
->r_filename
, rp
->r_linenum
);
2155 rp
->r_todo
= year
>= rp
->r_loyear
&&
2156 year
<= rp
->r_hiyear
&&
2157 yearistype(year
, rp
->r_yrtype
);
2159 rp
->r_temp
= rpytime(rp
, year
);
2163 register zic_t jtime
, ktime
;
2164 register long offset
;
2169 ** Turn untiltime into UTC
2170 ** assuming the current gmtoff and
2173 untiltime
= zp
->z_untiltime
;
2174 if (!zp
->z_untilrule
.r_todisgmt
)
2175 untiltime
= tadd(untiltime
,
2177 if (!zp
->z_untilrule
.r_todisstd
)
2178 untiltime
= tadd(untiltime
,
2182 ** Find the rule (of those to do, if any)
2183 ** that takes effect earliest in the year.
2186 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2187 rp
= &zp
->z_rules
[j
];
2190 eats(zp
->z_filename
, zp
->z_linenum
,
2191 rp
->r_filename
, rp
->r_linenum
);
2192 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2193 if (!rp
->r_todisstd
)
2194 offset
= oadd(offset
, stdoff
);
2196 if (jtime
== min_time
||
2199 jtime
= tadd(jtime
, -offset
);
2200 if (k
< 0 || jtime
< ktime
) {
2206 break; /* go on to next year */
2207 rp
= &zp
->z_rules
[k
];
2209 if (useuntil
&& ktime
>= untiltime
)
2211 stdoff
= rp
->r_stdoff
;
2212 if (usestart
&& ktime
== starttime
)
2215 if (ktime
< starttime
) {
2216 startoff
= oadd(zp
->z_gmtoff
,
2226 if (*startbuf
== '\0' &&
2227 startoff
== oadd(zp
->z_gmtoff
,
2238 eats(zp
->z_filename
, zp
->z_linenum
,
2239 rp
->r_filename
, rp
->r_linenum
);
2240 doabbr(ab
, max_abbr_len
+1, zp
->z_format
, rp
->r_abbrvar
,
2241 rp
->r_stdoff
!= 0, FALSE
);
2242 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2243 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2244 rp
->r_todisstd
, rp
->r_todisgmt
);
2249 if (*startbuf
== '\0' &&
2250 zp
->z_format
!= NULL
&&
2251 strchr(zp
->z_format
, '%') == NULL
&&
2252 strchr(zp
->z_format
, '/') == NULL
)
2253 (void)strncpy(startbuf
, zp
->z_format
,
2254 max_abbr_len
+ 1 - 1);
2255 eat(zp
->z_filename
, zp
->z_linenum
);
2256 if (*startbuf
== '\0')
2257 error(_("can't determine time zone abbreviation to use just after until time"));
2258 else addtt(starttime
,
2259 addtype(startoff
, startbuf
,
2260 startoff
!= zp
->z_gmtoff
,
2265 ** Now we may get to set starttime for the next zone line.
2268 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2269 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2270 starttime
= zp
->z_untiltime
;
2272 starttime
= tadd(starttime
, -stdoff
);
2274 starttime
= tadd(starttime
, -gmtoff
);
2277 writezone(zpfirst
->z_name
, envvar
);
2284 addtt(starttime
, type
)
2285 const zic_t starttime
;
2288 if (starttime
<= min_time
||
2289 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2290 gmtoffs
[0] = gmtoffs
[type
];
2291 isdsts
[0] = isdsts
[type
];
2292 ttisstds
[0] = ttisstds
[type
];
2293 ttisgmts
[0] = ttisgmts
[type
];
2294 if (abbrinds
[type
] != 0)
2295 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2297 charcnt
= strlen(chars
) + 1;
2302 if (timecnt
>= TZ_MAX_TIMES
) {
2303 error(_("too many transitions?!"));
2306 attypes
[timecnt
].at
= starttime
;
2307 attypes
[timecnt
].type
= type
;
2312 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
2314 const char * const abbr
;
2321 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2322 error(_("internal error - addtype called with bad isdst"));
2325 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2326 error(_("internal error - addtype called with bad ttisstd"));
2329 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2330 error(_("internal error - addtype called with bad ttisgmt"));
2334 ** See if there's already an entry for this zone type.
2335 ** If so, just return its index.
2337 for (i
= 0; i
< typecnt
; ++i
) {
2338 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2339 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2340 ttisstd
== ttisstds
[i
] &&
2341 ttisgmt
== ttisgmts
[i
])
2345 ** There isn't one; add a new one, unless there are already too
2348 if (typecnt
>= TZ_MAX_TYPES
) {
2349 error(_("too many local time types"));
2352 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2353 error(_("UTC offset out of range"));
2356 gmtoffs
[i
] = gmtoff
;
2358 ttisstds
[i
] = ttisstd
;
2359 ttisgmts
[i
] = ttisgmt
;
2361 for (j
= 0; j
< charcnt
; ++j
)
2362 if (strcmp(&chars
[j
], abbr
) == 0)
2372 leapadd(t
, positive
, rolling
, count
)
2380 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2381 error(_("too many leap seconds"));
2384 for (i
= 0; i
< leapcnt
; ++i
)
2385 if (t
<= trans
[i
]) {
2386 if (t
== trans
[i
]) {
2387 error(_("repeated leap second moment"));
2393 for (j
= leapcnt
; j
> i
; --j
) {
2394 trans
[j
] = trans
[j
- 1];
2395 corr
[j
] = corr
[j
- 1];
2396 roll
[j
] = roll
[j
- 1];
2399 corr
[i
] = positive
? 1L : eitol(-count
);
2402 } while (positive
&& --count
!= 0);
2409 register long last
= 0;
2412 ** propagate leap seconds forward
2414 for (i
= 0; i
< leapcnt
; ++i
) {
2415 trans
[i
] = tadd(trans
[i
], last
);
2416 last
= corr
[i
] += last
;
2421 yearistype(year
, type
)
2423 const char * const type
;
2428 if (type
== NULL
|| *type
== '\0')
2430 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
2431 (void)sprintf(buf
, "%s %d %s", yitcommand
, year
, type
); /* XXX: sprintf is safe */
2432 result
= system(buf
);
2433 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2439 error(_("Wild result from command execution"));
2440 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2441 progname
, buf
, result
);
2450 a
= (unsigned char) a
;
2451 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2455 ciequal(ap
, bp
) /* case-insensitive equality */
2456 register const char * ap
;
2457 register const char * bp
;
2459 while (lowerit(*ap
) == lowerit(*bp
++))
2467 register const char * abbr
;
2468 register const char * word
;
2470 if (lowerit(*abbr
) != lowerit(*word
))
2473 while (*++abbr
!= '\0')
2477 } while (lowerit(*word
++) != lowerit(*abbr
));
2481 static const struct lookup
*
2483 register const char * const word
;
2484 register const struct lookup
* const table
;
2486 register const struct lookup
* foundlp
;
2487 register const struct lookup
* lp
;
2489 if (word
== NULL
|| table
== NULL
)
2492 ** Look for exact match.
2494 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2495 if (ciequal(word
, lp
->l_word
))
2498 ** Look for inexact match.
2501 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2502 if (itsabbr(word
, lp
->l_word
)) {
2503 if (foundlp
== NULL
)
2505 else return NULL
; /* multiple inexact matches */
2515 register char ** array
;
2520 array
= (char **) (void *)
2521 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2524 while (isascii((unsigned char) *cp
) &&
2525 isspace((unsigned char) *cp
))
2527 if (*cp
== '\0' || *cp
== '#')
2529 array
[nsubs
++] = dp
= cp
;
2531 if ((*dp
= *cp
++) != '"')
2533 else while ((*dp
= *cp
++) != '"')
2538 "Odd number of quotation marks"
2542 } while (*cp
!= '\0' && *cp
!= '#' &&
2543 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2544 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2548 array
[nsubs
] = NULL
;
2560 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2561 error(_("time overflow"));
2574 if (t1
== max_time
&& t2
> 0)
2576 if (t1
== min_time
&& t2
< 0)
2579 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2580 error(_("time overflow"));
2587 ** Given a rule, and a year, compute the date - in seconds since January 1,
2588 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2592 rpytime(rp
, wantedy
)
2593 register const struct rule
* const rp
;
2594 register const int wantedy
;
2596 register int y
, m
, i
;
2597 register long dayoff
; /* with a nod to Margaret O. */
2600 if (wantedy
== INT_MIN
)
2602 if (wantedy
== INT_MAX
)
2607 while (wantedy
!= y
) {
2609 i
= len_years
[isleap(y
)];
2613 i
= -len_years
[isleap(y
)];
2615 dayoff
= oadd(dayoff
, eitol(i
));
2617 while (m
!= rp
->r_month
) {
2618 i
= len_months
[isleap(y
)][m
];
2619 dayoff
= oadd(dayoff
, eitol(i
));
2622 i
= rp
->r_dayofmonth
;
2623 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2624 if (rp
->r_dycode
== DC_DOWLEQ
)
2627 error(_("use of 2/29 in non leap-year"));
2632 dayoff
= oadd(dayoff
, eitol(i
));
2633 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2636 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2637 wday
= eitol(EPOCH_WDAY
);
2639 ** Don't trust mod of negative numbers.
2642 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2644 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2646 wday
+= LDAYSPERWEEK
;
2648 while (wday
!= eitol(rp
->r_wday
))
2649 if (rp
->r_dycode
== DC_DOWGEQ
) {
2650 dayoff
= oadd(dayoff
, (long) 1);
2651 if (++wday
>= LDAYSPERWEEK
)
2655 dayoff
= oadd(dayoff
, (long) -1);
2657 wday
= LDAYSPERWEEK
- 1;
2660 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2662 warning(_("rule goes past start/end of month--\
2663 will not work with pre-2004 versions of zic"));
2666 if (dayoff
< min_time
/ SECSPERDAY
)
2668 if (dayoff
> max_time
/ SECSPERDAY
)
2670 t
= (zic_t
) dayoff
* SECSPERDAY
;
2671 return tadd(t
, rp
->r_tod
);
2676 const char * const string
;
2680 if (strcmp(string
, GRANDPARENTED
) != 0) {
2681 register const char * cp
;
2685 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2686 ** optionally followed by a + or - and a number from 1 to 14.
2690 while (isascii((unsigned char) *cp
) &&
2691 isalpha((unsigned char) *cp
))
2693 if (cp
- string
== 0)
2694 wp
= _("time zone abbreviation lacks alphabetic at start");
2695 if (noise
&& cp
- string
> 3)
2696 wp
= _("time zone abbreviation has more than 3 alphabetics");
2697 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2698 wp
= _("time zone abbreviation has too many alphabetics");
2699 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2701 if (isascii((unsigned char) *cp
) &&
2702 isdigit((unsigned char) *cp
))
2704 *cp
>= '0' && *cp
<= '4')
2708 wp
= _("time zone abbreviation differs from POSIX standard");
2711 wp
= ecatalloc(wp
, " (");
2712 wp
= ecatalloc(wp
, string
);
2713 wp
= ecatalloc(wp
, ")");
2718 i
= strlen(string
) + 1;
2719 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2720 error(_("too many, or too long, time zone abbreviations"));
2723 (void)strncpy(&chars
[charcnt
], string
, sizeof(chars
) - charcnt
- 1);
2724 charcnt
+= eitol(i
);
2731 register char * name
;
2734 if (argname
== NULL
|| *argname
== '\0')
2736 cp
= name
= ecpyalloc(argname
);
2737 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2741 ** DOS drive specifier?
2743 if (isalpha((unsigned char) name
[0]) &&
2744 name
[1] == ':' && name
[2] == '\0') {
2748 #endif /* !defined __NetBSD__ */
2749 if (!itsdir(name
)) {
2751 ** It doesn't seem to exist, so we try to create it.
2752 ** Creation may fail because of the directory being
2753 ** created by some other multiprocessor, so we get
2754 ** to do extra checking.
2756 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2757 const char *e
= strerror(errno
);
2759 if (errno
!= EEXIST
|| !itsdir(name
)) {
2760 (void) fprintf(stderr
,
2761 _("%s: Can't create directory %s: %s\n"),
2781 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2782 (void) fprintf(stderr
,
2783 _("%s: %d did not sign extend correctly\n"),
2791 ** UNIX was a registered trademark of The Open Group in 2003.