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