3 static char elsieid
[] = "@(#)zic.c 7.28";
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 int r_todisuniv
; /* above is universal time if TRUE */
29 /* or local time if FALSE */
30 long r_stdoff
; /* offset from standard time */
31 const char * r_abbrvar
; /* variable part of abbreviation */
33 int r_todo
; /* a rule to do (used in outzone) */
34 time_t r_temp
; /* used in outzone */
38 ** r_dycode r_dayofmonth r_wday
41 #define DC_DOM 0 /* 1..31 */ /* unused */
42 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
43 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
46 const char * z_filename
;
52 const char * z_format
;
56 struct rule
* z_rules
;
59 struct rule z_untilrule
;
63 extern int emkdir
P((const char * name
, int mode
));
64 extern int getopt
P((int argc
, char * argv
[], const char * options
));
65 extern char * icatalloc
P((char * old
, const char * new));
66 extern char * icpyalloc
P((const char * string
));
67 extern void ifree
P((char * p
));
68 extern char * imalloc
P((int n
));
69 extern void * irealloc
P((void * old
, int n
));
70 extern int link
P((const char * fromname
, const char * toname
));
73 extern char * scheck
P((const char * string
, const char * format
));
75 static void addtt
P((time_t starttime
, int type
));
76 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
78 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
79 static void adjleap
P((void));
80 static void associate
P((void));
81 static int ciequal
P((const char * ap
, const char * bp
));
82 static void convert
P((long val
, char * buf
));
83 static void dolink
P((const char * fromfile
, const char * tofile
));
84 static void eat
P((const char * name
, int num
));
85 static void eats
P((const char * name
, int num
,
86 const char * rname
, int rnum
));
87 static long eitol
P((int i
));
88 static void error
P((const char * message
));
89 static char ** getfields
P((char * buf
));
90 static long gethms
P((const char * string
, const char * errstrng
,
92 static void infile
P((const char * filename
));
93 static void inleap
P((char ** fields
, int nfields
));
94 static void inlink
P((char ** fields
, int nfields
));
95 static void inrule
P((char ** fields
, int nfields
));
96 static int inzcont
P((char ** fields
, int nfields
));
97 static int inzone
P((char ** fields
, int nfields
));
98 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
99 static int itsabbr
P((const char * abbr
, const char * word
));
100 static int itsdir
P((const char * name
));
101 static int lowerit
P((int c
));
102 static char * memcheck
P((char * tocheck
));
103 static int mkdirs
P((char * filename
));
104 static void newabbr
P((const char * abbr
));
105 static long oadd
P((long t1
, long t2
));
106 static void outzone
P((const struct zone
* zp
, int ntzones
));
107 static void puttzcode
P((long code
, FILE * fp
));
108 static int rcomp
P((const genericptr_T leftp
, const genericptr_T rightp
));
109 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
110 static void rulesub
P((struct rule
* rp
,
111 const char * loyearp
, const char * hiyearp
,
112 const char * typep
, const char * monthp
,
113 const char * dayp
, const char * timep
));
114 static void setboundaries
P((void));
115 static time_t tadd
P((time_t t1
, long t2
));
116 static void usage
P((void));
117 static void writezone
P((const char * name
));
118 static int yearistype
P((int year
, const char * type
));
122 static const char * filename
;
126 static time_t max_time
;
129 static time_t min_time
;
132 static const char * rfilename
;
134 static const char * progname
;
137 static int tt_signed
;
149 ** Which fields are which on a Zone line.
157 #define ZF_TILMONTH 6
160 #define ZONE_MINFIELDS 5
161 #define ZONE_MAXFIELDS 9
164 ** Which fields are which on a Zone continuation line.
170 #define ZFC_TILYEAR 3
171 #define ZFC_TILMONTH 4
173 #define ZFC_TILTIME 6
174 #define ZONEC_MINFIELDS 3
175 #define ZONEC_MAXFIELDS 7
178 ** Which files are which on a Rule line.
190 #define RULE_FIELDS 10
193 ** Which fields are which on a Link line.
198 #define LINK_FIELDS 3
201 ** Which fields are which on a Leap line.
210 #define LEAP_FIELDS 7
220 static struct rule
* rules
;
221 static int nrules
; /* number of rules */
223 static struct zone
* zones
;
224 static int nzones
; /* number of zones */
227 const char * l_filename
;
233 static struct link
* links
;
241 static struct lookup
const * byword
P((const char * string
,
242 const struct lookup
* lp
));
244 static struct lookup
const line_codes
[] = {
252 static struct lookup
const mon_names
[] = {
253 { "January", TM_JANUARY
},
254 { "February", TM_FEBRUARY
},
255 { "March", TM_MARCH
},
256 { "April", TM_APRIL
},
260 { "August", TM_AUGUST
},
261 { "September", TM_SEPTEMBER
},
262 { "October", TM_OCTOBER
},
263 { "November", TM_NOVEMBER
},
264 { "December", TM_DECEMBER
},
268 static struct lookup
const wday_names
[] = {
269 { "Sunday", TM_SUNDAY
},
270 { "Monday", TM_MONDAY
},
271 { "Tuesday", TM_TUESDAY
},
272 { "Wednesday", TM_WEDNESDAY
},
273 { "Thursday", TM_THURSDAY
},
274 { "Friday", TM_FRIDAY
},
275 { "Saturday", TM_SATURDAY
},
279 static struct lookup
const lasts
[] = {
280 { "last-Sunday", TM_SUNDAY
},
281 { "last-Monday", TM_MONDAY
},
282 { "last-Tuesday", TM_TUESDAY
},
283 { "last-Wednesday", TM_WEDNESDAY
},
284 { "last-Thursday", TM_THURSDAY
},
285 { "last-Friday", TM_FRIDAY
},
286 { "last-Saturday", TM_SATURDAY
},
290 static struct lookup
const begin_years
[] = {
291 { "minimum", YR_MINIMUM
},
292 { "maximum", YR_MAXIMUM
},
296 static struct lookup
const end_years
[] = {
297 { "minimum", YR_MINIMUM
},
298 { "maximum", YR_MAXIMUM
},
303 static struct lookup
const leap_types
[] = {
305 { "Stationary", FALSE
},
309 static const int len_months
[2][MONSPERYEAR
] = {
310 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
311 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
314 static const int len_years
[2] = {
315 DAYSPERNYEAR
, DAYSPERLYEAR
318 static time_t ats
[TZ_MAX_TIMES
];
319 static unsigned char types
[TZ_MAX_TIMES
];
320 static long gmtoffs
[TZ_MAX_TYPES
];
321 static char isdsts
[TZ_MAX_TYPES
];
322 static unsigned char abbrinds
[TZ_MAX_TYPES
];
323 static char ttisstds
[TZ_MAX_TYPES
];
324 static char chars
[TZ_MAX_CHARS
];
325 static time_t trans
[TZ_MAX_LEAPS
];
326 static long corr
[TZ_MAX_LEAPS
];
327 static char roll
[TZ_MAX_LEAPS
];
330 ** Memory allocation.
338 (void) perror(progname
);
339 (void) exit(EXIT_FAILURE
);
344 #define emalloc(size) memcheck(imalloc(size))
345 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
346 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
347 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
354 eats(name
, num
, rname
, rnum
)
355 const char * const name
;
357 const char * const rname
;
368 const char * const name
;
371 eats(name
, num
, (char *) NULL
, -1);
376 const char * const string
;
379 ** Match the format of "cc" to allow sh users to
380 ** zic ... 2>&1 | error -t "*" -v
383 (void) fprintf(stderr
, "\"%s\", line %d: %s",
384 filename
, linenum
, string
);
385 if (rfilename
!= NULL
)
386 (void) fprintf(stderr
, " (rule from \"%s\", line %d)",
387 rfilename
, rlinenum
);
388 (void) fprintf(stderr
, "\n");
395 (void) fprintf(stderr
,
396 "%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ] \n\
397 \t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n",
399 (void) exit(EXIT_FAILURE
);
402 static const char * psxrules
;
403 static const char * lcltime
;
404 static const char * directory
;
405 static const char * leapsec
;
406 static const char * yitcommand
;
407 static int sflag
= FALSE
;
418 (void) umask(umask(022) | 022);
419 #endif /* defined unix */
421 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
)
426 if (directory
== NULL
)
429 (void) fprintf(stderr
,
430 "%s: More than one -d option specified\n",
432 (void) exit(EXIT_FAILURE
);
439 (void) fprintf(stderr
,
440 "%s: More than one -l option specified\n",
442 (void) exit(EXIT_FAILURE
);
446 if (psxrules
== NULL
)
449 (void) fprintf(stderr
,
450 "%s: More than one -p option specified\n",
452 (void) exit(EXIT_FAILURE
);
456 if (yitcommand
== NULL
)
459 (void) fprintf(stderr
,
460 "%s: More than one -y option specified\n",
462 (void) exit(EXIT_FAILURE
);
469 (void) fprintf(stderr
,
470 "%s: More than one -L option specified\n",
472 (void) exit(EXIT_FAILURE
);
482 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
483 usage(); /* usage message by request */
484 if (directory
== NULL
)
486 if (yitcommand
== NULL
)
487 yitcommand
= "yearistype";
491 if (optind
< argc
&& leapsec
!= NULL
) {
496 for (i
= optind
; i
< argc
; ++i
)
499 (void) exit(EXIT_FAILURE
);
501 for (i
= 0; i
< nzones
; i
= j
) {
503 ** Find the next non-continuation zone entry.
505 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
507 outzone(&zones
[i
], j
- i
);
512 for (i
= 0; i
< nlinks
; ++i
)
513 dolink(links
[i
].l_from
, links
[i
].l_to
);
515 dolink(lcltime
, TZDEFAULT
);
516 if (psxrules
!= NULL
)
517 dolink(psxrules
, TZDEFRULES
);
518 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
522 dolink(fromfile
, tofile
)
523 const char * const fromfile
;
524 const char * const tofile
;
526 register char * fromname
;
527 register char * toname
;
529 if (fromfile
[0] == '/')
530 fromname
= ecpyalloc(fromfile
);
532 fromname
= ecpyalloc(directory
);
533 fromname
= ecatalloc(fromname
, "/");
534 fromname
= ecatalloc(fromname
, fromfile
);
536 if (tofile
[0] == '/')
537 toname
= ecpyalloc(tofile
);
539 toname
= ecpyalloc(directory
);
540 toname
= ecatalloc(toname
, "/");
541 toname
= ecatalloc(toname
, tofile
);
544 ** We get to be careful here since
545 ** there's a fair chance of root running us.
548 (void) remove(toname
);
549 if (link(fromname
, toname
) != 0) {
550 if (mkdirs(toname
) != 0)
551 (void) exit(EXIT_FAILURE
);
552 if (link(fromname
, toname
) != 0) {
553 (void) fprintf(stderr
, "%s: Can't link from %s to ",
555 (void) perror(toname
);
556 (void) exit(EXIT_FAILURE
);
564 setboundaries
P((void))
569 for (bit
= 1; bit
> 0; bit
<<= 1)
571 if (bit
== 0) { /* time_t is an unsigned type */
574 max_time
= ~(time_t) 0;
582 max_time
= -max_time
;
586 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
587 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
589 for (bii
= 1; bii
> 0; bii
<<= 1)
597 const char * const name
;
599 register char * myname
;
602 myname
= ecpyalloc(name
);
603 myname
= ecatalloc(myname
, "/.");
604 accres
= access(myname
, 0);
610 ** Associate sets of rules with zones.
614 ** Sort by rule name.
619 const genericptr_T cp1
;
620 const genericptr_T cp2
;
622 return strcmp(((struct rule
*) cp1
)->r_name
,
623 ((struct rule
*) cp2
)->r_name
);
629 register struct zone
* zp
;
630 register struct rule
* rp
;
631 register int base
, out
;
635 (void) qsort((genericptr_T
) rules
,
636 (qsort_size_T
) nrules
,
637 (qsort_size_T
) sizeof *rules
, rcomp
);
638 for (i
= 0; i
< nzones
; ++i
) {
643 for (base
= 0; base
< nrules
; base
= out
) {
645 for (out
= base
+ 1; out
< nrules
; ++out
)
646 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
648 for (i
= 0; i
< nzones
; ++i
) {
650 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
653 zp
->z_nrules
= out
- base
;
656 for (i
= 0; i
< nzones
; ++i
) {
658 if (zp
->z_nrules
== 0) {
660 ** Maybe we have a local standard time offset.
662 eat(zp
->z_filename
, zp
->z_linenum
);
663 zp
->z_stdoff
= gethms(zp
->z_rule
, "unruly zone", TRUE
);
665 ** Note, though, that if there's no rule,
666 ** a '%s' in the format is a bad thing.
668 if (strchr(zp
->z_format
, '%') != 0)
669 error("%s in ruleless zone");
673 (void) exit(EXIT_FAILURE
);
681 register char ** fields
;
683 register const struct lookup
* lp
;
684 register int nfields
;
685 register int wantcont
;
689 if (strcmp(name
, "-") == 0) {
690 name
= "standard input";
692 } else if ((fp
= fopen(name
, "r")) == NULL
) {
693 (void) fprintf(stderr
, "%s: Can't open ", progname
);
695 (void) exit(EXIT_FAILURE
);
698 for (num
= 1; ; ++num
) {
700 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
702 cp
= strchr(buf
, '\n');
704 error("line too long");
705 (void) exit(EXIT_FAILURE
);
708 fields
= getfields(buf
);
710 while (fields
[nfields
] != NULL
) {
713 if (ciequal(fields
[nfields
], "-"))
714 fields
[nfields
] = &nada
;
719 } else if (wantcont
) {
720 wantcont
= inzcont(fields
, nfields
);
722 lp
= byword(fields
[0], line_codes
);
724 error("input line of unknown type");
725 else switch ((int) (lp
->l_value
)) {
727 inrule(fields
, nfields
);
731 wantcont
= inzone(fields
, nfields
);
734 inlink(fields
, nfields
);
739 (void) fprintf(stderr
,
740 "%s: Leap line in non leap seconds file %s\n",
742 else inleap(fields
, nfields
);
745 default: /* "cannot happen" */
746 (void) fprintf(stderr
,
747 "%s: panic: Invalid l_value %d\n",
748 progname
, lp
->l_value
);
749 (void) exit(EXIT_FAILURE
);
752 ifree((char *) fields
);
755 (void) fprintf(stderr
, "%s: Error reading ", progname
);
756 (void) perror(filename
);
757 (void) exit(EXIT_FAILURE
);
759 if (fp
!= stdin
&& fclose(fp
)) {
760 (void) fprintf(stderr
, "%s: Error closing ", progname
);
761 (void) perror(filename
);
762 (void) exit(EXIT_FAILURE
);
765 error("expected continuation line not found");
769 ** Convert a string of one of the forms
770 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
771 ** into a number of seconds.
772 ** A null string maps to zero.
773 ** Call error with errstring and return zero on errors.
777 gethms(string
, errstring
, signable
)
779 const char * const errstring
;
782 int hh
, mm
, ss
, sign
;
784 if (string
== NULL
|| *string
== '\0')
788 else if (*string
== '-') {
792 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
794 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
796 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
797 &hh
, &mm
, &ss
) != 3) {
801 if (hh
< 0 || hh
>= HOURSPERDAY
||
802 mm
< 0 || mm
>= MINSPERHOUR
||
803 ss
< 0 || ss
> SECSPERMIN
) {
808 (eitol(hh
* MINSPERHOUR
+ mm
) *
809 eitol(SECSPERMIN
) + eitol(ss
));
813 inrule(fields
, nfields
)
814 register char ** const fields
;
817 static struct rule r
;
819 if (nfields
!= RULE_FIELDS
) {
820 error("wrong number of fields on Rule line");
823 if (*fields
[RF_NAME
] == '\0') {
824 error("nameless rule");
827 r
.r_filename
= filename
;
828 r
.r_linenum
= linenum
;
829 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], "invalid saved time", TRUE
);
830 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
831 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
832 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
833 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
834 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
835 (int) ((nrules
+ 1) * sizeof *rules
));
840 inzone(fields
, nfields
)
841 register char ** const fields
;
847 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
848 error("wrong number of fields on Zone line");
851 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
852 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
854 "\"Zone %s\" line and -l option are mutually exclusive",
859 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
860 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
862 "\"Zone %s\" line and -p option are mutually exclusive",
867 for (i
= 0; i
< nzones
; ++i
)
868 if (zones
[i
].z_name
!= NULL
&&
869 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
870 buf
= erealloc(buf
, (int) (132 +
871 strlen(fields
[ZF_NAME
]) +
872 strlen(zones
[i
].z_filename
)));
874 "duplicate zone name %s (file \"%s\", line %d)",
881 return inzsub(fields
, nfields
, FALSE
);
885 inzcont(fields
, nfields
)
886 register char ** const fields
;
889 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
890 error("wrong number of fields on Zone continuation line");
893 return inzsub(fields
, nfields
, TRUE
);
897 inzsub(fields
, nfields
, iscont
)
898 register char ** const fields
;
903 static struct zone z
;
904 register int i_gmtoff
, i_rule
, i_format
;
905 register int i_untilyear
, i_untilmonth
;
906 register int i_untilday
, i_untiltime
;
907 register int hasuntil
;
910 i_gmtoff
= ZFC_GMTOFF
;
912 i_format
= ZFC_FORMAT
;
913 i_untilyear
= ZFC_TILYEAR
;
914 i_untilmonth
= ZFC_TILMONTH
;
915 i_untilday
= ZFC_TILDAY
;
916 i_untiltime
= ZFC_TILTIME
;
919 i_gmtoff
= ZF_GMTOFF
;
921 i_format
= ZF_FORMAT
;
922 i_untilyear
= ZF_TILYEAR
;
923 i_untilmonth
= ZF_TILMONTH
;
924 i_untilday
= ZF_TILDAY
;
925 i_untiltime
= ZF_TILTIME
;
926 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
928 z
.z_filename
= filename
;
929 z
.z_linenum
= linenum
;
930 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], "invalid GMT offset", TRUE
);
931 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
932 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
933 error("invalid abbreviation format");
937 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
938 z
.z_format
= ecpyalloc(fields
[i_format
]);
939 hasuntil
= nfields
> i_untilyear
;
941 z
.z_untilrule
.r_filename
= filename
;
942 z
.z_untilrule
.r_linenum
= linenum
;
943 rulesub(&z
.z_untilrule
,
947 (nfields
> i_untilmonth
) ?
948 fields
[i_untilmonth
] : "Jan",
949 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
950 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
951 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
952 z
.z_untilrule
.r_loyear
);
953 if (iscont
&& nzones
> 0 &&
954 z
.z_untiltime
> min_time
&&
955 z
.z_untiltime
< max_time
&&
956 zones
[nzones
- 1].z_untiltime
> min_time
&&
957 zones
[nzones
- 1].z_untiltime
< max_time
&&
958 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
959 error("Zone continuation line end time is not after end time of previous line");
963 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
964 (int) ((nzones
+ 1) * sizeof *zones
));
967 ** If there was an UNTIL field on this line,
968 ** there's more information about the zone on the next line.
974 inleap(fields
, nfields
)
975 register char ** const fields
;
978 register const char * cp
;
979 register const struct lookup
* lp
;
981 int year
, month
, day
;
985 if (nfields
!= LEAP_FIELDS
) {
986 error("wrong number of fields on Leap line");
990 cp
= fields
[LP_YEAR
];
991 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
995 error("invalid leaping year");
1001 i
= len_years
[isleap(j
)];
1005 i
= -len_years
[isleap(j
)];
1007 dayoff
= oadd(dayoff
, eitol(i
));
1009 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1010 error("invalid month name");
1013 month
= lp
->l_value
;
1015 while (j
!= month
) {
1016 i
= len_months
[isleap(year
)][j
];
1017 dayoff
= oadd(dayoff
, eitol(i
));
1020 cp
= fields
[LP_DAY
];
1021 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1022 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1023 error("invalid day of month");
1026 dayoff
= oadd(dayoff
, eitol(day
- 1));
1027 if (dayoff
< 0 && !tt_signed
) {
1028 error("time before zero");
1031 t
= (time_t) dayoff
* SECSPERDAY
;
1033 ** Cheap overflow check.
1035 if (t
/ SECSPERDAY
!= dayoff
) {
1036 error("time overflow");
1039 tod
= gethms(fields
[LP_TIME
], "invalid time of day", FALSE
);
1040 cp
= fields
[LP_CORR
];
1042 register int positive
;
1045 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1048 } else if (strcmp(cp
, "--") == 0) {
1051 } else if (strcmp(cp
, "+") == 0) {
1054 } else if (strcmp(cp
, "++") == 0) {
1058 error("illegal CORRECTION field on Leap line");
1061 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1062 error("illegal Rolling/Stationary field on Leap line");
1065 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1070 inlink(fields
, nfields
)
1071 register char ** const fields
;
1076 if (nfields
!= LINK_FIELDS
) {
1077 error("wrong number of fields on Link line");
1080 if (*fields
[LF_FROM
] == '\0') {
1081 error("blank FROM field on Link line");
1084 if (*fields
[LF_TO
] == '\0') {
1085 error("blank TO field on Link line");
1088 l
.l_filename
= filename
;
1089 l
.l_linenum
= linenum
;
1090 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1091 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1092 links
= (struct link
*) (void *) erealloc((char *) links
,
1093 (int) ((nlinks
+ 1) * sizeof *links
));
1094 links
[nlinks
++] = l
;
1098 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1099 register struct rule
* const rp
;
1100 const char * const loyearp
;
1101 const char * const hiyearp
;
1102 const char * const typep
;
1103 const char * const monthp
;
1104 const char * const dayp
;
1105 const char * const timep
;
1107 register const struct lookup
* lp
;
1108 register const char * cp
;
1112 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1113 error("invalid month name");
1116 rp
->r_month
= lp
->l_value
;
1117 rp
->r_todisstd
= FALSE
;
1118 rp
->r_todisuniv
= FALSE
;
1119 dp
= ecpyalloc(timep
);
1121 ep
= dp
+ strlen(dp
) - 1;
1122 switch (lowerit(*ep
)) {
1123 case 's': /* Standard */
1124 rp
->r_todisstd
= TRUE
;
1125 rp
->r_todisuniv
= FALSE
;
1128 case 'w': /* Wall */
1129 rp
->r_todisstd
= FALSE
;
1130 rp
->r_todisuniv
= FALSE
;
1132 case 'g': /* Greenwich */
1133 case 'u': /* Universal */
1134 case 'z': /* Zulu */
1135 rp
->r_todisstd
= TRUE
;
1136 rp
->r_todisuniv
= TRUE
;
1141 rp
->r_tod
= gethms(dp
, "invalid time of day", FALSE
);
1147 if ((lp
= byword(cp
, begin_years
)) != NULL
) switch ((int) lp
->l_value
) {
1149 rp
->r_loyear
= min_int
;
1152 rp
->r_loyear
= max_int
;
1154 default: /* "cannot happen" */
1155 (void) fprintf(stderr
,
1156 "%s: panic: Invalid l_value %d\n",
1157 progname
, lp
->l_value
);
1158 (void) exit(EXIT_FAILURE
);
1159 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1160 error("invalid starting year");
1164 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1166 rp
->r_hiyear
= min_int
;
1169 rp
->r_hiyear
= max_int
;
1172 rp
->r_hiyear
= rp
->r_loyear
;
1174 default: /* "cannot happen" */
1175 (void) fprintf(stderr
,
1176 "%s: panic: Invalid l_value %d\n",
1177 progname
, lp
->l_value
);
1178 (void) exit(EXIT_FAILURE
);
1179 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1180 error("invalid ending year");
1183 if (rp
->r_loyear
> rp
->r_hiyear
) {
1184 error("starting year greater than ending year");
1188 rp
->r_yrtype
= NULL
;
1190 if (rp
->r_loyear
== rp
->r_hiyear
) {
1191 error("typed single year");
1194 rp
->r_yrtype
= ecpyalloc(typep
);
1198 ** Accept things such as:
1204 dp
= ecpyalloc(dayp
);
1205 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1206 rp
->r_dycode
= DC_DOWLEQ
;
1207 rp
->r_wday
= lp
->l_value
;
1208 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1210 if ((ep
= strchr(dp
, '<')) != 0)
1211 rp
->r_dycode
= DC_DOWLEQ
;
1212 else if ((ep
= strchr(dp
, '>')) != 0)
1213 rp
->r_dycode
= DC_DOWGEQ
;
1216 rp
->r_dycode
= DC_DOM
;
1218 if (rp
->r_dycode
!= DC_DOM
) {
1221 error("invalid day of month");
1225 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1226 error("invalid weekday name");
1230 rp
->r_wday
= lp
->l_value
;
1232 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1233 rp
->r_dayofmonth
<= 0 ||
1234 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1235 error("invalid day of month");
1249 register long shift
;
1251 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1252 buf
[i
] = val
>> shift
;
1263 (void) fwrite((genericptr_T
) buf
,
1264 (fwrite_size_T
) sizeof buf
,
1265 (fwrite_size_T
) 1, fp
);
1270 const char * const name
;
1274 static char * fullname
;
1275 static struct tzhead tzh
;
1277 fullname
= erealloc(fullname
,
1278 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1279 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1280 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1281 if (mkdirs(fullname
) != 0)
1282 (void) exit(EXIT_FAILURE
);
1283 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1284 (void) fprintf(stderr
, "%s: Can't create ", progname
);
1285 (void) perror(fullname
);
1286 (void) exit(EXIT_FAILURE
);
1289 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1290 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1291 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1292 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1293 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1294 (void) fwrite((genericptr_T
) &tzh
,
1295 (fwrite_size_T
) sizeof tzh
,
1296 (fwrite_size_T
) 1, fp
);
1297 for (i
= 0; i
< timecnt
; ++i
) {
1300 if (ats
[i
] >= trans
[j
]) {
1301 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1304 puttzcode((long) ats
[i
], fp
);
1307 (void) fwrite((genericptr_T
) types
,
1308 (fwrite_size_T
) sizeof types
[0],
1309 (fwrite_size_T
) timecnt
, fp
);
1310 for (i
= 0; i
< typecnt
; ++i
) {
1311 puttzcode((long) gmtoffs
[i
], fp
);
1312 (void) putc(isdsts
[i
], fp
);
1313 (void) putc(abbrinds
[i
], fp
);
1316 (void) fwrite((genericptr_T
) chars
,
1317 (fwrite_size_T
) sizeof chars
[0],
1318 (fwrite_size_T
) charcnt
, fp
);
1319 for (i
= 0; i
< leapcnt
; ++i
) {
1321 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1324 if (++j
>= typecnt
) {
1330 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1334 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1335 } else puttzcode((long) trans
[i
], fp
);
1336 puttzcode((long) corr
[i
], fp
);
1338 for (i
= 0; i
< typecnt
; ++i
)
1339 (void) putc(ttisstds
[i
], fp
);
1340 if (ferror(fp
) || fclose(fp
)) {
1341 (void) fprintf(stderr
, "%s: Write error on ", progname
);
1342 (void) perror(fullname
);
1343 (void) exit(EXIT_FAILURE
);
1348 outzone(zpfirst
, zonecount
)
1349 const struct zone
* const zpfirst
;
1350 const int zonecount
;
1352 register const struct zone
* zp
;
1353 register struct rule
* rp
;
1355 register int usestart
, useuntil
;
1356 register time_t starttime
, untiltime
;
1357 register long gmtoff
;
1358 register long stdoff
;
1360 register long startoff
;
1361 register int startisdst
;
1362 register int startttisstd
;
1364 char startbuf
[BUFSIZ
];
1366 INITIALIZE(untiltime
);
1367 INITIALIZE(starttime
);
1368 INITIALIZE(startoff
);
1370 ** Now. . .finally. . .generate some useful data!
1376 ** A guess that may well be corrected later.
1380 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1381 ** for noting the need to unconditionally initialize startttisstd.
1383 startttisstd
= FALSE
;
1386 #endif /* defined lint */
1387 for (i
= 0; i
< zonecount
; ++i
) {
1389 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1390 useuntil
= i
< (zonecount
- 1);
1391 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1393 gmtoff
= zp
->z_gmtoff
;
1394 eat(zp
->z_filename
, zp
->z_linenum
);
1396 if (zp
->z_nrules
== 0) {
1397 stdoff
= zp
->z_stdoff
;
1398 (void) strcpy(startbuf
, zp
->z_format
);
1399 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1400 startbuf
, stdoff
!= 0, startttisstd
);
1402 addtt(starttime
, type
);
1403 else if (stdoff
!= 0)
1404 addtt(min_time
, type
);
1405 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1406 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1409 ** Mark which rules to do in the current year.
1410 ** For those to do, calculate rpytime(rp, year);
1412 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1413 rp
= &zp
->z_rules
[j
];
1414 eats(zp
->z_filename
, zp
->z_linenum
,
1415 rp
->r_filename
, rp
->r_linenum
);
1416 rp
->r_todo
= year
>= rp
->r_loyear
&&
1417 year
<= rp
->r_hiyear
&&
1418 yearistype(year
, rp
->r_yrtype
);
1420 rp
->r_temp
= rpytime(rp
, year
);
1424 register time_t jtime
, ktime
;
1425 register long offset
;
1431 ** Turn untiltime into GMT
1432 ** assuming the current gmtoff and
1435 untiltime
= zp
->z_untiltime
;
1436 if (!zp
->z_untilrule
.r_todisuniv
)
1437 untiltime
= tadd(untiltime
,
1439 if (!zp
->z_untilrule
.r_todisstd
)
1440 untiltime
= tadd(untiltime
,
1444 ** Find the rule (of those to do, if any)
1445 ** that takes effect earliest in the year.
1450 #endif /* defined lint */
1451 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1452 rp
= &zp
->z_rules
[j
];
1455 eats(zp
->z_filename
, zp
->z_linenum
,
1456 rp
->r_filename
, rp
->r_linenum
);
1457 offset
= rp
->r_todisuniv
? 0 : gmtoff
;
1458 if (!rp
->r_todisstd
)
1459 offset
= oadd(offset
, stdoff
);
1461 if (jtime
== min_time
||
1464 jtime
= tadd(jtime
, -offset
);
1465 if (k
< 0 || jtime
< ktime
) {
1471 break; /* go on to next year */
1472 rp
= &zp
->z_rules
[k
];
1474 if (useuntil
&& ktime
>= untiltime
)
1477 if (ktime
< starttime
) {
1478 stdoff
= rp
->r_stdoff
;
1479 startoff
= oadd(zp
->z_gmtoff
,
1481 (void) sprintf(startbuf
, zp
->z_format
,
1483 startisdst
= rp
->r_stdoff
!= 0;
1487 if (ktime
!= starttime
) {
1488 if (startisdst
< 0 &&
1490 (zp
- 1)->z_gmtoff
) {
1491 type
= (timecnt
== 0) ? 0 :
1493 startoff
= oadd(gmtoffs
[type
],
1494 -(zp
- 1)->z_gmtoff
);
1495 startisdst
= startoff
!= 0;
1496 startoff
= oadd(startoff
,
1498 (void) strcpy(startbuf
,
1499 &chars
[abbrinds
[type
]]);
1501 if (startisdst
>= 0)
1502 addtt(starttime
, addtype(startoff
, startbuf
, startisdst
, startttisstd
));
1505 eats(zp
->z_filename
, zp
->z_linenum
,
1506 rp
->r_filename
, rp
->r_linenum
);
1507 (void) sprintf(buf
, zp
->z_format
,
1509 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1510 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1513 stdoff
= rp
->r_stdoff
;
1517 ** Now we may get to set starttime for the next zone line.
1520 starttime
= tadd(zp
->z_untiltime
, -gmtoff
);
1521 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1523 starttime
= tadd(starttime
, -stdoff
);
1526 writezone(zpfirst
->z_name
);
1530 addtt(starttime
, type
)
1531 const time_t starttime
;
1534 if (timecnt
!= 0 && type
== types
[timecnt
- 1])
1535 return; /* easy enough! */
1536 if (timecnt
== 0 && type
== 0 && isdsts
[0] == 0)
1537 return; /* handled by default rule */
1538 if (timecnt
>= TZ_MAX_TIMES
) {
1539 error("too many transitions?!");
1540 (void) exit(EXIT_FAILURE
);
1542 ats
[timecnt
] = starttime
;
1543 types
[timecnt
] = type
;
1548 addtype(gmtoff
, abbr
, isdst
, ttisstd
)
1550 const char * const abbr
;
1557 ** See if there's already an entry for this zone type.
1558 ** If so, just return its index.
1560 for (i
= 0; i
< typecnt
; ++i
) {
1561 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1562 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1563 ttisstd
== ttisstds
[i
])
1567 ** There isn't one; add a new one, unless there are already too
1570 if (typecnt
>= TZ_MAX_TYPES
) {
1571 error("too many local time types");
1572 (void) exit(EXIT_FAILURE
);
1574 gmtoffs
[i
] = gmtoff
;
1576 ttisstds
[i
] = ttisstd
;
1578 for (j
= 0; j
< charcnt
; ++j
)
1579 if (strcmp(&chars
[j
], abbr
) == 0)
1589 leapadd(t
, positive
, rolling
, count
)
1597 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1598 error("too many leap seconds");
1599 (void) exit(EXIT_FAILURE
);
1601 for (i
= 0; i
< leapcnt
; ++i
)
1602 if (t
<= trans
[i
]) {
1603 if (t
== trans
[i
]) {
1604 error("repeated leap second moment");
1605 (void) exit(EXIT_FAILURE
);
1610 for (j
= leapcnt
; j
> i
; --j
) {
1611 trans
[j
] = trans
[j
- 1];
1612 corr
[j
] = corr
[j
- 1];
1613 roll
[j
] = roll
[j
- 1];
1616 corr
[i
] = positive
? 1L : eitol(-count
);
1619 } while (positive
&& --count
!= 0);
1626 register long last
= 0;
1629 ** propagate leap seconds forward
1631 for (i
= 0; i
< leapcnt
; ++i
) {
1632 trans
[i
] = tadd(trans
[i
], last
);
1633 last
= corr
[i
] += last
;
1638 yearistype(year
, type
)
1640 const char * const type
;
1645 if (type
== NULL
|| *type
== '\0')
1647 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1648 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1649 result
= system(buf
);
1652 if (result
== (1 << 8))
1654 error("Wild result from command execution");
1655 (void) fprintf(stderr
, "%s: command was '%s', result was %d\n",
1656 progname
, buf
, result
);
1658 (void) exit(EXIT_FAILURE
);
1665 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1669 ciequal(ap
, bp
) /* case-insensitive equality */
1670 register const char * ap
;
1671 register const char * bp
;
1673 while (lowerit(*ap
) == lowerit(*bp
++))
1681 register const char * abbr
;
1682 register const char * word
;
1684 if (lowerit(*abbr
) != lowerit(*word
))
1687 while (*++abbr
!= '\0')
1688 do if (*word
== '\0')
1690 while (lowerit(*word
++) != lowerit(*abbr
));
1694 static const struct lookup
*
1696 register const char * const word
;
1697 register const struct lookup
* const table
;
1699 register const struct lookup
* foundlp
;
1700 register const struct lookup
* lp
;
1702 if (word
== NULL
|| table
== NULL
)
1705 ** Look for exact match.
1707 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1708 if (ciequal(word
, lp
->l_word
))
1711 ** Look for inexact match.
1714 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1715 if (itsabbr(word
, lp
->l_word
))
1716 if (foundlp
== NULL
)
1718 else return NULL
; /* multiple inexact matches */
1727 register char ** array
;
1732 array
= (char **) (void *)
1733 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1736 while (isascii(*cp
) && isspace(*cp
))
1738 if (*cp
== '\0' || *cp
== '#')
1740 array
[nsubs
++] = dp
= cp
;
1742 if ((*dp
= *cp
++) != '"')
1744 else while ((*dp
= *cp
++) != '"')
1747 else error("Odd number of quotation marks");
1748 } while (*cp
!= '\0' && *cp
!= '#' &&
1749 (!isascii(*cp
) || !isspace(*cp
)));
1750 if (isascii(*cp
) && isspace(*cp
))
1754 array
[nsubs
] = NULL
;
1766 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1767 error("time overflow");
1768 (void) exit(EXIT_FAILURE
);
1780 if (t1
== max_time
&& t2
> 0)
1782 if (t1
== min_time
&& t2
< 0)
1785 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1786 error("time overflow");
1787 (void) exit(EXIT_FAILURE
);
1793 ** Given a rule, and a year, compute the date - in seconds since January 1,
1794 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1798 rpytime(rp
, wantedy
)
1799 register const struct rule
* const rp
;
1800 register const int wantedy
;
1802 register int y
, m
, i
;
1803 register long dayoff
; /* with a nod to Margaret O. */
1806 if (wantedy
== min_int
)
1808 if (wantedy
== max_int
)
1813 while (wantedy
!= y
) {
1815 i
= len_years
[isleap(y
)];
1819 i
= -len_years
[isleap(y
)];
1821 dayoff
= oadd(dayoff
, eitol(i
));
1823 while (m
!= rp
->r_month
) {
1824 i
= len_months
[isleap(y
)][m
];
1825 dayoff
= oadd(dayoff
, eitol(i
));
1828 i
= rp
->r_dayofmonth
;
1829 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
1830 if (rp
->r_dycode
== DC_DOWLEQ
)
1833 error("use of 2/29 in non leap-year");
1834 (void) exit(EXIT_FAILURE
);
1838 dayoff
= oadd(dayoff
, eitol(i
));
1839 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
1842 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
1843 wday
= eitol(EPOCH_WDAY
);
1845 ** Don't trust mod of negative numbers.
1848 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
1850 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
1852 wday
+= LDAYSPERWEEK
;
1854 while (wday
!= eitol(rp
->r_wday
))
1855 if (rp
->r_dycode
== DC_DOWGEQ
) {
1856 dayoff
= oadd(dayoff
, (long) 1);
1857 if (++wday
>= LDAYSPERWEEK
)
1861 dayoff
= oadd(dayoff
, (long) -1);
1863 wday
= LDAYSPERWEEK
- 1;
1866 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
1867 error("no day in month matches rule");
1868 (void) exit(EXIT_FAILURE
);
1871 if (dayoff
< 0 && !tt_signed
)
1873 t
= (time_t) dayoff
* SECSPERDAY
;
1875 ** Cheap overflow check.
1877 if (t
/ SECSPERDAY
!= dayoff
)
1878 return (dayoff
> 0) ? max_time
: min_time
;
1879 return tadd(t
, rp
->r_tod
);
1884 const char * const string
;
1888 i
= strlen(string
) + 1;
1889 if (charcnt
+ i
> TZ_MAX_CHARS
) {
1890 error("too many, or too long, time zone abbreviations");
1891 (void) exit(EXIT_FAILURE
);
1893 (void) strcpy(&chars
[charcnt
], string
);
1894 charcnt
+= eitol(i
);
1899 char * const argname
;
1901 register char * name
;
1904 if (argname
== NULL
|| *argname
== '\0')
1906 cp
= name
= ecpyalloc(argname
);
1907 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
1911 ** MS-DOS drive specifier?
1913 if (strlen(name
) == 2 && isascii(name
[0]) &&
1914 isalpha(name
[0]) && name
[1] == ':') {
1918 #endif /* !defined unix */
1919 if (!itsdir(name
)) {
1921 ** It doesn't seem to exist, so we try to create it.
1923 if (emkdir(name
, 0755) != 0) {
1924 (void) fprintf(stderr
,
1925 "%s: Can't create directory ",
1927 (void) perror(name
);
1945 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
1946 (void) fprintf(stderr
,
1947 "%s: %d did not sign extend correctly\n",
1949 (void) exit(EXIT_FAILURE
);
1955 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.