1 static char elsieid
[] = "@(#)zic.c 7.116";
11 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
13 #define MKDIR_UMASK 0755
17 ** On some ancient hosts, predicates like `isspace(C)' are defined
18 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
19 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
20 ** Neither the C Standard nor Posix require that `isascii' exist.
21 ** For portability, we check both ancient and modern requirements.
22 ** If isascii is not defined, the isascii check succeeds trivially.
30 const char * r_filename
;
34 int r_loyear
; /* for example, 1986 */
35 int r_hiyear
; /* for example, 1986 */
36 const char * r_yrtype
;
38 int r_month
; /* 0..11 */
40 int r_dycode
; /* see below */
44 long r_tod
; /* time from midnight */
45 int r_todisstd
; /* above is standard time if TRUE */
46 /* or wall clock time if FALSE */
47 int r_todisgmt
; /* above is GMT if TRUE */
48 /* or local time if FALSE */
49 long r_stdoff
; /* offset from standard time */
50 const char * r_abbrvar
; /* variable part of abbreviation */
52 int r_todo
; /* a rule to do (used in outzone) */
53 time_t r_temp
; /* used in outzone */
57 ** r_dycode r_dayofmonth r_wday
60 #define DC_DOM 0 /* 1..31 */ /* unused */
61 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
62 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
65 const char * z_filename
;
71 const char * z_format
;
75 struct rule
* z_rules
;
78 struct rule z_untilrule
;
82 extern int getopt
P((int argc
, char * const argv
[],
83 const char * options
));
84 extern int link
P((const char * fromname
, const char * toname
));
88 static void addtt
P((time_t starttime
, int type
));
89 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
90 int ttisstd
, int ttisgmt
));
91 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
92 static void adjleap
P((void));
93 static void associate
P((void));
94 static int ciequal
P((const char * ap
, const char * bp
));
95 static void convert
P((long val
, char * buf
));
96 static void dolink
P((const char * fromfile
, const char * tofile
));
97 static void doabbr
P((char * abbr
, const char * format
,
98 const char * letters
, int isdst
));
99 static void eat
P((const char * name
, int num
));
100 static void eats
P((const char * name
, int num
,
101 const char * rname
, int rnum
));
102 static long eitol
P((int i
));
103 static void error
P((const char * message
));
104 static char ** getfields
P((char * buf
));
105 static long gethms
P((const char * string
, const char * errstrng
,
107 static void infile
P((const char * filename
));
108 static void inleap
P((char ** fields
, int nfields
));
109 static void inlink
P((char ** fields
, int nfields
));
110 static void inrule
P((char ** fields
, int nfields
));
111 static int inzcont
P((char ** fields
, int nfields
));
112 static int inzone
P((char ** fields
, int nfields
));
113 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
114 static int itsabbr
P((const char * abbr
, const char * word
));
115 static int itsdir
P((const char * name
));
116 static int lowerit
P((int c
));
117 static char * memcheck
P((char * tocheck
));
118 static int mkdirs
P((char * filename
));
119 static void newabbr
P((const char * abbr
));
120 static long oadd
P((long t1
, long t2
));
121 static void outzone
P((const struct zone
* zp
, int ntzones
));
122 static void puttzcode
P((long code
, FILE * fp
));
123 static int rcomp
P((const void * leftp
, const void * rightp
));
124 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
125 static void rulesub
P((struct rule
* rp
,
126 const char * loyearp
, const char * hiyearp
,
127 const char * typep
, const char * monthp
,
128 const char * dayp
, const char * timep
));
129 static void setboundaries
P((void));
130 static time_t tadd
P((time_t t1
, long t2
));
131 static void usage
P((void));
132 static void writezone
P((const char * name
));
133 static int yearistype
P((int year
, const char * type
));
135 #if !(HAVE_STRERROR - 0)
136 static char * strerror
P((int));
137 #endif /* !(HAVE_STRERROR - 0) */
141 static const char * filename
;
144 static time_t max_time
;
146 static int max_year_representable
;
147 static time_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
P((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 time_t trans
[TZ_MAX_LEAPS
];
347 static long corr
[TZ_MAX_LEAPS
];
348 static char roll
[TZ_MAX_LEAPS
];
351 ** Memory allocation.
359 const char *e
= strerror(errno
);
361 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
363 (void) exit(EXIT_FAILURE
);
368 #define emalloc(size) memcheck(imalloc(size))
369 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
370 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
371 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
377 #if !(HAVE_STRERROR - 0)
382 extern char * sys_errlist
[];
385 return (errnum
> 0 && errnum
<= sys_nerr
) ?
386 sys_errlist
[errnum
] : _("Unknown system error");
388 #endif /* !(HAVE_STRERROR - 0) */
391 eats(name
, num
, rname
, rnum
)
392 const char * const name
;
394 const char * const rname
;
405 const char * const name
;
408 eats(name
, num
, (char *) NULL
, -1);
413 const char * const string
;
416 ** Match the format of "cc" to allow sh users to
417 ** zic ... 2>&1 | error -t "*" -v
420 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
421 filename
, linenum
, string
);
422 if (rfilename
!= NULL
)
423 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
424 rfilename
, rlinenum
);
425 (void) fprintf(stderr
, "\n");
431 const char * const string
;
435 cp
= ecpyalloc(_("warning: "));
436 cp
= ecatalloc(cp
, string
);
445 (void) fprintf(stderr
, _("%s: usage is %s [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
447 (void) exit(EXIT_FAILURE
);
450 static const char * psxrules
;
451 static const char * lcltime
;
452 static const char * directory
;
453 static const char * leapsec
;
454 static const char * yitcommand
;
455 static int sflag
= FALSE
;
467 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
468 #endif /* defined unix */
470 (void) setlocale(LC_CTYPE
, "");
471 (void) setlocale(LC_MESSAGES
, "");
473 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
474 #endif /* defined TEXTDOMAINDIR */
475 (void) textdomain(TZ_DOMAIN
);
476 #endif /* HAVE_GETTEXT - 0 */
478 for (i
= 1; i
< argc
; ++i
)
479 if (strcmp(argv
[i
], "--version") == 0) {
480 (void) printf("%s\n", elsieid
);
481 (void) exit(EXIT_SUCCESS
);
483 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
488 if (directory
== NULL
)
491 (void) fprintf(stderr
,
492 _("%s: More than one -d option specified\n"),
494 (void) exit(EXIT_FAILURE
);
501 (void) fprintf(stderr
,
502 _("%s: More than one -l option specified\n"),
504 (void) exit(EXIT_FAILURE
);
508 if (psxrules
== NULL
)
511 (void) fprintf(stderr
,
512 _("%s: More than one -p option specified\n"),
514 (void) exit(EXIT_FAILURE
);
518 if (yitcommand
== NULL
)
521 (void) fprintf(stderr
,
522 _("%s: More than one -y option specified\n"),
524 (void) exit(EXIT_FAILURE
);
531 (void) fprintf(stderr
,
532 _("%s: More than one -L option specified\n"),
534 (void) exit(EXIT_FAILURE
);
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 if (optind
< argc
&& leapsec
!= NULL
) {
558 for (i
= optind
; i
< argc
; ++i
)
561 (void) exit(EXIT_FAILURE
);
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 if (lcltime
!= NULL
) {
579 eat("command line", 1);
580 dolink(lcltime
, TZDEFAULT
);
582 if (psxrules
!= NULL
) {
583 eat("command line", 1);
584 dolink(psxrules
, TZDEFRULES
);
586 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
590 dolink(fromfile
, tofile
)
591 const char * const fromfile
;
592 const char * const tofile
;
594 register char * fromname
;
595 register char * toname
;
597 if (fromfile
[0] == '/')
598 fromname
= ecpyalloc(fromfile
);
600 fromname
= ecpyalloc(directory
);
601 fromname
= ecatalloc(fromname
, "/");
602 fromname
= ecatalloc(fromname
, fromfile
);
604 if (tofile
[0] == '/')
605 toname
= ecpyalloc(tofile
);
607 toname
= ecpyalloc(directory
);
608 toname
= ecatalloc(toname
, "/");
609 toname
= ecatalloc(toname
, tofile
);
612 ** We get to be careful here since
613 ** there's a fair chance of root running us.
616 (void) remove(toname
);
617 if (link(fromname
, toname
) != 0) {
620 if (mkdirs(toname
) != 0)
621 (void) exit(EXIT_FAILURE
);
623 result
= link(fromname
, toname
);
624 #if (HAVE_SYMLINK - 0)
626 access(fromname
, F_OK
) == 0 &&
628 const char *s
= tofile
;
629 register char * symlinkcontents
= NULL
;
630 while ((s
= strchr(s
+1, '/')) != NULL
)
631 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
632 symlinkcontents
= ecatalloc(symlinkcontents
, fromname
);
634 result
= unlink(toname
);
635 if (result
!= 0 && errno
!= ENOENT
) {
636 const char *e
= strerror(errno
);
638 (void) fprintf(stderr
,
639 _("%s: Can't unlink %s: %s\n"),
640 progname
, toname
, e
);
641 (void) exit(EXIT_FAILURE
);
644 result
= symlink(symlinkcontents
, toname
);
646 warning(_("hard link failed, symbolic link used"));
647 ifree(symlinkcontents
);
651 const char *e
= strerror(errno
);
653 (void) fprintf(stderr
,
654 _("%s: Can't link from %s to %s: %s\n"),
655 progname
, fromname
, toname
, e
);
656 (void) exit(EXIT_FAILURE
);
664 #define INT_MAX ((int) (((unsigned)~0)>>1))
665 #endif /* !defined INT_MAX */
668 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
669 #endif /* !defined INT_MIN */
672 ** The tz file format currently allows at most 32-bit quantities.
673 ** This restriction should be removed before signed 32-bit values
674 ** wrap around in 2038, but unfortunately this will require a
675 ** change to the tz file format.
678 #define MAX_BITS_IN_FILE 32
679 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
682 setboundaries
P((void))
684 if (TYPE_SIGNED(time_t)) {
685 min_time
= ~ (time_t) 0;
686 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
687 max_time
= ~ (time_t) 0 - min_time
;
692 max_time
= 2 - sflag
;
693 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
696 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
697 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
698 min_year_representable
= min_year
;
699 max_year_representable
= max_year
;
704 const char * const name
;
706 register char * myname
;
709 myname
= ecpyalloc(name
);
710 myname
= ecatalloc(myname
, "/.");
711 accres
= access(myname
, F_OK
);
717 ** Associate sets of rules with zones.
721 ** Sort by rule name.
729 return strcmp(((const struct rule
*) cp1
)->r_name
,
730 ((const struct rule
*) cp2
)->r_name
);
736 register struct zone
* zp
;
737 register struct rule
* rp
;
738 register int base
, out
;
742 (void) qsort((void *) rules
, (size_t) nrules
,
743 (size_t) sizeof *rules
, rcomp
);
744 for (i
= 0; i
< nrules
- 1; ++i
) {
745 if (strcmp(rules
[i
].r_name
,
746 rules
[i
+ 1].r_name
) != 0)
748 if (strcmp(rules
[i
].r_filename
,
749 rules
[i
+ 1].r_filename
) == 0)
751 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
752 warning(_("same rule name in multiple files"));
753 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
754 warning(_("same rule name in multiple files"));
755 for (j
= i
+ 2; j
< nrules
; ++j
) {
756 if (strcmp(rules
[i
].r_name
,
757 rules
[j
].r_name
) != 0)
759 if (strcmp(rules
[i
].r_filename
,
760 rules
[j
].r_filename
) == 0)
762 if (strcmp(rules
[i
+ 1].r_filename
,
763 rules
[j
].r_filename
) == 0)
770 for (i
= 0; i
< nzones
; ++i
) {
775 for (base
= 0; base
< nrules
; base
= out
) {
777 for (out
= base
+ 1; out
< nrules
; ++out
)
778 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
780 for (i
= 0; i
< nzones
; ++i
) {
782 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
785 zp
->z_nrules
= out
- base
;
788 for (i
= 0; i
< nzones
; ++i
) {
790 if (zp
->z_nrules
== 0) {
792 ** Maybe we have a local standard time offset.
794 eat(zp
->z_filename
, zp
->z_linenum
);
795 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
798 ** Note, though, that if there's no rule,
799 ** a '%s' in the format is a bad thing.
801 if (strchr(zp
->z_format
, '%') != 0)
802 error(_("%s in ruleless zone"));
806 (void) exit(EXIT_FAILURE
);
814 register char ** fields
;
816 register const struct lookup
* lp
;
817 register int nfields
;
818 register int wantcont
;
822 if (strcmp(name
, "-") == 0) {
823 name
= _("standard input");
825 } else if ((fp
= fopen(name
, "r")) == NULL
) {
826 const char *e
= strerror(errno
);
828 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
830 (void) exit(EXIT_FAILURE
);
833 for (num
= 1; ; ++num
) {
835 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
837 cp
= strchr(buf
, '\n');
839 error(_("line too long"));
840 (void) exit(EXIT_FAILURE
);
843 fields
= getfields(buf
);
845 while (fields
[nfields
] != NULL
) {
848 if (strcmp(fields
[nfields
], "-") == 0)
849 fields
[nfields
] = &nada
;
854 } else if (wantcont
) {
855 wantcont
= inzcont(fields
, nfields
);
857 lp
= byword(fields
[0], line_codes
);
859 error(_("input line of unknown type"));
860 else switch ((int) (lp
->l_value
)) {
862 inrule(fields
, nfields
);
866 wantcont
= inzone(fields
, nfields
);
869 inlink(fields
, nfields
);
874 (void) fprintf(stderr
,
875 _("%s: Leap line in non leap seconds file %s\n"),
877 else inleap(fields
, nfields
);
880 default: /* "cannot happen" */
881 (void) fprintf(stderr
,
882 _("%s: panic: Invalid l_value %d\n"),
883 progname
, lp
->l_value
);
884 (void) exit(EXIT_FAILURE
);
887 ifree((char *) fields
);
890 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
892 (void) exit(EXIT_FAILURE
);
894 if (fp
!= stdin
&& fclose(fp
)) {
895 const char *e
= strerror(errno
);
897 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
898 progname
, filename
, e
);
899 (void) exit(EXIT_FAILURE
);
902 error(_("expected continuation line not found"));
906 ** Convert a string of one of the forms
907 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
908 ** into a number of seconds.
909 ** A null string maps to zero.
910 ** Call error with errstring and return zero on errors.
914 gethms(string
, errstring
, signable
)
916 const char * const errstring
;
919 int hh
, mm
, ss
, sign
;
921 if (string
== NULL
|| *string
== '\0')
925 else if (*string
== '-') {
929 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
931 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
933 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
934 &hh
, &mm
, &ss
) != 3) {
938 if ((hh
< 0 || hh
>= HOURSPERDAY
||
939 mm
< 0 || mm
>= MINSPERHOUR
||
940 ss
< 0 || ss
> SECSPERMIN
) &&
941 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
945 if (noise
&& hh
== HOURSPERDAY
)
946 warning(_("24:00 not handled by pre-1998 versions of zic"));
948 (eitol(hh
* MINSPERHOUR
+ mm
) *
949 eitol(SECSPERMIN
) + eitol(ss
));
953 inrule(fields
, nfields
)
954 register char ** const fields
;
957 static struct rule r
;
959 if (nfields
!= RULE_FIELDS
) {
960 error(_("wrong number of fields on Rule line"));
963 if (*fields
[RF_NAME
] == '\0') {
964 error(_("nameless rule"));
967 r
.r_filename
= filename
;
968 r
.r_linenum
= linenum
;
969 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
970 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
971 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
972 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
973 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
974 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
975 (int) ((nrules
+ 1) * sizeof *rules
));
980 inzone(fields
, nfields
)
981 register char ** const fields
;
987 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
988 error(_("wrong number of fields on Zone line"));
991 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
992 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
994 _("\"Zone %s\" line and -l option are mutually exclusive"),
999 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1000 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1002 _("\"Zone %s\" line and -p option are mutually exclusive"),
1007 for (i
= 0; i
< nzones
; ++i
)
1008 if (zones
[i
].z_name
!= NULL
&&
1009 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1010 buf
= erealloc(buf
, (int) (132 +
1011 strlen(fields
[ZF_NAME
]) +
1012 strlen(zones
[i
].z_filename
)));
1014 _("duplicate zone name %s (file \"%s\", line %d)"),
1016 zones
[i
].z_filename
,
1017 zones
[i
].z_linenum
);
1021 return inzsub(fields
, nfields
, FALSE
);
1025 inzcont(fields
, nfields
)
1026 register char ** const fields
;
1029 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1030 error(_("wrong number of fields on Zone continuation line"));
1033 return inzsub(fields
, nfields
, TRUE
);
1037 inzsub(fields
, nfields
, iscont
)
1038 register char ** const fields
;
1043 static struct zone z
;
1044 register int i_gmtoff
, i_rule
, i_format
;
1045 register int i_untilyear
, i_untilmonth
;
1046 register int i_untilday
, i_untiltime
;
1047 register int hasuntil
;
1050 i_gmtoff
= ZFC_GMTOFF
;
1052 i_format
= ZFC_FORMAT
;
1053 i_untilyear
= ZFC_TILYEAR
;
1054 i_untilmonth
= ZFC_TILMONTH
;
1055 i_untilday
= ZFC_TILDAY
;
1056 i_untiltime
= ZFC_TILTIME
;
1059 i_gmtoff
= ZF_GMTOFF
;
1061 i_format
= ZF_FORMAT
;
1062 i_untilyear
= ZF_TILYEAR
;
1063 i_untilmonth
= ZF_TILMONTH
;
1064 i_untilday
= ZF_TILDAY
;
1065 i_untiltime
= ZF_TILTIME
;
1066 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1068 z
.z_filename
= filename
;
1069 z
.z_linenum
= linenum
;
1070 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1071 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1072 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1073 error(_("invalid abbreviation format"));
1077 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1078 z
.z_format
= ecpyalloc(fields
[i_format
]);
1079 hasuntil
= nfields
> i_untilyear
;
1081 z
.z_untilrule
.r_filename
= filename
;
1082 z
.z_untilrule
.r_linenum
= linenum
;
1083 rulesub(&z
.z_untilrule
,
1084 fields
[i_untilyear
],
1087 (nfields
> i_untilmonth
) ?
1088 fields
[i_untilmonth
] : "Jan",
1089 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1090 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1091 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1092 z
.z_untilrule
.r_loyear
);
1093 if (iscont
&& nzones
> 0 &&
1094 z
.z_untiltime
> min_time
&&
1095 z
.z_untiltime
< max_time
&&
1096 zones
[nzones
- 1].z_untiltime
> min_time
&&
1097 zones
[nzones
- 1].z_untiltime
< max_time
&&
1098 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1099 error(_("Zone continuation line end time is not after end time of previous line"));
1103 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1104 (int) ((nzones
+ 1) * sizeof *zones
));
1105 zones
[nzones
++] = z
;
1107 ** If there was an UNTIL field on this line,
1108 ** there's more information about the zone on the next line.
1114 inleap(fields
, nfields
)
1115 register char ** const fields
;
1118 register const char * cp
;
1119 register const struct lookup
* lp
;
1121 int year
, month
, day
;
1125 if (nfields
!= LEAP_FIELDS
) {
1126 error(_("wrong number of fields on Leap line"));
1130 cp
= fields
[LP_YEAR
];
1131 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1135 error(_("invalid leaping year"));
1141 i
= len_years
[isleap(j
)];
1145 i
= -len_years
[isleap(j
)];
1147 dayoff
= oadd(dayoff
, eitol(i
));
1149 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1150 error(_("invalid month name"));
1153 month
= lp
->l_value
;
1155 while (j
!= month
) {
1156 i
= len_months
[isleap(year
)][j
];
1157 dayoff
= oadd(dayoff
, eitol(i
));
1160 cp
= fields
[LP_DAY
];
1161 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1162 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1163 error(_("invalid day of month"));
1166 dayoff
= oadd(dayoff
, eitol(day
- 1));
1167 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1168 error(_("time before zero"));
1171 if (dayoff
< min_time
/ SECSPERDAY
) {
1172 error(_("time too small"));
1175 if (dayoff
> max_time
/ SECSPERDAY
) {
1176 error(_("time too large"));
1179 t
= (time_t) dayoff
* SECSPERDAY
;
1180 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1181 cp
= fields
[LP_CORR
];
1183 register int positive
;
1186 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1189 } else if (strcmp(cp
, "--") == 0) {
1192 } else if (strcmp(cp
, "+") == 0) {
1195 } else if (strcmp(cp
, "++") == 0) {
1199 error(_("illegal CORRECTION field on Leap line"));
1202 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1203 error(_("illegal Rolling/Stationary field on Leap line"));
1206 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1211 inlink(fields
, nfields
)
1212 register char ** const fields
;
1217 if (nfields
!= LINK_FIELDS
) {
1218 error(_("wrong number of fields on Link line"));
1221 if (*fields
[LF_FROM
] == '\0') {
1222 error(_("blank FROM field on Link line"));
1225 if (*fields
[LF_TO
] == '\0') {
1226 error(_("blank TO field on Link line"));
1229 l
.l_filename
= filename
;
1230 l
.l_linenum
= linenum
;
1231 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1232 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1233 links
= (struct link
*) (void *) erealloc((char *) links
,
1234 (int) ((nlinks
+ 1) * sizeof *links
));
1235 links
[nlinks
++] = l
;
1239 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1240 register struct rule
* const rp
;
1241 const char * const loyearp
;
1242 const char * const hiyearp
;
1243 const char * const typep
;
1244 const char * const monthp
;
1245 const char * const dayp
;
1246 const char * const timep
;
1248 register const struct lookup
* lp
;
1249 register const char * cp
;
1253 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1254 error(_("invalid month name"));
1257 rp
->r_month
= lp
->l_value
;
1258 rp
->r_todisstd
= FALSE
;
1259 rp
->r_todisgmt
= FALSE
;
1260 dp
= ecpyalloc(timep
);
1262 ep
= dp
+ strlen(dp
) - 1;
1263 switch (lowerit(*ep
)) {
1264 case 's': /* Standard */
1265 rp
->r_todisstd
= TRUE
;
1266 rp
->r_todisgmt
= FALSE
;
1269 case 'w': /* Wall */
1270 rp
->r_todisstd
= FALSE
;
1271 rp
->r_todisgmt
= FALSE
;
1274 case 'g': /* Greenwich */
1275 case 'u': /* Universal */
1276 case 'z': /* Zulu */
1277 rp
->r_todisstd
= TRUE
;
1278 rp
->r_todisgmt
= TRUE
;
1283 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1289 lp
= byword(cp
, begin_years
);
1290 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1292 rp
->r_loyear
= INT_MIN
;
1295 rp
->r_loyear
= INT_MAX
;
1297 default: /* "cannot happen" */
1298 (void) fprintf(stderr
,
1299 _("%s: panic: Invalid l_value %d\n"),
1300 progname
, lp
->l_value
);
1301 (void) exit(EXIT_FAILURE
);
1302 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1303 error(_("invalid starting year"));
1306 if (rp
->r_loyear
< min_year_representable
)
1307 warning(_("starting year too low to be represented"));
1308 else if (rp
->r_loyear
> max_year_representable
)
1309 warning(_("starting year too high to be represented"));
1312 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1314 rp
->r_hiyear
= INT_MIN
;
1317 rp
->r_hiyear
= INT_MAX
;
1320 rp
->r_hiyear
= rp
->r_loyear
;
1322 default: /* "cannot happen" */
1323 (void) fprintf(stderr
,
1324 _("%s: panic: Invalid l_value %d\n"),
1325 progname
, lp
->l_value
);
1326 (void) exit(EXIT_FAILURE
);
1327 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1328 error(_("invalid ending year"));
1331 if (rp
->r_loyear
< min_year_representable
)
1332 warning(_("ending year too low to be represented"));
1333 else if (rp
->r_loyear
> max_year_representable
)
1334 warning(_("ending year too high to be represented"));
1336 if (rp
->r_loyear
> rp
->r_hiyear
) {
1337 error(_("starting year greater than ending year"));
1341 rp
->r_yrtype
= NULL
;
1343 if (rp
->r_loyear
== rp
->r_hiyear
) {
1344 error(_("typed single year"));
1347 rp
->r_yrtype
= ecpyalloc(typep
);
1349 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1350 min_year
= rp
->r_loyear
;
1353 ** Accept things such as:
1359 dp
= ecpyalloc(dayp
);
1360 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1361 rp
->r_dycode
= DC_DOWLEQ
;
1362 rp
->r_wday
= lp
->l_value
;
1363 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1365 if ((ep
= strchr(dp
, '<')) != 0)
1366 rp
->r_dycode
= DC_DOWLEQ
;
1367 else if ((ep
= strchr(dp
, '>')) != 0)
1368 rp
->r_dycode
= DC_DOWGEQ
;
1371 rp
->r_dycode
= DC_DOM
;
1373 if (rp
->r_dycode
!= DC_DOM
) {
1376 error(_("invalid day of month"));
1380 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1381 error(_("invalid weekday name"));
1385 rp
->r_wday
= lp
->l_value
;
1387 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1388 rp
->r_dayofmonth
<= 0 ||
1389 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1390 error(_("invalid day of month"));
1404 register long shift
;
1406 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1407 buf
[i
] = val
>> shift
;
1418 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1426 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1428 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1435 const char * const name
;
1439 static char * fullname
;
1440 static struct tzhead tzh
;
1441 time_t ats
[TZ_MAX_TIMES
];
1442 unsigned char types
[TZ_MAX_TIMES
];
1448 (void) qsort((void *) attypes
, (size_t) timecnt
,
1449 (size_t) sizeof *attypes
, atcomp
);
1459 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1462 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1463 ++fromi
; /* handled by default rule */
1464 for ( ; fromi
< timecnt
; ++fromi
) {
1466 && ((attypes
[fromi
].at
1467 + gmtoffs
[attypes
[toi
- 1].type
])
1468 <= (attypes
[toi
- 1].at
1469 + gmtoffs
[toi
== 1 ? 0
1470 : attypes
[toi
- 2].type
]))) {
1471 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1475 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1476 attypes
[toi
++] = attypes
[fromi
];
1483 for (i
= 0; i
< timecnt
; ++i
) {
1484 ats
[i
] = attypes
[i
].at
;
1485 types
[i
] = attypes
[i
].type
;
1487 fullname
= erealloc(fullname
,
1488 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1489 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1491 ** Remove old file, if any, to snap links.
1493 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1494 const char *e
= strerror(errno
);
1496 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1497 progname
, fullname
, e
);
1498 (void) exit(EXIT_FAILURE
);
1500 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1501 if (mkdirs(fullname
) != 0)
1502 (void) exit(EXIT_FAILURE
);
1503 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1504 const char *e
= strerror(errno
);
1506 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1507 progname
, fullname
, e
);
1508 (void) exit(EXIT_FAILURE
);
1511 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1512 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1513 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1514 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1515 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1516 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1517 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1518 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1528 for (i
= 0; i
< timecnt
; ++i
) {
1531 if (ats
[i
] >= trans
[j
]) {
1532 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1535 puttzcode((long) ats
[i
], fp
);
1538 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1539 (size_t) timecnt
, fp
);
1540 for (i
= 0; i
< typecnt
; ++i
) {
1541 puttzcode((long) gmtoffs
[i
], fp
);
1542 (void) putc(isdsts
[i
], fp
);
1543 (void) putc(abbrinds
[i
], fp
);
1546 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1547 (size_t) charcnt
, fp
);
1548 for (i
= 0; i
< leapcnt
; ++i
) {
1550 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1553 if (++j
>= typecnt
) {
1559 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1563 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1564 } else puttzcode((long) trans
[i
], fp
);
1565 puttzcode((long) corr
[i
], fp
);
1567 for (i
= 0; i
< typecnt
; ++i
)
1568 (void) putc(ttisstds
[i
], fp
);
1569 for (i
= 0; i
< typecnt
; ++i
)
1570 (void) putc(ttisgmts
[i
], fp
);
1571 if (ferror(fp
) || fclose(fp
)) {
1572 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1573 progname
, fullname
);
1574 (void) exit(EXIT_FAILURE
);
1579 doabbr(abbr
, format
, letters
, isdst
)
1581 const char * const format
;
1582 const char * const letters
;
1585 if (strchr(format
, '/') == NULL
) {
1586 if (letters
== NULL
)
1587 (void) strcpy(abbr
, format
);
1588 else (void) sprintf(abbr
, format
, letters
);
1590 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1592 (void) strcpy(abbr
, format
);
1593 *strchr(abbr
, '/') = '\0';
1598 outzone(zpfirst
, zonecount
)
1599 const struct zone
* const zpfirst
;
1600 const int zonecount
;
1602 register const struct zone
* zp
;
1603 register struct rule
* rp
;
1605 register int usestart
, useuntil
;
1606 register time_t starttime
, untiltime
;
1607 register long gmtoff
;
1608 register long stdoff
;
1610 register long startoff
;
1611 register int startttisstd
;
1612 register int startttisgmt
;
1614 char startbuf
[BUFSIZ
];
1616 INITIALIZE(untiltime
);
1617 INITIALIZE(starttime
);
1619 ** Now. . .finally. . .generate some useful data!
1625 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1626 ** for noting the need to unconditionally initialize startttisstd.
1628 startttisstd
= FALSE
;
1629 startttisgmt
= FALSE
;
1630 for (i
= 0; i
< zonecount
; ++i
) {
1632 ** A guess that may well be corrected later.
1636 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1637 useuntil
= i
< (zonecount
- 1);
1638 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1640 gmtoff
= zp
->z_gmtoff
;
1641 eat(zp
->z_filename
, zp
->z_linenum
);
1643 startoff
= zp
->z_gmtoff
;
1644 if (zp
->z_nrules
== 0) {
1645 stdoff
= zp
->z_stdoff
;
1646 doabbr(startbuf
, zp
->z_format
,
1647 (char *) NULL
, stdoff
!= 0);
1648 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1649 startbuf
, stdoff
!= 0, startttisstd
,
1652 addtt(starttime
, type
);
1654 } else if (stdoff
!= 0)
1655 addtt(min_time
, type
);
1656 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1657 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1660 ** Mark which rules to do in the current year.
1661 ** For those to do, calculate rpytime(rp, year);
1663 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1664 rp
= &zp
->z_rules
[j
];
1665 eats(zp
->z_filename
, zp
->z_linenum
,
1666 rp
->r_filename
, rp
->r_linenum
);
1667 rp
->r_todo
= year
>= rp
->r_loyear
&&
1668 year
<= rp
->r_hiyear
&&
1669 yearistype(year
, rp
->r_yrtype
);
1671 rp
->r_temp
= rpytime(rp
, year
);
1675 register time_t jtime
, ktime
;
1676 register long offset
;
1682 ** Turn untiltime into UTC
1683 ** assuming the current gmtoff and
1686 untiltime
= zp
->z_untiltime
;
1687 if (!zp
->z_untilrule
.r_todisgmt
)
1688 untiltime
= tadd(untiltime
,
1690 if (!zp
->z_untilrule
.r_todisstd
)
1691 untiltime
= tadd(untiltime
,
1695 ** Find the rule (of those to do, if any)
1696 ** that takes effect earliest in the year.
1699 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1700 rp
= &zp
->z_rules
[j
];
1703 eats(zp
->z_filename
, zp
->z_linenum
,
1704 rp
->r_filename
, rp
->r_linenum
);
1705 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1706 if (!rp
->r_todisstd
)
1707 offset
= oadd(offset
, stdoff
);
1709 if (jtime
== min_time
||
1712 jtime
= tadd(jtime
, -offset
);
1713 if (k
< 0 || jtime
< ktime
) {
1719 break; /* go on to next year */
1720 rp
= &zp
->z_rules
[k
];
1722 if (useuntil
&& ktime
>= untiltime
)
1724 stdoff
= rp
->r_stdoff
;
1725 if (usestart
&& ktime
== starttime
)
1728 if (ktime
< starttime
) {
1729 startoff
= oadd(zp
->z_gmtoff
,
1731 doabbr(startbuf
, zp
->z_format
,
1736 if (*startbuf
== '\0' &&
1737 startoff
== oadd(zp
->z_gmtoff
,
1739 doabbr(startbuf
, zp
->z_format
,
1744 eats(zp
->z_filename
, zp
->z_linenum
,
1745 rp
->r_filename
, rp
->r_linenum
);
1746 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1748 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1749 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1750 rp
->r_todisstd
, rp
->r_todisgmt
);
1755 if (*startbuf
== '\0' &&
1756 zp
->z_format
!= NULL
&&
1757 strchr(zp
->z_format
, '%') == NULL
&&
1758 strchr(zp
->z_format
, '/') == NULL
)
1759 (void) strcpy(startbuf
, zp
->z_format
);
1760 eat(zp
->z_filename
, zp
->z_linenum
);
1761 if (*startbuf
== '\0')
1762 error(_("can't determine time zone abbreviation to use just after until time"));
1763 else addtt(starttime
,
1764 addtype(startoff
, startbuf
,
1765 startoff
!= zp
->z_gmtoff
,
1770 ** Now we may get to set starttime for the next zone line.
1773 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1774 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1775 starttime
= zp
->z_untiltime
;
1777 starttime
= tadd(starttime
, -stdoff
);
1779 starttime
= tadd(starttime
, -gmtoff
);
1782 writezone(zpfirst
->z_name
);
1786 addtt(starttime
, type
)
1787 const time_t starttime
;
1790 if (starttime
<= min_time
||
1791 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1792 gmtoffs
[0] = gmtoffs
[type
];
1793 isdsts
[0] = isdsts
[type
];
1794 ttisstds
[0] = ttisstds
[type
];
1795 ttisgmts
[0] = ttisgmts
[type
];
1796 if (abbrinds
[type
] != 0)
1797 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1799 charcnt
= strlen(chars
) + 1;
1804 if (timecnt
>= TZ_MAX_TIMES
) {
1805 error(_("too many transitions?!"));
1806 (void) exit(EXIT_FAILURE
);
1808 attypes
[timecnt
].at
= starttime
;
1809 attypes
[timecnt
].type
= type
;
1814 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1816 const char * const abbr
;
1823 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1824 error(_("internal error - addtype called with bad isdst"));
1825 (void) exit(EXIT_FAILURE
);
1827 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1828 error(_("internal error - addtype called with bad ttisstd"));
1829 (void) exit(EXIT_FAILURE
);
1831 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1832 error(_("internal error - addtype called with bad ttisgmt"));
1833 (void) exit(EXIT_FAILURE
);
1836 ** See if there's already an entry for this zone type.
1837 ** If so, just return its index.
1839 for (i
= 0; i
< typecnt
; ++i
) {
1840 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1841 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1842 ttisstd
== ttisstds
[i
] &&
1843 ttisgmt
== ttisgmts
[i
])
1847 ** There isn't one; add a new one, unless there are already too
1850 if (typecnt
>= TZ_MAX_TYPES
) {
1851 error(_("too many local time types"));
1852 (void) exit(EXIT_FAILURE
);
1854 gmtoffs
[i
] = gmtoff
;
1856 ttisstds
[i
] = ttisstd
;
1857 ttisgmts
[i
] = ttisgmt
;
1859 for (j
= 0; j
< charcnt
; ++j
)
1860 if (strcmp(&chars
[j
], abbr
) == 0)
1870 leapadd(t
, positive
, rolling
, count
)
1878 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1879 error(_("too many leap seconds"));
1880 (void) exit(EXIT_FAILURE
);
1882 for (i
= 0; i
< leapcnt
; ++i
)
1883 if (t
<= trans
[i
]) {
1884 if (t
== trans
[i
]) {
1885 error(_("repeated leap second moment"));
1886 (void) exit(EXIT_FAILURE
);
1891 for (j
= leapcnt
; j
> i
; --j
) {
1892 trans
[j
] = trans
[j
- 1];
1893 corr
[j
] = corr
[j
- 1];
1894 roll
[j
] = roll
[j
- 1];
1897 corr
[i
] = positive
? 1L : eitol(-count
);
1900 } while (positive
&& --count
!= 0);
1907 register long last
= 0;
1910 ** propagate leap seconds forward
1912 for (i
= 0; i
< leapcnt
; ++i
) {
1913 trans
[i
] = tadd(trans
[i
], last
);
1914 last
= corr
[i
] += last
;
1919 yearistype(year
, type
)
1921 const char * const type
;
1926 if (type
== NULL
|| *type
== '\0')
1928 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1929 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1930 result
= system(buf
);
1931 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1937 error(_("Wild result from command execution"));
1938 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1939 progname
, buf
, result
);
1941 (void) exit(EXIT_FAILURE
);
1948 a
= (unsigned char) a
;
1949 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1953 ciequal(ap
, bp
) /* case-insensitive equality */
1954 register const char * ap
;
1955 register const char * bp
;
1957 while (lowerit(*ap
) == lowerit(*bp
++))
1965 register const char * abbr
;
1966 register const char * word
;
1968 if (lowerit(*abbr
) != lowerit(*word
))
1971 while (*++abbr
!= '\0')
1975 } while (lowerit(*word
++) != lowerit(*abbr
));
1979 static const struct lookup
*
1981 register const char * const word
;
1982 register const struct lookup
* const table
;
1984 register const struct lookup
* foundlp
;
1985 register const struct lookup
* lp
;
1987 if (word
== NULL
|| table
== NULL
)
1990 ** Look for exact match.
1992 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1993 if (ciequal(word
, lp
->l_word
))
1996 ** Look for inexact match.
1999 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2000 if (itsabbr(word
, lp
->l_word
)) {
2001 if (foundlp
== NULL
)
2003 else return NULL
; /* multiple inexact matches */
2013 register char ** array
;
2018 array
= (char **) (void *)
2019 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2022 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2024 if (*cp
== '\0' || *cp
== '#')
2026 array
[nsubs
++] = dp
= cp
;
2028 if ((*dp
= *cp
++) != '"')
2030 else while ((*dp
= *cp
++) != '"')
2033 else error(_("Odd number of quotation marks"));
2034 } while (*cp
!= '\0' && *cp
!= '#' &&
2035 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2036 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2040 array
[nsubs
] = NULL
;
2052 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2053 error(_("time overflow"));
2054 (void) exit(EXIT_FAILURE
);
2066 if (t1
== max_time
&& t2
> 0)
2068 if (t1
== min_time
&& t2
< 0)
2071 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2072 error(_("time overflow"));
2073 (void) exit(EXIT_FAILURE
);
2079 ** Given a rule, and a year, compute the date - in seconds since January 1,
2080 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2084 rpytime(rp
, wantedy
)
2085 register const struct rule
* const rp
;
2086 register const int wantedy
;
2088 register int y
, m
, i
;
2089 register long dayoff
; /* with a nod to Margaret O. */
2092 if (wantedy
== INT_MIN
)
2094 if (wantedy
== INT_MAX
)
2099 while (wantedy
!= y
) {
2101 i
= len_years
[isleap(y
)];
2105 i
= -len_years
[isleap(y
)];
2107 dayoff
= oadd(dayoff
, eitol(i
));
2109 while (m
!= rp
->r_month
) {
2110 i
= len_months
[isleap(y
)][m
];
2111 dayoff
= oadd(dayoff
, eitol(i
));
2114 i
= rp
->r_dayofmonth
;
2115 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2116 if (rp
->r_dycode
== DC_DOWLEQ
)
2119 error(_("use of 2/29 in non leap-year"));
2120 (void) exit(EXIT_FAILURE
);
2124 dayoff
= oadd(dayoff
, eitol(i
));
2125 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2128 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2129 wday
= eitol(EPOCH_WDAY
);
2131 ** Don't trust mod of negative numbers.
2134 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2136 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2138 wday
+= LDAYSPERWEEK
;
2140 while (wday
!= eitol(rp
->r_wday
))
2141 if (rp
->r_dycode
== DC_DOWGEQ
) {
2142 dayoff
= oadd(dayoff
, (long) 1);
2143 if (++wday
>= LDAYSPERWEEK
)
2147 dayoff
= oadd(dayoff
, (long) -1);
2149 wday
= LDAYSPERWEEK
- 1;
2152 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2154 warning(_("rule goes past start/end of month--will not work with pre-2004 versions of zic"));
2157 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2159 if (dayoff
< min_time
/ SECSPERDAY
)
2161 if (dayoff
> max_time
/ SECSPERDAY
)
2163 t
= (time_t) dayoff
* SECSPERDAY
;
2164 if (t
> 0 && max_time
- t
< rp
->r_tod
)
2166 return tadd(t
, rp
->r_tod
);
2171 const char * const string
;
2175 i
= strlen(string
) + 1;
2176 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2177 error(_("too many, or too long, time zone abbreviations"));
2178 (void) exit(EXIT_FAILURE
);
2180 (void) strcpy(&chars
[charcnt
], string
);
2181 charcnt
+= eitol(i
);
2186 char * const argname
;
2188 register char * name
;
2191 if (argname
== NULL
|| *argname
== '\0')
2193 cp
= name
= ecpyalloc(argname
);
2194 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2198 ** DOS drive specifier?
2200 if (isalpha((unsigned char) name
[0]) &&
2201 name
[1] == ':' && name
[2] == '\0') {
2205 #endif /* !defined unix */
2206 if (!itsdir(name
)) {
2208 ** It doesn't seem to exist, so we try to create it.
2209 ** Creation may fail because of the directory being
2210 ** created by some other multiprocessor, so we get
2211 ** to do extra checking.
2213 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2214 const char *e
= strerror(errno
);
2216 if (errno
!= EEXIST
|| !itsdir(name
)) {
2217 (void) fprintf(stderr
,
2218 _("%s: Can't create directory %s: %s\n"),
2238 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2239 (void) fprintf(stderr
,
2240 _("%s: %d did not sign extend correctly\n"),
2242 (void) exit(EXIT_FAILURE
);
2248 ** UNIX was a registered trademark of The Open Group in 2003.