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