]>
git.saurik.com Git - apple/system_cmds.git/blob - zic.tproj/zic.c
1 static const char elsieid
[] = "@(#)zic.c 7.116";
4 static const char rcsid
[] =
5 "$FreeBSD: src/usr.sbin/zic/zic.c,v 1.17 2004/10/19 20:30:09 ru Exp $";
12 #include <sys/stat.h> /* for umask manifest constants */
13 #include <sys/types.h>
16 #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
19 ** On some ancient hosts, predicates like `isspace(C)' are defined
20 ** only if isascii(C) || C == EOF. Modern hosts obey the C Standard,
21 ** which says they are defined only if C == ((unsigned char) C) || C == EOF.
22 ** Neither the C Standard nor POSIX require that `isascii' exist.
23 ** For portability, we check both ancient and modern requirements.
24 ** If isascii is not defined, the isascii check succeeds trivially.
32 const char * r_filename
;
36 int r_loyear
; /* for example, 1986 */
37 int r_hiyear
; /* for example, 1986 */
38 const char * r_yrtype
;
40 int r_month
; /* 0..11 */
42 int r_dycode
; /* see below */
46 long r_tod
; /* time from midnight */
47 int r_todisstd
; /* above is standard time if TRUE */
48 /* or wall clock time if FALSE */
49 int r_todisgmt
; /* above is GMT if TRUE */
50 /* or local time if FALSE */
51 long r_stdoff
; /* offset from standard time */
52 const char * r_abbrvar
; /* variable part of abbreviation */
54 int r_todo
; /* a rule to do (used in outzone) */
55 time_t r_temp
; /* used in outzone */
59 ** r_dycode r_dayofmonth r_wday
62 #define DC_DOM 0 /* 1..31 */ /* unused */
63 #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
64 #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
67 const char * z_filename
;
73 const char * z_format
;
77 struct rule
* z_rules
;
80 struct rule z_untilrule
;
84 static void addtt
P((time_t starttime
, int type
));
85 static int addtype
P((long gmtoff
, const char * abbr
, int isdst
,
86 int ttisstd
, int ttisgmt
));
87 static void leapadd
P((time_t t
, int positive
, int rolling
, int count
));
88 static void adjleap
P((void));
89 static void associate
P((void));
90 static int ciequal
P((const char * ap
, const char * bp
));
91 static void convert
P((long val
, char * buf
));
92 static void dolink
P((const char * fromfile
, const char * tofile
));
93 static void doabbr
P((char * abbr
, const char * format
,
94 const char * letters
, int isdst
));
95 static void eat
P((const char * name
, int num
));
96 static void eats
P((const char * name
, int num
,
97 const char * rname
, int rnum
));
98 static long eitol
P((int i
));
99 static void error
P((const char * message
));
100 static char ** getfields
P((char * buf
));
101 static long gethms
P((const char * string
, const char * errstrng
,
103 static void infile
P((const char * filename
));
104 static void inleap
P((char ** fields
, int nfields
));
105 static void inlink
P((char ** fields
, int nfields
));
106 static void inrule
P((char ** fields
, int nfields
));
107 static int inzcont
P((char ** fields
, int nfields
));
108 static int inzone
P((char ** fields
, int nfields
));
109 static int inzsub
P((char ** fields
, int nfields
, int iscont
));
110 static int itsabbr
P((const char * abbr
, const char * word
));
111 static int itsdir
P((const char * name
));
112 static int lowerit
P((int c
));
113 static char * memcheck
P((char * tocheck
));
114 static int mkdirs
P((char * filename
));
115 static void newabbr
P((const char * abbr
));
116 static long oadd
P((long t1
, long t2
));
117 static void outzone
P((const struct zone
* zp
, int ntzones
));
118 static void puttzcode
P((long code
, FILE * fp
));
119 static int rcomp
P((const void * leftp
, const void * rightp
));
120 static time_t rpytime
P((const struct rule
* rp
, int wantedy
));
121 static void rulesub
P((struct rule
* rp
,
122 const char * loyearp
, const char * hiyearp
,
123 const char * typep
, const char * monthp
,
124 const char * dayp
, const char * timep
));
125 static void setboundaries
P((void));
126 static void setgroup
P((gid_t
*flag
, const char *name
));
127 static void setuser
P((uid_t
*flag
, const char *name
));
128 static time_t tadd
P((time_t t1
, long t2
));
129 static void usage
P((void));
130 static void writezone
P((const char * name
));
131 static int yearistype
P((int year
, const char * type
));
133 #if !(HAVE_STRERROR - 0)
134 static char * strerror
P((int));
135 #endif /* !(HAVE_STRERROR - 0) */
139 static const char * filename
;
142 static time_t max_time
;
144 static int max_year_representable
;
145 static time_t min_time
;
147 static int min_year_representable
;
149 static const char * rfilename
;
164 ** Which fields are which on a Zone line.
172 #define ZF_TILMONTH 6
175 #define ZONE_MINFIELDS 5
176 #define ZONE_MAXFIELDS 9
179 ** Which fields are which on a Zone continuation line.
185 #define ZFC_TILYEAR 3
186 #define ZFC_TILMONTH 4
188 #define ZFC_TILTIME 6
189 #define ZONEC_MINFIELDS 3
190 #define ZONEC_MAXFIELDS 7
193 ** Which files are which on a Rule line.
205 #define RULE_FIELDS 10
208 ** Which fields are which on a Link line.
213 #define LINK_FIELDS 3
216 ** Which fields are which on a Leap line.
225 #define LEAP_FIELDS 7
235 static struct rule
* rules
;
236 static int nrules
; /* number of rules */
238 static struct zone
* zones
;
239 static int nzones
; /* number of zones */
242 const char * l_filename
;
248 static struct link
* links
;
256 static struct lookup
const * byword
P((const char * string
,
257 const struct lookup
* lp
));
259 static struct lookup
const line_codes
[] = {
267 static struct lookup
const mon_names
[] = {
268 { "January", TM_JANUARY
},
269 { "February", TM_FEBRUARY
},
270 { "March", TM_MARCH
},
271 { "April", TM_APRIL
},
275 { "August", TM_AUGUST
},
276 { "September", TM_SEPTEMBER
},
277 { "October", TM_OCTOBER
},
278 { "November", TM_NOVEMBER
},
279 { "December", TM_DECEMBER
},
283 static struct lookup
const wday_names
[] = {
284 { "Sunday", TM_SUNDAY
},
285 { "Monday", TM_MONDAY
},
286 { "Tuesday", TM_TUESDAY
},
287 { "Wednesday", TM_WEDNESDAY
},
288 { "Thursday", TM_THURSDAY
},
289 { "Friday", TM_FRIDAY
},
290 { "Saturday", TM_SATURDAY
},
294 static struct lookup
const lasts
[] = {
295 { "last-Sunday", TM_SUNDAY
},
296 { "last-Monday", TM_MONDAY
},
297 { "last-Tuesday", TM_TUESDAY
},
298 { "last-Wednesday", TM_WEDNESDAY
},
299 { "last-Thursday", TM_THURSDAY
},
300 { "last-Friday", TM_FRIDAY
},
301 { "last-Saturday", TM_SATURDAY
},
305 static struct lookup
const begin_years
[] = {
306 { "minimum", YR_MINIMUM
},
307 { "maximum", YR_MAXIMUM
},
311 static struct lookup
const end_years
[] = {
312 { "minimum", YR_MINIMUM
},
313 { "maximum", YR_MAXIMUM
},
318 static struct lookup
const leap_types
[] = {
320 { "Stationary", FALSE
},
324 static const int len_months
[2][MONSPERYEAR
] = {
325 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
326 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
329 static const int len_years
[2] = {
330 DAYSPERNYEAR
, DAYSPERLYEAR
333 static struct attype
{
336 } attypes
[TZ_MAX_TIMES
];
337 static long gmtoffs
[TZ_MAX_TYPES
];
338 static char isdsts
[TZ_MAX_TYPES
];
339 static unsigned char abbrinds
[TZ_MAX_TYPES
];
340 static char ttisstds
[TZ_MAX_TYPES
];
341 static char ttisgmts
[TZ_MAX_TYPES
];
342 static char chars
[TZ_MAX_CHARS
];
343 static time_t trans
[TZ_MAX_LEAPS
];
344 static long corr
[TZ_MAX_LEAPS
];
345 static char roll
[TZ_MAX_LEAPS
];
348 ** Memory allocation.
356 errx(EXIT_FAILURE
, _("memory exhausted"));
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)))
369 #if !(HAVE_STRERROR - 0)
374 extern char * sys_errlist
[];
377 return (errnum
> 0 && errnum
<= sys_nerr
) ?
378 sys_errlist
[errnum
] : _("Unknown system error");
380 #endif /* !(HAVE_STRERROR - 0) */
383 eats(name
, num
, rname
, rnum
)
384 const char * const name
;
386 const char * const rname
;
397 const char * const name
;
400 eats(name
, num
, (char *) NULL
, -1);
405 const char * const string
;
408 ** Match the format of "cc" to allow sh users to
409 ** zic ... 2>&1 | error -t "*" -v
412 (void) fprintf(stderr
, _("\"%s\", line %d: %s"),
413 filename
, linenum
, string
);
414 if (rfilename
!= NULL
)
415 (void) fprintf(stderr
, _(" (rule from \"%s\", line %d)"),
416 rfilename
, rlinenum
);
417 (void) fprintf(stderr
, "\n");
423 const char * const string
;
427 cp
= ecpyalloc(_("warning: "));
428 cp
= ecatalloc(cp
, string
);
437 (void) fprintf(stderr
, "%s\n%s\n",
438 _("usage: zic [--version] [-s] [-v] [-l localtime] [-p posixrules] [-d directory]"),
439 _(" [-L leapseconds] [-y yearistype] [filename ... ]"));
440 (void) exit(EXIT_FAILURE
);
443 static const char * psxrules
;
444 static const char * lcltime
;
445 static const char * directory
;
446 static const char * leapsec
;
447 static const char * yitcommand
;
448 static int sflag
= FALSE
;
450 static uid_t uflag
= (uid_t
)-1;
451 static gid_t gflag
= (gid_t
)-1;
452 static mode_t mflag
= (S_IRUSR
| S_IRGRP
| S_IROTH
465 (void) umask(umask(S_IWGRP
| S_IWOTH
) | (S_IWGRP
| S_IWOTH
));
466 #endif /* defined unix */
468 (void) setlocale(LC_MESSAGES
, "");
470 (void) bindtextdomain(TZ_DOMAIN
, TZ_DOMAINDIR
);
471 #endif /* defined TEXTDOMAINDIR */
472 (void) textdomain(TZ_DOMAIN
);
473 #endif /* HAVE_GETTEXT - 0 */
474 for (i
= 1; i
< argc
; ++i
)
475 if (strcmp(argv
[i
], "--version") == 0) {
476 errx(EXIT_SUCCESS
, "%s", elsieid
);
478 while ((c
= getopt(argc
, argv
, "Dd:g:l:m:p:L:u:vsy:")) != -1)
486 if (directory
== NULL
)
490 _("more than one -d option specified"));
493 setgroup(&gflag
, optarg
);
500 _("more than one -l option specified"));
504 void *set
= setmode(optarg
);
507 _("invalid file mode"));
508 mflag
= getmode(set
, mflag
);
513 if (psxrules
== NULL
)
517 _("more than one -p option specified"));
520 setuser(&uflag
, optarg
);
523 if (yitcommand
== NULL
)
527 _("more than one -y option specified"));
534 _("more than one -L option specified"));
543 if (optind
== argc
- 1 && strcmp(argv
[optind
], "=") == 0)
544 usage(); /* usage message by request */
545 if (directory
== NULL
)
547 if (yitcommand
== NULL
)
548 yitcommand
= "yearistype";
552 if (optind
< argc
&& leapsec
!= NULL
) {
557 for (i
= optind
; i
< argc
; ++i
)
560 (void) exit(EXIT_FAILURE
);
562 for (i
= 0; i
< nzones
; i
= j
) {
564 ** Find the next non-continuation zone entry.
566 for (j
= i
+ 1; j
< nzones
&& zones
[j
].z_name
== NULL
; ++j
)
568 outzone(&zones
[i
], j
- i
);
573 for (i
= 0; i
< nlinks
; ++i
) {
574 eat(links
[i
].l_filename
, links
[i
].l_linenum
);
575 dolink(links
[i
].l_from
, links
[i
].l_to
);
577 if (lcltime
!= NULL
) {
578 eat("command line", 1);
579 dolink(lcltime
, TZDEFAULT
);
581 if (psxrules
!= NULL
) {
582 eat("command line", 1);
583 dolink(psxrules
, TZDEFRULES
);
585 return (errors
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;
589 dolink(fromfile
, tofile
)
590 const char * const fromfile
;
591 const char * const tofile
;
593 register char * fromname
;
594 register char * toname
;
596 if (fromfile
[0] == '/')
597 fromname
= ecpyalloc(fromfile
);
599 fromname
= ecpyalloc(directory
);
600 fromname
= ecatalloc(fromname
, "/");
601 fromname
= ecatalloc(fromname
, fromfile
);
603 if (tofile
[0] == '/')
604 toname
= ecpyalloc(tofile
);
606 toname
= ecpyalloc(directory
);
607 toname
= ecatalloc(toname
, "/");
608 toname
= ecatalloc(toname
, tofile
);
611 ** We get to be careful here since
612 ** there's a fair chance of root running us.
615 (void) remove(toname
);
616 if (link(fromname
, toname
) != 0) {
619 if (mkdirs(toname
) != 0)
620 (void) exit(EXIT_FAILURE
);
622 result
= link(fromname
, toname
);
623 #if (HAVE_SYMLINK - 0)
625 access(fromname
, F_OK
) == 0 &&
627 const char *s
= tofile
;
628 register char * symlinkcontents
= NULL
;
629 while ((s
= strchr(s
+1, '/')) != NULL
)
630 symlinkcontents
= ecatalloc(symlinkcontents
, "../");
631 symlinkcontents
= ecatalloc(symlinkcontents
, fromfile
);
633 result
= symlink(symlinkcontents
, toname
);
635 warning(_("hard link failed, symbolic link used"));
636 ifree(symlinkcontents
);
640 err(EXIT_FAILURE
, _("can't link from %s to %s"),
649 #define INT_MAX ((int) (((unsigned)~0)>>1))
650 #endif /* !defined INT_MAX */
653 #define INT_MIN ((int) ~(((unsigned)~0)>>1))
654 #endif /* !defined INT_MIN */
657 ** The tz file format currently allows at most 32-bit quantities.
658 ** This restriction should be removed before signed 32-bit values
659 ** wrap around in 2038, but unfortunately this will require a
660 ** change to the tz file format.
663 #define MAX_BITS_IN_FILE 32
664 #define TIME_T_BITS_IN_FILE ((TYPE_BIT(time_t) < MAX_BITS_IN_FILE) ? TYPE_BIT(time_t) : MAX_BITS_IN_FILE)
667 setboundaries
P((void))
669 if (TYPE_SIGNED(time_t)) {
670 min_time
= ~ (time_t) 0;
671 min_time
<<= TIME_T_BITS_IN_FILE
- 1;
672 max_time
= ~ (time_t) 0 - min_time
;
677 max_time
= 2 - sflag
;
678 max_time
<<= TIME_T_BITS_IN_FILE
- 1;
681 min_year
= TM_YEAR_BASE
+ gmtime(&min_time
)->tm_year
;
682 max_year
= TM_YEAR_BASE
+ gmtime(&max_time
)->tm_year
;
683 min_year_representable
= min_year
;
684 max_year_representable
= max_year
;
689 const char * const name
;
691 register char * myname
;
694 myname
= ecpyalloc(name
);
695 myname
= ecatalloc(myname
, "/.");
696 accres
= access(myname
, F_OK
);
702 ** Associate sets of rules with zones.
706 ** Sort by rule name.
714 return strcmp(((const struct rule
*) cp1
)->r_name
,
715 ((const struct rule
*) cp2
)->r_name
);
721 register struct zone
* zp
;
722 register struct rule
* rp
;
723 register int base
, out
;
727 (void) qsort((void *) rules
, (size_t) nrules
,
728 (size_t) sizeof *rules
, rcomp
);
729 for (i
= 0; i
< nrules
- 1; ++i
) {
730 if (strcmp(rules
[i
].r_name
,
731 rules
[i
+ 1].r_name
) != 0)
733 if (strcmp(rules
[i
].r_filename
,
734 rules
[i
+ 1].r_filename
) == 0)
736 eat(rules
[i
].r_filename
, rules
[i
].r_linenum
);
737 warning(_("same rule name in multiple files"));
738 eat(rules
[i
+ 1].r_filename
, rules
[i
+ 1].r_linenum
);
739 warning(_("same rule name in multiple files"));
740 for (j
= i
+ 2; j
< nrules
; ++j
) {
741 if (strcmp(rules
[i
].r_name
,
742 rules
[j
].r_name
) != 0)
744 if (strcmp(rules
[i
].r_filename
,
745 rules
[j
].r_filename
) == 0)
747 if (strcmp(rules
[i
+ 1].r_filename
,
748 rules
[j
].r_filename
) == 0)
755 for (i
= 0; i
< nzones
; ++i
) {
760 for (base
= 0; base
< nrules
; base
= out
) {
762 for (out
= base
+ 1; out
< nrules
; ++out
)
763 if (strcmp(rp
->r_name
, rules
[out
].r_name
) != 0)
765 for (i
= 0; i
< nzones
; ++i
) {
767 if (strcmp(zp
->z_rule
, rp
->r_name
) != 0)
770 zp
->z_nrules
= out
- base
;
773 for (i
= 0; i
< nzones
; ++i
) {
775 if (zp
->z_nrules
== 0) {
777 ** Maybe we have a local standard time offset.
779 eat(zp
->z_filename
, zp
->z_linenum
);
780 zp
->z_stdoff
= gethms(zp
->z_rule
, _("unruly zone"),
783 ** Note, though, that if there's no rule,
784 ** a '%s' in the format is a bad thing.
786 if (strchr(zp
->z_format
, '%') != 0)
787 error(_("%s in ruleless zone"));
791 (void) exit(EXIT_FAILURE
);
799 register char ** fields
;
801 register const struct lookup
* lp
;
802 register int nfields
;
803 register int wantcont
;
807 if (strcmp(name
, "-") == 0) {
808 name
= _("standard input");
810 } else if ((fp
= fopen(name
, "r")) == NULL
)
811 err(EXIT_FAILURE
, _("can't open %s"), name
);
813 for (num
= 1; ; ++num
) {
815 if (fgets(buf
, (int) sizeof buf
, fp
) != buf
)
817 cp
= strchr(buf
, '\n');
819 error(_("line too long"));
820 (void) exit(EXIT_FAILURE
);
823 fields
= getfields(buf
);
825 while (fields
[nfields
] != NULL
) {
828 if (strcmp(fields
[nfields
], "-") == 0)
829 fields
[nfields
] = &nada
;
834 } else if (wantcont
) {
835 wantcont
= inzcont(fields
, nfields
);
837 lp
= byword(fields
[0], line_codes
);
839 error(_("input line of unknown type"));
840 else switch ((int) (lp
->l_value
)) {
842 inrule(fields
, nfields
);
846 wantcont
= inzone(fields
, nfields
);
849 inlink(fields
, nfields
);
855 _("leap line in non leap seconds file %s"), name
);
856 else inleap(fields
, nfields
);
859 default: /* "cannot happen" */
861 _("panic: invalid l_value %d"), lp
->l_value
);
864 ifree((char *) fields
);
867 errx(EXIT_FAILURE
, _("error reading %s"), filename
);
868 if (fp
!= stdin
&& fclose(fp
))
869 err(EXIT_FAILURE
, _("error closing %s"), filename
);
871 error(_("expected continuation line not found"));
875 ** Convert a string of one of the forms
876 ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
877 ** into a number of seconds.
878 ** A null string maps to zero.
879 ** Call error with errstring and return zero on errors.
883 gethms(string
, errstring
, signable
)
885 const char * const errstring
;
888 int hh
, mm
, ss
, sign
;
890 if (string
== NULL
|| *string
== '\0')
894 else if (*string
== '-') {
898 if (sscanf(string
, scheck(string
, "%d"), &hh
) == 1)
900 else if (sscanf(string
, scheck(string
, "%d:%d"), &hh
, &mm
) == 2)
902 else if (sscanf(string
, scheck(string
, "%d:%d:%d"),
903 &hh
, &mm
, &ss
) != 3) {
907 if ((hh
< 0 || hh
>= HOURSPERDAY
||
908 mm
< 0 || mm
>= MINSPERHOUR
||
909 ss
< 0 || ss
> SECSPERMIN
) &&
910 !(hh
== HOURSPERDAY
&& mm
== 0 && ss
== 0)) {
914 if (noise
&& hh
== HOURSPERDAY
)
915 warning(_("24:00 not handled by pre-1998 versions of zic"));
917 (eitol(hh
* MINSPERHOUR
+ mm
) *
918 eitol(SECSPERMIN
) + eitol(ss
));
922 inrule(fields
, nfields
)
923 register char ** const fields
;
926 static struct rule r
;
928 if (nfields
!= RULE_FIELDS
) {
929 error(_("wrong number of fields on Rule line"));
932 if (*fields
[RF_NAME
] == '\0') {
933 error(_("nameless rule"));
936 r
.r_filename
= filename
;
937 r
.r_linenum
= linenum
;
938 r
.r_stdoff
= gethms(fields
[RF_STDOFF
], _("invalid saved time"), TRUE
);
939 rulesub(&r
, fields
[RF_LOYEAR
], fields
[RF_HIYEAR
], fields
[RF_COMMAND
],
940 fields
[RF_MONTH
], fields
[RF_DAY
], fields
[RF_TOD
]);
941 r
.r_name
= ecpyalloc(fields
[RF_NAME
]);
942 r
.r_abbrvar
= ecpyalloc(fields
[RF_ABBRVAR
]);
943 rules
= (struct rule
*) (void *) erealloc((char *) rules
,
944 (int) ((nrules
+ 1) * sizeof *rules
));
949 inzone(fields
, nfields
)
950 register char ** const fields
;
956 if (nfields
< ZONE_MINFIELDS
|| nfields
> ZONE_MAXFIELDS
) {
957 error(_("wrong number of fields on Zone line"));
960 if (strcmp(fields
[ZF_NAME
], TZDEFAULT
) == 0 && lcltime
!= NULL
) {
961 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFAULT
)));
963 _("\"Zone %s\" line and -l option are mutually exclusive"),
968 if (strcmp(fields
[ZF_NAME
], TZDEFRULES
) == 0 && psxrules
!= NULL
) {
969 buf
= erealloc(buf
, (int) (132 + strlen(TZDEFRULES
)));
971 _("\"Zone %s\" line and -p option are mutually exclusive"),
976 for (i
= 0; i
< nzones
; ++i
)
977 if (zones
[i
].z_name
!= NULL
&&
978 strcmp(zones
[i
].z_name
, fields
[ZF_NAME
]) == 0) {
979 buf
= erealloc(buf
, (int) (132 +
980 strlen(fields
[ZF_NAME
]) +
981 strlen(zones
[i
].z_filename
)));
983 _("duplicate zone name %s (file \"%s\", line %d)"),
990 return inzsub(fields
, nfields
, FALSE
);
994 inzcont(fields
, nfields
)
995 register char ** const fields
;
998 if (nfields
< ZONEC_MINFIELDS
|| nfields
> ZONEC_MAXFIELDS
) {
999 error(_("wrong number of fields on Zone continuation line"));
1002 return inzsub(fields
, nfields
, TRUE
);
1006 inzsub(fields
, nfields
, iscont
)
1007 register char ** const fields
;
1012 static struct zone z
;
1013 register int i_gmtoff
, i_rule
, i_format
;
1014 register int i_untilyear
, i_untilmonth
;
1015 register int i_untilday
, i_untiltime
;
1016 register int hasuntil
;
1019 i_gmtoff
= ZFC_GMTOFF
;
1021 i_format
= ZFC_FORMAT
;
1022 i_untilyear
= ZFC_TILYEAR
;
1023 i_untilmonth
= ZFC_TILMONTH
;
1024 i_untilday
= ZFC_TILDAY
;
1025 i_untiltime
= ZFC_TILTIME
;
1028 i_gmtoff
= ZF_GMTOFF
;
1030 i_format
= ZF_FORMAT
;
1031 i_untilyear
= ZF_TILYEAR
;
1032 i_untilmonth
= ZF_TILMONTH
;
1033 i_untilday
= ZF_TILDAY
;
1034 i_untiltime
= ZF_TILTIME
;
1035 z
.z_name
= ecpyalloc(fields
[ZF_NAME
]);
1037 z
.z_filename
= filename
;
1038 z
.z_linenum
= linenum
;
1039 z
.z_gmtoff
= gethms(fields
[i_gmtoff
], _("invalid UTC offset"), TRUE
);
1040 if ((cp
= strchr(fields
[i_format
], '%')) != 0) {
1041 if (*++cp
!= 's' || strchr(cp
, '%') != 0) {
1042 error(_("invalid abbreviation format"));
1046 z
.z_rule
= ecpyalloc(fields
[i_rule
]);
1047 z
.z_format
= ecpyalloc(fields
[i_format
]);
1048 hasuntil
= nfields
> i_untilyear
;
1050 z
.z_untilrule
.r_filename
= filename
;
1051 z
.z_untilrule
.r_linenum
= linenum
;
1052 rulesub(&z
.z_untilrule
,
1053 fields
[i_untilyear
],
1056 (nfields
> i_untilmonth
) ?
1057 fields
[i_untilmonth
] : "Jan",
1058 (nfields
> i_untilday
) ? fields
[i_untilday
] : "1",
1059 (nfields
> i_untiltime
) ? fields
[i_untiltime
] : "0");
1060 z
.z_untiltime
= rpytime(&z
.z_untilrule
,
1061 z
.z_untilrule
.r_loyear
);
1062 if (iscont
&& nzones
> 0 &&
1063 z
.z_untiltime
> min_time
&&
1064 z
.z_untiltime
< max_time
&&
1065 zones
[nzones
- 1].z_untiltime
> min_time
&&
1066 zones
[nzones
- 1].z_untiltime
< max_time
&&
1067 zones
[nzones
- 1].z_untiltime
>= z
.z_untiltime
) {
1068 error(_("Zone continuation line end time is not after end time of previous line"));
1072 zones
= (struct zone
*) (void *) erealloc((char *) zones
,
1073 (int) ((nzones
+ 1) * sizeof *zones
));
1074 zones
[nzones
++] = z
;
1076 ** If there was an UNTIL field on this line,
1077 ** there's more information about the zone on the next line.
1083 inleap(fields
, nfields
)
1084 register char ** const fields
;
1087 register const char * cp
;
1088 register const struct lookup
* lp
;
1090 int year
, month
, day
;
1094 if (nfields
!= LEAP_FIELDS
) {
1095 error(_("wrong number of fields on Leap line"));
1099 cp
= fields
[LP_YEAR
];
1100 if (sscanf(cp
, scheck(cp
, "%d"), &year
) != 1) {
1104 error(_("invalid leaping year"));
1110 i
= len_years
[isleap(j
)];
1114 i
= -len_years
[isleap(j
)];
1116 dayoff
= oadd(dayoff
, eitol(i
));
1118 if ((lp
= byword(fields
[LP_MONTH
], mon_names
)) == NULL
) {
1119 error(_("invalid month name"));
1122 month
= lp
->l_value
;
1124 while (j
!= month
) {
1125 i
= len_months
[isleap(year
)][j
];
1126 dayoff
= oadd(dayoff
, eitol(i
));
1129 cp
= fields
[LP_DAY
];
1130 if (sscanf(cp
, scheck(cp
, "%d"), &day
) != 1 ||
1131 day
<= 0 || day
> len_months
[isleap(year
)][month
]) {
1132 error(_("invalid day of month"));
1135 dayoff
= oadd(dayoff
, eitol(day
- 1));
1136 if (dayoff
< 0 && !TYPE_SIGNED(time_t)) {
1137 error(_("time before zero"));
1140 if (dayoff
< min_time
/ SECSPERDAY
) {
1141 error(_("time too small"));
1144 if (dayoff
> max_time
/ SECSPERDAY
) {
1145 error(_("time too large"));
1148 t
= (time_t) dayoff
* SECSPERDAY
;
1149 tod
= gethms(fields
[LP_TIME
], _("invalid time of day"), FALSE
);
1150 cp
= fields
[LP_CORR
];
1152 register int positive
;
1155 if (strcmp(cp
, "") == 0) { /* infile() turns "-" into "" */
1158 } else if (strcmp(cp
, "--") == 0) {
1161 } else if (strcmp(cp
, "+") == 0) {
1164 } else if (strcmp(cp
, "++") == 0) {
1168 error(_("illegal CORRECTION field on Leap line"));
1171 if ((lp
= byword(fields
[LP_ROLL
], leap_types
)) == NULL
) {
1172 error(_("illegal Rolling/Stationary field on Leap line"));
1175 leapadd(tadd(t
, tod
), positive
, lp
->l_value
, count
);
1180 inlink(fields
, nfields
)
1181 register char ** const fields
;
1186 if (nfields
!= LINK_FIELDS
) {
1187 error(_("wrong number of fields on Link line"));
1190 if (*fields
[LF_FROM
] == '\0') {
1191 error(_("blank FROM field on Link line"));
1194 if (*fields
[LF_TO
] == '\0') {
1195 error(_("blank TO field on Link line"));
1198 l
.l_filename
= filename
;
1199 l
.l_linenum
= linenum
;
1200 l
.l_from
= ecpyalloc(fields
[LF_FROM
]);
1201 l
.l_to
= ecpyalloc(fields
[LF_TO
]);
1202 links
= (struct link
*) (void *) erealloc((char *) links
,
1203 (int) ((nlinks
+ 1) * sizeof *links
));
1204 links
[nlinks
++] = l
;
1208 rulesub(rp
, loyearp
, hiyearp
, typep
, monthp
, dayp
, timep
)
1209 register struct rule
* const rp
;
1210 const char * const loyearp
;
1211 const char * const hiyearp
;
1212 const char * const typep
;
1213 const char * const monthp
;
1214 const char * const dayp
;
1215 const char * const timep
;
1217 register const struct lookup
* lp
;
1218 register const char * cp
;
1222 if ((lp
= byword(monthp
, mon_names
)) == NULL
) {
1223 error(_("invalid month name"));
1226 rp
->r_month
= lp
->l_value
;
1227 rp
->r_todisstd
= FALSE
;
1228 rp
->r_todisgmt
= FALSE
;
1229 dp
= ecpyalloc(timep
);
1231 ep
= dp
+ strlen(dp
) - 1;
1232 switch (lowerit(*ep
)) {
1233 case 's': /* Standard */
1234 rp
->r_todisstd
= TRUE
;
1235 rp
->r_todisgmt
= FALSE
;
1238 case 'w': /* Wall */
1239 rp
->r_todisstd
= FALSE
;
1240 rp
->r_todisgmt
= FALSE
;
1243 case 'g': /* Greenwich */
1244 case 'u': /* Universal */
1245 case 'z': /* Zulu */
1246 rp
->r_todisstd
= TRUE
;
1247 rp
->r_todisgmt
= TRUE
;
1252 rp
->r_tod
= gethms(dp
, _("invalid time of day"), FALSE
);
1258 lp
= byword(cp
, begin_years
);
1259 if (lp
!= NULL
) switch ((int) lp
->l_value
) {
1261 rp
->r_loyear
= INT_MIN
;
1264 rp
->r_loyear
= INT_MAX
;
1266 default: /* "cannot happen" */
1268 _("panic: invalid l_value %d"), lp
->l_value
);
1269 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_loyear
) != 1) {
1270 error(_("invalid starting year"));
1273 if (rp
->r_loyear
< min_year_representable
)
1274 warning(_("starting year too low to be represented"));
1275 else if (rp
->r_loyear
> max_year_representable
)
1276 warning(_("starting year too high to be represented"));
1279 if ((lp
= byword(cp
, end_years
)) != NULL
) switch ((int) lp
->l_value
) {
1281 rp
->r_hiyear
= INT_MIN
;
1284 rp
->r_hiyear
= INT_MAX
;
1287 rp
->r_hiyear
= rp
->r_loyear
;
1289 default: /* "cannot happen" */
1291 _("panic: invalid l_value %d"), lp
->l_value
);
1292 } else if (sscanf(cp
, scheck(cp
, "%d"), &rp
->r_hiyear
) != 1) {
1293 error(_("invalid ending year"));
1296 if (rp
->r_loyear
< min_year_representable
)
1297 warning(_("ending year too low to be represented"));
1298 else if (rp
->r_loyear
> max_year_representable
)
1299 warning(_("ending year too high to be represented"));
1301 if (rp
->r_loyear
> rp
->r_hiyear
) {
1302 error(_("starting year greater than ending year"));
1306 rp
->r_yrtype
= NULL
;
1308 if (rp
->r_loyear
== rp
->r_hiyear
) {
1309 error(_("typed single year"));
1312 rp
->r_yrtype
= ecpyalloc(typep
);
1314 if (rp
->r_loyear
< min_year
&& rp
->r_loyear
> 0)
1315 min_year
= rp
->r_loyear
;
1318 ** Accept things such as:
1324 dp
= ecpyalloc(dayp
);
1325 if ((lp
= byword(dp
, lasts
)) != NULL
) {
1326 rp
->r_dycode
= DC_DOWLEQ
;
1327 rp
->r_wday
= lp
->l_value
;
1328 rp
->r_dayofmonth
= len_months
[1][rp
->r_month
];
1330 if ((ep
= strchr(dp
, '<')) != 0)
1331 rp
->r_dycode
= DC_DOWLEQ
;
1332 else if ((ep
= strchr(dp
, '>')) != 0)
1333 rp
->r_dycode
= DC_DOWGEQ
;
1336 rp
->r_dycode
= DC_DOM
;
1338 if (rp
->r_dycode
!= DC_DOM
) {
1341 error(_("invalid day of month"));
1345 if ((lp
= byword(dp
, wday_names
)) == NULL
) {
1346 error(_("invalid weekday name"));
1350 rp
->r_wday
= lp
->l_value
;
1352 if (sscanf(ep
, scheck(ep
, "%d"), &rp
->r_dayofmonth
) != 1 ||
1353 rp
->r_dayofmonth
<= 0 ||
1354 (rp
->r_dayofmonth
> len_months
[1][rp
->r_month
])) {
1355 error(_("invalid day of month"));
1369 register long shift
;
1371 for (i
= 0, shift
= 24; i
< 4; ++i
, shift
-= 8)
1372 buf
[i
] = val
>> shift
;
1383 (void) fwrite((void *) buf
, (size_t) sizeof buf
, (size_t) 1, fp
);
1391 if (((struct attype
*) avp
)->at
< ((struct attype
*) bvp
)->at
)
1393 else if (((struct attype
*) avp
)->at
> ((struct attype
*) bvp
)->at
)
1400 const char * const name
;
1404 static char * fullname
;
1405 static struct tzhead tzh
;
1406 time_t ats
[TZ_MAX_TIMES
];
1407 unsigned char types
[TZ_MAX_TIMES
];
1413 (void) qsort((void *) attypes
, (size_t) timecnt
,
1414 (size_t) sizeof *attypes
, atcomp
);
1424 while (fromi
< timecnt
&& attypes
[fromi
].at
< min_time
)
1427 while (fromi
< timecnt
&& attypes
[fromi
].type
== 0)
1428 ++fromi
; /* handled by default rule */
1429 for ( ; fromi
< timecnt
; ++fromi
) {
1431 && ((attypes
[fromi
].at
1432 + gmtoffs
[attypes
[toi
- 1].type
])
1433 <= (attypes
[toi
- 1].at
1434 + gmtoffs
[toi
== 1 ? 0
1435 : attypes
[toi
- 2].type
]))) {
1436 attypes
[toi
- 1].type
= attypes
[fromi
].type
;
1440 attypes
[toi
- 1].type
!= attypes
[fromi
].type
)
1441 attypes
[toi
++] = attypes
[fromi
];
1448 for (i
= 0; i
< timecnt
; ++i
) {
1449 ats
[i
] = attypes
[i
].at
;
1450 types
[i
] = attypes
[i
].type
;
1452 fullname
= erealloc(fullname
,
1453 (int) (strlen(directory
) + 1 + strlen(name
) + 1));
1454 (void) sprintf(fullname
, "%s/%s", directory
, name
);
1457 * Remove old file, if any, to snap links.
1459 if (!itsdir(fullname
) && remove(fullname
) != 0 && errno
!= ENOENT
)
1460 err(EXIT_FAILURE
, _("can't remove %s"), fullname
);
1462 if ((fp
= fopen(fullname
, "wb")) == NULL
) {
1463 if (mkdirs(fullname
) != 0)
1464 (void) exit(EXIT_FAILURE
);
1465 if ((fp
= fopen(fullname
, "wb")) == NULL
)
1466 err(EXIT_FAILURE
, _("can't create %s"), fullname
);
1468 convert(eitol(typecnt
), tzh
.tzh_ttisgmtcnt
);
1469 convert(eitol(typecnt
), tzh
.tzh_ttisstdcnt
);
1470 convert(eitol(leapcnt
), tzh
.tzh_leapcnt
);
1471 convert(eitol(timecnt
), tzh
.tzh_timecnt
);
1472 convert(eitol(typecnt
), tzh
.tzh_typecnt
);
1473 convert(eitol(charcnt
), tzh
.tzh_charcnt
);
1474 (void) strncpy(tzh
.tzh_magic
, TZ_MAGIC
, sizeof tzh
.tzh_magic
);
1475 #define DO(field) (void) fwrite((void *) tzh.field, (size_t) sizeof tzh.field, (size_t) 1, fp)
1485 for (i
= 0; i
< timecnt
; ++i
) {
1488 if (ats
[i
] >= trans
[j
]) {
1489 ats
[i
] = tadd(ats
[i
], corr
[j
]);
1492 puttzcode((long) ats
[i
], fp
);
1495 (void) fwrite((void *) types
, (size_t) sizeof types
[0],
1496 (size_t) timecnt
, fp
);
1497 for (i
= 0; i
< typecnt
; ++i
) {
1498 puttzcode((long) gmtoffs
[i
], fp
);
1499 (void) putc(isdsts
[i
], fp
);
1500 (void) putc(abbrinds
[i
], fp
);
1503 (void) fwrite((void *) chars
, (size_t) sizeof chars
[0],
1504 (size_t) charcnt
, fp
);
1505 for (i
= 0; i
< leapcnt
; ++i
) {
1507 if (timecnt
== 0 || trans
[i
] < ats
[0]) {
1510 if (++j
>= typecnt
) {
1516 while (j
< timecnt
&& trans
[i
] >= ats
[j
])
1520 puttzcode((long) tadd(trans
[i
], -gmtoffs
[j
]), fp
);
1521 } else puttzcode((long) trans
[i
], fp
);
1522 puttzcode((long) corr
[i
], fp
);
1524 for (i
= 0; i
< typecnt
; ++i
)
1525 (void) putc(ttisstds
[i
], fp
);
1526 for (i
= 0; i
< typecnt
; ++i
)
1527 (void) putc(ttisgmts
[i
], fp
);
1528 if (ferror(fp
) || fclose(fp
))
1529 errx(EXIT_FAILURE
, _("error writing %s"), fullname
);
1530 if (chmod(fullname
, mflag
) < 0)
1531 err(EXIT_FAILURE
, _("cannot change mode of %s to %03o"),
1532 fullname
, (unsigned)mflag
);
1533 if ((uflag
!= (uid_t
)-1 || gflag
!= (gid_t
)-1)
1534 && chown(fullname
, uflag
, gflag
) < 0)
1535 err(EXIT_FAILURE
, _("cannot change ownership of %s"),
1540 doabbr(abbr
, format
, letters
, isdst
)
1542 const char * const format
;
1543 const char * const letters
;
1546 if (strchr(format
, '/') == NULL
) {
1547 if (letters
== NULL
)
1548 (void) strcpy(abbr
, format
);
1549 else (void) sprintf(abbr
, format
, letters
);
1551 (void) strcpy(abbr
, strchr(format
, '/') + 1);
1553 (void) strcpy(abbr
, format
);
1554 *strchr(abbr
, '/') = '\0';
1559 outzone(zpfirst
, zonecount
)
1560 const struct zone
* const zpfirst
;
1561 const int zonecount
;
1563 register const struct zone
* zp
;
1564 register struct rule
* rp
;
1566 register int usestart
, useuntil
;
1567 register time_t starttime
, untiltime
;
1568 register long gmtoff
;
1569 register long stdoff
;
1571 register long startoff
;
1572 register int startttisstd
;
1573 register int startttisgmt
;
1575 char startbuf
[BUFSIZ
];
1577 INITIALIZE(untiltime
);
1578 INITIALIZE(starttime
);
1580 ** Now. . .finally. . .generate some useful data!
1586 ** Thanks to Earl Chew (earl@dnd.icp.nec.com.au)
1587 ** for noting the need to unconditionally initialize startttisstd.
1589 startttisstd
= FALSE
;
1590 startttisgmt
= FALSE
;
1591 for (i
= 0; i
< zonecount
; ++i
) {
1593 ** A guess that may well be corrected later.
1597 usestart
= i
> 0 && (zp
- 1)->z_untiltime
> min_time
;
1598 useuntil
= i
< (zonecount
- 1);
1599 if (useuntil
&& zp
->z_untiltime
<= min_time
)
1601 gmtoff
= zp
->z_gmtoff
;
1602 eat(zp
->z_filename
, zp
->z_linenum
);
1604 startoff
= zp
->z_gmtoff
;
1605 if (zp
->z_nrules
== 0) {
1606 stdoff
= zp
->z_stdoff
;
1607 doabbr(startbuf
, zp
->z_format
,
1608 (char *) NULL
, stdoff
!= 0);
1609 type
= addtype(oadd(zp
->z_gmtoff
, stdoff
),
1610 startbuf
, stdoff
!= 0, startttisstd
,
1613 addtt(starttime
, type
);
1615 } else if (stdoff
!= 0)
1616 addtt(min_time
, type
);
1617 } else for (year
= min_year
; year
<= max_year
; ++year
) {
1618 if (useuntil
&& year
> zp
->z_untilrule
.r_hiyear
)
1621 ** Mark which rules to do in the current year.
1622 ** For those to do, calculate rpytime(rp, year);
1624 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1625 rp
= &zp
->z_rules
[j
];
1626 eats(zp
->z_filename
, zp
->z_linenum
,
1627 rp
->r_filename
, rp
->r_linenum
);
1628 rp
->r_todo
= year
>= rp
->r_loyear
&&
1629 year
<= rp
->r_hiyear
&&
1630 yearistype(year
, rp
->r_yrtype
);
1632 rp
->r_temp
= rpytime(rp
, year
);
1636 register time_t jtime
, ktime
;
1637 register long offset
;
1643 ** Turn untiltime into UTC
1644 ** assuming the current gmtoff and
1647 untiltime
= zp
->z_untiltime
;
1648 if (!zp
->z_untilrule
.r_todisgmt
)
1649 untiltime
= tadd(untiltime
,
1651 if (!zp
->z_untilrule
.r_todisstd
)
1652 untiltime
= tadd(untiltime
,
1656 ** Find the rule (of those to do, if any)
1657 ** that takes effect earliest in the year.
1660 for (j
= 0; j
< zp
->z_nrules
; ++j
) {
1661 rp
= &zp
->z_rules
[j
];
1664 eats(zp
->z_filename
, zp
->z_linenum
,
1665 rp
->r_filename
, rp
->r_linenum
);
1666 offset
= rp
->r_todisgmt
? 0 : gmtoff
;
1667 if (!rp
->r_todisstd
)
1668 offset
= oadd(offset
, stdoff
);
1670 if (jtime
== min_time
||
1673 jtime
= tadd(jtime
, -offset
);
1674 if (k
< 0 || jtime
< ktime
) {
1680 break; /* go on to next year */
1681 rp
= &zp
->z_rules
[k
];
1683 if (useuntil
&& ktime
>= untiltime
)
1685 stdoff
= rp
->r_stdoff
;
1686 if (usestart
&& ktime
== starttime
)
1689 if (ktime
< starttime
) {
1690 startoff
= oadd(zp
->z_gmtoff
,
1692 doabbr(startbuf
, zp
->z_format
,
1697 if (*startbuf
== '\0' &&
1698 startoff
== oadd(zp
->z_gmtoff
,
1700 doabbr(startbuf
, zp
->z_format
,
1705 eats(zp
->z_filename
, zp
->z_linenum
,
1706 rp
->r_filename
, rp
->r_linenum
);
1707 doabbr(buf
, zp
->z_format
, rp
->r_abbrvar
,
1709 offset
= oadd(zp
->z_gmtoff
, rp
->r_stdoff
);
1710 type
= addtype(offset
, buf
, rp
->r_stdoff
!= 0,
1711 rp
->r_todisstd
, rp
->r_todisgmt
);
1716 if (*startbuf
== '\0' &&
1717 zp
->z_format
!= NULL
&&
1718 strchr(zp
->z_format
, '%') == NULL
&&
1719 strchr(zp
->z_format
, '/') == NULL
)
1720 (void) strcpy(startbuf
, zp
->z_format
);
1721 eat(zp
->z_filename
, zp
->z_linenum
);
1722 if (*startbuf
== '\0')
1723 error(_("can't determine time zone abbreviation to use just after until time"));
1724 else addtt(starttime
,
1725 addtype(startoff
, startbuf
,
1726 startoff
!= zp
->z_gmtoff
,
1731 ** Now we may get to set starttime for the next zone line.
1734 startttisstd
= zp
->z_untilrule
.r_todisstd
;
1735 startttisgmt
= zp
->z_untilrule
.r_todisgmt
;
1736 starttime
= zp
->z_untiltime
;
1738 starttime
= tadd(starttime
, -stdoff
);
1740 starttime
= tadd(starttime
, -gmtoff
);
1743 writezone(zpfirst
->z_name
);
1747 addtt(starttime
, type
)
1748 const time_t starttime
;
1751 if (starttime
<= min_time
||
1752 (timecnt
== 1 && attypes
[0].at
< min_time
)) {
1753 gmtoffs
[0] = gmtoffs
[type
];
1754 isdsts
[0] = isdsts
[type
];
1755 ttisstds
[0] = ttisstds
[type
];
1756 ttisgmts
[0] = ttisgmts
[type
];
1757 if (abbrinds
[type
] != 0)
1758 (void) strcpy(chars
, &chars
[abbrinds
[type
]]);
1760 charcnt
= strlen(chars
) + 1;
1765 if (timecnt
>= TZ_MAX_TIMES
) {
1766 error(_("too many transitions?!"));
1767 (void) exit(EXIT_FAILURE
);
1769 attypes
[timecnt
].at
= starttime
;
1770 attypes
[timecnt
].type
= type
;
1775 addtype(gmtoff
, abbr
, isdst
, ttisstd
, ttisgmt
)
1777 const char * const abbr
;
1784 if (isdst
!= TRUE
&& isdst
!= FALSE
) {
1785 error(_("internal error - addtype called with bad isdst"));
1786 (void) exit(EXIT_FAILURE
);
1788 if (ttisstd
!= TRUE
&& ttisstd
!= FALSE
) {
1789 error(_("internal error - addtype called with bad ttisstd"));
1790 (void) exit(EXIT_FAILURE
);
1792 if (ttisgmt
!= TRUE
&& ttisgmt
!= FALSE
) {
1793 error(_("internal error - addtype called with bad ttisgmt"));
1794 (void) exit(EXIT_FAILURE
);
1797 ** See if there's already an entry for this zone type.
1798 ** If so, just return its index.
1800 for (i
= 0; i
< typecnt
; ++i
) {
1801 if (gmtoff
== gmtoffs
[i
] && isdst
== isdsts
[i
] &&
1802 strcmp(abbr
, &chars
[abbrinds
[i
]]) == 0 &&
1803 ttisstd
== ttisstds
[i
] &&
1804 ttisgmt
== ttisgmts
[i
])
1808 ** There isn't one; add a new one, unless there are already too
1811 if (typecnt
>= TZ_MAX_TYPES
) {
1812 error(_("too many local time types"));
1813 (void) exit(EXIT_FAILURE
);
1815 gmtoffs
[i
] = gmtoff
;
1817 ttisstds
[i
] = ttisstd
;
1818 ttisgmts
[i
] = ttisgmt
;
1820 for (j
= 0; j
< charcnt
; ++j
)
1821 if (strcmp(&chars
[j
], abbr
) == 0)
1831 leapadd(t
, positive
, rolling
, count
)
1839 if (leapcnt
+ (positive
? count
: 1) > TZ_MAX_LEAPS
) {
1840 error(_("too many leap seconds"));
1841 (void) exit(EXIT_FAILURE
);
1843 for (i
= 0; i
< leapcnt
; ++i
)
1844 if (t
<= trans
[i
]) {
1845 if (t
== trans
[i
]) {
1846 error(_("repeated leap second moment"));
1847 (void) exit(EXIT_FAILURE
);
1852 for (j
= leapcnt
; j
> i
; --j
) {
1853 trans
[j
] = trans
[j
- 1];
1854 corr
[j
] = corr
[j
- 1];
1855 roll
[j
] = roll
[j
- 1];
1858 corr
[i
] = positive
? 1L : eitol(-count
);
1861 } while (positive
&& --count
!= 0);
1868 register long last
= 0;
1871 ** propagate leap seconds forward
1873 for (i
= 0; i
< leapcnt
; ++i
) {
1874 trans
[i
] = tadd(trans
[i
], last
);
1875 last
= corr
[i
] += last
;
1880 yearistype(year
, type
)
1882 const char * const type
;
1887 if (type
== NULL
|| *type
== '\0')
1889 buf
= erealloc(buf
, (int) (132 + strlen(yitcommand
) + strlen(type
)));
1890 (void) sprintf(buf
, "%s %d %s", yitcommand
, year
, type
);
1891 result
= system(buf
);
1892 if (WIFEXITED(result
)) switch (WEXITSTATUS(result
)) {
1898 error(_("wild result from command execution"));
1899 warnx(_("command was '%s', result was %d"), buf
, result
);
1901 (void) exit(EXIT_FAILURE
);
1908 a
= (unsigned char) a
;
1909 return (isascii(a
) && isupper(a
)) ? tolower(a
) : a
;
1913 ciequal(ap
, bp
) /* case-insensitive equality */
1914 register const char * ap
;
1915 register const char * bp
;
1917 while (lowerit(*ap
) == lowerit(*bp
++))
1925 register const char * abbr
;
1926 register const char * word
;
1928 if (lowerit(*abbr
) != lowerit(*word
))
1931 while (*++abbr
!= '\0')
1935 } while (lowerit(*word
++) != lowerit(*abbr
));
1939 static const struct lookup
*
1941 register const char * const word
;
1942 register const struct lookup
* const table
;
1944 register const struct lookup
* foundlp
;
1945 register const struct lookup
* lp
;
1947 if (word
== NULL
|| table
== NULL
)
1950 ** Look for exact match.
1952 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1953 if (ciequal(word
, lp
->l_word
))
1956 ** Look for inexact match.
1959 for (lp
= table
; lp
->l_word
!= NULL
; ++lp
)
1960 if (itsabbr(word
, lp
->l_word
)) {
1961 if (foundlp
== NULL
)
1963 else return NULL
; /* multiple inexact matches */
1973 register char ** array
;
1978 array
= (char **) (void *)
1979 emalloc((int) ((strlen(cp
) + 1) * sizeof *array
));
1982 while (isascii(*cp
) && isspace((unsigned char) *cp
))
1984 if (*cp
== '\0' || *cp
== '#')
1986 array
[nsubs
++] = dp
= cp
;
1988 if ((*dp
= *cp
++) != '"')
1990 else while ((*dp
= *cp
++) != '"')
1993 else error(_("odd number of quotation marks"));
1994 } while (*cp
!= '\0' && *cp
!= '#' &&
1995 (!isascii(*cp
) || !isspace((unsigned char) *cp
)));
1996 if (isascii(*cp
) && isspace((unsigned char) *cp
))
2000 array
[nsubs
] = NULL
;
2012 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2013 error(_("time overflow"));
2014 (void) exit(EXIT_FAILURE
);
2026 if (t1
== max_time
&& t2
> 0)
2028 if (t1
== min_time
&& t2
< 0)
2031 if ((t2
> 0 && t
<= t1
) || (t2
< 0 && t
>= t1
)) {
2032 error(_("time overflow"));
2033 (void) exit(EXIT_FAILURE
);
2039 ** Given a rule, and a year, compute the date - in seconds since January 1,
2040 ** 1970, 00:00 LOCAL time - in that year that the rule refers to.
2044 rpytime(rp
, wantedy
)
2045 register const struct rule
* const rp
;
2046 register const int wantedy
;
2048 register int y
, m
, i
;
2049 register long dayoff
; /* with a nod to Margaret O. */
2052 if (wantedy
== INT_MIN
)
2054 if (wantedy
== INT_MAX
)
2059 while (wantedy
!= y
) {
2061 i
= len_years
[isleap(y
)];
2065 i
= -len_years
[isleap(y
)];
2067 dayoff
= oadd(dayoff
, eitol(i
));
2069 while (m
!= rp
->r_month
) {
2070 i
= len_months
[isleap(y
)][m
];
2071 dayoff
= oadd(dayoff
, eitol(i
));
2074 i
= rp
->r_dayofmonth
;
2075 if (m
== TM_FEBRUARY
&& i
== 29 && !isleap(y
)) {
2076 if (rp
->r_dycode
== DC_DOWLEQ
)
2079 error(_("use of 2/29 in non leap-year"));
2080 (void) exit(EXIT_FAILURE
);
2084 dayoff
= oadd(dayoff
, eitol(i
));
2085 if (rp
->r_dycode
== DC_DOWGEQ
|| rp
->r_dycode
== DC_DOWLEQ
) {
2088 #define LDAYSPERWEEK ((long) DAYSPERWEEK)
2089 wday
= eitol(EPOCH_WDAY
);
2091 ** Don't trust mod of negative numbers.
2094 wday
= (wday
+ dayoff
) % LDAYSPERWEEK
;
2096 wday
-= ((-dayoff
) % LDAYSPERWEEK
);
2098 wday
+= LDAYSPERWEEK
;
2100 while (wday
!= eitol(rp
->r_wday
))
2101 if (rp
->r_dycode
== DC_DOWGEQ
) {
2102 dayoff
= oadd(dayoff
, (long) 1);
2103 if (++wday
>= LDAYSPERWEEK
)
2107 dayoff
= oadd(dayoff
, (long) -1);
2109 wday
= LDAYSPERWEEK
- 1;
2112 if (i
< 0 || i
>= len_months
[isleap(y
)][m
]) {
2114 warning(_("rule goes past start/end of month--will not work with pre-2004 versions of zic"));
2117 if (dayoff
< 0 && !TYPE_SIGNED(time_t))
2119 if (dayoff
< min_time
/ SECSPERDAY
)
2121 if (dayoff
> max_time
/ SECSPERDAY
)
2123 t
= (time_t) dayoff
* SECSPERDAY
;
2124 return tadd(t
, rp
->r_tod
);
2129 const char * const string
;
2133 i
= strlen(string
) + 1;
2134 if (charcnt
+ i
> TZ_MAX_CHARS
) {
2135 error(_("too many, or too long, time zone abbreviations"));
2136 (void) exit(EXIT_FAILURE
);
2138 (void) strcpy(&chars
[charcnt
], string
);
2139 charcnt
+= eitol(i
);
2144 char * const argname
;
2146 register char * name
;
2149 if (argname
== NULL
|| *argname
== '\0' || Dflag
)
2151 cp
= name
= ecpyalloc(argname
);
2152 while ((cp
= strchr(cp
+ 1, '/')) != 0) {
2156 ** DOS drive specifier?
2158 if (isalpha((unsigned char) name
[0]) &&
2159 name
[1] == ':' && name
[2] == '\0') {
2163 #endif /* !defined unix */
2164 if (!itsdir(name
)) {
2166 ** It doesn't seem to exist, so we try to create it.
2167 ** Creation may fail because of the directory being
2168 ** created by some other multiprocessor, so we get
2169 ** to do extra checking.
2171 if (mkdir(name
, MKDIR_UMASK
) != 0
2172 && (errno
!= EEXIST
|| !itsdir(name
))) {
2173 warn(_("can't create directory %s"), name
);
2191 if ((i
< 0 && l
>= 0) || (i
== 0 && l
!= 0) || (i
> 0 && l
<= 0))
2192 errx(EXIT_FAILURE
, _("%d did not sign extend correctly"), i
);
2200 setgroup(flag
, name
)
2206 if (*flag
!= (gid_t
)-1)
2207 errx(EXIT_FAILURE
, _("multiple -g flags specified"));
2209 gr
= getgrnam(name
);
2214 ul
= strtoul(name
, &ep
, 10);
2215 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2219 errx(EXIT_FAILURE
, _("group `%s' not found"), name
);
2231 if (*flag
!= (gid_t
)-1)
2232 errx(EXIT_FAILURE
, _("multiple -u flags specified"));
2234 pw
= getpwnam(name
);
2239 ul
= strtoul(name
, &ep
, 10);
2240 if (ul
== (unsigned long)(gid_t
)ul
&& *ep
== '\0') {
2244 errx(EXIT_FAILURE
, _("user `%s' not found"), name
);
2250 ** UNIX was a registered trademark of The Open Group in 2003.