3 static char elsieid
[] = "@(#)zic.c 7.94";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
11 #include "sys/stat.h" /* for umask manifest constants */
12 #endif /* defined unix */
15 ** On some ancient hosts, predicates like `isspace(C)' are defined
16 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
17 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
18 ** Neither the C Standard nor Posix require that `isascii' exist.
19 ** For portability, we check both ancient and modern requirements.
20 ** If isascii is not defined, the isascii check succeeds trivially.
28 const char * r_filename
;
32 int r_loyear
; /* for example, 1986 */
33 int r_hiyear
; /* for example, 1986 */
34 const char * r_yrtype
;
36 int r_month
; /* 0..11 */
38 int r_dycode
; /* see below */
42 long r_tod
; /* time from midnight */
43 int r_todisstd
; /* above is standard time if TRUE */
44 /* or wall clock time if FALSE */
45 int r_todisgmt
; /* above is GMT if TRUE */
46 /* or local time if FALSE */
47 long r_stdoff
; /* offset from standard time */
48 const char * r_abbrvar
; /* variable part of abbreviation */
50 int r_todo
; /* a rule to do (used in outzone) */
51 time_t r_temp
; /* used in outzone */
55 ** r_dycode r_dayofmonth r_wday
58 #define DC_DOM 0 /* 1..31 */ /* unused */
59 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
60 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
63 const char * z_filename
;
69 const char * z_format
;
73 struct rule
* z_rules
;
76 struct rule z_untilrule
;
80 extern int getopt
P((int argc
, char * const argv
[],
81 const char * options
));
82 extern int link
P((const char * fromname
, const char * toname
));
86 static void addtt
P((time_t starttime
, int type
));
87 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
88 int ttisstd
, int ttisgmt
));
89 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
90 static void adjleap
P((void));
91 static void associate
P((void));
92 static int ciequal
P((const char * ap
, const char * bp
));
93 static void convert
P((long val
, char * buf
));
94 static void dolink
P((const char * fromfile
, const char * tofile
));
95 static void doabbr
P((char * abbr
, const char * format
,
96 const char * letters
, int isdst
));
97 static void eat
P((const char * name
, int num
));
98 static void eats
P((const char * name
, int num
,
99 const char * rname
, int rnum
));
100 static long eitol
P((int i
));
101 static void error
P((const char * message
));
102 static char ** getfields
P((char * buf
));
103 static long gethms
P((const char * string
, const char * errstrng
,
105 static void infile
P((const char * filename
));
106 static void inleap
P((char ** fields
, int nfields
));
107 static void inlink
P((char ** fields
, int nfields
));
108 static void inrule
P((char ** fields
, int nfields
));
109 static int inzcont
P((char ** fields
, int nfields
));
110 static int inzone
P((char ** fields
, int nfields
));
111 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
112 static int itsabbr
P((const char * abbr
, const char * word
));
113 static int itsdir
P((const char * name
));
114 static int lowerit
P((int c
));
115 static char * memcheck
P((char * tocheck
));
116 static int mkdirs
P((char * filename
));
117 static void newabbr
P((const char * abbr
));
118 static long oadd
P((long t1
, long t2
));
119 static void outzone
P((const struct zone
* zp
, int ntzones
));
120 static void puttzcode
P((long code
, FILE * fp
));
121 static int rcomp
P((const void * leftp
, const void * rightp
));
122 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
123 static void rulesub
P((struct rule
* rp
,
124 const char * loyearp
, const char * hiyearp
,
125 const char * typep
, const char * monthp
,
126 const char * dayp
, const char * timep
));
127 static void setboundaries
P((void));
128 static time_t tadd
P((time_t t1
, long t2
));
129 static void usage
P((void));
130 static void writezone
P((const char * name
));
131 static int yearistype
P((int year
, const char * type
));
133 #if !(HAVE_STRERROR - 0)
134 static char * strerror
P((int));
135 #endif /* !(HAVE_STRERROR - 0) */
139 static const char * filename
;
142 static time_t max_time
;
144 static int max_year_representable
;
145 static time_t min_time
;
147 static int min_year_representable
;
149 static const char * rfilename
;
151 static const char * progname
;
165 ** Which fields are which on a Zone line.
173 #define ZF_TILMONTH 6
176 #define ZONE_MINFIELDS 5
177 #define ZONE_MAXFIELDS 9
180 ** Which fields are which on a Zone continuation line.
186 #define ZFC_TILYEAR 3
187 #define ZFC_TILMONTH 4
189 #define ZFC_TILTIME 6
190 #define ZONEC_MINFIELDS 3
191 #define ZONEC_MAXFIELDS 7
194 ** Which files are which on a Rule line.
206 #define RULE_FIELDS 10
209 ** Which fields are which on a Link line.
214 #define LINK_FIELDS 3
217 ** Which fields are which on a Leap line.
226 #define LEAP_FIELDS 7
236 static struct rule
* rules
;
237 static int nrules
; /* number of rules */
239 static struct zone
* zones
;
240 static int nzones
; /* number of zones */
243 const char * l_filename
;
249 static struct link
* links
;
257 static struct lookup
const * byword
P((const char * string
,
258 const struct lookup
* lp
));
260 static struct lookup
const line_codes
[] = {
268 static struct lookup
const mon_names
[] = {
269 { "January", TM_JANUARY
},
270 { "February", TM_FEBRUARY
},
271 { "March", TM_MARCH
},
272 { "April", TM_APRIL
},
276 { "August", TM_AUGUST
},
277 { "September", TM_SEPTEMBER
},
278 { "October", TM_OCTOBER
},
279 { "November", TM_NOVEMBER
},
280 { "December", TM_DECEMBER
},
284 static struct lookup
const wday_names
[] = {
285 { "Sunday", TM_SUNDAY
},
286 { "Monday", TM_MONDAY
},
287 { "Tuesday", TM_TUESDAY
},
288 { "Wednesday", TM_WEDNESDAY
},
289 { "Thursday", TM_THURSDAY
},
290 { "Friday", TM_FRIDAY
},
291 { "Saturday", TM_SATURDAY
},
295 static struct lookup
const lasts
[] = {
296 { "last-Sunday", TM_SUNDAY
},
297 { "last-Monday", TM_MONDAY
},
298 { "last-Tuesday", TM_TUESDAY
},
299 { "last-Wednesday", TM_WEDNESDAY
},
300 { "last-Thursday", TM_THURSDAY
},
301 { "last-Friday", TM_FRIDAY
},
302 { "last-Saturday", TM_SATURDAY
},
306 static struct lookup
const begin_years
[] = {
307 { "minimum", YR_MINIMUM
},
308 { "maximum", YR_MAXIMUM
},
312 static struct lookup
const end_years
[] = {
313 { "minimum", YR_MINIMUM
},
314 { "maximum", YR_MAXIMUM
},
319 static struct lookup
const leap_types
[] = {
321 { "Stationary", FALSE
},
325 static const int len_months
[2][MONSPERYEAR
] = {
326 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
327 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
330 static const int len_years
[2] = {
331 DAYSPERNYEAR
, DAYSPERLYEAR
334 static struct attype
{
337 } attypes
[TZ_MAX_TIMES
];
338 static long gmtoffs
[TZ_MAX_TYPES
];
339 static char isdsts
[TZ_MAX_TYPES
];
340 static unsigned char abbrinds
[TZ_MAX_TYPES
];
341 static char ttisstds
[TZ_MAX_TYPES
];
342 static char ttisgmts
[TZ_MAX_TYPES
];
343 static char chars
[TZ_MAX_CHARS
];
344 static time_t trans
[TZ_MAX_LEAPS
];
345 static long corr
[TZ_MAX_LEAPS
];
346 static char roll
[TZ_MAX_LEAPS
];
349 ** Memory allocation.
357 const char *e
= strerror(errno
);
359 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
361 (void) exit(EXIT_FAILURE
);
366 #define emalloc(size) memcheck(imalloc(size))
367 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
368 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
369 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
375 #if !(HAVE_STRERROR - 0)
380 extern char * sys_errlist
[];
383 return (errnum
> 0 && errnum
<= sys_nerr
) ?
384 sys_errlist
[errnum
] : "Unknown system error";
386 #endif /* !(HAVE_STRERROR - 0) */
389 eats(name
, num
, rname
, rnum
)
390 const char * const name
;
392 const char * const rname
;
403 const char * const name
;
406 eats(name
, num
, (char *) NULL
, -1);
411 const char * const string
;
414 ** Match the format of "cc" to allow sh users to
415 ** zic ... 2>&1 | error -t "*" -v
418 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
419 filename
, linenum
, string
);
420 if (rfilename
!= NULL
)
421 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
422 rfilename
, rlinenum
);
423 (void) fprintf(stderr
, "\n");
429 const char * const string
;
433 cp
= ecpyalloc("warning: ");
434 cp
= ecatalloc(cp
, string
);
443 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
445 (void) exit(EXIT_FAILURE
);
448 static const char * psxrules
;
449 static const char * lcltime
;
450 static const char * directory
;
451 static const char * leapsec
;
452 static const char * yitcommand
;
453 static int sflag
= FALSE
;
465 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
466 #endif /* defined unix */
468 (void) setlocale(LC_MESSAGES
, "");
470 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
471 #endif /* defined TEXTDOMAINDIR */
472 (void) textdomain(TZ_DOMAIN
);
473 #endif /* HAVE_GETTEXT - 0 */
475 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
480 if (directory
== NULL
)
483 (void) fprintf(stderr
,
484 _("%s: More than one -d option specified\n"),
486 (void) exit(EXIT_FAILURE
);
493 (void) fprintf(stderr
,
494 _("%s: More than one -l option specified\n"),
496 (void) exit(EXIT_FAILURE
);
500 if (psxrules
== NULL
)
503 (void) fprintf(stderr
,
504 _("%s: More than one -p option specified\n"),
506 (void) exit(EXIT_FAILURE
);
510 if (yitcommand
== NULL
)
513 (void) fprintf(stderr
,
514 _("%s: More than one -y option specified\n"),
516 (void) exit(EXIT_FAILURE
);
523 (void) fprintf(stderr
,
524 _("%s: More than one -L option specified\n"),
526 (void) exit(EXIT_FAILURE
);
536 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
537 usage(); /* usage message by request */
538 if (directory
== NULL
)
540 if (yitcommand
== NULL
)
541 yitcommand
= "yearistype";
545 if (optind
< argc
&& leapsec
!= NULL
) {
550 for (i
= optind
; i
< argc
; ++i
)
553 (void) exit(EXIT_FAILURE
);
555 for (i
= 0; i
< nzones
; i
= j
) {
557 ** Find the next non-continuation zone entry.
559 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
561 outzone(&zones
[i
], j
- i
);
566 for (i
= 0; i
< nlinks
; ++i
)
567 dolink(links
[i
].l_from
, links
[i
].l_to
);
569 dolink(lcltime
, TZDEFAULT
);
570 if (psxrules
!= NULL
)
571 dolink(psxrules
, TZDEFRULES
);
572 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
576 dolink(fromfile
, tofile
)
577 const char * const fromfile
;
578 const char * const tofile
;
580 register char * fromname
;
581 register char * toname
;
583 if (fromfile
[0] == '/')
584 fromname
= ecpyalloc(fromfile
);
586 fromname
= ecpyalloc(directory
);
587 fromname
= ecatalloc(fromname
, "/");
588 fromname
= ecatalloc(fromname
, fromfile
);
590 if (tofile
[0] == '/')
591 toname
= ecpyalloc(tofile
);
593 toname
= ecpyalloc(directory
);
594 toname
= ecatalloc(toname
, "/");
595 toname
= ecatalloc(toname
, tofile
);
598 ** We get to be careful here since
599 ** there's a fair chance of root running us.
602 (void) remove(toname
);
603 if (link(fromname
, toname
) != 0) {
606 if (mkdirs(toname
) != 0)
607 (void) exit(EXIT_FAILURE
);
608 result
= link(fromname
, toname
);
609 #if (HAVE_SYMLINK - 0)
611 result
= symlink(fromname
, toname
);
613 warning(_("hard link failed, symbolic link used"));
617 const char *e
= strerror(errno
);
619 (void) fprintf(stderr
,
620 _("%s: Can't link from %s to %s: %s\n"),
621 progname
, fromname
, toname
, e
);
622 (void) exit(EXIT_FAILURE
);
630 #define INT_MAX ((int) (((unsigned)~0)>>1))
631 #endif /* !defined INT_MAX */
634 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
635 #endif /* !defined INT_MIN */
638 ** The tz file format currently allows at most 32-bit quantities.
639 ** This restriction should be removed before signed 32-bit values
640 ** wrap around in 2038, but unfortunately this will require a
641 ** change to the tz file format.
644 #define MAX_BITS_IN_FILE 32
645 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
648 setboundaries
P((void))
650 if (TYPE_SIGNED(time_t)) {
651 min_time
= ~ (time_t) 0;
652 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
653 max_time
= ~ (time_t) 0 - min_time
;
658 max_time
= 2 - sflag
;
659 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
662 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
663 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
664 min_year_representable
= min_year
;
665 max_year_representable
= max_year
;
670 const char * const name
;
672 register char * myname
;
675 myname
= ecpyalloc(name
);
676 myname
= ecatalloc(myname
, "/.");
677 accres
= access(myname
, F_OK
);
683 ** Associate sets of rules with zones.
687 ** Sort by rule name.
695 return strcmp(((const struct rule
*) cp1
)->r_name
,
696 ((const struct rule
*) cp2
)->r_name
);
702 register struct zone
* zp
;
703 register struct rule
* rp
;
704 register int base
, out
;
708 (void) qsort((void *) rules
, (size_t) nrules
,
709 (size_t) sizeof *rules
, rcomp
);
710 for (i
= 0; i
< nrules
- 1; ++i
) {
711 if (strcmp(rules
[i
].r_name
,
712 rules
[i
+ 1].r_name
) != 0)
714 if (strcmp(rules
[i
].r_filename
,
715 rules
[i
+ 1].r_filename
) == 0)
717 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
718 warning(_("same rule name in multiple files"));
719 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
720 warning(_("same rule name in multiple files"));
721 for (j
= i
+ 2; j
< nrules
; ++j
) {
722 if (strcmp(rules
[i
].r_name
,
723 rules
[j
].r_name
) != 0)
725 if (strcmp(rules
[i
].r_filename
,
726 rules
[j
].r_filename
) == 0)
728 if (strcmp(rules
[i
+ 1].r_filename
,
729 rules
[j
].r_filename
) == 0)
736 for (i
= 0; i
< nzones
; ++i
) {
741 for (base
= 0; base
< nrules
; base
= out
) {
743 for (out
= base
+ 1; out
< nrules
; ++out
)
744 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
746 for (i
= 0; i
< nzones
; ++i
) {
748 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
751 zp
->z_nrules
= out
- base
;
754 for (i
= 0; i
< nzones
; ++i
) {
756 if (zp
->z_nrules
== 0) {
758 ** Maybe we have a local standard time offset.
760 eat(zp
->z_filename
, zp
->z_linenum
);
761 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
764 ** Note, though, that if there's no rule,
765 ** a '%s' in the format is a bad thing.
767 if (strchr(zp
->z_format
, '%') != 0)
768 error(_("%s in ruleless zone"));
772 (void) exit(EXIT_FAILURE
);
780 register char ** fields
;
782 register const struct lookup
* lp
;
783 register int nfields
;
784 register int wantcont
;
788 if (strcmp(name
, "-") == 0) {
789 name
= _("standard input");
791 } else if ((fp
= fopen(name
, "r")) == NULL
) {
792 const char *e
= strerror(errno
);
794 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
796 (void) exit(EXIT_FAILURE
);
799 for (num
= 1; ; ++num
) {
801 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
803 cp
= strchr(buf
, '\n');
805 error(_("line too long"));
806 (void) exit(EXIT_FAILURE
);
809 fields
= getfields(buf
);
811 while (fields
[nfields
] != NULL
) {
814 if (strcmp(fields
[nfields
], "-") == 0)
815 fields
[nfields
] = &nada
;
820 } else if (wantcont
) {
821 wantcont
= inzcont(fields
, nfields
);
823 lp
= byword(fields
[0], line_codes
);
825 error(_("input line of unknown type"));
826 else switch ((int) (lp
->l_value
)) {
828 inrule(fields
, nfields
);
832 wantcont
= inzone(fields
, nfields
);
835 inlink(fields
, nfields
);
840 (void) fprintf(stderr
,
841 _("%s: Leap line in non leap seconds file %s\n"),
843 else inleap(fields
, nfields
);
846 default: /* "cannot happen" */
847 (void) fprintf(stderr
,
848 _("%s: panic: Invalid l_value %d\n"),
849 progname
, lp
->l_value
);
850 (void) exit(EXIT_FAILURE
);
853 ifree((char *) fields
);
856 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
858 (void) exit(EXIT_FAILURE
);
860 if (fp
!= stdin
&& fclose(fp
)) {
861 const char *e
= strerror(errno
);
863 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
864 progname
, filename
, e
);
865 (void) exit(EXIT_FAILURE
);
868 error(_("expected continuation line not found"));
872 ** Convert a string of one of the forms
873 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
874 ** into a number of seconds.
875 ** A null string maps to zero.
876 ** Call error with errstring and return zero on errors.
880 gethms(string
, errstring
, signable
)
882 const char * const errstring
;
885 int hh
, mm
, ss
, sign
;
887 if (string
== NULL
|| *string
== '\0')
891 else if (*string
== '-') {
895 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
897 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
899 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
900 &hh
, &mm
, &ss
) != 3) {
904 if (hh
< 0 || hh
>= HOURSPERDAY
||
905 mm
< 0 || mm
>= MINSPERHOUR
||
906 ss
< 0 || ss
> SECSPERMIN
) {
911 (eitol(hh
* MINSPERHOUR
+ mm
) *
912 eitol(SECSPERMIN
) + eitol(ss
));
916 inrule(fields
, nfields
)
917 register char ** const fields
;
920 static struct rule r
;
922 if (nfields
!= RULE_FIELDS
) {
923 error(_("wrong number of fields on Rule line"));
926 if (*fields
[RF_NAME
] == '\0') {
927 error(_("nameless rule"));
930 r
.r_filename
= filename
;
931 r
.r_linenum
= linenum
;
932 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
933 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
934 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
935 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
936 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
937 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
938 (int) ((nrules
+ 1) * sizeof *rules
));
943 inzone(fields
, nfields
)
944 register char ** const fields
;
950 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
951 error(_("wrong number of fields on Zone line"));
954 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
955 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
957 _("\"Zone %s\" line and -l option are mutually exclusive"),
962 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
963 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
965 _("\"Zone %s\" line and -p option are mutually exclusive"),
970 for (i
= 0; i
< nzones
; ++i
)
971 if (zones
[i
].z_name
!= NULL
&&
972 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
973 buf
= erealloc(buf
, (int) (132 +
974 strlen(fields
[ZF_NAME
]) +
975 strlen(zones
[i
].z_filename
)));
977 _("duplicate zone name %s (file \"%s\", line %d)"),
984 return inzsub(fields
, nfields
, FALSE
);
988 inzcont(fields
, nfields
)
989 register char ** const fields
;
992 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
993 error(_("wrong number of fields on Zone continuation line"));
996 return inzsub(fields
, nfields
, TRUE
);
1000 inzsub(fields
, nfields
, iscont
)
1001 register char ** const fields
;
1006 static struct zone z
;
1007 register int i_gmtoff
, i_rule
, i_format
;
1008 register int i_untilyear
, i_untilmonth
;
1009 register int i_untilday
, i_untiltime
;
1010 register int hasuntil
;
1013 i_gmtoff
= ZFC_GMTOFF
;
1015 i_format
= ZFC_FORMAT
;
1016 i_untilyear
= ZFC_TILYEAR
;
1017 i_untilmonth
= ZFC_TILMONTH
;
1018 i_untilday
= ZFC_TILDAY
;
1019 i_untiltime
= ZFC_TILTIME
;
1022 i_gmtoff
= ZF_GMTOFF
;
1024 i_format
= ZF_FORMAT
;
1025 i_untilyear
= ZF_TILYEAR
;
1026 i_untilmonth
= ZF_TILMONTH
;
1027 i_untilday
= ZF_TILDAY
;
1028 i_untiltime
= ZF_TILTIME
;
1029 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1031 z
.z_filename
= filename
;
1032 z
.z_linenum
= linenum
;
1033 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1034 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1035 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1036 error(_("invalid abbreviation format"));
1040 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1041 z
.z_format
= ecpyalloc(fields
[i_format
]);
1042 hasuntil
= nfields
> i_untilyear
;
1044 z
.z_untilrule
.r_filename
= filename
;
1045 z
.z_untilrule
.r_linenum
= linenum
;
1046 rulesub(&z
.z_untilrule
,
1047 fields
[i_untilyear
],
1050 (nfields
> i_untilmonth
) ?
1051 fields
[i_untilmonth
] : "Jan",
1052 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1053 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1054 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1055 z
.z_untilrule
.r_loyear
);
1056 if (iscont
&& nzones
> 0 &&
1057 z
.z_untiltime
> min_time
&&
1058 z
.z_untiltime
< max_time
&&
1059 zones
[nzones
- 1].z_untiltime
> min_time
&&
1060 zones
[nzones
- 1].z_untiltime
< max_time
&&
1061 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1062 error(_("Zone continuation line end time is not after end time of previous line"));
1066 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1067 (int) ((nzones
+ 1) * sizeof *zones
));
1068 zones
[nzones
++] = z
;
1070 ** If there was an UNTIL field on this line,
1071 ** there's more information about the zone on the next line.
1077 inleap(fields
, nfields
)
1078 register char ** const fields
;
1081 register const char * cp
;
1082 register const struct lookup
* lp
;
1084 int year
, month
, day
;
1088 if (nfields
!= LEAP_FIELDS
) {
1089 error(_("wrong number of fields on Leap line"));
1093 cp
= fields
[LP_YEAR
];
1094 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1098 error(_("invalid leaping year"));
1104 i
= len_years
[isleap(j
)];
1108 i
= -len_years
[isleap(j
)];
1110 dayoff
= oadd(dayoff
, eitol(i
));
1112 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1113 error(_("invalid month name"));
1116 month
= lp
->l_value
;
1118 while (j
!= month
) {
1119 i
= len_months
[isleap(year
)][j
];
1120 dayoff
= oadd(dayoff
, eitol(i
));
1123 cp
= fields
[LP_DAY
];
1124 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1125 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1126 error(_("invalid day of month"));
1129 dayoff
= oadd(dayoff
, eitol(day
- 1));
1130 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1131 error(_("time before zero"));
1134 t
= (time_t) dayoff
* SECSPERDAY
;
1136 ** Cheap overflow check.
1138 if (t
/ SECSPERDAY
!= dayoff
) {
1139 error(_("time overflow"));
1142 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1143 cp
= fields
[LP_CORR
];
1145 register int positive
;
1148 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1151 } else if (strcmp(cp
, "--") == 0) {
1154 } else if (strcmp(cp
, "+") == 0) {
1157 } else if (strcmp(cp
, "++") == 0) {
1161 error(_("illegal CORRECTION field on Leap line"));
1164 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1165 error(_("illegal Rolling/Stationary field on Leap line"));
1168 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1173 inlink(fields
, nfields
)
1174 register char ** const fields
;
1179 if (nfields
!= LINK_FIELDS
) {
1180 error(_("wrong number of fields on Link line"));
1183 if (*fields
[LF_FROM
] == '\0') {
1184 error(_("blank FROM field on Link line"));
1187 if (*fields
[LF_TO
] == '\0') {
1188 error(_("blank TO field on Link line"));
1191 l
.l_filename
= filename
;
1192 l
.l_linenum
= linenum
;
1193 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1194 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1195 links
= (struct link
*) (void *) erealloc((char *) links
,
1196 (int) ((nlinks
+ 1) * sizeof *links
));
1197 links
[nlinks
++] = l
;
1201 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1202 register struct rule
* const rp
;
1203 const char * const loyearp
;
1204 const char * const hiyearp
;
1205 const char * const typep
;
1206 const char * const monthp
;
1207 const char * const dayp
;
1208 const char * const timep
;
1210 register const struct lookup
* lp
;
1211 register const char * cp
;
1215 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1216 error(_("invalid month name"));
1219 rp
->r_month
= lp
->l_value
;
1220 rp
->r_todisstd
= FALSE
;
1221 rp
->r_todisgmt
= FALSE
;
1222 dp
= ecpyalloc(timep
);
1224 ep
= dp
+ strlen(dp
) - 1;
1225 switch (lowerit(*ep
)) {
1226 case 's': /* Standard */
1227 rp
->r_todisstd
= TRUE
;
1228 rp
->r_todisgmt
= FALSE
;
1231 case 'w': /* Wall */
1232 rp
->r_todisstd
= FALSE
;
1233 rp
->r_todisgmt
= FALSE
;
1236 case 'g': /* Greenwich */
1237 case 'u': /* Universal */
1238 case 'z': /* Zulu */
1239 rp
->r_todisstd
= TRUE
;
1240 rp
->r_todisgmt
= TRUE
;
1245 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1251 lp
= byword(cp
, begin_years
);
1252 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1254 rp
->r_loyear
= INT_MIN
;
1257 rp
->r_loyear
= INT_MAX
;
1259 default: /* "cannot happen" */
1260 (void) fprintf(stderr
,
1261 _("%s: panic: Invalid l_value %d\n"),
1262 progname
, lp
->l_value
);
1263 (void) exit(EXIT_FAILURE
);
1264 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1265 error(_("invalid starting year"));
1268 if (rp
->r_loyear
< min_year_representable
)
1269 warning(_("starting year too low to be represented"));
1270 else if (rp
->r_loyear
> max_year_representable
)
1271 warning(_("starting year too high to be represented"));
1274 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1276 rp
->r_hiyear
= INT_MIN
;
1279 rp
->r_hiyear
= INT_MAX
;
1282 rp
->r_hiyear
= rp
->r_loyear
;
1284 default: /* "cannot happen" */
1285 (void) fprintf(stderr
,
1286 _("%s: panic: Invalid l_value %d\n"),
1287 progname
, lp
->l_value
);
1288 (void) exit(EXIT_FAILURE
);
1289 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1290 error(_("invalid ending year"));
1293 if (rp
->r_loyear
< min_year_representable
)
1294 warning(_("starting year too low to be represented"));
1295 else if (rp
->r_loyear
> max_year_representable
)
1296 warning(_("starting year too high to be represented"));
1298 if (rp
->r_loyear
> rp
->r_hiyear
) {
1299 error(_("starting year greater than ending year"));
1303 rp
->r_yrtype
= NULL
;
1305 if (rp
->r_loyear
== rp
->r_hiyear
) {
1306 error(_("typed single year"));
1309 rp
->r_yrtype
= ecpyalloc(typep
);
1311 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1312 min_year
= rp
->r_loyear
;
1315 ** Accept things such as:
1321 dp
= ecpyalloc(dayp
);
1322 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1323 rp
->r_dycode
= DC_DOWLEQ
;
1324 rp
->r_wday
= lp
->l_value
;
1325 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1327 if ((ep
= strchr(dp
, '<')) != 0)
1328 rp
->r_dycode
= DC_DOWLEQ
;
1329 else if ((ep
= strchr(dp
, '>')) != 0)
1330 rp
->r_dycode
= DC_DOWGEQ
;
1333 rp
->r_dycode
= DC_DOM
;
1335 if (rp
->r_dycode
!= DC_DOM
) {
1338 error(_("invalid day of month"));
1342 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1343 error(_("invalid weekday name"));
1347 rp
->r_wday
= lp
->l_value
;
1349 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1350 rp
->r_dayofmonth
<= 0 ||
1351 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1352 error(_("invalid day of month"));
1366 register long shift
;
1368 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1369 buf
[i
] = val
>> shift
;
1380 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1388 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1390 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1397 const char * const name
;
1401 static char * fullname
;
1402 static struct tzhead tzh
;
1403 time_t ats
[TZ_MAX_TIMES
];
1404 unsigned char types
[TZ_MAX_TIMES
];
1410 (void) qsort((void *) attypes
, (size_t) timecnt
,
1411 (size_t) sizeof *attypes
, atcomp
);
1421 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1424 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1425 ++fromi
; /* handled by default rule */
1426 for ( ; fromi
< timecnt
; ++fromi
) {
1428 && ((attypes
[fromi
].at
1429 + gmtoffs
[attypes
[toi
- 1].type
])
1430 <= (attypes
[toi
- 1].at
1431 + gmtoffs
[toi
== 1 ? 0
1432 : attypes
[toi
- 2].type
]))) {
1433 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1437 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1438 attypes
[toi
++] = attypes
[fromi
];
1445 for (i
= 0; i
< timecnt
; ++i
) {
1446 ats
[i
] = attypes
[i
].at
;
1447 types
[i
] = attypes
[i
].type
;
1449 fullname
= erealloc(fullname
,
1450 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1451 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1453 ** Remove old file, if any, to snap links.
1455 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1456 const char *e
= strerror(errno
);
1458 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1459 progname
, fullname
, e
);
1460 (void) exit(EXIT_FAILURE
);
1462 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1463 if (mkdirs(fullname
) != 0)
1464 (void) exit(EXIT_FAILURE
);
1465 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1466 const char *e
= strerror(errno
);
1468 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1469 progname
, fullname
, e
);
1470 (void) exit(EXIT_FAILURE
);
1473 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1474 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1475 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1476 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1477 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1478 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1479 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1480 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1490 for (i
= 0; i
< timecnt
; ++i
) {
1493 if (ats
[i
] >= trans
[j
]) {
1494 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1497 puttzcode((long) ats
[i
], fp
);
1500 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1501 (size_t) timecnt
, fp
);
1502 for (i
= 0; i
< typecnt
; ++i
) {
1503 puttzcode((long) gmtoffs
[i
], fp
);
1504 (void) putc(isdsts
[i
], fp
);
1505 (void) putc(abbrinds
[i
], fp
);
1508 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1509 (size_t) charcnt
, fp
);
1510 for (i
= 0; i
< leapcnt
; ++i
) {
1512 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1515 if (++j
>= typecnt
) {
1521 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1525 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1526 } else puttzcode((long) trans
[i
], fp
);
1527 puttzcode((long) corr
[i
], fp
);
1529 for (i
= 0; i
< typecnt
; ++i
)
1530 (void) putc(ttisstds
[i
], fp
);
1531 for (i
= 0; i
< typecnt
; ++i
)
1532 (void) putc(ttisgmts
[i
], fp
);
1533 if (ferror(fp
) || fclose(fp
)) {
1534 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1535 progname
, fullname
);
1536 (void) exit(EXIT_FAILURE
);
1541 doabbr(abbr
, format
, letters
, isdst
)
1543 const char * const format
;
1544 const char * const letters
;
1547 if (strchr(format
, '/') == NULL
) {
1548 if (letters
== NULL
)
1549 (void) strcpy(abbr
, format
);
1550 else (void) sprintf(abbr
, format
, letters
);
1552 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1554 (void) strcpy(abbr
, format
);
1555 *strchr(abbr
, '/') = '\0';
1560 outzone(zpfirst
, zonecount
)
1561 const struct zone
* const zpfirst
;
1562 const int zonecount
;
1564 register const struct zone
* zp
;
1565 register struct rule
* rp
;
1567 register int usestart
, useuntil
;
1568 register time_t starttime
, untiltime
;
1569 register long gmtoff
;
1570 register long stdoff
;
1572 register long startoff
;
1573 register int startttisstd
;
1574 register int startttisgmt
;
1576 char startbuf
[BUFSIZ
];
1578 INITIALIZE(untiltime
);
1579 INITIALIZE(starttime
);
1581 ** Now. . .finally. . .generate some useful data!
1587 ** A guess that may well be corrected later.
1591 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1592 ** for noting the need to unconditionally initialize startttisstd.
1594 startttisstd
= FALSE
;
1595 startttisgmt
= FALSE
;
1596 for (i
= 0; i
< zonecount
; ++i
) {
1598 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1599 useuntil
= i
< (zonecount
- 1);
1600 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1602 gmtoff
= zp
->z_gmtoff
;
1603 eat(zp
->z_filename
, zp
->z_linenum
);
1605 startoff
= zp
->z_gmtoff
;
1606 if (zp
->z_nrules
== 0) {
1607 stdoff
= zp
->z_stdoff
;
1608 doabbr(startbuf
, zp
->z_format
,
1609 (char *) NULL
, stdoff
!= 0);
1610 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1611 startbuf
, stdoff
!= 0, startttisstd
,
1614 addtt(starttime
, type
);
1617 else if (stdoff
!= 0)
1618 addtt(min_time
, type
);
1619 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1620 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1623 ** Mark which rules to do in the current year.
1624 ** For those to do, calculate rpytime(rp, year);
1626 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1627 rp
= &zp
->z_rules
[j
];
1628 eats(zp
->z_filename
, zp
->z_linenum
,
1629 rp
->r_filename
, rp
->r_linenum
);
1630 rp
->r_todo
= year
>= rp
->r_loyear
&&
1631 year
<= rp
->r_hiyear
&&
1632 yearistype(year
, rp
->r_yrtype
);
1634 rp
->r_temp
= rpytime(rp
, year
);
1638 register time_t jtime
, ktime
;
1639 register long offset
;
1645 ** Turn untiltime into UTC
1646 ** assuming the current gmtoff and
1649 untiltime
= zp
->z_untiltime
;
1650 if (!zp
->z_untilrule
.r_todisgmt
)
1651 untiltime
= tadd(untiltime
,
1653 if (!zp
->z_untilrule
.r_todisstd
)
1654 untiltime
= tadd(untiltime
,
1658 ** Find the rule (of those to do, if any)
1659 ** that takes effect earliest in the year.
1662 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1663 rp
= &zp
->z_rules
[j
];
1666 eats(zp
->z_filename
, zp
->z_linenum
,
1667 rp
->r_filename
, rp
->r_linenum
);
1668 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1669 if (!rp
->r_todisstd
)
1670 offset
= oadd(offset
, stdoff
);
1672 if (jtime
== min_time
||
1675 jtime
= tadd(jtime
, -offset
);
1676 if (k
< 0 || jtime
< ktime
) {
1682 break; /* go on to next year */
1683 rp
= &zp
->z_rules
[k
];
1685 if (useuntil
&& ktime
>= untiltime
)
1687 stdoff
= rp
->r_stdoff
;
1688 if (usestart
&& ktime
== starttime
)
1691 if (ktime
< starttime
) {
1692 startoff
= oadd(zp
->z_gmtoff
,
1694 doabbr(startbuf
, zp
->z_format
,
1699 if (*startbuf
== '\0' &&
1700 startoff
== oadd(zp
->z_gmtoff
,
1702 doabbr(startbuf
, zp
->z_format
,
1707 eats(zp
->z_filename
, zp
->z_linenum
,
1708 rp
->r_filename
, rp
->r_linenum
);
1709 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1711 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1712 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1713 rp
->r_todisstd
, rp
->r_todisgmt
);
1718 if (*startbuf
== '\0' &&
1719 zp
->z_format
!= NULL
&&
1720 strchr(zp
->z_format
, '%') == NULL
&&
1721 strchr(zp
->z_format
, '/') == NULL
)
1722 (void) strcpy(startbuf
, zp
->z_format
);
1723 eat(zp
->z_filename
, zp
->z_linenum
);
1724 if (*startbuf
== '\0')
1725 error(_("can't determine time zone abbreviation to use just after until time"));
1726 else addtt(starttime
,
1727 addtype(startoff
, startbuf
,
1728 startoff
!= zp
->z_gmtoff
,
1733 ** Now we may get to set starttime for the next zone line.
1736 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1737 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1738 starttime
= zp
->z_untiltime
;
1740 starttime
= tadd(starttime
, -stdoff
);
1742 starttime
= tadd(starttime
, -gmtoff
);
1745 writezone(zpfirst
->z_name
);
1749 addtt(starttime
, type
)
1750 const time_t starttime
;
1753 if (starttime
<= min_time
||
1754 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1755 gmtoffs
[0] = gmtoffs
[type
];
1756 isdsts
[0] = isdsts
[type
];
1757 ttisstds
[0] = ttisstds
[type
];
1758 ttisgmts
[0] = ttisgmts
[type
];
1759 if (abbrinds
[type
] != 0)
1760 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1762 charcnt
= strlen(chars
) + 1;
1767 if (timecnt
>= TZ_MAX_TIMES
) {
1768 error(_("too many transitions?!"));
1769 (void) exit(EXIT_FAILURE
);
1771 attypes
[timecnt
].at
= starttime
;
1772 attypes
[timecnt
].type
= type
;
1777 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1779 const char * const abbr
;
1786 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1787 error(_("internal error - addtype called with bad isdst"));
1788 (void) exit(EXIT_FAILURE
);
1790 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1791 error(_("internal error - addtype called with bad ttisstd"));
1792 (void) exit(EXIT_FAILURE
);
1794 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1795 error(_("internal error - addtype called with bad ttisgmt"));
1796 (void) exit(EXIT_FAILURE
);
1799 ** See if there's already an entry for this zone type.
1800 ** If so, just return its index.
1802 for (i
= 0; i
< typecnt
; ++i
) {
1803 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1804 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1805 ttisstd
== ttisstds
[i
] &&
1806 ttisgmt
== ttisgmts
[i
])
1810 ** There isn't one; add a new one, unless there are already too
1813 if (typecnt
>= TZ_MAX_TYPES
) {
1814 error(_("too many local time types"));
1815 (void) exit(EXIT_FAILURE
);
1817 gmtoffs
[i
] = gmtoff
;
1819 ttisstds
[i
] = ttisstd
;
1820 ttisgmts
[i
] = ttisgmt
;
1822 for (j
= 0; j
< charcnt
; ++j
)
1823 if (strcmp(&chars
[j
], abbr
) == 0)
1833 leapadd(t
, positive
, rolling
, count
)
1841 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1842 error(_("too many leap seconds"));
1843 (void) exit(EXIT_FAILURE
);
1845 for (i
= 0; i
< leapcnt
; ++i
)
1846 if (t
<= trans
[i
]) {
1847 if (t
== trans
[i
]) {
1848 error(_("repeated leap second moment"));
1849 (void) exit(EXIT_FAILURE
);
1854 for (j
= leapcnt
; j
> i
; --j
) {
1855 trans
[j
] = trans
[j
- 1];
1856 corr
[j
] = corr
[j
- 1];
1857 roll
[j
] = roll
[j
- 1];
1860 corr
[i
] = positive
? 1L : eitol(-count
);
1863 } while (positive
&& --count
!= 0);
1870 register long last
= 0;
1873 ** propagate leap seconds forward
1875 for (i
= 0; i
< leapcnt
; ++i
) {
1876 trans
[i
] = tadd(trans
[i
], last
);
1877 last
= corr
[i
] += last
;
1882 yearistype(year
, type
)
1884 const char * const type
;
1889 if (type
== NULL
|| *type
== '\0')
1891 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1892 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1893 result
= system(buf
);
1896 if (result
== (1 << 8))
1898 error(_("Wild result from command execution"));
1899 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1900 progname
, buf
, result
);
1902 (void) exit(EXIT_FAILURE
);
1909 a
= (unsigned char) a
;
1910 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1914 ciequal(ap
, bp
) /* case-insensitive equality */
1915 register const char * ap
;
1916 register const char * bp
;
1918 while (lowerit(*ap
) == lowerit(*bp
++))
1926 register const char * abbr
;
1927 register const char * word
;
1929 if (lowerit(*abbr
) != lowerit(*word
))
1932 while (*++abbr
!= '\0')
1936 } while (lowerit(*word
++) != lowerit(*abbr
));
1940 static const struct lookup
*
1942 register const char * const word
;
1943 register const struct lookup
* const table
;
1945 register const struct lookup
* foundlp
;
1946 register const struct lookup
* lp
;
1948 if (word
== NULL
|| table
== NULL
)
1951 ** Look for exact match.
1953 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1954 if (ciequal(word
, lp
->l_word
))
1957 ** Look for inexact match.
1960 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1961 if (itsabbr(word
, lp
->l_word
)) {
1962 if (foundlp
== NULL
)
1964 else return NULL
; /* multiple inexact matches */
1974 register char ** array
;
1979 array
= (char **) (void *)
1980 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1983 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1985 if (*cp
== '\0' || *cp
== '#')
1987 array
[nsubs
++] = dp
= cp
;
1989 if ((*dp
= *cp
++) != '"')
1991 else while ((*dp
= *cp
++) != '"')
1994 else error(_("Odd number of quotation marks"));
1995 } while (*cp
!= '\0' && *cp
!= '#' &&
1996 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1997 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2001 array
[nsubs
] = NULL
;
2013 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2014 error(_("time overflow"));
2015 (void) exit(EXIT_FAILURE
);
2027 if (t1
== max_time
&& t2
> 0)
2029 if (t1
== min_time
&& t2
< 0)
2032 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2033 error(_("time overflow"));
2034 (void) exit(EXIT_FAILURE
);
2040 ** Given a rule, and a year, compute the date - in seconds since January 1,
2041 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2045 rpytime(rp
, wantedy
)
2046 register const struct rule
* const rp
;
2047 register const int wantedy
;
2049 register int y
, m
, i
;
2050 register long dayoff
; /* with a nod to Margaret O. */
2053 if (wantedy
== INT_MIN
)
2055 if (wantedy
== INT_MAX
)
2060 while (wantedy
!= y
) {
2062 i
= len_years
[isleap(y
)];
2066 i
= -len_years
[isleap(y
)];
2068 dayoff
= oadd(dayoff
, eitol(i
));
2070 while (m
!= rp
->r_month
) {
2071 i
= len_months
[isleap(y
)][m
];
2072 dayoff
= oadd(dayoff
, eitol(i
));
2075 i
= rp
->r_dayofmonth
;
2076 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2077 if (rp
->r_dycode
== DC_DOWLEQ
)
2080 error(_("use of 2/29 in non leap-year"));
2081 (void) exit(EXIT_FAILURE
);
2085 dayoff
= oadd(dayoff
, eitol(i
));
2086 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2089 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2090 wday
= eitol(EPOCH_WDAY
);
2092 ** Don't trust mod of negative numbers.
2095 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2097 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2099 wday
+= LDAYSPERWEEK
;
2101 while (wday
!= eitol(rp
->r_wday
))
2102 if (rp
->r_dycode
== DC_DOWGEQ
) {
2103 dayoff
= oadd(dayoff
, (long) 1);
2104 if (++wday
>= LDAYSPERWEEK
)
2108 dayoff
= oadd(dayoff
, (long) -1);
2110 wday
= LDAYSPERWEEK
- 1;
2113 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2114 error(_("no day in month matches rule"));
2115 (void) exit(EXIT_FAILURE
);
2118 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2120 t
= (time_t) dayoff
* SECSPERDAY
;
2122 ** Cheap overflow check.
2124 if (t
/ SECSPERDAY
!= dayoff
)
2125 return (dayoff
> 0) ? max_time
: min_time
;
2126 return tadd(t
, rp
->r_tod
);
2131 const char * const string
;
2135 i
= strlen(string
) + 1;
2136 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2137 error(_("too many, or too long, time zone abbreviations"));
2138 (void) exit(EXIT_FAILURE
);
2140 (void) strcpy(&chars
[charcnt
], string
);
2141 charcnt
+= eitol(i
);
2146 char * const argname
;
2148 register char * name
;
2151 if (argname
== NULL
|| *argname
== '\0')
2153 cp
= name
= ecpyalloc(argname
);
2154 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2158 ** DOS drive specifier?
2160 if (isalpha((unsigned char) name
[0]) &&
2161 name
[1] == ':' && name
[2] == '\0') {
2165 #endif /* !defined unix */
2166 if (!itsdir(name
)) {
2168 ** It doesn't seem to exist, so we try to create it.
2169 ** Creation may fail because of the directory being
2170 ** created by some other multiprocessor, so we get
2171 ** to do extra checking.
2173 if (mkdir(name
, S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
) != 0) {
2174 const char *e
= strerror(errno
);
2176 if (errno
!= EEXIST
|| !itsdir(name
)) {
2177 (void) fprintf(stderr
,
2178 _("%s: Can't create directory %s: %s\n"),
2198 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2199 (void) fprintf(stderr
,
2200 _("%s: %d did not sign extend correctly\n"),
2202 (void) exit(EXIT_FAILURE
);
2208 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.