3 static char elsieid
[] = "@(#)zic.c 7.100";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
11 #include "sys/stat.h" /* for umask manifest constants */
12 #endif /* defined unix */
17 ** On some ancient hosts, predicates like `isspace(C)' are defined
18 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
19 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
20 ** Neither the C Standard nor Posix require that `isascii' exist.
21 ** For portability, we check both ancient and modern requirements.
22 ** If isascii is not defined, the isascii check succeeds trivially.
30 const char * r_filename
;
34 int r_loyear
; /* for example, 1986 */
35 int r_hiyear
; /* for example, 1986 */
36 const char * r_yrtype
;
38 int r_month
; /* 0..11 */
40 int r_dycode
; /* see below */
44 long r_tod
; /* time from midnight */
45 int r_todisstd
; /* above is standard time if TRUE */
46 /* or wall clock time if FALSE */
47 int r_todisgmt
; /* above is GMT if TRUE */
48 /* or local time if FALSE */
49 long r_stdoff
; /* offset from standard time */
50 const char * r_abbrvar
; /* variable part of abbreviation */
52 int r_todo
; /* a rule to do (used in outzone) */
53 time_t r_temp
; /* used in outzone */
57 ** r_dycode r_dayofmonth r_wday
60 #define DC_DOM 0 /* 1..31 */ /* unused */
61 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
62 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
65 const char * z_filename
;
71 const char * z_format
;
75 struct rule
* z_rules
;
78 struct rule z_untilrule
;
82 extern int getopt
P((int argc
, char * const argv
[],
83 const char * options
));
84 extern int link
P((const char * fromname
, const char * toname
));
88 static void addtt
P((time_t starttime
, int type
));
89 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
90 int ttisstd
, int ttisgmt
));
91 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
92 static void adjleap
P((void));
93 static void associate
P((void));
94 static int ciequal
P((const char * ap
, const char * bp
));
95 static void convert
P((long val
, char * buf
));
96 static void dolink
P((const char * fromfile
, const char * tofile
));
97 static void doabbr
P((char * abbr
, const char * format
,
98 const char * letters
, int isdst
));
99 static void eat
P((const char * name
, int num
));
100 static void eats
P((const char * name
, int num
,
101 const char * rname
, int rnum
));
102 static long eitol
P((int i
));
103 static void error
P((const char * message
));
104 static char ** getfields
P((char * buf
));
105 static long gethms
P((const char * string
, const char * errstrng
,
107 static void infile
P((const char * filename
));
108 static void inleap
P((char ** fields
, int nfields
));
109 static void inlink
P((char ** fields
, int nfields
));
110 static void inrule
P((char ** fields
, int nfields
));
111 static int inzcont
P((char ** fields
, int nfields
));
112 static int inzone
P((char ** fields
, int nfields
));
113 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
114 static int itsabbr
P((const char * abbr
, const char * word
));
115 static int itsdir
P((const char * name
));
116 static int lowerit
P((int c
));
117 static char * memcheck
P((char * tocheck
));
118 static int mkdirs
P((char * filename
));
119 static void newabbr
P((const char * abbr
));
120 static long oadd
P((long t1
, long t2
));
121 static void outzone
P((const struct zone
* zp
, int ntzones
));
122 static void puttzcode
P((long code
, FILE * fp
));
123 static int rcomp
P((const void * leftp
, const void * rightp
));
124 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
125 static void rulesub
P((struct rule
* rp
,
126 const char * loyearp
, const char * hiyearp
,
127 const char * typep
, const char * monthp
,
128 const char * dayp
, const char * timep
));
129 static void setboundaries
P((void));
130 static time_t tadd
P((time_t t1
, long t2
));
131 static void usage
P((void));
132 static void writezone
P((const char * name
));
133 static int yearistype
P((int year
, const char * type
));
135 #if !(HAVE_STRERROR - 0)
136 static char * strerror
P((int));
137 #endif /* !(HAVE_STRERROR - 0) */
141 static const char * filename
;
144 static time_t max_time
;
146 static int max_year_representable
;
147 static time_t min_time
;
149 static int min_year_representable
;
151 static const char * rfilename
;
153 static const char * progname
;
167 ** Which fields are which on a Zone line.
175 #define ZF_TILMONTH 6
178 #define ZONE_MINFIELDS 5
179 #define ZONE_MAXFIELDS 9
182 ** Which fields are which on a Zone continuation line.
188 #define ZFC_TILYEAR 3
189 #define ZFC_TILMONTH 4
191 #define ZFC_TILTIME 6
192 #define ZONEC_MINFIELDS 3
193 #define ZONEC_MAXFIELDS 7
196 ** Which files are which on a Rule line.
208 #define RULE_FIELDS 10
211 ** Which fields are which on a Link line.
216 #define LINK_FIELDS 3
219 ** Which fields are which on a Leap line.
228 #define LEAP_FIELDS 7
238 static struct rule
* rules
;
239 static int nrules
; /* number of rules */
241 static struct zone
* zones
;
242 static int nzones
; /* number of zones */
245 const char * l_filename
;
251 static struct link
* links
;
259 static struct lookup
const * byword
P((const char * string
,
260 const struct lookup
* lp
));
262 static struct lookup
const line_codes
[] = {
270 static struct lookup
const mon_names
[] = {
271 { "January", TM_JANUARY
},
272 { "February", TM_FEBRUARY
},
273 { "March", TM_MARCH
},
274 { "April", TM_APRIL
},
278 { "August", TM_AUGUST
},
279 { "September", TM_SEPTEMBER
},
280 { "October", TM_OCTOBER
},
281 { "November", TM_NOVEMBER
},
282 { "December", TM_DECEMBER
},
286 static struct lookup
const wday_names
[] = {
287 { "Sunday", TM_SUNDAY
},
288 { "Monday", TM_MONDAY
},
289 { "Tuesday", TM_TUESDAY
},
290 { "Wednesday", TM_WEDNESDAY
},
291 { "Thursday", TM_THURSDAY
},
292 { "Friday", TM_FRIDAY
},
293 { "Saturday", TM_SATURDAY
},
297 static struct lookup
const lasts
[] = {
298 { "last-Sunday", TM_SUNDAY
},
299 { "last-Monday", TM_MONDAY
},
300 { "last-Tuesday", TM_TUESDAY
},
301 { "last-Wednesday", TM_WEDNESDAY
},
302 { "last-Thursday", TM_THURSDAY
},
303 { "last-Friday", TM_FRIDAY
},
304 { "last-Saturday", TM_SATURDAY
},
308 static struct lookup
const begin_years
[] = {
309 { "minimum", YR_MINIMUM
},
310 { "maximum", YR_MAXIMUM
},
314 static struct lookup
const end_years
[] = {
315 { "minimum", YR_MINIMUM
},
316 { "maximum", YR_MAXIMUM
},
321 static struct lookup
const leap_types
[] = {
323 { "Stationary", FALSE
},
327 static const int len_months
[2][MONSPERYEAR
] = {
328 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
329 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
332 static const int len_years
[2] = {
333 DAYSPERNYEAR
, DAYSPERLYEAR
336 static struct attype
{
339 } attypes
[TZ_MAX_TIMES
];
340 static long gmtoffs
[TZ_MAX_TYPES
];
341 static char isdsts
[TZ_MAX_TYPES
];
342 static unsigned char abbrinds
[TZ_MAX_TYPES
];
343 static char ttisstds
[TZ_MAX_TYPES
];
344 static char ttisgmts
[TZ_MAX_TYPES
];
345 static char chars
[TZ_MAX_CHARS
];
346 static time_t trans
[TZ_MAX_LEAPS
];
347 static long corr
[TZ_MAX_LEAPS
];
348 static char roll
[TZ_MAX_LEAPS
];
351 ** Memory allocation.
359 const char *e
= strerror(errno
);
361 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
363 (void) exit(EXIT_FAILURE
);
368 #define emalloc(size) memcheck(imalloc(size))
369 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
370 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
371 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
377 #if !(HAVE_STRERROR - 0)
382 extern char * sys_errlist
[];
385 return (errnum
> 0 && errnum
<= sys_nerr
) ?
386 sys_errlist
[errnum
] : _("Unknown system error");
388 #endif /* !(HAVE_STRERROR - 0) */
391 eats(name
, num
, rname
, rnum
)
392 const char * const name
;
394 const char * const rname
;
405 const char * const name
;
408 eats(name
, num
, (char *) NULL
, -1);
413 const char * const string
;
416 ** Match the format of "cc" to allow sh users to
417 ** zic ... 2>&1 | error -t "*" -v
420 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
421 filename
, linenum
, string
);
422 if (rfilename
!= NULL
)
423 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
424 rfilename
, rlinenum
);
425 (void) fprintf(stderr
, "\n");
431 const char * const string
;
435 cp
= ecpyalloc(_("warning: "));
436 cp
= ecatalloc(cp
, string
);
445 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
447 (void) exit(EXIT_FAILURE
);
450 static const char * psxrules
;
451 static const char * lcltime
;
452 static const char * directory
;
453 static const char * leapsec
;
454 static const char * yitcommand
;
455 static int sflag
= FALSE
;
467 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
468 #endif /* defined unix */
470 (void) setlocale(LC_MESSAGES
, "");
472 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
473 #endif /* defined TEXTDOMAINDIR */
474 (void) textdomain(TZ_DOMAIN
);
475 #endif /* HAVE_GETTEXT - 0 */
477 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
482 if (directory
== NULL
)
485 (void) fprintf(stderr
,
486 _("%s: More than one -d option specified\n"),
488 (void) exit(EXIT_FAILURE
);
495 (void) fprintf(stderr
,
496 _("%s: More than one -l option specified\n"),
498 (void) exit(EXIT_FAILURE
);
502 if (psxrules
== NULL
)
505 (void) fprintf(stderr
,
506 _("%s: More than one -p option specified\n"),
508 (void) exit(EXIT_FAILURE
);
512 if (yitcommand
== NULL
)
515 (void) fprintf(stderr
,
516 _("%s: More than one -y option specified\n"),
518 (void) exit(EXIT_FAILURE
);
525 (void) fprintf(stderr
,
526 _("%s: More than one -L option specified\n"),
528 (void) exit(EXIT_FAILURE
);
538 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
539 usage(); /* usage message by request */
540 if (directory
== NULL
)
542 if (yitcommand
== NULL
)
543 yitcommand
= "yearistype";
547 if (optind
< argc
&& leapsec
!= NULL
) {
552 for (i
= optind
; i
< argc
; ++i
)
555 (void) exit(EXIT_FAILURE
);
557 for (i
= 0; i
< nzones
; i
= j
) {
559 ** Find the next non-continuation zone entry.
561 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
563 outzone(&zones
[i
], j
- i
);
568 for (i
= 0; i
< nlinks
; ++i
) {
569 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
570 dolink(links
[i
].l_from
, links
[i
].l_to
);
572 if (lcltime
!= NULL
) {
573 eat("command line", 1);
574 dolink(lcltime
, TZDEFAULT
);
576 if (psxrules
!= NULL
) {
577 eat("command line", 1);
578 dolink(psxrules
, TZDEFRULES
);
580 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
584 dolink(fromfile
, tofile
)
585 const char * const fromfile
;
586 const char * const tofile
;
588 register char * fromname
;
589 register char * toname
;
591 if (fromfile
[0] == '/')
592 fromname
= ecpyalloc(fromfile
);
594 fromname
= ecpyalloc(directory
);
595 fromname
= ecatalloc(fromname
, "/");
596 fromname
= ecatalloc(fromname
, fromfile
);
598 if (tofile
[0] == '/')
599 toname
= ecpyalloc(tofile
);
601 toname
= ecpyalloc(directory
);
602 toname
= ecatalloc(toname
, "/");
603 toname
= ecatalloc(toname
, tofile
);
606 ** We get to be careful here since
607 ** there's a fair chance of root running us.
610 (void) remove(toname
);
611 if (link(fromname
, toname
) != 0) {
614 if (mkdirs(toname
) != 0)
615 (void) exit(EXIT_FAILURE
);
617 result
= link(fromname
, toname
);
618 #if (HAVE_SYMLINK - 0)
620 const char *s
= tofile
;
621 register char *symlinkcontents
= NULL
;
622 while ((s
= strchr(s
+1, '/')) != NULL
)
623 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
624 symlinkcontents
= ecatalloc(symlinkcontents
, fromname
);
626 result
= unlink(toname
);
627 if (result
!= 0 && errno
!= ENOENT
) {
628 const char *e
= strerror(errno
);
630 (void) fprintf(stderr
,
631 _("%s: Can't unlink %s: %s\n"),
632 progname
, toname
, e
);
633 (void) exit(EXIT_FAILURE
);
636 result
= symlink(symlinkcontents
, toname
);
638 warning(_("hard link failed, symbolic link used"));
639 ifree(symlinkcontents
);
643 const char *e
= strerror(errno
);
645 (void) fprintf(stderr
,
646 _("%s: Can't link from %s to %s: %s\n"),
647 progname
, fromname
, toname
, e
);
648 (void) exit(EXIT_FAILURE
);
656 #define INT_MAX ((int) (((unsigned)~0)>>1))
657 #endif /* !defined INT_MAX */
660 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
661 #endif /* !defined INT_MIN */
664 ** The tz file format currently allows at most 32-bit quantities.
665 ** This restriction should be removed before signed 32-bit values
666 ** wrap around in 2038, but unfortunately this will require a
667 ** change to the tz file format.
670 #define MAX_BITS_IN_FILE 32
671 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
674 setboundaries
P((void))
676 if (TYPE_SIGNED(time_t)) {
677 min_time
= ~ (time_t) 0;
678 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
679 max_time
= ~ (time_t) 0 - min_time
;
684 max_time
= 2 - sflag
;
685 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
688 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
689 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
690 min_year_representable
= min_year
;
691 max_year_representable
= max_year
;
696 const char * const name
;
698 register char * myname
;
701 myname
= ecpyalloc(name
);
702 myname
= ecatalloc(myname
, "/.");
703 accres
= access(myname
, F_OK
);
709 ** Associate sets of rules with zones.
713 ** Sort by rule name.
721 return strcmp(((const struct rule
*) cp1
)->r_name
,
722 ((const struct rule
*) cp2
)->r_name
);
728 register struct zone
* zp
;
729 register struct rule
* rp
;
730 register int base
, out
;
734 (void) qsort((void *) rules
, (size_t) nrules
,
735 (size_t) sizeof *rules
, rcomp
);
736 for (i
= 0; i
< nrules
- 1; ++i
) {
737 if (strcmp(rules
[i
].r_name
,
738 rules
[i
+ 1].r_name
) != 0)
740 if (strcmp(rules
[i
].r_filename
,
741 rules
[i
+ 1].r_filename
) == 0)
743 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
744 warning(_("same rule name in multiple files"));
745 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
746 warning(_("same rule name in multiple files"));
747 for (j
= i
+ 2; j
< nrules
; ++j
) {
748 if (strcmp(rules
[i
].r_name
,
749 rules
[j
].r_name
) != 0)
751 if (strcmp(rules
[i
].r_filename
,
752 rules
[j
].r_filename
) == 0)
754 if (strcmp(rules
[i
+ 1].r_filename
,
755 rules
[j
].r_filename
) == 0)
762 for (i
= 0; i
< nzones
; ++i
) {
767 for (base
= 0; base
< nrules
; base
= out
) {
769 for (out
= base
+ 1; out
< nrules
; ++out
)
770 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
772 for (i
= 0; i
< nzones
; ++i
) {
774 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
777 zp
->z_nrules
= out
- base
;
780 for (i
= 0; i
< nzones
; ++i
) {
782 if (zp
->z_nrules
== 0) {
784 ** Maybe we have a local standard time offset.
786 eat(zp
->z_filename
, zp
->z_linenum
);
787 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
790 ** Note, though, that if there's no rule,
791 ** a '%s' in the format is a bad thing.
793 if (strchr(zp
->z_format
, '%') != 0)
794 error(_("%s in ruleless zone"));
798 (void) exit(EXIT_FAILURE
);
806 register char ** fields
;
808 register const struct lookup
* lp
;
809 register int nfields
;
810 register int wantcont
;
814 if (strcmp(name
, "-") == 0) {
815 name
= _("standard input");
817 } else if ((fp
= fopen(name
, "r")) == NULL
) {
818 const char *e
= strerror(errno
);
820 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
822 (void) exit(EXIT_FAILURE
);
825 for (num
= 1; ; ++num
) {
827 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
829 cp
= strchr(buf
, '\n');
831 error(_("line too long"));
832 (void) exit(EXIT_FAILURE
);
835 fields
= getfields(buf
);
837 while (fields
[nfields
] != NULL
) {
840 if (strcmp(fields
[nfields
], "-") == 0)
841 fields
[nfields
] = &nada
;
846 } else if (wantcont
) {
847 wantcont
= inzcont(fields
, nfields
);
849 lp
= byword(fields
[0], line_codes
);
851 error(_("input line of unknown type"));
852 else switch ((int) (lp
->l_value
)) {
854 inrule(fields
, nfields
);
858 wantcont
= inzone(fields
, nfields
);
861 inlink(fields
, nfields
);
866 (void) fprintf(stderr
,
867 _("%s: Leap line in non leap seconds file %s\n"),
869 else inleap(fields
, nfields
);
872 default: /* "cannot happen" */
873 (void) fprintf(stderr
,
874 _("%s: panic: Invalid l_value %d\n"),
875 progname
, lp
->l_value
);
876 (void) exit(EXIT_FAILURE
);
879 ifree((char *) fields
);
882 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
884 (void) exit(EXIT_FAILURE
);
886 if (fp
!= stdin
&& fclose(fp
)) {
887 const char *e
= strerror(errno
);
889 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
890 progname
, filename
, e
);
891 (void) exit(EXIT_FAILURE
);
894 error(_("expected continuation line not found"));
898 ** Convert a string of one of the forms
899 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
900 ** into a number of seconds.
901 ** A null string maps to zero.
902 ** Call error with errstring and return zero on errors.
906 gethms(string
, errstring
, signable
)
908 const char * const errstring
;
911 int hh
, mm
, ss
, sign
;
913 if (string
== NULL
|| *string
== '\0')
917 else if (*string
== '-') {
921 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
923 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
925 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
926 &hh
, &mm
, &ss
) != 3) {
930 if ((hh
< 0 || hh
>= HOURSPERDAY
||
931 mm
< 0 || mm
>= MINSPERHOUR
||
932 ss
< 0 || ss
> SECSPERMIN
) &&
933 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
938 (eitol(hh
* MINSPERHOUR
+ mm
) *
939 eitol(SECSPERMIN
) + eitol(ss
));
943 inrule(fields
, nfields
)
944 register char ** const fields
;
947 static struct rule r
;
949 if (nfields
!= RULE_FIELDS
) {
950 error(_("wrong number of fields on Rule line"));
953 if (*fields
[RF_NAME
] == '\0') {
954 error(_("nameless rule"));
957 r
.r_filename
= filename
;
958 r
.r_linenum
= linenum
;
959 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
960 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
961 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
962 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
963 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
964 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
965 (int) ((nrules
+ 1) * sizeof *rules
));
970 inzone(fields
, nfields
)
971 register char ** const fields
;
977 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
978 error(_("wrong number of fields on Zone line"));
981 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
982 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
984 _("\"Zone %s\" line and -l option are mutually exclusive"),
989 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
990 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
992 _("\"Zone %s\" line and -p option are mutually exclusive"),
997 for (i
= 0; i
< nzones
; ++i
)
998 if (zones
[i
].z_name
!= NULL
&&
999 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1000 buf
= erealloc(buf
, (int) (132 +
1001 strlen(fields
[ZF_NAME
]) +
1002 strlen(zones
[i
].z_filename
)));
1004 _("duplicate zone name %s (file \"%s\", line %d)"),
1006 zones
[i
].z_filename
,
1007 zones
[i
].z_linenum
);
1011 return inzsub(fields
, nfields
, FALSE
);
1015 inzcont(fields
, nfields
)
1016 register char ** const fields
;
1019 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1020 error(_("wrong number of fields on Zone continuation line"));
1023 return inzsub(fields
, nfields
, TRUE
);
1027 inzsub(fields
, nfields
, iscont
)
1028 register char ** const fields
;
1033 static struct zone z
;
1034 register int i_gmtoff
, i_rule
, i_format
;
1035 register int i_untilyear
, i_untilmonth
;
1036 register int i_untilday
, i_untiltime
;
1037 register int hasuntil
;
1040 i_gmtoff
= ZFC_GMTOFF
;
1042 i_format
= ZFC_FORMAT
;
1043 i_untilyear
= ZFC_TILYEAR
;
1044 i_untilmonth
= ZFC_TILMONTH
;
1045 i_untilday
= ZFC_TILDAY
;
1046 i_untiltime
= ZFC_TILTIME
;
1049 i_gmtoff
= ZF_GMTOFF
;
1051 i_format
= ZF_FORMAT
;
1052 i_untilyear
= ZF_TILYEAR
;
1053 i_untilmonth
= ZF_TILMONTH
;
1054 i_untilday
= ZF_TILDAY
;
1055 i_untiltime
= ZF_TILTIME
;
1056 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1058 z
.z_filename
= filename
;
1059 z
.z_linenum
= linenum
;
1060 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1061 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1062 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1063 error(_("invalid abbreviation format"));
1067 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1068 z
.z_format
= ecpyalloc(fields
[i_format
]);
1069 hasuntil
= nfields
> i_untilyear
;
1071 z
.z_untilrule
.r_filename
= filename
;
1072 z
.z_untilrule
.r_linenum
= linenum
;
1073 rulesub(&z
.z_untilrule
,
1074 fields
[i_untilyear
],
1077 (nfields
> i_untilmonth
) ?
1078 fields
[i_untilmonth
] : "Jan",
1079 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1080 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1081 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1082 z
.z_untilrule
.r_loyear
);
1083 if (iscont
&& nzones
> 0 &&
1084 z
.z_untiltime
> min_time
&&
1085 z
.z_untiltime
< max_time
&&
1086 zones
[nzones
- 1].z_untiltime
> min_time
&&
1087 zones
[nzones
- 1].z_untiltime
< max_time
&&
1088 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1089 error(_("Zone continuation line end time is not after end time of previous line"));
1093 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1094 (int) ((nzones
+ 1) * sizeof *zones
));
1095 zones
[nzones
++] = z
;
1097 ** If there was an UNTIL field on this line,
1098 ** there's more information about the zone on the next line.
1104 inleap(fields
, nfields
)
1105 register char ** const fields
;
1108 register const char * cp
;
1109 register const struct lookup
* lp
;
1111 int year
, month
, day
;
1115 if (nfields
!= LEAP_FIELDS
) {
1116 error(_("wrong number of fields on Leap line"));
1120 cp
= fields
[LP_YEAR
];
1121 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1125 error(_("invalid leaping year"));
1131 i
= len_years
[isleap(j
)];
1135 i
= -len_years
[isleap(j
)];
1137 dayoff
= oadd(dayoff
, eitol(i
));
1139 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1140 error(_("invalid month name"));
1143 month
= lp
->l_value
;
1145 while (j
!= month
) {
1146 i
= len_months
[isleap(year
)][j
];
1147 dayoff
= oadd(dayoff
, eitol(i
));
1150 cp
= fields
[LP_DAY
];
1151 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1152 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1153 error(_("invalid day of month"));
1156 dayoff
= oadd(dayoff
, eitol(day
- 1));
1157 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1158 error(_("time before zero"));
1161 t
= (time_t) dayoff
* SECSPERDAY
;
1163 ** Cheap overflow check.
1165 if (t
/ SECSPERDAY
!= dayoff
) {
1166 error(_("time overflow"));
1169 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1170 cp
= fields
[LP_CORR
];
1172 register int positive
;
1175 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1178 } else if (strcmp(cp
, "--") == 0) {
1181 } else if (strcmp(cp
, "+") == 0) {
1184 } else if (strcmp(cp
, "++") == 0) {
1188 error(_("illegal CORRECTION field on Leap line"));
1191 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1192 error(_("illegal Rolling/Stationary field on Leap line"));
1195 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1200 inlink(fields
, nfields
)
1201 register char ** const fields
;
1206 if (nfields
!= LINK_FIELDS
) {
1207 error(_("wrong number of fields on Link line"));
1210 if (*fields
[LF_FROM
] == '\0') {
1211 error(_("blank FROM field on Link line"));
1214 if (*fields
[LF_TO
] == '\0') {
1215 error(_("blank TO field on Link line"));
1218 l
.l_filename
= filename
;
1219 l
.l_linenum
= linenum
;
1220 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1221 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1222 links
= (struct link
*) (void *) erealloc((char *) links
,
1223 (int) ((nlinks
+ 1) * sizeof *links
));
1224 links
[nlinks
++] = l
;
1228 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1229 register struct rule
* const rp
;
1230 const char * const loyearp
;
1231 const char * const hiyearp
;
1232 const char * const typep
;
1233 const char * const monthp
;
1234 const char * const dayp
;
1235 const char * const timep
;
1237 register const struct lookup
* lp
;
1238 register const char * cp
;
1242 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1243 error(_("invalid month name"));
1246 rp
->r_month
= lp
->l_value
;
1247 rp
->r_todisstd
= FALSE
;
1248 rp
->r_todisgmt
= FALSE
;
1249 dp
= ecpyalloc(timep
);
1251 ep
= dp
+ strlen(dp
) - 1;
1252 switch (lowerit(*ep
)) {
1253 case 's': /* Standard */
1254 rp
->r_todisstd
= TRUE
;
1255 rp
->r_todisgmt
= FALSE
;
1258 case 'w': /* Wall */
1259 rp
->r_todisstd
= FALSE
;
1260 rp
->r_todisgmt
= FALSE
;
1263 case 'g': /* Greenwich */
1264 case 'u': /* Universal */
1265 case 'z': /* Zulu */
1266 rp
->r_todisstd
= TRUE
;
1267 rp
->r_todisgmt
= TRUE
;
1272 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1278 lp
= byword(cp
, begin_years
);
1279 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1281 rp
->r_loyear
= INT_MIN
;
1284 rp
->r_loyear
= INT_MAX
;
1286 default: /* "cannot happen" */
1287 (void) fprintf(stderr
,
1288 _("%s: panic: Invalid l_value %d\n"),
1289 progname
, lp
->l_value
);
1290 (void) exit(EXIT_FAILURE
);
1291 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1292 error(_("invalid starting year"));
1295 if (rp
->r_loyear
< min_year_representable
)
1296 warning(_("starting year too low to be represented"));
1297 else if (rp
->r_loyear
> max_year_representable
)
1298 warning(_("starting year too high to be represented"));
1301 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1303 rp
->r_hiyear
= INT_MIN
;
1306 rp
->r_hiyear
= INT_MAX
;
1309 rp
->r_hiyear
= rp
->r_loyear
;
1311 default: /* "cannot happen" */
1312 (void) fprintf(stderr
,
1313 _("%s: panic: Invalid l_value %d\n"),
1314 progname
, lp
->l_value
);
1315 (void) exit(EXIT_FAILURE
);
1316 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1317 error(_("invalid ending year"));
1320 if (rp
->r_loyear
< min_year_representable
)
1321 warning(_("starting year too low to be represented"));
1322 else if (rp
->r_loyear
> max_year_representable
)
1323 warning(_("starting year too high to be represented"));
1325 if (rp
->r_loyear
> rp
->r_hiyear
) {
1326 error(_("starting year greater than ending year"));
1330 rp
->r_yrtype
= NULL
;
1332 if (rp
->r_loyear
== rp
->r_hiyear
) {
1333 error(_("typed single year"));
1336 rp
->r_yrtype
= ecpyalloc(typep
);
1338 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1339 min_year
= rp
->r_loyear
;
1342 ** Accept things such as:
1348 dp
= ecpyalloc(dayp
);
1349 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1350 rp
->r_dycode
= DC_DOWLEQ
;
1351 rp
->r_wday
= lp
->l_value
;
1352 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1354 if ((ep
= strchr(dp
, '<')) != 0)
1355 rp
->r_dycode
= DC_DOWLEQ
;
1356 else if ((ep
= strchr(dp
, '>')) != 0)
1357 rp
->r_dycode
= DC_DOWGEQ
;
1360 rp
->r_dycode
= DC_DOM
;
1362 if (rp
->r_dycode
!= DC_DOM
) {
1365 error(_("invalid day of month"));
1369 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1370 error(_("invalid weekday name"));
1374 rp
->r_wday
= lp
->l_value
;
1376 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1377 rp
->r_dayofmonth
<= 0 ||
1378 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1379 error(_("invalid day of month"));
1393 register long shift
;
1395 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1396 buf
[i
] = val
>> shift
;
1407 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1415 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1417 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1424 const char * const name
;
1428 static char * fullname
;
1429 static struct tzhead tzh
;
1430 time_t ats
[TZ_MAX_TIMES
];
1431 unsigned char types
[TZ_MAX_TIMES
];
1437 (void) qsort((void *) attypes
, (size_t) timecnt
,
1438 (size_t) sizeof *attypes
, atcomp
);
1448 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1451 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1452 ++fromi
; /* handled by default rule */
1453 for ( ; fromi
< timecnt
; ++fromi
) {
1455 && ((attypes
[fromi
].at
1456 + gmtoffs
[attypes
[toi
- 1].type
])
1457 <= (attypes
[toi
- 1].at
1458 + gmtoffs
[toi
== 1 ? 0
1459 : attypes
[toi
- 2].type
]))) {
1460 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1464 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1465 attypes
[toi
++] = attypes
[fromi
];
1472 for (i
= 0; i
< timecnt
; ++i
) {
1473 ats
[i
] = attypes
[i
].at
;
1474 types
[i
] = attypes
[i
].type
;
1476 fullname
= erealloc(fullname
,
1477 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1478 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1480 ** Remove old file, if any, to snap links.
1482 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1483 const char *e
= strerror(errno
);
1485 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1486 progname
, fullname
, e
);
1487 (void) exit(EXIT_FAILURE
);
1489 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1490 if (mkdirs(fullname
) != 0)
1491 (void) exit(EXIT_FAILURE
);
1492 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1493 const char *e
= strerror(errno
);
1495 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1496 progname
, fullname
, e
);
1497 (void) exit(EXIT_FAILURE
);
1500 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1501 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1502 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1503 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1504 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1505 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1506 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1507 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1517 for (i
= 0; i
< timecnt
; ++i
) {
1520 if (ats
[i
] >= trans
[j
]) {
1521 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1524 puttzcode((long) ats
[i
], fp
);
1527 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1528 (size_t) timecnt
, fp
);
1529 for (i
= 0; i
< typecnt
; ++i
) {
1530 puttzcode((long) gmtoffs
[i
], fp
);
1531 (void) putc(isdsts
[i
], fp
);
1532 (void) putc(abbrinds
[i
], fp
);
1535 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1536 (size_t) charcnt
, fp
);
1537 for (i
= 0; i
< leapcnt
; ++i
) {
1539 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1542 if (++j
>= typecnt
) {
1548 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1552 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1553 } else puttzcode((long) trans
[i
], fp
);
1554 puttzcode((long) corr
[i
], fp
);
1556 for (i
= 0; i
< typecnt
; ++i
)
1557 (void) putc(ttisstds
[i
], fp
);
1558 for (i
= 0; i
< typecnt
; ++i
)
1559 (void) putc(ttisgmts
[i
], fp
);
1560 if (ferror(fp
) || fclose(fp
)) {
1561 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1562 progname
, fullname
);
1563 (void) exit(EXIT_FAILURE
);
1568 doabbr(abbr
, format
, letters
, isdst
)
1570 const char * const format
;
1571 const char * const letters
;
1574 if (strchr(format
, '/') == NULL
) {
1575 if (letters
== NULL
)
1576 (void) strcpy(abbr
, format
);
1577 else (void) sprintf(abbr
, format
, letters
);
1579 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1581 (void) strcpy(abbr
, format
);
1582 *strchr(abbr
, '/') = '\0';
1587 outzone(zpfirst
, zonecount
)
1588 const struct zone
* const zpfirst
;
1589 const int zonecount
;
1591 register const struct zone
* zp
;
1592 register struct rule
* rp
;
1594 register int usestart
, useuntil
;
1595 register time_t starttime
, untiltime
;
1596 register long gmtoff
;
1597 register long stdoff
;
1599 register long startoff
;
1600 register int startttisstd
;
1601 register int startttisgmt
;
1603 char startbuf
[BUFSIZ
];
1605 INITIALIZE(untiltime
);
1606 INITIALIZE(starttime
);
1608 ** Now. . .finally. . .generate some useful data!
1614 ** A guess that may well be corrected later.
1618 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1619 ** for noting the need to unconditionally initialize startttisstd.
1621 startttisstd
= FALSE
;
1622 startttisgmt
= FALSE
;
1623 for (i
= 0; i
< zonecount
; ++i
) {
1625 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1626 useuntil
= i
< (zonecount
- 1);
1627 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1629 gmtoff
= zp
->z_gmtoff
;
1630 eat(zp
->z_filename
, zp
->z_linenum
);
1632 startoff
= zp
->z_gmtoff
;
1633 if (zp
->z_nrules
== 0) {
1634 stdoff
= zp
->z_stdoff
;
1635 doabbr(startbuf
, zp
->z_format
,
1636 (char *) NULL
, stdoff
!= 0);
1637 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1638 startbuf
, stdoff
!= 0, startttisstd
,
1641 addtt(starttime
, type
);
1644 else if (stdoff
!= 0)
1645 addtt(min_time
, type
);
1646 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1647 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1650 ** Mark which rules to do in the current year.
1651 ** For those to do, calculate rpytime(rp, year);
1653 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1654 rp
= &zp
->z_rules
[j
];
1655 eats(zp
->z_filename
, zp
->z_linenum
,
1656 rp
->r_filename
, rp
->r_linenum
);
1657 rp
->r_todo
= year
>= rp
->r_loyear
&&
1658 year
<= rp
->r_hiyear
&&
1659 yearistype(year
, rp
->r_yrtype
);
1661 rp
->r_temp
= rpytime(rp
, year
);
1665 register time_t jtime
, ktime
;
1666 register long offset
;
1672 ** Turn untiltime into UTC
1673 ** assuming the current gmtoff and
1676 untiltime
= zp
->z_untiltime
;
1677 if (!zp
->z_untilrule
.r_todisgmt
)
1678 untiltime
= tadd(untiltime
,
1680 if (!zp
->z_untilrule
.r_todisstd
)
1681 untiltime
= tadd(untiltime
,
1685 ** Find the rule (of those to do, if any)
1686 ** that takes effect earliest in the year.
1689 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1690 rp
= &zp
->z_rules
[j
];
1693 eats(zp
->z_filename
, zp
->z_linenum
,
1694 rp
->r_filename
, rp
->r_linenum
);
1695 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1696 if (!rp
->r_todisstd
)
1697 offset
= oadd(offset
, stdoff
);
1699 if (jtime
== min_time
||
1702 jtime
= tadd(jtime
, -offset
);
1703 if (k
< 0 || jtime
< ktime
) {
1709 break; /* go on to next year */
1710 rp
= &zp
->z_rules
[k
];
1712 if (useuntil
&& ktime
>= untiltime
)
1714 stdoff
= rp
->r_stdoff
;
1715 if (usestart
&& ktime
== starttime
)
1718 if (ktime
< starttime
) {
1719 startoff
= oadd(zp
->z_gmtoff
,
1721 doabbr(startbuf
, zp
->z_format
,
1726 if (*startbuf
== '\0' &&
1727 startoff
== oadd(zp
->z_gmtoff
,
1729 doabbr(startbuf
, zp
->z_format
,
1734 eats(zp
->z_filename
, zp
->z_linenum
,
1735 rp
->r_filename
, rp
->r_linenum
);
1736 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1738 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1739 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1740 rp
->r_todisstd
, rp
->r_todisgmt
);
1745 if (*startbuf
== '\0' &&
1746 zp
->z_format
!= NULL
&&
1747 strchr(zp
->z_format
, '%') == NULL
&&
1748 strchr(zp
->z_format
, '/') == NULL
)
1749 (void) strcpy(startbuf
, zp
->z_format
);
1750 eat(zp
->z_filename
, zp
->z_linenum
);
1751 if (*startbuf
== '\0')
1752 error(_("can't determine time zone abbreviation to use just after until time"));
1753 else addtt(starttime
,
1754 addtype(startoff
, startbuf
,
1755 startoff
!= zp
->z_gmtoff
,
1760 ** Now we may get to set starttime for the next zone line.
1763 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1764 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1765 starttime
= zp
->z_untiltime
;
1767 starttime
= tadd(starttime
, -stdoff
);
1769 starttime
= tadd(starttime
, -gmtoff
);
1772 writezone(zpfirst
->z_name
);
1776 addtt(starttime
, type
)
1777 const time_t starttime
;
1780 if (starttime
<= min_time
||
1781 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1782 gmtoffs
[0] = gmtoffs
[type
];
1783 isdsts
[0] = isdsts
[type
];
1784 ttisstds
[0] = ttisstds
[type
];
1785 ttisgmts
[0] = ttisgmts
[type
];
1786 if (abbrinds
[type
] != 0)
1787 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1789 charcnt
= strlen(chars
) + 1;
1794 if (timecnt
>= TZ_MAX_TIMES
) {
1795 error(_("too many transitions?!"));
1796 (void) exit(EXIT_FAILURE
);
1798 attypes
[timecnt
].at
= starttime
;
1799 attypes
[timecnt
].type
= type
;
1804 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1806 const char * const abbr
;
1813 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1814 error(_("internal error - addtype called with bad isdst"));
1815 (void) exit(EXIT_FAILURE
);
1817 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1818 error(_("internal error - addtype called with bad ttisstd"));
1819 (void) exit(EXIT_FAILURE
);
1821 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1822 error(_("internal error - addtype called with bad ttisgmt"));
1823 (void) exit(EXIT_FAILURE
);
1826 ** See if there's already an entry for this zone type.
1827 ** If so, just return its index.
1829 for (i
= 0; i
< typecnt
; ++i
) {
1830 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1831 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1832 ttisstd
== ttisstds
[i
] &&
1833 ttisgmt
== ttisgmts
[i
])
1837 ** There isn't one; add a new one, unless there are already too
1840 if (typecnt
>= TZ_MAX_TYPES
) {
1841 error(_("too many local time types"));
1842 (void) exit(EXIT_FAILURE
);
1844 gmtoffs
[i
] = gmtoff
;
1846 ttisstds
[i
] = ttisstd
;
1847 ttisgmts
[i
] = ttisgmt
;
1849 for (j
= 0; j
< charcnt
; ++j
)
1850 if (strcmp(&chars
[j
], abbr
) == 0)
1860 leapadd(t
, positive
, rolling
, count
)
1868 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1869 error(_("too many leap seconds"));
1870 (void) exit(EXIT_FAILURE
);
1872 for (i
= 0; i
< leapcnt
; ++i
)
1873 if (t
<= trans
[i
]) {
1874 if (t
== trans
[i
]) {
1875 error(_("repeated leap second moment"));
1876 (void) exit(EXIT_FAILURE
);
1881 for (j
= leapcnt
; j
> i
; --j
) {
1882 trans
[j
] = trans
[j
- 1];
1883 corr
[j
] = corr
[j
- 1];
1884 roll
[j
] = roll
[j
- 1];
1887 corr
[i
] = positive
? 1L : eitol(-count
);
1890 } while (positive
&& --count
!= 0);
1897 register long last
= 0;
1900 ** propagate leap seconds forward
1902 for (i
= 0; i
< leapcnt
; ++i
) {
1903 trans
[i
] = tadd(trans
[i
], last
);
1904 last
= corr
[i
] += last
;
1909 yearistype(year
, type
)
1911 const char * const type
;
1916 if (type
== NULL
|| *type
== '\0')
1918 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1919 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1920 result
= system(buf
);
1921 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1927 error(_("Wild result from command execution"));
1928 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1929 progname
, buf
, result
);
1931 (void) exit(EXIT_FAILURE
);
1938 a
= (unsigned char) a
;
1939 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1943 ciequal(ap
, bp
) /* case-insensitive equality */
1944 register const char * ap
;
1945 register const char * bp
;
1947 while (lowerit(*ap
) == lowerit(*bp
++))
1955 register const char * abbr
;
1956 register const char * word
;
1958 if (lowerit(*abbr
) != lowerit(*word
))
1961 while (*++abbr
!= '\0')
1965 } while (lowerit(*word
++) != lowerit(*abbr
));
1969 static const struct lookup
*
1971 register const char * const word
;
1972 register const struct lookup
* const table
;
1974 register const struct lookup
* foundlp
;
1975 register const struct lookup
* lp
;
1977 if (word
== NULL
|| table
== NULL
)
1980 ** Look for exact match.
1982 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1983 if (ciequal(word
, lp
->l_word
))
1986 ** Look for inexact match.
1989 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1990 if (itsabbr(word
, lp
->l_word
)) {
1991 if (foundlp
== NULL
)
1993 else return NULL
; /* multiple inexact matches */
2003 register char ** array
;
2008 array
= (char **) (void *)
2009 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2012 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2014 if (*cp
== '\0' || *cp
== '#')
2016 array
[nsubs
++] = dp
= cp
;
2018 if ((*dp
= *cp
++) != '"')
2020 else while ((*dp
= *cp
++) != '"')
2023 else error(_("Odd number of quotation marks"));
2024 } while (*cp
!= '\0' && *cp
!= '#' &&
2025 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2026 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2030 array
[nsubs
] = NULL
;
2042 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2043 error(_("time overflow"));
2044 (void) exit(EXIT_FAILURE
);
2056 if (t1
== max_time
&& t2
> 0)
2058 if (t1
== min_time
&& t2
< 0)
2061 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2062 error(_("time overflow"));
2063 (void) exit(EXIT_FAILURE
);
2069 ** Given a rule, and a year, compute the date - in seconds since January 1,
2070 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2074 rpytime(rp
, wantedy
)
2075 register const struct rule
* const rp
;
2076 register const int wantedy
;
2078 register int y
, m
, i
;
2079 register long dayoff
; /* with a nod to Margaret O. */
2082 if (wantedy
== INT_MIN
)
2084 if (wantedy
== INT_MAX
)
2089 while (wantedy
!= y
) {
2091 i
= len_years
[isleap(y
)];
2095 i
= -len_years
[isleap(y
)];
2097 dayoff
= oadd(dayoff
, eitol(i
));
2099 while (m
!= rp
->r_month
) {
2100 i
= len_months
[isleap(y
)][m
];
2101 dayoff
= oadd(dayoff
, eitol(i
));
2104 i
= rp
->r_dayofmonth
;
2105 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2106 if (rp
->r_dycode
== DC_DOWLEQ
)
2109 error(_("use of 2/29 in non leap-year"));
2110 (void) exit(EXIT_FAILURE
);
2114 dayoff
= oadd(dayoff
, eitol(i
));
2115 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2118 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2119 wday
= eitol(EPOCH_WDAY
);
2121 ** Don't trust mod of negative numbers.
2124 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2126 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2128 wday
+= LDAYSPERWEEK
;
2130 while (wday
!= eitol(rp
->r_wday
))
2131 if (rp
->r_dycode
== DC_DOWGEQ
) {
2132 dayoff
= oadd(dayoff
, (long) 1);
2133 if (++wday
>= LDAYSPERWEEK
)
2137 dayoff
= oadd(dayoff
, (long) -1);
2139 wday
= LDAYSPERWEEK
- 1;
2142 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2143 error(_("no day in month matches rule"));
2144 (void) exit(EXIT_FAILURE
);
2147 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2149 t
= (time_t) dayoff
* SECSPERDAY
;
2151 ** Cheap overflow check.
2153 if (t
/ SECSPERDAY
!= dayoff
)
2154 return (dayoff
> 0) ? max_time
: min_time
;
2155 return tadd(t
, rp
->r_tod
);
2160 const char * const string
;
2164 i
= strlen(string
) + 1;
2165 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2166 error(_("too many, or too long, time zone abbreviations"));
2167 (void) exit(EXIT_FAILURE
);
2169 (void) strcpy(&chars
[charcnt
], string
);
2170 charcnt
+= eitol(i
);
2175 char * const argname
;
2177 register char * name
;
2180 if (argname
== NULL
|| *argname
== '\0')
2182 cp
= name
= ecpyalloc(argname
);
2183 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2187 ** DOS drive specifier?
2189 if (isalpha((unsigned char) name
[0]) &&
2190 name
[1] == ':' && name
[2] == '\0') {
2194 #endif /* !defined unix */
2195 if (!itsdir(name
)) {
2197 ** It doesn't seem to exist, so we try to create it.
2198 ** Creation may fail because of the directory being
2199 ** created by some other multiprocessor, so we get
2200 ** to do extra checking.
2202 if (mkdir(name
, S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
) != 0) {
2203 const char *e
= strerror(errno
);
2205 if (errno
!= EEXIST
|| !itsdir(name
)) {
2206 (void) fprintf(stderr
,
2207 _("%s: Can't create directory %s: %s\n"),
2227 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2228 (void) fprintf(stderr
,
2229 _("%s: %d did not sign extend correctly\n"),
2231 (void) exit(EXIT_FAILURE
);
2237 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.