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