1 static char elsieid
[] = "@(#)zic.c 7.118";
4 ** Regardless of the type of time_t, we do our work using this type.
17 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
19 #define MKDIR_UMASK 0755
23 ** On some ancient hosts, predicates like `isspace(C)' are defined
24 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
25 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
26 ** Neither the C Standard nor Posix require that `isascii' exist.
27 ** For portability, we check both ancient and modern requirements.
28 ** If isascii is not defined, the isascii check succeeds trivially.
36 const char * r_filename
;
40 int r_loyear
; /* for example, 1986 */
41 int r_hiyear
; /* for example, 1986 */
42 const char * r_yrtype
;
44 int r_month
; /* 0..11 */
46 int r_dycode
; /* see below */
50 long r_tod
; /* time from midnight */
51 int r_todisstd
; /* above is standard time if TRUE */
52 /* or wall clock time if FALSE */
53 int r_todisgmt
; /* above is GMT if TRUE */
54 /* or local time if FALSE */
55 long r_stdoff
; /* offset from standard time */
56 const char * r_abbrvar
; /* variable part of abbreviation */
58 int r_todo
; /* a rule to do (used in outzone) */
59 zic_t r_temp
; /* used in outzone */
63 ** r_dycode r_dayofmonth r_wday
66 #define DC_DOM 0 /* 1..31 */ /* unused */
67 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
68 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
71 const char * z_filename
;
77 const char * z_format
;
81 struct rule
* z_rules
;
84 struct rule z_untilrule
;
88 extern int getopt
P((int argc
, char * const argv
[],
89 const char * options
));
90 extern int link
P((const char * fromname
, const char * toname
));
94 static void addtt
P((zic_t starttime
, int type
));
95 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
96 int ttisstd
, int ttisgmt
));
97 static void leapadd
P((zic_t t
, int positive
, int rolling
, int count
));
98 static void adjleap
P((void));
99 static void associate
P((void));
100 static int ciequal
P((const char * ap
, const char * bp
));
101 static void convert
P((long val
, char * buf
));
102 static void dolink
P((const char * fromfile
, const char * tofile
));
103 static void doabbr
P((char * abbr
, const char * format
,
104 const char * letters
, int isdst
));
105 static void eat
P((const char * name
, int num
));
106 static void eats
P((const char * name
, int num
,
107 const char * rname
, int rnum
));
108 static long eitol
P((int i
));
109 static void error
P((const char * message
));
110 static char ** getfields
P((char * buf
));
111 static long gethms
P((const char * string
, const char * errstrng
,
113 static void infile
P((const char * filename
));
114 static void inleap
P((char ** fields
, int nfields
));
115 static void inlink
P((char ** fields
, int nfields
));
116 static void inrule
P((char ** fields
, int nfields
));
117 static int inzcont
P((char ** fields
, int nfields
));
118 static int inzone
P((char ** fields
, int nfields
));
119 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
120 static int itsabbr
P((const char * abbr
, const char * word
));
121 static int itsdir
P((const char * name
));
122 static int lowerit
P((int c
));
123 static char * memcheck
P((char * tocheck
));
124 static int mkdirs
P((char * filename
));
125 static void newabbr
P((const char * abbr
));
126 static long oadd
P((long t1
, long t2
));
127 static void outzone
P((const struct zone
* zp
, int ntzones
));
128 static void puttzcode
P((long code
, FILE * fp
));
129 static int rcomp
P((const void * leftp
, const void * rightp
));
130 static zic_t rpytime
P((const struct rule
* rp
, int wantedy
));
131 static void rulesub
P((struct rule
* rp
,
132 const char * loyearp
, const char * hiyearp
,
133 const char * typep
, const char * monthp
,
134 const char * dayp
, const char * timep
));
135 static void setboundaries
P((void));
136 static zic_t tadd
P((zic_t t1
, long t2
));
137 static void usage
P((void));
138 static void writezone
P((const char * name
));
139 static int yearistype
P((int year
, const char * type
));
141 #if !(HAVE_STRERROR - 0)
142 static char * strerror
P((int));
143 #endif /* !(HAVE_STRERROR - 0) */
147 static const char * filename
;
150 static zic_t max_time
;
152 static int max_year_representable
;
153 static zic_t min_time
;
155 static int min_year_representable
;
157 static const char * rfilename
;
159 static const char * progname
;
173 ** Which fields are which on a Zone line.
181 #define ZF_TILMONTH 6
184 #define ZONE_MINFIELDS 5
185 #define ZONE_MAXFIELDS 9
188 ** Which fields are which on a Zone continuation line.
194 #define ZFC_TILYEAR 3
195 #define ZFC_TILMONTH 4
197 #define ZFC_TILTIME 6
198 #define ZONEC_MINFIELDS 3
199 #define ZONEC_MAXFIELDS 7
202 ** Which files are which on a Rule line.
214 #define RULE_FIELDS 10
217 ** Which fields are which on a Link line.
222 #define LINK_FIELDS 3
225 ** Which fields are which on a Leap line.
234 #define LEAP_FIELDS 7
244 static struct rule
* rules
;
245 static int nrules
; /* number of rules */
247 static struct zone
* zones
;
248 static int nzones
; /* number of zones */
251 const char * l_filename
;
257 static struct link
* links
;
265 static struct lookup
const * byword
P((const char * string
,
266 const struct lookup
* lp
));
268 static struct lookup
const line_codes
[] = {
276 static struct lookup
const mon_names
[] = {
277 { "January", TM_JANUARY
},
278 { "February", TM_FEBRUARY
},
279 { "March", TM_MARCH
},
280 { "April", TM_APRIL
},
284 { "August", TM_AUGUST
},
285 { "September", TM_SEPTEMBER
},
286 { "October", TM_OCTOBER
},
287 { "November", TM_NOVEMBER
},
288 { "December", TM_DECEMBER
},
292 static struct lookup
const wday_names
[] = {
293 { "Sunday", TM_SUNDAY
},
294 { "Monday", TM_MONDAY
},
295 { "Tuesday", TM_TUESDAY
},
296 { "Wednesday", TM_WEDNESDAY
},
297 { "Thursday", TM_THURSDAY
},
298 { "Friday", TM_FRIDAY
},
299 { "Saturday", TM_SATURDAY
},
303 static struct lookup
const lasts
[] = {
304 { "last-Sunday", TM_SUNDAY
},
305 { "last-Monday", TM_MONDAY
},
306 { "last-Tuesday", TM_TUESDAY
},
307 { "last-Wednesday", TM_WEDNESDAY
},
308 { "last-Thursday", TM_THURSDAY
},
309 { "last-Friday", TM_FRIDAY
},
310 { "last-Saturday", TM_SATURDAY
},
314 static struct lookup
const begin_years
[] = {
315 { "minimum", YR_MINIMUM
},
316 { "maximum", YR_MAXIMUM
},
320 static struct lookup
const end_years
[] = {
321 { "minimum", YR_MINIMUM
},
322 { "maximum", YR_MAXIMUM
},
327 static struct lookup
const leap_types
[] = {
329 { "Stationary", FALSE
},
333 static const int len_months
[2][MONSPERYEAR
] = {
334 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
335 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
338 static const int len_years
[2] = {
339 DAYSPERNYEAR
, DAYSPERLYEAR
342 static struct attype
{
345 } attypes
[TZ_MAX_TIMES
];
346 static long gmtoffs
[TZ_MAX_TYPES
];
347 static char isdsts
[TZ_MAX_TYPES
];
348 static unsigned char abbrinds
[TZ_MAX_TYPES
];
349 static char ttisstds
[TZ_MAX_TYPES
];
350 static char ttisgmts
[TZ_MAX_TYPES
];
351 static char chars
[TZ_MAX_CHARS
];
352 static zic_t trans
[TZ_MAX_LEAPS
];
353 static long corr
[TZ_MAX_LEAPS
];
354 static char roll
[TZ_MAX_LEAPS
];
357 ** Memory allocation.
365 const char *e
= strerror(errno
);
367 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
369 (void) exit(EXIT_FAILURE
);
374 #define emalloc(size) memcheck(imalloc(size))
375 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
376 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
377 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
383 #if !(HAVE_STRERROR - 0)
388 extern char * sys_errlist
[];
391 return (errnum
> 0 && errnum
<= sys_nerr
) ?
392 sys_errlist
[errnum
] : _("Unknown system error");
394 #endif /* !(HAVE_STRERROR - 0) */
397 eats(name
, num
, rname
, rnum
)
398 const char * const name
;
400 const char * const rname
;
411 const char * const name
;
414 eats(name
, num
, (char *) NULL
, -1);
419 const char * const string
;
422 ** Match the format of "cc" to allow sh users to
423 ** zic ... 2>&1 | error -t "*" -v
426 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
427 filename
, linenum
, string
);
428 if (rfilename
!= NULL
)
429 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
430 rfilename
, rlinenum
);
431 (void) fprintf(stderr
, "\n");
437 const char * const string
;
441 cp
= ecpyalloc(_("warning: "));
442 cp
= ecatalloc(cp
, string
);
451 (void) fprintf(stderr
, _("%s: usage is %s [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
453 (void) exit(EXIT_FAILURE
);
456 static const char * psxrules
;
457 static const char * lcltime
;
458 static const char * directory
;
459 static const char * leapsec
;
460 static const char * yitcommand
;
461 static int sflag
= FALSE
;
473 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
474 #endif /* defined unix */
476 (void) setlocale(LC_CTYPE
, "");
477 (void) setlocale(LC_MESSAGES
, "");
479 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
480 #endif /* defined TEXTDOMAINDIR */
481 (void) textdomain(TZ_DOMAIN
);
482 #endif /* HAVE_GETTEXT - 0 */
484 for (i
= 1; i
< argc
; ++i
)
485 if (strcmp(argv
[i
], "--version") == 0) {
486 (void) printf("%s\n", elsieid
);
487 (void) exit(EXIT_SUCCESS
);
489 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
494 if (directory
== NULL
)
497 (void) fprintf(stderr
,
498 _("%s: More than one -d option specified\n"),
500 (void) exit(EXIT_FAILURE
);
507 (void) fprintf(stderr
,
508 _("%s: More than one -l option specified\n"),
510 (void) exit(EXIT_FAILURE
);
514 if (psxrules
== NULL
)
517 (void) fprintf(stderr
,
518 _("%s: More than one -p option specified\n"),
520 (void) exit(EXIT_FAILURE
);
524 if (yitcommand
== NULL
)
527 (void) fprintf(stderr
,
528 _("%s: More than one -y option specified\n"),
530 (void) exit(EXIT_FAILURE
);
537 (void) fprintf(stderr
,
538 _("%s: More than one -L option specified\n"),
540 (void) exit(EXIT_FAILURE
);
550 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
551 usage(); /* usage message by request */
552 if (directory
== NULL
)
554 if (yitcommand
== NULL
)
555 yitcommand
= "yearistype";
559 if (optind
< argc
&& leapsec
!= NULL
) {
564 for (i
= optind
; i
< argc
; ++i
)
567 (void) exit(EXIT_FAILURE
);
569 for (i
= 0; i
< nzones
; i
= j
) {
571 ** Find the next non-continuation zone entry.
573 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
575 outzone(&zones
[i
], j
- i
);
580 for (i
= 0; i
< nlinks
; ++i
) {
581 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
582 dolink(links
[i
].l_from
, links
[i
].l_to
);
584 if (lcltime
!= NULL
) {
585 eat("command line", 1);
586 dolink(lcltime
, TZDEFAULT
);
588 if (psxrules
!= NULL
) {
589 eat("command line", 1);
590 dolink(psxrules
, TZDEFRULES
);
592 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
596 dolink(fromfile
, tofile
)
597 const char * const fromfile
;
598 const char * const tofile
;
600 register char * fromname
;
601 register char * toname
;
603 if (fromfile
[0] == '/')
604 fromname
= ecpyalloc(fromfile
);
606 fromname
= ecpyalloc(directory
);
607 fromname
= ecatalloc(fromname
, "/");
608 fromname
= ecatalloc(fromname
, fromfile
);
610 if (tofile
[0] == '/')
611 toname
= ecpyalloc(tofile
);
613 toname
= ecpyalloc(directory
);
614 toname
= ecatalloc(toname
, "/");
615 toname
= ecatalloc(toname
, tofile
);
618 ** We get to be careful here since
619 ** there's a fair chance of root running us.
622 (void) remove(toname
);
623 if (link(fromname
, toname
) != 0) {
626 if (mkdirs(toname
) != 0)
627 (void) exit(EXIT_FAILURE
);
629 result
= link(fromname
, toname
);
630 #if (HAVE_SYMLINK - 0)
632 access(fromname
, F_OK
) == 0 &&
634 const char *s
= tofile
;
635 register char * symlinkcontents
= NULL
;
636 while ((s
= strchr(s
+1, '/')) != NULL
)
637 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
638 symlinkcontents
= ecatalloc(symlinkcontents
, fromfile
);
640 result
= unlink(toname
);
641 if (result
!= 0 && errno
!= ENOENT
) {
642 const char *e
= strerror(errno
);
644 (void) fprintf(stderr
,
645 _("%s: Can't unlink %s: %s\n"),
646 progname
, toname
, e
);
647 (void) exit(EXIT_FAILURE
);
650 result
= symlink(symlinkcontents
, toname
);
652 warning(_("hard link failed, symbolic link used"));
653 ifree(symlinkcontents
);
657 const char *e
= strerror(errno
);
659 (void) fprintf(stderr
,
660 _("%s: Can't link from %s to %s: %s\n"),
661 progname
, fromname
, toname
, e
);
662 (void) exit(EXIT_FAILURE
);
670 #define INT_MAX ((int) (((unsigned)~0)>>1))
671 #endif /* !defined INT_MAX */
674 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
675 #endif /* !defined INT_MIN */
678 ** The tz file format currently allows at most 32-bit quantities.
679 ** This restriction should be removed before signed 32-bit values
680 ** wrap around in 2038, but unfortunately this will require a
681 ** change to the tz file format.
684 #define MAX_BITS_IN_FILE 32
685 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(zic_t) < MAX_BITS_IN_FILE) ? \
686 TYPE_BIT(zic_t) : MAX_BITS_IN_FILE)
689 setboundaries
P((void))
693 if (TYPE_SIGNED(zic_t
)) {
695 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
697 max_time
= -(min_time
+ 1);
702 max_time
= 2 - sflag
;
703 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
710 t
= (time_t) min_time
;
711 min_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
712 t
= (time_t) max_time
;
713 max_year
= TM_YEAR_BASE
+ gmtime(&t
)->tm_year
;
715 min_year_representable
= min_year
;
716 max_year_representable
= max_year
;
721 const char * const name
;
723 register char * myname
;
726 myname
= ecpyalloc(name
);
727 myname
= ecatalloc(myname
, "/.");
728 accres
= access(myname
, F_OK
);
734 ** Associate sets of rules with zones.
738 ** Sort by rule name.
746 return strcmp(((const struct rule
*) cp1
)->r_name
,
747 ((const struct rule
*) cp2
)->r_name
);
753 register struct zone
* zp
;
754 register struct rule
* rp
;
755 register int base
, out
;
759 (void) qsort((void *) rules
, (size_t) nrules
,
760 (size_t) sizeof *rules
, rcomp
);
761 for (i
= 0; i
< nrules
- 1; ++i
) {
762 if (strcmp(rules
[i
].r_name
,
763 rules
[i
+ 1].r_name
) != 0)
765 if (strcmp(rules
[i
].r_filename
,
766 rules
[i
+ 1].r_filename
) == 0)
768 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
769 warning(_("same rule name in multiple files"));
770 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
771 warning(_("same rule name in multiple files"));
772 for (j
= i
+ 2; j
< nrules
; ++j
) {
773 if (strcmp(rules
[i
].r_name
,
774 rules
[j
].r_name
) != 0)
776 if (strcmp(rules
[i
].r_filename
,
777 rules
[j
].r_filename
) == 0)
779 if (strcmp(rules
[i
+ 1].r_filename
,
780 rules
[j
].r_filename
) == 0)
787 for (i
= 0; i
< nzones
; ++i
) {
792 for (base
= 0; base
< nrules
; base
= out
) {
794 for (out
= base
+ 1; out
< nrules
; ++out
)
795 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
797 for (i
= 0; i
< nzones
; ++i
) {
799 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
802 zp
->z_nrules
= out
- base
;
805 for (i
= 0; i
< nzones
; ++i
) {
807 if (zp
->z_nrules
== 0) {
809 ** Maybe we have a local standard time offset.
811 eat(zp
->z_filename
, zp
->z_linenum
);
812 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
815 ** Note, though, that if there's no rule,
816 ** a '%s' in the format is a bad thing.
818 if (strchr(zp
->z_format
, '%') != 0)
819 error(_("%s in ruleless zone"));
823 (void) exit(EXIT_FAILURE
);
831 register char ** fields
;
833 register const struct lookup
* lp
;
834 register int nfields
;
835 register int wantcont
;
839 if (strcmp(name
, "-") == 0) {
840 name
= _("standard input");
842 } else if ((fp
= fopen(name
, "r")) == NULL
) {
843 const char *e
= strerror(errno
);
845 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
847 (void) exit(EXIT_FAILURE
);
850 for (num
= 1; ; ++num
) {
852 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
854 cp
= strchr(buf
, '\n');
856 error(_("line too long"));
857 (void) exit(EXIT_FAILURE
);
860 fields
= getfields(buf
);
862 while (fields
[nfields
] != NULL
) {
865 if (strcmp(fields
[nfields
], "-") == 0)
866 fields
[nfields
] = &nada
;
871 } else if (wantcont
) {
872 wantcont
= inzcont(fields
, nfields
);
874 lp
= byword(fields
[0], line_codes
);
876 error(_("input line of unknown type"));
877 else switch ((int) (lp
->l_value
)) {
879 inrule(fields
, nfields
);
883 wantcont
= inzone(fields
, nfields
);
886 inlink(fields
, nfields
);
891 (void) fprintf(stderr
,
892 _("%s: Leap line in non leap seconds file %s\n"),
894 else inleap(fields
, nfields
);
897 default: /* "cannot happen" */
898 (void) fprintf(stderr
,
899 _("%s: panic: Invalid l_value %d\n"),
900 progname
, lp
->l_value
);
901 (void) exit(EXIT_FAILURE
);
904 ifree((char *) fields
);
907 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
909 (void) exit(EXIT_FAILURE
);
911 if (fp
!= stdin
&& fclose(fp
)) {
912 const char *e
= strerror(errno
);
914 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
915 progname
, filename
, e
);
916 (void) exit(EXIT_FAILURE
);
919 error(_("expected continuation line not found"));
923 ** Convert a string of one of the forms
924 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
925 ** into a number of seconds.
926 ** A null string maps to zero.
927 ** Call error with errstring and return zero on errors.
931 gethms(string
, errstring
, signable
)
933 const char * const errstring
;
936 int hh
, mm
, ss
, sign
;
938 if (string
== NULL
|| *string
== '\0')
942 else if (*string
== '-') {
946 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
948 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
950 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
951 &hh
, &mm
, &ss
) != 3) {
955 if ((hh
< 0 || hh
>= HOURSPERDAY
||
956 mm
< 0 || mm
>= MINSPERHOUR
||
957 ss
< 0 || ss
> SECSPERMIN
) &&
958 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
962 if (noise
&& hh
== HOURSPERDAY
)
963 warning(_("24:00 not handled by pre-1998 versions of zic"));
965 (eitol(hh
* MINSPERHOUR
+ mm
) *
966 eitol(SECSPERMIN
) + eitol(ss
));
970 inrule(fields
, nfields
)
971 register char ** const fields
;
974 static struct rule r
;
976 if (nfields
!= RULE_FIELDS
) {
977 error(_("wrong number of fields on Rule line"));
980 if (*fields
[RF_NAME
] == '\0') {
981 error(_("nameless rule"));
984 r
.r_filename
= filename
;
985 r
.r_linenum
= linenum
;
986 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
987 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
988 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
989 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
990 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
991 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
992 (int) ((nrules
+ 1) * sizeof *rules
));
997 inzone(fields
, nfields
)
998 register char ** const fields
;
1004 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
1005 error(_("wrong number of fields on Zone line"));
1008 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
1009 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
1011 _("\"Zone %s\" line and -l option are mutually exclusive"),
1016 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
1017 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1019 _("\"Zone %s\" line and -p option are mutually exclusive"),
1024 for (i
= 0; i
< nzones
; ++i
)
1025 if (zones
[i
].z_name
!= NULL
&&
1026 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1027 buf
= erealloc(buf
, (int) (132 +
1028 strlen(fields
[ZF_NAME
]) +
1029 strlen(zones
[i
].z_filename
)));
1031 _("duplicate zone name %s (file \"%s\", line %d)"),
1033 zones
[i
].z_filename
,
1034 zones
[i
].z_linenum
);
1038 return inzsub(fields
, nfields
, FALSE
);
1042 inzcont(fields
, nfields
)
1043 register char ** const fields
;
1046 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1047 error(_("wrong number of fields on Zone continuation line"));
1050 return inzsub(fields
, nfields
, TRUE
);
1054 inzsub(fields
, nfields
, iscont
)
1055 register char ** const fields
;
1060 static struct zone z
;
1061 register int i_gmtoff
, i_rule
, i_format
;
1062 register int i_untilyear
, i_untilmonth
;
1063 register int i_untilday
, i_untiltime
;
1064 register int hasuntil
;
1067 i_gmtoff
= ZFC_GMTOFF
;
1069 i_format
= ZFC_FORMAT
;
1070 i_untilyear
= ZFC_TILYEAR
;
1071 i_untilmonth
= ZFC_TILMONTH
;
1072 i_untilday
= ZFC_TILDAY
;
1073 i_untiltime
= ZFC_TILTIME
;
1076 i_gmtoff
= ZF_GMTOFF
;
1078 i_format
= ZF_FORMAT
;
1079 i_untilyear
= ZF_TILYEAR
;
1080 i_untilmonth
= ZF_TILMONTH
;
1081 i_untilday
= ZF_TILDAY
;
1082 i_untiltime
= ZF_TILTIME
;
1083 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1085 z
.z_filename
= filename
;
1086 z
.z_linenum
= linenum
;
1087 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1088 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1089 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1090 error(_("invalid abbreviation format"));
1094 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1095 z
.z_format
= ecpyalloc(fields
[i_format
]);
1096 hasuntil
= nfields
> i_untilyear
;
1098 z
.z_untilrule
.r_filename
= filename
;
1099 z
.z_untilrule
.r_linenum
= linenum
;
1100 rulesub(&z
.z_untilrule
,
1101 fields
[i_untilyear
],
1104 (nfields
> i_untilmonth
) ?
1105 fields
[i_untilmonth
] : "Jan",
1106 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1107 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1108 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1109 z
.z_untilrule
.r_loyear
);
1110 if (iscont
&& nzones
> 0 &&
1111 z
.z_untiltime
> min_time
&&
1112 z
.z_untiltime
< max_time
&&
1113 zones
[nzones
- 1].z_untiltime
> min_time
&&
1114 zones
[nzones
- 1].z_untiltime
< max_time
&&
1115 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1116 error(_("Zone continuation line end time is not after end time of previous line"));
1120 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1121 (int) ((nzones
+ 1) * sizeof *zones
));
1122 zones
[nzones
++] = z
;
1124 ** If there was an UNTIL field on this line,
1125 ** there's more information about the zone on the next line.
1131 inleap(fields
, nfields
)
1132 register char ** const fields
;
1135 register const char * cp
;
1136 register const struct lookup
* lp
;
1138 int year
, month
, day
;
1142 if (nfields
!= LEAP_FIELDS
) {
1143 error(_("wrong number of fields on Leap line"));
1147 cp
= fields
[LP_YEAR
];
1148 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1152 error(_("invalid leaping year"));
1158 i
= len_years
[isleap(j
)];
1162 i
= -len_years
[isleap(j
)];
1164 dayoff
= oadd(dayoff
, eitol(i
));
1166 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1167 error(_("invalid month name"));
1170 month
= lp
->l_value
;
1172 while (j
!= month
) {
1173 i
= len_months
[isleap(year
)][j
];
1174 dayoff
= oadd(dayoff
, eitol(i
));
1177 cp
= fields
[LP_DAY
];
1178 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1179 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1180 error(_("invalid day of month"));
1183 dayoff
= oadd(dayoff
, eitol(day
- 1));
1184 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1185 error(_("time before zero"));
1188 if (dayoff
< min_time
/ SECSPERDAY
) {
1189 error(_("time too small"));
1192 if (dayoff
> max_time
/ SECSPERDAY
) {
1193 error(_("time too large"));
1196 t
= (zic_t
) dayoff
* SECSPERDAY
;
1197 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1198 cp
= fields
[LP_CORR
];
1200 register int positive
;
1203 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1206 } else if (strcmp(cp
, "--") == 0) {
1209 } else if (strcmp(cp
, "+") == 0) {
1212 } else if (strcmp(cp
, "++") == 0) {
1216 error(_("illegal CORRECTION field on Leap line"));
1219 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1220 error(_("illegal Rolling/Stationary field on Leap line"));
1223 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1228 inlink(fields
, nfields
)
1229 register char ** const fields
;
1234 if (nfields
!= LINK_FIELDS
) {
1235 error(_("wrong number of fields on Link line"));
1238 if (*fields
[LF_FROM
] == '\0') {
1239 error(_("blank FROM field on Link line"));
1242 if (*fields
[LF_TO
] == '\0') {
1243 error(_("blank TO field on Link line"));
1246 l
.l_filename
= filename
;
1247 l
.l_linenum
= linenum
;
1248 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1249 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1250 links
= (struct link
*) (void *) erealloc((char *) links
,
1251 (int) ((nlinks
+ 1) * sizeof *links
));
1252 links
[nlinks
++] = l
;
1256 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1257 register struct rule
* const rp
;
1258 const char * const loyearp
;
1259 const char * const hiyearp
;
1260 const char * const typep
;
1261 const char * const monthp
;
1262 const char * const dayp
;
1263 const char * const timep
;
1265 register const struct lookup
* lp
;
1266 register const char * cp
;
1270 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1271 error(_("invalid month name"));
1274 rp
->r_month
= lp
->l_value
;
1275 rp
->r_todisstd
= FALSE
;
1276 rp
->r_todisgmt
= FALSE
;
1277 dp
= ecpyalloc(timep
);
1279 ep
= dp
+ strlen(dp
) - 1;
1280 switch (lowerit(*ep
)) {
1281 case 's': /* Standard */
1282 rp
->r_todisstd
= TRUE
;
1283 rp
->r_todisgmt
= FALSE
;
1286 case 'w': /* Wall */
1287 rp
->r_todisstd
= FALSE
;
1288 rp
->r_todisgmt
= FALSE
;
1291 case 'g': /* Greenwich */
1292 case 'u': /* Universal */
1293 case 'z': /* Zulu */
1294 rp
->r_todisstd
= TRUE
;
1295 rp
->r_todisgmt
= TRUE
;
1300 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1306 lp
= byword(cp
, begin_years
);
1307 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1309 rp
->r_loyear
= INT_MIN
;
1312 rp
->r_loyear
= INT_MAX
;
1314 default: /* "cannot happen" */
1315 (void) fprintf(stderr
,
1316 _("%s: panic: Invalid l_value %d\n"),
1317 progname
, lp
->l_value
);
1318 (void) exit(EXIT_FAILURE
);
1319 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1320 error(_("invalid starting year"));
1323 if (rp
->r_loyear
< min_year_representable
)
1324 warning(_("starting year too low to be represented"));
1325 else if (rp
->r_loyear
> max_year_representable
)
1326 warning(_("starting year too high to be represented"));
1329 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1331 rp
->r_hiyear
= INT_MIN
;
1334 rp
->r_hiyear
= INT_MAX
;
1337 rp
->r_hiyear
= rp
->r_loyear
;
1339 default: /* "cannot happen" */
1340 (void) fprintf(stderr
,
1341 _("%s: panic: Invalid l_value %d\n"),
1342 progname
, lp
->l_value
);
1343 (void) exit(EXIT_FAILURE
);
1344 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1345 error(_("invalid ending year"));
1348 if (rp
->r_loyear
< min_year_representable
)
1349 warning(_("ending year too low to be represented"));
1350 else if (rp
->r_loyear
> max_year_representable
)
1351 warning(_("ending year too high to be represented"));
1353 if (rp
->r_loyear
> rp
->r_hiyear
) {
1354 error(_("starting year greater than ending year"));
1358 rp
->r_yrtype
= NULL
;
1360 if (rp
->r_loyear
== rp
->r_hiyear
) {
1361 error(_("typed single year"));
1364 rp
->r_yrtype
= ecpyalloc(typep
);
1366 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1367 min_year
= rp
->r_loyear
;
1370 ** Accept things such as:
1376 dp
= ecpyalloc(dayp
);
1377 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1378 rp
->r_dycode
= DC_DOWLEQ
;
1379 rp
->r_wday
= lp
->l_value
;
1380 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1382 if ((ep
= strchr(dp
, '<')) != 0)
1383 rp
->r_dycode
= DC_DOWLEQ
;
1384 else if ((ep
= strchr(dp
, '>')) != 0)
1385 rp
->r_dycode
= DC_DOWGEQ
;
1388 rp
->r_dycode
= DC_DOM
;
1390 if (rp
->r_dycode
!= DC_DOM
) {
1393 error(_("invalid day of month"));
1397 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1398 error(_("invalid weekday name"));
1402 rp
->r_wday
= lp
->l_value
;
1404 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1405 rp
->r_dayofmonth
<= 0 ||
1406 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1407 error(_("invalid day of month"));
1421 register long shift
;
1423 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1424 buf
[i
] = val
>> shift
;
1435 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1443 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1445 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1452 const char * const name
;
1456 static char * fullname
;
1457 static struct tzhead tzh
;
1458 zic_t ats
[TZ_MAX_TIMES
];
1459 unsigned char types
[TZ_MAX_TIMES
];
1465 (void) qsort((void *) attypes
, (size_t) timecnt
,
1466 (size_t) sizeof *attypes
, atcomp
);
1476 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1479 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1480 ++fromi
; /* handled by default rule */
1481 for ( ; fromi
< timecnt
; ++fromi
) {
1483 && ((attypes
[fromi
].at
1484 + gmtoffs
[attypes
[toi
- 1].type
])
1485 <= (attypes
[toi
- 1].at
1486 + gmtoffs
[toi
== 1 ? 0
1487 : attypes
[toi
- 2].type
]))) {
1488 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1492 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1493 attypes
[toi
++] = attypes
[fromi
];
1500 for (i
= 0; i
< timecnt
; ++i
) {
1501 ats
[i
] = attypes
[i
].at
;
1502 types
[i
] = attypes
[i
].type
;
1504 fullname
= erealloc(fullname
,
1505 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1506 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1508 ** Remove old file, if any, to snap links.
1510 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1511 const char *e
= strerror(errno
);
1513 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1514 progname
, fullname
, e
);
1515 (void) exit(EXIT_FAILURE
);
1517 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1518 if (mkdirs(fullname
) != 0)
1519 (void) exit(EXIT_FAILURE
);
1520 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1521 const char *e
= strerror(errno
);
1523 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1524 progname
, fullname
, e
);
1525 (void) exit(EXIT_FAILURE
);
1528 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1529 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1530 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1531 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1532 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1533 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1534 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1535 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1545 for (i
= 0; i
< timecnt
; ++i
) {
1548 if (ats
[i
] >= trans
[j
]) {
1549 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1552 puttzcode((long) ats
[i
], fp
);
1555 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1556 (size_t) timecnt
, fp
);
1557 for (i
= 0; i
< typecnt
; ++i
) {
1558 puttzcode((long) gmtoffs
[i
], fp
);
1559 (void) putc(isdsts
[i
], fp
);
1560 (void) putc(abbrinds
[i
], fp
);
1563 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1564 (size_t) charcnt
, fp
);
1565 for (i
= 0; i
< leapcnt
; ++i
) {
1567 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1570 if (++j
>= typecnt
) {
1576 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1580 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1581 } else puttzcode((long) trans
[i
], fp
);
1582 puttzcode((long) corr
[i
], fp
);
1584 for (i
= 0; i
< typecnt
; ++i
)
1585 (void) putc(ttisstds
[i
], fp
);
1586 for (i
= 0; i
< typecnt
; ++i
)
1587 (void) putc(ttisgmts
[i
], fp
);
1588 if (ferror(fp
) || fclose(fp
)) {
1589 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1590 progname
, fullname
);
1591 (void) exit(EXIT_FAILURE
);
1596 doabbr(abbr
, format
, letters
, isdst
)
1598 const char * const format
;
1599 const char * const letters
;
1602 if (strchr(format
, '/') == NULL
) {
1603 if (letters
== NULL
)
1604 (void) strcpy(abbr
, format
);
1605 else (void) sprintf(abbr
, format
, letters
);
1607 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1609 (void) strcpy(abbr
, format
);
1610 *strchr(abbr
, '/') = '\0';
1615 outzone(zpfirst
, zonecount
)
1616 const struct zone
* const zpfirst
;
1617 const int zonecount
;
1619 register const struct zone
* zp
;
1620 register struct rule
* rp
;
1622 register int usestart
, useuntil
;
1623 register zic_t starttime
, untiltime
;
1624 register long gmtoff
;
1625 register long stdoff
;
1627 register long startoff
;
1628 register int startttisstd
;
1629 register int startttisgmt
;
1631 char startbuf
[BUFSIZ
];
1633 INITIALIZE(untiltime
);
1634 INITIALIZE(starttime
);
1636 ** Now. . .finally. . .generate some useful data!
1642 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1643 ** for noting the need to unconditionally initialize startttisstd.
1645 startttisstd
= FALSE
;
1646 startttisgmt
= FALSE
;
1647 for (i
= 0; i
< zonecount
; ++i
) {
1649 ** A guess that may well be corrected later.
1653 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1654 useuntil
= i
< (zonecount
- 1);
1655 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1657 gmtoff
= zp
->z_gmtoff
;
1658 eat(zp
->z_filename
, zp
->z_linenum
);
1660 startoff
= zp
->z_gmtoff
;
1661 if (zp
->z_nrules
== 0) {
1662 stdoff
= zp
->z_stdoff
;
1663 doabbr(startbuf
, zp
->z_format
,
1664 (char *) NULL
, stdoff
!= 0);
1665 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1666 startbuf
, stdoff
!= 0, startttisstd
,
1669 addtt(starttime
, type
);
1671 } else if (stdoff
!= 0)
1672 addtt(min_time
, type
);
1673 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1674 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1677 ** Mark which rules to do in the current year.
1678 ** For those to do, calculate rpytime(rp, year);
1680 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1681 rp
= &zp
->z_rules
[j
];
1682 eats(zp
->z_filename
, zp
->z_linenum
,
1683 rp
->r_filename
, rp
->r_linenum
);
1684 rp
->r_todo
= year
>= rp
->r_loyear
&&
1685 year
<= rp
->r_hiyear
&&
1686 yearistype(year
, rp
->r_yrtype
);
1688 rp
->r_temp
= rpytime(rp
, year
);
1692 register zic_t jtime
, ktime
;
1693 register long offset
;
1699 ** Turn untiltime into UTC
1700 ** assuming the current gmtoff and
1703 untiltime
= zp
->z_untiltime
;
1704 if (!zp
->z_untilrule
.r_todisgmt
)
1705 untiltime
= tadd(untiltime
,
1707 if (!zp
->z_untilrule
.r_todisstd
)
1708 untiltime
= tadd(untiltime
,
1712 ** Find the rule (of those to do, if any)
1713 ** that takes effect earliest in the year.
1716 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1717 rp
= &zp
->z_rules
[j
];
1720 eats(zp
->z_filename
, zp
->z_linenum
,
1721 rp
->r_filename
, rp
->r_linenum
);
1722 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1723 if (!rp
->r_todisstd
)
1724 offset
= oadd(offset
, stdoff
);
1726 if (jtime
== min_time
||
1729 jtime
= tadd(jtime
, -offset
);
1730 if (k
< 0 || jtime
< ktime
) {
1736 break; /* go on to next year */
1737 rp
= &zp
->z_rules
[k
];
1739 if (useuntil
&& ktime
>= untiltime
)
1741 stdoff
= rp
->r_stdoff
;
1742 if (usestart
&& ktime
== starttime
)
1745 if (ktime
< starttime
) {
1746 startoff
= oadd(zp
->z_gmtoff
,
1748 doabbr(startbuf
, zp
->z_format
,
1753 if (*startbuf
== '\0' &&
1754 startoff
== oadd(zp
->z_gmtoff
,
1756 doabbr(startbuf
, zp
->z_format
,
1761 eats(zp
->z_filename
, zp
->z_linenum
,
1762 rp
->r_filename
, rp
->r_linenum
);
1763 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1765 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1766 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1767 rp
->r_todisstd
, rp
->r_todisgmt
);
1772 if (*startbuf
== '\0' &&
1773 zp
->z_format
!= NULL
&&
1774 strchr(zp
->z_format
, '%') == NULL
&&
1775 strchr(zp
->z_format
, '/') == NULL
)
1776 (void) strcpy(startbuf
, zp
->z_format
);
1777 eat(zp
->z_filename
, zp
->z_linenum
);
1778 if (*startbuf
== '\0')
1779 error(_("can't determine time zone abbreviation to use just after until time"));
1780 else addtt(starttime
,
1781 addtype(startoff
, startbuf
,
1782 startoff
!= zp
->z_gmtoff
,
1787 ** Now we may get to set starttime for the next zone line.
1790 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1791 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1792 starttime
= zp
->z_untiltime
;
1794 starttime
= tadd(starttime
, -stdoff
);
1796 starttime
= tadd(starttime
, -gmtoff
);
1799 writezone(zpfirst
->z_name
);
1803 addtt(starttime
, type
)
1804 const zic_t starttime
;
1807 if (starttime
<= min_time
||
1808 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1809 gmtoffs
[0] = gmtoffs
[type
];
1810 isdsts
[0] = isdsts
[type
];
1811 ttisstds
[0] = ttisstds
[type
];
1812 ttisgmts
[0] = ttisgmts
[type
];
1813 if (abbrinds
[type
] != 0)
1814 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1816 charcnt
= strlen(chars
) + 1;
1821 if (timecnt
>= TZ_MAX_TIMES
) {
1822 error(_("too many transitions?!"));
1823 (void) exit(EXIT_FAILURE
);
1825 attypes
[timecnt
].at
= starttime
;
1826 attypes
[timecnt
].type
= type
;
1831 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1833 const char * const abbr
;
1840 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1841 error(_("internal error - addtype called with bad isdst"));
1842 (void) exit(EXIT_FAILURE
);
1844 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1845 error(_("internal error - addtype called with bad ttisstd"));
1846 (void) exit(EXIT_FAILURE
);
1848 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1849 error(_("internal error - addtype called with bad ttisgmt"));
1850 (void) exit(EXIT_FAILURE
);
1853 ** See if there's already an entry for this zone type.
1854 ** If so, just return its index.
1856 for (i
= 0; i
< typecnt
; ++i
) {
1857 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1858 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1859 ttisstd
== ttisstds
[i
] &&
1860 ttisgmt
== ttisgmts
[i
])
1864 ** There isn't one; add a new one, unless there are already too
1867 if (typecnt
>= TZ_MAX_TYPES
) {
1868 error(_("too many local time types"));
1869 (void) exit(EXIT_FAILURE
);
1871 gmtoffs
[i
] = gmtoff
;
1873 ttisstds
[i
] = ttisstd
;
1874 ttisgmts
[i
] = ttisgmt
;
1876 for (j
= 0; j
< charcnt
; ++j
)
1877 if (strcmp(&chars
[j
], abbr
) == 0)
1887 leapadd(t
, positive
, rolling
, count
)
1895 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1896 error(_("too many leap seconds"));
1897 (void) exit(EXIT_FAILURE
);
1899 for (i
= 0; i
< leapcnt
; ++i
)
1900 if (t
<= trans
[i
]) {
1901 if (t
== trans
[i
]) {
1902 error(_("repeated leap second moment"));
1903 (void) exit(EXIT_FAILURE
);
1908 for (j
= leapcnt
; j
> i
; --j
) {
1909 trans
[j
] = trans
[j
- 1];
1910 corr
[j
] = corr
[j
- 1];
1911 roll
[j
] = roll
[j
- 1];
1914 corr
[i
] = positive
? 1L : eitol(-count
);
1917 } while (positive
&& --count
!= 0);
1924 register long last
= 0;
1927 ** propagate leap seconds forward
1929 for (i
= 0; i
< leapcnt
; ++i
) {
1930 trans
[i
] = tadd(trans
[i
], last
);
1931 last
= corr
[i
] += last
;
1936 yearistype(year
, type
)
1938 const char * const type
;
1943 if (type
== NULL
|| *type
== '\0')
1945 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1946 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1947 result
= system(buf
);
1948 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1954 error(_("Wild result from command execution"));
1955 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1956 progname
, buf
, result
);
1958 (void) exit(EXIT_FAILURE
);
1965 a
= (unsigned char) a
;
1966 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1970 ciequal(ap
, bp
) /* case-insensitive equality */
1971 register const char * ap
;
1972 register const char * bp
;
1974 while (lowerit(*ap
) == lowerit(*bp
++))
1982 register const char * abbr
;
1983 register const char * word
;
1985 if (lowerit(*abbr
) != lowerit(*word
))
1988 while (*++abbr
!= '\0')
1992 } while (lowerit(*word
++) != lowerit(*abbr
));
1996 static const struct lookup
*
1998 register const char * const word
;
1999 register const struct lookup
* const table
;
2001 register const struct lookup
* foundlp
;
2002 register const struct lookup
* lp
;
2004 if (word
== NULL
|| table
== NULL
)
2007 ** Look for exact match.
2009 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2010 if (ciequal(word
, lp
->l_word
))
2013 ** Look for inexact match.
2016 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2017 if (itsabbr(word
, lp
->l_word
)) {
2018 if (foundlp
== NULL
)
2020 else return NULL
; /* multiple inexact matches */
2030 register char ** array
;
2035 array
= (char **) (void *)
2036 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2039 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2041 if (*cp
== '\0' || *cp
== '#')
2043 array
[nsubs
++] = dp
= cp
;
2045 if ((*dp
= *cp
++) != '"')
2047 else while ((*dp
= *cp
++) != '"')
2050 else error(_("Odd number of quotation marks"));
2051 } while (*cp
!= '\0' && *cp
!= '#' &&
2052 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2053 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2057 array
[nsubs
] = NULL
;
2069 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2070 error(_("time overflow"));
2071 (void) exit(EXIT_FAILURE
);
2083 if (t1
== max_time
&& t2
> 0)
2085 if (t1
== min_time
&& t2
< 0)
2088 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2089 error(_("time overflow"));
2090 (void) exit(EXIT_FAILURE
);
2096 ** Given a rule, and a year, compute the date - in seconds since January 1,
2097 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2101 rpytime(rp
, wantedy
)
2102 register const struct rule
* const rp
;
2103 register const int wantedy
;
2105 register int y
, m
, i
;
2106 register long dayoff
; /* with a nod to Margaret O. */
2109 if (wantedy
== INT_MIN
)
2111 if (wantedy
== INT_MAX
)
2116 while (wantedy
!= y
) {
2118 i
= len_years
[isleap(y
)];
2122 i
= -len_years
[isleap(y
)];
2124 dayoff
= oadd(dayoff
, eitol(i
));
2126 while (m
!= rp
->r_month
) {
2127 i
= len_months
[isleap(y
)][m
];
2128 dayoff
= oadd(dayoff
, eitol(i
));
2131 i
= rp
->r_dayofmonth
;
2132 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2133 if (rp
->r_dycode
== DC_DOWLEQ
)
2136 error(_("use of 2/29 in non leap-year"));
2137 (void) exit(EXIT_FAILURE
);
2141 dayoff
= oadd(dayoff
, eitol(i
));
2142 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2145 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2146 wday
= eitol(EPOCH_WDAY
);
2148 ** Don't trust mod of negative numbers.
2151 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2153 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2155 wday
+= LDAYSPERWEEK
;
2157 while (wday
!= eitol(rp
->r_wday
))
2158 if (rp
->r_dycode
== DC_DOWGEQ
) {
2159 dayoff
= oadd(dayoff
, (long) 1);
2160 if (++wday
>= LDAYSPERWEEK
)
2164 dayoff
= oadd(dayoff
, (long) -1);
2166 wday
= LDAYSPERWEEK
- 1;
2169 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2171 warning(_("rule goes past start/end of month--will not work with pre-2004 versions of zic"));
2174 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
))
2176 if (dayoff
< min_time
/ SECSPERDAY
)
2178 if (dayoff
> max_time
/ SECSPERDAY
)
2180 t
= (zic_t
) dayoff
* SECSPERDAY
;
2181 return tadd(t
, rp
->r_tod
);
2186 const char * const string
;
2190 i
= strlen(string
) + 1;
2191 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2192 error(_("too many, or too long, time zone abbreviations"));
2193 (void) exit(EXIT_FAILURE
);
2195 (void) strcpy(&chars
[charcnt
], string
);
2196 charcnt
+= eitol(i
);
2201 char * const argname
;
2203 register char * name
;
2206 if (argname
== NULL
|| *argname
== '\0')
2208 cp
= name
= ecpyalloc(argname
);
2209 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2213 ** DOS drive specifier?
2215 if (isalpha((unsigned char) name
[0]) &&
2216 name
[1] == ':' && name
[2] == '\0') {
2220 #endif /* !defined unix */
2221 if (!itsdir(name
)) {
2223 ** It doesn't seem to exist, so we try to create it.
2224 ** Creation may fail because of the directory being
2225 ** created by some other multiprocessor, so we get
2226 ** to do extra checking.
2228 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2229 const char *e
= strerror(errno
);
2231 if (errno
!= EEXIST
|| !itsdir(name
)) {
2232 (void) fprintf(stderr
,
2233 _("%s: Can't create directory %s: %s\n"),
2253 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2254 (void) fprintf(stderr
,
2255 _("%s: %d did not sign extend correctly\n"),
2257 (void) exit(EXIT_FAILURE
);
2263 ** UNIX was a registered trademark of The Open Group in 2003.