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