2 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
3 * Use is subject to license terms.
6 static char elsieid
[] = "@(#)zic.c 7.128.1";
9 * #define LEAPSECOND_SUPPORT
13 * Regardless of the type of time_t, we do our work using this type.
19 #include <tzfile.h> /* this is in system headers at Sun */
21 #include <sys/stat.h> /* for umask manifest constants */
24 #include <stdlib.h> /* for getopt */
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 */
31 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
33 #define MKDIR_UMASK 0755
37 const char *r_filename
;
41 int r_loyear
; /* for example, 1986 */
42 int r_hiyear
; /* for example, 1986 */
45 int r_month
; /* 0..11 */
47 int r_dycode
; /* see below */
51 long r_tod
; /* time from midnight */
52 int r_todisstd
; /* above is standard time if TRUE */
53 /* or wall clock time if FALSE */
54 int r_todisgmt
; /* above is GMT if TRUE */
55 /* or local time if FALSE */
56 long r_stdoff
; /* offset from standard time */
57 const char *r_abbrvar
; /* variable part of abbreviation */
59 int r_todo
; /* a rule to do (used in outzone) */
60 zic_t r_temp
; /* used in outzone */
64 * r_dycode r_dayofmonth r_wday
67 #define DC_DOM 0 /* 1..31 */ /* unused */
68 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
69 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
72 const char *z_filename
;
85 struct rule z_untilrule
;
89 static void addtt(zic_t starttime
, int type
);
90 static int addtype(long gmtoff
, const char *abbr
, int isdst
,
91 int ttisstd
, int ttisgmt
);
92 #ifdef LEAPSECOND_SUPPORT
93 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
94 static void adjleap(void);
96 static void associate(void);
97 static int ciequal(const char *ap
, const char *bp
);
98 static void convert(long val
, char *buf
);
99 static void dolink(const char *fromfield
, const char *tofield
);
100 static void doabbr(char *abbr
, const char *format
,
101 const char *letters
, int isdst
);
102 static void eat(const char *name
, int num
);
103 static void eats(const char *name
, int num
,
104 const char *rname
, int rnum
);
105 static long eitol(int i
);
106 static void error(const char *message
);
107 static char **getfields(char *buf
);
108 static long gethms(const char *string
, const char *errstrng
, int signable
);
109 static void infile(const char *filename
);
110 #ifdef LEAPSECOND_SUPPORT
111 static void inleap(char **fields
, int nfields
);
113 static void inlink(char **fields
, int nfields
);
114 static void inrule(char **fields
, int nfields
);
115 static int inzcont(char **fields
, int nfields
);
116 static int inzone(char **fields
, int nfields
);
117 static int inzsub(char **fields
, int nfields
, int iscont
);
118 static int itsabbr(const char *abbr
, const char *word
);
119 static int itsdir(const char *name
);
120 static int lowerit(int c
);
121 static char *memcheck(char *tocheck
);
122 static int mkdirs(char *filename
);
123 static void newabbr(const char *abbr
);
124 static long oadd(long t1
, long t2
);
125 static void outzone(const struct zone
*zp
, int ntzones
);
126 static void puttzcode(long code
, FILE *fp
);
127 static int rcomp(const void *leftp
, const void *rightp
);
128 static zic_t
rpytime(const struct rule
*rp
, int wantedy
);
129 static void rulesub(struct rule
*rp
,
130 const char *loyearp
, const char *hiyearp
,
131 const char *typep
, const char *monthp
,
132 const char *dayp
, const char *timep
);
133 static void setboundaries(void);
134 static zic_t
tadd(zic_t t1
, long t2
);
135 static void usage(void);
136 static void writezone(const char *name
);
137 static int yearistype(int year
, const char *type
);
141 static const char *filename
;
144 static zic_t max_time
;
146 static int max_year_representable
;
147 static zic_t min_time
;
149 static int min_year_representable
;
151 static const char *rfilename
;
153 static const char *progname
;
167 * Which fields are which on a Zone line.
175 #define ZF_TILMONTH 6
178 #define ZONE_MINFIELDS 5
179 #define ZONE_MAXFIELDS 9
182 * Which fields are which on a Zone continuation line.
188 #define ZFC_TILYEAR 3
189 #define ZFC_TILMONTH 4
191 #define ZFC_TILTIME 6
192 #define ZONEC_MINFIELDS 3
193 #define ZONEC_MAXFIELDS 7
196 * Which files are which on a Rule line.
208 #define RULE_FIELDS 10
211 * Which fields are which on a Link line.
216 #define LINK_FIELDS 3
219 * Which fields are which on a Leap line.
228 #define LEAP_FIELDS 7
238 static struct rule
*rules
;
239 static int nrules
; /* number of rules */
241 static struct zone
*zones
;
242 static int nzones
; /* number of zones */
245 const char *l_filename
;
251 static struct link
*links
;
259 static struct lookup
const *byword(const char *string
,
260 const struct lookup
*lp
);
262 static struct lookup
const line_codes
[] = {
270 static struct lookup
const mon_names
[] = {
271 { "January", TM_JANUARY
},
272 { "February", TM_FEBRUARY
},
273 { "March", TM_MARCH
},
274 { "April", TM_APRIL
},
278 { "August", TM_AUGUST
},
279 { "September", TM_SEPTEMBER
},
280 { "October", TM_OCTOBER
},
281 { "November", TM_NOVEMBER
},
282 { "December", TM_DECEMBER
},
286 static struct lookup
const wday_names
[] = {
287 { "Sunday", TM_SUNDAY
},
288 { "Monday", TM_MONDAY
},
289 { "Tuesday", TM_TUESDAY
},
290 { "Wednesday", TM_WEDNESDAY
},
291 { "Thursday", TM_THURSDAY
},
292 { "Friday", TM_FRIDAY
},
293 { "Saturday", TM_SATURDAY
},
297 static struct lookup
const lasts
[] = {
298 { "last-Sunday", TM_SUNDAY
},
299 { "last-Monday", TM_MONDAY
},
300 { "last-Tuesday", TM_TUESDAY
},
301 { "last-Wednesday", TM_WEDNESDAY
},
302 { "last-Thursday", TM_THURSDAY
},
303 { "last-Friday", TM_FRIDAY
},
304 { "last-Saturday", TM_SATURDAY
},
308 static struct lookup
const begin_years
[] = {
309 { "minimum", YR_MINIMUM
},
310 { "maximum", YR_MAXIMUM
},
314 static struct lookup
const end_years
[] = {
315 { "minimum", YR_MINIMUM
},
316 { "maximum", YR_MAXIMUM
},
321 static struct lookup
const leap_types
[] = {
323 { "Stationary", FALSE
},
327 static const int len_months
[2][MONSPERYEAR
] = {
328 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
329 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
332 static const int len_years
[2] = {
333 DAYSPERNYEAR
, DAYSPERLYEAR
336 static struct attype
{
339 } attypes
[TZ_MAX_TIMES
];
340 static long gmtoffs
[TZ_MAX_TYPES
];
341 static char isdsts
[TZ_MAX_TYPES
];
342 static unsigned char abbrinds
[TZ_MAX_TYPES
];
343 static char ttisstds
[TZ_MAX_TYPES
];
344 static char ttisgmts
[TZ_MAX_TYPES
];
345 static char chars
[TZ_MAX_CHARS
];
346 static zic_t trans
[TZ_MAX_LEAPS
];
347 static long corr
[TZ_MAX_LEAPS
];
348 static char roll
[TZ_MAX_LEAPS
];
359 const char *e
= strerror(errno
);
360 (void) fprintf(stderr
, gettext("%s: Memory exhausted: %s\n"),
367 #define emalloc(size) memcheck(imalloc(size))
368 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
369 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
370 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
377 eats(name
, num
, rname
, rnum
)
378 const char * const name
;
380 const char * const rname
;
391 const char * const name
;
394 eats(name
, num
, (char *)NULL
, -1);
399 const char * const string
;
402 * Match the format of "cc" to allow sh users to
403 * zic ... 2>&1 | error -t "*" -v
406 (void) fprintf(stderr
, gettext("\"%s\", line %d: %s"),
407 filename
, linenum
, string
);
408 if (rfilename
!= NULL
)
409 (void) fprintf(stderr
, gettext(" (rule from \"%s\", line %d)"),
410 rfilename
, rlinenum
);
411 (void) fprintf(stderr
, "\n");
417 const char * const string
;
421 cp
= ecpyalloc(gettext("warning: "));
422 cp
= ecatalloc(cp
, string
);
431 #ifdef LEAPSECOND_SUPPORT
432 (void) fprintf(stderr
, gettext("%s: usage is %s "
433 "[ --version ] [ -s ] [ -v ] [ -l localtime ] "
434 "\n\t[ -p posixrules ] [ -d directory ] [ -L leapseconds ] "
435 "[ -y yearistype ] [ filename ... ]\n"), progname
, progname
);
436 #else /* ! LEAPSECOND_SUPPORT */
437 (void) fprintf(stderr
, gettext("%s: usage is %s "
438 "[ --version ] [ -s ] [ -v ] [ -l localtime ]"
439 "\n\t[ -p posixrules ] [ -d directory ] [ -y yearistype ] "
440 "[ filename ... ]\n"), progname
, progname
);
441 #endif /* LEAPSECOND_SUPPORT */
444 static const char *psxrules
;
445 static const char *lcltime
;
446 static const char *directory
;
447 static const char *leapsec
;
448 static const char *yitcommand
;
449 static int sflag
= FALSE
;
460 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
462 (void) setlocale(LC_ALL
, "");
463 #if !defined(TEXT_DOMAIN)
464 #define TEXT_DOMAIN "SYS_TEST"
466 (void) textdomain(TEXT_DOMAIN
);
469 for (i
= 1; i
< argc
; ++i
)
470 if (strcmp(argv
[i
], "--version") == 0) {
471 (void) printf("%s\n", elsieid
);
475 #ifdef LEAPSECOND_SUPPORT
476 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
)
478 while ((c
= getopt(argc
, argv
, "d:l:p:vsy:")) != EOF
)
484 if (directory
== NULL
)
487 (void) fprintf(stderr
, gettext(
488 "%s: More than one -d option specified\n"),
497 (void) fprintf(stderr
, gettext(
498 "%s: More than one -l option specified\n"),
504 if (psxrules
== NULL
)
507 (void) fprintf(stderr
, gettext(
508 "%s: More than one -p option specified\n"),
514 if (yitcommand
== NULL
)
517 (void) fprintf(stderr
, gettext(
518 "%s: More than one -y option specified\n"),
523 #ifdef LEAPSECOND_SUPPORT
528 (void) fprintf(stderr
, gettext(
529 "%s: More than one -L option specified\n"),
534 #endif /* LEAPSECOND_SUPPORT */
542 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
543 usage(); /* usage message by request */
544 if (directory
== NULL
)
546 if (yitcommand
== NULL
)
547 yitcommand
= "yearistype";
551 #ifdef LEAPSECOND_SUPPORT
552 if (optind
< argc
&& leapsec
!= NULL
) {
556 #endif /* LEAPSECOND_SUPPORT */
558 for (i
= optind
; i
< argc
; ++i
)
563 for (i
= 0; i
< nzones
; i
= j
) {
565 * Find the next non-continuation zone entry.
567 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
569 outzone(&zones
[i
], j
- i
);
574 for (i
= 0; i
< nlinks
; ++i
) {
575 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
576 dolink(links
[i
].l_from
, links
[i
].l_to
);
578 for (j
= 0; j
< nlinks
; ++j
)
579 if (strcmp(links
[i
].l_to
, links
[j
].l_from
) == 0)
580 warning(gettext("link to link"));
582 if (lcltime
!= NULL
) {
583 eat("command line", 1);
584 dolink(lcltime
, TZDEFAULT
);
586 if (psxrules
!= NULL
) {
587 eat("command line", 1);
588 dolink(psxrules
, TZDEFRULES
);
590 return ((errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
);
594 dolink(fromfield
, tofield
)
595 const char * const fromfield
;
596 const char * const tofield
;
598 register char *fromname
;
599 register char *toname
;
601 if (fromfield
[0] == '/')
602 fromname
= ecpyalloc(fromfield
);
604 fromname
= ecpyalloc(directory
);
605 fromname
= ecatalloc(fromname
, "/");
606 fromname
= ecatalloc(fromname
, fromfield
);
608 if (tofield
[0] == '/')
609 toname
= ecpyalloc(tofield
);
611 toname
= ecpyalloc(directory
);
612 toname
= ecatalloc(toname
, "/");
613 toname
= ecatalloc(toname
, tofield
);
616 * We get to be careful here since
617 * there's a fair chance of root running us.
620 (void) remove(toname
);
621 if (link(fromname
, toname
) != 0) {
624 if (mkdirs(toname
) != 0)
627 result
= link(fromname
, toname
);
629 if (result
!= 0 && access(fromname
, F_OK
) == 0 &&
631 const char *s
= tofield
;
632 register char *symlinkcontents
= NULL
;
634 while ((s
= strchr(s
+1, '/')) != NULL
)
635 symlinkcontents
= ecatalloc(symlinkcontents
,
637 symlinkcontents
= ecatalloc(symlinkcontents
, fromname
);
638 result
= symlink(symlinkcontents
, toname
);
641 "hard link failed, symbolic link used"));
642 ifree(symlinkcontents
);
646 const char *e
= strerror(errno
);
648 (void) fprintf(stderr
, gettext(
649 "%s: Can't link from %s to %s: %s\n"),
650 progname
, fromname
, toname
, e
);
659 #define INT_MAX ((int)(((unsigned)~0)>>1))
660 #endif /* !defined INT_MAX */
663 #define INT_MIN ((int)~(((unsigned)~0)>>1))
664 #endif /* !defined INT_MIN */
667 * The tz file format currently allows at most 32-bit quantities.
668 * This restriction should be removed before signed 32-bit values
669 * wrap around in 2038, but unfortunately this will require a
670 * change to the tz file format.
673 #define MAX_BITS_IN_FILE 32
675 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
676 TYPE_BIT(zic_t): MAX_BITS_IN_FILE)
683 if (TYPE_SIGNED(zic_t
)) {
685 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
687 max_time
= -(min_time
+ 1);
692 max_time
= 2 - sflag
;
693 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
700 t
= (time_t)min_time
;
701 min_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
702 t
= (time_t)max_time
;
703 max_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
705 min_year_representable
= min_year
;
706 max_year_representable
= max_year
;
711 const char * const name
;
713 register char *myname
;
716 myname
= ecpyalloc(name
);
717 myname
= ecatalloc(myname
, "/.");
718 accres
= access(myname
, F_OK
);
720 return (accres
== 0);
724 * Associate sets of rules with zones.
736 return (strcmp(((const struct rule
*) cp1
)->r_name
,
737 ((const struct rule
*) cp2
)->r_name
));
743 register struct zone
*zp
;
744 register struct rule
*rp
;
745 register int base
, out
;
749 (void) qsort((void *)rules
, (size_t)nrules
,
750 (size_t)sizeof (*rules
), rcomp
);
751 for (i
= 0; i
< nrules
- 1; ++i
) {
752 if (strcmp(rules
[i
].r_name
,
753 rules
[i
+ 1].r_name
) != 0)
755 if (strcmp(rules
[i
].r_filename
,
756 rules
[i
+ 1].r_filename
) == 0)
758 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
759 warning(gettext("same rule name in multiple files"));
760 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
761 warning(gettext("same rule name in multiple files"));
762 for (j
= i
+ 2; j
< nrules
; ++j
) {
763 if (strcmp(rules
[i
].r_name
,
764 rules
[j
].r_name
) != 0)
766 if (strcmp(rules
[i
].r_filename
,
767 rules
[j
].r_filename
) == 0)
769 if (strcmp(rules
[i
+ 1].r_filename
,
770 rules
[j
].r_filename
) == 0)
777 for (i
= 0; i
< nzones
; ++i
) {
782 for (base
= 0; base
< nrules
; base
= out
) {
784 for (out
= base
+ 1; out
< nrules
; ++out
)
785 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
787 for (i
= 0; i
< nzones
; ++i
) {
789 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
792 zp
->z_nrules
= out
- base
;
795 for (i
= 0; i
< nzones
; ++i
) {
797 if (zp
->z_nrules
== 0) {
799 * Maybe we have a local standard time offset.
801 eat(zp
->z_filename
, zp
->z_linenum
);
802 zp
->z_stdoff
= gethms(zp
->z_rule
,
803 gettext("unruly zone"), TRUE
);
805 * Note, though, that if there's no rule,
806 * a '%s' in the format is a bad thing.
808 if (strchr(zp
->z_format
, '%') != 0)
809 error(gettext("%s in ruleless zone"));
821 register char **fields
;
823 register const struct lookup
*lp
;
824 register int nfields
;
825 register int wantcont
;
829 if (strcmp(name
, "-") == 0) {
830 name
= gettext("standard input");
832 } else if ((fp
= fopen(name
, "r")) == NULL
) {
833 const char *e
= strerror(errno
);
835 (void) fprintf(stderr
, gettext("%s: Can't open %s: %s\n"),
840 for (num
= 1; ; ++num
) {
842 if (fgets(buf
, (int)sizeof (buf
), fp
) != buf
)
844 cp
= strchr(buf
, '\n');
846 error(gettext("line too long"));
850 fields
= getfields(buf
);
852 while (fields
[nfields
] != NULL
) {
855 if (strcmp(fields
[nfields
], "-") == 0)
856 fields
[nfields
] = &nada
;
861 } else if (wantcont
) {
862 wantcont
= inzcont(fields
, nfields
);
864 lp
= byword(fields
[0], line_codes
);
866 error(gettext("input line of unknown type"));
867 else switch ((int)(lp
->l_value
)) {
869 inrule(fields
, nfields
);
873 wantcont
= inzone(fields
, nfields
);
876 inlink(fields
, nfields
);
879 #ifdef LEAPSECOND_SUPPORT
882 (void) fprintf(stderr
, gettext(
883 "%s: Leap line in non leap seconds file %s\n"),
885 else inleap(fields
, nfields
);
888 #endif /* LEAPSECOND_SUPPORT */
889 default: /* "cannot happen" */
890 (void) fprintf(stderr
, gettext(
891 "%s: panic: Invalid l_value %d\n"),
892 progname
, lp
->l_value
);
896 ifree((char *)fields
);
899 (void) fprintf(stderr
, gettext("%s: Error reading %s\n"),
903 if (fp
!= stdin
&& fclose(fp
)) {
904 const char *e
= strerror(errno
);
905 (void) fprintf(stderr
, gettext("%s: Error closing %s: %s\n"),
906 progname
, filename
, e
);
910 error(gettext("expected continuation line not found"));
914 * Convert a string of one of the forms
915 * h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
916 * into a number of seconds.
917 * A null string maps to zero.
918 * Call error with errstring and return zero on errors.
922 gethms(string
, errstring
, signable
)
924 const char * const errstring
;
930 if (string
== NULL
|| *string
== '\0')
934 else if (*string
== '-') {
938 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
940 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
942 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
943 &hh
, &mm
, &ss
) != 3) {
948 mm
< 0 || mm
>= MINSPERHOUR
||
949 ss
< 0 || ss
> SECSPERMIN
) {
953 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
954 error(gettext("time overflow"));
957 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
959 gettext("24:00 not handled by pre-1998 versions of zic"));
960 if (noise
&& (hh
> HOURSPERDAY
||
961 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
962 warning(gettext("values over 24 hours not handled by "
963 "pre-2007 versions of zic"));
965 return (oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
966 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
))));
970 inrule(fields
, nfields
)
971 register char ** const fields
;
974 static struct rule r
;
976 if (nfields
!= RULE_FIELDS
) {
977 error(gettext("wrong number of fields on Rule line"));
980 if (*fields
[RF_NAME
] == '\0') {
981 error(gettext("nameless rule"));
984 r
.r_filename
= filename
;
985 r
.r_linenum
= linenum
;
986 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], gettext("invalid saved time"),
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 rules
= (struct rule
*)(void *)erealloc((char *)rules
,
993 (int)((nrules
+ 1) * sizeof (*rules
)));
998 inzone(fields
, nfields
)
999 register char ** const fields
;
1005 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1006 error(gettext("wrong number of fields on Zone line"));
1009 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1010 buf
= erealloc(buf
, (int)(132 + strlen(TZDEFAULT
)));
1012 gettext("\"Zone %s\" line and -l option are mutually exclusive"),
1017 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1018 buf
= erealloc(buf
, (int)(132 + strlen(TZDEFRULES
)));
1020 gettext("\"Zone %s\" line and -p option are mutually exclusive"),
1025 for (i
= 0; i
< nzones
; ++i
)
1026 if (zones
[i
].z_name
!= NULL
&&
1027 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1028 buf
= erealloc(buf
, (int)(132 +
1029 strlen(fields
[ZF_NAME
]) +
1030 strlen(zones
[i
].z_filename
)));
1032 gettext("duplicate zone name %s (file \"%s\", line %d)"),
1034 zones
[i
].z_filename
,
1035 zones
[i
].z_linenum
);
1039 return (inzsub(fields
, nfields
, FALSE
));
1043 inzcont(fields
, nfields
)
1044 register char ** const fields
;
1047 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1049 "wrong number of fields on Zone continuation line"));
1052 return (inzsub(fields
, nfields
, TRUE
));
1056 inzsub(fields
, nfields
, iscont
)
1057 register char ** const fields
;
1062 static struct zone z
;
1063 register int i_gmtoff
, i_rule
, i_format
;
1064 register int i_untilyear
, i_untilmonth
;
1065 register int i_untilday
, i_untiltime
;
1066 register int hasuntil
;
1069 i_gmtoff
= ZFC_GMTOFF
;
1071 i_format
= ZFC_FORMAT
;
1072 i_untilyear
= ZFC_TILYEAR
;
1073 i_untilmonth
= ZFC_TILMONTH
;
1074 i_untilday
= ZFC_TILDAY
;
1075 i_untiltime
= ZFC_TILTIME
;
1078 i_gmtoff
= ZF_GMTOFF
;
1080 i_format
= ZF_FORMAT
;
1081 i_untilyear
= ZF_TILYEAR
;
1082 i_untilmonth
= ZF_TILMONTH
;
1083 i_untilday
= ZF_TILDAY
;
1084 i_untiltime
= ZF_TILTIME
;
1085 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1087 z
.z_filename
= filename
;
1088 z
.z_linenum
= linenum
;
1089 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], gettext("invalid UTC offset"),
1091 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1092 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1093 error(gettext("invalid abbreviation format"));
1097 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1098 z
.z_format
= ecpyalloc(fields
[i_format
]);
1099 hasuntil
= nfields
> i_untilyear
;
1101 z
.z_untilrule
.r_filename
= filename
;
1102 z
.z_untilrule
.r_linenum
= linenum
;
1103 rulesub(&z
.z_untilrule
,
1104 fields
[i_untilyear
],
1107 (nfields
> i_untilmonth
) ?
1108 fields
[i_untilmonth
] : "Jan",
1109 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1110 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1111 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1112 z
.z_untilrule
.r_loyear
);
1113 if (iscont
&& nzones
> 0 &&
1114 z
.z_untiltime
> min_time
&&
1115 z
.z_untiltime
< max_time
&&
1116 zones
[nzones
- 1].z_untiltime
> min_time
&&
1117 zones
[nzones
- 1].z_untiltime
< max_time
&&
1118 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1120 "Zone continuation line end time is not after end time of previous line"));
1124 zones
= (struct zone
*)(void *)erealloc((char *)zones
,
1125 (int)((nzones
+ 1) * sizeof (*zones
)));
1126 zones
[nzones
++] = z
;
1128 * If there was an UNTIL field on this line,
1129 * there's more information about the zone on the next line.
1134 #ifdef LEAPSECOND_SUPPORT
1136 inleap(fields
, nfields
)
1137 register char ** const fields
;
1140 register const char *cp
;
1141 register const struct lookup
*lp
;
1143 int year
, month
, day
;
1147 if (nfields
!= LEAP_FIELDS
) {
1148 error(gettext("wrong number of fields on Leap line"));
1152 cp
= fields
[LP_YEAR
];
1153 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1157 error(gettext("invalid leaping year"));
1163 i
= len_years
[isleap(j
)];
1167 i
= -len_years
[isleap(j
)];
1169 dayoff
= oadd(dayoff
, eitol(i
));
1171 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1172 error(gettext("invalid month name"));
1175 month
= lp
->l_value
;
1177 while (j
!= month
) {
1178 i
= len_months
[isleap(year
)][j
];
1179 dayoff
= oadd(dayoff
, eitol(i
));
1182 cp
= fields
[LP_DAY
];
1183 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1184 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1185 error(gettext("invalid day of month"));
1188 dayoff
= oadd(dayoff
, eitol(day
- 1));
1189 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1190 error(gettext("time before zero"));
1193 if (dayoff
< min_time
/ SECSPERDAY
) {
1194 error(gettext("time too small"));
1197 if (dayoff
> max_time
/ SECSPERDAY
) {
1198 error(gettext("time too large"));
1201 t
= (zic_t
)dayoff
* SECSPERDAY
;
1202 tod
= gethms(fields
[LP_TIME
], gettext("invalid time of day"), FALSE
);
1203 cp
= fields
[LP_CORR
];
1205 register int positive
;
1208 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1211 } else if (strcmp(cp
, "--") == 0) {
1214 } else if (strcmp(cp
, "+") == 0) {
1217 } else if (strcmp(cp
, "++") == 0) {
1221 error(gettext("illegal CORRECTION field on Leap line"));
1224 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1226 "illegal Rolling/Stationary field on Leap line"));
1229 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1232 #endif /* LEAPSECOND_SUPPORT */
1235 inlink(fields
, nfields
)
1236 register char ** const fields
;
1241 if (nfields
!= LINK_FIELDS
) {
1242 error(gettext("wrong number of fields on Link line"));
1245 if (*fields
[LF_FROM
] == '\0') {
1246 error(gettext("blank FROM field on Link line"));
1249 if (*fields
[LF_TO
] == '\0') {
1250 error(gettext("blank TO field on Link line"));
1253 l
.l_filename
= filename
;
1254 l
.l_linenum
= linenum
;
1255 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1256 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1257 links
= (struct link
*)(void *)erealloc((char *)links
,
1258 (int)((nlinks
+ 1) * sizeof (*links
)));
1259 links
[nlinks
++] = l
;
1263 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1264 register struct rule
* const rp
;
1265 const char * const loyearp
;
1266 const char * const hiyearp
;
1267 const char * const typep
;
1268 const char * const monthp
;
1269 const char * const dayp
;
1270 const char * const timep
;
1272 register const struct lookup
*lp
;
1273 register const char *cp
;
1277 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1278 error(gettext("invalid month name"));
1281 rp
->r_month
= lp
->l_value
;
1282 rp
->r_todisstd
= FALSE
;
1283 rp
->r_todisgmt
= FALSE
;
1284 dp
= ecpyalloc(timep
);
1286 ep
= dp
+ strlen(dp
) - 1;
1287 switch (lowerit(*ep
)) {
1288 case 's': /* Standard */
1289 rp
->r_todisstd
= TRUE
;
1290 rp
->r_todisgmt
= FALSE
;
1293 case 'w': /* Wall */
1294 rp
->r_todisstd
= FALSE
;
1295 rp
->r_todisgmt
= FALSE
;
1298 case 'g': /* Greenwich */
1299 case 'u': /* Universal */
1300 case 'z': /* Zulu */
1301 rp
->r_todisstd
= TRUE
;
1302 rp
->r_todisgmt
= TRUE
;
1307 rp
->r_tod
= gethms(dp
, gettext("invalid time of day"), FALSE
);
1313 lp
= byword(cp
, begin_years
);
1315 switch ((int)lp
->l_value
) {
1317 rp
->r_loyear
= INT_MIN
;
1320 rp
->r_loyear
= INT_MAX
;
1322 default: /* "cannot happen" */
1323 (void) fprintf(stderr
,
1324 gettext("%s: panic: Invalid l_value %d\n"),
1325 progname
, lp
->l_value
);
1328 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1329 error(gettext("invalid starting year"));
1332 if (rp
->r_loyear
< min_year_representable
)
1334 "starting year too low to be represented"));
1335 else if (rp
->r_loyear
> max_year_representable
)
1337 "starting year too high to be represented"));
1340 if ((lp
= byword(cp
, end_years
)) != NULL
) {
1341 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 gettext("%s: panic: Invalid l_value %d\n"),
1354 progname
, lp
->l_value
);
1357 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1358 error(gettext("invalid ending year"));
1361 if (rp
->r_loyear
< min_year_representable
)
1363 "ending year too low to be represented"));
1364 else if (rp
->r_loyear
> max_year_representable
)
1366 "ending year too high to be represented"));
1368 if (rp
->r_loyear
> rp
->r_hiyear
) {
1369 error(gettext("starting year greater than ending year"));
1373 rp
->r_yrtype
= NULL
;
1375 if (rp
->r_loyear
== rp
->r_hiyear
) {
1376 error(gettext("typed single year"));
1379 rp
->r_yrtype
= ecpyalloc(typep
);
1381 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1382 min_year
= rp
->r_loyear
;
1385 * Accept things such as:
1391 dp
= ecpyalloc(dayp
);
1392 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1393 rp
->r_dycode
= DC_DOWLEQ
;
1394 rp
->r_wday
= lp
->l_value
;
1395 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1397 if ((ep
= strchr(dp
, '<')) != 0)
1398 rp
->r_dycode
= DC_DOWLEQ
;
1399 else if ((ep
= strchr(dp
, '>')) != 0)
1400 rp
->r_dycode
= DC_DOWGEQ
;
1403 rp
->r_dycode
= DC_DOM
;
1405 if (rp
->r_dycode
!= DC_DOM
) {
1408 error(gettext("invalid day of month"));
1412 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1413 error(gettext("invalid weekday name"));
1417 rp
->r_wday
= lp
->l_value
;
1419 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1420 rp
->r_dayofmonth
<= 0 ||
1421 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1422 error(gettext("invalid day of month"));
1436 register long shift
;
1438 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1439 buf
[i
] = val
>> shift
;
1450 (void) fwrite((void *)buf
, (size_t)sizeof (buf
), (size_t)1, fp
);
1458 if (((struct attype
*)avp
)->at
< ((struct attype
*)bvp
)->at
)
1460 else if (((struct attype
*)avp
)->at
> ((struct attype
*)bvp
)->at
)
1467 const char * const name
;
1471 static char *fullname
;
1472 static struct tzhead tzh
;
1473 zic_t ats
[TZ_MAX_TIMES
];
1474 unsigned char types
[TZ_MAX_TIMES
];
1480 (void) qsort((void *)attypes
, (size_t)timecnt
,
1481 (size_t)sizeof (*attypes
), atcomp
);
1491 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1494 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1495 ++fromi
; /* handled by default rule */
1496 for (; fromi
< timecnt
; ++fromi
) {
1497 if (toi
!= 0 && ((attypes
[fromi
].at
+
1498 gmtoffs
[attypes
[toi
- 1].type
]) <=
1499 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1500 : attypes
[toi
- 2].type
]))) {
1501 attypes
[toi
- 1].type
=
1502 attypes
[fromi
].type
;
1506 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1507 attypes
[toi
++] = attypes
[fromi
];
1514 for (i
= 0; i
< timecnt
; ++i
) {
1515 ats
[i
] = attypes
[i
].at
;
1516 types
[i
] = attypes
[i
].type
;
1518 fullname
= erealloc(fullname
,
1519 (int)(strlen(directory
) + 1 + strlen(name
) + 1));
1520 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1522 * Remove old file, if any, to snap links.
1524 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1525 const char *e
= strerror(errno
);
1527 (void) fprintf(stderr
, gettext("%s: Can't remove %s: %s\n"),
1528 progname
, fullname
, e
);
1531 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1532 if (mkdirs(fullname
) != 0)
1534 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1535 const char *e
= strerror(errno
);
1536 (void) fprintf(stderr
, gettext(
1537 "%s: Can't create %s: %s\n"),
1538 progname
, fullname
, e
);
1542 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1543 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1544 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1545 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1546 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1547 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1548 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof (tzh
.tzh_magic
));
1549 #define DO(field) (void) fwrite((void *) tzh.field, \
1550 (size_t)sizeof (tzh.field), (size_t)1, fp)
1560 for (i
= 0; i
< timecnt
; ++i
) {
1563 if (ats
[i
] >= trans
[j
]) {
1564 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1567 puttzcode((long)ats
[i
], fp
);
1570 (void) fwrite((void *)types
, (size_t)sizeof (types
[0]),
1571 (size_t)timecnt
, fp
);
1572 for (i
= 0; i
< typecnt
; ++i
) {
1573 puttzcode((long)gmtoffs
[i
], fp
);
1574 (void) putc(isdsts
[i
], fp
);
1575 (void) putc(abbrinds
[i
], fp
);
1578 (void) fwrite((void *)chars
, (size_t)sizeof (chars
[0]),
1579 (size_t)charcnt
, fp
);
1580 for (i
= 0; i
< leapcnt
; ++i
) {
1582 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1585 if (++j
>= typecnt
) {
1591 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1595 puttzcode((long)tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1596 } else puttzcode((long)trans
[i
], fp
);
1597 puttzcode((long)corr
[i
], fp
);
1599 for (i
= 0; i
< typecnt
; ++i
)
1600 (void) putc(ttisstds
[i
], fp
);
1601 for (i
= 0; i
< typecnt
; ++i
)
1602 (void) putc(ttisgmts
[i
], fp
);
1603 if (ferror(fp
) || fclose(fp
)) {
1604 (void) fprintf(stderr
, gettext("%s: Error writing %s\n"),
1605 progname
, fullname
);
1611 doabbr(abbr
, format
, letters
, isdst
)
1613 const char * const format
;
1614 const char * const letters
;
1617 if (strchr(format
, '/') == NULL
) {
1618 if (letters
== NULL
)
1619 (void) strcpy(abbr
, format
);
1621 (void) sprintf(abbr
, format
, letters
);
1623 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1625 (void) strcpy(abbr
, format
);
1626 *strchr(abbr
, '/') = '\0';
1631 outzone(zpfirst
, zonecount
)
1632 const struct zone
* const zpfirst
;
1633 const int zonecount
;
1635 register const struct zone
*zp
;
1636 register struct rule
*rp
;
1638 register int usestart
, useuntil
;
1639 register zic_t starttime
, untiltime
;
1640 register long gmtoff
;
1641 register long stdoff
;
1643 register long startoff
;
1644 register int startttisstd
;
1645 register int startttisgmt
;
1647 char startbuf
[BUFSIZ
];
1649 INITIALIZE(untiltime
);
1650 INITIALIZE(starttime
);
1652 * Now. . .finally. . .generate some useful data!
1658 * Thanks to Earl Chew
1659 * for noting the need to unconditionally initialize startttisstd.
1661 startttisstd
= FALSE
;
1662 startttisgmt
= FALSE
;
1663 for (i
= 0; i
< zonecount
; ++i
) {
1665 * A guess that may well be corrected later.
1669 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1670 useuntil
= i
< (zonecount
- 1);
1671 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1673 gmtoff
= zp
->z_gmtoff
;
1674 eat(zp
->z_filename
, zp
->z_linenum
);
1676 startoff
= zp
->z_gmtoff
;
1677 if (zp
->z_nrules
== 0) {
1678 stdoff
= zp
->z_stdoff
;
1679 doabbr(startbuf
, zp
->z_format
,
1680 (char *)NULL
, stdoff
!= 0);
1681 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1682 startbuf
, stdoff
!= 0, startttisstd
,
1685 addtt(starttime
, type
);
1687 } else if (stdoff
!= 0)
1688 addtt(min_time
, type
);
1690 for (year
= min_year
; year
<= max_year
; ++year
) {
1691 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1694 * Mark which rules to do in the current year.
1695 * For those to do, calculate rpytime(rp, year);
1697 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1698 rp
= &zp
->z_rules
[j
];
1699 eats(zp
->z_filename
, zp
->z_linenum
,
1700 rp
->r_filename
, rp
->r_linenum
);
1701 rp
->r_todo
= year
>= rp
->r_loyear
&&
1702 year
<= rp
->r_hiyear
&&
1703 yearistype(year
, rp
->r_yrtype
);
1705 rp
->r_temp
= rpytime(rp
, year
);
1709 register zic_t jtime
, ktime
;
1710 register long offset
;
1716 * Turn untiltime into UTC * assuming
1717 * the current gmtoff and stdoff values.
1719 untiltime
= zp
->z_untiltime
;
1720 if (!zp
->z_untilrule
.r_todisgmt
)
1721 untiltime
= tadd(untiltime
,
1723 if (!zp
->z_untilrule
.r_todisstd
)
1724 untiltime
= tadd(untiltime
,
1728 * Find the rule (of those to do, if any)
1729 * that takes effect earliest in the year.
1732 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1733 rp
= &zp
->z_rules
[j
];
1736 eats(zp
->z_filename
, zp
->z_linenum
,
1737 rp
->r_filename
, rp
->r_linenum
);
1738 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1739 if (!rp
->r_todisstd
)
1740 offset
= oadd(offset
, stdoff
);
1742 if (jtime
== min_time
||
1745 jtime
= tadd(jtime
, -offset
);
1746 if (k
< 0 || jtime
< ktime
) {
1752 break; /* go on to next year */
1753 rp
= &zp
->z_rules
[k
];
1755 if (useuntil
&& ktime
>= untiltime
)
1757 stdoff
= rp
->r_stdoff
;
1758 if (usestart
&& ktime
== starttime
)
1761 if (ktime
< starttime
) {
1762 startoff
= oadd(zp
->z_gmtoff
,
1764 doabbr(startbuf
, zp
->z_format
,
1769 if (*startbuf
== '\0' &&
1770 startoff
== oadd(zp
->z_gmtoff
,
1772 doabbr(startbuf
, zp
->z_format
,
1777 eats(zp
->z_filename
, zp
->z_linenum
,
1778 rp
->r_filename
, rp
->r_linenum
);
1779 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1781 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1782 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1783 rp
->r_todisstd
, rp
->r_todisgmt
);
1788 if (*startbuf
== '\0' &&
1789 zp
->z_format
!= NULL
&&
1790 strchr(zp
->z_format
, '%') == NULL
&&
1791 strchr(zp
->z_format
, '/') == NULL
)
1792 (void) strcpy(startbuf
, zp
->z_format
);
1793 eat(zp
->z_filename
, zp
->z_linenum
);
1794 if (*startbuf
== '\0')
1796 "can't determine time zone abbrevation to use just after until time"));
1797 else addtt(starttime
,
1798 addtype(startoff
, startbuf
,
1799 startoff
!= zp
->z_gmtoff
,
1804 * Now we may get to set starttime for the next zone line.
1807 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1808 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1809 starttime
= zp
->z_untiltime
;
1811 starttime
= tadd(starttime
, -stdoff
);
1813 starttime
= tadd(starttime
, -gmtoff
);
1816 writezone(zpfirst
->z_name
);
1820 addtt(starttime
, type
)
1821 const zic_t starttime
;
1824 if (starttime
<= min_time
||
1825 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1826 gmtoffs
[0] = gmtoffs
[type
];
1827 isdsts
[0] = isdsts
[type
];
1828 ttisstds
[0] = ttisstds
[type
];
1829 ttisgmts
[0] = ttisgmts
[type
];
1830 if (abbrinds
[type
] != 0)
1831 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1833 charcnt
= strlen(chars
) + 1;
1838 if (timecnt
>= TZ_MAX_TIMES
) {
1839 error(gettext("too many transitions?!"));
1842 attypes
[timecnt
].at
= starttime
;
1843 attypes
[timecnt
].type
= type
;
1848 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1850 const char * const abbr
;
1857 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1859 "internal error - addtype called with bad isdst"));
1862 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1864 "internal error - addtype called with bad ttisstd"));
1867 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1869 "internal error - addtype called with bad ttisgmt"));
1873 * See if there's already an entry for this zone type.
1874 * If so, just return its index.
1876 for (i
= 0; i
< typecnt
; ++i
) {
1877 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1878 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1879 ttisstd
== ttisstds
[i
] &&
1880 ttisgmt
== ttisgmts
[i
])
1884 * There isn't one; add a new one, unless there are already too
1887 if (typecnt
>= TZ_MAX_TYPES
) {
1888 error(gettext("too many local time types"));
1891 if (!(-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
1892 error(gettext("UTC offset out of range"));
1895 gmtoffs
[i
] = gmtoff
;
1897 ttisstds
[i
] = ttisstd
;
1898 ttisgmts
[i
] = ttisgmt
;
1900 for (j
= 0; j
< charcnt
; ++j
)
1901 if (strcmp(&chars
[j
], abbr
) == 0)
1910 #ifdef LEAPSECOND_SUPPORT
1912 leapadd(t
, positive
, rolling
, count
)
1920 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1921 error(gettext("too many leap seconds"));
1924 for (i
= 0; i
< leapcnt
; ++i
)
1925 if (t
<= trans
[i
]) {
1926 if (t
== trans
[i
]) {
1927 error(gettext("repeated leap second moment"));
1933 for (j
= leapcnt
; j
> i
; --j
) {
1934 trans
[j
] = trans
[j
- 1];
1935 corr
[j
] = corr
[j
- 1];
1936 roll
[j
] = roll
[j
- 1];
1939 corr
[i
] = positive
? 1L : eitol(-count
);
1942 } while (positive
&& --count
!= 0);
1944 #endif /* LEAPSECOND_SUPPORT */
1946 #ifdef LEAPSECOND_SUPPORT
1951 register long last
= 0;
1954 * propagate leap seconds forward
1956 for (i
= 0; i
< leapcnt
; ++i
) {
1957 trans
[i
] = tadd(trans
[i
], last
);
1958 last
= corr
[i
] += last
;
1961 #endif /* LEAPSECOND_SUPPORT */
1964 yearistype(year
, type
)
1966 const char * const type
;
1971 if (type
== NULL
|| *type
== '\0')
1974 if (strcmp(type
, "uspres") == 0)
1975 return ((year
% 4) == 0);
1976 if (strcmp(type
, "nonpres") == 0)
1977 return ((year
% 4) != 0);
1978 if (strcmp(type
, "even") == 0)
1979 return ((year
% 2) == 0);
1980 if (strcmp(type
, "odd") == 0)
1981 return ((year
% 2) != 0);
1982 #endif /* defined(sun) */
1984 buf
= erealloc(buf
, (int)(132 + strlen(yitcommand
) + strlen(type
)));
1985 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1986 result
= system(buf
);
1987 if (WIFEXITED(result
)) {
1988 switch (WEXITSTATUS(result
)) {
1995 error(gettext("Wild result from command execution"));
1996 (void) fprintf(stderr
, gettext("%s: command was '%s', result was %d\n"),
1997 progname
, buf
, result
);
2006 a
= (unsigned char) a
;
2007 return ((isascii(a
) && isupper(a
)) ? tolower(a
) : a
);
2011 ciequal(ap
, bp
) /* case-insensitive equality */
2012 register const char *ap
;
2013 register const char *bp
;
2015 while (lowerit(*ap
) == lowerit(*bp
++))
2023 register const char *abbr
;
2024 register const char *word
;
2026 if (lowerit(*abbr
) != lowerit(*word
))
2029 while (*++abbr
!= '\0')
2033 } while (lowerit(*word
++) != lowerit(*abbr
));
2037 static const struct lookup
*
2039 register const char * const word
;
2040 register const struct lookup
* const table
;
2042 register const struct lookup
*foundlp
;
2043 register const struct lookup
*lp
;
2045 if (word
== NULL
|| table
== NULL
)
2048 * Look for exact match.
2050 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2051 if (ciequal(word
, lp
->l_word
))
2054 * Look for inexact match.
2057 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2058 if (itsabbr(word
, lp
->l_word
)) {
2059 if (foundlp
== NULL
)
2061 else return (NULL
); /* multiple inexact matches */
2071 register char **array
;
2076 array
= (char **)(void *)
2077 emalloc((int)((strlen(cp
) + 1) * sizeof (*array
)));
2080 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2082 if (*cp
== '\0' || *cp
== '#')
2084 array
[nsubs
++] = dp
= cp
;
2086 if ((*dp
= *cp
++) != '"')
2088 else while ((*dp
= *cp
++) != '"')
2093 "Odd number of quotation marks"));
2096 } while (*cp
!= '\0' && *cp
!= '#' &&
2097 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2098 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2102 array
[nsubs
] = NULL
;
2114 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2115 error(gettext("time overflow"));
2128 if (t1
== max_time
&& t2
> 0)
2130 if (t1
== min_time
&& t2
< 0)
2133 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2134 error(gettext("time overflow"));
2141 * Given a rule, and a year, compute the date - in seconds since January 1,
2142 * 1970, 00:00 LOCAL time - in that year that the rule refers to.
2146 rpytime(rp
, wantedy
)
2147 register const struct rule
* const rp
;
2148 register const int wantedy
;
2150 register int y
, m
, i
;
2151 register long dayoff
; /* with a nod to Margaret O. */
2154 if (wantedy
== INT_MIN
)
2156 if (wantedy
== INT_MAX
)
2161 while (wantedy
!= y
) {
2163 i
= len_years
[isleap(y
)];
2167 i
= -len_years
[isleap(y
)];
2169 dayoff
= oadd(dayoff
, eitol(i
));
2171 while (m
!= rp
->r_month
) {
2172 i
= len_months
[isleap(y
)][m
];
2173 dayoff
= oadd(dayoff
, eitol(i
));
2176 i
= rp
->r_dayofmonth
;
2177 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2178 if (rp
->r_dycode
== DC_DOWLEQ
)
2181 error(gettext("use of 2/29 in non leap-year"));
2186 dayoff
= oadd(dayoff
, eitol(i
));
2187 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2190 #define LDAYSPERWEEK ((long)DAYSPERWEEK)
2191 wday
= eitol(EPOCH_WDAY
);
2193 * Don't trust mod of negative numbers.
2196 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2198 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2200 wday
+= LDAYSPERWEEK
;
2202 while (wday
!= eitol(rp
->r_wday
))
2203 if (rp
->r_dycode
== DC_DOWGEQ
) {
2204 dayoff
= oadd(dayoff
, (long)1);
2205 if (++wday
>= LDAYSPERWEEK
)
2209 dayoff
= oadd(dayoff
, (long)-1);
2211 wday
= LDAYSPERWEEK
- 1;
2214 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2216 warning(gettext("rule goes past start/end of "
2217 "month--will not work with pre-2004 "
2218 "versions of zic"));
2221 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
))
2223 if (dayoff
< min_time
/ SECSPERDAY
)
2225 if (dayoff
> max_time
/ SECSPERDAY
)
2227 t
= (zic_t
)dayoff
* SECSPERDAY
;
2228 return (tadd(t
, rp
->r_tod
));
2232 newabbr(const char * const string
)
2236 if (strcmp(string
, GRANDPARENTED
) != 0) {
2237 register const char *cp
;
2242 while (isalpha(*cp
) || ('0' <= *cp
&& *cp
<= '9') ||
2243 *cp
== '-' || *cp
== '+') {
2246 if (noise
&& cp
- string
< 3)
2247 wp
= gettext(("time zone abbreviation has less than 3 "
2249 if (wp
== NULL
&& cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2250 wp
= gettext(("time zone abbreviation has too many "
2252 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2254 if (isascii(*cp
) && isdigit(*cp
))
2255 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
2258 if (wp
== NULL
&& *cp
!= '\0')
2259 wp
= gettext("time zone abbreviation differs from "
2263 wp
= ecatalloc(wp
, " (");
2264 wp
= ecatalloc(wp
, string
);
2265 wp
= ecatalloc(wp
, ")");
2270 i
= strlen(string
) + 1;
2271 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2272 error(gettext("too many, or too long, time zone "
2276 (void) strcpy(&chars
[charcnt
], string
);
2277 charcnt
+= eitol(i
);
2284 register char *name
;
2287 if (argname
== NULL
|| *argname
== '\0')
2289 cp
= name
= ecpyalloc(argname
);
2290 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2292 if (!itsdir(name
)) {
2294 * It doesn't seem to exist, so we try to create it.
2295 * Creation may fail because of the directory being
2296 * created by some other multiprocessor, so we get
2297 * to do extra checking.
2299 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2300 const char *e
= strerror(errno
);
2302 if (errno
!= EEXIST
|| !itsdir(name
)) {
2303 (void) fprintf(stderr
, gettext(
2304 "%s: Can't create directory %s: %s\n"),
2324 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2325 (void) fprintf(stderr
,
2326 gettext("%s: %d did not sign extend correctly\n"),
2334 * UNIX was a registered trademark of The Open Group in 2003.