3 static char elsieid
[] = "@(#)zic.c 7.78";
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 char * icatalloc
P((char * old
, const char * new));
83 extern char * icpyalloc
P((const char * string
));
84 extern void ifree
P((char * p
));
85 extern char * imalloc
P((int n
));
86 extern void * irealloc
P((void * old
, int n
));
87 extern int link
P((const char * fromname
, const char * toname
));
90 extern char * scheck
P((const char * string
, const char * format
));
92 static void addtt
P((time_t starttime
, int type
));
93 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
94 int ttisstd
, int ttisgmt
));
95 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
96 static void adjleap
P((void));
97 static void associate
P((void));
98 static int ciequal
P((const char * ap
, const char * bp
));
99 static void convert
P((long val
, char * buf
));
100 static void dolink
P((const char * fromfile
, const char * tofile
));
101 static void doabbr
P((char * abbr
, const char * format
,
102 const char * letters
, int isdst
));
103 static void eat
P((const char * name
, int num
));
104 static void eats
P((const char * name
, int num
,
105 const char * rname
, int rnum
));
106 static long eitol
P((int i
));
107 static void error
P((const char * message
));
108 static char ** getfields
P((char * buf
));
109 static long gethms
P((const char * string
, const char * errstrng
,
111 static void infile
P((const char * filename
));
112 static void inleap
P((char ** fields
, int nfields
));
113 static void inlink
P((char ** fields
, int nfields
));
114 static void inrule
P((char ** fields
, int nfields
));
115 static int inzcont
P((char ** fields
, int nfields
));
116 static int inzone
P((char ** fields
, int nfields
));
117 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
118 static int itsabbr
P((const char * abbr
, const char * word
));
119 static int itsdir
P((const char * name
));
120 static int lowerit
P((int c
));
121 static char * memcheck
P((char * tocheck
));
122 static int mkdirs
P((char * filename
));
123 static void newabbr
P((const char * abbr
));
124 static long oadd
P((long t1
, long t2
));
125 static void outzone
P((const struct zone
* zp
, int ntzones
));
126 static void puttzcode
P((long code
, FILE * fp
));
127 static int rcomp
P((const void * leftp
, const void * rightp
));
128 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
129 static void rulesub
P((struct rule
* rp
,
130 const char * loyearp
, const char * hiyearp
,
131 const char * typep
, const char * monthp
,
132 const char * dayp
, const char * timep
));
133 static void setboundaries
P((void));
134 static time_t tadd
P((time_t t1
, long t2
));
135 static void usage
P((void));
136 static void writezone
P((const char * name
));
137 static int yearistype
P((int year
, const char * type
));
139 #if !(HAVE_STRERROR - 0)
140 static char * strerror
P((int));
141 #endif /* !(HAVE_STRERROR - 0) */
145 static const char * filename
;
148 static time_t max_time
;
150 static time_t min_time
;
153 static const char * rfilename
;
155 static const char * progname
;
169 ** Which fields are which on a Zone line.
177 #define ZF_TILMONTH 6
180 #define ZONE_MINFIELDS 5
181 #define ZONE_MAXFIELDS 9
184 ** Which fields are which on a Zone continuation line.
190 #define ZFC_TILYEAR 3
191 #define ZFC_TILMONTH 4
193 #define ZFC_TILTIME 6
194 #define ZONEC_MINFIELDS 3
195 #define ZONEC_MAXFIELDS 7
198 ** Which files are which on a Rule line.
210 #define RULE_FIELDS 10
213 ** Which fields are which on a Link line.
218 #define LINK_FIELDS 3
221 ** Which fields are which on a Leap line.
230 #define LEAP_FIELDS 7
240 static struct rule
* rules
;
241 static int nrules
; /* number of rules */
243 static struct zone
* zones
;
244 static int nzones
; /* number of zones */
247 const char * l_filename
;
253 static struct link
* links
;
261 static struct lookup
const * byword
P((const char * string
,
262 const struct lookup
* lp
));
264 static struct lookup
const line_codes
[] = {
272 static struct lookup
const mon_names
[] = {
273 { "January", TM_JANUARY
},
274 { "February", TM_FEBRUARY
},
275 { "March", TM_MARCH
},
276 { "April", TM_APRIL
},
280 { "August", TM_AUGUST
},
281 { "September", TM_SEPTEMBER
},
282 { "October", TM_OCTOBER
},
283 { "November", TM_NOVEMBER
},
284 { "December", TM_DECEMBER
},
288 static struct lookup
const wday_names
[] = {
289 { "Sunday", TM_SUNDAY
},
290 { "Monday", TM_MONDAY
},
291 { "Tuesday", TM_TUESDAY
},
292 { "Wednesday", TM_WEDNESDAY
},
293 { "Thursday", TM_THURSDAY
},
294 { "Friday", TM_FRIDAY
},
295 { "Saturday", TM_SATURDAY
},
299 static struct lookup
const lasts
[] = {
300 { "last-Sunday", TM_SUNDAY
},
301 { "last-Monday", TM_MONDAY
},
302 { "last-Tuesday", TM_TUESDAY
},
303 { "last-Wednesday", TM_WEDNESDAY
},
304 { "last-Thursday", TM_THURSDAY
},
305 { "last-Friday", TM_FRIDAY
},
306 { "last-Saturday", TM_SATURDAY
},
310 static struct lookup
const begin_years
[] = {
311 { "minimum", YR_MINIMUM
},
312 { "maximum", YR_MAXIMUM
},
316 static struct lookup
const end_years
[] = {
317 { "minimum", YR_MINIMUM
},
318 { "maximum", YR_MAXIMUM
},
323 static struct lookup
const leap_types
[] = {
325 { "Stationary", FALSE
},
329 static const int len_months
[2][MONSPERYEAR
] = {
330 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
331 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
334 static const int len_years
[2] = {
335 DAYSPERNYEAR
, DAYSPERLYEAR
338 static struct attype
{
341 } attypes
[TZ_MAX_TIMES
];
342 static long gmtoffs
[TZ_MAX_TYPES
];
343 static char isdsts
[TZ_MAX_TYPES
];
344 static unsigned char abbrinds
[TZ_MAX_TYPES
];
345 static char ttisstds
[TZ_MAX_TYPES
];
346 static char ttisgmts
[TZ_MAX_TYPES
];
347 static char chars
[TZ_MAX_CHARS
];
348 static time_t trans
[TZ_MAX_LEAPS
];
349 static long corr
[TZ_MAX_LEAPS
];
350 static char roll
[TZ_MAX_LEAPS
];
353 ** Memory allocation.
361 const char *e
= strerror(errno
);
362 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
364 (void) exit(EXIT_FAILURE
);
369 #define emalloc(size) memcheck(imalloc(size))
370 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
371 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
372 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
378 #if !(HAVE_STRERROR - 0)
383 extern char * sys_errlist
[];
386 return (errnum
> 0 && errnum
<= sys_nerr
) ?
387 sys_errlist
[errnum
] : "Unknown system error";
389 #endif /* !(HAVE_STRERROR - 0) */
392 eats(name
, num
, rname
, rnum
)
393 const char * const name
;
395 const char * const rname
;
406 const char * const name
;
409 eats(name
, num
, (char *) NULL
, -1);
414 const char * const string
;
417 ** Match the format of "cc" to allow sh users to
418 ** zic ... 2>&1 | error -t "*" -v
421 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
422 filename
, linenum
, string
);
423 if (rfilename
!= NULL
)
424 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
425 rfilename
, rlinenum
);
426 (void) fprintf(stderr
, "\n");
432 const char * const string
;
436 cp
= ecpyalloc("warning: ");
437 cp
= ecatalloc(cp
, string
);
446 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
448 (void) exit(EXIT_FAILURE
);
451 static const char * psxrules
;
452 static const char * lcltime
;
453 static const char * directory
;
454 static const char * leapsec
;
455 static const char * yitcommand
;
456 static int sflag
= FALSE
;
468 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
469 #endif /* defined unix */
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 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
)
483 if (directory
== NULL
)
486 (void) fprintf(stderr
,
487 _("%s: More than one -d option specified\n"),
489 (void) exit(EXIT_FAILURE
);
496 (void) fprintf(stderr
,
497 _("%s: More than one -l option specified\n"),
499 (void) exit(EXIT_FAILURE
);
503 if (psxrules
== NULL
)
506 (void) fprintf(stderr
,
507 _("%s: More than one -p option specified\n"),
509 (void) exit(EXIT_FAILURE
);
513 if (yitcommand
== NULL
)
516 (void) fprintf(stderr
,
517 _("%s: More than one -y option specified\n"),
519 (void) exit(EXIT_FAILURE
);
526 (void) fprintf(stderr
,
527 _("%s: More than one -L option specified\n"),
529 (void) exit(EXIT_FAILURE
);
539 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
540 usage(); /* usage message by request */
541 if (directory
== NULL
)
543 if (yitcommand
== NULL
)
544 yitcommand
= "yearistype";
548 if (optind
< argc
&& leapsec
!= NULL
) {
553 for (i
= optind
; i
< argc
; ++i
)
556 (void) exit(EXIT_FAILURE
);
558 for (i
= 0; i
< nzones
; i
= j
) {
560 ** Find the next non-continuation zone entry.
562 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
564 outzone(&zones
[i
], j
- i
);
569 for (i
= 0; i
< nlinks
; ++i
)
570 dolink(links
[i
].l_from
, links
[i
].l_to
);
572 dolink(lcltime
, TZDEFAULT
);
573 if (psxrules
!= NULL
)
574 dolink(psxrules
, TZDEFRULES
);
575 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
579 dolink(fromfile
, tofile
)
580 const char * const fromfile
;
581 const char * const tofile
;
583 register char * fromname
;
584 register char * toname
;
586 if (fromfile
[0] == '/')
587 fromname
= ecpyalloc(fromfile
);
589 fromname
= ecpyalloc(directory
);
590 fromname
= ecatalloc(fromname
, "/");
591 fromname
= ecatalloc(fromname
, fromfile
);
593 if (tofile
[0] == '/')
594 toname
= ecpyalloc(tofile
);
596 toname
= ecpyalloc(directory
);
597 toname
= ecatalloc(toname
, "/");
598 toname
= ecatalloc(toname
, tofile
);
601 ** We get to be careful here since
602 ** there's a fair chance of root running us.
605 (void) remove(toname
);
606 if (link(fromname
, toname
) != 0) {
607 if (mkdirs(toname
) != 0)
608 (void) exit(EXIT_FAILURE
);
609 if (link(fromname
, toname
) != 0) {
610 const char *e
= strerror(errno
);
611 (void) fprintf(stderr
,
612 _("%s: Can't link from %s to %s: %s\n"),
613 progname
, fromname
, toname
, e
);
614 (void) exit(EXIT_FAILURE
);
622 #define INT_MAX ((int) (((unsigned)~0)>>1))
623 #endif /* !defined INT_MAX */
626 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
627 #endif /* !defined INT_MIN */
630 ** The tz file format currently allows at most 32-bit quantities.
631 ** This restriction should be removed before signed 32-bit values
632 ** wrap around in 2038, but unfortunately this will require a
633 ** change to the tz file format.
636 #define MAX_BITS_IN_FILE 32
637 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
640 setboundaries
P((void))
642 if (TYPE_SIGNED(time_t)) {
643 min_time
= ~ (time_t) 0;
644 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
645 max_time
= ~ (time_t) 0 - min_time
;
650 max_time
= 2 - sflag
;
651 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
654 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
655 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
660 const char * const name
;
662 register char * myname
;
665 myname
= ecpyalloc(name
);
666 myname
= ecatalloc(myname
, "/.");
667 accres
= access(myname
, F_OK
);
673 ** Associate sets of rules with zones.
677 ** Sort by rule name.
685 return strcmp(((const struct rule
*) cp1
)->r_name
,
686 ((const struct rule
*) cp2
)->r_name
);
692 register struct zone
* zp
;
693 register struct rule
* rp
;
694 register int base
, out
;
698 (void) qsort((void *) rules
, (size_t) nrules
,
699 (size_t) sizeof *rules
, rcomp
);
700 for (i
= 0; i
< nrules
- 1; ++i
) {
701 if (strcmp(rules
[i
].r_name
,
702 rules
[i
+ 1].r_name
) != 0)
704 if (strcmp(rules
[i
].r_filename
,
705 rules
[i
+ 1].r_filename
) == 0)
707 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
708 warning(_("same rule name in multiple files"));
709 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
710 warning(_("same rule name in multiple files"));
711 for (j
= i
+ 2; j
< nrules
; ++j
) {
712 if (strcmp(rules
[i
].r_name
,
713 rules
[j
].r_name
) != 0)
715 if (strcmp(rules
[i
].r_filename
,
716 rules
[j
].r_filename
) == 0)
718 if (strcmp(rules
[i
+ 1].r_filename
,
719 rules
[j
].r_filename
) == 0)
726 for (i
= 0; i
< nzones
; ++i
) {
731 for (base
= 0; base
< nrules
; base
= out
) {
733 for (out
= base
+ 1; out
< nrules
; ++out
)
734 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
736 for (i
= 0; i
< nzones
; ++i
) {
738 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
741 zp
->z_nrules
= out
- base
;
744 for (i
= 0; i
< nzones
; ++i
) {
746 if (zp
->z_nrules
== 0) {
748 ** Maybe we have a local standard time offset.
750 eat(zp
->z_filename
, zp
->z_linenum
);
751 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
754 ** Note, though, that if there's no rule,
755 ** a '%s' in the format is a bad thing.
757 if (strchr(zp
->z_format
, '%') != 0)
758 error(_("%s in ruleless zone"));
762 (void) exit(EXIT_FAILURE
);
770 register char ** fields
;
772 register const struct lookup
* lp
;
773 register int nfields
;
774 register int wantcont
;
778 if (strcmp(name
, "-") == 0) {
779 name
= _("standard input");
781 } else if ((fp
= fopen(name
, "r")) == NULL
) {
782 const char *e
= strerror(errno
);
783 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
785 (void) exit(EXIT_FAILURE
);
788 for (num
= 1; ; ++num
) {
790 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
792 cp
= strchr(buf
, '\n');
794 error(_("line too long"));
795 (void) exit(EXIT_FAILURE
);
798 fields
= getfields(buf
);
800 while (fields
[nfields
] != NULL
) {
803 if (strcmp(fields
[nfields
], "-") == 0)
804 fields
[nfields
] = &nada
;
809 } else if (wantcont
) {
810 wantcont
= inzcont(fields
, nfields
);
812 lp
= byword(fields
[0], line_codes
);
814 error(_("input line of unknown type"));
815 else switch ((int) (lp
->l_value
)) {
817 inrule(fields
, nfields
);
821 wantcont
= inzone(fields
, nfields
);
824 inlink(fields
, nfields
);
829 (void) fprintf(stderr
,
830 _("%s: Leap line in non leap seconds file %s\n"),
832 else inleap(fields
, nfields
);
835 default: /* "cannot happen" */
836 (void) fprintf(stderr
,
837 _("%s: panic: Invalid l_value %d\n"),
838 progname
, lp
->l_value
);
839 (void) exit(EXIT_FAILURE
);
842 ifree((char *) fields
);
845 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
847 (void) exit(EXIT_FAILURE
);
849 if (fp
!= stdin
&& fclose(fp
)) {
850 const char *e
= strerror(errno
);
851 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
852 progname
, filename
, e
);
853 (void) exit(EXIT_FAILURE
);
856 error(_("expected continuation line not found"));
860 ** Convert a string of one of the forms
861 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
862 ** into a number of seconds.
863 ** A null string maps to zero.
864 ** Call error with errstring and return zero on errors.
868 gethms(string
, errstring
, signable
)
870 const char * const errstring
;
873 int hh
, mm
, ss
, sign
;
875 if (string
== NULL
|| *string
== '\0')
879 else if (*string
== '-') {
883 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
885 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
887 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
888 &hh
, &mm
, &ss
) != 3) {
892 if (hh
< 0 || hh
>= HOURSPERDAY
||
893 mm
< 0 || mm
>= MINSPERHOUR
||
894 ss
< 0 || ss
> SECSPERMIN
) {
899 (eitol(hh
* MINSPERHOUR
+ mm
) *
900 eitol(SECSPERMIN
) + eitol(ss
));
904 inrule(fields
, nfields
)
905 register char ** const fields
;
908 static struct rule r
;
910 if (nfields
!= RULE_FIELDS
) {
911 error(_("wrong number of fields on Rule line"));
914 if (*fields
[RF_NAME
] == '\0') {
915 error(_("nameless rule"));
918 r
.r_filename
= filename
;
919 r
.r_linenum
= linenum
;
920 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
921 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
922 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
923 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
924 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
925 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
926 (int) ((nrules
+ 1) * sizeof *rules
));
931 inzone(fields
, nfields
)
932 register char ** const fields
;
938 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
939 error(_("wrong number of fields on Zone line"));
942 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
943 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
945 _("\"Zone %s\" line and -l option are mutually exclusive"),
950 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
951 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
953 _("\"Zone %s\" line and -p option are mutually exclusive"),
958 for (i
= 0; i
< nzones
; ++i
)
959 if (zones
[i
].z_name
!= NULL
&&
960 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
961 buf
= erealloc(buf
, (int) (132 +
962 strlen(fields
[ZF_NAME
]) +
963 strlen(zones
[i
].z_filename
)));
965 _("duplicate zone name %s (file \"%s\", line %d)"),
972 return inzsub(fields
, nfields
, FALSE
);
976 inzcont(fields
, nfields
)
977 register char ** const fields
;
980 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
981 error(_("wrong number of fields on Zone continuation line"));
984 return inzsub(fields
, nfields
, TRUE
);
988 inzsub(fields
, nfields
, iscont
)
989 register char ** const fields
;
994 static struct zone z
;
995 register int i_gmtoff
, i_rule
, i_format
;
996 register int i_untilyear
, i_untilmonth
;
997 register int i_untilday
, i_untiltime
;
998 register int hasuntil
;
1001 i_gmtoff
= ZFC_GMTOFF
;
1003 i_format
= ZFC_FORMAT
;
1004 i_untilyear
= ZFC_TILYEAR
;
1005 i_untilmonth
= ZFC_TILMONTH
;
1006 i_untilday
= ZFC_TILDAY
;
1007 i_untiltime
= ZFC_TILTIME
;
1010 i_gmtoff
= ZF_GMTOFF
;
1012 i_format
= ZF_FORMAT
;
1013 i_untilyear
= ZF_TILYEAR
;
1014 i_untilmonth
= ZF_TILMONTH
;
1015 i_untilday
= ZF_TILDAY
;
1016 i_untiltime
= ZF_TILTIME
;
1017 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1019 z
.z_filename
= filename
;
1020 z
.z_linenum
= linenum
;
1021 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid GMT offset"), TRUE
);
1022 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1023 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1024 error(_("invalid abbreviation format"));
1028 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1029 z
.z_format
= ecpyalloc(fields
[i_format
]);
1030 hasuntil
= nfields
> i_untilyear
;
1032 z
.z_untilrule
.r_filename
= filename
;
1033 z
.z_untilrule
.r_linenum
= linenum
;
1034 rulesub(&z
.z_untilrule
,
1035 fields
[i_untilyear
],
1038 (nfields
> i_untilmonth
) ?
1039 fields
[i_untilmonth
] : "Jan",
1040 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1041 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1042 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1043 z
.z_untilrule
.r_loyear
);
1044 if (iscont
&& nzones
> 0 &&
1045 z
.z_untiltime
> min_time
&&
1046 z
.z_untiltime
< max_time
&&
1047 zones
[nzones
- 1].z_untiltime
> min_time
&&
1048 zones
[nzones
- 1].z_untiltime
< max_time
&&
1049 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1050 error(_("Zone continuation line end time is not after end time of previous line"));
1054 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1055 (int) ((nzones
+ 1) * sizeof *zones
));
1056 zones
[nzones
++] = z
;
1058 ** If there was an UNTIL field on this line,
1059 ** there's more information about the zone on the next line.
1065 inleap(fields
, nfields
)
1066 register char ** const fields
;
1069 register const char * cp
;
1070 register const struct lookup
* lp
;
1072 int year
, month
, day
;
1076 if (nfields
!= LEAP_FIELDS
) {
1077 error(_("wrong number of fields on Leap line"));
1081 cp
= fields
[LP_YEAR
];
1082 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1086 error(_("invalid leaping year"));
1092 i
= len_years
[isleap(j
)];
1096 i
= -len_years
[isleap(j
)];
1098 dayoff
= oadd(dayoff
, eitol(i
));
1100 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1101 error(_("invalid month name"));
1104 month
= lp
->l_value
;
1106 while (j
!= month
) {
1107 i
= len_months
[isleap(year
)][j
];
1108 dayoff
= oadd(dayoff
, eitol(i
));
1111 cp
= fields
[LP_DAY
];
1112 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1113 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1114 error(_("invalid day of month"));
1117 dayoff
= oadd(dayoff
, eitol(day
- 1));
1118 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1119 error(_("time before zero"));
1122 t
= (time_t) dayoff
* SECSPERDAY
;
1124 ** Cheap overflow check.
1126 if (t
/ SECSPERDAY
!= dayoff
) {
1127 error(_("time overflow"));
1130 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1131 cp
= fields
[LP_CORR
];
1133 register int positive
;
1136 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1139 } else if (strcmp(cp
, "--") == 0) {
1142 } else if (strcmp(cp
, "+") == 0) {
1145 } else if (strcmp(cp
, "++") == 0) {
1149 error(_("illegal CORRECTION field on Leap line"));
1152 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1153 error(_("illegal Rolling/Stationary field on Leap line"));
1156 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1161 inlink(fields
, nfields
)
1162 register char ** const fields
;
1167 if (nfields
!= LINK_FIELDS
) {
1168 error(_("wrong number of fields on Link line"));
1171 if (*fields
[LF_FROM
] == '\0') {
1172 error(_("blank FROM field on Link line"));
1175 if (*fields
[LF_TO
] == '\0') {
1176 error(_("blank TO field on Link line"));
1179 l
.l_filename
= filename
;
1180 l
.l_linenum
= linenum
;
1181 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1182 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1183 links
= (struct link
*) (void *) erealloc((char *) links
,
1184 (int) ((nlinks
+ 1) * sizeof *links
));
1185 links
[nlinks
++] = l
;
1189 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1190 register struct rule
* const rp
;
1191 const char * const loyearp
;
1192 const char * const hiyearp
;
1193 const char * const typep
;
1194 const char * const monthp
;
1195 const char * const dayp
;
1196 const char * const timep
;
1198 register const struct lookup
* lp
;
1199 register const char * cp
;
1203 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1204 error(_("invalid month name"));
1207 rp
->r_month
= lp
->l_value
;
1208 rp
->r_todisstd
= FALSE
;
1209 rp
->r_todisgmt
= FALSE
;
1210 dp
= ecpyalloc(timep
);
1212 ep
= dp
+ strlen(dp
) - 1;
1213 switch (lowerit(*ep
)) {
1214 case 's': /* Standard */
1215 rp
->r_todisstd
= TRUE
;
1216 rp
->r_todisgmt
= FALSE
;
1219 case 'w': /* Wall */
1220 rp
->r_todisstd
= FALSE
;
1221 rp
->r_todisgmt
= FALSE
;
1223 case 'g': /* Greenwich */
1224 case 'u': /* Universal */
1225 case 'z': /* Zulu */
1226 rp
->r_todisstd
= TRUE
;
1227 rp
->r_todisgmt
= TRUE
;
1232 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1238 lp
= byword(cp
, begin_years
);
1239 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1241 rp
->r_loyear
= INT_MIN
;
1244 rp
->r_loyear
= INT_MAX
;
1246 default: /* "cannot happen" */
1247 (void) fprintf(stderr
,
1248 _("%s: panic: Invalid l_value %d\n"),
1249 progname
, lp
->l_value
);
1250 (void) exit(EXIT_FAILURE
);
1251 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1252 error(_("invalid starting year"));
1256 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1258 rp
->r_hiyear
= INT_MIN
;
1261 rp
->r_hiyear
= INT_MAX
;
1264 rp
->r_hiyear
= rp
->r_loyear
;
1266 default: /* "cannot happen" */
1267 (void) fprintf(stderr
,
1268 _("%s: panic: Invalid l_value %d\n"),
1269 progname
, lp
->l_value
);
1270 (void) exit(EXIT_FAILURE
);
1271 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1272 error(_("invalid ending year"));
1275 if (rp
->r_loyear
> rp
->r_hiyear
) {
1276 error(_("starting year greater than ending year"));
1280 rp
->r_yrtype
= NULL
;
1282 if (rp
->r_loyear
== rp
->r_hiyear
) {
1283 error(_("typed single year"));
1286 rp
->r_yrtype
= ecpyalloc(typep
);
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
);
1397 while (attypes
[fromi
].type
== 0)
1398 ++fromi
; /* handled by default rule */
1399 for ( ; fromi
< timecnt
; ++fromi
) {
1401 && ((attypes
[fromi
].at
1402 + gmtoffs
[attypes
[toi
- 1].type
])
1403 <= (attypes
[toi
- 1].at
1404 + gmtoffs
[toi
== 1 ? 0
1405 : attypes
[toi
- 2].type
]))) {
1406 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1410 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1411 attypes
[toi
++] = attypes
[fromi
];
1418 for (i
= 0; i
< timecnt
; ++i
) {
1419 ats
[i
] = attypes
[i
].at
;
1420 types
[i
] = attypes
[i
].type
;
1422 fullname
= erealloc(fullname
,
1423 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1424 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1425 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1426 if (mkdirs(fullname
) != 0)
1427 (void) exit(EXIT_FAILURE
);
1428 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1429 const char *e
= strerror(errno
);
1430 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1431 progname
, fullname
, e
);
1432 (void) exit(EXIT_FAILURE
);
1435 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1436 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1437 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1438 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1439 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1440 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1441 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1450 for (i
= 0; i
< timecnt
; ++i
) {
1453 if (ats
[i
] >= trans
[j
]) {
1454 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1457 puttzcode((long) ats
[i
], fp
);
1460 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1461 (size_t) timecnt
, fp
);
1462 for (i
= 0; i
< typecnt
; ++i
) {
1463 puttzcode((long) gmtoffs
[i
], fp
);
1464 (void) putc(isdsts
[i
], fp
);
1465 (void) putc(abbrinds
[i
], fp
);
1468 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1469 (size_t) charcnt
, fp
);
1470 for (i
= 0; i
< leapcnt
; ++i
) {
1472 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1475 if (++j
>= typecnt
) {
1481 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1485 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1486 } else puttzcode((long) trans
[i
], fp
);
1487 puttzcode((long) corr
[i
], fp
);
1489 for (i
= 0; i
< typecnt
; ++i
)
1490 (void) putc(ttisstds
[i
], fp
);
1491 for (i
= 0; i
< typecnt
; ++i
)
1492 (void) putc(ttisgmts
[i
], fp
);
1493 if (ferror(fp
) || fclose(fp
)) {
1494 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1495 progname
, fullname
);
1496 (void) exit(EXIT_FAILURE
);
1501 doabbr(abbr
, format
, letters
, isdst
)
1503 const char * const format
;
1504 const char * const letters
;
1507 if (strchr(format
, '/') == NULL
) {
1508 if (letters
== NULL
)
1509 (void) strcpy(abbr
, format
);
1510 else (void) sprintf(abbr
, format
, letters
);
1512 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1514 (void) strcpy(abbr
, format
);
1515 *strchr(abbr
, '/') = '\0';
1520 outzone(zpfirst
, zonecount
)
1521 const struct zone
* const zpfirst
;
1522 const int zonecount
;
1524 register const struct zone
* zp
;
1525 register struct rule
* rp
;
1527 register int usestart
, useuntil
;
1528 register time_t starttime
, untiltime
;
1529 register long gmtoff
;
1530 register long stdoff
;
1532 register long startoff
;
1533 register int startttisstd
;
1534 register int startttisgmt
;
1536 char startbuf
[BUFSIZ
];
1538 INITIALIZE(untiltime
);
1539 INITIALIZE(starttime
);
1541 ** Now. . .finally. . .generate some useful data!
1547 ** A guess that may well be corrected later.
1551 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1552 ** for noting the need to unconditionally initialize startttisstd.
1554 startttisstd
= FALSE
;
1555 startttisgmt
= FALSE
;
1556 for (i
= 0; i
< zonecount
; ++i
) {
1558 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1559 useuntil
= i
< (zonecount
- 1);
1560 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1562 gmtoff
= zp
->z_gmtoff
;
1563 eat(zp
->z_filename
, zp
->z_linenum
);
1565 startoff
= zp
->z_gmtoff
;
1566 if (zp
->z_nrules
== 0) {
1567 stdoff
= zp
->z_stdoff
;
1568 doabbr(startbuf
, zp
->z_format
,
1569 (char *) NULL
, stdoff
!= 0);
1570 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1571 startbuf
, stdoff
!= 0, startttisstd
,
1574 addtt(starttime
, type
);
1577 else if (stdoff
!= 0)
1578 addtt(min_time
, type
);
1579 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1580 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1583 ** Mark which rules to do in the current year.
1584 ** For those to do, calculate rpytime(rp, year);
1586 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1587 rp
= &zp
->z_rules
[j
];
1588 eats(zp
->z_filename
, zp
->z_linenum
,
1589 rp
->r_filename
, rp
->r_linenum
);
1590 rp
->r_todo
= year
>= rp
->r_loyear
&&
1591 year
<= rp
->r_hiyear
&&
1592 yearistype(year
, rp
->r_yrtype
);
1594 rp
->r_temp
= rpytime(rp
, year
);
1598 register time_t jtime
, ktime
;
1599 register long offset
;
1605 ** Turn untiltime into GMT
1606 ** assuming the current gmtoff and
1609 untiltime
= zp
->z_untiltime
;
1610 if (!zp
->z_untilrule
.r_todisgmt
)
1611 untiltime
= tadd(untiltime
,
1613 if (!zp
->z_untilrule
.r_todisstd
)
1614 untiltime
= tadd(untiltime
,
1618 ** Find the rule (of those to do, if any)
1619 ** that takes effect earliest in the year.
1622 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1623 rp
= &zp
->z_rules
[j
];
1626 eats(zp
->z_filename
, zp
->z_linenum
,
1627 rp
->r_filename
, rp
->r_linenum
);
1628 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1629 if (!rp
->r_todisstd
)
1630 offset
= oadd(offset
, stdoff
);
1632 if (jtime
== min_time
||
1635 jtime
= tadd(jtime
, -offset
);
1636 if (k
< 0 || jtime
< ktime
) {
1642 break; /* go on to next year */
1643 rp
= &zp
->z_rules
[k
];
1645 if (useuntil
&& ktime
>= untiltime
)
1647 stdoff
= rp
->r_stdoff
;
1648 if (usestart
&& ktime
== starttime
)
1651 if (ktime
< starttime
) {
1652 startoff
= oadd(zp
->z_gmtoff
,
1654 doabbr(startbuf
, zp
->z_format
,
1659 if (*startbuf
== '\0' &&
1660 startoff
== oadd(zp
->z_gmtoff
,
1662 doabbr(startbuf
, zp
->z_format
,
1667 eats(zp
->z_filename
, zp
->z_linenum
,
1668 rp
->r_filename
, rp
->r_linenum
);
1669 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1671 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1672 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1673 rp
->r_todisstd
, rp
->r_todisgmt
);
1678 if (*startbuf
== '\0' &&
1679 zp
->z_format
!= NULL
&&
1680 strchr(zp
->z_format
, '%') == NULL
&&
1681 strchr(zp
->z_format
, '/') == NULL
)
1682 (void) strcpy(startbuf
, zp
->z_format
);
1683 eat(zp
->z_filename
, zp
->z_linenum
);
1684 if (*startbuf
== '\0')
1685 error(_("can't determine time zone abbrevation to use just after until time"));
1686 else addtt(starttime
,
1687 addtype(startoff
, startbuf
,
1688 startoff
!= zp
->z_gmtoff
,
1693 ** Now we may get to set starttime for the next zone line.
1696 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1697 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1698 starttime
= zp
->z_untiltime
;
1700 starttime
= tadd(starttime
, -stdoff
);
1702 starttime
= tadd(starttime
, -gmtoff
);
1705 writezone(zpfirst
->z_name
);
1709 addtt(starttime
, type
)
1710 const time_t starttime
;
1713 if (timecnt
>= TZ_MAX_TIMES
) {
1714 error(_("too many transitions?!"));
1715 (void) exit(EXIT_FAILURE
);
1717 attypes
[timecnt
].at
= starttime
;
1718 attypes
[timecnt
].type
= type
;
1723 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1725 const char * const abbr
;
1732 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1733 error(_("internal error - addtype called with bad isdst"));
1734 (void) exit(EXIT_FAILURE
);
1736 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1737 error(_("internal error - addtype called with bad ttisstd"));
1738 (void) exit(EXIT_FAILURE
);
1740 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1741 error(_("internal error - addtype called with bad ttisgmt"));
1742 (void) exit(EXIT_FAILURE
);
1745 ** See if there's already an entry for this zone type.
1746 ** If so, just return its index.
1748 for (i
= 0; i
< typecnt
; ++i
) {
1749 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1750 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1751 ttisstd
== ttisstds
[i
] &&
1752 ttisgmt
== ttisgmts
[i
])
1756 ** There isn't one; add a new one, unless there are already too
1759 if (typecnt
>= TZ_MAX_TYPES
) {
1760 error(_("too many local time types"));
1761 (void) exit(EXIT_FAILURE
);
1763 gmtoffs
[i
] = gmtoff
;
1765 ttisstds
[i
] = ttisstd
;
1766 ttisgmts
[i
] = ttisgmt
;
1768 for (j
= 0; j
< charcnt
; ++j
)
1769 if (strcmp(&chars
[j
], abbr
) == 0)
1779 leapadd(t
, positive
, rolling
, count
)
1787 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1788 error(_("too many leap seconds"));
1789 (void) exit(EXIT_FAILURE
);
1791 for (i
= 0; i
< leapcnt
; ++i
)
1792 if (t
<= trans
[i
]) {
1793 if (t
== trans
[i
]) {
1794 error(_("repeated leap second moment"));
1795 (void) exit(EXIT_FAILURE
);
1800 for (j
= leapcnt
; j
> i
; --j
) {
1801 trans
[j
] = trans
[j
- 1];
1802 corr
[j
] = corr
[j
- 1];
1803 roll
[j
] = roll
[j
- 1];
1806 corr
[i
] = positive
? 1L : eitol(-count
);
1809 } while (positive
&& --count
!= 0);
1816 register long last
= 0;
1819 ** propagate leap seconds forward
1821 for (i
= 0; i
< leapcnt
; ++i
) {
1822 trans
[i
] = tadd(trans
[i
], last
);
1823 last
= corr
[i
] += last
;
1828 yearistype(year
, type
)
1830 const char * const type
;
1835 if (type
== NULL
|| *type
== '\0')
1837 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1838 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1839 result
= system(buf
);
1842 if (result
== (1 << 8))
1844 error(_("Wild result from command execution"));
1845 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1846 progname
, buf
, result
);
1848 (void) exit(EXIT_FAILURE
);
1855 a
= (unsigned char) a
;
1856 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1860 ciequal(ap
, bp
) /* case-insensitive equality */
1861 register const char * ap
;
1862 register const char * bp
;
1864 while (lowerit(*ap
) == lowerit(*bp
++))
1872 register const char * abbr
;
1873 register const char * word
;
1875 if (lowerit(*abbr
) != lowerit(*word
))
1878 while (*++abbr
!= '\0')
1882 } while (lowerit(*word
++) != lowerit(*abbr
));
1886 static const struct lookup
*
1888 register const char * const word
;
1889 register const struct lookup
* const table
;
1891 register const struct lookup
* foundlp
;
1892 register const struct lookup
* lp
;
1894 if (word
== NULL
|| table
== NULL
)
1897 ** Look for exact match.
1899 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1900 if (ciequal(word
, lp
->l_word
))
1903 ** Look for inexact match.
1906 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1907 if (itsabbr(word
, lp
->l_word
))
1908 if (foundlp
== NULL
)
1910 else return NULL
; /* multiple inexact matches */
1919 register char ** array
;
1924 array
= (char **) (void *)
1925 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1928 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1930 if (*cp
== '\0' || *cp
== '#')
1932 array
[nsubs
++] = dp
= cp
;
1934 if ((*dp
= *cp
++) != '"')
1936 else while ((*dp
= *cp
++) != '"')
1939 else error(_("Odd number of quotation marks"));
1940 } while (*cp
!= '\0' && *cp
!= '#' &&
1941 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1942 if (isascii(*cp
) && isspace((unsigned char) *cp
))
1946 array
[nsubs
] = NULL
;
1958 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1959 error(_("time overflow"));
1960 (void) exit(EXIT_FAILURE
);
1972 if (t1
== max_time
&& t2
> 0)
1974 if (t1
== min_time
&& t2
< 0)
1977 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1978 error(_("time overflow"));
1979 (void) exit(EXIT_FAILURE
);
1985 ** Given a rule, and a year, compute the date - in seconds since January 1,
1986 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1990 rpytime(rp
, wantedy
)
1991 register const struct rule
* const rp
;
1992 register const int wantedy
;
1994 register int y
, m
, i
;
1995 register long dayoff
; /* with a nod to Margaret O. */
1998 if (wantedy
== INT_MIN
)
2000 if (wantedy
== INT_MAX
)
2005 while (wantedy
!= y
) {
2007 i
= len_years
[isleap(y
)];
2011 i
= -len_years
[isleap(y
)];
2013 dayoff
= oadd(dayoff
, eitol(i
));
2015 while (m
!= rp
->r_month
) {
2016 i
= len_months
[isleap(y
)][m
];
2017 dayoff
= oadd(dayoff
, eitol(i
));
2020 i
= rp
->r_dayofmonth
;
2021 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2022 if (rp
->r_dycode
== DC_DOWLEQ
)
2025 error(_("use of 2/29 in non leap-year"));
2026 (void) exit(EXIT_FAILURE
);
2030 dayoff
= oadd(dayoff
, eitol(i
));
2031 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2034 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2035 wday
= eitol(EPOCH_WDAY
);
2037 ** Don't trust mod of negative numbers.
2040 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2042 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2044 wday
+= LDAYSPERWEEK
;
2046 while (wday
!= eitol(rp
->r_wday
))
2047 if (rp
->r_dycode
== DC_DOWGEQ
) {
2048 dayoff
= oadd(dayoff
, (long) 1);
2049 if (++wday
>= LDAYSPERWEEK
)
2053 dayoff
= oadd(dayoff
, (long) -1);
2055 wday
= LDAYSPERWEEK
- 1;
2058 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2059 error(_("no day in month matches rule"));
2060 (void) exit(EXIT_FAILURE
);
2063 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2065 t
= (time_t) dayoff
* SECSPERDAY
;
2067 ** Cheap overflow check.
2069 if (t
/ SECSPERDAY
!= dayoff
)
2070 return (dayoff
> 0) ? max_time
: min_time
;
2071 return tadd(t
, rp
->r_tod
);
2076 const char * const string
;
2080 i
= strlen(string
) + 1;
2081 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2082 error(_("too many, or too long, time zone abbreviations"));
2083 (void) exit(EXIT_FAILURE
);
2085 (void) strcpy(&chars
[charcnt
], string
);
2086 charcnt
+= eitol(i
);
2091 char * const argname
;
2093 register char * name
;
2096 if (argname
== NULL
|| *argname
== '\0')
2098 cp
= name
= ecpyalloc(argname
);
2099 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2103 ** DOS drive specifier?
2105 if (isalpha((unsigned char) name
[0]) &&
2106 name
[1] == ':' && name
[2] == '\0') {
2110 #endif /* !defined unix */
2111 if (!itsdir(name
)) {
2113 ** It doesn't seem to exist, so we try to create it.
2115 if (mkdir(name
, 0755) != 0) {
2116 const char *e
= strerror(errno
);
2117 (void) fprintf(stderr
,
2118 _("%s: Can't create directory %s: %s\n"),
2137 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2138 (void) fprintf(stderr
,
2139 _("%s: %d did not sign extend correctly\n"),
2141 (void) exit(EXIT_FAILURE
);
2147 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.