3 static char elsieid
[] = "@(#)zic.c 7.107";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
15 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
17 #define MKDIR_UMASK 0755
21 ** On some ancient hosts, predicates like `isspace(C)' are defined
22 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
23 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
24 ** Neither the C Standard nor Posix require that `isascii' exist.
25 ** For portability, we check both ancient and modern requirements.
26 ** If isascii is not defined, the isascii check succeeds trivially.
34 const char * r_filename
;
38 int r_loyear
; /* for example, 1986 */
39 int r_hiyear
; /* for example, 1986 */
40 const char * r_yrtype
;
42 int r_month
; /* 0..11 */
44 int r_dycode
; /* see below */
48 long r_tod
; /* time from midnight */
49 int r_todisstd
; /* above is standard time if TRUE */
50 /* or wall clock time if FALSE */
51 int r_todisgmt
; /* above is GMT if TRUE */
52 /* or local time if FALSE */
53 long r_stdoff
; /* offset from standard time */
54 const char * r_abbrvar
; /* variable part of abbreviation */
56 int r_todo
; /* a rule to do (used in outzone) */
57 time_t r_temp
; /* used in outzone */
61 ** r_dycode r_dayofmonth r_wday
64 #define DC_DOM 0 /* 1..31 */ /* unused */
65 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
66 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
69 const char * z_filename
;
75 const char * z_format
;
79 struct rule
* z_rules
;
82 struct rule z_untilrule
;
86 extern int getopt
P((int argc
, char * const argv
[],
87 const char * options
));
88 extern int link
P((const char * fromname
, const char * toname
));
92 static void addtt
P((time_t starttime
, int type
));
93 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
94 int ttisstd
, int ttisgmt
));
95 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
96 static void adjleap
P((void));
97 static void associate
P((void));
98 static int ciequal
P((const char * ap
, const char * bp
));
99 static void convert
P((long val
, char * buf
));
100 static void dolink
P((const char * fromfile
, const char * tofile
));
101 static void doabbr
P((char * abbr
, const char * format
,
102 const char * letters
, int isdst
));
103 static void eat
P((const char * name
, int num
));
104 static void eats
P((const char * name
, int num
,
105 const char * rname
, int rnum
));
106 static long eitol
P((int i
));
107 static void error
P((const char * message
));
108 static char ** getfields
P((char * buf
));
109 static long gethms
P((const char * string
, const char * errstrng
,
111 static void infile
P((const char * filename
));
112 static void inleap
P((char ** fields
, int nfields
));
113 static void inlink
P((char ** fields
, int nfields
));
114 static void inrule
P((char ** fields
, int nfields
));
115 static int inzcont
P((char ** fields
, int nfields
));
116 static int inzone
P((char ** fields
, int nfields
));
117 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
118 static int itsabbr
P((const char * abbr
, const char * word
));
119 static int itsdir
P((const char * name
));
120 static int lowerit
P((int c
));
121 static char * memcheck
P((char * tocheck
));
122 static int mkdirs
P((char * filename
));
123 static void newabbr
P((const char * abbr
));
124 static long oadd
P((long t1
, long t2
));
125 static void outzone
P((const struct zone
* zp
, int ntzones
));
126 static void puttzcode
P((long code
, FILE * fp
));
127 static int rcomp
P((const void * leftp
, const void * rightp
));
128 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
129 static void rulesub
P((struct rule
* rp
,
130 const char * loyearp
, const char * hiyearp
,
131 const char * typep
, const char * monthp
,
132 const char * dayp
, const char * timep
));
133 static void setboundaries
P((void));
134 static time_t tadd
P((time_t t1
, long t2
));
135 static void usage
P((void));
136 static void writezone
P((const char * name
));
137 static int yearistype
P((int year
, const char * type
));
139 #if !(HAVE_STRERROR - 0)
140 static char * strerror
P((int));
141 #endif /* !(HAVE_STRERROR - 0) */
145 static const char * filename
;
148 static time_t max_time
;
150 static int max_year_representable
;
151 static time_t min_time
;
153 static int min_year_representable
;
155 static const char * rfilename
;
157 static const char * progname
;
171 ** Which fields are which on a Zone line.
179 #define ZF_TILMONTH 6
182 #define ZONE_MINFIELDS 5
183 #define ZONE_MAXFIELDS 9
186 ** Which fields are which on a Zone continuation line.
192 #define ZFC_TILYEAR 3
193 #define ZFC_TILMONTH 4
195 #define ZFC_TILTIME 6
196 #define ZONEC_MINFIELDS 3
197 #define ZONEC_MAXFIELDS 7
200 ** Which files are which on a Rule line.
212 #define RULE_FIELDS 10
215 ** Which fields are which on a Link line.
220 #define LINK_FIELDS 3
223 ** Which fields are which on a Leap line.
232 #define LEAP_FIELDS 7
242 static struct rule
* rules
;
243 static int nrules
; /* number of rules */
245 static struct zone
* zones
;
246 static int nzones
; /* number of zones */
249 const char * l_filename
;
255 static struct link
* links
;
263 static struct lookup
const * byword
P((const char * string
,
264 const struct lookup
* lp
));
266 static struct lookup
const line_codes
[] = {
274 static struct lookup
const mon_names
[] = {
275 { "January", TM_JANUARY
},
276 { "February", TM_FEBRUARY
},
277 { "March", TM_MARCH
},
278 { "April", TM_APRIL
},
282 { "August", TM_AUGUST
},
283 { "September", TM_SEPTEMBER
},
284 { "October", TM_OCTOBER
},
285 { "November", TM_NOVEMBER
},
286 { "December", TM_DECEMBER
},
290 static struct lookup
const wday_names
[] = {
291 { "Sunday", TM_SUNDAY
},
292 { "Monday", TM_MONDAY
},
293 { "Tuesday", TM_TUESDAY
},
294 { "Wednesday", TM_WEDNESDAY
},
295 { "Thursday", TM_THURSDAY
},
296 { "Friday", TM_FRIDAY
},
297 { "Saturday", TM_SATURDAY
},
301 static struct lookup
const lasts
[] = {
302 { "last-Sunday", TM_SUNDAY
},
303 { "last-Monday", TM_MONDAY
},
304 { "last-Tuesday", TM_TUESDAY
},
305 { "last-Wednesday", TM_WEDNESDAY
},
306 { "last-Thursday", TM_THURSDAY
},
307 { "last-Friday", TM_FRIDAY
},
308 { "last-Saturday", TM_SATURDAY
},
312 static struct lookup
const begin_years
[] = {
313 { "minimum", YR_MINIMUM
},
314 { "maximum", YR_MAXIMUM
},
318 static struct lookup
const end_years
[] = {
319 { "minimum", YR_MINIMUM
},
320 { "maximum", YR_MAXIMUM
},
325 static struct lookup
const leap_types
[] = {
327 { "Stationary", FALSE
},
331 static const int len_months
[2][MONSPERYEAR
] = {
332 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
333 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
336 static const int len_years
[2] = {
337 DAYSPERNYEAR
, DAYSPERLYEAR
340 static struct attype
{
343 } attypes
[TZ_MAX_TIMES
];
344 static long gmtoffs
[TZ_MAX_TYPES
];
345 static char isdsts
[TZ_MAX_TYPES
];
346 static unsigned char abbrinds
[TZ_MAX_TYPES
];
347 static char ttisstds
[TZ_MAX_TYPES
];
348 static char ttisgmts
[TZ_MAX_TYPES
];
349 static char chars
[TZ_MAX_CHARS
];
350 static time_t trans
[TZ_MAX_LEAPS
];
351 static long corr
[TZ_MAX_LEAPS
];
352 static char roll
[TZ_MAX_LEAPS
];
355 ** Memory allocation.
363 const char *e
= strerror(errno
);
365 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
367 (void) exit(EXIT_FAILURE
);
372 #define emalloc(size) memcheck(imalloc(size))
373 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
374 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
375 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
381 #if !(HAVE_STRERROR - 0)
386 extern char * sys_errlist
[];
389 return (errnum
> 0 && errnum
<= sys_nerr
) ?
390 sys_errlist
[errnum
] : _("Unknown system error");
392 #endif /* !(HAVE_STRERROR - 0) */
395 eats(name
, num
, rname
, rnum
)
396 const char * const name
;
398 const char * const rname
;
409 const char * const name
;
412 eats(name
, num
, (char *) NULL
, -1);
417 const char * const string
;
420 ** Match the format of "cc" to allow sh users to
421 ** zic ... 2>&1 | error -t "*" -v
424 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
425 filename
, linenum
, string
);
426 if (rfilename
!= NULL
)
427 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
428 rfilename
, rlinenum
);
429 (void) fprintf(stderr
, "\n");
435 const char * const string
;
439 cp
= ecpyalloc(_("warning: "));
440 cp
= ecatalloc(cp
, string
);
449 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
451 (void) exit(EXIT_FAILURE
);
454 static const char * psxrules
;
455 static const char * lcltime
;
456 static const char * directory
;
457 static const char * leapsec
;
458 static const char * yitcommand
;
459 static int sflag
= FALSE
;
471 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
472 #endif /* defined unix */
474 (void) setlocale(LC_CTYPE
, "");
475 (void) setlocale(LC_MESSAGES
, "");
477 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
478 #endif /* defined TEXTDOMAINDIR */
479 (void) textdomain(TZ_DOMAIN
);
480 #endif /* HAVE_GETTEXT - 0 */
482 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
487 if (directory
== NULL
)
490 (void) fprintf(stderr
,
491 _("%s: More than one -d option specified\n"),
493 (void) exit(EXIT_FAILURE
);
500 (void) fprintf(stderr
,
501 _("%s: More than one -l option specified\n"),
503 (void) exit(EXIT_FAILURE
);
507 if (psxrules
== NULL
)
510 (void) fprintf(stderr
,
511 _("%s: More than one -p option specified\n"),
513 (void) exit(EXIT_FAILURE
);
517 if (yitcommand
== NULL
)
520 (void) fprintf(stderr
,
521 _("%s: More than one -y option specified\n"),
523 (void) exit(EXIT_FAILURE
);
530 (void) fprintf(stderr
,
531 _("%s: More than one -L option specified\n"),
533 (void) exit(EXIT_FAILURE
);
543 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
544 usage(); /* usage message by request */
545 if (directory
== NULL
)
547 if (yitcommand
== NULL
)
548 yitcommand
= "yearistype";
552 if (optind
< argc
&& leapsec
!= NULL
) {
557 for (i
= optind
; i
< argc
; ++i
)
560 (void) exit(EXIT_FAILURE
);
562 for (i
= 0; i
< nzones
; i
= j
) {
564 ** Find the next non-continuation zone entry.
566 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
568 outzone(&zones
[i
], j
- i
);
573 for (i
= 0; i
< nlinks
; ++i
) {
574 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
575 dolink(links
[i
].l_from
, links
[i
].l_to
);
577 if (lcltime
!= NULL
) {
578 eat("command line", 1);
579 dolink(lcltime
, TZDEFAULT
);
581 if (psxrules
!= NULL
) {
582 eat("command line", 1);
583 dolink(psxrules
, TZDEFRULES
);
585 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
589 dolink(fromfile
, tofile
)
590 const char * const fromfile
;
591 const char * const tofile
;
593 register char * fromname
;
594 register char * toname
;
596 if (fromfile
[0] == '/')
597 fromname
= ecpyalloc(fromfile
);
599 fromname
= ecpyalloc(directory
);
600 fromname
= ecatalloc(fromname
, "/");
601 fromname
= ecatalloc(fromname
, fromfile
);
603 if (tofile
[0] == '/')
604 toname
= ecpyalloc(tofile
);
606 toname
= ecpyalloc(directory
);
607 toname
= ecatalloc(toname
, "/");
608 toname
= ecatalloc(toname
, tofile
);
611 ** We get to be careful here since
612 ** there's a fair chance of root running us.
615 (void) remove(toname
);
616 if (link(fromname
, toname
) != 0) {
619 if (mkdirs(toname
) != 0)
620 (void) exit(EXIT_FAILURE
);
622 result
= link(fromname
, toname
);
623 #if (HAVE_SYMLINK - 0)
625 access(fromname
, F_OK
) == 0 &&
627 const char *s
= tofile
;
628 register char * symlinkcontents
= NULL
;
629 while ((s
= strchr(s
+1, '/')) != NULL
)
630 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
631 symlinkcontents
= ecatalloc(symlinkcontents
, fromname
);
633 result
= unlink(toname
);
634 if (result
!= 0 && errno
!= ENOENT
) {
635 const char *e
= strerror(errno
);
637 (void) fprintf(stderr
,
638 _("%s: Can't unlink %s: %s\n"),
639 progname
, toname
, e
);
640 (void) exit(EXIT_FAILURE
);
643 result
= symlink(symlinkcontents
, toname
);
645 warning(_("hard link failed, symbolic link used"));
646 ifree(symlinkcontents
);
650 const char *e
= strerror(errno
);
652 (void) fprintf(stderr
,
653 _("%s: Can't link from %s to %s: %s\n"),
654 progname
, fromname
, toname
, e
);
655 (void) exit(EXIT_FAILURE
);
663 #define INT_MAX ((int) (((unsigned)~0)>>1))
664 #endif /* !defined INT_MAX */
667 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
668 #endif /* !defined INT_MIN */
671 ** The tz file format currently allows at most 32-bit quantities.
672 ** This restriction should be removed before signed 32-bit values
673 ** wrap around in 2038, but unfortunately this will require a
674 ** change to the tz file format.
677 #define MAX_BITS_IN_FILE 32
678 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
681 setboundaries
P((void))
683 if (TYPE_SIGNED(time_t)) {
684 min_time
= ~ (time_t) 0;
685 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
686 max_time
= ~ (time_t) 0 - min_time
;
691 max_time
= 2 - sflag
;
692 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
695 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
696 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
697 min_year_representable
= min_year
;
698 max_year_representable
= max_year
;
703 const char * const name
;
705 register char * myname
;
708 myname
= ecpyalloc(name
);
709 myname
= ecatalloc(myname
, "/.");
710 accres
= access(myname
, F_OK
);
716 ** Associate sets of rules with zones.
720 ** Sort by rule name.
728 return strcmp(((const struct rule
*) cp1
)->r_name
,
729 ((const struct rule
*) cp2
)->r_name
);
735 register struct zone
* zp
;
736 register struct rule
* rp
;
737 register int base
, out
;
741 (void) qsort((void *) rules
, (size_t) nrules
,
742 (size_t) sizeof *rules
, rcomp
);
743 for (i
= 0; i
< nrules
- 1; ++i
) {
744 if (strcmp(rules
[i
].r_name
,
745 rules
[i
+ 1].r_name
) != 0)
747 if (strcmp(rules
[i
].r_filename
,
748 rules
[i
+ 1].r_filename
) == 0)
750 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
751 warning(_("same rule name in multiple files"));
752 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
753 warning(_("same rule name in multiple files"));
754 for (j
= i
+ 2; j
< nrules
; ++j
) {
755 if (strcmp(rules
[i
].r_name
,
756 rules
[j
].r_name
) != 0)
758 if (strcmp(rules
[i
].r_filename
,
759 rules
[j
].r_filename
) == 0)
761 if (strcmp(rules
[i
+ 1].r_filename
,
762 rules
[j
].r_filename
) == 0)
769 for (i
= 0; i
< nzones
; ++i
) {
774 for (base
= 0; base
< nrules
; base
= out
) {
776 for (out
= base
+ 1; out
< nrules
; ++out
)
777 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
779 for (i
= 0; i
< nzones
; ++i
) {
781 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
784 zp
->z_nrules
= out
- base
;
787 for (i
= 0; i
< nzones
; ++i
) {
789 if (zp
->z_nrules
== 0) {
791 ** Maybe we have a local standard time offset.
793 eat(zp
->z_filename
, zp
->z_linenum
);
794 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
797 ** Note, though, that if there's no rule,
798 ** a '%s' in the format is a bad thing.
800 if (strchr(zp
->z_format
, '%') != 0)
801 error(_("%s in ruleless zone"));
805 (void) exit(EXIT_FAILURE
);
813 register char ** fields
;
815 register const struct lookup
* lp
;
816 register int nfields
;
817 register int wantcont
;
821 if (strcmp(name
, "-") == 0) {
822 name
= _("standard input");
824 } else if ((fp
= fopen(name
, "r")) == NULL
) {
825 const char *e
= strerror(errno
);
827 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
829 (void) exit(EXIT_FAILURE
);
832 for (num
= 1; ; ++num
) {
834 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
836 cp
= strchr(buf
, '\n');
838 error(_("line too long"));
839 (void) exit(EXIT_FAILURE
);
842 fields
= getfields(buf
);
844 while (fields
[nfields
] != NULL
) {
847 if (strcmp(fields
[nfields
], "-") == 0)
848 fields
[nfields
] = &nada
;
853 } else if (wantcont
) {
854 wantcont
= inzcont(fields
, nfields
);
856 lp
= byword(fields
[0], line_codes
);
858 error(_("input line of unknown type"));
859 else switch ((int) (lp
->l_value
)) {
861 inrule(fields
, nfields
);
865 wantcont
= inzone(fields
, nfields
);
868 inlink(fields
, nfields
);
873 (void) fprintf(stderr
,
874 _("%s: Leap line in non leap seconds file %s\n"),
876 else inleap(fields
, nfields
);
879 default: /* "cannot happen" */
880 (void) fprintf(stderr
,
881 _("%s: panic: Invalid l_value %d\n"),
882 progname
, lp
->l_value
);
883 (void) exit(EXIT_FAILURE
);
886 ifree((char *) fields
);
889 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
891 (void) exit(EXIT_FAILURE
);
893 if (fp
!= stdin
&& fclose(fp
)) {
894 const char *e
= strerror(errno
);
896 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
897 progname
, filename
, e
);
898 (void) exit(EXIT_FAILURE
);
901 error(_("expected continuation line not found"));
905 ** Convert a string of one of the forms
906 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
907 ** into a number of seconds.
908 ** A null string maps to zero.
909 ** Call error with errstring and return zero on errors.
913 gethms(string
, errstring
, signable
)
915 const char * const errstring
;
918 int hh
, mm
, ss
, sign
;
920 if (string
== NULL
|| *string
== '\0')
924 else if (*string
== '-') {
928 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
930 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
932 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
933 &hh
, &mm
, &ss
) != 3) {
937 if ((hh
< 0 || hh
>= HOURSPERDAY
||
938 mm
< 0 || mm
>= MINSPERHOUR
||
939 ss
< 0 || ss
> SECSPERMIN
) &&
940 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
945 (eitol(hh
* MINSPERHOUR
+ mm
) *
946 eitol(SECSPERMIN
) + eitol(ss
));
950 inrule(fields
, nfields
)
951 register char ** const fields
;
954 static struct rule r
;
956 if (nfields
!= RULE_FIELDS
) {
957 error(_("wrong number of fields on Rule line"));
960 if (*fields
[RF_NAME
] == '\0') {
961 error(_("nameless rule"));
964 r
.r_filename
= filename
;
965 r
.r_linenum
= linenum
;
966 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
967 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
968 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
969 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
970 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
971 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
972 (int) ((nrules
+ 1) * sizeof *rules
));
977 inzone(fields
, nfields
)
978 register char ** const fields
;
984 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
985 error(_("wrong number of fields on Zone line"));
988 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
989 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
991 _("\"Zone %s\" line and -l option are mutually exclusive"),
996 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
997 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
999 _("\"Zone %s\" line and -p option are mutually exclusive"),
1004 for (i
= 0; i
< nzones
; ++i
)
1005 if (zones
[i
].z_name
!= NULL
&&
1006 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1007 buf
= erealloc(buf
, (int) (132 +
1008 strlen(fields
[ZF_NAME
]) +
1009 strlen(zones
[i
].z_filename
)));
1011 _("duplicate zone name %s (file \"%s\", line %d)"),
1013 zones
[i
].z_filename
,
1014 zones
[i
].z_linenum
);
1018 return inzsub(fields
, nfields
, FALSE
);
1022 inzcont(fields
, nfields
)
1023 register char ** const fields
;
1026 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1027 error(_("wrong number of fields on Zone continuation line"));
1030 return inzsub(fields
, nfields
, TRUE
);
1034 inzsub(fields
, nfields
, iscont
)
1035 register char ** const fields
;
1040 static struct zone z
;
1041 register int i_gmtoff
, i_rule
, i_format
;
1042 register int i_untilyear
, i_untilmonth
;
1043 register int i_untilday
, i_untiltime
;
1044 register int hasuntil
;
1047 i_gmtoff
= ZFC_GMTOFF
;
1049 i_format
= ZFC_FORMAT
;
1050 i_untilyear
= ZFC_TILYEAR
;
1051 i_untilmonth
= ZFC_TILMONTH
;
1052 i_untilday
= ZFC_TILDAY
;
1053 i_untiltime
= ZFC_TILTIME
;
1056 i_gmtoff
= ZF_GMTOFF
;
1058 i_format
= ZF_FORMAT
;
1059 i_untilyear
= ZF_TILYEAR
;
1060 i_untilmonth
= ZF_TILMONTH
;
1061 i_untilday
= ZF_TILDAY
;
1062 i_untiltime
= ZF_TILTIME
;
1063 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1065 z
.z_filename
= filename
;
1066 z
.z_linenum
= linenum
;
1067 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1068 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1069 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1070 error(_("invalid abbreviation format"));
1074 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1075 z
.z_format
= ecpyalloc(fields
[i_format
]);
1076 hasuntil
= nfields
> i_untilyear
;
1078 z
.z_untilrule
.r_filename
= filename
;
1079 z
.z_untilrule
.r_linenum
= linenum
;
1080 rulesub(&z
.z_untilrule
,
1081 fields
[i_untilyear
],
1084 (nfields
> i_untilmonth
) ?
1085 fields
[i_untilmonth
] : "Jan",
1086 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1087 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1088 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1089 z
.z_untilrule
.r_loyear
);
1090 if (iscont
&& nzones
> 0 &&
1091 z
.z_untiltime
> min_time
&&
1092 z
.z_untiltime
< max_time
&&
1093 zones
[nzones
- 1].z_untiltime
> min_time
&&
1094 zones
[nzones
- 1].z_untiltime
< max_time
&&
1095 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1096 error(_("Zone continuation line end time is not after end time of previous line"));
1100 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1101 (int) ((nzones
+ 1) * sizeof *zones
));
1102 zones
[nzones
++] = z
;
1104 ** If there was an UNTIL field on this line,
1105 ** there's more information about the zone on the next line.
1111 inleap(fields
, nfields
)
1112 register char ** const fields
;
1115 register const char * cp
;
1116 register const struct lookup
* lp
;
1118 int year
, month
, day
;
1122 if (nfields
!= LEAP_FIELDS
) {
1123 error(_("wrong number of fields on Leap line"));
1127 cp
= fields
[LP_YEAR
];
1128 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1132 error(_("invalid leaping year"));
1138 i
= len_years
[isleap(j
)];
1142 i
= -len_years
[isleap(j
)];
1144 dayoff
= oadd(dayoff
, eitol(i
));
1146 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1147 error(_("invalid month name"));
1150 month
= lp
->l_value
;
1152 while (j
!= month
) {
1153 i
= len_months
[isleap(year
)][j
];
1154 dayoff
= oadd(dayoff
, eitol(i
));
1157 cp
= fields
[LP_DAY
];
1158 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1159 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1160 error(_("invalid day of month"));
1163 dayoff
= oadd(dayoff
, eitol(day
- 1));
1164 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1165 error(_("time before zero"));
1168 t
= (time_t) dayoff
* SECSPERDAY
;
1170 ** Cheap overflow check.
1172 if (t
/ SECSPERDAY
!= dayoff
) {
1173 error(_("time overflow"));
1176 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1177 cp
= fields
[LP_CORR
];
1179 register int positive
;
1182 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1185 } else if (strcmp(cp
, "--") == 0) {
1188 } else if (strcmp(cp
, "+") == 0) {
1191 } else if (strcmp(cp
, "++") == 0) {
1195 error(_("illegal CORRECTION field on Leap line"));
1198 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1199 error(_("illegal Rolling/Stationary field on Leap line"));
1202 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1207 inlink(fields
, nfields
)
1208 register char ** const fields
;
1213 if (nfields
!= LINK_FIELDS
) {
1214 error(_("wrong number of fields on Link line"));
1217 if (*fields
[LF_FROM
] == '\0') {
1218 error(_("blank FROM field on Link line"));
1221 if (*fields
[LF_TO
] == '\0') {
1222 error(_("blank TO field on Link line"));
1225 l
.l_filename
= filename
;
1226 l
.l_linenum
= linenum
;
1227 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1228 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1229 links
= (struct link
*) (void *) erealloc((char *) links
,
1230 (int) ((nlinks
+ 1) * sizeof *links
));
1231 links
[nlinks
++] = l
;
1235 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1236 register struct rule
* const rp
;
1237 const char * const loyearp
;
1238 const char * const hiyearp
;
1239 const char * const typep
;
1240 const char * const monthp
;
1241 const char * const dayp
;
1242 const char * const timep
;
1244 register const struct lookup
* lp
;
1245 register const char * cp
;
1249 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1250 error(_("invalid month name"));
1253 rp
->r_month
= lp
->l_value
;
1254 rp
->r_todisstd
= FALSE
;
1255 rp
->r_todisgmt
= FALSE
;
1256 dp
= ecpyalloc(timep
);
1258 ep
= dp
+ strlen(dp
) - 1;
1259 switch (lowerit(*ep
)) {
1260 case 's': /* Standard */
1261 rp
->r_todisstd
= TRUE
;
1262 rp
->r_todisgmt
= FALSE
;
1265 case 'w': /* Wall */
1266 rp
->r_todisstd
= FALSE
;
1267 rp
->r_todisgmt
= FALSE
;
1270 case 'g': /* Greenwich */
1271 case 'u': /* Universal */
1272 case 'z': /* Zulu */
1273 rp
->r_todisstd
= TRUE
;
1274 rp
->r_todisgmt
= TRUE
;
1279 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1285 lp
= byword(cp
, begin_years
);
1286 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1288 rp
->r_loyear
= INT_MIN
;
1291 rp
->r_loyear
= INT_MAX
;
1293 default: /* "cannot happen" */
1294 (void) fprintf(stderr
,
1295 _("%s: panic: Invalid l_value %d\n"),
1296 progname
, lp
->l_value
);
1297 (void) exit(EXIT_FAILURE
);
1298 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1299 error(_("invalid starting year"));
1302 if (rp
->r_loyear
< min_year_representable
)
1303 warning(_("starting year too low to be represented"));
1304 else if (rp
->r_loyear
> max_year_representable
)
1305 warning(_("starting year too high to be represented"));
1308 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1310 rp
->r_hiyear
= INT_MIN
;
1313 rp
->r_hiyear
= INT_MAX
;
1316 rp
->r_hiyear
= rp
->r_loyear
;
1318 default: /* "cannot happen" */
1319 (void) fprintf(stderr
,
1320 _("%s: panic: Invalid l_value %d\n"),
1321 progname
, lp
->l_value
);
1322 (void) exit(EXIT_FAILURE
);
1323 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1324 error(_("invalid ending year"));
1327 if (rp
->r_loyear
< min_year_representable
)
1328 warning(_("starting year too low to be represented"));
1329 else if (rp
->r_loyear
> max_year_representable
)
1330 warning(_("starting year too high to be represented"));
1332 if (rp
->r_loyear
> rp
->r_hiyear
) {
1333 error(_("starting year greater than ending year"));
1337 rp
->r_yrtype
= NULL
;
1339 if (rp
->r_loyear
== rp
->r_hiyear
) {
1340 error(_("typed single year"));
1343 rp
->r_yrtype
= ecpyalloc(typep
);
1345 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1346 min_year
= rp
->r_loyear
;
1349 ** Accept things such as:
1355 dp
= ecpyalloc(dayp
);
1356 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1357 rp
->r_dycode
= DC_DOWLEQ
;
1358 rp
->r_wday
= lp
->l_value
;
1359 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1361 if ((ep
= strchr(dp
, '<')) != 0)
1362 rp
->r_dycode
= DC_DOWLEQ
;
1363 else if ((ep
= strchr(dp
, '>')) != 0)
1364 rp
->r_dycode
= DC_DOWGEQ
;
1367 rp
->r_dycode
= DC_DOM
;
1369 if (rp
->r_dycode
!= DC_DOM
) {
1372 error(_("invalid day of month"));
1376 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1377 error(_("invalid weekday name"));
1381 rp
->r_wday
= lp
->l_value
;
1383 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1384 rp
->r_dayofmonth
<= 0 ||
1385 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1386 error(_("invalid day of month"));
1400 register long shift
;
1402 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1403 buf
[i
] = val
>> shift
;
1414 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1422 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1424 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1431 const char * const name
;
1435 static char * fullname
;
1436 static struct tzhead tzh
;
1437 time_t ats
[TZ_MAX_TIMES
];
1438 unsigned char types
[TZ_MAX_TIMES
];
1444 (void) qsort((void *) attypes
, (size_t) timecnt
,
1445 (size_t) sizeof *attypes
, atcomp
);
1455 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1458 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1459 ++fromi
; /* handled by default rule */
1460 for ( ; fromi
< timecnt
; ++fromi
) {
1462 && ((attypes
[fromi
].at
1463 + gmtoffs
[attypes
[toi
- 1].type
])
1464 <= (attypes
[toi
- 1].at
1465 + gmtoffs
[toi
== 1 ? 0
1466 : attypes
[toi
- 2].type
]))) {
1467 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1471 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1472 attypes
[toi
++] = attypes
[fromi
];
1479 for (i
= 0; i
< timecnt
; ++i
) {
1480 ats
[i
] = attypes
[i
].at
;
1481 types
[i
] = attypes
[i
].type
;
1483 fullname
= erealloc(fullname
,
1484 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1485 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1487 ** Remove old file, if any, to snap links.
1489 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1490 const char *e
= strerror(errno
);
1492 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1493 progname
, fullname
, e
);
1494 (void) exit(EXIT_FAILURE
);
1496 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1497 if (mkdirs(fullname
) != 0)
1498 (void) exit(EXIT_FAILURE
);
1499 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1500 const char *e
= strerror(errno
);
1502 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1503 progname
, fullname
, e
);
1504 (void) exit(EXIT_FAILURE
);
1507 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1508 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1509 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1510 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1511 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1512 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1513 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1514 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1524 for (i
= 0; i
< timecnt
; ++i
) {
1527 if (ats
[i
] >= trans
[j
]) {
1528 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1531 puttzcode((long) ats
[i
], fp
);
1534 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1535 (size_t) timecnt
, fp
);
1536 for (i
= 0; i
< typecnt
; ++i
) {
1537 puttzcode((long) gmtoffs
[i
], fp
);
1538 (void) putc(isdsts
[i
], fp
);
1539 (void) putc(abbrinds
[i
], fp
);
1542 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1543 (size_t) charcnt
, fp
);
1544 for (i
= 0; i
< leapcnt
; ++i
) {
1546 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1549 if (++j
>= typecnt
) {
1555 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1559 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1560 } else puttzcode((long) trans
[i
], fp
);
1561 puttzcode((long) corr
[i
], fp
);
1563 for (i
= 0; i
< typecnt
; ++i
)
1564 (void) putc(ttisstds
[i
], fp
);
1565 for (i
= 0; i
< typecnt
; ++i
)
1566 (void) putc(ttisgmts
[i
], fp
);
1567 if (ferror(fp
) || fclose(fp
)) {
1568 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1569 progname
, fullname
);
1570 (void) exit(EXIT_FAILURE
);
1575 doabbr(abbr
, format
, letters
, isdst
)
1577 const char * const format
;
1578 const char * const letters
;
1581 if (strchr(format
, '/') == NULL
) {
1582 if (letters
== NULL
)
1583 (void) strcpy(abbr
, format
);
1584 else (void) sprintf(abbr
, format
, letters
);
1586 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1588 (void) strcpy(abbr
, format
);
1589 *strchr(abbr
, '/') = '\0';
1594 outzone(zpfirst
, zonecount
)
1595 const struct zone
* const zpfirst
;
1596 const int zonecount
;
1598 register const struct zone
* zp
;
1599 register struct rule
* rp
;
1601 register int usestart
, useuntil
;
1602 register time_t starttime
, untiltime
;
1603 register long gmtoff
;
1604 register long stdoff
;
1606 register long startoff
;
1607 register int startttisstd
;
1608 register int startttisgmt
;
1610 char startbuf
[BUFSIZ
];
1612 INITIALIZE(untiltime
);
1613 INITIALIZE(starttime
);
1615 ** Now. . .finally. . .generate some useful data!
1621 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1622 ** for noting the need to unconditionally initialize startttisstd.
1624 startttisstd
= FALSE
;
1625 startttisgmt
= FALSE
;
1626 for (i
= 0; i
< zonecount
; ++i
) {
1628 ** A guess that may well be corrected later.
1632 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1633 useuntil
= i
< (zonecount
- 1);
1634 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1636 gmtoff
= zp
->z_gmtoff
;
1637 eat(zp
->z_filename
, zp
->z_linenum
);
1639 startoff
= zp
->z_gmtoff
;
1640 if (zp
->z_nrules
== 0) {
1641 stdoff
= zp
->z_stdoff
;
1642 doabbr(startbuf
, zp
->z_format
,
1643 (char *) NULL
, stdoff
!= 0);
1644 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1645 startbuf
, stdoff
!= 0, startttisstd
,
1648 addtt(starttime
, type
);
1650 } else if (stdoff
!= 0)
1651 addtt(min_time
, type
);
1652 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1653 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1656 ** Mark which rules to do in the current year.
1657 ** For those to do, calculate rpytime(rp, year);
1659 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1660 rp
= &zp
->z_rules
[j
];
1661 eats(zp
->z_filename
, zp
->z_linenum
,
1662 rp
->r_filename
, rp
->r_linenum
);
1663 rp
->r_todo
= year
>= rp
->r_loyear
&&
1664 year
<= rp
->r_hiyear
&&
1665 yearistype(year
, rp
->r_yrtype
);
1667 rp
->r_temp
= rpytime(rp
, year
);
1671 register time_t jtime
, ktime
;
1672 register long offset
;
1678 ** Turn untiltime into UTC
1679 ** assuming the current gmtoff and
1682 untiltime
= zp
->z_untiltime
;
1683 if (!zp
->z_untilrule
.r_todisgmt
)
1684 untiltime
= tadd(untiltime
,
1686 if (!zp
->z_untilrule
.r_todisstd
)
1687 untiltime
= tadd(untiltime
,
1691 ** Find the rule (of those to do, if any)
1692 ** that takes effect earliest in the year.
1695 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1696 rp
= &zp
->z_rules
[j
];
1699 eats(zp
->z_filename
, zp
->z_linenum
,
1700 rp
->r_filename
, rp
->r_linenum
);
1701 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1702 if (!rp
->r_todisstd
)
1703 offset
= oadd(offset
, stdoff
);
1705 if (jtime
== min_time
||
1708 jtime
= tadd(jtime
, -offset
);
1709 if (k
< 0 || jtime
< ktime
) {
1715 break; /* go on to next year */
1716 rp
= &zp
->z_rules
[k
];
1718 if (useuntil
&& ktime
>= untiltime
)
1720 stdoff
= rp
->r_stdoff
;
1721 if (usestart
&& ktime
== starttime
)
1724 if (ktime
< starttime
) {
1725 startoff
= oadd(zp
->z_gmtoff
,
1727 doabbr(startbuf
, zp
->z_format
,
1732 if (*startbuf
== '\0' &&
1733 startoff
== oadd(zp
->z_gmtoff
,
1735 doabbr(startbuf
, zp
->z_format
,
1740 eats(zp
->z_filename
, zp
->z_linenum
,
1741 rp
->r_filename
, rp
->r_linenum
);
1742 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1744 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1745 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1746 rp
->r_todisstd
, rp
->r_todisgmt
);
1751 if (*startbuf
== '\0' &&
1752 zp
->z_format
!= NULL
&&
1753 strchr(zp
->z_format
, '%') == NULL
&&
1754 strchr(zp
->z_format
, '/') == NULL
)
1755 (void) strcpy(startbuf
, zp
->z_format
);
1756 eat(zp
->z_filename
, zp
->z_linenum
);
1757 if (*startbuf
== '\0')
1758 error(_("can't determine time zone abbreviation to use just after until time"));
1759 else addtt(starttime
,
1760 addtype(startoff
, startbuf
,
1761 startoff
!= zp
->z_gmtoff
,
1766 ** Now we may get to set starttime for the next zone line.
1769 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1770 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1771 starttime
= zp
->z_untiltime
;
1773 starttime
= tadd(starttime
, -stdoff
);
1775 starttime
= tadd(starttime
, -gmtoff
);
1778 writezone(zpfirst
->z_name
);
1782 addtt(starttime
, type
)
1783 const time_t starttime
;
1786 if (starttime
<= min_time
||
1787 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1788 gmtoffs
[0] = gmtoffs
[type
];
1789 isdsts
[0] = isdsts
[type
];
1790 ttisstds
[0] = ttisstds
[type
];
1791 ttisgmts
[0] = ttisgmts
[type
];
1792 if (abbrinds
[type
] != 0)
1793 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1795 charcnt
= strlen(chars
) + 1;
1800 if (timecnt
>= TZ_MAX_TIMES
) {
1801 error(_("too many transitions?!"));
1802 (void) exit(EXIT_FAILURE
);
1804 attypes
[timecnt
].at
= starttime
;
1805 attypes
[timecnt
].type
= type
;
1810 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1812 const char * const abbr
;
1819 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1820 error(_("internal error - addtype called with bad isdst"));
1821 (void) exit(EXIT_FAILURE
);
1823 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1824 error(_("internal error - addtype called with bad ttisstd"));
1825 (void) exit(EXIT_FAILURE
);
1827 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1828 error(_("internal error - addtype called with bad ttisgmt"));
1829 (void) exit(EXIT_FAILURE
);
1832 ** See if there's already an entry for this zone type.
1833 ** If so, just return its index.
1835 for (i
= 0; i
< typecnt
; ++i
) {
1836 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1837 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1838 ttisstd
== ttisstds
[i
] &&
1839 ttisgmt
== ttisgmts
[i
])
1843 ** There isn't one; add a new one, unless there are already too
1846 if (typecnt
>= TZ_MAX_TYPES
) {
1847 error(_("too many local time types"));
1848 (void) exit(EXIT_FAILURE
);
1850 gmtoffs
[i
] = gmtoff
;
1852 ttisstds
[i
] = ttisstd
;
1853 ttisgmts
[i
] = ttisgmt
;
1855 for (j
= 0; j
< charcnt
; ++j
)
1856 if (strcmp(&chars
[j
], abbr
) == 0)
1866 leapadd(t
, positive
, rolling
, count
)
1874 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1875 error(_("too many leap seconds"));
1876 (void) exit(EXIT_FAILURE
);
1878 for (i
= 0; i
< leapcnt
; ++i
)
1879 if (t
<= trans
[i
]) {
1880 if (t
== trans
[i
]) {
1881 error(_("repeated leap second moment"));
1882 (void) exit(EXIT_FAILURE
);
1887 for (j
= leapcnt
; j
> i
; --j
) {
1888 trans
[j
] = trans
[j
- 1];
1889 corr
[j
] = corr
[j
- 1];
1890 roll
[j
] = roll
[j
- 1];
1893 corr
[i
] = positive
? 1L : eitol(-count
);
1896 } while (positive
&& --count
!= 0);
1903 register long last
= 0;
1906 ** propagate leap seconds forward
1908 for (i
= 0; i
< leapcnt
; ++i
) {
1909 trans
[i
] = tadd(trans
[i
], last
);
1910 last
= corr
[i
] += last
;
1915 yearistype(year
, type
)
1917 const char * const type
;
1922 if (type
== NULL
|| *type
== '\0')
1924 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1925 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1926 result
= system(buf
);
1927 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1933 error(_("Wild result from command execution"));
1934 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1935 progname
, buf
, result
);
1937 (void) exit(EXIT_FAILURE
);
1944 a
= (unsigned char) a
;
1945 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1949 ciequal(ap
, bp
) /* case-insensitive equality */
1950 register const char * ap
;
1951 register const char * bp
;
1953 while (lowerit(*ap
) == lowerit(*bp
++))
1961 register const char * abbr
;
1962 register const char * word
;
1964 if (lowerit(*abbr
) != lowerit(*word
))
1967 while (*++abbr
!= '\0')
1971 } while (lowerit(*word
++) != lowerit(*abbr
));
1975 static const struct lookup
*
1977 register const char * const word
;
1978 register const struct lookup
* const table
;
1980 register const struct lookup
* foundlp
;
1981 register const struct lookup
* lp
;
1983 if (word
== NULL
|| table
== NULL
)
1986 ** Look for exact match.
1988 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1989 if (ciequal(word
, lp
->l_word
))
1992 ** Look for inexact match.
1995 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1996 if (itsabbr(word
, lp
->l_word
)) {
1997 if (foundlp
== NULL
)
1999 else return NULL
; /* multiple inexact matches */
2009 register char ** array
;
2014 array
= (char **) (void *)
2015 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2018 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2020 if (*cp
== '\0' || *cp
== '#')
2022 array
[nsubs
++] = dp
= cp
;
2024 if ((*dp
= *cp
++) != '"')
2026 else while ((*dp
= *cp
++) != '"')
2029 else error(_("Odd number of quotation marks"));
2030 } while (*cp
!= '\0' && *cp
!= '#' &&
2031 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2032 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2036 array
[nsubs
] = NULL
;
2048 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2049 error(_("time overflow"));
2050 (void) exit(EXIT_FAILURE
);
2062 if (t1
== max_time
&& t2
> 0)
2064 if (t1
== min_time
&& t2
< 0)
2067 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2068 error(_("time overflow"));
2069 (void) exit(EXIT_FAILURE
);
2075 ** Given a rule, and a year, compute the date - in seconds since January 1,
2076 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2080 rpytime(rp
, wantedy
)
2081 register const struct rule
* const rp
;
2082 register const int wantedy
;
2084 register int y
, m
, i
;
2085 register long dayoff
; /* with a nod to Margaret O. */
2088 if (wantedy
== INT_MIN
)
2090 if (wantedy
== INT_MAX
)
2095 while (wantedy
!= y
) {
2097 i
= len_years
[isleap(y
)];
2101 i
= -len_years
[isleap(y
)];
2103 dayoff
= oadd(dayoff
, eitol(i
));
2105 while (m
!= rp
->r_month
) {
2106 i
= len_months
[isleap(y
)][m
];
2107 dayoff
= oadd(dayoff
, eitol(i
));
2110 i
= rp
->r_dayofmonth
;
2111 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2112 if (rp
->r_dycode
== DC_DOWLEQ
)
2115 error(_("use of 2/29 in non leap-year"));
2116 (void) exit(EXIT_FAILURE
);
2120 dayoff
= oadd(dayoff
, eitol(i
));
2121 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2124 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2125 wday
= eitol(EPOCH_WDAY
);
2127 ** Don't trust mod of negative numbers.
2130 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2132 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2134 wday
+= LDAYSPERWEEK
;
2136 while (wday
!= eitol(rp
->r_wday
))
2137 if (rp
->r_dycode
== DC_DOWGEQ
) {
2138 dayoff
= oadd(dayoff
, (long) 1);
2139 if (++wday
>= LDAYSPERWEEK
)
2143 dayoff
= oadd(dayoff
, (long) -1);
2145 wday
= LDAYSPERWEEK
- 1;
2148 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2149 error(_("no day in month matches rule"));
2150 (void) exit(EXIT_FAILURE
);
2153 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2155 t
= (time_t) dayoff
* SECSPERDAY
;
2157 ** Cheap overflow check.
2159 if (t
/ SECSPERDAY
!= dayoff
)
2160 return (dayoff
> 0) ? max_time
: min_time
;
2161 return tadd(t
, rp
->r_tod
);
2166 const char * const string
;
2170 i
= strlen(string
) + 1;
2171 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2172 error(_("too many, or too long, time zone abbreviations"));
2173 (void) exit(EXIT_FAILURE
);
2175 (void) strcpy(&chars
[charcnt
], string
);
2176 charcnt
+= eitol(i
);
2181 char * const argname
;
2183 register char * name
;
2186 if (argname
== NULL
|| *argname
== '\0')
2188 cp
= name
= ecpyalloc(argname
);
2189 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2193 ** DOS drive specifier?
2195 if (isalpha((unsigned char) name
[0]) &&
2196 name
[1] == ':' && name
[2] == '\0') {
2200 #endif /* !defined unix */
2201 if (!itsdir(name
)) {
2203 ** It doesn't seem to exist, so we try to create it.
2204 ** Creation may fail because of the directory being
2205 ** created by some other multiprocessor, so we get
2206 ** to do extra checking.
2208 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2209 const char *e
= strerror(errno
);
2211 if (errno
!= EEXIST
|| !itsdir(name
)) {
2212 (void) fprintf(stderr
,
2213 _("%s: Can't create directory %s: %s\n"),
2233 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2234 (void) fprintf(stderr
,
2235 _("%s: %d did not sign extend correctly\n"),
2237 (void) exit(EXIT_FAILURE
);
2243 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.