zic.c revision 1.71 1 1.71 christos /* $NetBSD: zic.c,v 1.71 2018/05/04 15:51:00 christos Exp $ */
2 1.25 mlelstv /*
3 1.25 mlelstv ** This file is in the public domain, so clarified as of
4 1.25 mlelstv ** 2006-07-17 by Arthur David Olson.
5 1.25 mlelstv */
6 1.2 jtc
7 1.26 tsutsui #if HAVE_NBTOOL_CONFIG_H
8 1.26 tsutsui #include "nbtool_config.h"
9 1.26 tsutsui #endif
10 1.26 tsutsui
11 1.9 christos #include <sys/cdefs.h>
12 1.1 jtc #ifndef lint
13 1.71 christos __RCSID("$NetBSD: zic.c,v 1.71 2018/05/04 15:51:00 christos Exp $");
14 1.1 jtc #endif /* !defined lint */
15 1.1 jtc
16 1.1 jtc #include "private.h"
17 1.1 jtc #include "tzfile.h"
18 1.19 kleink
19 1.68 christos #include <fcntl.h>
20 1.68 christos #include <locale.h>
21 1.43 christos #include <stdarg.h>
22 1.65 christos #include <stddef.h>
23 1.69 christos #include <stdio.h>
24 1.43 christos #include <unistd.h>
25 1.63 christos #include <util.h>
26 1.43 christos
27 1.43 christos #define ZIC_VERSION_PRE_2013 '2'
28 1.43 christos #define ZIC_VERSION '3'
29 1.25 mlelstv
30 1.41 christos typedef int_fast64_t zic_t;
31 1.41 christos #define ZIC_MIN INT_FAST64_MIN
32 1.41 christos #define ZIC_MAX INT_FAST64_MAX
33 1.65 christos #define PRIdZIC PRIdFAST64
34 1.41 christos #define SCNdZIC SCNdFAST64
35 1.25 mlelstv
36 1.25 mlelstv #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
37 1.25 mlelstv #define ZIC_MAX_ABBR_LEN_WO_WARN 6
38 1.25 mlelstv #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
39 1.25 mlelstv
40 1.57 christos #ifdef HAVE_DIRECT_H
41 1.57 christos # include <direct.h>
42 1.57 christos # include <io.h>
43 1.57 christos # undef mkdir
44 1.57 christos # define mkdir(name, mode) _mkdir(name)
45 1.57 christos #endif
46 1.57 christos
47 1.19 kleink #if HAVE_SYS_STAT_H
48 1.47 christos #include <sys/stat.h>
49 1.19 kleink #endif
50 1.19 kleink #ifdef S_IRUSR
51 1.19 kleink #define MKDIR_UMASK (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)
52 1.19 kleink #else
53 1.19 kleink #define MKDIR_UMASK 0755
54 1.19 kleink #endif
55 1.71 christos /* Port to native MS-Windows and to ancient UNIX. */
56 1.71 christos #if !defined S_ISDIR && defined S_IFDIR && defined S_IFMT
57 1.71 christos # define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
58 1.71 christos #endif
59 1.19 kleink
60 1.69 christos #if HAVE_SYS_WAIT_H
61 1.69 christos #include <sys/wait.h> /* for WIFEXITED and WEXITSTATUS */
62 1.69 christos #endif /* HAVE_SYS_WAIT_H */
63 1.69 christos
64 1.69 christos #ifndef WIFEXITED
65 1.69 christos #define WIFEXITED(status) (((status) & 0xff) == 0)
66 1.69 christos #endif /* !defined WIFEXITED */
67 1.69 christos #ifndef WEXITSTATUS
68 1.69 christos #define WEXITSTATUS(status) (((status) >> 8) & 0xff)
69 1.69 christos #endif /* !defined WEXITSTATUS */
70 1.69 christos
71 1.65 christos /* The maximum ptrdiff_t value, for pre-C99 platforms. */
72 1.65 christos #ifndef PTRDIFF_MAX
73 1.65 christos static ptrdiff_t const PTRDIFF_MAX = MAXVAL(ptrdiff_t, TYPE_BIT(ptrdiff_t));
74 1.65 christos #endif
75 1.65 christos
76 1.69 christos /* The type for line numbers. Use PRIdMAX to format them; formerly
77 1.69 christos there was also "#define PRIdLINENO PRIdMAX" and formats used
78 1.69 christos PRIdLINENO, but xgettext cannot grok that. */
79 1.65 christos typedef intmax_t lineno;
80 1.65 christos
81 1.1 jtc struct rule {
82 1.1 jtc const char * r_filename;
83 1.65 christos lineno r_linenum;
84 1.1 jtc const char * r_name;
85 1.1 jtc
86 1.41 christos zic_t r_loyear; /* for example, 1986 */
87 1.41 christos zic_t r_hiyear; /* for example, 1986 */
88 1.1 jtc const char * r_yrtype;
89 1.51 christos bool r_lowasnum;
90 1.51 christos bool r_hiwasnum;
91 1.1 jtc
92 1.1 jtc int r_month; /* 0..11 */
93 1.1 jtc
94 1.1 jtc int r_dycode; /* see below */
95 1.1 jtc int r_dayofmonth;
96 1.1 jtc int r_wday;
97 1.1 jtc
98 1.38 christos zic_t r_tod; /* time from midnight */
99 1.51 christos bool r_todisstd; /* above is standard time if 1 */
100 1.51 christos /* or wall clock time if 0 */
101 1.51 christos bool r_todisgmt; /* above is GMT if 1 */
102 1.51 christos /* or local time if 0 */
103 1.71 christos bool r_isdst; /* is this daylight saving time? */
104 1.71 christos zic_t r_stdoff; /* offset from default time (which is
105 1.71 christos usually standard time) */
106 1.1 jtc const char * r_abbrvar; /* variable part of abbreviation */
107 1.1 jtc
108 1.65 christos bool r_todo; /* a rule to do (used in outzone) */
109 1.25 mlelstv zic_t r_temp; /* used in outzone */
110 1.1 jtc };
111 1.1 jtc
112 1.1 jtc /*
113 1.1 jtc ** r_dycode r_dayofmonth r_wday
114 1.1 jtc */
115 1.1 jtc
116 1.1 jtc #define DC_DOM 0 /* 1..31 */ /* unused */
117 1.1 jtc #define DC_DOWGEQ 1 /* 1..31 */ /* 0..6 (Sun..Sat) */
118 1.1 jtc #define DC_DOWLEQ 2 /* 1..31 */ /* 0..6 (Sun..Sat) */
119 1.1 jtc
120 1.1 jtc struct zone {
121 1.1 jtc const char * z_filename;
122 1.65 christos lineno z_linenum;
123 1.1 jtc
124 1.1 jtc const char * z_name;
125 1.38 christos zic_t z_gmtoff;
126 1.71 christos char * z_rule;
127 1.1 jtc const char * z_format;
128 1.55 christos char z_format_specifier;
129 1.1 jtc
130 1.71 christos bool z_isdst;
131 1.38 christos zic_t z_stdoff;
132 1.1 jtc
133 1.1 jtc struct rule * z_rules;
134 1.65 christos ptrdiff_t z_nrules;
135 1.1 jtc
136 1.1 jtc struct rule z_untilrule;
137 1.25 mlelstv zic_t z_untiltime;
138 1.1 jtc };
139 1.1 jtc
140 1.57 christos #if !HAVE_POSIX_DECLS
141 1.25 mlelstv extern int getopt(int argc, char * const argv[],
142 1.25 mlelstv const char * options);
143 1.25 mlelstv extern int link(const char * fromname, const char * toname);
144 1.1 jtc extern char * optarg;
145 1.1 jtc extern int optind;
146 1.57 christos #endif
147 1.1 jtc
148 1.44 christos #if ! HAVE_LINK
149 1.57 christos # define link(from, to) (errno = ENOTSUP, -1)
150 1.44 christos #endif
151 1.44 christos #if ! HAVE_SYMLINK
152 1.68 christos # define readlink(file, buf, size) (errno = ENOTSUP, -1)
153 1.57 christos # define symlink(from, to) (errno = ENOTSUP, -1)
154 1.63 christos # define S_ISLNK(m) 0
155 1.44 christos #endif
156 1.68 christos #ifndef AT_SYMLINK_FOLLOW
157 1.68 christos # define linkat(fromdir, from, todir, to, flag) \
158 1.68 christos (itssymlink(from) ? (errno = ENOTSUP, -1) : link(from, to))
159 1.68 christos #endif
160 1.44 christos
161 1.25 mlelstv static void addtt(zic_t starttime, int type);
162 1.57 christos static int addtype(zic_t, char const *, bool, bool, bool);
163 1.51 christos static void leapadd(zic_t, bool, int, int);
164 1.25 mlelstv static void adjleap(void);
165 1.25 mlelstv static void associate(void);
166 1.63 christos static void dolink(const char *, const char *, bool);
167 1.25 mlelstv static char ** getfields(char * buf);
168 1.47 christos static zic_t gethms(const char * string, const char * errstring,
169 1.51 christos bool);
170 1.71 christos static zic_t getstdoff(char *, bool *);
171 1.25 mlelstv static void infile(const char * filename);
172 1.25 mlelstv static void inleap(char ** fields, int nfields);
173 1.25 mlelstv static void inlink(char ** fields, int nfields);
174 1.25 mlelstv static void inrule(char ** fields, int nfields);
175 1.51 christos static bool inzcont(char ** fields, int nfields);
176 1.51 christos static bool inzone(char ** fields, int nfields);
177 1.57 christos static bool inzsub(char **, int, int);
178 1.68 christos static bool itsdir(const char *);
179 1.68 christos static bool itssymlink(const char *);
180 1.51 christos static bool is_alpha(char a);
181 1.47 christos static char lowerit(char);
182 1.63 christos static void mkdirs(char const *, bool);
183 1.25 mlelstv static void newabbr(const char * abbr);
184 1.38 christos static zic_t oadd(zic_t t1, zic_t t2);
185 1.65 christos static void outzone(const struct zone * zp, ptrdiff_t ntzones);
186 1.41 christos static zic_t rpytime(const struct rule * rp, zic_t wantedy);
187 1.25 mlelstv static void rulesub(struct rule * rp,
188 1.1 jtc const char * loyearp, const char * hiyearp,
189 1.1 jtc const char * typep, const char * monthp,
190 1.25 mlelstv const char * dayp, const char * timep);
191 1.38 christos static zic_t tadd(zic_t t1, zic_t t2);
192 1.65 christos static bool yearistype(zic_t year, const char * type);
193 1.5 jtc
194 1.55 christos /* Bound on length of what %z can expand to. */
195 1.55 christos enum { PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1 };
196 1.55 christos
197 1.59 christos /* If true, work around a bug in Qt 5.6.1 and earlier, which mishandles
198 1.63 christos tz binary files whose POSIX-TZ-style strings contain '<'; see
199 1.59 christos QTBUG-53071 <https://bugreports.qt.io/browse/QTBUG-53071>. This
200 1.59 christos workaround will no longer be needed when Qt 5.6.1 and earlier are
201 1.59 christos obsolete, say in the year 2021. */
202 1.60 christos enum { WORK_AROUND_QTBUG_53071 = 1 };
203 1.59 christos
204 1.1 jtc static int charcnt;
205 1.51 christos static bool errors;
206 1.51 christos static bool warnings;
207 1.1 jtc static const char * filename;
208 1.1 jtc static int leapcnt;
209 1.51 christos static bool leapseen;
210 1.41 christos static zic_t leapminyear;
211 1.41 christos static zic_t leapmaxyear;
212 1.65 christos static lineno linenum;
213 1.55 christos static size_t max_abbrvar_len = PERCENT_Z_LEN_BOUND;
214 1.36 christos static size_t max_format_len;
215 1.41 christos static zic_t max_year;
216 1.41 christos static zic_t min_year;
217 1.51 christos static bool noise;
218 1.1 jtc static const char * rfilename;
219 1.65 christos static lineno rlinenum;
220 1.1 jtc static const char * progname;
221 1.65 christos static ptrdiff_t timecnt;
222 1.65 christos static ptrdiff_t timecnt_alloc;
223 1.1 jtc static int typecnt;
224 1.1 jtc
225 1.1 jtc /*
226 1.1 jtc ** Line codes.
227 1.1 jtc */
228 1.1 jtc
229 1.1 jtc #define LC_RULE 0
230 1.1 jtc #define LC_ZONE 1
231 1.1 jtc #define LC_LINK 2
232 1.1 jtc #define LC_LEAP 3
233 1.1 jtc
234 1.1 jtc /*
235 1.1 jtc ** Which fields are which on a Zone line.
236 1.1 jtc */
237 1.1 jtc
238 1.1 jtc #define ZF_NAME 1
239 1.1 jtc #define ZF_GMTOFF 2
240 1.1 jtc #define ZF_RULE 3
241 1.1 jtc #define ZF_FORMAT 4
242 1.1 jtc #define ZF_TILYEAR 5
243 1.1 jtc #define ZF_TILMONTH 6
244 1.1 jtc #define ZF_TILDAY 7
245 1.1 jtc #define ZF_TILTIME 8
246 1.1 jtc #define ZONE_MINFIELDS 5
247 1.1 jtc #define ZONE_MAXFIELDS 9
248 1.1 jtc
249 1.1 jtc /*
250 1.1 jtc ** Which fields are which on a Zone continuation line.
251 1.1 jtc */
252 1.1 jtc
253 1.1 jtc #define ZFC_GMTOFF 0
254 1.1 jtc #define ZFC_RULE 1
255 1.1 jtc #define ZFC_FORMAT 2
256 1.1 jtc #define ZFC_TILYEAR 3
257 1.1 jtc #define ZFC_TILMONTH 4
258 1.1 jtc #define ZFC_TILDAY 5
259 1.1 jtc #define ZFC_TILTIME 6
260 1.1 jtc #define ZONEC_MINFIELDS 3
261 1.1 jtc #define ZONEC_MAXFIELDS 7
262 1.1 jtc
263 1.1 jtc /*
264 1.1 jtc ** Which files are which on a Rule line.
265 1.1 jtc */
266 1.1 jtc
267 1.1 jtc #define RF_NAME 1
268 1.1 jtc #define RF_LOYEAR 2
269 1.1 jtc #define RF_HIYEAR 3
270 1.1 jtc #define RF_COMMAND 4
271 1.1 jtc #define RF_MONTH 5
272 1.1 jtc #define RF_DAY 6
273 1.1 jtc #define RF_TOD 7
274 1.1 jtc #define RF_STDOFF 8
275 1.1 jtc #define RF_ABBRVAR 9
276 1.1 jtc #define RULE_FIELDS 10
277 1.1 jtc
278 1.1 jtc /*
279 1.1 jtc ** Which fields are which on a Link line.
280 1.1 jtc */
281 1.1 jtc
282 1.1 jtc #define LF_FROM 1
283 1.1 jtc #define LF_TO 2
284 1.1 jtc #define LINK_FIELDS 3
285 1.1 jtc
286 1.1 jtc /*
287 1.1 jtc ** Which fields are which on a Leap line.
288 1.1 jtc */
289 1.1 jtc
290 1.1 jtc #define LP_YEAR 1
291 1.1 jtc #define LP_MONTH 2
292 1.1 jtc #define LP_DAY 3
293 1.1 jtc #define LP_TIME 4
294 1.1 jtc #define LP_CORR 5
295 1.1 jtc #define LP_ROLL 6
296 1.1 jtc #define LEAP_FIELDS 7
297 1.1 jtc
298 1.1 jtc /*
299 1.1 jtc ** Year synonyms.
300 1.1 jtc */
301 1.1 jtc
302 1.1 jtc #define YR_MINIMUM 0
303 1.1 jtc #define YR_MAXIMUM 1
304 1.1 jtc #define YR_ONLY 2
305 1.1 jtc
306 1.1 jtc static struct rule * rules;
307 1.65 christos static ptrdiff_t nrules; /* number of rules */
308 1.65 christos static ptrdiff_t nrules_alloc;
309 1.1 jtc
310 1.1 jtc static struct zone * zones;
311 1.65 christos static ptrdiff_t nzones; /* number of zones */
312 1.65 christos static ptrdiff_t nzones_alloc;
313 1.1 jtc
314 1.1 jtc struct link {
315 1.1 jtc const char * l_filename;
316 1.65 christos lineno l_linenum;
317 1.1 jtc const char * l_from;
318 1.1 jtc const char * l_to;
319 1.1 jtc };
320 1.1 jtc
321 1.1 jtc static struct link * links;
322 1.65 christos static ptrdiff_t nlinks;
323 1.65 christos static ptrdiff_t nlinks_alloc;
324 1.1 jtc
325 1.1 jtc struct lookup {
326 1.1 jtc const char * l_word;
327 1.1 jtc const int l_value;
328 1.1 jtc };
329 1.1 jtc
330 1.25 mlelstv static struct lookup const * byword(const char * string,
331 1.25 mlelstv const struct lookup * lp);
332 1.1 jtc
333 1.69 christos static struct lookup const zi_line_codes[] = {
334 1.1 jtc { "Rule", LC_RULE },
335 1.1 jtc { "Zone", LC_ZONE },
336 1.1 jtc { "Link", LC_LINK },
337 1.69 christos { NULL, 0 }
338 1.69 christos };
339 1.69 christos static struct lookup const leap_line_codes[] = {
340 1.1 jtc { "Leap", LC_LEAP },
341 1.1 jtc { NULL, 0}
342 1.1 jtc };
343 1.1 jtc
344 1.1 jtc static struct lookup const mon_names[] = {
345 1.1 jtc { "January", TM_JANUARY },
346 1.1 jtc { "February", TM_FEBRUARY },
347 1.1 jtc { "March", TM_MARCH },
348 1.1 jtc { "April", TM_APRIL },
349 1.1 jtc { "May", TM_MAY },
350 1.1 jtc { "June", TM_JUNE },
351 1.1 jtc { "July", TM_JULY },
352 1.1 jtc { "August", TM_AUGUST },
353 1.1 jtc { "September", TM_SEPTEMBER },
354 1.1 jtc { "October", TM_OCTOBER },
355 1.1 jtc { "November", TM_NOVEMBER },
356 1.1 jtc { "December", TM_DECEMBER },
357 1.1 jtc { NULL, 0 }
358 1.1 jtc };
359 1.1 jtc
360 1.1 jtc static struct lookup const wday_names[] = {
361 1.1 jtc { "Sunday", TM_SUNDAY },
362 1.1 jtc { "Monday", TM_MONDAY },
363 1.1 jtc { "Tuesday", TM_TUESDAY },
364 1.1 jtc { "Wednesday", TM_WEDNESDAY },
365 1.1 jtc { "Thursday", TM_THURSDAY },
366 1.1 jtc { "Friday", TM_FRIDAY },
367 1.1 jtc { "Saturday", TM_SATURDAY },
368 1.1 jtc { NULL, 0 }
369 1.1 jtc };
370 1.1 jtc
371 1.1 jtc static struct lookup const lasts[] = {
372 1.1 jtc { "last-Sunday", TM_SUNDAY },
373 1.1 jtc { "last-Monday", TM_MONDAY },
374 1.1 jtc { "last-Tuesday", TM_TUESDAY },
375 1.1 jtc { "last-Wednesday", TM_WEDNESDAY },
376 1.1 jtc { "last-Thursday", TM_THURSDAY },
377 1.1 jtc { "last-Friday", TM_FRIDAY },
378 1.1 jtc { "last-Saturday", TM_SATURDAY },
379 1.1 jtc { NULL, 0 }
380 1.1 jtc };
381 1.1 jtc
382 1.1 jtc static struct lookup const begin_years[] = {
383 1.1 jtc { "minimum", YR_MINIMUM },
384 1.1 jtc { "maximum", YR_MAXIMUM },
385 1.1 jtc { NULL, 0 }
386 1.1 jtc };
387 1.1 jtc
388 1.1 jtc static struct lookup const end_years[] = {
389 1.1 jtc { "minimum", YR_MINIMUM },
390 1.1 jtc { "maximum", YR_MAXIMUM },
391 1.1 jtc { "only", YR_ONLY },
392 1.1 jtc { NULL, 0 }
393 1.1 jtc };
394 1.1 jtc
395 1.1 jtc static struct lookup const leap_types[] = {
396 1.51 christos { "Rolling", true },
397 1.51 christos { "Stationary", false },
398 1.1 jtc { NULL, 0 }
399 1.1 jtc };
400 1.1 jtc
401 1.1 jtc static const int len_months[2][MONSPERYEAR] = {
402 1.1 jtc { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
403 1.1 jtc { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
404 1.1 jtc };
405 1.1 jtc
406 1.1 jtc static const int len_years[2] = {
407 1.1 jtc DAYSPERNYEAR, DAYSPERLYEAR
408 1.1 jtc };
409 1.1 jtc
410 1.5 jtc static struct attype {
411 1.25 mlelstv zic_t at;
412 1.63 christos bool dontmerge;
413 1.5 jtc unsigned char type;
414 1.45 christos } * attypes;
415 1.38 christos static zic_t gmtoffs[TZ_MAX_TYPES];
416 1.1 jtc static char isdsts[TZ_MAX_TYPES];
417 1.1 jtc static unsigned char abbrinds[TZ_MAX_TYPES];
418 1.51 christos static bool ttisstds[TZ_MAX_TYPES];
419 1.51 christos static bool ttisgmts[TZ_MAX_TYPES];
420 1.1 jtc static char chars[TZ_MAX_CHARS];
421 1.25 mlelstv static zic_t trans[TZ_MAX_LEAPS];
422 1.38 christos static zic_t corr[TZ_MAX_LEAPS];
423 1.1 jtc static char roll[TZ_MAX_LEAPS];
424 1.1 jtc
425 1.1 jtc /*
426 1.1 jtc ** Memory allocation.
427 1.1 jtc */
428 1.1 jtc
429 1.45 christos static _Noreturn void
430 1.45 christos memory_exhausted(const char *msg)
431 1.45 christos {
432 1.45 christos fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
433 1.45 christos exit(EXIT_FAILURE);
434 1.45 christos }
435 1.45 christos
436 1.45 christos static ATTRIBUTE_PURE size_t
437 1.45 christos size_product(size_t nitems, size_t itemsize)
438 1.45 christos {
439 1.45 christos if (SIZE_MAX / itemsize < nitems)
440 1.51 christos memory_exhausted(_("size overflow"));
441 1.45 christos return nitems * itemsize;
442 1.45 christos }
443 1.45 christos
444 1.53 christos #if !HAVE_STRDUP
445 1.53 christos static char *
446 1.53 christos strdup(char const *str)
447 1.53 christos {
448 1.53 christos char *result = malloc(strlen(str) + 1);
449 1.53 christos return result ? strcpy(result, str) : result;
450 1.53 christos }
451 1.53 christos #endif
452 1.53 christos
453 1.69 christos static void *
454 1.53 christos memcheck(void *ptr)
455 1.1 jtc {
456 1.45 christos if (ptr == NULL)
457 1.45 christos memory_exhausted(strerror(errno));
458 1.1 jtc return ptr;
459 1.1 jtc }
460 1.1 jtc
461 1.71 christos static void * ATTRIBUTE_MALLOC
462 1.53 christos zic_malloc(size_t size)
463 1.53 christos {
464 1.53 christos return memcheck(malloc(size));
465 1.53 christos }
466 1.53 christos
467 1.53 christos static void *
468 1.53 christos zic_realloc(void *ptr, size_t size)
469 1.53 christos {
470 1.53 christos return memcheck(realloc(ptr, size));
471 1.53 christos }
472 1.53 christos
473 1.71 christos static char * ATTRIBUTE_MALLOC
474 1.53 christos ecpyalloc(char const *str)
475 1.53 christos {
476 1.53 christos return memcheck(strdup(str));
477 1.53 christos }
478 1.1 jtc
479 1.45 christos static void *
480 1.65 christos growalloc(void *ptr, size_t itemsize, ptrdiff_t nitems, ptrdiff_t *nitems_alloc)
481 1.45 christos {
482 1.45 christos if (nitems < *nitems_alloc)
483 1.45 christos return ptr;
484 1.45 christos else {
485 1.65 christos ptrdiff_t nitems_max = PTRDIFF_MAX - WORK_AROUND_QTBUG_53071;
486 1.67 kre ptrdiff_t amax = (ptrdiff_t)((size_t)nitems_max < SIZE_MAX ?
487 1.67 kre (size_t)nitems_max : SIZE_MAX);
488 1.45 christos if ((amax - 1) / 3 * 2 < *nitems_alloc)
489 1.65 christos memory_exhausted(_("integer overflow"));
490 1.65 christos *nitems_alloc += (*nitems_alloc >> 1) + 1;
491 1.53 christos return zic_realloc(ptr, size_product(*nitems_alloc, itemsize));
492 1.45 christos }
493 1.45 christos }
494 1.45 christos
495 1.1 jtc /*
496 1.1 jtc ** Error handling.
497 1.1 jtc */
498 1.1 jtc
499 1.1 jtc static void
500 1.65 christos eats(char const *name, lineno num, char const *rname, lineno rnum)
501 1.1 jtc {
502 1.1 jtc filename = name;
503 1.1 jtc linenum = num;
504 1.1 jtc rfilename = rname;
505 1.1 jtc rlinenum = rnum;
506 1.1 jtc }
507 1.1 jtc
508 1.1 jtc static void
509 1.65 christos eat(char const *name, lineno num)
510 1.1 jtc {
511 1.31 christos eats(name, num, NULL, -1);
512 1.1 jtc }
513 1.1 jtc
514 1.43 christos static void ATTRIBUTE_FORMAT((printf, 1, 0))
515 1.43 christos verror(const char *const string, va_list args)
516 1.1 jtc {
517 1.1 jtc /*
518 1.1 jtc ** Match the format of "cc" to allow sh users to
519 1.1 jtc ** zic ... 2>&1 | error -t "*" -v
520 1.1 jtc ** on BSD systems.
521 1.1 jtc */
522 1.54 christos if (filename)
523 1.69 christos fprintf(stderr, _("\"%s\", line %"PRIdMAX": "), filename, linenum);
524 1.43 christos vfprintf(stderr, string, args);
525 1.1 jtc if (rfilename != NULL)
526 1.69 christos fprintf(stderr, _(" (rule from \"%s\", line %"PRIdMAX")"),
527 1.1 jtc rfilename, rlinenum);
528 1.51 christos fprintf(stderr, "\n");
529 1.1 jtc }
530 1.1 jtc
531 1.43 christos static void ATTRIBUTE_FORMAT((printf, 1, 2))
532 1.43 christos error(const char *const string, ...)
533 1.5 jtc {
534 1.43 christos va_list args;
535 1.43 christos va_start(args, string);
536 1.43 christos verror(string, args);
537 1.43 christos va_end(args);
538 1.51 christos errors = true;
539 1.43 christos }
540 1.43 christos
541 1.43 christos static void ATTRIBUTE_FORMAT((printf, 1, 2))
542 1.43 christos warning(const char *const string, ...)
543 1.43 christos {
544 1.43 christos va_list args;
545 1.43 christos fprintf(stderr, _("warning: "));
546 1.43 christos va_start(args, string);
547 1.43 christos verror(string, args);
548 1.43 christos va_end(args);
549 1.51 christos warnings = true;
550 1.51 christos }
551 1.51 christos
552 1.51 christos static void
553 1.63 christos close_file(FILE *stream, char const *dir, char const *name)
554 1.51 christos {
555 1.51 christos char const *e = (ferror(stream) ? _("I/O error")
556 1.51 christos : fclose(stream) != 0 ? strerror(errno) : NULL);
557 1.51 christos if (e) {
558 1.63 christos fprintf(stderr, "%s: %s%s%s%s%s\n", progname,
559 1.63 christos dir ? dir : "", dir ? "/" : "",
560 1.63 christos name ? name : "", name ? ": " : "",
561 1.63 christos e);
562 1.51 christos exit(EXIT_FAILURE);
563 1.51 christos }
564 1.5 jtc }
565 1.5 jtc
566 1.41 christos static _Noreturn void
567 1.25 mlelstv usage(FILE *stream, int status)
568 1.1 jtc {
569 1.51 christos fprintf(stream,
570 1.51 christos _("%s: usage is %s [ --version ] [ --help ] [ -v ] \\\n"
571 1.51 christos "\t[ -l localtime ] [ -p posixrules ] [ -d directory ] \\\n"
572 1.70 christos "\t[ -t localtime-link ] [ -L leapseconds ] [ filename ... ]\n\n"
573 1.51 christos "Report bugs to %s.\n"),
574 1.51 christos progname, progname, REPORT_BUGS_TO);
575 1.51 christos if (status == EXIT_SUCCESS)
576 1.63 christos close_file(stream, NULL, NULL);
577 1.51 christos exit(status);
578 1.1 jtc }
579 1.1 jtc
580 1.63 christos /* Change the working directory to DIR, possibly creating DIR and its
581 1.63 christos ancestors. After this is done, all files are accessed with names
582 1.63 christos relative to DIR. */
583 1.63 christos static void
584 1.63 christos change_directory (char const *dir)
585 1.63 christos {
586 1.63 christos if (chdir(dir) != 0) {
587 1.63 christos int chdir_errno = errno;
588 1.63 christos if (chdir_errno == ENOENT) {
589 1.63 christos mkdirs(dir, false);
590 1.63 christos chdir_errno = chdir(dir) == 0 ? 0 : errno;
591 1.63 christos }
592 1.63 christos if (chdir_errno != 0) {
593 1.63 christos fprintf(stderr, _("%s: Can't chdir to %s: %s\n"),
594 1.63 christos progname, dir, strerror(chdir_errno));
595 1.63 christos exit(EXIT_FAILURE);
596 1.63 christos }
597 1.63 christos }
598 1.63 christos }
599 1.63 christos
600 1.1 jtc static const char * psxrules;
601 1.1 jtc static const char * lcltime;
602 1.1 jtc static const char * directory;
603 1.1 jtc static const char * leapsec;
604 1.70 christos static const char * tzdefault;
605 1.1 jtc static const char * yitcommand;
606 1.1 jtc
607 1.1 jtc int
608 1.57 christos main(int argc, char **argv)
609 1.31 christos {
610 1.65 christos int c, k;
611 1.65 christos ptrdiff_t i, j;
612 1.1 jtc
613 1.41 christos #ifdef S_IWGRP
614 1.57 christos umask(umask(S_IWGRP | S_IWOTH) | (S_IWGRP | S_IWOTH));
615 1.41 christos #endif
616 1.57 christos #if HAVE_GETTEXT
617 1.57 christos setlocale(LC_MESSAGES, "");
618 1.5 jtc #ifdef TZ_DOMAINDIR
619 1.57 christos bindtextdomain(TZ_DOMAIN, TZ_DOMAINDIR);
620 1.5 jtc #endif /* defined TEXTDOMAINDIR */
621 1.57 christos textdomain(TZ_DOMAIN);
622 1.25 mlelstv #endif /* HAVE_GETTEXT */
623 1.1 jtc progname = argv[0];
624 1.25 mlelstv if (TYPE_BIT(zic_t) < 64) {
625 1.57 christos fprintf(stderr, "%s: %s\n", progname,
626 1.25 mlelstv _("wild compilation-time specification of zic_t"));
627 1.51 christos return EXIT_FAILURE;
628 1.25 mlelstv }
629 1.65 christos for (k = 1; k < argc; k++)
630 1.65 christos if (strcmp(argv[k], "--version") == 0) {
631 1.57 christos printf("zic %s%s\n", PKGVERSION, TZVERSION);
632 1.63 christos close_file(stdout, NULL, NULL);
633 1.51 christos return EXIT_SUCCESS;
634 1.65 christos } else if (strcmp(argv[k], "--help") == 0) {
635 1.25 mlelstv usage(stdout, EXIT_SUCCESS);
636 1.20 kleink }
637 1.70 christos while ((c = getopt(argc, argv, "d:l:L:p:st:vy:")) != EOF && c != -1)
638 1.1 jtc switch (c) {
639 1.1 jtc default:
640 1.25 mlelstv usage(stderr, EXIT_FAILURE);
641 1.1 jtc case 'd':
642 1.1 jtc if (directory == NULL)
643 1.1 jtc directory = optarg;
644 1.1 jtc else {
645 1.51 christos fprintf(stderr,
646 1.5 jtc _("%s: More than one -d option specified\n"),
647 1.1 jtc progname);
648 1.51 christos return EXIT_FAILURE;
649 1.1 jtc }
650 1.1 jtc break;
651 1.1 jtc case 'l':
652 1.1 jtc if (lcltime == NULL)
653 1.1 jtc lcltime = optarg;
654 1.1 jtc else {
655 1.51 christos fprintf(stderr,
656 1.5 jtc _("%s: More than one -l option specified\n"),
657 1.1 jtc progname);
658 1.51 christos return EXIT_FAILURE;
659 1.1 jtc }
660 1.1 jtc break;
661 1.1 jtc case 'p':
662 1.1 jtc if (psxrules == NULL)
663 1.1 jtc psxrules = optarg;
664 1.1 jtc else {
665 1.51 christos fprintf(stderr,
666 1.5 jtc _("%s: More than one -p option specified\n"),
667 1.1 jtc progname);
668 1.51 christos return EXIT_FAILURE;
669 1.1 jtc }
670 1.1 jtc break;
671 1.70 christos case 't':
672 1.70 christos if (tzdefault != NULL) {
673 1.70 christos fprintf(stderr,
674 1.70 christos _("%s: More than one -t option"
675 1.70 christos " specified\n"),
676 1.70 christos progname);
677 1.70 christos return EXIT_FAILURE;
678 1.70 christos }
679 1.70 christos tzdefault = optarg;
680 1.70 christos break;
681 1.1 jtc case 'y':
682 1.69 christos if (yitcommand == NULL) {
683 1.69 christos warning(_("-y is obsolescent"));
684 1.1 jtc yitcommand = optarg;
685 1.69 christos } else {
686 1.51 christos fprintf(stderr,
687 1.5 jtc _("%s: More than one -y option specified\n"),
688 1.1 jtc progname);
689 1.51 christos return EXIT_FAILURE;
690 1.1 jtc }
691 1.1 jtc break;
692 1.1 jtc case 'L':
693 1.1 jtc if (leapsec == NULL)
694 1.1 jtc leapsec = optarg;
695 1.1 jtc else {
696 1.51 christos fprintf(stderr,
697 1.5 jtc _("%s: More than one -L option specified\n"),
698 1.1 jtc progname);
699 1.51 christos return EXIT_FAILURE;
700 1.1 jtc }
701 1.1 jtc break;
702 1.1 jtc case 'v':
703 1.51 christos noise = true;
704 1.1 jtc break;
705 1.1 jtc case 's':
706 1.54 christos warning(_("-s ignored"));
707 1.1 jtc break;
708 1.1 jtc }
709 1.1 jtc if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
710 1.25 mlelstv usage(stderr, EXIT_FAILURE); /* usage message by request */
711 1.1 jtc if (directory == NULL)
712 1.1 jtc directory = TZDIR;
713 1.70 christos if (tzdefault == NULL)
714 1.70 christos tzdefault = TZDEFAULT;
715 1.1 jtc if (yitcommand == NULL)
716 1.1 jtc yitcommand = "yearistype";
717 1.1 jtc
718 1.1 jtc if (optind < argc && leapsec != NULL) {
719 1.1 jtc infile(leapsec);
720 1.1 jtc adjleap();
721 1.1 jtc }
722 1.1 jtc
723 1.65 christos for (k = optind; k < argc; k++)
724 1.65 christos infile(argv[k]);
725 1.1 jtc if (errors)
726 1.51 christos return EXIT_FAILURE;
727 1.1 jtc associate();
728 1.63 christos change_directory(directory);
729 1.1 jtc for (i = 0; i < nzones; i = j) {
730 1.1 jtc /*
731 1.1 jtc ** Find the next non-continuation zone entry.
732 1.1 jtc */
733 1.1 jtc for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
734 1.1 jtc continue;
735 1.1 jtc outzone(&zones[i], j - i);
736 1.1 jtc }
737 1.1 jtc /*
738 1.1 jtc ** Make links.
739 1.1 jtc */
740 1.15 kleink for (i = 0; i < nlinks; ++i) {
741 1.15 kleink eat(links[i].l_filename, links[i].l_linenum);
742 1.63 christos dolink(links[i].l_from, links[i].l_to, false);
743 1.25 mlelstv if (noise)
744 1.25 mlelstv for (j = 0; j < nlinks; ++j)
745 1.25 mlelstv if (strcmp(links[i].l_to,
746 1.25 mlelstv links[j].l_from) == 0)
747 1.25 mlelstv warning(_("link to link"));
748 1.15 kleink }
749 1.15 kleink if (lcltime != NULL) {
750 1.51 christos eat(_("command line"), 1);
751 1.70 christos dolink(lcltime, tzdefault, true);
752 1.15 kleink }
753 1.15 kleink if (psxrules != NULL) {
754 1.51 christos eat(_("command line"), 1);
755 1.63 christos dolink(psxrules, TZDEFRULES, true);
756 1.15 kleink }
757 1.51 christos if (warnings && (ferror(stderr) || fclose(stderr) != 0))
758 1.51 christos return EXIT_FAILURE;
759 1.51 christos return errors ? EXIT_FAILURE : EXIT_SUCCESS;
760 1.1 jtc }
761 1.1 jtc
762 1.54 christos static bool
763 1.47 christos componentcheck(char const *name, char const *component,
764 1.47 christos char const *component_end)
765 1.47 christos {
766 1.47 christos enum { component_len_max = 14 };
767 1.65 christos ptrdiff_t component_len = component_end - component;
768 1.53 christos if (component_len == 0) {
769 1.54 christos if (!*name)
770 1.54 christos error (_("empty file name"));
771 1.54 christos else
772 1.54 christos error (_(component == name
773 1.54 christos ? "file name '%s' begins with '/'"
774 1.54 christos : *component_end
775 1.54 christos ? "file name '%s' contains '//'"
776 1.54 christos : "file name '%s' ends with '/'"),
777 1.54 christos name);
778 1.54 christos return false;
779 1.53 christos }
780 1.47 christos if (0 < component_len && component_len <= 2
781 1.47 christos && component[0] == '.' && component_end[-1] == '.') {
782 1.65 christos int len = component_len;
783 1.54 christos error(_("file name '%s' contains '%.*s' component"),
784 1.65 christos name, len, component);
785 1.54 christos return false;
786 1.54 christos }
787 1.54 christos if (noise) {
788 1.54 christos if (0 < component_len && component[0] == '-')
789 1.54 christos warning(_("file name '%s' component contains leading '-'"),
790 1.54 christos name);
791 1.54 christos if (component_len_max < component_len)
792 1.54 christos warning(_("file name '%s' contains overlength component"
793 1.54 christos " '%.*s...'"),
794 1.54 christos name, component_len_max, component);
795 1.47 christos }
796 1.54 christos return true;
797 1.47 christos }
798 1.47 christos
799 1.54 christos static bool
800 1.47 christos namecheck(const char *name)
801 1.47 christos {
802 1.47 christos char const *cp;
803 1.47 christos
804 1.47 christos /* Benign characters in a portable file name. */
805 1.47 christos static char const benign[] =
806 1.47 christos "-/_"
807 1.47 christos "abcdefghijklmnopqrstuvwxyz"
808 1.47 christos "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
809 1.47 christos
810 1.47 christos /* Non-control chars in the POSIX portable character set,
811 1.47 christos excluding the benign characters. */
812 1.47 christos static char const printable_and_not_benign[] =
813 1.47 christos " !\"#$%&'()*+,.0123456789:;<=>?@[\\]^`{|}~";
814 1.47 christos
815 1.47 christos char const *component = name;
816 1.47 christos for (cp = name; *cp; cp++) {
817 1.47 christos unsigned char c = *cp;
818 1.47 christos if (noise && !strchr(benign, c)) {
819 1.47 christos warning((strchr(printable_and_not_benign, c)
820 1.47 christos ? _("file name '%s' contains byte '%c'")
821 1.47 christos : _("file name '%s' contains byte '\\%o'")),
822 1.47 christos name, c);
823 1.47 christos }
824 1.47 christos if (c == '/') {
825 1.54 christos if (!componentcheck(name, component, cp))
826 1.54 christos return false;
827 1.47 christos component = cp + 1;
828 1.47 christos }
829 1.47 christos }
830 1.54 christos return componentcheck(name, component, cp);
831 1.47 christos }
832 1.47 christos
833 1.64 kre /* Create symlink contents suitable for symlinking FROM to TO, as a
834 1.64 kre freshly allocated string. FROM should be a relative file name, and
835 1.64 kre is relative to the global variable DIRECTORY. TO can be either
836 1.64 kre relative or absolute. */
837 1.64 kre static char *
838 1.64 kre relname(char const *from, char const *to)
839 1.64 kre {
840 1.64 kre size_t i, taillen, dotdotetcsize;
841 1.64 kre size_t dir_len = 0, dotdots = 0, linksize = SIZE_MAX;
842 1.64 kre char const *f = from;
843 1.64 kre char *result = NULL;
844 1.64 kre if (*to == '/') {
845 1.64 kre /* Make F absolute too. */
846 1.64 kre size_t len = strlen(directory);
847 1.64 kre bool needslash = len && directory[len - 1] != '/';
848 1.64 kre linksize = len + needslash + strlen(from) + 1;
849 1.64 kre f = result = emalloc(linksize);
850 1.64 kre strcpy(result, directory);
851 1.64 kre result[len] = '/';
852 1.64 kre strcpy(result + len + needslash, from);
853 1.64 kre }
854 1.64 kre for (i = 0; f[i] && f[i] == to[i]; i++)
855 1.64 kre if (f[i] == '/')
856 1.64 kre dir_len = i + 1;
857 1.68 christos for (; to[i]; i++)
858 1.68 christos dotdots += to[i] == '/' && to[i - 1] != '/';
859 1.68 christos taillen = strlen(f + dir_len);
860 1.64 kre dotdotetcsize = 3 * dotdots + taillen + 1;
861 1.64 kre if (dotdotetcsize <= linksize) {
862 1.64 kre if (!result)
863 1.64 kre result = emalloc(dotdotetcsize);
864 1.64 kre for (i = 0; i < dotdots; i++)
865 1.64 kre memcpy(result + 3 * i, "../", 3);
866 1.64 kre memmove(result + 3 * dotdots, f + dir_len, taillen + 1);
867 1.64 kre }
868 1.64 kre return result;
869 1.64 kre }
870 1.64 kre
871 1.68 christos /* Hard link FROM to TO, following any symbolic links.
872 1.68 christos Return 0 if successful, an error number otherwise. */
873 1.68 christos static int
874 1.68 christos hardlinkerr(char const *from, char const *to)
875 1.68 christos {
876 1.68 christos int r = linkat(AT_FDCWD, from, AT_FDCWD, to, AT_SYMLINK_FOLLOW);
877 1.68 christos return r == 0 ? 0 : errno;
878 1.68 christos }
879 1.68 christos
880 1.47 christos static void
881 1.63 christos dolink(char const *fromfield, char const *tofield, bool staysymlink)
882 1.1 jtc {
883 1.63 christos bool todirs_made = false;
884 1.63 christos int link_errno;
885 1.1 jtc
886 1.1 jtc /*
887 1.1 jtc ** We get to be careful here since
888 1.1 jtc ** there's a fair chance of root running us.
889 1.1 jtc */
890 1.68 christos if (itsdir(fromfield)) {
891 1.63 christos fprintf(stderr, _("%s: link from %s/%s failed: %s\n"),
892 1.68 christos progname, directory, fromfield, strerror(EPERM));
893 1.47 christos exit(EXIT_FAILURE);
894 1.47 christos }
895 1.63 christos if (staysymlink)
896 1.68 christos staysymlink = itssymlink(tofield);
897 1.63 christos if (remove(tofield) == 0)
898 1.63 christos todirs_made = true;
899 1.63 christos else if (errno != ENOENT) {
900 1.63 christos char const *e = strerror(errno);
901 1.63 christos fprintf(stderr, _("%s: Can't remove %s/%s: %s\n"),
902 1.63 christos progname, directory, tofield, e);
903 1.63 christos exit(EXIT_FAILURE);
904 1.63 christos }
905 1.68 christos link_errno = staysymlink ? ENOTSUP : hardlinkerr(fromfield, tofield);
906 1.63 christos if (link_errno == ENOENT && !todirs_made) {
907 1.63 christos mkdirs(tofield, true);
908 1.63 christos todirs_made = true;
909 1.68 christos link_errno = hardlinkerr(fromfield, tofield);
910 1.63 christos }
911 1.63 christos if (link_errno != 0) {
912 1.64 kre bool absolute = *fromfield == '/';
913 1.64 kre char *linkalloc = absolute ? NULL : relname(fromfield, tofield);
914 1.64 kre char const *contents = absolute ? fromfield : linkalloc;
915 1.64 kre int symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno;
916 1.71 christos if (!todirs_made
917 1.71 christos && (symlink_errno == ENOENT || symlink_errno == ENOTSUP)) {
918 1.63 christos mkdirs(tofield, true);
919 1.71 christos if (symlink_errno == ENOENT)
920 1.71 christos symlink_errno = symlink(contents, tofield) == 0 ? 0 : errno;
921 1.63 christos }
922 1.64 kre free(linkalloc);
923 1.63 christos if (symlink_errno == 0) {
924 1.63 christos if (link_errno != ENOTSUP)
925 1.63 christos warning(_("symbolic link used because hard link failed: %s"),
926 1.63 christos strerror(link_errno));
927 1.63 christos } else {
928 1.63 christos FILE *fp, *tp;
929 1.63 christos int c;
930 1.63 christos fp = fopen(fromfield, "rb");
931 1.63 christos if (!fp) {
932 1.63 christos char const *e = strerror(errno);
933 1.63 christos fprintf(stderr, _("%s: Can't read %s/%s: %s\n"),
934 1.63 christos progname, directory, fromfield, e);
935 1.63 christos exit(EXIT_FAILURE);
936 1.63 christos }
937 1.63 christos tp = fopen(tofield, "wb");
938 1.63 christos if (!tp) {
939 1.63 christos char const *e = strerror(errno);
940 1.63 christos fprintf(stderr, _("%s: Can't create %s/%s: %s\n"),
941 1.63 christos progname, directory, tofield, e);
942 1.57 christos exit(EXIT_FAILURE);
943 1.57 christos }
944 1.63 christos while ((c = getc(fp)) != EOF)
945 1.63 christos putc(c, tp);
946 1.63 christos close_file(fp, directory, fromfield);
947 1.63 christos close_file(tp, directory, tofield);
948 1.63 christos if (link_errno != ENOTSUP)
949 1.63 christos warning(_("copy used because hard link failed: %s"),
950 1.63 christos strerror(link_errno));
951 1.63 christos else if (symlink_errno != ENOTSUP)
952 1.63 christos warning(_("copy used because symbolic link failed: %s"),
953 1.63 christos strerror(symlink_errno));
954 1.57 christos }
955 1.1 jtc }
956 1.1 jtc }
957 1.1 jtc
958 1.41 christos #define TIME_T_BITS_IN_FILE 64
959 1.41 christos
960 1.65 christos static zic_t const min_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE);
961 1.65 christos static zic_t const max_time = MAXVAL(zic_t, TIME_T_BITS_IN_FILE);
962 1.41 christos
963 1.46 christos /* Estimated time of the Big Bang, in seconds since the POSIX epoch.
964 1.46 christos rounded downward to the negation of a power of two that is
965 1.46 christos comfortably outside the error bounds.
966 1.46 christos
967 1.46 christos For the time of the Big Bang, see:
968 1.46 christos
969 1.46 christos Ade PAR, Aghanim N, Armitage-Caplan C et al. Planck 2013 results.
970 1.46 christos I. Overview of products and scientific results.
971 1.46 christos arXiv:1303.5062 2013-03-20 20:10:01 UTC
972 1.69 christos <https://arxiv.org/pdf/1303.5062v1> [PDF]
973 1.46 christos
974 1.46 christos Page 36, Table 9, row Age/Gyr, column Planck+WP+highL+BAO 68% limits
975 1.46 christos gives the value 13.798 plus-or-minus 0.037 billion years.
976 1.46 christos Multiplying this by 1000000000 and then by 31557600 (the number of
977 1.46 christos seconds in an astronomical year) gives a value that is comfortably
978 1.46 christos less than 2**59, so BIG_BANG is - 2**59.
979 1.46 christos
980 1.46 christos BIG_BANG is approximate, and may change in future versions.
981 1.46 christos Please do not rely on its exact value. */
982 1.46 christos
983 1.46 christos #ifndef BIG_BANG
984 1.46 christos #define BIG_BANG (- (1LL << 59))
985 1.46 christos #endif
986 1.46 christos
987 1.59 christos /* If true, work around GNOME bug 730332
988 1.59 christos <https://bugzilla.gnome.org/show_bug.cgi?id=730332>
989 1.59 christos by refusing to output time stamps before BIG_BANG.
990 1.59 christos Such time stamps are physically suspect anyway.
991 1.59 christos
992 1.59 christos The GNOME bug is scheduled to be fixed in GNOME 3.22, and if so
993 1.59 christos this workaround will no longer be needed when GNOME 3.21 and
994 1.59 christos earlier are obsolete, say in the year 2021. */
995 1.59 christos enum { WORK_AROUND_GNOME_BUG_730332 = true };
996 1.59 christos
997 1.59 christos static const zic_t early_time = (WORK_AROUND_GNOME_BUG_730332
998 1.59 christos ? BIG_BANG
999 1.59 christos : MINVAL(zic_t, TIME_T_BITS_IN_FILE));
1000 1.46 christos
1001 1.68 christos /* Return true if NAME is a directory. */
1002 1.68 christos static bool
1003 1.68 christos itsdir(const char *name)
1004 1.1 jtc {
1005 1.47 christos struct stat st;
1006 1.68 christos int res = stat(name, &st);
1007 1.47 christos #ifdef S_ISDIR
1008 1.68 christos if (res == 0)
1009 1.68 christos return S_ISDIR(st.st_mode) != 0;
1010 1.68 christos #endif
1011 1.68 christos if (res == 0 || errno == EOVERFLOW) {
1012 1.63 christos size_t n = strlen(name);
1013 1.63 christos char *nameslashdot = emalloc(n + 3);
1014 1.63 christos bool dir;
1015 1.63 christos memcpy(nameslashdot, name, n);
1016 1.63 christos strcpy(&nameslashdot[n], &"/."[! (n && name[n - 1] != '/')]);
1017 1.68 christos dir = stat(nameslashdot, &st) == 0 || errno == EOVERFLOW;
1018 1.47 christos free(nameslashdot);
1019 1.57 christos return dir;
1020 1.47 christos }
1021 1.68 christos return false;
1022 1.68 christos }
1023 1.68 christos
1024 1.68 christos /* Return true if NAME is a symbolic link. */
1025 1.68 christos static bool
1026 1.68 christos itssymlink(char const *name)
1027 1.68 christos {
1028 1.68 christos char c;
1029 1.68 christos return 0 <= readlink(name, &c, 1);
1030 1.1 jtc }
1031 1.1 jtc
1032 1.1 jtc /*
1033 1.1 jtc ** Associate sets of rules with zones.
1034 1.1 jtc */
1035 1.1 jtc
1036 1.1 jtc /*
1037 1.1 jtc ** Sort by rule name.
1038 1.1 jtc */
1039 1.1 jtc
1040 1.1 jtc static int
1041 1.31 christos rcomp(const void *cp1, const void *cp2)
1042 1.1 jtc {
1043 1.1 jtc return strcmp(((const struct rule *) cp1)->r_name,
1044 1.1 jtc ((const struct rule *) cp2)->r_name);
1045 1.1 jtc }
1046 1.1 jtc
1047 1.1 jtc static void
1048 1.25 mlelstv associate(void)
1049 1.1 jtc {
1050 1.31 christos struct zone * zp;
1051 1.31 christos struct rule * rp;
1052 1.65 christos ptrdiff_t base, out;
1053 1.31 christos int i, j;
1054 1.1 jtc
1055 1.5 jtc if (nrules != 0) {
1056 1.57 christos qsort(rules, (size_t)nrules, sizeof *rules, rcomp);
1057 1.5 jtc for (i = 0; i < nrules - 1; ++i) {
1058 1.5 jtc if (strcmp(rules[i].r_name,
1059 1.5 jtc rules[i + 1].r_name) != 0)
1060 1.5 jtc continue;
1061 1.5 jtc if (strcmp(rules[i].r_filename,
1062 1.5 jtc rules[i + 1].r_filename) == 0)
1063 1.5 jtc continue;
1064 1.5 jtc eat(rules[i].r_filename, rules[i].r_linenum);
1065 1.5 jtc warning(_("same rule name in multiple files"));
1066 1.5 jtc eat(rules[i + 1].r_filename, rules[i + 1].r_linenum);
1067 1.5 jtc warning(_("same rule name in multiple files"));
1068 1.5 jtc for (j = i + 2; j < nrules; ++j) {
1069 1.5 jtc if (strcmp(rules[i].r_name,
1070 1.5 jtc rules[j].r_name) != 0)
1071 1.5 jtc break;
1072 1.5 jtc if (strcmp(rules[i].r_filename,
1073 1.5 jtc rules[j].r_filename) == 0)
1074 1.5 jtc continue;
1075 1.5 jtc if (strcmp(rules[i + 1].r_filename,
1076 1.5 jtc rules[j].r_filename) == 0)
1077 1.5 jtc continue;
1078 1.5 jtc break;
1079 1.5 jtc }
1080 1.5 jtc i = j - 1;
1081 1.5 jtc }
1082 1.5 jtc }
1083 1.1 jtc for (i = 0; i < nzones; ++i) {
1084 1.1 jtc zp = &zones[i];
1085 1.1 jtc zp->z_rules = NULL;
1086 1.1 jtc zp->z_nrules = 0;
1087 1.1 jtc }
1088 1.1 jtc for (base = 0; base < nrules; base = out) {
1089 1.1 jtc rp = &rules[base];
1090 1.1 jtc for (out = base + 1; out < nrules; ++out)
1091 1.1 jtc if (strcmp(rp->r_name, rules[out].r_name) != 0)
1092 1.1 jtc break;
1093 1.1 jtc for (i = 0; i < nzones; ++i) {
1094 1.1 jtc zp = &zones[i];
1095 1.1 jtc if (strcmp(zp->z_rule, rp->r_name) != 0)
1096 1.1 jtc continue;
1097 1.1 jtc zp->z_rules = rp;
1098 1.1 jtc zp->z_nrules = out - base;
1099 1.1 jtc }
1100 1.1 jtc }
1101 1.1 jtc for (i = 0; i < nzones; ++i) {
1102 1.1 jtc zp = &zones[i];
1103 1.1 jtc if (zp->z_nrules == 0) {
1104 1.1 jtc /*
1105 1.1 jtc ** Maybe we have a local standard time offset.
1106 1.1 jtc */
1107 1.1 jtc eat(zp->z_filename, zp->z_linenum);
1108 1.71 christos zp->z_stdoff = getstdoff(zp->z_rule, &zp->z_isdst);
1109 1.1 jtc /*
1110 1.1 jtc ** Note, though, that if there's no rule,
1111 1.1 jtc ** a '%s' in the format is a bad thing.
1112 1.1 jtc */
1113 1.55 christos if (zp->z_format_specifier == 's')
1114 1.43 christos error("%s", _("%s in ruleless zone"));
1115 1.1 jtc }
1116 1.1 jtc }
1117 1.1 jtc if (errors)
1118 1.25 mlelstv exit(EXIT_FAILURE);
1119 1.1 jtc }
1120 1.1 jtc
1121 1.1 jtc static void
1122 1.31 christos infile(const char *name)
1123 1.1 jtc {
1124 1.31 christos FILE * fp;
1125 1.31 christos char ** fields;
1126 1.31 christos char * cp;
1127 1.31 christos const struct lookup * lp;
1128 1.31 christos int nfields;
1129 1.51 christos bool wantcont;
1130 1.65 christos lineno num;
1131 1.1 jtc char buf[BUFSIZ];
1132 1.1 jtc
1133 1.1 jtc if (strcmp(name, "-") == 0) {
1134 1.5 jtc name = _("standard input");
1135 1.1 jtc fp = stdin;
1136 1.1 jtc } else if ((fp = fopen(name, "r")) == NULL) {
1137 1.5 jtc const char *e = strerror(errno);
1138 1.7 jtc
1139 1.51 christos fprintf(stderr, _("%s: Can't open %s: %s\n"),
1140 1.5 jtc progname, name, e);
1141 1.25 mlelstv exit(EXIT_FAILURE);
1142 1.1 jtc }
1143 1.51 christos wantcont = false;
1144 1.1 jtc for (num = 1; ; ++num) {
1145 1.1 jtc eat(name, num);
1146 1.1 jtc if (fgets(buf, (int) sizeof buf, fp) != buf)
1147 1.1 jtc break;
1148 1.1 jtc cp = strchr(buf, '\n');
1149 1.1 jtc if (cp == NULL) {
1150 1.40 christos error(_("line too long"));
1151 1.25 mlelstv exit(EXIT_FAILURE);
1152 1.1 jtc }
1153 1.1 jtc *cp = '\0';
1154 1.1 jtc fields = getfields(buf);
1155 1.1 jtc nfields = 0;
1156 1.1 jtc while (fields[nfields] != NULL) {
1157 1.1 jtc static char nada;
1158 1.1 jtc
1159 1.3 jtc if (strcmp(fields[nfields], "-") == 0)
1160 1.1 jtc fields[nfields] = &nada;
1161 1.1 jtc ++nfields;
1162 1.1 jtc }
1163 1.1 jtc if (nfields == 0) {
1164 1.1 jtc /* nothing to do */
1165 1.1 jtc } else if (wantcont) {
1166 1.1 jtc wantcont = inzcont(fields, nfields);
1167 1.1 jtc } else {
1168 1.69 christos struct lookup const *line_codes
1169 1.69 christos = name == leapsec ? leap_line_codes : zi_line_codes;
1170 1.1 jtc lp = byword(fields[0], line_codes);
1171 1.1 jtc if (lp == NULL)
1172 1.5 jtc error(_("input line of unknown type"));
1173 1.65 christos else switch (lp->l_value) {
1174 1.1 jtc case LC_RULE:
1175 1.1 jtc inrule(fields, nfields);
1176 1.51 christos wantcont = false;
1177 1.1 jtc break;
1178 1.1 jtc case LC_ZONE:
1179 1.1 jtc wantcont = inzone(fields, nfields);
1180 1.1 jtc break;
1181 1.1 jtc case LC_LINK:
1182 1.1 jtc inlink(fields, nfields);
1183 1.51 christos wantcont = false;
1184 1.1 jtc break;
1185 1.1 jtc case LC_LEAP:
1186 1.69 christos inleap(fields, nfields);
1187 1.51 christos wantcont = false;
1188 1.1 jtc break;
1189 1.1 jtc default: /* "cannot happen" */
1190 1.51 christos fprintf(stderr,
1191 1.5 jtc _("%s: panic: Invalid l_value %d\n"),
1192 1.1 jtc progname, lp->l_value);
1193 1.25 mlelstv exit(EXIT_FAILURE);
1194 1.1 jtc }
1195 1.1 jtc }
1196 1.31 christos free(fields);
1197 1.1 jtc }
1198 1.63 christos close_file(fp, NULL, filename);
1199 1.1 jtc if (wantcont)
1200 1.5 jtc error(_("expected continuation line not found"));
1201 1.1 jtc }
1202 1.1 jtc
1203 1.1 jtc /*
1204 1.1 jtc ** Convert a string of one of the forms
1205 1.1 jtc ** h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
1206 1.1 jtc ** into a number of seconds.
1207 1.1 jtc ** A null string maps to zero.
1208 1.1 jtc ** Call error with errstring and return zero on errors.
1209 1.1 jtc */
1210 1.1 jtc
1211 1.38 christos static zic_t
1212 1.51 christos gethms(char const *string, char const *errstring, bool signable)
1213 1.1 jtc {
1214 1.38 christos zic_t hh;
1215 1.71 christos int sign, mm = 0, ss = 0;
1216 1.71 christos char hhx, mmx, ssx, xr = '0', xs;
1217 1.71 christos int tenths = 0;
1218 1.71 christos bool ok = true;
1219 1.1 jtc
1220 1.1 jtc if (string == NULL || *string == '\0')
1221 1.1 jtc return 0;
1222 1.1 jtc if (!signable)
1223 1.1 jtc sign = 1;
1224 1.1 jtc else if (*string == '-') {
1225 1.1 jtc sign = -1;
1226 1.1 jtc ++string;
1227 1.1 jtc } else sign = 1;
1228 1.71 christos switch (sscanf(string,
1229 1.71 christos "%"SCNdZIC"%c%d%c%d%c%1d%*[0]%c%*[0123456789]%c",
1230 1.71 christos &hh, &hhx, &mm, &mmx, &ss, &ssx, &tenths, &xr, &xs)) {
1231 1.71 christos default: ok = false; break;
1232 1.71 christos case 8:
1233 1.71 christos ok = '0' <= xr && xr <= '9';
1234 1.71 christos /* fallthrough */
1235 1.71 christos case 7:
1236 1.71 christos ok &= ssx == '.';
1237 1.71 christos if (ok && noise)
1238 1.71 christos warning(_("fractional seconds rejected by"
1239 1.71 christos " pre-2018 versions of zic"));
1240 1.71 christos /* fallthrough */
1241 1.71 christos case 5: ok &= mmx == ':'; /* fallthrough */
1242 1.71 christos case 3: ok &= hhx == ':'; /* fallthrough */
1243 1.71 christos case 1: break;
1244 1.71 christos }
1245 1.71 christos if (!ok) {
1246 1.43 christos error("%s", errstring);
1247 1.1 jtc return 0;
1248 1.1 jtc }
1249 1.25 mlelstv if (hh < 0 ||
1250 1.1 jtc mm < 0 || mm >= MINSPERHOUR ||
1251 1.25 mlelstv ss < 0 || ss > SECSPERMIN) {
1252 1.43 christos error("%s", errstring);
1253 1.1 jtc return 0;
1254 1.1 jtc }
1255 1.41 christos if (ZIC_MAX / SECSPERHOUR < hh) {
1256 1.25 mlelstv error(_("time overflow"));
1257 1.25 mlelstv return 0;
1258 1.25 mlelstv }
1259 1.71 christos ss += 5 + ((ss ^ 1) & (xr == '0')) <= tenths; /* Round to even. */
1260 1.25 mlelstv if (noise && (hh > HOURSPERDAY ||
1261 1.25 mlelstv (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
1262 1.25 mlelstv warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
1263 1.41 christos return oadd(sign * hh * SECSPERHOUR,
1264 1.41 christos sign * (mm * SECSPERMIN + ss));
1265 1.1 jtc }
1266 1.1 jtc
1267 1.71 christos static zic_t
1268 1.71 christos getstdoff(char *field, bool *isdst)
1269 1.71 christos {
1270 1.71 christos int dst = -1;
1271 1.71 christos zic_t stdoff;
1272 1.71 christos size_t fieldlen = strlen(field);
1273 1.71 christos if (fieldlen != 0) {
1274 1.71 christos char *ep = field + fieldlen - 1;
1275 1.71 christos switch (*ep) {
1276 1.71 christos case 'd': dst = 1; *ep = '\0'; break;
1277 1.71 christos case 's': dst = 0; *ep = '\0'; break;
1278 1.71 christos }
1279 1.71 christos }
1280 1.71 christos stdoff = gethms(field, _("invalid saved time"), true);
1281 1.71 christos *isdst = dst < 0 ? stdoff != 0 : dst;
1282 1.71 christos return stdoff;
1283 1.71 christos }
1284 1.71 christos
1285 1.1 jtc static void
1286 1.55 christos inrule(char **fields, int nfields)
1287 1.1 jtc {
1288 1.1 jtc static struct rule r;
1289 1.1 jtc
1290 1.1 jtc if (nfields != RULE_FIELDS) {
1291 1.5 jtc error(_("wrong number of fields on Rule line"));
1292 1.1 jtc return;
1293 1.1 jtc }
1294 1.1 jtc if (*fields[RF_NAME] == '\0') {
1295 1.5 jtc error(_("nameless rule"));
1296 1.1 jtc return;
1297 1.1 jtc }
1298 1.1 jtc r.r_filename = filename;
1299 1.1 jtc r.r_linenum = linenum;
1300 1.71 christos r.r_stdoff = getstdoff(fields[RF_STDOFF], &r.r_isdst);
1301 1.1 jtc rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR], fields[RF_COMMAND],
1302 1.1 jtc fields[RF_MONTH], fields[RF_DAY], fields[RF_TOD]);
1303 1.1 jtc r.r_name = ecpyalloc(fields[RF_NAME]);
1304 1.1 jtc r.r_abbrvar = ecpyalloc(fields[RF_ABBRVAR]);
1305 1.25 mlelstv if (max_abbrvar_len < strlen(r.r_abbrvar))
1306 1.25 mlelstv max_abbrvar_len = strlen(r.r_abbrvar);
1307 1.45 christos rules = growalloc(rules, sizeof *rules, nrules, &nrules_alloc);
1308 1.1 jtc rules[nrules++] = r;
1309 1.1 jtc }
1310 1.1 jtc
1311 1.51 christos static bool
1312 1.55 christos inzone(char **fields, int nfields)
1313 1.1 jtc {
1314 1.65 christos ptrdiff_t i;
1315 1.1 jtc
1316 1.1 jtc if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS) {
1317 1.5 jtc error(_("wrong number of fields on Zone line"));
1318 1.51 christos return false;
1319 1.1 jtc }
1320 1.70 christos if (lcltime != NULL && strcmp(fields[ZF_NAME], tzdefault) == 0) {
1321 1.43 christos error(
1322 1.5 jtc _("\"Zone %s\" line and -l option are mutually exclusive"),
1323 1.70 christos tzdefault);
1324 1.51 christos return false;
1325 1.1 jtc }
1326 1.1 jtc if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL) {
1327 1.43 christos error(
1328 1.5 jtc _("\"Zone %s\" line and -p option are mutually exclusive"),
1329 1.1 jtc TZDEFRULES);
1330 1.51 christos return false;
1331 1.1 jtc }
1332 1.1 jtc for (i = 0; i < nzones; ++i)
1333 1.1 jtc if (zones[i].z_name != NULL &&
1334 1.65 christos strcmp(zones[i].z_name, fields[ZF_NAME]) == 0) {
1335 1.65 christos error(_("duplicate zone name %s"
1336 1.69 christos " (file \"%s\", line %"PRIdMAX")"),
1337 1.65 christos fields[ZF_NAME],
1338 1.65 christos zones[i].z_filename,
1339 1.65 christos zones[i].z_linenum);
1340 1.65 christos return false;
1341 1.1 jtc }
1342 1.51 christos return inzsub(fields, nfields, false);
1343 1.1 jtc }
1344 1.1 jtc
1345 1.51 christos static bool
1346 1.55 christos inzcont(char **fields, int nfields)
1347 1.1 jtc {
1348 1.1 jtc if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS) {
1349 1.5 jtc error(_("wrong number of fields on Zone continuation line"));
1350 1.51 christos return false;
1351 1.1 jtc }
1352 1.51 christos return inzsub(fields, nfields, true);
1353 1.1 jtc }
1354 1.1 jtc
1355 1.51 christos static bool
1356 1.57 christos inzsub(char **fields, int nfields, const int iscont)
1357 1.1 jtc {
1358 1.31 christos char * cp;
1359 1.55 christos char * cp1;
1360 1.1 jtc static struct zone z;
1361 1.31 christos int i_gmtoff, i_rule, i_format;
1362 1.31 christos int i_untilyear, i_untilmonth;
1363 1.31 christos int i_untilday, i_untiltime;
1364 1.51 christos bool hasuntil;
1365 1.1 jtc
1366 1.1 jtc if (iscont) {
1367 1.1 jtc i_gmtoff = ZFC_GMTOFF;
1368 1.1 jtc i_rule = ZFC_RULE;
1369 1.1 jtc i_format = ZFC_FORMAT;
1370 1.1 jtc i_untilyear = ZFC_TILYEAR;
1371 1.1 jtc i_untilmonth = ZFC_TILMONTH;
1372 1.1 jtc i_untilday = ZFC_TILDAY;
1373 1.1 jtc i_untiltime = ZFC_TILTIME;
1374 1.1 jtc z.z_name = NULL;
1375 1.54 christos } else if (!namecheck(fields[ZF_NAME]))
1376 1.54 christos return false;
1377 1.54 christos else {
1378 1.1 jtc i_gmtoff = ZF_GMTOFF;
1379 1.1 jtc i_rule = ZF_RULE;
1380 1.1 jtc i_format = ZF_FORMAT;
1381 1.1 jtc i_untilyear = ZF_TILYEAR;
1382 1.1 jtc i_untilmonth = ZF_TILMONTH;
1383 1.1 jtc i_untilday = ZF_TILDAY;
1384 1.1 jtc i_untiltime = ZF_TILTIME;
1385 1.1 jtc z.z_name = ecpyalloc(fields[ZF_NAME]);
1386 1.1 jtc }
1387 1.1 jtc z.z_filename = filename;
1388 1.1 jtc z.z_linenum = linenum;
1389 1.51 christos z.z_gmtoff = gethms(fields[i_gmtoff], _("invalid UT offset"), true);
1390 1.1 jtc if ((cp = strchr(fields[i_format], '%')) != 0) {
1391 1.55 christos if ((*++cp != 's' && *cp != 'z') || strchr(cp, '%')
1392 1.55 christos || strchr(fields[i_format], '/')) {
1393 1.5 jtc error(_("invalid abbreviation format"));
1394 1.51 christos return false;
1395 1.1 jtc }
1396 1.1 jtc }
1397 1.1 jtc z.z_rule = ecpyalloc(fields[i_rule]);
1398 1.55 christos z.z_format = cp1 = ecpyalloc(fields[i_format]);
1399 1.55 christos z.z_format_specifier = cp ? *cp : '\0';
1400 1.55 christos if (z.z_format_specifier == 'z') {
1401 1.55 christos if (noise)
1402 1.55 christos warning(_("format '%s' not handled by pre-2015 versions of zic"),
1403 1.55 christos z.z_format);
1404 1.55 christos cp1[cp - fields[i_format]] = 's';
1405 1.55 christos }
1406 1.25 mlelstv if (max_format_len < strlen(z.z_format))
1407 1.25 mlelstv max_format_len = strlen(z.z_format);
1408 1.1 jtc hasuntil = nfields > i_untilyear;
1409 1.1 jtc if (hasuntil) {
1410 1.1 jtc z.z_untilrule.r_filename = filename;
1411 1.1 jtc z.z_untilrule.r_linenum = linenum;
1412 1.1 jtc rulesub(&z.z_untilrule,
1413 1.1 jtc fields[i_untilyear],
1414 1.1 jtc "only",
1415 1.1 jtc "",
1416 1.1 jtc (nfields > i_untilmonth) ?
1417 1.1 jtc fields[i_untilmonth] : "Jan",
1418 1.1 jtc (nfields > i_untilday) ? fields[i_untilday] : "1",
1419 1.1 jtc (nfields > i_untiltime) ? fields[i_untiltime] : "0");
1420 1.1 jtc z.z_untiltime = rpytime(&z.z_untilrule,
1421 1.1 jtc z.z_untilrule.r_loyear);
1422 1.1 jtc if (iscont && nzones > 0 &&
1423 1.1 jtc z.z_untiltime > min_time &&
1424 1.1 jtc z.z_untiltime < max_time &&
1425 1.1 jtc zones[nzones - 1].z_untiltime > min_time &&
1426 1.1 jtc zones[nzones - 1].z_untiltime < max_time &&
1427 1.1 jtc zones[nzones - 1].z_untiltime >= z.z_untiltime) {
1428 1.25 mlelstv error(_(
1429 1.25 mlelstv "Zone continuation line end time is not after end time of previous line"
1430 1.25 mlelstv ));
1431 1.51 christos return false;
1432 1.1 jtc }
1433 1.1 jtc }
1434 1.45 christos zones = growalloc(zones, sizeof *zones, nzones, &nzones_alloc);
1435 1.1 jtc zones[nzones++] = z;
1436 1.1 jtc /*
1437 1.1 jtc ** If there was an UNTIL field on this line,
1438 1.1 jtc ** there's more information about the zone on the next line.
1439 1.1 jtc */
1440 1.1 jtc return hasuntil;
1441 1.1 jtc }
1442 1.1 jtc
1443 1.1 jtc static void
1444 1.55 christos inleap(char **fields, int nfields)
1445 1.31 christos {
1446 1.31 christos const char * cp;
1447 1.31 christos const struct lookup * lp;
1448 1.65 christos zic_t i, j;
1449 1.41 christos zic_t year;
1450 1.41 christos int month, day;
1451 1.41 christos zic_t dayoff, tod;
1452 1.41 christos zic_t t;
1453 1.53 christos char xs;
1454 1.1 jtc
1455 1.1 jtc if (nfields != LEAP_FIELDS) {
1456 1.5 jtc error(_("wrong number of fields on Leap line"));
1457 1.1 jtc return;
1458 1.1 jtc }
1459 1.1 jtc dayoff = 0;
1460 1.1 jtc cp = fields[LP_YEAR];
1461 1.53 christos if (sscanf(cp, "%"SCNdZIC"%c", &year, &xs) != 1) {
1462 1.25 mlelstv /*
1463 1.25 mlelstv ** Leapin' Lizards!
1464 1.25 mlelstv */
1465 1.25 mlelstv error(_("invalid leaping year"));
1466 1.25 mlelstv return;
1467 1.1 jtc }
1468 1.25 mlelstv if (!leapseen || leapmaxyear < year)
1469 1.25 mlelstv leapmaxyear = year;
1470 1.25 mlelstv if (!leapseen || leapminyear > year)
1471 1.25 mlelstv leapminyear = year;
1472 1.51 christos leapseen = true;
1473 1.1 jtc j = EPOCH_YEAR;
1474 1.1 jtc while (j != year) {
1475 1.1 jtc if (year > j) {
1476 1.1 jtc i = len_years[isleap(j)];
1477 1.1 jtc ++j;
1478 1.1 jtc } else {
1479 1.1 jtc --j;
1480 1.1 jtc i = -len_years[isleap(j)];
1481 1.1 jtc }
1482 1.41 christos dayoff = oadd(dayoff, i);
1483 1.1 jtc }
1484 1.1 jtc if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL) {
1485 1.5 jtc error(_("invalid month name"));
1486 1.1 jtc return;
1487 1.1 jtc }
1488 1.1 jtc month = lp->l_value;
1489 1.1 jtc j = TM_JANUARY;
1490 1.1 jtc while (j != month) {
1491 1.1 jtc i = len_months[isleap(year)][j];
1492 1.41 christos dayoff = oadd(dayoff, i);
1493 1.1 jtc ++j;
1494 1.1 jtc }
1495 1.1 jtc cp = fields[LP_DAY];
1496 1.53 christos if (sscanf(cp, "%d%c", &day, &xs) != 1 ||
1497 1.1 jtc day <= 0 || day > len_months[isleap(year)][month]) {
1498 1.5 jtc error(_("invalid day of month"));
1499 1.1 jtc return;
1500 1.1 jtc }
1501 1.41 christos dayoff = oadd(dayoff, day - 1);
1502 1.34 martin if (dayoff < min_time / SECSPERDAY) {
1503 1.20 kleink error(_("time too small"));
1504 1.20 kleink return;
1505 1.20 kleink }
1506 1.34 martin if (dayoff > max_time / SECSPERDAY) {
1507 1.20 kleink error(_("time too large"));
1508 1.1 jtc return;
1509 1.1 jtc }
1510 1.46 christos t = dayoff * SECSPERDAY;
1511 1.51 christos tod = gethms(fields[LP_TIME], _("invalid time of day"), false);
1512 1.1 jtc cp = fields[LP_CORR];
1513 1.1 jtc {
1514 1.51 christos bool positive;
1515 1.51 christos int count;
1516 1.1 jtc
1517 1.1 jtc if (strcmp(cp, "") == 0) { /* infile() turns "-" into "" */
1518 1.51 christos positive = false;
1519 1.1 jtc count = 1;
1520 1.1 jtc } else if (strcmp(cp, "+") == 0) {
1521 1.51 christos positive = true;
1522 1.1 jtc count = 1;
1523 1.1 jtc } else {
1524 1.5 jtc error(_("illegal CORRECTION field on Leap line"));
1525 1.1 jtc return;
1526 1.1 jtc }
1527 1.1 jtc if ((lp = byword(fields[LP_ROLL], leap_types)) == NULL) {
1528 1.25 mlelstv error(_(
1529 1.25 mlelstv "illegal Rolling/Stationary field on Leap line"
1530 1.25 mlelstv ));
1531 1.1 jtc return;
1532 1.1 jtc }
1533 1.46 christos t = tadd(t, tod);
1534 1.69 christos if (t < 0) {
1535 1.69 christos error(_("leap second precedes Epoch"));
1536 1.46 christos return;
1537 1.46 christos }
1538 1.46 christos leapadd(t, positive, lp->l_value, count);
1539 1.1 jtc }
1540 1.1 jtc }
1541 1.1 jtc
1542 1.1 jtc static void
1543 1.57 christos inlink(char **fields, int nfields)
1544 1.1 jtc {
1545 1.1 jtc struct link l;
1546 1.1 jtc
1547 1.1 jtc if (nfields != LINK_FIELDS) {
1548 1.5 jtc error(_("wrong number of fields on Link line"));
1549 1.1 jtc return;
1550 1.1 jtc }
1551 1.1 jtc if (*fields[LF_FROM] == '\0') {
1552 1.5 jtc error(_("blank FROM field on Link line"));
1553 1.1 jtc return;
1554 1.1 jtc }
1555 1.54 christos if (! namecheck(fields[LF_TO]))
1556 1.54 christos return;
1557 1.1 jtc l.l_filename = filename;
1558 1.1 jtc l.l_linenum = linenum;
1559 1.1 jtc l.l_from = ecpyalloc(fields[LF_FROM]);
1560 1.1 jtc l.l_to = ecpyalloc(fields[LF_TO]);
1561 1.45 christos links = growalloc(links, sizeof *links, nlinks, &nlinks_alloc);
1562 1.1 jtc links[nlinks++] = l;
1563 1.1 jtc }
1564 1.1 jtc
1565 1.1 jtc static void
1566 1.55 christos rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
1567 1.55 christos const char *typep, const char *monthp, const char *dayp,
1568 1.55 christos const char *timep)
1569 1.31 christos {
1570 1.31 christos const struct lookup * lp;
1571 1.31 christos const char * cp;
1572 1.31 christos char * dp;
1573 1.31 christos char * ep;
1574 1.53 christos char xs;
1575 1.1 jtc
1576 1.1 jtc if ((lp = byword(monthp, mon_names)) == NULL) {
1577 1.5 jtc error(_("invalid month name"));
1578 1.1 jtc return;
1579 1.1 jtc }
1580 1.1 jtc rp->r_month = lp->l_value;
1581 1.51 christos rp->r_todisstd = false;
1582 1.51 christos rp->r_todisgmt = false;
1583 1.1 jtc dp = ecpyalloc(timep);
1584 1.1 jtc if (*dp != '\0') {
1585 1.1 jtc ep = dp + strlen(dp) - 1;
1586 1.1 jtc switch (lowerit(*ep)) {
1587 1.1 jtc case 's': /* Standard */
1588 1.51 christos rp->r_todisstd = true;
1589 1.51 christos rp->r_todisgmt = false;
1590 1.1 jtc *ep = '\0';
1591 1.1 jtc break;
1592 1.1 jtc case 'w': /* Wall */
1593 1.51 christos rp->r_todisstd = false;
1594 1.51 christos rp->r_todisgmt = false;
1595 1.1 jtc *ep = '\0';
1596 1.7 jtc break;
1597 1.1 jtc case 'g': /* Greenwich */
1598 1.1 jtc case 'u': /* Universal */
1599 1.1 jtc case 'z': /* Zulu */
1600 1.51 christos rp->r_todisstd = true;
1601 1.51 christos rp->r_todisgmt = true;
1602 1.1 jtc *ep = '\0';
1603 1.1 jtc break;
1604 1.1 jtc }
1605 1.1 jtc }
1606 1.51 christos rp->r_tod = gethms(dp, _("invalid time of day"), false);
1607 1.31 christos free(dp);
1608 1.1 jtc /*
1609 1.1 jtc ** Year work.
1610 1.1 jtc */
1611 1.1 jtc cp = loyearp;
1612 1.1 jtc lp = byword(cp, begin_years);
1613 1.25 mlelstv rp->r_lowasnum = lp == NULL;
1614 1.65 christos if (!rp->r_lowasnum) switch (lp->l_value) {
1615 1.1 jtc case YR_MINIMUM:
1616 1.41 christos rp->r_loyear = ZIC_MIN;
1617 1.1 jtc break;
1618 1.1 jtc case YR_MAXIMUM:
1619 1.41 christos rp->r_loyear = ZIC_MAX;
1620 1.1 jtc break;
1621 1.1 jtc default: /* "cannot happen" */
1622 1.51 christos fprintf(stderr,
1623 1.5 jtc _("%s: panic: Invalid l_value %d\n"),
1624 1.1 jtc progname, lp->l_value);
1625 1.25 mlelstv exit(EXIT_FAILURE);
1626 1.53 christos } else if (sscanf(cp, "%"SCNdZIC"%c", &rp->r_loyear, &xs) != 1) {
1627 1.5 jtc error(_("invalid starting year"));
1628 1.1 jtc return;
1629 1.11 jtc }
1630 1.1 jtc cp = hiyearp;
1631 1.25 mlelstv lp = byword(cp, end_years);
1632 1.25 mlelstv rp->r_hiwasnum = lp == NULL;
1633 1.65 christos if (!rp->r_hiwasnum) switch (lp->l_value) {
1634 1.1 jtc case YR_MINIMUM:
1635 1.41 christos rp->r_hiyear = ZIC_MIN;
1636 1.1 jtc break;
1637 1.1 jtc case YR_MAXIMUM:
1638 1.41 christos rp->r_hiyear = ZIC_MAX;
1639 1.1 jtc break;
1640 1.1 jtc case YR_ONLY:
1641 1.1 jtc rp->r_hiyear = rp->r_loyear;
1642 1.1 jtc break;
1643 1.1 jtc default: /* "cannot happen" */
1644 1.51 christos fprintf(stderr,
1645 1.5 jtc _("%s: panic: Invalid l_value %d\n"),
1646 1.1 jtc progname, lp->l_value);
1647 1.25 mlelstv exit(EXIT_FAILURE);
1648 1.53 christos } else if (sscanf(cp, "%"SCNdZIC"%c", &rp->r_hiyear, &xs) != 1) {
1649 1.5 jtc error(_("invalid ending year"));
1650 1.1 jtc return;
1651 1.11 jtc }
1652 1.1 jtc if (rp->r_loyear > rp->r_hiyear) {
1653 1.5 jtc error(_("starting year greater than ending year"));
1654 1.1 jtc return;
1655 1.1 jtc }
1656 1.1 jtc if (*typep == '\0')
1657 1.1 jtc rp->r_yrtype = NULL;
1658 1.1 jtc else {
1659 1.1 jtc if (rp->r_loyear == rp->r_hiyear) {
1660 1.5 jtc error(_("typed single year"));
1661 1.1 jtc return;
1662 1.1 jtc }
1663 1.69 christos warning(_("year type \"%s\" is obsolete; use \"-\" instead"),
1664 1.69 christos typep);
1665 1.1 jtc rp->r_yrtype = ecpyalloc(typep);
1666 1.1 jtc }
1667 1.1 jtc /*
1668 1.1 jtc ** Day work.
1669 1.1 jtc ** Accept things such as:
1670 1.1 jtc ** 1
1671 1.69 christos ** lastSunday
1672 1.69 christos ** last-Sunday (undocumented; warn about this)
1673 1.1 jtc ** Sun<=20
1674 1.1 jtc ** Sun>=7
1675 1.1 jtc */
1676 1.1 jtc dp = ecpyalloc(dayp);
1677 1.1 jtc if ((lp = byword(dp, lasts)) != NULL) {
1678 1.1 jtc rp->r_dycode = DC_DOWLEQ;
1679 1.1 jtc rp->r_wday = lp->l_value;
1680 1.1 jtc rp->r_dayofmonth = len_months[1][rp->r_month];
1681 1.1 jtc } else {
1682 1.1 jtc if ((ep = strchr(dp, '<')) != 0)
1683 1.1 jtc rp->r_dycode = DC_DOWLEQ;
1684 1.1 jtc else if ((ep = strchr(dp, '>')) != 0)
1685 1.1 jtc rp->r_dycode = DC_DOWGEQ;
1686 1.1 jtc else {
1687 1.1 jtc ep = dp;
1688 1.1 jtc rp->r_dycode = DC_DOM;
1689 1.1 jtc }
1690 1.1 jtc if (rp->r_dycode != DC_DOM) {
1691 1.1 jtc *ep++ = 0;
1692 1.1 jtc if (*ep++ != '=') {
1693 1.5 jtc error(_("invalid day of month"));
1694 1.31 christos free(dp);
1695 1.1 jtc return;
1696 1.1 jtc }
1697 1.1 jtc if ((lp = byword(dp, wday_names)) == NULL) {
1698 1.5 jtc error(_("invalid weekday name"));
1699 1.31 christos free(dp);
1700 1.1 jtc return;
1701 1.1 jtc }
1702 1.1 jtc rp->r_wday = lp->l_value;
1703 1.1 jtc }
1704 1.53 christos if (sscanf(ep, "%d%c", &rp->r_dayofmonth, &xs) != 1 ||
1705 1.1 jtc rp->r_dayofmonth <= 0 ||
1706 1.1 jtc (rp->r_dayofmonth > len_months[1][rp->r_month])) {
1707 1.5 jtc error(_("invalid day of month"));
1708 1.31 christos free(dp);
1709 1.1 jtc return;
1710 1.1 jtc }
1711 1.1 jtc }
1712 1.31 christos free(dp);
1713 1.1 jtc }
1714 1.1 jtc
1715 1.1 jtc static void
1716 1.38 christos convert(const zic_t val, char *const buf)
1717 1.1 jtc {
1718 1.31 christos int i;
1719 1.31 christos int shift;
1720 1.31 christos unsigned char *const b = (unsigned char *) buf;
1721 1.1 jtc
1722 1.1 jtc for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
1723 1.31 christos b[i] = val >> shift;
1724 1.1 jtc }
1725 1.1 jtc
1726 1.1 jtc static void
1727 1.31 christos convert64(const zic_t val, char *const buf)
1728 1.25 mlelstv {
1729 1.31 christos int i;
1730 1.31 christos int shift;
1731 1.31 christos unsigned char *const b = (unsigned char *) buf;
1732 1.25 mlelstv
1733 1.25 mlelstv for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
1734 1.31 christos b[i] = val >> shift;
1735 1.25 mlelstv }
1736 1.25 mlelstv
1737 1.25 mlelstv static void
1738 1.38 christos puttzcode(const zic_t val, FILE *const fp)
1739 1.1 jtc {
1740 1.1 jtc char buf[4];
1741 1.1 jtc
1742 1.1 jtc convert(val, buf);
1743 1.57 christos fwrite(buf, sizeof buf, (size_t) 1, fp);
1744 1.1 jtc }
1745 1.1 jtc
1746 1.25 mlelstv static void
1747 1.31 christos puttzcode64(const zic_t val, FILE *const fp)
1748 1.25 mlelstv {
1749 1.25 mlelstv char buf[8];
1750 1.25 mlelstv
1751 1.25 mlelstv convert64(val, buf);
1752 1.57 christos fwrite(buf, sizeof buf, (size_t) 1, fp);
1753 1.25 mlelstv }
1754 1.25 mlelstv
1755 1.5 jtc static int
1756 1.31 christos atcomp(const void *avp, const void *bvp)
1757 1.5 jtc {
1758 1.25 mlelstv const zic_t a = ((const struct attype *) avp)->at;
1759 1.25 mlelstv const zic_t b = ((const struct attype *) bvp)->at;
1760 1.25 mlelstv
1761 1.25 mlelstv return (a < b) ? -1 : (a > b);
1762 1.25 mlelstv }
1763 1.25 mlelstv
1764 1.51 christos static bool
1765 1.31 christos is32(const zic_t x)
1766 1.25 mlelstv {
1767 1.25 mlelstv return INT32_MIN <= x && x <= INT32_MAX;
1768 1.5 jtc }
1769 1.5 jtc
1770 1.1 jtc static void
1771 1.43 christos writezone(const char *const name, const char *const string, char version)
1772 1.31 christos {
1773 1.31 christos FILE * fp;
1774 1.65 christos ptrdiff_t i, j;
1775 1.31 christos int leapcnt32, leapi32;
1776 1.65 christos ptrdiff_t timecnt32, timei32;
1777 1.31 christos int pass;
1778 1.25 mlelstv static const struct tzhead tzh0;
1779 1.25 mlelstv static struct tzhead tzh;
1780 1.63 christos bool dir_checked = false;
1781 1.59 christos zic_t one = 1;
1782 1.59 christos zic_t y2038_boundary = one << 31;
1783 1.65 christos ptrdiff_t nats = timecnt + WORK_AROUND_QTBUG_53071;
1784 1.60 christos zic_t *ats = zic_malloc(size_product(nats, sizeof *ats + 1));
1785 1.59 christos void *typesptr = ats + nats;
1786 1.45 christos unsigned char *types = typesptr;
1787 1.5 jtc
1788 1.5 jtc /*
1789 1.5 jtc ** Sort.
1790 1.5 jtc */
1791 1.5 jtc if (timecnt > 1)
1792 1.57 christos qsort(attypes, (size_t) timecnt, sizeof *attypes, atcomp);
1793 1.5 jtc /*
1794 1.5 jtc ** Optimize.
1795 1.5 jtc */
1796 1.5 jtc {
1797 1.65 christos ptrdiff_t fromi, toi;
1798 1.1 jtc
1799 1.5 jtc toi = 0;
1800 1.5 jtc fromi = 0;
1801 1.59 christos while (fromi < timecnt && attypes[fromi].at < early_time)
1802 1.7 jtc ++fromi;
1803 1.5 jtc for ( ; fromi < timecnt; ++fromi) {
1804 1.45 christos if (toi > 1 && ((attypes[fromi].at +
1805 1.25 mlelstv gmtoffs[attypes[toi - 1].type]) <=
1806 1.45 christos (attypes[toi - 1].at +
1807 1.45 christos gmtoffs[attypes[toi - 2].type]))) {
1808 1.25 mlelstv attypes[toi - 1].type =
1809 1.25 mlelstv attypes[fromi].type;
1810 1.25 mlelstv continue;
1811 1.5 jtc }
1812 1.63 christos if (toi == 0
1813 1.63 christos || attypes[fromi].dontmerge
1814 1.63 christos || attypes[toi - 1].type != attypes[fromi].type)
1815 1.5 jtc attypes[toi++] = attypes[fromi];
1816 1.5 jtc }
1817 1.5 jtc timecnt = toi;
1818 1.5 jtc }
1819 1.65 christos
1820 1.65 christos if (noise && timecnt > 1200) {
1821 1.65 christos if (timecnt > TZ_MAX_TIMES)
1822 1.65 christos warning(_("reference clients mishandle"
1823 1.65 christos " more than %d transition times"),
1824 1.65 christos TZ_MAX_TIMES);
1825 1.65 christos else
1826 1.45 christos warning(_("pre-2014 clients may mishandle"
1827 1.45 christos " more than 1200 transition times"));
1828 1.65 christos }
1829 1.5 jtc /*
1830 1.5 jtc ** Transfer.
1831 1.5 jtc */
1832 1.5 jtc for (i = 0; i < timecnt; ++i) {
1833 1.5 jtc ats[i] = attypes[i].at;
1834 1.5 jtc types[i] = attypes[i].type;
1835 1.5 jtc }
1836 1.59 christos
1837 1.59 christos /* Work around QTBUG-53071 for time stamps less than y2038_boundary - 1,
1838 1.59 christos by inserting a no-op transition at time y2038_boundary - 1.
1839 1.59 christos This works only for timestamps before the boundary, which
1840 1.59 christos should be good enough in practice as QTBUG-53071 should be
1841 1.59 christos long-dead by 2038. */
1842 1.59 christos if (WORK_AROUND_QTBUG_53071 && timecnt != 0
1843 1.59 christos && ats[timecnt - 1] < y2038_boundary - 1 && strchr(string, '<')) {
1844 1.59 christos ats[timecnt] = y2038_boundary - 1;
1845 1.59 christos types[timecnt] = types[timecnt - 1];
1846 1.59 christos timecnt++;
1847 1.59 christos }
1848 1.59 christos
1849 1.25 mlelstv /*
1850 1.25 mlelstv ** Correct for leap seconds.
1851 1.25 mlelstv */
1852 1.25 mlelstv for (i = 0; i < timecnt; ++i) {
1853 1.25 mlelstv j = leapcnt;
1854 1.25 mlelstv while (--j >= 0)
1855 1.25 mlelstv if (ats[i] > trans[j] - corr[j]) {
1856 1.25 mlelstv ats[i] = tadd(ats[i], corr[j]);
1857 1.25 mlelstv break;
1858 1.25 mlelstv }
1859 1.25 mlelstv }
1860 1.25 mlelstv /*
1861 1.25 mlelstv ** Figure out 32-bit-limited starts and counts.
1862 1.25 mlelstv */
1863 1.25 mlelstv timecnt32 = timecnt;
1864 1.25 mlelstv timei32 = 0;
1865 1.25 mlelstv leapcnt32 = leapcnt;
1866 1.25 mlelstv leapi32 = 0;
1867 1.25 mlelstv while (timecnt32 > 0 && !is32(ats[timecnt32 - 1]))
1868 1.25 mlelstv --timecnt32;
1869 1.25 mlelstv while (timecnt32 > 0 && !is32(ats[timei32])) {
1870 1.25 mlelstv --timecnt32;
1871 1.25 mlelstv ++timei32;
1872 1.25 mlelstv }
1873 1.45 christos /*
1874 1.47 christos ** Output an INT32_MIN "transition" if appropriate; see below.
1875 1.45 christos */
1876 1.45 christos if (timei32 > 0 && ats[timei32] > INT32_MIN) {
1877 1.45 christos --timei32;
1878 1.45 christos ++timecnt32;
1879 1.45 christos }
1880 1.25 mlelstv while (leapcnt32 > 0 && !is32(trans[leapcnt32 - 1]))
1881 1.25 mlelstv --leapcnt32;
1882 1.25 mlelstv while (leapcnt32 > 0 && !is32(trans[leapi32])) {
1883 1.25 mlelstv --leapcnt32;
1884 1.25 mlelstv ++leapi32;
1885 1.25 mlelstv }
1886 1.7 jtc /*
1887 1.7 jtc ** Remove old file, if any, to snap links.
1888 1.7 jtc */
1889 1.63 christos if (remove(name) == 0)
1890 1.63 christos dir_checked = true;
1891 1.63 christos else if (errno != ENOENT) {
1892 1.7 jtc const char *e = strerror(errno);
1893 1.7 jtc
1894 1.63 christos fprintf(stderr, _("%s: Can't remove %s/%s: %s\n"),
1895 1.63 christos progname, directory, name, e);
1896 1.25 mlelstv exit(EXIT_FAILURE);
1897 1.7 jtc }
1898 1.63 christos fp = fopen(name, "wb");
1899 1.63 christos if (!fp) {
1900 1.63 christos int fopen_errno = errno;
1901 1.63 christos if (fopen_errno == ENOENT && !dir_checked) {
1902 1.63 christos mkdirs(name, true);
1903 1.63 christos fp = fopen(name, "wb");
1904 1.63 christos fopen_errno = errno;
1905 1.63 christos }
1906 1.63 christos if (!fp) {
1907 1.63 christos fprintf(stderr, _("%s: Can't create %s/%s: %s\n"),
1908 1.63 christos progname, directory, name, strerror(fopen_errno));
1909 1.63 christos exit(EXIT_FAILURE);
1910 1.63 christos }
1911 1.1 jtc }
1912 1.25 mlelstv for (pass = 1; pass <= 2; ++pass) {
1913 1.65 christos ptrdiff_t thistimei, thistimecnt, thistimelim;
1914 1.65 christos int thisleapi, thisleapcnt, thisleaplim;
1915 1.45 christos int writetype[TZ_MAX_TYPES];
1916 1.25 mlelstv int typemap[TZ_MAX_TYPES];
1917 1.31 christos int thistypecnt;
1918 1.25 mlelstv char thischars[TZ_MAX_CHARS];
1919 1.65 christos int thischarcnt;
1920 1.65 christos bool toomanytimes;
1921 1.51 christos int indmap[TZ_MAX_CHARS];
1922 1.25 mlelstv
1923 1.25 mlelstv if (pass == 1) {
1924 1.25 mlelstv thistimei = timei32;
1925 1.25 mlelstv thistimecnt = timecnt32;
1926 1.65 christos toomanytimes = thistimecnt >> 31 >> 1 != 0;
1927 1.25 mlelstv thisleapi = leapi32;
1928 1.25 mlelstv thisleapcnt = leapcnt32;
1929 1.25 mlelstv } else {
1930 1.25 mlelstv thistimei = 0;
1931 1.25 mlelstv thistimecnt = timecnt;
1932 1.65 christos toomanytimes = thistimecnt >> 31 >> 31 >> 2 != 0;
1933 1.25 mlelstv thisleapi = 0;
1934 1.25 mlelstv thisleapcnt = leapcnt;
1935 1.25 mlelstv }
1936 1.65 christos if (toomanytimes)
1937 1.65 christos error(_("too many transition times"));
1938 1.25 mlelstv thistimelim = thistimei + thistimecnt;
1939 1.25 mlelstv thisleaplim = thisleapi + thisleapcnt;
1940 1.45 christos for (i = 0; i < typecnt; ++i)
1941 1.25 mlelstv writetype[i] = thistimecnt == timecnt;
1942 1.25 mlelstv if (thistimecnt == 0) {
1943 1.25 mlelstv /*
1944 1.25 mlelstv ** No transition times fall in the current
1945 1.25 mlelstv ** (32- or 64-bit) window.
1946 1.25 mlelstv */
1947 1.25 mlelstv if (typecnt != 0)
1948 1.51 christos writetype[typecnt - 1] = true;
1949 1.25 mlelstv } else {
1950 1.25 mlelstv for (i = thistimei - 1; i < thistimelim; ++i)
1951 1.25 mlelstv if (i >= 0)
1952 1.51 christos writetype[types[i]] = true;
1953 1.25 mlelstv /*
1954 1.25 mlelstv ** For America/Godthab and Antarctica/Palmer
1955 1.25 mlelstv */
1956 1.25 mlelstv if (thistimei == 0)
1957 1.51 christos writetype[0] = true;
1958 1.25 mlelstv }
1959 1.29 christos #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
1960 1.29 christos /*
1961 1.29 christos ** For some pre-2011 systems: if the last-to-be-written
1962 1.29 christos ** standard (or daylight) type has an offset different from the
1963 1.29 christos ** most recently used offset,
1964 1.29 christos ** append an (unused) copy of the most recently used type
1965 1.29 christos ** (to help get global "altzone" and "timezone" variables
1966 1.29 christos ** set correctly).
1967 1.29 christos */
1968 1.29 christos {
1969 1.31 christos int mrudst, mrustd, hidst, histd, type;
1970 1.29 christos
1971 1.29 christos hidst = histd = mrudst = mrustd = -1;
1972 1.42 christos for (i = thistimei; i < thistimelim; ++i) {
1973 1.42 christos if (i < 0)
1974 1.42 christos continue;
1975 1.29 christos if (isdsts[types[i]])
1976 1.29 christos mrudst = types[i];
1977 1.29 christos else mrustd = types[i];
1978 1.42 christos }
1979 1.29 christos for (i = 0; i < typecnt; ++i)
1980 1.29 christos if (writetype[i]) {
1981 1.29 christos if (isdsts[i])
1982 1.29 christos hidst = i;
1983 1.29 christos else histd = i;
1984 1.29 christos }
1985 1.29 christos if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
1986 1.29 christos gmtoffs[hidst] != gmtoffs[mrudst]) {
1987 1.29 christos isdsts[mrudst] = -1;
1988 1.29 christos type = addtype(gmtoffs[mrudst],
1989 1.29 christos &chars[abbrinds[mrudst]],
1990 1.51 christos true,
1991 1.29 christos ttisstds[mrudst],
1992 1.29 christos ttisgmts[mrudst]);
1993 1.51 christos isdsts[mrudst] = 1;
1994 1.51 christos writetype[type] = true;
1995 1.29 christos }
1996 1.29 christos if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
1997 1.29 christos gmtoffs[histd] != gmtoffs[mrustd]) {
1998 1.29 christos isdsts[mrustd] = -1;
1999 1.29 christos type = addtype(gmtoffs[mrustd],
2000 1.29 christos &chars[abbrinds[mrustd]],
2001 1.51 christos false,
2002 1.29 christos ttisstds[mrustd],
2003 1.29 christos ttisgmts[mrustd]);
2004 1.51 christos isdsts[mrustd] = 0;
2005 1.51 christos writetype[type] = true;
2006 1.29 christos }
2007 1.29 christos }
2008 1.29 christos #endif /* !defined LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
2009 1.25 mlelstv thistypecnt = 0;
2010 1.25 mlelstv for (i = 0; i < typecnt; ++i)
2011 1.45 christos typemap[i] = writetype[i] ? thistypecnt++ : -1;
2012 1.36 christos for (i = 0; i < (int)(sizeof indmap / sizeof indmap[0]); ++i)
2013 1.25 mlelstv indmap[i] = -1;
2014 1.25 mlelstv thischarcnt = 0;
2015 1.25 mlelstv for (i = 0; i < typecnt; ++i) {
2016 1.31 christos char * thisabbr;
2017 1.25 mlelstv
2018 1.25 mlelstv if (!writetype[i])
2019 1.25 mlelstv continue;
2020 1.25 mlelstv if (indmap[abbrinds[i]] >= 0)
2021 1.25 mlelstv continue;
2022 1.25 mlelstv thisabbr = &chars[abbrinds[i]];
2023 1.25 mlelstv for (j = 0; j < thischarcnt; ++j)
2024 1.25 mlelstv if (strcmp(&thischars[j], thisabbr) == 0)
2025 1.25 mlelstv break;
2026 1.25 mlelstv if (j == thischarcnt) {
2027 1.65 christos strcpy(&thischars[thischarcnt], thisabbr);
2028 1.25 mlelstv thischarcnt += strlen(thisabbr) + 1;
2029 1.25 mlelstv }
2030 1.25 mlelstv indmap[abbrinds[i]] = j;
2031 1.25 mlelstv }
2032 1.57 christos #define DO(field) fwrite(tzh.field, sizeof tzh.field, (size_t) 1, fp)
2033 1.25 mlelstv tzh = tzh0;
2034 1.70 christos memcpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
2035 1.43 christos tzh.tzh_version[0] = version;
2036 1.41 christos convert(thistypecnt, tzh.tzh_ttisgmtcnt);
2037 1.41 christos convert(thistypecnt, tzh.tzh_ttisstdcnt);
2038 1.41 christos convert(thisleapcnt, tzh.tzh_leapcnt);
2039 1.41 christos convert(thistimecnt, tzh.tzh_timecnt);
2040 1.41 christos convert(thistypecnt, tzh.tzh_typecnt);
2041 1.41 christos convert(thischarcnt, tzh.tzh_charcnt);
2042 1.25 mlelstv DO(tzh_magic);
2043 1.25 mlelstv DO(tzh_version);
2044 1.25 mlelstv DO(tzh_reserved);
2045 1.25 mlelstv DO(tzh_ttisgmtcnt);
2046 1.25 mlelstv DO(tzh_ttisstdcnt);
2047 1.25 mlelstv DO(tzh_leapcnt);
2048 1.25 mlelstv DO(tzh_timecnt);
2049 1.25 mlelstv DO(tzh_typecnt);
2050 1.25 mlelstv DO(tzh_charcnt);
2051 1.1 jtc #undef DO
2052 1.25 mlelstv for (i = thistimei; i < thistimelim; ++i)
2053 1.25 mlelstv if (pass == 1)
2054 1.45 christos /*
2055 1.45 christos ** Output an INT32_MIN "transition"
2056 1.47 christos ** if appropriate; see above.
2057 1.45 christos */
2058 1.45 christos puttzcode(((ats[i] < INT32_MIN) ?
2059 1.45 christos INT32_MIN : ats[i]), fp);
2060 1.25 mlelstv else puttzcode64(ats[i], fp);
2061 1.25 mlelstv for (i = thistimei; i < thistimelim; ++i) {
2062 1.25 mlelstv unsigned char uc;
2063 1.25 mlelstv
2064 1.25 mlelstv uc = typemap[types[i]];
2065 1.57 christos fwrite(&uc, sizeof uc, (size_t) 1, fp);
2066 1.25 mlelstv }
2067 1.25 mlelstv for (i = 0; i < typecnt; ++i)
2068 1.25 mlelstv if (writetype[i]) {
2069 1.25 mlelstv puttzcode(gmtoffs[i], fp);
2070 1.57 christos putc(isdsts[i], fp);
2071 1.57 christos putc((unsigned char) indmap[abbrinds[i]], fp);
2072 1.1 jtc }
2073 1.25 mlelstv if (thischarcnt != 0)
2074 1.57 christos fwrite(thischars, sizeof thischars[0],
2075 1.25 mlelstv (size_t) thischarcnt, fp);
2076 1.25 mlelstv for (i = thisleapi; i < thisleaplim; ++i) {
2077 1.31 christos zic_t todo;
2078 1.25 mlelstv
2079 1.25 mlelstv if (roll[i]) {
2080 1.25 mlelstv if (timecnt == 0 || trans[i] < ats[0]) {
2081 1.25 mlelstv j = 0;
2082 1.25 mlelstv while (isdsts[j])
2083 1.25 mlelstv if (++j >= typecnt) {
2084 1.25 mlelstv j = 0;
2085 1.25 mlelstv break;
2086 1.25 mlelstv }
2087 1.25 mlelstv } else {
2088 1.25 mlelstv j = 1;
2089 1.25 mlelstv while (j < timecnt &&
2090 1.25 mlelstv trans[i] >= ats[j])
2091 1.25 mlelstv ++j;
2092 1.25 mlelstv j = types[j - 1];
2093 1.25 mlelstv }
2094 1.25 mlelstv todo = tadd(trans[i], -gmtoffs[j]);
2095 1.25 mlelstv } else todo = trans[i];
2096 1.25 mlelstv if (pass == 1)
2097 1.41 christos puttzcode(todo, fp);
2098 1.25 mlelstv else puttzcode64(todo, fp);
2099 1.25 mlelstv puttzcode(corr[i], fp);
2100 1.25 mlelstv }
2101 1.25 mlelstv for (i = 0; i < typecnt; ++i)
2102 1.25 mlelstv if (writetype[i])
2103 1.57 christos putc(ttisstds[i], fp);
2104 1.25 mlelstv for (i = 0; i < typecnt; ++i)
2105 1.25 mlelstv if (writetype[i])
2106 1.57 christos putc(ttisgmts[i], fp);
2107 1.1 jtc }
2108 1.57 christos fprintf(fp, "\n%s\n", string);
2109 1.63 christos close_file(fp, directory, name);
2110 1.45 christos free(ats);
2111 1.1 jtc }
2112 1.1 jtc
2113 1.55 christos static char const *
2114 1.55 christos abbroffset(char *buf, zic_t offset)
2115 1.55 christos {
2116 1.55 christos char sign = '+';
2117 1.55 christos int seconds, minutes;
2118 1.55 christos
2119 1.55 christos if (offset < 0) {
2120 1.55 christos offset = -offset;
2121 1.55 christos sign = '-';
2122 1.55 christos }
2123 1.55 christos
2124 1.55 christos seconds = offset % SECSPERMIN;
2125 1.55 christos offset /= SECSPERMIN;
2126 1.55 christos minutes = offset % MINSPERHOUR;
2127 1.55 christos offset /= MINSPERHOUR;
2128 1.55 christos if (100 <= offset) {
2129 1.70 christos error(_("%%z UT offset magnitude exceeds 99:59:59"));
2130 1.55 christos return "%z";
2131 1.55 christos } else {
2132 1.55 christos char *p = buf;
2133 1.55 christos *p++ = sign;
2134 1.55 christos *p++ = '0' + offset / 10;
2135 1.55 christos *p++ = '0' + offset % 10;
2136 1.55 christos if (minutes | seconds) {
2137 1.55 christos *p++ = '0' + minutes / 10;
2138 1.55 christos *p++ = '0' + minutes % 10;
2139 1.55 christos if (seconds) {
2140 1.55 christos *p++ = '0' + seconds / 10;
2141 1.55 christos *p++ = '0' + seconds % 10;
2142 1.55 christos }
2143 1.55 christos }
2144 1.55 christos *p = '\0';
2145 1.55 christos return buf;
2146 1.55 christos }
2147 1.55 christos }
2148 1.55 christos
2149 1.53 christos static size_t
2150 1.55 christos doabbr(char *abbr, int abbrlen, struct zone const *zp, const char *letters,
2151 1.71 christos bool isdst, zic_t stdoff, bool doquotes)
2152 1.31 christos {
2153 1.31 christos char * cp;
2154 1.31 christos char * slashp;
2155 1.53 christos size_t len;
2156 1.55 christos char const *format = zp->z_format;
2157 1.25 mlelstv
2158 1.25 mlelstv slashp = strchr(format, '/');
2159 1.25 mlelstv if (slashp == NULL) {
2160 1.55 christos char letterbuf[PERCENT_Z_LEN_BOUND + 1];
2161 1.55 christos if (zp->z_format_specifier == 'z')
2162 1.57 christos letters = abbroffset(letterbuf, zp->z_gmtoff + stdoff);
2163 1.55 christos else if (!letters)
2164 1.55 christos letters = "%s";
2165 1.57 christos snprintf(abbr, abbrlen, format, letters);
2166 1.71 christos } else if (isdst) {
2167 1.57 christos strlcpy(abbr, slashp + 1, abbrlen);
2168 1.25 mlelstv } else {
2169 1.57 christos memcpy(abbr, format, slashp - format);
2170 1.25 mlelstv abbr[slashp - format] = '\0';
2171 1.25 mlelstv }
2172 1.53 christos len = strlen(abbr);
2173 1.25 mlelstv if (!doquotes)
2174 1.53 christos return len;
2175 1.47 christos for (cp = abbr; is_alpha(*cp); cp++)
2176 1.47 christos continue;
2177 1.25 mlelstv if (len > 0 && *cp == '\0')
2178 1.53 christos return len;
2179 1.25 mlelstv abbr[len + 2] = '\0';
2180 1.25 mlelstv abbr[len + 1] = '>';
2181 1.53 christos memmove(abbr + 1, abbr, len);
2182 1.25 mlelstv abbr[0] = '<';
2183 1.53 christos return len + 2;
2184 1.25 mlelstv }
2185 1.25 mlelstv
2186 1.25 mlelstv static void
2187 1.41 christos updateminmax(const zic_t x)
2188 1.25 mlelstv {
2189 1.25 mlelstv if (min_year > x)
2190 1.25 mlelstv min_year = x;
2191 1.25 mlelstv if (max_year < x)
2192 1.25 mlelstv max_year = x;
2193 1.25 mlelstv }
2194 1.25 mlelstv
2195 1.53 christos static int
2196 1.38 christos stringoffset(char *result, zic_t offset)
2197 1.31 christos {
2198 1.31 christos int hours;
2199 1.31 christos int minutes;
2200 1.31 christos int seconds;
2201 1.53 christos bool negative = offset < 0;
2202 1.53 christos int len = negative;
2203 1.25 mlelstv
2204 1.53 christos if (negative) {
2205 1.25 mlelstv offset = -offset;
2206 1.53 christos result[0] = '-';
2207 1.25 mlelstv }
2208 1.25 mlelstv seconds = offset % SECSPERMIN;
2209 1.25 mlelstv offset /= SECSPERMIN;
2210 1.25 mlelstv minutes = offset % MINSPERHOUR;
2211 1.25 mlelstv offset /= MINSPERHOUR;
2212 1.25 mlelstv hours = offset;
2213 1.43 christos if (hours >= HOURSPERDAY * DAYSPERWEEK) {
2214 1.25 mlelstv result[0] = '\0';
2215 1.53 christos return 0;
2216 1.25 mlelstv }
2217 1.53 christos len += sprintf(result + len, "%d", hours);
2218 1.25 mlelstv if (minutes != 0 || seconds != 0) {
2219 1.53 christos len += sprintf(result + len, ":%02d", minutes);
2220 1.25 mlelstv if (seconds != 0)
2221 1.53 christos len += sprintf(result + len, ":%02d", seconds);
2222 1.25 mlelstv }
2223 1.53 christos return len;
2224 1.25 mlelstv }
2225 1.25 mlelstv
2226 1.25 mlelstv static int
2227 1.38 christos stringrule(char *result, const struct rule *const rp, const zic_t dstoff,
2228 1.38 christos const zic_t gmtoff)
2229 1.25 mlelstv {
2230 1.43 christos zic_t tod = rp->r_tod;
2231 1.43 christos int compat = 0;
2232 1.25 mlelstv
2233 1.25 mlelstv if (rp->r_dycode == DC_DOM) {
2234 1.31 christos int month, total;
2235 1.25 mlelstv
2236 1.25 mlelstv if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
2237 1.25 mlelstv return -1;
2238 1.25 mlelstv total = 0;
2239 1.25 mlelstv for (month = 0; month < rp->r_month; ++month)
2240 1.25 mlelstv total += len_months[0][month];
2241 1.43 christos /* Omit the "J" in Jan and Feb, as that's shorter. */
2242 1.43 christos if (rp->r_month <= 1)
2243 1.53 christos result += sprintf(result, "%d", total + rp->r_dayofmonth - 1);
2244 1.43 christos else
2245 1.53 christos result += sprintf(result, "J%d", total + rp->r_dayofmonth);
2246 1.25 mlelstv } else {
2247 1.31 christos int week;
2248 1.43 christos int wday = rp->r_wday;
2249 1.43 christos int wdayoff;
2250 1.25 mlelstv
2251 1.25 mlelstv if (rp->r_dycode == DC_DOWGEQ) {
2252 1.43 christos wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
2253 1.43 christos if (wdayoff)
2254 1.43 christos compat = 2013;
2255 1.43 christos wday -= wdayoff;
2256 1.43 christos tod += wdayoff * SECSPERDAY;
2257 1.43 christos week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
2258 1.25 mlelstv } else if (rp->r_dycode == DC_DOWLEQ) {
2259 1.25 mlelstv if (rp->r_dayofmonth == len_months[1][rp->r_month])
2260 1.25 mlelstv week = 5;
2261 1.25 mlelstv else {
2262 1.43 christos wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
2263 1.43 christos if (wdayoff)
2264 1.43 christos compat = 2013;
2265 1.43 christos wday -= wdayoff;
2266 1.43 christos tod += wdayoff * SECSPERDAY;
2267 1.29 christos week = rp->r_dayofmonth / DAYSPERWEEK;
2268 1.25 mlelstv }
2269 1.25 mlelstv } else return -1; /* "cannot happen" */
2270 1.43 christos if (wday < 0)
2271 1.43 christos wday += DAYSPERWEEK;
2272 1.53 christos result += sprintf(result, "M%d.%d.%d",
2273 1.53 christos rp->r_month + 1, week, wday);
2274 1.25 mlelstv }
2275 1.25 mlelstv if (rp->r_todisgmt)
2276 1.25 mlelstv tod += gmtoff;
2277 1.71 christos if (rp->r_todisstd && !rp->r_isdst)
2278 1.25 mlelstv tod += dstoff;
2279 1.25 mlelstv if (tod != 2 * SECSPERMIN * MINSPERHOUR) {
2280 1.53 christos *result++ = '/';
2281 1.53 christos if (! stringoffset(result, tod))
2282 1.25 mlelstv return -1;
2283 1.43 christos if (tod < 0) {
2284 1.43 christos if (compat < 2013)
2285 1.43 christos compat = 2013;
2286 1.43 christos } else if (SECSPERDAY <= tod) {
2287 1.43 christos if (compat < 1994)
2288 1.43 christos compat = 1994;
2289 1.43 christos }
2290 1.25 mlelstv }
2291 1.43 christos return compat;
2292 1.43 christos }
2293 1.43 christos
2294 1.43 christos static int
2295 1.43 christos rule_cmp(struct rule const *a, struct rule const *b)
2296 1.43 christos {
2297 1.43 christos if (!a)
2298 1.43 christos return -!!b;
2299 1.43 christos if (!b)
2300 1.43 christos return 1;
2301 1.43 christos if (a->r_hiyear != b->r_hiyear)
2302 1.43 christos return a->r_hiyear < b->r_hiyear ? -1 : 1;
2303 1.43 christos if (a->r_month - b->r_month != 0)
2304 1.43 christos return a->r_month - b->r_month;
2305 1.43 christos return a->r_dayofmonth - b->r_dayofmonth;
2306 1.25 mlelstv }
2307 1.25 mlelstv
2308 1.43 christos enum { YEAR_BY_YEAR_ZONE = 1 };
2309 1.43 christos
2310 1.43 christos static int
2311 1.31 christos stringzone(char *result, const int resultlen, const struct zone *const zpfirst,
2312 1.31 christos const int zonecount)
2313 1.31 christos {
2314 1.31 christos const struct zone * zp;
2315 1.31 christos struct rule * rp;
2316 1.31 christos struct rule * stdrp;
2317 1.31 christos struct rule * dstrp;
2318 1.65 christos ptrdiff_t i;
2319 1.31 christos const char * abbrvar;
2320 1.43 christos int compat = 0;
2321 1.43 christos int c;
2322 1.53 christos size_t len;
2323 1.53 christos int offsetlen;
2324 1.43 christos struct rule stdr, dstr;
2325 1.25 mlelstv
2326 1.25 mlelstv result[0] = '\0';
2327 1.25 mlelstv zp = zpfirst + zonecount - 1;
2328 1.25 mlelstv stdrp = dstrp = NULL;
2329 1.25 mlelstv for (i = 0; i < zp->z_nrules; ++i) {
2330 1.25 mlelstv rp = &zp->z_rules[i];
2331 1.41 christos if (rp->r_hiwasnum || rp->r_hiyear != ZIC_MAX)
2332 1.25 mlelstv continue;
2333 1.25 mlelstv if (rp->r_yrtype != NULL)
2334 1.25 mlelstv continue;
2335 1.71 christos if (!rp->r_isdst) {
2336 1.25 mlelstv if (stdrp == NULL)
2337 1.25 mlelstv stdrp = rp;
2338 1.43 christos else return -1;
2339 1.25 mlelstv } else {
2340 1.25 mlelstv if (dstrp == NULL)
2341 1.25 mlelstv dstrp = rp;
2342 1.43 christos else return -1;
2343 1.25 mlelstv }
2344 1.25 mlelstv }
2345 1.25 mlelstv if (stdrp == NULL && dstrp == NULL) {
2346 1.25 mlelstv /*
2347 1.25 mlelstv ** There are no rules running through "max".
2348 1.43 christos ** Find the latest std rule in stdabbrrp
2349 1.43 christos ** and latest rule of any type in stdrp.
2350 1.25 mlelstv */
2351 1.43 christos struct rule *stdabbrrp = NULL;
2352 1.25 mlelstv for (i = 0; i < zp->z_nrules; ++i) {
2353 1.25 mlelstv rp = &zp->z_rules[i];
2354 1.71 christos if (!rp->r_isdst && rule_cmp(stdabbrrp, rp) < 0)
2355 1.43 christos stdabbrrp = rp;
2356 1.43 christos if (rule_cmp(stdrp, rp) < 0)
2357 1.43 christos stdrp = rp;
2358 1.25 mlelstv }
2359 1.25 mlelstv /*
2360 1.25 mlelstv ** Horrid special case: if year is 2037,
2361 1.25 mlelstv ** presume this is a zone handled on a year-by-year basis;
2362 1.25 mlelstv ** do not try to apply a rule to the zone.
2363 1.25 mlelstv */
2364 1.25 mlelstv if (stdrp != NULL && stdrp->r_hiyear == 2037)
2365 1.43 christos return YEAR_BY_YEAR_ZONE;
2366 1.43 christos
2367 1.71 christos if (stdrp != NULL && stdrp->r_isdst) {
2368 1.43 christos /* Perpetual DST. */
2369 1.43 christos dstr.r_month = TM_JANUARY;
2370 1.43 christos dstr.r_dycode = DC_DOM;
2371 1.43 christos dstr.r_dayofmonth = 1;
2372 1.43 christos dstr.r_tod = 0;
2373 1.51 christos dstr.r_todisstd = dstr.r_todisgmt = false;
2374 1.71 christos dstr.r_isdst = stdrp->r_isdst;
2375 1.43 christos dstr.r_stdoff = stdrp->r_stdoff;
2376 1.43 christos dstr.r_abbrvar = stdrp->r_abbrvar;
2377 1.43 christos stdr.r_month = TM_DECEMBER;
2378 1.43 christos stdr.r_dycode = DC_DOM;
2379 1.43 christos stdr.r_dayofmonth = 31;
2380 1.43 christos stdr.r_tod = SECSPERDAY + stdrp->r_stdoff;
2381 1.51 christos stdr.r_todisstd = stdr.r_todisgmt = false;
2382 1.71 christos stdr.r_isdst = false;
2383 1.43 christos stdr.r_stdoff = 0;
2384 1.43 christos stdr.r_abbrvar
2385 1.43 christos = (stdabbrrp ? stdabbrrp->r_abbrvar : "");
2386 1.43 christos dstrp = &dstr;
2387 1.43 christos stdrp = &stdr;
2388 1.43 christos }
2389 1.25 mlelstv }
2390 1.71 christos if (stdrp == NULL && (zp->z_nrules != 0 || zp->z_isdst))
2391 1.43 christos return -1;
2392 1.25 mlelstv abbrvar = (stdrp == NULL) ? "" : stdrp->r_abbrvar;
2393 1.71 christos len = doabbr(result, resultlen, zp, abbrvar, false, 0, true);
2394 1.53 christos offsetlen = stringoffset(result + len, -zp->z_gmtoff);
2395 1.53 christos if (! offsetlen) {
2396 1.25 mlelstv result[0] = '\0';
2397 1.43 christos return -1;
2398 1.25 mlelstv }
2399 1.53 christos len += offsetlen;
2400 1.25 mlelstv if (dstrp == NULL)
2401 1.43 christos return compat;
2402 1.71 christos len += doabbr(result + len, resultlen - len, zp, dstrp->r_abbrvar,
2403 1.71 christos dstrp->r_isdst, dstrp->r_stdoff, true);
2404 1.53 christos if (dstrp->r_stdoff != SECSPERMIN * MINSPERHOUR) {
2405 1.53 christos offsetlen = stringoffset(result + len,
2406 1.53 christos -(zp->z_gmtoff + dstrp->r_stdoff));
2407 1.53 christos if (! offsetlen) {
2408 1.51 christos result[0] = '\0';
2409 1.51 christos return -1;
2410 1.25 mlelstv }
2411 1.53 christos len += offsetlen;
2412 1.53 christos }
2413 1.53 christos result[len++] = ',';
2414 1.53 christos c = stringrule(result + len, dstrp, dstrp->r_stdoff, zp->z_gmtoff);
2415 1.43 christos if (c < 0) {
2416 1.25 mlelstv result[0] = '\0';
2417 1.43 christos return -1;
2418 1.25 mlelstv }
2419 1.43 christos if (compat < c)
2420 1.43 christos compat = c;
2421 1.53 christos len += strlen(result + len);
2422 1.53 christos result[len++] = ',';
2423 1.53 christos c = stringrule(result + len, stdrp, dstrp->r_stdoff, zp->z_gmtoff);
2424 1.43 christos if (c < 0) {
2425 1.25 mlelstv result[0] = '\0';
2426 1.43 christos return -1;
2427 1.1 jtc }
2428 1.43 christos if (compat < c)
2429 1.43 christos compat = c;
2430 1.43 christos return compat;
2431 1.1 jtc }
2432 1.1 jtc
2433 1.1 jtc static void
2434 1.65 christos outzone(const struct zone *zpfirst, ptrdiff_t zonecount)
2435 1.31 christos {
2436 1.31 christos const struct zone * zp;
2437 1.31 christos struct rule * rp;
2438 1.65 christos ptrdiff_t i, j;
2439 1.51 christos bool usestart, useuntil;
2440 1.31 christos zic_t starttime, untiltime;
2441 1.38 christos zic_t gmtoff;
2442 1.38 christos zic_t stdoff;
2443 1.41 christos zic_t year;
2444 1.38 christos zic_t startoff;
2445 1.51 christos bool startttisstd;
2446 1.51 christos bool startttisgmt;
2447 1.31 christos int type;
2448 1.31 christos char * startbuf;
2449 1.31 christos char * ab;
2450 1.31 christos char * envvar;
2451 1.36 christos size_t max_abbr_len;
2452 1.36 christos size_t max_envvar_len;
2453 1.51 christos bool prodstic; /* all rules are min to max */
2454 1.43 christos int compat;
2455 1.51 christos bool do_extend;
2456 1.43 christos int version;
2457 1.65 christos ptrdiff_t lastatmax = -1;
2458 1.68 christos zic_t one = 1;
2459 1.68 christos zic_t y2038_boundary = one << 31;
2460 1.68 christos zic_t max_year0;
2461 1.25 mlelstv
2462 1.25 mlelstv max_abbr_len = 2 + max_format_len + max_abbrvar_len;
2463 1.25 mlelstv max_envvar_len = 2 * max_abbr_len + 5 * 9;
2464 1.53 christos startbuf = zic_malloc(max_abbr_len + 1);
2465 1.53 christos ab = zic_malloc(max_abbr_len + 1);
2466 1.53 christos envvar = zic_malloc(max_envvar_len + 1);
2467 1.1 jtc INITIALIZE(untiltime);
2468 1.1 jtc INITIALIZE(starttime);
2469 1.1 jtc /*
2470 1.1 jtc ** Now. . .finally. . .generate some useful data!
2471 1.1 jtc */
2472 1.1 jtc timecnt = 0;
2473 1.1 jtc typecnt = 0;
2474 1.1 jtc charcnt = 0;
2475 1.29 christos prodstic = zonecount == 1;
2476 1.1 jtc /*
2477 1.25 mlelstv ** Thanks to Earl Chew
2478 1.1 jtc ** for noting the need to unconditionally initialize startttisstd.
2479 1.1 jtc */
2480 1.51 christos startttisstd = false;
2481 1.51 christos startttisgmt = false;
2482 1.25 mlelstv min_year = max_year = EPOCH_YEAR;
2483 1.25 mlelstv if (leapseen) {
2484 1.25 mlelstv updateminmax(leapminyear);
2485 1.41 christos updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
2486 1.25 mlelstv }
2487 1.25 mlelstv for (i = 0; i < zonecount; ++i) {
2488 1.25 mlelstv zp = &zpfirst[i];
2489 1.25 mlelstv if (i < zonecount - 1)
2490 1.25 mlelstv updateminmax(zp->z_untilrule.r_loyear);
2491 1.25 mlelstv for (j = 0; j < zp->z_nrules; ++j) {
2492 1.25 mlelstv rp = &zp->z_rules[j];
2493 1.25 mlelstv if (rp->r_lowasnum)
2494 1.25 mlelstv updateminmax(rp->r_loyear);
2495 1.25 mlelstv if (rp->r_hiwasnum)
2496 1.25 mlelstv updateminmax(rp->r_hiyear);
2497 1.41 christos if (rp->r_lowasnum || rp->r_hiwasnum)
2498 1.51 christos prodstic = false;
2499 1.25 mlelstv }
2500 1.25 mlelstv }
2501 1.25 mlelstv /*
2502 1.25 mlelstv ** Generate lots of data if a rule can't cover all future times.
2503 1.25 mlelstv */
2504 1.43 christos compat = stringzone(envvar, max_envvar_len + 1, zpfirst, zonecount);
2505 1.43 christos version = compat < 2013 ? ZIC_VERSION_PRE_2013 : ZIC_VERSION;
2506 1.43 christos do_extend = compat < 0 || compat == YEAR_BY_YEAR_ZONE;
2507 1.44 christos if (noise) {
2508 1.44 christos if (!*envvar)
2509 1.43 christos warning("%s %s",
2510 1.43 christos _("no POSIX environment variable for zone"),
2511 1.43 christos zpfirst->z_name);
2512 1.44 christos else if (compat != 0 && compat != YEAR_BY_YEAR_ZONE) {
2513 1.43 christos /* Circa-COMPAT clients, and earlier clients, might
2514 1.43 christos not work for this zone when given dates before
2515 1.43 christos 1970 or after 2038. */
2516 1.43 christos warning(_("%s: pre-%d clients may mishandle"
2517 1.43 christos " distant timestamps"),
2518 1.43 christos zpfirst->z_name, compat);
2519 1.43 christos }
2520 1.43 christos }
2521 1.43 christos if (do_extend) {
2522 1.43 christos /*
2523 1.43 christos ** Search through a couple of extra years past the obvious
2524 1.43 christos ** 400, to avoid edge cases. For example, suppose a non-POSIX
2525 1.43 christos ** rule applies from 2012 onwards and has transitions in March
2526 1.43 christos ** and September, plus some one-off transitions in November
2527 1.43 christos ** 2013. If zic looked only at the last 400 years, it would
2528 1.43 christos ** set max_year=2413, with the intent that the 400 years 2014
2529 1.43 christos ** through 2413 will be repeated. The last transition listed
2530 1.43 christos ** in the tzfile would be in 2413-09, less than 400 years
2531 1.43 christos ** after the last one-off transition in 2013-11. Two years
2532 1.43 christos ** might be overkill, but with the kind of edge cases
2533 1.43 christos ** available we're not sure that one year would suffice.
2534 1.43 christos */
2535 1.43 christos enum { years_of_observations = YEARSPERREPEAT + 2 };
2536 1.43 christos
2537 1.43 christos if (min_year >= ZIC_MIN + years_of_observations)
2538 1.43 christos min_year -= years_of_observations;
2539 1.41 christos else min_year = ZIC_MIN;
2540 1.43 christos if (max_year <= ZIC_MAX - years_of_observations)
2541 1.43 christos max_year += years_of_observations;
2542 1.41 christos else max_year = ZIC_MAX;
2543 1.29 christos /*
2544 1.29 christos ** Regardless of any of the above,
2545 1.29 christos ** for a "proDSTic" zone which specifies that its rules
2546 1.29 christos ** always have and always will be in effect,
2547 1.29 christos ** we only need one cycle to define the zone.
2548 1.29 christos */
2549 1.29 christos if (prodstic) {
2550 1.29 christos min_year = 1900;
2551 1.43 christos max_year = min_year + years_of_observations;
2552 1.29 christos }
2553 1.25 mlelstv }
2554 1.25 mlelstv /*
2555 1.25 mlelstv ** For the benefit of older systems,
2556 1.68 christos ** generate data from 1900 through 2038.
2557 1.25 mlelstv */
2558 1.25 mlelstv if (min_year > 1900)
2559 1.25 mlelstv min_year = 1900;
2560 1.68 christos max_year0 = max_year;
2561 1.68 christos if (max_year < 2038)
2562 1.68 christos max_year = 2038;
2563 1.1 jtc for (i = 0; i < zonecount; ++i) {
2564 1.19 kleink /*
2565 1.19 kleink ** A guess that may well be corrected later.
2566 1.19 kleink */
2567 1.19 kleink stdoff = 0;
2568 1.1 jtc zp = &zpfirst[i];
2569 1.59 christos usestart = i > 0 && (zp - 1)->z_untiltime > early_time;
2570 1.1 jtc useuntil = i < (zonecount - 1);
2571 1.59 christos if (useuntil && zp->z_untiltime <= early_time)
2572 1.1 jtc continue;
2573 1.1 jtc gmtoff = zp->z_gmtoff;
2574 1.1 jtc eat(zp->z_filename, zp->z_linenum);
2575 1.5 jtc *startbuf = '\0';
2576 1.5 jtc startoff = zp->z_gmtoff;
2577 1.1 jtc if (zp->z_nrules == 0) {
2578 1.1 jtc stdoff = zp->z_stdoff;
2579 1.55 christos doabbr(startbuf, max_abbr_len + 1, zp,
2580 1.71 christos NULL, zp->z_isdst, stdoff, false);
2581 1.1 jtc type = addtype(oadd(zp->z_gmtoff, stdoff),
2582 1.1 jtc startbuf, stdoff != 0, startttisstd,
2583 1.1 jtc startttisgmt);
2584 1.5 jtc if (usestart) {
2585 1.1 jtc addtt(starttime, type);
2586 1.51 christos usestart = false;
2587 1.59 christos } else addtt(early_time, type);
2588 1.1 jtc } else for (year = min_year; year <= max_year; ++year) {
2589 1.1 jtc if (useuntil && year > zp->z_untilrule.r_hiyear)
2590 1.1 jtc break;
2591 1.1 jtc /*
2592 1.1 jtc ** Mark which rules to do in the current year.
2593 1.1 jtc ** For those to do, calculate rpytime(rp, year);
2594 1.1 jtc */
2595 1.1 jtc for (j = 0; j < zp->z_nrules; ++j) {
2596 1.1 jtc rp = &zp->z_rules[j];
2597 1.1 jtc eats(zp->z_filename, zp->z_linenum,
2598 1.1 jtc rp->r_filename, rp->r_linenum);
2599 1.1 jtc rp->r_todo = year >= rp->r_loyear &&
2600 1.1 jtc year <= rp->r_hiyear &&
2601 1.1 jtc yearistype(year, rp->r_yrtype);
2602 1.68 christos if (rp->r_todo) {
2603 1.1 jtc rp->r_temp = rpytime(rp, year);
2604 1.68 christos rp->r_todo
2605 1.68 christos = (rp->r_temp < y2038_boundary
2606 1.68 christos || year <= max_year0);
2607 1.68 christos }
2608 1.1 jtc }
2609 1.1 jtc for ( ; ; ) {
2610 1.65 christos ptrdiff_t k;
2611 1.31 christos zic_t jtime, ktime;
2612 1.38 christos zic_t offset;
2613 1.1 jtc
2614 1.1 jtc INITIALIZE(ktime);
2615 1.1 jtc if (useuntil) {
2616 1.1 jtc /*
2617 1.43 christos ** Turn untiltime into UT
2618 1.1 jtc ** assuming the current gmtoff and
2619 1.1 jtc ** stdoff values.
2620 1.1 jtc */
2621 1.1 jtc untiltime = zp->z_untiltime;
2622 1.1 jtc if (!zp->z_untilrule.r_todisgmt)
2623 1.1 jtc untiltime = tadd(untiltime,
2624 1.1 jtc -gmtoff);
2625 1.1 jtc if (!zp->z_untilrule.r_todisstd)
2626 1.1 jtc untiltime = tadd(untiltime,
2627 1.1 jtc -stdoff);
2628 1.49 christos }
2629 1.1 jtc /*
2630 1.1 jtc ** Find the rule (of those to do, if any)
2631 1.1 jtc ** that takes effect earliest in the year.
2632 1.1 jtc */
2633 1.1 jtc k = -1;
2634 1.1 jtc for (j = 0; j < zp->z_nrules; ++j) {
2635 1.1 jtc rp = &zp->z_rules[j];
2636 1.1 jtc if (!rp->r_todo)
2637 1.1 jtc continue;
2638 1.1 jtc eats(zp->z_filename, zp->z_linenum,
2639 1.1 jtc rp->r_filename, rp->r_linenum);
2640 1.1 jtc offset = rp->r_todisgmt ? 0 : gmtoff;
2641 1.1 jtc if (!rp->r_todisstd)
2642 1.1 jtc offset = oadd(offset, stdoff);
2643 1.1 jtc jtime = rp->r_temp;
2644 1.1 jtc if (jtime == min_time ||
2645 1.1 jtc jtime == max_time)
2646 1.1 jtc continue;
2647 1.1 jtc jtime = tadd(jtime, -offset);
2648 1.1 jtc if (k < 0 || jtime < ktime) {
2649 1.1 jtc k = j;
2650 1.1 jtc ktime = jtime;
2651 1.55 christos } else if (jtime == ktime) {
2652 1.55 christos char const *dup_rules_msg =
2653 1.55 christos _("two rules for same instant");
2654 1.55 christos eats(zp->z_filename, zp->z_linenum,
2655 1.55 christos rp->r_filename, rp->r_linenum);
2656 1.55 christos warning("%s", dup_rules_msg);
2657 1.55 christos rp = &zp->z_rules[k];
2658 1.55 christos eats(zp->z_filename, zp->z_linenum,
2659 1.55 christos rp->r_filename, rp->r_linenum);
2660 1.55 christos error("%s", dup_rules_msg);
2661 1.1 jtc }
2662 1.1 jtc }
2663 1.1 jtc if (k < 0)
2664 1.1 jtc break; /* go on to next year */
2665 1.1 jtc rp = &zp->z_rules[k];
2666 1.51 christos rp->r_todo = false;
2667 1.1 jtc if (useuntil && ktime >= untiltime)
2668 1.1 jtc break;
2669 1.5 jtc stdoff = rp->r_stdoff;
2670 1.5 jtc if (usestart && ktime == starttime)
2671 1.51 christos usestart = false;
2672 1.1 jtc if (usestart) {
2673 1.5 jtc if (ktime < starttime) {
2674 1.5 jtc startoff = oadd(zp->z_gmtoff,
2675 1.5 jtc stdoff);
2676 1.25 mlelstv doabbr(startbuf,
2677 1.25 mlelstv max_abbr_len + 1,
2678 1.55 christos zp,
2679 1.5 jtc rp->r_abbrvar,
2680 1.71 christos rp->r_isdst,
2681 1.56 christos rp->r_stdoff,
2682 1.51 christos false);
2683 1.5 jtc continue;
2684 1.5 jtc }
2685 1.5 jtc if (*startbuf == '\0' &&
2686 1.25 mlelstv startoff == oadd(zp->z_gmtoff,
2687 1.25 mlelstv stdoff)) {
2688 1.25 mlelstv doabbr(startbuf,
2689 1.25 mlelstv max_abbr_len + 1,
2690 1.55 christos zp,
2691 1.25 mlelstv rp->r_abbrvar,
2692 1.71 christos rp->r_isdst,
2693 1.56 christos rp->r_stdoff,
2694 1.51 christos false);
2695 1.1 jtc }
2696 1.1 jtc }
2697 1.1 jtc eats(zp->z_filename, zp->z_linenum,
2698 1.1 jtc rp->r_filename, rp->r_linenum);
2699 1.55 christos doabbr(ab, max_abbr_len + 1, zp, rp->r_abbrvar,
2700 1.71 christos rp->r_isdst, rp->r_stdoff, false);
2701 1.1 jtc offset = oadd(zp->z_gmtoff, rp->r_stdoff);
2702 1.71 christos type = addtype(offset, ab, rp->r_isdst,
2703 1.1 jtc rp->r_todisstd, rp->r_todisgmt);
2704 1.63 christos if (rp->r_hiyear == ZIC_MAX
2705 1.63 christos && ! (0 <= lastatmax
2706 1.63 christos && ktime < attypes[lastatmax].at))
2707 1.63 christos lastatmax = timecnt;
2708 1.1 jtc addtt(ktime, type);
2709 1.1 jtc }
2710 1.1 jtc }
2711 1.5 jtc if (usestart) {
2712 1.5 jtc if (*startbuf == '\0' &&
2713 1.5 jtc zp->z_format != NULL &&
2714 1.5 jtc strchr(zp->z_format, '%') == NULL &&
2715 1.5 jtc strchr(zp->z_format, '/') == NULL)
2716 1.57 christos strncpy(startbuf, zp->z_format,
2717 1.25 mlelstv max_abbr_len + 1 - 1);
2718 1.5 jtc eat(zp->z_filename, zp->z_linenum);
2719 1.5 jtc if (*startbuf == '\0')
2720 1.7 jtc error(_("can't determine time zone abbreviation to use just after until time"));
2721 1.5 jtc else addtt(starttime,
2722 1.5 jtc addtype(startoff, startbuf,
2723 1.5 jtc startoff != zp->z_gmtoff,
2724 1.5 jtc startttisstd,
2725 1.5 jtc startttisgmt));
2726 1.5 jtc }
2727 1.1 jtc /*
2728 1.1 jtc ** Now we may get to set starttime for the next zone line.
2729 1.1 jtc */
2730 1.1 jtc if (useuntil) {
2731 1.1 jtc startttisstd = zp->z_untilrule.r_todisstd;
2732 1.1 jtc startttisgmt = zp->z_untilrule.r_todisgmt;
2733 1.5 jtc starttime = zp->z_untiltime;
2734 1.1 jtc if (!startttisstd)
2735 1.1 jtc starttime = tadd(starttime, -stdoff);
2736 1.5 jtc if (!startttisgmt)
2737 1.5 jtc starttime = tadd(starttime, -gmtoff);
2738 1.1 jtc }
2739 1.1 jtc }
2740 1.63 christos if (0 <= lastatmax)
2741 1.63 christos attypes[lastatmax].dontmerge = true;
2742 1.43 christos if (do_extend) {
2743 1.43 christos /*
2744 1.43 christos ** If we're extending the explicitly listed observations
2745 1.43 christos ** for 400 years because we can't fill the POSIX-TZ field,
2746 1.43 christos ** check whether we actually ended up explicitly listing
2747 1.43 christos ** observations through that period. If there aren't any
2748 1.43 christos ** near the end of the 400-year period, add a redundant
2749 1.43 christos ** one at the end of the final year, to make it clear
2750 1.43 christos ** that we are claiming to have definite knowledge of
2751 1.43 christos ** the lack of transitions up to that point.
2752 1.43 christos */
2753 1.43 christos struct rule xr;
2754 1.43 christos struct attype *lastat;
2755 1.58 dholland memset(&xr, 0, sizeof(xr));
2756 1.43 christos xr.r_month = TM_JANUARY;
2757 1.43 christos xr.r_dycode = DC_DOM;
2758 1.43 christos xr.r_dayofmonth = 1;
2759 1.43 christos xr.r_tod = 0;
2760 1.43 christos for (lastat = &attypes[0], i = 1; i < timecnt; i++)
2761 1.43 christos if (attypes[i].at > lastat->at)
2762 1.43 christos lastat = &attypes[i];
2763 1.43 christos if (lastat->at < rpytime(&xr, max_year - 1)) {
2764 1.43 christos addtt(rpytime(&xr, max_year + 1), typecnt-1);
2765 1.63 christos attypes[timecnt - 1].dontmerge = true;
2766 1.43 christos }
2767 1.43 christos }
2768 1.43 christos writezone(zpfirst->z_name, envvar, version);
2769 1.31 christos free(startbuf);
2770 1.31 christos free(ab);
2771 1.31 christos free(envvar);
2772 1.1 jtc }
2773 1.1 jtc
2774 1.1 jtc static void
2775 1.55 christos addtt(zic_t starttime, int type)
2776 1.1 jtc {
2777 1.59 christos if (starttime <= early_time
2778 1.59 christos || (timecnt == 1 && attypes[0].at < early_time)) {
2779 1.7 jtc gmtoffs[0] = gmtoffs[type];
2780 1.7 jtc isdsts[0] = isdsts[type];
2781 1.7 jtc ttisstds[0] = ttisstds[type];
2782 1.7 jtc ttisgmts[0] = ttisgmts[type];
2783 1.7 jtc if (abbrinds[type] != 0)
2784 1.51 christos strcpy(chars, &chars[abbrinds[type]]);
2785 1.7 jtc abbrinds[0] = 0;
2786 1.7 jtc charcnt = strlen(chars) + 1;
2787 1.7 jtc typecnt = 1;
2788 1.7 jtc timecnt = 0;
2789 1.7 jtc type = 0;
2790 1.7 jtc }
2791 1.45 christos attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
2792 1.5 jtc attypes[timecnt].at = starttime;
2793 1.63 christos attypes[timecnt].dontmerge = false;
2794 1.5 jtc attypes[timecnt].type = type;
2795 1.1 jtc ++timecnt;
2796 1.1 jtc }
2797 1.1 jtc
2798 1.1 jtc static int
2799 1.57 christos addtype(zic_t gmtoff, char const *abbr, bool isdst, bool ttisstd, bool ttisgmt)
2800 1.1 jtc {
2801 1.31 christos int i, j;
2802 1.1 jtc
2803 1.1 jtc /*
2804 1.1 jtc ** See if there's already an entry for this zone type.
2805 1.1 jtc ** If so, just return its index.
2806 1.1 jtc */
2807 1.1 jtc for (i = 0; i < typecnt; ++i) {
2808 1.1 jtc if (gmtoff == gmtoffs[i] && isdst == isdsts[i] &&
2809 1.1 jtc strcmp(abbr, &chars[abbrinds[i]]) == 0 &&
2810 1.1 jtc ttisstd == ttisstds[i] &&
2811 1.1 jtc ttisgmt == ttisgmts[i])
2812 1.1 jtc return i;
2813 1.1 jtc }
2814 1.1 jtc /*
2815 1.1 jtc ** There isn't one; add a new one, unless there are already too
2816 1.1 jtc ** many.
2817 1.1 jtc */
2818 1.1 jtc if (typecnt >= TZ_MAX_TYPES) {
2819 1.5 jtc error(_("too many local time types"));
2820 1.25 mlelstv exit(EXIT_FAILURE);
2821 1.25 mlelstv }
2822 1.25 mlelstv if (! (-1L - 2147483647L <= gmtoff && gmtoff <= 2147483647L)) {
2823 1.43 christos error(_("UT offset out of range"));
2824 1.25 mlelstv exit(EXIT_FAILURE);
2825 1.1 jtc }
2826 1.1 jtc gmtoffs[i] = gmtoff;
2827 1.1 jtc isdsts[i] = isdst;
2828 1.1 jtc ttisstds[i] = ttisstd;
2829 1.1 jtc ttisgmts[i] = ttisgmt;
2830 1.1 jtc
2831 1.1 jtc for (j = 0; j < charcnt; ++j)
2832 1.1 jtc if (strcmp(&chars[j], abbr) == 0)
2833 1.1 jtc break;
2834 1.1 jtc if (j == charcnt)
2835 1.1 jtc newabbr(abbr);
2836 1.1 jtc abbrinds[i] = j;
2837 1.1 jtc ++typecnt;
2838 1.1 jtc return i;
2839 1.1 jtc }
2840 1.1 jtc
2841 1.1 jtc static void
2842 1.51 christos leapadd(zic_t t, bool positive, int rolling, int count)
2843 1.1 jtc {
2844 1.31 christos int i, j;
2845 1.1 jtc
2846 1.1 jtc if (leapcnt + (positive ? count : 1) > TZ_MAX_LEAPS) {
2847 1.5 jtc error(_("too many leap seconds"));
2848 1.25 mlelstv exit(EXIT_FAILURE);
2849 1.1 jtc }
2850 1.1 jtc for (i = 0; i < leapcnt; ++i)
2851 1.69 christos if (t <= trans[i])
2852 1.1 jtc break;
2853 1.1 jtc do {
2854 1.1 jtc for (j = leapcnt; j > i; --j) {
2855 1.1 jtc trans[j] = trans[j - 1];
2856 1.1 jtc corr[j] = corr[j - 1];
2857 1.1 jtc roll[j] = roll[j - 1];
2858 1.1 jtc }
2859 1.1 jtc trans[i] = t;
2860 1.41 christos corr[i] = positive ? 1 : -count;
2861 1.1 jtc roll[i] = rolling;
2862 1.1 jtc ++leapcnt;
2863 1.1 jtc } while (positive && --count != 0);
2864 1.1 jtc }
2865 1.1 jtc
2866 1.1 jtc static void
2867 1.25 mlelstv adjleap(void)
2868 1.1 jtc {
2869 1.31 christos int i;
2870 1.38 christos zic_t last = 0;
2871 1.69 christos zic_t prevtrans = 0;
2872 1.1 jtc
2873 1.1 jtc /*
2874 1.1 jtc ** propagate leap seconds forward
2875 1.1 jtc */
2876 1.1 jtc for (i = 0; i < leapcnt; ++i) {
2877 1.69 christos if (trans[i] - prevtrans < 28 * SECSPERDAY) {
2878 1.69 christos error(_("Leap seconds too close together"));
2879 1.69 christos exit(EXIT_FAILURE);
2880 1.69 christos }
2881 1.69 christos prevtrans = trans[i];
2882 1.1 jtc trans[i] = tadd(trans[i], last);
2883 1.1 jtc last = corr[i] += last;
2884 1.1 jtc }
2885 1.1 jtc }
2886 1.1 jtc
2887 1.65 christos static char *
2888 1.65 christos shellquote(char *b, char const *s)
2889 1.65 christos {
2890 1.65 christos *b++ = '\'';
2891 1.65 christos while (*s) {
2892 1.65 christos if (*s == '\'')
2893 1.65 christos *b++ = '\'', *b++ = '\\', *b++ = '\'';
2894 1.65 christos *b++ = *s++;
2895 1.65 christos }
2896 1.65 christos *b++ = '\'';
2897 1.65 christos return b;
2898 1.65 christos }
2899 1.65 christos
2900 1.51 christos static bool
2901 1.65 christos yearistype(zic_t year, const char *type)
2902 1.1 jtc {
2903 1.65 christos char *buf;
2904 1.65 christos char *b;
2905 1.65 christos int result;
2906 1.65 christos size_t len;
2907 1.1 jtc
2908 1.1 jtc if (type == NULL || *type == '\0')
2909 1.51 christos return true;
2910 1.65 christos buf = zic_malloc(len = 1 + 4 * strlen(yitcommand) + 2
2911 1.65 christos + INT_STRLEN_MAXIMUM(zic_t) + 2 + 4 * strlen(type) + 2);
2912 1.65 christos b = shellquote(buf, yitcommand);
2913 1.65 christos *b++ = ' ';
2914 1.65 christos b += snprintf(b, len - (b - buf), "%"PRIdZIC, year);
2915 1.65 christos *b++ = ' ';
2916 1.65 christos b = shellquote(b, type);
2917 1.65 christos *b = '\0';
2918 1.1 jtc result = system(buf);
2919 1.65 christos if (WIFEXITED(result)) {
2920 1.65 christos int status = WEXITSTATUS(result);
2921 1.65 christos if (status <= 1) {
2922 1.65 christos free(buf);
2923 1.65 christos return status == 0;
2924 1.65 christos }
2925 1.16 kleink }
2926 1.5 jtc error(_("Wild result from command execution"));
2927 1.57 christos fprintf(stderr, _("%s: command was '%s', result was %d\n"),
2928 1.1 jtc progname, buf, result);
2929 1.65 christos exit(EXIT_FAILURE);
2930 1.1 jtc }
2931 1.1 jtc
2932 1.47 christos /* Is A a space character in the C locale? */
2933 1.51 christos static bool
2934 1.47 christos is_space(char a)
2935 1.1 jtc {
2936 1.47 christos switch (a) {
2937 1.47 christos default:
2938 1.51 christos return false;
2939 1.47 christos case ' ': case '\f': case '\n': case '\r': case '\t': case '\v':
2940 1.51 christos return true;
2941 1.47 christos }
2942 1.47 christos }
2943 1.47 christos
2944 1.47 christos /* Is A an alphabetic character in the C locale? */
2945 1.51 christos static bool
2946 1.47 christos is_alpha(char a)
2947 1.47 christos {
2948 1.47 christos switch (a) {
2949 1.47 christos default:
2950 1.47 christos return 0;
2951 1.47 christos case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
2952 1.47 christos case 'H': case 'I': case 'J': case 'K': case 'L': case 'M': case 'N':
2953 1.47 christos case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
2954 1.47 christos case 'V': case 'W': case 'X': case 'Y': case 'Z':
2955 1.47 christos case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
2956 1.47 christos case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
2957 1.47 christos case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
2958 1.47 christos case 'v': case 'w': case 'x': case 'y': case 'z':
2959 1.51 christos return true;
2960 1.47 christos }
2961 1.47 christos }
2962 1.47 christos
2963 1.47 christos /* If A is an uppercase character in the C locale, return its lowercase
2964 1.47 christos counterpart. Otherwise, return A. */
2965 1.47 christos static char
2966 1.47 christos lowerit(char a)
2967 1.47 christos {
2968 1.47 christos switch (a) {
2969 1.47 christos default: return a;
2970 1.47 christos case 'A': return 'a'; case 'B': return 'b'; case 'C': return 'c';
2971 1.47 christos case 'D': return 'd'; case 'E': return 'e'; case 'F': return 'f';
2972 1.47 christos case 'G': return 'g'; case 'H': return 'h'; case 'I': return 'i';
2973 1.47 christos case 'J': return 'j'; case 'K': return 'k'; case 'L': return 'l';
2974 1.47 christos case 'M': return 'm'; case 'N': return 'n'; case 'O': return 'o';
2975 1.47 christos case 'P': return 'p'; case 'Q': return 'q'; case 'R': return 'r';
2976 1.47 christos case 'S': return 's'; case 'T': return 't'; case 'U': return 'u';
2977 1.47 christos case 'V': return 'v'; case 'W': return 'w'; case 'X': return 'x';
2978 1.47 christos case 'Y': return 'y'; case 'Z': return 'z';
2979 1.47 christos }
2980 1.1 jtc }
2981 1.1 jtc
2982 1.31 christos /* case-insensitive equality */
2983 1.69 christos static bool
2984 1.31 christos ciequal(const char *ap, const char *bp)
2985 1.1 jtc {
2986 1.1 jtc while (lowerit(*ap) == lowerit(*bp++))
2987 1.1 jtc if (*ap++ == '\0')
2988 1.51 christos return true;
2989 1.51 christos return false;
2990 1.1 jtc }
2991 1.1 jtc
2992 1.69 christos static bool
2993 1.31 christos itsabbr(const char *abbr, const char *word)
2994 1.1 jtc {
2995 1.1 jtc if (lowerit(*abbr) != lowerit(*word))
2996 1.51 christos return false;
2997 1.1 jtc ++word;
2998 1.1 jtc while (*++abbr != '\0')
2999 1.3 jtc do {
3000 1.3 jtc if (*word == '\0')
3001 1.51 christos return false;
3002 1.3 jtc } while (lowerit(*word++) != lowerit(*abbr));
3003 1.51 christos return true;
3004 1.1 jtc }
3005 1.1 jtc
3006 1.69 christos /* Return true if ABBR is an initial prefix of WORD, ignoring ASCII case. */
3007 1.69 christos
3008 1.69 christos static bool
3009 1.69 christos ciprefix(char const *abbr, char const *word)
3010 1.69 christos {
3011 1.69 christos do
3012 1.69 christos if (!*abbr)
3013 1.69 christos return true;
3014 1.69 christos while (lowerit(*abbr++) == lowerit(*word++));
3015 1.69 christos
3016 1.69 christos return false;
3017 1.69 christos }
3018 1.69 christos
3019 1.69 christos static const struct lookup *
3020 1.55 christos byword(const char *word, const struct lookup *table)
3021 1.1 jtc {
3022 1.31 christos const struct lookup * foundlp;
3023 1.31 christos const struct lookup * lp;
3024 1.1 jtc
3025 1.1 jtc if (word == NULL || table == NULL)
3026 1.1 jtc return NULL;
3027 1.69 christos
3028 1.69 christos /* If TABLE is LASTS and the word starts with "last" followed
3029 1.69 christos by a non-'-', skip the "last" and look in WDAY_NAMES instead.
3030 1.69 christos Warn about any usage of the undocumented prefix "last-". */
3031 1.69 christos if (table == lasts && ciprefix("last", word) && word[4]) {
3032 1.69 christos if (word[4] == '-')
3033 1.69 christos warning(_("\"%s\" is undocumented; use \"last%s\" instead"),
3034 1.69 christos word, word + 5);
3035 1.69 christos else {
3036 1.69 christos word += 4;
3037 1.69 christos table = wday_names;
3038 1.69 christos }
3039 1.69 christos }
3040 1.69 christos
3041 1.1 jtc /*
3042 1.1 jtc ** Look for exact match.
3043 1.1 jtc */
3044 1.1 jtc for (lp = table; lp->l_word != NULL; ++lp)
3045 1.1 jtc if (ciequal(word, lp->l_word))
3046 1.1 jtc return lp;
3047 1.1 jtc /*
3048 1.1 jtc ** Look for inexact match.
3049 1.1 jtc */
3050 1.1 jtc foundlp = NULL;
3051 1.1 jtc for (lp = table; lp->l_word != NULL; ++lp)
3052 1.69 christos if (ciprefix(word, lp->l_word)) {
3053 1.1 jtc if (foundlp == NULL)
3054 1.1 jtc foundlp = lp;
3055 1.1 jtc else return NULL; /* multiple inexact matches */
3056 1.11 jtc }
3057 1.69 christos
3058 1.69 christos /* Warn about any backward-compatibility issue with pre-2017c zic. */
3059 1.69 christos if (foundlp) {
3060 1.69 christos bool pre_2017c_match = false;
3061 1.69 christos for (lp = table; lp->l_word; lp++)
3062 1.69 christos if (itsabbr(word, lp->l_word)) {
3063 1.69 christos if (pre_2017c_match) {
3064 1.69 christos warning(_("\"%s\" is ambiguous in pre-2017c zic"), word);
3065 1.69 christos break;
3066 1.69 christos }
3067 1.69 christos pre_2017c_match = true;
3068 1.69 christos }
3069 1.69 christos }
3070 1.69 christos
3071 1.1 jtc return foundlp;
3072 1.1 jtc }
3073 1.1 jtc
3074 1.1 jtc static char **
3075 1.31 christos getfields(char *cp)
3076 1.1 jtc {
3077 1.31 christos char * dp;
3078 1.31 christos char ** array;
3079 1.31 christos int nsubs;
3080 1.1 jtc
3081 1.1 jtc if (cp == NULL)
3082 1.1 jtc return NULL;
3083 1.53 christos array = zic_malloc(size_product(strlen(cp) + 1, sizeof *array));
3084 1.1 jtc nsubs = 0;
3085 1.1 jtc for ( ; ; ) {
3086 1.47 christos while (is_space(*cp))
3087 1.25 mlelstv ++cp;
3088 1.1 jtc if (*cp == '\0' || *cp == '#')
3089 1.1 jtc break;
3090 1.1 jtc array[nsubs++] = dp = cp;
3091 1.1 jtc do {
3092 1.1 jtc if ((*dp = *cp++) != '"')
3093 1.1 jtc ++dp;
3094 1.1 jtc else while ((*dp = *cp++) != '"')
3095 1.1 jtc if (*dp != '\0')
3096 1.1 jtc ++dp;
3097 1.25 mlelstv else {
3098 1.65 christos error(_("Odd number of quotation marks"));
3099 1.65 christos exit(EXIT_FAILURE);
3100 1.25 mlelstv }
3101 1.47 christos } while (*cp && *cp != '#' && !is_space(*cp));
3102 1.47 christos if (is_space(*cp))
3103 1.1 jtc ++cp;
3104 1.1 jtc *dp = '\0';
3105 1.1 jtc }
3106 1.1 jtc array[nsubs] = NULL;
3107 1.1 jtc return array;
3108 1.1 jtc }
3109 1.1 jtc
3110 1.53 christos static _Noreturn void
3111 1.53 christos time_overflow(void)
3112 1.53 christos {
3113 1.53 christos error(_("time overflow"));
3114 1.53 christos exit(EXIT_FAILURE);
3115 1.53 christos }
3116 1.53 christos
3117 1.41 christos static ATTRIBUTE_PURE zic_t
3118 1.55 christos oadd(zic_t t1, zic_t t2)
3119 1.1 jtc {
3120 1.53 christos if (t1 < 0 ? t2 < ZIC_MIN - t1 : ZIC_MAX - t1 < t2)
3121 1.53 christos time_overflow();
3122 1.31 christos return t1 + t2;
3123 1.1 jtc }
3124 1.1 jtc
3125 1.43 christos static ATTRIBUTE_PURE zic_t
3126 1.55 christos tadd(zic_t t1, zic_t t2)
3127 1.1 jtc {
3128 1.53 christos if (t1 < 0) {
3129 1.53 christos if (t2 < min_time - t1) {
3130 1.53 christos if (t1 != min_time)
3131 1.53 christos time_overflow();
3132 1.53 christos return min_time;
3133 1.53 christos }
3134 1.53 christos } else {
3135 1.53 christos if (max_time - t1 < t2) {
3136 1.53 christos if (t1 != max_time)
3137 1.53 christos time_overflow();
3138 1.53 christos return max_time;
3139 1.53 christos }
3140 1.1 jtc }
3141 1.31 christos return t1 + t2;
3142 1.1 jtc }
3143 1.1 jtc
3144 1.1 jtc /*
3145 1.47 christos ** Given a rule, and a year, compute the date (in seconds since January 1,
3146 1.47 christos ** 1970, 00:00 LOCAL time) in that year that the rule refers to.
3147 1.1 jtc */
3148 1.1 jtc
3149 1.25 mlelstv static zic_t
3150 1.55 christos rpytime(const struct rule *rp, zic_t wantedy)
3151 1.31 christos {
3152 1.41 christos int m, i;
3153 1.38 christos zic_t dayoff; /* with a nod to Margaret O. */
3154 1.41 christos zic_t t, y;
3155 1.1 jtc
3156 1.41 christos if (wantedy == ZIC_MIN)
3157 1.1 jtc return min_time;
3158 1.41 christos if (wantedy == ZIC_MAX)
3159 1.1 jtc return max_time;
3160 1.1 jtc dayoff = 0;
3161 1.1 jtc m = TM_JANUARY;
3162 1.1 jtc y = EPOCH_YEAR;
3163 1.1 jtc while (wantedy != y) {
3164 1.1 jtc if (wantedy > y) {
3165 1.1 jtc i = len_years[isleap(y)];
3166 1.1 jtc ++y;
3167 1.1 jtc } else {
3168 1.1 jtc --y;
3169 1.1 jtc i = -len_years[isleap(y)];
3170 1.1 jtc }
3171 1.41 christos dayoff = oadd(dayoff, i);
3172 1.1 jtc }
3173 1.1 jtc while (m != rp->r_month) {
3174 1.1 jtc i = len_months[isleap(y)][m];
3175 1.41 christos dayoff = oadd(dayoff, i);
3176 1.1 jtc ++m;
3177 1.1 jtc }
3178 1.1 jtc i = rp->r_dayofmonth;
3179 1.1 jtc if (m == TM_FEBRUARY && i == 29 && !isleap(y)) {
3180 1.1 jtc if (rp->r_dycode == DC_DOWLEQ)
3181 1.1 jtc --i;
3182 1.1 jtc else {
3183 1.5 jtc error(_("use of 2/29 in non leap-year"));
3184 1.25 mlelstv exit(EXIT_FAILURE);
3185 1.1 jtc }
3186 1.1 jtc }
3187 1.1 jtc --i;
3188 1.41 christos dayoff = oadd(dayoff, i);
3189 1.1 jtc if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ) {
3190 1.38 christos zic_t wday;
3191 1.1 jtc
3192 1.38 christos #define LDAYSPERWEEK ((zic_t) DAYSPERWEEK)
3193 1.41 christos wday = EPOCH_WDAY;
3194 1.1 jtc /*
3195 1.1 jtc ** Don't trust mod of negative numbers.
3196 1.1 jtc */
3197 1.1 jtc if (dayoff >= 0)
3198 1.1 jtc wday = (wday + dayoff) % LDAYSPERWEEK;
3199 1.1 jtc else {
3200 1.1 jtc wday -= ((-dayoff) % LDAYSPERWEEK);
3201 1.1 jtc if (wday < 0)
3202 1.1 jtc wday += LDAYSPERWEEK;
3203 1.1 jtc }
3204 1.41 christos while (wday != rp->r_wday)
3205 1.1 jtc if (rp->r_dycode == DC_DOWGEQ) {
3206 1.38 christos dayoff = oadd(dayoff, (zic_t) 1);
3207 1.1 jtc if (++wday >= LDAYSPERWEEK)
3208 1.1 jtc wday = 0;
3209 1.1 jtc ++i;
3210 1.1 jtc } else {
3211 1.38 christos dayoff = oadd(dayoff, (zic_t) -1);
3212 1.1 jtc if (--wday < 0)
3213 1.1 jtc wday = LDAYSPERWEEK - 1;
3214 1.1 jtc --i;
3215 1.1 jtc }
3216 1.1 jtc if (i < 0 || i >= len_months[isleap(y)][m]) {
3217 1.23 kleink if (noise)
3218 1.47 christos warning(_("rule goes past start/end of month; \
3219 1.25 mlelstv will not work with pre-2004 versions of zic"));
3220 1.1 jtc }
3221 1.1 jtc }
3222 1.34 martin if (dayoff < min_time / SECSPERDAY)
3223 1.20 kleink return min_time;
3224 1.34 martin if (dayoff > max_time / SECSPERDAY)
3225 1.20 kleink return max_time;
3226 1.25 mlelstv t = (zic_t) dayoff * SECSPERDAY;
3227 1.1 jtc return tadd(t, rp->r_tod);
3228 1.1 jtc }
3229 1.1 jtc
3230 1.1 jtc static void
3231 1.55 christos newabbr(const char *string)
3232 1.1 jtc {
3233 1.31 christos int i;
3234 1.1 jtc
3235 1.25 mlelstv if (strcmp(string, GRANDPARENTED) != 0) {
3236 1.31 christos const char * cp;
3237 1.31 christos const char * mp;
3238 1.25 mlelstv
3239 1.25 mlelstv cp = string;
3240 1.31 christos mp = NULL;
3241 1.55 christos while (is_alpha(*cp) || ('0' <= *cp && *cp <= '9')
3242 1.55 christos || *cp == '-' || *cp == '+')
3243 1.25 mlelstv ++cp;
3244 1.35 christos if (noise && cp - string < 3)
3245 1.55 christos mp = _("time zone abbreviation has fewer than 3 characters");
3246 1.25 mlelstv if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
3247 1.55 christos mp = _("time zone abbreviation has too many characters");
3248 1.25 mlelstv if (*cp != '\0')
3249 1.31 christos mp = _("time zone abbreviation differs from POSIX standard");
3250 1.43 christos if (mp != NULL)
3251 1.43 christos warning("%s (%s)", mp, string);
3252 1.25 mlelstv }
3253 1.1 jtc i = strlen(string) + 1;
3254 1.1 jtc if (charcnt + i > TZ_MAX_CHARS) {
3255 1.40 christos error(_("too many, or too long, time zone abbreviations"));
3256 1.25 mlelstv exit(EXIT_FAILURE);
3257 1.1 jtc }
3258 1.57 christos strncpy(&chars[charcnt], string, sizeof(chars) - charcnt - 1);
3259 1.41 christos charcnt += i;
3260 1.1 jtc }
3261 1.63 christos
3262 1.63 christos /* Ensure that the directories of ARGNAME exist, by making any missing
3263 1.63 christos ones. If ANCESTORS, do this only for ARGNAME's ancestors; otherwise,
3264 1.68 christos do it for ARGNAME too. Exit with failure if there is trouble.
3265 1.68 christos Do not consider an existing non-directory to be trouble. */
3266 1.63 christos static void
3267 1.63 christos mkdirs(char const *argname, bool ancestors)
3268 1.1 jtc {
3269 1.31 christos char * name;
3270 1.31 christos char * cp;
3271 1.1 jtc
3272 1.1 jtc cp = name = ecpyalloc(argname);
3273 1.63 christos
3274 1.69 christos /* On MS-Windows systems, do not worry about drive letters or
3275 1.69 christos backslashes, as this should suffice in practice. Time zone
3276 1.69 christos names do not use drive letters and backslashes. If the -d
3277 1.69 christos option of zic does not name an already-existing directory,
3278 1.69 christos it can use slashes to separate the already-existing
3279 1.69 christos ancestor prefix from the to-be-created subdirectories. */
3280 1.69 christos
3281 1.63 christos /* Do not mkdir a root directory, as it must exist. */
3282 1.63 christos while (*cp == '/')
3283 1.63 christos cp++;
3284 1.63 christos
3285 1.63 christos while (cp && ((cp = strchr(cp, '/')) || !ancestors)) {
3286 1.63 christos if (cp)
3287 1.63 christos *cp = '\0';
3288 1.47 christos /*
3289 1.47 christos ** Try to create it. It's OK if creation fails because
3290 1.47 christos ** the directory already exists, perhaps because some
3291 1.63 christos ** other process just created it. For simplicity do
3292 1.63 christos ** not check first whether it already exists, as that
3293 1.63 christos ** is checked anyway if the mkdir fails.
3294 1.47 christos */
3295 1.47 christos if (mkdir(name, MKDIR_UMASK) != 0) {
3296 1.68 christos /* For speed, skip itsdir if errno == EEXIST. Since
3297 1.68 christos mkdirs is called only after open fails with ENOENT
3298 1.68 christos on a subfile, EEXIST implies itsdir here. */
3299 1.47 christos int err = errno;
3300 1.68 christos if (err != EEXIST && !itsdir(name)) {
3301 1.63 christos error(_("%s: Can't create directory %s: %s"),
3302 1.63 christos progname, name, strerror(err));
3303 1.63 christos exit(EXIT_FAILURE);
3304 1.63 christos }
3305 1.1 jtc }
3306 1.63 christos if (cp)
3307 1.63 christos *cp++ = '/';
3308 1.1 jtc }
3309 1.31 christos free(name);
3310 1.1 jtc }
3311