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 */
445 static const char *psxrules
;
446 static const char *lcltime
;
447 static const char *directory
;
448 static const char *leapsec
;
449 static const char *yitcommand
;
450 static int sflag
= FALSE
;
461 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
463 (void) setlocale(LC_ALL
, "");
464 #if !defined(TEXT_DOMAIN)
465 #define TEXT_DOMAIN "SYS_TEST"
467 (void) textdomain(TEXT_DOMAIN
);
470 for (i
= 1; i
< argc
; ++i
)
471 if (strcmp(argv
[i
], "--version") == 0) {
472 (void) printf("%s\n", elsieid
);
476 #ifdef LEAPSECOND_SUPPORT
477 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
)
479 while ((c
= getopt(argc
, argv
, "d:l:p:vsy:")) != EOF
)
486 if (directory
== NULL
)
489 (void) fprintf(stderr
, gettext(
490 "%s: More than one -d option specified\n"),
499 (void) fprintf(stderr
, gettext(
500 "%s: More than one -l option specified\n"),
506 if (psxrules
== NULL
)
509 (void) fprintf(stderr
, gettext(
510 "%s: More than one -p option specified\n"),
516 if (yitcommand
== NULL
)
519 (void) fprintf(stderr
, gettext(
520 "%s: More than one -y option specified\n"),
525 #ifdef LEAPSECOND_SUPPORT
530 (void) fprintf(stderr
, gettext(
531 "%s: More than one -L option specified\n"),
536 #endif /* LEAPSECOND_SUPPORT */
544 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
545 usage(); /* usage message by request */
546 if (directory
== NULL
)
548 if (yitcommand
== NULL
)
549 yitcommand
= "yearistype";
553 #ifdef LEAPSECOND_SUPPORT
554 if (optind
< argc
&& leapsec
!= NULL
) {
558 #endif /* LEAPSECOND_SUPPORT */
560 for (i
= optind
; i
< argc
; ++i
)
565 for (i
= 0; i
< nzones
; i
= j
) {
567 * Find the next non-continuation zone entry.
569 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
571 outzone(&zones
[i
], j
- i
);
576 for (i
= 0; i
< nlinks
; ++i
) {
577 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
578 dolink(links
[i
].l_from
, links
[i
].l_to
);
580 for (j
= 0; j
< nlinks
; ++j
)
581 if (strcmp(links
[i
].l_to
, links
[j
].l_from
) == 0)
582 warning(gettext("link to link"));
584 if (lcltime
!= NULL
) {
585 eat("command line", 1);
586 dolink(lcltime
, TZDEFAULT
);
588 if (psxrules
!= NULL
) {
589 eat("command line", 1);
590 dolink(psxrules
, TZDEFRULES
);
592 return ((errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
);
596 dolink(fromfield
, tofield
)
597 const char * const fromfield
;
598 const char * const tofield
;
600 register char *fromname
;
601 register char *toname
;
603 if (fromfield
[0] == '/')
604 fromname
= ecpyalloc(fromfield
);
606 fromname
= ecpyalloc(directory
);
607 fromname
= ecatalloc(fromname
, "/");
608 fromname
= ecatalloc(fromname
, fromfield
);
610 if (tofield
[0] == '/')
611 toname
= ecpyalloc(tofield
);
613 toname
= ecpyalloc(directory
);
614 toname
= ecatalloc(toname
, "/");
615 toname
= ecatalloc(toname
, tofield
);
618 * We get to be careful here since
619 * there's a fair chance of root running us.
622 (void) remove(toname
);
623 if (link(fromname
, toname
) != 0) {
626 if (mkdirs(toname
) != 0)
629 result
= link(fromname
, toname
);
631 if (result
!= 0 && access(fromname
, F_OK
) == 0 &&
633 const char *s
= tofield
;
634 register char *symlinkcontents
= NULL
;
636 while ((s
= strchr(s
+1, '/')) != NULL
)
637 symlinkcontents
= ecatalloc(symlinkcontents
,
639 symlinkcontents
= ecatalloc(symlinkcontents
, fromname
);
640 result
= symlink(symlinkcontents
, toname
);
643 "hard link failed, symbolic link used"));
644 ifree(symlinkcontents
);
648 const char *e
= strerror(errno
);
650 (void) fprintf(stderr
, gettext(
651 "%s: Can't link from %s to %s: %s\n"),
652 progname
, fromname
, toname
, e
);
661 #define INT_MAX ((int)(((unsigned)~0)>>1))
662 #endif /* !defined INT_MAX */
665 #define INT_MIN ((int)~(((unsigned)~0)>>1))
666 #endif /* !defined INT_MIN */
669 * The tz file format currently allows at most 32-bit quantities.
670 * This restriction should be removed before signed 32-bit values
671 * wrap around in 2038, but unfortunately this will require a
672 * change to the tz file format.
675 #define MAX_BITS_IN_FILE 32
677 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
678 TYPE_BIT(zic_t): MAX_BITS_IN_FILE)
685 if (TYPE_SIGNED(zic_t
)) {
687 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
689 max_time
= -(min_time
+ 1);
694 max_time
= 2 - sflag
;
695 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
702 t
= (time_t)min_time
;
703 min_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
704 t
= (time_t)max_time
;
705 max_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
707 min_year_representable
= min_year
;
708 max_year_representable
= max_year
;
713 const char * const name
;
715 register char *myname
;
718 myname
= ecpyalloc(name
);
719 myname
= ecatalloc(myname
, "/.");
720 accres
= access(myname
, F_OK
);
722 return (accres
== 0);
726 * Associate sets of rules with zones.
738 return (strcmp(((const struct rule
*) cp1
)->r_name
,
739 ((const struct rule
*) cp2
)->r_name
));
745 register struct zone
*zp
;
746 register struct rule
*rp
;
747 register int base
, out
;
751 (void) qsort((void *)rules
, (size_t)nrules
,
752 (size_t)sizeof (*rules
), rcomp
);
753 for (i
= 0; i
< nrules
- 1; ++i
) {
754 if (strcmp(rules
[i
].r_name
,
755 rules
[i
+ 1].r_name
) != 0)
757 if (strcmp(rules
[i
].r_filename
,
758 rules
[i
+ 1].r_filename
) == 0)
760 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
761 warning(gettext("same rule name in multiple files"));
762 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
763 warning(gettext("same rule name in multiple files"));
764 for (j
= i
+ 2; j
< nrules
; ++j
) {
765 if (strcmp(rules
[i
].r_name
,
766 rules
[j
].r_name
) != 0)
768 if (strcmp(rules
[i
].r_filename
,
769 rules
[j
].r_filename
) == 0)
771 if (strcmp(rules
[i
+ 1].r_filename
,
772 rules
[j
].r_filename
) == 0)
779 for (i
= 0; i
< nzones
; ++i
) {
784 for (base
= 0; base
< nrules
; base
= out
) {
786 for (out
= base
+ 1; out
< nrules
; ++out
)
787 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
789 for (i
= 0; i
< nzones
; ++i
) {
791 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
794 zp
->z_nrules
= out
- base
;
797 for (i
= 0; i
< nzones
; ++i
) {
799 if (zp
->z_nrules
== 0) {
801 * Maybe we have a local standard time offset.
803 eat(zp
->z_filename
, zp
->z_linenum
);
804 zp
->z_stdoff
= gethms(zp
->z_rule
,
805 gettext("unruly zone"), TRUE
);
807 * Note, though, that if there's no rule,
808 * a '%s' in the format is a bad thing.
810 if (strchr(zp
->z_format
, '%') != 0)
811 error(gettext("%s in ruleless zone"));
823 register char **fields
;
825 register const struct lookup
*lp
;
826 register int nfields
;
827 register int wantcont
;
831 if (strcmp(name
, "-") == 0) {
832 name
= gettext("standard input");
834 } else if ((fp
= fopen(name
, "r")) == NULL
) {
835 const char *e
= strerror(errno
);
837 (void) fprintf(stderr
, gettext("%s: Can't open %s: %s\n"),
842 for (num
= 1; ; ++num
) {
844 if (fgets(buf
, (int)sizeof (buf
), fp
) != buf
)
846 cp
= strchr(buf
, '\n');
848 error(gettext("line too long"));
852 fields
= getfields(buf
);
854 while (fields
[nfields
] != NULL
) {
857 if (strcmp(fields
[nfields
], "-") == 0)
858 fields
[nfields
] = &nada
;
863 } else if (wantcont
) {
864 wantcont
= inzcont(fields
, nfields
);
866 lp
= byword(fields
[0], line_codes
);
868 error(gettext("input line of unknown type"));
869 else switch ((int)(lp
->l_value
)) {
871 inrule(fields
, nfields
);
875 wantcont
= inzone(fields
, nfields
);
878 inlink(fields
, nfields
);
881 #ifdef LEAPSECOND_SUPPORT
884 (void) fprintf(stderr
, gettext(
885 "%s: Leap line in non leap seconds file %s\n"),
887 else inleap(fields
, nfields
);
890 #endif /* LEAPSECOND_SUPPORT */
891 default: /* "cannot happen" */
892 (void) fprintf(stderr
, gettext(
893 "%s: panic: Invalid l_value %d\n"),
894 progname
, lp
->l_value
);
898 ifree((char *)fields
);
901 (void) fprintf(stderr
, gettext("%s: Error reading %s\n"),
905 if (fp
!= stdin
&& fclose(fp
)) {
906 const char *e
= strerror(errno
);
907 (void) fprintf(stderr
, gettext("%s: Error closing %s: %s\n"),
908 progname
, filename
, e
);
912 error(gettext("expected continuation line not found"));
916 * Convert a string of one of the forms
917 * h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
918 * into a number of seconds.
919 * A null string maps to zero.
920 * Call error with errstring and return zero on errors.
924 gethms(string
, errstring
, signable
)
926 const char * const errstring
;
932 if (string
== NULL
|| *string
== '\0')
936 else if (*string
== '-') {
940 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
942 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
944 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
945 &hh
, &mm
, &ss
) != 3) {
950 mm
< 0 || mm
>= MINSPERHOUR
||
951 ss
< 0 || ss
> SECSPERMIN
) {
955 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
956 error(gettext("time overflow"));
959 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
961 gettext("24:00 not handled by pre-1998 versions of zic"));
962 if (noise
&& (hh
> HOURSPERDAY
||
963 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
964 warning(gettext("values over 24 hours not handled by "
965 "pre-2007 versions of zic"));
967 return (oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
968 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
))));
972 inrule(fields
, nfields
)
973 register char ** const fields
;
976 static struct rule r
;
978 if (nfields
!= RULE_FIELDS
) {
979 error(gettext("wrong number of fields on Rule line"));
982 if (*fields
[RF_NAME
] == '\0') {
983 error(gettext("nameless rule"));
986 r
.r_filename
= filename
;
987 r
.r_linenum
= linenum
;
988 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], gettext("invalid saved time"),
990 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
991 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
992 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
993 r
.r_abbrvar
= ecpyalloc(fields
[RF_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(gettext("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
)));
1014 gettext("\"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
)));
1022 gettext("\"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
)));
1034 gettext("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
) {
1051 "wrong number of fields on Zone continuation line"));
1054 return (inzsub(fields
, nfields
, TRUE
));
1058 inzsub(fields
, nfields
, iscont
)
1059 register char ** const fields
;
1064 static struct zone z
;
1065 register int i_gmtoff
, i_rule
, i_format
;
1066 register int i_untilyear
, i_untilmonth
;
1067 register int i_untilday
, i_untiltime
;
1068 register int hasuntil
;
1071 i_gmtoff
= ZFC_GMTOFF
;
1073 i_format
= ZFC_FORMAT
;
1074 i_untilyear
= ZFC_TILYEAR
;
1075 i_untilmonth
= ZFC_TILMONTH
;
1076 i_untilday
= ZFC_TILDAY
;
1077 i_untiltime
= ZFC_TILTIME
;
1080 i_gmtoff
= ZF_GMTOFF
;
1082 i_format
= ZF_FORMAT
;
1083 i_untilyear
= ZF_TILYEAR
;
1084 i_untilmonth
= ZF_TILMONTH
;
1085 i_untilday
= ZF_TILDAY
;
1086 i_untiltime
= ZF_TILTIME
;
1087 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1089 z
.z_filename
= filename
;
1090 z
.z_linenum
= linenum
;
1091 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], gettext("invalid UTC offset"),
1093 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1094 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1095 error(gettext("invalid abbreviation format"));
1099 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1100 z
.z_format
= ecpyalloc(fields
[i_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"));
1126 zones
= (struct zone
*)(void *)erealloc((char *)zones
,
1127 (int)((nzones
+ 1) * sizeof (*zones
)));
1128 zones
[nzones
++] = z
;
1130 * If there was an UNTIL field on this line,
1131 * there's more information about the zone on the next line.
1136 #ifdef LEAPSECOND_SUPPORT
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(gettext("wrong number of fields on Leap line"));
1154 cp
= fields
[LP_YEAR
];
1155 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1159 error(gettext("invalid leaping year"));
1165 i
= len_years
[isleap(j
)];
1169 i
= -len_years
[isleap(j
)];
1171 dayoff
= oadd(dayoff
, eitol(i
));
1173 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1174 error(gettext("invalid month name"));
1177 month
= lp
->l_value
;
1179 while (j
!= month
) {
1180 i
= len_months
[isleap(year
)][j
];
1181 dayoff
= oadd(dayoff
, eitol(i
));
1184 cp
= fields
[LP_DAY
];
1185 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1186 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1187 error(gettext("invalid day of month"));
1190 dayoff
= oadd(dayoff
, eitol(day
- 1));
1191 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1192 error(gettext("time before zero"));
1195 if (dayoff
< min_time
/ SECSPERDAY
) {
1196 error(gettext("time too small"));
1199 if (dayoff
> max_time
/ SECSPERDAY
) {
1200 error(gettext("time too large"));
1203 t
= (zic_t
)dayoff
* SECSPERDAY
;
1204 tod
= gethms(fields
[LP_TIME
], gettext("invalid time of day"), FALSE
);
1205 cp
= fields
[LP_CORR
];
1207 register int positive
;
1210 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1213 } else if (strcmp(cp
, "--") == 0) {
1216 } else if (strcmp(cp
, "+") == 0) {
1219 } else if (strcmp(cp
, "++") == 0) {
1223 error(gettext("illegal CORRECTION field on Leap line"));
1226 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1228 "illegal Rolling/Stationary field on Leap line"));
1231 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1234 #endif /* LEAPSECOND_SUPPORT */
1237 inlink(fields
, nfields
)
1238 register char ** const fields
;
1243 if (nfields
!= LINK_FIELDS
) {
1244 error(gettext("wrong number of fields on Link line"));
1247 if (*fields
[LF_FROM
] == '\0') {
1248 error(gettext("blank FROM field on Link line"));
1251 if (*fields
[LF_TO
] == '\0') {
1252 error(gettext("blank TO field on Link line"));
1255 l
.l_filename
= filename
;
1256 l
.l_linenum
= linenum
;
1257 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1258 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1259 links
= (struct link
*)(void *)erealloc((char *)links
,
1260 (int)((nlinks
+ 1) * sizeof (*links
)));
1261 links
[nlinks
++] = l
;
1265 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1266 register struct rule
* const rp
;
1267 const char * const loyearp
;
1268 const char * const hiyearp
;
1269 const char * const typep
;
1270 const char * const monthp
;
1271 const char * const dayp
;
1272 const char * const timep
;
1274 register const struct lookup
*lp
;
1275 register const char *cp
;
1279 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1280 error(gettext("invalid month name"));
1283 rp
->r_month
= lp
->l_value
;
1284 rp
->r_todisstd
= FALSE
;
1285 rp
->r_todisgmt
= FALSE
;
1286 dp
= ecpyalloc(timep
);
1288 ep
= dp
+ strlen(dp
) - 1;
1289 switch (lowerit(*ep
)) {
1290 case 's': /* Standard */
1291 rp
->r_todisstd
= TRUE
;
1292 rp
->r_todisgmt
= FALSE
;
1295 case 'w': /* Wall */
1296 rp
->r_todisstd
= FALSE
;
1297 rp
->r_todisgmt
= FALSE
;
1300 case 'g': /* Greenwich */
1301 case 'u': /* Universal */
1302 case 'z': /* Zulu */
1303 rp
->r_todisstd
= TRUE
;
1304 rp
->r_todisgmt
= TRUE
;
1309 rp
->r_tod
= gethms(dp
, gettext("invalid time of day"), FALSE
);
1315 lp
= byword(cp
, begin_years
);
1317 switch ((int)lp
->l_value
) {
1319 rp
->r_loyear
= INT_MIN
;
1322 rp
->r_loyear
= INT_MAX
;
1324 default: /* "cannot happen" */
1325 (void) fprintf(stderr
,
1326 gettext("%s: panic: Invalid l_value %d\n"),
1327 progname
, lp
->l_value
);
1330 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1331 error(gettext("invalid starting year"));
1334 if (rp
->r_loyear
< min_year_representable
)
1336 "starting year too low to be represented"));
1337 else if (rp
->r_loyear
> max_year_representable
)
1339 "starting year too high to be represented"));
1342 if ((lp
= byword(cp
, end_years
)) != NULL
) {
1343 switch ((int)lp
->l_value
) {
1345 rp
->r_hiyear
= INT_MIN
;
1348 rp
->r_hiyear
= INT_MAX
;
1351 rp
->r_hiyear
= rp
->r_loyear
;
1353 default: /* "cannot happen" */
1354 (void) fprintf(stderr
,
1355 gettext("%s: panic: Invalid l_value %d\n"),
1356 progname
, lp
->l_value
);
1359 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1360 error(gettext("invalid ending year"));
1363 if (rp
->r_loyear
< min_year_representable
)
1365 "ending year too low to be represented"));
1366 else if (rp
->r_loyear
> max_year_representable
)
1368 "ending year too high to be represented"));
1370 if (rp
->r_loyear
> rp
->r_hiyear
) {
1371 error(gettext("starting year greater than ending year"));
1375 rp
->r_yrtype
= NULL
;
1377 if (rp
->r_loyear
== rp
->r_hiyear
) {
1378 error(gettext("typed single year"));
1381 rp
->r_yrtype
= ecpyalloc(typep
);
1383 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1384 min_year
= rp
->r_loyear
;
1387 * Accept things such as:
1393 dp
= ecpyalloc(dayp
);
1394 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1395 rp
->r_dycode
= DC_DOWLEQ
;
1396 rp
->r_wday
= lp
->l_value
;
1397 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1399 if ((ep
= strchr(dp
, '<')) != 0)
1400 rp
->r_dycode
= DC_DOWLEQ
;
1401 else if ((ep
= strchr(dp
, '>')) != 0)
1402 rp
->r_dycode
= DC_DOWGEQ
;
1405 rp
->r_dycode
= DC_DOM
;
1407 if (rp
->r_dycode
!= DC_DOM
) {
1410 error(gettext("invalid day of month"));
1414 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1415 error(gettext("invalid weekday name"));
1419 rp
->r_wday
= lp
->l_value
;
1421 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1422 rp
->r_dayofmonth
<= 0 ||
1423 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1424 error(gettext("invalid day of month"));
1438 register long shift
;
1440 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1441 buf
[i
] = val
>> shift
;
1452 (void) fwrite((void *)buf
, (size_t)sizeof (buf
), (size_t)1, fp
);
1460 if (((struct attype
*)avp
)->at
< ((struct attype
*)bvp
)->at
)
1462 else if (((struct attype
*)avp
)->at
> ((struct attype
*)bvp
)->at
)
1469 const char * const name
;
1473 static char *fullname
;
1474 static struct tzhead tzh
;
1475 zic_t ats
[TZ_MAX_TIMES
];
1476 unsigned char types
[TZ_MAX_TIMES
];
1482 (void) qsort((void *)attypes
, (size_t)timecnt
,
1483 (size_t)sizeof (*attypes
), atcomp
);
1493 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1496 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1497 ++fromi
; /* handled by default rule */
1498 for (; fromi
< timecnt
; ++fromi
) {
1499 if (toi
!= 0 && ((attypes
[fromi
].at
+
1500 gmtoffs
[attypes
[toi
- 1].type
]) <=
1501 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1502 : attypes
[toi
- 2].type
]))) {
1503 attypes
[toi
- 1].type
=
1504 attypes
[fromi
].type
;
1508 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1509 attypes
[toi
++] = attypes
[fromi
];
1516 for (i
= 0; i
< timecnt
; ++i
) {
1517 ats
[i
] = attypes
[i
].at
;
1518 types
[i
] = attypes
[i
].type
;
1520 fullname
= erealloc(fullname
,
1521 (int)(strlen(directory
) + 1 + strlen(name
) + 1));
1522 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1524 * Remove old file, if any, to snap links.
1526 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1527 const char *e
= strerror(errno
);
1529 (void) fprintf(stderr
, gettext("%s: Can't remove %s: %s\n"),
1530 progname
, fullname
, e
);
1533 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1534 if (mkdirs(fullname
) != 0)
1536 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1537 const char *e
= strerror(errno
);
1538 (void) fprintf(stderr
, gettext(
1539 "%s: Can't create %s: %s\n"),
1540 progname
, fullname
, e
);
1544 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1545 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1546 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1547 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1548 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1549 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1550 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof (tzh
.tzh_magic
));
1551 #define DO(field) (void) fwrite((void *) tzh.field, \
1552 (size_t)sizeof (tzh.field), (size_t)1, fp)
1562 for (i
= 0; i
< timecnt
; ++i
) {
1565 if (ats
[i
] >= trans
[j
]) {
1566 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1569 puttzcode((long)ats
[i
], fp
);
1572 (void) fwrite((void *)types
, (size_t)sizeof (types
[0]),
1573 (size_t)timecnt
, fp
);
1574 for (i
= 0; i
< typecnt
; ++i
) {
1575 puttzcode((long)gmtoffs
[i
], fp
);
1576 (void) putc(isdsts
[i
], fp
);
1577 (void) putc(abbrinds
[i
], fp
);
1580 (void) fwrite((void *)chars
, (size_t)sizeof (chars
[0]),
1581 (size_t)charcnt
, fp
);
1582 for (i
= 0; i
< leapcnt
; ++i
) {
1584 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1587 if (++j
>= typecnt
) {
1593 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1597 puttzcode((long)tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1598 } else puttzcode((long)trans
[i
], fp
);
1599 puttzcode((long)corr
[i
], fp
);
1601 for (i
= 0; i
< typecnt
; ++i
)
1602 (void) putc(ttisstds
[i
], fp
);
1603 for (i
= 0; i
< typecnt
; ++i
)
1604 (void) putc(ttisgmts
[i
], fp
);
1605 if (ferror(fp
) || fclose(fp
)) {
1606 (void) fprintf(stderr
, gettext("%s: Error writing %s\n"),
1607 progname
, fullname
);
1613 doabbr(abbr
, format
, letters
, isdst
)
1615 const char * const format
;
1616 const char * const letters
;
1619 if (strchr(format
, '/') == NULL
) {
1620 if (letters
== NULL
)
1621 (void) strcpy(abbr
, format
);
1623 (void) sprintf(abbr
, format
, letters
);
1625 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1627 (void) strcpy(abbr
, format
);
1628 *strchr(abbr
, '/') = '\0';
1633 outzone(zpfirst
, zonecount
)
1634 const struct zone
* const zpfirst
;
1635 const int zonecount
;
1637 register const struct zone
*zp
;
1638 register struct rule
*rp
;
1640 register int usestart
, useuntil
;
1641 register zic_t starttime
, untiltime
;
1642 register long gmtoff
;
1643 register long stdoff
;
1645 register long startoff
;
1646 register int startttisstd
;
1647 register int startttisgmt
;
1649 char startbuf
[BUFSIZ
];
1651 INITIALIZE(untiltime
);
1652 INITIALIZE(starttime
);
1654 * Now. . .finally. . .generate some useful data!
1660 * Thanks to Earl Chew
1661 * for noting the need to unconditionally initialize startttisstd.
1663 startttisstd
= FALSE
;
1664 startttisgmt
= FALSE
;
1665 for (i
= 0; i
< zonecount
; ++i
) {
1667 * A guess that may well be corrected later.
1671 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1672 useuntil
= i
< (zonecount
- 1);
1673 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1675 gmtoff
= zp
->z_gmtoff
;
1676 eat(zp
->z_filename
, zp
->z_linenum
);
1678 startoff
= zp
->z_gmtoff
;
1679 if (zp
->z_nrules
== 0) {
1680 stdoff
= zp
->z_stdoff
;
1681 doabbr(startbuf
, zp
->z_format
,
1682 (char *)NULL
, stdoff
!= 0);
1683 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1684 startbuf
, stdoff
!= 0, startttisstd
,
1687 addtt(starttime
, type
);
1689 } else if (stdoff
!= 0)
1690 addtt(min_time
, type
);
1692 for (year
= min_year
; year
<= max_year
; ++year
) {
1693 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1696 * Mark which rules to do in the current year.
1697 * For those to do, calculate rpytime(rp, year);
1699 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1700 rp
= &zp
->z_rules
[j
];
1701 eats(zp
->z_filename
, zp
->z_linenum
,
1702 rp
->r_filename
, rp
->r_linenum
);
1703 rp
->r_todo
= year
>= rp
->r_loyear
&&
1704 year
<= rp
->r_hiyear
&&
1705 yearistype(year
, rp
->r_yrtype
);
1707 rp
->r_temp
= rpytime(rp
, year
);
1711 register zic_t jtime
, ktime
;
1712 register long offset
;
1718 * Turn untiltime into UTC * assuming
1719 * the current gmtoff and stdoff values.
1721 untiltime
= zp
->z_untiltime
;
1722 if (!zp
->z_untilrule
.r_todisgmt
)
1723 untiltime
= tadd(untiltime
,
1725 if (!zp
->z_untilrule
.r_todisstd
)
1726 untiltime
= tadd(untiltime
,
1730 * Find the rule (of those to do, if any)
1731 * that takes effect earliest in the year.
1734 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1735 rp
= &zp
->z_rules
[j
];
1738 eats(zp
->z_filename
, zp
->z_linenum
,
1739 rp
->r_filename
, rp
->r_linenum
);
1740 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1741 if (!rp
->r_todisstd
)
1742 offset
= oadd(offset
, stdoff
);
1744 if (jtime
== min_time
||
1747 jtime
= tadd(jtime
, -offset
);
1748 if (k
< 0 || jtime
< ktime
) {
1754 break; /* go on to next year */
1755 rp
= &zp
->z_rules
[k
];
1757 if (useuntil
&& ktime
>= untiltime
)
1759 stdoff
= rp
->r_stdoff
;
1760 if (usestart
&& ktime
== starttime
)
1763 if (ktime
< starttime
) {
1764 startoff
= oadd(zp
->z_gmtoff
,
1766 doabbr(startbuf
, zp
->z_format
,
1771 if (*startbuf
== '\0' &&
1772 startoff
== oadd(zp
->z_gmtoff
,
1774 doabbr(startbuf
, zp
->z_format
,
1779 eats(zp
->z_filename
, zp
->z_linenum
,
1780 rp
->r_filename
, rp
->r_linenum
);
1781 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1783 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1784 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1785 rp
->r_todisstd
, rp
->r_todisgmt
);
1790 if (*startbuf
== '\0' &&
1791 zp
->z_format
!= NULL
&&
1792 strchr(zp
->z_format
, '%') == NULL
&&
1793 strchr(zp
->z_format
, '/') == NULL
)
1794 (void) strcpy(startbuf
, zp
->z_format
);
1795 eat(zp
->z_filename
, zp
->z_linenum
);
1796 if (*startbuf
== '\0')
1798 "can't determine time zone abbrevation to use just after until time"));
1799 else addtt(starttime
,
1800 addtype(startoff
, startbuf
,
1801 startoff
!= zp
->z_gmtoff
,
1806 * Now we may get to set starttime for the next zone line.
1809 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1810 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1811 starttime
= zp
->z_untiltime
;
1813 starttime
= tadd(starttime
, -stdoff
);
1815 starttime
= tadd(starttime
, -gmtoff
);
1818 writezone(zpfirst
->z_name
);
1822 addtt(starttime
, type
)
1823 const zic_t starttime
;
1826 if (starttime
<= min_time
||
1827 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1828 gmtoffs
[0] = gmtoffs
[type
];
1829 isdsts
[0] = isdsts
[type
];
1830 ttisstds
[0] = ttisstds
[type
];
1831 ttisgmts
[0] = ttisgmts
[type
];
1832 if (abbrinds
[type
] != 0)
1833 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1835 charcnt
= strlen(chars
) + 1;
1840 if (timecnt
>= TZ_MAX_TIMES
) {
1841 error(gettext("too many transitions?!"));
1844 attypes
[timecnt
].at
= starttime
;
1845 attypes
[timecnt
].type
= type
;
1850 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1852 const char * const abbr
;
1859 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1861 "internal error - addtype called with bad isdst"));
1864 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1866 "internal error - addtype called with bad ttisstd"));
1869 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1871 "internal error - addtype called with bad ttisgmt"));
1875 * See if there's already an entry for this zone type.
1876 * If so, just return its index.
1878 for (i
= 0; i
< typecnt
; ++i
) {
1879 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1880 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1881 ttisstd
== ttisstds
[i
] &&
1882 ttisgmt
== ttisgmts
[i
])
1886 * There isn't one; add a new one, unless there are already too
1889 if (typecnt
>= TZ_MAX_TYPES
) {
1890 error(gettext("too many local time types"));
1893 if (!(-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
1894 error(gettext("UTC offset out of range"));
1897 gmtoffs
[i
] = gmtoff
;
1899 ttisstds
[i
] = ttisstd
;
1900 ttisgmts
[i
] = ttisgmt
;
1902 for (j
= 0; j
< charcnt
; ++j
)
1903 if (strcmp(&chars
[j
], abbr
) == 0)
1912 #ifdef LEAPSECOND_SUPPORT
1914 leapadd(t
, positive
, rolling
, count
)
1922 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1923 error(gettext("too many leap seconds"));
1926 for (i
= 0; i
< leapcnt
; ++i
)
1927 if (t
<= trans
[i
]) {
1928 if (t
== trans
[i
]) {
1929 error(gettext("repeated leap second moment"));
1935 for (j
= leapcnt
; j
> i
; --j
) {
1936 trans
[j
] = trans
[j
- 1];
1937 corr
[j
] = corr
[j
- 1];
1938 roll
[j
] = roll
[j
- 1];
1941 corr
[i
] = positive
? 1L : eitol(-count
);
1944 } while (positive
&& --count
!= 0);
1946 #endif /* LEAPSECOND_SUPPORT */
1948 #ifdef LEAPSECOND_SUPPORT
1953 register long last
= 0;
1956 * propagate leap seconds forward
1958 for (i
= 0; i
< leapcnt
; ++i
) {
1959 trans
[i
] = tadd(trans
[i
], last
);
1960 last
= corr
[i
] += last
;
1963 #endif /* LEAPSECOND_SUPPORT */
1966 yearistype(year
, type
)
1968 const char * const type
;
1973 if (type
== NULL
|| *type
== '\0')
1976 if (strcmp(type
, "uspres") == 0)
1977 return ((year
% 4) == 0);
1978 if (strcmp(type
, "nonpres") == 0)
1979 return ((year
% 4) != 0);
1980 if (strcmp(type
, "even") == 0)
1981 return ((year
% 2) == 0);
1982 if (strcmp(type
, "odd") == 0)
1983 return ((year
% 2) != 0);
1984 #endif /* defined(sun) */
1986 buf
= erealloc(buf
, (int)(132 + strlen(yitcommand
) + strlen(type
)));
1987 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1988 result
= system(buf
);
1989 if (WIFEXITED(result
)) {
1990 switch (WEXITSTATUS(result
)) {
1997 error(gettext("Wild result from command execution"));
1998 (void) fprintf(stderr
, gettext("%s: command was '%s', result was %d\n"),
1999 progname
, buf
, result
);
2008 a
= (unsigned char) a
;
2009 return ((isascii(a
) && isupper(a
)) ? tolower(a
) : a
);
2013 ciequal(ap
, bp
) /* case-insensitive equality */
2014 register const char *ap
;
2015 register const char *bp
;
2017 while (lowerit(*ap
) == lowerit(*bp
++))
2025 register const char *abbr
;
2026 register const char *word
;
2028 if (lowerit(*abbr
) != lowerit(*word
))
2031 while (*++abbr
!= '\0')
2035 } while (lowerit(*word
++) != lowerit(*abbr
));
2039 static const struct lookup
*
2041 register const char * const word
;
2042 register const struct lookup
* const table
;
2044 register const struct lookup
*foundlp
;
2045 register const struct lookup
*lp
;
2047 if (word
== NULL
|| table
== NULL
)
2050 * Look for exact match.
2052 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2053 if (ciequal(word
, lp
->l_word
))
2056 * Look for inexact match.
2059 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2060 if (itsabbr(word
, lp
->l_word
)) {
2061 if (foundlp
== NULL
)
2063 else return (NULL
); /* multiple inexact matches */
2073 register char **array
;
2078 array
= (char **)(void *)
2079 emalloc((int)((strlen(cp
) + 1) * sizeof (*array
)));
2082 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2084 if (*cp
== '\0' || *cp
== '#')
2086 array
[nsubs
++] = dp
= cp
;
2088 if ((*dp
= *cp
++) != '"')
2090 else while ((*dp
= *cp
++) != '"')
2095 "Odd number of quotation marks"));
2098 } while (*cp
!= '\0' && *cp
!= '#' &&
2099 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2100 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2104 array
[nsubs
] = NULL
;
2116 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2117 error(gettext("time overflow"));
2130 if (t1
== max_time
&& t2
> 0)
2132 if (t1
== min_time
&& t2
< 0)
2135 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2136 error(gettext("time overflow"));
2143 * Given a rule, and a year, compute the date - in seconds since January 1,
2144 * 1970, 00:00 LOCAL time - in that year that the rule refers to.
2148 rpytime(rp
, wantedy
)
2149 register const struct rule
* const rp
;
2150 register const int wantedy
;
2152 register int y
, m
, i
;
2153 register long dayoff
; /* with a nod to Margaret O. */
2156 if (wantedy
== INT_MIN
)
2158 if (wantedy
== INT_MAX
)
2163 while (wantedy
!= y
) {
2165 i
= len_years
[isleap(y
)];
2169 i
= -len_years
[isleap(y
)];
2171 dayoff
= oadd(dayoff
, eitol(i
));
2173 while (m
!= rp
->r_month
) {
2174 i
= len_months
[isleap(y
)][m
];
2175 dayoff
= oadd(dayoff
, eitol(i
));
2178 i
= rp
->r_dayofmonth
;
2179 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2180 if (rp
->r_dycode
== DC_DOWLEQ
)
2183 error(gettext("use of 2/29 in non leap-year"));
2188 dayoff
= oadd(dayoff
, eitol(i
));
2189 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2192 #define LDAYSPERWEEK ((long)DAYSPERWEEK)
2193 wday
= eitol(EPOCH_WDAY
);
2195 * Don't trust mod of negative numbers.
2198 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2200 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2202 wday
+= LDAYSPERWEEK
;
2204 while (wday
!= eitol(rp
->r_wday
))
2205 if (rp
->r_dycode
== DC_DOWGEQ
) {
2206 dayoff
= oadd(dayoff
, (long)1);
2207 if (++wday
>= LDAYSPERWEEK
)
2211 dayoff
= oadd(dayoff
, (long)-1);
2213 wday
= LDAYSPERWEEK
- 1;
2216 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2218 warning(gettext("rule goes past start/end of "
2219 "month--will not work with pre-2004 "
2220 "versions of zic"));
2223 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
))
2225 if (dayoff
< min_time
/ SECSPERDAY
)
2227 if (dayoff
> max_time
/ SECSPERDAY
)
2229 t
= (zic_t
)dayoff
* SECSPERDAY
;
2230 return (tadd(t
, rp
->r_tod
));
2234 newabbr(const char * const string
)
2238 if (strcmp(string
, GRANDPARENTED
) != 0) {
2239 register const char *cp
;
2244 while (isalpha(*cp
) || ('0' <= *cp
&& *cp
<= '9') ||
2245 *cp
== '-' || *cp
== '+') {
2248 if (noise
&& cp
- string
< 3)
2249 wp
= gettext(("time zone abbreviation has less than 3 "
2251 if (wp
== NULL
&& cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2252 wp
= gettext(("time zone abbreviation has too many "
2254 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2256 if (isascii(*cp
) && isdigit(*cp
))
2257 if (*cp
++ == '1' && *cp
>= '0' && *cp
<= '4')
2260 if (wp
== NULL
&& *cp
!= '\0')
2261 wp
= gettext("time zone abbreviation differs from "
2265 wp
= ecatalloc(wp
, " (");
2266 wp
= ecatalloc(wp
, string
);
2267 wp
= ecatalloc(wp
, ")");
2272 i
= strlen(string
) + 1;
2273 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2274 error(gettext("too many, or too long, time zone "
2278 (void) strcpy(&chars
[charcnt
], string
);
2279 charcnt
+= eitol(i
);
2286 register char *name
;
2289 if (argname
== NULL
|| *argname
== '\0')
2291 cp
= name
= ecpyalloc(argname
);
2292 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2294 if (!itsdir(name
)) {
2296 * It doesn't seem to exist, so we try to create it.
2297 * Creation may fail because of the directory being
2298 * created by some other multiprocessor, so we get
2299 * to do extra checking.
2301 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2302 const char *e
= strerror(errno
);
2304 if (errno
!= EEXIST
|| !itsdir(name
)) {
2305 (void) fprintf(stderr
, gettext(
2306 "%s: Can't create directory %s: %s\n"),
2326 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2327 (void) fprintf(stderr
,
2328 gettext("%s: %d did not sign extend correctly\n"),
2336 * UNIX was a registered trademark of The Open Group in 2003.