]> git.saurik.com Git - apple/system_cmds.git/blob - zic.tproj/zic.c
system_cmds-258.tar.gz
[apple/system_cmds.git] / zic.tproj / zic.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * "Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License."
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #if defined(LIBC_SCCS) && !defined(lint)
25 #if 0
26 static char elsieid[] = "@(#)zic.c 7.80";
27 #else
28 static char rcsid[] = "$OpenBSD: zic.c,v 1.6 1997/01/15 23:40:55 millert Exp $";
29 #endif
30 #endif /* LIBC_SCCS and not lint */
31
32 #include "private.h"
33 #include "locale.h"
34 #include "tzfile.h"
35 #ifdef unix
36 #include "sys/stat.h" /* for umask manifest constants */
37 #endif /* defined unix */
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 struct rule {
53 const char * r_filename;
54 int r_linenum;
55 const char * r_name;
56
57 int r_loyear; /* for example, 1986 */
58 int r_hiyear; /* for example, 1986 */
59 const char * r_yrtype;
60
61 int r_month; /* 0..11 */
62
63 int r_dycode; /* see below */
64 int r_dayofmonth;
65 int r_wday;
66
67 long r_tod; /* time from midnight */
68 int r_todisstd; /* above is standard time if TRUE */
69 /* or wall clock time if FALSE */
70 int r_todisgmt; /* above is GMT if TRUE */
71 /* or local time if FALSE */
72 long r_stdoff; /* offset from standard time */
73 const char * r_abbrvar; /* variable part of abbreviation */
74
75 int r_todo; /* a rule to do (used in outzone) */
76 time_t r_temp; /* used in outzone */
77 };
78
79 /*
80 ** r_dycode r_dayofmonth r_wday
81 */
82
83 #define DC_DOM 0 /* 1..31 */ /* unused */
84 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
85 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
86
87 struct zone {
88 const char * z_filename;
89 int z_linenum;
90
91 const char * z_name;
92 long z_gmtoff;
93 const char * z_rule;
94 const char * z_format;
95
96 long z_stdoff;
97
98 struct rule * z_rules;
99 int z_nrules;
100
101 struct rule z_untilrule;
102 time_t z_untiltime;
103 };
104
105 extern int getopt P((int argc, char * const argv[],
106 const char * options));
107 extern char * icatalloc P((char * old, const char * new));
108 extern char * icpyalloc P((const char * string));
109 extern void ifree P((char * p));
110 extern char * imalloc P((int n));
111 extern void * irealloc P((void * old, int n));
112 extern int link P((const char * fromname, const char * toname));
113 extern char * optarg;
114 extern int optind;
115 extern char * scheck P((const char * string, const char * format));
116
117 static void addtt P((time_t starttime, int type));
118 static int addtype P((long gmtoff, const char * abbr, int isdst,
119 int ttisstd, int ttisgmt));
120 static void leapadd P((time_t t, int positive, int rolling, int count));
121 static void adjleap P((void));
122 static void associate P((void));
123 static int ciequal P((const char * ap, const char * bp));
124 static void convert P((long val, char * buf));
125 static void dolink P((const char * fromfile, const char * tofile));
126 static void doabbr P((char * abbr, const char * format,
127 const char * letters, int isdst));
128 static void eat P((const char * name, int num));
129 static void eats P((const char * name, int num,
130 const char * rname, int rnum));
131 static long eitol P((int i));
132 static void error P((const char * message));
133 static char ** getfields P((char * buf));
134 static long gethms P((const char * string, const char * errstrng,
135 int signable));
136 static void infile P((const char * filename));
137 static void inleap P((char ** fields, int nfields));
138 static void inlink P((char ** fields, int nfields));
139 static void inrule P((char ** fields, int nfields));
140 static int inzcont P((char ** fields, int nfields));
141 static int inzone P((char ** fields, int nfields));
142 static int inzsub P((char ** fields, int nfields, int iscont));
143 static int itsabbr P((const char * abbr, const char * word));
144 static int itsdir P((const char * name));
145 static int lowerit P((int c));
146 static char * memcheck P((char * tocheck));
147 static int mkdirs P((char * filename));
148 static void newabbr P((const char * abbr));
149 static long oadd P((long t1, long t2));
150 static void outzone P((const struct zone * zp, int ntzones));
151 static void puttzcode P((long code, FILE * fp));
152 static int rcomp P((const void * leftp, const void * rightp));
153 static time_t rpytime P((const struct rule * rp, int wantedy));
154 static void rulesub P((struct rule * rp,
155 const char * loyearp, const char * hiyearp,
156 const char * typep, const char * monthp,
157 const char * dayp, const char * timep));
158 static void setboundaries P((void));
159 static time_t tadd P((time_t t1, long t2));
160 static void usage P((void));
161 static void writezone P((const char * name));
162 static int yearistype P((int year, const char * type));
163
164 #if !(HAVE_STRERROR - 0)
165 static char * strerror P((int));
166 #endif /* !(HAVE_STRERROR - 0) */
167
168 static int charcnt;
169 static int errors;
170 static const char * filename;
171 static int leapcnt;
172 static int linenum;
173 static time_t max_time;
174 static int max_year;
175 static time_t min_time;
176 static int min_year;
177 static int noise;
178 static const char * rfilename;
179 static int rlinenum;
180 static const char * progname;
181 static int timecnt;
182 static int typecnt;
183
184 /*
185 ** Line codes.
186 */
187
188 #define LC_RULE 0
189 #define LC_ZONE 1
190 #define LC_LINK 2
191 #define LC_LEAP 3
192
193 /*
194 ** Which fields are which on a Zone line.
195 */
196
197 #define ZF_NAME 1
198 #define ZF_GMTOFF 2
199 #define ZF_RULE 3
200 #define ZF_FORMAT 4
201 #define ZF_TILYEAR 5
202 #define ZF_TILMONTH 6
203 #define ZF_TILDAY 7
204 #define ZF_TILTIME 8
205 #define ZONE_MINFIELDS 5
206 #define ZONE_MAXFIELDS 9
207
208 /*
209 ** Which fields are which on a Zone continuation line.
210 */
211
212 #define ZFC_GMTOFF 0
213 #define ZFC_RULE 1
214 #define ZFC_FORMAT 2
215 #define ZFC_TILYEAR 3
216 #define ZFC_TILMONTH 4
217 #define ZFC_TILDAY 5
218 #define ZFC_TILTIME 6
219 #define ZONEC_MINFIELDS 3
220 #define ZONEC_MAXFIELDS 7
221
222 /*
223 ** Which files are which on a Rule line.
224 */
225
226 #define RF_NAME 1
227 #define RF_LOYEAR 2
228 #define RF_HIYEAR 3
229 #define RF_COMMAND 4
230 #define RF_MONTH 5
231 #define RF_DAY 6
232 #define RF_TOD 7
233 #define RF_STDOFF 8
234 #define RF_ABBRVAR 9
235 #define RULE_FIELDS 10
236
237 /*
238 ** Which fields are which on a Link line.
239 */
240
241 #define LF_FROM 1
242 #define LF_TO 2
243 #define LINK_FIELDS 3
244
245 /*
246 ** Which fields are which on a Leap line.
247 */
248
249 #define LP_YEAR 1
250 #define LP_MONTH 2
251 #define LP_DAY 3
252 #define LP_TIME 4
253 #define LP_CORR 5
254 #define LP_ROLL 6
255 #define LEAP_FIELDS 7
256
257 /*
258 ** Year synonyms.
259 */
260
261 #define YR_MINIMUM 0
262 #define YR_MAXIMUM 1
263 #define YR_ONLY 2
264
265 static struct rule * rules;
266 static int nrules; /* number of rules */
267
268 static struct zone * zones;
269 static int nzones; /* number of zones */
270
271 struct link {
272 const char * l_filename;
273 int l_linenum;
274 const char * l_from;
275 const char * l_to;
276 };
277
278 static struct link * links;
279 static int nlinks;
280
281 struct lookup {
282 const char * l_word;
283 const int l_value;
284 };
285
286 static struct lookup const * byword P((const char * string,
287 const struct lookup * lp));
288
289 static struct lookup const line_codes[] = {
290 { "Rule", LC_RULE },
291 { "Zone", LC_ZONE },
292 { "Link", LC_LINK },
293 { "Leap", LC_LEAP },
294 { NULL, 0}
295 };
296
297 static struct lookup const mon_names[] = {
298 { "January", TM_JANUARY },
299 { "February", TM_FEBRUARY },
300 { "March", TM_MARCH },
301 { "April", TM_APRIL },
302 { "May", TM_MAY },
303 { "June", TM_JUNE },
304 { "July", TM_JULY },
305 { "August", TM_AUGUST },
306 { "September", TM_SEPTEMBER },
307 { "October", TM_OCTOBER },
308 { "November", TM_NOVEMBER },
309 { "December", TM_DECEMBER },
310 { NULL, 0 }
311 };
312
313 static struct lookup const wday_names[] = {
314 { "Sunday", TM_SUNDAY },
315 { "Monday", TM_MONDAY },
316 { "Tuesday", TM_TUESDAY },
317 { "Wednesday", TM_WEDNESDAY },
318 { "Thursday", TM_THURSDAY },
319 { "Friday", TM_FRIDAY },
320 { "Saturday", TM_SATURDAY },
321 { NULL, 0 }
322 };
323
324 static struct lookup const lasts[] = {
325 { "last-Sunday", TM_SUNDAY },
326 { "last-Monday", TM_MONDAY },
327 { "last-Tuesday", TM_TUESDAY },
328 { "last-Wednesday", TM_WEDNESDAY },
329 { "last-Thursday", TM_THURSDAY },
330 { "last-Friday", TM_FRIDAY },
331 { "last-Saturday", TM_SATURDAY },
332 { NULL, 0 }
333 };
334
335 static struct lookup const begin_years[] = {
336 { "minimum", YR_MINIMUM },
337 { "maximum", YR_MAXIMUM },
338 { NULL, 0 }
339 };
340
341 static struct lookup const end_years[] = {
342 { "minimum", YR_MINIMUM },
343 { "maximum", YR_MAXIMUM },
344 { "only", YR_ONLY },
345 { NULL, 0 }
346 };
347
348 static struct lookup const leap_types[] = {
349 { "Rolling", TRUE },
350 { "Stationary", FALSE },
351 { NULL, 0 }
352 };
353
354 static const int len_months[2][MONSPERYEAR] = {
355 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
356 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
357 };
358
359 static const int len_years[2] = {
360 DAYSPERNYEAR, DAYSPERLYEAR
361 };
362
363 static struct attype {
364 time_t at;
365 unsigned char type;
366 } attypes[TZ_MAX_TIMES];
367 static long gmtoffs[TZ_MAX_TYPES];
368 static char isdsts[TZ_MAX_TYPES];
369 static unsigned char abbrinds[TZ_MAX_TYPES];
370 static char ttisstds[TZ_MAX_TYPES];
371 static char ttisgmts[TZ_MAX_TYPES];
372 static char chars[TZ_MAX_CHARS];
373 static time_t trans[TZ_MAX_LEAPS];
374 static long corr[TZ_MAX_LEAPS];
375 static char roll[TZ_MAX_LEAPS];
376
377 /*
378 ** Memory allocation.
379 */
380
381 static char *
382 memcheck(ptr)
383 char * const ptr;
384 {
385 if (ptr == NULL) {
386 const char *e = strerror(errno);
387
388 (void) fprintf(stderr, _("%s: Memory exhausted: %s\n"),
389 progname, e);
390 (void) exit(EXIT_FAILURE);
391 }
392 return ptr;
393 }
394
395 #define emalloc(size) memcheck(imalloc(size))
396 #define erealloc(ptr, size) memcheck(irealloc((ptr), (size)))
397 #define ecpyalloc(ptr) memcheck(icpyalloc(ptr))
398 #define ecatalloc(oldp, newp) memcheck(icatalloc((oldp), (newp)))
399
400 /*
401 ** Error handling.
402 */
403
404 #if !(HAVE_STRERROR - 0)
405 static char *
406 strerror(errnum)
407 int errnum;
408 {
409 extern char * sys_errlist[];
410 extern int sys_nerr;
411
412 return (errnum > 0 && errnum <= sys_nerr) ?
413 sys_errlist[errnum] : "Unknown system error";
414 }
415 #endif /* !(HAVE_STRERROR - 0) */
416
417 static void
418 eats(name, num, rname, rnum)
419 const char * const name;
420 const int num;
421 const char * const rname;
422 const int rnum;
423 {
424 filename = name;
425 linenum = num;
426 rfilename = rname;
427 rlinenum = rnum;
428 }
429
430 static void
431 eat(name, num)
432 const char * const name;
433 const int num;
434 {
435 eats(name, num, (char *) NULL, -1);
436 }
437
438 static void
439 error(string)
440 const char * const string;
441 {
442 /*
443 ** Match the format of "cc" to allow sh users to
444 ** zic ... 2>&1 | error -t "*" -v
445 ** on BSD systems.
446 */
447 (void) fprintf(stderr, _("\"%s\", line %d: %s"),
448 filename, linenum, string);
449 if (rfilename != NULL)
450 (void) fprintf(stderr, _(" (rule from \"%s\", line %d)"),
451 rfilename, rlinenum);
452 (void) fprintf(stderr, "\n");
453 ++errors;
454 }
455
456 static void
457 warning(string)
458 const char * const string;
459 {
460 char * cp;
461
462 cp = ecpyalloc("warning: ");
463 cp = ecatalloc(cp, string);
464 error(string);
465 ifree(cp);
466 --errors;
467 }
468
469 static void
470 usage P((void))
471 {
472 (void) fprintf(stderr, _("%s: usage is %s [ -s ] [ -v ] [ -l localtime ] [ -p posixrules ] [ -d directory ]\n\t[ -L leapseconds ] [ -y yearistype ] [ filename ... ]\n"),
473 progname, progname);
474 (void) exit(EXIT_FAILURE);
475 }
476
477 static const char * psxrules;
478 static const char * lcltime;
479 static const char * directory;
480 static const char * leapsec;
481 static const char * yitcommand;
482 static int sflag = FALSE;
483
484 int
485 main(argc, argv)
486 int argc;
487 char * argv[];
488 {
489 register int i;
490 register int j;
491 register int c;
492
493 #ifdef unix
494 (void) umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
495 #endif /* defined unix */
496 #if HAVE_GETTEXT - 0
497 (void) setlocale(LC_MESSAGES, "");
498 #ifdef TZ_DOMAINDIR
499 (void) bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
500 #endif /* defined TEXTDOMAINDIR */
501 (void) textdomain(TZ_DOMAIN);
502 #endif /* HAVE_GETTEXT - 0 */
503 progname = argv[0];
504 while ((c = getopt(argc, argv, "d:l:p:L:vsy:")) != -1)
505 switch (c) {
506 default:
507 usage();
508 case 'd':
509 if (directory == NULL)
510 directory = optarg;
511 else {
512 (void) fprintf(stderr,
513 _("%s: More than one -d option specified\n"),
514 progname);
515 (void) exit(EXIT_FAILURE);
516 }
517 break;
518 case 'l':
519 if (lcltime == NULL)
520 lcltime = optarg;
521 else {
522 (void) fprintf(stderr,
523 _("%s: More than one -l option specified\n"),
524 progname);
525 (void) exit(EXIT_FAILURE);
526 }
527 break;
528 case 'p':
529 if (psxrules == NULL)
530 psxrules = optarg;
531 else {
532 (void) fprintf(stderr,
533 _("%s: More than one -p option specified\n"),
534 progname);
535 (void) exit(EXIT_FAILURE);
536 }
537 break;
538 case 'y':
539 if (yitcommand == NULL)
540 yitcommand = optarg;
541 else {
542 (void) fprintf(stderr,
543 _("%s: More than one -y option specified\n"),
544 progname);
545 (void) exit(EXIT_FAILURE);
546 }
547 break;
548 case 'L':
549 if (leapsec == NULL)
550 leapsec = optarg;
551 else {
552 (void) fprintf(stderr,
553 _("%s: More than one -L option specified\n"),
554 progname);
555 (void) exit(EXIT_FAILURE);
556 }
557 break;
558 case 'v':
559 noise = TRUE;
560 break;
561 case 's':
562 sflag = TRUE;
563 break;
564 }
565 if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
566 usage(); /* usage message by request */
567 if (directory == NULL)
568 directory = TZDIR;
569 if (yitcommand == NULL)
570 yitcommand = "yearistype";
571
572 setboundaries();
573
574 if (optind < argc && leapsec != NULL) {
575 infile(leapsec);
576 adjleap();
577 }
578
579 for (i = optind; i < argc; ++i)
580 infile(argv[i]);
581 if (errors)
582 (void) exit(EXIT_FAILURE);
583 associate();
584 for (i = 0; i < nzones; i = j) {
585 /*
586 ** Find the next non-continuation zone entry.
587 */
588 for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
589 continue;
590 outzone(&zones[i], j - i);
591 }
592 /*
593 ** Make links.
594 */
595 for (i = 0; i < nlinks; ++i)
596 dolink(links[i].l_from, links[i].l_to);
597 if (lcltime != NULL)
598 dolink(lcltime, TZDEFAULT);
599 if (psxrules != NULL)
600 dolink(psxrules, TZDEFRULES);
601 return (errors == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
602 }
603
604 static void
605 dolink(fromfile, tofile)
606 const char * const fromfile;
607 const char * const tofile;
608 {
609 register char * fromname;
610 register char * toname;
611
612 if (fromfile[0] == '/')
613 fromname = ecpyalloc(fromfile);
614 else {
615 fromname = ecpyalloc(directory);
616 fromname = ecatalloc(fromname, "/");
617 fromname = ecatalloc(fromname, fromfile);
618 }
619 if (tofile[0] == '/')
620 toname = ecpyalloc(tofile);
621 else {
622 toname = ecpyalloc(directory);
623 toname = ecatalloc(toname, "/");
624 toname = ecatalloc(toname, tofile);
625 }
626 /*
627 ** We get to be careful here since
628 ** there's a fair chance of root running us.
629 */
630 if (!itsdir(toname))
631 (void) remove(toname);
632 if (link(fromname, toname) != 0) {
633 if (mkdirs(toname) != 0)
634 (void) exit(EXIT_FAILURE);
635 if (link(fromname, toname) != 0) {
636 const char *e = strerror(errno);
637
638 (void) fprintf(stderr,
639 _("%s: Can't link from %s to %s: %s\n"),
640 progname, fromname, toname, e);
641 (void) exit(EXIT_FAILURE);
642 }
643 }
644 ifree(fromname);
645 ifree(toname);
646 }
647
648 #ifndef INT_MAX
649 #define INT_MAX ((int) (((unsigned)~0)>>1))
650 #endif /* !defined INT_MAX */
651
652 #ifndef INT_MIN
653 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
654 #endif /* !defined INT_MIN */
655
656 /*
657 ** The tz file format currently allows at most 32-bit quantities.
658 ** This restriction should be removed before signed 32-bit values
659 ** wrap around in 2038, but unfortunately this will require a
660 ** change to the tz file format.
661 */
662
663 #define MAX_BITS_IN_FILE 32
664 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
665
666 static void
667 setboundaries P((void))
668 {
669 if (TYPE_SIGNED(time_t)) {
670 min_time = ~ (time_t) 0;
671 min_time <<= TIME_T_BITS_IN_FILE - 1;
672 max_time = ~ (time_t) 0 - min_time;
673 if (sflag)
674 min_time = 0;
675 } else {
676 min_time = 0;
677 max_time = 2 - sflag;
678 max_time <<= TIME_T_BITS_IN_FILE - 1;
679 --max_time;
680 }
681 min_year = TM_YEAR_BASE + gmtime(&min_time)->tm_year;
682 max_year = TM_YEAR_BASE + gmtime(&max_time)->tm_year;
683 }
684
685 static int
686 itsdir(name)
687 const char * const name;
688 {
689 register char * myname;
690 register int accres;
691
692 myname = ecpyalloc(name);
693 myname = ecatalloc(myname, "/.");
694 accres = access(myname, F_OK);
695 ifree(myname);
696 return accres == 0;
697 }
698
699 /*
700 ** Associate sets of rules with zones.
701 */
702
703 /*
704 ** Sort by rule name.
705 */
706
707 static int
708 rcomp(cp1, cp2)
709 const void * cp1;
710 const void * cp2;
711 {
712 return strcmp(((const struct rule *) cp1)->r_name,
713 ((const struct rule *) cp2)->r_name);
714 }
715
716 static void
717 associate P((void))
718 {
719 register struct zone * zp;
720 register struct rule * rp;
721 register int base, out;
722 register int i, j;
723
724 if (nrules != 0) {
725 (void) qsort((void *) rules, (size_t) nrules,
726 (size_t) sizeof *rules, rcomp);
727 for (i = 0; i < nrules - 1; ++i) {
728 if (strcmp(rules[i].r_name,
729 rules[i + 1].r_name) != 0)
730 continue;
731 if (strcmp(rules[i].r_filename,
732 rules[i + 1].r_filename) == 0)
733 continue;
734 eat(rules[i].r_filename, rules[i].r_linenum);
735 warning(_("same rule name in multiple files"));
736 eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
737 warning(_("same rule name in multiple files"));
738 for (j = i + 2; j < nrules; ++j) {
739 if (strcmp(rules[i].r_name,
740 rules[j].r_name) != 0)
741 break;
742 if (strcmp(rules[i].r_filename,
743 rules[j].r_filename) == 0)
744 continue;
745 if (strcmp(rules[i + 1].r_filename,
746 rules[j].r_filename) == 0)
747 continue;
748 break;
749 }
750 i = j - 1;
751 }
752 }
753 for (i = 0; i < nzones; ++i) {
754 zp = &zones[i];
755 zp->z_rules = NULL;
756 zp->z_nrules = 0;
757 }
758 for (base = 0; base < nrules; base = out) {
759 rp = &rules[base];
760 for (out = base + 1; out < nrules; ++out)
761 if (strcmp(rp->r_name, rules[out].r_name) != 0)
762 break;
763 for (i = 0; i < nzones; ++i) {
764 zp = &zones[i];
765 if (strcmp(zp->z_rule, rp->r_name) != 0)
766 continue;
767 zp->z_rules = rp;
768 zp->z_nrules = out - base;
769 }
770 }
771 for (i = 0; i < nzones; ++i) {
772 zp = &zones[i];
773 if (zp->z_nrules == 0) {
774 /*
775 ** Maybe we have a local standard time offset.
776 */
777 eat(zp->z_filename, zp->z_linenum);
778 zp->z_stdoff = gethms(zp->z_rule, _("unruly zone"),
779 TRUE);
780 /*
781 ** Note, though, that if there's no rule,
782 ** a '%s' in the format is a bad thing.
783 */
784 if (strchr(zp->z_format, '%') != 0)
785 error(_("%s in ruleless zone"));
786 }
787 }
788 if (errors)
789 (void) exit(EXIT_FAILURE);
790 }
791
792 static void
793 infile(name)
794 const char * name;
795 {
796 register FILE * fp;
797 register char ** fields;
798 register char * cp;
799 register const struct lookup * lp;
800 register int nfields;
801 register int wantcont;
802 register int num;
803 char buf[BUFSIZ];
804
805 if (strcmp(name, "-") == 0) {
806 name = _("standard input");
807 fp = stdin;
808 } else if ((fp = fopen(name, "r")) == NULL) {
809 const char *e = strerror(errno);
810
811 (void) fprintf(stderr, _("%s: Can't open %s: %s\n"),
812 progname, name, e);
813 (void) exit(EXIT_FAILURE);
814 }
815 wantcont = FALSE;
816 for (num = 1; ; ++num) {
817 eat(name, num);
818 if (fgets(buf, (int) sizeof buf, fp) != buf)
819 break;
820 cp = strchr(buf, '\n');
821 if (cp == NULL) {
822 error(_("line too long"));
823 (void) exit(EXIT_FAILURE);
824 }
825 *cp = '\0';
826 fields = getfields(buf);
827 nfields = 0;
828 while (fields[nfields] != NULL) {
829 static char nada;
830
831 if (strcmp(fields[nfields], "-") == 0)
832 fields[nfields] = &nada;
833 ++nfields;
834 }
835 if (nfields == 0) {
836 /* nothing to do */
837 } else if (wantcont) {
838 wantcont = inzcont(fields, nfields);
839 } else {
840 lp = byword(fields[0], line_codes);
841 if (lp == NULL)
842 error(_("input line of unknown type"));
843 else switch ((int) (lp->l_value)) {
844 case LC_RULE:
845 inrule(fields, nfields);
846 wantcont = FALSE;
847 break;
848 case LC_ZONE:
849 wantcont = inzone(fields, nfields);
850 break;
851 case LC_LINK:
852 inlink(fields, nfields);
853 wantcont = FALSE;
854 break;
855 case LC_LEAP:
856 if (name != leapsec)
857 (void) fprintf(stderr,
858 _("%s: Leap line in non leap seconds file %s\n"),
859 progname, name);
860 else inleap(fields, nfields);
861 wantcont = FALSE;
862 break;
863 default: /* "cannot happen" */
864 (void) fprintf(stderr,
865 _("%s: panic: Invalid l_value %d\n"),
866 progname, lp->l_value);
867 (void) exit(EXIT_FAILURE);
868 }
869 }
870 ifree((char *) fields);
871 }
872 if (ferror(fp)) {
873 (void) fprintf(stderr, _("%s: Error reading %s\n"),
874 progname, filename);
875 (void) exit(EXIT_FAILURE);
876 }
877 if (fp != stdin && fclose(fp)) {
878 const char *e = strerror(errno);
879
880 (void) fprintf(stderr, _("%s: Error closing %s: %s\n"),
881 progname, filename, e);
882 (void) exit(EXIT_FAILURE);
883 }
884 if (wantcont)
885 error(_("expected continuation line not found"));
886 }
887
888 /*
889 ** Convert a string of one of the forms
890 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
891 ** into a number of seconds.
892 ** A null string maps to zero.
893 ** Call error with errstring and return zero on errors.
894 */
895
896 static long
897 gethms(string, errstring, signable)
898 const char * string;
899 const char * const errstring;
900 const int signable;
901 {
902 int hh, mm, ss, sign;
903
904 if (string == NULL || *string == '\0')
905 return 0;
906 if (!signable)
907 sign = 1;
908 else if (*string == '-') {
909 sign = -1;
910 ++string;
911 } else sign = 1;
912 if (sscanf(string, scheck(string, "%d"), &hh) == 1)
913 mm = ss = 0;
914 else if (sscanf(string, scheck(string, "%d:%d"), &hh, &mm) == 2)
915 ss = 0;
916 else if (sscanf(string, scheck(string, "%d:%d:%d"),
917 &hh, &mm, &ss) != 3) {
918 error(errstring);
919 return 0;
920 }
921 if (hh < 0 || hh >= HOURSPERDAY ||
922 mm < 0 || mm >= MINSPERHOUR ||
923 ss < 0 || ss > SECSPERMIN) {
924 error(errstring);
925 return 0;
926 }
927 return eitol(sign) *
928 (eitol(hh * MINSPERHOUR + mm) *
929 eitol(SECSPERMIN) + eitol(ss));
930 }
931
932 static void
933 inrule(fields, nfields)
934 register char ** const fields;
935 const int nfields;
936 {
937 static struct rule r;
938
939 if (nfields != RULE_FIELDS) {
940 error(_("wrong number of fields on Rule line"));
941 return;
942 }
943 if (*fields[RF_NAME] == '\0') {
944 error(_("nameless rule"));
945 return;
946 }
947 r.r_filename = filename;
948 r.r_linenum = linenum;
949 r.r_stdoff = gethms(fields[RF_STDOFF], _("invalid saved time"), TRUE);
950 rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
951 fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
952 r.r_name = ecpyalloc(fields[RF_NAME]);
953 r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
954 rules = (struct rule *) (void *) erealloc((char *) rules,
955 (int) ((nrules + 1) * sizeof *rules));
956 rules[nrules++] = r;
957 }
958
959 static int
960 inzone(fields, nfields)
961 register char ** const fields;
962 const int nfields;
963 {
964 register int i;
965 static char * buf;
966
967 if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
968 error(_("wrong number of fields on Zone line"));
969 return FALSE;
970 }
971 if (strcmp(fields[ZF_NAME], TZDEFAULT) == 0 && lcltime != NULL) {
972 buf = erealloc(buf, (int) (132 + strlen(TZDEFAULT)));
973 (void) sprintf(buf,
974 _("\"Zone %s\" line and -l option are mutually exclusive"),
975 TZDEFAULT);
976 error(buf);
977 return FALSE;
978 }
979 if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
980 buf = erealloc(buf, (int) (132 + strlen(TZDEFRULES)));
981 (void) sprintf(buf,
982 _("\"Zone %s\" line and -p option are mutually exclusive"),
983 TZDEFRULES);
984 error(buf);
985 return FALSE;
986 }
987 for (i = 0; i < nzones; ++i)
988 if (zones[i].z_name != NULL &&
989 strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
990 buf = erealloc(buf, (int) (132 +
991 strlen(fields[ZF_NAME]) +
992 strlen(zones[i].z_filename)));
993 (void) sprintf(buf,
994 _("duplicate zone name %s (file \"%s\", line %d)"),
995 fields[ZF_NAME],
996 zones[i].z_filename,
997 zones[i].z_linenum);
998 error(buf);
999 return FALSE;
1000 }
1001 return inzsub(fields, nfields, FALSE);
1002 }
1003
1004 static int
1005 inzcont(fields, nfields)
1006 register char ** const fields;
1007 const int nfields;
1008 {
1009 if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1010 error(_("wrong number of fields on Zone continuation line"));
1011 return FALSE;
1012 }
1013 return inzsub(fields, nfields, TRUE);
1014 }
1015
1016 static int
1017 inzsub(fields, nfields, iscont)
1018 register char ** const fields;
1019 const int nfields;
1020 const int iscont;
1021 {
1022 register char * cp;
1023 static struct zone z;
1024 register int i_gmtoff, i_rule, i_format;
1025 register int i_untilyear, i_untilmonth;
1026 register int i_untilday, i_untiltime;
1027 register int hasuntil;
1028
1029 if (iscont) {
1030 i_gmtoff = ZFC_GMTOFF;
1031 i_rule = ZFC_RULE;
1032 i_format = ZFC_FORMAT;
1033 i_untilyear = ZFC_TILYEAR;
1034 i_untilmonth = ZFC_TILMONTH;
1035 i_untilday = ZFC_TILDAY;
1036 i_untiltime = ZFC_TILTIME;
1037 z.z_name = NULL;
1038 } else {
1039 i_gmtoff = ZF_GMTOFF;
1040 i_rule = ZF_RULE;
1041 i_format = ZF_FORMAT;
1042 i_untilyear = ZF_TILYEAR;
1043 i_untilmonth = ZF_TILMONTH;
1044 i_untilday = ZF_TILDAY;
1045 i_untiltime = ZF_TILTIME;
1046 z.z_name = ecpyalloc(fields[ZF_NAME]);
1047 }
1048 z.z_filename = filename;
1049 z.z_linenum = linenum;
1050 z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid GMT offset"), TRUE);
1051 if ((cp = strchr(fields[i_format], '%')) != 0) {
1052 if (*++cp != 's' || strchr(cp, '%') != 0) {
1053 error(_("invalid abbreviation format"));
1054 return FALSE;
1055 }
1056 }
1057 z.z_rule = ecpyalloc(fields[i_rule]);
1058 z.z_format = ecpyalloc(fields[i_format]);
1059 hasuntil = nfields > i_untilyear;
1060 if (hasuntil) {
1061 z.z_untilrule.r_filename = filename;
1062 z.z_untilrule.r_linenum = linenum;
1063 rulesub(&z.z_untilrule,
1064 fields[i_untilyear],
1065 "only",
1066 "",
1067 (nfields > i_untilmonth) ?
1068 fields[i_untilmonth] : "Jan",
1069 (nfields > i_untilday) ? fields[i_untilday] : "1",
1070 (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1071 z.z_untiltime = rpytime(&z.z_untilrule,
1072 z.z_untilrule.r_loyear);
1073 if (iscont && nzones > 0 &&
1074 z.z_untiltime > min_time &&
1075 z.z_untiltime < max_time &&
1076 zones[nzones - 1].z_untiltime > min_time &&
1077 zones[nzones - 1].z_untiltime < max_time &&
1078 zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1079 error(_("Zone continuation line end time is not after end time of previous line"));
1080 return FALSE;
1081 }
1082 }
1083 zones = (struct zone *) (void *) erealloc((char *) zones,
1084 (int) ((nzones + 1) * sizeof *zones));
1085 zones[nzones++] = z;
1086 /*
1087 ** If there was an UNTIL field on this line,
1088 ** there's more information about the zone on the next line.
1089 */
1090 return hasuntil;
1091 }
1092
1093 static void
1094 inleap(fields, nfields)
1095 register char ** const fields;
1096 const int nfields;
1097 {
1098 register const char * cp;
1099 register const struct lookup * lp;
1100 register int i, j;
1101 int year, month, day;
1102 long dayoff, tod;
1103 time_t t;
1104
1105 if (nfields != LEAP_FIELDS) {
1106 error(_("wrong number of fields on Leap line"));
1107 return;
1108 }
1109 dayoff = 0;
1110 cp = fields[LP_YEAR];
1111 if (sscanf(cp, scheck(cp, "%d"), &year) != 1) {
1112 /*
1113 * Leapin' Lizards!
1114 */
1115 error(_("invalid leaping year"));
1116 return;
1117 }
1118 j = EPOCH_YEAR;
1119 while (j != year) {
1120 if (year > j) {
1121 i = len_years[isleap(j)];
1122 ++j;
1123 } else {
1124 --j;
1125 i = -len_years[isleap(j)];
1126 }
1127 dayoff = oadd(dayoff, eitol(i));
1128 }
1129 if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1130 error(_("invalid month name"));
1131 return;
1132 }
1133 month = lp->l_value;
1134 j = TM_JANUARY;
1135 while (j != month) {
1136 i = len_months[isleap(year)][j];
1137 dayoff = oadd(dayoff, eitol(i));
1138 ++j;
1139 }
1140 cp = fields[LP_DAY];
1141 if (sscanf(cp, scheck(cp, "%d"), &day) != 1 ||
1142 day <= 0 || day > len_months[isleap(year)][month]) {
1143 error(_("invalid day of month"));
1144 return;
1145 }
1146 dayoff = oadd(dayoff, eitol(day - 1));
1147 if (dayoff < 0 && !TYPE_SIGNED(time_t)) {
1148 error(_("time before zero"));
1149 return;
1150 }
1151 t = (time_t) dayoff * SECSPERDAY;
1152 /*
1153 ** Cheap overflow check.
1154 */
1155 if (t / SECSPERDAY != dayoff) {
1156 error(_("time overflow"));
1157 return;
1158 }
1159 tod = gethms(fields[LP_TIME], _("invalid time of day"), FALSE);
1160 cp = fields[LP_CORR];
1161 {
1162 register int positive;
1163 int count;
1164
1165 if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1166 positive = FALSE;
1167 count = 1;
1168 } else if (strcmp(cp, "--") == 0) {
1169 positive = FALSE;
1170 count = 2;
1171 } else if (strcmp(cp, "+") == 0) {
1172 positive = TRUE;
1173 count = 1;
1174 } else if (strcmp(cp, "++") == 0) {
1175 positive = TRUE;
1176 count = 2;
1177 } else {
1178 error(_("illegal CORRECTION field on Leap line"));
1179 return;
1180 }
1181 if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1182 error(_("illegal Rolling/Stationary field on Leap line"));
1183 return;
1184 }
1185 leapadd(tadd(t, tod), positive, lp->l_value, count);
1186 }
1187 }
1188
1189 static void
1190 inlink(fields, nfields)
1191 register char ** const fields;
1192 const int nfields;
1193 {
1194 struct link l;
1195
1196 if (nfields != LINK_FIELDS) {
1197 error(_("wrong number of fields on Link line"));
1198 return;
1199 }
1200 if (*fields[LF_FROM] == '\0') {
1201 error(_("blank FROM field on Link line"));
1202 return;
1203 }
1204 if (*fields[LF_TO] == '\0') {
1205 error(_("blank TO field on Link line"));
1206 return;
1207 }
1208 l.l_filename = filename;
1209 l.l_linenum = linenum;
1210 l.l_from = ecpyalloc(fields[LF_FROM]);
1211 l.l_to = ecpyalloc(fields[LF_TO]);
1212 links = (struct link *) (void *) erealloc((char *) links,
1213 (int) ((nlinks + 1) * sizeof *links));
1214 links[nlinks++] = l;
1215 }
1216
1217 static void
1218 rulesub(rp, loyearp, hiyearp, typep, monthp, dayp, timep)
1219 register struct rule * const rp;
1220 const char * const loyearp;
1221 const char * const hiyearp;
1222 const char * const typep;
1223 const char * const monthp;
1224 const char * const dayp;
1225 const char * const timep;
1226 {
1227 register const struct lookup * lp;
1228 register const char * cp;
1229 register char * dp;
1230 register char * ep;
1231
1232 if ((lp = byword(monthp, mon_names)) == NULL) {
1233 error(_("invalid month name"));
1234 return;
1235 }
1236 rp->r_month = lp->l_value;
1237 rp->r_todisstd = FALSE;
1238 rp->r_todisgmt = FALSE;
1239 dp = ecpyalloc(timep);
1240 if (*dp != '\0') {
1241 ep = dp + strlen(dp) - 1;
1242 switch (lowerit(*ep)) {
1243 case 's': /* Standard */
1244 rp->r_todisstd = TRUE;
1245 rp->r_todisgmt = FALSE;
1246 *ep = '\0';
1247 break;
1248 case 'w': /* Wall */
1249 rp->r_todisstd = FALSE;
1250 rp->r_todisgmt = FALSE;
1251 *ep = '\0';
1252 case 'g': /* Greenwich */
1253 case 'u': /* Universal */
1254 case 'z': /* Zulu */
1255 rp->r_todisstd = TRUE;
1256 rp->r_todisgmt = TRUE;
1257 *ep = '\0';
1258 break;
1259 }
1260 }
1261 rp->r_tod = gethms(dp, _("invalid time of day"), FALSE);
1262 ifree(dp);
1263 /*
1264 ** Year work.
1265 */
1266 cp = loyearp;
1267 lp = byword(cp, begin_years);
1268 if (lp != NULL) switch ((int) lp->l_value) {
1269 case YR_MINIMUM:
1270 rp->r_loyear = INT_MIN;
1271 break;
1272 case YR_MAXIMUM:
1273 rp->r_loyear = INT_MAX;
1274 break;
1275 default: /* "cannot happen" */
1276 (void) fprintf(stderr,
1277 _("%s: panic: Invalid l_value %d\n"),
1278 progname, lp->l_value);
1279 (void) exit(EXIT_FAILURE);
1280 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_loyear) != 1) {
1281 error(_("invalid starting year"));
1282 return;
1283 }
1284 cp = hiyearp;
1285 if ((lp = byword(cp, end_years)) != NULL) switch ((int) lp->l_value) {
1286 case YR_MINIMUM:
1287 rp->r_hiyear = INT_MIN;
1288 break;
1289 case YR_MAXIMUM:
1290 rp->r_hiyear = INT_MAX;
1291 break;
1292 case YR_ONLY:
1293 rp->r_hiyear = rp->r_loyear;
1294 break;
1295 default: /* "cannot happen" */
1296 (void) fprintf(stderr,
1297 _("%s: panic: Invalid l_value %d\n"),
1298 progname, lp->l_value);
1299 (void) exit(EXIT_FAILURE);
1300 } else if (sscanf(cp, scheck(cp, "%d"), &rp->r_hiyear) != 1) {
1301 error(_("invalid ending year"));
1302 return;
1303 }
1304 if (rp->r_loyear > rp->r_hiyear) {
1305 error(_("starting year greater than ending year"));
1306 return;
1307 }
1308 if (*typep == '\0')
1309 rp->r_yrtype = NULL;
1310 else {
1311 if (rp->r_loyear == rp->r_hiyear) {
1312 error(_("typed single year"));
1313 return;
1314 }
1315 rp->r_yrtype = ecpyalloc(typep);
1316 }
1317 /*
1318 ** Day work.
1319 ** Accept things such as:
1320 ** 1
1321 ** last-Sunday
1322 ** Sun<=20
1323 ** Sun>=7
1324 */
1325 dp = ecpyalloc(dayp);
1326 if ((lp = byword(dp, lasts)) != NULL) {
1327 rp->r_dycode = DC_DOWLEQ;
1328 rp->r_wday = lp->l_value;
1329 rp->r_dayofmonth = len_months[1][rp->r_month];
1330 } else {
1331 if ((ep = strchr(dp, '<')) != 0)
1332 rp->r_dycode = DC_DOWLEQ;
1333 else if ((ep = strchr(dp, '>')) != 0)
1334 rp->r_dycode = DC_DOWGEQ;
1335 else {
1336 ep = dp;
1337 rp->r_dycode = DC_DOM;
1338 }
1339 if (rp->r_dycode != DC_DOM) {
1340 *ep++ = 0;
1341 if (*ep++ != '=') {
1342 error(_("invalid day of month"));
1343 ifree(dp);
1344 return;
1345 }
1346 if ((lp = byword(dp, wday_names)) == NULL) {
1347 error(_("invalid weekday name"));
1348 ifree(dp);
1349 return;
1350 }
1351 rp->r_wday = lp->l_value;
1352 }
1353 if (sscanf(ep, scheck(ep, "%d"), &rp->r_dayofmonth) != 1 ||
1354 rp->r_dayofmonth <= 0 ||
1355 (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1356 error(_("invalid day of month"));
1357 ifree(dp);
1358 return;
1359 }
1360 }
1361 ifree(dp);
1362 }
1363
1364 static void
1365 convert(val, buf)
1366 const long val;
1367 char * const buf;
1368 {
1369 register int i;
1370 register long shift;
1371
1372 for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1373 buf[i] = val >> shift;
1374 }
1375
1376 static void
1377 puttzcode(val, fp)
1378 const long val;
1379 FILE * const fp;
1380 {
1381 char buf[4];
1382
1383 convert(val, buf);
1384 (void) fwrite((void *) buf, (size_t) sizeof buf, (size_t) 1, fp);
1385 }
1386
1387 static int
1388 atcomp(avp, bvp)
1389 void * avp;
1390 void * bvp;
1391 {
1392 if (((struct attype *) avp)->at < ((struct attype *) bvp)->at)
1393 return -1;
1394 else if (((struct attype *) avp)->at > ((struct attype *) bvp)->at)
1395 return 1;
1396 else return 0;
1397 }
1398
1399 static void
1400 writezone(name)
1401 const char * const name;
1402 {
1403 register FILE * fp;
1404 register int i, j;
1405 static char * fullname;
1406 static struct tzhead tzh;
1407 time_t ats[TZ_MAX_TIMES];
1408 unsigned char types[TZ_MAX_TIMES];
1409
1410 /*
1411 ** Sort.
1412 */
1413 if (timecnt > 1)
1414 (void) qsort((void *) attypes, (size_t) timecnt,
1415 (size_t) sizeof *attypes, atcomp);
1416 /*
1417 ** Optimize.
1418 */
1419 {
1420 int fromi;
1421 int toi;
1422
1423 toi = 0;
1424 fromi = 0;
1425 if (isdsts[0] == 0)
1426 while (attypes[fromi].type == 0)
1427 ++fromi; /* handled by default rule */
1428 for ( ; fromi < timecnt; ++fromi) {
1429 if (toi != 0
1430 && ((attypes[fromi].at
1431 + gmtoffs[attypes[toi - 1].type])
1432 <= (attypes[toi - 1].at
1433 + gmtoffs[toi == 1 ? 0
1434 : attypes[toi - 2].type]))) {
1435 attypes[toi - 1].type = attypes[fromi].type;
1436 continue;
1437 }
1438 if (toi == 0 ||
1439 attypes[toi - 1].type != attypes[fromi].type)
1440 attypes[toi++] = attypes[fromi];
1441 }
1442 timecnt = toi;
1443 }
1444 /*
1445 ** Transfer.
1446 */
1447 for (i = 0; i < timecnt; ++i) {
1448 ats[i] = attypes[i].at;
1449 types[i] = attypes[i].type;
1450 }
1451 fullname = erealloc(fullname,
1452 (int) (strlen(directory) + 1 + strlen(name) + 1));
1453 (void) sprintf(fullname, "%s/%s", directory, name);
1454 /*
1455 ** Remove old file, if any, to snap links.
1456 */
1457 if (!itsdir(fullname) && remove(fullname) != 0 && errno != ENOENT) {
1458 const char *e = strerror(errno);
1459
1460 (void) fprintf(stderr, _("%s: Can't remove %s: %s\n"),
1461 progname, fullname, e);
1462 (void) exit(EXIT_FAILURE);
1463 }
1464 if ((fp = fopen(fullname, "wb")) == NULL) {
1465 if (mkdirs(fullname) != 0)
1466 (void) exit(EXIT_FAILURE);
1467 if ((fp = fopen(fullname, "wb")) == NULL) {
1468 const char *e = strerror(errno);
1469
1470 (void) fprintf(stderr, _("%s: Can't create %s: %s\n"),
1471 progname, fullname, e);
1472 (void) exit(EXIT_FAILURE);
1473 }
1474 }
1475 convert(eitol(typecnt), tzh.tzh_ttisgmtcnt);
1476 convert(eitol(typecnt), tzh.tzh_ttisstdcnt);
1477 convert(eitol(leapcnt), tzh.tzh_leapcnt);
1478 convert(eitol(timecnt), tzh.tzh_timecnt);
1479 convert(eitol(typecnt), tzh.tzh_typecnt);
1480 convert(eitol(charcnt), tzh.tzh_charcnt);
1481 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1482 DO(tzh_reserved);
1483 DO(tzh_ttisgmtcnt);
1484 DO(tzh_ttisstdcnt);
1485 DO(tzh_leapcnt);
1486 DO(tzh_timecnt);
1487 DO(tzh_typecnt);
1488 DO(tzh_charcnt);
1489 #undef DO
1490 for (i = 0; i < timecnt; ++i) {
1491 j = leapcnt;
1492 while (--j >= 0)
1493 if (ats[i] >= trans[j]) {
1494 ats[i] = tadd(ats[i], corr[j]);
1495 break;
1496 }
1497 puttzcode((long) ats[i], fp);
1498 }
1499 if (timecnt > 0)
1500 (void) fwrite((void *) types, (size_t) sizeof types[0],
1501 (size_t) timecnt, fp);
1502 for (i = 0; i < typecnt; ++i) {
1503 puttzcode((long) gmtoffs[i], fp);
1504 (void) putc(isdsts[i], fp);
1505 (void) putc(abbrinds[i], fp);
1506 }
1507 if (charcnt != 0)
1508 (void) fwrite((void *) chars, (size_t) sizeof chars[0],
1509 (size_t) charcnt, fp);
1510 for (i = 0; i < leapcnt; ++i) {
1511 if (roll[i]) {
1512 if (timecnt == 0 || trans[i] < ats[0]) {
1513 j = 0;
1514 while (isdsts[j])
1515 if (++j >= typecnt) {
1516 j = 0;
1517 break;
1518 }
1519 } else {
1520 j = 1;
1521 while (j < timecnt && trans[i] >= ats[j])
1522 ++j;
1523 j = types[j - 1];
1524 }
1525 puttzcode((long) tadd(trans[i], -gmtoffs[j]), fp);
1526 } else puttzcode((long) trans[i], fp);
1527 puttzcode((long) corr[i], fp);
1528 }
1529 for (i = 0; i < typecnt; ++i)
1530 (void) putc(ttisstds[i], fp);
1531 for (i = 0; i < typecnt; ++i)
1532 (void) putc(ttisgmts[i], fp);
1533 if (ferror(fp) || fclose(fp)) {
1534 (void) fprintf(stderr, _("%s: Error writing %s\n"),
1535 progname, fullname);
1536 (void) exit(EXIT_FAILURE);
1537 }
1538 }
1539
1540 static void
1541 doabbr(abbr, format, letters, isdst)
1542 char * const abbr;
1543 const char * const format;
1544 const char * const letters;
1545 const int isdst;
1546 {
1547 if (strchr(format, '/') == NULL) {
1548 if (letters == NULL)
1549 (void) strcpy(abbr, format);
1550 else (void) sprintf(abbr, format, letters);
1551 } else if (isdst)
1552 (void) strcpy(abbr, strchr(format, '/') + 1);
1553 else {
1554 (void) strcpy(abbr, format);
1555 *strchr(abbr, '/') = '\0';
1556 }
1557 }
1558
1559 static void
1560 outzone(zpfirst, zonecount)
1561 const struct zone * const zpfirst;
1562 const int zonecount;
1563 {
1564 register const struct zone * zp;
1565 register struct rule * rp;
1566 register int i, j;
1567 register int usestart, useuntil;
1568 register time_t starttime, untiltime;
1569 register long gmtoff;
1570 register long stdoff;
1571 register int year;
1572 register long startoff;
1573 register int startttisstd;
1574 register int startttisgmt;
1575 register int type;
1576 char startbuf[BUFSIZ];
1577
1578 INITIALIZE(untiltime);
1579 INITIALIZE(starttime);
1580 /*
1581 ** Now. . .finally. . .generate some useful data!
1582 */
1583 timecnt = 0;
1584 typecnt = 0;
1585 charcnt = 0;
1586 /*
1587 ** A guess that may well be corrected later.
1588 */
1589 stdoff = 0;
1590 /*
1591 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1592 ** for noting the need to unconditionally initialize startttisstd.
1593 */
1594 startttisstd = FALSE;
1595 startttisgmt = FALSE;
1596 for (i = 0; i < zonecount; ++i) {
1597 zp = &zpfirst[i];
1598 usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
1599 useuntil = i < (zonecount - 1);
1600 if (useuntil && zp->z_untiltime <= min_time)
1601 continue;
1602 gmtoff = zp->z_gmtoff;
1603 eat(zp->z_filename, zp->z_linenum);
1604 *startbuf = '\0';
1605 startoff = zp->z_gmtoff;
1606 if (zp->z_nrules == 0) {
1607 stdoff = zp->z_stdoff;
1608 doabbr(startbuf, zp->z_format,
1609 (char *) NULL, stdoff != 0);
1610 type = addtype(oadd(zp->z_gmtoff, stdoff),
1611 startbuf, stdoff != 0, startttisstd,
1612 startttisgmt);
1613 if (usestart) {
1614 addtt(starttime, type);
1615 usestart = FALSE;
1616 }
1617 else if (stdoff != 0)
1618 addtt(min_time, type);
1619 } else for (year = min_year; year <= max_year; ++year) {
1620 if (useuntil && year > zp->z_untilrule.r_hiyear)
1621 break;
1622 /*
1623 ** Mark which rules to do in the current year.
1624 ** For those to do, calculate rpytime(rp, year);
1625 */
1626 for (j = 0; j < zp->z_nrules; ++j) {
1627 rp = &zp->z_rules[j];
1628 eats(zp->z_filename, zp->z_linenum,
1629 rp->r_filename, rp->r_linenum);
1630 rp->r_todo = year >= rp->r_loyear &&
1631 year <= rp->r_hiyear &&
1632 yearistype(year, rp->r_yrtype);
1633 if (rp->r_todo)
1634 rp->r_temp = rpytime(rp, year);
1635 }
1636 for ( ; ; ) {
1637 register int k;
1638 register time_t jtime, ktime;
1639 register long offset;
1640 char buf[BUFSIZ];
1641
1642 INITIALIZE(ktime);
1643 if (useuntil) {
1644 /*
1645 ** Turn untiltime into GMT
1646 ** assuming the current gmtoff and
1647 ** stdoff values.
1648 */
1649 untiltime = zp->z_untiltime;
1650 if (!zp->z_untilrule.r_todisgmt)
1651 untiltime = tadd(untiltime,
1652 -gmtoff);
1653 if (!zp->z_untilrule.r_todisstd)
1654 untiltime = tadd(untiltime,
1655 -stdoff);
1656 }
1657 /*
1658 ** Find the rule (of those to do, if any)
1659 ** that takes effect earliest in the year.
1660 */
1661 k = -1;
1662 for (j = 0; j < zp->z_nrules; ++j) {
1663 rp = &zp->z_rules[j];
1664 if (!rp->r_todo)
1665 continue;
1666 eats(zp->z_filename, zp->z_linenum,
1667 rp->r_filename, rp->r_linenum);
1668 offset = rp->r_todisgmt ? 0 : gmtoff;
1669 if (!rp->r_todisstd)
1670 offset = oadd(offset, stdoff);
1671 jtime = rp->r_temp;
1672 if (jtime == min_time ||
1673 jtime == max_time)
1674 continue;
1675 jtime = tadd(jtime, -offset);
1676 if (k < 0 || jtime < ktime) {
1677 k = j;
1678 ktime = jtime;
1679 }
1680 }
1681 if (k < 0)
1682 break; /* go on to next year */
1683 rp = &zp->z_rules[k];
1684 rp->r_todo = FALSE;
1685 if (useuntil && ktime >= untiltime)
1686 break;
1687 stdoff = rp->r_stdoff;
1688 if (usestart && ktime == starttime)
1689 usestart = FALSE;
1690 if (usestart) {
1691 if (ktime < starttime) {
1692 startoff = oadd(zp->z_gmtoff,
1693 stdoff);
1694 doabbr(startbuf, zp->z_format,
1695 rp->r_abbrvar,
1696 rp->r_stdoff != 0);
1697 continue;
1698 }
1699 if (*startbuf == '\0' &&
1700 startoff == oadd(zp->z_gmtoff,
1701 stdoff)) {
1702 doabbr(startbuf, zp->z_format,
1703 rp->r_abbrvar,
1704 rp->r_stdoff != 0);
1705 }
1706 }
1707 eats(zp->z_filename, zp->z_linenum,
1708 rp->r_filename, rp->r_linenum);
1709 doabbr(buf, zp->z_format, rp->r_abbrvar,
1710 rp->r_stdoff != 0);
1711 offset = oadd(zp->z_gmtoff, rp->r_stdoff);
1712 type = addtype(offset, buf, rp->r_stdoff != 0,
1713 rp->r_todisstd, rp->r_todisgmt);
1714 addtt(ktime, type);
1715 }
1716 }
1717 if (usestart) {
1718 if (*startbuf == '\0' &&
1719 zp->z_format != NULL &&
1720 strchr(zp->z_format, '%') == NULL &&
1721 strchr(zp->z_format, '/') == NULL)
1722 (void) strcpy(startbuf, zp->z_format);
1723 eat(zp->z_filename, zp->z_linenum);
1724 if (*startbuf == '\0')
1725 error(_("can't determine time zone abbreviation to use just after until time"));
1726 else addtt(starttime,
1727 addtype(startoff, startbuf,
1728 startoff != zp->z_gmtoff,
1729 startttisstd,
1730 startttisgmt));
1731 }
1732 /*
1733 ** Now we may get to set starttime for the next zone line.
1734 */
1735 if (useuntil) {
1736 startttisstd = zp->z_untilrule.r_todisstd;
1737 startttisgmt = zp->z_untilrule.r_todisgmt;
1738 starttime = zp->z_untiltime;
1739 if (!startttisstd)
1740 starttime = tadd(starttime, -stdoff);
1741 if (!startttisgmt)
1742 starttime = tadd(starttime, -gmtoff);
1743 }
1744 }
1745 writezone(zpfirst->z_name);
1746 }
1747
1748 static void
1749 addtt(starttime, type)
1750 const time_t starttime;
1751 const int type;
1752 {
1753 if (timecnt >= TZ_MAX_TIMES) {
1754 error(_("too many transitions?!"));
1755 (void) exit(EXIT_FAILURE);
1756 }
1757 attypes[timecnt].at = starttime;
1758 attypes[timecnt].type = type;
1759 ++timecnt;
1760 }
1761
1762 static int
1763 addtype(gmtoff, abbr, isdst, ttisstd, ttisgmt)
1764 const long gmtoff;
1765 const char * const abbr;
1766 const int isdst;
1767 const int ttisstd;
1768 const int ttisgmt;
1769 {
1770 register int i, j;
1771
1772 if (isdst != TRUE && isdst != FALSE) {
1773 error(_("internal error - addtype called with bad isdst"));
1774 (void) exit(EXIT_FAILURE);
1775 }
1776 if (ttisstd != TRUE && ttisstd != FALSE) {
1777 error(_("internal error - addtype called with bad ttisstd"));
1778 (void) exit(EXIT_FAILURE);
1779 }
1780 if (ttisgmt != TRUE && ttisgmt != FALSE) {
1781 error(_("internal error - addtype called with bad ttisgmt"));
1782 (void) exit(EXIT_FAILURE);
1783 }
1784 /*
1785 ** See if there's already an entry for this zone type.
1786 ** If so, just return its index.
1787 */
1788 for (i = 0; i < typecnt; ++i) {
1789 if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
1790 strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
1791 ttisstd == ttisstds[i] &&
1792 ttisgmt == ttisgmts[i])
1793 return i;
1794 }
1795 /*
1796 ** There isn't one; add a new one, unless there are already too
1797 ** many.
1798 */
1799 if (typecnt >= TZ_MAX_TYPES) {
1800 error(_("too many local time types"));
1801 (void) exit(EXIT_FAILURE);
1802 }
1803 gmtoffs[i] = gmtoff;
1804 isdsts[i] = isdst;
1805 ttisstds[i] = ttisstd;
1806 ttisgmts[i] = ttisgmt;
1807
1808 for (j = 0; j < charcnt; ++j)
1809 if (strcmp(&chars[j], abbr) == 0)
1810 break;
1811 if (j == charcnt)
1812 newabbr(abbr);
1813 abbrinds[i] = j;
1814 ++typecnt;
1815 return i;
1816 }
1817
1818 static void
1819 leapadd(t, positive, rolling, count)
1820 const time_t t;
1821 const int positive;
1822 const int rolling;
1823 int count;
1824 {
1825 register int i, j;
1826
1827 if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
1828 error(_("too many leap seconds"));
1829 (void) exit(EXIT_FAILURE);
1830 }
1831 for (i = 0; i < leapcnt; ++i)
1832 if (t <= trans[i]) {
1833 if (t == trans[i]) {
1834 error(_("repeated leap second moment"));
1835 (void) exit(EXIT_FAILURE);
1836 }
1837 break;
1838 }
1839 do {
1840 for (j = leapcnt; j > i; --j) {
1841 trans[j] = trans[j - 1];
1842 corr[j] = corr[j - 1];
1843 roll[j] = roll[j - 1];
1844 }
1845 trans[i] = t;
1846 corr[i] = positive ? 1L : eitol(-count);
1847 roll[i] = rolling;
1848 ++leapcnt;
1849 } while (positive && --count != 0);
1850 }
1851
1852 static void
1853 adjleap P((void))
1854 {
1855 register int i;
1856 register long last = 0;
1857
1858 /*
1859 ** propagate leap seconds forward
1860 */
1861 for (i = 0; i < leapcnt; ++i) {
1862 trans[i] = tadd(trans[i], last);
1863 last = corr[i] += last;
1864 }
1865 }
1866
1867 static int
1868 yearistype(year, type)
1869 const int year;
1870 const char * const type;
1871 {
1872 static char * buf;
1873 int result;
1874
1875 if (type == NULL || *type == '\0')
1876 return TRUE;
1877 buf = erealloc(buf, (int) (132 + strlen(yitcommand) + strlen(type)));
1878 (void) sprintf(buf, "%s %d %s", yitcommand, year, type);
1879 result = system(buf);
1880 if (result == 0)
1881 return TRUE;
1882 if (result == (1 << 8))
1883 return FALSE;
1884 error(_("Wild result from command execution"));
1885 (void) fprintf(stderr, _("%s: command was '%s', result was %d\n"),
1886 progname, buf, result);
1887 for ( ; ; )
1888 (void) exit(EXIT_FAILURE);
1889 }
1890
1891 static int
1892 lowerit(a)
1893 int a;
1894 {
1895 a = (unsigned char) a;
1896 return (isascii(a) && isupper(a)) ? tolower(a) : a;
1897 }
1898
1899 static int
1900 ciequal(ap, bp) /* case-insensitive equality */
1901 register const char * ap;
1902 register const char * bp;
1903 {
1904 while (lowerit(*ap) == lowerit(*bp++))
1905 if (*ap++ == '\0')
1906 return TRUE;
1907 return FALSE;
1908 }
1909
1910 static int
1911 itsabbr(abbr, word)
1912 register const char * abbr;
1913 register const char * word;
1914 {
1915 if (lowerit(*abbr) != lowerit(*word))
1916 return FALSE;
1917 ++word;
1918 while (*++abbr != '\0')
1919 do {
1920 if (*word == '\0')
1921 return FALSE;
1922 } while (lowerit(*word++) != lowerit(*abbr));
1923 return TRUE;
1924 }
1925
1926 static const struct lookup *
1927 byword(word, table)
1928 register const char * const word;
1929 register const struct lookup * const table;
1930 {
1931 register const struct lookup * foundlp;
1932 register const struct lookup * lp;
1933
1934 if (word == NULL || table == NULL)
1935 return NULL;
1936 /*
1937 ** Look for exact match.
1938 */
1939 for (lp = table; lp->l_word != NULL; ++lp)
1940 if (ciequal(word, lp->l_word))
1941 return lp;
1942 /*
1943 ** Look for inexact match.
1944 */
1945 foundlp = NULL;
1946 for (lp = table; lp->l_word != NULL; ++lp)
1947 if (itsabbr(word, lp->l_word))
1948 if (foundlp == NULL)
1949 foundlp = lp;
1950 else return NULL; /* multiple inexact matches */
1951 return foundlp;
1952 }
1953
1954 static char **
1955 getfields(cp)
1956 register char * cp;
1957 {
1958 register char * dp;
1959 register char ** array;
1960 register int nsubs;
1961
1962 if (cp == NULL)
1963 return NULL;
1964 array = (char **) (void *)
1965 emalloc((int) ((strlen(cp) + 1) * sizeof *array));
1966 nsubs = 0;
1967 for ( ; ; ) {
1968 while (isascii(*cp) && isspace((unsigned char) *cp))
1969 ++cp;
1970 if (*cp == '\0' || *cp == '#')
1971 break;
1972 array[nsubs++] = dp = cp;
1973 do {
1974 if ((*dp = *cp++) != '"')
1975 ++dp;
1976 else while ((*dp = *cp++) != '"')
1977 if (*dp != '\0')
1978 ++dp;
1979 else error(_("Odd number of quotation marks"));
1980 } while (*cp != '\0' && *cp != '#' &&
1981 (!isascii(*cp) || !isspace((unsigned char) *cp)));
1982 if (isascii(*cp) && isspace((unsigned char) *cp))
1983 ++cp;
1984 *dp = '\0';
1985 }
1986 array[nsubs] = NULL;
1987 return array;
1988 }
1989
1990 static long
1991 oadd(t1, t2)
1992 const long t1;
1993 const long t2;
1994 {
1995 register long t;
1996
1997 t = t1 + t2;
1998 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
1999 error(_("time overflow"));
2000 (void) exit(EXIT_FAILURE);
2001 }
2002 return t;
2003 }
2004
2005 static time_t
2006 tadd(t1, t2)
2007 const time_t t1;
2008 const long t2;
2009 {
2010 register time_t t;
2011
2012 if (t1 == max_time && t2 > 0)
2013 return max_time;
2014 if (t1 == min_time && t2 < 0)
2015 return min_time;
2016 t = t1 + t2;
2017 if ((t2 > 0 && t <= t1) || (t2 < 0 && t >= t1)) {
2018 error(_("time overflow"));
2019 (void) exit(EXIT_FAILURE);
2020 }
2021 return t;
2022 }
2023
2024 /*
2025 ** Given a rule, and a year, compute the date - in seconds since January 1,
2026 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2027 */
2028
2029 static time_t
2030 rpytime(rp, wantedy)
2031 register const struct rule * const rp;
2032 register const int wantedy;
2033 {
2034 register int y, m, i;
2035 register long dayoff; /* with a nod to Margaret O. */
2036 register time_t t;
2037
2038 if (wantedy == INT_MIN)
2039 return min_time;
2040 if (wantedy == INT_MAX)
2041 return max_time;
2042 dayoff = 0;
2043 m = TM_JANUARY;
2044 y = EPOCH_YEAR;
2045 while (wantedy != y) {
2046 if (wantedy > y) {
2047 i = len_years[isleap(y)];
2048 ++y;
2049 } else {
2050 --y;
2051 i = -len_years[isleap(y)];
2052 }
2053 dayoff = oadd(dayoff, eitol(i));
2054 }
2055 while (m != rp->r_month) {
2056 i = len_months[isleap(y)][m];
2057 dayoff = oadd(dayoff, eitol(i));
2058 ++m;
2059 }
2060 i = rp->r_dayofmonth;
2061 if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
2062 if (rp->r_dycode == DC_DOWLEQ)
2063 --i;
2064 else {
2065 error(_("use of 2/29 in non leap-year"));
2066 (void) exit(EXIT_FAILURE);
2067 }
2068 }
2069 --i;
2070 dayoff = oadd(dayoff, eitol(i));
2071 if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
2072 register long wday;
2073
2074 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2075 wday = eitol(EPOCH_WDAY);
2076 /*
2077 ** Don't trust mod of negative numbers.
2078 */
2079 if (dayoff >= 0)
2080 wday = (wday + dayoff) % LDAYSPERWEEK;
2081 else {
2082 wday -= ((-dayoff) % LDAYSPERWEEK);
2083 if (wday < 0)
2084 wday += LDAYSPERWEEK;
2085 }
2086 while (wday != eitol(rp->r_wday))
2087 if (rp->r_dycode == DC_DOWGEQ) {
2088 dayoff = oadd(dayoff, (long) 1);
2089 if (++wday >= LDAYSPERWEEK)
2090 wday = 0;
2091 ++i;
2092 } else {
2093 dayoff = oadd(dayoff, (long) -1);
2094 if (--wday < 0)
2095 wday = LDAYSPERWEEK - 1;
2096 --i;
2097 }
2098 if (i < 0 || i >= len_months[isleap(y)][m]) {
2099 error(_("no day in month matches rule"));
2100 (void) exit(EXIT_FAILURE);
2101 }
2102 }
2103 if (dayoff < 0 && !TYPE_SIGNED(time_t))
2104 return min_time;
2105 t = (time_t) dayoff * SECSPERDAY;
2106 /*
2107 ** Cheap overflow check.
2108 */
2109 if (t / SECSPERDAY != dayoff)
2110 return (dayoff > 0) ? max_time : min_time;
2111 return tadd(t, rp->r_tod);
2112 }
2113
2114 static void
2115 newabbr(string)
2116 const char * const string;
2117 {
2118 register int i;
2119
2120 i = strlen(string) + 1;
2121 if (charcnt + i > TZ_MAX_CHARS) {
2122 error(_("too many, or too long, time zone abbreviations"));
2123 (void) exit(EXIT_FAILURE);
2124 }
2125 (void) strcpy(&chars[charcnt], string);
2126 charcnt += eitol(i);
2127 }
2128
2129 static int
2130 mkdirs(argname)
2131 char * const argname;
2132 {
2133 register char * name;
2134 register char * cp;
2135
2136 if (argname == NULL || *argname == '\0')
2137 return 0;
2138 cp = name = ecpyalloc(argname);
2139 while ((cp = strchr(cp + 1, '/')) != 0) {
2140 *cp = '\0';
2141 #ifndef unix
2142 /*
2143 ** DOS drive specifier?
2144 */
2145 if (isalpha((unsigned char) name[0]) &&
2146 name[1] == ':' && name[2] == '\0') {
2147 *cp = '/';
2148 continue;
2149 }
2150 #endif /* !defined unix */
2151 if (!itsdir(name)) {
2152 /*
2153 ** It doesn't seem to exist, so we try to create it.
2154 */
2155 if (mkdir(name, 0755) != 0) {
2156 const char *e = strerror(errno);
2157
2158 (void) fprintf(stderr,
2159 _("%s: Can't create directory %s: %s\n"),
2160 progname, name, e);
2161 ifree(name);
2162 return -1;
2163 }
2164 }
2165 *cp = '/';
2166 }
2167 ifree(name);
2168 return 0;
2169 }
2170
2171 static long
2172 eitol(i)
2173 const int i;
2174 {
2175 long l;
2176
2177 l = i;
2178 if ((i < 0 && l >= 0) || (i == 0 && l != 0) || (i > 0 && l <= 0)) {
2179 (void) fprintf(stderr,
2180 _("%s: %d did not sign extend correctly\n"),
2181 progname, i);
2182 (void) exit(EXIT_FAILURE);
2183 }
2184 return l;
2185 }
2186
2187 /*
2188 ** UNIX was a registered trademark of UNIX System Laboratories in 1993.
2189 */