3 static char elsieid
[] = "@(#)zic.c 7.77";
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
));
140 static char * strerror
P((int));
141 #endif /* !HAVE_STRERROR */
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)))
383 extern char *sys_errlist
[];
386 if (errnum
> 0 && errnum
<= sys_nerr
)
387 return sys_errlist
[errnum
];
388 return "Unknown system error";
390 #endif /* ! HAVE_STRERROR */
393 eats(name
, num
, rname
, rnum
)
394 const char * const name
;
396 const char * const rname
;
407 const char * const name
;
410 eats(name
, num
, (char *) NULL
, -1);
415 const char * const string
;
418 ** Match the format of "cc" to allow sh users to
419 ** zic ... 2>&1 | error -t "*" -v
422 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
423 filename
, linenum
, string
);
424 if (rfilename
!= NULL
)
425 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
426 rfilename
, rlinenum
);
427 (void) fprintf(stderr
, "\n");
433 const char * const string
;
437 cp
= ecpyalloc("warning: ");
438 cp
= ecatalloc(cp
, string
);
447 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
449 (void) exit(EXIT_FAILURE
);
452 static const char * psxrules
;
453 static const char * lcltime
;
454 static const char * directory
;
455 static const char * leapsec
;
456 static const char * yitcommand
;
457 static int sflag
= FALSE
;
469 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
470 #endif /* defined unix */
472 (void) setlocale(LC_MESSAGES
, "");
474 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
475 #endif /* defined TEXTDOMAINDIR */
476 (void) textdomain(TZ_DOMAIN
);
477 #endif /* HAVE_GETTEXT - 0 */
479 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
)
484 if (directory
== NULL
)
487 (void) fprintf(stderr
,
488 _("%s: More than one -d option specified\n"),
490 (void) exit(EXIT_FAILURE
);
497 (void) fprintf(stderr
,
498 _("%s: More than one -l option specified\n"),
500 (void) exit(EXIT_FAILURE
);
504 if (psxrules
== NULL
)
507 (void) fprintf(stderr
,
508 _("%s: More than one -p option specified\n"),
510 (void) exit(EXIT_FAILURE
);
514 if (yitcommand
== NULL
)
517 (void) fprintf(stderr
,
518 _("%s: More than one -y option specified\n"),
520 (void) exit(EXIT_FAILURE
);
527 (void) fprintf(stderr
,
528 _("%s: More than one -L option specified\n"),
530 (void) exit(EXIT_FAILURE
);
540 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
541 usage(); /* usage message by request */
542 if (directory
== NULL
)
544 if (yitcommand
== NULL
)
545 yitcommand
= "yearistype";
549 if (optind
< argc
&& leapsec
!= NULL
) {
554 for (i
= optind
; i
< argc
; ++i
)
557 (void) exit(EXIT_FAILURE
);
559 for (i
= 0; i
< nzones
; i
= j
) {
561 ** Find the next non-continuation zone entry.
563 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
565 outzone(&zones
[i
], j
- i
);
570 for (i
= 0; i
< nlinks
; ++i
)
571 dolink(links
[i
].l_from
, links
[i
].l_to
);
573 dolink(lcltime
, TZDEFAULT
);
574 if (psxrules
!= NULL
)
575 dolink(psxrules
, TZDEFRULES
);
576 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
580 dolink(fromfile
, tofile
)
581 const char * const fromfile
;
582 const char * const tofile
;
584 register char * fromname
;
585 register char * toname
;
587 if (fromfile
[0] == '/')
588 fromname
= ecpyalloc(fromfile
);
590 fromname
= ecpyalloc(directory
);
591 fromname
= ecatalloc(fromname
, "/");
592 fromname
= ecatalloc(fromname
, fromfile
);
594 if (tofile
[0] == '/')
595 toname
= ecpyalloc(tofile
);
597 toname
= ecpyalloc(directory
);
598 toname
= ecatalloc(toname
, "/");
599 toname
= ecatalloc(toname
, tofile
);
602 ** We get to be careful here since
603 ** there's a fair chance of root running us.
606 (void) remove(toname
);
607 if (link(fromname
, toname
) != 0) {
608 if (mkdirs(toname
) != 0)
609 (void) exit(EXIT_FAILURE
);
610 if (link(fromname
, toname
) != 0) {
611 const char *e
= strerror(errno
);
612 (void) fprintf(stderr
,
613 _("%s: Can't link from %s to %s: %s\n"),
614 progname
, fromname
, toname
, e
);
615 (void) exit(EXIT_FAILURE
);
623 #define INT_MAX ((int) (((unsigned)~0)>>1))
624 #endif /* !defined INT_MAX */
627 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
628 #endif /* !defined INT_MIN */
631 ** The tz file format currently allows at most 32-bit quantities.
632 ** This restriction should be removed before signed 32-bit values
633 ** wrap around in 2038, but unfortunately this will require a
634 ** change to the tz file format.
637 #define MAX_BITS_IN_FILE 32
638 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
641 setboundaries
P((void))
643 if (TYPE_SIGNED(time_t)) {
644 min_time
= ~ (time_t) 0;
645 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
646 max_time
= ~ (time_t) 0 - min_time
;
651 max_time
= 2 - sflag
;
652 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
655 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
656 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
661 const char * const name
;
663 register char * myname
;
666 myname
= ecpyalloc(name
);
667 myname
= ecatalloc(myname
, "/.");
668 accres
= access(myname
, F_OK
);
674 ** Associate sets of rules with zones.
678 ** Sort by rule name.
686 return strcmp(((const struct rule
*) cp1
)->r_name
,
687 ((const struct rule
*) cp2
)->r_name
);
693 register struct zone
* zp
;
694 register struct rule
* rp
;
695 register int base
, out
;
699 (void) qsort((void *) rules
, (size_t) nrules
,
700 (size_t) sizeof *rules
, rcomp
);
701 for (i
= 0; i
< nrules
- 1; ++i
) {
702 if (strcmp(rules
[i
].r_name
,
703 rules
[i
+ 1].r_name
) != 0)
705 if (strcmp(rules
[i
].r_filename
,
706 rules
[i
+ 1].r_filename
) == 0)
708 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
709 warning(_("same rule name in multiple files"));
710 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
711 warning(_("same rule name in multiple files"));
712 for (j
= i
+ 2; j
< nrules
; ++j
) {
713 if (strcmp(rules
[i
].r_name
,
714 rules
[j
].r_name
) != 0)
716 if (strcmp(rules
[i
].r_filename
,
717 rules
[j
].r_filename
) == 0)
719 if (strcmp(rules
[i
+ 1].r_filename
,
720 rules
[j
].r_filename
) == 0)
727 for (i
= 0; i
< nzones
; ++i
) {
732 for (base
= 0; base
< nrules
; base
= out
) {
734 for (out
= base
+ 1; out
< nrules
; ++out
)
735 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
737 for (i
= 0; i
< nzones
; ++i
) {
739 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
742 zp
->z_nrules
= out
- base
;
745 for (i
= 0; i
< nzones
; ++i
) {
747 if (zp
->z_nrules
== 0) {
749 ** Maybe we have a local standard time offset.
751 eat(zp
->z_filename
, zp
->z_linenum
);
752 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
755 ** Note, though, that if there's no rule,
756 ** a '%s' in the format is a bad thing.
758 if (strchr(zp
->z_format
, '%') != 0)
759 error(_("%s in ruleless zone"));
763 (void) exit(EXIT_FAILURE
);
771 register char ** fields
;
773 register const struct lookup
* lp
;
774 register int nfields
;
775 register int wantcont
;
779 if (strcmp(name
, "-") == 0) {
780 name
= _("standard input");
782 } else if ((fp
= fopen(name
, "r")) == NULL
) {
783 const char *e
= strerror(errno
);
784 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
786 (void) exit(EXIT_FAILURE
);
789 for (num
= 1; ; ++num
) {
791 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
793 cp
= strchr(buf
, '\n');
795 error(_("line too long"));
796 (void) exit(EXIT_FAILURE
);
799 fields
= getfields(buf
);
801 while (fields
[nfields
] != NULL
) {
804 if (strcmp(fields
[nfields
], "-") == 0)
805 fields
[nfields
] = &nada
;
810 } else if (wantcont
) {
811 wantcont
= inzcont(fields
, nfields
);
813 lp
= byword(fields
[0], line_codes
);
815 error(_("input line of unknown type"));
816 else switch ((int) (lp
->l_value
)) {
818 inrule(fields
, nfields
);
822 wantcont
= inzone(fields
, nfields
);
825 inlink(fields
, nfields
);
830 (void) fprintf(stderr
,
831 _("%s: Leap line in non leap seconds file %s\n"),
833 else inleap(fields
, nfields
);
836 default: /* "cannot happen" */
837 (void) fprintf(stderr
,
838 _("%s: panic: Invalid l_value %d\n"),
839 progname
, lp
->l_value
);
840 (void) exit(EXIT_FAILURE
);
843 ifree((char *) fields
);
846 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
848 (void) exit(EXIT_FAILURE
);
850 if (fp
!= stdin
&& fclose(fp
)) {
851 const char *e
= strerror(errno
);
852 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
853 progname
, filename
, e
);
854 (void) exit(EXIT_FAILURE
);
857 error(_("expected continuation line not found"));
861 ** Convert a string of one of the forms
862 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
863 ** into a number of seconds.
864 ** A null string maps to zero.
865 ** Call error with errstring and return zero on errors.
869 gethms(string
, errstring
, signable
)
871 const char * const errstring
;
874 int hh
, mm
, ss
, sign
;
876 if (string
== NULL
|| *string
== '\0')
880 else if (*string
== '-') {
884 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
886 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
888 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
889 &hh
, &mm
, &ss
) != 3) {
893 if (hh
< 0 || hh
>= HOURSPERDAY
||
894 mm
< 0 || mm
>= MINSPERHOUR
||
895 ss
< 0 || ss
> SECSPERMIN
) {
900 (eitol(hh
* MINSPERHOUR
+ mm
) *
901 eitol(SECSPERMIN
) + eitol(ss
));
905 inrule(fields
, nfields
)
906 register char ** const fields
;
909 static struct rule r
;
911 if (nfields
!= RULE_FIELDS
) {
912 error(_("wrong number of fields on Rule line"));
915 if (*fields
[RF_NAME
] == '\0') {
916 error(_("nameless rule"));
919 r
.r_filename
= filename
;
920 r
.r_linenum
= linenum
;
921 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
922 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
923 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
924 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
925 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
926 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
927 (int) ((nrules
+ 1) * sizeof *rules
));
932 inzone(fields
, nfields
)
933 register char ** const fields
;
939 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
940 error(_("wrong number of fields on Zone line"));
943 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
944 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
946 _("\"Zone %s\" line and -l option are mutually exclusive"),
951 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
952 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
954 _("\"Zone %s\" line and -p option are mutually exclusive"),
959 for (i
= 0; i
< nzones
; ++i
)
960 if (zones
[i
].z_name
!= NULL
&&
961 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
962 buf
= erealloc(buf
, (int) (132 +
963 strlen(fields
[ZF_NAME
]) +
964 strlen(zones
[i
].z_filename
)));
966 _("duplicate zone name %s (file \"%s\", line %d)"),
973 return inzsub(fields
, nfields
, FALSE
);
977 inzcont(fields
, nfields
)
978 register char ** const fields
;
981 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
982 error(_("wrong number of fields on Zone continuation line"));
985 return inzsub(fields
, nfields
, TRUE
);
989 inzsub(fields
, nfields
, iscont
)
990 register char ** const fields
;
995 static struct zone z
;
996 register int i_gmtoff
, i_rule
, i_format
;
997 register int i_untilyear
, i_untilmonth
;
998 register int i_untilday
, i_untiltime
;
999 register int hasuntil
;
1002 i_gmtoff
= ZFC_GMTOFF
;
1004 i_format
= ZFC_FORMAT
;
1005 i_untilyear
= ZFC_TILYEAR
;
1006 i_untilmonth
= ZFC_TILMONTH
;
1007 i_untilday
= ZFC_TILDAY
;
1008 i_untiltime
= ZFC_TILTIME
;
1011 i_gmtoff
= ZF_GMTOFF
;
1013 i_format
= ZF_FORMAT
;
1014 i_untilyear
= ZF_TILYEAR
;
1015 i_untilmonth
= ZF_TILMONTH
;
1016 i_untilday
= ZF_TILDAY
;
1017 i_untiltime
= ZF_TILTIME
;
1018 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1020 z
.z_filename
= filename
;
1021 z
.z_linenum
= linenum
;
1022 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid GMT offset"), TRUE
);
1023 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1024 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1025 error(_("invalid abbreviation format"));
1029 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1030 z
.z_format
= ecpyalloc(fields
[i_format
]);
1031 hasuntil
= nfields
> i_untilyear
;
1033 z
.z_untilrule
.r_filename
= filename
;
1034 z
.z_untilrule
.r_linenum
= linenum
;
1035 rulesub(&z
.z_untilrule
,
1036 fields
[i_untilyear
],
1039 (nfields
> i_untilmonth
) ?
1040 fields
[i_untilmonth
] : "Jan",
1041 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1042 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1043 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1044 z
.z_untilrule
.r_loyear
);
1045 if (iscont
&& nzones
> 0 &&
1046 z
.z_untiltime
> min_time
&&
1047 z
.z_untiltime
< max_time
&&
1048 zones
[nzones
- 1].z_untiltime
> min_time
&&
1049 zones
[nzones
- 1].z_untiltime
< max_time
&&
1050 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1051 error(_("Zone continuation line end time is not after end time of previous line"));
1055 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1056 (int) ((nzones
+ 1) * sizeof *zones
));
1057 zones
[nzones
++] = z
;
1059 ** If there was an UNTIL field on this line,
1060 ** there's more information about the zone on the next line.
1066 inleap(fields
, nfields
)
1067 register char ** const fields
;
1070 register const char * cp
;
1071 register const struct lookup
* lp
;
1073 int year
, month
, day
;
1077 if (nfields
!= LEAP_FIELDS
) {
1078 error(_("wrong number of fields on Leap line"));
1082 cp
= fields
[LP_YEAR
];
1083 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1087 error(_("invalid leaping year"));
1093 i
= len_years
[isleap(j
)];
1097 i
= -len_years
[isleap(j
)];
1099 dayoff
= oadd(dayoff
, eitol(i
));
1101 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1102 error(_("invalid month name"));
1105 month
= lp
->l_value
;
1107 while (j
!= month
) {
1108 i
= len_months
[isleap(year
)][j
];
1109 dayoff
= oadd(dayoff
, eitol(i
));
1112 cp
= fields
[LP_DAY
];
1113 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1114 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1115 error(_("invalid day of month"));
1118 dayoff
= oadd(dayoff
, eitol(day
- 1));
1119 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1120 error(_("time before zero"));
1123 t
= (time_t) dayoff
* SECSPERDAY
;
1125 ** Cheap overflow check.
1127 if (t
/ SECSPERDAY
!= dayoff
) {
1128 error(_("time overflow"));
1131 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1132 cp
= fields
[LP_CORR
];
1134 register int positive
;
1137 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1140 } else if (strcmp(cp
, "--") == 0) {
1143 } else if (strcmp(cp
, "+") == 0) {
1146 } else if (strcmp(cp
, "++") == 0) {
1150 error(_("illegal CORRECTION field on Leap line"));
1153 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1154 error(_("illegal Rolling/Stationary field on Leap line"));
1157 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1162 inlink(fields
, nfields
)
1163 register char ** const fields
;
1168 if (nfields
!= LINK_FIELDS
) {
1169 error(_("wrong number of fields on Link line"));
1172 if (*fields
[LF_FROM
] == '\0') {
1173 error(_("blank FROM field on Link line"));
1176 if (*fields
[LF_TO
] == '\0') {
1177 error(_("blank TO field on Link line"));
1180 l
.l_filename
= filename
;
1181 l
.l_linenum
= linenum
;
1182 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1183 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1184 links
= (struct link
*) (void *) erealloc((char *) links
,
1185 (int) ((nlinks
+ 1) * sizeof *links
));
1186 links
[nlinks
++] = l
;
1190 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1191 register struct rule
* const rp
;
1192 const char * const loyearp
;
1193 const char * const hiyearp
;
1194 const char * const typep
;
1195 const char * const monthp
;
1196 const char * const dayp
;
1197 const char * const timep
;
1199 register const struct lookup
* lp
;
1200 register const char * cp
;
1204 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1205 error(_("invalid month name"));
1208 rp
->r_month
= lp
->l_value
;
1209 rp
->r_todisstd
= FALSE
;
1210 rp
->r_todisgmt
= FALSE
;
1211 dp
= ecpyalloc(timep
);
1213 ep
= dp
+ strlen(dp
) - 1;
1214 switch (lowerit(*ep
)) {
1215 case 's': /* Standard */
1216 rp
->r_todisstd
= TRUE
;
1217 rp
->r_todisgmt
= FALSE
;
1220 case 'w': /* Wall */
1221 rp
->r_todisstd
= FALSE
;
1222 rp
->r_todisgmt
= FALSE
;
1224 case 'g': /* Greenwich */
1225 case 'u': /* Universal */
1226 case 'z': /* Zulu */
1227 rp
->r_todisstd
= TRUE
;
1228 rp
->r_todisgmt
= TRUE
;
1233 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1239 lp
= byword(cp
, begin_years
);
1240 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1242 rp
->r_loyear
= INT_MIN
;
1245 rp
->r_loyear
= INT_MAX
;
1247 default: /* "cannot happen" */
1248 (void) fprintf(stderr
,
1249 _("%s: panic: Invalid l_value %d\n"),
1250 progname
, lp
->l_value
);
1251 (void) exit(EXIT_FAILURE
);
1252 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1253 error(_("invalid starting year"));
1257 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1259 rp
->r_hiyear
= INT_MIN
;
1262 rp
->r_hiyear
= INT_MAX
;
1265 rp
->r_hiyear
= rp
->r_loyear
;
1267 default: /* "cannot happen" */
1268 (void) fprintf(stderr
,
1269 _("%s: panic: Invalid l_value %d\n"),
1270 progname
, lp
->l_value
);
1271 (void) exit(EXIT_FAILURE
);
1272 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1273 error(_("invalid ending year"));
1276 if (rp
->r_loyear
> rp
->r_hiyear
) {
1277 error(_("starting year greater than ending year"));
1281 rp
->r_yrtype
= NULL
;
1283 if (rp
->r_loyear
== rp
->r_hiyear
) {
1284 error(_("typed single year"));
1287 rp
->r_yrtype
= ecpyalloc(typep
);
1291 ** Accept things such as:
1297 dp
= ecpyalloc(dayp
);
1298 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1299 rp
->r_dycode
= DC_DOWLEQ
;
1300 rp
->r_wday
= lp
->l_value
;
1301 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1303 if ((ep
= strchr(dp
, '<')) != 0)
1304 rp
->r_dycode
= DC_DOWLEQ
;
1305 else if ((ep
= strchr(dp
, '>')) != 0)
1306 rp
->r_dycode
= DC_DOWGEQ
;
1309 rp
->r_dycode
= DC_DOM
;
1311 if (rp
->r_dycode
!= DC_DOM
) {
1314 error(_("invalid day of month"));
1318 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1319 error(_("invalid weekday name"));
1323 rp
->r_wday
= lp
->l_value
;
1325 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1326 rp
->r_dayofmonth
<= 0 ||
1327 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1328 error(_("invalid day of month"));
1342 register long shift
;
1344 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1345 buf
[i
] = val
>> shift
;
1356 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1364 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1366 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1373 const char * const name
;
1377 static char * fullname
;
1378 static struct tzhead tzh
;
1379 time_t ats
[TZ_MAX_TIMES
];
1380 unsigned char types
[TZ_MAX_TIMES
];
1386 (void) qsort((void *) attypes
, (size_t) timecnt
,
1387 (size_t) sizeof *attypes
, atcomp
);
1398 while (attypes
[fromi
].type
== 0)
1399 ++fromi
; /* handled by default rule */
1400 for ( ; fromi
< timecnt
; ++fromi
) {
1402 && ((attypes
[fromi
].at
1403 + gmtoffs
[attypes
[toi
- 1].type
])
1404 <= (attypes
[toi
- 1].at
1405 + gmtoffs
[toi
== 1 ? 0
1406 : attypes
[toi
- 2].type
]))) {
1407 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1411 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1412 attypes
[toi
++] = attypes
[fromi
];
1419 for (i
= 0; i
< timecnt
; ++i
) {
1420 ats
[i
] = attypes
[i
].at
;
1421 types
[i
] = attypes
[i
].type
;
1423 fullname
= erealloc(fullname
,
1424 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1425 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1426 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1427 if (mkdirs(fullname
) != 0)
1428 (void) exit(EXIT_FAILURE
);
1429 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1430 const char *e
= strerror(errno
);
1431 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1432 progname
, fullname
, e
);
1433 (void) exit(EXIT_FAILURE
);
1436 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1437 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1438 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1439 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1440 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1441 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1442 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1451 for (i
= 0; i
< timecnt
; ++i
) {
1454 if (ats
[i
] >= trans
[j
]) {
1455 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1458 puttzcode((long) ats
[i
], fp
);
1461 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1462 (size_t) timecnt
, fp
);
1463 for (i
= 0; i
< typecnt
; ++i
) {
1464 puttzcode((long) gmtoffs
[i
], fp
);
1465 (void) putc(isdsts
[i
], fp
);
1466 (void) putc(abbrinds
[i
], fp
);
1469 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1470 (size_t) charcnt
, fp
);
1471 for (i
= 0; i
< leapcnt
; ++i
) {
1473 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1476 if (++j
>= typecnt
) {
1482 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1486 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1487 } else puttzcode((long) trans
[i
], fp
);
1488 puttzcode((long) corr
[i
], fp
);
1490 for (i
= 0; i
< typecnt
; ++i
)
1491 (void) putc(ttisstds
[i
], fp
);
1492 for (i
= 0; i
< typecnt
; ++i
)
1493 (void) putc(ttisgmts
[i
], fp
);
1494 if (ferror(fp
) || fclose(fp
)) {
1495 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1496 progname
, fullname
);
1497 (void) exit(EXIT_FAILURE
);
1502 doabbr(abbr
, format
, letters
, isdst
)
1504 const char * const format
;
1505 const char * const letters
;
1508 if (strchr(format
, '/') == NULL
) {
1509 if (letters
== NULL
)
1510 (void) strcpy(abbr
, format
);
1511 else (void) sprintf(abbr
, format
, letters
);
1513 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1515 (void) strcpy(abbr
, format
);
1516 *strchr(abbr
, '/') = '\0';
1521 outzone(zpfirst
, zonecount
)
1522 const struct zone
* const zpfirst
;
1523 const int zonecount
;
1525 register const struct zone
* zp
;
1526 register struct rule
* rp
;
1528 register int usestart
, useuntil
;
1529 register time_t starttime
, untiltime
;
1530 register long gmtoff
;
1531 register long stdoff
;
1533 register long startoff
;
1534 register int startttisstd
;
1535 register int startttisgmt
;
1537 char startbuf
[BUFSIZ
];
1539 INITIALIZE(untiltime
);
1540 INITIALIZE(starttime
);
1542 ** Now. . .finally. . .generate some useful data!
1548 ** A guess that may well be corrected later.
1552 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1553 ** for noting the need to unconditionally initialize startttisstd.
1555 startttisstd
= FALSE
;
1556 startttisgmt
= FALSE
;
1557 for (i
= 0; i
< zonecount
; ++i
) {
1559 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1560 useuntil
= i
< (zonecount
- 1);
1561 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1563 gmtoff
= zp
->z_gmtoff
;
1564 eat(zp
->z_filename
, zp
->z_linenum
);
1566 startoff
= zp
->z_gmtoff
;
1567 if (zp
->z_nrules
== 0) {
1568 stdoff
= zp
->z_stdoff
;
1569 doabbr(startbuf
, zp
->z_format
,
1570 (char *) NULL
, stdoff
!= 0);
1571 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1572 startbuf
, stdoff
!= 0, startttisstd
,
1575 addtt(starttime
, type
);
1578 else if (stdoff
!= 0)
1579 addtt(min_time
, type
);
1580 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1581 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1584 ** Mark which rules to do in the current year.
1585 ** For those to do, calculate rpytime(rp, year);
1587 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1588 rp
= &zp
->z_rules
[j
];
1589 eats(zp
->z_filename
, zp
->z_linenum
,
1590 rp
->r_filename
, rp
->r_linenum
);
1591 rp
->r_todo
= year
>= rp
->r_loyear
&&
1592 year
<= rp
->r_hiyear
&&
1593 yearistype(year
, rp
->r_yrtype
);
1595 rp
->r_temp
= rpytime(rp
, year
);
1599 register time_t jtime
, ktime
;
1600 register long offset
;
1606 ** Turn untiltime into GMT
1607 ** assuming the current gmtoff and
1610 untiltime
= zp
->z_untiltime
;
1611 if (!zp
->z_untilrule
.r_todisgmt
)
1612 untiltime
= tadd(untiltime
,
1614 if (!zp
->z_untilrule
.r_todisstd
)
1615 untiltime
= tadd(untiltime
,
1619 ** Find the rule (of those to do, if any)
1620 ** that takes effect earliest in the year.
1623 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1624 rp
= &zp
->z_rules
[j
];
1627 eats(zp
->z_filename
, zp
->z_linenum
,
1628 rp
->r_filename
, rp
->r_linenum
);
1629 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1630 if (!rp
->r_todisstd
)
1631 offset
= oadd(offset
, stdoff
);
1633 if (jtime
== min_time
||
1636 jtime
= tadd(jtime
, -offset
);
1637 if (k
< 0 || jtime
< ktime
) {
1643 break; /* go on to next year */
1644 rp
= &zp
->z_rules
[k
];
1646 if (useuntil
&& ktime
>= untiltime
)
1648 stdoff
= rp
->r_stdoff
;
1649 if (usestart
&& ktime
== starttime
)
1652 if (ktime
< starttime
) {
1653 startoff
= oadd(zp
->z_gmtoff
,
1655 doabbr(startbuf
, zp
->z_format
,
1660 if (*startbuf
== '\0' &&
1661 startoff
== oadd(zp
->z_gmtoff
,
1663 doabbr(startbuf
, zp
->z_format
,
1668 eats(zp
->z_filename
, zp
->z_linenum
,
1669 rp
->r_filename
, rp
->r_linenum
);
1670 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1672 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1673 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1674 rp
->r_todisstd
, rp
->r_todisgmt
);
1679 if (*startbuf
== '\0' &&
1680 zp
->z_format
!= NULL
&&
1681 strchr(zp
->z_format
, '%') == NULL
&&
1682 strchr(zp
->z_format
, '/') == NULL
)
1683 (void) strcpy(startbuf
, zp
->z_format
);
1684 eat(zp
->z_filename
, zp
->z_linenum
);
1685 if (*startbuf
== '\0')
1686 error(_("can't determine time zone abbrevation to use just after until time"));
1687 else addtt(starttime
,
1688 addtype(startoff
, startbuf
,
1689 startoff
!= zp
->z_gmtoff
,
1694 ** Now we may get to set starttime for the next zone line.
1697 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1698 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1699 starttime
= zp
->z_untiltime
;
1701 starttime
= tadd(starttime
, -stdoff
);
1703 starttime
= tadd(starttime
, -gmtoff
);
1706 writezone(zpfirst
->z_name
);
1710 addtt(starttime
, type
)
1711 const time_t starttime
;
1714 if (timecnt
>= TZ_MAX_TIMES
) {
1715 error(_("too many transitions?!"));
1716 (void) exit(EXIT_FAILURE
);
1718 attypes
[timecnt
].at
= starttime
;
1719 attypes
[timecnt
].type
= type
;
1724 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1726 const char * const abbr
;
1733 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1734 error(_("internal error - addtype called with bad isdst"));
1735 (void) exit(EXIT_FAILURE
);
1737 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1738 error(_("internal error - addtype called with bad ttisstd"));
1739 (void) exit(EXIT_FAILURE
);
1741 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1742 error(_("internal error - addtype called with bad ttisgmt"));
1743 (void) exit(EXIT_FAILURE
);
1746 ** See if there's already an entry for this zone type.
1747 ** If so, just return its index.
1749 for (i
= 0; i
< typecnt
; ++i
) {
1750 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1751 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1752 ttisstd
== ttisstds
[i
] &&
1753 ttisgmt
== ttisgmts
[i
])
1757 ** There isn't one; add a new one, unless there are already too
1760 if (typecnt
>= TZ_MAX_TYPES
) {
1761 error(_("too many local time types"));
1762 (void) exit(EXIT_FAILURE
);
1764 gmtoffs
[i
] = gmtoff
;
1766 ttisstds
[i
] = ttisstd
;
1767 ttisgmts
[i
] = ttisgmt
;
1769 for (j
= 0; j
< charcnt
; ++j
)
1770 if (strcmp(&chars
[j
], abbr
) == 0)
1780 leapadd(t
, positive
, rolling
, count
)
1788 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1789 error(_("too many leap seconds"));
1790 (void) exit(EXIT_FAILURE
);
1792 for (i
= 0; i
< leapcnt
; ++i
)
1793 if (t
<= trans
[i
]) {
1794 if (t
== trans
[i
]) {
1795 error(_("repeated leap second moment"));
1796 (void) exit(EXIT_FAILURE
);
1801 for (j
= leapcnt
; j
> i
; --j
) {
1802 trans
[j
] = trans
[j
- 1];
1803 corr
[j
] = corr
[j
- 1];
1804 roll
[j
] = roll
[j
- 1];
1807 corr
[i
] = positive
? 1L : eitol(-count
);
1810 } while (positive
&& --count
!= 0);
1817 register long last
= 0;
1820 ** propagate leap seconds forward
1822 for (i
= 0; i
< leapcnt
; ++i
) {
1823 trans
[i
] = tadd(trans
[i
], last
);
1824 last
= corr
[i
] += last
;
1829 yearistype(year
, type
)
1831 const char * const type
;
1836 if (type
== NULL
|| *type
== '\0')
1838 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1839 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1840 result
= system(buf
);
1843 if (result
== (1 << 8))
1845 error(_("Wild result from command execution"));
1846 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1847 progname
, buf
, result
);
1849 (void) exit(EXIT_FAILURE
);
1856 a
= (unsigned char) a
;
1857 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1861 ciequal(ap
, bp
) /* case-insensitive equality */
1862 register const char * ap
;
1863 register const char * bp
;
1865 while (lowerit(*ap
) == lowerit(*bp
++))
1873 register const char * abbr
;
1874 register const char * word
;
1876 if (lowerit(*abbr
) != lowerit(*word
))
1879 while (*++abbr
!= '\0')
1883 } while (lowerit(*word
++) != lowerit(*abbr
));
1887 static const struct lookup
*
1889 register const char * const word
;
1890 register const struct lookup
* const table
;
1892 register const struct lookup
* foundlp
;
1893 register const struct lookup
* lp
;
1895 if (word
== NULL
|| table
== NULL
)
1898 ** Look for exact match.
1900 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1901 if (ciequal(word
, lp
->l_word
))
1904 ** Look for inexact match.
1907 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1908 if (itsabbr(word
, lp
->l_word
))
1909 if (foundlp
== NULL
)
1911 else return NULL
; /* multiple inexact matches */
1920 register char ** array
;
1925 array
= (char **) (void *)
1926 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1929 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1931 if (*cp
== '\0' || *cp
== '#')
1933 array
[nsubs
++] = dp
= cp
;
1935 if ((*dp
= *cp
++) != '"')
1937 else while ((*dp
= *cp
++) != '"')
1940 else error(_("Odd number of quotation marks"));
1941 } while (*cp
!= '\0' && *cp
!= '#' &&
1942 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1943 if (isascii(*cp
) && isspace((unsigned char) *cp
))
1947 array
[nsubs
] = NULL
;
1959 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1960 error(_("time overflow"));
1961 (void) exit(EXIT_FAILURE
);
1973 if (t1
== max_time
&& t2
> 0)
1975 if (t1
== min_time
&& t2
< 0)
1978 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1979 error(_("time overflow"));
1980 (void) exit(EXIT_FAILURE
);
1986 ** Given a rule, and a year, compute the date - in seconds since January 1,
1987 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
1991 rpytime(rp
, wantedy
)
1992 register const struct rule
* const rp
;
1993 register const int wantedy
;
1995 register int y
, m
, i
;
1996 register long dayoff
; /* with a nod to Margaret O. */
1999 if (wantedy
== INT_MIN
)
2001 if (wantedy
== INT_MAX
)
2006 while (wantedy
!= y
) {
2008 i
= len_years
[isleap(y
)];
2012 i
= -len_years
[isleap(y
)];
2014 dayoff
= oadd(dayoff
, eitol(i
));
2016 while (m
!= rp
->r_month
) {
2017 i
= len_months
[isleap(y
)][m
];
2018 dayoff
= oadd(dayoff
, eitol(i
));
2021 i
= rp
->r_dayofmonth
;
2022 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2023 if (rp
->r_dycode
== DC_DOWLEQ
)
2026 error(_("use of 2/29 in non leap-year"));
2027 (void) exit(EXIT_FAILURE
);
2031 dayoff
= oadd(dayoff
, eitol(i
));
2032 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2035 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2036 wday
= eitol(EPOCH_WDAY
);
2038 ** Don't trust mod of negative numbers.
2041 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2043 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2045 wday
+= LDAYSPERWEEK
;
2047 while (wday
!= eitol(rp
->r_wday
))
2048 if (rp
->r_dycode
== DC_DOWGEQ
) {
2049 dayoff
= oadd(dayoff
, (long) 1);
2050 if (++wday
>= LDAYSPERWEEK
)
2054 dayoff
= oadd(dayoff
, (long) -1);
2056 wday
= LDAYSPERWEEK
- 1;
2059 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2060 error(_("no day in month matches rule"));
2061 (void) exit(EXIT_FAILURE
);
2064 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2066 t
= (time_t) dayoff
* SECSPERDAY
;
2068 ** Cheap overflow check.
2070 if (t
/ SECSPERDAY
!= dayoff
)
2071 return (dayoff
> 0) ? max_time
: min_time
;
2072 return tadd(t
, rp
->r_tod
);
2077 const char * const string
;
2081 i
= strlen(string
) + 1;
2082 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2083 error(_("too many, or too long, time zone abbreviations"));
2084 (void) exit(EXIT_FAILURE
);
2086 (void) strcpy(&chars
[charcnt
], string
);
2087 charcnt
+= eitol(i
);
2092 char * const argname
;
2094 register char * name
;
2097 if (argname
== NULL
|| *argname
== '\0')
2099 cp
= name
= ecpyalloc(argname
);
2100 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2104 ** DOS drive specifier?
2106 if (isalpha((unsigned char) name
[0]) &&
2107 name
[1] == ':' && name
[2] == '\0') {
2111 #endif /* !defined unix */
2112 if (!itsdir(name
)) {
2114 ** It doesn't seem to exist, so we try to create it.
2116 if (mkdir(name
, 0755) != 0) {
2117 const char *e
= strerror(errno
);
2118 (void) fprintf(stderr
,
2119 _("%s: Can't create directory %s: %s\n"),
2138 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2139 (void) fprintf(stderr
,
2140 _("%s: %d did not sign extend correctly\n"),
2142 (void) exit(EXIT_FAILURE
);
2148 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.