3 static char elsieid
[] = "@(#)zic.c 7.22";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
11 const char * r_filename
;
15 int r_loyear
; /* for example, 1986 */
16 int r_hiyear
; /* for example, 1986 */
17 const char * r_yrtype
;
19 int r_month
; /* 0..11 */
21 int r_dycode
; /* see below */
25 long r_tod
; /* time from midnight */
26 int r_todisstd
; /* above is standard time if TRUE */
27 /* or wall clock time if FALSE */
28 long r_stdoff
; /* offset from standard time */
29 const char * r_abbrvar
; /* variable part of abbreviation */
31 int r_todo
; /* a rule to do (used in outzone) */
32 time_t r_temp
; /* used in outzone */
36 ** r_dycode r_dayofmonth r_wday
39 #define DC_DOM 0 /* 1..31 */ /* unused */
40 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
41 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
44 const char * z_filename
;
50 const char * z_format
;
54 struct rule
* z_rules
;
57 struct rule z_untilrule
;
61 extern int emkdir
P((const char * name
, int mode
));
62 extern int getopt
P((int argc
, char * argv
[], const char * options
));
63 extern char * icatalloc
P((char * old
, const char * new));
64 extern char * icpyalloc
P((const char * string
));
65 extern void ifree
P((char * p
));
66 extern char * imalloc
P((int n
));
67 extern char * irealloc
P((char * old
, int n
));
68 extern int link
P((const char * fromname
, const char * toname
));
71 extern char * scheck
P((const char * string
, const char * format
));
73 static void addtt
P((time_t starttime
, int type
));
74 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
76 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
77 static void adjleap
P((void));
78 static void associate
P((void));
79 static int ciequal
P((const char * ap
, const char * bp
));
80 static void convert
P((long val
, char * buf
));
81 static void dolink
P((const char * fromfile
, const char * tofile
));
82 static void eat
P((const char * name
, int num
));
83 static void eats
P((const char * name
, int num
,
84 const char * rname
, int rnum
));
85 static long eitol
P((int i
));
86 static void error
P((const char * message
));
87 static char ** getfields
P((char * buf
));
88 static long gethms
P((const char * string
, const char * errstrng
,
90 static void infile
P((const char * filename
));
91 static void inleap
P((char ** fields
, int nfields
));
92 static void inlink
P((char ** fields
, int nfields
));
93 static void inrule
P((char ** fields
, int nfields
));
94 static int inzcont
P((char ** fields
, int nfields
));
95 static int inzone
P((char ** fields
, int nfields
));
96 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
97 static int itsabbr
P((const char * abbr
, const char * word
));
98 static int itsdir
P((const char * name
));
99 static int lowerit
P((int c
));
100 static char * memcheck
P((char * tocheck
));
101 static int mkdirs
P((char * filename
));
102 static void newabbr
P((const char * abbr
));
103 static long oadd
P((long t1
, long t2
));
104 static void outzone
P((const struct zone
* zp
, int ntzones
));
105 static void puttzcode
P((long code
, FILE * fp
));
106 static int rcomp
P((const genericptr_t leftp
, const genericptr_t rightp
));
107 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
108 static void rulesub
P((struct rule
* rp
,
109 char * loyearp
, char * hiyearp
,
110 char * typep
, char * monthp
,
111 char * dayp
, char * timep
));
112 static void setboundaries
P((void));
113 static time_t tadd
P((time_t t1
, long t2
));
114 static void usage
P((void));
115 static void writezone
P((const char * name
));
116 static int yearistype
P((int year
, const char * type
));
120 static const char * filename
;
124 static time_t max_time
;
127 static time_t min_time
;
130 static const char * rfilename
;
132 static const char * progname
;
135 static int tt_signed
;
147 ** Which fields are which on a Zone line.
155 #define ZF_TILMONTH 6
158 #define ZONE_MINFIELDS 5
159 #define ZONE_MAXFIELDS 9
162 ** Which fields are which on a Zone continuation line.
168 #define ZFC_TILYEAR 3
169 #define ZFC_TILMONTH 4
171 #define ZFC_TILTIME 6
172 #define ZONEC_MINFIELDS 3
173 #define ZONEC_MAXFIELDS 7
176 ** Which files are which on a Rule line.
188 #define RULE_FIELDS 10
191 ** Which fields are which on a Link line.
196 #define LINK_FIELDS 3
199 ** Which fields are which on a Leap line.
208 #define LEAP_FIELDS 7
218 static struct rule
* rules
;
219 static int nrules
; /* number of rules */
221 static struct zone
* zones
;
222 static int nzones
; /* number of zones */
225 const char * l_filename
;
231 static struct link
* links
;
239 static struct lookup
const * byword
P((const char * string
,
240 const struct lookup
* lp
));
242 static struct lookup
const line_codes
[] = {
250 static struct lookup
const mon_names
[] = {
251 { "January", TM_JANUARY
},
252 { "February", TM_FEBRUARY
},
253 { "March", TM_MARCH
},
254 { "April", TM_APRIL
},
258 { "August", TM_AUGUST
},
259 { "September", TM_SEPTEMBER
},
260 { "October", TM_OCTOBER
},
261 { "November", TM_NOVEMBER
},
262 { "December", TM_DECEMBER
},
266 static struct lookup
const wday_names
[] = {
267 { "Sunday", TM_SUNDAY
},
268 { "Monday", TM_MONDAY
},
269 { "Tuesday", TM_TUESDAY
},
270 { "Wednesday", TM_WEDNESDAY
},
271 { "Thursday", TM_THURSDAY
},
272 { "Friday", TM_FRIDAY
},
273 { "Saturday", TM_SATURDAY
},
277 static struct lookup
const lasts
[] = {
278 { "last-Sunday", TM_SUNDAY
},
279 { "last-Monday", TM_MONDAY
},
280 { "last-Tuesday", TM_TUESDAY
},
281 { "last-Wednesday", TM_WEDNESDAY
},
282 { "last-Thursday", TM_THURSDAY
},
283 { "last-Friday", TM_FRIDAY
},
284 { "last-Saturday", TM_SATURDAY
},
288 static struct lookup
const begin_years
[] = {
289 { "minimum", YR_MINIMUM
},
290 { "maximum", YR_MAXIMUM
},
294 static struct lookup
const end_years
[] = {
295 { "minimum", YR_MINIMUM
},
296 { "maximum", YR_MAXIMUM
},
301 static struct lookup
const leap_types
[] = {
303 { "Stationary", FALSE
},
307 static const int len_months
[2][MONSPERYEAR
] = {
308 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
309 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
312 static const int len_years
[2] = {
313 DAYSPERNYEAR
, DAYSPERLYEAR
316 static time_t ats
[TZ_MAX_TIMES
];
317 static unsigned char types
[TZ_MAX_TIMES
];
318 static long gmtoffs
[TZ_MAX_TYPES
];
319 static char isdsts
[TZ_MAX_TYPES
];
320 static unsigned char abbrinds
[TZ_MAX_TYPES
];
321 static char ttisstds
[TZ_MAX_TYPES
];
322 static char chars
[TZ_MAX_CHARS
];
323 static time_t trans
[TZ_MAX_LEAPS
];
324 static long corr
[TZ_MAX_LEAPS
];
325 static char roll
[TZ_MAX_LEAPS
];
328 ** Memory allocation.
336 (void) perror(progname
);
337 (void) exit(EXIT_FAILURE
);
342 #define emalloc(size) memcheck(imalloc(size))
343 #define erealloc(ptr, size) memcheck(irealloc(ptr, size))
344 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
345 #define ecatalloc(oldp, newp) memcheck(icatalloc(oldp, newp))
352 eats(name
, num
, rname
, rnum
)
353 const char * const name
;
355 const char * const rname
;
366 const char * const name
;
369 eats(name
, num
, (char *) NULL
, -1);
374 const char * const string
;
377 ** Match the format of "cc" to allow sh users to
378 ** zic ... 2>&1 | error -t "*" -v
381 (void) fprintf(stderr
, "\"%s\", line %d: %s",
382 filename
, linenum
, string
);
383 if (rfilename
!= NULL
)
384 (void) fprintf(stderr
, " (rule from \"%s\", line %d)",
385 rfilename
, rlinenum
);
386 (void) fprintf(stderr
, "\n");
393 (void) fprintf(stderr
,
394 "%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ] \n\
395 \t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n",
397 (void) exit(EXIT_FAILURE
);
400 static const char * psxrules
;
401 static const char * lcltime
;
402 static const char * directory
;
403 static const char * leapsec
;
404 static const char * yitcommand
;
405 static int sflag
= FALSE
;
416 (void) umask(umask(022) | 022);
417 #endif /* defined unix */
419 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
)
424 if (directory
== NULL
)
427 (void) fprintf(stderr
,
428 "%s: More than one -d option specified\n",
430 (void) exit(EXIT_FAILURE
);
437 (void) fprintf(stderr
,
438 "%s: More than one -l option specified\n",
440 (void) exit(EXIT_FAILURE
);
444 if (psxrules
== NULL
)
447 (void) fprintf(stderr
,
448 "%s: More than one -p option specified\n",
450 (void) exit(EXIT_FAILURE
);
454 if (yitcommand
== NULL
)
457 (void) fprintf(stderr
,
458 "%s: More than one -y option specified\n",
460 (void) exit(EXIT_FAILURE
);
467 (void) fprintf(stderr
,
468 "%s: More than one -L option specified\n",
470 (void) exit(EXIT_FAILURE
);
480 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
481 usage(); /* usage message by request */
482 if (directory
== NULL
)
484 if (yitcommand
== NULL
)
485 yitcommand
= "yearistype";
489 if (optind
< argc
&& leapsec
!= NULL
) {
494 zones
= (struct zone
*) emalloc(0);
495 rules
= (struct rule
*) emalloc(0);
496 links
= (struct link
*) emalloc(0);
497 for (i
= optind
; i
< argc
; ++i
)
500 (void) exit(EXIT_FAILURE
);
502 for (i
= 0; i
< nzones
; i
= j
) {
504 ** Find the next non-continuation zone entry.
506 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
508 outzone(&zones
[i
], j
- i
);
513 for (i
= 0; i
< nlinks
; ++i
)
514 dolink(links
[i
].l_from
, links
[i
].l_to
);
516 dolink(lcltime
, TZDEFAULT
);
517 if (psxrules
!= NULL
)
518 dolink(psxrules
, TZDEFRULES
);
519 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
523 dolink(fromfile
, tofile
)
524 const char * const fromfile
;
525 const char * const tofile
;
527 register char * fromname
;
528 register char * toname
;
530 if (fromfile
[0] == '/')
533 fromname
= ecpyalloc(directory
);
534 fromname
= ecatalloc(fromname
, "/");
535 fromname
= ecatalloc(fromname
, fromfile
);
537 if (tofile
[0] == '/')
540 toname
= ecpyalloc(directory
);
541 toname
= ecatalloc(toname
, "/");
542 toname
= ecatalloc(toname
, tofile
);
545 ** We get to be careful here since
546 ** there's a fair chance of root running us.
549 (void) remove(toname
);
550 if (link(fromname
, toname
) != 0) {
551 if (mkdirs(toname
) != 0)
552 (void) exit(EXIT_FAILURE
);
553 if (link(fromname
, toname
) != 0) {
554 (void) fprintf(stderr
, "%s: Can't link from %s to ",
556 (void) perror(toname
);
557 (void) exit(EXIT_FAILURE
);
560 if (fromname
!= fromfile
)
562 if (toname
!= tofile
)
572 for (bit
= 1; bit
> 0; bit
<<= 1)
574 if (bit
== 0) { /* time_t is an unsigned type */
577 max_time
= ~(time_t) 0;
585 max_time
= -max_time
;
589 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
590 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
592 for (bii
= 1; bii
> 0; bii
<<= 1)
600 const char * const name
;
602 register char * myname
;
605 myname
= ecpyalloc(name
);
606 myname
= ecatalloc(myname
, "/.");
607 accres
= access(myname
, 0);
613 ** Associate sets of rules with zones.
617 ** Sort by rule name.
622 const genericptr_t cp1
;
623 const genericptr_t cp2
;
625 return strcmp(((struct rule
*) cp1
)->r_name
,
626 ((struct rule
*) cp2
)->r_name
);
632 register struct zone
* zp
;
633 register struct rule
* rp
;
634 register int base
, out
;
638 (void) qsort((genericptr_t
) rules
,
639 (qsort_size_t
) nrules
,
640 (qsort_size_t
) sizeof *rules
, rcomp
);
641 for (i
= 0; i
< nzones
; ++i
) {
646 for (base
= 0; base
< nrules
; base
= out
) {
648 for (out
= base
+ 1; out
< nrules
; ++out
)
649 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
651 for (i
= 0; i
< nzones
; ++i
) {
653 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
656 zp
->z_nrules
= out
- base
;
659 for (i
= 0; i
< nzones
; ++i
) {
661 if (zp
->z_nrules
== 0) {
663 ** Maybe we have a local standard time offset.
665 eat(zp
->z_filename
, zp
->z_linenum
);
666 zp
->z_stdoff
= gethms(zp
->z_rule
, "unruly zone", TRUE
);
668 ** Note, though, that if there's no rule,
669 ** a '%s' in the format is a bad thing.
671 if (strchr(zp
->z_format
, '%') != 0)
672 error("%s in ruleless zone");
676 (void) exit(EXIT_FAILURE
);
684 register char ** fields
;
686 register const struct lookup
* lp
;
687 register int nfields
;
688 register int wantcont
;
692 if (strcmp(name
, "-") == 0) {
693 name
= "standard input";
695 } else if ((fp
= fopen(name
, "r")) == NULL
) {
696 (void) fprintf(stderr
, "%s: Can't open ", progname
);
698 (void) exit(EXIT_FAILURE
);
701 for (num
= 1; ; ++num
) {
703 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
705 cp
= strchr(buf
, '\n');
707 error("line too long");
708 (void) exit(EXIT_FAILURE
);
711 fields
= getfields(buf
);
713 while (fields
[nfields
] != NULL
) {
716 if (ciequal(fields
[nfields
], "-"))
717 fields
[nfields
] = nada
;
722 } else if (wantcont
) {
723 wantcont
= inzcont(fields
, nfields
);
725 lp
= byword(fields
[0], line_codes
);
727 error("input line of unknown type");
728 else switch ((int) (lp
->l_value
)) {
730 inrule(fields
, nfields
);
734 wantcont
= inzone(fields
, nfields
);
737 inlink(fields
, nfields
);
742 (void) fprintf(stderr
,
743 "%s: Leap line in non leap seconds file %s\n",
745 else inleap(fields
, nfields
);
748 default: /* "cannot happen" */
749 (void) fprintf(stderr
,
750 "%s: panic: Invalid l_value %d\n",
751 progname
, lp
->l_value
);
752 (void) exit(EXIT_FAILURE
);
755 ifree((char *) fields
);
758 (void) fprintf(stderr
, "%s: Error reading ", progname
);
759 (void) perror(filename
);
760 (void) exit(EXIT_FAILURE
);
762 if (fp
!= stdin
&& fclose(fp
)) {
763 (void) fprintf(stderr
, "%s: Error closing ", progname
);
764 (void) perror(filename
);
765 (void) exit(EXIT_FAILURE
);
768 error("expected continuation line not found");
772 ** Convert a string of one of the forms
773 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
774 ** into a number of seconds.
775 ** A null string maps to zero.
776 ** Call error with errstring and return zero on errors.
780 gethms(string
, errstring
, signable
)
782 const char * const errstring
;
785 int hh
, mm
, ss
, sign
;
787 if (string
== NULL
|| *string
== '\0')
791 else if (*string
== '-') {
795 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
797 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
799 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
800 &hh
, &mm
, &ss
) != 3) {
804 if (hh
< 0 || hh
>= HOURSPERDAY
||
805 mm
< 0 || mm
>= MINSPERHOUR
||
806 ss
< 0 || ss
> SECSPERMIN
) {
811 (eitol(hh
* MINSPERHOUR
+ mm
) *
812 eitol(SECSPERMIN
) + eitol(ss
));
816 inrule(fields
, nfields
)
817 register char ** const fields
;
820 static struct rule r
;
822 if (nfields
!= RULE_FIELDS
) {
823 error("wrong number of fields on Rule line");
826 if (*fields
[RF_NAME
] == '\0') {
827 error("nameless rule");
830 r
.r_filename
= filename
;
831 r
.r_linenum
= linenum
;
832 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], "invalid saved time", TRUE
);
833 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
834 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
835 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
836 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
837 rules
= (struct rule
*) erealloc((char *) rules
,
838 (int) ((nrules
+ 1) * sizeof *rules
));
843 inzone(fields
, nfields
)
844 register char ** const fields
;
850 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
851 error("wrong number of fields on Zone line");
854 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
855 buf
= erealloc(buf
, 132 + strlen(TZDEFAULT
));
857 "\"Zone %s\" line and -l option are mutually exclusive",
862 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
863 buf
= erealloc(buf
, 132 + strlen(TZDEFRULES
));
865 "\"Zone %s\" line and -p option are mutually exclusive",
870 for (i
= 0; i
< nzones
; ++i
)
871 if (zones
[i
].z_name
!= NULL
&&
872 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
873 buf
= erealloc(buf
, 132 +
874 strlen(fields
[ZF_NAME
]) +
875 strlen(zones
[i
].z_filename
));
877 "duplicate zone name %s (file \"%s\", line %d)",
884 return inzsub(fields
, nfields
, FALSE
);
888 inzcont(fields
, nfields
)
889 register char ** const fields
;
892 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
893 error("wrong number of fields on Zone continuation line");
896 return inzsub(fields
, nfields
, TRUE
);
900 inzsub(fields
, nfields
, iscont
)
901 register char ** const fields
;
906 static struct zone z
;
907 register int i_gmtoff
, i_rule
, i_format
;
908 register int i_untilyear
, i_untilmonth
;
909 register int i_untilday
, i_untiltime
;
910 register int hasuntil
;
913 i_gmtoff
= ZFC_GMTOFF
;
915 i_format
= ZFC_FORMAT
;
916 i_untilyear
= ZFC_TILYEAR
;
917 i_untilmonth
= ZFC_TILMONTH
;
918 i_untilday
= ZFC_TILDAY
;
919 i_untiltime
= ZFC_TILTIME
;
922 i_gmtoff
= ZF_GMTOFF
;
924 i_format
= ZF_FORMAT
;
925 i_untilyear
= ZF_TILYEAR
;
926 i_untilmonth
= ZF_TILMONTH
;
927 i_untilday
= ZF_TILDAY
;
928 i_untiltime
= ZF_TILTIME
;
929 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
931 z
.z_filename
= filename
;
932 z
.z_linenum
= linenum
;
933 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], "invalid GMT offset", TRUE
);
934 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
935 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
936 error("invalid abbreviation format");
940 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
941 z
.z_format
= ecpyalloc(fields
[i_format
]);
942 hasuntil
= nfields
> i_untilyear
;
944 z
.z_untilrule
.r_filename
= filename
;
945 z
.z_untilrule
.r_linenum
= linenum
;
946 rulesub(&z
.z_untilrule
,
950 (nfields
> i_untilmonth
) ?
951 fields
[i_untilmonth
] : "Jan",
952 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
953 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
954 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
955 z
.z_untilrule
.r_loyear
);
956 if (iscont
&& nzones
> 0 &&
957 z
.z_untiltime
> min_time
&&
958 z
.z_untiltime
< max_time
&&
959 zones
[nzones
- 1].z_untiltime
> min_time
&&
960 zones
[nzones
- 1].z_untiltime
< max_time
&&
961 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
962 error("Zone continuation line end time is not after end time of previous line");
966 zones
= (struct zone
*) erealloc((char *) zones
,
967 (int) ((nzones
+ 1) * sizeof *zones
));
970 ** If there was an UNTIL field on this line,
971 ** there's more information about the zone on the next line.
977 inleap(fields
, nfields
)
978 register char ** const fields
;
981 register const char * cp
;
982 register const struct lookup
* lp
;
984 int year
, month
, day
;
988 if (nfields
!= LEAP_FIELDS
) {
989 error("wrong number of fields on Leap line");
993 cp
= fields
[LP_YEAR
];
994 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
998 error("invalid leaping year");
1004 i
= len_years
[isleap(j
)];
1008 i
= -len_years
[isleap(j
)];
1010 dayoff
= oadd(dayoff
, eitol(i
));
1012 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1013 error("invalid month name");
1016 month
= lp
->l_value
;
1018 while (j
!= month
) {
1019 i
= len_months
[isleap(year
)][j
];
1020 dayoff
= oadd(dayoff
, eitol(i
));
1023 cp
= fields
[LP_DAY
];
1024 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1025 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1026 error("invalid day of month");
1029 dayoff
= oadd(dayoff
, eitol(day
- 1));
1030 if (dayoff
< 0 && !tt_signed
) {
1031 error("time before zero");
1034 t
= (time_t) dayoff
* SECSPERDAY
;
1036 ** Cheap overflow check.
1038 if (t
/ SECSPERDAY
!= dayoff
) {
1039 error("time overflow");
1042 tod
= gethms(fields
[LP_TIME
], "invalid time of day", FALSE
);
1043 cp
= fields
[LP_CORR
];
1045 register int positive
;
1048 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1051 } else if (strcmp(cp
, "--") == 0) {
1054 } else if (strcmp(cp
, "+") == 0) {
1057 } else if (strcmp(cp
, "++") == 0) {
1061 error("illegal CORRECTION field on Leap line");
1064 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1065 error("illegal Rolling/Stationary field on Leap line");
1068 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1073 inlink(fields
, nfields
)
1074 register char ** const fields
;
1079 if (nfields
!= LINK_FIELDS
) {
1080 error("wrong number of fields on Link line");
1083 if (*fields
[LF_FROM
] == '\0') {
1084 error("blank FROM field on Link line");
1087 if (*fields
[LF_TO
] == '\0') {
1088 error("blank TO field on Link line");
1091 l
.l_filename
= filename
;
1092 l
.l_linenum
= linenum
;
1093 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1094 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1095 links
= (struct link
*) erealloc((char *) links
,
1096 (int) ((nlinks
+ 1) * sizeof *links
));
1097 links
[nlinks
++] = l
;
1101 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1102 register struct rule
* const rp
;
1103 char * const loyearp
;
1104 char * const hiyearp
;
1106 char * const monthp
;
1110 register struct lookup
const * lp
;
1113 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1114 error("invalid month name");
1117 rp
->r_month
= lp
->l_value
;
1118 rp
->r_todisstd
= FALSE
;
1121 cp
+= strlen(cp
) - 1;
1122 switch (lowerit(*cp
)) {
1124 rp
->r_todisstd
= TRUE
;
1128 rp
->r_todisstd
= FALSE
;
1133 rp
->r_tod
= gethms(timep
, "invalid time of day", FALSE
);
1138 if ((lp
= byword(cp
, begin_years
)) != NULL
) switch ((int) lp
->l_value
) {
1140 rp
->r_loyear
= min_int
;
1143 rp
->r_loyear
= max_int
;
1145 default: /* "cannot happen" */
1146 (void) fprintf(stderr
,
1147 "%s: panic: Invalid l_value %d\n",
1148 progname
, lp
->l_value
);
1149 (void) exit(EXIT_FAILURE
);
1150 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1151 error("invalid starting year");
1155 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1157 rp
->r_hiyear
= min_int
;
1160 rp
->r_hiyear
= max_int
;
1163 rp
->r_hiyear
= rp
->r_loyear
;
1165 default: /* "cannot happen" */
1166 (void) fprintf(stderr
,
1167 "%s: panic: Invalid l_value %d\n",
1168 progname
, lp
->l_value
);
1169 (void) exit(EXIT_FAILURE
);
1170 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1171 error("invalid ending year");
1174 if (rp
->r_loyear
> rp
->r_hiyear
) {
1175 error("starting year greater than ending year");
1179 rp
->r_yrtype
= NULL
;
1181 if (rp
->r_loyear
== rp
->r_hiyear
) {
1182 error("typed single year");
1185 rp
->r_yrtype
= ecpyalloc(typep
);
1189 ** Accept things such as:
1195 if ((lp
= byword(dayp
, lasts
)) != NULL
) {
1196 rp
->r_dycode
= DC_DOWLEQ
;
1197 rp
->r_wday
= lp
->l_value
;
1198 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1200 if ((cp
= strchr(dayp
, '<')) != 0)
1201 rp
->r_dycode
= DC_DOWLEQ
;
1202 else if ((cp
= strchr(dayp
, '>')) != 0)
1203 rp
->r_dycode
= DC_DOWGEQ
;
1206 rp
->r_dycode
= DC_DOM
;
1208 if (rp
->r_dycode
!= DC_DOM
) {
1211 error("invalid day of month");
1214 if ((lp
= byword(dayp
, wday_names
)) == NULL
) {
1215 error("invalid weekday name");
1218 rp
->r_wday
= lp
->l_value
;
1220 if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1221 rp
->r_dayofmonth
<= 0 ||
1222 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1223 error("invalid day of month");
1235 register long shift
;
1237 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1238 buf
[i
] = val
>> shift
;
1249 (void) fwrite((genericptr_t
) buf
,
1250 (fwrite_size_t
) sizeof buf
,
1251 (fwrite_size_t
) 1, fp
);
1256 const char * const name
;
1260 static char * fullname
;
1261 static struct tzhead tzh
;
1263 fullname
= erealloc(fullname
,
1264 strlen(directory
) + 1 + strlen(name
) + 1);
1265 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1266 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1267 if (mkdirs(fullname
) != 0)
1268 (void) exit(EXIT_FAILURE
);
1269 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1270 (void) fprintf(stderr
, "%s: Can't create ", progname
);
1271 (void) perror(fullname
);
1272 (void) exit(EXIT_FAILURE
);
1275 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1276 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1277 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1278 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1279 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1280 (void) fwrite((genericptr_t
) &tzh
,
1281 (fwrite_size_t
) sizeof tzh
,
1282 (fwrite_size_t
) 1, fp
);
1283 for (i
= 0; i
< timecnt
; ++i
) {
1286 if (ats
[i
] >= trans
[j
]) {
1287 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1290 puttzcode((long) ats
[i
], fp
);
1293 (void) fwrite((genericptr_t
) types
,
1294 (fwrite_size_t
) sizeof types
[0],
1295 (fwrite_size_t
) timecnt
, fp
);
1296 for (i
= 0; i
< typecnt
; ++i
) {
1297 puttzcode((long) gmtoffs
[i
], fp
);
1298 (void) putc(isdsts
[i
], fp
);
1299 (void) putc(abbrinds
[i
], fp
);
1302 (void) fwrite((genericptr_t
) chars
,
1303 (fwrite_size_t
) sizeof chars
[0],
1304 (fwrite_size_t
) charcnt
, fp
);
1305 for (i
= 0; i
< leapcnt
; ++i
) {
1307 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1310 if (++j
>= typecnt
) {
1316 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1320 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1321 } else puttzcode((long) trans
[i
], fp
);
1322 puttzcode((long) corr
[i
], fp
);
1324 for (i
= 0; i
< typecnt
; ++i
)
1325 (void) putc(ttisstds
[i
], fp
);
1326 if (ferror(fp
) || fclose(fp
)) {
1327 (void) fprintf(stderr
, "%s: Write error on ", progname
);
1328 (void) perror(fullname
);
1329 (void) exit(EXIT_FAILURE
);
1334 outzone(zpfirst
, zonecount
)
1335 const struct zone
* const zpfirst
;
1336 const int zonecount
;
1338 register const struct zone
* zp
;
1339 register struct rule
* rp
;
1341 register int usestart
, useuntil
;
1342 register time_t starttime
, untiltime
;
1343 register long gmtoff
;
1344 register long stdoff
;
1346 register long startoff
;
1347 register int startisdst
;
1348 register int startttisstd
;
1350 char startbuf
[BUFSIZ
];
1353 ** Now. . .finally. . .generate some useful data!
1359 ** A guess that may well be corrected later.
1363 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1364 ** for noting the need to unconditionally initialize startttisstd.
1366 startttisstd
= FALSE
;
1369 #endif /* defined lint */
1370 for (i
= 0; i
< zonecount
; ++i
) {
1372 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1373 useuntil
= i
< (zonecount
- 1);
1374 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1376 gmtoff
= zp
->z_gmtoff
;
1377 eat(zp
->z_filename
, zp
->z_linenum
);
1379 if (zp
->z_nrules
== 0) {
1380 stdoff
= zp
->z_stdoff
;
1381 (void) strcpy(startbuf
, zp
->z_format
);
1382 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1383 startbuf
, stdoff
!= 0, startttisstd
);
1385 addtt(starttime
, type
);
1386 else if (stdoff
!= 0)
1387 addtt(min_time
, type
);
1388 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1389 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1392 ** Mark which rules to do in the current year.
1393 ** For those to do, calculate rpytime(rp, year);
1395 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1396 rp
= &zp
->z_rules
[j
];
1397 eats(zp
->z_filename
, zp
->z_linenum
,
1398 rp
->r_filename
, rp
->r_linenum
);
1399 rp
->r_todo
= year
>= rp
->r_loyear
&&
1400 year
<= rp
->r_hiyear
&&
1401 yearistype(year
, rp
->r_yrtype
);
1403 rp
->r_temp
= rpytime(rp
, year
);
1407 register time_t jtime
, ktime
;
1408 register long offset
;
1413 ** Turn untiltime into GMT
1414 ** assuming the current gmtoff and
1417 untiltime
= tadd(zp
->z_untiltime
,
1419 if (!zp
->z_untilrule
.r_todisstd
)
1420 untiltime
= tadd(untiltime
,
1424 ** Find the rule (of those to do, if any)
1425 ** that takes effect earliest in the year.
1430 #endif /* defined lint */
1431 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1432 rp
= &zp
->z_rules
[j
];
1435 eats(zp
->z_filename
, zp
->z_linenum
,
1436 rp
->r_filename
, rp
->r_linenum
);
1438 if (!rp
->r_todisstd
)
1439 offset
= oadd(offset
, stdoff
);
1441 if (jtime
== min_time
||
1444 jtime
= tadd(jtime
, -offset
);
1445 if (k
< 0 || jtime
< ktime
) {
1451 break; /* go on to next year */
1452 rp
= &zp
->z_rules
[k
];
1454 if (useuntil
&& ktime
>= untiltime
)
1457 if (ktime
< starttime
) {
1458 stdoff
= rp
->r_stdoff
;
1459 startoff
= oadd(zp
->z_gmtoff
,
1461 (void) sprintf(startbuf
, zp
->z_format
,
1463 startisdst
= rp
->r_stdoff
!= 0;
1467 if (ktime
!= starttime
) {
1468 if (startisdst
< 0 &&
1470 (zp
- 1)->z_gmtoff
) {
1471 type
= (timecnt
== 0) ? 0 :
1473 startoff
= oadd(gmtoffs
[type
],
1474 -(zp
- 1)->z_gmtoff
);
1475 startisdst
= startoff
!= 0;
1476 startoff
= oadd(startoff
,
1478 (void) strcpy(startbuf
,
1479 &chars
[abbrinds
[type
]]);
1481 if (startisdst
>= 0)
1482 addtt(starttime
, addtype(startoff
, startbuf
, startisdst
, startttisstd
));
1485 eats(zp
->z_filename
, zp
->z_linenum
,
1486 rp
->r_filename
, rp
->r_linenum
);
1487 (void) sprintf(buf
, zp
->z_format
,
1489 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1490 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1493 stdoff
= rp
->r_stdoff
;
1497 ** Now we may get to set starttime for the next zone line.
1500 starttime
= tadd(zp
->z_untiltime
, -gmtoff
);
1501 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1503 starttime
= tadd(starttime
, -stdoff
);
1506 writezone(zpfirst
->z_name
);
1510 addtt(starttime
, type
)
1511 const time_t starttime
;
1514 if (timecnt
!= 0 && type
== types
[timecnt
- 1])
1515 return; /* easy enough! */
1516 if (timecnt
== 0 && type
== 0 && isdsts
[0] == 0)
1517 return; /* handled by default rule */
1518 if (timecnt
>= TZ_MAX_TIMES
) {
1519 error("too many transitions?!");
1520 (void) exit(EXIT_FAILURE
);
1522 ats
[timecnt
] = starttime
;
1523 types
[timecnt
] = type
;
1528 addtype(gmtoff
, abbr
, isdst
, ttisstd
)
1530 const char * const abbr
;
1537 ** See if there's already an entry for this zone type.
1538 ** If so, just return its index.
1540 for (i
= 0; i
< typecnt
; ++i
) {
1541 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1542 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1543 ttisstd
== ttisstds
[i
])
1547 ** There isn't one; add a new one, unless there are already too
1550 if (typecnt
>= TZ_MAX_TYPES
) {
1551 error("too many local time types");
1552 (void) exit(EXIT_FAILURE
);
1554 gmtoffs
[i
] = gmtoff
;
1556 ttisstds
[i
] = ttisstd
;
1558 for (j
= 0; j
< charcnt
; ++j
)
1559 if (strcmp(&chars
[j
], abbr
) == 0)
1569 leapadd(t
, positive
, rolling
, count
)
1577 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1578 error("too many leap seconds");
1579 (void) exit(EXIT_FAILURE
);
1581 for (i
= 0; i
< leapcnt
; ++i
)
1582 if (t
<= trans
[i
]) {
1583 if (t
== trans
[i
]) {
1584 error("repeated leap second moment");
1585 (void) exit(EXIT_FAILURE
);
1590 for (j
= leapcnt
; j
> i
; --j
) {
1591 trans
[j
] = trans
[j
- 1];
1592 corr
[j
] = corr
[j
- 1];
1593 roll
[j
] = roll
[j
- 1];
1596 corr
[i
] = positive
? 1L : eitol(-count
);
1599 } while (positive
&& --count
!= 0);
1606 register long last
= 0;
1609 ** propagate leap seconds forward
1611 for (i
= 0; i
< leapcnt
; ++i
) {
1612 trans
[i
] = tadd(trans
[i
], last
);
1613 last
= corr
[i
] += last
;
1618 yearistype(year
, type
)
1620 const char * const type
;
1625 if (type
== NULL
|| *type
== '\0')
1627 if (strcmp(type
, "uspres") == 0)
1628 return (year
% 4) == 0;
1629 if (strcmp(type
, "nonpres") == 0)
1630 return (year
% 4) != 0;
1631 buf
= erealloc(buf
, 132 + strlen(yitcommand
) + strlen(type
));
1632 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1633 result
= system(buf
);
1636 if (result
== (1 << 8))
1638 error("Wild result from command execution");
1639 (void) fprintf(stderr
, "%s: command was '%s', result was %d\n",
1640 progname
, buf
, result
);
1642 (void) exit(EXIT_FAILURE
);
1649 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1653 ciequal(ap
, bp
) /* case-insensitive equality */
1654 register const char * ap
;
1655 register const char * bp
;
1657 while (lowerit(*ap
) == lowerit(*bp
++))
1665 register const char * abbr
;
1666 register const char * word
;
1668 if (lowerit(*abbr
) != lowerit(*word
))
1671 while (*++abbr
!= '\0')
1672 do if (*word
== '\0')
1674 while (lowerit(*word
++) != lowerit(*abbr
));
1678 static const struct lookup
*
1680 register const char * const word
;
1681 register const struct lookup
* const table
;
1683 register const struct lookup
* foundlp
;
1684 register const struct lookup
* lp
;
1686 if (word
== NULL
|| table
== NULL
)
1689 ** Look for exact match.
1691 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1692 if (ciequal(word
, lp
->l_word
))
1695 ** Look for inexact match.
1698 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1699 if (itsabbr(word
, lp
->l_word
))
1700 if (foundlp
== NULL
)
1702 else return NULL
; /* multiple inexact matches */
1711 register char ** array
;
1716 array
= (char **) emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1719 while (isascii(*cp
) && isspace(*cp
))
1721 if (*cp
== '\0' || *cp
== '#')
1723 array
[nsubs
++] = dp
= cp
;
1725 if ((*dp
= *cp
++) != '"')
1727 else while ((*dp
= *cp
++) != '"')
1730 else error("Odd number of quotation marks");
1731 } while (*cp
!= '\0' && *cp
!= '#' &&
1732 (!isascii(*cp
) || !isspace(*cp
)));
1733 if (isascii(*cp
) && isspace(*cp
))
1737 array
[nsubs
] = NULL
;
1749 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1750 error("time overflow");
1751 (void) exit(EXIT_FAILURE
);
1763 if (t1
== max_time
&& t2
> 0)
1765 if (t1
== min_time
&& t2
< 0)
1768 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1769 error("time overflow");
1770 (void) exit(EXIT_FAILURE
);
1776 ** Given a rule, and a year, compute the date - in seconds since January 1,
1777 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1781 rpytime(rp
, wantedy
)
1782 register const struct rule
* const rp
;
1783 register const int wantedy
;
1785 register int y
, m
, i
;
1786 register long dayoff
; /* with a nod to Margaret O. */
1789 if (wantedy
== min_int
)
1791 if (wantedy
== max_int
)
1796 while (wantedy
!= y
) {
1798 i
= len_years
[isleap(y
)];
1802 i
= -len_years
[isleap(y
)];
1804 dayoff
= oadd(dayoff
, eitol(i
));
1806 while (m
!= rp
->r_month
) {
1807 i
= len_months
[isleap(y
)][m
];
1808 dayoff
= oadd(dayoff
, eitol(i
));
1811 i
= rp
->r_dayofmonth
;
1812 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
1813 if (rp
->r_dycode
== DC_DOWLEQ
)
1816 error("use of 2/29 in non leap-year");
1817 (void) exit(EXIT_FAILURE
);
1821 dayoff
= oadd(dayoff
, eitol(i
));
1822 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
1825 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
1826 wday
= eitol(EPOCH_WDAY
);
1828 ** Don't trust mod of negative numbers.
1831 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
1833 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
1835 wday
+= LDAYSPERWEEK
;
1837 while (wday
!= eitol(rp
->r_wday
))
1838 if (rp
->r_dycode
== DC_DOWGEQ
) {
1839 dayoff
= oadd(dayoff
, (long) 1);
1840 if (++wday
>= LDAYSPERWEEK
)
1844 dayoff
= oadd(dayoff
, (long) -1);
1846 wday
= LDAYSPERWEEK
- 1;
1849 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
1850 error("no day in month matches rule");
1851 (void) exit(EXIT_FAILURE
);
1854 if (dayoff
< 0 && !tt_signed
)
1856 t
= (time_t) dayoff
* SECSPERDAY
;
1858 ** Cheap overflow check.
1860 if (t
/ SECSPERDAY
!= dayoff
)
1861 return (dayoff
> 0) ? max_time
: min_time
;
1862 return tadd(t
, rp
->r_tod
);
1867 const char * const string
;
1871 i
= strlen(string
) + 1;
1872 if (charcnt
+ i
> TZ_MAX_CHARS
) {
1873 error("too many, or too long, time zone abbreviations");
1874 (void) exit(EXIT_FAILURE
);
1876 (void) strcpy(&chars
[charcnt
], string
);
1877 charcnt
+= eitol(i
);
1882 char * const argname
;
1884 register char * name
;
1887 if (argname
== NULL
|| *argname
== '\0')
1889 cp
= name
= ecpyalloc(argname
);
1890 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
1894 ** MS-DOS drive specifier?
1896 if (strlen(name
) == 2 && isascii(name
[0]) &&
1897 isalpha(name
[0]) && name
[1] == ':') {
1901 #endif /* !defined unix */
1902 if (!itsdir(name
)) {
1904 ** It doesn't seem to exist, so we try to create it.
1906 if (emkdir(name
, 0755) != 0) {
1907 (void) fprintf(stderr
,
1908 "%s: Can't create directory ",
1910 (void) perror(name
);
1928 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
1929 (void) fprintf(stderr
,
1930 "%s: %d did not sign extend correctly\n",
1932 (void) exit(EXIT_FAILURE
);
1938 ** UNIX is a registered trademark of AT&T.