2 ** This file is in the public domain, so clarified as of
3 ** 2006-07-17 by Arthur David Olson.
6 ** $FreeBSD: src/usr.sbin/zic/zic.c,v 1.11 1999/08/28 01:21:20 peter Exp $
7 ** $DragonFly: src/usr.sbin/zic/zic.c,v 1.7 2008/10/19 20:15:58 swildner Exp $
12 #include <sys/stat.h> /* for umask manifest constants */
13 #include <sys/types.h>
18 #define ZIC_VERSION '2'
20 typedef int_fast64_t zic_t
;
22 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
23 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
24 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
26 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
29 ** On some ancient hosts, predicates like `isspace(C)' are defined
30 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
31 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
32 ** Neither the C Standard nor Posix require that `isascii' exist.
33 ** For portability, we check both ancient and modern requirements.
34 ** If isascii is not defined, the isascii check succeeds trivially.
41 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
42 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
44 #define end(cp) (strchr((cp), '\0'))
47 const char * r_filename
;
51 int r_loyear
; /* for example, 1986 */
52 int r_hiyear
; /* for example, 1986 */
53 const char * r_yrtype
;
57 int r_month
; /* 0..11 */
59 int r_dycode
; /* see below */
63 long r_tod
; /* time from midnight */
64 int r_todisstd
; /* above is standard time if TRUE */
65 /* or wall clock time if FALSE */
66 int r_todisgmt
; /* above is GMT if TRUE */
67 /* or local time if FALSE */
68 long r_stdoff
; /* offset from standard time */
69 const char * r_abbrvar
; /* variable part of abbreviation */
71 int r_todo
; /* a rule to do (used in outzone) */
72 zic_t r_temp
; /* used in outzone */
76 ** r_dycode r_dayofmonth r_wday
79 #define DC_DOM 0 /* 1..31 */ /* unused */
80 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
81 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
84 const char * z_filename
;
90 const char * z_format
;
94 struct rule
* z_rules
;
97 struct rule z_untilrule
;
101 static void addtt(zic_t starttime
, int type
);
102 static int addtype(long gmtoff
, const char *abbr
, int isdst
,
103 int ttisstd
, int ttisgmt
);
104 static void leapadd(zic_t t
, int positive
, int rolling
, int count
);
105 static void adjleap(void);
106 static void associate(void);
107 static int ciequal(const char *ap
, const char *bp
);
108 static void convert(long val
, char *buf
);
109 static void convert64(zic_t val
, char *buf
);
110 static void dolink(const char *fromfield
, const char *tofield
);
111 static void doabbr(char *abbr
, const char *format
,
112 const char *letters
, int isdst
, int doquotes
);
113 static void eat(const char *name
, int num
);
114 static void eats(const char *name
, int num
,
115 const char *rname
, int rnum
);
116 static long eitol(int i
);
117 static void error(const char *message
);
118 static char ** getfields(char *buf
);
119 static long gethms(const char *string
, const char *errstrng
,
121 static void infile(const char *filename
);
122 static void inleap(char **fields
, int nfields
);
123 static void inlink(char **fields
, int nfields
);
124 static void inrule(char **fields
, int nfields
);
125 static int inzcont(char **fields
, int nfields
);
126 static int inzone(char **fields
, int nfields
);
127 static int inzsub(char **fields
, int nfields
, int iscont
);
128 static int is32(zic_t x
);
129 static int itsabbr(const char *abbr
, const char *word
);
130 static int itsdir(const char *name
);
131 static int lowerit(int c
);
132 static char * memcheck(char *tocheck
);
133 static int mkdirs(char *filename
);
134 static void newabbr(const char *abbr
);
135 static long oadd(long t1
, long t2
);
136 static void outzone(const struct zone
*zp
, int ntzones
);
137 static void puttzcode(long code
, FILE *fp
);
138 static void puttzcode64(zic_t code
, FILE *fp
);
139 static int rcomp(const void *leftp
, const void *rightp
);
140 static zic_t
rpytime(const struct rule
*rp
, int 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 int stringoffset(char *result
, long offset
);
146 static int stringrule(char *result
, const struct rule
*rp
,
147 long dstoff
, long gmtoff
);
148 static void stringzone(char *result
,
149 const struct zone
*zp
, int ntzones
);
150 static void setboundaries(void);
151 static void setgroup(gid_t
*flag
, const char *name
);
152 static void setuser(uid_t
*flag
, const char *name
);
153 static zic_t
tadd(const zic_t t1
, const long t2
);
154 static void usage(void);
155 static void writezone(const char *name
, const char *string
);
156 static int yearistype(int year
, const char *type
);
160 static const char * filename
;
163 static int leapminyear
;
164 static int leapmaxyear
;
166 static int max_abbrvar_len
;
167 static int max_format_len
;
168 static zic_t max_time
;
170 static zic_t min_time
;
173 static const char * rfilename
;
188 ** Which fields are which on a Zone line.
196 #define ZF_TILMONTH 6
199 #define ZONE_MINFIELDS 5
200 #define ZONE_MAXFIELDS 9
203 ** Which fields are which on a Zone continuation line.
209 #define ZFC_TILYEAR 3
210 #define ZFC_TILMONTH 4
212 #define ZFC_TILTIME 6
213 #define ZONEC_MINFIELDS 3
214 #define ZONEC_MAXFIELDS 7
217 ** Which files are which on a Rule line.
229 #define RULE_FIELDS 10
232 ** Which fields are which on a Link line.
237 #define LINK_FIELDS 3
240 ** Which fields are which on a Leap line.
249 #define LEAP_FIELDS 7
259 static struct rule
* rules
;
260 static int nrules
; /* number of rules */
262 static struct zone
* zones
;
263 static int nzones
; /* number of zones */
266 const char * l_filename
;
272 static struct link
* links
;
280 static struct lookup
const *byword(const char *string
,
281 const struct lookup
*lp
);
283 static struct lookup
const line_codes
[] = {
291 static struct lookup
const mon_names
[] = {
292 { "January", TM_JANUARY
},
293 { "February", TM_FEBRUARY
},
294 { "March", TM_MARCH
},
295 { "April", TM_APRIL
},
299 { "August", TM_AUGUST
},
300 { "September", TM_SEPTEMBER
},
301 { "October", TM_OCTOBER
},
302 { "November", TM_NOVEMBER
},
303 { "December", TM_DECEMBER
},
307 static struct lookup
const wday_names
[] = {
308 { "Sunday", TM_SUNDAY
},
309 { "Monday", TM_MONDAY
},
310 { "Tuesday", TM_TUESDAY
},
311 { "Wednesday", TM_WEDNESDAY
},
312 { "Thursday", TM_THURSDAY
},
313 { "Friday", TM_FRIDAY
},
314 { "Saturday", TM_SATURDAY
},
318 static struct lookup
const lasts
[] = {
319 { "last-Sunday", TM_SUNDAY
},
320 { "last-Monday", TM_MONDAY
},
321 { "last-Tuesday", TM_TUESDAY
},
322 { "last-Wednesday", TM_WEDNESDAY
},
323 { "last-Thursday", TM_THURSDAY
},
324 { "last-Friday", TM_FRIDAY
},
325 { "last-Saturday", TM_SATURDAY
},
329 static struct lookup
const begin_years
[] = {
330 { "minimum", YR_MINIMUM
},
331 { "maximum", YR_MAXIMUM
},
335 static struct lookup
const end_years
[] = {
336 { "minimum", YR_MINIMUM
},
337 { "maximum", YR_MAXIMUM
},
342 static struct lookup
const leap_types
[] = {
344 { "Stationary", FALSE
},
348 static const int len_months
[2][MONSPERYEAR
] = {
349 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
350 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
353 static const int len_years
[2] = {
354 DAYSPERNYEAR
, DAYSPERLYEAR
357 static struct attype
{
360 } attypes
[TZ_MAX_TIMES
];
361 static long gmtoffs
[TZ_MAX_TYPES
];
362 static char isdsts
[TZ_MAX_TYPES
];
363 static unsigned char abbrinds
[TZ_MAX_TYPES
];
364 static char ttisstds
[TZ_MAX_TYPES
];
365 static char ttisgmts
[TZ_MAX_TYPES
];
366 static char chars
[TZ_MAX_CHARS
];
367 static zic_t trans
[TZ_MAX_LEAPS
];
368 static long corr
[TZ_MAX_LEAPS
];
369 static char roll
[TZ_MAX_LEAPS
];
372 ** Memory allocation.
376 memcheck(char * const ptr
)
379 errx(EXIT_FAILURE
, _("memory exhausted"));
383 #define emalloc(size) memcheck(imalloc(size))
384 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
385 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
386 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
393 eats(const char * const name
, const int num
,
394 const char * const rname
, const int rnum
)
403 eat(const char * const name
, const int num
)
405 eats(name
, num
, NULL
, -1);
409 error(const char * const string
)
412 ** Match the format of "cc" to allow sh users to
413 ** zic ... 2>&1 | error -t "*" -v
416 fprintf(stderr
, _("\"%s\", line %d: %s"),
417 filename
, linenum
, string
);
418 if (rfilename
!= NULL
)
419 fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
420 rfilename
, rlinenum
);
421 fprintf(stderr
, "\n");
426 warning(const char * const string
)
430 cp
= ecpyalloc(_("warning: "));
431 cp
= ecatalloc(cp
, string
);
440 fprintf(stderr
, "%s\n%s\n",
441 _("usage: zic [-v] [-l localtime] [-p posixrules] [-d directory]"),
442 _(" [-L leapseconds] [-y yearistype] [filename ...]"));
446 static const char * psxrules
;
447 static const char * lcltime
;
448 static const char * directory
;
449 static const char * leapsec
;
450 static const char * yitcommand
;
452 static uid_t uflag
= (uid_t
)-1;
453 static gid_t gflag
= (gid_t
)-1;
454 static mode_t mflag
= (S_IRUSR
| S_IRGRP
| S_IROTH
458 main(int argc
, char *argv
[])
465 umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
466 #endif /* defined unix */
467 while ((c
= getopt(argc
, argv
, "Dd:g:l:m:p:L:u:vsy:")) != -1)
475 if (directory
== NULL
)
479 _("more than one -d option specified"));
482 setgroup(&gflag
, optarg
);
489 _("more than one -l option specified"));
493 void *set
= setmode(optarg
);
498 if (psxrules
== NULL
)
502 _("more than one -p option specified"));
505 setuser(&uflag
, optarg
);
508 if (yitcommand
== NULL
)
512 _("more than one -y option specified"));
519 _("more than one -L option specified"));
525 warnx(_("-s ignored\n"));
528 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
529 usage(); /* usage message by request */
530 if (directory
== NULL
)
532 if (yitcommand
== NULL
)
533 yitcommand
= "yearistype";
537 if (optind
< argc
&& leapsec
!= NULL
) {
542 for (i
= optind
; i
< argc
; ++i
)
547 for (i
= 0; i
< nzones
; i
= j
) {
549 ** Find the next non-continuation zone entry.
551 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
553 outzone(&zones
[i
], j
- i
);
558 for (i
= 0; i
< nlinks
; ++i
) {
559 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
560 dolink(links
[i
].l_from
, links
[i
].l_to
);
562 for (j
= 0; j
< nlinks
; ++j
)
563 if (strcmp(links
[i
].l_to
,
564 links
[j
].l_from
) == 0)
565 warning(_("link to link"));
567 if (lcltime
!= NULL
) {
568 eat("command line", 1);
569 dolink(lcltime
, TZDEFAULT
);
571 if (psxrules
!= NULL
) {
572 eat("command line", 1);
573 dolink(psxrules
, TZDEFRULES
);
575 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
579 dolink(const char * const fromfield
, const char * const tofield
)
584 if (fromfield
[0] == '/')
585 fromname
= ecpyalloc(fromfield
);
587 fromname
= ecpyalloc(directory
);
588 fromname
= ecatalloc(fromname
, "/");
589 fromname
= ecatalloc(fromname
, fromfield
);
591 if (tofield
[0] == '/')
592 toname
= ecpyalloc(tofield
);
594 toname
= ecpyalloc(directory
);
595 toname
= ecatalloc(toname
, "/");
596 toname
= ecatalloc(toname
, tofield
);
599 ** We get to be careful here since
600 ** there's a fair chance of root running us.
604 if (link(fromname
, toname
) != 0) {
607 if (mkdirs(toname
) != 0)
610 result
= link(fromname
, toname
);
612 access(fromname
, F_OK
) == 0 &&
614 const char *s
= tofield
;
615 char * symlinkcontents
= NULL
;
617 while ((s
= strchr(s
+1, '/')) != NULL
)
619 ecatalloc(symlinkcontents
,
622 ecatalloc(symlinkcontents
,
624 result
= symlink(symlinkcontents
,
627 warning(_("hard link failed, symbolic link used"));
628 ifree(symlinkcontents
);
631 err(EXIT_FAILURE
, _("can't link from %s to %s"),
639 #define TIME_T_BITS_IN_FILE 64
647 for (i
= 0; i
< TIME_T_BITS_IN_FILE
- 1; ++i
)
649 max_time
= -(min_time
+ 1);
653 itsdir(const char * const name
)
658 myname
= ecpyalloc(name
);
659 myname
= ecatalloc(myname
, "/.");
660 accres
= access(myname
, F_OK
);
666 ** Associate sets of rules with zones.
670 ** Sort by rule name.
674 rcomp(const void *cp1
, const void *cp2
)
676 return strcmp(((const struct rule
*) cp1
)->r_name
,
677 ((const struct rule
*) cp2
)->r_name
);
689 qsort((void *) rules
, (size_t) nrules
,
690 (size_t) sizeof *rules
, rcomp
);
691 for (i
= 0; i
< nrules
- 1; ++i
) {
692 if (strcmp(rules
[i
].r_name
,
693 rules
[i
+ 1].r_name
) != 0)
695 if (strcmp(rules
[i
].r_filename
,
696 rules
[i
+ 1].r_filename
) == 0)
698 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
699 warning(_("same rule name in multiple files"));
700 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
701 warning(_("same rule name in multiple files"));
702 for (j
= i
+ 2; j
< nrules
; ++j
) {
703 if (strcmp(rules
[i
].r_name
,
704 rules
[j
].r_name
) != 0)
706 if (strcmp(rules
[i
].r_filename
,
707 rules
[j
].r_filename
) == 0)
709 if (strcmp(rules
[i
+ 1].r_filename
,
710 rules
[j
].r_filename
) == 0)
717 for (i
= 0; i
< nzones
; ++i
) {
722 for (base
= 0; base
< nrules
; base
= out
) {
724 for (out
= base
+ 1; out
< nrules
; ++out
)
725 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
727 for (i
= 0; i
< nzones
; ++i
) {
729 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
732 zp
->z_nrules
= out
- base
;
735 for (i
= 0; i
< nzones
; ++i
) {
737 if (zp
->z_nrules
== 0) {
739 ** Maybe we have a local standard time offset.
741 eat(zp
->z_filename
, zp
->z_linenum
);
742 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
745 ** Note, though, that if there's no rule,
746 ** a '%s' in the format is a bad thing.
748 if (strchr(zp
->z_format
, '%') != 0)
749 error(_("%s in ruleless zone"));
757 infile(const char *name
)
762 const struct lookup
*lp
;
768 if (strcmp(name
, "-") == 0) {
769 name
= _("standard input");
771 } else if ((fp
= fopen(name
, "r")) == NULL
)
772 err(EXIT_FAILURE
, _("can't open %s"), name
);
774 for (num
= 1; ; ++num
) {
776 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
778 cp
= strchr(buf
, '\n');
780 error(_("line too long"));
784 fields
= getfields(buf
);
786 while (fields
[nfields
] != NULL
) {
789 if (strcmp(fields
[nfields
], "-") == 0)
790 fields
[nfields
] = &nada
;
795 } else if (wantcont
) {
796 wantcont
= inzcont(fields
, nfields
);
798 lp
= byword(fields
[0], line_codes
);
800 error(_("input line of unknown type"));
801 else switch ((int) (lp
->l_value
)) {
803 inrule(fields
, nfields
);
807 wantcont
= inzone(fields
, nfields
);
810 inlink(fields
, nfields
);
816 _("leap line in non leap seconds file %s"), name
);
817 else inleap(fields
, nfields
);
820 default: /* "cannot happen" */
822 _("panic: invalid l_value %d"), lp
->l_value
);
825 ifree((char *) fields
);
828 errx(EXIT_FAILURE
, _("error reading %s"), filename
);
829 if (fp
!= stdin
&& fclose(fp
))
830 err(EXIT_FAILURE
, _("error closing %s"), filename
);
832 error(_("expected continuation line not found"));
836 ** Convert a string of one of the forms
837 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
838 ** into a number of seconds.
839 ** A null string maps to zero.
840 ** Call error with errstring and return zero on errors.
844 gethms(const char *string
, const char * const errstring
, const int signable
)
849 if (string
== NULL
|| *string
== '\0')
853 else if (*string
== '-') {
857 if (sscanf(string
, scheck(string
, "%ld"), &hh
) == 1)
859 else if (sscanf(string
, scheck(string
, "%ld:%d"), &hh
, &mm
) == 2)
861 else if (sscanf(string
, scheck(string
, "%ld:%d:%d"),
862 &hh
, &mm
, &ss
) != 3) {
867 mm
< 0 || mm
>= MINSPERHOUR
||
868 ss
< 0 || ss
> SECSPERMIN
) {
872 if (LONG_MAX
/ SECSPERHOUR
< hh
) {
873 error(_("time overflow"));
876 if (noise
&& hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)
877 warning(_("24:00 not handled by pre-1998 versions of zic"));
878 if (noise
&& (hh
> HOURSPERDAY
||
879 (hh
== HOURSPERDAY
&& (mm
!= 0 || ss
!= 0))))
880 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
881 return oadd(eitol(sign
) * hh
* eitol(SECSPERHOUR
),
882 eitol(sign
) * (eitol(mm
) * eitol(SECSPERMIN
) + eitol(ss
)));
886 inrule(char ** const fields
, const int nfields
)
888 static struct rule r
;
890 if (nfields
!= RULE_FIELDS
) {
891 error(_("wrong number of fields on Rule line"));
894 if (*fields
[RF_NAME
] == '\0') {
895 error(_("nameless rule"));
898 r
.r_filename
= filename
;
899 r
.r_linenum
= linenum
;
900 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
901 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
902 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
903 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
904 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
905 if (max_abbrvar_len
< strlen(r
.r_abbrvar
))
906 max_abbrvar_len
= strlen(r
.r_abbrvar
);
907 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
908 (int) ((nrules
+ 1) * sizeof *rules
));
913 inzone(char ** const fields
, const int nfields
)
918 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
919 error(_("wrong number of fields on Zone line"));
922 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
923 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
925 _("\"Zone %s\" line and -l option are mutually exclusive"),
930 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
931 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
933 _("\"Zone %s\" line and -p option are mutually exclusive"),
938 for (i
= 0; i
< nzones
; ++i
)
939 if (zones
[i
].z_name
!= NULL
&&
940 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
941 buf
= erealloc(buf
, (int) (132 +
942 strlen(fields
[ZF_NAME
]) +
943 strlen(zones
[i
].z_filename
)));
945 _("duplicate zone name %s (file \"%s\", line %d)"),
952 return inzsub(fields
, nfields
, FALSE
);
956 inzcont(char ** const fields
, const int nfields
)
958 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
959 error(_("wrong number of fields on Zone continuation line"));
962 return inzsub(fields
, nfields
, TRUE
);
966 inzsub(char ** const fields
, const int nfields
, const int iscont
)
969 static struct zone z
;
970 int i_gmtoff
, i_rule
, i_format
;
971 int i_untilyear
, i_untilmonth
;
972 int i_untilday
, i_untiltime
;
976 i_gmtoff
= ZFC_GMTOFF
;
978 i_format
= ZFC_FORMAT
;
979 i_untilyear
= ZFC_TILYEAR
;
980 i_untilmonth
= ZFC_TILMONTH
;
981 i_untilday
= ZFC_TILDAY
;
982 i_untiltime
= ZFC_TILTIME
;
985 i_gmtoff
= ZF_GMTOFF
;
987 i_format
= ZF_FORMAT
;
988 i_untilyear
= ZF_TILYEAR
;
989 i_untilmonth
= ZF_TILMONTH
;
990 i_untilday
= ZF_TILDAY
;
991 i_untiltime
= ZF_TILTIME
;
992 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
994 z
.z_filename
= filename
;
995 z
.z_linenum
= linenum
;
996 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
997 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
998 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
999 error(_("invalid abbreviation format"));
1003 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1004 z
.z_format
= ecpyalloc(fields
[i_format
]);
1005 if (max_format_len
< strlen(z
.z_format
))
1006 max_format_len
= strlen(z
.z_format
);
1007 hasuntil
= nfields
> i_untilyear
;
1009 z
.z_untilrule
.r_filename
= filename
;
1010 z
.z_untilrule
.r_linenum
= linenum
;
1011 rulesub(&z
.z_untilrule
,
1012 fields
[i_untilyear
],
1015 (nfields
> i_untilmonth
) ?
1016 fields
[i_untilmonth
] : "Jan",
1017 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1018 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1019 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1020 z
.z_untilrule
.r_loyear
);
1021 if (iscont
&& nzones
> 0 &&
1022 z
.z_untiltime
> min_time
&&
1023 z
.z_untiltime
< max_time
&&
1024 zones
[nzones
- 1].z_untiltime
> min_time
&&
1025 zones
[nzones
- 1].z_untiltime
< max_time
&&
1026 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1028 "Zone continuation line end time is not after end time of previous line"
1033 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1034 (int) ((nzones
+ 1) * sizeof *zones
));
1035 zones
[nzones
++] = z
;
1037 ** If there was an UNTIL field on this line,
1038 ** there's more information about the zone on the next line.
1044 inleap(char ** const fields
, const int nfields
)
1047 const struct lookup
*lp
;
1049 int year
, month
, day
;
1053 if (nfields
!= LEAP_FIELDS
) {
1054 error(_("wrong number of fields on Leap line"));
1058 cp
= fields
[LP_YEAR
];
1059 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1063 error(_("invalid leaping year"));
1066 if (!leapseen
|| leapmaxyear
< year
)
1068 if (!leapseen
|| leapminyear
> year
)
1074 i
= len_years
[isleap(j
)];
1078 i
= -len_years
[isleap(j
)];
1080 dayoff
= oadd(dayoff
, eitol(i
));
1082 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1083 error(_("invalid month name"));
1086 month
= lp
->l_value
;
1088 while (j
!= month
) {
1089 i
= len_months
[isleap(year
)][j
];
1090 dayoff
= oadd(dayoff
, eitol(i
));
1093 cp
= fields
[LP_DAY
];
1094 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1095 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1096 error(_("invalid day of month"));
1099 dayoff
= oadd(dayoff
, eitol(day
- 1));
1100 if (dayoff
< 0 && !TYPE_SIGNED(zic_t
)) {
1101 error(_("time before zero"));
1104 if (dayoff
< min_time
/ SECSPERDAY
) {
1105 error(_("time too small"));
1108 if (dayoff
> max_time
/ SECSPERDAY
) {
1109 error(_("time too large"));
1112 t
= (zic_t
) dayoff
* SECSPERDAY
;
1113 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1114 cp
= fields
[LP_CORR
];
1119 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1122 } else if (strcmp(cp
, "--") == 0) {
1125 } else if (strcmp(cp
, "+") == 0) {
1128 } else if (strcmp(cp
, "++") == 0) {
1132 error(_("illegal CORRECTION field on Leap line"));
1135 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1137 "illegal Rolling/Stationary field on Leap line"
1141 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1146 inlink(char ** const fields
, const int nfields
)
1150 if (nfields
!= LINK_FIELDS
) {
1151 error(_("wrong number of fields on Link line"));
1154 if (*fields
[LF_FROM
] == '\0') {
1155 error(_("blank FROM field on Link line"));
1158 if (*fields
[LF_TO
] == '\0') {
1159 error(_("blank TO field on Link line"));
1162 l
.l_filename
= filename
;
1163 l
.l_linenum
= linenum
;
1164 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1165 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1166 links
= (struct link
*) (void *) erealloc((char *) links
,
1167 (int) ((nlinks
+ 1) * sizeof *links
));
1168 links
[nlinks
++] = l
;
1172 rulesub(struct rule
* const rp
,
1173 const char * const loyearp
,
1174 const char * const hiyearp
,
1175 const char * const typep
,
1176 const char * const monthp
,
1177 const char * const dayp
,
1178 const char * const timep
)
1180 const struct lookup
*lp
;
1185 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1186 error(_("invalid month name"));
1189 rp
->r_month
= lp
->l_value
;
1190 rp
->r_todisstd
= FALSE
;
1191 rp
->r_todisgmt
= FALSE
;
1192 dp
= ecpyalloc(timep
);
1194 ep
= dp
+ strlen(dp
) - 1;
1195 switch (lowerit(*ep
)) {
1196 case 's': /* Standard */
1197 rp
->r_todisstd
= TRUE
;
1198 rp
->r_todisgmt
= FALSE
;
1201 case 'w': /* Wall */
1202 rp
->r_todisstd
= FALSE
;
1203 rp
->r_todisgmt
= FALSE
;
1206 case 'g': /* Greenwich */
1207 case 'u': /* Universal */
1208 case 'z': /* Zulu */
1209 rp
->r_todisstd
= TRUE
;
1210 rp
->r_todisgmt
= TRUE
;
1215 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1221 lp
= byword(cp
, begin_years
);
1222 rp
->r_lowasnum
= lp
== NULL
;
1223 if (!rp
->r_lowasnum
) switch ((int) lp
->l_value
) {
1225 rp
->r_loyear
= INT_MIN
;
1228 rp
->r_loyear
= INT_MAX
;
1230 default: /* "cannot happen" */
1232 _("panic: invalid l_value %d"), lp
->l_value
);
1233 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1234 error(_("invalid starting year"));
1238 lp
= byword(cp
, end_years
);
1239 rp
->r_hiwasnum
= lp
== NULL
;
1240 if (!rp
->r_hiwasnum
) switch ((int) lp
->l_value
) {
1242 rp
->r_hiyear
= INT_MIN
;
1245 rp
->r_hiyear
= INT_MAX
;
1248 rp
->r_hiyear
= rp
->r_loyear
;
1250 default: /* "cannot happen" */
1252 _("panic: invalid l_value %d"), lp
->l_value
);
1253 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1254 error(_("invalid ending year"));
1257 if (rp
->r_loyear
> rp
->r_hiyear
) {
1258 error(_("starting year greater than ending year"));
1262 rp
->r_yrtype
= NULL
;
1264 if (rp
->r_loyear
== rp
->r_hiyear
) {
1265 error(_("typed single year"));
1268 rp
->r_yrtype
= ecpyalloc(typep
);
1272 ** Accept things such as:
1278 dp
= ecpyalloc(dayp
);
1279 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1280 rp
->r_dycode
= DC_DOWLEQ
;
1281 rp
->r_wday
= lp
->l_value
;
1282 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1284 if ((ep
= strchr(dp
, '<')) != 0)
1285 rp
->r_dycode
= DC_DOWLEQ
;
1286 else if ((ep
= strchr(dp
, '>')) != 0)
1287 rp
->r_dycode
= DC_DOWGEQ
;
1290 rp
->r_dycode
= DC_DOM
;
1292 if (rp
->r_dycode
!= DC_DOM
) {
1295 error(_("invalid day of month"));
1299 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1300 error(_("invalid weekday name"));
1304 rp
->r_wday
= lp
->l_value
;
1306 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1307 rp
->r_dayofmonth
<= 0 ||
1308 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1309 error(_("invalid day of month"));
1318 convert(const long val
, char * const buf
)
1323 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1324 buf
[i
] = val
>> shift
;
1328 convert64(const zic_t val
, char * const buf
)
1333 for (i
= 0, shift
= 56; i
< 8; ++i
, shift
-= 8)
1334 buf
[i
] = val
>> shift
;
1338 puttzcode(const long val
, FILE * const fp
)
1343 fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1347 puttzcode64(const zic_t val
, FILE * const fp
)
1351 convert64(val
, buf
);
1352 fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1356 atcomp(const void * avp
, const void * bvp
)
1358 const zic_t a
= ((const struct attype
*) avp
)->at
;
1359 const zic_t b
= ((const struct attype
*) bvp
)->at
;
1361 return (a
< b
) ? -1 : (a
> b
);
1367 return INT32_MIN
<= x
&& x
<= INT32_MAX
;
1371 writezone(const char * const name
, const char * const string
)
1375 int leapcnt32
, leapi32
;
1376 int timecnt32
, timei32
;
1378 static char * fullname
;
1379 static const struct tzhead tzh0
;
1380 static struct tzhead tzh
;
1381 zic_t ats
[TZ_MAX_TIMES
];
1382 unsigned char types
[TZ_MAX_TIMES
];
1388 qsort((void *) attypes
, (size_t) timecnt
,
1389 (size_t) sizeof *attypes
, atcomp
);
1399 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1402 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1403 ++fromi
; /* handled by default rule */
1404 for ( ; fromi
< timecnt
; ++fromi
) {
1405 if (toi
!= 0 && ((attypes
[fromi
].at
+
1406 gmtoffs
[attypes
[toi
- 1].type
]) <=
1407 (attypes
[toi
- 1].at
+ gmtoffs
[toi
== 1 ? 0
1408 : attypes
[toi
- 2].type
]))) {
1409 attypes
[toi
- 1].type
=
1410 attypes
[fromi
].type
;
1414 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1415 attypes
[toi
++] = attypes
[fromi
];
1422 for (i
= 0; i
< timecnt
; ++i
) {
1423 ats
[i
] = attypes
[i
].at
;
1424 types
[i
] = attypes
[i
].type
;
1427 ** Correct for leap seconds.
1429 for (i
= 0; i
< timecnt
; ++i
) {
1432 if (ats
[i
] > trans
[j
] - corr
[j
]) {
1433 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1438 ** Figure out 32-bit-limited starts and counts.
1440 timecnt32
= timecnt
;
1442 leapcnt32
= leapcnt
;
1444 while (timecnt32
> 0 && !is32(ats
[timecnt32
- 1]))
1446 while (timecnt32
> 0 && !is32(ats
[timei32
])) {
1450 while (leapcnt32
> 0 && !is32(trans
[leapcnt32
- 1]))
1452 while (leapcnt32
> 0 && !is32(trans
[leapi32
])) {
1456 fullname
= erealloc(fullname
,
1457 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1458 sprintf(fullname
, "%s/%s", directory
, name
);
1460 ** Remove old file, if any, to snap links.
1462 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
)
1463 err(EXIT_FAILURE
, _("can't remove %s"), fullname
);
1464 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1465 if (mkdirs(fullname
) != 0)
1467 if ((fp
= fopen(fullname
, "wb")) == NULL
)
1468 err(EXIT_FAILURE
, _("can't create %s"), fullname
);
1470 for (pass
= 1; pass
<= 2; ++pass
) {
1471 int thistimei
, thistimecnt
;
1472 int thisleapi
, thisleapcnt
;
1473 int thistimelim
, thisleaplim
;
1474 int writetype
[TZ_MAX_TIMES
];
1475 int typemap
[TZ_MAX_TYPES
];
1477 char thischars
[TZ_MAX_CHARS
];
1479 int indmap
[TZ_MAX_CHARS
];
1482 thistimei
= timei32
;
1483 thistimecnt
= timecnt32
;
1484 thisleapi
= leapi32
;
1485 thisleapcnt
= leapcnt32
;
1488 thistimecnt
= timecnt
;
1490 thisleapcnt
= leapcnt
;
1492 thistimelim
= thistimei
+ thistimecnt
;
1493 thisleaplim
= thisleapi
+ thisleapcnt
;
1494 for (i
= 0; i
< typecnt
; ++i
)
1495 writetype
[i
] = thistimecnt
== timecnt
;
1496 if (thistimecnt
== 0) {
1498 ** No transition times fall in the current
1499 ** (32- or 64-bit) window.
1502 writetype
[typecnt
- 1] = TRUE
;
1504 for (i
= thistimei
- 1; i
< thistimelim
; ++i
)
1506 writetype
[types
[i
]] = TRUE
;
1508 ** For America/Godthab and Antarctica/Palmer
1511 writetype
[0] = TRUE
;
1514 for (i
= 0; i
< typecnt
; ++i
)
1515 typemap
[i
] = writetype
[i
] ? thistypecnt
++ : -1;
1516 for (i
= 0; i
< sizeof indmap
/ sizeof indmap
[0]; ++i
)
1519 for (i
= 0; i
< typecnt
; ++i
) {
1524 if (indmap
[abbrinds
[i
]] >= 0)
1526 thisabbr
= &chars
[abbrinds
[i
]];
1527 for (j
= 0; j
< thischarcnt
; ++j
)
1528 if (strcmp(&thischars
[j
], thisabbr
) == 0)
1530 if (j
== thischarcnt
) {
1531 strcpy(&thischars
[(int) thischarcnt
],
1533 thischarcnt
+= strlen(thisabbr
) + 1;
1535 indmap
[abbrinds
[i
]] = j
;
1537 #define DO(field) fwrite((void *) tzh.field, \
1538 (size_t) sizeof tzh.field, (size_t) 1, fp)
1540 strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1541 tzh
.tzh_version
[0] = ZIC_VERSION
;
1542 convert(eitol(thistypecnt
), tzh
.tzh_ttisgmtcnt
);
1543 convert(eitol(thistypecnt
), tzh
.tzh_ttisstdcnt
);
1544 convert(eitol(thisleapcnt
), tzh
.tzh_leapcnt
);
1545 convert(eitol(thistimecnt
), tzh
.tzh_timecnt
);
1546 convert(eitol(thistypecnt
), tzh
.tzh_typecnt
);
1547 convert(eitol(thischarcnt
), tzh
.tzh_charcnt
);
1558 for (i
= thistimei
; i
< thistimelim
; ++i
)
1560 puttzcode((long) ats
[i
], fp
);
1561 else puttzcode64(ats
[i
], fp
);
1562 for (i
= thistimei
; i
< thistimelim
; ++i
) {
1565 uc
= typemap
[types
[i
]];
1566 fwrite((void *) &uc
,
1571 for (i
= 0; i
< typecnt
; ++i
)
1573 puttzcode(gmtoffs
[i
], fp
);
1574 putc(isdsts
[i
], fp
);
1575 putc((unsigned char) indmap
[abbrinds
[i
]], fp
);
1577 if (thischarcnt
!= 0)
1578 fwrite((void *) thischars
,
1579 (size_t) sizeof thischars
[0],
1580 (size_t) thischarcnt
, fp
);
1581 for (i
= thisleapi
; i
< thisleaplim
; ++i
) {
1582 register zic_t todo
;
1585 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1588 if (++j
>= typecnt
) {
1594 while (j
< timecnt
&&
1599 todo
= tadd(trans
[i
], -gmtoffs
[j
]);
1600 } else todo
= trans
[i
];
1602 puttzcode((long) todo
, fp
);
1603 else puttzcode64(todo
, fp
);
1604 puttzcode(corr
[i
], fp
);
1606 for (i
= 0; i
< typecnt
; ++i
)
1608 putc(ttisstds
[i
], fp
);
1609 for (i
= 0; i
< typecnt
; ++i
)
1611 putc(ttisgmts
[i
], fp
);
1613 fprintf(fp
, "\n%s\n", string
);
1614 if (ferror(fp
) || fclose(fp
))
1615 errx(EXIT_FAILURE
, _("error writing %s"), fullname
);
1616 if (chmod(fullname
, mflag
) < 0)
1617 err(EXIT_FAILURE
, _("cannot change mode of %s to %03o"),
1618 fullname
, (unsigned)mflag
);
1619 if ((uflag
!= (uid_t
)-1 || gflag
!= (gid_t
)-1)
1620 && chown(fullname
, uflag
, gflag
) < 0)
1621 err(EXIT_FAILURE
, _("cannot change ownership of %s"),
1626 doabbr(char * const abbr
, const char * const format
,
1627 const char * const letters
, const int isdst
, const int doquotes
)
1633 slashp
= strchr(format
, '/');
1634 if (slashp
== NULL
) {
1635 if (letters
== NULL
)
1636 strcpy(abbr
, format
);
1637 else sprintf(abbr
, format
, letters
);
1639 strcpy(abbr
, slashp
+ 1);
1641 if (slashp
> format
)
1642 strncpy(abbr
, format
,
1643 (unsigned) (slashp
- format
));
1644 abbr
[slashp
- format
] = '\0';
1648 for (cp
= abbr
; *cp
!= '\0'; ++cp
)
1649 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp
) == NULL
&&
1650 strchr("abcdefghijklmnopqrstuvwxyz", *cp
) == NULL
)
1653 if (len
> 0 && *cp
== '\0')
1655 abbr
[len
+ 2] = '\0';
1656 abbr
[len
+ 1] = '>';
1657 for ( ; len
> 0; --len
)
1658 abbr
[len
] = abbr
[len
- 1];
1663 updateminmax(const int x
)
1672 stringoffset(char *result
, long offset
)
1680 strcpy(result
, "-");
1683 seconds
= offset
% SECSPERMIN
;
1684 offset
/= SECSPERMIN
;
1685 minutes
= offset
% MINSPERHOUR
;
1686 offset
/= MINSPERHOUR
;
1688 if (hours
>= HOURSPERDAY
) {
1692 sprintf(end(result
), "%d", hours
);
1693 if (minutes
!= 0 || seconds
!= 0) {
1694 sprintf(end(result
), ":%02d", minutes
);
1696 sprintf(end(result
), ":%02d", seconds
);
1702 stringrule(char *result
, const struct rule
* const rp
, const long dstoff
,
1707 result
= end(result
);
1708 if (rp
->r_dycode
== DC_DOM
) {
1711 if (rp
->r_dayofmonth
== 29 && rp
->r_month
== TM_FEBRUARY
)
1714 for (month
= 0; month
< rp
->r_month
; ++month
)
1715 total
+= len_months
[0][month
];
1716 sprintf(result
, "J%d", total
+ rp
->r_dayofmonth
);
1720 if (rp
->r_dycode
== DC_DOWGEQ
) {
1721 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1722 if ((week
- 1) * DAYSPERWEEK
+ 1 != rp
->r_dayofmonth
)
1724 } else if (rp
->r_dycode
== DC_DOWLEQ
) {
1725 if (rp
->r_dayofmonth
== len_months
[1][rp
->r_month
])
1728 week
= 1 + rp
->r_dayofmonth
/ DAYSPERWEEK
;
1729 if (week
* DAYSPERWEEK
- 1 != rp
->r_dayofmonth
)
1732 } else return -1; /* "cannot happen" */
1733 sprintf(result
, "M%d.%d.%d",
1734 rp
->r_month
+ 1, week
, rp
->r_wday
);
1739 if (rp
->r_todisstd
&& rp
->r_stdoff
== 0)
1745 if (tod
!= 2 * SECSPERMIN
* MINSPERHOUR
) {
1746 strcat(result
, "/");
1747 if (stringoffset(end(result
), tod
) != 0)
1754 stringzone(char *result
, const struct zone
* const zpfirst
,
1755 const int zonecount
)
1757 const struct zone
* zp
;
1759 struct rule
* stdrp
;
1760 struct rule
* dstrp
;
1762 const char * abbrvar
;
1765 zp
= zpfirst
+ zonecount
- 1;
1766 stdrp
= dstrp
= NULL
;
1767 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1768 rp
= &zp
->z_rules
[i
];
1769 if (rp
->r_hiwasnum
|| rp
->r_hiyear
!= INT_MAX
)
1771 if (rp
->r_yrtype
!= NULL
)
1773 if (rp
->r_stdoff
== 0) {
1783 if (stdrp
== NULL
&& dstrp
== NULL
) {
1785 ** There are no rules running through "max".
1786 ** Let's find the latest rule.
1788 for (i
= 0; i
< zp
->z_nrules
; ++i
) {
1789 rp
= &zp
->z_rules
[i
];
1790 if (stdrp
== NULL
|| rp
->r_hiyear
> stdrp
->r_hiyear
||
1791 (rp
->r_hiyear
== stdrp
->r_hiyear
&&
1792 rp
->r_month
> stdrp
->r_month
))
1795 if (stdrp
!= NULL
&& stdrp
->r_stdoff
!= 0)
1796 return; /* We end up in DST (a POSIX no-no). */
1798 ** Horrid special case: if year is 2037,
1799 ** presume this is a zone handled on a year-by-year basis;
1800 ** do not try to apply a rule to the zone.
1802 if (stdrp
!= NULL
&& stdrp
->r_hiyear
== 2037)
1805 if (stdrp
== NULL
&& (zp
->z_nrules
!= 0 || zp
->z_stdoff
!= 0))
1807 abbrvar
= (stdrp
== NULL
) ? "" : stdrp
->r_abbrvar
;
1808 doabbr(result
, zp
->z_format
, abbrvar
, FALSE
, TRUE
);
1809 if (stringoffset(end(result
), -zp
->z_gmtoff
) != 0) {
1815 doabbr(end(result
), zp
->z_format
, dstrp
->r_abbrvar
, TRUE
, TRUE
);
1816 if (dstrp
->r_stdoff
!= SECSPERMIN
* MINSPERHOUR
)
1817 if (stringoffset(end(result
),
1818 -(zp
->z_gmtoff
+ dstrp
->r_stdoff
)) != 0) {
1822 strcat(result
, ",");
1823 if (stringrule(result
, dstrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1827 strcat(result
, ",");
1828 if (stringrule(result
, stdrp
, dstrp
->r_stdoff
, zp
->z_gmtoff
) != 0) {
1835 outzone(const struct zone
* const zpfirst
, const int zonecount
)
1837 const struct zone
*zp
;
1840 int usestart
, useuntil
;
1841 zic_t starttime
, untiltime
;
1855 max_abbr_len
= 2 + max_format_len
+ max_abbrvar_len
;
1856 max_envvar_len
= 2 * max_abbr_len
+ 5 * 9;
1857 startbuf
= emalloc(max_abbr_len
+ 1);
1858 ab
= emalloc(max_abbr_len
+ 1);
1859 envvar
= emalloc(max_envvar_len
+ 1);
1860 INITIALIZE(untiltime
);
1861 INITIALIZE(starttime
);
1863 ** Now. . .finally. . .generate some useful data!
1869 ** Thanks to Earl Chew
1870 ** for noting the need to unconditionally initialize startttisstd.
1872 startttisstd
= FALSE
;
1873 startttisgmt
= FALSE
;
1874 min_year
= max_year
= EPOCH_YEAR
;
1876 updateminmax(leapminyear
);
1877 updateminmax(leapmaxyear
+ (leapmaxyear
< INT_MAX
));
1879 for (i
= 0; i
< zonecount
; ++i
) {
1881 if (i
< zonecount
- 1)
1882 updateminmax(zp
->z_untilrule
.r_loyear
);
1883 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1884 rp
= &zp
->z_rules
[j
];
1886 updateminmax(rp
->r_loyear
);
1888 updateminmax(rp
->r_hiyear
);
1892 ** Generate lots of data if a rule can't cover all future times.
1894 stringzone(envvar
, zpfirst
, zonecount
);
1895 if (noise
&& envvar
[0] == '\0') {
1898 wp
= ecpyalloc(_("no POSIX environment variable for zone"));
1899 wp
= ecatalloc(wp
, " ");
1900 wp
= ecatalloc(wp
, zpfirst
->z_name
);
1904 if (envvar
[0] == '\0') {
1905 if (min_year
>= INT_MIN
+ YEARSPERREPEAT
)
1906 min_year
-= YEARSPERREPEAT
;
1907 else min_year
= INT_MIN
;
1908 if (max_year
<= INT_MAX
- YEARSPERREPEAT
)
1909 max_year
+= YEARSPERREPEAT
;
1910 else max_year
= INT_MAX
;
1913 ** For the benefit of older systems,
1914 ** generate data from 1900 through 2037.
1916 if (min_year
> 1900)
1918 if (max_year
< 2037)
1920 for (i
= 0; i
< zonecount
; ++i
) {
1922 ** A guess that may well be corrected later.
1926 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1927 useuntil
= i
< (zonecount
- 1);
1928 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1930 gmtoff
= zp
->z_gmtoff
;
1931 eat(zp
->z_filename
, zp
->z_linenum
);
1933 startoff
= zp
->z_gmtoff
;
1934 if (zp
->z_nrules
== 0) {
1935 stdoff
= zp
->z_stdoff
;
1936 doabbr(startbuf
, zp
->z_format
,
1937 NULL
, stdoff
!= 0, FALSE
);
1938 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1939 startbuf
, stdoff
!= 0, startttisstd
,
1942 addtt(starttime
, type
);
1944 } else if (stdoff
!= 0)
1945 addtt(min_time
, type
);
1946 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1947 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1950 ** Mark which rules to do in the current year.
1951 ** For those to do, calculate rpytime(rp, year);
1953 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1954 rp
= &zp
->z_rules
[j
];
1955 eats(zp
->z_filename
, zp
->z_linenum
,
1956 rp
->r_filename
, rp
->r_linenum
);
1957 rp
->r_todo
= year
>= rp
->r_loyear
&&
1958 year
<= rp
->r_hiyear
&&
1959 yearistype(year
, rp
->r_yrtype
);
1961 rp
->r_temp
= rpytime(rp
, year
);
1971 ** Turn untiltime into UTC
1972 ** assuming the current gmtoff and
1975 untiltime
= zp
->z_untiltime
;
1976 if (!zp
->z_untilrule
.r_todisgmt
)
1977 untiltime
= tadd(untiltime
,
1979 if (!zp
->z_untilrule
.r_todisstd
)
1980 untiltime
= tadd(untiltime
,
1984 ** Find the rule (of those to do, if any)
1985 ** that takes effect earliest in the year.
1988 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1989 rp
= &zp
->z_rules
[j
];
1992 eats(zp
->z_filename
, zp
->z_linenum
,
1993 rp
->r_filename
, rp
->r_linenum
);
1994 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1995 if (!rp
->r_todisstd
)
1996 offset
= oadd(offset
, stdoff
);
1998 if (jtime
== min_time
||
2001 jtime
= tadd(jtime
, -offset
);
2002 if (k
< 0 || jtime
< ktime
) {
2008 break; /* go on to next year */
2009 rp
= &zp
->z_rules
[k
];
2011 if (useuntil
&& ktime
>= untiltime
)
2013 stdoff
= rp
->r_stdoff
;
2014 if (usestart
&& ktime
== starttime
)
2017 if (ktime
< starttime
) {
2018 startoff
= oadd(zp
->z_gmtoff
,
2020 doabbr(startbuf
, zp
->z_format
,
2026 if (*startbuf
== '\0' &&
2027 startoff
== oadd(zp
->z_gmtoff
,
2037 eats(zp
->z_filename
, zp
->z_linenum
,
2038 rp
->r_filename
, rp
->r_linenum
);
2039 doabbr(ab
, zp
->z_format
, rp
->r_abbrvar
,
2040 rp
->r_stdoff
!= 0, FALSE
);
2041 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
2042 type
= addtype(offset
, ab
, rp
->r_stdoff
!= 0,
2043 rp
->r_todisstd
, rp
->r_todisgmt
);
2048 if (*startbuf
== '\0' &&
2049 zp
->z_format
!= NULL
&&
2050 strchr(zp
->z_format
, '%') == NULL
&&
2051 strchr(zp
->z_format
, '/') == NULL
)
2052 strcpy(startbuf
, zp
->z_format
);
2053 eat(zp
->z_filename
, zp
->z_linenum
);
2054 if (*startbuf
== '\0')
2055 error(_("can't determine time zone abbreviation to use just after until time"));
2056 else addtt(starttime
,
2057 addtype(startoff
, startbuf
,
2058 startoff
!= zp
->z_gmtoff
,
2063 ** Now we may get to set starttime for the next zone line.
2066 startttisstd
= zp
->z_untilrule
.r_todisstd
;
2067 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
2068 starttime
= zp
->z_untiltime
;
2070 starttime
= tadd(starttime
, -stdoff
);
2072 starttime
= tadd(starttime
, -gmtoff
);
2075 writezone(zpfirst
->z_name
, envvar
);
2082 addtt(const zic_t starttime
, int type
)
2084 if (starttime
<= min_time
||
2085 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
2086 gmtoffs
[0] = gmtoffs
[type
];
2087 isdsts
[0] = isdsts
[type
];
2088 ttisstds
[0] = ttisstds
[type
];
2089 ttisgmts
[0] = ttisgmts
[type
];
2090 if (abbrinds
[type
] != 0)
2091 strcpy(chars
, &chars
[abbrinds
[type
]]);
2093 charcnt
= strlen(chars
) + 1;
2098 if (timecnt
>= TZ_MAX_TIMES
) {
2099 error(_("too many transitions?!"));
2102 attypes
[timecnt
].at
= starttime
;
2103 attypes
[timecnt
].type
= type
;
2108 addtype(const long gmtoff
, const char * const abbr
, const int isdst
,
2109 const int ttisstd
, const int ttisgmt
)
2113 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
2114 error(_("internal error - addtype called with bad isdst"));
2117 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
2118 error(_("internal error - addtype called with bad ttisstd"));
2121 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
2122 error(_("internal error - addtype called with bad ttisgmt"));
2126 ** See if there's already an entry for this zone type.
2127 ** If so, just return its index.
2129 for (i
= 0; i
< typecnt
; ++i
) {
2130 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
2131 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
2132 ttisstd
== ttisstds
[i
] &&
2133 ttisgmt
== ttisgmts
[i
])
2137 ** There isn't one; add a new one, unless there are already too
2140 if (typecnt
>= TZ_MAX_TYPES
) {
2141 error(_("too many local time types"));
2144 if (! (-1L - 2147483647L <= gmtoff
&& gmtoff
<= 2147483647L)) {
2145 error(_("UTC offset out of range"));
2148 gmtoffs
[i
] = gmtoff
;
2150 ttisstds
[i
] = ttisstd
;
2151 ttisgmts
[i
] = ttisgmt
;
2153 for (j
= 0; j
< charcnt
; ++j
)
2154 if (strcmp(&chars
[j
], abbr
) == 0)
2164 leapadd(const zic_t t
, const int positive
, const int rolling
, int count
)
2168 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
2169 error(_("too many leap seconds"));
2172 for (i
= 0; i
< leapcnt
; ++i
)
2173 if (t
<= trans
[i
]) {
2174 if (t
== trans
[i
]) {
2175 error(_("repeated leap second moment"));
2181 for (j
= leapcnt
; j
> i
; --j
) {
2182 trans
[j
] = trans
[j
- 1];
2183 corr
[j
] = corr
[j
- 1];
2184 roll
[j
] = roll
[j
- 1];
2187 corr
[i
] = positive
? 1L : eitol(-count
);
2190 } while (positive
&& --count
!= 0);
2200 ** propagate leap seconds forward
2202 for (i
= 0; i
< leapcnt
; ++i
) {
2203 trans
[i
] = tadd(trans
[i
], last
);
2204 last
= corr
[i
] += last
;
2209 yearistype(const int year
, const char * const type
)
2214 if (type
== NULL
|| *type
== '\0')
2216 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
2217 sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
2218 result
= system(buf
);
2219 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
2225 error(_("Wild result from command execution"));
2226 warnx(_("command was '%s', result was %d"), buf
, result
);
2234 a
= (unsigned char) a
;
2235 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
2239 ciequal(const char *ap
, const char *bp
) /* case-insensitive equality */
2241 while (lowerit(*ap
) == lowerit(*bp
++))
2248 itsabbr(const char *abbr
, const char *word
)
2250 if (lowerit(*abbr
) != lowerit(*word
))
2253 while (*++abbr
!= '\0')
2257 } while (lowerit(*word
++) != lowerit(*abbr
));
2261 static const struct lookup
*
2262 byword(const char * const word
, const struct lookup
* const table
)
2264 const struct lookup
*foundlp
;
2265 const struct lookup
*lp
;
2267 if (word
== NULL
|| table
== NULL
)
2270 ** Look for exact match.
2272 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2273 if (ciequal(word
, lp
->l_word
))
2276 ** Look for inexact match.
2279 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
2280 if (itsabbr(word
, lp
->l_word
)) {
2281 if (foundlp
== NULL
)
2283 else return NULL
; /* multiple inexact matches */
2297 array
= (char **) (void *)
2298 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
2301 while (isascii((unsigned char) *cp
) &&
2302 isspace((unsigned char) *cp
))
2304 if (*cp
== '\0' || *cp
== '#')
2306 array
[nsubs
++] = dp
= cp
;
2308 if ((*dp
= *cp
++) != '"')
2310 else while ((*dp
= *cp
++) != '"')
2315 "Odd number of quotation marks"
2319 } while (*cp
!= '\0' && *cp
!= '#' &&
2320 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
2321 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2325 array
[nsubs
] = NULL
;
2330 oadd(const long t1
, const long t2
)
2335 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2336 error(_("time overflow"));
2343 tadd(const zic_t t1
, const long t2
)
2347 if (t1
== max_time
&& t2
> 0)
2349 if (t1
== min_time
&& t2
< 0)
2352 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2353 error(_("time overflow"));
2360 ** Given a rule, and a year, compute the date - in seconds since January 1,
2361 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2365 rpytime(const struct rule
* const rp
, const int wantedy
)
2368 long dayoff
; /* with a nod to Margaret O. */
2371 if (wantedy
== INT_MIN
)
2373 if (wantedy
== INT_MAX
)
2378 while (wantedy
!= y
) {
2380 i
= len_years
[isleap(y
)];
2384 i
= -len_years
[isleap(y
)];
2386 dayoff
= oadd(dayoff
, eitol(i
));
2388 while (m
!= rp
->r_month
) {
2389 i
= len_months
[isleap(y
)][m
];
2390 dayoff
= oadd(dayoff
, eitol(i
));
2393 i
= rp
->r_dayofmonth
;
2394 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2395 if (rp
->r_dycode
== DC_DOWLEQ
)
2398 error(_("use of 2/29 in non leap-year"));
2403 dayoff
= oadd(dayoff
, eitol(i
));
2404 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2407 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2408 wday
= eitol(EPOCH_WDAY
);
2410 ** Don't trust mod of negative numbers.
2413 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2415 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2417 wday
+= LDAYSPERWEEK
;
2419 while (wday
!= eitol(rp
->r_wday
))
2420 if (rp
->r_dycode
== DC_DOWGEQ
) {
2421 dayoff
= oadd(dayoff
, (long) 1);
2422 if (++wday
>= LDAYSPERWEEK
)
2426 dayoff
= oadd(dayoff
, (long) -1);
2428 wday
= LDAYSPERWEEK
- 1;
2431 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2433 warning(_("rule goes past start/end of month--\
2434 will not work with pre-2004 versions of zic"));
2437 if (dayoff
< min_time
/ SECSPERDAY
)
2439 if (dayoff
> max_time
/ SECSPERDAY
)
2441 t
= (zic_t
) dayoff
* SECSPERDAY
;
2442 return tadd(t
, rp
->r_tod
);
2446 newabbr(const char * const string
)
2450 if (strcmp(string
, GRANDPARENTED
) != 0) {
2455 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2456 ** optionally followed by a + or - and a number from 1 to 14.
2460 while (isascii((unsigned char) *cp
) &&
2461 isalpha((unsigned char) *cp
))
2463 if (cp
- string
== 0)
2464 wp
= _("time zone abbreviation lacks alphabetic at start");
2465 if (noise
&& cp
- string
> 3)
2466 wp
= _("time zone abbreviation has more than 3 alphabetics");
2467 if (cp
- string
> ZIC_MAX_ABBR_LEN_WO_WARN
)
2468 wp
= _("time zone abbreviation has too many alphabetics");
2469 if (wp
== NULL
&& (*cp
== '+' || *cp
== '-')) {
2471 if (isascii((unsigned char) *cp
) &&
2472 isdigit((unsigned char) *cp
))
2474 *cp
>= '0' && *cp
<= '4')
2478 wp
= _("time zone abbreviation differs from POSIX standard");
2481 wp
= ecatalloc(wp
, " (");
2482 wp
= ecatalloc(wp
, string
);
2483 wp
= ecatalloc(wp
, ")");
2488 i
= strlen(string
) + 1;
2489 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2490 error(_("too many, or too long, time zone abbreviations"));
2493 strcpy(&chars
[charcnt
], string
);
2494 charcnt
+= eitol(i
);
2498 mkdirs(char *argname
)
2503 if (argname
== NULL
|| *argname
== '\0' || Dflag
)
2505 cp
= name
= ecpyalloc(argname
);
2506 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2510 ** DOS drive specifier?
2512 if (isalpha((unsigned char) name
[0]) &&
2513 name
[1] == ':' && name
[2] == '\0') {
2517 #endif /* !defined unix */
2518 if (!itsdir(name
)) {
2520 ** It doesn't seem to exist, so we try to create it.
2521 ** Creation may fail because of the directory being
2522 ** created by some other multiprocessor, so we get
2523 ** to do extra checking.
2525 if ((mkdir(name
, MKDIR_UMASK
) != 0) &&
2526 (errno
!= EEXIST
|| !itsdir(name
))) {
2527 warn(_("can't create directory %s"), name
);
2544 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0))
2545 errx(EXIT_FAILURE
, _("%d did not sign extend correctly"), i
);
2553 setgroup(gid_t
*flag
, const char *name
)
2557 if (*flag
!= (gid_t
)-1)
2558 errx(EXIT_FAILURE
, _("multiple -g flags specified"));
2560 gr
= getgrnam(name
);
2565 ul
= strtoul(name
, &ep
, 10);
2566 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2570 errx(EXIT_FAILURE
, _("group `%s' not found"), name
);
2576 setuser(uid_t
*flag
, const char *name
)
2580 if (*flag
!= (gid_t
)-1)
2581 errx(EXIT_FAILURE
, _("multiple -u flags specified"));
2583 pw
= getpwnam(name
);
2588 ul
= strtoul(name
, &ep
, 10);
2589 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2593 errx(EXIT_FAILURE
, _("user `%s' not found"), name
);
2599 ** UNIX was a registered trademark of The Open Group in 2003.