2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
13 #define ZIC_VERSION_PRE_2013 '2'
14 #define ZIC_VERSION '3'
16 typedef int_fast64_t zic_t
;
17 #define ZIC_MIN INT_FAST64_MIN
18 #define ZIC_MAX INT_FAST64_MAX
19 #define SCNdZIC SCNdFAST64
21 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
22 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
23 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
29 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
31 #define MKDIR_UMASK 0755
35 ** On some ancient hosts, predicates like `isspace(C)' are defined
36 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
37 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
38 ** Neither the C Standard nor Posix require that `isascii' exist.
39 ** For portability, we check both ancient and modern requirements.
40 ** If isascii is not defined, the isascii check succeeds trivially.
47 #define end(cp) (strchr((cp), '\0'))
50 const char * r_filename
;
54 zic_t r_loyear
; /* for example, 1986 */
55 zic_t r_hiyear
; /* for example, 1986 */
56 const char * r_yrtype
;
60 int r_month
; /* 0..11 */
62 int r_dycode
; /* see below */
66 zic_t r_tod
; /* time from midnight */
67 int r_todisstd
; /* above is standard time if TRUE */
68 /* or wall clock time if FALSE */
69 int r_todisgmt
; /* above is GMT if TRUE */
70 /* or local time if FALSE */
71 zic_t r_stdoff
; /* offset from standard time */
72 const char * r_abbrvar
; /* variable part of abbreviation */
74 int r_todo
; /* a rule to do (used in outzone) */
75 zic_t r_temp
; /* used in outzone */
79 ** r_dycode r_dayofmonth r_wday
82 #define DC_DOM 0 /* 1..31 */ /* unused */
83 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
84 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
87 const char * z_filename
;
93 const char * z_format
;
97 struct rule
* z_rules
;
100 struct rule z_untilrule
;
104 extern int getopt(int argc
, char * const argv
[],
105 const char * options
);
106 extern int link(const char * fromname
, const char * toname
);
107 extern char * optarg
;
111 # define link(from, to) (-1)
114 # define symlink(from, to) (-1)
117 static void addtt(zic_t starttime
, int type
);
118 static int addtype(zic_t gmtoff
, const char * abbr
, int isdst
,
119 int ttisstd
, int ttisgmt
);
120 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
121 static void adjleap(void);
122 static void associate(void);
123 static void dolink(const char * fromfield
, const char * tofield
);
124 static char ** getfields(char * buf
);
125 static zic_t
gethms(const char * string
, const char * errstrng
,
127 static void infile(const char * filename
);
128 static void inleap(char ** fields
, int nfields
);
129 static void inlink(char ** fields
, int nfields
);
130 static void inrule(char ** fields
, int nfields
);
131 static int inzcont(char ** fields
, int nfields
);
132 static int inzone(char ** fields
, int nfields
);
133 static int inzsub(char ** fields
, int nfields
, int iscont
);
134 static int itsdir(const char * name
);
135 static int lowerit(int c
);
136 static int mkdirs(char * filename
);
137 static void newabbr(const char * abbr
);
138 static zic_t
oadd(zic_t t1
, zic_t t2
);
139 static void outzone(const struct zone
* zp
, int ntzones
);
140 static zic_t
rpytime(const struct rule
* rp
, zic_t wantedy
);
141 static void rulesub(struct rule
* rp
,
142 const char * loyearp
, const char * hiyearp
,
143 const char * typep
, const char * monthp
,
144 const char * dayp
, const char * timep
);
145 static zic_t
tadd(zic_t t1
, zic_t t2
);
146 static int yearistype(int year
, const char * type
);
150 static const char * filename
;
153 static zic_t leapminyear
;
154 static zic_t leapmaxyear
;
156 static int max_abbrvar_len
;
157 static int max_format_len
;
158 static zic_t max_year
;
159 static zic_t min_year
;
161 static const char * rfilename
;
163 static const char * progname
;
177 ** Which fields are which on a Zone line.
185 #define ZF_TILMONTH 6
188 #define ZONE_MINFIELDS 5
189 #define ZONE_MAXFIELDS 9
192 ** Which fields are which on a Zone continuation line.
198 #define ZFC_TILYEAR 3
199 #define ZFC_TILMONTH 4
201 #define ZFC_TILTIME 6
202 #define ZONEC_MINFIELDS 3
203 #define ZONEC_MAXFIELDS 7
206 ** Which files are which on a Rule line.
218 #define RULE_FIELDS 10
221 ** Which fields are which on a Link line.
226 #define LINK_FIELDS 3
229 ** Which fields are which on a Leap line.
238 #define LEAP_FIELDS 7
248 static struct rule
* rules
;
249 static int nrules
; /* number of rules */
251 static struct zone
* zones
;
252 static int nzones
; /* number of zones */
255 const char * l_filename
;
261 static struct link
* links
;
269 static struct lookup
const * byword(const char * string
,
270 const struct lookup
* lp
);
272 static struct lookup
const line_codes
[] = {
280 static struct lookup
const mon_names
[] = {
281 { "January", TM_JANUARY
},
282 { "February", TM_FEBRUARY
},
283 { "March", TM_MARCH
},
284 { "April", TM_APRIL
},
288 { "August", TM_AUGUST
},
289 { "September", TM_SEPTEMBER
},
290 { "October", TM_OCTOBER
},
291 { "November", TM_NOVEMBER
},
292 { "December", TM_DECEMBER
},
296 static struct lookup
const wday_names
[] = {
297 { "Sunday", TM_SUNDAY
},
298 { "Monday", TM_MONDAY
},
299 { "Tuesday", TM_TUESDAY
},
300 { "Wednesday", TM_WEDNESDAY
},
301 { "Thursday", TM_THURSDAY
},
302 { "Friday", TM_FRIDAY
},
303 { "Saturday", TM_SATURDAY
},
307 static struct lookup
const lasts
[] = {
308 { "last-Sunday", TM_SUNDAY
},
309 { "last-Monday", TM_MONDAY
},
310 { "last-Tuesday", TM_TUESDAY
},
311 { "last-Wednesday", TM_WEDNESDAY
},
312 { "last-Thursday", TM_THURSDAY
},
313 { "last-Friday", TM_FRIDAY
},
314 { "last-Saturday", TM_SATURDAY
},
318 static struct lookup
const begin_years
[] = {
319 { "minimum", YR_MINIMUM
},
320 { "maximum", YR_MAXIMUM
},
324 static struct lookup
const end_years
[] = {
325 { "minimum", YR_MINIMUM
},
326 { "maximum", YR_MAXIMUM
},
331 static struct lookup
const leap_types
[] = {
333 { "Stationary", FALSE
},
337 static const int len_months
[2][MONSPERYEAR
] = {
338 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
339 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
342 static const int len_years
[2] = {
343 DAYSPERNYEAR
, DAYSPERLYEAR
346 static struct attype
{
349 } attypes
[TZ_MAX_TIMES
];
350 static zic_t gmtoffs
[TZ_MAX_TYPES
];
351 static char isdsts
[TZ_MAX_TYPES
];
352 static unsigned char abbrinds
[TZ_MAX_TYPES
];
353 static char ttisstds
[TZ_MAX_TYPES
];
354 static char ttisgmts
[TZ_MAX_TYPES
];
355 static char chars
[TZ_MAX_CHARS
];
356 static zic_t trans
[TZ_MAX_LEAPS
];
357 static zic_t corr
[TZ_MAX_LEAPS
];
358 static char roll
[TZ_MAX_LEAPS
];
361 ** Memory allocation.
364 static ATTRIBUTE_PURE
void *
365 memcheck(void *const ptr
)
368 const char *e
= strerror(errno
);
370 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
377 #define emalloc(size) memcheck(malloc(size))
378 #define erealloc(ptr, size) memcheck(realloc(ptr, size))
379 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
380 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
387 eats(const char *const name
, const int num
, const char *const rname
,
397 eat(const char *const name
, const int num
)
399 eats(name
, num
, NULL
, -1);
402 static void ATTRIBUTE_FORMAT((printf
, 1, 0))
403 verror(const char *const string
, va_list args
)
406 ** Match the format of "cc" to allow sh users to
407 ** zic ... 2>&1 | error -t "*" -v
410 fprintf(stderr
, _("\"%s\", line %d: "), filename
, linenum
);
411 vfprintf(stderr
, string
, args
);
412 if (rfilename
!= NULL
)
413 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
414 rfilename
, rlinenum
);
415 (void) fprintf(stderr
, "\n");
419 static void ATTRIBUTE_FORMAT((printf
, 1, 2))
420 error(const char *const string
, ...)
423 va_start(args
, string
);
424 verror(string
, args
);
428 static void ATTRIBUTE_FORMAT((printf
, 1, 2))
429 warning(const char *const string
, ...)
432 fprintf(stderr
, _("warning: "));
433 va_start(args
, string
);
434 verror(string
, args
);
439 static _Noreturn
void
440 usage(FILE *stream
, int status
)
442 (void) fprintf(stream
, _("%s: usage is %s \
443 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
444 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
446 Report bugs to %s.\n"),
447 progname
, progname
, REPORT_BUGS_TO
);
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
;
458 main(int argc
, char **argv
)
465 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
468 (void) setlocale(LC_ALL
, "");
470 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
471 #endif /* defined TEXTDOMAINDIR */
472 (void) textdomain(TZ_DOMAIN
);
473 #endif /* HAVE_GETTEXT */
475 if (TYPE_BIT(zic_t
) < 64) {
476 (void) fprintf(stderr
, "%s: %s\n", progname
,
477 _("wild compilation-time specification of zic_t"));
480 for (i
= 1; i
< argc
; ++i
)
481 if (strcmp(argv
[i
], "--version") == 0) {
482 (void) printf("zic %s%s\n", PKGVERSION
, TZVERSION
);
484 } else if (strcmp(argv
[i
], "--help") == 0) {
485 usage(stdout
, EXIT_SUCCESS
);
487 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
490 usage(stderr
, EXIT_FAILURE
);
492 if (directory
== NULL
)
495 (void) fprintf(stderr
,
496 _("%s: More than one -d option specified\n"),
505 (void) fprintf(stderr
,
506 _("%s: More than one -l option specified\n"),
512 if (psxrules
== NULL
)
515 (void) fprintf(stderr
,
516 _("%s: More than one -p option specified\n"),
522 if (yitcommand
== NULL
)
525 (void) fprintf(stderr
,
526 _("%s: More than one -y option specified\n"),
535 (void) fprintf(stderr
,
536 _("%s: More than one -L option specified\n"),
545 (void) printf("%s: -s ignored\n", progname
);
548 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
549 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
550 if (directory
== NULL
)
552 if (yitcommand
== NULL
)
553 yitcommand
= "yearistype";
555 if (optind
< argc
&& leapsec
!= NULL
) {
560 for (i
= optind
; i
< argc
; ++i
)
565 for (i
= 0; i
< nzones
; i
= j
) {
567 ** Find the next non-continuation zone entry.
569 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
571 outzone(&zones
[i
], j
- i
);
576 for (i
= 0; i
< nlinks
; ++i
) {
577 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
578 dolink(links
[i
].l_from
, links
[i
].l_to
);
580 for (j
= 0; j
< nlinks
; ++j
)
581 if (strcmp(links
[i
].l_to
,
582 links
[j
].l_from
) == 0)
583 warning(_("link to link"));
585 if (lcltime
!= NULL
) {
586 eat("command line", 1);
587 dolink(lcltime
, TZDEFAULT
);
589 if (psxrules
!= NULL
) {
590 eat("command line", 1);
591 dolink(psxrules
, TZDEFRULES
);
593 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
597 dolink(const char *const fromfield
, const char *const tofield
)
599 register char * fromname
;
600 register char * toname
;
602 if (fromfield
[0] == '/')
603 fromname
= ecpyalloc(fromfield
);
605 fromname
= ecpyalloc(directory
);
606 fromname
= ecatalloc(fromname
, "/");
607 fromname
= ecatalloc(fromname
, fromfield
);
609 if (tofield
[0] == '/')
610 toname
= ecpyalloc(tofield
);
612 toname
= ecpyalloc(directory
);
613 toname
= ecatalloc(toname
, "/");
614 toname
= ecatalloc(toname
, tofield
);
617 ** We get to be careful here since
618 ** there's a fair chance of root running us.
621 (void) remove(toname
);
622 if (link(fromname
, toname
) != 0
623 && access(fromname
, F_OK
) == 0 && !itsdir(fromname
)) {
626 if (mkdirs(toname
) != 0)
629 result
= link(fromname
, toname
);
631 const char *s
= fromfield
;
633 register char * symlinkcontents
= NULL
;
637 while ((s
= strchr(s
, '/'))
638 && ! strncmp (fromfield
, tofield
,
641 for (s
= tofield
+ (t
- fromfield
);
642 (s
= strchr(s
, '/'));
645 ecatalloc(symlinkcontents
,
647 symlinkcontents
= ecatalloc(symlinkcontents
, t
);
648 result
= symlink(symlinkcontents
, toname
);
650 warning(_("hard link failed, symbolic link used"));
651 free(symlinkcontents
);
656 fp
= fopen(fromname
, "rb");
658 const char *e
= strerror(errno
);
659 (void) fprintf(stderr
,
660 _("%s: Can't read %s: %s\n"),
661 progname
, fromname
, e
);
664 tp
= fopen(toname
, "wb");
666 const char *e
= strerror(errno
);
667 (void) fprintf(stderr
,
668 _("%s: Can't create %s: %s\n"),
669 progname
, toname
, e
);
672 while ((c
= getc(fp
)) != EOF
)
674 if (ferror(fp
) || fclose(fp
)) {
675 (void) fprintf(stderr
,
676 _("%s: Error reading %s\n"),
680 if (ferror(tp
) || fclose(tp
)) {
681 (void) fprintf(stderr
,
682 _("%s: Error writing %s\n"),
686 warning(_("link failed, copy used"));
693 #define TIME_T_BITS_IN_FILE 64
695 static const zic_t min_time
= (zic_t
) -1 << (TIME_T_BITS_IN_FILE
- 1);
696 static const zic_t max_time
= -1 - ((zic_t
) -1 << (TIME_T_BITS_IN_FILE
- 1));
699 itsdir(const char *const name
)
701 register char * myname
;
704 myname
= ecpyalloc(name
);
705 myname
= ecatalloc(myname
, "/.");
706 accres
= access(myname
, F_OK
);
712 ** Associate sets of rules with zones.
716 ** Sort by rule name.
720 rcomp(const void *cp1
, const void *cp2
)
722 return strcmp(((const struct rule
*) cp1
)->r_name
,
723 ((const struct rule
*) cp2
)->r_name
);
729 register struct zone
* zp
;
730 register struct rule
* rp
;
731 register int base
, out
;
735 (void) qsort(rules
, nrules
, sizeof *rules
, rcomp
);
736 for (i
= 0; i
< nrules
- 1; ++i
) {
737 if (strcmp(rules
[i
].r_name
,
738 rules
[i
+ 1].r_name
) != 0)
740 if (strcmp(rules
[i
].r_filename
,
741 rules
[i
+ 1].r_filename
) == 0)
743 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
744 warning(_("same rule name in multiple files"));
745 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
746 warning(_("same rule name in multiple files"));
747 for (j
= i
+ 2; j
< nrules
; ++j
) {
748 if (strcmp(rules
[i
].r_name
,
749 rules
[j
].r_name
) != 0)
751 if (strcmp(rules
[i
].r_filename
,
752 rules
[j
].r_filename
) == 0)
754 if (strcmp(rules
[i
+ 1].r_filename
,
755 rules
[j
].r_filename
) == 0)
762 for (i
= 0; i
< nzones
; ++i
) {
767 for (base
= 0; base
< nrules
; base
= out
) {
769 for (out
= base
+ 1; out
< nrules
; ++out
)
770 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
772 for (i
= 0; i
< nzones
; ++i
) {
774 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
777 zp
->z_nrules
= out
- base
;
780 for (i
= 0; i
< nzones
; ++i
) {
782 if (zp
->z_nrules
== 0) {
784 ** Maybe we have a local standard time offset.
786 eat(zp
->z_filename
, zp
->z_linenum
);
787 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
790 ** Note, though, that if there's no rule,
791 ** a '%s' in the format is a bad thing.
793 if (strchr(zp
->z_format
, '%') != 0)
794 error("%s", _("%s in ruleless zone"));
802 infile(const char *name
)
805 register char ** fields
;
807 register const struct lookup
* lp
;
808 register int nfields
;
809 register int wantcont
;
813 if (strcmp(name
, "-") == 0) {
814 name
= _("standard input");
816 } else if ((fp
= fopen(name
, "r")) == NULL
) {
817 const char *e
= strerror(errno
);
819 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
824 for (num
= 1; ; ++num
) {
826 if (fgets(buf
, sizeof buf
, fp
) != buf
)
828 cp
= strchr(buf
, '\n');
830 error(_("line too long"));
834 fields
= getfields(buf
);
836 while (fields
[nfields
] != NULL
) {
839 if (strcmp(fields
[nfields
], "-") == 0)
840 fields
[nfields
] = &nada
;
845 } else if (wantcont
) {
846 wantcont
= inzcont(fields
, nfields
);
848 lp
= byword(fields
[0], line_codes
);
850 error(_("input line of unknown type"));
851 else switch ((int) (lp
->l_value
)) {
853 inrule(fields
, nfields
);
857 wantcont
= inzone(fields
, nfields
);
860 inlink(fields
, nfields
);
865 (void) fprintf(stderr
,
866 _("%s: Leap line in non leap seconds file %s\n"),
868 else inleap(fields
, nfields
);
871 default: /* "cannot happen" */
872 (void) fprintf(stderr
,
873 _("%s: panic: Invalid l_value %d\n"),
874 progname
, lp
->l_value
);
881 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
885 if (fp
!= stdin
&& fclose(fp
)) {
886 const char *e
= strerror(errno
);
888 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
889 progname
, filename
, e
);
893 error(_("expected continuation line not found"));
897 ** Convert a string of one of the forms
898 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
899 ** into a number of seconds.
900 ** A null string maps to zero.
901 ** Call error with errstring and return zero on errors.
905 gethms(const char *string
, const char *const errstring
, const int signable
)
910 if (string
== NULL
|| *string
== '\0')
914 else if (*string
== '-') {
918 if (sscanf(string
, scheck(string
, "%"SCNdZIC
), &hh
) == 1)
920 else if (sscanf(string
, scheck(string
, "%"SCNdZIC
":%d"), &hh
, &mm
) == 2)
922 else if (sscanf(string
, scheck(string
, "%"SCNdZIC
":%d:%d"),
923 &hh
, &mm
, &ss
) != 3) {
924 error("%s", errstring
);
928 mm
< 0 || mm
>= MINSPERHOUR
||
929 ss
< 0 || ss
> SECSPERMIN
) {
930 error("%s", errstring
);
933 if (ZIC_MAX
/ SECSPERHOUR
< hh
) {
934 error(_("time overflow"));
937 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
938 warning(_("24:00 not handled by pre-1998 versions of zic"));
939 if (noise
&& (hh
> HOURSPERDAY
||
940 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
941 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
942 return oadd(sign
* hh
* SECSPERHOUR
,
943 sign
* (mm
* SECSPERMIN
+ ss
));
947 inrule(register char **const fields
, const int nfields
)
949 static struct rule r
;
951 if (nfields
!= RULE_FIELDS
) {
952 error(_("wrong number of fields on Rule line"));
955 if (*fields
[RF_NAME
] == '\0') {
956 error(_("nameless rule"));
959 r
.r_filename
= filename
;
960 r
.r_linenum
= linenum
;
961 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
962 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
963 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
964 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
965 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
966 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
967 max_abbrvar_len
= strlen(r
.r_abbrvar
);
968 rules
= erealloc(rules
, (nrules
+ 1) * sizeof *rules
);
973 inzone(register char **const fields
, const int nfields
)
977 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
978 error(_("wrong number of fields on Zone line"));
981 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
983 _("\"Zone %s\" line and -l option are mutually exclusive"),
987 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
989 _("\"Zone %s\" line and -p option are mutually exclusive"),
993 for (i
= 0; i
< nzones
; ++i
)
994 if (zones
[i
].z_name
!= NULL
&&
995 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
997 _("duplicate zone name %s (file \"%s\", line %d)"),
1000 zones
[i
].z_linenum
);
1003 return inzsub(fields
, nfields
, FALSE
);
1007 inzcont(register char **const fields
, const int nfields
)
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(register char **const fields
, const int nfields
, const int iscont
)
1020 static struct zone z
;
1021 register int i_gmtoff
, i_rule
, i_format
;
1022 register int i_untilyear
, i_untilmonth
;
1023 register int i_untilday
, i_untiltime
;
1024 register int hasuntil
;
1027 i_gmtoff
= ZFC_GMTOFF
;
1029 i_format
= ZFC_FORMAT
;
1030 i_untilyear
= ZFC_TILYEAR
;
1031 i_untilmonth
= ZFC_TILMONTH
;
1032 i_untilday
= ZFC_TILDAY
;
1033 i_untiltime
= ZFC_TILTIME
;
1036 i_gmtoff
= ZF_GMTOFF
;
1038 i_format
= ZF_FORMAT
;
1039 i_untilyear
= ZF_TILYEAR
;
1040 i_untilmonth
= ZF_TILMONTH
;
1041 i_untilday
= ZF_TILDAY
;
1042 i_untiltime
= ZF_TILTIME
;
1043 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1045 z
.z_filename
= filename
;
1046 z
.z_linenum
= linenum
;
1047 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UT offset"), TRUE
);
1048 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1049 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1050 error(_("invalid abbreviation format"));
1054 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1055 z
.z_format
= ecpyalloc(fields
[i_format
]);
1056 if (max_format_len
< strlen(z
.z_format
))
1057 max_format_len
= strlen(z
.z_format
);
1058 hasuntil
= nfields
> i_untilyear
;
1060 z
.z_untilrule
.r_filename
= filename
;
1061 z
.z_untilrule
.r_linenum
= linenum
;
1062 rulesub(&z
.z_untilrule
,
1063 fields
[i_untilyear
],
1066 (nfields
> i_untilmonth
) ?
1067 fields
[i_untilmonth
] : "Jan",
1068 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1069 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1070 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1071 z
.z_untilrule
.r_loyear
);
1072 if (iscont
&& nzones
> 0 &&
1073 z
.z_untiltime
> min_time
&&
1074 z
.z_untiltime
< max_time
&&
1075 zones
[nzones
- 1].z_untiltime
> min_time
&&
1076 zones
[nzones
- 1].z_untiltime
< max_time
&&
1077 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1079 "Zone continuation line end time is not after end time of previous line"
1084 zones
= erealloc(zones
, (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(register char ** const fields
, const int nfields
)
1096 register const char * cp
;
1097 register const struct lookup
* lp
;
1104 if (nfields
!= LEAP_FIELDS
) {
1105 error(_("wrong number of fields on Leap line"));
1109 cp
= fields
[LP_YEAR
];
1110 if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &year
) != 1) {
1114 error(_("invalid leaping year"));
1117 if (!leapseen
|| leapmaxyear
< year
)
1119 if (!leapseen
|| leapminyear
> year
)
1125 i
= len_years
[isleap(j
)];
1129 i
= -len_years
[isleap(j
)];
1131 dayoff
= oadd(dayoff
, i
);
1133 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1134 error(_("invalid month name"));
1137 month
= lp
->l_value
;
1139 while (j
!= month
) {
1140 i
= len_months
[isleap(year
)][j
];
1141 dayoff
= oadd(dayoff
, i
);
1144 cp
= fields
[LP_DAY
];
1145 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1146 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1147 error(_("invalid day of month"));
1150 dayoff
= oadd(dayoff
, day
- 1);
1151 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1152 error(_("time before zero"));
1155 if (dayoff
< min_time
/ SECSPERDAY
) {
1156 error(_("time too small"));
1159 if (dayoff
> max_time
/ SECSPERDAY
) {
1160 error(_("time too large"));
1163 t
= (zic_t
) dayoff
* SECSPERDAY
;
1164 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1165 cp
= fields
[LP_CORR
];
1167 register int positive
;
1170 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1173 } else if (strcmp(cp
, "--") == 0) {
1176 } else if (strcmp(cp
, "+") == 0) {
1179 } else if (strcmp(cp
, "++") == 0) {
1183 error(_("illegal CORRECTION field on Leap line"));
1186 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1188 "illegal Rolling/Stationary field on Leap line"
1192 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1197 inlink(register char **const fields
, const int nfields
)
1201 if (nfields
!= LINK_FIELDS
) {
1202 error(_("wrong number of fields on Link line"));
1205 if (*fields
[LF_FROM
] == '\0') {
1206 error(_("blank FROM field on Link line"));
1209 if (*fields
[LF_TO
] == '\0') {
1210 error(_("blank TO field on Link line"));
1213 l
.l_filename
= filename
;
1214 l
.l_linenum
= linenum
;
1215 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1216 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1217 links
= erealloc(links
, (nlinks
+ 1) * sizeof *links
);
1218 links
[nlinks
++] = l
;
1222 rulesub(register struct rule
*const rp
,
1223 const char *const loyearp
,
1224 const char *const hiyearp
,
1225 const char *const typep
,
1226 const char *const monthp
,
1227 const char *const dayp
,
1228 const char *const timep
)
1230 register const struct lookup
* lp
;
1231 register const char * cp
;
1235 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1236 error(_("invalid month name"));
1239 rp
->r_month
= lp
->l_value
;
1240 rp
->r_todisstd
= FALSE
;
1241 rp
->r_todisgmt
= FALSE
;
1242 dp
= ecpyalloc(timep
);
1244 ep
= dp
+ strlen(dp
) - 1;
1245 switch (lowerit(*ep
)) {
1246 case 's': /* Standard */
1247 rp
->r_todisstd
= TRUE
;
1248 rp
->r_todisgmt
= FALSE
;
1251 case 'w': /* Wall */
1252 rp
->r_todisstd
= FALSE
;
1253 rp
->r_todisgmt
= FALSE
;
1256 case 'g': /* Greenwich */
1257 case 'u': /* Universal */
1258 case 'z': /* Zulu */
1259 rp
->r_todisstd
= TRUE
;
1260 rp
->r_todisgmt
= TRUE
;
1265 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1271 lp
= byword(cp
, begin_years
);
1272 rp
->r_lowasnum
= lp
== NULL
;
1273 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1275 rp
->r_loyear
= ZIC_MIN
;
1278 rp
->r_loyear
= ZIC_MAX
;
1280 default: /* "cannot happen" */
1281 (void) fprintf(stderr
,
1282 _("%s: panic: Invalid l_value %d\n"),
1283 progname
, lp
->l_value
);
1285 } else if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &rp
->r_loyear
) != 1) {
1286 error(_("invalid starting year"));
1290 lp
= byword(cp
, end_years
);
1291 rp
->r_hiwasnum
= lp
== NULL
;
1292 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1294 rp
->r_hiyear
= ZIC_MIN
;
1297 rp
->r_hiyear
= ZIC_MAX
;
1300 rp
->r_hiyear
= rp
->r_loyear
;
1302 default: /* "cannot happen" */
1303 (void) fprintf(stderr
,
1304 _("%s: panic: Invalid l_value %d\n"),
1305 progname
, lp
->l_value
);
1307 } else if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &rp
->r_hiyear
) != 1) {
1308 error(_("invalid ending year"));
1311 if (rp
->r_loyear
> rp
->r_hiyear
) {
1312 error(_("starting year greater than ending year"));
1316 rp
->r_yrtype
= NULL
;
1318 if (rp
->r_loyear
== rp
->r_hiyear
) {
1319 error(_("typed single year"));
1322 rp
->r_yrtype
= ecpyalloc(typep
);
1326 ** Accept things such as:
1332 dp
= ecpyalloc(dayp
);
1333 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1334 rp
->r_dycode
= DC_DOWLEQ
;
1335 rp
->r_wday
= lp
->l_value
;
1336 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1338 if ((ep
= strchr(dp
, '<')) != 0)
1339 rp
->r_dycode
= DC_DOWLEQ
;
1340 else if ((ep
= strchr(dp
, '>')) != 0)
1341 rp
->r_dycode
= DC_DOWGEQ
;
1344 rp
->r_dycode
= DC_DOM
;
1346 if (rp
->r_dycode
!= DC_DOM
) {
1349 error(_("invalid day of month"));
1353 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1354 error(_("invalid weekday name"));
1358 rp
->r_wday
= lp
->l_value
;
1360 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1361 rp
->r_dayofmonth
<= 0 ||
1362 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1363 error(_("invalid day of month"));
1372 convert(const int_fast32_t val
, char *const buf
)
1376 unsigned char *const b
= (unsigned char *) buf
;
1378 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1379 b
[i
] = val
>> shift
;
1383 convert64(const zic_t val
, char *const buf
)
1387 unsigned char *const b
= (unsigned char *) buf
;
1389 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1390 b
[i
] = val
>> shift
;
1394 puttzcode(const int_fast32_t val
, FILE *const fp
)
1399 (void) fwrite(buf
, sizeof buf
, 1, fp
);
1403 puttzcode64(const zic_t val
, FILE *const fp
)
1407 convert64(val
, buf
);
1408 (void) fwrite(buf
, sizeof buf
, 1, fp
);
1412 atcomp(const void *avp
, const void *bvp
)
1414 const zic_t a
= ((const struct attype
*) avp
)->at
;
1415 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1417 return (a
< b
) ? -1 : (a
> b
);
1423 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1427 writezone(const char *const name
, const char *const string
, char version
)
1431 register int leapcnt32
, leapi32
;
1432 register int timecnt32
, timei32
;
1434 static char * fullname
;
1435 static const struct tzhead tzh0
;
1436 static struct tzhead tzh
;
1437 zic_t ats
[TZ_MAX_TIMES
];
1438 unsigned char types
[TZ_MAX_TIMES
];
1444 (void) qsort(attypes
, timecnt
, sizeof *attypes
, atcomp
);
1454 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1457 ** Remember that type 0 is reserved.
1460 while (fromi
< timecnt
&& attypes
[fromi
].type
== 1)
1461 ++fromi
; /* handled by default rule */
1462 for ( ; fromi
< timecnt
; ++fromi
) {
1463 if (toi
!= 0 && ((attypes
[fromi
].at
+
1464 gmtoffs
[attypes
[toi
- 1].type
]) <=
1465 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1466 : attypes
[toi
- 2].type
]))) {
1467 attypes
[toi
- 1].type
=
1468 attypes
[fromi
].type
;
1472 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1473 attypes
[toi
++] = attypes
[fromi
];
1480 for (i
= 0; i
< timecnt
; ++i
) {
1481 ats
[i
] = attypes
[i
].at
;
1482 types
[i
] = attypes
[i
].type
;
1485 ** Correct for leap seconds.
1487 for (i
= 0; i
< timecnt
; ++i
) {
1490 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1491 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1496 ** Figure out 32-bit-limited starts and counts.
1498 timecnt32
= timecnt
;
1500 leapcnt32
= leapcnt
;
1502 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1504 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1508 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1510 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1514 fullname
= erealloc(fullname
,
1515 strlen(directory
) + 1 + strlen(name
) + 1);
1516 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1518 ** Remove old file, if any, to snap links.
1520 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1521 const char *e
= strerror(errno
);
1523 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1524 progname
, fullname
, e
);
1527 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1528 if (mkdirs(fullname
) != 0)
1530 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1531 const char *e
= strerror(errno
);
1533 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1534 progname
, fullname
, e
);
1538 for (pass
= 1; pass
<= 2; ++pass
) {
1539 register int thistimei
, thistimecnt
;
1540 register int thisleapi
, thisleapcnt
;
1541 register int thistimelim
, thisleaplim
;
1542 int writetype
[TZ_MAX_TIMES
];
1543 int typemap
[TZ_MAX_TYPES
];
1544 register int thistypecnt
;
1545 char thischars
[TZ_MAX_CHARS
];
1547 int indmap
[TZ_MAX_CHARS
];
1550 thistimei
= timei32
;
1551 thistimecnt
= timecnt32
;
1552 thisleapi
= leapi32
;
1553 thisleapcnt
= leapcnt32
;
1556 thistimecnt
= timecnt
;
1558 thisleapcnt
= leapcnt
;
1560 thistimelim
= thistimei
+ thistimecnt
;
1561 thisleaplim
= thisleapi
+ thisleapcnt
;
1563 ** Remember that type 0 is reserved.
1565 writetype
[0] = FALSE
;
1566 for (i
= 1; i
< typecnt
; ++i
)
1567 writetype
[i
] = thistimecnt
== timecnt
;
1568 if (thistimecnt
== 0) {
1570 ** No transition times fall in the current
1571 ** (32- or 64-bit) window.
1574 writetype
[typecnt
- 1] = TRUE
;
1576 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1578 writetype
[types
[i
]] = TRUE
;
1580 ** For America/Godthab and Antarctica/Palmer
1583 ** Remember that type 0 is reserved.
1586 writetype
[1] = TRUE
;
1588 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1590 ** For some pre-2011 systems: if the last-to-be-written
1591 ** standard (or daylight) type has an offset different from the
1592 ** most recently used offset,
1593 ** append an (unused) copy of the most recently used type
1594 ** (to help get global "altzone" and "timezone" variables
1598 register int mrudst
, mrustd
, hidst
, histd
, type
;
1600 hidst
= histd
= mrudst
= mrustd
= -1;
1601 for (i
= thistimei
; i
< thistimelim
; ++i
)
1602 if (isdsts
[types
[i
]])
1604 else mrustd
= types
[i
];
1605 for (i
= 0; i
< typecnt
; ++i
)
1611 if (hidst
>= 0 && mrudst
>= 0 && hidst
!= mrudst
&&
1612 gmtoffs
[hidst
] != gmtoffs
[mrudst
]) {
1613 isdsts
[mrudst
] = -1;
1614 type
= addtype(gmtoffs
[mrudst
],
1615 &chars
[abbrinds
[mrudst
]],
1619 isdsts
[mrudst
] = TRUE
;
1620 writetype
[type
] = TRUE
;
1622 if (histd
>= 0 && mrustd
>= 0 && histd
!= mrustd
&&
1623 gmtoffs
[histd
] != gmtoffs
[mrustd
]) {
1624 isdsts
[mrustd
] = -1;
1625 type
= addtype(gmtoffs
[mrustd
],
1626 &chars
[abbrinds
[mrustd
]],
1630 isdsts
[mrustd
] = FALSE
;
1631 writetype
[type
] = TRUE
;
1634 #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1637 ** Potentially, set type 0 to that of lowest-valued time.
1639 if (thistimei
> 0) {
1640 for (i
= 1; i
< typecnt
; ++i
)
1641 if (writetype
[i
] && !isdsts
[i
])
1643 if (i
!= types
[thistimei
- 1]) {
1644 i
= types
[thistimei
- 1];
1645 gmtoffs
[0] = gmtoffs
[i
];
1646 isdsts
[0] = isdsts
[i
];
1647 ttisstds
[0] = ttisstds
[i
];
1648 ttisgmts
[0] = ttisgmts
[i
];
1649 abbrinds
[0] = abbrinds
[i
];
1650 writetype
[0] = TRUE
;
1651 writetype
[i
] = FALSE
;
1654 for (i
= 0; i
< typecnt
; ++i
)
1655 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : 0;
1656 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1659 for (i
= 0; i
< typecnt
; ++i
) {
1660 register char * thisabbr
;
1664 if (indmap
[abbrinds
[i
]] >= 0)
1666 thisabbr
= &chars
[abbrinds
[i
]];
1667 for (j
= 0; j
< thischarcnt
; ++j
)
1668 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1670 if (j
== thischarcnt
) {
1671 (void) strcpy(&thischars
[(int) thischarcnt
],
1673 thischarcnt
+= strlen(thisabbr
) + 1;
1675 indmap
[abbrinds
[i
]] = j
;
1677 #define DO(field) ((void) fwrite(tzh.field, sizeof tzh.field, 1, fp))
1679 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1680 tzh
.tzh_version
[0] = version
;
1681 convert(thistypecnt
, tzh
.tzh_ttisgmtcnt
);
1682 convert(thistypecnt
, tzh
.tzh_ttisstdcnt
);
1683 convert(thisleapcnt
, tzh
.tzh_leapcnt
);
1684 convert(thistimecnt
, tzh
.tzh_timecnt
);
1685 convert(thistypecnt
, tzh
.tzh_typecnt
);
1686 convert(thischarcnt
, tzh
.tzh_charcnt
);
1697 for (i
= thistimei
; i
< thistimelim
; ++i
)
1699 puttzcode(ats
[i
], fp
);
1700 else puttzcode64(ats
[i
], fp
);
1701 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1704 uc
= typemap
[types
[i
]];
1705 (void) fwrite(&uc
, sizeof uc
, 1, fp
);
1707 for (i
= 0; i
< typecnt
; ++i
)
1709 puttzcode(gmtoffs
[i
], fp
);
1710 (void) putc(isdsts
[i
], fp
);
1711 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1713 if (thischarcnt
!= 0)
1714 (void) fwrite(thischars
, sizeof thischars
[0],
1716 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1717 register zic_t todo
;
1720 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1723 if (++j
>= typecnt
) {
1729 while (j
< timecnt
&&
1734 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1735 } else todo
= trans
[i
];
1737 puttzcode(todo
, fp
);
1738 else puttzcode64(todo
, fp
);
1739 puttzcode(corr
[i
], fp
);
1741 for (i
= 0; i
< typecnt
; ++i
)
1743 (void) putc(ttisstds
[i
], fp
);
1744 for (i
= 0; i
< typecnt
; ++i
)
1746 (void) putc(ttisgmts
[i
], fp
);
1748 (void) fprintf(fp
, "\n%s\n", string
);
1749 if (ferror(fp
) || fclose(fp
)) {
1750 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1751 progname
, fullname
);
1757 doabbr(char *const abbr
, const char *const format
, const char *const letters
,
1758 const int isdst
, const int doquotes
)
1761 register char * slashp
;
1764 slashp
= strchr(format
, '/');
1765 if (slashp
== NULL
) {
1766 if (letters
== NULL
)
1767 (void) strcpy(abbr
, format
);
1768 else (void) sprintf(abbr
, format
, letters
);
1770 (void) strcpy(abbr
, slashp
+ 1);
1772 if (slashp
> format
)
1773 (void) strncpy(abbr
, format
, slashp
- format
);
1774 abbr
[slashp
- format
] = '\0';
1778 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1779 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1780 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1783 if (len
> 0 && *cp
== '\0')
1785 abbr
[len
+ 2] = '\0';
1786 abbr
[len
+ 1] = '>';
1787 for ( ; len
> 0; --len
)
1788 abbr
[len
] = abbr
[len
- 1];
1793 updateminmax(const zic_t x
)
1802 stringoffset(char *result
, zic_t offset
)
1805 register int minutes
;
1806 register int seconds
;
1810 (void) strcpy(result
, "-");
1813 seconds
= offset
% SECSPERMIN
;
1814 offset
/= SECSPERMIN
;
1815 minutes
= offset
% MINSPERHOUR
;
1816 offset
/= MINSPERHOUR
;
1818 if (hours
>= HOURSPERDAY
* DAYSPERWEEK
) {
1822 (void) sprintf(end(result
), "%d", hours
);
1823 if (minutes
!= 0 || seconds
!= 0) {
1824 (void) sprintf(end(result
), ":%02d", minutes
);
1826 (void) sprintf(end(result
), ":%02d", seconds
);
1832 stringrule(char *result
, const struct rule
*const rp
, const zic_t dstoff
,
1835 register zic_t tod
= rp
->r_tod
;
1836 register int compat
= 0;
1838 result
= end(result
);
1839 if (rp
->r_dycode
== DC_DOM
) {
1840 register int month
, total
;
1842 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1845 for (month
= 0; month
< rp
->r_month
; ++month
)
1846 total
+= len_months
[0][month
];
1847 /* Omit the "J" in Jan and Feb, as that's shorter. */
1848 if (rp
->r_month
<= 1)
1849 (void) sprintf(result
, "%d", total
+ rp
->r_dayofmonth
- 1);
1851 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1854 register int wday
= rp
->r_wday
;
1855 register int wdayoff
;
1857 if (rp
->r_dycode
== DC_DOWGEQ
) {
1858 wdayoff
= (rp
->r_dayofmonth
- 1) % DAYSPERWEEK
;
1862 tod
+= wdayoff
* SECSPERDAY
;
1863 week
= 1 + (rp
->r_dayofmonth
- 1) / DAYSPERWEEK
;
1864 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1865 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1868 wdayoff
= rp
->r_dayofmonth
% DAYSPERWEEK
;
1872 tod
+= wdayoff
* SECSPERDAY
;
1873 week
= rp
->r_dayofmonth
/ DAYSPERWEEK
;
1875 } else return -1; /* "cannot happen" */
1877 wday
+= DAYSPERWEEK
;
1878 (void) sprintf(result
, "M%d.%d.%d",
1879 rp
->r_month
+ 1, week
, wday
);
1883 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1885 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1886 (void) strcat(result
, "/");
1887 if (stringoffset(end(result
), tod
) != 0)
1892 } else if (SECSPERDAY
<= tod
) {
1901 rule_cmp(struct rule
const *a
, struct rule
const *b
)
1907 if (a
->r_hiyear
!= b
->r_hiyear
)
1908 return a
->r_hiyear
< b
->r_hiyear
? -1 : 1;
1909 if (a
->r_month
- b
->r_month
!= 0)
1910 return a
->r_month
- b
->r_month
;
1911 return a
->r_dayofmonth
- b
->r_dayofmonth
;
1914 enum { YEAR_BY_YEAR_ZONE
= 1 };
1917 stringzone(char *result
, const struct zone
*const zpfirst
, const int zonecount
)
1919 register const struct zone
* zp
;
1920 register struct rule
* rp
;
1921 register struct rule
* stdrp
;
1922 register struct rule
* dstrp
;
1924 register const char * abbrvar
;
1925 register int compat
= 0;
1927 struct rule stdr
, dstr
;
1930 zp
= zpfirst
+ zonecount
- 1;
1931 stdrp
= dstrp
= NULL
;
1932 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1933 rp
= &zp
->z_rules
[i
];
1934 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= ZIC_MAX
)
1936 if (rp
->r_yrtype
!= NULL
)
1938 if (rp
->r_stdoff
== 0) {
1948 if (stdrp
== NULL
&& dstrp
== NULL
) {
1950 ** There are no rules running through "max".
1951 ** Find the latest std rule in stdabbrrp
1952 ** and latest rule of any type in stdrp.
1954 register struct rule
*stdabbrrp
= NULL
;
1955 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1956 rp
= &zp
->z_rules
[i
];
1957 if (rp
->r_stdoff
== 0 && rule_cmp(stdabbrrp
, rp
) < 0)
1959 if (rule_cmp(stdrp
, rp
) < 0)
1963 ** Horrid special case: if year is 2037,
1964 ** presume this is a zone handled on a year-by-year basis;
1965 ** do not try to apply a rule to the zone.
1967 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1968 return YEAR_BY_YEAR_ZONE
;
1970 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0) {
1971 /* Perpetual DST. */
1972 dstr
.r_month
= TM_JANUARY
;
1973 dstr
.r_dycode
= DC_DOM
;
1974 dstr
.r_dayofmonth
= 1;
1976 dstr
.r_todisstd
= dstr
.r_todisgmt
= FALSE
;
1977 dstr
.r_stdoff
= stdrp
->r_stdoff
;
1978 dstr
.r_abbrvar
= stdrp
->r_abbrvar
;
1979 stdr
.r_month
= TM_DECEMBER
;
1980 stdr
.r_dycode
= DC_DOM
;
1981 stdr
.r_dayofmonth
= 31;
1982 stdr
.r_tod
= SECSPERDAY
+ stdrp
->r_stdoff
;
1983 stdr
.r_todisstd
= stdr
.r_todisgmt
= FALSE
;
1986 = (stdabbrrp
? stdabbrrp
->r_abbrvar
: "");
1991 if (stdrp
== NULL
&& (zp
->z_nrules
!= 0 || zp
->z_stdoff
!= 0))
1993 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1994 doabbr(result
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1995 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
2001 doabbr(end(result
), zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
2002 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
2003 if (stringoffset(end(result
),
2004 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
2008 (void) strcat(result
, ",");
2009 c
= stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
);
2016 (void) strcat(result
, ",");
2017 c
= stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
);
2028 outzone(const struct zone
* const zpfirst
, const int zonecount
)
2030 register const struct zone
* zp
;
2031 register struct rule
* rp
;
2033 register int usestart
, useuntil
;
2034 register zic_t starttime
, untiltime
;
2035 register zic_t gmtoff
;
2036 register zic_t stdoff
;
2037 register zic_t year
;
2038 register zic_t startoff
;
2039 register int startttisstd
;
2040 register int startttisgmt
;
2042 register char * startbuf
;
2044 register char * envvar
;
2045 register int max_abbr_len
;
2046 register int max_envvar_len
;
2047 register int prodstic
; /* all rules are min to max */
2048 register int compat
;
2049 register int do_extend
;
2050 register char version
;
2052 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
2053 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
2054 startbuf
= emalloc(max_abbr_len
+ 1);
2055 ab
= emalloc(max_abbr_len
+ 1);
2056 envvar
= emalloc(max_envvar_len
+ 1);
2057 INITIALIZE(untiltime
);
2058 INITIALIZE(starttime
);
2060 ** Now. . .finally. . .generate some useful data!
2065 prodstic
= zonecount
== 1;
2067 ** Thanks to Earl Chew
2068 ** for noting the need to unconditionally initialize startttisstd.
2070 startttisstd
= FALSE
;
2071 startttisgmt
= FALSE
;
2072 min_year
= max_year
= EPOCH_YEAR
;
2074 updateminmax(leapminyear
);
2075 updateminmax(leapmaxyear
+ (leapmaxyear
< ZIC_MAX
));
2080 gmtoffs
[0] = isdsts
[0] = ttisstds
[0] = ttisgmts
[0] = abbrinds
[0] = -1;
2082 for (i
= 0; i
< zonecount
; ++i
) {
2084 if (i
< zonecount
- 1)
2085 updateminmax(zp
->z_untilrule
.r_loyear
);
2086 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2087 rp
= &zp
->z_rules
[j
];
2089 updateminmax(rp
->r_loyear
);
2091 updateminmax(rp
->r_hiyear
);
2092 if (rp
->r_lowasnum
|| rp
->r_hiwasnum
)
2097 ** Generate lots of data if a rule can't cover all future times.
2099 compat
= stringzone(envvar
, zpfirst
, zonecount
);
2100 version
= compat
< 2013 ? ZIC_VERSION_PRE_2013
: ZIC_VERSION
;
2101 do_extend
= compat
< 0 || compat
== YEAR_BY_YEAR_ZONE
;
2105 _("no POSIX environment variable for zone"),
2107 else if (compat
!= 0 && compat
!= YEAR_BY_YEAR_ZONE
) {
2108 /* Circa-COMPAT clients, and earlier clients, might
2109 not work for this zone when given dates before
2110 1970 or after 2038. */
2111 warning(_("%s: pre-%d clients may mishandle"
2112 " distant timestamps"),
2113 zpfirst
->z_name
, compat
);
2118 ** Search through a couple of extra years past the obvious
2119 ** 400, to avoid edge cases. For example, suppose a non-POSIX
2120 ** rule applies from 2012 onwards and has transitions in March
2121 ** and September, plus some one-off transitions in November
2122 ** 2013. If zic looked only at the last 400 years, it would
2123 ** set max_year=2413, with the intent that the 400 years 2014
2124 ** through 2413 will be repeated. The last transition listed
2125 ** in the tzfile would be in 2413-09, less than 400 years
2126 ** after the last one-off transition in 2013-11. Two years
2127 ** might be overkill, but with the kind of edge cases
2128 ** available we're not sure that one year would suffice.
2130 enum { years_of_observations
= YEARSPERREPEAT
+ 2 };
2132 if (min_year
>= ZIC_MIN
+ years_of_observations
)
2133 min_year
-= years_of_observations
;
2134 else min_year
= ZIC_MIN
;
2135 if (max_year
<= ZIC_MAX
- years_of_observations
)
2136 max_year
+= years_of_observations
;
2137 else max_year
= ZIC_MAX
;
2139 ** Regardless of any of the above,
2140 ** for a "proDSTic" zone which specifies that its rules
2141 ** always have and always will be in effect,
2142 ** we only need one cycle to define the zone.
2146 max_year
= min_year
+ years_of_observations
;
2150 ** For the benefit of older systems,
2151 ** generate data from 1900 through 2037.
2153 if (min_year
> 1900)
2155 if (max_year
< 2037)
2157 for (i
= 0; i
< zonecount
; ++i
) {
2159 ** A guess that may well be corrected later.
2163 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2164 useuntil
= i
< (zonecount
- 1);
2165 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2167 gmtoff
= zp
->z_gmtoff
;
2168 eat(zp
->z_filename
, zp
->z_linenum
);
2170 startoff
= zp
->z_gmtoff
;
2171 if (zp
->z_nrules
== 0) {
2172 stdoff
= zp
->z_stdoff
;
2173 doabbr(startbuf
, zp
->z_format
,
2174 NULL
, stdoff
!= 0, FALSE
);
2175 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2176 startbuf
, stdoff
!= 0, startttisstd
,
2179 addtt(starttime
, type
);
2181 } else if (stdoff
!= 0)
2182 addtt(min_time
, type
);
2183 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2184 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2187 ** Mark which rules to do in the current year.
2188 ** For those to do, calculate rpytime(rp, year);
2190 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2191 rp
= &zp
->z_rules
[j
];
2192 eats(zp
->z_filename
, zp
->z_linenum
,
2193 rp
->r_filename
, rp
->r_linenum
);
2194 rp
->r_todo
= year
>= rp
->r_loyear
&&
2195 year
<= rp
->r_hiyear
&&
2196 yearistype(year
, rp
->r_yrtype
);
2198 rp
->r_temp
= rpytime(rp
, year
);
2202 register zic_t jtime
, ktime
;
2203 register zic_t offset
;
2208 ** Turn untiltime into UT
2209 ** assuming the current gmtoff and
2212 untiltime
= zp
->z_untiltime
;
2213 if (!zp
->z_untilrule
.r_todisgmt
)
2214 untiltime
= tadd(untiltime
,
2216 if (!zp
->z_untilrule
.r_todisstd
)
2217 untiltime
= tadd(untiltime
,
2221 ** Find the rule (of those to do, if any)
2222 ** that takes effect earliest in the year.
2225 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2226 rp
= &zp
->z_rules
[j
];
2229 eats(zp
->z_filename
, zp
->z_linenum
,
2230 rp
->r_filename
, rp
->r_linenum
);
2231 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2232 if (!rp
->r_todisstd
)
2233 offset
= oadd(offset
, stdoff
);
2235 if (jtime
== min_time
||
2238 jtime
= tadd(jtime
, -offset
);
2239 if (k
< 0 || jtime
< ktime
) {
2245 break; /* go on to next year */
2246 rp
= &zp
->z_rules
[k
];
2248 if (useuntil
&& ktime
>= untiltime
)
2250 stdoff
= rp
->r_stdoff
;
2251 if (usestart
&& ktime
== starttime
)
2254 if (ktime
< starttime
) {
2255 startoff
= oadd(zp
->z_gmtoff
,
2257 doabbr(startbuf
, zp
->z_format
,
2263 if (*startbuf
== '\0' &&
2264 startoff
== oadd(zp
->z_gmtoff
,
2274 eats(zp
->z_filename
, zp
->z_linenum
,
2275 rp
->r_filename
, rp
->r_linenum
);
2276 doabbr(ab
, zp
->z_format
, rp
->r_abbrvar
,
2277 rp
->r_stdoff
!= 0, FALSE
);
2278 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2279 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2280 rp
->r_todisstd
, rp
->r_todisgmt
);
2285 if (*startbuf
== '\0' &&
2286 zp
->z_format
!= NULL
&&
2287 strchr(zp
->z_format
, '%') == NULL
&&
2288 strchr(zp
->z_format
, '/') == NULL
)
2289 (void) strcpy(startbuf
, zp
->z_format
);
2290 eat(zp
->z_filename
, zp
->z_linenum
);
2291 if (*startbuf
== '\0')
2292 error(_("can't determine time zone abbreviation to use just after until time"));
2293 else addtt(starttime
,
2294 addtype(startoff
, startbuf
,
2295 startoff
!= zp
->z_gmtoff
,
2300 ** Now we may get to set starttime for the next zone line.
2303 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2304 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2305 starttime
= zp
->z_untiltime
;
2307 starttime
= tadd(starttime
, -stdoff
);
2309 starttime
= tadd(starttime
, -gmtoff
);
2314 ** If we're extending the explicitly listed observations
2315 ** for 400 years because we can't fill the POSIX-TZ field,
2316 ** check whether we actually ended up explicitly listing
2317 ** observations through that period. If there aren't any
2318 ** near the end of the 400-year period, add a redundant
2319 ** one at the end of the final year, to make it clear
2320 ** that we are claiming to have definite knowledge of
2321 ** the lack of transitions up to that point.
2324 struct attype
*lastat
;
2325 xr
.r_month
= TM_JANUARY
;
2326 xr
.r_dycode
= DC_DOM
;
2327 xr
.r_dayofmonth
= 1;
2329 for (lastat
= &attypes
[0], i
= 1; i
< timecnt
; i
++)
2330 if (attypes
[i
].at
> lastat
->at
)
2331 lastat
= &attypes
[i
];
2332 if (lastat
->at
< rpytime(&xr
, max_year
- 1)) {
2334 ** Create new type code for the redundant entry,
2335 ** to prevent it being optimised away.
2337 if (typecnt
>= TZ_MAX_TYPES
) {
2338 error(_("too many local time types"));
2341 gmtoffs
[typecnt
] = gmtoffs
[lastat
->type
];
2342 isdsts
[typecnt
] = isdsts
[lastat
->type
];
2343 ttisstds
[typecnt
] = ttisstds
[lastat
->type
];
2344 ttisgmts
[typecnt
] = ttisgmts
[lastat
->type
];
2345 abbrinds
[typecnt
] = abbrinds
[lastat
->type
];
2347 addtt(rpytime(&xr
, max_year
+ 1), typecnt
-1);
2350 writezone(zpfirst
->z_name
, envvar
, version
);
2357 addtt(const zic_t starttime
, int type
)
2359 if (starttime
<= min_time
||
2360 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2361 gmtoffs
[0] = gmtoffs
[type
];
2362 isdsts
[0] = isdsts
[type
];
2363 ttisstds
[0] = ttisstds
[type
];
2364 ttisgmts
[0] = ttisgmts
[type
];
2365 if (abbrinds
[type
] != 0)
2366 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2368 charcnt
= strlen(chars
) + 1;
2373 if (timecnt
>= TZ_MAX_TIMES
) {
2374 error(_("too many transitions?!"));
2377 attypes
[timecnt
].at
= starttime
;
2378 attypes
[timecnt
].type
= type
;
2383 addtype(const zic_t gmtoff
, const char *const abbr
, const int isdst
,
2384 const int ttisstd
, const int ttisgmt
)
2388 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2389 error(_("internal error - addtype called with bad isdst"));
2392 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2393 error(_("internal error - addtype called with bad ttisstd"));
2396 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2397 error(_("internal error - addtype called with bad ttisgmt"));
2401 ** See if there's already an entry for this zone type.
2402 ** If so, just return its index.
2404 for (i
= 0; i
< typecnt
; ++i
) {
2405 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2406 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2407 ttisstd
== ttisstds
[i
] &&
2408 ttisgmt
== ttisgmts
[i
])
2412 ** There isn't one; add a new one, unless there are already too
2415 if (typecnt
>= TZ_MAX_TYPES
) {
2416 error(_("too many local time types"));
2419 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2420 error(_("UT offset out of range"));
2423 gmtoffs
[i
] = gmtoff
;
2425 ttisstds
[i
] = ttisstd
;
2426 ttisgmts
[i
] = ttisgmt
;
2428 for (j
= 0; j
< charcnt
; ++j
)
2429 if (strcmp(&chars
[j
], abbr
) == 0)
2439 leapadd(const zic_t t
, const int positive
, const int rolling
, int count
)
2443 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2444 error(_("too many leap seconds"));
2447 for (i
= 0; i
< leapcnt
; ++i
)
2448 if (t
<= trans
[i
]) {
2449 if (t
== trans
[i
]) {
2450 error(_("repeated leap second moment"));
2456 for (j
= leapcnt
; j
> i
; --j
) {
2457 trans
[j
] = trans
[j
- 1];
2458 corr
[j
] = corr
[j
- 1];
2459 roll
[j
] = roll
[j
- 1];
2462 corr
[i
] = positive
? 1 : -count
;
2465 } while (positive
&& --count
!= 0);
2472 register zic_t last
= 0;
2475 ** propagate leap seconds forward
2477 for (i
= 0; i
< leapcnt
; ++i
) {
2478 trans
[i
] = tadd(trans
[i
], last
);
2479 last
= corr
[i
] += last
;
2484 yearistype(const int year
, const char *const type
)
2489 if (type
== NULL
|| *type
== '\0')
2491 buf
= erealloc(buf
, 132 + strlen(yitcommand
) + strlen(type
));
2492 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
2493 result
= system(buf
);
2494 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2500 error(_("Wild result from command execution"));
2501 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2502 progname
, buf
, result
);
2510 a
= (unsigned char) a
;
2511 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2514 /* case-insensitive equality */
2515 static ATTRIBUTE_PURE
int
2516 ciequal(register const char *ap
, register const char *bp
)
2518 while (lowerit(*ap
) == lowerit(*bp
++))
2524 static ATTRIBUTE_PURE
int
2525 itsabbr(register const char *abbr
, register const char *word
)
2527 if (lowerit(*abbr
) != lowerit(*word
))
2530 while (*++abbr
!= '\0')
2534 } while (lowerit(*word
++) != lowerit(*abbr
));
2538 static ATTRIBUTE_PURE
const struct lookup
*
2539 byword(register const char *const word
,
2540 register const struct lookup
*const table
)
2542 register const struct lookup
* foundlp
;
2543 register const struct lookup
* lp
;
2545 if (word
== NULL
|| table
== NULL
)
2548 ** Look for exact match.
2550 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2551 if (ciequal(word
, lp
->l_word
))
2554 ** Look for inexact match.
2557 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2558 if (itsabbr(word
, lp
->l_word
)) {
2559 if (foundlp
== NULL
)
2561 else return NULL
; /* multiple inexact matches */
2567 getfields(register char *cp
)
2570 register char ** array
;
2575 array
= emalloc((strlen(cp
) + 1) * sizeof *array
);
2578 while (isascii((unsigned char) *cp
) &&
2579 isspace((unsigned char) *cp
))
2581 if (*cp
== '\0' || *cp
== '#')
2583 array
[nsubs
++] = dp
= cp
;
2585 if ((*dp
= *cp
++) != '"')
2587 else while ((*dp
= *cp
++) != '"')
2592 "Odd number of quotation marks"
2596 } while (*cp
!= '\0' && *cp
!= '#' &&
2597 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2598 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2602 array
[nsubs
] = NULL
;
2606 static ATTRIBUTE_PURE zic_t
2607 oadd(const zic_t t1
, const zic_t t2
)
2609 if (t1
< 0 ? t2
< ZIC_MIN
- t1
: ZIC_MAX
- t1
< t2
) {
2610 error(_("time overflow"));
2616 static ATTRIBUTE_PURE zic_t
2617 tadd(const zic_t t1
, const zic_t t2
)
2619 if (t1
== max_time
&& t2
> 0)
2621 if (t1
== min_time
&& t2
< 0)
2623 if (t1
< 0 ? t2
< min_time
- t1
: max_time
- t1
< t2
) {
2624 error(_("time overflow"));
2631 ** Given a rule, and a year, compute the date - in seconds since January 1,
2632 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2636 rpytime(register const struct rule
*const rp
, register const zic_t wantedy
)
2639 register zic_t dayoff
; /* with a nod to Margaret O. */
2640 register zic_t t
, y
;
2642 if (wantedy
== ZIC_MIN
)
2644 if (wantedy
== ZIC_MAX
)
2649 while (wantedy
!= y
) {
2651 i
= len_years
[isleap(y
)];
2655 i
= -len_years
[isleap(y
)];
2657 dayoff
= oadd(dayoff
, i
);
2659 while (m
!= rp
->r_month
) {
2660 i
= len_months
[isleap(y
)][m
];
2661 dayoff
= oadd(dayoff
, i
);
2664 i
= rp
->r_dayofmonth
;
2665 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2666 if (rp
->r_dycode
== DC_DOWLEQ
)
2669 error(_("use of 2/29 in non leap-year"));
2674 dayoff
= oadd(dayoff
, i
);
2675 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2676 register zic_t wday
;
2678 #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
2681 ** Don't trust mod of negative numbers.
2684 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2686 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2688 wday
+= LDAYSPERWEEK
;
2690 while (wday
!= rp
->r_wday
)
2691 if (rp
->r_dycode
== DC_DOWGEQ
) {
2692 dayoff
= oadd(dayoff
, 1);
2693 if (++wday
>= LDAYSPERWEEK
)
2697 dayoff
= oadd(dayoff
, -1);
2699 wday
= LDAYSPERWEEK
- 1;
2702 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2704 warning(_("rule goes past start/end of month--\
2705 will not work with pre-2004 versions of zic"));
2708 if (dayoff
< min_time
/ SECSPERDAY
)
2710 if (dayoff
> max_time
/ SECSPERDAY
)
2712 t
= (zic_t
) dayoff
* SECSPERDAY
;
2713 return tadd(t
, rp
->r_tod
);
2717 newabbr(const char *const string
)
2721 if (strcmp(string
, GRANDPARENTED
) != 0) {
2722 register const char * cp
;
2726 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2727 ** optionally followed by a + or - and a number from 1 to 14.
2731 while (isascii((unsigned char) *cp
) &&
2732 isalpha((unsigned char) *cp
))
2734 if (cp
- string
== 0)
2735 mp
= _("time zone abbreviation lacks alphabetic at start");
2736 if (noise
&& cp
- string
< 3)
2737 mp
= _("time zone abbreviation has fewer than 3 alphabetics");
2738 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2739 mp
= _("time zone abbreviation has too many alphabetics");
2740 if (mp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2742 if (isascii((unsigned char) *cp
) &&
2743 isdigit((unsigned char) *cp
))
2745 *cp
>= '0' && *cp
<= '4')
2749 mp
= _("time zone abbreviation differs from POSIX standard");
2751 warning("%s (%s)", mp
, string
);
2753 i
= strlen(string
) + 1;
2754 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2755 error(_("too many, or too long, time zone abbreviations"));
2758 (void) strcpy(&chars
[charcnt
], string
);
2763 mkdirs(char *argname
)
2765 register char * name
;
2768 if (argname
== NULL
|| *argname
== '\0')
2770 cp
= name
= ecpyalloc(argname
);
2771 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2773 #ifdef HAVE_DOS_FILE_NAMES
2775 ** DOS drive specifier?
2777 if (isalpha((unsigned char) name
[0]) &&
2778 name
[1] == ':' && name
[2] == '\0') {
2783 if (!itsdir(name
)) {
2785 ** It doesn't seem to exist, so we try to create it.
2786 ** Creation may fail because of the directory being
2787 ** created by some other multiprocessor, so we get
2788 ** to do extra checking.
2790 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2791 const char *e
= strerror(errno
);
2793 if (errno
!= EEXIST
|| !itsdir(name
)) {
2794 (void) fprintf(stderr
,
2795 _("%s: Can't create directory %s: %s\n"),
2809 ** UNIX was a registered trademark of The Open Group in 2003.