3 static char elsieid
[] = "@(#)zic.c 7.83";
4 #endif /* !defined NOID */
5 #endif /* !defined lint */
11 #include "sys/stat.h" /* for umask manifest constants */
12 #endif /* defined unix */
15 ** On some ancient hosts, predicates like `isspace(C)' are defined
16 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
17 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
18 ** Neither the C Standard nor Posix require that `isascii' exist.
19 ** For portability, we check both ancient and modern requirements.
20 ** If isascii is not defined, the isascii check succeeds trivially.
28 const char * r_filename
;
32 int r_loyear
; /* for example, 1986 */
33 int r_hiyear
; /* for example, 1986 */
34 const char * r_yrtype
;
36 int r_month
; /* 0..11 */
38 int r_dycode
; /* see below */
42 long r_tod
; /* time from midnight */
43 int r_todisstd
; /* above is standard time if TRUE */
44 /* or wall clock time if FALSE */
45 int r_todisgmt
; /* above is GMT if TRUE */
46 /* or local time if FALSE */
47 long r_stdoff
; /* offset from standard time */
48 const char * r_abbrvar
; /* variable part of abbreviation */
50 int r_todo
; /* a rule to do (used in outzone) */
51 time_t r_temp
; /* used in outzone */
55 ** r_dycode r_dayofmonth r_wday
58 #define DC_DOM 0 /* 1..31 */ /* unused */
59 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
60 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
63 const char * z_filename
;
69 const char * z_format
;
73 struct rule
* z_rules
;
76 struct rule z_untilrule
;
80 extern int getopt
P((int argc
, char * const argv
[],
81 const char * options
));
82 extern int link
P((const char * fromname
, const char * toname
));
86 static void addtt
P((time_t starttime
, int type
));
87 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
88 int ttisstd
, int ttisgmt
));
89 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
90 static void adjleap
P((void));
91 static void associate
P((void));
92 static int ciequal
P((const char * ap
, const char * bp
));
93 static void convert
P((long val
, char * buf
));
94 static void dolink
P((const char * fromfile
, const char * tofile
));
95 static void doabbr
P((char * abbr
, const char * format
,
96 const char * letters
, int isdst
));
97 static void eat
P((const char * name
, int num
));
98 static void eats
P((const char * name
, int num
,
99 const char * rname
, int rnum
));
100 static long eitol
P((int i
));
101 static void error
P((const char * message
));
102 static char ** getfields
P((char * buf
));
103 static long gethms
P((const char * string
, const char * errstrng
,
105 static void infile
P((const char * filename
));
106 static void inleap
P((char ** fields
, int nfields
));
107 static void inlink
P((char ** fields
, int nfields
));
108 static void inrule
P((char ** fields
, int nfields
));
109 static int inzcont
P((char ** fields
, int nfields
));
110 static int inzone
P((char ** fields
, int nfields
));
111 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
112 static int itsabbr
P((const char * abbr
, const char * word
));
113 static int itsdir
P((const char * name
));
114 static int lowerit
P((int c
));
115 static char * memcheck
P((char * tocheck
));
116 static int mkdirs
P((char * filename
));
117 static void newabbr
P((const char * abbr
));
118 static long oadd
P((long t1
, long t2
));
119 static void outzone
P((const struct zone
* zp
, int ntzones
));
120 static void puttzcode
P((long code
, FILE * fp
));
121 static int rcomp
P((const void * leftp
, const void * rightp
));
122 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
123 static void rulesub
P((struct rule
* rp
,
124 const char * loyearp
, const char * hiyearp
,
125 const char * typep
, const char * monthp
,
126 const char * dayp
, const char * timep
));
127 static void setboundaries
P((void));
128 static time_t tadd
P((time_t t1
, long t2
));
129 static void usage
P((void));
130 static void writezone
P((const char * name
));
131 static int yearistype
P((int year
, const char * type
));
133 #if !(HAVE_STRERROR - 0)
134 static char * strerror
P((int));
135 #endif /* !(HAVE_STRERROR - 0) */
139 static const char * filename
;
142 static time_t max_time
;
144 static time_t min_time
;
147 static const char * rfilename
;
149 static const char * progname
;
163 ** Which fields are which on a Zone line.
171 #define ZF_TILMONTH 6
174 #define ZONE_MINFIELDS 5
175 #define ZONE_MAXFIELDS 9
178 ** Which fields are which on a Zone continuation line.
184 #define ZFC_TILYEAR 3
185 #define ZFC_TILMONTH 4
187 #define ZFC_TILTIME 6
188 #define ZONEC_MINFIELDS 3
189 #define ZONEC_MAXFIELDS 7
192 ** Which files are which on a Rule line.
204 #define RULE_FIELDS 10
207 ** Which fields are which on a Link line.
212 #define LINK_FIELDS 3
215 ** Which fields are which on a Leap line.
224 #define LEAP_FIELDS 7
234 static struct rule
* rules
;
235 static int nrules
; /* number of rules */
237 static struct zone
* zones
;
238 static int nzones
; /* number of zones */
241 const char * l_filename
;
247 static struct link
* links
;
255 static struct lookup
const * byword
P((const char * string
,
256 const struct lookup
* lp
));
258 static struct lookup
const line_codes
[] = {
266 static struct lookup
const mon_names
[] = {
267 { "January", TM_JANUARY
},
268 { "February", TM_FEBRUARY
},
269 { "March", TM_MARCH
},
270 { "April", TM_APRIL
},
274 { "August", TM_AUGUST
},
275 { "September", TM_SEPTEMBER
},
276 { "October", TM_OCTOBER
},
277 { "November", TM_NOVEMBER
},
278 { "December", TM_DECEMBER
},
282 static struct lookup
const wday_names
[] = {
283 { "Sunday", TM_SUNDAY
},
284 { "Monday", TM_MONDAY
},
285 { "Tuesday", TM_TUESDAY
},
286 { "Wednesday", TM_WEDNESDAY
},
287 { "Thursday", TM_THURSDAY
},
288 { "Friday", TM_FRIDAY
},
289 { "Saturday", TM_SATURDAY
},
293 static struct lookup
const lasts
[] = {
294 { "last-Sunday", TM_SUNDAY
},
295 { "last-Monday", TM_MONDAY
},
296 { "last-Tuesday", TM_TUESDAY
},
297 { "last-Wednesday", TM_WEDNESDAY
},
298 { "last-Thursday", TM_THURSDAY
},
299 { "last-Friday", TM_FRIDAY
},
300 { "last-Saturday", TM_SATURDAY
},
304 static struct lookup
const begin_years
[] = {
305 { "minimum", YR_MINIMUM
},
306 { "maximum", YR_MAXIMUM
},
310 static struct lookup
const end_years
[] = {
311 { "minimum", YR_MINIMUM
},
312 { "maximum", YR_MAXIMUM
},
317 static struct lookup
const leap_types
[] = {
319 { "Stationary", FALSE
},
323 static const int len_months
[2][MONSPERYEAR
] = {
324 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
325 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
328 static const int len_years
[2] = {
329 DAYSPERNYEAR
, DAYSPERLYEAR
332 static struct attype
{
335 } attypes
[TZ_MAX_TIMES
];
336 static long gmtoffs
[TZ_MAX_TYPES
];
337 static char isdsts
[TZ_MAX_TYPES
];
338 static unsigned char abbrinds
[TZ_MAX_TYPES
];
339 static char ttisstds
[TZ_MAX_TYPES
];
340 static char ttisgmts
[TZ_MAX_TYPES
];
341 static char chars
[TZ_MAX_CHARS
];
342 static time_t trans
[TZ_MAX_LEAPS
];
343 static long corr
[TZ_MAX_LEAPS
];
344 static char roll
[TZ_MAX_LEAPS
];
347 ** Memory allocation.
355 const char *e
= strerror(errno
);
357 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
359 (void) exit(EXIT_FAILURE
);
364 #define emalloc(size) memcheck(imalloc(size))
365 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
366 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
367 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
373 #if !(HAVE_STRERROR - 0)
378 extern char * sys_errlist
[];
381 return (errnum
> 0 && errnum
<= sys_nerr
) ?
382 sys_errlist
[errnum
] : "Unknown system error";
384 #endif /* !(HAVE_STRERROR - 0) */
387 eats(name
, num
, rname
, rnum
)
388 const char * const name
;
390 const char * const rname
;
401 const char * const name
;
404 eats(name
, num
, (char *) NULL
, -1);
409 const char * const string
;
412 ** Match the format of "cc" to allow sh users to
413 ** zic ... 2>&1 | error -t "*" -v
416 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
417 filename
, linenum
, string
);
418 if (rfilename
!= NULL
)
419 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
420 rfilename
, rlinenum
);
421 (void) fprintf(stderr
, "\n");
427 const char * const string
;
431 cp
= ecpyalloc("warning: ");
432 cp
= ecatalloc(cp
, string
);
441 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
443 (void) exit(EXIT_FAILURE
);
446 static const char * psxrules
;
447 static const char * lcltime
;
448 static const char * directory
;
449 static const char * leapsec
;
450 static const char * yitcommand
;
451 static int sflag
= FALSE
;
463 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
464 #endif /* defined unix */
466 (void) setlocale(LC_MESSAGES
, "");
468 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
469 #endif /* defined TEXTDOMAINDIR */
470 (void) textdomain(TZ_DOMAIN
);
471 #endif /* HAVE_GETTEXT - 0 */
473 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
478 if (directory
== NULL
)
481 (void) fprintf(stderr
,
482 _("%s: More than one -d option specified\n"),
484 (void) exit(EXIT_FAILURE
);
491 (void) fprintf(stderr
,
492 _("%s: More than one -l option specified\n"),
494 (void) exit(EXIT_FAILURE
);
498 if (psxrules
== NULL
)
501 (void) fprintf(stderr
,
502 _("%s: More than one -p option specified\n"),
504 (void) exit(EXIT_FAILURE
);
508 if (yitcommand
== NULL
)
511 (void) fprintf(stderr
,
512 _("%s: More than one -y option specified\n"),
514 (void) exit(EXIT_FAILURE
);
521 (void) fprintf(stderr
,
522 _("%s: More than one -L option specified\n"),
524 (void) exit(EXIT_FAILURE
);
534 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
535 usage(); /* usage message by request */
536 if (directory
== NULL
)
538 if (yitcommand
== NULL
)
539 yitcommand
= "yearistype";
543 if (optind
< argc
&& leapsec
!= NULL
) {
548 for (i
= optind
; i
< argc
; ++i
)
551 (void) exit(EXIT_FAILURE
);
553 for (i
= 0; i
< nzones
; i
= j
) {
555 ** Find the next non-continuation zone entry.
557 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
559 outzone(&zones
[i
], j
- i
);
564 for (i
= 0; i
< nlinks
; ++i
)
565 dolink(links
[i
].l_from
, links
[i
].l_to
);
567 dolink(lcltime
, TZDEFAULT
);
568 if (psxrules
!= NULL
)
569 dolink(psxrules
, TZDEFRULES
);
570 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
574 dolink(fromfile
, tofile
)
575 const char * const fromfile
;
576 const char * const tofile
;
578 register char * fromname
;
579 register char * toname
;
581 if (fromfile
[0] == '/')
582 fromname
= ecpyalloc(fromfile
);
584 fromname
= ecpyalloc(directory
);
585 fromname
= ecatalloc(fromname
, "/");
586 fromname
= ecatalloc(fromname
, fromfile
);
588 if (tofile
[0] == '/')
589 toname
= ecpyalloc(tofile
);
591 toname
= ecpyalloc(directory
);
592 toname
= ecatalloc(toname
, "/");
593 toname
= ecatalloc(toname
, tofile
);
596 ** We get to be careful here since
597 ** there's a fair chance of root running us.
600 (void) remove(toname
);
601 if (link(fromname
, toname
) != 0) {
602 if (mkdirs(toname
) != 0)
603 (void) exit(EXIT_FAILURE
);
604 if (link(fromname
, toname
) != 0) {
605 const char *e
= strerror(errno
);
607 (void) fprintf(stderr
,
608 _("%s: Can't link from %s to %s: %s\n"),
609 progname
, fromname
, toname
, e
);
610 (void) exit(EXIT_FAILURE
);
618 #define INT_MAX ((int) (((unsigned)~0)>>1))
619 #endif /* !defined INT_MAX */
622 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
623 #endif /* !defined INT_MIN */
626 ** The tz file format currently allows at most 32-bit quantities.
627 ** This restriction should be removed before signed 32-bit values
628 ** wrap around in 2038, but unfortunately this will require a
629 ** change to the tz file format.
632 #define MAX_BITS_IN_FILE 32
633 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
636 setboundaries
P((void))
638 if (TYPE_SIGNED(time_t)) {
639 min_time
= ~ (time_t) 0;
640 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
641 max_time
= ~ (time_t) 0 - min_time
;
646 max_time
= 2 - sflag
;
647 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
650 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
651 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
656 const char * const name
;
658 register char * myname
;
661 myname
= ecpyalloc(name
);
662 myname
= ecatalloc(myname
, "/.");
663 accres
= access(myname
, F_OK
);
669 ** Associate sets of rules with zones.
673 ** Sort by rule name.
681 return strcmp(((const struct rule
*) cp1
)->r_name
,
682 ((const struct rule
*) cp2
)->r_name
);
688 register struct zone
* zp
;
689 register struct rule
* rp
;
690 register int base
, out
;
694 (void) qsort((void *) rules
, (size_t) nrules
,
695 (size_t) sizeof *rules
, rcomp
);
696 for (i
= 0; i
< nrules
- 1; ++i
) {
697 if (strcmp(rules
[i
].r_name
,
698 rules
[i
+ 1].r_name
) != 0)
700 if (strcmp(rules
[i
].r_filename
,
701 rules
[i
+ 1].r_filename
) == 0)
703 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
704 warning(_("same rule name in multiple files"));
705 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
706 warning(_("same rule name in multiple files"));
707 for (j
= i
+ 2; j
< nrules
; ++j
) {
708 if (strcmp(rules
[i
].r_name
,
709 rules
[j
].r_name
) != 0)
711 if (strcmp(rules
[i
].r_filename
,
712 rules
[j
].r_filename
) == 0)
714 if (strcmp(rules
[i
+ 1].r_filename
,
715 rules
[j
].r_filename
) == 0)
722 for (i
= 0; i
< nzones
; ++i
) {
727 for (base
= 0; base
< nrules
; base
= out
) {
729 for (out
= base
+ 1; out
< nrules
; ++out
)
730 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
732 for (i
= 0; i
< nzones
; ++i
) {
734 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
737 zp
->z_nrules
= out
- base
;
740 for (i
= 0; i
< nzones
; ++i
) {
742 if (zp
->z_nrules
== 0) {
744 ** Maybe we have a local standard time offset.
746 eat(zp
->z_filename
, zp
->z_linenum
);
747 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
750 ** Note, though, that if there's no rule,
751 ** a '%s' in the format is a bad thing.
753 if (strchr(zp
->z_format
, '%') != 0)
754 error(_("%s in ruleless zone"));
758 (void) exit(EXIT_FAILURE
);
766 register char ** fields
;
768 register const struct lookup
* lp
;
769 register int nfields
;
770 register int wantcont
;
774 if (strcmp(name
, "-") == 0) {
775 name
= _("standard input");
777 } else if ((fp
= fopen(name
, "r")) == NULL
) {
778 const char *e
= strerror(errno
);
780 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
782 (void) exit(EXIT_FAILURE
);
785 for (num
= 1; ; ++num
) {
787 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
789 cp
= strchr(buf
, '\n');
791 error(_("line too long"));
792 (void) exit(EXIT_FAILURE
);
795 fields
= getfields(buf
);
797 while (fields
[nfields
] != NULL
) {
800 if (strcmp(fields
[nfields
], "-") == 0)
801 fields
[nfields
] = &nada
;
806 } else if (wantcont
) {
807 wantcont
= inzcont(fields
, nfields
);
809 lp
= byword(fields
[0], line_codes
);
811 error(_("input line of unknown type"));
812 else switch ((int) (lp
->l_value
)) {
814 inrule(fields
, nfields
);
818 wantcont
= inzone(fields
, nfields
);
821 inlink(fields
, nfields
);
826 (void) fprintf(stderr
,
827 _("%s: Leap line in non leap seconds file %s\n"),
829 else inleap(fields
, nfields
);
832 default: /* "cannot happen" */
833 (void) fprintf(stderr
,
834 _("%s: panic: Invalid l_value %d\n"),
835 progname
, lp
->l_value
);
836 (void) exit(EXIT_FAILURE
);
839 ifree((char *) fields
);
842 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
844 (void) exit(EXIT_FAILURE
);
846 if (fp
!= stdin
&& fclose(fp
)) {
847 const char *e
= strerror(errno
);
849 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
850 progname
, filename
, e
);
851 (void) exit(EXIT_FAILURE
);
854 error(_("expected continuation line not found"));
858 ** Convert a string of one of the forms
859 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
860 ** into a number of seconds.
861 ** A null string maps to zero.
862 ** Call error with errstring and return zero on errors.
866 gethms(string
, errstring
, signable
)
868 const char * const errstring
;
871 int hh
, mm
, ss
, sign
;
873 if (string
== NULL
|| *string
== '\0')
877 else if (*string
== '-') {
881 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
883 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
885 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
886 &hh
, &mm
, &ss
) != 3) {
890 if (hh
< 0 || hh
>= HOURSPERDAY
||
891 mm
< 0 || mm
>= MINSPERHOUR
||
892 ss
< 0 || ss
> SECSPERMIN
) {
897 (eitol(hh
* MINSPERHOUR
+ mm
) *
898 eitol(SECSPERMIN
) + eitol(ss
));
902 inrule(fields
, nfields
)
903 register char ** const fields
;
906 static struct rule r
;
908 if (nfields
!= RULE_FIELDS
) {
909 error(_("wrong number of fields on Rule line"));
912 if (*fields
[RF_NAME
] == '\0') {
913 error(_("nameless rule"));
916 r
.r_filename
= filename
;
917 r
.r_linenum
= linenum
;
918 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
919 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
920 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
921 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
922 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
923 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
924 (int) ((nrules
+ 1) * sizeof *rules
));
929 inzone(fields
, nfields
)
930 register char ** const fields
;
936 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
937 error(_("wrong number of fields on Zone line"));
940 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
941 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
943 _("\"Zone %s\" line and -l option are mutually exclusive"),
948 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
949 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
951 _("\"Zone %s\" line and -p option are mutually exclusive"),
956 for (i
= 0; i
< nzones
; ++i
)
957 if (zones
[i
].z_name
!= NULL
&&
958 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
959 buf
= erealloc(buf
, (int) (132 +
960 strlen(fields
[ZF_NAME
]) +
961 strlen(zones
[i
].z_filename
)));
963 _("duplicate zone name %s (file \"%s\", line %d)"),
970 return inzsub(fields
, nfields
, FALSE
);
974 inzcont(fields
, nfields
)
975 register char ** const fields
;
978 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
979 error(_("wrong number of fields on Zone continuation line"));
982 return inzsub(fields
, nfields
, TRUE
);
986 inzsub(fields
, nfields
, iscont
)
987 register char ** const fields
;
992 static struct zone z
;
993 register int i_gmtoff
, i_rule
, i_format
;
994 register int i_untilyear
, i_untilmonth
;
995 register int i_untilday
, i_untiltime
;
996 register int hasuntil
;
999 i_gmtoff
= ZFC_GMTOFF
;
1001 i_format
= ZFC_FORMAT
;
1002 i_untilyear
= ZFC_TILYEAR
;
1003 i_untilmonth
= ZFC_TILMONTH
;
1004 i_untilday
= ZFC_TILDAY
;
1005 i_untiltime
= ZFC_TILTIME
;
1008 i_gmtoff
= ZF_GMTOFF
;
1010 i_format
= ZF_FORMAT
;
1011 i_untilyear
= ZF_TILYEAR
;
1012 i_untilmonth
= ZF_TILMONTH
;
1013 i_untilday
= ZF_TILDAY
;
1014 i_untiltime
= ZF_TILTIME
;
1015 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1017 z
.z_filename
= filename
;
1018 z
.z_linenum
= linenum
;
1019 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid GMT offset"), TRUE
);
1020 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1021 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1022 error(_("invalid abbreviation format"));
1026 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1027 z
.z_format
= ecpyalloc(fields
[i_format
]);
1028 hasuntil
= nfields
> i_untilyear
;
1030 z
.z_untilrule
.r_filename
= filename
;
1031 z
.z_untilrule
.r_linenum
= linenum
;
1032 rulesub(&z
.z_untilrule
,
1033 fields
[i_untilyear
],
1036 (nfields
> i_untilmonth
) ?
1037 fields
[i_untilmonth
] : "Jan",
1038 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1039 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1040 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1041 z
.z_untilrule
.r_loyear
);
1042 if (iscont
&& nzones
> 0 &&
1043 z
.z_untiltime
> min_time
&&
1044 z
.z_untiltime
< max_time
&&
1045 zones
[nzones
- 1].z_untiltime
> min_time
&&
1046 zones
[nzones
- 1].z_untiltime
< max_time
&&
1047 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1048 error(_("Zone continuation line end time is not after end time of previous line"));
1052 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1053 (int) ((nzones
+ 1) * sizeof *zones
));
1054 zones
[nzones
++] = z
;
1056 ** If there was an UNTIL field on this line,
1057 ** there's more information about the zone on the next line.
1063 inleap(fields
, nfields
)
1064 register char ** const fields
;
1067 register const char * cp
;
1068 register const struct lookup
* lp
;
1070 int year
, month
, day
;
1074 if (nfields
!= LEAP_FIELDS
) {
1075 error(_("wrong number of fields on Leap line"));
1079 cp
= fields
[LP_YEAR
];
1080 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1084 error(_("invalid leaping year"));
1090 i
= len_years
[isleap(j
)];
1094 i
= -len_years
[isleap(j
)];
1096 dayoff
= oadd(dayoff
, eitol(i
));
1098 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1099 error(_("invalid month name"));
1102 month
= lp
->l_value
;
1104 while (j
!= month
) {
1105 i
= len_months
[isleap(year
)][j
];
1106 dayoff
= oadd(dayoff
, eitol(i
));
1109 cp
= fields
[LP_DAY
];
1110 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1111 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1112 error(_("invalid day of month"));
1115 dayoff
= oadd(dayoff
, eitol(day
- 1));
1116 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1117 error(_("time before zero"));
1120 t
= (time_t) dayoff
* SECSPERDAY
;
1122 ** Cheap overflow check.
1124 if (t
/ SECSPERDAY
!= dayoff
) {
1125 error(_("time overflow"));
1128 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1129 cp
= fields
[LP_CORR
];
1131 register int positive
;
1134 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1137 } else if (strcmp(cp
, "--") == 0) {
1140 } else if (strcmp(cp
, "+") == 0) {
1143 } else if (strcmp(cp
, "++") == 0) {
1147 error(_("illegal CORRECTION field on Leap line"));
1150 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1151 error(_("illegal Rolling/Stationary field on Leap line"));
1154 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1159 inlink(fields
, nfields
)
1160 register char ** const fields
;
1165 if (nfields
!= LINK_FIELDS
) {
1166 error(_("wrong number of fields on Link line"));
1169 if (*fields
[LF_FROM
] == '\0') {
1170 error(_("blank FROM field on Link line"));
1173 if (*fields
[LF_TO
] == '\0') {
1174 error(_("blank TO field on Link line"));
1177 l
.l_filename
= filename
;
1178 l
.l_linenum
= linenum
;
1179 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1180 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1181 links
= (struct link
*) (void *) erealloc((char *) links
,
1182 (int) ((nlinks
+ 1) * sizeof *links
));
1183 links
[nlinks
++] = l
;
1187 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1188 register struct rule
* const rp
;
1189 const char * const loyearp
;
1190 const char * const hiyearp
;
1191 const char * const typep
;
1192 const char * const monthp
;
1193 const char * const dayp
;
1194 const char * const timep
;
1196 register const struct lookup
* lp
;
1197 register const char * cp
;
1201 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1202 error(_("invalid month name"));
1205 rp
->r_month
= lp
->l_value
;
1206 rp
->r_todisstd
= FALSE
;
1207 rp
->r_todisgmt
= FALSE
;
1208 dp
= ecpyalloc(timep
);
1210 ep
= dp
+ strlen(dp
) - 1;
1211 switch (lowerit(*ep
)) {
1212 case 's': /* Standard */
1213 rp
->r_todisstd
= TRUE
;
1214 rp
->r_todisgmt
= FALSE
;
1217 case 'w': /* Wall */
1218 rp
->r_todisstd
= FALSE
;
1219 rp
->r_todisgmt
= FALSE
;
1221 case 'g': /* Greenwich */
1222 case 'u': /* Universal */
1223 case 'z': /* Zulu */
1224 rp
->r_todisstd
= TRUE
;
1225 rp
->r_todisgmt
= TRUE
;
1230 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1236 lp
= byword(cp
, begin_years
);
1237 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1239 rp
->r_loyear
= INT_MIN
;
1242 rp
->r_loyear
= INT_MAX
;
1244 default: /* "cannot happen" */
1245 (void) fprintf(stderr
,
1246 _("%s: panic: Invalid l_value %d\n"),
1247 progname
, lp
->l_value
);
1248 (void) exit(EXIT_FAILURE
);
1249 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1250 error(_("invalid starting year"));
1254 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1256 rp
->r_hiyear
= INT_MIN
;
1259 rp
->r_hiyear
= INT_MAX
;
1262 rp
->r_hiyear
= rp
->r_loyear
;
1264 default: /* "cannot happen" */
1265 (void) fprintf(stderr
,
1266 _("%s: panic: Invalid l_value %d\n"),
1267 progname
, lp
->l_value
);
1268 (void) exit(EXIT_FAILURE
);
1269 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1270 error(_("invalid ending year"));
1273 if (rp
->r_loyear
> rp
->r_hiyear
) {
1274 error(_("starting year greater than ending year"));
1278 rp
->r_yrtype
= NULL
;
1280 if (rp
->r_loyear
== rp
->r_hiyear
) {
1281 error(_("typed single year"));
1284 rp
->r_yrtype
= ecpyalloc(typep
);
1286 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1287 min_year
= rp
->r_loyear
;
1290 ** Accept things such as:
1296 dp
= ecpyalloc(dayp
);
1297 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1298 rp
->r_dycode
= DC_DOWLEQ
;
1299 rp
->r_wday
= lp
->l_value
;
1300 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1302 if ((ep
= strchr(dp
, '<')) != 0)
1303 rp
->r_dycode
= DC_DOWLEQ
;
1304 else if ((ep
= strchr(dp
, '>')) != 0)
1305 rp
->r_dycode
= DC_DOWGEQ
;
1308 rp
->r_dycode
= DC_DOM
;
1310 if (rp
->r_dycode
!= DC_DOM
) {
1313 error(_("invalid day of month"));
1317 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1318 error(_("invalid weekday name"));
1322 rp
->r_wday
= lp
->l_value
;
1324 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1325 rp
->r_dayofmonth
<= 0 ||
1326 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1327 error(_("invalid day of month"));
1341 register long shift
;
1343 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1344 buf
[i
] = val
>> shift
;
1355 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1363 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1365 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1372 const char * const name
;
1376 static char * fullname
;
1377 static struct tzhead tzh
;
1378 time_t ats
[TZ_MAX_TIMES
];
1379 unsigned char types
[TZ_MAX_TIMES
];
1385 (void) qsort((void *) attypes
, (size_t) timecnt
,
1386 (size_t) sizeof *attypes
, atcomp
);
1396 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1399 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1400 ++fromi
; /* handled by default rule */
1401 for ( ; fromi
< timecnt
; ++fromi
) {
1403 && ((attypes
[fromi
].at
1404 + gmtoffs
[attypes
[toi
- 1].type
])
1405 <= (attypes
[toi
- 1].at
1406 + gmtoffs
[toi
== 1 ? 0
1407 : attypes
[toi
- 2].type
]))) {
1408 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1412 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1413 attypes
[toi
++] = attypes
[fromi
];
1420 for (i
= 0; i
< timecnt
; ++i
) {
1421 ats
[i
] = attypes
[i
].at
;
1422 types
[i
] = attypes
[i
].type
;
1424 fullname
= erealloc(fullname
,
1425 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1426 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1428 ** Remove old file, if any, to snap links.
1430 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1431 const char *e
= strerror(errno
);
1433 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1434 progname
, fullname
, e
);
1435 (void) exit(EXIT_FAILURE
);
1437 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1438 if (mkdirs(fullname
) != 0)
1439 (void) exit(EXIT_FAILURE
);
1440 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1441 const char *e
= strerror(errno
);
1443 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1444 progname
, fullname
, e
);
1445 (void) exit(EXIT_FAILURE
);
1448 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1449 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1450 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1451 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1452 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1453 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1454 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1463 for (i
= 0; i
< timecnt
; ++i
) {
1466 if (ats
[i
] >= trans
[j
]) {
1467 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1470 puttzcode((long) ats
[i
], fp
);
1473 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1474 (size_t) timecnt
, fp
);
1475 for (i
= 0; i
< typecnt
; ++i
) {
1476 puttzcode((long) gmtoffs
[i
], fp
);
1477 (void) putc(isdsts
[i
], fp
);
1478 (void) putc(abbrinds
[i
], fp
);
1481 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1482 (size_t) charcnt
, fp
);
1483 for (i
= 0; i
< leapcnt
; ++i
) {
1485 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1488 if (++j
>= typecnt
) {
1494 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1498 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1499 } else puttzcode((long) trans
[i
], fp
);
1500 puttzcode((long) corr
[i
], fp
);
1502 for (i
= 0; i
< typecnt
; ++i
)
1503 (void) putc(ttisstds
[i
], fp
);
1504 for (i
= 0; i
< typecnt
; ++i
)
1505 (void) putc(ttisgmts
[i
], fp
);
1506 if (ferror(fp
) || fclose(fp
)) {
1507 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1508 progname
, fullname
);
1509 (void) exit(EXIT_FAILURE
);
1514 doabbr(abbr
, format
, letters
, isdst
)
1516 const char * const format
;
1517 const char * const letters
;
1520 if (strchr(format
, '/') == NULL
) {
1521 if (letters
== NULL
)
1522 (void) strcpy(abbr
, format
);
1523 else (void) sprintf(abbr
, format
, letters
);
1525 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1527 (void) strcpy(abbr
, format
);
1528 *strchr(abbr
, '/') = '\0';
1533 outzone(zpfirst
, zonecount
)
1534 const struct zone
* const zpfirst
;
1535 const int zonecount
;
1537 register const struct zone
* zp
;
1538 register struct rule
* rp
;
1540 register int usestart
, useuntil
;
1541 register time_t starttime
, untiltime
;
1542 register long gmtoff
;
1543 register long stdoff
;
1545 register long startoff
;
1546 register int startttisstd
;
1547 register int startttisgmt
;
1549 char startbuf
[BUFSIZ
];
1551 INITIALIZE(untiltime
);
1552 INITIALIZE(starttime
);
1554 ** Now. . .finally. . .generate some useful data!
1560 ** A guess that may well be corrected later.
1564 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1565 ** for noting the need to unconditionally initialize startttisstd.
1567 startttisstd
= FALSE
;
1568 startttisgmt
= FALSE
;
1569 for (i
= 0; i
< zonecount
; ++i
) {
1571 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1572 useuntil
= i
< (zonecount
- 1);
1573 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1575 gmtoff
= zp
->z_gmtoff
;
1576 eat(zp
->z_filename
, zp
->z_linenum
);
1578 startoff
= zp
->z_gmtoff
;
1579 if (zp
->z_nrules
== 0) {
1580 stdoff
= zp
->z_stdoff
;
1581 doabbr(startbuf
, zp
->z_format
,
1582 (char *) NULL
, stdoff
!= 0);
1583 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1584 startbuf
, stdoff
!= 0, startttisstd
,
1587 addtt(starttime
, type
);
1590 else if (stdoff
!= 0)
1591 addtt(min_time
, type
);
1592 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1593 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1596 ** Mark which rules to do in the current year.
1597 ** For those to do, calculate rpytime(rp, year);
1599 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1600 rp
= &zp
->z_rules
[j
];
1601 eats(zp
->z_filename
, zp
->z_linenum
,
1602 rp
->r_filename
, rp
->r_linenum
);
1603 rp
->r_todo
= year
>= rp
->r_loyear
&&
1604 year
<= rp
->r_hiyear
&&
1605 yearistype(year
, rp
->r_yrtype
);
1607 rp
->r_temp
= rpytime(rp
, year
);
1611 register time_t jtime
, ktime
;
1612 register long offset
;
1618 ** Turn untiltime into GMT
1619 ** assuming the current gmtoff and
1622 untiltime
= zp
->z_untiltime
;
1623 if (!zp
->z_untilrule
.r_todisgmt
)
1624 untiltime
= tadd(untiltime
,
1626 if (!zp
->z_untilrule
.r_todisstd
)
1627 untiltime
= tadd(untiltime
,
1631 ** Find the rule (of those to do, if any)
1632 ** that takes effect earliest in the year.
1635 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1636 rp
= &zp
->z_rules
[j
];
1639 eats(zp
->z_filename
, zp
->z_linenum
,
1640 rp
->r_filename
, rp
->r_linenum
);
1641 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1642 if (!rp
->r_todisstd
)
1643 offset
= oadd(offset
, stdoff
);
1645 if (jtime
== min_time
||
1648 jtime
= tadd(jtime
, -offset
);
1649 if (k
< 0 || jtime
< ktime
) {
1655 break; /* go on to next year */
1656 rp
= &zp
->z_rules
[k
];
1658 if (useuntil
&& ktime
>= untiltime
)
1660 stdoff
= rp
->r_stdoff
;
1661 if (usestart
&& ktime
== starttime
)
1664 if (ktime
< starttime
) {
1665 startoff
= oadd(zp
->z_gmtoff
,
1667 doabbr(startbuf
, zp
->z_format
,
1672 if (*startbuf
== '\0' &&
1673 startoff
== oadd(zp
->z_gmtoff
,
1675 doabbr(startbuf
, zp
->z_format
,
1680 eats(zp
->z_filename
, zp
->z_linenum
,
1681 rp
->r_filename
, rp
->r_linenum
);
1682 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1684 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1685 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1686 rp
->r_todisstd
, rp
->r_todisgmt
);
1691 if (*startbuf
== '\0' &&
1692 zp
->z_format
!= NULL
&&
1693 strchr(zp
->z_format
, '%') == NULL
&&
1694 strchr(zp
->z_format
, '/') == NULL
)
1695 (void) strcpy(startbuf
, zp
->z_format
);
1696 eat(zp
->z_filename
, zp
->z_linenum
);
1697 if (*startbuf
== '\0')
1698 error(_("can't determine time zone abbreviation to use just after until time"));
1699 else addtt(starttime
,
1700 addtype(startoff
, startbuf
,
1701 startoff
!= zp
->z_gmtoff
,
1706 ** Now we may get to set starttime for the next zone line.
1709 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1710 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1711 starttime
= zp
->z_untiltime
;
1713 starttime
= tadd(starttime
, -stdoff
);
1715 starttime
= tadd(starttime
, -gmtoff
);
1718 writezone(zpfirst
->z_name
);
1722 addtt(starttime
, type
)
1723 const time_t starttime
;
1726 if (starttime
<= min_time
||
1727 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1728 gmtoffs
[0] = gmtoffs
[type
];
1729 isdsts
[0] = isdsts
[type
];
1730 ttisstds
[0] = ttisstds
[type
];
1731 ttisgmts
[0] = ttisgmts
[type
];
1732 if (abbrinds
[type
] != 0)
1733 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1735 charcnt
= strlen(chars
) + 1;
1740 if (timecnt
>= TZ_MAX_TIMES
) {
1741 error(_("too many transitions?!"));
1742 (void) exit(EXIT_FAILURE
);
1744 attypes
[timecnt
].at
= starttime
;
1745 attypes
[timecnt
].type
= type
;
1750 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1752 const char * const abbr
;
1759 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1760 error(_("internal error - addtype called with bad isdst"));
1761 (void) exit(EXIT_FAILURE
);
1763 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1764 error(_("internal error - addtype called with bad ttisstd"));
1765 (void) exit(EXIT_FAILURE
);
1767 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1768 error(_("internal error - addtype called with bad ttisgmt"));
1769 (void) exit(EXIT_FAILURE
);
1772 ** See if there's already an entry for this zone type.
1773 ** If so, just return its index.
1775 for (i
= 0; i
< typecnt
; ++i
) {
1776 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1777 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1778 ttisstd
== ttisstds
[i
] &&
1779 ttisgmt
== ttisgmts
[i
])
1783 ** There isn't one; add a new one, unless there are already too
1786 if (typecnt
>= TZ_MAX_TYPES
) {
1787 error(_("too many local time types"));
1788 (void) exit(EXIT_FAILURE
);
1790 gmtoffs
[i
] = gmtoff
;
1792 ttisstds
[i
] = ttisstd
;
1793 ttisgmts
[i
] = ttisgmt
;
1795 for (j
= 0; j
< charcnt
; ++j
)
1796 if (strcmp(&chars
[j
], abbr
) == 0)
1806 leapadd(t
, positive
, rolling
, count
)
1814 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1815 error(_("too many leap seconds"));
1816 (void) exit(EXIT_FAILURE
);
1818 for (i
= 0; i
< leapcnt
; ++i
)
1819 if (t
<= trans
[i
]) {
1820 if (t
== trans
[i
]) {
1821 error(_("repeated leap second moment"));
1822 (void) exit(EXIT_FAILURE
);
1827 for (j
= leapcnt
; j
> i
; --j
) {
1828 trans
[j
] = trans
[j
- 1];
1829 corr
[j
] = corr
[j
- 1];
1830 roll
[j
] = roll
[j
- 1];
1833 corr
[i
] = positive
? 1L : eitol(-count
);
1836 } while (positive
&& --count
!= 0);
1843 register long last
= 0;
1846 ** propagate leap seconds forward
1848 for (i
= 0; i
< leapcnt
; ++i
) {
1849 trans
[i
] = tadd(trans
[i
], last
);
1850 last
= corr
[i
] += last
;
1855 yearistype(year
, type
)
1857 const char * const type
;
1862 if (type
== NULL
|| *type
== '\0')
1864 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1865 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1866 result
= system(buf
);
1869 if (result
== (1 << 8))
1871 error(_("Wild result from command execution"));
1872 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1873 progname
, buf
, result
);
1875 (void) exit(EXIT_FAILURE
);
1882 a
= (unsigned char) a
;
1883 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1887 ciequal(ap
, bp
) /* case-insensitive equality */
1888 register const char * ap
;
1889 register const char * bp
;
1891 while (lowerit(*ap
) == lowerit(*bp
++))
1899 register const char * abbr
;
1900 register const char * word
;
1902 if (lowerit(*abbr
) != lowerit(*word
))
1905 while (*++abbr
!= '\0')
1909 } while (lowerit(*word
++) != lowerit(*abbr
));
1913 static const struct lookup
*
1915 register const char * const word
;
1916 register const struct lookup
* const table
;
1918 register const struct lookup
* foundlp
;
1919 register const struct lookup
* lp
;
1921 if (word
== NULL
|| table
== NULL
)
1924 ** Look for exact match.
1926 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1927 if (ciequal(word
, lp
->l_word
))
1930 ** Look for inexact match.
1933 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1934 if (itsabbr(word
, lp
->l_word
))
1935 if (foundlp
== NULL
)
1937 else return NULL
; /* multiple inexact matches */
1946 register char ** array
;
1951 array
= (char **) (void *)
1952 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1955 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1957 if (*cp
== '\0' || *cp
== '#')
1959 array
[nsubs
++] = dp
= cp
;
1961 if ((*dp
= *cp
++) != '"')
1963 else while ((*dp
= *cp
++) != '"')
1966 else error(_("Odd number of quotation marks"));
1967 } while (*cp
!= '\0' && *cp
!= '#' &&
1968 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1969 if (isascii(*cp
) && isspace((unsigned char) *cp
))
1973 array
[nsubs
] = NULL
;
1985 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1986 error(_("time overflow"));
1987 (void) exit(EXIT_FAILURE
);
1999 if (t1
== max_time
&& t2
> 0)
2001 if (t1
== min_time
&& t2
< 0)
2004 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2005 error(_("time overflow"));
2006 (void) exit(EXIT_FAILURE
);
2012 ** Given a rule, and a year, compute the date - in seconds since January 1,
2013 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2017 rpytime(rp
, wantedy
)
2018 register const struct rule
* const rp
;
2019 register const int wantedy
;
2021 register int y
, m
, i
;
2022 register long dayoff
; /* with a nod to Margaret O. */
2025 if (wantedy
== INT_MIN
)
2027 if (wantedy
== INT_MAX
)
2032 while (wantedy
!= y
) {
2034 i
= len_years
[isleap(y
)];
2038 i
= -len_years
[isleap(y
)];
2040 dayoff
= oadd(dayoff
, eitol(i
));
2042 while (m
!= rp
->r_month
) {
2043 i
= len_months
[isleap(y
)][m
];
2044 dayoff
= oadd(dayoff
, eitol(i
));
2047 i
= rp
->r_dayofmonth
;
2048 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2049 if (rp
->r_dycode
== DC_DOWLEQ
)
2052 error(_("use of 2/29 in non leap-year"));
2053 (void) exit(EXIT_FAILURE
);
2057 dayoff
= oadd(dayoff
, eitol(i
));
2058 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2061 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2062 wday
= eitol(EPOCH_WDAY
);
2064 ** Don't trust mod of negative numbers.
2067 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2069 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2071 wday
+= LDAYSPERWEEK
;
2073 while (wday
!= eitol(rp
->r_wday
))
2074 if (rp
->r_dycode
== DC_DOWGEQ
) {
2075 dayoff
= oadd(dayoff
, (long) 1);
2076 if (++wday
>= LDAYSPERWEEK
)
2080 dayoff
= oadd(dayoff
, (long) -1);
2082 wday
= LDAYSPERWEEK
- 1;
2085 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2086 error(_("no day in month matches rule"));
2087 (void) exit(EXIT_FAILURE
);
2090 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2092 t
= (time_t) dayoff
* SECSPERDAY
;
2094 ** Cheap overflow check.
2096 if (t
/ SECSPERDAY
!= dayoff
)
2097 return (dayoff
> 0) ? max_time
: min_time
;
2098 return tadd(t
, rp
->r_tod
);
2103 const char * const string
;
2107 i
= strlen(string
) + 1;
2108 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2109 error(_("too many, or too long, time zone abbreviations"));
2110 (void) exit(EXIT_FAILURE
);
2112 (void) strcpy(&chars
[charcnt
], string
);
2113 charcnt
+= eitol(i
);
2118 char * const argname
;
2120 register char * name
;
2123 if (argname
== NULL
|| *argname
== '\0')
2125 cp
= name
= ecpyalloc(argname
);
2126 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2130 ** DOS drive specifier?
2132 if (isalpha((unsigned char) name
[0]) &&
2133 name
[1] == ':' && name
[2] == '\0') {
2137 #endif /* !defined unix */
2138 if (!itsdir(name
)) {
2140 ** It doesn't seem to exist, so we try to create it.
2141 ** Creation may fail because of the directory being
2142 ** created by some other multiprocessor, so we get
2143 ** to do extra checking.
2145 if (mkdir(name
, S_IRUSR
|S_IWUSR
|S_IXUSR
|S_IRGRP
|S_IXGRP
|S_IROTH
|S_IXOTH
) != 0) {
2146 const char *e
= strerror(errno
);
2148 if (errno
!= EEXIST
|| !itsdir(name
)) {
2149 (void) fprintf(stderr
,
2150 _("%s: Can't create directory %s: %s\n"),
2170 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2171 (void) fprintf(stderr
,
2172 _("%s: %d did not sign extend correctly\n"),
2174 (void) exit(EXIT_FAILURE
);
2180 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.