1 /* $NetBSD: zic.c,v 1.43 2013/09/20 19:06:54 christos Exp $ */
3 ** This file is in the public domain, so clarified as of
4 ** 2006-07-17 by Arthur David Olson.
7 #if HAVE_NBTOOL_CONFIG_H
8 #include "nbtool_config.h"
11 #include <sys/cdefs.h>
13 __RCSID("$NetBSD: zic.c,v 1.43 2013/09/20 19:06:54 christos Exp $");
14 #endif /* !defined lint */
24 #define ZIC_VERSION_PRE_2013 '2'
25 #define ZIC_VERSION '3'
27 typedef int_fast64_t zic_t
;
28 #define ZIC_MIN INT_FAST64_MIN
29 #define ZIC_MAX INT_FAST64_MAX
30 #define SCNdZIC SCNdFAST64
32 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
33 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
34 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
40 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
42 #define MKDIR_UMASK 0755
46 ** On some ancient hosts, predicates like `isspace(C)' are defined
47 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
48 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
49 ** Neither the C Standard nor Posix require that `isascii' exist.
50 ** For portability, we check both ancient and modern requirements.
51 ** If isascii is not defined, the isascii check succeeds trivially.
58 #define end(cp) (strchr((cp), '\0'))
61 const char * r_filename
;
65 zic_t r_loyear
; /* for example, 1986 */
66 zic_t r_hiyear
; /* for example, 1986 */
67 const char * r_yrtype
;
71 int r_month
; /* 0..11 */
73 int r_dycode
; /* see below */
77 zic_t r_tod
; /* time from midnight */
78 int r_todisstd
; /* above is standard time if TRUE */
79 /* or wall clock time if FALSE */
80 int r_todisgmt
; /* above is GMT if TRUE */
81 /* or local time if FALSE */
82 zic_t r_stdoff
; /* offset from standard time */
83 const char * r_abbrvar
; /* variable part of abbreviation */
85 int r_todo
; /* a rule to do (used in outzone) */
86 zic_t r_temp
; /* used in outzone */
90 ** r_dycode r_dayofmonth r_wday
93 #define DC_DOM 0 /* 1..31 */ /* unused */
94 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
95 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
98 const char * z_filename
;
104 const char * z_format
;
108 struct rule
* z_rules
;
111 struct rule z_untilrule
;
115 extern int getopt(int argc
, char * const argv
[],
116 const char * options
);
117 extern int link(const char * fromname
, const char * toname
);
118 extern char * optarg
;
121 static void addtt(zic_t starttime
, int type
);
122 static int addtype(zic_t gmtoff
, const char * abbr
, int isdst
,
123 int ttisstd
, int ttisgmt
);
124 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
125 static void adjleap(void);
126 static void associate(void);
127 static void dolink(const char * fromfield
, const char * tofield
);
128 static char ** getfields(char * buf
);
129 static zic_t
gethms(const char * string
, const char * errstrng
,
131 static void infile(const char * filename
);
132 static void inleap(char ** fields
, int nfields
);
133 static void inlink(char ** fields
, int nfields
);
134 static void inrule(char ** fields
, int nfields
);
135 static int inzcont(char ** fields
, int nfields
);
136 static int inzone(char ** fields
, int nfields
);
137 static int inzsub(char ** fields
, int nfields
, int iscont
);
138 static int itsdir(const char * name
);
139 static int lowerit(int c
);
140 static int mkdirs(char * filename
);
141 static void newabbr(const char * abbr
);
142 static zic_t
oadd(zic_t t1
, zic_t t2
);
143 static void outzone(const struct zone
* zp
, int ntzones
);
144 static int rcomp(const void * leftp
, const void * rightp
);
145 static zic_t
rpytime(const struct rule
* rp
, zic_t wantedy
);
146 static void rulesub(struct rule
* rp
,
147 const char * loyearp
, const char * hiyearp
,
148 const char * typep
, const char * monthp
,
149 const char * dayp
, const char * timep
);
150 static zic_t
tadd(zic_t t1
, zic_t t2
);
151 static int yearistype(int year
, const char * type
);
152 static int atcomp(const void *avp
, const void *bvp
);
153 static void updateminmax(zic_t x
);
157 static const char * filename
;
160 static zic_t leapminyear
;
161 static zic_t leapmaxyear
;
163 static size_t max_abbrvar_len
;
164 static size_t max_format_len
;
165 static zic_t max_year
;
166 static zic_t min_year
;
168 static const char * rfilename
;
170 static const char * progname
;
184 ** Which fields are which on a Zone line.
192 #define ZF_TILMONTH 6
195 #define ZONE_MINFIELDS 5
196 #define ZONE_MAXFIELDS 9
199 ** Which fields are which on a Zone continuation line.
205 #define ZFC_TILYEAR 3
206 #define ZFC_TILMONTH 4
208 #define ZFC_TILTIME 6
209 #define ZONEC_MINFIELDS 3
210 #define ZONEC_MAXFIELDS 7
213 ** Which files are which on a Rule line.
225 #define RULE_FIELDS 10
228 ** Which fields are which on a Link line.
233 #define LINK_FIELDS 3
236 ** Which fields are which on a Leap line.
245 #define LEAP_FIELDS 7
255 static struct rule
* rules
;
256 static int nrules
; /* number of rules */
258 static struct zone
* zones
;
259 static int nzones
; /* number of zones */
262 const char * l_filename
;
268 static struct link
* links
;
276 static struct lookup
const * byword(const char * string
,
277 const struct lookup
* lp
);
279 static struct lookup
const line_codes
[] = {
287 static struct lookup
const mon_names
[] = {
288 { "January", TM_JANUARY
},
289 { "February", TM_FEBRUARY
},
290 { "March", TM_MARCH
},
291 { "April", TM_APRIL
},
295 { "August", TM_AUGUST
},
296 { "September", TM_SEPTEMBER
},
297 { "October", TM_OCTOBER
},
298 { "November", TM_NOVEMBER
},
299 { "December", TM_DECEMBER
},
303 static struct lookup
const wday_names
[] = {
304 { "Sunday", TM_SUNDAY
},
305 { "Monday", TM_MONDAY
},
306 { "Tuesday", TM_TUESDAY
},
307 { "Wednesday", TM_WEDNESDAY
},
308 { "Thursday", TM_THURSDAY
},
309 { "Friday", TM_FRIDAY
},
310 { "Saturday", TM_SATURDAY
},
314 static struct lookup
const lasts
[] = {
315 { "last-Sunday", TM_SUNDAY
},
316 { "last-Monday", TM_MONDAY
},
317 { "last-Tuesday", TM_TUESDAY
},
318 { "last-Wednesday", TM_WEDNESDAY
},
319 { "last-Thursday", TM_THURSDAY
},
320 { "last-Friday", TM_FRIDAY
},
321 { "last-Saturday", TM_SATURDAY
},
325 static struct lookup
const begin_years
[] = {
326 { "minimum", YR_MINIMUM
},
327 { "maximum", YR_MAXIMUM
},
331 static struct lookup
const end_years
[] = {
332 { "minimum", YR_MINIMUM
},
333 { "maximum", YR_MAXIMUM
},
338 static struct lookup
const leap_types
[] = {
340 { "Stationary", FALSE
},
344 static const int len_months
[2][MONSPERYEAR
] = {
345 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
346 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
349 static const int len_years
[2] = {
350 DAYSPERNYEAR
, DAYSPERLYEAR
353 static struct attype
{
356 } attypes
[TZ_MAX_TIMES
];
357 static zic_t gmtoffs
[TZ_MAX_TYPES
];
358 static char isdsts
[TZ_MAX_TYPES
];
359 static unsigned char abbrinds
[TZ_MAX_TYPES
];
360 static char ttisstds
[TZ_MAX_TYPES
];
361 static char ttisgmts
[TZ_MAX_TYPES
];
362 static char chars
[TZ_MAX_CHARS
];
363 static zic_t trans
[TZ_MAX_LEAPS
];
364 static zic_t corr
[TZ_MAX_LEAPS
];
365 static char roll
[TZ_MAX_LEAPS
];
368 ** Memory allocation.
371 static ATTRIBUTE_PURE
void *
372 memcheck(void *const ptr
)
375 const char *e
= strerror(errno
);
377 (void) fprintf(stderr
, _("%s: Memory exhausted: %s\n"),
384 #define emalloc(size) memcheck(malloc(size))
385 #define erealloc(ptr, size) memcheck(realloc((ptr), (size)))
386 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
387 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
394 eats(const char *const name
, const int num
, const char *const rname
,
404 eat(const char *const name
, const int num
)
406 eats(name
, num
, NULL
, -1);
409 static void ATTRIBUTE_FORMAT((printf
, 1, 0))
410 verror(const char *const string
, va_list args
)
413 ** Match the format of "cc" to allow sh users to
414 ** zic ... 2>&1 | error -t "*" -v
417 fprintf(stderr
, _("\"%s\", line %d: "), filename
, linenum
);
418 vfprintf(stderr
, string
, args
);
419 if (rfilename
!= NULL
)
420 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
421 rfilename
, rlinenum
);
422 (void) fprintf(stderr
, "\n");
426 static void ATTRIBUTE_FORMAT((printf
, 1, 2))
427 error(const char *const string
, ...)
430 va_start(args
, string
);
431 verror(string
, args
);
435 static void ATTRIBUTE_FORMAT((printf
, 1, 2))
436 warning(const char *const string
, ...)
439 fprintf(stderr
, _("warning: "));
440 va_start(args
, string
);
441 verror(string
, args
);
446 static _Noreturn
void
447 usage(FILE *stream
, int status
)
449 (void) fprintf(stream
, _("%s: usage is %s \
450 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
451 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
453 Report bugs to %s.\n"),
454 progname
, progname
, REPORT_BUGS_TO
);
458 static const char * psxrules
;
459 static const char * lcltime
;
460 static const char * directory
;
461 static const char * leapsec
;
462 static const char * yitcommand
;
465 main(int argc
, char *argv
[])
472 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
475 (void) setlocale(LC_MESSAGES
, "");
477 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
478 #endif /* defined TEXTDOMAINDIR */
479 (void) textdomain(TZ_DOMAIN
);
480 #endif /* HAVE_GETTEXT */
482 if (TYPE_BIT(zic_t
) < 64) {
483 (void) fprintf(stderr
, "%s: %s\n", progname
,
484 _("wild compilation-time specification of zic_t"));
487 for (i
= 1; i
< argc
; ++i
)
488 if (strcmp(argv
[i
], "--version") == 0) {
489 (void) printf("zic %s%s\n", PKGVERSION
, TZVERSION
);
491 } else if (strcmp(argv
[i
], "--help") == 0) {
492 usage(stdout
, EXIT_SUCCESS
);
494 while ((c
= getopt(argc
, argv
, "d:l:p:L:vsy:")) != EOF
&& c
!= -1)
497 usage(stderr
, EXIT_FAILURE
);
499 if (directory
== NULL
)
502 (void) fprintf(stderr
,
503 _("%s: More than one -d option specified\n"),
512 (void) fprintf(stderr
,
513 _("%s: More than one -l option specified\n"),
519 if (psxrules
== NULL
)
522 (void) fprintf(stderr
,
523 _("%s: More than one -p option specified\n"),
529 if (yitcommand
== NULL
)
532 (void) fprintf(stderr
,
533 _("%s: More than one -y option specified\n"),
542 (void) fprintf(stderr
,
543 _("%s: More than one -L option specified\n"),
552 (void) printf("%s: -s ignored\n", progname
);
555 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
556 usage(stderr
, EXIT_FAILURE
); /* usage message by request */
557 if (directory
== NULL
)
559 if (yitcommand
== NULL
)
560 yitcommand
= "yearistype";
562 if (optind
< argc
&& leapsec
!= NULL
) {
567 for (i
= optind
; i
< argc
; ++i
)
572 for (i
= 0; i
< nzones
; i
= j
) {
574 ** Find the next non-continuation zone entry.
576 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
578 outzone(&zones
[i
], j
- i
);
583 for (i
= 0; i
< nlinks
; ++i
) {
584 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
585 dolink(links
[i
].l_from
, links
[i
].l_to
);
587 for (j
= 0; j
< nlinks
; ++j
)
588 if (strcmp(links
[i
].l_to
,
589 links
[j
].l_from
) == 0)
590 warning(_("link to link"));
592 if (lcltime
!= NULL
) {
593 eat("command line", 1);
594 dolink(lcltime
, TZDEFAULT
);
596 if (psxrules
!= NULL
) {
597 eat("command line", 1);
598 dolink(psxrules
, TZDEFRULES
);
600 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
604 dolink(const char *const fromfield
, const char *const tofield
)
609 if (fromfield
[0] == '/')
610 fromname
= ecpyalloc(fromfield
);
612 fromname
= ecpyalloc(directory
);
613 fromname
= ecatalloc(fromname
, "/");
614 fromname
= ecatalloc(fromname
, fromfield
);
616 if (tofield
[0] == '/')
617 toname
= ecpyalloc(tofield
);
619 toname
= ecpyalloc(directory
);
620 toname
= ecatalloc(toname
, "/");
621 toname
= ecatalloc(toname
, tofield
);
624 ** We get to be careful here since
625 ** there's a fair chance of root running us.
628 (void) remove(toname
);
629 if (link(fromname
, toname
) != 0) {
632 if (mkdirs(toname
) != 0)
635 result
= link(fromname
, toname
);
638 access(fromname
, F_OK
) == 0 &&
640 const char *s
= tofield
;
641 char * symlinkcontents
= NULL
;
643 while ((s
= strchr(s
+1, '/')) != NULL
)
645 ecatalloc(symlinkcontents
,
648 ecatalloc(symlinkcontents
,
650 result
= symlink(symlinkcontents
,
653 warning(_("hard link failed, symbolic link used"));
654 free(symlinkcontents
);
656 #endif /* HAVE_SYMLINK */
658 const char *e
= strerror(errno
);
660 (void) fprintf(stderr
,
661 _("%s: Can't link from %s to %s: %s\n"),
662 progname
, fromname
, toname
, e
);
670 #define TIME_T_BITS_IN_FILE 64
672 static const zic_t min_time
= (zic_t
) -1 << (TIME_T_BITS_IN_FILE
- 1);
673 static const zic_t max_time
= -1 - ((zic_t
) -1 << (TIME_T_BITS_IN_FILE
- 1));
676 itsdir(const char *const name
)
681 myname
= ecpyalloc(name
);
682 myname
= ecatalloc(myname
, "/.");
683 accres
= access(myname
, F_OK
);
689 ** Associate sets of rules with zones.
693 ** Sort by rule name.
697 rcomp(const void *cp1
, const void *cp2
)
699 return strcmp(((const struct rule
*) cp1
)->r_name
,
700 ((const struct rule
*) cp2
)->r_name
);
712 (void) qsort(rules
, (size_t)nrules
, sizeof *rules
, rcomp
);
713 for (i
= 0; i
< nrules
- 1; ++i
) {
714 if (strcmp(rules
[i
].r_name
,
715 rules
[i
+ 1].r_name
) != 0)
717 if (strcmp(rules
[i
].r_filename
,
718 rules
[i
+ 1].r_filename
) == 0)
720 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
721 warning(_("same rule name in multiple files"));
722 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
723 warning(_("same rule name in multiple files"));
724 for (j
= i
+ 2; j
< nrules
; ++j
) {
725 if (strcmp(rules
[i
].r_name
,
726 rules
[j
].r_name
) != 0)
728 if (strcmp(rules
[i
].r_filename
,
729 rules
[j
].r_filename
) == 0)
731 if (strcmp(rules
[i
+ 1].r_filename
,
732 rules
[j
].r_filename
) == 0)
739 for (i
= 0; i
< nzones
; ++i
) {
744 for (base
= 0; base
< nrules
; base
= out
) {
746 for (out
= base
+ 1; out
< nrules
; ++out
)
747 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
749 for (i
= 0; i
< nzones
; ++i
) {
751 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
754 zp
->z_nrules
= out
- base
;
757 for (i
= 0; i
< nzones
; ++i
) {
759 if (zp
->z_nrules
== 0) {
761 ** Maybe we have a local standard time offset.
763 eat(zp
->z_filename
, zp
->z_linenum
);
764 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
767 ** Note, though, that if there's no rule,
768 ** a '%s' in the format is a bad thing.
770 if (strchr(zp
->z_format
, '%') != 0)
771 error("%s", _("%s in ruleless zone"));
779 infile(const char *name
)
784 const struct lookup
* lp
;
790 if (strcmp(name
, "-") == 0) {
791 name
= _("standard input");
793 } else if ((fp
= fopen(name
, "r")) == NULL
) {
794 const char *e
= strerror(errno
);
796 (void) fprintf(stderr
, _("%s: Can't open %s: %s\n"),
801 for (num
= 1; ; ++num
) {
803 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
805 cp
= strchr(buf
, '\n');
807 error(_("line too long"));
811 fields
= getfields(buf
);
813 while (fields
[nfields
] != NULL
) {
816 if (strcmp(fields
[nfields
], "-") == 0)
817 fields
[nfields
] = &nada
;
822 } else if (wantcont
) {
823 wantcont
= inzcont(fields
, nfields
);
825 lp
= byword(fields
[0], line_codes
);
827 error(_("input line of unknown type"));
828 else switch ((int) (lp
->l_value
)) {
830 inrule(fields
, nfields
);
834 wantcont
= inzone(fields
, nfields
);
837 inlink(fields
, nfields
);
842 (void) fprintf(stderr
,
843 _("%s: Leap line in non leap seconds file %s\n"),
845 else inleap(fields
, nfields
);
848 default: /* "cannot happen" */
849 (void) fprintf(stderr
,
850 _("%s: panic: Invalid l_value %d\n"),
851 progname
, lp
->l_value
);
858 (void) fprintf(stderr
, _("%s: Error reading %s\n"),
862 if (fp
!= stdin
&& fclose(fp
)) {
863 const char *e
= strerror(errno
);
865 (void) fprintf(stderr
, _("%s: Error closing %s: %s\n"),
866 progname
, filename
, e
);
870 error(_("expected continuation line not found"));
874 ** Convert a string of one of the forms
875 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
876 ** into a number of seconds.
877 ** A null string maps to zero.
878 ** Call error with errstring and return zero on errors.
882 gethms(const char *string
, const char *const errstring
, const int signable
)
887 if (string
== NULL
|| *string
== '\0')
891 else if (*string
== '-') {
895 if (sscanf(string
, scheck(string
, "%"SCNdZIC
), &hh
) == 1)
897 else if (sscanf(string
, scheck(string
, "%"SCNdZIC
":%d"), &hh
, &mm
) == 2)
899 else if (sscanf(string
, scheck(string
, "%"SCNdZIC
":%d:%d"),
900 &hh
, &mm
, &ss
) != 3) {
901 error("%s", errstring
);
905 mm
< 0 || mm
>= MINSPERHOUR
||
906 ss
< 0 || ss
> SECSPERMIN
) {
907 error("%s", errstring
);
910 if (ZIC_MAX
/ SECSPERHOUR
< hh
) {
911 error(_("time overflow"));
914 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
915 warning(_("24:00 not handled by pre-1998 versions of zic"));
916 if (noise
&& (hh
> HOURSPERDAY
||
917 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
918 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
919 return oadd(sign
* hh
* SECSPERHOUR
,
920 sign
* (mm
* SECSPERMIN
+ ss
));
924 inrule(char **const fields
, const int nfields
)
926 static struct rule r
;
928 if (nfields
!= RULE_FIELDS
) {
929 error(_("wrong number of fields on Rule line"));
932 if (*fields
[RF_NAME
] == '\0') {
933 error(_("nameless rule"));
936 r
.r_filename
= filename
;
937 r
.r_linenum
= linenum
;
938 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
939 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
940 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
941 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
942 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
943 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
944 max_abbrvar_len
= strlen(r
.r_abbrvar
);
945 rules
= erealloc(rules
, (nrules
+ 1) * sizeof *rules
);
950 inzone(char **const fields
, const int nfields
)
954 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
955 error(_("wrong number of fields on Zone line"));
958 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
960 _("\"Zone %s\" line and -l option are mutually exclusive"),
964 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
966 _("\"Zone %s\" line and -p option are mutually exclusive"),
970 for (i
= 0; i
< nzones
; ++i
)
971 if (zones
[i
].z_name
!= NULL
&&
972 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
974 _("duplicate zone name %s (file \"%s\", line %d)"),
980 return inzsub(fields
, nfields
, FALSE
);
984 inzcont(char **const fields
, const int nfields
)
986 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
987 error(_("wrong number of fields on Zone continuation line"));
990 return inzsub(fields
, nfields
, TRUE
);
994 inzsub(char **const fields
, const int nfields
, const int iscont
)
997 static struct zone z
;
998 int i_gmtoff
, i_rule
, i_format
;
999 int i_untilyear
, i_untilmonth
;
1000 int i_untilday
, i_untiltime
;
1004 i_gmtoff
= ZFC_GMTOFF
;
1006 i_format
= ZFC_FORMAT
;
1007 i_untilyear
= ZFC_TILYEAR
;
1008 i_untilmonth
= ZFC_TILMONTH
;
1009 i_untilday
= ZFC_TILDAY
;
1010 i_untiltime
= ZFC_TILTIME
;
1013 i_gmtoff
= ZF_GMTOFF
;
1015 i_format
= ZF_FORMAT
;
1016 i_untilyear
= ZF_TILYEAR
;
1017 i_untilmonth
= ZF_TILMONTH
;
1018 i_untilday
= ZF_TILDAY
;
1019 i_untiltime
= ZF_TILTIME
;
1020 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1022 z
.z_filename
= filename
;
1023 z
.z_linenum
= linenum
;
1024 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UT offset"), TRUE
);
1025 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1026 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1027 error(_("invalid abbreviation format"));
1031 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1032 z
.z_format
= ecpyalloc(fields
[i_format
]);
1033 if (max_format_len
< strlen(z
.z_format
))
1034 max_format_len
= strlen(z
.z_format
);
1035 hasuntil
= nfields
> i_untilyear
;
1037 z
.z_untilrule
.r_filename
= filename
;
1038 z
.z_untilrule
.r_linenum
= linenum
;
1039 rulesub(&z
.z_untilrule
,
1040 fields
[i_untilyear
],
1043 (nfields
> i_untilmonth
) ?
1044 fields
[i_untilmonth
] : "Jan",
1045 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1046 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1047 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1048 z
.z_untilrule
.r_loyear
);
1049 if (iscont
&& nzones
> 0 &&
1050 z
.z_untiltime
> min_time
&&
1051 z
.z_untiltime
< max_time
&&
1052 zones
[nzones
- 1].z_untiltime
> min_time
&&
1053 zones
[nzones
- 1].z_untiltime
< max_time
&&
1054 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1056 "Zone continuation line end time is not after end time of previous line"
1061 zones
= erealloc(zones
, (nzones
+ 1) * sizeof *zones
);
1062 zones
[nzones
++] = z
;
1064 ** If there was an UNTIL field on this line,
1065 ** there's more information about the zone on the next line.
1071 inleap(char **const fields
, const int nfields
)
1074 const struct lookup
* lp
;
1081 if (nfields
!= LEAP_FIELDS
) {
1082 error(_("wrong number of fields on Leap line"));
1086 cp
= fields
[LP_YEAR
];
1087 if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &year
) != 1) {
1091 error(_("invalid leaping year"));
1094 if (!leapseen
|| leapmaxyear
< year
)
1096 if (!leapseen
|| leapminyear
> year
)
1102 i
= len_years
[isleap(j
)];
1106 i
= -len_years
[isleap(j
)];
1108 dayoff
= oadd(dayoff
, i
);
1110 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1111 error(_("invalid month name"));
1114 month
= lp
->l_value
;
1116 while (j
!= month
) {
1117 i
= len_months
[isleap(year
)][j
];
1118 dayoff
= oadd(dayoff
, i
);
1121 cp
= fields
[LP_DAY
];
1122 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1123 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1124 error(_("invalid day of month"));
1127 dayoff
= oadd(dayoff
, day
- 1);
1128 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1129 error(_("time before zero"));
1132 if (dayoff
< min_time
/ SECSPERDAY
) {
1133 error(_("time too small"));
1136 if (dayoff
> max_time
/ SECSPERDAY
) {
1137 error(_("time too large"));
1140 t
= (zic_t
) dayoff
* SECSPERDAY
;
1141 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1142 cp
= fields
[LP_CORR
];
1147 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1150 } else if (strcmp(cp
, "--") == 0) {
1153 } else if (strcmp(cp
, "+") == 0) {
1156 } else if (strcmp(cp
, "++") == 0) {
1160 error(_("illegal CORRECTION field on Leap line"));
1163 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1165 "illegal Rolling/Stationary field on Leap line"
1169 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1174 inlink(char **const fields
, const int nfields
)
1178 if (nfields
!= LINK_FIELDS
) {
1179 error(_("wrong number of fields on Link line"));
1182 if (*fields
[LF_FROM
] == '\0') {
1183 error(_("blank FROM field on Link line"));
1186 if (*fields
[LF_TO
] == '\0') {
1187 error(_("blank TO field on Link line"));
1190 l
.l_filename
= filename
;
1191 l
.l_linenum
= linenum
;
1192 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1193 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1194 links
= erealloc(links
, (nlinks
+ 1) * sizeof *links
);
1195 links
[nlinks
++] = l
;
1199 rulesub(struct rule
*const rp
, const char *const loyearp
,
1200 const char *const hiyearp
, const char *const typep
,
1201 const char *const monthp
, const char *const dayp
, const char *const timep
)
1203 const struct lookup
* lp
;
1208 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1209 error(_("invalid month name"));
1212 rp
->r_month
= lp
->l_value
;
1213 rp
->r_todisstd
= FALSE
;
1214 rp
->r_todisgmt
= FALSE
;
1215 dp
= ecpyalloc(timep
);
1217 ep
= dp
+ strlen(dp
) - 1;
1218 switch (lowerit(*ep
)) {
1219 case 's': /* Standard */
1220 rp
->r_todisstd
= TRUE
;
1221 rp
->r_todisgmt
= FALSE
;
1224 case 'w': /* Wall */
1225 rp
->r_todisstd
= FALSE
;
1226 rp
->r_todisgmt
= FALSE
;
1229 case 'g': /* Greenwich */
1230 case 'u': /* Universal */
1231 case 'z': /* Zulu */
1232 rp
->r_todisstd
= TRUE
;
1233 rp
->r_todisgmt
= TRUE
;
1238 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1244 lp
= byword(cp
, begin_years
);
1245 rp
->r_lowasnum
= lp
== NULL
;
1246 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1248 rp
->r_loyear
= ZIC_MIN
;
1251 rp
->r_loyear
= ZIC_MAX
;
1253 default: /* "cannot happen" */
1254 (void) fprintf(stderr
,
1255 _("%s: panic: Invalid l_value %d\n"),
1256 progname
, lp
->l_value
);
1258 } else if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &rp
->r_loyear
) != 1) {
1259 error(_("invalid starting year"));
1263 lp
= byword(cp
, end_years
);
1264 rp
->r_hiwasnum
= lp
== NULL
;
1265 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1267 rp
->r_hiyear
= ZIC_MIN
;
1270 rp
->r_hiyear
= ZIC_MAX
;
1273 rp
->r_hiyear
= rp
->r_loyear
;
1275 default: /* "cannot happen" */
1276 (void) fprintf(stderr
,
1277 _("%s: panic: Invalid l_value %d\n"),
1278 progname
, lp
->l_value
);
1280 } else if (sscanf(cp
, scheck(cp
, "%"SCNdZIC
), &rp
->r_hiyear
) != 1) {
1281 error(_("invalid ending year"));
1284 if (rp
->r_loyear
> rp
->r_hiyear
) {
1285 error(_("starting year greater than ending year"));
1289 rp
->r_yrtype
= NULL
;
1291 if (rp
->r_loyear
== rp
->r_hiyear
) {
1292 error(_("typed single year"));
1295 rp
->r_yrtype
= ecpyalloc(typep
);
1299 ** Accept things such as:
1305 dp
= ecpyalloc(dayp
);
1306 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1307 rp
->r_dycode
= DC_DOWLEQ
;
1308 rp
->r_wday
= lp
->l_value
;
1309 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1311 if ((ep
= strchr(dp
, '<')) != 0)
1312 rp
->r_dycode
= DC_DOWLEQ
;
1313 else if ((ep
= strchr(dp
, '>')) != 0)
1314 rp
->r_dycode
= DC_DOWGEQ
;
1317 rp
->r_dycode
= DC_DOM
;
1319 if (rp
->r_dycode
!= DC_DOM
) {
1322 error(_("invalid day of month"));
1326 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1327 error(_("invalid weekday name"));
1331 rp
->r_wday
= lp
->l_value
;
1333 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1334 rp
->r_dayofmonth
<= 0 ||
1335 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1336 error(_("invalid day of month"));
1345 convert(const zic_t val
, char *const buf
)
1349 unsigned char *const b
= (unsigned char *) buf
;
1351 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1352 b
[i
] = val
>> shift
;
1356 convert64(const zic_t val
, char *const buf
)
1360 unsigned char *const b
= (unsigned char *) buf
;
1362 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1363 b
[i
] = val
>> shift
;
1367 puttzcode(const zic_t val
, FILE *const fp
)
1372 (void) fwrite(buf
, sizeof buf
, (size_t) 1, fp
);
1376 puttzcode64(const zic_t val
, FILE *const fp
)
1380 convert64(val
, buf
);
1381 (void) fwrite(buf
, sizeof buf
, (size_t) 1, fp
);
1385 atcomp(const void *avp
, const void *bvp
)
1387 const zic_t a
= ((const struct attype
*) avp
)->at
;
1388 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1390 return (a
< b
) ? -1 : (a
> b
);
1396 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1400 writezone(const char *const name
, const char *const string
, char version
)
1404 int leapcnt32
, leapi32
;
1405 int timecnt32
, timei32
;
1407 static char * fullname
;
1408 static const struct tzhead tzh0
;
1409 static struct tzhead tzh
;
1410 zic_t ats
[TZ_MAX_TIMES
];
1411 unsigned char types
[TZ_MAX_TIMES
];
1417 (void) qsort(attypes
, (size_t) timecnt
, sizeof *attypes
,
1428 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1431 ** Remember that type 0 is reserved.
1434 while (fromi
< timecnt
&& attypes
[fromi
].type
== 1)
1435 ++fromi
; /* handled by default rule */
1436 for ( ; fromi
< timecnt
; ++fromi
) {
1437 if (toi
!= 0 && ((attypes
[fromi
].at
+
1438 gmtoffs
[attypes
[toi
- 1].type
]) <=
1439 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1440 : attypes
[toi
- 2].type
]))) {
1441 attypes
[toi
- 1].type
=
1442 attypes
[fromi
].type
;
1446 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1447 attypes
[toi
++] = attypes
[fromi
];
1454 for (i
= 0; i
< timecnt
; ++i
) {
1455 ats
[i
] = attypes
[i
].at
;
1456 types
[i
] = attypes
[i
].type
;
1459 ** Correct for leap seconds.
1461 for (i
= 0; i
< timecnt
; ++i
) {
1464 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1465 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1470 ** Figure out 32-bit-limited starts and counts.
1472 timecnt32
= timecnt
;
1474 leapcnt32
= leapcnt
;
1476 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1478 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1482 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1484 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1488 fullname
= erealloc(fullname
,
1489 strlen(directory
) + 1 + strlen(name
) + 1);
1490 (void) sprintf(fullname
, "%s/%s", directory
, name
); /* XXX: sprintf is safe */
1492 ** Remove old file, if any, to snap links.
1494 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
) {
1495 const char *e
= strerror(errno
);
1497 (void) fprintf(stderr
, _("%s: Can't remove %s: %s\n"),
1498 progname
, fullname
, e
);
1501 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1502 if (mkdirs(fullname
) != 0)
1504 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1505 const char *e
= strerror(errno
);
1507 (void) fprintf(stderr
, _("%s: Can't create %s: %s\n"),
1508 progname
, fullname
, e
);
1512 for (pass
= 1; pass
<= 2; ++pass
) {
1513 int thistimei
, thistimecnt
;
1514 int thisleapi
, thisleapcnt
;
1515 int thistimelim
, thisleaplim
;
1516 int writetype
[TZ_MAX_TIMES
];
1517 int typemap
[TZ_MAX_TYPES
];
1519 char thischars
[TZ_MAX_CHARS
];
1521 int indmap
[TZ_MAX_CHARS
];
1524 thistimei
= timei32
;
1525 thistimecnt
= timecnt32
;
1526 thisleapi
= leapi32
;
1527 thisleapcnt
= leapcnt32
;
1530 thistimecnt
= timecnt
;
1532 thisleapcnt
= leapcnt
;
1534 thistimelim
= thistimei
+ thistimecnt
;
1535 thisleaplim
= thisleapi
+ thisleapcnt
;
1537 ** Remember that type 0 is reserved.
1539 writetype
[0] = FALSE
;
1540 for (i
= 1; i
< typecnt
; ++i
)
1541 writetype
[i
] = thistimecnt
== timecnt
;
1542 if (thistimecnt
== 0) {
1544 ** No transition times fall in the current
1545 ** (32- or 64-bit) window.
1548 writetype
[typecnt
- 1] = TRUE
;
1550 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1552 writetype
[types
[i
]] = TRUE
;
1554 ** For America/Godthab and Antarctica/Palmer
1557 ** Remember that type 0 is reserved.
1560 writetype
[1] = TRUE
;
1562 #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1564 ** For some pre-2011 systems: if the last-to-be-written
1565 ** standard (or daylight) type has an offset different from the
1566 ** most recently used offset,
1567 ** append an (unused) copy of the most recently used type
1568 ** (to help get global "altzone" and "timezone" variables
1572 int mrudst
, mrustd
, hidst
, histd
, type
;
1574 hidst
= histd
= mrudst
= mrustd
= -1;
1575 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1578 if (isdsts
[types
[i
]])
1580 else mrustd
= types
[i
];
1582 for (i
= 0; i
< typecnt
; ++i
)
1588 if (hidst
>= 0 && mrudst
>= 0 && hidst
!= mrudst
&&
1589 gmtoffs
[hidst
] != gmtoffs
[mrudst
]) {
1590 isdsts
[mrudst
] = -1;
1591 type
= addtype(gmtoffs
[mrudst
],
1592 &chars
[abbrinds
[mrudst
]],
1596 isdsts
[mrudst
] = TRUE
;
1597 writetype
[type
] = TRUE
;
1599 if (histd
>= 0 && mrustd
>= 0 && histd
!= mrustd
&&
1600 gmtoffs
[histd
] != gmtoffs
[mrustd
]) {
1601 isdsts
[mrustd
] = -1;
1602 type
= addtype(gmtoffs
[mrustd
],
1603 &chars
[abbrinds
[mrustd
]],
1607 isdsts
[mrustd
] = FALSE
;
1608 writetype
[type
] = TRUE
;
1611 #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
1614 ** Potentially, set type 0 to that of lowest-valued time.
1616 if (thistimei
> 0) {
1617 for (i
= 1; i
< typecnt
; ++i
)
1618 if (writetype
[i
] && !isdsts
[i
])
1620 if (i
!= types
[thistimei
- 1]) {
1621 i
= types
[thistimei
- 1];
1622 gmtoffs
[0] = gmtoffs
[i
];
1623 isdsts
[0] = isdsts
[i
];
1624 ttisstds
[0] = ttisstds
[i
];
1625 ttisgmts
[0] = ttisgmts
[i
];
1626 abbrinds
[0] = abbrinds
[i
];
1627 writetype
[0] = TRUE
;
1628 writetype
[i
] = FALSE
;
1631 for (i
= 0; i
< typecnt
; ++i
)
1632 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : 0;
1633 for (i
= 0; i
< (int)(sizeof indmap
/ sizeof indmap
[0]); ++i
)
1636 for (i
= 0; i
< typecnt
; ++i
) {
1641 if (indmap
[abbrinds
[i
]] >= 0)
1643 thisabbr
= &chars
[abbrinds
[i
]];
1644 for (j
= 0; j
< thischarcnt
; ++j
)
1645 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1647 if (j
== thischarcnt
) {
1648 (void) strcpy(&thischars
[(int) thischarcnt
],
1650 thischarcnt
+= strlen(thisabbr
) + 1;
1652 indmap
[abbrinds
[i
]] = j
;
1654 #define DO(field) (void) fwrite(tzh.field, \
1655 sizeof tzh.field, (size_t) 1, fp)
1657 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1658 tzh
.tzh_version
[0] = version
;
1659 convert(thistypecnt
, tzh
.tzh_ttisgmtcnt
);
1660 convert(thistypecnt
, tzh
.tzh_ttisstdcnt
);
1661 convert(thisleapcnt
, tzh
.tzh_leapcnt
);
1662 convert(thistimecnt
, tzh
.tzh_timecnt
);
1663 convert(thistypecnt
, tzh
.tzh_typecnt
);
1664 convert(thischarcnt
, tzh
.tzh_charcnt
);
1675 for (i
= thistimei
; i
< thistimelim
; ++i
)
1677 puttzcode(ats
[i
], fp
);
1678 else puttzcode64(ats
[i
], fp
);
1679 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1682 uc
= typemap
[types
[i
]];
1683 (void) fwrite(&uc
, sizeof uc
, (size_t) 1, fp
);
1685 for (i
= 0; i
< typecnt
; ++i
)
1687 puttzcode(gmtoffs
[i
], fp
);
1688 (void) putc(isdsts
[i
], fp
);
1689 (void) putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1691 if (thischarcnt
!= 0)
1692 (void) fwrite(thischars
, sizeof thischars
[0],
1693 (size_t) thischarcnt
, fp
);
1694 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1698 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1701 if (++j
>= typecnt
) {
1707 while (j
< timecnt
&&
1712 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1713 } else todo
= trans
[i
];
1715 puttzcode(todo
, fp
);
1716 else puttzcode64(todo
, fp
);
1717 puttzcode(corr
[i
], fp
);
1719 for (i
= 0; i
< typecnt
; ++i
)
1721 (void) putc(ttisstds
[i
], fp
);
1722 for (i
= 0; i
< typecnt
; ++i
)
1724 (void) putc(ttisgmts
[i
], fp
);
1726 (void) fprintf(fp
, "\n%s\n", string
);
1727 if (ferror(fp
) || fclose(fp
)) {
1728 (void) fprintf(stderr
, _("%s: Error writing %s\n"),
1729 progname
, fullname
);
1735 doabbr(char *const abbr
, const int abbrlen
, const char *const format
,
1736 const char *const letters
, const int isdst
, const int doquotes
)
1742 slashp
= strchr(format
, '/');
1743 if (slashp
== NULL
) {
1744 if (letters
== NULL
)
1745 (void) strlcpy(abbr
, format
, abbrlen
);
1746 else (void) snprintf(abbr
, abbrlen
, format
, letters
);
1748 (void) strlcpy(abbr
, slashp
+ 1, abbrlen
);
1750 if (slashp
> format
)
1751 (void) strncpy(abbr
, format
, (size_t)(slashp
- format
));
1752 abbr
[slashp
- format
] = '\0';
1756 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1757 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1758 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1761 if (len
> 0 && *cp
== '\0')
1763 abbr
[len
+ 2] = '\0';
1764 abbr
[len
+ 1] = '>';
1765 for ( ; len
> 0; --len
)
1766 abbr
[len
] = abbr
[len
- 1];
1771 updateminmax(const zic_t x
)
1780 stringoffset(char *result
, zic_t offset
)
1788 (void) strcpy(result
, "-");
1791 seconds
= offset
% SECSPERMIN
;
1792 offset
/= SECSPERMIN
;
1793 minutes
= offset
% MINSPERHOUR
;
1794 offset
/= MINSPERHOUR
;
1796 if (hours
>= HOURSPERDAY
* DAYSPERWEEK
) {
1800 (void) sprintf(end(result
), "%d", hours
);
1801 if (minutes
!= 0 || seconds
!= 0) {
1802 (void) sprintf(end(result
), ":%02d", minutes
);
1804 (void) sprintf(end(result
), ":%02d", seconds
);
1810 stringrule(char *result
, const struct rule
*const rp
, const zic_t dstoff
,
1813 zic_t tod
= rp
->r_tod
;
1816 result
= end(result
);
1817 if (rp
->r_dycode
== DC_DOM
) {
1820 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1823 for (month
= 0; month
< rp
->r_month
; ++month
)
1824 total
+= len_months
[0][month
];
1825 /* Omit the "J" in Jan and Feb, as that's shorter. */
1826 if (rp
->r_month
<= 1)
1827 (void) sprintf(result
, "%d", total
+ rp
->r_dayofmonth
- 1);
1829 (void) sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1832 int wday
= rp
->r_wday
;
1835 if (rp
->r_dycode
== DC_DOWGEQ
) {
1836 wdayoff
= (rp
->r_dayofmonth
- 1) % DAYSPERWEEK
;
1840 tod
+= wdayoff
* SECSPERDAY
;
1841 week
= 1 + (rp
->r_dayofmonth
- 1) / DAYSPERWEEK
;
1842 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1843 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1846 wdayoff
= rp
->r_dayofmonth
% DAYSPERWEEK
;
1850 tod
+= wdayoff
* SECSPERDAY
;
1851 week
= rp
->r_dayofmonth
/ DAYSPERWEEK
;
1853 } else return -1; /* "cannot happen" */
1855 wday
+= DAYSPERWEEK
;
1856 (void) sprintf(result
, "M%d.%d.%d",
1857 rp
->r_month
+ 1, week
, wday
);
1861 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1863 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1864 (void) strcat(result
, "/");
1865 if (stringoffset(end(result
), tod
) != 0)
1870 } else if (SECSPERDAY
<= tod
) {
1879 rule_cmp(struct rule
const *a
, struct rule
const *b
)
1885 if (a
->r_hiyear
!= b
->r_hiyear
)
1886 return a
->r_hiyear
< b
->r_hiyear
? -1 : 1;
1887 if (a
->r_month
- b
->r_month
!= 0)
1888 return a
->r_month
- b
->r_month
;
1889 return a
->r_dayofmonth
- b
->r_dayofmonth
;
1892 enum { YEAR_BY_YEAR_ZONE
= 1 };
1895 stringzone(char *result
, const int resultlen
, const struct zone
*const zpfirst
,
1896 const int zonecount
)
1898 const struct zone
* zp
;
1900 struct rule
* stdrp
;
1901 struct rule
* dstrp
;
1903 const char * abbrvar
;
1906 struct rule stdr
, dstr
;
1909 zp
= zpfirst
+ zonecount
- 1;
1910 stdrp
= dstrp
= NULL
;
1911 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1912 rp
= &zp
->z_rules
[i
];
1913 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= ZIC_MAX
)
1915 if (rp
->r_yrtype
!= NULL
)
1917 if (rp
->r_stdoff
== 0) {
1927 if (stdrp
== NULL
&& dstrp
== NULL
) {
1929 ** There are no rules running through "max".
1930 ** Find the latest std rule in stdabbrrp
1931 ** and latest rule of any type in stdrp.
1933 struct rule
*stdabbrrp
= NULL
;
1934 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1935 rp
= &zp
->z_rules
[i
];
1936 if (rp
->r_stdoff
== 0 && rule_cmp(stdabbrrp
, rp
) < 0)
1938 if (rule_cmp(stdrp
, rp
) < 0)
1942 ** Horrid special case: if year is 2037,
1943 ** presume this is a zone handled on a year-by-year basis;
1944 ** do not try to apply a rule to the zone.
1946 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1947 return YEAR_BY_YEAR_ZONE
;
1949 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0) {
1950 /* Perpetual DST. */
1951 dstr
.r_month
= TM_JANUARY
;
1952 dstr
.r_dycode
= DC_DOM
;
1953 dstr
.r_dayofmonth
= 1;
1955 dstr
.r_todisstd
= dstr
.r_todisgmt
= FALSE
;
1956 dstr
.r_stdoff
= stdrp
->r_stdoff
;
1957 dstr
.r_abbrvar
= stdrp
->r_abbrvar
;
1958 stdr
.r_month
= TM_DECEMBER
;
1959 stdr
.r_dycode
= DC_DOM
;
1960 stdr
.r_dayofmonth
= 31;
1961 stdr
.r_tod
= SECSPERDAY
+ stdrp
->r_stdoff
;
1962 stdr
.r_todisstd
= stdr
.r_todisgmt
= FALSE
;
1965 = (stdabbrrp
? stdabbrrp
->r_abbrvar
: "");
1970 if (stdrp
== NULL
&& (zp
->z_nrules
!= 0 || zp
->z_stdoff
!= 0))
1972 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1973 doabbr(result
, resultlen
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1974 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
1980 doabbr(end(result
), resultlen
- strlen(result
),
1981 zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
1982 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
1983 if (stringoffset(end(result
),
1984 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
1988 (void) strcat(result
, ",");
1989 c
= stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
);
1996 (void) strcat(result
, ",");
1997 c
= stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
);
2008 outzone(const struct zone
*const zpfirst
, const int zonecount
)
2010 const struct zone
* zp
;
2013 int usestart
, useuntil
;
2014 zic_t starttime
, untiltime
;
2025 size_t max_abbr_len
;
2026 size_t max_envvar_len
;
2027 int prodstic
; /* all rules are min to max */
2032 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
2033 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
2034 startbuf
= emalloc(max_abbr_len
+ 1);
2035 ab
= emalloc(max_abbr_len
+ 1);
2036 envvar
= emalloc(max_envvar_len
+ 1);
2037 INITIALIZE(untiltime
);
2038 INITIALIZE(starttime
);
2040 ** Now. . .finally. . .generate some useful data!
2045 prodstic
= zonecount
== 1;
2047 ** Thanks to Earl Chew
2048 ** for noting the need to unconditionally initialize startttisstd.
2050 startttisstd
= FALSE
;
2051 startttisgmt
= FALSE
;
2052 min_year
= max_year
= EPOCH_YEAR
;
2054 updateminmax(leapminyear
);
2055 updateminmax(leapmaxyear
+ (leapmaxyear
< ZIC_MAX
));
2060 gmtoffs
[0] = isdsts
[0] = ttisstds
[0] = ttisgmts
[0] = abbrinds
[0] = -1;
2062 for (i
= 0; i
< zonecount
; ++i
) {
2064 if (i
< zonecount
- 1)
2065 updateminmax(zp
->z_untilrule
.r_loyear
);
2066 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2067 rp
= &zp
->z_rules
[j
];
2069 updateminmax(rp
->r_loyear
);
2071 updateminmax(rp
->r_hiyear
);
2072 if (rp
->r_lowasnum
|| rp
->r_hiwasnum
)
2077 ** Generate lots of data if a rule can't cover all future times.
2079 compat
= stringzone(envvar
, max_envvar_len
+ 1, zpfirst
, zonecount
);
2080 version
= compat
< 2013 ? ZIC_VERSION_PRE_2013
: ZIC_VERSION
;
2081 do_extend
= compat
< 0 || compat
== YEAR_BY_YEAR_ZONE
;
2082 if (noise
&& compat
!= 0 && compat
!= YEAR_BY_YEAR_ZONE
) {
2085 _("no POSIX environment variable for zone"),
2088 /* Circa-COMPAT clients, and earlier clients, might
2089 not work for this zone when given dates before
2090 1970 or after 2038. */
2091 warning(_("%s: pre-%d clients may mishandle"
2092 " distant timestamps"),
2093 zpfirst
->z_name
, compat
);
2098 ** Search through a couple of extra years past the obvious
2099 ** 400, to avoid edge cases. For example, suppose a non-POSIX
2100 ** rule applies from 2012 onwards and has transitions in March
2101 ** and September, plus some one-off transitions in November
2102 ** 2013. If zic looked only at the last 400 years, it would
2103 ** set max_year=2413, with the intent that the 400 years 2014
2104 ** through 2413 will be repeated. The last transition listed
2105 ** in the tzfile would be in 2413-09, less than 400 years
2106 ** after the last one-off transition in 2013-11. Two years
2107 ** might be overkill, but with the kind of edge cases
2108 ** available we're not sure that one year would suffice.
2110 enum { years_of_observations
= YEARSPERREPEAT
+ 2 };
2112 if (min_year
>= ZIC_MIN
+ years_of_observations
)
2113 min_year
-= years_of_observations
;
2114 else min_year
= ZIC_MIN
;
2115 if (max_year
<= ZIC_MAX
- years_of_observations
)
2116 max_year
+= years_of_observations
;
2117 else max_year
= ZIC_MAX
;
2119 ** Regardless of any of the above,
2120 ** for a "proDSTic" zone which specifies that its rules
2121 ** always have and always will be in effect,
2122 ** we only need one cycle to define the zone.
2126 max_year
= min_year
+ years_of_observations
;
2130 ** For the benefit of older systems,
2131 ** generate data from 1900 through 2037.
2133 if (min_year
> 1900)
2135 if (max_year
< 2037)
2137 for (i
= 0; i
< zonecount
; ++i
) {
2139 ** A guess that may well be corrected later.
2143 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
2144 useuntil
= i
< (zonecount
- 1);
2145 if (useuntil
&& zp
->z_untiltime
<= min_time
)
2147 gmtoff
= zp
->z_gmtoff
;
2148 eat(zp
->z_filename
, zp
->z_linenum
);
2150 startoff
= zp
->z_gmtoff
;
2151 if (zp
->z_nrules
== 0) {
2152 stdoff
= zp
->z_stdoff
;
2153 doabbr(startbuf
, max_abbr_len
+ 1, zp
->z_format
,
2154 NULL
, stdoff
!= 0, FALSE
);
2155 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
2156 startbuf
, stdoff
!= 0, startttisstd
,
2159 addtt(starttime
, type
);
2161 } else if (stdoff
!= 0)
2162 addtt(min_time
, type
);
2163 } else for (year
= min_year
; year
<= max_year
; ++year
) {
2164 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
2167 ** Mark which rules to do in the current year.
2168 ** For those to do, calculate rpytime(rp, year);
2170 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2171 rp
= &zp
->z_rules
[j
];
2172 eats(zp
->z_filename
, zp
->z_linenum
,
2173 rp
->r_filename
, rp
->r_linenum
);
2174 rp
->r_todo
= year
>= rp
->r_loyear
&&
2175 year
<= rp
->r_hiyear
&&
2176 yearistype(year
, rp
->r_yrtype
);
2178 rp
->r_temp
= rpytime(rp
, year
);
2188 ** Turn untiltime into UT
2189 ** assuming the current gmtoff and
2192 untiltime
= zp
->z_untiltime
;
2193 if (!zp
->z_untilrule
.r_todisgmt
)
2194 untiltime
= tadd(untiltime
,
2196 if (!zp
->z_untilrule
.r_todisstd
)
2197 untiltime
= tadd(untiltime
,
2201 ** Find the rule (of those to do, if any)
2202 ** that takes effect earliest in the year.
2205 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
2206 rp
= &zp
->z_rules
[j
];
2209 eats(zp
->z_filename
, zp
->z_linenum
,
2210 rp
->r_filename
, rp
->r_linenum
);
2211 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
2212 if (!rp
->r_todisstd
)
2213 offset
= oadd(offset
, stdoff
);
2215 if (jtime
== min_time
||
2218 jtime
= tadd(jtime
, -offset
);
2219 if (k
< 0 || jtime
< ktime
) {
2225 break; /* go on to next year */
2226 rp
= &zp
->z_rules
[k
];
2228 if (useuntil
&& ktime
>= untiltime
)
2230 stdoff
= rp
->r_stdoff
;
2231 if (usestart
&& ktime
== starttime
)
2234 if (ktime
< starttime
) {
2235 startoff
= oadd(zp
->z_gmtoff
,
2245 if (*startbuf
== '\0' &&
2246 startoff
== oadd(zp
->z_gmtoff
,
2257 eats(zp
->z_filename
, zp
->z_linenum
,
2258 rp
->r_filename
, rp
->r_linenum
);
2259 doabbr(ab
, max_abbr_len
+1, zp
->z_format
, rp
->r_abbrvar
,
2260 rp
->r_stdoff
!= 0, FALSE
);
2261 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2262 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2263 rp
->r_todisstd
, rp
->r_todisgmt
);
2268 if (*startbuf
== '\0' &&
2269 zp
->z_format
!= NULL
&&
2270 strchr(zp
->z_format
, '%') == NULL
&&
2271 strchr(zp
->z_format
, '/') == NULL
)
2272 (void)strncpy(startbuf
, zp
->z_format
,
2273 max_abbr_len
+ 1 - 1);
2274 eat(zp
->z_filename
, zp
->z_linenum
);
2275 if (*startbuf
== '\0')
2276 error(_("can't determine time zone abbreviation to use just after until time"));
2277 else addtt(starttime
,
2278 addtype(startoff
, startbuf
,
2279 startoff
!= zp
->z_gmtoff
,
2284 ** Now we may get to set starttime for the next zone line.
2287 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2288 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2289 starttime
= zp
->z_untiltime
;
2291 starttime
= tadd(starttime
, -stdoff
);
2293 starttime
= tadd(starttime
, -gmtoff
);
2298 ** If we're extending the explicitly listed observations
2299 ** for 400 years because we can't fill the POSIX-TZ field,
2300 ** check whether we actually ended up explicitly listing
2301 ** observations through that period. If there aren't any
2302 ** near the end of the 400-year period, add a redundant
2303 ** one at the end of the final year, to make it clear
2304 ** that we are claiming to have definite knowledge of
2305 ** the lack of transitions up to that point.
2308 struct attype
*lastat
;
2309 xr
.r_month
= TM_JANUARY
;
2310 xr
.r_dycode
= DC_DOM
;
2311 xr
.r_dayofmonth
= 1;
2313 for (lastat
= &attypes
[0], i
= 1; i
< timecnt
; i
++)
2314 if (attypes
[i
].at
> lastat
->at
)
2315 lastat
= &attypes
[i
];
2316 if (lastat
->at
< rpytime(&xr
, max_year
- 1)) {
2318 ** Create new type code for the redundant entry,
2319 ** to prevent it being optimised away.
2321 if (typecnt
>= TZ_MAX_TYPES
) {
2322 error(_("too many local time types"));
2325 gmtoffs
[typecnt
] = gmtoffs
[lastat
->type
];
2326 isdsts
[typecnt
] = isdsts
[lastat
->type
];
2327 ttisstds
[typecnt
] = ttisstds
[lastat
->type
];
2328 ttisgmts
[typecnt
] = ttisgmts
[lastat
->type
];
2329 abbrinds
[typecnt
] = abbrinds
[lastat
->type
];
2331 addtt(rpytime(&xr
, max_year
+ 1), typecnt
-1);
2334 writezone(zpfirst
->z_name
, envvar
, version
);
2341 addtt(const zic_t starttime
, int type
)
2343 if (starttime
<= min_time
||
2344 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2345 gmtoffs
[0] = gmtoffs
[type
];
2346 isdsts
[0] = isdsts
[type
];
2347 ttisstds
[0] = ttisstds
[type
];
2348 ttisgmts
[0] = ttisgmts
[type
];
2349 if (abbrinds
[type
] != 0)
2350 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
2352 charcnt
= strlen(chars
) + 1;
2357 if (timecnt
>= TZ_MAX_TIMES
) {
2358 error(_("too many transitions?!"));
2361 attypes
[timecnt
].at
= starttime
;
2362 attypes
[timecnt
].type
= type
;
2367 addtype(const zic_t gmtoff
, const char *const abbr
, const int isdst
,
2368 const int ttisstd
, const int ttisgmt
)
2372 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2373 error(_("internal error - addtype called with bad isdst"));
2376 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2377 error(_("internal error - addtype called with bad ttisstd"));
2380 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2381 error(_("internal error - addtype called with bad ttisgmt"));
2385 ** See if there's already an entry for this zone type.
2386 ** If so, just return its index.
2388 for (i
= 0; i
< typecnt
; ++i
) {
2389 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2390 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2391 ttisstd
== ttisstds
[i
] &&
2392 ttisgmt
== ttisgmts
[i
])
2396 ** There isn't one; add a new one, unless there are already too
2399 if (typecnt
>= TZ_MAX_TYPES
) {
2400 error(_("too many local time types"));
2403 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2404 error(_("UT offset out of range"));
2407 gmtoffs
[i
] = gmtoff
;
2409 ttisstds
[i
] = ttisstd
;
2410 ttisgmts
[i
] = ttisgmt
;
2412 for (j
= 0; j
< charcnt
; ++j
)
2413 if (strcmp(&chars
[j
], abbr
) == 0)
2423 leapadd(const zic_t t
, const int positive
, const int rolling
, int count
)
2427 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2428 error(_("too many leap seconds"));
2431 for (i
= 0; i
< leapcnt
; ++i
)
2432 if (t
<= trans
[i
]) {
2433 if (t
== trans
[i
]) {
2434 error(_("repeated leap second moment"));
2440 for (j
= leapcnt
; j
> i
; --j
) {
2441 trans
[j
] = trans
[j
- 1];
2442 corr
[j
] = corr
[j
- 1];
2443 roll
[j
] = roll
[j
- 1];
2446 corr
[i
] = positive
? 1 : -count
;
2449 } while (positive
&& --count
!= 0);
2459 ** propagate leap seconds forward
2461 for (i
= 0; i
< leapcnt
; ++i
) {
2462 trans
[i
] = tadd(trans
[i
], last
);
2463 last
= corr
[i
] += last
;
2468 yearistype(const int year
, const char *const type
)
2473 if (type
== NULL
|| *type
== '\0')
2475 buf
= erealloc(buf
, 132 + strlen(yitcommand
) + strlen(type
));
2476 (void)sprintf(buf
, "%s %d %s", yitcommand
, year
, type
); /* XXX: sprintf is safe */
2477 result
= system(buf
);
2478 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2484 error(_("Wild result from command execution"));
2485 (void) fprintf(stderr
, _("%s: command was '%s', result was %d\n"),
2486 progname
, buf
, result
);
2494 a
= (unsigned char) a
;
2495 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2498 /* case-insensitive equality */
2499 static ATTRIBUTE_PURE
int
2500 ciequal(const char *ap
, const char *bp
)
2502 while (lowerit(*ap
) == lowerit(*bp
++))
2508 static ATTRIBUTE_PURE
int
2509 itsabbr(const char *abbr
, const char *word
)
2511 if (lowerit(*abbr
) != lowerit(*word
))
2514 while (*++abbr
!= '\0')
2518 } while (lowerit(*word
++) != lowerit(*abbr
));
2522 static ATTRIBUTE_PURE
const struct lookup
*
2523 byword(const char *const word
, const struct lookup
*const table
)
2525 const struct lookup
* foundlp
;
2526 const struct lookup
* lp
;
2528 if (word
== NULL
|| table
== NULL
)
2531 ** Look for exact match.
2533 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2534 if (ciequal(word
, lp
->l_word
))
2537 ** Look for inexact match.
2540 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2541 if (itsabbr(word
, lp
->l_word
)) {
2542 if (foundlp
== NULL
)
2544 else return NULL
; /* multiple inexact matches */
2558 array
= emalloc((strlen(cp
) + 1) * sizeof *array
);
2561 while (isascii((unsigned char) *cp
) &&
2562 isspace((unsigned char) *cp
))
2564 if (*cp
== '\0' || *cp
== '#')
2566 array
[nsubs
++] = dp
= cp
;
2568 if ((*dp
= *cp
++) != '"')
2570 else while ((*dp
= *cp
++) != '"')
2575 "Odd number of quotation marks"
2579 } while (*cp
!= '\0' && *cp
!= '#' &&
2580 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2581 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2585 array
[nsubs
] = NULL
;
2589 static ATTRIBUTE_PURE zic_t
2590 oadd(const zic_t t1
, const zic_t t2
)
2592 if (t1
< 0 ? t2
< ZIC_MIN
- t1
: ZIC_MAX
- t1
< t2
) {
2593 error(_("time overflow"));
2599 static ATTRIBUTE_PURE zic_t
2600 tadd(const zic_t t1
, const zic_t t2
)
2602 if (t1
== max_time
&& t2
> 0)
2604 if (t1
== min_time
&& t2
< 0)
2606 if (t1
< 0 ? t2
< min_time
- t1
: max_time
- t1
< t2
) {
2607 error(_("time overflow"));
2614 ** Given a rule, and a year, compute the date - in seconds since January 1,
2615 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2619 rpytime(const struct rule
*const rp
, const zic_t wantedy
)
2622 zic_t dayoff
; /* with a nod to Margaret O. */
2625 if (wantedy
== ZIC_MIN
)
2627 if (wantedy
== ZIC_MAX
)
2632 while (wantedy
!= y
) {
2634 i
= len_years
[isleap(y
)];
2638 i
= -len_years
[isleap(y
)];
2640 dayoff
= oadd(dayoff
, i
);
2642 while (m
!= rp
->r_month
) {
2643 i
= len_months
[isleap(y
)][m
];
2644 dayoff
= oadd(dayoff
, i
);
2647 i
= rp
->r_dayofmonth
;
2648 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2649 if (rp
->r_dycode
== DC_DOWLEQ
)
2652 error(_("use of 2/29 in non leap-year"));
2657 dayoff
= oadd(dayoff
, i
);
2658 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2661 #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
2664 ** Don't trust mod of negative numbers.
2667 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2669 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2671 wday
+= LDAYSPERWEEK
;
2673 while (wday
!= rp
->r_wday
)
2674 if (rp
->r_dycode
== DC_DOWGEQ
) {
2675 dayoff
= oadd(dayoff
, (zic_t
) 1);
2676 if (++wday
>= LDAYSPERWEEK
)
2680 dayoff
= oadd(dayoff
, (zic_t
) -1);
2682 wday
= LDAYSPERWEEK
- 1;
2685 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2687 warning(_("rule goes past start/end of month--\
2688 will not work with pre-2004 versions of zic"));
2691 if (dayoff
< min_time
/ SECSPERDAY
)
2693 if (dayoff
> max_time
/ SECSPERDAY
)
2695 t
= (zic_t
) dayoff
* SECSPERDAY
;
2696 return tadd(t
, rp
->r_tod
);
2700 newabbr(const char *const string
)
2704 if (strcmp(string
, GRANDPARENTED
) != 0) {
2709 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2710 ** optionally followed by a + or - and a number from 1 to 14.
2714 while (isascii((unsigned char) *cp
) &&
2715 isalpha((unsigned char) *cp
))
2717 if (cp
- string
== 0)
2718 mp
= _("time zone abbreviation lacks alphabetic at start");
2719 if (noise
&& cp
- string
< 3)
2720 mp
= _("time zone abbreviation has fewer than 3 alphabetics");
2721 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2722 mp
= _("time zone abbreviation has too many alphabetics");
2723 if (mp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2725 if (isascii((unsigned char) *cp
) &&
2726 isdigit((unsigned char) *cp
))
2728 *cp
>= '0' && *cp
<= '4')
2732 mp
= _("time zone abbreviation differs from POSIX standard");
2734 warning("%s (%s)", mp
, string
);
2736 i
= strlen(string
) + 1;
2737 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2738 error(_("too many, or too long, time zone abbreviations"));
2741 (void)strncpy(&chars
[charcnt
], string
, sizeof(chars
) - charcnt
- 1);
2746 mkdirs(char *argname
)
2751 if (argname
== NULL
|| *argname
== '\0')
2753 cp
= name
= ecpyalloc(argname
);
2754 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2756 #ifdef HAVE_DOS_FILE_NAMES
2758 ** DOS drive specifier?
2760 if (isalpha((unsigned char) name
[0]) &&
2761 name
[1] == ':' && name
[2] == '\0') {
2766 if (!itsdir(name
)) {
2768 ** It doesn't seem to exist, so we try to create it.
2769 ** Creation may fail because of the directory being
2770 ** created by some other multiprocessor, so we get
2771 ** to do extra checking.
2773 if (mkdir(name
, MKDIR_UMASK
) != 0) {
2774 const char *e
= strerror(errno
);
2776 if (errno
!= EEXIST
|| !itsdir(name
)) {
2777 (void) fprintf(stderr
,
2778 _("%s: Can't create directory %s: %s\n"),
2792 ** UNIX was a registered trademark of The Open Group in 2003.