1 static char elsieid
[] = "@(#)zic.c 7.113";
11 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
13 #define MKDIR_UMASK 0755
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 [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -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_CTYPE
, "");
471 (void) setlocale(LC_MESSAGES
, "");
473 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
474 #endif /* defined TEXTDOMAINDIR */
475 (void) textdomain(TZ_DOMAIN
);
476 #endif /* HAVE_GETTEXT - 0 */
478 for (i
= 1; i
< argc
; ++i
)
479 if (strcmp(argv
[i
], "--version") == 0) {
480 (void) printf("%s\n", elsieid
);
481 (void) exit(EXIT_SUCCESS
);
483 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
488 if (directory
== NULL
)
491 (void) fprintf(stderr
,
492 _("%s: More than one -d option specified\n"),
494 (void) exit(EXIT_FAILURE
);
501 (void) fprintf(stderr
,
502 _("%s: More than one -l option specified\n"),
504 (void) exit(EXIT_FAILURE
);
508 if (psxrules
== NULL
)
511 (void) fprintf(stderr
,
512 _("%s: More than one -p option specified\n"),
514 (void) exit(EXIT_FAILURE
);
518 if (yitcommand
== NULL
)
521 (void) fprintf(stderr
,
522 _("%s: More than one -y option specified\n"),
524 (void) exit(EXIT_FAILURE
);
531 (void) fprintf(stderr
,
532 _("%s: More than one -L option specified\n"),
534 (void) exit(EXIT_FAILURE
);
544 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
545 usage(); /* usage message by request */
546 if (directory
== NULL
)
548 if (yitcommand
== NULL
)
549 yitcommand
= "yearistype";
553 if (optind
< argc
&& leapsec
!= NULL
) {
558 for (i
= optind
; i
< argc
; ++i
)
561 (void) exit(EXIT_FAILURE
);
563 for (i
= 0; i
< nzones
; i
= j
) {
565 ** Find the next non-continuation zone entry.
567 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
569 outzone(&zones
[i
], j
- i
);
574 for (i
= 0; i
< nlinks
; ++i
) {
575 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
576 dolink(links
[i
].l_from
, links
[i
].l_to
);
578 if (lcltime
!= NULL
) {
579 eat("command line", 1);
580 dolink(lcltime
, TZDEFAULT
);
582 if (psxrules
!= NULL
) {
583 eat("command line", 1);
584 dolink(psxrules
, TZDEFRULES
);
586 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
590 dolink(fromfile
, tofile
)
591 const char * const fromfile
;
592 const char * const tofile
;
594 register char * fromname
;
595 register char * toname
;
597 if (fromfile
[0] == '/')
598 fromname
= ecpyalloc(fromfile
);
600 fromname
= ecpyalloc(directory
);
601 fromname
= ecatalloc(fromname
, "/");
602 fromname
= ecatalloc(fromname
, fromfile
);
604 if (tofile
[0] == '/')
605 toname
= ecpyalloc(tofile
);
607 toname
= ecpyalloc(directory
);
608 toname
= ecatalloc(toname
, "/");
609 toname
= ecatalloc(toname
, tofile
);
612 ** We get to be careful here since
613 ** there's a fair chance of root running us.
616 (void) remove(toname
);
617 if (link(fromname
, toname
) != 0) {
620 if (mkdirs(toname
) != 0)
621 (void) exit(EXIT_FAILURE
);
623 result
= link(fromname
, toname
);
624 #if (HAVE_SYMLINK - 0)
626 access(fromname
, F_OK
) == 0 &&
628 const char *s
= tofile
;
629 register char * symlinkcontents
= NULL
;
630 while ((s
= strchr(s
+1, '/')) != NULL
)
631 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
632 symlinkcontents
= ecatalloc(symlinkcontents
, fromname
);
634 result
= unlink(toname
);
635 if (result
!= 0 && errno
!= ENOENT
) {
636 const char *e
= strerror(errno
);
638 (void) fprintf(stderr
,
639 _("%s: Can't unlink %s: %s\n"),
640 progname
, toname
, e
);
641 (void) exit(EXIT_FAILURE
);
644 result
= symlink(symlinkcontents
, toname
);
646 warning(_("hard link failed, symbolic link used"));
647 ifree(symlinkcontents
);
651 const char *e
= strerror(errno
);
653 (void) fprintf(stderr
,
654 _("%s: Can't link from %s to %s: %s\n"),
655 progname
, fromname
, toname
, e
);
656 (void) exit(EXIT_FAILURE
);
664 #define INT_MAX ((int) (((unsigned)~0)>>1))
665 #endif /* !defined INT_MAX */
668 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
669 #endif /* !defined INT_MIN */
672 ** The tz file format currently allows at most 32-bit quantities.
673 ** This restriction should be removed before signed 32-bit values
674 ** wrap around in 2038, but unfortunately this will require a
675 ** change to the tz file format.
678 #define MAX_BITS_IN_FILE 32
679 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
682 setboundaries
P((void))
684 if (TYPE_SIGNED(time_t)) {
685 min_time
= ~ (time_t) 0;
686 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
687 max_time
= ~ (time_t) 0 - min_time
;
692 max_time
= 2 - sflag
;
693 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
696 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
697 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
698 min_year_representable
= min_year
;
699 max_year_representable
= max_year
;
704 const char * const name
;
706 register char * myname
;
709 myname
= ecpyalloc(name
);
710 myname
= ecatalloc(myname
, "/.");
711 accres
= access(myname
, F_OK
);
717 ** Associate sets of rules with zones.
721 ** Sort by rule name.
729 return strcmp(((const struct rule
*) cp1
)->r_name
,
730 ((const struct rule
*) cp2
)->r_name
);
736 register struct zone
* zp
;
737 register struct rule
* rp
;
738 register int base
, out
;
742 (void) qsort((void *) rules
, (size_t) nrules
,
743 (size_t) sizeof *rules
, rcomp
);
744 for (i
= 0; i
< nrules
- 1; ++i
) {
745 if (strcmp(rules
[i
].r_name
,
746 rules
[i
+ 1].r_name
) != 0)
748 if (strcmp(rules
[i
].r_filename
,
749 rules
[i
+ 1].r_filename
) == 0)
751 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
752 warning(_("same rule name in multiple files"));
753 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
754 warning(_("same rule name in multiple files"));
755 for (j
= i
+ 2; j
< nrules
; ++j
) {
756 if (strcmp(rules
[i
].r_name
,
757 rules
[j
].r_name
) != 0)
759 if (strcmp(rules
[i
].r_filename
,
760 rules
[j
].r_filename
) == 0)
762 if (strcmp(rules
[i
+ 1].r_filename
,
763 rules
[j
].r_filename
) == 0)
770 for (i
= 0; i
< nzones
; ++i
) {
775 for (base
= 0; base
< nrules
; base
= out
) {
777 for (out
= base
+ 1; out
< nrules
; ++out
)
778 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
780 for (i
= 0; i
< nzones
; ++i
) {
782 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
785 zp
->z_nrules
= out
- base
;
788 for (i
= 0; i
< nzones
; ++i
) {
790 if (zp
->z_nrules
== 0) {
792 ** Maybe we have a local standard time offset.
794 eat(zp
->z_filename
, zp
->z_linenum
);
795 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
798 ** Note, though, that if there's no rule,
799 ** a '%s' in the format is a bad thing.
801 if (strchr(zp
->z_format
, '%') != 0)
802 error(_("%s in ruleless zone"));
806 (void) exit(EXIT_FAILURE
);
814 register char ** fields
;
816 register const struct lookup
* lp
;
817 register int nfields
;
818 register int wantcont
;
822 if (strcmp(name
, "-") == 0) {
823 name
= _("standard input");
825 } else if ((fp
= fopen(name
, "r")) == NULL
) {
826 const char *e
= strerror(errno
);
828 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
830 (void) exit(EXIT_FAILURE
);
833 for (num
= 1; ; ++num
) {
835 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
837 cp
= strchr(buf
, '\n');
839 error(_("line too long"));
840 (void) exit(EXIT_FAILURE
);
843 fields
= getfields(buf
);
845 while (fields
[nfields
] != NULL
) {
848 if (strcmp(fields
[nfields
], "-") == 0)
849 fields
[nfields
] = &nada
;
854 } else if (wantcont
) {
855 wantcont
= inzcont(fields
, nfields
);
857 lp
= byword(fields
[0], line_codes
);
859 error(_("input line of unknown type"));
860 else switch ((int) (lp
->l_value
)) {
862 inrule(fields
, nfields
);
866 wantcont
= inzone(fields
, nfields
);
869 inlink(fields
, nfields
);
874 (void) fprintf(stderr
,
875 _("%s: Leap line in non leap seconds file %s\n"),
877 else inleap(fields
, nfields
);
880 default: /* "cannot happen" */
881 (void) fprintf(stderr
,
882 _("%s: panic: Invalid l_value %d\n"),
883 progname
, lp
->l_value
);
884 (void) exit(EXIT_FAILURE
);
887 ifree((char *) fields
);
890 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
892 (void) exit(EXIT_FAILURE
);
894 if (fp
!= stdin
&& fclose(fp
)) {
895 const char *e
= strerror(errno
);
897 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
898 progname
, filename
, e
);
899 (void) exit(EXIT_FAILURE
);
902 error(_("expected continuation line not found"));
906 ** Convert a string of one of the forms
907 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
908 ** into a number of seconds.
909 ** A null string maps to zero.
910 ** Call error with errstring and return zero on errors.
914 gethms(string
, errstring
, signable
)
916 const char * const errstring
;
919 int hh
, mm
, ss
, sign
;
921 if (string
== NULL
|| *string
== '\0')
925 else if (*string
== '-') {
929 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
931 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
933 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
934 &hh
, &mm
, &ss
) != 3) {
938 if ((hh
< 0 || hh
>= HOURSPERDAY
||
939 mm
< 0 || mm
>= MINSPERHOUR
||
940 ss
< 0 || ss
> SECSPERMIN
) &&
941 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
946 (eitol(hh
* MINSPERHOUR
+ mm
) *
947 eitol(SECSPERMIN
) + eitol(ss
));
951 inrule(fields
, nfields
)
952 register char ** const fields
;
955 static struct rule r
;
957 if (nfields
!= RULE_FIELDS
) {
958 error(_("wrong number of fields on Rule line"));
961 if (*fields
[RF_NAME
] == '\0') {
962 error(_("nameless rule"));
965 r
.r_filename
= filename
;
966 r
.r_linenum
= linenum
;
967 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
968 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
969 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
970 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
971 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
972 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
973 (int) ((nrules
+ 1) * sizeof *rules
));
978 inzone(fields
, nfields
)
979 register char ** const fields
;
985 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
986 error(_("wrong number of fields on Zone line"));
989 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
990 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
992 _("\"Zone %s\" line and -l option are mutually exclusive"),
997 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
998 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
1000 _("\"Zone %s\" line and -p option are mutually exclusive"),
1005 for (i
= 0; i
< nzones
; ++i
)
1006 if (zones
[i
].z_name
!= NULL
&&
1007 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
1008 buf
= erealloc(buf
, (int) (132 +
1009 strlen(fields
[ZF_NAME
]) +
1010 strlen(zones
[i
].z_filename
)));
1012 _("duplicate zone name %s (file \"%s\", line %d)"),
1014 zones
[i
].z_filename
,
1015 zones
[i
].z_linenum
);
1019 return inzsub(fields
, nfields
, FALSE
);
1023 inzcont(fields
, nfields
)
1024 register char ** const fields
;
1027 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1028 error(_("wrong number of fields on Zone continuation line"));
1031 return inzsub(fields
, nfields
, TRUE
);
1035 inzsub(fields
, nfields
, iscont
)
1036 register char ** const fields
;
1041 static struct zone z
;
1042 register int i_gmtoff
, i_rule
, i_format
;
1043 register int i_untilyear
, i_untilmonth
;
1044 register int i_untilday
, i_untiltime
;
1045 register int hasuntil
;
1048 i_gmtoff
= ZFC_GMTOFF
;
1050 i_format
= ZFC_FORMAT
;
1051 i_untilyear
= ZFC_TILYEAR
;
1052 i_untilmonth
= ZFC_TILMONTH
;
1053 i_untilday
= ZFC_TILDAY
;
1054 i_untiltime
= ZFC_TILTIME
;
1057 i_gmtoff
= ZF_GMTOFF
;
1059 i_format
= ZF_FORMAT
;
1060 i_untilyear
= ZF_TILYEAR
;
1061 i_untilmonth
= ZF_TILMONTH
;
1062 i_untilday
= ZF_TILDAY
;
1063 i_untiltime
= ZF_TILTIME
;
1064 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1066 z
.z_filename
= filename
;
1067 z
.z_linenum
= linenum
;
1068 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1069 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1070 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1071 error(_("invalid abbreviation format"));
1075 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1076 z
.z_format
= ecpyalloc(fields
[i_format
]);
1077 hasuntil
= nfields
> i_untilyear
;
1079 z
.z_untilrule
.r_filename
= filename
;
1080 z
.z_untilrule
.r_linenum
= linenum
;
1081 rulesub(&z
.z_untilrule
,
1082 fields
[i_untilyear
],
1085 (nfields
> i_untilmonth
) ?
1086 fields
[i_untilmonth
] : "Jan",
1087 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1088 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1089 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1090 z
.z_untilrule
.r_loyear
);
1091 if (iscont
&& nzones
> 0 &&
1092 z
.z_untiltime
> min_time
&&
1093 z
.z_untiltime
< max_time
&&
1094 zones
[nzones
- 1].z_untiltime
> min_time
&&
1095 zones
[nzones
- 1].z_untiltime
< max_time
&&
1096 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1097 error(_("Zone continuation line end time is not after end time of previous line"));
1101 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1102 (int) ((nzones
+ 1) * sizeof *zones
));
1103 zones
[nzones
++] = z
;
1105 ** If there was an UNTIL field on this line,
1106 ** there's more information about the zone on the next line.
1112 inleap(fields
, nfields
)
1113 register char ** const fields
;
1116 register const char * cp
;
1117 register const struct lookup
* lp
;
1119 int year
, month
, day
;
1123 if (nfields
!= LEAP_FIELDS
) {
1124 error(_("wrong number of fields on Leap line"));
1128 cp
= fields
[LP_YEAR
];
1129 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1133 error(_("invalid leaping year"));
1139 i
= len_years
[isleap(j
)];
1143 i
= -len_years
[isleap(j
)];
1145 dayoff
= oadd(dayoff
, eitol(i
));
1147 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1148 error(_("invalid month name"));
1151 month
= lp
->l_value
;
1153 while (j
!= month
) {
1154 i
= len_months
[isleap(year
)][j
];
1155 dayoff
= oadd(dayoff
, eitol(i
));
1158 cp
= fields
[LP_DAY
];
1159 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1160 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1161 error(_("invalid day of month"));
1164 dayoff
= oadd(dayoff
, eitol(day
- 1));
1165 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1166 error(_("time before zero"));
1169 if (dayoff
< min_time
/ SECSPERDAY
) {
1170 error(_("time too small"));
1173 if (dayoff
> max_time
/ SECSPERDAY
) {
1174 error(_("time too large"));
1177 t
= (time_t) dayoff
* SECSPERDAY
;
1178 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1179 cp
= fields
[LP_CORR
];
1181 register int positive
;
1184 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1187 } else if (strcmp(cp
, "--") == 0) {
1190 } else if (strcmp(cp
, "+") == 0) {
1193 } else if (strcmp(cp
, "++") == 0) {
1197 error(_("illegal CORRECTION field on Leap line"));
1200 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1201 error(_("illegal Rolling/Stationary field on Leap line"));
1204 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1209 inlink(fields
, nfields
)
1210 register char ** const fields
;
1215 if (nfields
!= LINK_FIELDS
) {
1216 error(_("wrong number of fields on Link line"));
1219 if (*fields
[LF_FROM
] == '\0') {
1220 error(_("blank FROM field on Link line"));
1223 if (*fields
[LF_TO
] == '\0') {
1224 error(_("blank TO field on Link line"));
1227 l
.l_filename
= filename
;
1228 l
.l_linenum
= linenum
;
1229 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1230 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1231 links
= (struct link
*) (void *) erealloc((char *) links
,
1232 (int) ((nlinks
+ 1) * sizeof *links
));
1233 links
[nlinks
++] = l
;
1237 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1238 register struct rule
* const rp
;
1239 const char * const loyearp
;
1240 const char * const hiyearp
;
1241 const char * const typep
;
1242 const char * const monthp
;
1243 const char * const dayp
;
1244 const char * const timep
;
1246 register const struct lookup
* lp
;
1247 register const char * cp
;
1251 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1252 error(_("invalid month name"));
1255 rp
->r_month
= lp
->l_value
;
1256 rp
->r_todisstd
= FALSE
;
1257 rp
->r_todisgmt
= FALSE
;
1258 dp
= ecpyalloc(timep
);
1260 ep
= dp
+ strlen(dp
) - 1;
1261 switch (lowerit(*ep
)) {
1262 case 's': /* Standard */
1263 rp
->r_todisstd
= TRUE
;
1264 rp
->r_todisgmt
= FALSE
;
1267 case 'w': /* Wall */
1268 rp
->r_todisstd
= FALSE
;
1269 rp
->r_todisgmt
= FALSE
;
1272 case 'g': /* Greenwich */
1273 case 'u': /* Universal */
1274 case 'z': /* Zulu */
1275 rp
->r_todisstd
= TRUE
;
1276 rp
->r_todisgmt
= TRUE
;
1281 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1287 lp
= byword(cp
, begin_years
);
1288 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1290 rp
->r_loyear
= INT_MIN
;
1293 rp
->r_loyear
= INT_MAX
;
1295 default: /* "cannot happen" */
1296 (void) fprintf(stderr
,
1297 _("%s: panic: Invalid l_value %d\n"),
1298 progname
, lp
->l_value
);
1299 (void) exit(EXIT_FAILURE
);
1300 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1301 error(_("invalid starting year"));
1304 if (rp
->r_loyear
< min_year_representable
)
1305 warning(_("starting year too low to be represented"));
1306 else if (rp
->r_loyear
> max_year_representable
)
1307 warning(_("starting year too high to be represented"));
1310 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1312 rp
->r_hiyear
= INT_MIN
;
1315 rp
->r_hiyear
= INT_MAX
;
1318 rp
->r_hiyear
= rp
->r_loyear
;
1320 default: /* "cannot happen" */
1321 (void) fprintf(stderr
,
1322 _("%s: panic: Invalid l_value %d\n"),
1323 progname
, lp
->l_value
);
1324 (void) exit(EXIT_FAILURE
);
1325 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1326 error(_("invalid ending year"));
1329 if (rp
->r_loyear
< min_year_representable
)
1330 warning(_("ending year too low to be represented"));
1331 else if (rp
->r_loyear
> max_year_representable
)
1332 warning(_("ending year too high to be represented"));
1334 if (rp
->r_loyear
> rp
->r_hiyear
) {
1335 error(_("starting year greater than ending year"));
1339 rp
->r_yrtype
= NULL
;
1341 if (rp
->r_loyear
== rp
->r_hiyear
) {
1342 error(_("typed single year"));
1345 rp
->r_yrtype
= ecpyalloc(typep
);
1347 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1348 min_year
= rp
->r_loyear
;
1351 ** Accept things such as:
1357 dp
= ecpyalloc(dayp
);
1358 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1359 rp
->r_dycode
= DC_DOWLEQ
;
1360 rp
->r_wday
= lp
->l_value
;
1361 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1363 if ((ep
= strchr(dp
, '<')) != 0)
1364 rp
->r_dycode
= DC_DOWLEQ
;
1365 else if ((ep
= strchr(dp
, '>')) != 0)
1366 rp
->r_dycode
= DC_DOWGEQ
;
1369 rp
->r_dycode
= DC_DOM
;
1371 if (rp
->r_dycode
!= DC_DOM
) {
1374 error(_("invalid day of month"));
1378 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1379 error(_("invalid weekday name"));
1383 rp
->r_wday
= lp
->l_value
;
1385 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1386 rp
->r_dayofmonth
<= 0 ||
1387 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1388 error(_("invalid day of month"));
1402 register long shift
;
1404 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1405 buf
[i
] = val
>> shift
;
1416 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1424 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1426 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1433 const char * const name
;
1437 static char * fullname
;
1438 static struct tzhead tzh
;
1439 time_t ats
[TZ_MAX_TIMES
];
1440 unsigned char types
[TZ_MAX_TIMES
];
1446 (void) qsort((void *) attypes
, (size_t) timecnt
,
1447 (size_t) sizeof *attypes
, atcomp
);
1457 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1460 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1461 ++fromi
; /* handled by default rule */
1462 for ( ; fromi
< timecnt
; ++fromi
) {
1464 && ((attypes
[fromi
].at
1465 + gmtoffs
[attypes
[toi
- 1].type
])
1466 <= (attypes
[toi
- 1].at
1467 + gmtoffs
[toi
== 1 ? 0
1468 : attypes
[toi
- 2].type
]))) {
1469 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1473 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1474 attypes
[toi
++] = attypes
[fromi
];
1481 for (i
= 0; i
< timecnt
; ++i
) {
1482 ats
[i
] = attypes
[i
].at
;
1483 types
[i
] = attypes
[i
].type
;
1485 fullname
= erealloc(fullname
,
1486 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1487 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1489 ** Remove old file, if any, to snap links.
1491 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1492 const char *e
= strerror(errno
);
1494 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1495 progname
, fullname
, e
);
1496 (void) exit(EXIT_FAILURE
);
1498 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1499 if (mkdirs(fullname
) != 0)
1500 (void) exit(EXIT_FAILURE
);
1501 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1502 const char *e
= strerror(errno
);
1504 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1505 progname
, fullname
, e
);
1506 (void) exit(EXIT_FAILURE
);
1509 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1510 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1511 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1512 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1513 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1514 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1515 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1516 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1526 for (i
= 0; i
< timecnt
; ++i
) {
1529 if (ats
[i
] >= trans
[j
]) {
1530 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1533 puttzcode((long) ats
[i
], fp
);
1536 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1537 (size_t) timecnt
, fp
);
1538 for (i
= 0; i
< typecnt
; ++i
) {
1539 puttzcode((long) gmtoffs
[i
], fp
);
1540 (void) putc(isdsts
[i
], fp
);
1541 (void) putc(abbrinds
[i
], fp
);
1544 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1545 (size_t) charcnt
, fp
);
1546 for (i
= 0; i
< leapcnt
; ++i
) {
1548 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1551 if (++j
>= typecnt
) {
1557 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1561 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1562 } else puttzcode((long) trans
[i
], fp
);
1563 puttzcode((long) corr
[i
], fp
);
1565 for (i
= 0; i
< typecnt
; ++i
)
1566 (void) putc(ttisstds
[i
], fp
);
1567 for (i
= 0; i
< typecnt
; ++i
)
1568 (void) putc(ttisgmts
[i
], fp
);
1569 if (ferror(fp
) || fclose(fp
)) {
1570 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1571 progname
, fullname
);
1572 (void) exit(EXIT_FAILURE
);
1577 doabbr(abbr
, format
, letters
, isdst
)
1579 const char * const format
;
1580 const char * const letters
;
1583 if (strchr(format
, '/') == NULL
) {
1584 if (letters
== NULL
)
1585 (void) strcpy(abbr
, format
);
1586 else (void) sprintf(abbr
, format
, letters
);
1588 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1590 (void) strcpy(abbr
, format
);
1591 *strchr(abbr
, '/') = '\0';
1596 outzone(zpfirst
, zonecount
)
1597 const struct zone
* const zpfirst
;
1598 const int zonecount
;
1600 register const struct zone
* zp
;
1601 register struct rule
* rp
;
1603 register int usestart
, useuntil
;
1604 register time_t starttime
, untiltime
;
1605 register long gmtoff
;
1606 register long stdoff
;
1608 register long startoff
;
1609 register int startttisstd
;
1610 register int startttisgmt
;
1612 char startbuf
[BUFSIZ
];
1614 INITIALIZE(untiltime
);
1615 INITIALIZE(starttime
);
1617 ** Now. . .finally. . .generate some useful data!
1623 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1624 ** for noting the need to unconditionally initialize startttisstd.
1626 startttisstd
= FALSE
;
1627 startttisgmt
= FALSE
;
1628 for (i
= 0; i
< zonecount
; ++i
) {
1630 ** A guess that may well be corrected later.
1634 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1635 useuntil
= i
< (zonecount
- 1);
1636 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1638 gmtoff
= zp
->z_gmtoff
;
1639 eat(zp
->z_filename
, zp
->z_linenum
);
1641 startoff
= zp
->z_gmtoff
;
1642 if (zp
->z_nrules
== 0) {
1643 stdoff
= zp
->z_stdoff
;
1644 doabbr(startbuf
, zp
->z_format
,
1645 (char *) NULL
, stdoff
!= 0);
1646 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1647 startbuf
, stdoff
!= 0, startttisstd
,
1650 addtt(starttime
, type
);
1652 } else if (stdoff
!= 0)
1653 addtt(min_time
, type
);
1654 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1655 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1658 ** Mark which rules to do in the current year.
1659 ** For those to do, calculate rpytime(rp, year);
1661 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1662 rp
= &zp
->z_rules
[j
];
1663 eats(zp
->z_filename
, zp
->z_linenum
,
1664 rp
->r_filename
, rp
->r_linenum
);
1665 rp
->r_todo
= year
>= rp
->r_loyear
&&
1666 year
<= rp
->r_hiyear
&&
1667 yearistype(year
, rp
->r_yrtype
);
1669 rp
->r_temp
= rpytime(rp
, year
);
1673 register time_t jtime
, ktime
;
1674 register long offset
;
1680 ** Turn untiltime into UTC
1681 ** assuming the current gmtoff and
1684 untiltime
= zp
->z_untiltime
;
1685 if (!zp
->z_untilrule
.r_todisgmt
)
1686 untiltime
= tadd(untiltime
,
1688 if (!zp
->z_untilrule
.r_todisstd
)
1689 untiltime
= tadd(untiltime
,
1693 ** Find the rule (of those to do, if any)
1694 ** that takes effect earliest in the year.
1697 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1698 rp
= &zp
->z_rules
[j
];
1701 eats(zp
->z_filename
, zp
->z_linenum
,
1702 rp
->r_filename
, rp
->r_linenum
);
1703 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1704 if (!rp
->r_todisstd
)
1705 offset
= oadd(offset
, stdoff
);
1707 if (jtime
== min_time
||
1710 jtime
= tadd(jtime
, -offset
);
1711 if (k
< 0 || jtime
< ktime
) {
1717 break; /* go on to next year */
1718 rp
= &zp
->z_rules
[k
];
1720 if (useuntil
&& ktime
>= untiltime
)
1722 stdoff
= rp
->r_stdoff
;
1723 if (usestart
&& ktime
== starttime
)
1726 if (ktime
< starttime
) {
1727 startoff
= oadd(zp
->z_gmtoff
,
1729 doabbr(startbuf
, zp
->z_format
,
1734 if (*startbuf
== '\0' &&
1735 startoff
== oadd(zp
->z_gmtoff
,
1737 doabbr(startbuf
, zp
->z_format
,
1742 eats(zp
->z_filename
, zp
->z_linenum
,
1743 rp
->r_filename
, rp
->r_linenum
);
1744 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1746 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1747 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1748 rp
->r_todisstd
, rp
->r_todisgmt
);
1753 if (*startbuf
== '\0' &&
1754 zp
->z_format
!= NULL
&&
1755 strchr(zp
->z_format
, '%') == NULL
&&
1756 strchr(zp
->z_format
, '/') == NULL
)
1757 (void) strcpy(startbuf
, zp
->z_format
);
1758 eat(zp
->z_filename
, zp
->z_linenum
);
1759 if (*startbuf
== '\0')
1760 error(_("can't determine time zone abbreviation to use just after until time"));
1761 else addtt(starttime
,
1762 addtype(startoff
, startbuf
,
1763 startoff
!= zp
->z_gmtoff
,
1768 ** Now we may get to set starttime for the next zone line.
1771 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1772 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1773 starttime
= zp
->z_untiltime
;
1775 starttime
= tadd(starttime
, -stdoff
);
1777 starttime
= tadd(starttime
, -gmtoff
);
1780 writezone(zpfirst
->z_name
);
1784 addtt(starttime
, type
)
1785 const time_t starttime
;
1788 if (starttime
<= min_time
||
1789 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1790 gmtoffs
[0] = gmtoffs
[type
];
1791 isdsts
[0] = isdsts
[type
];
1792 ttisstds
[0] = ttisstds
[type
];
1793 ttisgmts
[0] = ttisgmts
[type
];
1794 if (abbrinds
[type
] != 0)
1795 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1797 charcnt
= strlen(chars
) + 1;
1802 if (timecnt
>= TZ_MAX_TIMES
) {
1803 error(_("too many transitions?!"));
1804 (void) exit(EXIT_FAILURE
);
1806 attypes
[timecnt
].at
= starttime
;
1807 attypes
[timecnt
].type
= type
;
1812 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1814 const char * const abbr
;
1821 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1822 error(_("internal error - addtype called with bad isdst"));
1823 (void) exit(EXIT_FAILURE
);
1825 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1826 error(_("internal error - addtype called with bad ttisstd"));
1827 (void) exit(EXIT_FAILURE
);
1829 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1830 error(_("internal error - addtype called with bad ttisgmt"));
1831 (void) exit(EXIT_FAILURE
);
1834 ** See if there's already an entry for this zone type.
1835 ** If so, just return its index.
1837 for (i
= 0; i
< typecnt
; ++i
) {
1838 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1839 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1840 ttisstd
== ttisstds
[i
] &&
1841 ttisgmt
== ttisgmts
[i
])
1845 ** There isn't one; add a new one, unless there are already too
1848 if (typecnt
>= TZ_MAX_TYPES
) {
1849 error(_("too many local time types"));
1850 (void) exit(EXIT_FAILURE
);
1852 gmtoffs
[i
] = gmtoff
;
1854 ttisstds
[i
] = ttisstd
;
1855 ttisgmts
[i
] = ttisgmt
;
1857 for (j
= 0; j
< charcnt
; ++j
)
1858 if (strcmp(&chars
[j
], abbr
) == 0)
1868 leapadd(t
, positive
, rolling
, count
)
1876 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1877 error(_("too many leap seconds"));
1878 (void) exit(EXIT_FAILURE
);
1880 for (i
= 0; i
< leapcnt
; ++i
)
1881 if (t
<= trans
[i
]) {
1882 if (t
== trans
[i
]) {
1883 error(_("repeated leap second moment"));
1884 (void) exit(EXIT_FAILURE
);
1889 for (j
= leapcnt
; j
> i
; --j
) {
1890 trans
[j
] = trans
[j
- 1];
1891 corr
[j
] = corr
[j
- 1];
1892 roll
[j
] = roll
[j
- 1];
1895 corr
[i
] = positive
? 1L : eitol(-count
);
1898 } while (positive
&& --count
!= 0);
1905 register long last
= 0;
1908 ** propagate leap seconds forward
1910 for (i
= 0; i
< leapcnt
; ++i
) {
1911 trans
[i
] = tadd(trans
[i
], last
);
1912 last
= corr
[i
] += last
;
1917 yearistype(year
, type
)
1919 const char * const type
;
1924 if (type
== NULL
|| *type
== '\0')
1926 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1927 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1928 result
= system(buf
);
1929 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1935 error(_("Wild result from command execution"));
1936 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1937 progname
, buf
, result
);
1939 (void) exit(EXIT_FAILURE
);
1946 a
= (unsigned char) a
;
1947 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1951 ciequal(ap
, bp
) /* case-insensitive equality */
1952 register const char * ap
;
1953 register const char * bp
;
1955 while (lowerit(*ap
) == lowerit(*bp
++))
1963 register const char * abbr
;
1964 register const char * word
;
1966 if (lowerit(*abbr
) != lowerit(*word
))
1969 while (*++abbr
!= '\0')
1973 } while (lowerit(*word
++) != lowerit(*abbr
));
1977 static const struct lookup
*
1979 register const char * const word
;
1980 register const struct lookup
* const table
;
1982 register const struct lookup
* foundlp
;
1983 register const struct lookup
* lp
;
1985 if (word
== NULL
|| table
== NULL
)
1988 ** Look for exact match.
1990 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1991 if (ciequal(word
, lp
->l_word
))
1994 ** Look for inexact match.
1997 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1998 if (itsabbr(word
, lp
->l_word
)) {
1999 if (foundlp
== NULL
)
2001 else return NULL
; /* multiple inexact matches */
2011 register char ** array
;
2016 array
= (char **) (void *)
2017 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2020 while (isascii(*cp
) && isspace((unsigned char) *cp
))
2022 if (*cp
== '\0' || *cp
== '#')
2024 array
[nsubs
++] = dp
= cp
;
2026 if ((*dp
= *cp
++) != '"')
2028 else while ((*dp
= *cp
++) != '"')
2031 else error(_("Odd number of quotation marks"));
2032 } while (*cp
!= '\0' && *cp
!= '#' &&
2033 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2034 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2038 array
[nsubs
] = NULL
;
2050 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2051 error(_("time overflow"));
2052 (void) exit(EXIT_FAILURE
);
2064 if (t1
== max_time
&& t2
> 0)
2066 if (t1
== min_time
&& t2
< 0)
2069 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2070 error(_("time overflow"));
2071 (void) exit(EXIT_FAILURE
);
2077 ** Given a rule, and a year, compute the date - in seconds since January 1,
2078 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2082 rpytime(rp
, wantedy
)
2083 register const struct rule
* const rp
;
2084 register const int wantedy
;
2086 register int y
, m
, i
;
2087 register long dayoff
; /* with a nod to Margaret O. */
2090 if (wantedy
== INT_MIN
)
2092 if (wantedy
== INT_MAX
)
2097 while (wantedy
!= y
) {
2099 i
= len_years
[isleap(y
)];
2103 i
= -len_years
[isleap(y
)];
2105 dayoff
= oadd(dayoff
, eitol(i
));
2107 while (m
!= rp
->r_month
) {
2108 i
= len_months
[isleap(y
)][m
];
2109 dayoff
= oadd(dayoff
, eitol(i
));
2112 i
= rp
->r_dayofmonth
;
2113 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2114 if (rp
->r_dycode
== DC_DOWLEQ
)
2117 error(_("use of 2/29 in non leap-year"));
2118 (void) exit(EXIT_FAILURE
);
2122 dayoff
= oadd(dayoff
, eitol(i
));
2123 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2126 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2127 wday
= eitol(EPOCH_WDAY
);
2129 ** Don't trust mod of negative numbers.
2132 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2134 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2136 wday
+= LDAYSPERWEEK
;
2138 while (wday
!= eitol(rp
->r_wday
))
2139 if (rp
->r_dycode
== DC_DOWGEQ
) {
2140 dayoff
= oadd(dayoff
, (long) 1);
2141 if (++wday
>= LDAYSPERWEEK
)
2145 dayoff
= oadd(dayoff
, (long) -1);
2147 wday
= LDAYSPERWEEK
- 1;
2150 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2151 error(_("no day in month matches rule"));
2152 (void) exit(EXIT_FAILURE
);
2155 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2157 if (dayoff
< min_time
/ SECSPERDAY
)
2159 if (dayoff
> max_time
/ SECSPERDAY
)
2161 t
= (time_t) dayoff
* SECSPERDAY
;
2162 if (t
> 0 && max_time
- t
< rp
->r_tod
)
2164 return tadd(t
, rp
->r_tod
);
2169 const char * const string
;
2173 i
= strlen(string
) + 1;
2174 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2175 error(_("too many, or too long, time zone abbreviations"));
2176 (void) exit(EXIT_FAILURE
);
2178 (void) strcpy(&chars
[charcnt
], string
);
2179 charcnt
+= eitol(i
);
2184 char * const argname
;
2186 register char * name
;
2189 if (argname
== NULL
|| *argname
== '\0')
2191 cp
= name
= ecpyalloc(argname
);
2192 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2196 ** DOS drive specifier?
2198 if (isalpha((unsigned char) name
[0]) &&
2199 name
[1] == ':' && name
[2] == '\0') {
2203 #endif /* !defined unix */
2204 if (!itsdir(name
)) {
2206 ** It doesn't seem to exist, so we try to create it.
2207 ** Creation may fail because of the directory being
2208 ** created by some other multiprocessor, so we get
2209 ** to do extra checking.
2211 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2212 const char *e
= strerror(errno
);
2214 if (errno
!= EEXIST
|| !itsdir(name
)) {
2215 (void) fprintf(stderr
,
2216 _("%s: Can't create directory %s: %s\n"),
2236 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2237 (void) fprintf(stderr
,
2238 _("%s: %d did not sign extend correctly\n"),
2240 (void) exit(EXIT_FAILURE
);
2246 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.