]>
git.saurik.com Git - apple/system_cmds.git/blob - zic.tproj/zic.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
24 #if defined(LIBC_SCCS) && !defined(lint)
26 static char elsieid
[] = "@(#)zic.c 7.80";
28 static char rcsid
[] = "$OpenBSD: zic.c,v 1.6 1997/01/15 23:40:55 millert Exp $";
30 #endif /* LIBC_SCCS and not lint */
36 #include "sys/stat.h" /* for umask manifest constants */
37 #endif /* defined unix */
40 ** On some ancient hosts, predicates like `isspace(C)' are defined
41 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
42 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
43 ** Neither the C Standard nor Posix require that `isascii' exist.
44 ** For portability, we check both ancient and modern requirements.
45 ** If isascii is not defined, the isascii check succeeds trivially.
53 const char * r_filename
;
57 int r_loyear
; /* for example, 1986 */
58 int r_hiyear
; /* for example, 1986 */
59 const char * r_yrtype
;
61 int r_month
; /* 0..11 */
63 int r_dycode
; /* see below */
67 long r_tod
; /* time from midnight */
68 int r_todisstd
; /* above is standard time if TRUE */
69 /* or wall clock time if FALSE */
70 int r_todisgmt
; /* above is GMT if TRUE */
71 /* or local time if FALSE */
72 long r_stdoff
; /* offset from standard time */
73 const char * r_abbrvar
; /* variable part of abbreviation */
75 int r_todo
; /* a rule to do (used in outzone) */
76 time_t r_temp
; /* used in outzone */
80 ** r_dycode r_dayofmonth r_wday
83 #define DC_DOM 0 /* 1..31 */ /* unused */
84 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
85 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
88 const char * z_filename
;
94 const char * z_format
;
98 struct rule
* z_rules
;
101 struct rule z_untilrule
;
105 extern int getopt
P((int argc
, char * const argv
[],
106 const char * options
));
107 extern char * icatalloc
P((char * old
, const char * new));
108 extern char * icpyalloc
P((const char * string
));
109 extern void ifree
P((char * p
));
110 extern char * imalloc
P((int n
));
111 extern void * irealloc
P((void * old
, int n
));
112 extern int link
P((const char * fromname
, const char * toname
));
113 extern char * optarg
;
115 extern char * scheck
P((const char * string
, const char * format
));
117 static void addtt
P((time_t starttime
, int type
));
118 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
119 int ttisstd
, int ttisgmt
));
120 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
121 static void adjleap
P((void));
122 static void associate
P((void));
123 static int ciequal
P((const char * ap
, const char * bp
));
124 static void convert
P((long val
, char * buf
));
125 static void dolink
P((const char * fromfile
, const char * tofile
));
126 static void doabbr
P((char * abbr
, const char * format
,
127 const char * letters
, int isdst
));
128 static void eat
P((const char * name
, int num
));
129 static void eats
P((const char * name
, int num
,
130 const char * rname
, int rnum
));
131 static long eitol
P((int i
));
132 static void error
P((const char * message
));
133 static char ** getfields
P((char * buf
));
134 static long gethms
P((const char * string
, const char * errstrng
,
136 static void infile
P((const char * filename
));
137 static void inleap
P((char ** fields
, int nfields
));
138 static void inlink
P((char ** fields
, int nfields
));
139 static void inrule
P((char ** fields
, int nfields
));
140 static int inzcont
P((char ** fields
, int nfields
));
141 static int inzone
P((char ** fields
, int nfields
));
142 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
143 static int itsabbr
P((const char * abbr
, const char * word
));
144 static int itsdir
P((const char * name
));
145 static int lowerit
P((int c
));
146 static char * memcheck
P((char * tocheck
));
147 static int mkdirs
P((char * filename
));
148 static void newabbr
P((const char * abbr
));
149 static long oadd
P((long t1
, long t2
));
150 static void outzone
P((const struct zone
* zp
, int ntzones
));
151 static void puttzcode
P((long code
, FILE * fp
));
152 static int rcomp
P((const void * leftp
, const void * rightp
));
153 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
154 static void rulesub
P((struct rule
* rp
,
155 const char * loyearp
, const char * hiyearp
,
156 const char * typep
, const char * monthp
,
157 const char * dayp
, const char * timep
));
158 static void setboundaries
P((void));
159 static time_t tadd
P((time_t t1
, long t2
));
160 static void usage
P((void));
161 static void writezone
P((const char * name
));
162 static int yearistype
P((int year
, const char * type
));
164 #if !(HAVE_STRERROR - 0)
165 static char * strerror
P((int));
166 #endif /* !(HAVE_STRERROR - 0) */
170 static const char * filename
;
173 static time_t max_time
;
175 static time_t min_time
;
178 static const char * rfilename
;
180 static const char * progname
;
194 ** Which fields are which on a Zone line.
202 #define ZF_TILMONTH 6
205 #define ZONE_MINFIELDS 5
206 #define ZONE_MAXFIELDS 9
209 ** Which fields are which on a Zone continuation line.
215 #define ZFC_TILYEAR 3
216 #define ZFC_TILMONTH 4
218 #define ZFC_TILTIME 6
219 #define ZONEC_MINFIELDS 3
220 #define ZONEC_MAXFIELDS 7
223 ** Which files are which on a Rule line.
235 #define RULE_FIELDS 10
238 ** Which fields are which on a Link line.
243 #define LINK_FIELDS 3
246 ** Which fields are which on a Leap line.
255 #define LEAP_FIELDS 7
265 static struct rule
* rules
;
266 static int nrules
; /* number of rules */
268 static struct zone
* zones
;
269 static int nzones
; /* number of zones */
272 const char * l_filename
;
278 static struct link
* links
;
286 static struct lookup
const * byword
P((const char * string
,
287 const struct lookup
* lp
));
289 static struct lookup
const line_codes
[] = {
297 static struct lookup
const mon_names
[] = {
298 { "January", TM_JANUARY
},
299 { "February", TM_FEBRUARY
},
300 { "March", TM_MARCH
},
301 { "April", TM_APRIL
},
305 { "August", TM_AUGUST
},
306 { "September", TM_SEPTEMBER
},
307 { "October", TM_OCTOBER
},
308 { "November", TM_NOVEMBER
},
309 { "December", TM_DECEMBER
},
313 static struct lookup
const wday_names
[] = {
314 { "Sunday", TM_SUNDAY
},
315 { "Monday", TM_MONDAY
},
316 { "Tuesday", TM_TUESDAY
},
317 { "Wednesday", TM_WEDNESDAY
},
318 { "Thursday", TM_THURSDAY
},
319 { "Friday", TM_FRIDAY
},
320 { "Saturday", TM_SATURDAY
},
324 static struct lookup
const lasts
[] = {
325 { "last-Sunday", TM_SUNDAY
},
326 { "last-Monday", TM_MONDAY
},
327 { "last-Tuesday", TM_TUESDAY
},
328 { "last-Wednesday", TM_WEDNESDAY
},
329 { "last-Thursday", TM_THURSDAY
},
330 { "last-Friday", TM_FRIDAY
},
331 { "last-Saturday", TM_SATURDAY
},
335 static struct lookup
const begin_years
[] = {
336 { "minimum", YR_MINIMUM
},
337 { "maximum", YR_MAXIMUM
},
341 static struct lookup
const end_years
[] = {
342 { "minimum", YR_MINIMUM
},
343 { "maximum", YR_MAXIMUM
},
348 static struct lookup
const leap_types
[] = {
350 { "Stationary", FALSE
},
354 static const int len_months
[2][MONSPERYEAR
] = {
355 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
356 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
359 static const int len_years
[2] = {
360 DAYSPERNYEAR
, DAYSPERLYEAR
363 static struct attype
{
366 } attypes
[TZ_MAX_TIMES
];
367 static long gmtoffs
[TZ_MAX_TYPES
];
368 static char isdsts
[TZ_MAX_TYPES
];
369 static unsigned char abbrinds
[TZ_MAX_TYPES
];
370 static char ttisstds
[TZ_MAX_TYPES
];
371 static char ttisgmts
[TZ_MAX_TYPES
];
372 static char chars
[TZ_MAX_CHARS
];
373 static time_t trans
[TZ_MAX_LEAPS
];
374 static long corr
[TZ_MAX_LEAPS
];
375 static char roll
[TZ_MAX_LEAPS
];
378 ** Memory allocation.
386 const char *e
= strerror(errno
);
388 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
390 (void) exit(EXIT_FAILURE
);
395 #define emalloc(size) memcheck(imalloc(size))
396 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
397 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
398 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
404 #if !(HAVE_STRERROR - 0)
409 extern char * sys_errlist
[];
412 return (errnum
> 0 && errnum
<= sys_nerr
) ?
413 sys_errlist
[errnum
] : "Unknown system error";
415 #endif /* !(HAVE_STRERROR - 0) */
418 eats(name
, num
, rname
, rnum
)
419 const char * const name
;
421 const char * const rname
;
432 const char * const name
;
435 eats(name
, num
, (char *) NULL
, -1);
440 const char * const string
;
443 ** Match the format of "cc" to allow sh users to
444 ** zic ... 2>&1 | error -t "*" -v
447 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
448 filename
, linenum
, string
);
449 if (rfilename
!= NULL
)
450 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
451 rfilename
, rlinenum
);
452 (void) fprintf(stderr
, "\n");
458 const char * const string
;
462 cp
= ecpyalloc("warning: ");
463 cp
= ecatalloc(cp
, string
);
472 (void) fprintf(stderr
, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
474 (void) exit(EXIT_FAILURE
);
477 static const char * psxrules
;
478 static const char * lcltime
;
479 static const char * directory
;
480 static const char * leapsec
;
481 static const char * yitcommand
;
482 static int sflag
= FALSE
;
494 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
495 #endif /* defined unix */
497 (void) setlocale(LC_MESSAGES
, "");
499 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
500 #endif /* defined TEXTDOMAINDIR */
501 (void) textdomain(TZ_DOMAIN
);
502 #endif /* HAVE_GETTEXT - 0 */
504 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != -1)
509 if (directory
== NULL
)
512 (void) fprintf(stderr
,
513 _("%s: More than one -d option specified\n"),
515 (void) exit(EXIT_FAILURE
);
522 (void) fprintf(stderr
,
523 _("%s: More than one -l option specified\n"),
525 (void) exit(EXIT_FAILURE
);
529 if (psxrules
== NULL
)
532 (void) fprintf(stderr
,
533 _("%s: More than one -p option specified\n"),
535 (void) exit(EXIT_FAILURE
);
539 if (yitcommand
== NULL
)
542 (void) fprintf(stderr
,
543 _("%s: More than one -y option specified\n"),
545 (void) exit(EXIT_FAILURE
);
552 (void) fprintf(stderr
,
553 _("%s: More than one -L option specified\n"),
555 (void) exit(EXIT_FAILURE
);
565 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
566 usage(); /* usage message by request */
567 if (directory
== NULL
)
569 if (yitcommand
== NULL
)
570 yitcommand
= "yearistype";
574 if (optind
< argc
&& leapsec
!= NULL
) {
579 for (i
= optind
; i
< argc
; ++i
)
582 (void) exit(EXIT_FAILURE
);
584 for (i
= 0; i
< nzones
; i
= j
) {
586 ** Find the next non-continuation zone entry.
588 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
590 outzone(&zones
[i
], j
- i
);
595 for (i
= 0; i
< nlinks
; ++i
)
596 dolink(links
[i
].l_from
, links
[i
].l_to
);
598 dolink(lcltime
, TZDEFAULT
);
599 if (psxrules
!= NULL
)
600 dolink(psxrules
, TZDEFRULES
);
601 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
605 dolink(fromfile
, tofile
)
606 const char * const fromfile
;
607 const char * const tofile
;
609 register char * fromname
;
610 register char * toname
;
612 if (fromfile
[0] == '/')
613 fromname
= ecpyalloc(fromfile
);
615 fromname
= ecpyalloc(directory
);
616 fromname
= ecatalloc(fromname
, "/");
617 fromname
= ecatalloc(fromname
, fromfile
);
619 if (tofile
[0] == '/')
620 toname
= ecpyalloc(tofile
);
622 toname
= ecpyalloc(directory
);
623 toname
= ecatalloc(toname
, "/");
624 toname
= ecatalloc(toname
, tofile
);
627 ** We get to be careful here since
628 ** there's a fair chance of root running us.
631 (void) remove(toname
);
632 if (link(fromname
, toname
) != 0) {
633 if (mkdirs(toname
) != 0)
634 (void) exit(EXIT_FAILURE
);
635 if (link(fromname
, toname
) != 0) {
636 const char *e
= strerror(errno
);
638 (void) fprintf(stderr
,
639 _("%s: Can't link from %s to %s: %s\n"),
640 progname
, fromname
, toname
, e
);
641 (void) exit(EXIT_FAILURE
);
649 #define INT_MAX ((int) (((unsigned)~0)>>1))
650 #endif /* !defined INT_MAX */
653 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
654 #endif /* !defined INT_MIN */
657 ** The tz file format currently allows at most 32-bit quantities.
658 ** This restriction should be removed before signed 32-bit values
659 ** wrap around in 2038, but unfortunately this will require a
660 ** change to the tz file format.
663 #define MAX_BITS_IN_FILE 32
664 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
667 setboundaries
P((void))
669 if (TYPE_SIGNED(time_t)) {
670 min_time
= ~ (time_t) 0;
671 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
672 max_time
= ~ (time_t) 0 - min_time
;
677 max_time
= 2 - sflag
;
678 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
681 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
682 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
687 const char * const name
;
689 register char * myname
;
692 myname
= ecpyalloc(name
);
693 myname
= ecatalloc(myname
, "/.");
694 accres
= access(myname
, F_OK
);
700 ** Associate sets of rules with zones.
704 ** Sort by rule name.
712 return strcmp(((const struct rule
*) cp1
)->r_name
,
713 ((const struct rule
*) cp2
)->r_name
);
719 register struct zone
* zp
;
720 register struct rule
* rp
;
721 register int base
, out
;
725 (void) qsort((void *) rules
, (size_t) nrules
,
726 (size_t) sizeof *rules
, rcomp
);
727 for (i
= 0; i
< nrules
- 1; ++i
) {
728 if (strcmp(rules
[i
].r_name
,
729 rules
[i
+ 1].r_name
) != 0)
731 if (strcmp(rules
[i
].r_filename
,
732 rules
[i
+ 1].r_filename
) == 0)
734 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
735 warning(_("same rule name in multiple files"));
736 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
737 warning(_("same rule name in multiple files"));
738 for (j
= i
+ 2; j
< nrules
; ++j
) {
739 if (strcmp(rules
[i
].r_name
,
740 rules
[j
].r_name
) != 0)
742 if (strcmp(rules
[i
].r_filename
,
743 rules
[j
].r_filename
) == 0)
745 if (strcmp(rules
[i
+ 1].r_filename
,
746 rules
[j
].r_filename
) == 0)
753 for (i
= 0; i
< nzones
; ++i
) {
758 for (base
= 0; base
< nrules
; base
= out
) {
760 for (out
= base
+ 1; out
< nrules
; ++out
)
761 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
763 for (i
= 0; i
< nzones
; ++i
) {
765 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
768 zp
->z_nrules
= out
- base
;
771 for (i
= 0; i
< nzones
; ++i
) {
773 if (zp
->z_nrules
== 0) {
775 ** Maybe we have a local standard time offset.
777 eat(zp
->z_filename
, zp
->z_linenum
);
778 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
781 ** Note, though, that if there's no rule,
782 ** a '%s' in the format is a bad thing.
784 if (strchr(zp
->z_format
, '%') != 0)
785 error(_("%s in ruleless zone"));
789 (void) exit(EXIT_FAILURE
);
797 register char ** fields
;
799 register const struct lookup
* lp
;
800 register int nfields
;
801 register int wantcont
;
805 if (strcmp(name
, "-") == 0) {
806 name
= _("standard input");
808 } else if ((fp
= fopen(name
, "r")) == NULL
) {
809 const char *e
= strerror(errno
);
811 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
813 (void) exit(EXIT_FAILURE
);
816 for (num
= 1; ; ++num
) {
818 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
820 cp
= strchr(buf
, '\n');
822 error(_("line too long"));
823 (void) exit(EXIT_FAILURE
);
826 fields
= getfields(buf
);
828 while (fields
[nfields
] != NULL
) {
831 if (strcmp(fields
[nfields
], "-") == 0)
832 fields
[nfields
] = &nada
;
837 } else if (wantcont
) {
838 wantcont
= inzcont(fields
, nfields
);
840 lp
= byword(fields
[0], line_codes
);
842 error(_("input line of unknown type"));
843 else switch ((int) (lp
->l_value
)) {
845 inrule(fields
, nfields
);
849 wantcont
= inzone(fields
, nfields
);
852 inlink(fields
, nfields
);
857 (void) fprintf(stderr
,
858 _("%s: Leap line in non leap seconds file %s\n"),
860 else inleap(fields
, nfields
);
863 default: /* "cannot happen" */
864 (void) fprintf(stderr
,
865 _("%s: panic: Invalid l_value %d\n"),
866 progname
, lp
->l_value
);
867 (void) exit(EXIT_FAILURE
);
870 ifree((char *) fields
);
873 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
875 (void) exit(EXIT_FAILURE
);
877 if (fp
!= stdin
&& fclose(fp
)) {
878 const char *e
= strerror(errno
);
880 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
881 progname
, filename
, e
);
882 (void) exit(EXIT_FAILURE
);
885 error(_("expected continuation line not found"));
889 ** Convert a string of one of the forms
890 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
891 ** into a number of seconds.
892 ** A null string maps to zero.
893 ** Call error with errstring and return zero on errors.
897 gethms(string
, errstring
, signable
)
899 const char * const errstring
;
902 int hh
, mm
, ss
, sign
;
904 if (string
== NULL
|| *string
== '\0')
908 else if (*string
== '-') {
912 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
914 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
916 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
917 &hh
, &mm
, &ss
) != 3) {
921 if (hh
< 0 || hh
>= HOURSPERDAY
||
922 mm
< 0 || mm
>= MINSPERHOUR
||
923 ss
< 0 || ss
> SECSPERMIN
) {
928 (eitol(hh
* MINSPERHOUR
+ mm
) *
929 eitol(SECSPERMIN
) + eitol(ss
));
933 inrule(fields
, nfields
)
934 register char ** const fields
;
937 static struct rule r
;
939 if (nfields
!= RULE_FIELDS
) {
940 error(_("wrong number of fields on Rule line"));
943 if (*fields
[RF_NAME
] == '\0') {
944 error(_("nameless rule"));
947 r
.r_filename
= filename
;
948 r
.r_linenum
= linenum
;
949 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
950 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
951 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
952 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
953 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
954 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
955 (int) ((nrules
+ 1) * sizeof *rules
));
960 inzone(fields
, nfields
)
961 register char ** const fields
;
967 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
968 error(_("wrong number of fields on Zone line"));
971 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
972 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
974 _("\"Zone %s\" line and -l option are mutually exclusive"),
979 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
980 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
982 _("\"Zone %s\" line and -p option are mutually exclusive"),
987 for (i
= 0; i
< nzones
; ++i
)
988 if (zones
[i
].z_name
!= NULL
&&
989 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
990 buf
= erealloc(buf
, (int) (132 +
991 strlen(fields
[ZF_NAME
]) +
992 strlen(zones
[i
].z_filename
)));
994 _("duplicate zone name %s (file \"%s\", line %d)"),
1001 return inzsub(fields
, nfields
, FALSE
);
1005 inzcont(fields
, nfields
)
1006 register char ** const fields
;
1009 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
1010 error(_("wrong number of fields on Zone continuation line"));
1013 return inzsub(fields
, nfields
, TRUE
);
1017 inzsub(fields
, nfields
, iscont
)
1018 register char ** const fields
;
1023 static struct zone z
;
1024 register int i_gmtoff
, i_rule
, i_format
;
1025 register int i_untilyear
, i_untilmonth
;
1026 register int i_untilday
, i_untiltime
;
1027 register int hasuntil
;
1030 i_gmtoff
= ZFC_GMTOFF
;
1032 i_format
= ZFC_FORMAT
;
1033 i_untilyear
= ZFC_TILYEAR
;
1034 i_untilmonth
= ZFC_TILMONTH
;
1035 i_untilday
= ZFC_TILDAY
;
1036 i_untiltime
= ZFC_TILTIME
;
1039 i_gmtoff
= ZF_GMTOFF
;
1041 i_format
= ZF_FORMAT
;
1042 i_untilyear
= ZF_TILYEAR
;
1043 i_untilmonth
= ZF_TILMONTH
;
1044 i_untilday
= ZF_TILDAY
;
1045 i_untiltime
= ZF_TILTIME
;
1046 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1048 z
.z_filename
= filename
;
1049 z
.z_linenum
= linenum
;
1050 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid GMT offset"), TRUE
);
1051 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1052 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1053 error(_("invalid abbreviation format"));
1057 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1058 z
.z_format
= ecpyalloc(fields
[i_format
]);
1059 hasuntil
= nfields
> i_untilyear
;
1061 z
.z_untilrule
.r_filename
= filename
;
1062 z
.z_untilrule
.r_linenum
= linenum
;
1063 rulesub(&z
.z_untilrule
,
1064 fields
[i_untilyear
],
1067 (nfields
> i_untilmonth
) ?
1068 fields
[i_untilmonth
] : "Jan",
1069 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1070 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1071 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1072 z
.z_untilrule
.r_loyear
);
1073 if (iscont
&& nzones
> 0 &&
1074 z
.z_untiltime
> min_time
&&
1075 z
.z_untiltime
< max_time
&&
1076 zones
[nzones
- 1].z_untiltime
> min_time
&&
1077 zones
[nzones
- 1].z_untiltime
< max_time
&&
1078 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1079 error(_("Zone continuation line end time is not after end time of previous line"));
1083 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1084 (int) ((nzones
+ 1) * sizeof *zones
));
1085 zones
[nzones
++] = z
;
1087 ** If there was an UNTIL field on this line,
1088 ** there's more information about the zone on the next line.
1094 inleap(fields
, nfields
)
1095 register char ** const fields
;
1098 register const char * cp
;
1099 register const struct lookup
* lp
;
1101 int year
, month
, day
;
1105 if (nfields
!= LEAP_FIELDS
) {
1106 error(_("wrong number of fields on Leap line"));
1110 cp
= fields
[LP_YEAR
];
1111 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1115 error(_("invalid leaping year"));
1121 i
= len_years
[isleap(j
)];
1125 i
= -len_years
[isleap(j
)];
1127 dayoff
= oadd(dayoff
, eitol(i
));
1129 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1130 error(_("invalid month name"));
1133 month
= lp
->l_value
;
1135 while (j
!= month
) {
1136 i
= len_months
[isleap(year
)][j
];
1137 dayoff
= oadd(dayoff
, eitol(i
));
1140 cp
= fields
[LP_DAY
];
1141 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1142 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1143 error(_("invalid day of month"));
1146 dayoff
= oadd(dayoff
, eitol(day
- 1));
1147 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1148 error(_("time before zero"));
1151 t
= (time_t) dayoff
* SECSPERDAY
;
1153 ** Cheap overflow check.
1155 if (t
/ SECSPERDAY
!= dayoff
) {
1156 error(_("time overflow"));
1159 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1160 cp
= fields
[LP_CORR
];
1162 register int positive
;
1165 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1168 } else if (strcmp(cp
, "--") == 0) {
1171 } else if (strcmp(cp
, "+") == 0) {
1174 } else if (strcmp(cp
, "++") == 0) {
1178 error(_("illegal CORRECTION field on Leap line"));
1181 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1182 error(_("illegal Rolling/Stationary field on Leap line"));
1185 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1190 inlink(fields
, nfields
)
1191 register char ** const fields
;
1196 if (nfields
!= LINK_FIELDS
) {
1197 error(_("wrong number of fields on Link line"));
1200 if (*fields
[LF_FROM
] == '\0') {
1201 error(_("blank FROM field on Link line"));
1204 if (*fields
[LF_TO
] == '\0') {
1205 error(_("blank TO field on Link line"));
1208 l
.l_filename
= filename
;
1209 l
.l_linenum
= linenum
;
1210 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1211 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1212 links
= (struct link
*) (void *) erealloc((char *) links
,
1213 (int) ((nlinks
+ 1) * sizeof *links
));
1214 links
[nlinks
++] = l
;
1218 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1219 register struct rule
* const rp
;
1220 const char * const loyearp
;
1221 const char * const hiyearp
;
1222 const char * const typep
;
1223 const char * const monthp
;
1224 const char * const dayp
;
1225 const char * const timep
;
1227 register const struct lookup
* lp
;
1228 register const char * cp
;
1232 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1233 error(_("invalid month name"));
1236 rp
->r_month
= lp
->l_value
;
1237 rp
->r_todisstd
= FALSE
;
1238 rp
->r_todisgmt
= FALSE
;
1239 dp
= ecpyalloc(timep
);
1241 ep
= dp
+ strlen(dp
) - 1;
1242 switch (lowerit(*ep
)) {
1243 case 's': /* Standard */
1244 rp
->r_todisstd
= TRUE
;
1245 rp
->r_todisgmt
= FALSE
;
1248 case 'w': /* Wall */
1249 rp
->r_todisstd
= FALSE
;
1250 rp
->r_todisgmt
= FALSE
;
1252 case 'g': /* Greenwich */
1253 case 'u': /* Universal */
1254 case 'z': /* Zulu */
1255 rp
->r_todisstd
= TRUE
;
1256 rp
->r_todisgmt
= TRUE
;
1261 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1267 lp
= byword(cp
, begin_years
);
1268 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1270 rp
->r_loyear
= INT_MIN
;
1273 rp
->r_loyear
= INT_MAX
;
1275 default: /* "cannot happen" */
1276 (void) fprintf(stderr
,
1277 _("%s: panic: Invalid l_value %d\n"),
1278 progname
, lp
->l_value
);
1279 (void) exit(EXIT_FAILURE
);
1280 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1281 error(_("invalid starting year"));
1285 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1287 rp
->r_hiyear
= INT_MIN
;
1290 rp
->r_hiyear
= INT_MAX
;
1293 rp
->r_hiyear
= rp
->r_loyear
;
1295 default: /* "cannot happen" */
1296 (void) fprintf(stderr
,
1297 _("%s: panic: Invalid l_value %d\n"),
1298 progname
, lp
->l_value
);
1299 (void) exit(EXIT_FAILURE
);
1300 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1301 error(_("invalid ending year"));
1304 if (rp
->r_loyear
> rp
->r_hiyear
) {
1305 error(_("starting year greater than ending year"));
1309 rp
->r_yrtype
= NULL
;
1311 if (rp
->r_loyear
== rp
->r_hiyear
) {
1312 error(_("typed single year"));
1315 rp
->r_yrtype
= ecpyalloc(typep
);
1319 ** Accept things such as:
1325 dp
= ecpyalloc(dayp
);
1326 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1327 rp
->r_dycode
= DC_DOWLEQ
;
1328 rp
->r_wday
= lp
->l_value
;
1329 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1331 if ((ep
= strchr(dp
, '<')) != 0)
1332 rp
->r_dycode
= DC_DOWLEQ
;
1333 else if ((ep
= strchr(dp
, '>')) != 0)
1334 rp
->r_dycode
= DC_DOWGEQ
;
1337 rp
->r_dycode
= DC_DOM
;
1339 if (rp
->r_dycode
!= DC_DOM
) {
1342 error(_("invalid day of month"));
1346 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1347 error(_("invalid weekday name"));
1351 rp
->r_wday
= lp
->l_value
;
1353 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1354 rp
->r_dayofmonth
<= 0 ||
1355 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1356 error(_("invalid day of month"));
1370 register long shift
;
1372 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1373 buf
[i
] = val
>> shift
;
1384 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1392 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1394 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1401 const char * const name
;
1405 static char * fullname
;
1406 static struct tzhead tzh
;
1407 time_t ats
[TZ_MAX_TIMES
];
1408 unsigned char types
[TZ_MAX_TIMES
];
1414 (void) qsort((void *) attypes
, (size_t) timecnt
,
1415 (size_t) sizeof *attypes
, atcomp
);
1426 while (attypes
[fromi
].type
== 0)
1427 ++fromi
; /* handled by default rule */
1428 for ( ; fromi
< timecnt
; ++fromi
) {
1430 && ((attypes
[fromi
].at
1431 + gmtoffs
[attypes
[toi
- 1].type
])
1432 <= (attypes
[toi
- 1].at
1433 + gmtoffs
[toi
== 1 ? 0
1434 : attypes
[toi
- 2].type
]))) {
1435 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1439 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1440 attypes
[toi
++] = attypes
[fromi
];
1447 for (i
= 0; i
< timecnt
; ++i
) {
1448 ats
[i
] = attypes
[i
].at
;
1449 types
[i
] = attypes
[i
].type
;
1451 fullname
= erealloc(fullname
,
1452 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1453 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1455 ** Remove old file, if any, to snap links.
1457 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1458 const char *e
= strerror(errno
);
1460 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1461 progname
, fullname
, e
);
1462 (void) exit(EXIT_FAILURE
);
1464 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1465 if (mkdirs(fullname
) != 0)
1466 (void) exit(EXIT_FAILURE
);
1467 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1468 const char *e
= strerror(errno
);
1470 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1471 progname
, fullname
, e
);
1472 (void) exit(EXIT_FAILURE
);
1475 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1476 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1477 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1478 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1479 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1480 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1481 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1490 for (i
= 0; i
< timecnt
; ++i
) {
1493 if (ats
[i
] >= trans
[j
]) {
1494 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1497 puttzcode((long) ats
[i
], fp
);
1500 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1501 (size_t) timecnt
, fp
);
1502 for (i
= 0; i
< typecnt
; ++i
) {
1503 puttzcode((long) gmtoffs
[i
], fp
);
1504 (void) putc(isdsts
[i
], fp
);
1505 (void) putc(abbrinds
[i
], fp
);
1508 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1509 (size_t) charcnt
, fp
);
1510 for (i
= 0; i
< leapcnt
; ++i
) {
1512 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1515 if (++j
>= typecnt
) {
1521 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1525 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1526 } else puttzcode((long) trans
[i
], fp
);
1527 puttzcode((long) corr
[i
], fp
);
1529 for (i
= 0; i
< typecnt
; ++i
)
1530 (void) putc(ttisstds
[i
], fp
);
1531 for (i
= 0; i
< typecnt
; ++i
)
1532 (void) putc(ttisgmts
[i
], fp
);
1533 if (ferror(fp
) || fclose(fp
)) {
1534 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1535 progname
, fullname
);
1536 (void) exit(EXIT_FAILURE
);
1541 doabbr(abbr
, format
, letters
, isdst
)
1543 const char * const format
;
1544 const char * const letters
;
1547 if (strchr(format
, '/') == NULL
) {
1548 if (letters
== NULL
)
1549 (void) strcpy(abbr
, format
);
1550 else (void) sprintf(abbr
, format
, letters
);
1552 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1554 (void) strcpy(abbr
, format
);
1555 *strchr(abbr
, '/') = '\0';
1560 outzone(zpfirst
, zonecount
)
1561 const struct zone
* const zpfirst
;
1562 const int zonecount
;
1564 register const struct zone
* zp
;
1565 register struct rule
* rp
;
1567 register int usestart
, useuntil
;
1568 register time_t starttime
, untiltime
;
1569 register long gmtoff
;
1570 register long stdoff
;
1572 register long startoff
;
1573 register int startttisstd
;
1574 register int startttisgmt
;
1576 char startbuf
[BUFSIZ
];
1578 INITIALIZE(untiltime
);
1579 INITIALIZE(starttime
);
1581 ** Now. . .finally. . .generate some useful data!
1587 ** A guess that may well be corrected later.
1591 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1592 ** for noting the need to unconditionally initialize startttisstd.
1594 startttisstd
= FALSE
;
1595 startttisgmt
= FALSE
;
1596 for (i
= 0; i
< zonecount
; ++i
) {
1598 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1599 useuntil
= i
< (zonecount
- 1);
1600 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1602 gmtoff
= zp
->z_gmtoff
;
1603 eat(zp
->z_filename
, zp
->z_linenum
);
1605 startoff
= zp
->z_gmtoff
;
1606 if (zp
->z_nrules
== 0) {
1607 stdoff
= zp
->z_stdoff
;
1608 doabbr(startbuf
, zp
->z_format
,
1609 (char *) NULL
, stdoff
!= 0);
1610 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1611 startbuf
, stdoff
!= 0, startttisstd
,
1614 addtt(starttime
, type
);
1617 else if (stdoff
!= 0)
1618 addtt(min_time
, type
);
1619 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1620 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1623 ** Mark which rules to do in the current year.
1624 ** For those to do, calculate rpytime(rp, year);
1626 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1627 rp
= &zp
->z_rules
[j
];
1628 eats(zp
->z_filename
, zp
->z_linenum
,
1629 rp
->r_filename
, rp
->r_linenum
);
1630 rp
->r_todo
= year
>= rp
->r_loyear
&&
1631 year
<= rp
->r_hiyear
&&
1632 yearistype(year
, rp
->r_yrtype
);
1634 rp
->r_temp
= rpytime(rp
, year
);
1638 register time_t jtime
, ktime
;
1639 register long offset
;
1645 ** Turn untiltime into GMT
1646 ** assuming the current gmtoff and
1649 untiltime
= zp
->z_untiltime
;
1650 if (!zp
->z_untilrule
.r_todisgmt
)
1651 untiltime
= tadd(untiltime
,
1653 if (!zp
->z_untilrule
.r_todisstd
)
1654 untiltime
= tadd(untiltime
,
1658 ** Find the rule (of those to do, if any)
1659 ** that takes effect earliest in the year.
1662 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1663 rp
= &zp
->z_rules
[j
];
1666 eats(zp
->z_filename
, zp
->z_linenum
,
1667 rp
->r_filename
, rp
->r_linenum
);
1668 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1669 if (!rp
->r_todisstd
)
1670 offset
= oadd(offset
, stdoff
);
1672 if (jtime
== min_time
||
1675 jtime
= tadd(jtime
, -offset
);
1676 if (k
< 0 || jtime
< ktime
) {
1682 break; /* go on to next year */
1683 rp
= &zp
->z_rules
[k
];
1685 if (useuntil
&& ktime
>= untiltime
)
1687 stdoff
= rp
->r_stdoff
;
1688 if (usestart
&& ktime
== starttime
)
1691 if (ktime
< starttime
) {
1692 startoff
= oadd(zp
->z_gmtoff
,
1694 doabbr(startbuf
, zp
->z_format
,
1699 if (*startbuf
== '\0' &&
1700 startoff
== oadd(zp
->z_gmtoff
,
1702 doabbr(startbuf
, zp
->z_format
,
1707 eats(zp
->z_filename
, zp
->z_linenum
,
1708 rp
->r_filename
, rp
->r_linenum
);
1709 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1711 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1712 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1713 rp
->r_todisstd
, rp
->r_todisgmt
);
1718 if (*startbuf
== '\0' &&
1719 zp
->z_format
!= NULL
&&
1720 strchr(zp
->z_format
, '%') == NULL
&&
1721 strchr(zp
->z_format
, '/') == NULL
)
1722 (void) strcpy(startbuf
, zp
->z_format
);
1723 eat(zp
->z_filename
, zp
->z_linenum
);
1724 if (*startbuf
== '\0')
1725 error(_("can't determine time zone abbreviation to use just after until time"));
1726 else addtt(starttime
,
1727 addtype(startoff
, startbuf
,
1728 startoff
!= zp
->z_gmtoff
,
1733 ** Now we may get to set starttime for the next zone line.
1736 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1737 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1738 starttime
= zp
->z_untiltime
;
1740 starttime
= tadd(starttime
, -stdoff
);
1742 starttime
= tadd(starttime
, -gmtoff
);
1745 writezone(zpfirst
->z_name
);
1749 addtt(starttime
, type
)
1750 const time_t starttime
;
1753 if (timecnt
>= TZ_MAX_TIMES
) {
1754 error(_("too many transitions?!"));
1755 (void) exit(EXIT_FAILURE
);
1757 attypes
[timecnt
].at
= starttime
;
1758 attypes
[timecnt
].type
= type
;
1763 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1765 const char * const abbr
;
1772 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1773 error(_("internal error - addtype called with bad isdst"));
1774 (void) exit(EXIT_FAILURE
);
1776 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1777 error(_("internal error - addtype called with bad ttisstd"));
1778 (void) exit(EXIT_FAILURE
);
1780 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1781 error(_("internal error - addtype called with bad ttisgmt"));
1782 (void) exit(EXIT_FAILURE
);
1785 ** See if there's already an entry for this zone type.
1786 ** If so, just return its index.
1788 for (i
= 0; i
< typecnt
; ++i
) {
1789 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1790 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1791 ttisstd
== ttisstds
[i
] &&
1792 ttisgmt
== ttisgmts
[i
])
1796 ** There isn't one; add a new one, unless there are already too
1799 if (typecnt
>= TZ_MAX_TYPES
) {
1800 error(_("too many local time types"));
1801 (void) exit(EXIT_FAILURE
);
1803 gmtoffs
[i
] = gmtoff
;
1805 ttisstds
[i
] = ttisstd
;
1806 ttisgmts
[i
] = ttisgmt
;
1808 for (j
= 0; j
< charcnt
; ++j
)
1809 if (strcmp(&chars
[j
], abbr
) == 0)
1819 leapadd(t
, positive
, rolling
, count
)
1827 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1828 error(_("too many leap seconds"));
1829 (void) exit(EXIT_FAILURE
);
1831 for (i
= 0; i
< leapcnt
; ++i
)
1832 if (t
<= trans
[i
]) {
1833 if (t
== trans
[i
]) {
1834 error(_("repeated leap second moment"));
1835 (void) exit(EXIT_FAILURE
);
1840 for (j
= leapcnt
; j
> i
; --j
) {
1841 trans
[j
] = trans
[j
- 1];
1842 corr
[j
] = corr
[j
- 1];
1843 roll
[j
] = roll
[j
- 1];
1846 corr
[i
] = positive
? 1L : eitol(-count
);
1849 } while (positive
&& --count
!= 0);
1856 register long last
= 0;
1859 ** propagate leap seconds forward
1861 for (i
= 0; i
< leapcnt
; ++i
) {
1862 trans
[i
] = tadd(trans
[i
], last
);
1863 last
= corr
[i
] += last
;
1868 yearistype(year
, type
)
1870 const char * const type
;
1875 if (type
== NULL
|| *type
== '\0')
1877 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1878 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1879 result
= system(buf
);
1882 if (result
== (1 << 8))
1884 error(_("Wild result from command execution"));
1885 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
1886 progname
, buf
, result
);
1888 (void) exit(EXIT_FAILURE
);
1895 a
= (unsigned char) a
;
1896 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1900 ciequal(ap
, bp
) /* case-insensitive equality */
1901 register const char * ap
;
1902 register const char * bp
;
1904 while (lowerit(*ap
) == lowerit(*bp
++))
1912 register const char * abbr
;
1913 register const char * word
;
1915 if (lowerit(*abbr
) != lowerit(*word
))
1918 while (*++abbr
!= '\0')
1922 } while (lowerit(*word
++) != lowerit(*abbr
));
1926 static const struct lookup
*
1928 register const char * const word
;
1929 register const struct lookup
* const table
;
1931 register const struct lookup
* foundlp
;
1932 register const struct lookup
* lp
;
1934 if (word
== NULL
|| table
== NULL
)
1937 ** Look for exact match.
1939 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1940 if (ciequal(word
, lp
->l_word
))
1943 ** Look for inexact match.
1946 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1947 if (itsabbr(word
, lp
->l_word
))
1948 if (foundlp
== NULL
)
1950 else return NULL
; /* multiple inexact matches */
1959 register char ** array
;
1964 array
= (char **) (void *)
1965 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1968 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1970 if (*cp
== '\0' || *cp
== '#')
1972 array
[nsubs
++] = dp
= cp
;
1974 if ((*dp
= *cp
++) != '"')
1976 else while ((*dp
= *cp
++) != '"')
1979 else error(_("Odd number of quotation marks"));
1980 } while (*cp
!= '\0' && *cp
!= '#' &&
1981 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1982 if (isascii(*cp
) && isspace((unsigned char) *cp
))
1986 array
[nsubs
] = NULL
;
1998 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
1999 error(_("time overflow"));
2000 (void) exit(EXIT_FAILURE
);
2012 if (t1
== max_time
&& t2
> 0)
2014 if (t1
== min_time
&& t2
< 0)
2017 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2018 error(_("time overflow"));
2019 (void) exit(EXIT_FAILURE
);
2025 ** Given a rule, and a year, compute the date - in seconds since January 1,
2026 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2030 rpytime(rp
, wantedy
)
2031 register const struct rule
* const rp
;
2032 register const int wantedy
;
2034 register int y
, m
, i
;
2035 register long dayoff
; /* with a nod to Margaret O. */
2038 if (wantedy
== INT_MIN
)
2040 if (wantedy
== INT_MAX
)
2045 while (wantedy
!= y
) {
2047 i
= len_years
[isleap(y
)];
2051 i
= -len_years
[isleap(y
)];
2053 dayoff
= oadd(dayoff
, eitol(i
));
2055 while (m
!= rp
->r_month
) {
2056 i
= len_months
[isleap(y
)][m
];
2057 dayoff
= oadd(dayoff
, eitol(i
));
2060 i
= rp
->r_dayofmonth
;
2061 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2062 if (rp
->r_dycode
== DC_DOWLEQ
)
2065 error(_("use of 2/29 in non leap-year"));
2066 (void) exit(EXIT_FAILURE
);
2070 dayoff
= oadd(dayoff
, eitol(i
));
2071 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2074 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2075 wday
= eitol(EPOCH_WDAY
);
2077 ** Don't trust mod of negative numbers.
2080 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2082 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2084 wday
+= LDAYSPERWEEK
;
2086 while (wday
!= eitol(rp
->r_wday
))
2087 if (rp
->r_dycode
== DC_DOWGEQ
) {
2088 dayoff
= oadd(dayoff
, (long) 1);
2089 if (++wday
>= LDAYSPERWEEK
)
2093 dayoff
= oadd(dayoff
, (long) -1);
2095 wday
= LDAYSPERWEEK
- 1;
2098 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2099 error(_("no day in month matches rule"));
2100 (void) exit(EXIT_FAILURE
);
2103 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2105 t
= (time_t) dayoff
* SECSPERDAY
;
2107 ** Cheap overflow check.
2109 if (t
/ SECSPERDAY
!= dayoff
)
2110 return (dayoff
> 0) ? max_time
: min_time
;
2111 return tadd(t
, rp
->r_tod
);
2116 const char * const string
;
2120 i
= strlen(string
) + 1;
2121 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2122 error(_("too many, or too long, time zone abbreviations"));
2123 (void) exit(EXIT_FAILURE
);
2125 (void) strcpy(&chars
[charcnt
], string
);
2126 charcnt
+= eitol(i
);
2131 char * const argname
;
2133 register char * name
;
2136 if (argname
== NULL
|| *argname
== '\0')
2138 cp
= name
= ecpyalloc(argname
);
2139 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2143 ** DOS drive specifier?
2145 if (isalpha((unsigned char) name
[0]) &&
2146 name
[1] == ':' && name
[2] == '\0') {
2150 #endif /* !defined unix */
2151 if (!itsdir(name
)) {
2153 ** It doesn't seem to exist, so we try to create it.
2155 if (mkdir(name
, 0755) != 0) {
2156 const char *e
= strerror(errno
);
2158 (void) fprintf(stderr
,
2159 _("%s: Can't create directory %s: %s\n"),
2178 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0)) {
2179 (void) fprintf(stderr
,
2180 _("%s: %d did not sign extend correctly\n"),
2182 (void) exit(EXIT_FAILURE
);
2188 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.