2
* This file is in the public domain, so clarified as of
3
* 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov).
6
* $PostgreSQL: pgsql/src/timezone/zic.c,v 1.13 2004-09-27 19:16:03 momjian Exp $
21
#ifdef HAVE_SYS_STAT_H
27
#define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
29
#define MKDIR_UMASK 0755
33
static char elsieid[] = "@(#)zic.c 7.115";
36
* On some ancient hosts, predicates like `isspace(C)' are defined
37
* only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
38
* which says they are defined only if C == ((unsigned char) C) || C == EOF.
39
* Neither the C Standard nor Posix require that `isascii' exist.
40
* For portability, we check both ancient and modern requirements.
41
* If isascii is not defined, the isascii check succeeds trivially.
50
const char *r_filename;
54
int r_loyear; /* for example, 1986 */
55
int r_hiyear; /* for example, 1986 */
58
int r_month; /* 0..11 */
60
int r_dycode; /* see below */
64
long r_tod; /* time from midnight */
65
int r_todisstd; /* above is standard time if TRUE */
66
/* or wall clock time if FALSE */
67
int r_todisgmt; /* above is GMT if TRUE */
68
/* or local time if FALSE */
69
long r_stdoff; /* offset from standard time */
70
const char *r_abbrvar; /* variable part of abbreviation */
72
int r_todo; /* a rule to do (used in outzone) */
73
pg_time_t r_temp; /* used in outzone */
77
* r_dycode r_dayofmonth r_wday
80
#define DC_DOM 0 /* 1..31 */ /* unused */
81
#define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
82
#define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
86
const char *z_filename;
99
struct rule z_untilrule;
100
pg_time_t z_untiltime;
103
extern int link(const char *fromname, const char *toname);
104
static void addtt(pg_time_t starttime, int type);
105
static int addtype(long gmtoff, const char *abbr, int isdst,
106
int ttisstd, int ttisgmt);
107
static void leapadd(pg_time_t t, int positive, int rolling, int count);
108
static void adjleap(void);
109
static void associate(void);
110
static int ciequal(const char *ap, const char *bp);
111
static void convert(long val, char *buf);
112
static void dolink(const char *fromfile, const char *tofile);
113
static void doabbr(char *abbr, const char *format,
114
const char *letters, int isdst);
115
static void eat(const char *name, int num);
116
static void eats(const char *name, int num,
117
const char *rname, int rnum);
118
static long eitol(int i);
119
static void error(const char *message);
120
static char **getfields(char *buf);
121
static long gethms(const char *string, const char *errstrng,
123
static void infile(const char *filename);
124
static void inleap(char **fields, int nfields);
125
static void inlink(char **fields, int nfields);
126
static void inrule(char **fields, int nfields);
127
static int inzcont(char **fields, int nfields);
128
static int inzone(char **fields, int nfields);
129
static int inzsub(char **fields, int nfields, int iscont);
130
static int itsabbr(const char *abbr, const char *word);
131
static int itsdir(const char *name);
132
static int lowerit(int c);
133
static char *memcheck(char *tocheck);
134
static int mkdirs(char *filename);
135
static void newabbr(const char *abbr);
136
static long oadd(long t1, long t2);
137
static void outzone(const struct zone * zp, int ntzones);
138
static void puttzcode(long code, FILE *fp);
139
static int rcomp(const void *leftp, const void *rightp);
140
static pg_time_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 void setboundaries(void);
146
static pg_time_t tadd(pg_time_t t1, long t2);
147
static void usage(void);
148
static void writezone(const char *name);
149
static int yearistype(int year, const char *type);
153
static const char *filename;
156
static pg_time_t max_time;
158
static int max_year_representable;
159
static pg_time_t min_time;
161
static int min_year_representable;
163
static const char *rfilename;
165
static const char *progname;
179
* Which fields are which on a Zone line.
187
#define ZF_TILMONTH 6
190
#define ZONE_MINFIELDS 5
191
#define ZONE_MAXFIELDS 9
194
* Which fields are which on a Zone continuation line.
200
#define ZFC_TILYEAR 3
201
#define ZFC_TILMONTH 4
203
#define ZFC_TILTIME 6
204
#define ZONEC_MINFIELDS 3
205
#define ZONEC_MAXFIELDS 7
208
* Which files are which on a Rule line.
220
#define RULE_FIELDS 10
223
* Which fields are which on a Link line.
228
#define LINK_FIELDS 3
231
* Which fields are which on a Leap line.
240
#define LEAP_FIELDS 7
250
static struct rule *rules;
251
static int nrules; /* number of rules */
253
static struct zone *zones;
254
static int nzones; /* number of zones */
258
const char *l_filename;
264
static struct link *links;
273
static struct lookup const *byword(const char *string,
274
const struct lookup * lp);
276
static struct lookup const line_codes[] = {
284
static struct lookup const mon_names[] = {
285
{"January", TM_JANUARY},
286
{"February", TM_FEBRUARY},
292
{"August", TM_AUGUST},
293
{"September", TM_SEPTEMBER},
294
{"October", TM_OCTOBER},
295
{"November", TM_NOVEMBER},
296
{"December", TM_DECEMBER},
300
static struct lookup const wday_names[] = {
301
{"Sunday", TM_SUNDAY},
302
{"Monday", TM_MONDAY},
303
{"Tuesday", TM_TUESDAY},
304
{"Wednesday", TM_WEDNESDAY},
305
{"Thursday", TM_THURSDAY},
306
{"Friday", TM_FRIDAY},
307
{"Saturday", TM_SATURDAY},
311
static struct lookup const lasts[] = {
312
{"last-Sunday", TM_SUNDAY},
313
{"last-Monday", TM_MONDAY},
314
{"last-Tuesday", TM_TUESDAY},
315
{"last-Wednesday", TM_WEDNESDAY},
316
{"last-Thursday", TM_THURSDAY},
317
{"last-Friday", TM_FRIDAY},
318
{"last-Saturday", TM_SATURDAY},
322
static struct lookup const begin_years[] = {
323
{"minimum", YR_MINIMUM},
324
{"maximum", YR_MAXIMUM},
328
static struct lookup const end_years[] = {
329
{"minimum", YR_MINIMUM},
330
{"maximum", YR_MAXIMUM},
335
static struct lookup const leap_types[] = {
337
{"Stationary", FALSE},
341
static const int len_months[2][MONSPERYEAR] = {
342
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
343
{31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
346
static const int len_years[2] = {
347
DAYSPERNYEAR, DAYSPERLYEAR
354
} attypes[TZ_MAX_TIMES];
355
static long gmtoffs[TZ_MAX_TYPES];
356
static char isdsts[TZ_MAX_TYPES];
357
static unsigned char abbrinds[TZ_MAX_TYPES];
358
static char ttisstds[TZ_MAX_TYPES];
359
static char ttisgmts[TZ_MAX_TYPES];
360
static char chars[TZ_MAX_CHARS];
361
static pg_time_t trans[TZ_MAX_LEAPS];
362
static long corr[TZ_MAX_LEAPS];
363
static char roll[TZ_MAX_LEAPS];
374
const char *e = strerror(errno);
376
(void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
378
(void) exit(EXIT_FAILURE);
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 *name, const int num, const char *rname, const int rnum)
402
eat(const char *name, const int num)
404
eats(name, num, (char *) NULL, -1);
408
error(const char *string)
411
* Match the format of "cc" to allow sh users to zic ... 2>&1 | error
412
* -t "*" -v on BSD systems.
414
(void) fprintf(stderr, _("\"%s\", line %d: %s"),
415
filename, linenum, string);
416
if (rfilename != NULL)
417
(void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
418
rfilename, rlinenum);
419
(void) fprintf(stderr, "\n");
424
warning(const char *string)
428
cp = ecpyalloc(_("warning: "));
429
cp = ecatalloc(cp, string);
438
(void) fprintf(stderr, _("%s: usage is %s [ --version ] [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
440
(void) exit(EXIT_FAILURE);
443
static const char *psxrules;
444
static const char *lcltime;
445
static const char *directory;
446
static const char *leapsec;
447
static const char *yitcommand;
448
static int sflag = FALSE;
451
main(int argc, char *argv[])
458
(void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
461
for (i = 1; i < argc; ++i)
462
if (strcmp(argv[i], "--version") == 0)
464
(void) printf("%s\n", elsieid);
465
(void) exit(EXIT_SUCCESS);
467
while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
473
if (directory == NULL)
477
(void) fprintf(stderr,
478
_("%s: More than one -d option specified\n"),
480
(void) exit(EXIT_FAILURE);
488
(void) fprintf(stderr,
489
_("%s: More than one -l option specified\n"),
491
(void) exit(EXIT_FAILURE);
495
if (psxrules == NULL)
499
(void) fprintf(stderr,
500
_("%s: More than one -p option specified\n"),
502
(void) exit(EXIT_FAILURE);
506
if (yitcommand == NULL)
510
(void) fprintf(stderr,
511
_("%s: More than one -y option specified\n"),
513
(void) exit(EXIT_FAILURE);
521
(void) fprintf(stderr,
522
_("%s: More than one -L option specified\n"),
524
(void) exit(EXIT_FAILURE);
534
if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
535
usage(); /* usage message by request */
536
if (directory == NULL)
538
if (yitcommand == NULL)
539
yitcommand = "yearistype";
543
if (optind < argc && leapsec != NULL)
549
for (i = optind; i < argc; ++i)
552
(void) exit(EXIT_FAILURE);
554
for (i = 0; i < nzones; i = j)
557
* Find the next non-continuation zone entry.
559
for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
561
outzone(&zones[i], j - i);
567
for (i = 0; i < nlinks; ++i)
569
eat(links[i].l_filename, links[i].l_linenum);
570
dolink(links[i].l_from, links[i].l_to);
574
eat("command line", 1);
575
dolink(lcltime, TZDEFAULT);
577
if (psxrules != NULL)
579
eat("command line", 1);
580
dolink(psxrules, TZDEFRULES);
582
return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
586
dolink(const char *fromfile, const char *tofile)
588
register char *fromname;
589
register char *toname;
591
if (fromfile[0] == '/')
592
fromname = ecpyalloc(fromfile);
595
fromname = ecpyalloc(directory);
596
fromname = ecatalloc(fromname, "/");
597
fromname = ecatalloc(fromname, fromfile);
599
if (tofile[0] == '/')
600
toname = ecpyalloc(tofile);
603
toname = ecpyalloc(directory);
604
toname = ecatalloc(toname, "/");
605
toname = ecatalloc(toname, tofile);
609
* We get to be careful here since there's a fair chance of root
613
(void) remove(toname);
614
if (link(fromname, toname) != 0)
618
if (mkdirs(toname) != 0)
619
(void) exit(EXIT_FAILURE);
621
result = link(fromname, toname);
624
access(fromname, F_OK) == 0 &&
627
const char *s = tofile;
628
register char *symlinkcontents = NULL;
630
while ((s = strchr(s + 1, '/')) != NULL)
631
symlinkcontents = ecatalloc(symlinkcontents, "../");
632
symlinkcontents = ecatalloc(symlinkcontents, fromfile);
634
result = symlink(symlinkcontents, toname);
636
warning(_("hard link failed, symbolic link used"));
637
ifree(symlinkcontents);
642
const char *e = strerror(errno);
644
(void) fprintf(stderr,
645
_("%s: Can't link from %s to %s: %s\n"),
646
progname, fromname, toname, e);
647
(void) exit(EXIT_FAILURE);
655
#define INT_MAX ((int) (((unsigned)~0)>>1))
656
#endif /* !defined INT_MAX */
659
#define INT_MIN ((int) ~(((unsigned)~0)>>1))
660
#endif /* !defined INT_MIN */
663
* The tz file format currently allows at most 32-bit quantities.
664
* This restriction should be removed before signed 32-bit values
665
* wrap around in 2038, but unfortunately this will require a
666
* change to the tz file format.
669
#define TIME_T_BITS_IN_FILE 32
675
* pg_time_t is always signed, but might be only 32 bits ...
677
min_time = ~(pg_time_t) 0;
678
min_time <<= TYPE_BIT(pg_time_t) -1;
679
max_time = ~(pg_time_t) 0 - min_time;
682
* For the moment, hard-wire the range as 1901 to 2038. We cannot go
683
* wider without adopting an incompatible zone file format, which is a
684
* step I'd just as soon not take just yet.
686
min_time = Max(min_time, (pg_time_t) INT_MIN);
687
max_time = Min(max_time, (pg_time_t) INT_MAX);
689
min_year = TM_YEAR_BASE + pg_gmtime(&min_time)->tm_year;
690
max_year = TM_YEAR_BASE + pg_gmtime(&max_time)->tm_year;
691
min_year_representable = min_year;
692
max_year_representable = max_year;
696
itsdir(const char *name)
698
register char *myname;
701
myname = ecpyalloc(name);
702
myname = ecatalloc(myname, "/.");
703
accres = access(myname, F_OK);
709
* Associate sets of rules with zones.
717
rcomp(const void *cp1, const void *cp2)
719
return strcmp(((const struct rule *) cp1)->r_name,
720
((const struct rule *) cp2)->r_name);
726
register struct zone *zp;
727
register struct rule *rp;
735
(void) qsort((void *) rules, (size_t) nrules,
736
(size_t) sizeof *rules, rcomp);
737
for (i = 0; i < nrules - 1; ++i)
739
if (strcmp(rules[i].r_name,
740
rules[i + 1].r_name) != 0)
742
if (strcmp(rules[i].r_filename,
743
rules[i + 1].r_filename) == 0)
745
eat(rules[i].r_filename, rules[i].r_linenum);
746
warning(_("same rule name in multiple files"));
747
eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
748
warning(_("same rule name in multiple files"));
749
for (j = i + 2; j < nrules; ++j)
751
if (strcmp(rules[i].r_name,
752
rules[j].r_name) != 0)
754
if (strcmp(rules[i].r_filename,
755
rules[j].r_filename) == 0)
757
if (strcmp(rules[i + 1].r_filename,
758
rules[j].r_filename) == 0)
765
for (i = 0; i < nzones; ++i)
771
for (base = 0; base < nrules; base = out)
774
for (out = base + 1; out < nrules; ++out)
775
if (strcmp(rp->r_name, rules[out].r_name) != 0)
777
for (i = 0; i < nzones; ++i)
780
if (strcmp(zp->z_rule, rp->r_name) != 0)
783
zp->z_nrules = out - base;
786
for (i = 0; i < nzones; ++i)
789
if (zp->z_nrules == 0)
792
* Maybe we have a local standard time offset.
794
eat(zp->z_filename, zp->z_linenum);
795
zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
799
* Note, though, that if there's no rule, a '%s' in the format
802
if (strchr(zp->z_format, '%') != 0)
803
error(_("%s in ruleless zone"));
807
(void) exit(EXIT_FAILURE);
811
infile(const char *name)
814
register char **fields;
816
register const struct lookup *lp;
817
register int nfields;
818
register int wantcont;
822
if (strcmp(name, "-") == 0)
824
name = _("standard input");
827
else if ((fp = fopen(name, "r")) == NULL)
829
const char *e = strerror(errno);
831
(void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
833
(void) exit(EXIT_FAILURE);
836
for (num = 1;; ++num)
839
if (fgets(buf, (int) sizeof buf, fp) != buf)
841
cp = strchr(buf, '\n');
844
error(_("line too long"));
845
(void) exit(EXIT_FAILURE);
848
fields = getfields(buf);
850
while (fields[nfields] != NULL)
854
if (strcmp(fields[nfields], "-") == 0)
855
fields[nfields] = &nada;
863
wantcont = inzcont(fields, nfields);
866
lp = byword(fields[0], line_codes);
868
error(_("input line of unknown type"));
870
switch ((int) (lp->l_value))
873
inrule(fields, nfields);
877
wantcont = inzone(fields, nfields);
880
inlink(fields, nfields);
885
(void) fprintf(stderr,
886
_("%s: Leap line in non leap seconds file %s\n"),
889
inleap(fields, nfields);
892
default: /* "cannot happen" */
893
(void) fprintf(stderr,
894
_("%s: panic: Invalid l_value %d\n"),
895
progname, lp->l_value);
896
(void) exit(EXIT_FAILURE);
899
ifree((char *) fields);
903
(void) fprintf(stderr, _("%s: Error reading %s\n"),
905
(void) exit(EXIT_FAILURE);
907
if (fp != stdin && fclose(fp))
909
const char *e = strerror(errno);
911
(void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
912
progname, filename, e);
913
(void) exit(EXIT_FAILURE);
916
error(_("expected continuation line not found"));
920
* Convert a string of one of the forms
921
* h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
922
* into a number of seconds.
923
* A null string maps to zero.
924
* Call error with errstring and return zero on errors.
928
gethms(const char *string, const char *errstring, const int signable)
935
if (string == NULL || *string == '\0')
939
else if (*string == '-')
946
if (sscanf(string, scheck(string, "%d"), &hh) == 1)
948
else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
950
else if (sscanf(string, scheck(string, "%d:%d:%d"),
956
if ((hh < 0 || hh >= HOURSPERDAY ||
957
mm < 0 || mm >= MINSPERHOUR ||
958
ss < 0 || ss > SECSPERMIN) &&
959
!(hh == HOURSPERDAY && mm == 0 && ss == 0))
964
if (noise && hh == HOURSPERDAY)
965
warning(_("24:00 not handled by pre-1998 versions of zic"));
967
(eitol(hh * MINSPERHOUR + mm) *
968
eitol(SECSPERMIN) + eitol(ss));
972
inrule(register char **fields, const int nfields)
974
static struct rule r;
976
if (nfields != RULE_FIELDS)
978
error(_("wrong number of fields on Rule line"));
981
if (*fields[RF_NAME] == '\0')
983
error(_("nameless rule"));
986
r.r_filename = filename;
987
r.r_linenum = linenum;
988
r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
989
rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
990
fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
991
r.r_name = ecpyalloc(fields[RF_NAME]);
992
r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
993
rules = (struct rule *) (void *) erealloc((char *) rules,
994
(int) ((nrules + 1) * sizeof *rules));
999
inzone(register char **fields, const int nfields)
1004
if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
1006
error(_("wrong number of fields on Zone line"));
1009
if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL)
1011
buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1013
_("\"Zone %s\" line and -l option are mutually exclusive"),
1018
if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
1020
buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1022
_("\"Zone %s\" line and -p option are mutually exclusive"),
1027
for (i = 0; i < nzones; ++i)
1028
if (zones[i].z_name != NULL &&
1029
strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
1031
buf = erealloc(buf, (int) (132 +
1032
strlen(fields[ZF_NAME]) +
1033
strlen(zones[i].z_filename)));
1035
_("duplicate zone name %s (file \"%s\", line %d)"),
1037
zones[i].z_filename,
1038
zones[i].z_linenum);
1042
return inzsub(fields, nfields, FALSE);
1046
inzcont(register char **fields, const int nfields)
1048
if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
1050
error(_("wrong number of fields on Zone continuation line"));
1053
return inzsub(fields, nfields, TRUE);
1057
inzsub(register char **fields, const int nfields, const int iscont)
1060
static struct zone z;
1061
register int i_gmtoff,
1064
register int i_untilyear,
1066
register int i_untilday,
1068
register int hasuntil;
1072
i_gmtoff = ZFC_GMTOFF;
1074
i_format = ZFC_FORMAT;
1075
i_untilyear = ZFC_TILYEAR;
1076
i_untilmonth = ZFC_TILMONTH;
1077
i_untilday = ZFC_TILDAY;
1078
i_untiltime = ZFC_TILTIME;
1083
i_gmtoff = ZF_GMTOFF;
1085
i_format = ZF_FORMAT;
1086
i_untilyear = ZF_TILYEAR;
1087
i_untilmonth = ZF_TILMONTH;
1088
i_untilday = ZF_TILDAY;
1089
i_untiltime = ZF_TILTIME;
1090
z.z_name = ecpyalloc(fields[ZF_NAME]);
1092
z.z_filename = filename;
1093
z.z_linenum = linenum;
1094
z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1095
if ((cp = strchr(fields[i_format], '%')) != 0)
1097
if (*++cp != 's' || strchr(cp, '%') != 0)
1099
error(_("invalid abbreviation format"));
1103
z.z_rule = ecpyalloc(fields[i_rule]);
1104
z.z_format = ecpyalloc(fields[i_format]);
1105
hasuntil = nfields > i_untilyear;
1108
z.z_untilrule.r_filename = filename;
1109
z.z_untilrule.r_linenum = linenum;
1110
rulesub(&z.z_untilrule,
1111
fields[i_untilyear],
1114
(nfields > i_untilmonth) ?
1115
fields[i_untilmonth] : "Jan",
1116
(nfields > i_untilday) ? fields[i_untilday] : "1",
1117
(nfields > i_untiltime) ? fields[i_untiltime] : "0");
1118
z.z_untiltime = rpytime(&z.z_untilrule,
1119
z.z_untilrule.r_loyear);
1120
if (iscont && nzones > 0 &&
1121
z.z_untiltime > min_time &&
1122
z.z_untiltime < max_time &&
1123
zones[nzones - 1].z_untiltime > min_time &&
1124
zones[nzones - 1].z_untiltime < max_time &&
1125
zones[nzones - 1].z_untiltime >= z.z_untiltime)
1127
error(_("Zone continuation line end time is not after end time of previous line"));
1131
zones = (struct zone *) (void *) erealloc((char *) zones,
1132
(int) ((nzones + 1) * sizeof *zones));
1133
zones[nzones++] = z;
1136
* If there was an UNTIL field on this line, there's more information
1137
* about the zone on the next line.
1143
inleap(register char **fields, const int nfields)
1145
register const char *cp;
1146
register const struct lookup *lp;
1156
if (nfields != LEAP_FIELDS)
1158
error(_("wrong number of fields on Leap line"));
1162
cp = fields[LP_YEAR];
1163
if (sscanf(cp, scheck(cp, "%d"), &year) != 1)
1168
error(_("invalid leaping year"));
1176
i = len_years[isleap(j)];
1182
i = -len_years[isleap(j)];
1184
dayoff = oadd(dayoff, eitol(i));
1186
if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
1188
error(_("invalid month name"));
1191
month = lp->l_value;
1195
i = len_months[isleap(year)][j];
1196
dayoff = oadd(dayoff, eitol(i));
1199
cp = fields[LP_DAY];
1200
if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1201
day <= 0 || day > len_months[isleap(year)][month])
1203
error(_("invalid day of month"));
1206
dayoff = oadd(dayoff, eitol(day - 1));
1207
if (dayoff < min_time / SECSPERDAY)
1209
error(_("time too small"));
1212
if (dayoff > max_time / SECSPERDAY)
1214
error(_("time too large"));
1217
t = (pg_time_t) dayoff *SECSPERDAY;
1219
tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1220
cp = fields[LP_CORR];
1222
register int positive;
1225
if (strcmp(cp, "") == 0)
1226
{ /* infile() turns "-" into "" */
1230
else if (strcmp(cp, "--") == 0)
1235
else if (strcmp(cp, "+") == 0)
1240
else if (strcmp(cp, "++") == 0)
1247
error(_("illegal CORRECTION field on Leap line"));
1250
if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL)
1252
error(_("illegal Rolling/Stationary field on Leap line"));
1255
leapadd(tadd(t, tod), positive, lp->l_value, count);
1260
inlink(register char **fields, const int nfields)
1264
if (nfields != LINK_FIELDS)
1266
error(_("wrong number of fields on Link line"));
1269
if (*fields[LF_FROM] == '\0')
1271
error(_("blank FROM field on Link line"));
1274
if (*fields[LF_TO] == '\0')
1276
error(_("blank TO field on Link line"));
1279
l.l_filename = filename;
1280
l.l_linenum = linenum;
1281
l.l_from = ecpyalloc(fields[LF_FROM]);
1282
l.l_to = ecpyalloc(fields[LF_TO]);
1283
links = (struct link *) (void *) erealloc((char *) links,
1284
(int) ((nlinks + 1) * sizeof *links));
1285
links[nlinks++] = l;
1289
rulesub(register struct rule * rp, const char *loyearp, const char *hiyearp,
1290
const char *typep, const char *monthp, const char *dayp,
1293
register const struct lookup *lp;
1294
register const char *cp;
1298
if ((lp = byword(monthp, mon_names)) == NULL)
1300
error(_("invalid month name"));
1303
rp->r_month = lp->l_value;
1304
rp->r_todisstd = FALSE;
1305
rp->r_todisgmt = FALSE;
1306
dp = ecpyalloc(timep);
1309
ep = dp + strlen(dp) - 1;
1310
switch (lowerit(*ep))
1312
case 's': /* Standard */
1313
rp->r_todisstd = TRUE;
1314
rp->r_todisgmt = FALSE;
1317
case 'w': /* Wall */
1318
rp->r_todisstd = FALSE;
1319
rp->r_todisgmt = FALSE;
1322
case 'g': /* Greenwich */
1323
case 'u': /* Universal */
1324
case 'z': /* Zulu */
1325
rp->r_todisstd = TRUE;
1326
rp->r_todisgmt = TRUE;
1331
rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1338
lp = byword(cp, begin_years);
1340
switch ((int) lp->l_value)
1343
rp->r_loyear = INT_MIN;
1346
rp->r_loyear = INT_MAX;
1348
default: /* "cannot happen" */
1349
(void) fprintf(stderr,
1350
_("%s: panic: Invalid l_value %d\n"),
1351
progname, lp->l_value);
1352
(void) exit(EXIT_FAILURE);
1354
else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1)
1356
error(_("invalid starting year"));
1361
if (rp->r_loyear < min_year_representable)
1362
warning(_("starting year too low to be represented"));
1363
else if (rp->r_loyear > max_year_representable)
1364
warning(_("starting year too high to be represented"));
1367
if ((lp = byword(cp, end_years)) != NULL)
1368
switch ((int) lp->l_value)
1371
rp->r_hiyear = INT_MIN;
1374
rp->r_hiyear = INT_MAX;
1377
rp->r_hiyear = rp->r_loyear;
1379
default: /* "cannot happen" */
1380
(void) fprintf(stderr,
1381
_("%s: panic: Invalid l_value %d\n"),
1382
progname, lp->l_value);
1383
(void) exit(EXIT_FAILURE);
1385
else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1)
1387
error(_("invalid ending year"));
1392
if (rp->r_loyear < min_year_representable)
1393
warning(_("ending year too low to be represented"));
1394
else if (rp->r_loyear > max_year_representable)
1395
warning(_("ending year too high to be represented"));
1397
if (rp->r_loyear > rp->r_hiyear)
1399
error(_("starting year greater than ending year"));
1403
rp->r_yrtype = NULL;
1406
if (rp->r_loyear == rp->r_hiyear)
1408
error(_("typed single year"));
1411
rp->r_yrtype = ecpyalloc(typep);
1413
if (rp->r_loyear < min_year && rp->r_loyear > 0)
1414
min_year = rp->r_loyear;
1417
* Day work. Accept things such as: 1 last-Sunday Sun<=20 Sun>=7
1419
dp = ecpyalloc(dayp);
1420
if ((lp = byword(dp, lasts)) != NULL)
1422
rp->r_dycode = DC_DOWLEQ;
1423
rp->r_wday = lp->l_value;
1424
rp->r_dayofmonth = len_months[1][rp->r_month];
1428
if ((ep = strchr(dp, '<')) != 0)
1429
rp->r_dycode = DC_DOWLEQ;
1430
else if ((ep = strchr(dp, '>')) != 0)
1431
rp->r_dycode = DC_DOWGEQ;
1435
rp->r_dycode = DC_DOM;
1437
if (rp->r_dycode != DC_DOM)
1442
error(_("invalid day of month"));
1446
if ((lp = byword(dp, wday_names)) == NULL)
1448
error(_("invalid weekday name"));
1452
rp->r_wday = lp->l_value;
1454
if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1455
rp->r_dayofmonth <= 0 ||
1456
(rp->r_dayofmonth > len_months[1][rp->r_month]))
1458
error(_("invalid day of month"));
1467
convert(const long val, char *buf)
1470
register long shift;
1472
for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1473
buf[i] = val >> shift;
1477
puttzcode(const long val, FILE *fp)
1482
(void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1486
atcomp(const void *avp, const void *bvp)
1488
if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1490
else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1497
writezone(const char *name)
1502
static char *fullname;
1503
static struct tzhead tzh;
1504
pg_time_t ats[TZ_MAX_TIMES];
1505
unsigned char types[TZ_MAX_TIMES];
1511
(void) qsort((void *) attypes, (size_t) timecnt,
1512
(size_t) sizeof *attypes, atcomp);
1523
while (fromi < timecnt && attypes[fromi].at < min_time)
1526
while (fromi < timecnt && attypes[fromi].type == 0)
1527
++fromi; /* handled by default rule */
1528
for (; fromi < timecnt; ++fromi)
1531
&& ((attypes[fromi].at
1532
+ gmtoffs[attypes[toi - 1].type])
1533
<= (attypes[toi - 1].at
1534
+ gmtoffs[toi == 1 ? 0
1535
: attypes[toi - 2].type])))
1537
attypes[toi - 1].type = attypes[fromi].type;
1541
attypes[toi - 1].type != attypes[fromi].type)
1542
attypes[toi++] = attypes[fromi];
1550
for (i = 0; i < timecnt; ++i)
1552
ats[i] = attypes[i].at;
1553
types[i] = attypes[i].type;
1555
fullname = erealloc(fullname,
1556
(int) (strlen(directory) + 1 + strlen(name) + 1));
1557
(void) sprintf(fullname, "%s/%s", directory, name);
1560
* Remove old file, if any, to snap links.
1562
if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT)
1564
const char *e = strerror(errno);
1566
(void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1567
progname, fullname, e);
1568
(void) exit(EXIT_FAILURE);
1570
if ((fp = fopen(fullname, "wb")) == NULL)
1572
if (mkdirs(fullname) != 0)
1573
(void) exit(EXIT_FAILURE);
1574
if ((fp = fopen(fullname, "wb")) == NULL)
1576
const char *e = strerror(errno);
1578
(void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1579
progname, fullname, e);
1580
(void) exit(EXIT_FAILURE);
1583
convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1584
convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1585
convert(eitol(leapcnt), tzh.tzh_leapcnt);
1586
convert(eitol(timecnt), tzh.tzh_timecnt);
1587
convert(eitol(typecnt), tzh.tzh_typecnt);
1588
convert(eitol(charcnt), tzh.tzh_charcnt);
1589
(void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1590
#define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1600
for (i = 0; i < timecnt; ++i)
1604
if (ats[i] >= trans[j])
1606
ats[i] = tadd(ats[i], corr[j]);
1609
puttzcode((long) ats[i], fp);
1612
(void) fwrite((void *) types, (size_t) sizeof types[0],
1613
(size_t) timecnt, fp);
1614
for (i = 0; i < typecnt; ++i)
1616
puttzcode((long) gmtoffs[i], fp);
1617
(void) putc(isdsts[i], fp);
1618
(void) putc(abbrinds[i], fp);
1621
(void) fwrite((void *) chars, (size_t) sizeof chars[0],
1622
(size_t) charcnt, fp);
1623
for (i = 0; i < leapcnt; ++i)
1627
if (timecnt == 0 || trans[i] < ats[0])
1640
while (j < timecnt && trans[i] >= ats[j])
1644
puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1647
puttzcode((long) trans[i], fp);
1648
puttzcode((long) corr[i], fp);
1650
for (i = 0; i < typecnt; ++i)
1651
(void) putc(ttisstds[i], fp);
1652
for (i = 0; i < typecnt; ++i)
1653
(void) putc(ttisgmts[i], fp);
1654
if (ferror(fp) || fclose(fp))
1656
(void) fprintf(stderr, _("%s: Error writing %s\n"),
1657
progname, fullname);
1658
(void) exit(EXIT_FAILURE);
1663
doabbr(char *abbr, const char *format, const char *letters, const int isdst)
1665
if (strchr(format, '/') == NULL)
1667
if (letters == NULL)
1668
(void) strcpy(abbr, format);
1670
(void) sprintf(abbr, format, letters);
1673
(void) strcpy(abbr, strchr(format, '/') + 1);
1676
(void) strcpy(abbr, format);
1677
*strchr(abbr, '/') = '\0';
1682
outzone(const struct zone * zpfirst, const int zonecount)
1684
register const struct zone *zp;
1685
register struct rule *rp;
1688
register int usestart,
1690
register pg_time_t starttime = 0;
1691
register pg_time_t untiltime = 0;
1692
register long gmtoff;
1693
register long stdoff;
1695
register long startoff;
1696
register int startttisstd;
1697
register int startttisgmt;
1699
char startbuf[BUFSIZ];
1702
* Now. . .finally. . .generate some useful data!
1709
* Thanks to Earl Chew (earl@dnd.icp.nec.com.au) for noting the need
1710
* to unconditionally initialize startttisstd.
1712
startttisstd = FALSE;
1713
startttisgmt = FALSE;
1714
for (i = 0; i < zonecount; ++i)
1717
* A guess that may well be corrected later.
1721
usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1722
useuntil = i < (zonecount - 1);
1723
if (useuntil && zp->z_untiltime <= min_time)
1725
gmtoff = zp->z_gmtoff;
1726
eat(zp->z_filename, zp->z_linenum);
1728
startoff = zp->z_gmtoff;
1729
if (zp->z_nrules == 0)
1731
stdoff = zp->z_stdoff;
1732
doabbr(startbuf, zp->z_format,
1733
(char *) NULL, stdoff != 0);
1734
type = addtype(oadd(zp->z_gmtoff, stdoff),
1735
startbuf, stdoff != 0, startttisstd,
1739
addtt(starttime, type);
1742
else if (stdoff != 0)
1743
addtt(min_time, type);
1746
for (year = min_year; year <= max_year; ++year)
1748
if (useuntil && year > zp->z_untilrule.r_hiyear)
1752
* Mark which rules to do in the current year. For those
1753
* to do, calculate rpytime(rp, year);
1755
for (j = 0; j < zp->z_nrules; ++j)
1757
rp = &zp->z_rules[j];
1758
eats(zp->z_filename, zp->z_linenum,
1759
rp->r_filename, rp->r_linenum);
1760
rp->r_todo = year >= rp->r_loyear &&
1761
year <= rp->r_hiyear &&
1762
yearistype(year, rp->r_yrtype);
1764
rp->r_temp = rpytime(rp, year);
1769
register pg_time_t jtime,
1771
register long offset;
1777
* Turn untiltime into UTC assuming the current
1778
* gmtoff and stdoff values.
1780
untiltime = zp->z_untiltime;
1781
if (!zp->z_untilrule.r_todisgmt)
1782
untiltime = tadd(untiltime,
1784
if (!zp->z_untilrule.r_todisstd)
1785
untiltime = tadd(untiltime,
1790
* Find the rule (of those to do, if any) that takes
1791
* effect earliest in the year.
1794
for (j = 0; j < zp->z_nrules; ++j)
1796
rp = &zp->z_rules[j];
1799
eats(zp->z_filename, zp->z_linenum,
1800
rp->r_filename, rp->r_linenum);
1801
offset = rp->r_todisgmt ? 0 : gmtoff;
1802
if (!rp->r_todisstd)
1803
offset = oadd(offset, stdoff);
1805
if (jtime == min_time ||
1808
jtime = tadd(jtime, -offset);
1809
if (k < 0 || jtime < ktime)
1816
break; /* go on to next year */
1817
rp = &zp->z_rules[k];
1819
if (useuntil && ktime >= untiltime)
1821
stdoff = rp->r_stdoff;
1822
if (usestart && ktime == starttime)
1826
if (ktime < starttime)
1828
startoff = oadd(zp->z_gmtoff,
1830
doabbr(startbuf, zp->z_format,
1835
if (*startbuf == '\0' &&
1836
startoff == oadd(zp->z_gmtoff,
1839
doabbr(startbuf, zp->z_format,
1844
eats(zp->z_filename, zp->z_linenum,
1845
rp->r_filename, rp->r_linenum);
1846
doabbr(buf, zp->z_format, rp->r_abbrvar,
1848
offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1849
type = addtype(offset, buf, rp->r_stdoff != 0,
1850
rp->r_todisstd, rp->r_todisgmt);
1856
if (*startbuf == '\0' &&
1857
zp->z_format != NULL &&
1858
strchr(zp->z_format, '%') == NULL &&
1859
strchr(zp->z_format, '/') == NULL)
1860
(void) strcpy(startbuf, zp->z_format);
1861
eat(zp->z_filename, zp->z_linenum);
1862
if (*startbuf == '\0')
1863
error(_("can't determine time zone abbreviation to use just after until time"));
1866
addtype(startoff, startbuf,
1867
startoff != zp->z_gmtoff,
1873
* Now we may get to set starttime for the next zone line.
1877
startttisstd = zp->z_untilrule.r_todisstd;
1878
startttisgmt = zp->z_untilrule.r_todisgmt;
1879
starttime = zp->z_untiltime;
1881
starttime = tadd(starttime, -stdoff);
1883
starttime = tadd(starttime, -gmtoff);
1886
writezone(zpfirst->z_name);
1890
addtt(const pg_time_t starttime, int type)
1892
if (starttime <= min_time ||
1893
(timecnt == 1 && attypes[0].at < min_time))
1895
gmtoffs[0] = gmtoffs[type];
1896
isdsts[0] = isdsts[type];
1897
ttisstds[0] = ttisstds[type];
1898
ttisgmts[0] = ttisgmts[type];
1899
if (abbrinds[type] != 0)
1900
(void) strcpy(chars, &chars[abbrinds[type]]);
1902
charcnt = strlen(chars) + 1;
1907
if (timecnt >= TZ_MAX_TIMES)
1909
error(_("too many transitions?!"));
1910
(void) exit(EXIT_FAILURE);
1912
attypes[timecnt].at = starttime;
1913
attypes[timecnt].type = type;
1918
addtype(const long gmtoff, const char *abbr, const int isdst,
1919
const int ttisstd, const int ttisgmt)
1924
if (isdst != TRUE && isdst != FALSE)
1926
error(_("internal error - addtype called with bad isdst"));
1927
(void) exit(EXIT_FAILURE);
1929
if (ttisstd != TRUE && ttisstd != FALSE)
1931
error(_("internal error - addtype called with bad ttisstd"));
1932
(void) exit(EXIT_FAILURE);
1934
if (ttisgmt != TRUE && ttisgmt != FALSE)
1936
error(_("internal error - addtype called with bad ttisgmt"));
1937
(void) exit(EXIT_FAILURE);
1941
* See if there's already an entry for this zone type. If so, just
1944
for (i = 0; i < typecnt; ++i)
1946
if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1947
strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1948
ttisstd == ttisstds[i] &&
1949
ttisgmt == ttisgmts[i])
1954
* There isn't one; add a new one, unless there are already too many.
1956
if (typecnt >= TZ_MAX_TYPES)
1958
error(_("too many local time types"));
1959
(void) exit(EXIT_FAILURE);
1961
gmtoffs[i] = gmtoff;
1963
ttisstds[i] = ttisstd;
1964
ttisgmts[i] = ttisgmt;
1966
for (j = 0; j < charcnt; ++j)
1967
if (strcmp(&chars[j], abbr) == 0)
1977
leapadd(const pg_time_t t, const int positive, const int rolling, int count)
1982
if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS)
1984
error(_("too many leap seconds"));
1985
(void) exit(EXIT_FAILURE);
1987
for (i = 0; i < leapcnt; ++i)
1992
error(_("repeated leap second moment"));
1993
(void) exit(EXIT_FAILURE);
1999
for (j = leapcnt; j > i; --j)
2001
trans[j] = trans[j - 1];
2002
corr[j] = corr[j - 1];
2003
roll[j] = roll[j - 1];
2006
corr[i] = positive ? 1L : eitol(-count);
2009
} while (positive && --count != 0);
2016
register long last = 0;
2019
* propagate leap seconds forward
2021
for (i = 0; i < leapcnt; ++i)
2023
trans[i] = tadd(trans[i], last);
2024
last = corr[i] += last;
2029
yearistype(const int year, const char *type)
2034
if (type == NULL || *type == '\0')
2036
buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2037
(void) sprintf(buf, "%s %d %s", yitcommand, year, type);
2038
result = system(buf);
2039
if (WIFEXITED(result))
2040
switch (WEXITSTATUS(result))
2047
error(_("Wild result from command execution"));
2048
(void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2049
progname, buf, result);
2051
(void) exit(EXIT_FAILURE);
2057
a = (unsigned char) a;
2058
return (isascii(a) && isupper(a)) ? tolower(a) : a;
2062
ciequal(register const char *ap, register const char *bp)
2064
while (lowerit(*ap) == lowerit(*bp++))
2071
itsabbr(register const char *abbr, register const char *word)
2073
if (lowerit(*abbr) != lowerit(*word))
2076
while (*++abbr != '\0')
2081
} while (lowerit(*word++) != lowerit(*abbr));
2085
static const struct lookup *
2086
byword(register const char *word, register const struct lookup * table)
2088
register const struct lookup *foundlp;
2089
register const struct lookup *lp;
2091
if (word == NULL || table == NULL)
2095
* Look for exact match.
2097
for (lp = table; lp->l_word != NULL; ++lp)
2098
if (ciequal(word, lp->l_word))
2102
* Look for inexact match.
2105
for (lp = table; lp->l_word != NULL; ++lp)
2106
if (itsabbr(word, lp->l_word))
2108
if (foundlp == NULL)
2111
return NULL; /* multiple inexact matches */
2117
getfields(register char *cp)
2120
register char **array;
2125
array = (char **) (void *)
2126
emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2130
while (isascii(*cp) && isspace((unsigned char) *cp))
2132
if (*cp == '\0' || *cp == '#')
2134
array[nsubs++] = dp = cp;
2137
if ((*dp = *cp++) != '"')
2140
while ((*dp = *cp++) != '"')
2144
error(_("Odd number of quotation marks"));
2145
} while (*cp != '\0' && *cp != '#' &&
2146
(!isascii(*cp) || !isspace((unsigned char) *cp)));
2147
if (isascii(*cp) && isspace((unsigned char) *cp))
2151
array[nsubs] = NULL;
2156
oadd(const long t1, const long t2)
2161
if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
2163
error(_("time overflow"));
2164
(void) exit(EXIT_FAILURE);
2170
tadd(const pg_time_t t1, const long t2)
2172
register pg_time_t t;
2174
if (t1 == max_time && t2 > 0)
2176
if (t1 == min_time && t2 < 0)
2179
if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1))
2181
error(_("time overflow"));
2182
(void) exit(EXIT_FAILURE);
2188
* Given a rule, and a year, compute the date - in seconds since January 1,
2189
* 1970, 00:00 LOCAL time - in that year that the rule refers to.
2193
rpytime(register const struct rule * rp, register const int wantedy)
2198
register long dayoff; /* with a nod to Margaret O. */
2199
register pg_time_t t;
2201
if (wantedy == INT_MIN)
2203
if (wantedy == INT_MAX)
2208
while (wantedy != y)
2212
i = len_years[isleap(y)];
2218
i = -len_years[isleap(y)];
2220
dayoff = oadd(dayoff, eitol(i));
2222
while (m != rp->r_month)
2224
i = len_months[isleap(y)][m];
2225
dayoff = oadd(dayoff, eitol(i));
2228
i = rp->r_dayofmonth;
2229
if (m == TM_FEBRUARY && i == 29 && !isleap(y))
2231
if (rp->r_dycode == DC_DOWLEQ)
2235
error(_("use of 2/29 in non leap-year"));
2236
(void) exit(EXIT_FAILURE);
2240
dayoff = oadd(dayoff, eitol(i));
2241
if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
2245
#define LDAYSPERWEEK ((long) DAYSPERWEEK)
2246
wday = eitol(EPOCH_WDAY);
2249
* Don't trust mod of negative numbers.
2252
wday = (wday + dayoff) % LDAYSPERWEEK;
2255
wday -= ((-dayoff) % LDAYSPERWEEK);
2257
wday += LDAYSPERWEEK;
2259
while (wday != eitol(rp->r_wday))
2260
if (rp->r_dycode == DC_DOWGEQ)
2262
dayoff = oadd(dayoff, (long) 1);
2263
if (++wday >= LDAYSPERWEEK)
2269
dayoff = oadd(dayoff, (long) -1);
2271
wday = LDAYSPERWEEK - 1;
2274
if (i < 0 || i >= len_months[isleap(y)][m])
2276
error(_("no day in month matches rule"));
2277
(void) exit(EXIT_FAILURE);
2280
if (dayoff < min_time / SECSPERDAY)
2282
if (dayoff > max_time / SECSPERDAY)
2284
t = (pg_time_t) dayoff *SECSPERDAY;
2286
return tadd(t, rp->r_tod);
2290
newabbr(const char *string)
2294
i = strlen(string) + 1;
2295
if (charcnt + i > TZ_MAX_CHARS)
2297
error(_("too many, or too long, time zone abbreviations"));
2298
(void) exit(EXIT_FAILURE);
2300
(void) strcpy(&chars[charcnt], string);
2301
charcnt += eitol(i);
2305
mkdirs(char *argname)
2307
register char *name;
2310
if (argname == NULL || *argname == '\0')
2312
cp = name = ecpyalloc(argname);
2313
while ((cp = strchr(cp + 1, '/')) != 0)
2319
* DOS drive specifier?
2321
if (isalpha((unsigned char) name[0]) &&
2322
name[1] == ':' && name[2] == '\0')
2331
* It doesn't seem to exist, so we try to create it. Creation
2332
* may fail because of the directory being created by some
2333
* other multiprocessor, so we get to do extra checking.
2335
if (mkdir(name, MKDIR_UMASK) != 0)
2337
const char *e = strerror(errno);
2339
if (errno != EEXIST || !itsdir(name))
2341
(void) fprintf(stderr,
2342
_("%s: Can't create directory %s: %s\n"),
2361
if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0))
2363
(void) fprintf(stderr,
2364
_("%s: %d did not sign extend correctly\n"),
2366
(void) exit(EXIT_FAILURE);
2372
* UNIX was a registered trademark of The Open Group in 2003.
2381
link(const char *oldpath, const char *newpath)
2383
if (!CopyFileEx(oldpath, newpath, NULL, NULL, FALSE, 0))
2390
* This allows zic to compile by just assigning a dummy value.
2391
* localtime.c references it, but no one uses it from zic.