2
* This file is in the public domain, so clarified as of
3
* 2006-07-17 by Arthur David Olson.
9
#include "postgres_fe.h"
24
#define ZIC_VERSION '2'
28
#ifndef ZIC_MAX_ABBR_LEN_WO_WARN
29
#define ZIC_MAX_ABBR_LEN_WO_WARN 6
30
#endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
32
#ifdef HAVE_SYS_STAT_H
38
#define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
40
#define MKDIR_UMASK 0755
44
static char elsieid[] = "@(#)zic.c 8.17";
47
* On some ancient hosts, predicates like `isspace(C)' are defined
48
* only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
49
* which says they are defined only if C == ((unsigned char) C) || C == EOF.
50
* Neither the C Standard nor Posix require that `isascii' exist.
51
* For portability, we check both ancient and modern requirements.
52
* If isascii is not defined, the isascii check succeeds trivially.
59
#define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
60
#define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
62
#define end(cp) (strchr((cp), '\0'))
66
const char *r_filename;
70
int r_loyear; /* for example, 1986 */
71
int r_hiyear; /* for example, 1986 */
76
int r_month; /* 0..11 */
78
int r_dycode; /* see below */
82
long r_tod; /* time from midnight */
83
int r_todisstd; /* above is standard time if TRUE */
84
/* or wall clock time if FALSE */
85
int r_todisgmt; /* above is GMT if TRUE */
86
/* or local time if FALSE */
87
long r_stdoff; /* offset from standard time */
88
const char *r_abbrvar; /* variable part of abbreviation */
90
int r_todo; /* a rule to do (used in outzone) */
91
zic_t r_temp; /* used in outzone */
95
* r_dycode r_dayofmonth r_wday
98
#define DC_DOM 0 /* 1..31 */ /* unused */
99
#define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
100
#define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
104
const char *z_filename;
110
const char *z_format;
114
struct rule *z_rules;
117
struct rule z_untilrule;
121
extern int link(const char *fromname, const char *toname);
122
static void addtt(const pg_time_t starttime, int type);
123
static int addtype(long gmtoff, const char *abbr, int isdst,
124
int ttisstd, int ttisgmt);
125
static void leapadd(const pg_time_t t, int positive, int rolling, int count);
126
static void adjleap(void);
127
static void associate(void);
128
static int ciequal(const char *ap, const char *bp);
129
static void convert(long val, char *buf);
130
static void dolink(const char *fromfile, const char *tofile);
131
static void doabbr(char *abbr, const char *format,
132
const char *letters, int isdst, int doquotes);
133
static void eat(const char *name, int num);
134
static void eats(const char *name, int num,
135
const char *rname, int rnum);
136
static long eitol(int i);
137
static void error(const char *message);
138
static char **getfields(char *buf);
139
static long gethms(const char *string, const char *errstrng,
141
static void infile(const char *filename);
142
static void inleap(char **fields, int nfields);
143
static void inlink(char **fields, int nfields);
144
static void inrule(char **fields, int nfields);
145
static int inzcont(char **fields, int nfields);
146
static int inzone(char **fields, int nfields);
147
static int inzsub(char **fields, int nfields, int iscont);
148
static int itsabbr(const char *abbr, const char *word);
149
static int itsdir(const char *name);
150
static int lowerit(int c);
151
static char *memcheck(char *tocheck);
152
static int mkdirs(char *filename);
153
static void newabbr(const char *abbr);
154
static long oadd(long t1, long t2);
155
static void outzone(const struct zone * zp, int ntzones);
156
static void puttzcode(long code, FILE *fp);
157
static int rcomp(const void *leftp, const void *rightp);
158
static pg_time_t rpytime(const struct rule * rp, int wantedy);
159
static void rulesub(struct rule * rp,
160
const char *loyearp, const char *hiyearp,
161
const char *typep, const char *monthp,
162
const char *dayp, const char *timep);
163
static void setboundaries(void);
164
static pg_time_t tadd(const pg_time_t t1, long t2);
165
static void usage(void);
166
static void writezone(const char *name, const char *string);
167
static int yearistype(int year, const char *type);
171
static const char *filename;
174
static int leapminyear;
175
static int leapmaxyear;
177
static int max_abbrvar_len;
178
static int max_format_len;
179
static zic_t max_time;
181
static zic_t min_time;
184
static const char *rfilename;
186
static const char *progname;
200
* Which fields are which on a Zone line.
208
#define ZF_TILMONTH 6
211
#define ZONE_MINFIELDS 5
212
#define ZONE_MAXFIELDS 9
215
* Which fields are which on a Zone continuation line.
221
#define ZFC_TILYEAR 3
222
#define ZFC_TILMONTH 4
224
#define ZFC_TILTIME 6
225
#define ZONEC_MINFIELDS 3
226
#define ZONEC_MAXFIELDS 7
229
* Which files are which on a Rule line.
241
#define RULE_FIELDS 10
244
* Which fields are which on a Link line.
249
#define LINK_FIELDS 3
252
* Which fields are which on a Leap line.
261
#define LEAP_FIELDS 7
271
static struct rule *rules;
272
static int nrules; /* number of rules */
274
static struct zone *zones;
275
static int nzones; /* number of zones */
279
const char *l_filename;
285
static struct link *links;
294
static struct lookup const *byword(const char *string,
295
const struct lookup * lp);
297
static struct lookup const line_codes[] = {
305
static struct lookup const mon_names[] = {
306
{"January", TM_JANUARY},
307
{"February", TM_FEBRUARY},
313
{"August", TM_AUGUST},
314
{"September", TM_SEPTEMBER},
315
{"October", TM_OCTOBER},
316
{"November", TM_NOVEMBER},
317
{"December", TM_DECEMBER},
321
static struct lookup const wday_names[] = {
322
{"Sunday", TM_SUNDAY},
323
{"Monday", TM_MONDAY},
324
{"Tuesday", TM_TUESDAY},
325
{"Wednesday", TM_WEDNESDAY},
326
{"Thursday", TM_THURSDAY},
327
{"Friday", TM_FRIDAY},
328
{"Saturday", TM_SATURDAY},
332
static struct lookup const lasts[] = {
333
{"last-Sunday", TM_SUNDAY},
334
{"last-Monday", TM_MONDAY},
335
{"last-Tuesday", TM_TUESDAY},
336
{"last-Wednesday", TM_WEDNESDAY},
337
{"last-Thursday", TM_THURSDAY},
338
{"last-Friday", TM_FRIDAY},
339
{"last-Saturday", TM_SATURDAY},
343
static struct lookup const begin_years[] = {
344
{"minimum", YR_MINIMUM},
345
{"maximum", YR_MAXIMUM},
349
static struct lookup const end_years[] = {
350
{"minimum", YR_MINIMUM},
351
{"maximum", YR_MAXIMUM},
356
static struct lookup const leap_types[] = {
358
{"Stationary", FALSE},
362
static const int len_months[2][MONSPERYEAR] = {
363
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
364
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
367
static const int len_years[2] = {
368
DAYSPERNYEAR, DAYSPERLYEAR
375
} attypes[TZ_MAX_TIMES];
376
static long gmtoffs[TZ_MAX_TYPES];
377
static char isdsts[TZ_MAX_TYPES];
378
static unsigned char abbrinds[TZ_MAX_TYPES];
379
static char ttisstds[TZ_MAX_TYPES];
380
static char ttisgmts[TZ_MAX_TYPES];
381
static char chars[TZ_MAX_CHARS];
382
static zic_t trans[TZ_MAX_LEAPS];
383
static long corr[TZ_MAX_LEAPS];
384
static char roll[TZ_MAX_LEAPS];
395
const char *e = strerror(errno);
397
(void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
404
#define emalloc(size) memcheck(imalloc(size))
405
#define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
406
#define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
407
#define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
414
eats(const char *name, int num, const char *rname, int rnum)
423
eat(const char *name, int num)
425
eats(name, num, (char *) NULL, -1);
429
error(const char *string)
432
* Match the format of "cc" to allow sh users to zic ... 2>&1 | error -t
433
* "*" -v on BSD systems.
435
(void) fprintf(stderr, _("\"%s\", line %d: %s"),
436
filename, linenum, string);
437
if (rfilename != NULL)
438
(void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
439
rfilename, rlinenum);
440
(void) fprintf(stderr, "\n");
445
warning(const char *string)
449
cp = ecpyalloc(_("warning: "));
450
cp = ecatalloc(cp, string);
459
(void) fprintf(stderr, _("%s: usage is %s \
460
[ --version ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
461
\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
466
static const char *psxrules;
467
static const char *lcltime;
468
static const char *directory;
469
static const char *leapsec;
470
static const char *yitcommand;
473
main(int argc, char *argv[])
480
(void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
483
if (TYPE_BIT(zic_t) < 64) {
484
(void) fprintf(stderr, "%s: %s\n", progname,
485
_("wild compilation-time specification of zic_t"));
488
for (i = 1; i < argc; ++i)
489
if (strcmp(argv[i], "--version") == 0)
491
(void) printf("%s\n", elsieid);
494
while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
500
if (directory == NULL)
504
(void) fprintf(stderr,
505
_("%s: More than one -d option specified\n"),
515
(void) fprintf(stderr,
516
_("%s: More than one -l option specified\n"),
522
if (psxrules == NULL)
526
(void) fprintf(stderr,
527
_("%s: More than one -p option specified\n"),
533
if (yitcommand == NULL)
537
(void) fprintf(stderr,
538
_("%s: More than one -y option specified\n"),
548
(void) fprintf(stderr,
549
_("%s: More than one -L option specified\n"),
558
(void) printf("%s: -s ignored\n", progname);
561
if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
562
usage(); /* usage message by request */
563
if (directory == NULL)
565
if (yitcommand == NULL)
566
yitcommand = "yearistype";
570
if (optind < argc && leapsec != NULL)
576
for (i = optind; i < argc; ++i)
581
for (i = 0; i < nzones; i = j)
584
* Find the next non-continuation zone entry.
586
for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
588
outzone(&zones[i], j - i);
594
for (i = 0; i < nlinks; ++i)
596
eat(links[i].l_filename, links[i].l_linenum);
597
dolink(links[i].l_from, links[i].l_to);
599
for (j = 0; j < nlinks; ++j)
600
if (strcmp(links[i].l_to,
601
links[j].l_from) == 0)
602
warning(_("link to link"));
606
eat("command line", 1);
607
dolink(lcltime, TZDEFAULT);
609
if (psxrules != NULL)
611
eat("command line", 1);
612
dolink(psxrules, TZDEFRULES);
614
return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
618
dolink(const char *fromfield, const char *tofield)
623
if (fromfield[0] == '/')
624
fromname = ecpyalloc(fromfield);
627
fromname = ecpyalloc(directory);
628
fromname = ecatalloc(fromname, "/");
629
fromname = ecatalloc(fromname, fromfield);
631
if (tofield[0] == '/')
632
toname = ecpyalloc(tofield);
635
toname = ecpyalloc(directory);
636
toname = ecatalloc(toname, "/");
637
toname = ecatalloc(toname, tofield);
641
* We get to be careful here since there's a fair chance of root running
645
(void) remove(toname);
646
if (link(fromname, toname) != 0)
650
if (mkdirs(toname) != 0)
653
result = link(fromname, toname);
656
access(fromname, F_OK) == 0 &&
659
const char *s = tofield;
660
char *symlinkcontents = NULL;
662
while ((s = strchr(s + 1, '/')) != NULL)
663
symlinkcontents = ecatalloc(symlinkcontents, "../");
664
symlinkcontents = ecatalloc(symlinkcontents, fromfield);
666
result = symlink(symlinkcontents, toname);
668
warning(_("hard link failed, symbolic link used"));
669
ifree(symlinkcontents);
674
const char *e = strerror(errno);
676
(void) fprintf(stderr,
677
_("%s: Cannot link from %s to %s: %s\n"),
678
progname, fromname, toname, e);
686
#define TIME_T_BITS_IN_FILE 64
694
for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
696
max_time = -(min_time + 1);
700
itsdir(const char *name)
705
myname = ecpyalloc(name);
706
myname = ecatalloc(myname, "/.");
707
accres = access(myname, F_OK);
713
* Associate sets of rules with zones.
721
rcomp(const void *cp1, const void *cp2)
723
return strcmp(((const struct rule *) cp1)->r_name,
724
((const struct rule *) cp2)->r_name);
739
(void) qsort((void *) rules, (size_t) nrules,
740
(size_t) sizeof *rules, rcomp);
741
for (i = 0; i < nrules - 1; ++i)
743
if (strcmp(rules[i].r_name,
744
rules[i + 1].r_name) != 0)
746
if (strcmp(rules[i].r_filename,
747
rules[i + 1].r_filename) == 0)
749
eat(rules[i].r_filename, rules[i].r_linenum);
750
warning(_("same rule name in multiple files"));
751
eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
752
warning(_("same rule name in multiple files"));
753
for (j = i + 2; j < nrules; ++j)
755
if (strcmp(rules[i].r_name,
756
rules[j].r_name) != 0)
758
if (strcmp(rules[i].r_filename,
759
rules[j].r_filename) == 0)
761
if (strcmp(rules[i + 1].r_filename,
762
rules[j].r_filename) == 0)
769
for (i = 0; i < nzones; ++i)
775
for (base = 0; base < nrules; base = out)
778
for (out = base + 1; out < nrules; ++out)
779
if (strcmp(rp->r_name, rules[out].r_name) != 0)
781
for (i = 0; i < nzones; ++i)
784
if (strcmp(zp->z_rule, rp->r_name) != 0)
787
zp->z_nrules = out - base;
790
for (i = 0; i < nzones; ++i)
793
if (zp->z_nrules == 0)
796
* Maybe we have a local standard time offset.
798
eat(zp->z_filename, zp->z_linenum);
799
zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
803
* Note, though, that if there's no rule, a '%s' in the format is
806
if (strchr(zp->z_format, '%') != 0)
807
error(_("%s in ruleless zone"));
815
infile(const char *name)
820
const struct lookup *lp;
826
if (strcmp(name, "-") == 0)
828
name = _("standard input");
831
else if ((fp = fopen(name, "r")) == NULL)
833
const char *e = strerror(errno);
835
(void) fprintf(stderr, _("%s: Cannot open %s: %s\n"),
840
for (num = 1;; ++num)
843
if (fgets(buf, (int) sizeof buf, fp) != buf)
845
cp = strchr(buf, '\n');
848
error(_("line too long"));
852
fields = getfields(buf);
854
while (fields[nfields] != NULL)
858
if (strcmp(fields[nfields], "-") == 0)
859
fields[nfields] = &nada;
867
wantcont = inzcont(fields, nfields);
870
lp = byword(fields[0], line_codes);
872
error(_("input line of unknown type"));
874
switch ((int) (lp->l_value))
877
inrule(fields, nfields);
881
wantcont = inzone(fields, nfields);
884
inlink(fields, nfields);
889
(void) fprintf(stderr,
890
_("%s: Leap line in non leap seconds file %s\n"),
893
inleap(fields, nfields);
896
default: /* "cannot happen" */
897
(void) fprintf(stderr,
898
_("%s: panic: Invalid l_value %d\n"),
899
progname, lp->l_value);
903
ifree((char *) fields);
907
(void) fprintf(stderr, _("%s: Error reading %s\n"),
911
if (fp != stdin && fclose(fp))
913
const char *e = strerror(errno);
915
(void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
916
progname, filename, e);
920
error(_("expected continuation line not found"));
924
* Convert a string of one of the forms
925
* h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
926
* into a number of seconds.
927
* A null string maps to zero.
928
* Call error with errstring and return zero on errors.
932
gethms(const char *string, const char *errstring, int signable)
939
if (string == NULL || *string == '\0')
943
else if (*string == '-')
950
if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
952
else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
954
else if (sscanf(string, scheck(string, "%ld:%d:%d"),
961
mm < 0 || mm >= MINSPERHOUR ||
962
ss < 0 || ss > SECSPERMIN)
967
if (LONG_MAX / SECSPERHOUR < hh) {
968
error(_("time overflow"));
971
if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
972
warning(_("24:00 not handled by pre-1998 versions of zic"));
973
if (noise && (hh > HOURSPERDAY ||
974
(hh == HOURSPERDAY && (mm != 0 || ss != 0))))
975
warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
976
return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
977
eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
981
inrule(char **fields, int nfields)
983
static struct rule r;
985
if (nfields != RULE_FIELDS)
987
error(_("wrong number of fields on Rule line"));
990
if (*fields[RF_NAME] == '\0')
992
error(_("nameless rule"));
995
r.r_filename = filename;
996
r.r_linenum = linenum;
997
r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
998
rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
999
fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
1000
r.r_name = ecpyalloc(fields[RF_NAME]);
1001
r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
1002
if (max_abbrvar_len < strlen(r.r_abbrvar))
1003
max_abbrvar_len = strlen(r.r_abbrvar);
1004
rules = (struct rule *) (void *) erealloc((char *) rules,
1005
(int) ((nrules + 1) * sizeof *rules));
1006
rules[nrules++] = r;
1010
inzone(char **fields, int nfields)
1015
if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
1017
error(_("wrong number of fields on Zone line"));
1020
if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL)
1022
buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1024
_("\"Zone %s\" line and -l option are mutually exclusive"),
1029
if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
1031
buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1033
_("\"Zone %s\" line and -p option are mutually exclusive"),
1038
for (i = 0; i < nzones; ++i)
1039
if (zones[i].z_name != NULL &&
1040
strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
1042
buf = erealloc(buf, (int) (132 +
1043
strlen(fields[ZF_NAME]) +
1044
strlen(zones[i].z_filename)));
1046
_("duplicate zone name %s (file \"%s\", line %d)"),
1048
zones[i].z_filename,
1049
zones[i].z_linenum);
1053
return inzsub(fields, nfields, FALSE);
1057
inzcont(char **fields, int nfields)
1059
if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
1061
error(_("wrong number of fields on Zone continuation line"));
1064
return inzsub(fields, nfields, TRUE);
1068
inzsub(char **fields, int nfields, int iscont)
1071
static struct zone z;
1083
i_gmtoff = ZFC_GMTOFF;
1085
i_format = ZFC_FORMAT;
1086
i_untilyear = ZFC_TILYEAR;
1087
i_untilmonth = ZFC_TILMONTH;
1088
i_untilday = ZFC_TILDAY;
1089
i_untiltime = ZFC_TILTIME;
1094
i_gmtoff = ZF_GMTOFF;
1096
i_format = ZF_FORMAT;
1097
i_untilyear = ZF_TILYEAR;
1098
i_untilmonth = ZF_TILMONTH;
1099
i_untilday = ZF_TILDAY;
1100
i_untiltime = ZF_TILTIME;
1101
z.z_name = ecpyalloc(fields[ZF_NAME]);
1103
z.z_filename = filename;
1104
z.z_linenum = linenum;
1105
z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1106
if ((cp = strchr(fields[i_format], '%')) != 0)
1108
if (*++cp != 's' || strchr(cp, '%') != 0)
1110
error(_("invalid abbreviation format"));
1114
z.z_rule = ecpyalloc(fields[i_rule]);
1115
z.z_format = ecpyalloc(fields[i_format]);
1116
if (max_format_len < strlen(z.z_format))
1117
max_format_len = strlen(z.z_format);
1118
hasuntil = nfields > i_untilyear;
1121
z.z_untilrule.r_filename = filename;
1122
z.z_untilrule.r_linenum = linenum;
1123
rulesub(&z.z_untilrule,
1124
fields[i_untilyear],
1127
(nfields > i_untilmonth) ?
1128
fields[i_untilmonth] : "Jan",
1129
(nfields > i_untilday) ? fields[i_untilday] : "1",
1130
(nfields > i_untiltime) ? fields[i_untiltime] : "0");
1131
z.z_untiltime = rpytime(&z.z_untilrule,
1132
z.z_untilrule.r_loyear);
1133
if (iscont && nzones > 0 &&
1134
z.z_untiltime > min_time &&
1135
z.z_untiltime < max_time &&
1136
zones[nzones - 1].z_untiltime > min_time &&
1137
zones[nzones - 1].z_untiltime < max_time &&
1138
zones[nzones - 1].z_untiltime >= z.z_untiltime)
1140
error(_("Zone continuation line end time is not after end time of previous line"));
1144
zones = (struct zone *) (void *) erealloc((char *) zones,
1145
(int) ((nzones + 1) * sizeof *zones));
1146
zones[nzones++] = z;
1149
* If there was an UNTIL field on this line, there's more information
1150
* about the zone on the next line.
1156
inleap(char **fields, int nfields)
1159
const struct lookup *lp;
1169
if (nfields != LEAP_FIELDS)
1171
error(_("wrong number of fields on Leap line"));
1175
cp = fields[LP_YEAR];
1176
if (sscanf(cp, scheck(cp, "%d"), &year) != 1)
1181
error(_("invalid leaping year"));
1184
if (!leapseen || leapmaxyear < year)
1186
if (!leapseen || leapminyear > year)
1194
i = len_years[isleap(j)];
1200
i = -len_years[isleap(j)];
1202
dayoff = oadd(dayoff, eitol(i));
1204
if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
1206
error(_("invalid month name"));
1209
month = lp->l_value;
1213
i = len_months[isleap(year)][j];
1214
dayoff = oadd(dayoff, eitol(i));
1217
cp = fields[LP_DAY];
1218
if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1219
day <= 0 || day > len_months[isleap(year)][month])
1221
error(_("invalid day of month"));
1224
dayoff = oadd(dayoff, eitol(day - 1));
1225
if (dayoff < min_time / SECSPERDAY)
1227
error(_("time too small"));
1230
if (dayoff > max_time / SECSPERDAY)
1232
error(_("time too large"));
1235
t = (zic_t) dayoff *SECSPERDAY;
1237
tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1238
cp = fields[LP_CORR];
1243
if (strcmp(cp, "") == 0)
1244
{ /* infile() turns "-" into "" */
1248
else if (strcmp(cp, "--") == 0)
1253
else if (strcmp(cp, "+") == 0)
1258
else if (strcmp(cp, "++") == 0)
1265
error(_("illegal CORRECTION field on Leap line"));
1268
if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL)
1270
error(_("illegal Rolling/Stationary field on Leap line"));
1273
leapadd(tadd(t, tod), positive, lp->l_value, count);
1278
inlink(char **fields, int nfields)
1282
if (nfields != LINK_FIELDS)
1284
error(_("wrong number of fields on Link line"));
1287
if (*fields[LF_FROM] == '\0')
1289
error(_("blank FROM field on Link line"));
1292
if (*fields[LF_TO] == '\0')
1294
error(_("blank TO field on Link line"));
1297
l.l_filename = filename;
1298
l.l_linenum = linenum;
1299
l.l_from = ecpyalloc(fields[LF_FROM]);
1300
l.l_to = ecpyalloc(fields[LF_TO]);
1301
links = (struct link *) (void *) erealloc((char *) links,
1302
(int) ((nlinks + 1) * sizeof *links));
1303
links[nlinks++] = l;
1307
rulesub(struct rule * rp, const char *loyearp, const char *hiyearp,
1308
const char *typep, const char *monthp, const char *dayp,
1311
const struct lookup *lp;
1316
if ((lp = byword(monthp, mon_names)) == NULL)
1318
error(_("invalid month name"));
1321
rp->r_month = lp->l_value;
1322
rp->r_todisstd = FALSE;
1323
rp->r_todisgmt = FALSE;
1324
dp = ecpyalloc(timep);
1327
ep = dp + strlen(dp) - 1;
1328
switch (lowerit(*ep))
1330
case 's': /* Standard */
1331
rp->r_todisstd = TRUE;
1332
rp->r_todisgmt = FALSE;
1335
case 'w': /* Wall */
1336
rp->r_todisstd = FALSE;
1337
rp->r_todisgmt = FALSE;
1340
case 'g': /* Greenwich */
1341
case 'u': /* Universal */
1342
case 'z': /* Zulu */
1343
rp->r_todisstd = TRUE;
1344
rp->r_todisgmt = TRUE;
1349
rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1356
lp = byword(cp, begin_years);
1357
rp->r_lowasnum = lp == NULL;
1358
if (!rp->r_lowasnum)
1359
switch ((int) lp->l_value)
1362
rp->r_loyear = INT_MIN;
1365
rp->r_loyear = INT_MAX;
1367
default: /* "cannot happen" */
1368
(void) fprintf(stderr,
1369
_("%s: panic: Invalid l_value %d\n"),
1370
progname, lp->l_value);
1373
else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1)
1375
error(_("invalid starting year"));
1379
lp = byword(cp, end_years);
1380
rp->r_hiwasnum = lp == NULL;
1381
if (!rp->r_hiwasnum)
1382
switch ((int) lp->l_value)
1385
rp->r_hiyear = INT_MIN;
1388
rp->r_hiyear = INT_MAX;
1391
rp->r_hiyear = rp->r_loyear;
1393
default: /* "cannot happen" */
1394
(void) fprintf(stderr,
1395
_("%s: panic: Invalid l_value %d\n"),
1396
progname, lp->l_value);
1399
else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1)
1401
error(_("invalid ending year"));
1404
if (rp->r_loyear > rp->r_hiyear)
1406
error(_("starting year greater than ending year"));
1410
rp->r_yrtype = NULL;
1413
if (rp->r_loyear == rp->r_hiyear)
1415
error(_("typed single year"));
1418
rp->r_yrtype = ecpyalloc(typep);
1422
* Day work. Accept things such as: 1 last-Sunday Sun<=20 Sun>=7
1424
dp = ecpyalloc(dayp);
1425
if ((lp = byword(dp, lasts)) != NULL)
1427
rp->r_dycode = DC_DOWLEQ;
1428
rp->r_wday = lp->l_value;
1429
rp->r_dayofmonth = len_months[1][rp->r_month];
1433
if ((ep = strchr(dp, '<')) != 0)
1434
rp->r_dycode = DC_DOWLEQ;
1435
else if ((ep = strchr(dp, '>')) != 0)
1436
rp->r_dycode = DC_DOWGEQ;
1440
rp->r_dycode = DC_DOM;
1442
if (rp->r_dycode != DC_DOM)
1447
error(_("invalid day of month"));
1451
if ((lp = byword(dp, wday_names)) == NULL)
1453
error(_("invalid weekday name"));
1457
rp->r_wday = lp->l_value;
1459
if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1460
rp->r_dayofmonth <= 0 ||
1461
(rp->r_dayofmonth > len_months[1][rp->r_month]))
1463
error(_("invalid day of month"));
1472
convert(long val, char *buf)
1477
for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1478
buf[i] = val >> shift;
1482
convert64(zic_t val, char *buf)
1487
for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1488
buf[i] = val >> shift;
1492
puttzcode(long val, FILE *fp)
1497
(void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1501
puttzcode64(zic_t val, FILE *fp)
1505
convert64(val, buf);
1506
(void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1510
atcomp(const void *avp, const void *bvp)
1512
const zic_t a = ((const struct attype *) avp)->at;
1513
const zic_t b = ((const struct attype *) bvp)->at;
1515
return (a < b) ? -1 : (a > b);
1521
return x == ((zic_t) ((int32) x));
1525
writezone(const char *name, const char *string)
1530
int leapcnt32, leapi32;
1531
int timecnt32, timei32;
1533
static char *fullname;
1534
static const struct tzhead tzh0;
1535
static struct tzhead tzh;
1536
zic_t ats[TZ_MAX_TIMES];
1537
unsigned char types[TZ_MAX_TIMES];
1543
(void) qsort((void *) attypes, (size_t) timecnt,
1544
(size_t) sizeof *attypes, atcomp);
1555
while (fromi < timecnt && attypes[fromi].at < min_time)
1558
while (fromi < timecnt && attypes[fromi].type == 0)
1559
++fromi; /* handled by default rule */
1560
for (; fromi < timecnt; ++fromi)
1563
&& ((attypes[fromi].at
1564
+ gmtoffs[attypes[toi - 1].type])
1565
<= (attypes[toi - 1].at
1566
+ gmtoffs[toi == 1 ? 0
1567
: attypes[toi - 2].type])))
1569
attypes[toi - 1].type = attypes[fromi].type;
1573
attypes[toi - 1].type != attypes[fromi].type)
1574
attypes[toi++] = attypes[fromi];
1582
for (i = 0; i < timecnt; ++i)
1584
ats[i] = attypes[i].at;
1585
types[i] = attypes[i].type;
1588
* Correct for leap seconds.
1590
for (i = 0; i < timecnt; ++i) {
1593
if (ats[i] > trans[j] - corr[j]) {
1594
ats[i] = tadd(ats[i], corr[j]);
1599
* Figure out 32-bit-limited starts and counts.
1601
timecnt32 = timecnt;
1603
leapcnt32 = leapcnt;
1605
while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1607
while (timecnt32 > 0 && !is32(ats[timei32]))
1612
while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1614
while (leapcnt32 > 0 && !is32(trans[leapi32]))
1619
fullname = erealloc(fullname,
1620
(int) (strlen(directory) + 1 + strlen(name) + 1));
1621
(void) sprintf(fullname, "%s/%s", directory, name);
1624
* Remove old file, if any, to snap links.
1626
if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1628
const char *e = strerror(errno);
1630
(void) fprintf(stderr, _("%s: Cannot remove %s: %s\n"),
1631
progname, fullname, e);
1634
if ((fp = fopen(fullname, "wb")) == NULL)
1636
if (mkdirs(fullname) != 0)
1637
(void) exit(EXIT_FAILURE);
1638
if ((fp = fopen(fullname, "wb")) == NULL)
1640
const char *e = strerror(errno);
1642
(void) fprintf(stderr, _("%s: Cannot create %s: %s\n"),
1643
progname, fullname, e);
1647
for (pass = 1; pass <= 2; ++pass) {
1648
register int thistimei, thistimecnt;
1649
register int thisleapi, thisleapcnt;
1650
register int thistimelim, thisleaplim;
1651
int writetype[TZ_MAX_TIMES];
1652
int typemap[TZ_MAX_TYPES];
1653
register int thistypecnt;
1654
char thischars[TZ_MAX_CHARS];
1656
int indmap[TZ_MAX_CHARS];
1659
thistimei = timei32;
1660
thistimecnt = timecnt32;
1661
thisleapi = leapi32;
1662
thisleapcnt = leapcnt32;
1665
thistimecnt = timecnt;
1667
thisleapcnt = leapcnt;
1669
thistimelim = thistimei + thistimecnt;
1670
thisleaplim = thisleapi + thisleapcnt;
1671
for (i = 0; i < typecnt; ++i)
1672
writetype[i] = thistimecnt == timecnt;
1673
if (thistimecnt == 0) {
1675
** No transition times fall in the current
1676
** (32- or 64-bit) window.
1679
writetype[typecnt - 1] = TRUE;
1681
for (i = thistimei - 1; i < thistimelim; ++i)
1683
writetype[types[i]] = TRUE;
1685
** For America/Godthab and Antarctica/Palmer
1688
writetype[0] = TRUE;
1691
for (i = 0; i < typecnt; ++i)
1692
typemap[i] = writetype[i] ? thistypecnt++ : -1;
1693
for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1696
for (i = 0; i < typecnt; ++i) {
1697
register char * thisabbr;
1701
if (indmap[abbrinds[i]] >= 0)
1703
thisabbr = &chars[abbrinds[i]];
1704
for (j = 0; j < thischarcnt; ++j)
1705
if (strcmp(&thischars[j], thisabbr) == 0)
1707
if (j == thischarcnt) {
1708
(void) strcpy(&thischars[(int) thischarcnt],
1710
thischarcnt += strlen(thisabbr) + 1;
1712
indmap[abbrinds[i]] = j;
1714
#define DO(field) (void) fwrite((void *) tzh.field, \
1715
(size_t) sizeof tzh.field, (size_t) 1, fp)
1717
(void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1718
tzh.tzh_version[0] = ZIC_VERSION;
1719
convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1720
convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1721
convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1722
convert(eitol(thistimecnt), tzh.tzh_timecnt);
1723
convert(eitol(thistypecnt), tzh.tzh_typecnt);
1724
convert(eitol(thischarcnt), tzh.tzh_charcnt);
1735
for (i = thistimei; i < thistimelim; ++i)
1737
puttzcode((long) ats[i], fp);
1738
else puttzcode64(ats[i], fp);
1739
for (i = thistimei; i < thistimelim; ++i) {
1742
uc = typemap[types[i]];
1743
(void) fwrite((void *) &uc,
1748
for (i = 0; i < typecnt; ++i)
1750
puttzcode(gmtoffs[i], fp);
1751
(void) putc(isdsts[i], fp);
1752
(void) putc((unsigned char) indmap[abbrinds[i]], fp);
1754
if (thischarcnt != 0)
1755
(void) fwrite((void *) thischars,
1756
(size_t) sizeof thischars[0],
1757
(size_t) thischarcnt, fp);
1758
for (i = thisleapi; i < thisleaplim; ++i) {
1759
register zic_t todo;
1762
if (timecnt == 0 || trans[i] < ats[0]) {
1765
if (++j >= typecnt) {
1771
while (j < timecnt &&
1776
todo = tadd(trans[i], -gmtoffs[j]);
1777
} else todo = trans[i];
1779
puttzcode((long) todo, fp);
1780
else puttzcode64(todo, fp);
1781
puttzcode(corr[i], fp);
1783
for (i = 0; i < typecnt; ++i)
1785
(void) putc(ttisstds[i], fp);
1786
for (i = 0; i < typecnt; ++i)
1788
(void) putc(ttisgmts[i], fp);
1790
(void) fprintf(fp, "\n%s\n", string);
1791
if (ferror(fp) || fclose(fp)) {
1792
(void) fprintf(stderr, _("%s: Error writing %s\n"),
1793
progname, fullname);
1799
doabbr(char *abbr, const char *format, const char *letters, int isdst,
1806
slashp = strchr(format, '/');
1809
if (letters == NULL)
1810
(void) strcpy(abbr, format);
1812
(void) sprintf(abbr, format, letters);
1815
(void) strcpy(abbr, slashp + 1);
1818
if (slashp > format)
1819
(void) strncpy(abbr, format,
1820
(unsigned) (slashp - format));
1821
abbr[slashp - format] = '\0';
1825
for (cp = abbr; *cp != '\0'; ++cp)
1826
if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1827
strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1830
if (len > 0 && *cp == '\0')
1832
abbr[len + 2] = '\0';
1833
abbr[len + 1] = '>';
1834
for ( ; len > 0; --len)
1835
abbr[len] = abbr[len - 1];
1849
stringoffset(char *result, long offset)
1857
(void) strcpy(result, "-");
1860
seconds = offset % SECSPERMIN;
1861
offset /= SECSPERMIN;
1862
minutes = offset % MINSPERHOUR;
1863
offset /= MINSPERHOUR;
1865
if (hours >= HOURSPERDAY) {
1869
(void) sprintf(end(result), "%d", hours);
1870
if (minutes != 0 || seconds != 0) {
1871
(void) sprintf(end(result), ":%02d", minutes);
1873
(void) sprintf(end(result), ":%02d", seconds);
1879
stringrule(char *result, const struct rule *rp, long dstoff, long gmtoff)
1883
result = end(result);
1884
if (rp->r_dycode == DC_DOM)
1888
if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1891
for (month = 0; month < rp->r_month; ++month)
1892
total += len_months[0][month];
1893
(void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1899
if (rp->r_dycode == DC_DOWGEQ)
1901
week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1902
if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1905
else if (rp->r_dycode == DC_DOWLEQ)
1907
if (rp->r_dayofmonth == len_months[1][rp->r_month])
1910
week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1911
if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1916
return -1; /* "cannot happen" */
1917
(void) sprintf(result, "M%d.%d.%d",
1918
rp->r_month + 1, week, rp->r_wday);
1923
if (rp->r_todisstd && rp->r_stdoff == 0)
1930
if (tod != 2 * SECSPERMIN * MINSPERHOUR)
1932
(void) strcat(result, "/");
1933
if (stringoffset(end(result), tod) != 0)
1940
stringzone(char *result, const struct zone *zpfirst, int zonecount)
1942
const struct zone * zp;
1944
struct rule * stdrp;
1945
struct rule * dstrp;
1947
const char * abbrvar;
1950
zp = zpfirst + zonecount - 1;
1951
stdrp = dstrp = NULL;
1952
for (i = 0; i < zp->z_nrules; ++i)
1954
rp = &zp->z_rules[i];
1955
if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1957
if (rp->r_yrtype != NULL)
1959
if (rp->r_stdoff == 0) {
1971
if (stdrp == NULL && dstrp == NULL)
1974
* There are no rules running through "max".
1975
* Let's find the latest rule.
1977
for (i = 0; i < zp->z_nrules; ++i)
1979
rp = &zp->z_rules[i];
1980
if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1981
(rp->r_hiyear == stdrp->r_hiyear &&
1982
rp->r_month > stdrp->r_month))
1985
if (stdrp != NULL && stdrp->r_stdoff != 0)
1986
return; /* We end up in DST (a POSIX no-no). */
1988
* Horrid special case: if year is 2037,
1989
* presume this is a zone handled on a year-by-year basis;
1990
* do not try to apply a rule to the zone.
1992
if (stdrp != NULL && stdrp->r_hiyear == 2037)
1995
if (stdrp == NULL && zp->z_nrules != 0)
1997
abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1998
doabbr(result, zp->z_format, abbrvar, FALSE, TRUE);
1999
if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
2005
doabbr(end(result), zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
2006
if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
2007
if (stringoffset(end(result),
2008
-(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
2012
(void) strcat(result, ",");
2013
if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
2017
(void) strcat(result, ",");
2018
if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
2025
outzone(const struct zone * zpfirst, int zonecount)
2027
const struct zone *zp;
2033
zic_t starttime = 0;
2034
zic_t untiltime = 0;
2048
max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2049
max_envvar_len = 2 * max_abbr_len + 5 * 9;
2050
startbuf = emalloc(max_abbr_len + 1);
2051
ab = emalloc(max_abbr_len + 1);
2052
envvar = emalloc(max_envvar_len + 1);
2055
* Now. . .finally. . .generate some useful data!
2062
* Thanks to Earl Chew for noting the need to
2063
* unconditionally initialize startttisstd.
2065
startttisstd = FALSE;
2066
startttisgmt = FALSE;
2067
min_year = max_year = EPOCH_YEAR;
2070
updateminmax(leapminyear);
2071
updateminmax(leapmaxyear);
2073
for (i = 0; i < zonecount; ++i)
2076
if (i < zonecount - 1)
2077
updateminmax(zp->z_untilrule.r_loyear);
2078
for (j = 0; j < zp->z_nrules; ++j)
2080
rp = &zp->z_rules[j];
2082
updateminmax(rp->r_loyear);
2084
updateminmax(rp->r_hiyear);
2088
* Generate lots of data if a rule can't cover all future times.
2090
stringzone(envvar, zpfirst, zonecount);
2091
if (noise && envvar[0] == '\0') {
2094
wp = ecpyalloc(_("no POSIX environment variable for zone"));
2095
wp = ecatalloc(wp, " ");
2096
wp = ecatalloc(wp, zpfirst->z_name);
2100
if (envvar[0] == '\0')
2102
if (min_year >= INT_MIN + YEARSPERREPEAT)
2103
min_year -= YEARSPERREPEAT;
2104
else min_year = INT_MIN;
2105
if (max_year <= INT_MAX - YEARSPERREPEAT)
2106
max_year += YEARSPERREPEAT;
2107
else max_year = INT_MAX;
2110
* For the benefit of older systems,
2111
* generate data from 1900 through 2037.
2113
if (min_year > 1900)
2115
if (max_year < 2037)
2117
for (i = 0; i < zonecount; ++i)
2120
* A guess that may well be corrected later.
2124
usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2125
useuntil = i < (zonecount - 1);
2126
if (useuntil && zp->z_untiltime <= min_time)
2128
gmtoff = zp->z_gmtoff;
2129
eat(zp->z_filename, zp->z_linenum);
2131
startoff = zp->z_gmtoff;
2132
if (zp->z_nrules == 0)
2134
stdoff = zp->z_stdoff;
2135
doabbr(startbuf, zp->z_format,
2136
(char *) NULL, stdoff != 0, FALSE);
2137
type = addtype(oadd(zp->z_gmtoff, stdoff),
2138
startbuf, stdoff != 0, startttisstd,
2142
addtt(starttime, type);
2145
else if (stdoff != 0)
2146
addtt(min_time, type);
2149
for (year = min_year; year <= max_year; ++year)
2151
if (useuntil && year > zp->z_untilrule.r_hiyear)
2155
* Mark which rules to do in the current year. For those to
2156
* do, calculate rpytime(rp, year);
2158
for (j = 0; j < zp->z_nrules; ++j)
2160
rp = &zp->z_rules[j];
2161
eats(zp->z_filename, zp->z_linenum,
2162
rp->r_filename, rp->r_linenum);
2163
rp->r_todo = year >= rp->r_loyear &&
2164
year <= rp->r_hiyear &&
2165
yearistype(year, rp->r_yrtype);
2167
rp->r_temp = rpytime(rp, year);
2179
* Turn untiltime into UTC assuming the current gmtoff
2180
* and stdoff values.
2182
untiltime = zp->z_untiltime;
2183
if (!zp->z_untilrule.r_todisgmt)
2184
untiltime = tadd(untiltime,
2186
if (!zp->z_untilrule.r_todisstd)
2187
untiltime = tadd(untiltime,
2192
* Find the rule (of those to do, if any) that takes
2193
* effect earliest in the year.
2196
for (j = 0; j < zp->z_nrules; ++j)
2198
rp = &zp->z_rules[j];
2201
eats(zp->z_filename, zp->z_linenum,
2202
rp->r_filename, rp->r_linenum);
2203
offset = rp->r_todisgmt ? 0 : gmtoff;
2204
if (!rp->r_todisstd)
2205
offset = oadd(offset, stdoff);
2207
if (jtime == min_time ||
2210
jtime = tadd(jtime, -offset);
2211
if (k < 0 || jtime < ktime)
2218
break; /* go on to next year */
2219
rp = &zp->z_rules[k];
2221
if (useuntil && ktime >= untiltime)
2223
stdoff = rp->r_stdoff;
2224
if (usestart && ktime == starttime)
2228
if (ktime < starttime)
2230
startoff = oadd(zp->z_gmtoff,
2232
doabbr(startbuf, zp->z_format,
2238
if (*startbuf == '\0' &&
2239
startoff == oadd(zp->z_gmtoff, stdoff))
2249
eats(zp->z_filename, zp->z_linenum,
2250
rp->r_filename, rp->r_linenum);
2251
doabbr(ab, zp->z_format, rp->r_abbrvar,
2252
rp->r_stdoff != 0, FALSE);
2253
offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2254
type = addtype(offset, ab, rp->r_stdoff != 0,
2255
rp->r_todisstd, rp->r_todisgmt);
2261
if (*startbuf == '\0' &&
2262
zp->z_format != NULL &&
2263
strchr(zp->z_format, '%') == NULL &&
2264
strchr(zp->z_format, '/') == NULL)
2265
(void) strcpy(startbuf, zp->z_format);
2266
eat(zp->z_filename, zp->z_linenum);
2267
if (*startbuf == '\0')
2268
error(_("cannot determine time zone abbreviation to use just after until time"));
2271
addtype(startoff, startbuf,
2272
startoff != zp->z_gmtoff,
2278
* Now we may get to set starttime for the next zone line.
2282
startttisstd = zp->z_untilrule.r_todisstd;
2283
startttisgmt = zp->z_untilrule.r_todisgmt;
2284
starttime = zp->z_untiltime;
2286
starttime = tadd(starttime, -stdoff);
2288
starttime = tadd(starttime, -gmtoff);
2291
writezone(zpfirst->z_name, envvar);
2298
addtt(const zic_t starttime, int type)
2300
if (starttime <= min_time ||
2301
(timecnt == 1 && attypes[0].at < min_time))
2303
gmtoffs[0] = gmtoffs[type];
2304
isdsts[0] = isdsts[type];
2305
ttisstds[0] = ttisstds[type];
2306
ttisgmts[0] = ttisgmts[type];
2307
if (abbrinds[type] != 0)
2308
(void) strcpy(chars, &chars[abbrinds[type]]);
2310
charcnt = strlen(chars) + 1;
2315
if (timecnt >= TZ_MAX_TIMES)
2317
error(_("too many transitions?!"));
2320
attypes[timecnt].at = starttime;
2321
attypes[timecnt].type = type;
2326
addtype(long gmtoff, const char *abbr, int isdst,
2327
int ttisstd, int ttisgmt)
2332
if (isdst != TRUE && isdst != FALSE)
2334
error(_("internal error - addtype called with bad isdst"));
2337
if (ttisstd != TRUE && ttisstd != FALSE)
2339
error(_("internal error - addtype called with bad ttisstd"));
2342
if (ttisgmt != TRUE && ttisgmt != FALSE)
2344
error(_("internal error - addtype called with bad ttisgmt"));
2349
* See if there's already an entry for this zone type. If so, just return
2352
for (i = 0; i < typecnt; ++i)
2354
if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2355
strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2356
ttisstd == ttisstds[i] &&
2357
ttisgmt == ttisgmts[i])
2362
* There isn't one; add a new one, unless there are already too many.
2364
if (typecnt >= TZ_MAX_TYPES)
2366
error(_("too many local time types"));
2369
if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2370
error(_("UTC offset out of range"));
2373
gmtoffs[i] = gmtoff;
2375
ttisstds[i] = ttisstd;
2376
ttisgmts[i] = ttisgmt;
2378
for (j = 0; j < charcnt; ++j)
2379
if (strcmp(&chars[j], abbr) == 0)
2389
leapadd(const zic_t t, int positive, int rolling, int count)
2394
if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS)
2396
error(_("too many leap seconds"));
2399
for (i = 0; i < leapcnt; ++i)
2404
error(_("repeated leap second moment"));
2411
for (j = leapcnt; j > i; --j)
2413
trans[j] = trans[j - 1];
2414
corr[j] = corr[j - 1];
2415
roll[j] = roll[j - 1];
2418
corr[i] = positive ? 1L : eitol(-count);
2421
} while (positive && --count != 0);
2431
* propagate leap seconds forward
2433
for (i = 0; i < leapcnt; ++i)
2435
trans[i] = tadd(trans[i], last);
2436
last = corr[i] += last;
2441
yearistype(int year, const char *type)
2446
if (type == NULL || *type == '\0')
2448
buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2449
(void) sprintf(buf, "%s %d %s", yitcommand, year, type);
2450
result = system(buf);
2451
if (WIFEXITED(result))
2452
switch (WEXITSTATUS(result))
2459
error(_("Wild result from command execution"));
2460
(void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2461
progname, buf, result);
2469
a = (unsigned char) a;
2470
return (isascii(a) && isupper(a)) ? tolower(a) : a;
2474
ciequal(const char *ap, const char *bp)
2476
while (lowerit(*ap) == lowerit(*bp++))
2483
itsabbr(const char *abbr, const char *word)
2485
if (lowerit(*abbr) != lowerit(*word))
2488
while (*++abbr != '\0')
2493
} while (lowerit(*word++) != lowerit(*abbr));
2497
static const struct lookup *
2498
byword(const char *word, const struct lookup * table)
2500
const struct lookup *foundlp;
2501
const struct lookup *lp;
2503
if (word == NULL || table == NULL)
2507
* Look for exact match.
2509
for (lp = table; lp->l_word != NULL; ++lp)
2510
if (ciequal(word, lp->l_word))
2514
* Look for inexact match.
2517
for (lp = table; lp->l_word != NULL; ++lp)
2518
if (itsabbr(word, lp->l_word))
2520
if (foundlp == NULL)
2523
return NULL; /* multiple inexact matches */
2537
array = (char **) (void *)
2538
emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2542
while (isascii((unsigned char) *cp) &&
2543
isspace((unsigned char) *cp))
2545
if (*cp == '\0' || *cp == '#')
2547
array[nsubs++] = dp = cp;
2550
if ((*dp = *cp++) != '"')
2553
while ((*dp = *cp++) != '"')
2558
error(_("Odd number of quotation marks"));
2561
} while (*cp != '\0' && *cp != '#' &&
2562
(!isascii(*cp) || !isspace((unsigned char) *cp)));
2563
if (isascii(*cp) && isspace((unsigned char) *cp))
2567
array[nsubs] = NULL;
2572
oadd(long t1, long t2)
2577
if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
2579
error(_("time overflow"));
2586
tadd(const zic_t t1, long t2)
2590
if (t1 == max_time && t2 > 0)
2592
if (t1 == min_time && t2 < 0)
2595
if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
2597
error(_("time overflow"));
2604
* Given a rule, and a year, compute the date - in seconds since January 1,
2605
* 1970, 00:00 LOCAL time - in that year that the rule refers to.
2609
rpytime(const struct rule * rp, int wantedy)
2614
long dayoff; /* with a nod to Margaret O. */
2617
if (wantedy == INT_MIN)
2619
if (wantedy == INT_MAX)
2624
while (wantedy != y)
2628
i = len_years[isleap(y)];
2634
i = -len_years[isleap(y)];
2636
dayoff = oadd(dayoff, eitol(i));
2638
while (m != rp->r_month)
2640
i = len_months[isleap(y)][m];
2641
dayoff = oadd(dayoff, eitol(i));
2644
i = rp->r_dayofmonth;
2645
if (m == TM_FEBRUARY && i == 29 && !isleap(y))
2647
if (rp->r_dycode == DC_DOWLEQ)
2651
error(_("use of 2/29 in non leap-year"));
2656
dayoff = oadd(dayoff, eitol(i));
2657
if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
2661
#define LDAYSPERWEEK ((long) DAYSPERWEEK)
2662
wday = eitol(EPOCH_WDAY);
2665
* Don't trust mod of negative numbers.
2668
wday = (wday + dayoff) % LDAYSPERWEEK;
2671
wday -= ((-dayoff) % LDAYSPERWEEK);
2673
wday += LDAYSPERWEEK;
2675
while (wday != eitol(rp->r_wday))
2676
if (rp->r_dycode == DC_DOWGEQ)
2678
dayoff = oadd(dayoff, (long) 1);
2679
if (++wday >= LDAYSPERWEEK)
2685
dayoff = oadd(dayoff, (long) -1);
2687
wday = LDAYSPERWEEK - 1;
2690
if (i < 0 || i >= len_months[isleap(y)][m])
2693
warning(_("rule goes past start/end of month--\
2694
will not work with pre-2004 versions of zic"));
2697
if (dayoff < min_time / SECSPERDAY)
2699
if (dayoff > max_time / SECSPERDAY)
2701
t = (zic_t) dayoff *SECSPERDAY;
2702
return tadd(t, rp->r_tod);
2706
newabbr(const char *string)
2710
if (strcmp(string, GRANDPARENTED) != 0)
2716
* Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2717
* optionally followed by a + or - and a number from 1 to 14.
2721
while (isascii((unsigned char) *cp) &&
2722
isalpha((unsigned char) *cp))
2724
if (cp - string == 0)
2725
wp = _("time zone abbreviation lacks alphabetic at start");
2726
if (noise && cp - string > 3)
2727
wp = _("time zone abbreviation has more than 3 alphabetics");
2728
if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2729
wp = _("time zone abbreviation has too many alphabetics");
2730
if (wp == NULL && (*cp == '+' || *cp == '-')) {
2732
if (isascii((unsigned char) *cp) &&
2733
isdigit((unsigned char) *cp))
2735
*cp >= '0' && *cp <= '4')
2739
wp = _("time zone abbreviation differs from POSIX standard");
2742
wp = ecatalloc(wp, " (");
2743
wp = ecatalloc(wp, string);
2744
wp = ecatalloc(wp, ")");
2749
i = strlen(string) + 1;
2750
if (charcnt + i > TZ_MAX_CHARS)
2752
error(_("too many, or too long, time zone abbreviations"));
2755
(void) strcpy(&chars[charcnt], string);
2756
charcnt += eitol(i);
2760
mkdirs(char *argname)
2765
if (argname == NULL || *argname == '\0')
2767
cp = name = ecpyalloc(argname);
2768
while ((cp = strchr(cp + 1, '/')) != 0)
2774
* DOS drive specifier?
2776
if (isalpha((unsigned char) name[0]) &&
2777
name[1] == ':' && name[2] == '\0')
2786
* It doesn't seem to exist, so we try to create it. Creation may
2787
* fail because of the directory being created by some other
2788
* multiprocessor, so we get to do extra checking.
2790
if (mkdir(name, MKDIR_UMASK) != 0)
2792
const char *e = strerror(errno);
2794
if (errno != EEXIST || !itsdir(name))
2796
(void) fprintf(stderr,
2797
_("%s: Cannot create directory %s: %s\n"),
2816
if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2818
(void) fprintf(stderr,
2819
_("%s: %d did not sign extend correctly\n"),
2827
* UNIX was a registered trademark of The Open Group in 2003.
2836
link(const char *oldpath, const char *newpath)
2838
if (!CopyFile(oldpath, newpath, FALSE))
2845
* This allows zic to compile by just returning a dummy value.
2846
* localtime.c references it, but no one uses it from zic.
2849
pg_open_tzfile(const char *name, char *canonname)