1 static char elsieid
[] = "@(#)zic.c 7.128";
4 ** Regardless of the type of time_t, we do our work using this type.
13 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
14 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
15 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
21 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
23 #define MKDIR_UMASK 0755
27 ** On some ancient hosts, predicates like `isspace(C)' are defined
28 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
29 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
30 ** Neither the C Standard nor Posix require that `isascii' exist.
31 ** For portability, we check both ancient and modern requirements.
32 ** If isascii is not defined, the isascii check succeeds trivially.
40 const char * r_filename
;
44 int r_loyear
; /* for example, 1986 */
45 int r_hiyear
; /* for example, 1986 */
46 const char * r_yrtype
;
48 int r_month
; /* 0..11 */
50 int r_dycode
; /* see below */
54 long r_tod
; /* time from midnight */
55 int r_todisstd
; /* above is standard time if TRUE */
56 /* or wall clock time if FALSE */
57 int r_todisgmt
; /* above is GMT if TRUE */
58 /* or local time if FALSE */
59 long r_stdoff
; /* offset from standard time */
60 const char * r_abbrvar
; /* variable part of abbreviation */
62 int r_todo
; /* a rule to do (used in outzone) */
63 zic_t r_temp
; /* used in outzone */
67 ** r_dycode r_dayofmonth r_wday
70 #define DC_DOM 0 /* 1..31 */ /* unused */
71 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
72 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
75 const char * z_filename
;
81 const char * z_format
;
85 struct rule
* z_rules
;
88 struct rule z_untilrule
;
92 extern int getopt
P((int argc
, char * const argv
[],
93 const char * options
));
94 extern int link
P((const char * fromname
, const char * toname
));
98 static void addtt
P((zic_t starttime
, int type
));
99 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
100 int ttisstd
, int ttisgmt
));
101 static void leapadd
P((zic_t t
, int positive
, int rolling
, int count
));
102 static void adjleap
P((void));
103 static void associate
P((void));
104 static int ciequal
P((const char * ap
, const char * bp
));
105 static void convert
P((long val
, char * buf
));
106 static void dolink
P((const char * fromfile
, const char * tofile
));
107 static void doabbr
P((char * abbr
, const char * format
,
108 const char * letters
, int isdst
));
109 static void eat
P((const char * name
, int num
));
110 static void eats
P((const char * name
, int num
,
111 const char * rname
, int rnum
));
112 static long eitol
P((int i
));
113 static void error
P((const char * message
));
114 static char ** getfields
P((char * buf
));
115 static long gethms
P((const char * string
, const char * errstrng
,
117 static void infile
P((const char * filename
));
118 static void inleap
P((char ** fields
, int nfields
));
119 static void inlink
P((char ** fields
, int nfields
));
120 static void inrule
P((char ** fields
, int nfields
));
121 static int inzcont
P((char ** fields
, int nfields
));
122 static int inzone
P((char ** fields
, int nfields
));
123 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
124 static int itsabbr
P((const char * abbr
, const char * word
));
125 static int itsdir
P((const char * name
));
126 static int lowerit
P((int c
));
127 static char * memcheck
P((char * tocheck
));
128 static int mkdirs
P((char * filename
));
129 static void newabbr
P((const char * abbr
));
130 static long oadd
P((long t1
, long t2
));
131 static void outzone
P((const struct zone
* zp
, int ntzones
));
132 static void puttzcode
P((long code
, FILE * fp
));
133 static int rcomp
P((const void * leftp
, const void * rightp
));
134 static zic_t rpytime
P((const struct rule
* rp
, int wantedy
));
135 static void rulesub
P((struct rule
* rp
,
136 const char * loyearp
, const char * hiyearp
,
137 const char * typep
, const char * monthp
,
138 const char * dayp
, const char * timep
));
139 static void setboundaries
P((void));
140 static zic_t tadd
P((zic_t t1
, long t2
));
141 static void usage
P((void));
142 static void writezone
P((const char * name
));
143 static int yearistype
P((int year
, const char * type
));
146 static char * strerror
P((int));
147 #endif /* !HAVE_STRERROR */
151 static const char * filename
;
154 static zic_t max_time
;
156 static int max_year_representable
;
157 static zic_t min_time
;
159 static int min_year_representable
;
161 static const char * rfilename
;
163 static const char * progname
;
177 ** Which fields are which on a Zone line.
185 #define ZF_TILMONTH 6
188 #define ZONE_MINFIELDS 5
189 #define ZONE_MAXFIELDS 9
192 ** Which fields are which on a Zone continuation line.
198 #define ZFC_TILYEAR 3
199 #define ZFC_TILMONTH 4
201 #define ZFC_TILTIME 6
202 #define ZONEC_MINFIELDS 3
203 #define ZONEC_MAXFIELDS 7
206 ** Which files are which on a Rule line.
218 #define RULE_FIELDS 10
221 ** Which fields are which on a Link line.
226 #define LINK_FIELDS 3
229 ** Which fields are which on a Leap line.
238 #define LEAP_FIELDS 7
248 static struct rule
* rules
;
249 static int nrules
; /* number of rules */
251 static struct zone
* zones
;
252 static int nzones
; /* number of zones */
255 const char * l_filename
;
261 static struct link
* links
;
269 static struct lookup
const * byword
P((const char * string
,
270 const struct lookup
* lp
));
272 static struct lookup
const line_codes
[] = {
280 static struct lookup
const mon_names
[] = {
281 { "January", TM_JANUARY
},
282 { "February", TM_FEBRUARY
},
283 { "March", TM_MARCH
},
284 { "April", TM_APRIL
},
288 { "August", TM_AUGUST
},
289 { "September", TM_SEPTEMBER
},
290 { "October", TM_OCTOBER
},
291 { "November", TM_NOVEMBER
},
292 { "December", TM_DECEMBER
},
296 static struct lookup
const wday_names
[] = {
297 { "Sunday", TM_SUNDAY
},
298 { "Monday", TM_MONDAY
},
299 { "Tuesday", TM_TUESDAY
},
300 { "Wednesday", TM_WEDNESDAY
},
301 { "Thursday", TM_THURSDAY
},
302 { "Friday", TM_FRIDAY
},
303 { "Saturday", TM_SATURDAY
},
307 static struct lookup
const lasts
[] = {
308 { "last-Sunday", TM_SUNDAY
},
309 { "last-Monday", TM_MONDAY
},
310 { "last-Tuesday", TM_TUESDAY
},
311 { "last-Wednesday", TM_WEDNESDAY
},
312 { "last-Thursday", TM_THURSDAY
},
313 { "last-Friday", TM_FRIDAY
},
314 { "last-Saturday", TM_SATURDAY
},
318 static struct lookup
const begin_years
[] = {
319 { "minimum", YR_MINIMUM
},
320 { "maximum", YR_MAXIMUM
},
324 static struct lookup
const end_years
[] = {
325 { "minimum", YR_MINIMUM
},
326 { "maximum", YR_MAXIMUM
},
331 static struct lookup
const leap_types
[] = {
333 { "Stationary", FALSE
},
337 static const int len_months
[2][MONSPERYEAR
] = {
338 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
339 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
342 static const int len_years
[2] = {
343 DAYSPERNYEAR
, DAYSPERLYEAR
346 static struct attype
{
349 } attypes
[TZ_MAX_TIMES
];
350 static long gmtoffs
[TZ_MAX_TYPES
];
351 static char isdsts
[TZ_MAX_TYPES
];
352 static unsigned char abbrinds
[TZ_MAX_TYPES
];
353 static char ttisstds
[TZ_MAX_TYPES
];
354 static char ttisgmts
[TZ_MAX_TYPES
];
355 static char chars
[TZ_MAX_CHARS
];
356 static zic_t trans
[TZ_MAX_LEAPS
];
357 static long corr
[TZ_MAX_LEAPS
];
358 static char roll
[TZ_MAX_LEAPS
];
361 ** Memory allocation.
369 const char *e
= strerror(errno
);
371 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
378 #define emalloc(size) memcheck(imalloc(size))
379 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
380 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
381 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
392 extern char * sys_errlist
[];
395 return (errnum
> 0 && errnum
<= sys_nerr
) ?
396 sys_errlist
[errnum
] : _("Unknown system error");
398 #endif /* !HAVE_STRERROR */
401 eats(name
, num
, rname
, rnum
)
402 const char * const name
;
404 const char * const rname
;
415 const char * const name
;
418 eats(name
, num
, (char *) NULL
, -1);
423 const char * const string
;
426 ** Match the format of "cc" to allow sh users to
427 ** zic ... 2>&1 | error -t "*" -v
430 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
431 filename
, linenum
, string
);
432 if (rfilename
!= NULL
)
433 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
434 rfilename
, rlinenum
);
435 (void) fprintf(stderr
, "\n");
440 warning(const char * const string
)
444 cp
= ecpyalloc(_("warning: "));
445 cp
= ecatalloc(cp
, string
);
454 (void) fprintf(stderr
, _("%s: usage is %s \
455 [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
456 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
461 static const char * psxrules
;
462 static const char * lcltime
;
463 static const char * directory
;
464 static const char * leapsec
;
465 static const char * yitcommand
;
466 static int sflag
= FALSE
;
469 main( int argc
, char * argv
[])
476 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
477 #endif /* defined unix */
479 (void) setlocale(LC_ALL
, "");
481 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
482 #endif /* defined TEXTDOMAINDIR */
483 (void) textdomain(TZ_DOMAIN
);
484 #endif /* HAVE_GETTEXT */
486 for (i
= 1; i
< argc
; ++i
)
487 if (strcmp(argv
[i
], "--version") == 0) {
488 (void) printf("%s\n", elsieid
);
491 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
496 if (directory
== NULL
)
499 (void) fprintf(stderr
,
500 _("%s: More than one -d option specified\n"),
509 (void) fprintf(stderr
,
510 _("%s: More than one -l option specified\n"),
516 if (psxrules
== NULL
)
519 (void) fprintf(stderr
,
520 _("%s: More than one -p option specified\n"),
526 if (yitcommand
== NULL
)
529 (void) fprintf(stderr
,
530 _("%s: More than one -y option specified\n"),
539 (void) fprintf(stderr
,
540 _("%s: More than one -L option specified\n"),
552 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
553 usage(); /* usage message by request */
554 if (directory
== NULL
)
556 if (yitcommand
== NULL
)
557 yitcommand
= "yearistype";
561 if (optind
< argc
&& leapsec
!= NULL
) {
566 for (i
= optind
; i
< argc
; ++i
)
571 for (i
= 0; i
< nzones
; i
= j
) {
573 ** Find the next non-continuation zone entry.
575 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
577 outzone(&zones
[i
], j
- i
);
582 for (i
= 0; i
< nlinks
; ++i
) {
583 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
584 dolink(links
[i
].l_from
, links
[i
].l_to
);
586 for (j
= 0; j
< nlinks
; ++j
)
587 if (strcmp(links
[i
].l_to
,
588 links
[j
].l_from
) == 0)
589 warning(_("link to link"));
591 if (lcltime
!= NULL
) {
592 eat("command line", 1);
593 dolink(lcltime
, TZDEFAULT
);
595 if (psxrules
!= NULL
) {
596 eat("command line", 1);
597 dolink(psxrules
, TZDEFRULES
);
599 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
603 dolink(fromfile
, tofile
)
604 const char * const fromfile
;
605 const char * const tofile
;
607 register char * fromname
;
608 register char * toname
;
610 if (fromfile
[0] == '/')
611 fromname
= ecpyalloc(fromfile
);
613 fromname
= ecpyalloc(directory
);
614 fromname
= ecatalloc(fromname
, "/");
615 fromname
= ecatalloc(fromname
, fromfile
);
617 if (tofile
[0] == '/')
618 toname
= ecpyalloc(tofile
);
620 toname
= ecpyalloc(directory
);
621 toname
= ecatalloc(toname
, "/");
622 toname
= ecatalloc(toname
, tofile
);
625 ** We get to be careful here since
626 ** there's a fair chance of root running us.
629 (void) remove(toname
);
630 if (link(fromname
, toname
) != 0) {
633 if (mkdirs(toname
) != 0)
636 result
= link(fromname
, toname
);
639 access(fromname
, F_OK
) == 0 &&
641 const char *s
= tofile
;
642 register char * symlinkcontents
= NULL
;
644 while ((s
= strchr(s
+1, '/')) != NULL
)
646 ecatalloc(symlinkcontents
,
649 ecatalloc(symlinkcontents
,
651 result
= symlink(symlinkcontents
,
654 warning(_("hard link failed, symbolic link used"));
655 ifree(symlinkcontents
);
657 #endif /* HAVE_SYMLINK */
659 const char *e
= strerror(errno
);
661 (void) fprintf(stderr
,
662 _("%s: Can't link from %s to %s: %s\n"),
663 progname
, fromname
, toname
, e
);
672 #define INT_MAX ((int) (((unsigned)~0)>>1))
673 #endif /* !defined INT_MAX */
676 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
677 #endif /* !defined INT_MIN */
680 ** The tz file format currently allows at most 32-bit quantities.
681 ** This restriction should be removed before signed 32-bit values
682 ** wrap around in 2038, but unfortunately this will require a
683 ** change to the tz file format.
686 #define MAX_BITS_IN_FILE 32
687 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
688 TYPE_BIT(zic_t) : MAX_BITS_IN_FILE)
691 setboundaries
P((void))
695 if (TYPE_SIGNED(zic_t
)) {
697 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
699 max_time
= -(min_time
+ 1);
704 max_time
= 2 - sflag
;
705 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
712 t
= (time_t) min_time
;
713 min_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
714 t
= (time_t) max_time
;
715 max_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
717 min_year_representable
= min_year
;
718 max_year_representable
= max_year
;
723 const char * const name
;
725 register char * myname
;
728 myname
= ecpyalloc(name
);
729 myname
= ecatalloc(myname
, "/.");
730 accres
= access(myname
, F_OK
);
736 ** Associate sets of rules with zones.
740 ** Sort by rule name.
748 return strcmp(((const struct rule
*) cp1
)->r_name
,
749 ((const struct rule
*) cp2
)->r_name
);
755 register struct zone
* zp
;
756 register struct rule
* rp
;
757 register int base
, out
;
761 (void) qsort((void *) rules
, (size_t) nrules
,
762 (size_t) sizeof *rules
, rcomp
);
763 for (i
= 0; i
< nrules
- 1; ++i
) {
764 if (strcmp(rules
[i
].r_name
,
765 rules
[i
+ 1].r_name
) != 0)
767 if (strcmp(rules
[i
].r_filename
,
768 rules
[i
+ 1].r_filename
) == 0)
770 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
771 warning(_("same rule name in multiple files"));
772 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
773 warning(_("same rule name in multiple files"));
774 for (j
= i
+ 2; j
< nrules
; ++j
) {
775 if (strcmp(rules
[i
].r_name
,
776 rules
[j
].r_name
) != 0)
778 if (strcmp(rules
[i
].r_filename
,
779 rules
[j
].r_filename
) == 0)
781 if (strcmp(rules
[i
+ 1].r_filename
,
782 rules
[j
].r_filename
) == 0)
789 for (i
= 0; i
< nzones
; ++i
) {
794 for (base
= 0; base
< nrules
; base
= out
) {
796 for (out
= base
+ 1; out
< nrules
; ++out
)
797 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
799 for (i
= 0; i
< nzones
; ++i
) {
801 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
804 zp
->z_nrules
= out
- base
;
807 for (i
= 0; i
< nzones
; ++i
) {
809 if (zp
->z_nrules
== 0) {
811 ** Maybe we have a local standard time offset.
813 eat(zp
->z_filename
, zp
->z_linenum
);
814 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
817 ** Note, though, that if there's no rule,
818 ** a '%s' in the format is a bad thing.
820 if (strchr(zp
->z_format
, '%') != 0)
821 error(_("%s in ruleless zone"));
833 register char ** fields
;
835 register const struct lookup
* lp
;
836 register int nfields
;
837 register int wantcont
;
841 if (strcmp(name
, "-") == 0) {
842 name
= _("standard input");
844 } else if ((fp
= fopen(name
, "r")) == NULL
) {
845 const char *e
= strerror(errno
);
847 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
852 for (num
= 1; ; ++num
) {
854 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
856 cp
= strchr(buf
, '\n');
858 error(_("line too long"));
862 fields
= getfields(buf
);
864 while (fields
[nfields
] != NULL
) {
867 if (strcmp(fields
[nfields
], "-") == 0)
868 fields
[nfields
] = &nada
;
873 } else if (wantcont
) {
874 wantcont
= inzcont(fields
, nfields
);
876 lp
= byword(fields
[0], line_codes
);
878 error(_("input line of unknown type"));
879 else switch ((int) (lp
->l_value
)) {
881 inrule(fields
, nfields
);
885 wantcont
= inzone(fields
, nfields
);
888 inlink(fields
, nfields
);
893 (void) fprintf(stderr
,
894 _("%s: Leap line in non leap seconds file %s\n"),
896 else inleap(fields
, nfields
);
899 default: /* "cannot happen" */
900 (void) fprintf(stderr
,
901 _("%s: panic: Invalid l_value %d\n"),
902 progname
, lp
->l_value
);
906 ifree((char *) fields
);
909 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
913 if (fp
!= stdin
&& fclose(fp
)) {
914 const char *e
= strerror(errno
);
916 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
917 progname
, filename
, e
);
921 error(_("expected continuation line not found"));
925 ** Convert a string of one of the forms
926 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
927 ** into a number of seconds.
928 ** A null string maps to zero.
929 ** Call error with errstring and return zero on errors.
933 gethms(string
, errstring
, signable
)
935 const char * const errstring
;
938 int hh
, mm
, ss
, sign
;
940 if (string
== NULL
|| *string
== '\0')
944 else if (*string
== '-') {
948 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
950 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
952 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
953 &hh
, &mm
, &ss
) != 3) {
957 if ((hh
< 0 || hh
>= HOURSPERDAY
||
958 mm
< 0 || mm
>= MINSPERHOUR
||
959 ss
< 0 || ss
> SECSPERMIN
) &&
960 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
964 if (noise
&& hh
== HOURSPERDAY
)
965 warning(_("24:00 not handled by pre-1998 versions of zic"));
967 (eitol(hh
* MINSPERHOUR
+ mm
) *
968 eitol(SECSPERMIN
) + eitol(ss
));
972 inrule(fields
, nfields
)
973 register char ** const fields
;
976 static struct rule r
;
978 if (nfields
!= RULE_FIELDS
) {
979 error(_("wrong number of fields on Rule line"));
982 if (*fields
[RF_NAME
] == '\0') {
983 error(_("nameless rule"));
986 r
.r_filename
= filename
;
987 r
.r_linenum
= linenum
;
988 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
989 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
990 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
991 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
992 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
993 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
994 (int) ((nrules
+ 1) * sizeof *rules
));
999 inzone(fields
, nfields
)
1000 register char ** const fields
;
1006 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1007 error(_("wrong number of fields on Zone line"));
1010 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1011 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
1013 _("\"Zone %s\" line and -l option are mutually exclusive"),
1018 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1019 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1021 _("\"Zone %s\" line and -p option are mutually exclusive"),
1026 for (i
= 0; i
< nzones
; ++i
)
1027 if (zones
[i
].z_name
!= NULL
&&
1028 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1029 buf
= erealloc(buf
, (int) (132 +
1030 strlen(fields
[ZF_NAME
]) +
1031 strlen(zones
[i
].z_filename
)));
1033 _("duplicate zone name %s (file \"%s\", line %d)"),
1035 zones
[i
].z_filename
,
1036 zones
[i
].z_linenum
);
1040 return inzsub(fields
, nfields
, FALSE
);
1044 inzcont(fields
, nfields
)
1045 register char ** const fields
;
1048 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1049 error(_("wrong number of fields on Zone continuation line"));
1052 return inzsub(fields
, nfields
, TRUE
);
1056 inzsub(fields
, nfields
, iscont
)
1057 register char ** const fields
;
1062 static struct zone z
;
1063 register int i_gmtoff
, i_rule
, i_format
;
1064 register int i_untilyear
, i_untilmonth
;
1065 register int i_untilday
, i_untiltime
;
1066 register int hasuntil
;
1069 i_gmtoff
= ZFC_GMTOFF
;
1071 i_format
= ZFC_FORMAT
;
1072 i_untilyear
= ZFC_TILYEAR
;
1073 i_untilmonth
= ZFC_TILMONTH
;
1074 i_untilday
= ZFC_TILDAY
;
1075 i_untiltime
= ZFC_TILTIME
;
1078 i_gmtoff
= ZF_GMTOFF
;
1080 i_format
= ZF_FORMAT
;
1081 i_untilyear
= ZF_TILYEAR
;
1082 i_untilmonth
= ZF_TILMONTH
;
1083 i_untilday
= ZF_TILDAY
;
1084 i_untiltime
= ZF_TILTIME
;
1085 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1087 z
.z_filename
= filename
;
1088 z
.z_linenum
= linenum
;
1089 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1090 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1091 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1092 error(_("invalid abbreviation format"));
1096 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1097 z
.z_format
= ecpyalloc(fields
[i_format
]);
1098 hasuntil
= nfields
> i_untilyear
;
1100 z
.z_untilrule
.r_filename
= filename
;
1101 z
.z_untilrule
.r_linenum
= linenum
;
1102 rulesub(&z
.z_untilrule
,
1103 fields
[i_untilyear
],
1106 (nfields
> i_untilmonth
) ?
1107 fields
[i_untilmonth
] : "Jan",
1108 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1109 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1110 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1111 z
.z_untilrule
.r_loyear
);
1112 if (iscont
&& nzones
> 0 &&
1113 z
.z_untiltime
> min_time
&&
1114 z
.z_untiltime
< max_time
&&
1115 zones
[nzones
- 1].z_untiltime
> min_time
&&
1116 zones
[nzones
- 1].z_untiltime
< max_time
&&
1117 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1119 "Zone continuation line end time is not after end time of previous line"
1124 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1125 (int) ((nzones
+ 1) * sizeof *zones
));
1126 zones
[nzones
++] = z
;
1128 ** If there was an UNTIL field on this line,
1129 ** there's more information about the zone on the next line.
1135 inleap(fields
, nfields
)
1136 register char ** const fields
;
1139 register const char * cp
;
1140 register const struct lookup
* lp
;
1142 int year
, month
, day
;
1146 if (nfields
!= LEAP_FIELDS
) {
1147 error(_("wrong number of fields on Leap line"));
1151 cp
= fields
[LP_YEAR
];
1152 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1156 error(_("invalid leaping year"));
1162 i
= len_years
[isleap(j
)];
1166 i
= -len_years
[isleap(j
)];
1168 dayoff
= oadd(dayoff
, eitol(i
));
1170 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1171 error(_("invalid month name"));
1174 month
= lp
->l_value
;
1176 while (j
!= month
) {
1177 i
= len_months
[isleap(year
)][j
];
1178 dayoff
= oadd(dayoff
, eitol(i
));
1181 cp
= fields
[LP_DAY
];
1182 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1183 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1184 error(_("invalid day of month"));
1187 dayoff
= oadd(dayoff
, eitol(day
- 1));
1188 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1189 error(_("time before zero"));
1192 if (dayoff
< min_time
/ SECSPERDAY
) {
1193 error(_("time too small"));
1196 if (dayoff
> max_time
/ SECSPERDAY
) {
1197 error(_("time too large"));
1200 t
= (zic_t
) dayoff
* SECSPERDAY
;
1201 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1202 cp
= fields
[LP_CORR
];
1204 register int positive
;
1207 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1210 } else if (strcmp(cp
, "--") == 0) {
1213 } else if (strcmp(cp
, "+") == 0) {
1216 } else if (strcmp(cp
, "++") == 0) {
1220 error(_("illegal CORRECTION field on Leap line"));
1223 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1225 "illegal Rolling/Stationary field on Leap line"
1229 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1234 inlink(fields
, nfields
)
1235 register char ** const fields
;
1240 if (nfields
!= LINK_FIELDS
) {
1241 error(_("wrong number of fields on Link line"));
1244 if (*fields
[LF_FROM
] == '\0') {
1245 error(_("blank FROM field on Link line"));
1248 if (*fields
[LF_TO
] == '\0') {
1249 error(_("blank TO field on Link line"));
1252 l
.l_filename
= filename
;
1253 l
.l_linenum
= linenum
;
1254 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1255 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1256 links
= (struct link
*) (void *) erealloc((char *) links
,
1257 (int) ((nlinks
+ 1) * sizeof *links
));
1258 links
[nlinks
++] = l
;
1262 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1263 register struct rule
* const rp
;
1264 const char * const loyearp
;
1265 const char * const hiyearp
;
1266 const char * const typep
;
1267 const char * const monthp
;
1268 const char * const dayp
;
1269 const char * const timep
;
1271 register const struct lookup
* lp
;
1272 register const char * cp
;
1276 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1277 error(_("invalid month name"));
1280 rp
->r_month
= lp
->l_value
;
1281 rp
->r_todisstd
= FALSE
;
1282 rp
->r_todisgmt
= FALSE
;
1283 dp
= ecpyalloc(timep
);
1285 ep
= dp
+ strlen(dp
) - 1;
1286 switch (lowerit(*ep
)) {
1287 case 's': /* Standard */
1288 rp
->r_todisstd
= TRUE
;
1289 rp
->r_todisgmt
= FALSE
;
1292 case 'w': /* Wall */
1293 rp
->r_todisstd
= FALSE
;
1294 rp
->r_todisgmt
= FALSE
;
1297 case 'g': /* Greenwich */
1298 case 'u': /* Universal */
1299 case 'z': /* Zulu */
1300 rp
->r_todisstd
= TRUE
;
1301 rp
->r_todisgmt
= TRUE
;
1306 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1312 lp
= byword(cp
, begin_years
);
1313 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1315 rp
->r_loyear
= INT_MIN
;
1318 rp
->r_loyear
= INT_MAX
;
1320 default: /* "cannot happen" */
1321 (void) fprintf(stderr
,
1322 _("%s: panic: Invalid l_value %d\n"),
1323 progname
, lp
->l_value
);
1325 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1326 error(_("invalid starting year"));
1329 if (rp
->r_loyear
< min_year_representable
)
1330 warning(_("starting year too low to be represented"));
1331 else if (rp
->r_loyear
> max_year_representable
)
1332 warning(_("starting year too high to be represented"));
1335 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1337 rp
->r_hiyear
= INT_MIN
;
1340 rp
->r_hiyear
= INT_MAX
;
1343 rp
->r_hiyear
= rp
->r_loyear
;
1345 default: /* "cannot happen" */
1346 (void) fprintf(stderr
,
1347 _("%s: panic: Invalid l_value %d\n"),
1348 progname
, lp
->l_value
);
1350 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1351 error(_("invalid ending year"));
1354 if (rp
->r_loyear
< min_year_representable
)
1355 warning(_("ending year too low to be represented"));
1356 else if (rp
->r_loyear
> max_year_representable
)
1357 warning(_("ending year too high to be represented"));
1359 if (rp
->r_loyear
> rp
->r_hiyear
) {
1360 error(_("starting year greater than ending year"));
1364 rp
->r_yrtype
= NULL
;
1366 if (rp
->r_loyear
== rp
->r_hiyear
) {
1367 error(_("typed single year"));
1370 rp
->r_yrtype
= ecpyalloc(typep
);
1372 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1373 min_year
= rp
->r_loyear
;
1376 ** Accept things such as:
1382 dp
= ecpyalloc(dayp
);
1383 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1384 rp
->r_dycode
= DC_DOWLEQ
;
1385 rp
->r_wday
= lp
->l_value
;
1386 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1388 if ((ep
= strchr(dp
, '<')) != 0)
1389 rp
->r_dycode
= DC_DOWLEQ
;
1390 else if ((ep
= strchr(dp
, '>')) != 0)
1391 rp
->r_dycode
= DC_DOWGEQ
;
1394 rp
->r_dycode
= DC_DOM
;
1396 if (rp
->r_dycode
!= DC_DOM
) {
1399 error(_("invalid day of month"));
1403 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1404 error(_("invalid weekday name"));
1408 rp
->r_wday
= lp
->l_value
;
1410 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1411 rp
->r_dayofmonth
<= 0 ||
1412 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1413 error(_("invalid day of month"));
1427 register long shift
;
1429 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1430 buf
[i
] = val
>> shift
;
1441 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1445 atcomp(const void * avp
, const void * bvp
)
1447 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1449 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1456 const char * const name
;
1460 static char * fullname
;
1461 static struct tzhead tzh
;
1462 zic_t ats
[TZ_MAX_TIMES
];
1463 unsigned char types
[TZ_MAX_TIMES
];
1469 (void) qsort((void *) attypes
, (size_t) timecnt
,
1470 (size_t) sizeof *attypes
, atcomp
);
1480 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1483 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1484 ++fromi
; /* handled by default rule */
1485 for ( ; fromi
< timecnt
; ++fromi
) {
1486 if (toi
!= 0 && ((attypes
[fromi
].at
+
1487 gmtoffs
[attypes
[toi
- 1].type
]) <=
1488 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1489 : attypes
[toi
- 2].type
]))) {
1490 attypes
[toi
- 1].type
=
1491 attypes
[fromi
].type
;
1495 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1496 attypes
[toi
++] = attypes
[fromi
];
1503 for (i
= 0; i
< timecnt
; ++i
) {
1504 ats
[i
] = attypes
[i
].at
;
1505 types
[i
] = attypes
[i
].type
;
1507 fullname
= erealloc(fullname
,
1508 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1509 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1511 ** Remove old file, if any, to snap links.
1513 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1514 const char *e
= strerror(errno
);
1516 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1517 progname
, fullname
, e
);
1520 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1521 if (mkdirs(fullname
) != 0)
1523 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1524 const char *e
= strerror(errno
);
1526 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1527 progname
, fullname
, e
);
1531 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1532 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1533 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1534 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1535 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1536 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1537 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1538 #define DO(field) (void) fwrite((void *) tzh.field, \
1539 (size_t) sizeof tzh.field, (size_t) 1, fp)
1549 for (i
= 0; i
< timecnt
; ++i
) {
1552 if (ats
[i
] >= trans
[j
]) {
1553 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1556 puttzcode((long) ats
[i
], fp
);
1559 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1560 (size_t) timecnt
, fp
);
1561 for (i
= 0; i
< typecnt
; ++i
) {
1562 puttzcode((long) gmtoffs
[i
], fp
);
1563 (void) putc(isdsts
[i
], fp
);
1564 (void) putc(abbrinds
[i
], fp
);
1567 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1568 (size_t) charcnt
, fp
);
1569 for (i
= 0; i
< leapcnt
; ++i
) {
1571 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1574 if (++j
>= typecnt
) {
1580 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1584 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1585 } else puttzcode((long) trans
[i
], fp
);
1586 puttzcode((long) corr
[i
], fp
);
1588 for (i
= 0; i
< typecnt
; ++i
)
1589 (void) putc(ttisstds
[i
], fp
);
1590 for (i
= 0; i
< typecnt
; ++i
)
1591 (void) putc(ttisgmts
[i
], fp
);
1592 if (ferror(fp
) || fclose(fp
)) {
1593 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1594 progname
, fullname
);
1600 doabbr(abbr
, format
, letters
, isdst
)
1602 const char * const format
;
1603 const char * const letters
;
1606 if (strchr(format
, '/') == NULL
) {
1607 if (letters
== NULL
)
1608 (void) strcpy(abbr
, format
);
1609 else (void) sprintf(abbr
, format
, letters
);
1611 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1613 (void) strcpy(abbr
, format
);
1614 *strchr(abbr
, '/') = '\0';
1619 outzone(zpfirst
, zonecount
)
1620 const struct zone
* const zpfirst
;
1621 const int zonecount
;
1623 register const struct zone
* zp
;
1624 register struct rule
* rp
;
1626 register int usestart
, useuntil
;
1627 register zic_t starttime
, untiltime
;
1628 register long gmtoff
;
1629 register long stdoff
;
1631 register long startoff
;
1632 register int startttisstd
;
1633 register int startttisgmt
;
1635 char startbuf
[BUFSIZ
];
1637 INITIALIZE(untiltime
);
1638 INITIALIZE(starttime
);
1640 ** Now. . .finally. . .generate some useful data!
1646 ** Thanks to Earl Chew
1647 ** for noting the need to unconditionally initialize startttisstd.
1649 startttisstd
= FALSE
;
1650 startttisgmt
= FALSE
;
1651 for (i
= 0; i
< zonecount
; ++i
) {
1653 ** A guess that may well be corrected later.
1657 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1658 useuntil
= i
< (zonecount
- 1);
1659 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1661 gmtoff
= zp
->z_gmtoff
;
1662 eat(zp
->z_filename
, zp
->z_linenum
);
1664 startoff
= zp
->z_gmtoff
;
1665 if (zp
->z_nrules
== 0) {
1666 stdoff
= zp
->z_stdoff
;
1667 doabbr(startbuf
, zp
->z_format
,
1668 (char *) NULL
, stdoff
!= 0);
1669 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1670 startbuf
, stdoff
!= 0, startttisstd
,
1673 addtt(starttime
, type
);
1675 } else if (stdoff
!= 0)
1676 addtt(min_time
, type
);
1677 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1678 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1681 ** Mark which rules to do in the current year.
1682 ** For those to do, calculate rpytime(rp, year);
1684 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1685 rp
= &zp
->z_rules
[j
];
1686 eats(zp
->z_filename
, zp
->z_linenum
,
1687 rp
->r_filename
, rp
->r_linenum
);
1688 rp
->r_todo
= year
>= rp
->r_loyear
&&
1689 year
<= rp
->r_hiyear
&&
1690 yearistype(year
, rp
->r_yrtype
);
1692 rp
->r_temp
= rpytime(rp
, year
);
1696 register zic_t jtime
, ktime
;
1697 register long offset
;
1703 ** Turn untiltime into UTC
1704 ** assuming the current gmtoff and
1707 untiltime
= zp
->z_untiltime
;
1708 if (!zp
->z_untilrule
.r_todisgmt
)
1709 untiltime
= tadd(untiltime
,
1711 if (!zp
->z_untilrule
.r_todisstd
)
1712 untiltime
= tadd(untiltime
,
1716 ** Find the rule (of those to do, if any)
1717 ** that takes effect earliest in the year.
1720 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1721 rp
= &zp
->z_rules
[j
];
1724 eats(zp
->z_filename
, zp
->z_linenum
,
1725 rp
->r_filename
, rp
->r_linenum
);
1726 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1727 if (!rp
->r_todisstd
)
1728 offset
= oadd(offset
, stdoff
);
1730 if (jtime
== min_time
||
1733 jtime
= tadd(jtime
, -offset
);
1734 if (k
< 0 || jtime
< ktime
) {
1740 break; /* go on to next year */
1741 rp
= &zp
->z_rules
[k
];
1743 if (useuntil
&& ktime
>= untiltime
)
1745 stdoff
= rp
->r_stdoff
;
1746 if (usestart
&& ktime
== starttime
)
1749 if (ktime
< starttime
) {
1750 startoff
= oadd(zp
->z_gmtoff
,
1752 doabbr(startbuf
, zp
->z_format
,
1757 if (*startbuf
== '\0' &&
1758 startoff
== oadd(zp
->z_gmtoff
,
1766 eats(zp
->z_filename
, zp
->z_linenum
,
1767 rp
->r_filename
, rp
->r_linenum
);
1768 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1770 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1771 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1772 rp
->r_todisstd
, rp
->r_todisgmt
);
1777 if (*startbuf
== '\0' &&
1778 zp
->z_format
!= NULL
&&
1779 strchr(zp
->z_format
, '%') == NULL
&&
1780 strchr(zp
->z_format
, '/') == NULL
)
1781 (void) strcpy(startbuf
, zp
->z_format
);
1782 eat(zp
->z_filename
, zp
->z_linenum
);
1783 if (*startbuf
== '\0')
1784 error(_("can't determine time zone abbreviation to use just after until time"));
1785 else addtt(starttime
,
1786 addtype(startoff
, startbuf
,
1787 startoff
!= zp
->z_gmtoff
,
1792 ** Now we may get to set starttime for the next zone line.
1795 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1796 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1797 starttime
= zp
->z_untiltime
;
1799 starttime
= tadd(starttime
, -stdoff
);
1801 starttime
= tadd(starttime
, -gmtoff
);
1804 writezone(zpfirst
->z_name
);
1808 addtt(starttime
, type
)
1809 const zic_t starttime
;
1812 if (starttime
<= min_time
||
1813 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1814 gmtoffs
[0] = gmtoffs
[type
];
1815 isdsts
[0] = isdsts
[type
];
1816 ttisstds
[0] = ttisstds
[type
];
1817 ttisgmts
[0] = ttisgmts
[type
];
1818 if (abbrinds
[type
] != 0)
1819 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1821 charcnt
= strlen(chars
) + 1;
1826 if (timecnt
>= TZ_MAX_TIMES
) {
1827 error(_("too many transitions?!"));
1830 attypes
[timecnt
].at
= starttime
;
1831 attypes
[timecnt
].type
= type
;
1836 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1838 const char * const abbr
;
1845 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1846 error(_("internal error - addtype called with bad isdst"));
1849 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1850 error(_("internal error - addtype called with bad ttisstd"));
1853 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1854 error(_("internal error - addtype called with bad ttisgmt"));
1858 ** See if there's already an entry for this zone type.
1859 ** If so, just return its index.
1861 for (i
= 0; i
< typecnt
; ++i
) {
1862 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1863 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1864 ttisstd
== ttisstds
[i
] &&
1865 ttisgmt
== ttisgmts
[i
])
1869 ** There isn't one; add a new one, unless there are already too
1872 if (typecnt
>= TZ_MAX_TYPES
) {
1873 error(_("too many local time types"));
1876 gmtoffs
[i
] = gmtoff
;
1878 ttisstds
[i
] = ttisstd
;
1879 ttisgmts
[i
] = ttisgmt
;
1881 for (j
= 0; j
< charcnt
; ++j
)
1882 if (strcmp(&chars
[j
], abbr
) == 0)
1892 leapadd(t
, positive
, rolling
, count
)
1900 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1901 error(_("too many leap seconds"));
1904 for (i
= 0; i
< leapcnt
; ++i
)
1905 if (t
<= trans
[i
]) {
1906 if (t
== trans
[i
]) {
1907 error(_("repeated leap second moment"));
1913 for (j
= leapcnt
; j
> i
; --j
) {
1914 trans
[j
] = trans
[j
- 1];
1915 corr
[j
] = corr
[j
- 1];
1916 roll
[j
] = roll
[j
- 1];
1919 corr
[i
] = positive
? 1L : eitol(-count
);
1922 } while (positive
&& --count
!= 0);
1929 register long last
= 0;
1932 ** propagate leap seconds forward
1934 for (i
= 0; i
< leapcnt
; ++i
) {
1935 trans
[i
] = tadd(trans
[i
], last
);
1936 last
= corr
[i
] += last
;
1941 yearistype(year
, type
)
1943 const char * const type
;
1948 if (type
== NULL
|| *type
== '\0')
1950 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1951 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1952 result
= system(buf
);
1953 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1959 error(_("Wild result from command execution"));
1960 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1961 progname
, buf
, result
);
1970 a
= (unsigned char) a
;
1971 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1975 ciequal(ap
, bp
) /* case-insensitive equality */
1976 register const char * ap
;
1977 register const char * bp
;
1979 while (lowerit(*ap
) == lowerit(*bp
++))
1987 register const char * abbr
;
1988 register const char * word
;
1990 if (lowerit(*abbr
) != lowerit(*word
))
1993 while (*++abbr
!= '\0')
1997 } while (lowerit(*word
++) != lowerit(*abbr
));
2001 static const struct lookup
*
2003 register const char * const word
;
2004 register const struct lookup
* const table
;
2006 register const struct lookup
* foundlp
;
2007 register const struct lookup
* lp
;
2009 if (word
== NULL
|| table
== NULL
)
2012 ** Look for exact match.
2014 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2015 if (ciequal(word
, lp
->l_word
))
2018 ** Look for inexact match.
2021 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2022 if (itsabbr(word
, lp
->l_word
)) {
2023 if (foundlp
== NULL
)
2025 else return NULL
; /* multiple inexact matches */
2035 register char ** array
;
2040 array
= (char **) (void *)
2041 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2044 while (isascii((unsigned char) *cp
) &&
2045 isspace((unsigned char) *cp
))
2047 if (*cp
== '\0' || *cp
== '#')
2049 array
[nsubs
++] = dp
= cp
;
2051 if ((*dp
= *cp
++) != '"')
2053 else while ((*dp
= *cp
++) != '"')
2057 "Odd number of quotation marks"
2059 } while (*cp
!= '\0' && *cp
!= '#' &&
2060 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2061 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2065 array
[nsubs
] = NULL
;
2077 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2078 error(_("time overflow"));
2091 if (t1
== max_time
&& t2
> 0)
2093 if (t1
== min_time
&& t2
< 0)
2096 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2097 error(_("time overflow"));
2104 ** Given a rule, and a year, compute the date - in seconds since January 1,
2105 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2109 rpytime(rp
, wantedy
)
2110 register const struct rule
* const rp
;
2111 register const int wantedy
;
2113 register int y
, m
, i
;
2114 register long dayoff
; /* with a nod to Margaret O. */
2117 if (wantedy
== INT_MIN
)
2119 if (wantedy
== INT_MAX
)
2124 while (wantedy
!= y
) {
2126 i
= len_years
[isleap(y
)];
2130 i
= -len_years
[isleap(y
)];
2132 dayoff
= oadd(dayoff
, eitol(i
));
2134 while (m
!= rp
->r_month
) {
2135 i
= len_months
[isleap(y
)][m
];
2136 dayoff
= oadd(dayoff
, eitol(i
));
2139 i
= rp
->r_dayofmonth
;
2140 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2141 if (rp
->r_dycode
== DC_DOWLEQ
)
2144 error(_("use of 2/29 in non leap-year"));
2149 dayoff
= oadd(dayoff
, eitol(i
));
2150 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2153 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2154 wday
= eitol(EPOCH_WDAY
);
2156 ** Don't trust mod of negative numbers.
2159 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2161 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2163 wday
+= LDAYSPERWEEK
;
2165 while (wday
!= eitol(rp
->r_wday
))
2166 if (rp
->r_dycode
== DC_DOWGEQ
) {
2167 dayoff
= oadd(dayoff
, (long) 1);
2168 if (++wday
>= LDAYSPERWEEK
)
2172 dayoff
= oadd(dayoff
, (long) -1);
2174 wday
= LDAYSPERWEEK
- 1;
2177 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2179 warning(_("rule goes past start/end of month--\
2180 will not work with pre-2004 versions of zic"));
2183 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
))
2185 if (dayoff
< min_time
/ SECSPERDAY
)
2187 if (dayoff
> max_time
/ SECSPERDAY
)
2189 t
= (zic_t
) dayoff
* SECSPERDAY
;
2190 return tadd(t
, rp
->r_tod
);
2195 const char * const string
;
2199 if (strcmp(string
, GRANDPARENTED
) != 0) {
2200 register const char * cp
;
2204 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2205 ** optionally followed by a + or - and a number from 1 to 14.
2209 while (isascii((unsigned char) *cp
) &&
2210 isalpha((unsigned char) *cp
))
2212 if (cp
- string
== 0)
2213 wp
= _("time zone abbreviation lacks alphabetic at start");
2214 if (noise
&& cp
- string
> 3)
2215 wp
= _("time zone abbreviation has more than 3 alphabetics");
2216 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2217 wp
= _("time zone abbreviation has too many alphabetics");
2218 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2220 if (isascii((unsigned char) *cp
) &&
2221 isdigit((unsigned char) *cp
))
2223 *cp
>= '0' && *cp
<= '4')
2227 wp
= _("time zone abbreviation differs from POSIX standard");
2230 wp
= ecatalloc(wp
, " (");
2231 wp
= ecatalloc(wp
, string
);
2232 wp
= ecatalloc(wp
, ")");
2237 i
= strlen(string
) + 1;
2238 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2239 error(_("too many, or too long, time zone abbreviations"));
2242 (void) strcpy(&chars
[charcnt
], string
);
2243 charcnt
+= eitol(i
);
2248 char * const argname
;
2250 register char * name
;
2253 if (argname
== NULL
|| *argname
== '\0')
2255 cp
= name
= ecpyalloc(argname
);
2256 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2260 ** DOS drive specifier?
2262 if (isalpha((unsigned char) name
[0]) &&
2263 name
[1] == ':' && name
[2] == '\0') {
2267 #endif /* !defined unix */
2268 if (!itsdir(name
)) {
2270 ** It doesn't seem to exist, so we try to create it.
2271 ** Creation may fail because of the directory being
2272 ** created by some other multiprocessor, so we get
2273 ** to do extra checking.
2275 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2276 const char *e
= strerror(errno
);
2278 if (errno
!= EEXIST
|| !itsdir(name
)) {
2279 (void) fprintf(stderr
,
2280 _("%s: Can't create directory %s: %s\n"),
2300 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2301 (void) fprintf(stderr
,
2302 _("%s: %d did not sign extend correctly\n"),
2310 ** UNIX was a registered trademark of The Open Group in 2003.