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