zic.c revision 1.25 1 /* $NetBSD: zic.c,v 1.25 2009/12/31 22:49:16 mlelstv Exp $ */
2 /*
3 ** This file is in the public domain, so clarified as of
4 ** 2006-07-17 by Arthur David Olson.
5 */
6
7 #include <sys/cdefs.h>
8 #ifndef lint
9 #ifndef NOID
10 __RCSID("$NetBSD: zic.c,v 1.25 2009/12/31 22:49:16 mlelstv Exp $");
11 #endif /* !defined NOID */
12 #endif /* !defined lint */
13
14 static char elsieid[] = "@(#)zic.c 8.20";
15
16 #include "private.h"
17 #include "locale.h"
18 #include "tzfile.h"
19
20 #define ZIC_VERSION '2'
21
22 typedef int_fast64_t zic_t;
23
24 #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
25 #define ZIC_MAX_ABBR_LEN_WO_WARN 6
26 #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
27
28 #if HAVE_SYS_STAT_H
29 #include "sys/stat.h"
30 #endif
31 #ifdef S_IRUSR
32 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
33 #else
34 #define MKDIR_UMASK 0755
35 #endif
36
37 #include "unistd.h"
38
39 /*
40 ** On some ancient hosts, predicates like `isspace(C)' are defined
41 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
42 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
43 ** Neither the C Standard nor Posix require that `isascii' exist.
44 ** For portability, we check both ancient and modern requirements.
45 ** If isascii is not defined, the isascii check succeeds trivially.
46 */
47 #include "ctype.h"
48 #ifndef isascii
49 #define isascii(x) 1
50 #endif
51
52 #define OFFSET_STRLEN_MAXIMUM (7 + INT_STRLEN_MAXIMUM(long))
53 #define RULE_STRLEN_MAXIMUM 8 /* "Mdd.dd.d" */
54
55 #define end(cp) (strchr((cp), '\0'))
56
57 struct rule {
58 const char * r_filename;
59 int r_linenum;
60 const char * r_name;
61
62 int r_loyear; /* for example, 1986 */
63 int r_hiyear; /* for example, 1986 */
64 const char * r_yrtype;
65 int r_lowasnum;
66 int r_hiwasnum;
67
68 int r_month; /* 0..11 */
69
70 int r_dycode; /* see below */
71 int r_dayofmonth;
72 int r_wday;
73
74 long r_tod; /* time from midnight */
75 int r_todisstd; /* above is standard time if TRUE */
76 /* or wall clock time if FALSE */
77 int r_todisgmt; /* above is GMT if TRUE */
78 /* or local time if FALSE */
79 long r_stdoff; /* offset from standard time */
80 const char * r_abbrvar; /* variable part of abbreviation */
81
82 int r_todo; /* a rule to do (used in outzone) */
83 zic_t r_temp; /* used in outzone */
84 };
85
86 /*
87 ** r_dycode r_dayofmonth r_wday
88 */
89
90 #define DC_DOM 0 /* 1..31 */ /* unused */
91 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
92 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
93
94 struct zone {
95 const char * z_filename;
96 int z_linenum;
97
98 const char * z_name;
99 long z_gmtoff;
100 const char * z_rule;
101 const char * z_format;
102
103 long z_stdoff;
104
105 struct rule * z_rules;
106 int z_nrules;
107
108 struct rule z_untilrule;
109 zic_t z_untiltime;
110 };
111
112 extern int getopt(int argc, char * const argv[],
113 const char * options);
114 extern int link(const char * fromname, const char * toname);
115 extern char * optarg;
116 extern int optind;
117
118 static void addtt(zic_t starttime, int type);
119 static int addtype(long gmtoff, const char * abbr, int isdst,
120 int ttisstd, int ttisgmt);
121 static void leapadd(zic_t t, int positive, int rolling, int count);
122 static void adjleap(void);
123 static void associate(void);
124 static int ciequal(const char * ap, const char * bp);
125 static void convert(long val, char * buf);
126 static void convert64(zic_t val, char * buf);
127 static void dolink(const char * fromfield, const char * tofield);
128 static void doabbr(char * abbr, const int, const char * format,
129 const char * letters, int isdst, int doquotes);
130 static void eat(const char * name, int num);
131 static void eats(const char * name, int num,
132 const char * rname, int rnum);
133 static long eitol(int i);
134 static void error(const char * message);
135 static char ** getfields(char * buf);
136 static long gethms(const char * string, const char * errstrng,
137 int signable);
138 static void infile(const char * filename);
139 static void inleap(char ** fields, int nfields);
140 static void inlink(char ** fields, int nfields);
141 static void inrule(char ** fields, int nfields);
142 static int inzcont(char ** fields, int nfields);
143 static int inzone(char ** fields, int nfields);
144 static int inzsub(char ** fields, int nfields, int iscont);
145 static int is32(zic_t x);
146 static int itsabbr(const char * abbr, const char * word);
147 static int itsdir(const char * name);
148 static int lowerit(int c);
149 int main(int, char **);
150 static char * memcheck(char * tocheck);
151 static int mkdirs(char * filename);
152 static void newabbr(const char * abbr);
153 static long oadd(long t1, long t2);
154 static void outzone(const struct zone * zp, int ntzones);
155 static void puttzcode(long code, FILE * fp);
156 static void puttzcode64(zic_t code, FILE * fp);
157 static int rcomp(const void * leftp, const void * rightp);
158 static zic_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 int stringoffset(char * result, long offset);
164 static int stringrule(char * result, const struct rule * rp,
165 long dstoff, long gmtoff);
166 static void stringzone(char * result, const int,
167 const struct zone * zp, int ntzones);
168 static void setboundaries(void);
169 static zic_t tadd(zic_t t1, long t2);
170 static void usage(FILE *stream, int status);
171 static void warning(const char * const);
172 static void writezone(const char * name, const char * string);
173 static int yearistype(int year, const char * type);
174 static int atcomp(const void *avp, const void *bvp);
175 static void updateminmax(int x);
176
177 static int charcnt;
178 static int errors;
179 static const char * filename;
180 static int leapcnt;
181 static int leapseen;
182 static int leapminyear;
183 static int leapmaxyear;
184 static int linenum;
185 static int max_abbrvar_len;
186 static int max_format_len;
187 static zic_t max_time;
188 static int max_year;
189 static zic_t min_time;
190 static int min_year;
191 static int noise;
192 static const char * rfilename;
193 static int rlinenum;
194 static const char * progname;
195 static int timecnt;
196 static int typecnt;
197
198 /*
199 ** Line codes.
200 */
201
202 #define LC_RULE 0
203 #define LC_ZONE 1
204 #define LC_LINK 2
205 #define LC_LEAP 3
206
207 /*
208 ** Which fields are which on a Zone line.
209 */
210
211 #define ZF_NAME 1
212 #define ZF_GMTOFF 2
213 #define ZF_RULE 3
214 #define ZF_FORMAT 4
215 #define ZF_TILYEAR 5
216 #define ZF_TILMONTH 6
217 #define ZF_TILDAY 7
218 #define ZF_TILTIME 8
219 #define ZONE_MINFIELDS 5
220 #define ZONE_MAXFIELDS 9
221
222 /*
223 ** Which fields are which on a Zone continuation line.
224 */
225
226 #define ZFC_GMTOFF 0
227 #define ZFC_RULE 1
228 #define ZFC_FORMAT 2
229 #define ZFC_TILYEAR 3
230 #define ZFC_TILMONTH 4
231 #define ZFC_TILDAY 5
232 #define ZFC_TILTIME 6
233 #define ZONEC_MINFIELDS 3
234 #define ZONEC_MAXFIELDS 7
235
236 /*
237 ** Which files are which on a Rule line.
238 */
239
240 #define RF_NAME 1
241 #define RF_LOYEAR 2
242 #define RF_HIYEAR 3
243 #define RF_COMMAND 4
244 #define RF_MONTH 5
245 #define RF_DAY 6
246 #define RF_TOD 7
247 #define RF_STDOFF 8
248 #define RF_ABBRVAR 9
249 #define RULE_FIELDS 10
250
251 /*
252 ** Which fields are which on a Link line.
253 */
254
255 #define LF_FROM 1
256 #define LF_TO 2
257 #define LINK_FIELDS 3
258
259 /*
260 ** Which fields are which on a Leap line.
261 */
262
263 #define LP_YEAR 1
264 #define LP_MONTH 2
265 #define LP_DAY 3
266 #define LP_TIME 4
267 #define LP_CORR 5
268 #define LP_ROLL 6
269 #define LEAP_FIELDS 7
270
271 /*
272 ** Year synonyms.
273 */
274
275 #define YR_MINIMUM 0
276 #define YR_MAXIMUM 1
277 #define YR_ONLY 2
278
279 static struct rule * rules;
280 static int nrules; /* number of rules */
281
282 static struct zone * zones;
283 static int nzones; /* number of zones */
284
285 struct link {
286 const char * l_filename;
287 int l_linenum;
288 const char * l_from;
289 const char * l_to;
290 };
291
292 static struct link * links;
293 static int nlinks;
294
295 struct lookup {
296 const char * l_word;
297 const int l_value;
298 };
299
300 static struct lookup const * byword(const char * string,
301 const struct lookup * lp);
302
303 static struct lookup const line_codes[] = {
304 { "Rule", LC_RULE },
305 { "Zone", LC_ZONE },
306 { "Link", LC_LINK },
307 { "Leap", LC_LEAP },
308 { NULL, 0}
309 };
310
311 static struct lookup const mon_names[] = {
312 { "January", TM_JANUARY },
313 { "February", TM_FEBRUARY },
314 { "March", TM_MARCH },
315 { "April", TM_APRIL },
316 { "May", TM_MAY },
317 { "June", TM_JUNE },
318 { "July", TM_JULY },
319 { "August", TM_AUGUST },
320 { "September", TM_SEPTEMBER },
321 { "October", TM_OCTOBER },
322 { "November", TM_NOVEMBER },
323 { "December", TM_DECEMBER },
324 { NULL, 0 }
325 };
326
327 static struct lookup const wday_names[] = {
328 { "Sunday", TM_SUNDAY },
329 { "Monday", TM_MONDAY },
330 { "Tuesday", TM_TUESDAY },
331 { "Wednesday", TM_WEDNESDAY },
332 { "Thursday", TM_THURSDAY },
333 { "Friday", TM_FRIDAY },
334 { "Saturday", TM_SATURDAY },
335 { NULL, 0 }
336 };
337
338 static struct lookup const lasts[] = {
339 { "last-Sunday", TM_SUNDAY },
340 { "last-Monday", TM_MONDAY },
341 { "last-Tuesday", TM_TUESDAY },
342 { "last-Wednesday", TM_WEDNESDAY },
343 { "last-Thursday", TM_THURSDAY },
344 { "last-Friday", TM_FRIDAY },
345 { "last-Saturday", TM_SATURDAY },
346 { NULL, 0 }
347 };
348
349 static struct lookup const begin_years[] = {
350 { "minimum", YR_MINIMUM },
351 { "maximum", YR_MAXIMUM },
352 { NULL, 0 }
353 };
354
355 static struct lookup const end_years[] = {
356 { "minimum", YR_MINIMUM },
357 { "maximum", YR_MAXIMUM },
358 { "only", YR_ONLY },
359 { NULL, 0 }
360 };
361
362 static struct lookup const leap_types[] = {
363 { "Rolling", TRUE },
364 { "Stationary", FALSE },
365 { NULL, 0 }
366 };
367
368 static const int len_months[2][MONSPERYEAR] = {
369 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
370 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
371 };
372
373 static const int len_years[2] = {
374 DAYSPERNYEAR, DAYSPERLYEAR
375 };
376
377 static struct attype {
378 zic_t at;
379 unsigned char type;
380 } attypes[TZ_MAX_TIMES];
381 static long gmtoffs[TZ_MAX_TYPES];
382 static char isdsts[TZ_MAX_TYPES];
383 static unsigned char abbrinds[TZ_MAX_TYPES];
384 static char ttisstds[TZ_MAX_TYPES];
385 static char ttisgmts[TZ_MAX_TYPES];
386 static char chars[TZ_MAX_CHARS];
387 static zic_t trans[TZ_MAX_LEAPS];
388 static long corr[TZ_MAX_LEAPS];
389 static char roll[TZ_MAX_LEAPS];
390
391 /*
392 ** Memory allocation.
393 */
394
395 static char *
396 memcheck(ptr)
397 char * const ptr;
398 {
399 if (ptr == NULL) {
400 const char *e = strerror(errno);
401
402 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
403 progname, e);
404 exit(EXIT_FAILURE);
405 }
406 return ptr;
407 }
408
409 #define emalloc(size) memcheck(imalloc(size))
410 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
411 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
412 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
413
414 /*
415 ** Error handling.
416 */
417
418 static void
419 eats(name, num, rname, rnum)
420 const char * const name;
421 const int num;
422 const char * const rname;
423 const int rnum;
424 {
425 filename = name;
426 linenum = num;
427 rfilename = rname;
428 rlinenum = rnum;
429 }
430
431 static void
432 eat(name, num)
433 const char * const name;
434 const int num;
435 {
436 eats(name, num, (char *) NULL, -1);
437 }
438
439 static void
440 error(string)
441 const char * const string;
442 {
443 /*
444 ** Match the format of "cc" to allow sh users to
445 ** zic ... 2>&1 | error -t "*" -v
446 ** on BSD systems.
447 */
448 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
449 filename, linenum, string);
450 if (rfilename != NULL)
451 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
452 rfilename, rlinenum);
453 (void) fprintf(stderr, "\n");
454 ++errors;
455 }
456
457 static void
458 warning(string)
459 const char * const string;
460 {
461 char * cp;
462
463 cp = ecpyalloc(_("warning: "));
464 cp = ecatalloc(cp, string);
465 error(cp);
466 ifree(cp);
467 --errors;
468 }
469
470 static void
471 usage(FILE *stream, int status)
472 {
473 (void) fprintf(stream, _("%s: usage is %s \
474 [ --version ] [ --help ] [ -v ] [ -l localtime ] [ -p posixrules ] \\\n\
475 \t[ -d directory ] [ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n\
476 \n\
477 Report bugs to tz (at) elsie.nci.nih.gov.\n"),
478 progname, progname);
479 exit(status);
480 }
481
482 static const char * psxrules;
483 static const char * lcltime;
484 static const char * directory;
485 static const char * leapsec;
486 static const char * yitcommand;
487
488 int
489 main(argc, argv)
490 int argc;
491 char * argv[];
492 {
493 register int i;
494 register int j;
495 register int c;
496
497 #ifdef _POSIX_VERSION
498 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
499 #endif /* defined _POSIX_VERSION */
500 #if HAVE_GETTEXT - 0
501 (void) setlocale(LC_MESSAGES, "");
502 #ifdef TZ_DOMAINDIR
503 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
504 #endif /* defined TEXTDOMAINDIR */
505 (void) textdomain(TZ_DOMAIN);
506 #endif /* HAVE_GETTEXT */
507 progname = argv[0];
508 if (TYPE_BIT(zic_t) < 64) {
509 (void) fprintf(stderr, "%s: %s\n", progname,
510 _("wild compilation-time specification of zic_t"));
511 exit(EXIT_FAILURE);
512 }
513 for (i = 1; i < argc; ++i)
514 if (strcmp(argv[i], "--version") == 0) {
515 (void) printf("%s\n", elsieid);
516 exit(EXIT_SUCCESS);
517 } else if (strcmp(argv[i], "--help") == 0) {
518 usage(stdout, EXIT_SUCCESS);
519 }
520 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != EOF && c != -1)
521 switch (c) {
522 default:
523 usage(stderr, EXIT_FAILURE);
524 case 'd':
525 if (directory == NULL)
526 directory = optarg;
527 else {
528 (void) fprintf(stderr,
529 _("%s: More than one -d option specified\n"),
530 progname);
531 exit(EXIT_FAILURE);
532 }
533 break;
534 case 'l':
535 if (lcltime == NULL)
536 lcltime = optarg;
537 else {
538 (void) fprintf(stderr,
539 _("%s: More than one -l option specified\n"),
540 progname);
541 exit(EXIT_FAILURE);
542 }
543 break;
544 case 'p':
545 if (psxrules == NULL)
546 psxrules = optarg;
547 else {
548 (void) fprintf(stderr,
549 _("%s: More than one -p option specified\n"),
550 progname);
551 exit(EXIT_FAILURE);
552 }
553 break;
554 case 'y':
555 if (yitcommand == NULL)
556 yitcommand = optarg;
557 else {
558 (void) fprintf(stderr,
559 _("%s: More than one -y option specified\n"),
560 progname);
561 exit(EXIT_FAILURE);
562 }
563 break;
564 case 'L':
565 if (leapsec == NULL)
566 leapsec = optarg;
567 else {
568 (void) fprintf(stderr,
569 _("%s: More than one -L option specified\n"),
570 progname);
571 exit(EXIT_FAILURE);
572 }
573 break;
574 case 'v':
575 noise = TRUE;
576 break;
577 case 's':
578 (void) printf("%s: -s ignored\n", progname);
579 break;
580 }
581 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
582 usage(stderr, EXIT_FAILURE); /* usage message by request */
583 if (directory == NULL)
584 directory = TZDIR;
585 if (yitcommand == NULL)
586 yitcommand = "yearistype";
587
588 setboundaries();
589
590 if (optind < argc && leapsec != NULL) {
591 infile(leapsec);
592 adjleap();
593 }
594
595 for (i = optind; i < argc; ++i)
596 infile(argv[i]);
597 if (errors)
598 exit(EXIT_FAILURE);
599 associate();
600 for (i = 0; i < nzones; i = j) {
601 /*
602 ** Find the next non-continuation zone entry.
603 */
604 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
605 continue;
606 outzone(&zones[i], j - i);
607 }
608 /*
609 ** Make links.
610 */
611 for (i = 0; i < nlinks; ++i) {
612 eat(links[i].l_filename, links[i].l_linenum);
613 dolink(links[i].l_from, links[i].l_to);
614 if (noise)
615 for (j = 0; j < nlinks; ++j)
616 if (strcmp(links[i].l_to,
617 links[j].l_from) == 0)
618 warning(_("link to link"));
619 }
620 if (lcltime != NULL) {
621 eat("command line", 1);
622 dolink(lcltime, TZDEFAULT);
623 }
624 if (psxrules != NULL) {
625 eat("command line", 1);
626 dolink(psxrules, TZDEFRULES);
627 }
628 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
629 }
630
631 static void
632 dolink(fromfield, tofield)
633 const char * const fromfield;
634 const char * const tofield;
635 {
636 register char * fromname;
637 register char * toname;
638
639 if (fromfield[0] == '/')
640 fromname = ecpyalloc(fromfield);
641 else {
642 fromname = ecpyalloc(directory);
643 fromname = ecatalloc(fromname, "/");
644 fromname = ecatalloc(fromname, fromfield);
645 }
646 if (tofield[0] == '/')
647 toname = ecpyalloc(tofield);
648 else {
649 toname = ecpyalloc(directory);
650 toname = ecatalloc(toname, "/");
651 toname = ecatalloc(toname, tofield);
652 }
653 /*
654 ** We get to be careful here since
655 ** there's a fair chance of root running us.
656 */
657 if (!itsdir(toname))
658 (void) remove(toname);
659 if (link(fromname, toname) != 0) {
660 int result;
661
662 if (mkdirs(toname) != 0)
663 exit(EXIT_FAILURE);
664
665 result = link(fromname, toname);
666 #if HAVE_SYMLINK
667 if (result != 0 &&
668 access(fromname, F_OK) == 0 &&
669 !itsdir(fromname)) {
670 const char *s = tofield;
671 register char * symlinkcontents = NULL;
672
673 while ((s = strchr(s+1, '/')) != NULL)
674 symlinkcontents =
675 ecatalloc(symlinkcontents,
676 "../");
677 symlinkcontents =
678 ecatalloc(symlinkcontents,
679 fromname);
680 result = symlink(symlinkcontents,
681 toname);
682 if (result == 0)
683 warning(_("hard link failed, symbolic link used"));
684 ifree(symlinkcontents);
685 }
686 #endif /* HAVE_SYMLINK */
687 if (result != 0) {
688 const char *e = strerror(errno);
689
690 (void) fprintf(stderr,
691 _("%s: Can't link from %s to %s: %s\n"),
692 progname, fromname, toname, e);
693 exit(EXIT_FAILURE);
694 }
695 }
696 ifree(fromname);
697 ifree(toname);
698 }
699
700 #define TIME_T_BITS_IN_FILE 64
701
702 static void
703 setboundaries(void)
704 {
705 register int i;
706
707 min_time = -1;
708 for (i = 0; i < TIME_T_BITS_IN_FILE - 1; ++i)
709 min_time *= 2;
710 max_time = -(min_time + 1);
711 }
712
713 static int
714 itsdir(name)
715 const char * const name;
716 {
717 register char * myname;
718 register int accres;
719
720 myname = ecpyalloc(name);
721 myname = ecatalloc(myname, "/.");
722 accres = access(myname, F_OK);
723 ifree(myname);
724 return accres == 0;
725 }
726
727 /*
728 ** Associate sets of rules with zones.
729 */
730
731 /*
732 ** Sort by rule name.
733 */
734
735 static int
736 rcomp(cp1, cp2)
737 const void * cp1;
738 const void * cp2;
739 {
740 return strcmp(((const struct rule *) cp1)->r_name,
741 ((const struct rule *) cp2)->r_name);
742 }
743
744 static void
745 associate(void)
746 {
747 register struct zone * zp;
748 register struct rule * rp;
749 register int base, out;
750 register int i, j;
751
752 if (nrules != 0) {
753 (void) qsort((void *) rules, (size_t) nrules,
754 (size_t) sizeof *rules, rcomp);
755 for (i = 0; i < nrules - 1; ++i) {
756 if (strcmp(rules[i].r_name,
757 rules[i + 1].r_name) != 0)
758 continue;
759 if (strcmp(rules[i].r_filename,
760 rules[i + 1].r_filename) == 0)
761 continue;
762 eat(rules[i].r_filename, rules[i].r_linenum);
763 warning(_("same rule name in multiple files"));
764 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
765 warning(_("same rule name in multiple files"));
766 for (j = i + 2; j < nrules; ++j) {
767 if (strcmp(rules[i].r_name,
768 rules[j].r_name) != 0)
769 break;
770 if (strcmp(rules[i].r_filename,
771 rules[j].r_filename) == 0)
772 continue;
773 if (strcmp(rules[i + 1].r_filename,
774 rules[j].r_filename) == 0)
775 continue;
776 break;
777 }
778 i = j - 1;
779 }
780 }
781 for (i = 0; i < nzones; ++i) {
782 zp = &zones[i];
783 zp->z_rules = NULL;
784 zp->z_nrules = 0;
785 }
786 for (base = 0; base < nrules; base = out) {
787 rp = &rules[base];
788 for (out = base + 1; out < nrules; ++out)
789 if (strcmp(rp->r_name, rules[out].r_name) != 0)
790 break;
791 for (i = 0; i < nzones; ++i) {
792 zp = &zones[i];
793 if (strcmp(zp->z_rule, rp->r_name) != 0)
794 continue;
795 zp->z_rules = rp;
796 zp->z_nrules = out - base;
797 }
798 }
799 for (i = 0; i < nzones; ++i) {
800 zp = &zones[i];
801 if (zp->z_nrules == 0) {
802 /*
803 ** Maybe we have a local standard time offset.
804 */
805 eat(zp->z_filename, zp->z_linenum);
806 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
807 TRUE);
808 /*
809 ** Note, though, that if there's no rule,
810 ** a '%s' in the format is a bad thing.
811 */
812 if (strchr(zp->z_format, '%') != 0)
813 error(_("%s in ruleless zone"));
814 }
815 }
816 if (errors)
817 exit(EXIT_FAILURE);
818 }
819
820 static void
821 infile(name)
822 const char * name;
823 {
824 register FILE * fp;
825 register char ** fields;
826 register char * cp;
827 register const struct lookup * lp;
828 register int nfields;
829 register int wantcont;
830 register int num;
831 char buf[BUFSIZ];
832
833 if (strcmp(name, "-") == 0) {
834 name = _("standard input");
835 fp = stdin;
836 } else if ((fp = fopen(name, "r")) == NULL) {
837 const char *e = strerror(errno);
838
839 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
840 progname, name, e);
841 exit(EXIT_FAILURE);
842 }
843 wantcont = FALSE;
844 for (num = 1; ; ++num) {
845 eat(name, num);
846 if (fgets(buf, (int) sizeof buf, fp) != buf)
847 break;
848 cp = strchr(buf, '\n');
849 if (cp == NULL) {
850 error(_("line too long"));
851 exit(EXIT_FAILURE);
852 }
853 *cp = '\0';
854 fields = getfields(buf);
855 nfields = 0;
856 while (fields[nfields] != NULL) {
857 static char nada;
858
859 if (strcmp(fields[nfields], "-") == 0)
860 fields[nfields] = &nada;
861 ++nfields;
862 }
863 if (nfields == 0) {
864 /* nothing to do */
865 } else if (wantcont) {
866 wantcont = inzcont(fields, nfields);
867 } else {
868 lp = byword(fields[0], line_codes);
869 if (lp == NULL)
870 error(_("input line of unknown type"));
871 else switch ((int) (lp->l_value)) {
872 case LC_RULE:
873 inrule(fields, nfields);
874 wantcont = FALSE;
875 break;
876 case LC_ZONE:
877 wantcont = inzone(fields, nfields);
878 break;
879 case LC_LINK:
880 inlink(fields, nfields);
881 wantcont = FALSE;
882 break;
883 case LC_LEAP:
884 if (name != leapsec)
885 (void) fprintf(stderr,
886 _("%s: Leap line in non leap seconds file %s\n"),
887 progname, name);
888 else inleap(fields, nfields);
889 wantcont = FALSE;
890 break;
891 default: /* "cannot happen" */
892 (void) fprintf(stderr,
893 _("%s: panic: Invalid l_value %d\n"),
894 progname, lp->l_value);
895 exit(EXIT_FAILURE);
896 }
897 }
898 ifree((char *) fields);
899 }
900 if (ferror(fp)) {
901 (void) fprintf(stderr, _("%s: Error reading %s\n"),
902 progname, filename);
903 exit(EXIT_FAILURE);
904 }
905 if (fp != stdin && fclose(fp)) {
906 const char *e = strerror(errno);
907
908 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
909 progname, filename, e);
910 exit(EXIT_FAILURE);
911 }
912 if (wantcont)
913 error(_("expected continuation line not found"));
914 }
915
916 /*
917 ** Convert a string of one of the forms
918 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
919 ** into a number of seconds.
920 ** A null string maps to zero.
921 ** Call error with errstring and return zero on errors.
922 */
923
924 static long
925 gethms(string, errstring, signable)
926 const char * string;
927 const char * const errstring;
928 const int signable;
929 {
930 long hh;
931 int mm, ss, sign;
932
933 if (string == NULL || *string == '\0')
934 return 0;
935 if (!signable)
936 sign = 1;
937 else if (*string == '-') {
938 sign = -1;
939 ++string;
940 } else sign = 1;
941 if (sscanf(string, scheck(string, "%ld"), &hh) == 1)
942 mm = ss = 0;
943 else if (sscanf(string, scheck(string, "%ld:%d"), &hh, &mm) == 2)
944 ss = 0;
945 else if (sscanf(string, scheck(string, "%ld:%d:%d"),
946 &hh, &mm, &ss) != 3) {
947 error(errstring);
948 return 0;
949 }
950 if (hh < 0 ||
951 mm < 0 || mm >= MINSPERHOUR ||
952 ss < 0 || ss > SECSPERMIN) {
953 error(errstring);
954 return 0;
955 }
956 if (LONG_MAX / SECSPERHOUR < hh) {
957 error(_("time overflow"));
958 return 0;
959 }
960 if (noise && hh == HOURSPERDAY && mm == 0 && ss == 0)
961 warning(_("24:00 not handled by pre-1998 versions of zic"));
962 if (noise && (hh > HOURSPERDAY ||
963 (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
964 warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
965 return oadd(eitol(sign) * hh * eitol(SECSPERHOUR),
966 eitol(sign) * (eitol(mm) * eitol(SECSPERMIN) + eitol(ss)));
967 }
968
969 static void
970 inrule(fields, nfields)
971 register char ** const fields;
972 const int nfields;
973 {
974 static struct rule r;
975
976 if (nfields != RULE_FIELDS) {
977 error(_("wrong number of fields on Rule line"));
978 return;
979 }
980 if (*fields[RF_NAME] == '\0') {
981 error(_("nameless rule"));
982 return;
983 }
984 r.r_filename = filename;
985 r.r_linenum = linenum;
986 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
987 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
988 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
989 r.r_name = ecpyalloc(fields[RF_NAME]);
990 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
991 if (max_abbrvar_len < strlen(r.r_abbrvar))
992 max_abbrvar_len = strlen(r.r_abbrvar);
993 rules = (struct rule *) (void *) erealloc((char *) rules,
994 (int) ((nrules + 1) * sizeof *rules));
995 rules[nrules++] = r;
996 }
997
998 static int
999 inzone(fields, nfields)
1000 register char ** const fields;
1001 const int nfields;
1002 {
1003 register int i;
1004 static char * buf;
1005
1006 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1007 error(_("wrong number of fields on Zone line"));
1008 return FALSE;
1009 }
1010 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
1011 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
1012 (void)sprintf(buf, /* XXX: sprintf is safe */
1013 _("\"Zone %s\" line and -l option are mutually exclusive"),
1014 TZDEFAULT);
1015 error(buf);
1016 return FALSE;
1017 }
1018 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1019 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
1020 (void)sprintf(buf, /* XXX: sprintf is safe */
1021 _("\"Zone %s\" line and -p option are mutually exclusive"),
1022 TZDEFRULES);
1023 error(buf);
1024 return FALSE;
1025 }
1026 for (i = 0; i < nzones; ++i)
1027 if (zones[i].z_name != NULL &&
1028 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1029 buf = erealloc(buf, (int) (132 +
1030 strlen(fields[ZF_NAME]) +
1031 strlen(zones[i].z_filename)));
1032 (void)sprintf(buf, /* XXX: sprintf is safe */
1033 _("duplicate zone name %s (file \"%s\", line %d)"),
1034 fields[ZF_NAME],
1035 zones[i].z_filename,
1036 zones[i].z_linenum);
1037 error(buf);
1038 return FALSE;
1039 }
1040 return inzsub(fields, nfields, FALSE);
1041 }
1042
1043 static int
1044 inzcont(fields, nfields)
1045 register char ** const fields;
1046 const int nfields;
1047 {
1048 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1049 error(_("wrong number of fields on Zone continuation line"));
1050 return FALSE;
1051 }
1052 return inzsub(fields, nfields, TRUE);
1053 }
1054
1055 static int
1056 inzsub(fields, nfields, iscont)
1057 register char ** const fields;
1058 const int nfields;
1059 const int iscont;
1060 {
1061 register char * cp;
1062 static struct zone z;
1063 register int i_gmtoff, i_rule, i_format;
1064 register int i_untilyear, i_untilmonth;
1065 register int i_untilday, i_untiltime;
1066 register int hasuntil;
1067
1068 if (iscont) {
1069 i_gmtoff = ZFC_GMTOFF;
1070 i_rule = ZFC_RULE;
1071 i_format = ZFC_FORMAT;
1072 i_untilyear = ZFC_TILYEAR;
1073 i_untilmonth = ZFC_TILMONTH;
1074 i_untilday = ZFC_TILDAY;
1075 i_untiltime = ZFC_TILTIME;
1076 z.z_name = NULL;
1077 } else {
1078 i_gmtoff = ZF_GMTOFF;
1079 i_rule = ZF_RULE;
1080 i_format = ZF_FORMAT;
1081 i_untilyear = ZF_TILYEAR;
1082 i_untilmonth = ZF_TILMONTH;
1083 i_untilday = ZF_TILDAY;
1084 i_untiltime = ZF_TILTIME;
1085 z.z_name = ecpyalloc(fields[ZF_NAME]);
1086 }
1087 z.z_filename = filename;
1088 z.z_linenum = linenum;
1089 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UTC offset"), TRUE);
1090 if ((cp = strchr(fields[i_format], '%')) != 0) {
1091 if (*++cp != 's' || strchr(cp, '%') != 0) {
1092 error(_("invalid abbreviation format"));
1093 return FALSE;
1094 }
1095 }
1096 z.z_rule = ecpyalloc(fields[i_rule]);
1097 z.z_format = ecpyalloc(fields[i_format]);
1098 if (max_format_len < strlen(z.z_format))
1099 max_format_len = strlen(z.z_format);
1100 hasuntil = nfields > i_untilyear;
1101 if (hasuntil) {
1102 z.z_untilrule.r_filename = filename;
1103 z.z_untilrule.r_linenum = linenum;
1104 rulesub(&z.z_untilrule,
1105 fields[i_untilyear],
1106 "only",
1107 "",
1108 (nfields > i_untilmonth) ?
1109 fields[i_untilmonth] : "Jan",
1110 (nfields > i_untilday) ? fields[i_untilday] : "1",
1111 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1112 z.z_untiltime = rpytime(&z.z_untilrule,
1113 z.z_untilrule.r_loyear);
1114 if (iscont && nzones > 0 &&
1115 z.z_untiltime > min_time &&
1116 z.z_untiltime < max_time &&
1117 zones[nzones - 1].z_untiltime > min_time &&
1118 zones[nzones - 1].z_untiltime < max_time &&
1119 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1120 error(_(
1121 "Zone continuation line end time is not after end time of previous line"
1122 ));
1123 return FALSE;
1124 }
1125 }
1126 zones = (struct zone *) (void *) erealloc((char *) zones,
1127 (int) ((nzones + 1) * sizeof *zones));
1128 zones[nzones++] = z;
1129 /*
1130 ** If there was an UNTIL field on this line,
1131 ** there's more information about the zone on the next line.
1132 */
1133 return hasuntil;
1134 }
1135
1136 static void
1137 inleap(fields, nfields)
1138 register char ** const fields;
1139 const int nfields;
1140 {
1141 register const char * cp;
1142 register const struct lookup * lp;
1143 register int i, j;
1144 int year, month, day;
1145 long dayoff, tod;
1146 zic_t t;
1147
1148 if (nfields != LEAP_FIELDS) {
1149 error(_("wrong number of fields on Leap line"));
1150 return;
1151 }
1152 dayoff = 0;
1153 cp = fields[LP_YEAR];
1154 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1155 /*
1156 ** Leapin' Lizards!
1157 */
1158 error(_("invalid leaping year"));
1159 return;
1160 }
1161 if (!leapseen || leapmaxyear < year)
1162 leapmaxyear = year;
1163 if (!leapseen || leapminyear > year)
1164 leapminyear = year;
1165 leapseen = TRUE;
1166 j = EPOCH_YEAR;
1167 while (j != year) {
1168 if (year > j) {
1169 i = len_years[isleap(j)];
1170 ++j;
1171 } else {
1172 --j;
1173 i = -len_years[isleap(j)];
1174 }
1175 dayoff = oadd(dayoff, eitol(i));
1176 }
1177 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1178 error(_("invalid month name"));
1179 return;
1180 }
1181 month = lp->l_value;
1182 j = TM_JANUARY;
1183 while (j != month) {
1184 i = len_months[isleap(year)][j];
1185 dayoff = oadd(dayoff, eitol(i));
1186 ++j;
1187 }
1188 cp = fields[LP_DAY];
1189 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1190 day <= 0 || day > len_months[isleap(year)][month]) {
1191 error(_("invalid day of month"));
1192 return;
1193 }
1194 dayoff = oadd(dayoff, eitol(day - 1));
1195 if (dayoff < 0 && !TYPE_SIGNED(zic_t)) {
1196 error(_("time before zero"));
1197 return;
1198 }
1199 if (dayoff < min_time / SECSPERDAY) {
1200 error(_("time too small"));
1201 return;
1202 }
1203 if (dayoff > max_time / SECSPERDAY) {
1204 error(_("time too large"));
1205 return;
1206 }
1207 t = (zic_t) dayoff * SECSPERDAY;
1208 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1209 cp = fields[LP_CORR];
1210 {
1211 register int positive;
1212 int count;
1213
1214 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1215 positive = FALSE;
1216 count = 1;
1217 } else if (strcmp(cp, "--") == 0) {
1218 positive = FALSE;
1219 count = 2;
1220 } else if (strcmp(cp, "+") == 0) {
1221 positive = TRUE;
1222 count = 1;
1223 } else if (strcmp(cp, "++") == 0) {
1224 positive = TRUE;
1225 count = 2;
1226 } else {
1227 error(_("illegal CORRECTION field on Leap line"));
1228 return;
1229 }
1230 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1231 error(_(
1232 "illegal Rolling/Stationary field on Leap line"
1233 ));
1234 return;
1235 }
1236 leapadd(tadd(t, tod), positive, lp->l_value, count);
1237 }
1238 }
1239
1240 static void
1241 inlink(fields, nfields)
1242 register char ** const fields;
1243 const int nfields;
1244 {
1245 struct link l;
1246
1247 if (nfields != LINK_FIELDS) {
1248 error(_("wrong number of fields on Link line"));
1249 return;
1250 }
1251 if (*fields[LF_FROM] == '\0') {
1252 error(_("blank FROM field on Link line"));
1253 return;
1254 }
1255 if (*fields[LF_TO] == '\0') {
1256 error(_("blank TO field on Link line"));
1257 return;
1258 }
1259 l.l_filename = filename;
1260 l.l_linenum = linenum;
1261 l.l_from = ecpyalloc(fields[LF_FROM]);
1262 l.l_to = ecpyalloc(fields[LF_TO]);
1263 links = (struct link *) (void *) erealloc((char *) links,
1264 (int) ((nlinks + 1) * sizeof *links));
1265 links[nlinks++] = l;
1266 }
1267
1268 static void
1269 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1270 register struct rule * const rp;
1271 const char * const loyearp;
1272 const char * const hiyearp;
1273 const char * const typep;
1274 const char * const monthp;
1275 const char * const dayp;
1276 const char * const timep;
1277 {
1278 register const struct lookup * lp;
1279 register const char * cp;
1280 register char * dp;
1281 register char * ep;
1282
1283 if ((lp = byword(monthp, mon_names)) == NULL) {
1284 error(_("invalid month name"));
1285 return;
1286 }
1287 rp->r_month = lp->l_value;
1288 rp->r_todisstd = FALSE;
1289 rp->r_todisgmt = FALSE;
1290 dp = ecpyalloc(timep);
1291 if (*dp != '\0') {
1292 ep = dp + strlen(dp) - 1;
1293 switch (lowerit(*ep)) {
1294 case 's': /* Standard */
1295 rp->r_todisstd = TRUE;
1296 rp->r_todisgmt = FALSE;
1297 *ep = '\0';
1298 break;
1299 case 'w': /* Wall */
1300 rp->r_todisstd = FALSE;
1301 rp->r_todisgmt = FALSE;
1302 *ep = '\0';
1303 break;
1304 case 'g': /* Greenwich */
1305 case 'u': /* Universal */
1306 case 'z': /* Zulu */
1307 rp->r_todisstd = TRUE;
1308 rp->r_todisgmt = TRUE;
1309 *ep = '\0';
1310 break;
1311 }
1312 }
1313 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1314 ifree(dp);
1315 /*
1316 ** Year work.
1317 */
1318 cp = loyearp;
1319 lp = byword(cp, begin_years);
1320 rp->r_lowasnum = lp == NULL;
1321 if (!rp->r_lowasnum) switch ((int) lp->l_value) {
1322 case YR_MINIMUM:
1323 rp->r_loyear = INT_MIN;
1324 break;
1325 case YR_MAXIMUM:
1326 rp->r_loyear = INT_MAX;
1327 break;
1328 default: /* "cannot happen" */
1329 (void) fprintf(stderr,
1330 _("%s: panic: Invalid l_value %d\n"),
1331 progname, lp->l_value);
1332 exit(EXIT_FAILURE);
1333 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1334 error(_("invalid starting year"));
1335 return;
1336 }
1337 cp = hiyearp;
1338 lp = byword(cp, end_years);
1339 rp->r_hiwasnum = lp == NULL;
1340 if (!rp->r_hiwasnum) switch ((int) lp->l_value) {
1341 case YR_MINIMUM:
1342 rp->r_hiyear = INT_MIN;
1343 break;
1344 case YR_MAXIMUM:
1345 rp->r_hiyear = INT_MAX;
1346 break;
1347 case YR_ONLY:
1348 rp->r_hiyear = rp->r_loyear;
1349 break;
1350 default: /* "cannot happen" */
1351 (void) fprintf(stderr,
1352 _("%s: panic: Invalid l_value %d\n"),
1353 progname, lp->l_value);
1354 exit(EXIT_FAILURE);
1355 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1356 error(_("invalid ending year"));
1357 return;
1358 }
1359 if (rp->r_loyear > rp->r_hiyear) {
1360 error(_("starting year greater than ending year"));
1361 return;
1362 }
1363 if (*typep == '\0')
1364 rp->r_yrtype = NULL;
1365 else {
1366 if (rp->r_loyear == rp->r_hiyear) {
1367 error(_("typed single year"));
1368 return;
1369 }
1370 rp->r_yrtype = ecpyalloc(typep);
1371 }
1372 /*
1373 ** Day work.
1374 ** Accept things such as:
1375 ** 1
1376 ** last-Sunday
1377 ** Sun<=20
1378 ** Sun>=7
1379 */
1380 dp = ecpyalloc(dayp);
1381 if ((lp = byword(dp, lasts)) != NULL) {
1382 rp->r_dycode = DC_DOWLEQ;
1383 rp->r_wday = lp->l_value;
1384 rp->r_dayofmonth = len_months[1][rp->r_month];
1385 } else {
1386 if ((ep = strchr(dp, '<')) != 0)
1387 rp->r_dycode = DC_DOWLEQ;
1388 else if ((ep = strchr(dp, '>')) != 0)
1389 rp->r_dycode = DC_DOWGEQ;
1390 else {
1391 ep = dp;
1392 rp->r_dycode = DC_DOM;
1393 }
1394 if (rp->r_dycode != DC_DOM) {
1395 *ep++ = 0;
1396 if (*ep++ != '=') {
1397 error(_("invalid day of month"));
1398 ifree(dp);
1399 return;
1400 }
1401 if ((lp = byword(dp, wday_names)) == NULL) {
1402 error(_("invalid weekday name"));
1403 ifree(dp);
1404 return;
1405 }
1406 rp->r_wday = lp->l_value;
1407 }
1408 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1409 rp->r_dayofmonth <= 0 ||
1410 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1411 error(_("invalid day of month"));
1412 ifree(dp);
1413 return;
1414 }
1415 }
1416 ifree(dp);
1417 }
1418
1419 static void
1420 convert(val, buf)
1421 const long val;
1422 char * const buf;
1423 {
1424 register int i;
1425 register int shift;
1426
1427 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1428 buf[i] = val >> shift;
1429 }
1430
1431 static void
1432 convert64(val, buf)
1433 const zic_t val;
1434 char * const buf;
1435 {
1436 register int i;
1437 register int shift;
1438
1439 for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1440 buf[i] = val >> shift;
1441 }
1442
1443 static void
1444 puttzcode(val, fp)
1445 const long val;
1446 FILE * const fp;
1447 {
1448 char buf[4];
1449
1450 convert(val, buf);
1451 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1452 }
1453
1454 static void
1455 puttzcode64(val, fp)
1456 const zic_t val;
1457 FILE * const fp;
1458 {
1459 char buf[8];
1460
1461 convert64(val, buf);
1462 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1463 }
1464
1465 static int
1466 atcomp(avp, bvp)
1467 const void * avp;
1468 const void * bvp;
1469 {
1470 const zic_t a = ((const struct attype *) avp)->at;
1471 const zic_t b = ((const struct attype *) bvp)->at;
1472
1473 return (a < b) ? -1 : (a > b);
1474 }
1475
1476 static int
1477 is32(x)
1478 const zic_t x;
1479 {
1480 return INT32_MIN <= x && x <= INT32_MAX;
1481 }
1482
1483 static void
1484 writezone(name, string)
1485 const char * const name;
1486 const char * const string;
1487 {
1488 register FILE * fp;
1489 register int i, j;
1490 register int leapcnt32, leapi32;
1491 register int timecnt32, timei32;
1492 register int pass;
1493 static char * fullname;
1494 static const struct tzhead tzh0;
1495 static struct tzhead tzh;
1496 zic_t ats[TZ_MAX_TIMES];
1497 unsigned char types[TZ_MAX_TIMES];
1498
1499 /*
1500 ** Sort.
1501 */
1502 if (timecnt > 1)
1503 (void) qsort((void *) attypes, (size_t) timecnt,
1504 (size_t) sizeof *attypes, atcomp);
1505 /*
1506 ** Optimize.
1507 */
1508 {
1509 int fromi;
1510 int toi;
1511
1512 toi = 0;
1513 fromi = 0;
1514 while (fromi < timecnt && attypes[fromi].at < min_time)
1515 ++fromi;
1516 if (isdsts[0] == 0)
1517 while (fromi < timecnt && attypes[fromi].type == 0)
1518 ++fromi; /* handled by default rule */
1519 for ( ; fromi < timecnt; ++fromi) {
1520 if (toi != 0 && ((attypes[fromi].at +
1521 gmtoffs[attypes[toi - 1].type]) <=
1522 (attypes[toi - 1].at + gmtoffs[toi == 1 ? 0
1523 : attypes[toi - 2].type]))) {
1524 attypes[toi - 1].type =
1525 attypes[fromi].type;
1526 continue;
1527 }
1528 if (toi == 0 ||
1529 attypes[toi - 1].type != attypes[fromi].type)
1530 attypes[toi++] = attypes[fromi];
1531 }
1532 timecnt = toi;
1533 }
1534 /*
1535 ** Transfer.
1536 */
1537 for (i = 0; i < timecnt; ++i) {
1538 ats[i] = attypes[i].at;
1539 types[i] = attypes[i].type;
1540 }
1541 /*
1542 ** Correct for leap seconds.
1543 */
1544 for (i = 0; i < timecnt; ++i) {
1545 j = leapcnt;
1546 while (--j >= 0)
1547 if (ats[i] > trans[j] - corr[j]) {
1548 ats[i] = tadd(ats[i], corr[j]);
1549 break;
1550 }
1551 }
1552 /*
1553 ** Figure out 32-bit-limited starts and counts.
1554 */
1555 timecnt32 = timecnt;
1556 timei32 = 0;
1557 leapcnt32 = leapcnt;
1558 leapi32 = 0;
1559 while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1560 --timecnt32;
1561 while (timecnt32 > 0 && !is32(ats[timei32])) {
1562 --timecnt32;
1563 ++timei32;
1564 }
1565 while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1566 --leapcnt32;
1567 while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1568 --leapcnt32;
1569 ++leapi32;
1570 }
1571 fullname = erealloc(fullname,
1572 (int) (strlen(directory) + 1 + strlen(name) + 1));
1573 (void) sprintf(fullname, "%s/%s", directory, name); /* XXX: sprintf is safe */
1574 /*
1575 ** Remove old file, if any, to snap links.
1576 */
1577 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1578 const char *e = strerror(errno);
1579
1580 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1581 progname, fullname, e);
1582 exit(EXIT_FAILURE);
1583 }
1584 if ((fp = fopen(fullname, "wb")) == NULL) {
1585 if (mkdirs(fullname) != 0)
1586 exit(EXIT_FAILURE);
1587 if ((fp = fopen(fullname, "wb")) == NULL) {
1588 const char *e = strerror(errno);
1589
1590 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1591 progname, fullname, e);
1592 exit(EXIT_FAILURE);
1593 }
1594 }
1595 for (pass = 1; pass <= 2; ++pass) {
1596 register int thistimei, thistimecnt;
1597 register int thisleapi, thisleapcnt;
1598 register int thistimelim, thisleaplim;
1599 int writetype[TZ_MAX_TIMES];
1600 int typemap[TZ_MAX_TYPES];
1601 register int thistypecnt;
1602 char thischars[TZ_MAX_CHARS];
1603 char thischarcnt;
1604 int indmap[TZ_MAX_CHARS];
1605
1606 if (pass == 1) {
1607 thistimei = timei32;
1608 thistimecnt = timecnt32;
1609 thisleapi = leapi32;
1610 thisleapcnt = leapcnt32;
1611 } else {
1612 thistimei = 0;
1613 thistimecnt = timecnt;
1614 thisleapi = 0;
1615 thisleapcnt = leapcnt;
1616 }
1617 thistimelim = thistimei + thistimecnt;
1618 thisleaplim = thisleapi + thisleapcnt;
1619 for (i = 0; i < typecnt; ++i)
1620 writetype[i] = thistimecnt == timecnt;
1621 if (thistimecnt == 0) {
1622 /*
1623 ** No transition times fall in the current
1624 ** (32- or 64-bit) window.
1625 */
1626 if (typecnt != 0)
1627 writetype[typecnt - 1] = TRUE;
1628 } else {
1629 for (i = thistimei - 1; i < thistimelim; ++i)
1630 if (i >= 0)
1631 writetype[types[i]] = TRUE;
1632 /*
1633 ** For America/Godthab and Antarctica/Palmer
1634 */
1635 if (thistimei == 0)
1636 writetype[0] = TRUE;
1637 }
1638 thistypecnt = 0;
1639 for (i = 0; i < typecnt; ++i)
1640 typemap[i] = writetype[i] ? thistypecnt++ : -1;
1641 for (i = 0; i < sizeof indmap / sizeof indmap[0]; ++i)
1642 indmap[i] = -1;
1643 thischarcnt = 0;
1644 for (i = 0; i < typecnt; ++i) {
1645 register char * thisabbr;
1646
1647 if (!writetype[i])
1648 continue;
1649 if (indmap[abbrinds[i]] >= 0)
1650 continue;
1651 thisabbr = &chars[abbrinds[i]];
1652 for (j = 0; j < thischarcnt; ++j)
1653 if (strcmp(&thischars[j], thisabbr) == 0)
1654 break;
1655 if (j == thischarcnt) {
1656 (void) strcpy(&thischars[(int) thischarcnt],
1657 thisabbr);
1658 thischarcnt += strlen(thisabbr) + 1;
1659 }
1660 indmap[abbrinds[i]] = j;
1661 }
1662 #define DO(field) (void) fwrite((void *) tzh.field, \
1663 (size_t) sizeof tzh.field, (size_t) 1, fp)
1664 tzh = tzh0;
1665 (void) strncpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
1666 tzh.tzh_version[0] = ZIC_VERSION;
1667 convert(eitol(thistypecnt), tzh.tzh_ttisgmtcnt);
1668 convert(eitol(thistypecnt), tzh.tzh_ttisstdcnt);
1669 convert(eitol(thisleapcnt), tzh.tzh_leapcnt);
1670 convert(eitol(thistimecnt), tzh.tzh_timecnt);
1671 convert(eitol(thistypecnt), tzh.tzh_typecnt);
1672 convert(eitol(thischarcnt), tzh.tzh_charcnt);
1673 DO(tzh_magic);
1674 DO(tzh_version);
1675 DO(tzh_reserved);
1676 DO(tzh_ttisgmtcnt);
1677 DO(tzh_ttisstdcnt);
1678 DO(tzh_leapcnt);
1679 DO(tzh_timecnt);
1680 DO(tzh_typecnt);
1681 DO(tzh_charcnt);
1682 #undef DO
1683 for (i = thistimei; i < thistimelim; ++i)
1684 if (pass == 1)
1685 puttzcode((long) ats[i], fp);
1686 else puttzcode64(ats[i], fp);
1687 for (i = thistimei; i < thistimelim; ++i) {
1688 unsigned char uc;
1689
1690 uc = typemap[types[i]];
1691 (void) fwrite((void *) &uc,
1692 (size_t) sizeof uc,
1693 (size_t) 1,
1694 fp);
1695 }
1696 for (i = 0; i < typecnt; ++i)
1697 if (writetype[i]) {
1698 puttzcode(gmtoffs[i], fp);
1699 (void) putc(isdsts[i], fp);
1700 (void) putc((unsigned char) indmap[abbrinds[i]], fp);
1701 }
1702 if (thischarcnt != 0)
1703 (void) fwrite((void *) thischars,
1704 (size_t) sizeof thischars[0],
1705 (size_t) thischarcnt, fp);
1706 for (i = thisleapi; i < thisleaplim; ++i) {
1707 register zic_t todo;
1708
1709 if (roll[i]) {
1710 if (timecnt == 0 || trans[i] < ats[0]) {
1711 j = 0;
1712 while (isdsts[j])
1713 if (++j >= typecnt) {
1714 j = 0;
1715 break;
1716 }
1717 } else {
1718 j = 1;
1719 while (j < timecnt &&
1720 trans[i] >= ats[j])
1721 ++j;
1722 j = types[j - 1];
1723 }
1724 todo = tadd(trans[i], -gmtoffs[j]);
1725 } else todo = trans[i];
1726 if (pass == 1)
1727 puttzcode((long) todo, fp);
1728 else puttzcode64(todo, fp);
1729 puttzcode(corr[i], fp);
1730 }
1731 for (i = 0; i < typecnt; ++i)
1732 if (writetype[i])
1733 (void) putc(ttisstds[i], fp);
1734 for (i = 0; i < typecnt; ++i)
1735 if (writetype[i])
1736 (void) putc(ttisgmts[i], fp);
1737 }
1738 (void) fprintf(fp, "\n%s\n", string);
1739 if (ferror(fp) || fclose(fp)) {
1740 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1741 progname, fullname);
1742 exit(EXIT_FAILURE);
1743 }
1744 }
1745
1746 static void
1747 doabbr(abbr, abbrlen, format, letters, isdst, doquotes)
1748 char * const abbr;
1749 const int abbrlen;
1750 const char * const format;
1751 const char * const letters;
1752 const int isdst;
1753 const int doquotes;
1754 {
1755 register char * cp;
1756 register char * slashp;
1757 register int len;
1758
1759 slashp = strchr(format, '/');
1760 if (slashp == NULL) {
1761 if (letters == NULL)
1762 (void) strlcpy(abbr, format, abbrlen);
1763 else (void) snprintf(abbr, abbrlen, format, letters);
1764 } else if (isdst) {
1765 (void) strlcpy(abbr, slashp + 1, abbrlen);
1766 } else {
1767 if (slashp > format)
1768 (void) strncpy(abbr, format,
1769 (unsigned) (slashp - format));
1770 abbr[slashp - format] = '\0';
1771 }
1772 if (!doquotes)
1773 return;
1774 for (cp = abbr; *cp != '\0'; ++cp)
1775 if (strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ", *cp) == NULL &&
1776 strchr("abcdefghijklmnopqrstuvwxyz", *cp) == NULL)
1777 break;
1778 len = strlen(abbr);
1779 if (len > 0 && *cp == '\0')
1780 return;
1781 abbr[len + 2] = '\0';
1782 abbr[len + 1] = '>';
1783 for ( ; len > 0; --len)
1784 abbr[len] = abbr[len - 1];
1785 abbr[0] = '<';
1786 }
1787
1788 static void
1789 updateminmax(x)
1790 const int x;
1791 {
1792 if (min_year > x)
1793 min_year = x;
1794 if (max_year < x)
1795 max_year = x;
1796 }
1797
1798 static int
1799 stringoffset(result, offset)
1800 char * result;
1801 long offset;
1802 {
1803 register int hours;
1804 register int minutes;
1805 register int seconds;
1806
1807 result[0] = '\0';
1808 if (offset < 0) {
1809 (void) strcpy(result, "-");
1810 offset = -offset;
1811 }
1812 seconds = offset % SECSPERMIN;
1813 offset /= SECSPERMIN;
1814 minutes = offset % MINSPERHOUR;
1815 offset /= MINSPERHOUR;
1816 hours = offset;
1817 if (hours >= HOURSPERDAY) {
1818 result[0] = '\0';
1819 return -1;
1820 }
1821 (void) sprintf(end(result), "%d", hours);
1822 if (minutes != 0 || seconds != 0) {
1823 (void) sprintf(end(result), ":%02d", minutes);
1824 if (seconds != 0)
1825 (void) sprintf(end(result), ":%02d", seconds);
1826 }
1827 return 0;
1828 }
1829
1830 static int
1831 stringrule(result, rp, dstoff, gmtoff)
1832 char * result;
1833 const struct rule * const rp;
1834 const long dstoff;
1835 const long gmtoff;
1836 {
1837 register long tod;
1838
1839 result = end(result);
1840 if (rp->r_dycode == DC_DOM) {
1841 register int month, total;
1842
1843 if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
1844 return -1;
1845 total = 0;
1846 for (month = 0; month < rp->r_month; ++month)
1847 total += len_months[0][month];
1848 (void) sprintf(result, "J%d", total + rp->r_dayofmonth);
1849 } else {
1850 register int week;
1851
1852 if (rp->r_dycode == DC_DOWGEQ) {
1853 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1854 if ((week - 1) * DAYSPERWEEK + 1 != rp->r_dayofmonth)
1855 return -1;
1856 } else if (rp->r_dycode == DC_DOWLEQ) {
1857 if (rp->r_dayofmonth == len_months[1][rp->r_month])
1858 week = 5;
1859 else {
1860 week = 1 + rp->r_dayofmonth / DAYSPERWEEK;
1861 if (week * DAYSPERWEEK - 1 != rp->r_dayofmonth)
1862 return -1;
1863 }
1864 } else return -1; /* "cannot happen" */
1865 (void) sprintf(result, "M%d.%d.%d",
1866 rp->r_month + 1, week, rp->r_wday);
1867 }
1868 tod = rp->r_tod;
1869 if (rp->r_todisgmt)
1870 tod += gmtoff;
1871 if (rp->r_todisstd && rp->r_stdoff == 0)
1872 tod += dstoff;
1873 if (tod < 0) {
1874 result[0] = '\0';
1875 return -1;
1876 }
1877 if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
1878 (void) strcat(result, "/");
1879 if (stringoffset(end(result), tod) != 0)
1880 return -1;
1881 }
1882 return 0;
1883 }
1884
1885 static void
1886 stringzone(result, resultlen, zpfirst, zonecount)
1887 char * result;
1888 const int resultlen;
1889 const struct zone * const zpfirst;
1890 const int zonecount;
1891 {
1892 register const struct zone * zp;
1893 register struct rule * rp;
1894 register struct rule * stdrp;
1895 register struct rule * dstrp;
1896 register int i;
1897 register const char * abbrvar;
1898
1899 result[0] = '\0';
1900 zp = zpfirst + zonecount - 1;
1901 stdrp = dstrp = NULL;
1902 for (i = 0; i < zp->z_nrules; ++i) {
1903 rp = &zp->z_rules[i];
1904 if (rp->r_hiwasnum || rp->r_hiyear != INT_MAX)
1905 continue;
1906 if (rp->r_yrtype != NULL)
1907 continue;
1908 if (rp->r_stdoff == 0) {
1909 if (stdrp == NULL)
1910 stdrp = rp;
1911 else return;
1912 } else {
1913 if (dstrp == NULL)
1914 dstrp = rp;
1915 else return;
1916 }
1917 }
1918 if (stdrp == NULL && dstrp == NULL) {
1919 /*
1920 ** There are no rules running through "max".
1921 ** Let's find the latest rule.
1922 */
1923 for (i = 0; i < zp->z_nrules; ++i) {
1924 rp = &zp->z_rules[i];
1925 if (stdrp == NULL || rp->r_hiyear > stdrp->r_hiyear ||
1926 (rp->r_hiyear == stdrp->r_hiyear &&
1927 rp->r_month > stdrp->r_month))
1928 stdrp = rp;
1929 }
1930 if (stdrp != NULL && stdrp->r_stdoff != 0)
1931 return; /* We end up in DST (a POSIX no-no). */
1932 /*
1933 ** Horrid special case: if year is 2037,
1934 ** presume this is a zone handled on a year-by-year basis;
1935 ** do not try to apply a rule to the zone.
1936 */
1937 if (stdrp != NULL && stdrp->r_hiyear == 2037)
1938 return;
1939 }
1940 if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_stdoff != 0))
1941 return;
1942 abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
1943 doabbr(result, resultlen, zp->z_format, abbrvar, FALSE, TRUE);
1944 if (stringoffset(end(result), -zp->z_gmtoff) != 0) {
1945 result[0] = '\0';
1946 return;
1947 }
1948 if (dstrp == NULL)
1949 return;
1950 doabbr(end(result), resultlen - strlen(result),
1951 zp->z_format, dstrp->r_abbrvar, TRUE, TRUE);
1952 if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR)
1953 if (stringoffset(end(result),
1954 -(zp->z_gmtoff + dstrp->r_stdoff)) != 0) {
1955 result[0] = '\0';
1956 return;
1957 }
1958 (void) strcat(result, ",");
1959 if (stringrule(result, dstrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1960 result[0] = '\0';
1961 return;
1962 }
1963 (void) strcat(result, ",");
1964 if (stringrule(result, stdrp, dstrp->r_stdoff, zp->z_gmtoff) != 0) {
1965 result[0] = '\0';
1966 return;
1967 }
1968 }
1969
1970 static void
1971 outzone(zpfirst, zonecount)
1972 const struct zone * const zpfirst;
1973 const int zonecount;
1974 {
1975 register const struct zone * zp;
1976 register struct rule * rp;
1977 register int i, j;
1978 register int usestart, useuntil;
1979 register zic_t starttime, untiltime;
1980 register long gmtoff;
1981 register long stdoff;
1982 register int year;
1983 register long startoff;
1984 register int startttisstd;
1985 register int startttisgmt;
1986 register int type;
1987 register char * startbuf;
1988 register char * ab;
1989 register char * envvar;
1990 register int max_abbr_len;
1991 register int max_envvar_len;
1992
1993 max_abbr_len = 2 + max_format_len + max_abbrvar_len;
1994 max_envvar_len = 2 * max_abbr_len + 5 * 9;
1995 startbuf = emalloc(max_abbr_len + 1);
1996 ab = emalloc(max_abbr_len + 1);
1997 envvar = emalloc(max_envvar_len + 1);
1998 INITIALIZE(untiltime);
1999 INITIALIZE(starttime);
2000 /*
2001 ** Now. . .finally. . .generate some useful data!
2002 */
2003 timecnt = 0;
2004 typecnt = 0;
2005 charcnt = 0;
2006 /*
2007 ** Thanks to Earl Chew
2008 ** for noting the need to unconditionally initialize startttisstd.
2009 */
2010 startttisstd = FALSE;
2011 startttisgmt = FALSE;
2012 min_year = max_year = EPOCH_YEAR;
2013 if (leapseen) {
2014 updateminmax(leapminyear);
2015 updateminmax(leapmaxyear + (leapmaxyear < INT_MAX));
2016 }
2017 for (i = 0; i < zonecount; ++i) {
2018 zp = &zpfirst[i];
2019 if (i < zonecount - 1)
2020 updateminmax(zp->z_untilrule.r_loyear);
2021 for (j = 0; j < zp->z_nrules; ++j) {
2022 rp = &zp->z_rules[j];
2023 if (rp->r_lowasnum)
2024 updateminmax(rp->r_loyear);
2025 if (rp->r_hiwasnum)
2026 updateminmax(rp->r_hiyear);
2027 }
2028 }
2029 /*
2030 ** Generate lots of data if a rule can't cover all future times.
2031 */
2032 stringzone(envvar, max_envvar_len+1, zpfirst, zonecount);
2033 if (noise && envvar[0] == '\0') {
2034 register char * wp;
2035
2036 wp = ecpyalloc(_("no POSIX environment variable for zone"));
2037 wp = ecatalloc(wp, " ");
2038 wp = ecatalloc(wp, zpfirst->z_name);
2039 warning(wp);
2040 ifree(wp);
2041 }
2042 if (envvar[0] == '\0') {
2043 if (min_year >= INT_MIN + YEARSPERREPEAT)
2044 min_year -= YEARSPERREPEAT;
2045 else min_year = INT_MIN;
2046 if (max_year <= INT_MAX - YEARSPERREPEAT)
2047 max_year += YEARSPERREPEAT;
2048 else max_year = INT_MAX;
2049 }
2050 /*
2051 ** For the benefit of older systems,
2052 ** generate data from 1900 through 2037.
2053 */
2054 if (min_year > 1900)
2055 min_year = 1900;
2056 if (max_year < 2037)
2057 max_year = 2037;
2058 for (i = 0; i < zonecount; ++i) {
2059 /*
2060 ** A guess that may well be corrected later.
2061 */
2062 stdoff = 0;
2063 zp = &zpfirst[i];
2064 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
2065 useuntil = i < (zonecount - 1);
2066 if (useuntil && zp->z_untiltime <= min_time)
2067 continue;
2068 gmtoff = zp->z_gmtoff;
2069 eat(zp->z_filename, zp->z_linenum);
2070 *startbuf = '\0';
2071 startoff = zp->z_gmtoff;
2072 if (zp->z_nrules == 0) {
2073 stdoff = zp->z_stdoff;
2074 doabbr(startbuf, max_abbr_len + 1, zp->z_format,
2075 (char *) NULL, stdoff != 0, FALSE);
2076 type = addtype(oadd(zp->z_gmtoff, stdoff),
2077 startbuf, stdoff != 0, startttisstd,
2078 startttisgmt);
2079 if (usestart) {
2080 addtt(starttime, type);
2081 usestart = FALSE;
2082 } else if (stdoff != 0)
2083 addtt(min_time, type);
2084 } else for (year = min_year; year <= max_year; ++year) {
2085 if (useuntil && year > zp->z_untilrule.r_hiyear)
2086 break;
2087 /*
2088 ** Mark which rules to do in the current year.
2089 ** For those to do, calculate rpytime(rp, year);
2090 */
2091 for (j = 0; j < zp->z_nrules; ++j) {
2092 rp = &zp->z_rules[j];
2093 eats(zp->z_filename, zp->z_linenum,
2094 rp->r_filename, rp->r_linenum);
2095 rp->r_todo = year >= rp->r_loyear &&
2096 year <= rp->r_hiyear &&
2097 yearistype(year, rp->r_yrtype);
2098 if (rp->r_todo)
2099 rp->r_temp = rpytime(rp, year);
2100 }
2101 for ( ; ; ) {
2102 register int k;
2103 register zic_t jtime, ktime;
2104 register long offset;
2105
2106 INITIALIZE(ktime);
2107 if (useuntil) {
2108 /*
2109 ** Turn untiltime into UTC
2110 ** assuming the current gmtoff and
2111 ** stdoff values.
2112 */
2113 untiltime = zp->z_untiltime;
2114 if (!zp->z_untilrule.r_todisgmt)
2115 untiltime = tadd(untiltime,
2116 -gmtoff);
2117 if (!zp->z_untilrule.r_todisstd)
2118 untiltime = tadd(untiltime,
2119 -stdoff);
2120 }
2121 /*
2122 ** Find the rule (of those to do, if any)
2123 ** that takes effect earliest in the year.
2124 */
2125 k = -1;
2126 for (j = 0; j < zp->z_nrules; ++j) {
2127 rp = &zp->z_rules[j];
2128 if (!rp->r_todo)
2129 continue;
2130 eats(zp->z_filename, zp->z_linenum,
2131 rp->r_filename, rp->r_linenum);
2132 offset = rp->r_todisgmt ? 0 : gmtoff;
2133 if (!rp->r_todisstd)
2134 offset = oadd(offset, stdoff);
2135 jtime = rp->r_temp;
2136 if (jtime == min_time ||
2137 jtime == max_time)
2138 continue;
2139 jtime = tadd(jtime, -offset);
2140 if (k < 0 || jtime < ktime) {
2141 k = j;
2142 ktime = jtime;
2143 }
2144 }
2145 if (k < 0)
2146 break; /* go on to next year */
2147 rp = &zp->z_rules[k];
2148 rp->r_todo = FALSE;
2149 if (useuntil && ktime >= untiltime)
2150 break;
2151 stdoff = rp->r_stdoff;
2152 if (usestart && ktime == starttime)
2153 usestart = FALSE;
2154 if (usestart) {
2155 if (ktime < starttime) {
2156 startoff = oadd(zp->z_gmtoff,
2157 stdoff);
2158 doabbr(startbuf,
2159 max_abbr_len + 1,
2160 zp->z_format,
2161 rp->r_abbrvar,
2162 rp->r_stdoff != 0,
2163 FALSE);
2164 continue;
2165 }
2166 if (*startbuf == '\0' &&
2167 startoff == oadd(zp->z_gmtoff,
2168 stdoff)) {
2169 doabbr(startbuf,
2170 max_abbr_len + 1,
2171 zp->z_format,
2172 rp->r_abbrvar,
2173 rp->r_stdoff !=
2174 0,
2175 FALSE);
2176 }
2177 }
2178 eats(zp->z_filename, zp->z_linenum,
2179 rp->r_filename, rp->r_linenum);
2180 doabbr(ab, max_abbr_len+1, zp->z_format, rp->r_abbrvar,
2181 rp->r_stdoff != 0, FALSE);
2182 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2183 type = addtype(offset, ab, rp->r_stdoff != 0,
2184 rp->r_todisstd, rp->r_todisgmt);
2185 addtt(ktime, type);
2186 }
2187 }
2188 if (usestart) {
2189 if (*startbuf == '\0' &&
2190 zp->z_format != NULL &&
2191 strchr(zp->z_format, '%') == NULL &&
2192 strchr(zp->z_format, '/') == NULL)
2193 (void)strncpy(startbuf, zp->z_format,
2194 max_abbr_len + 1 - 1);
2195 eat(zp->z_filename, zp->z_linenum);
2196 if (*startbuf == '\0')
2197 error(_("can't determine time zone abbreviation to use just after until time"));
2198 else addtt(starttime,
2199 addtype(startoff, startbuf,
2200 startoff != zp->z_gmtoff,
2201 startttisstd,
2202 startttisgmt));
2203 }
2204 /*
2205 ** Now we may get to set starttime for the next zone line.
2206 */
2207 if (useuntil) {
2208 startttisstd = zp->z_untilrule.r_todisstd;
2209 startttisgmt = zp->z_untilrule.r_todisgmt;
2210 starttime = zp->z_untiltime;
2211 if (!startttisstd)
2212 starttime = tadd(starttime, -stdoff);
2213 if (!startttisgmt)
2214 starttime = tadd(starttime, -gmtoff);
2215 }
2216 }
2217 writezone(zpfirst->z_name, envvar);
2218 ifree(startbuf);
2219 ifree(ab);
2220 ifree(envvar);
2221 }
2222
2223 static void
2224 addtt(starttime, type)
2225 const zic_t starttime;
2226 int type;
2227 {
2228 if (starttime <= min_time ||
2229 (timecnt == 1 && attypes[0].at < min_time)) {
2230 gmtoffs[0] = gmtoffs[type];
2231 isdsts[0] = isdsts[type];
2232 ttisstds[0] = ttisstds[type];
2233 ttisgmts[0] = ttisgmts[type];
2234 if (abbrinds[type] != 0)
2235 (void) strcpy(chars, &chars[abbrinds[type]]);
2236 abbrinds[0] = 0;
2237 charcnt = strlen(chars) + 1;
2238 typecnt = 1;
2239 timecnt = 0;
2240 type = 0;
2241 }
2242 if (timecnt >= TZ_MAX_TIMES) {
2243 error(_("too many transitions?!"));
2244 exit(EXIT_FAILURE);
2245 }
2246 attypes[timecnt].at = starttime;
2247 attypes[timecnt].type = type;
2248 ++timecnt;
2249 }
2250
2251 static int
2252 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
2253 const long gmtoff;
2254 const char * const abbr;
2255 const int isdst;
2256 const int ttisstd;
2257 const int ttisgmt;
2258 {
2259 register int i, j;
2260
2261 if (isdst != TRUE && isdst != FALSE) {
2262 error(_("internal error - addtype called with bad isdst"));
2263 exit(EXIT_FAILURE);
2264 }
2265 if (ttisstd != TRUE && ttisstd != FALSE) {
2266 error(_("internal error - addtype called with bad ttisstd"));
2267 exit(EXIT_FAILURE);
2268 }
2269 if (ttisgmt != TRUE && ttisgmt != FALSE) {
2270 error(_("internal error - addtype called with bad ttisgmt"));
2271 exit(EXIT_FAILURE);
2272 }
2273 /*
2274 ** See if there's already an entry for this zone type.
2275 ** If so, just return its index.
2276 */
2277 for (i = 0; i < typecnt; ++i) {
2278 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2279 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2280 ttisstd == ttisstds[i] &&
2281 ttisgmt == ttisgmts[i])
2282 return i;
2283 }
2284 /*
2285 ** There isn't one; add a new one, unless there are already too
2286 ** many.
2287 */
2288 if (typecnt >= TZ_MAX_TYPES) {
2289 error(_("too many local time types"));
2290 exit(EXIT_FAILURE);
2291 }
2292 if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2293 error(_("UTC offset out of range"));
2294 exit(EXIT_FAILURE);
2295 }
2296 gmtoffs[i] = gmtoff;
2297 isdsts[i] = isdst;
2298 ttisstds[i] = ttisstd;
2299 ttisgmts[i] = ttisgmt;
2300
2301 for (j = 0; j < charcnt; ++j)
2302 if (strcmp(&chars[j], abbr) == 0)
2303 break;
2304 if (j == charcnt)
2305 newabbr(abbr);
2306 abbrinds[i] = j;
2307 ++typecnt;
2308 return i;
2309 }
2310
2311 static void
2312 leapadd(t, positive, rolling, count)
2313 const zic_t t;
2314 const int positive;
2315 const int rolling;
2316 int count;
2317 {
2318 register int i, j;
2319
2320 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2321 error(_("too many leap seconds"));
2322 exit(EXIT_FAILURE);
2323 }
2324 for (i = 0; i < leapcnt; ++i)
2325 if (t <= trans[i]) {
2326 if (t == trans[i]) {
2327 error(_("repeated leap second moment"));
2328 exit(EXIT_FAILURE);
2329 }
2330 break;
2331 }
2332 do {
2333 for (j = leapcnt; j > i; --j) {
2334 trans[j] = trans[j - 1];
2335 corr[j] = corr[j - 1];
2336 roll[j] = roll[j - 1];
2337 }
2338 trans[i] = t;
2339 corr[i] = positive ? 1L : eitol(-count);
2340 roll[i] = rolling;
2341 ++leapcnt;
2342 } while (positive && --count != 0);
2343 }
2344
2345 static void
2346 adjleap(void)
2347 {
2348 register int i;
2349 register long last = 0;
2350
2351 /*
2352 ** propagate leap seconds forward
2353 */
2354 for (i = 0; i < leapcnt; ++i) {
2355 trans[i] = tadd(trans[i], last);
2356 last = corr[i] += last;
2357 }
2358 }
2359
2360 static int
2361 yearistype(year, type)
2362 const int year;
2363 const char * const type;
2364 {
2365 static char * buf;
2366 int result;
2367
2368 if (type == NULL || *type == '\0')
2369 return TRUE;
2370 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
2371 (void)sprintf(buf, "%s %d %s", yitcommand, year, type); /* XXX: sprintf is safe */
2372 result = system(buf);
2373 if (WIFEXITED(result)) switch (WEXITSTATUS(result)) {
2374 case 0:
2375 return TRUE;
2376 case 1:
2377 return FALSE;
2378 }
2379 error(_("Wild result from command execution"));
2380 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2381 progname, buf, result);
2382 for ( ; ; )
2383 exit(EXIT_FAILURE);
2384 }
2385
2386 static int
2387 lowerit(a)
2388 int a;
2389 {
2390 a = (unsigned char) a;
2391 return (isascii(a) && isupper(a)) ? tolower(a) : a;
2392 }
2393
2394 static int
2395 ciequal(ap, bp) /* case-insensitive equality */
2396 register const char * ap;
2397 register const char * bp;
2398 {
2399 while (lowerit(*ap) == lowerit(*bp++))
2400 if (*ap++ == '\0')
2401 return TRUE;
2402 return FALSE;
2403 }
2404
2405 static int
2406 itsabbr(abbr, word)
2407 register const char * abbr;
2408 register const char * word;
2409 {
2410 if (lowerit(*abbr) != lowerit(*word))
2411 return FALSE;
2412 ++word;
2413 while (*++abbr != '\0')
2414 do {
2415 if (*word == '\0')
2416 return FALSE;
2417 } while (lowerit(*word++) != lowerit(*abbr));
2418 return TRUE;
2419 }
2420
2421 static const struct lookup *
2422 byword(word, table)
2423 register const char * const word;
2424 register const struct lookup * const table;
2425 {
2426 register const struct lookup * foundlp;
2427 register const struct lookup * lp;
2428
2429 if (word == NULL || table == NULL)
2430 return NULL;
2431 /*
2432 ** Look for exact match.
2433 */
2434 for (lp = table; lp->l_word != NULL; ++lp)
2435 if (ciequal(word, lp->l_word))
2436 return lp;
2437 /*
2438 ** Look for inexact match.
2439 */
2440 foundlp = NULL;
2441 for (lp = table; lp->l_word != NULL; ++lp)
2442 if (itsabbr(word, lp->l_word)) {
2443 if (foundlp == NULL)
2444 foundlp = lp;
2445 else return NULL; /* multiple inexact matches */
2446 }
2447 return foundlp;
2448 }
2449
2450 static char **
2451 getfields(cp)
2452 register char * cp;
2453 {
2454 register char * dp;
2455 register char ** array;
2456 register int nsubs;
2457
2458 if (cp == NULL)
2459 return NULL;
2460 array = (char **) (void *)
2461 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
2462 nsubs = 0;
2463 for ( ; ; ) {
2464 while (isascii((unsigned char) *cp) &&
2465 isspace((unsigned char) *cp))
2466 ++cp;
2467 if (*cp == '\0' || *cp == '#')
2468 break;
2469 array[nsubs++] = dp = cp;
2470 do {
2471 if ((*dp = *cp++) != '"')
2472 ++dp;
2473 else while ((*dp = *cp++) != '"')
2474 if (*dp != '\0')
2475 ++dp;
2476 else {
2477 error(_(
2478 "Odd number of quotation marks"
2479 ));
2480 exit(1);
2481 }
2482 } while (*cp != '\0' && *cp != '#' &&
2483 (!isascii(*cp) || !isspace((unsigned char) *cp)));
2484 if (isascii(*cp) && isspace((unsigned char) *cp))
2485 ++cp;
2486 *dp = '\0';
2487 }
2488 array[nsubs] = NULL;
2489 return array;
2490 }
2491
2492 static long
2493 oadd(t1, t2)
2494 const long t1;
2495 const long t2;
2496 {
2497 register long t;
2498
2499 t = t1 + t2;
2500 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2501 error(_("time overflow"));
2502 exit(EXIT_FAILURE);
2503 }
2504 return t;
2505 }
2506
2507 static zic_t
2508 tadd(t1, t2)
2509 const zic_t t1;
2510 const long t2;
2511 {
2512 register zic_t t;
2513
2514 if (t1 == max_time && t2 > 0)
2515 return max_time;
2516 if (t1 == min_time && t2 < 0)
2517 return min_time;
2518 t = t1 + t2;
2519 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2520 error(_("time overflow"));
2521 exit(EXIT_FAILURE);
2522 }
2523 return t;
2524 }
2525
2526 /*
2527 ** Given a rule, and a year, compute the date - in seconds since January 1,
2528 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2529 */
2530
2531 static zic_t
2532 rpytime(rp, wantedy)
2533 register const struct rule * const rp;
2534 register const int wantedy;
2535 {
2536 register int y, m, i;
2537 register long dayoff; /* with a nod to Margaret O. */
2538 register zic_t t;
2539
2540 if (wantedy == INT_MIN)
2541 return min_time;
2542 if (wantedy == INT_MAX)
2543 return max_time;
2544 dayoff = 0;
2545 m = TM_JANUARY;
2546 y = EPOCH_YEAR;
2547 while (wantedy != y) {
2548 if (wantedy > y) {
2549 i = len_years[isleap(y)];
2550 ++y;
2551 } else {
2552 --y;
2553 i = -len_years[isleap(y)];
2554 }
2555 dayoff = oadd(dayoff, eitol(i));
2556 }
2557 while (m != rp->r_month) {
2558 i = len_months[isleap(y)][m];
2559 dayoff = oadd(dayoff, eitol(i));
2560 ++m;
2561 }
2562 i = rp->r_dayofmonth;
2563 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2564 if (rp->r_dycode == DC_DOWLEQ)
2565 --i;
2566 else {
2567 error(_("use of 2/29 in non leap-year"));
2568 exit(EXIT_FAILURE);
2569 }
2570 }
2571 --i;
2572 dayoff = oadd(dayoff, eitol(i));
2573 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2574 register long wday;
2575
2576 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2577 wday = eitol(EPOCH_WDAY);
2578 /*
2579 ** Don't trust mod of negative numbers.
2580 */
2581 if (dayoff >= 0)
2582 wday = (wday + dayoff) % LDAYSPERWEEK;
2583 else {
2584 wday -= ((-dayoff) % LDAYSPERWEEK);
2585 if (wday < 0)
2586 wday += LDAYSPERWEEK;
2587 }
2588 while (wday != eitol(rp->r_wday))
2589 if (rp->r_dycode == DC_DOWGEQ) {
2590 dayoff = oadd(dayoff, (long) 1);
2591 if (++wday >= LDAYSPERWEEK)
2592 wday = 0;
2593 ++i;
2594 } else {
2595 dayoff = oadd(dayoff, (long) -1);
2596 if (--wday < 0)
2597 wday = LDAYSPERWEEK - 1;
2598 --i;
2599 }
2600 if (i < 0 || i >= len_months[isleap(y)][m]) {
2601 if (noise)
2602 warning(_("rule goes past start/end of month--\
2603 will not work with pre-2004 versions of zic"));
2604 }
2605 }
2606 if (dayoff < min_time / SECSPERDAY)
2607 return min_time;
2608 if (dayoff > max_time / SECSPERDAY)
2609 return max_time;
2610 t = (zic_t) dayoff * SECSPERDAY;
2611 return tadd(t, rp->r_tod);
2612 }
2613
2614 static void
2615 newabbr(string)
2616 const char * const string;
2617 {
2618 register int i;
2619
2620 if (strcmp(string, GRANDPARENTED) != 0) {
2621 register const char * cp;
2622 register char * wp;
2623
2624 /*
2625 ** Want one to ZIC_MAX_ABBR_LEN_WO_WARN alphabetics
2626 ** optionally followed by a + or - and a number from 1 to 14.
2627 */
2628 cp = string;
2629 wp = NULL;
2630 while (isascii((unsigned char) *cp) &&
2631 isalpha((unsigned char) *cp))
2632 ++cp;
2633 if (cp - string == 0)
2634 wp = _("time zone abbreviation lacks alphabetic at start");
2635 if (noise && cp - string > 3)
2636 wp = _("time zone abbreviation has more than 3 alphabetics");
2637 if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
2638 wp = _("time zone abbreviation has too many alphabetics");
2639 if (wp == NULL && (*cp == '+' || *cp == '-')) {
2640 ++cp;
2641 if (isascii((unsigned char) *cp) &&
2642 isdigit((unsigned char) *cp))
2643 if (*cp++ == '1' &&
2644 *cp >= '0' && *cp <= '4')
2645 ++cp;
2646 }
2647 if (*cp != '\0')
2648 wp = _("time zone abbreviation differs from POSIX standard");
2649 if (wp != NULL) {
2650 wp = ecpyalloc(wp);
2651 wp = ecatalloc(wp, " (");
2652 wp = ecatalloc(wp, string);
2653 wp = ecatalloc(wp, ")");
2654 warning(wp);
2655 ifree(wp);
2656 }
2657 }
2658 i = strlen(string) + 1;
2659 if (charcnt + i > TZ_MAX_CHARS) {
2660 error(_("too many, or too long, time zone abbreviations"));
2661 exit(EXIT_FAILURE);
2662 }
2663 (void)strncpy(&chars[charcnt], string, sizeof(chars) - charcnt - 1);
2664 charcnt += eitol(i);
2665 }
2666
2667 static int
2668 mkdirs(argname)
2669 char * argname;
2670 {
2671 register char * name;
2672 register char * cp;
2673
2674 if (argname == NULL || *argname == '\0')
2675 return 0;
2676 cp = name = ecpyalloc(argname);
2677 while ((cp = strchr(cp + 1, '/')) != 0) {
2678 *cp = '\0';
2679 #ifndef __NetBSD__
2680 /*
2681 ** DOS drive specifier?
2682 */
2683 if (isalpha((unsigned char) name[0]) &&
2684 name[1] == ':' && name[2] == '\0') {
2685 *cp = '/';
2686 continue;
2687 }
2688 #endif /* !defined __NetBSD__ */
2689 if (!itsdir(name)) {
2690 /*
2691 ** It doesn't seem to exist, so we try to create it.
2692 ** Creation may fail because of the directory being
2693 ** created by some other multiprocessor, so we get
2694 ** to do extra checking.
2695 */
2696 if (mkdir(name, MKDIR_UMASK) != 0) {
2697 const char *e = strerror(errno);
2698
2699 if (errno != EEXIST || !itsdir(name)) {
2700 (void) fprintf(stderr,
2701 _("%s: Can't create directory %s: %s\n"),
2702 progname, name, e);
2703 ifree(name);
2704 return -1;
2705 }
2706 }
2707 }
2708 *cp = '/';
2709 }
2710 ifree(name);
2711 return 0;
2712 }
2713
2714 static long
2715 eitol(i)
2716 const int i;
2717 {
2718 long l;
2719
2720 l = i;
2721 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2722 (void) fprintf(stderr,
2723 _("%s: %d did not sign extend correctly\n"),
2724 progname, i);
2725 exit(EXIT_FAILURE);
2726 }
2727 return l;
2728 }
2729
2730 /*
2731 ** UNIX was a registered trademark of The Open Group in 2003.
2732 */
2733