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