Branch data Line data Source code
1 : : /* Compile .zi time zone data into TZif binary files. */
2 : :
3 : : /*
4 : : * This file is in the public domain, so clarified as of
5 : : * 2006-07-17 by Arthur David Olson.
6 : : *
7 : : * IDENTIFICATION
8 : : * src/timezone/zic.c
9 : : */
10 : :
11 : : #include "postgres_fe.h"
12 : :
13 : : #include <fcntl.h>
14 : : #include <grp.h>
15 : : #include <pwd.h>
16 : : #include <signal.h>
17 : : #include <sys/stat.h>
18 : : #include <time.h>
19 : :
20 : : #include "pg_getopt.h"
21 : :
22 : : #include "private.h"
23 : : #include "tzfile.h"
24 : :
25 : : #ifndef O_BINARY
26 : : #define O_BINARY 0 /* MS-Windows */
27 : : #endif
28 : :
29 : : typedef int_fast64_t zic_t;
30 : : static zic_t const
31 : : ZIC_MIN = INT_FAST64_MIN,
32 : : ZIC_MAX = INT_FAST64_MAX,
33 : : ZIC32_MIN = -1 - (zic_t) TWO_31_MINUS_1,
34 : : ZIC32_MAX = TWO_31_MINUS_1;
35 : : #define SCNdZIC SCNdFAST64
36 : :
37 : : #ifndef ZIC_MAX_ABBR_LEN_WO_WARN
38 : : #define ZIC_MAX_ABBR_LEN_WO_WARN 6
39 : : #endif /* !defined ZIC_MAX_ABBR_LEN_WO_WARN */
40 : :
41 : : /* Minimum and maximum years, assuming signed 32-bit pg_time_t. */
42 : : enum
43 : : {
44 : : YEAR_32BIT_MIN = 1901, YEAR_32BIT_MAX = 2038};
45 : :
46 : : /* An upper bound on how much a format might grow due to concatenation. */
47 : : enum
48 : : {
49 : : FORMAT_LEN_GROWTH_BOUND = 5};
50 : :
51 : : /* All file permission bits. */
52 : : #define ALL_PERMS (S_IRWXU | S_IRWXG | S_IRWXO)
53 : :
54 : : /* Troublesome file permission bits. */
55 : : #define TROUBLE_PERMS (S_IWGRP | S_IWOTH)
56 : :
57 : : /*
58 : : * File permission bits for making directories.
59 : : * The umask modifies these bits.
60 : : */
61 : : #define MKDIR_PERMS (ALL_PERMS & ~TROUBLE_PERMS)
62 : :
63 : : /*
64 : : * File permission bits for making regular files.
65 : : * The umask modifies these bits.
66 : : */
67 : : #define CREAT_PERMS (MKDIR_PERMS & ~(S_IXUSR | S_IXGRP | S_IXOTH))
68 : : static mode_t creat_perms = CREAT_PERMS;
69 : :
70 : : static gid_t const no_gid = -1;
71 : : static uid_t const no_uid = -1;
72 : : static gid_t output_group = -1;
73 : : static uid_t output_owner = -1;
74 : : #ifndef GID_T_MAX
75 : : #define GID_T_MAX MAXVAL(gid_t, TYPE_BIT(gid_t))
76 : : #endif
77 : : #ifndef UID_T_MAX
78 : : #define UID_T_MAX MAXVAL(uid_t, TYPE_BIT(uid_t))
79 : : #endif
80 : :
81 : : /*
82 : : * The minimum alignment of a type, for pre-C23 platforms.
83 : : * The __SUNPRO_C test is because Oracle Developer Studio 12.6 lacks
84 : : * <stdalign.h> even though __STDC_VERSION__ == 201112.
85 : : */
86 : : #if __STDC_VERSION__ < 201112 || defined __SUNPRO_C
87 : : #define alignof(type) offsetof(struct { char a; type b; }, b)
88 : : #elif __STDC_VERSION__ < 202311
89 : : #include <stdalign.h>
90 : : #endif
91 : :
92 : : /* The name used for the file implementing the obsolete -p option. */
93 : : #ifndef TZDEFRULES
94 : : #define TZDEFRULES "posixrules"
95 : : #endif
96 : :
97 : : /* The maximum length of a text line, including the trailing newline. */
98 : : #ifndef _POSIX2_LINE_MAX
99 : : #define _POSIX2_LINE_MAX 2048
100 : : #endif
101 : :
102 : : /*
103 : : * The type for line numbers. Use PRIdMAX to format them; formerly
104 : : * there was also "#define PRIdLINENO PRIdMAX" and formats used
105 : : * PRIdLINENO, but xgettext cannot grok that.
106 : : */
107 : : typedef intmax_t lineno_t;
108 : :
109 : : struct rule
110 : : {
111 : : int r_filenum;
112 : : lineno_t r_linenum;
113 : : const char *r_name;
114 : :
115 : : zic_t r_loyear; /* for example, 1986 */
116 : : zic_t r_hiyear; /* for example, 1986 */
117 : : bool r_hiwasnum;
118 : :
119 : : int r_month; /* 0..11 */
120 : :
121 : : int r_dycode; /* see below */
122 : : int r_dayofmonth;
123 : : int r_wday;
124 : :
125 : : zic_t r_tod; /* time from midnight */
126 : : bool r_todisstd; /* is r_tod standard time? */
127 : : bool r_todisut; /* is r_tod UT? */
128 : : bool r_isdst; /* is this daylight saving time? */
129 : : zic_t r_save; /* offset from standard time */
130 : : const char *r_abbrvar; /* variable part of abbreviation */
131 : :
132 : : bool r_todo; /* a rule to do (used in outzone) */
133 : : zic_t r_temp; /* used in outzone */
134 : : };
135 : :
136 : : /*
137 : : * r_dycode r_dayofmonth r_wday
138 : : */
139 : : enum
140 : : {
141 : : DC_DOM, /* 1..31 */ /* unused */
142 : : DC_DOWGEQ, /* 1..31 */ /* 0..6 (Sun..Sat) */
143 : : DC_DOWLEQ /* 1..31 */ /* 0..6 (Sun..Sat) */
144 : : };
145 : :
146 : : struct zone
147 : : {
148 : : int z_filenum;
149 : : lineno_t z_linenum;
150 : :
151 : : const char *z_name;
152 : : zic_t z_stdoff;
153 : : char *z_rule;
154 : : const char *z_format;
155 : : char z_format_specifier;
156 : :
157 : : bool z_isdst;
158 : : zic_t z_save;
159 : :
160 : : struct rule *z_rules;
161 : : ptrdiff_t z_nrules;
162 : :
163 : : struct rule z_untilrule;
164 : : zic_t z_untiltime;
165 : : };
166 : :
167 : : #ifndef AT_SYMLINK_FOLLOW
168 : : #define linkat(targetdir, target, linknamedir, linkname, flag) \
169 : : (errno = ENOTSUP, -1)
170 : : #endif
171 : :
172 : : static void verror(const char *const string, va_list args) pg_attribute_printf(1, 0);
173 : : static void error(const char *const string, ...) pg_attribute_printf(1, 2);
174 : : static void warning(const char *const string, ...) pg_attribute_printf(1, 2);
175 : : static int addabbr(char chs[TZ_MAX_CHARS], int *pnchs, char const *abbr);
176 : : static void addtt(zic_t starttime, int type);
177 : : static int addtype(zic_t utoff, char const *abbr,
178 : : bool isdst, bool ttisstd, bool ttisut);
179 : : static void adjleap(void);
180 : : static void associate(void);
181 : : static void checkabbr(char const *string);
182 : : static void check_for_signal(void);
183 : : static void dolink(char const *target, char const *linkname, bool staysymlink);
184 : : static int getfields(char *cp, char **array, int arrayelts);
185 : : static zic_t gethms(const char *string, const char *errstring);
186 : : static zic_t getsave(char *field, bool *isdst);
187 : : static void inexpires(char **fields, int nfields);
188 : : static void infile(int fnum, char const *name);
189 : : static void inleap(char **fields, int nfields);
190 : : static void inlink(char **fields, int nfields);
191 : : static void inrule(char **fields, int nfields);
192 : : static bool inzcont(char **fields, int nfields);
193 : : static bool inzone(char **fields, int nfields);
194 : : static bool inzsub(char **fields, int nfields, bool iscont);
195 : : static bool is_alpha(char a);
196 : : static int itssymlink(char const *name, int *cache);
197 : : static void leapadd(zic_t t, int correction, int rolling);
198 : : static char lowerit(char a);
199 : : static void mkdirs(char const *argname, bool ancestors);
200 : : static zic_t oadd(zic_t t1, zic_t t2);
201 : : static zic_t omul(zic_t t1, zic_t t2);
202 : : static void outzone(const struct zone *zpfirst, ptrdiff_t zonecount);
203 : : static zic_t rpytime(const struct rule *rp, zic_t wantedy);
204 : : static bool rulesub(struct rule *rp,
205 : : const char *loyearp, const char *hiyearp,
206 : : const char *typep, const char *monthp,
207 : : const char *dayp, const char *timep);
208 : : static zic_t tadd(zic_t t1, zic_t t2);
209 : :
210 : : /* Is C an ASCII digit? */
211 : : static bool
212 : 149400 : is_digit(char c)
213 : : {
214 [ + + + - ]: 149400 : return '0' <= c && c <= '9';
215 : : }
216 : :
217 : : /* Bound on length of what %z can expand to. */
218 : : enum
219 : : {
220 : : PERCENT_Z_LEN_BOUND = sizeof "+995959" - 1};
221 : :
222 : : static int charcnt;
223 : : static bool errors;
224 : : static bool warnings;
225 : : static int filenum;
226 : : static ptrdiff_t leapcnt;
227 : : static ptrdiff_t leap_alloc;
228 : : static bool leapseen;
229 : : static zic_t leapminyear;
230 : : static zic_t leapmaxyear;
231 : : static lineno_t linenum;
232 : : static int max_abbrvar_len = PERCENT_Z_LEN_BOUND;
233 : : static int max_format_len;
234 : : static zic_t max_year;
235 : : static zic_t min_year;
236 : : static bool noise;
237 : : static bool print_abbrevs;
238 : : static zic_t print_cutoff;
239 : : static bool skip_mkdir;
240 : : static int rfilenum;
241 : : static lineno_t rlinenum;
242 : : static const char *progname;
243 : : static char const *leapsec;
244 : : static char *const *main_argv;
245 : : static ptrdiff_t timecnt;
246 : : static ptrdiff_t timecnt_alloc;
247 : : static int typecnt;
248 : : static int unspecifiedtype;
249 : :
250 : : /*
251 : : * Line codes.
252 : : */
253 : :
254 : : enum
255 : : {
256 : : LC_RULE,
257 : : LC_ZONE,
258 : : LC_LINK,
259 : : LC_LEAP,
260 : : LC_EXPIRES
261 : : };
262 : :
263 : : /*
264 : : * Which fields are which on a Zone line.
265 : : */
266 : :
267 : : enum
268 : : {
269 : : ZF_NAME = 1,
270 : : ZF_STDOFF,
271 : : ZF_RULE,
272 : : ZF_FORMAT,
273 : : ZF_TILYEAR,
274 : : ZF_TILMONTH,
275 : : ZF_TILDAY,
276 : : ZF_TILTIME,
277 : : ZONE_MAXFIELDS,
278 : : ZONE_MINFIELDS = ZF_TILYEAR
279 : : };
280 : :
281 : : /*
282 : : * Which fields are which on a Zone continuation line.
283 : : */
284 : :
285 : : enum
286 : : {
287 : : ZFC_STDOFF,
288 : : ZFC_RULE,
289 : : ZFC_FORMAT,
290 : : ZFC_TILYEAR,
291 : : ZFC_TILMONTH,
292 : : ZFC_TILDAY,
293 : : ZFC_TILTIME,
294 : : ZONEC_MAXFIELDS,
295 : : ZONEC_MINFIELDS = ZFC_TILYEAR
296 : : };
297 : :
298 : : /*
299 : : * Which files are which on a Rule line.
300 : : */
301 : :
302 : : enum
303 : : {
304 : : RF_NAME = 1,
305 : : RF_LOYEAR,
306 : : RF_HIYEAR,
307 : : RF_COMMAND,
308 : : RF_MONTH,
309 : : RF_DAY,
310 : : RF_TOD,
311 : : RF_SAVE,
312 : : RF_ABBRVAR,
313 : : RULE_FIELDS
314 : : };
315 : :
316 : : /*
317 : : * Which fields are which on a Link line.
318 : : */
319 : :
320 : : enum
321 : : {
322 : : LF_TARGET = 1,
323 : : LF_LINKNAME,
324 : : LINK_FIELDS
325 : : };
326 : :
327 : : /*
328 : : * Which fields are which on a Leap line.
329 : : */
330 : :
331 : : enum
332 : : {
333 : : LP_YEAR = 1,
334 : : LP_MONTH,
335 : : LP_DAY,
336 : : LP_TIME,
337 : : LP_CORR,
338 : : LP_ROLL,
339 : : LEAP_FIELDS,
340 : :
341 : : /*
342 : : * Expires lines are like Leap lines, except without CORR and ROLL fields.
343 : : */
344 : : EXPIRES_FIELDS = LP_TIME + 1
345 : : };
346 : :
347 : : /*
348 : : * The maximum number of fields on any of the above lines.
349 : : * (The "+"s pacify gcc -Wenum-compare.)
350 : : */
351 : : enum
352 : : {
353 : : MAX_FIELDS = max(max(+RULE_FIELDS, +LINK_FIELDS),
354 : : max(+LEAP_FIELDS, +EXPIRES_FIELDS))
355 : : };
356 : :
357 : : /*
358 : : * Year synonyms.
359 : : */
360 : :
361 : : enum
362 : : {
363 : : YR_MINIMUM, /* "minimum" is for backward compatibility
364 : : * only */
365 : : YR_MAXIMUM,
366 : : YR_ONLY
367 : : };
368 : :
369 : : static struct rule *rules;
370 : : static ptrdiff_t nrules; /* number of rules */
371 : : static ptrdiff_t nrules_alloc;
372 : :
373 : : static struct zone *zones;
374 : : static ptrdiff_t nzones; /* number of zones */
375 : : static ptrdiff_t nzones_alloc;
376 : :
377 : : struct link
378 : : {
379 : : int l_filenum;
380 : : lineno_t l_linenum;
381 : : const char *l_target;
382 : : const char *l_linkname;
383 : : };
384 : :
385 : : static struct link *links;
386 : : static ptrdiff_t nlinks;
387 : : static ptrdiff_t nlinks_alloc;
388 : :
389 : : struct lookup
390 : : {
391 : : const char *l_word;
392 : : const int l_value;
393 : : };
394 : :
395 : : static struct lookup const *byword(const char *word,
396 : : const struct lookup *table);
397 : :
398 : : static struct lookup const zi_line_codes[] = {
399 : : {"Rule", LC_RULE},
400 : : {"Zone", LC_ZONE},
401 : : {"Link", LC_LINK},
402 : : {NULL, 0}
403 : : };
404 : : static struct lookup const leap_line_codes[] = {
405 : : {"Leap", LC_LEAP},
406 : : {"Expires", LC_EXPIRES},
407 : : {NULL, 0}
408 : : };
409 : :
410 : : static struct lookup const mon_names[] = {
411 : : {"January", TM_JANUARY},
412 : : {"February", TM_FEBRUARY},
413 : : {"March", TM_MARCH},
414 : : {"April", TM_APRIL},
415 : : {"May", TM_MAY},
416 : : {"June", TM_JUNE},
417 : : {"July", TM_JULY},
418 : : {"August", TM_AUGUST},
419 : : {"September", TM_SEPTEMBER},
420 : : {"October", TM_OCTOBER},
421 : : {"November", TM_NOVEMBER},
422 : : {"December", TM_DECEMBER},
423 : : {NULL, 0}
424 : : };
425 : :
426 : : static struct lookup const wday_names[] = {
427 : : {"Sunday", TM_SUNDAY},
428 : : {"Monday", TM_MONDAY},
429 : : {"Tuesday", TM_TUESDAY},
430 : : {"Wednesday", TM_WEDNESDAY},
431 : : {"Thursday", TM_THURSDAY},
432 : : {"Friday", TM_FRIDAY},
433 : : {"Saturday", TM_SATURDAY},
434 : : {NULL, 0}
435 : : };
436 : :
437 : : static struct lookup const lasts[] = {
438 : : {"last-Sunday", TM_SUNDAY},
439 : : {"last-Monday", TM_MONDAY},
440 : : {"last-Tuesday", TM_TUESDAY},
441 : : {"last-Wednesday", TM_WEDNESDAY},
442 : : {"last-Thursday", TM_THURSDAY},
443 : : {"last-Friday", TM_FRIDAY},
444 : : {"last-Saturday", TM_SATURDAY},
445 : : {NULL, 0}
446 : : };
447 : :
448 : : static struct lookup const begin_years[] = {
449 : : {"minimum", YR_MINIMUM},
450 : : {NULL, 0}
451 : : };
452 : :
453 : : static struct lookup const end_years[] = {
454 : : {"maximum", YR_MAXIMUM},
455 : : {"only", YR_ONLY},
456 : : {NULL, 0}
457 : : };
458 : :
459 : : static struct lookup const leap_types[] = {
460 : : {"Rolling", true},
461 : : {"Stationary", false},
462 : : {NULL, 0}
463 : : };
464 : :
465 : : static const int len_months[2][MONSPERYEAR] = {
466 : : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
467 : : {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
468 : : };
469 : :
470 : : static const int len_years[2] = {
471 : : DAYSPERNYEAR, DAYSPERLYEAR
472 : : };
473 : :
474 : : static struct attype
475 : : {
476 : : zic_t at;
477 : : bool dontmerge;
478 : : unsigned char type;
479 : : } *attypes;
480 : : static zic_t utoffs[TZ_MAX_TYPES];
481 : : static char isdsts[TZ_MAX_TYPES];
482 : : static unsigned char desigidx[TZ_MAX_TYPES];
483 : : static bool ttisstds[TZ_MAX_TYPES];
484 : : static bool ttisuts[TZ_MAX_TYPES];
485 : : static char chars[TZ_MAX_CHARS];
486 : : static struct
487 : : {
488 : : zic_t trans;
489 : : zic_t corr;
490 : : char roll;
491 : : } *leap;
492 : :
493 : : /*
494 : : * Memory allocation.
495 : : */
496 : :
497 : : ATTRIBUTE_NORETURN static void
498 : 0 : memory_exhausted(const char *msg)
499 : : {
500 : 0 : fprintf(stderr, _("%s: Memory exhausted: %s\n"), progname, msg);
501 : 0 : exit(EXIT_FAILURE);
502 : : }
503 : :
504 : : ATTRIBUTE_NORETURN static void
505 : 0 : size_overflow(void)
506 : : {
507 : 0 : memory_exhausted(_("size overflow"));
508 : : }
509 : :
510 : : ATTRIBUTE_PURE_114833_HACK
511 : : static ptrdiff_t
512 : 3242 : size_sum(size_t a, size_t b)
513 : : {
514 : : #ifdef ckd_add
515 : : ptrdiff_t sum;
516 : :
517 [ + - ]: 3242 : if (!ckd_add(&sum, a, b) && sum <= INDEX_MAX)
518 : 3242 : return sum;
519 : : #else
520 : : if (a <= INDEX_MAX && b <= INDEX_MAX - a)
521 : : return a + b;
522 : : #endif
523 : 0 : size_overflow();
524 : : }
525 : :
526 : : ATTRIBUTE_PURE_114833_HACK
527 : : static ptrdiff_t
528 : 1364 : size_product(ptrdiff_t nitems, ptrdiff_t itemsize)
529 : : {
530 : : #ifdef ckd_mul
531 : : ptrdiff_t product;
532 : :
533 [ + - ]: 1364 : if (!ckd_mul(&product, nitems, itemsize) && product <= INDEX_MAX)
534 : 1364 : return product;
535 : : #else
536 : : ptrdiff_t nitems_max = INDEX_MAX / itemsize;
537 : :
538 : : if (nitems <= nitems_max)
539 : : return nitems * itemsize;
540 : : #endif
541 : 0 : size_overflow();
542 : : }
543 : :
544 : : ATTRIBUTE_PURE_114833_HACK
545 : : static ptrdiff_t
546 : 1364 : align_to(ptrdiff_t size, ptrdiff_t alignment)
547 : : {
548 : 1364 : ptrdiff_t lo_bits = alignment - 1,
549 : 1364 : sum = size_sum(size, lo_bits);
550 : :
551 : 1364 : return sum & ~lo_bits;
552 : : }
553 : :
554 : : static void *
555 : 72988 : memcheck(void *ptr)
556 : : {
557 [ - + ]: 72988 : if (ptr == NULL)
558 : 0 : memory_exhausted(strerror(errno));
559 : 72988 : return ptr;
560 : : }
561 : :
562 : : static void *
563 : 7334 : xmalloc(size_t size)
564 : : {
565 : 7334 : return memcheck(malloc(size));
566 : : }
567 : :
568 : : static void *
569 : 248 : xrealloc(void *ptr, size_t size)
570 : : {
571 : 248 : return memcheck(realloc(ptr, size));
572 : : }
573 : :
574 : : static char *
575 : 65406 : xstrdup(char const *str)
576 : : {
577 : 65406 : return memcheck(strdup(str));
578 : : }
579 : :
580 : : static ptrdiff_t
581 : 248 : grow_nitems_alloc(ptrdiff_t *nitems_alloc, ptrdiff_t itemsize)
582 : : {
583 : 248 : ptrdiff_t addend = (*nitems_alloc >> 1) + 1;
584 : : #if defined ckd_add && defined ckd_mul
585 : : ptrdiff_t product;
586 : :
587 [ + - ]: 248 : if (!ckd_add(nitems_alloc, *nitems_alloc, addend)
588 [ + - ]: 248 : && !ckd_mul(&product, *nitems_alloc, itemsize) && product <= INDEX_MAX)
589 : 248 : return product;
590 : : #else
591 : : if (*nitems_alloc <= ((INDEX_MAX - 1) / 3 * 2) / itemsize)
592 : : {
593 : : *nitems_alloc += addend;
594 : : return *nitems_alloc * itemsize;
595 : : }
596 : : #endif
597 : 0 : memory_exhausted(_("integer overflow"));
598 : : }
599 : :
600 : : static void *
601 : 87012 : growalloc(void *ptr, ptrdiff_t itemsize, ptrdiff_t nitems,
602 : : ptrdiff_t *nitems_alloc)
603 : : {
604 : 87012 : return (nitems < *nitems_alloc
605 : : ? ptr
606 [ + + ]: 87012 : : xrealloc(ptr, grow_nitems_alloc(nitems_alloc, itemsize)));
607 : : }
608 : :
609 : : /*
610 : : * Error handling.
611 : : */
612 : :
613 : : /*
614 : : * In most of the code, an input file name is represented by its index
615 : : * into the main argument vector, except that LEAPSEC_FILENUM stands
616 : : * for leapsec and COMMAND_LINE_FILENUM stands for the command line.
617 : : */
618 : : enum
619 : : {
620 : : LEAPSEC_FILENUM = -2, COMMAND_LINE_FILENUM = -1};
621 : :
622 : : /* Return the name of the Ith input file, for diagnostics. */
623 : : static char const *
624 : 4 : filename(int i)
625 : : {
626 [ - + ]: 4 : if (i == COMMAND_LINE_FILENUM)
627 : 0 : return _("command line");
628 : : else
629 : : {
630 [ + - ]: 4 : char const *fname = i == LEAPSEC_FILENUM ? leapsec : main_argv[i];
631 : :
632 [ + - ]: 4 : return strcmp(fname, "-") == 0 ? _("standard input") : fname;
633 : : }
634 : : }
635 : :
636 : : static void
637 : 5863264 : eats(int fnum, lineno_t num, int rfnum, lineno_t rnum)
638 : : {
639 : 5863264 : filenum = fnum;
640 : 5863264 : linenum = num;
641 : 5863264 : rfilenum = rfnum;
642 : 5863264 : rlinenum = rnum;
643 : 5863264 : }
644 : :
645 : : static void
646 : 33672 : eat(int fnum, lineno_t num)
647 : : {
648 : 33672 : eats(fnum, num, 0, -1);
649 : 33672 : }
650 : :
651 : : static void
652 : 0 : verror(const char *const string, va_list args)
653 : : {
654 : 0 : check_for_signal();
655 : :
656 : : /*
657 : : * Match the format of "cc" to allow sh users to zic ... 2>&1 | error -t
658 : : * "*" -v on BSD systems.
659 : : */
660 [ # # ]: 0 : if (filenum)
661 : 0 : fprintf(stderr, _("\"%s\", line %" PRIdMAX ": "),
662 : : filename(filenum), linenum);
663 : 0 : vfprintf(stderr, string, args);
664 [ # # ]: 0 : if (rfilenum)
665 : 0 : fprintf(stderr, _(" (rule from \"%s\", line %" PRIdMAX ")"),
666 : : filename(rfilenum), rlinenum);
667 : 0 : fprintf(stderr, "\n");
668 : 0 : }
669 : :
670 : : static void
671 : 0 : error(const char *const string, ...)
672 : : {
673 : : va_list args;
674 : :
675 : 0 : va_start(args, string);
676 : 0 : verror(string, args);
677 : 0 : va_end(args);
678 : 0 : errors = true;
679 : 0 : }
680 : :
681 : : static void
682 : 0 : warning(const char *const string, ...)
683 : : {
684 : : va_list args;
685 : :
686 : 0 : fprintf(stderr, _("warning: "));
687 : 0 : va_start(args, string);
688 : 0 : verror(string, args);
689 : 0 : va_end(args);
690 : 0 : warnings = true;
691 : 0 : }
692 : :
693 : : /*
694 : : * Convert ARG, a string in base BASE, to an unsigned long value no
695 : : * greater than MAXVAL. On failure, diagnose with MSGID and exit.
696 : : */
697 : : static unsigned long
698 : 0 : arg2num(char const *arg, int base, unsigned long maxval, char const *msgid)
699 : : {
700 : : unsigned long n;
701 : : char *ep;
702 : :
703 : 0 : errno = 0;
704 : 0 : n = strtoul(arg, &ep, base);
705 [ # # # # : 0 : if (ep == arg || *ep || maxval < n || errno)
# # # # ]
706 : : {
707 : 0 : fprintf(stderr, _(msgid), progname, arg);
708 : 0 : exit(EXIT_FAILURE);
709 : : }
710 : 0 : return n;
711 : : }
712 : :
713 : : #ifndef MODE_T_MAX
714 : : #define MODE_T_MAX MAXVAL(mode_t, TYPE_BIT(mode_t))
715 : : #endif
716 : :
717 : : #ifndef HAVE_SETMODE
718 : : #if (defined __FreeBSD__ || defined __NetBSD__ || defined __OpenBSD__ \
719 : : || (defined __APPLE__ && defined __MACH__))
720 : : #define HAVE_SETMODE 1
721 : : #else
722 : : #define HAVE_SETMODE 0
723 : : #endif
724 : : #endif
725 : :
726 : : static mode_t const no_mode = -1;
727 : : static mode_t output_mode = -1;
728 : :
729 : : static mode_t
730 : 0 : mode_option(char const *arg)
731 : : {
732 : : #if HAVE_SETMODE
733 : : void *set = setmode(arg);
734 : :
735 : : if (set)
736 : : {
737 : : mode_t mode = getmode(set, CREAT_PERMS);
738 : :
739 : : free(set);
740 : : return mode;
741 : : }
742 : : #endif
743 : 0 : return arg2num(arg, 8, min(MODE_T_MAX, ULONG_MAX),
744 : : N_("%s: -m '%s': invalid mode\n"));
745 : : }
746 : :
747 : : static int
748 : 1364 : chmetadata(FILE *stream)
749 : : {
750 : : #ifndef WIN32
751 [ + - - + ]: 1364 : if (output_owner != no_uid || output_group != no_gid)
752 : : {
753 : 0 : int r = fchown(fileno(stream), output_owner, output_group);
754 : :
755 [ # # ]: 0 : if (r < 0)
756 : 0 : return r;
757 : : }
758 [ - + ]: 1364 : return output_mode == no_mode ? 0 : fchmod(fileno(stream), output_mode);
759 : : #else
760 : : return 0;
761 : : #endif
762 : : }
763 : :
764 : : /*
765 : : * Close STREAM.
766 : : * If it had an I/O error, report it against DIR/NAME,
767 : : * remove TEMPNAME if nonnull, and then exit.
768 : : * If TEMPNAME is nonnull, and if requested,
769 : : * change the stream's metadata before closing.
770 : : */
771 : : static void
772 : 1368 : close_file(FILE *stream, char const *dir, char const *name,
773 : : char const *tempname)
774 : : {
775 : 1368 : char const *e = (ferror(stream) ? _("I/O error")
776 [ + - + + ]: 2736 : : ((tempname
777 [ + - + - ]: 1364 : && (fflush(stream) < 0 || chmetadata(stream) < 0))
778 [ - + ]: 1368 : || fclose(stream) < 0)
779 : 0 : ? strerror(errno) : NULL);
780 : :
781 [ - + ]: 1368 : if (e)
782 : : {
783 [ # # # # ]: 0 : if (name && *name == '/')
784 : 0 : dir = NULL;
785 [ # # # # : 0 : fprintf(stderr, "%s: %s%s%s%s%s\n", progname,
# # # # ]
786 : : dir ? dir : "", dir ? "/" : "",
787 : : name ? name : "", name ? ": " : "",
788 : : e);
789 [ # # ]: 0 : if (tempname)
790 : 0 : remove(tempname);
791 : 0 : exit(EXIT_FAILURE);
792 : : }
793 : 1368 : }
794 : :
795 : : ATTRIBUTE_NORETURN static void
796 : 0 : duplicate_options(char const *opt)
797 : : {
798 : 0 : fprintf(stderr, _("%s: More than one %s option specified\n"), progname, opt);
799 : 0 : exit(EXIT_FAILURE);
800 : : }
801 : :
802 : : ATTRIBUTE_NORETURN static void
803 : 0 : usage(FILE *stream, int status)
804 : : {
805 : 0 : fprintf(stream,
806 : : _("%s: usage is %s [ --version ] [ --help ] [ -v ] [ -P ] \\\n"
807 : : "\t[ -b {slim|fat} ] [ -d directory ] [ -D ] \\\n"
808 : : "\t[ -l localtime ] [ -L leapseconds ] [ -m mode ] \\\n"
809 : : "\t[ -p posixrules ] [ -r '[@lo][/@hi]' ] [ -R @hi ] \\\n"
810 : : "\t[ -t localtime-link ] [ -u 'owner[:group]' ] \\\n"
811 : : "\t[ filename ... ]\n\n"
812 : : "Report bugs to %s.\n"),
813 : : progname, progname, PACKAGE_BUGREPORT);
814 [ # # ]: 0 : if (status == EXIT_SUCCESS)
815 : 0 : close_file(stream, NULL, NULL, NULL);
816 : 0 : exit(status);
817 : : }
818 : :
819 : : static void
820 : 0 : group_option(char const *arg)
821 : : {
822 : : #ifndef WIN32
823 [ # # ]: 0 : if (*arg)
824 : : {
825 [ # # ]: 0 : if (output_group != no_gid)
826 : : {
827 : 0 : fprintf(stderr, _("multiple groups specified"));
828 : 0 : exit(EXIT_FAILURE);
829 : : }
830 : : else
831 : : {
832 : 0 : struct group *gr = getgrnam(arg);
833 : :
834 [ # # ]: 0 : output_group = (gr ? gr->gr_gid
835 : 0 : : arg2num(arg, 10, min(GID_T_MAX, ULONG_MAX),
836 : : N_("%s: invalid group: %s\n")));
837 : : }
838 : : }
839 : : #endif
840 : 0 : }
841 : :
842 : : static void
843 : 0 : owner_option(char const *arg)
844 : : {
845 : : #ifndef WIN32
846 [ # # ]: 0 : if (*arg)
847 : : {
848 [ # # ]: 0 : if (output_owner != no_uid)
849 : : {
850 : 0 : fprintf(stderr, _("multiple owners specified"));
851 : 0 : exit(EXIT_FAILURE);
852 : : }
853 : : else
854 : : {
855 : 0 : struct passwd *pw = getpwnam(arg);
856 : :
857 [ # # ]: 0 : output_owner = (pw ? pw->pw_uid
858 : 0 : : arg2num(arg, 10, min(UID_T_MAX, ULONG_MAX),
859 : : N_("%s: invalid owner: %s\n")));
860 : : }
861 : : }
862 : : #endif
863 : 0 : }
864 : :
865 : : /*
866 : : * If setting owner or group, use temp file permissions that avoid
867 : : * security races before the fchmod at the end.
868 : : */
869 : : static void
870 : 4 : use_safe_temp_permissions(void)
871 : : {
872 [ + - - + ]: 4 : if (output_owner != no_uid || output_group != no_gid)
873 : : {
874 : :
875 : : /* The mode when done with the file. */
876 : : mode_t omode;
877 : :
878 [ # # ]: 0 : if (output_mode == no_mode)
879 : : {
880 : 0 : mode_t cmask = umask(0);
881 : :
882 : 0 : umask(cmask);
883 : 0 : omode = CREAT_PERMS & ~cmask;
884 : : }
885 : : else
886 : 0 : omode = output_mode;
887 : :
888 : : /*
889 : : * The mode passed to open+O_CREAT. Do not bother with executable
890 : : * permissions, as they should not be used and this mode is merely a
891 : : * nicety (even a mode of 0 still work).
892 : : */
893 : 0 : creat_perms = ((((omode & (S_IRUSR | S_IRGRP | S_IROTH))
894 : : == (S_IRUSR | S_IRGRP | S_IROTH))
895 [ # # ]: 0 : ? S_IRUSR | S_IRGRP | S_IROTH : 0)
896 : 0 : | (((omode & (S_IWUSR | S_IWGRP | S_IWOTH))
897 : : == (S_IWUSR | S_IWGRP | S_IWOTH))
898 [ # # ]: 0 : ? S_IWUSR | S_IWGRP | S_IWOTH : 0));
899 : :
900 : : /*
901 : : * If creat_perms is not the final mode, arrange to run fchmod later,
902 : : * even if -m was not used.
903 : : */
904 [ # # ]: 0 : if (creat_perms != omode)
905 : 0 : output_mode = omode;
906 : : }
907 : 4 : }
908 : :
909 : : /*
910 : : * Change the working directory to DIR, possibly creating DIR and its
911 : : * ancestors. After this is done, all files are accessed with names
912 : : * relative to DIR.
913 : : */
914 : : static void
915 : 4 : change_directory(char const *dir)
916 : : {
917 [ + + ]: 4 : if (chdir(dir) != 0)
918 : : {
919 : 2 : int chdir_errno = errno;
920 : :
921 [ + - ]: 2 : if (chdir_errno == ENOENT)
922 : : {
923 : 2 : mkdirs(dir, false);
924 [ - + ]: 2 : chdir_errno = chdir(dir) == 0 ? 0 : errno;
925 : : }
926 [ - + ]: 2 : if (chdir_errno != 0)
927 : : {
928 : 0 : fprintf(stderr, _("%s: Can't chdir to %s: %s\n"),
929 : : progname, dir, strerror(chdir_errno));
930 : 0 : exit(EXIT_FAILURE);
931 : : }
932 : : }
933 : 4 : }
934 : :
935 : : /* Compare the two links A and B, for a stable sort by link name. */
936 : : static int
937 : 8408 : qsort_linkcmp(void const *a, void const *b)
938 : : {
939 : 8408 : struct link const *l = a;
940 : 8408 : struct link const *m = b;
941 : 8408 : int cmp = strcmp(l->l_linkname, m->l_linkname);
942 : :
943 [ + - ]: 8408 : if (cmp)
944 : 8408 : return cmp;
945 : :
946 : : /*
947 : : * The link names are the same. Make the sort stable by comparing file
948 : : * numbers (where subtraction cannot overflow) and possibly line numbers
949 : : * (where it can).
950 : : */
951 : 0 : cmp = l->l_filenum - m->l_filenum;
952 [ # # ]: 0 : if (cmp)
953 : 0 : return cmp;
954 : 0 : return (l->l_linenum > m->l_linenum) - (l->l_linenum < m->l_linenum);
955 : : }
956 : :
957 : : /* Compare the string KEY to the link B, for bsearch. */
958 : : static int
959 : 7108 : bsearch_linkcmp(void const *key, void const *b)
960 : : {
961 : 7108 : struct link const *m = b;
962 : :
963 : 7108 : return strcmp(key, m->l_linkname);
964 : : }
965 : :
966 : : /* Make the links specified by the Link lines. */
967 : : static void
968 : 4 : make_links(void)
969 : : {
970 : : ptrdiff_t i,
971 : : j,
972 : : nalinks,
973 : : pass_size;
974 : :
975 [ + - ]: 4 : if (1 < nlinks)
976 : 4 : qsort(links, nlinks, sizeof *links, qsort_linkcmp);
977 : :
978 : : /* Ignore each link superseded by a later link with the same name. */
979 : 4 : j = 0;
980 [ + + ]: 1032 : for (i = 0; i < nlinks; i++)
981 : : {
982 : 1028 : while (i + 1 < nlinks
983 [ + + - + ]: 1028 : && strcmp(links[i].l_linkname, links[i + 1].l_linkname) == 0)
984 : 0 : i++;
985 : 1028 : links[j++] = links[i];
986 : : }
987 : 4 : nlinks = pass_size = j;
988 : :
989 : : /*
990 : : * Walk through the link array making links. However, if a link's target
991 : : * has not been made yet, append a copy to the end of the array. The end
992 : : * of the array will gradually fill up with a small sorted subsequence of
993 : : * not-yet-made links. nalinks counts all the links in the array,
994 : : * including copies. When we reach the copied subsequence, it may still
995 : : * contain a link to a not-yet-made link, so the process repeats. At any
996 : : * given point in time, the link array consists of the following
997 : : * subregions, where 0 <= i <= j <= nalinks and 0 <= nlinks <= nalinks:
998 : : *
999 : : * 0 .. (i - 1): links that either have been made, or have been copied to
1000 : : * a later point point in the array (this later point can be in any of the
1001 : : * three subregions) i .. (j - 1): not-yet-made links for this pass j ..
1002 : : * (nalinks - 1): not-yet-made links that this pass has skipped because
1003 : : * they were links to not-yet-made links
1004 : : *
1005 : : * The first subregion might not be sorted if nlinks < i; the other two
1006 : : * subregions are sorted. This algorithm does not alter entries 0 ..
1007 : : * (nlinks - 1), which remain sorted.
1008 : : *
1009 : : * If there are L links, this algorithm is O(C*L*log(L)) where C is the
1010 : : * length of the longest link chain. Usually C is short (e.g., 3) though
1011 : : * its worst-case value is L.
1012 : : */
1013 : :
1014 : 4 : j = nalinks = nlinks;
1015 : :
1016 [ + + ]: 1032 : for (i = 0; i < nalinks; i++)
1017 : : {
1018 : : struct link *l;
1019 : :
1020 : 1028 : eat(links[i].l_filenum, links[i].l_linenum);
1021 : :
1022 : : /* If this pass examined all its links, start the next pass. */
1023 [ - + ]: 1028 : if (i == j)
1024 : : {
1025 [ # # ]: 0 : if (nalinks - i == pass_size)
1026 : : {
1027 : 0 : error(_("\"Link %s %s\" is part of a link cycle"),
1028 : 0 : links[i].l_target, links[i].l_linkname);
1029 : 0 : break;
1030 : : }
1031 : 0 : j = nalinks;
1032 : 0 : pass_size = nalinks - i;
1033 : : }
1034 : :
1035 : : /*
1036 : : * Diagnose self links, which the cycle detection algorithm would not
1037 : : * otherwise catch.
1038 : : */
1039 [ - + ]: 1028 : if (strcmp(links[i].l_target, links[i].l_linkname) == 0)
1040 : : {
1041 : 0 : error(_("link %s targets itself"), links[i].l_target);
1042 : 0 : continue;
1043 : : }
1044 : :
1045 : : /* Make this link unless its target has not been made yet. */
1046 : 1028 : l = bsearch(links[i].l_target, &links[i + 1], j - (i + 1),
1047 : : sizeof *links, bsearch_linkcmp);
1048 [ + - ]: 1028 : if (!l)
1049 : 1028 : l = bsearch(links[i].l_target, &links[j], nalinks - j,
1050 : : sizeof *links, bsearch_linkcmp);
1051 [ + - ]: 1028 : if (!l)
1052 : 1028 : dolink(links[i].l_target, links[i].l_linkname, false);
1053 : : else
1054 : : {
1055 : : /*
1056 : : * The link target has not been made yet; copy the link to the
1057 : : * end.
1058 : : */
1059 : 0 : links = growalloc(links, sizeof *links, nalinks, &nlinks_alloc);
1060 : 0 : links[nalinks++] = links[i];
1061 : : }
1062 : :
1063 [ - + - - ]: 1028 : if (noise && i < nlinks)
1064 : : {
1065 [ # # ]: 0 : if (l)
1066 : 0 : warning(_("link %s targeting link %s mishandled by pre-2023 zic"),
1067 : 0 : links[i].l_linkname, links[i].l_target);
1068 [ # # ]: 0 : else if (bsearch(links[i].l_target, links, nlinks, sizeof *links,
1069 : : bsearch_linkcmp))
1070 : 0 : warning(_("link %s targeting link %s"),
1071 : 0 : links[i].l_linkname, links[i].l_target);
1072 : : }
1073 : 1028 : check_for_signal();
1074 : : }
1075 : 4 : }
1076 : :
1077 : : /*
1078 : : * Simple signal handling: just set a flag that is checked
1079 : : * periodically outside critical sections. To set up the handler,
1080 : : * prefer sigaction if available to close a signal race.
1081 : : */
1082 : :
1083 : : static sig_atomic_t got_signal;
1084 : :
1085 : : static void
1086 : 0 : signal_handler(int sig)
1087 : : {
1088 : : #ifndef SA_SIGINFO
1089 : : signal(sig, signal_handler);
1090 : : #endif
1091 : 0 : got_signal = sig;
1092 : 0 : }
1093 : :
1094 : : /* Arrange for SIGINT etc. to be caught by the handler. */
1095 : : static void
1096 : 4 : catch_signals(void)
1097 : : {
1098 : : static int const signals[] = {
1099 : : #ifdef SIGHUP
1100 : : SIGHUP,
1101 : : #endif
1102 : : SIGINT,
1103 : : #ifdef SIGPIPE
1104 : : SIGPIPE,
1105 : : #endif
1106 : : SIGTERM
1107 : : };
1108 : : int i;
1109 : :
1110 [ + + ]: 20 : for (i = 0; i < sizeof signals / sizeof signals[0]; i++)
1111 : : {
1112 : : #ifdef SA_SIGINFO
1113 : : struct sigaction act0,
1114 : : act;
1115 : :
1116 : 16 : act.sa_handler = signal_handler;
1117 : 16 : sigemptyset(&act.sa_mask);
1118 : 16 : act.sa_flags = 0;
1119 [ + - ]: 16 : if (sigaction(signals[i], &act, &act0) == 0
1120 [ + - - + ]: 16 : && !(act0.sa_flags & SA_SIGINFO) && act0.sa_handler == SIG_IGN)
1121 : : {
1122 : 0 : sigaction(signals[i], &act0, NULL);
1123 : 0 : got_signal = 0;
1124 : : }
1125 : : #else
1126 : : if (signal(signals[i], signal_handler) == SIG_IGN)
1127 : : {
1128 : : signal(signals[i], SIG_IGN);
1129 : : got_signal = 0;
1130 : : }
1131 : : #endif
1132 : : }
1133 : 4 : }
1134 : :
1135 : : /* If a signal has arrived, terminate zic with appropriate status. */
1136 : : static void
1137 : 438138 : check_for_signal(void)
1138 : : {
1139 : 438138 : int sig = got_signal;
1140 : :
1141 [ - + ]: 438138 : if (sig)
1142 : : {
1143 : 0 : signal(sig, SIG_DFL);
1144 : 0 : raise(sig);
1145 : 0 : abort(); /* A bug in 'raise'. */
1146 : : }
1147 : 438138 : }
1148 : :
1149 : : enum
1150 : : {
1151 : : TIME_T_BITS_IN_FILE = 64};
1152 : :
1153 : : /* The minimum and maximum values representable in a TZif file. */
1154 : : static zic_t const min_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE);
1155 : : static zic_t const max_time = MAXVAL(zic_t, TIME_T_BITS_IN_FILE);
1156 : :
1157 : : /*
1158 : : * The minimum, and one less than the maximum, values specified by
1159 : : * the -r option. These default to MIN_TIME and MAX_TIME.
1160 : : */
1161 : : static zic_t lo_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE);
1162 : : static zic_t hi_time = MAXVAL(zic_t, TIME_T_BITS_IN_FILE);
1163 : :
1164 : : /*
1165 : : * The time specified by the -R option, defaulting to MIN_TIME;
1166 : : * or lo_time, whichever is greater.
1167 : : */
1168 : : static zic_t redundant_time = MINVAL(zic_t, TIME_T_BITS_IN_FILE);
1169 : :
1170 : : /* The time specified by an Expires line, or negative if no such line. */
1171 : : static zic_t leapexpires = -1;
1172 : :
1173 : : /*
1174 : : * Set the time range of the output to TIMERANGE.
1175 : : * Return true if successful.
1176 : : */
1177 : : static bool
1178 : 0 : timerange_option(char *timerange)
1179 : : {
1180 : 0 : intmax_t lo = min_time,
1181 : 0 : hi = max_time;
1182 : 0 : char *lo_end = timerange,
1183 : : *hi_end;
1184 : :
1185 [ # # ]: 0 : if (*timerange == '@')
1186 : : {
1187 : 0 : errno = 0;
1188 : 0 : lo = strtoimax(timerange + 1, &lo_end, 10);
1189 [ # # # # : 0 : if (lo_end == timerange + 1 || (lo == INTMAX_MAX && errno == ERANGE))
# # ]
1190 : 0 : return false;
1191 : : }
1192 : 0 : hi_end = lo_end;
1193 [ # # # # ]: 0 : if (lo_end[0] == '/' && lo_end[1] == '@')
1194 : : {
1195 : 0 : errno = 0;
1196 : 0 : hi = strtoimax(lo_end + 2, &hi_end, 10);
1197 [ # # # # ]: 0 : if (hi_end == lo_end + 2 || hi == INTMAX_MIN)
1198 : 0 : return false;
1199 [ # # # # ]: 0 : hi -= !(hi == INTMAX_MAX && errno == ERANGE);
1200 : : }
1201 [ # # # # : 0 : if (*hi_end || hi < lo || max_time < lo || hi < min_time)
# # # # ]
1202 : 0 : return false;
1203 : 0 : lo_time = max(lo, min_time);
1204 : 0 : hi_time = min(hi, max_time);
1205 : 0 : return true;
1206 : : }
1207 : :
1208 : : /* Generate redundant time stamps up to OPT. Return true if successful. */
1209 : : static bool
1210 : 0 : redundant_time_option(char *opt)
1211 : : {
1212 [ # # ]: 0 : if (*opt == '@')
1213 : : {
1214 : : intmax_t redundant;
1215 : : char *opt_end;
1216 : :
1217 : 0 : redundant = strtoimax(opt + 1, &opt_end, 10);
1218 [ # # # # ]: 0 : if (opt_end != opt + 1 && !*opt_end)
1219 : : {
1220 : 0 : redundant_time = max(redundant_time, redundant);
1221 : 0 : return true;
1222 : : }
1223 : : }
1224 : 0 : return false;
1225 : : }
1226 : :
1227 : : static const char *psxrules;
1228 : : static const char *lcltime;
1229 : : static const char *directory;
1230 : : static const char *tzdefault;
1231 : :
1232 : : /* True if DIRECTORY ends in '/'. */
1233 : : static bool directory_ends_in_slash;
1234 : :
1235 : : /*
1236 : : * -1 if the TZif output file should be slim, 0 if default, 1 if the
1237 : : * output should be fat for backward compatibility. ZIC_BLOAT_DEFAULT
1238 : : * determines the default.
1239 : : */
1240 : : static int bloat;
1241 : :
1242 : : static bool
1243 : 80728 : want_bloat(void)
1244 : : {
1245 : 80728 : return 0 <= bloat;
1246 : : }
1247 : :
1248 : : #ifndef ZIC_BLOAT_DEFAULT
1249 : : #define ZIC_BLOAT_DEFAULT "slim"
1250 : : #endif
1251 : :
1252 : : int
1253 : 4 : main(int argc, char **argv)
1254 : 6468 : {
1255 : : int c,
1256 : : k;
1257 : : ptrdiff_t i,
1258 : : j;
1259 : 4 : bool timerange_given = false;
1260 : :
1261 : 4 : main_argv = argv;
1262 [ + - ]: 4 : progname = argv[0] ? argv[0] : "zic";
1263 : : if (TYPE_BIT(zic_t) < 64)
1264 : : {
1265 : : fprintf(stderr, "%s: %s\n", progname,
1266 : : _("wild compilation-time specification of zic_t"));
1267 : : return EXIT_FAILURE;
1268 : : }
1269 [ + + ]: 16 : for (k = 1; k < argc; k++)
1270 [ - + ]: 12 : if (strcmp(argv[k], "--version") == 0)
1271 : : {
1272 : 0 : printf("zic %s\n", PG_VERSION);
1273 : 0 : close_file(stdout, NULL, NULL, NULL);
1274 : 0 : return EXIT_SUCCESS;
1275 : : }
1276 [ - + ]: 12 : else if (strcmp(argv[k], "--help") == 0)
1277 : : {
1278 : 0 : usage(stdout, EXIT_SUCCESS);
1279 : : }
1280 [ + + ]: 8 : while ((c = getopt(argc, argv, "b:d:Dg:l:L:m:p:Pr:R:st:u:vy:")) != -1)
1281 [ - - + - : 4 : switch (c)
- - - - -
- - - - -
- - - ]
1282 : : {
1283 : 0 : default:
1284 : 0 : usage(stderr, EXIT_FAILURE);
1285 : 0 : case 'b':
1286 [ # # ]: 0 : if (strcmp(optarg, "slim") == 0)
1287 : : {
1288 [ # # ]: 0 : if (0 < bloat)
1289 : 0 : error(_("incompatible -b options"));
1290 : 0 : bloat = -1;
1291 : : }
1292 [ # # ]: 0 : else if (strcmp(optarg, "fat") == 0)
1293 : : {
1294 [ # # ]: 0 : if (bloat < 0)
1295 : 0 : error(_("incompatible -b options"));
1296 : 0 : bloat = 1;
1297 : : }
1298 : : else
1299 : 0 : error(_("invalid option: -b '%s'"), optarg);
1300 : 0 : break;
1301 : 4 : case 'd':
1302 [ - + ]: 4 : if (directory)
1303 : 0 : duplicate_options("-d");
1304 : 4 : directory = strdup(optarg);
1305 : 4 : break;
1306 : 0 : case 'D':
1307 : 0 : skip_mkdir = true;
1308 : 0 : break;
1309 : 0 : case 'g':
1310 : :
1311 : : /*
1312 : : * This undocumented option is present for compatibility with
1313 : : * FreeBSD 14.
1314 : : */
1315 : 0 : group_option(optarg);
1316 : 0 : break;
1317 : 0 : case 'l':
1318 [ # # ]: 0 : if (lcltime)
1319 : 0 : duplicate_options("-l");
1320 : 0 : lcltime = strdup(optarg);
1321 : 0 : break;
1322 : 0 : case 'm':
1323 [ # # ]: 0 : if (output_mode != no_mode)
1324 : 0 : duplicate_options("-m");
1325 : 0 : output_mode = mode_option(optarg);
1326 : 0 : break;
1327 : 0 : case 'p':
1328 [ # # ]: 0 : if (psxrules)
1329 : 0 : duplicate_options("-p");
1330 [ # # ]: 0 : if (strcmp(optarg, "-") != 0)
1331 : 0 : warning(_("-p is obsolete"
1332 : : " and likely ineffective"));
1333 : 0 : psxrules = strdup(optarg);
1334 : 0 : break;
1335 : 0 : case 't':
1336 [ # # ]: 0 : if (tzdefault)
1337 : 0 : duplicate_options("-t");
1338 : 0 : tzdefault = strdup(optarg);
1339 : 0 : break;
1340 : 0 : case 'u':
1341 : : {
1342 : 0 : char *colon = strchr(optarg, ':');
1343 : :
1344 [ # # ]: 0 : if (colon)
1345 : 0 : *colon = '\0';
1346 : 0 : owner_option(optarg);
1347 [ # # ]: 0 : if (colon)
1348 : 0 : group_option(colon + 1);
1349 : : }
1350 : 0 : break;
1351 : 0 : case 'y':
1352 : 0 : warning(_("-y ignored"));
1353 : 0 : break;
1354 : 0 : case 'L':
1355 [ # # ]: 0 : if (leapsec)
1356 : 0 : duplicate_options("-L");
1357 : 0 : leapsec = strdup(optarg);
1358 : 0 : break;
1359 : 0 : case 'v':
1360 : 0 : noise = true;
1361 : 0 : break;
1362 : 0 : case 'P':
1363 : 0 : print_abbrevs = true;
1364 : 0 : print_cutoff = time(NULL);
1365 : 0 : break;
1366 : 0 : case 'r':
1367 [ # # ]: 0 : if (timerange_given)
1368 : 0 : duplicate_options("-r");
1369 [ # # ]: 0 : if (!timerange_option(optarg))
1370 : : {
1371 : 0 : fprintf(stderr,
1372 : : _("%s: invalid time range: %s\n"),
1373 : : progname, optarg);
1374 : 0 : return EXIT_FAILURE;
1375 : : }
1376 : 0 : timerange_given = true;
1377 : 0 : break;
1378 : 0 : case 'R':
1379 [ # # ]: 0 : if (!redundant_time_option(optarg))
1380 : : {
1381 : 0 : fprintf(stderr, _("%s: invalid time: %s\n"),
1382 : : progname, optarg);
1383 : 0 : return EXIT_FAILURE;
1384 : : }
1385 : 0 : break;
1386 : 0 : case 's':
1387 : 0 : warning(_("-s ignored"));
1388 : 0 : break;
1389 : : }
1390 [ + - - + ]: 4 : if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
1391 : 0 : usage(stderr, EXIT_FAILURE); /* usage message by request */
1392 [ - + ]: 4 : if (hi_time + (hi_time < ZIC_MAX) < redundant_time)
1393 : : {
1394 : 0 : fprintf(stderr, _("%s: -R time exceeds -r cutoff\n"), progname);
1395 : 0 : return EXIT_FAILURE;
1396 : : }
1397 [ - + ]: 4 : if (redundant_time < lo_time)
1398 : 0 : redundant_time = lo_time;
1399 [ + - ]: 4 : if (bloat == 0)
1400 : : {
1401 : : static char const bloat_default[] = ZIC_BLOAT_DEFAULT;
1402 : :
1403 [ + - ]: 4 : if (strcmp(bloat_default, "slim") == 0)
1404 : 4 : bloat = -1;
1405 [ # # ]: 0 : else if (strcmp(bloat_default, "fat") == 0)
1406 : 0 : bloat = 1;
1407 : : else
1408 : 0 : abort(); /* Configuration error. */
1409 : : }
1410 [ - + ]: 4 : if (directory == NULL)
1411 : 0 : directory = "data";
1412 [ + - ]: 4 : if (tzdefault == NULL)
1413 : 4 : tzdefault = TZDEFAULT;
1414 : :
1415 [ + - - + ]: 4 : if (optind < argc && leapsec != NULL)
1416 : : {
1417 : 0 : infile(LEAPSEC_FILENUM, leapsec);
1418 : 0 : adjleap();
1419 : : }
1420 : :
1421 [ + + ]: 8 : for (k = optind; k < argc; k++)
1422 : 4 : infile(k, argv[k]);
1423 [ - + ]: 4 : if (errors)
1424 : 0 : return EXIT_FAILURE;
1425 : 4 : associate();
1426 : 4 : use_safe_temp_permissions();
1427 : 4 : change_directory(directory);
1428 : 4 : directory_ends_in_slash = directory[strlen(directory) - 1] == '/';
1429 : 4 : catch_signals();
1430 [ + + ]: 1368 : for (i = 0; i < nzones; i = j)
1431 : : {
1432 : : /*
1433 : : * Find the next non-continuation zone entry.
1434 : : */
1435 [ + + + + ]: 7832 : for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
1436 : 6468 : continue;
1437 : 1364 : outzone(&zones[i], j - i);
1438 : 1364 : check_for_signal();
1439 : : }
1440 : 4 : make_links();
1441 [ - + ]: 4 : if (lcltime != NULL)
1442 : : {
1443 : 0 : eat(COMMAND_LINE_FILENUM, 1);
1444 : 0 : dolink(lcltime, tzdefault, true);
1445 : : }
1446 [ - + ]: 4 : if (psxrules != NULL)
1447 : : {
1448 : 0 : eat(COMMAND_LINE_FILENUM, 1);
1449 : 0 : dolink(psxrules, TZDEFRULES, true);
1450 : : }
1451 [ - + - - : 4 : if (warnings && (ferror(stderr) || fclose(stderr) != 0))
- - ]
1452 : 0 : return EXIT_FAILURE;
1453 : 4 : return errors ? EXIT_FAILURE : EXIT_SUCCESS;
1454 : : }
1455 : :
1456 : : static bool
1457 : 4708 : componentcheck(char const *name, char const *component,
1458 : : char const *component_end)
1459 : : {
1460 : : enum
1461 : : {
1462 : : component_len_max = 14};
1463 : 4708 : ptrdiff_t component_len = component_end - component;
1464 : :
1465 [ - + ]: 4708 : if (component_len == 0)
1466 : : {
1467 [ # # ]: 0 : if (!*name)
1468 : 0 : error(_("empty file name"));
1469 : : else
1470 [ # # # # ]: 0 : error(_(component == name
1471 : : ? "file name '%s' begins with '/'"
1472 : : : *component_end
1473 : : ? "file name '%s' contains '//'"
1474 : : : "file name '%s' ends with '/'"),
1475 : : name);
1476 : 0 : return false;
1477 : : }
1478 [ + - + + ]: 4708 : if (0 < component_len && component_len <= 2
1479 [ - + - - ]: 56 : && component[0] == '.' && component_end[-1] == '.')
1480 : : {
1481 : 0 : int len = component_len;
1482 : :
1483 : 0 : error(_("file name '%s' contains '%.*s' component"),
1484 : : name, len, component);
1485 : 0 : return false;
1486 : : }
1487 [ - + ]: 4708 : if (noise)
1488 : : {
1489 [ # # # # ]: 0 : if (0 < component_len && component[0] == '-')
1490 : 0 : warning(_("file name '%s' component contains leading '-'"),
1491 : : name);
1492 [ # # ]: 0 : if (component_len_max < component_len)
1493 : 0 : warning(_("file name '%s' contains overlength component"
1494 : : " '%.*s...'"),
1495 : : name, component_len_max, component);
1496 : : }
1497 : 4708 : return true;
1498 : : }
1499 : :
1500 : : static bool
1501 : 2392 : namecheck(const char *name)
1502 : : {
1503 : : char const *cp;
1504 : :
1505 : : /* Benign characters in a portable file name. */
1506 : : static char const benign[] =
1507 : : "-/_"
1508 : : "abcdefghijklmnopqrstuvwxyz"
1509 : : "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
1510 : :
1511 : : /*
1512 : : * Non-control chars in the POSIX portable character set, excluding the
1513 : : * benign characters.
1514 : : */
1515 : : static char const printable_and_not_benign[] =
1516 : : " !\"#$%&'()*+,.0123456789:;<=>?@[\\]^`{|}~";
1517 : :
1518 : 2392 : char const *component = name;
1519 : :
1520 [ + + ]: 36408 : for (cp = name; *cp; cp++)
1521 : : {
1522 : 34016 : unsigned char c = *cp;
1523 : :
1524 [ - + - - ]: 34016 : if (noise && !strchr(benign, c))
1525 : : {
1526 [ # # ]: 0 : warning((strchr(printable_and_not_benign, c)
1527 : : ? _("file name '%s' contains byte '%c'")
1528 : : : _("file name '%s' contains byte '\\%o'")),
1529 : : name, c);
1530 : : }
1531 [ + + ]: 34016 : if (c == '/')
1532 : : {
1533 [ - + ]: 2316 : if (!componentcheck(name, component, cp))
1534 : 0 : return false;
1535 : 2316 : component = cp + 1;
1536 : : }
1537 : : }
1538 : 2392 : return componentcheck(name, component, cp);
1539 : : }
1540 : :
1541 : : /* Return a random uint_fast64_t. */
1542 : : static uint_fast64_t
1543 : 1878 : get_rand_u64(void)
1544 : : {
1545 : : #if HAVE_GETRANDOM
1546 : : static uint_fast64_t entropy_buffer[max(1, 256 / sizeof(uint_fast64_t))];
1547 : : static int nwords;
1548 : :
1549 : : if (!nwords)
1550 : : {
1551 : : ssize_t s;
1552 : :
1553 : : for (;; check_for_signal())
1554 : : {
1555 : : s = getrandom(entropy_buffer, sizeof entropy_buffer, 0);
1556 : : if (!(s < 0 && errno == EINTR))
1557 : : break;
1558 : : }
1559 : :
1560 : : nwords = s < 0 ? -1 : s / sizeof *entropy_buffer;
1561 : : }
1562 : : if (0 < nwords)
1563 : : return entropy_buffer[--nwords];
1564 : : #endif
1565 : :
1566 : : /*
1567 : : * getrandom didn't work, so fall back on portable code that is not the
1568 : : * best because the seed isn't cryptographically random and 'rand' might
1569 : : * not be cryptographically secure.
1570 : : */
1571 : : {
1572 : : static bool initialized;
1573 : :
1574 [ + + ]: 1878 : if (!initialized)
1575 : : {
1576 : 4 : srand(time(NULL));
1577 : 4 : initialized = true;
1578 : : }
1579 : : }
1580 : :
1581 : : /*
1582 : : * Return a random number if rand() yields a random number and in the
1583 : : * typical case where RAND_MAX is one less than a power of two. In other
1584 : : * cases this code yields a sort-of-random number.
1585 : : */
1586 : : {
1587 : 1878 : uint_fast64_t rand_max = RAND_MAX,
1588 [ + - ]: 1878 : nrand = rand_max < UINT_FAST64_MAX ? rand_max + 1 : 0,
1589 : 1878 : rmod = INT_MAX < UINT_FAST64_MAX ? 0 : UINT_FAST64_MAX / nrand + 1,
1590 : 1878 : r = 0,
1591 : 1878 : rmax = 0;
1592 : :
1593 : 3756 : for (;; check_for_signal())
1594 : 3756 : {
1595 : 5634 : uint_fast64_t rmax1 = rmax;
1596 : :
1597 [ - + ]: 5634 : if (rmod)
1598 : : {
1599 : : /*
1600 : : * Avoid signed integer overflow on theoretical platforms
1601 : : * where uint_fast64_t promotes to int.
1602 : : */
1603 : 0 : rmax1 %= rmod;
1604 : 0 : r %= rmod;
1605 : : }
1606 : 5634 : rmax1 = nrand * rmax1 + rand_max;
1607 : 5634 : r = nrand * r + rand();
1608 [ + - ]: 5634 : rmax = rmax < rmax1 ? rmax1 : UINT_FAST64_MAX;
1609 [ + + ]: 5634 : if (UINT_FAST64_MAX <= rmax)
1610 : 1878 : break;
1611 : : }
1612 : :
1613 : 1878 : return r;
1614 : : }
1615 : : }
1616 : :
1617 : : /*
1618 : : * Generate a randomish name in the same directory as *NAME. If
1619 : : * *NAMEALLOC, put the name into *NAMEALLOC which is assumed to be
1620 : : * that returned by a previous call and is thus already almost set up
1621 : : * and equal to *NAME; otherwise, allocate a new name and put its
1622 : : * address into both *NAMEALLOC and *NAME.
1623 : : */
1624 : : static void
1625 : 1878 : random_dirent(char const **name, char **namealloc)
1626 : : {
1627 : 1878 : char const *src = *name;
1628 : 1878 : char *dst = *namealloc;
1629 : : static char const prefix[] = ".zic";
1630 : : static char const alphabet[] =
1631 : : "abcdefghijklmnopqrstuvwxyz"
1632 : : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
1633 : : "0123456789";
1634 : : enum
1635 : : {
1636 : : prefixlen = sizeof prefix - 1, alphabetlen = sizeof alphabet - 1};
1637 : 1878 : int suffixlen = 6;
1638 : 1878 : char const *lastslash = strrchr(src, '/');
1639 [ + + ]: 1878 : ptrdiff_t dirlen = lastslash ? lastslash + 1 - src : 0;
1640 : : int i;
1641 : : uint_fast64_t r;
1642 : 1878 : uint_fast64_t base = alphabetlen;
1643 : :
1644 : : /* BASE**6 */
1645 : 1878 : uint_fast64_t base__6 = base * base * base * base * base * base;
1646 : :
1647 : : /*
1648 : : * The largest uintmax_t that is a multiple of BASE**6. Any random
1649 : : * uintmax_t value that is this value or greater, yields a biased
1650 : : * remainder when divided by BASE**6. UNFAIR_MIN equals the mathematical
1651 : : * value of ((UINTMAX_MAX + 1) - (UINTMAX_MAX + 1) % BASE**6) computed
1652 : : * without overflow.
1653 : : */
1654 : 1878 : uint_fast64_t unfair_min = -((UINTMAX_MAX % base__6 + 1) % base__6);
1655 : :
1656 [ + - ]: 1878 : if (!dst)
1657 : : {
1658 : 1878 : char *cp = dst = xmalloc(size_sum(dirlen, prefixlen + suffixlen + 1));
1659 : :
1660 : 1878 : memcpy(cp, src, dirlen);
1661 : 1878 : cp += dirlen;
1662 : 1878 : memcpy(cp, prefix, prefixlen);
1663 : 1878 : cp += prefixlen;
1664 : 1878 : cp[suffixlen] = '\0';
1665 : 1878 : *name = *namealloc = dst;
1666 : : }
1667 : :
1668 : 0 : for (;; check_for_signal())
1669 : : {
1670 : 1878 : r = get_rand_u64();
1671 [ + - ]: 1878 : if (r < unfair_min)
1672 : 1878 : break;
1673 : : }
1674 : :
1675 [ + + ]: 13146 : for (i = 0; i < suffixlen; i++)
1676 : : {
1677 : 11268 : dst[dirlen + prefixlen + i] = alphabet[r % alphabetlen];
1678 : 11268 : r /= alphabetlen;
1679 : : }
1680 : 1878 : }
1681 : :
1682 : : /*
1683 : : * For diagnostics the directory, and file name relative to that
1684 : : * directory, respectively. A diagnostic routine can name FILENAME by
1685 : : * outputting diagdir(FILENAME), then diagslash(FILENAME), then FILENAME.
1686 : : */
1687 : : static char const *
1688 : 0 : diagdir(char const *filename)
1689 : : {
1690 [ # # ]: 0 : return *filename == '/' ? "" : directory;
1691 : : }
1692 : : static char const *
1693 : 0 : diagslash(char const *filename)
1694 : : {
1695 [ # # # # ]: 0 : return &"/"[*filename == '/' || directory_ends_in_slash];
1696 : : }
1697 : :
1698 : : /*
1699 : : * Prepare to write to the file *OUTNAME, using *TEMPNAME to store the
1700 : : * name of the temporary file that will eventually be renamed to
1701 : : * *OUTNAME. Assign the temporary file's name to both *OUTNAME and
1702 : : * *TEMPNAME. If *TEMPNAME is null, allocate the name of any such
1703 : : * temporary file; otherwise, reuse *TEMPNAME's storage, which is
1704 : : * already set up and only needs its trailing suffix updated.
1705 : : */
1706 : : static FILE *
1707 : 1364 : open_outfile(char const **outname, char **tempname)
1708 : : {
1709 : 1364 : bool dirs_made = false;
1710 : :
1711 [ + - ]: 1364 : if (!*tempname)
1712 : 1364 : random_dirent(outname, tempname);
1713 : :
1714 : 28 : for (;; check_for_signal())
1715 : 28 : {
1716 : 1392 : int oflags = O_WRONLY | O_BINARY | O_CREAT | O_EXCL;
1717 : 1392 : int fd = open(*outname, oflags, creat_perms);
1718 : : int err;
1719 : :
1720 [ + + ]: 1392 : if (fd < 0)
1721 : 28 : err = errno;
1722 : : else
1723 : : {
1724 : 1364 : FILE *fp = fdopen(fd, "wb");
1725 : :
1726 [ + - ]: 1364 : if (fp)
1727 : 1364 : return fp;
1728 : 0 : err = errno;
1729 : 0 : close(fd);
1730 : : }
1731 [ + - + - ]: 28 : if (err == ENOENT && !dirs_made)
1732 : : {
1733 : 28 : mkdirs(*outname, true);
1734 : 28 : dirs_made = true;
1735 : : }
1736 [ # # ]: 0 : else if (err == EEXIST)
1737 : 0 : random_dirent(outname, tempname);
1738 : : else
1739 : : {
1740 : 0 : fprintf(stderr, _("%s: Can't create %s%s%s: %s\n"),
1741 : : progname, diagdir(*outname), diagslash(*outname), *outname,
1742 : : strerror(err));
1743 : 0 : exit(EXIT_FAILURE);
1744 : : }
1745 : : }
1746 : : }
1747 : :
1748 : : /*
1749 : : * If TEMPNAME, the result is in the temporary file TEMPNAME even
1750 : : * though the user wanted it in NAME, so rename TEMPNAME to NAME.
1751 : : * Report an error and exit if there is trouble. Also, free TEMPNAME.
1752 : : */
1753 : : static void
1754 : 2392 : rename_dest(char *tempname, char const *name)
1755 : : {
1756 [ + + ]: 2392 : if (tempname)
1757 : : {
1758 [ - + ]: 1878 : if (rename(tempname, name) != 0)
1759 : : {
1760 : 0 : int rename_errno = errno;
1761 : :
1762 : 0 : remove(tempname);
1763 : 0 : fprintf(stderr, _("%s: rename to %s%s%s: %s\n"),
1764 : : progname, diagdir(name), diagslash(name), name,
1765 : : strerror(rename_errno));
1766 : 0 : exit(EXIT_FAILURE);
1767 : : }
1768 : 1878 : free(tempname);
1769 : : }
1770 : 2392 : }
1771 : :
1772 : : /*
1773 : : * Create symlink contents suitable for symlinking TARGET to LINKNAME, as a
1774 : : * freshly allocated string. TARGET should be a relative file name, and
1775 : : * is relative to the global variable DIRECTORY. LINKNAME can be either
1776 : : * relative or absolute. Return a null pointer if the symlink contents
1777 : : * was not computed because LINKNAME is absolute but DIRECTORY is not.
1778 : : */
1779 : : #ifdef HAVE_SYMLINK
1780 : : static char *
1781 : 0 : relname(char const *target, char const *linkname)
1782 : : {
1783 : : size_t i,
1784 : : taillen,
1785 : 0 : dir_len = 0,
1786 : 0 : dotdots = 0;
1787 : : ptrdiff_t dotdotetcsize,
1788 : 0 : linksize = INDEX_MAX;
1789 : 0 : char const *f = target;
1790 : 0 : char *result = NULL;
1791 : :
1792 [ # # ]: 0 : if (*linkname == '/')
1793 : : {
1794 : : /* Make F absolute too. */
1795 : 0 : size_t len = strlen(directory);
1796 [ # # # # ]: 0 : bool needs_slash = len && directory[len - 1] != '/';
1797 : 0 : size_t lenslash = len + needs_slash;
1798 : 0 : size_t targetsize = strlen(target) + 1;
1799 : : char *cp;
1800 : :
1801 [ # # ]: 0 : if (*directory != '/')
1802 : 0 : return NULL;
1803 : 0 : linksize = size_sum(lenslash, targetsize);
1804 : 0 : f = cp = result = xmalloc(linksize);
1805 : 0 : memcpy(cp, directory, len);
1806 : 0 : cp += len;
1807 : 0 : *cp = '/';
1808 : 0 : memcpy(cp + needs_slash, target, targetsize);
1809 : : }
1810 [ # # # # ]: 0 : for (i = 0; f[i] && f[i] == linkname[i]; i++)
1811 [ # # ]: 0 : if (f[i] == '/')
1812 : 0 : dir_len = i + 1;
1813 [ # # ]: 0 : for (; linkname[i]; i++)
1814 [ # # # # ]: 0 : dotdots += linkname[i] == '/' && linkname[i - 1] != '/';
1815 : 0 : taillen = strlen(f + dir_len);
1816 : 0 : dotdotetcsize = size_sum(size_product(dotdots, 3), taillen + 1);
1817 [ # # ]: 0 : if (dotdotetcsize <= linksize)
1818 : : {
1819 : : char *cp;
1820 : :
1821 [ # # ]: 0 : if (!result)
1822 : 0 : result = xmalloc(dotdotetcsize);
1823 : 0 : cp = result;
1824 [ # # ]: 0 : for (i = 0; i < dotdots; i++)
1825 : : {
1826 : 0 : memcpy(cp, "../", 3);
1827 : 0 : cp += 3;
1828 : : }
1829 : 0 : memmove(cp, f + dir_len, taillen + 1);
1830 : : }
1831 : 0 : return result;
1832 : : }
1833 : : #endif /* HAVE_SYMLINK */
1834 : :
1835 : : /*
1836 : : * Return true if A and B must have the same parent dir if A and B exist.
1837 : : * Return false if this is not necessarily true (though it might be true).
1838 : : * Keep it simple, and do not inspect the file system.
1839 : : */
1840 : : ATTRIBUTE_PURE_114833
1841 : : static bool
1842 : 0 : same_parent_dirs(char const *a, char const *b)
1843 : : {
1844 [ # # ]: 0 : for (; *a == *b; a++, b++)
1845 [ # # ]: 0 : if (!*a)
1846 : 0 : return true;
1847 [ # # # # ]: 0 : return !(strchr(a, '/') || strchr(b, '/'));
1848 : : }
1849 : :
1850 : : static void
1851 : 1028 : dolink(char const *target, char const *linkname, bool staysymlink)
1852 : : {
1853 : 1028 : bool linkdirs_made = false;
1854 : : int link_errno;
1855 : 1028 : char *tempname = NULL;
1856 : 1028 : char const *outname = linkname;
1857 : 1028 : int targetissym = -2,
1858 : 1028 : linknameissym = -2;
1859 : :
1860 [ - + ]: 1028 : if (strcmp(target, "-") == 0)
1861 : : {
1862 [ # # # # : 0 : if (remove(linkname) == 0 || errno == ENOENT || errno == ENOTDIR)
# # ]
1863 : 0 : return;
1864 : : else
1865 : : {
1866 : 0 : char const *e = strerror(errno);
1867 : :
1868 : 0 : fprintf(stderr, _("%s: Can't remove %s%s%s: %s\n"),
1869 : : progname, diagdir(linkname), diagslash(linkname), linkname,
1870 : : e);
1871 : 0 : exit(EXIT_FAILURE);
1872 : : }
1873 : : }
1874 : :
1875 : 526 : for (;; check_for_signal())
1876 : : {
1877 [ + + ]: 1554 : if (linkat(AT_FDCWD, target, AT_FDCWD, outname, AT_SYMLINK_FOLLOW)
1878 : : == 0)
1879 : : {
1880 : 1028 : link_errno = 0;
1881 : 1028 : break;
1882 : : }
1883 : 526 : link_errno = errno;
1884 : : /* Linux 2.6.16 and 2.6.17 mishandle AT_SYMLINK_FOLLOW. */
1885 [ - + ]: 526 : if (link_errno == EINVAL)
1886 : 0 : link_errno = ENOTSUP;
1887 : :
1888 : : /*
1889 : : * If linkat is not supported, fall back on link(A, B). However, skip
1890 : : * this if A is a relative symlink and A and B might not have the same
1891 : : * parent directory. On some platforms link(A, B) does not follow a
1892 : : * symlink A, and if A is relative it might misbehave elsewhere.
1893 : : */
1894 [ - + ]: 526 : if (link_errno == ENOTSUP
1895 [ # # ]: 0 : && (same_parent_dirs(target, outname)
1896 [ # # ]: 0 : || 0 <= itssymlink(target, &targetissym)))
1897 : : {
1898 [ # # ]: 0 : if (link(target, outname) == 0)
1899 : : {
1900 : 0 : link_errno = 0;
1901 : 0 : break;
1902 : : }
1903 : 0 : link_errno = errno;
1904 : : }
1905 [ + - + - ]: 526 : if (link_errno == EXDEV || link_errno == ENOTSUP)
1906 : : break;
1907 : :
1908 [ + + ]: 526 : if (link_errno == EEXIST)
1909 : : {
1910 : 514 : staysymlink &= !tempname;
1911 : 514 : random_dirent(&outname, &tempname);
1912 [ - + - - ]: 514 : if (staysymlink && itssymlink(linkname, &linknameissym))
1913 : 0 : break;
1914 : : }
1915 [ + - + - ]: 12 : else if (link_errno == ENOENT && !linkdirs_made)
1916 : : {
1917 : 12 : mkdirs(linkname, true);
1918 : 12 : linkdirs_made = true;
1919 : : }
1920 : : else
1921 : : {
1922 : 0 : fprintf(stderr, _("%s: Can't link %s%s%s to %s%s%s: %s\n"),
1923 : : progname, diagdir(target), diagslash(target), target,
1924 : : diagdir(outname), diagslash(outname), outname,
1925 : : strerror(link_errno));
1926 : 0 : exit(EXIT_FAILURE);
1927 : : }
1928 : : }
1929 [ - + ]: 1028 : if (link_errno != 0)
1930 : : {
1931 : : #ifdef HAVE_SYMLINK
1932 : 0 : bool absolute = *target == '/';
1933 [ # # ]: 0 : char *linkalloc = absolute ? NULL : relname(target, linkname);
1934 [ # # ]: 0 : char const *contents = absolute ? target : linkalloc;
1935 : 0 : int symlink_errno = -1;
1936 : :
1937 [ # # ]: 0 : if (contents)
1938 : : {
1939 : 0 : for (;; check_for_signal())
1940 : : {
1941 [ # # ]: 0 : if (symlink(contents, outname) == 0)
1942 : : {
1943 : 0 : symlink_errno = 0;
1944 : 0 : break;
1945 : : }
1946 : 0 : symlink_errno = errno;
1947 [ # # ]: 0 : if (symlink_errno == EEXIST)
1948 : 0 : random_dirent(&outname, &tempname);
1949 [ # # # # ]: 0 : else if (symlink_errno == ENOENT && !linkdirs_made)
1950 : : {
1951 : 0 : mkdirs(linkname, true);
1952 : 0 : linkdirs_made = true;
1953 : : }
1954 : : else
1955 : : break;
1956 : : }
1957 : : }
1958 : 0 : free(linkalloc);
1959 [ # # ]: 0 : if (symlink_errno == 0)
1960 : : {
1961 [ # # # # ]: 0 : if (link_errno != ENOTSUP && link_errno != EEXIST)
1962 : 0 : warning(_("symbolic link used because hard link failed: %s"),
1963 : : strerror(link_errno));
1964 : : }
1965 : : else
1966 : : #endif /* HAVE_SYMLINK */
1967 : : {
1968 : : FILE *fp,
1969 : : *tp;
1970 : : int c;
1971 : :
1972 : 0 : fp = fopen(target, "rb");
1973 [ # # ]: 0 : if (!fp)
1974 : : {
1975 : 0 : char const *e = strerror(errno);
1976 : :
1977 : 0 : fprintf(stderr, _("%s: Can't read %s%s%s: %s\n"),
1978 : : progname, diagdir(target), diagslash(target), target, e);
1979 : 0 : exit(EXIT_FAILURE);
1980 : : }
1981 : 0 : tp = open_outfile(&outname, &tempname);
1982 [ # # ]: 0 : for (; (c = getc(fp)) != EOF; check_for_signal())
1983 : 0 : putc(c, tp);
1984 : 0 : close_file(tp, directory, linkname, tempname);
1985 : 0 : close_file(fp, directory, target, NULL);
1986 [ # # ]: 0 : if (link_errno != ENOTSUP)
1987 : 0 : warning(_("copy used because hard link failed: %s"),
1988 : : strerror(link_errno));
1989 : : #ifdef HAVE_SYMLINK
1990 [ # # ]: 0 : else if (symlink_errno < 0)
1991 : 0 : warning(_("copy used because symbolic link not obvious"));
1992 [ # # ]: 0 : else if (symlink_errno != ENOTSUP)
1993 : 0 : warning(_("copy used because symbolic link failed: %s"),
1994 : : strerror(symlink_errno));
1995 : : #endif
1996 : : }
1997 : : }
1998 : 1028 : rename_dest(tempname, linkname);
1999 : : }
2000 : :
2001 : : /*
2002 : : * Return 1 if NAME is an absolute symbolic link, -1 if it is relative,
2003 : : * 0 if it is not a symbolic link. If *CACHE is not -2, it is the
2004 : : * cached result of a previous call to this function with the same NAME.
2005 : : */
2006 : : static int
2007 : 0 : itssymlink(char const *name, int *cache)
2008 : : {
2009 : : #ifdef HAVE_SYMLINK
2010 [ # # ]: 0 : if (*cache == -2)
2011 : : {
2012 : 0 : char c = '\0';
2013 : :
2014 [ # # # # ]: 0 : *cache = readlink(name, &c, 1) < 0 ? 0 : c == '/' ? 1 : -1;
2015 : : }
2016 : 0 : return *cache;
2017 : : #else
2018 : : return false;
2019 : : #endif
2020 : : }
2021 : :
2022 : : /*
2023 : : * Associate sets of rules with zones.
2024 : : */
2025 : :
2026 : : /*
2027 : : * Sort by rule name.
2028 : : */
2029 : :
2030 : : static int
2031 : 49296 : rcomp(const void *cp1, const void *cp2)
2032 : : {
2033 : 49296 : struct rule const *r1 = cp1,
2034 : 49296 : *r2 = cp2;
2035 : :
2036 : 49296 : return strcmp(r1->r_name, r2->r_name);
2037 : : }
2038 : :
2039 : : static void
2040 : 4 : associate(void)
2041 : : {
2042 : : struct zone *zp;
2043 : : struct rule *rp;
2044 : : ptrdiff_t i,
2045 : : j,
2046 : : base,
2047 : : out;
2048 : :
2049 [ + - ]: 4 : if (1 < nrules)
2050 : : {
2051 : 4 : qsort(rules, nrules, sizeof *rules, rcomp);
2052 [ + + ]: 8336 : for (i = 0; i < nrules - 1; ++i)
2053 : : {
2054 : 8848 : if (strcmp(rules[i].r_name,
2055 [ + + ]: 8332 : rules[i + 1].r_name) != 0)
2056 : 516 : continue;
2057 [ + - ]: 7816 : if (rules[i].r_filenum == rules[i + 1].r_filenum)
2058 : 7816 : continue;
2059 : 0 : eat(rules[i].r_filenum, rules[i].r_linenum);
2060 : 0 : warning(_("same rule name in multiple files"));
2061 : 0 : eat(rules[i + 1].r_filenum, rules[i + 1].r_linenum);
2062 : 0 : warning(_("same rule name in multiple files"));
2063 [ # # ]: 0 : for (j = i + 2; j < nrules; ++j)
2064 : : {
2065 : 0 : if (strcmp(rules[i].r_name,
2066 [ # # ]: 0 : rules[j].r_name) != 0)
2067 : 0 : break;
2068 [ # # ]: 0 : if (rules[i].r_filenum == rules[j].r_filenum)
2069 : 0 : continue;
2070 : 0 : if (rules[i + 1].r_filenum
2071 [ # # ]: 0 : == rules[j].r_filenum)
2072 : 0 : continue;
2073 : 0 : break;
2074 : : }
2075 : 0 : i = j - 1;
2076 : : }
2077 : : }
2078 [ + + ]: 7836 : for (i = 0; i < nzones; ++i)
2079 : : {
2080 : 7832 : zp = &zones[i];
2081 : 7832 : zp->z_rules = NULL;
2082 : 7832 : zp->z_nrules = 0;
2083 : : }
2084 [ + + ]: 524 : for (base = 0; base < nrules; base = out)
2085 : : {
2086 : 520 : rp = &rules[base];
2087 [ + + ]: 8336 : for (out = base + 1; out < nrules; ++out)
2088 [ + + ]: 8332 : if (strcmp(rp->r_name, rules[out].r_name) != 0)
2089 : 516 : break;
2090 [ + + ]: 1018680 : for (i = 0; i < nzones; ++i)
2091 : : {
2092 : 1018160 : zp = &zones[i];
2093 [ + + ]: 1018160 : if (strcmp(zp->z_rule, rp->r_name) != 0)
2094 : 1015128 : continue;
2095 : 3032 : zp->z_rules = rp;
2096 : 3032 : zp->z_nrules = out - base;
2097 : : }
2098 : : }
2099 [ + + ]: 7836 : for (i = 0; i < nzones; ++i)
2100 : : {
2101 : 7832 : zp = &zones[i];
2102 [ + + ]: 7832 : if (zp->z_nrules == 0)
2103 : : {
2104 : : /*
2105 : : * Maybe we have a local standard time offset.
2106 : : */
2107 : 4800 : eat(zp->z_filenum, zp->z_linenum);
2108 : 4800 : zp->z_save = getsave(zp->z_rule, &zp->z_isdst);
2109 : :
2110 : : /*
2111 : : * Note, though, that if there's no rule, a '%s' in the format is
2112 : : * a bad thing.
2113 : : */
2114 [ - + ]: 4800 : if (zp->z_format_specifier == 's')
2115 : 0 : error("%s", _("%s in ruleless zone"));
2116 : : }
2117 : : }
2118 [ - + ]: 4 : if (errors)
2119 : 0 : exit(EXIT_FAILURE);
2120 : 4 : }
2121 : :
2122 : : /*
2123 : : * Read a text line from FP into BUF, which is of size BUFSIZE.
2124 : : * Terminate it with a NUL byte instead of a newline.
2125 : : * Return true if successful, false if EOF.
2126 : : * On error, report the error and exit.
2127 : : */
2128 : : static bool
2129 : 17212 : inputline(FILE *fp, char *buf, ptrdiff_t bufsize)
2130 : : {
2131 : 17212 : ptrdiff_t linelen = 0,
2132 : : ch;
2133 : :
2134 [ + + ]: 430076 : for (; (ch = getc(fp)) != '\n'; check_for_signal())
2135 : : {
2136 [ + + ]: 412868 : if (ch < 0)
2137 : : {
2138 [ - + ]: 4 : if (ferror(fp))
2139 : : {
2140 : 0 : error(_("input error"));
2141 : 0 : exit(EXIT_FAILURE);
2142 : : }
2143 [ + - ]: 4 : if (linelen == 0)
2144 : 4 : return false;
2145 : 0 : error(_("unterminated line"));
2146 : 0 : exit(EXIT_FAILURE);
2147 : : }
2148 [ - + ]: 412864 : if (!ch)
2149 : : {
2150 : 0 : error(_("NUL input byte"));
2151 : 0 : exit(EXIT_FAILURE);
2152 : : }
2153 : 412864 : buf[linelen++] = ch;
2154 [ - + ]: 412864 : if (linelen == bufsize)
2155 : : {
2156 : 0 : error(_("line too long"));
2157 : 0 : exit(EXIT_FAILURE);
2158 : : }
2159 : : }
2160 : 17208 : buf[linelen] = '\0';
2161 : 17208 : return true;
2162 : : }
2163 : :
2164 : : static void
2165 : 4 : infile(int fnum, char const *name)
2166 : : {
2167 : : FILE *fp;
2168 : : const struct lookup *lp;
2169 : : bool wantcont;
2170 : : lineno_t num;
2171 : :
2172 [ - + ]: 4 : if (strcmp(name, "-") == 0)
2173 : : {
2174 : 0 : fp = stdin;
2175 : : }
2176 [ - + ]: 4 : else if ((fp = fopen(name, "r")) == NULL)
2177 : : {
2178 : 0 : const char *e = strerror(errno);
2179 : :
2180 : 0 : fprintf(stderr, _("%s: Cannot open %s: %s\n"),
2181 : : progname, name, e);
2182 : 0 : exit(EXIT_FAILURE);
2183 : : }
2184 : 4 : wantcont = false;
2185 : 4 : for (num = 1;; ++num)
2186 : 17208 : {
2187 : : enum
2188 : : {
2189 : : bufsize_bound
2190 : : = (min(INT_MAX, INDEX_MAX) / FORMAT_LEN_GROWTH_BOUND)};
2191 : : char buf[min(_POSIX2_LINE_MAX, bufsize_bound)];
2192 : : int nfields;
2193 : : char *fields[MAX_FIELDS];
2194 : :
2195 : 17212 : eat(fnum, num);
2196 [ + + ]: 17212 : if (!inputline(fp, buf, sizeof buf))
2197 : 4 : break;
2198 : 17208 : nfields = getfields(buf, fields,
2199 : : sizeof fields / sizeof *fields);
2200 [ + + ]: 17208 : if (nfields == 0)
2201 : : {
2202 : : /* nothing to do */
2203 : : }
2204 [ + + ]: 17196 : else if (wantcont)
2205 : : {
2206 : 6468 : wantcont = inzcont(fields, nfields);
2207 : : }
2208 : : else
2209 : : {
2210 : 10728 : struct lookup const *line_codes
2211 [ - + ]: 10728 : = fnum < 0 ? leap_line_codes : zi_line_codes;
2212 : :
2213 : 10728 : lp = byword(fields[0], line_codes);
2214 [ - + ]: 10728 : if (lp == NULL)
2215 : 0 : error(_("input line of unknown type"));
2216 : : else
2217 [ + + + - : 10728 : switch (lp->l_value)
- - ]
2218 : : {
2219 : 8336 : case LC_RULE:
2220 : 8336 : inrule(fields, nfields);
2221 : 8336 : wantcont = false;
2222 : 8336 : break;
2223 : 1364 : case LC_ZONE:
2224 : 1364 : wantcont = inzone(fields, nfields);
2225 : 1364 : break;
2226 : 1028 : case LC_LINK:
2227 : 1028 : inlink(fields, nfields);
2228 : 1028 : wantcont = false;
2229 : 1028 : break;
2230 : 0 : case LC_LEAP:
2231 : 0 : inleap(fields, nfields);
2232 : 0 : wantcont = false;
2233 : 0 : break;
2234 : 0 : case LC_EXPIRES:
2235 : 0 : inexpires(fields, nfields);
2236 : 0 : wantcont = false;
2237 : 0 : break;
2238 : 0 : default:
2239 : 0 : unreachable();
2240 : : }
2241 : : }
2242 : 17208 : check_for_signal();
2243 : : }
2244 : 4 : close_file(fp, NULL, filename(fnum), NULL);
2245 [ - + ]: 4 : if (wantcont)
2246 : 0 : error(_("expected continuation line not found"));
2247 : 4 : }
2248 : :
2249 : : /*
2250 : : * Convert a string of one of the forms
2251 : : * h -h hh:mm -hh:mm hh:mm:ss -hh:mm:ss
2252 : : * into a number of seconds.
2253 : : * A null string maps to zero.
2254 : : * Call error with errstring and return zero on errors.
2255 : : */
2256 : :
2257 : : static zic_t
2258 : 35772 : gethms(char const *string, char const *errstring)
2259 : : {
2260 : : zic_t hh;
2261 : : int sign,
2262 : 35772 : mm = 0,
2263 : 35772 : ss = 0;
2264 : : char hhx,
2265 : : mmx,
2266 : : ssx,
2267 : 35772 : xr = '0',
2268 : : xs;
2269 : 35772 : int tenths = 0;
2270 : 35772 : bool ok = true;
2271 : :
2272 [ + - + + ]: 35772 : if (string == NULL || *string == '\0')
2273 : 4552 : return 0;
2274 [ + + ]: 31220 : if (*string == '-')
2275 : : {
2276 : 3948 : sign = -1;
2277 : 3948 : ++string;
2278 : : }
2279 : : else
2280 : 27272 : sign = 1;
2281 [ - - - + : 31220 : switch (sscanf(string,
+ + ]
2282 : : "%" SCNdZIC "%c%d%c%d%c%1d%*[0]%c%*[0123456789]%c",
2283 : : &hh, &hhx, &mm, &mmx, &ss, &ssx, &tenths, &xr, &xs))
2284 : : {
2285 : 0 : default:
2286 : 0 : ok = false;
2287 : 0 : break;
2288 : 0 : case 8:
2289 : 0 : ok = is_digit(xr);
2290 : : ATTRIBUTE_FALLTHROUGH;
2291 : 0 : case 7:
2292 : 0 : ok &= ssx == '.';
2293 [ # # # # ]: 0 : if (ok && noise)
2294 : 0 : warning(_("fractional seconds rejected by"
2295 : : " pre-2018 versions of zic"));
2296 : : ATTRIBUTE_FALLTHROUGH;
2297 : : case 5:
2298 : 1588 : ok &= mmx == ':';
2299 : : ATTRIBUTE_FALLTHROUGH;
2300 : 2420 : case 3:
2301 : 2420 : ok &= hhx == ':';
2302 : : ATTRIBUTE_FALLTHROUGH;
2303 : 31220 : case 1:
2304 : 31220 : break;
2305 : : }
2306 [ - + ]: 31220 : if (!ok)
2307 : : {
2308 : 0 : error("%s", errstring);
2309 : 0 : return 0;
2310 : : }
2311 [ + - ]: 31220 : if (hh < 0 ||
2312 [ + - + - ]: 31220 : mm < 0 || mm >= MINSPERHOUR ||
2313 [ + - - + ]: 31220 : ss < 0 || ss > SECSPERMIN)
2314 : : {
2315 : 0 : error("%s", errstring);
2316 : 0 : return 0;
2317 : : }
2318 : 31220 : ss += 5 + ((ss ^ 1) & (xr == '0')) <= tenths; /* Round to even. */
2319 [ - + - - ]: 31220 : if (noise && (hh > HOURSPERDAY ||
2320 [ # # # # : 0 : (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
# # ]
2321 : 0 : warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
2322 : 31220 : return oadd(omul(hh, sign * SECSPERHOUR),
2323 : 31220 : sign * (mm * SECSPERMIN + ss));
2324 : : }
2325 : :
2326 : : static zic_t
2327 : 13136 : getsave(char *field, bool *isdst)
2328 : : {
2329 : 13136 : int dst = -1;
2330 : : zic_t save;
2331 : 13136 : ptrdiff_t fieldlen = strlen(field);
2332 : :
2333 [ + + ]: 13136 : if (fieldlen != 0)
2334 : : {
2335 : 8584 : char *ep = field + fieldlen - 1;
2336 : :
2337 [ - - + ]: 8584 : switch (*ep)
2338 : : {
2339 : 0 : case 'd':
2340 : 0 : dst = 1;
2341 : 0 : *ep = '\0';
2342 : 0 : break;
2343 : 0 : case 's':
2344 : 0 : dst = 0;
2345 : 0 : *ep = '\0';
2346 : 0 : break;
2347 : : }
2348 : : }
2349 : 13136 : save = gethms(field, _("invalid saved time"));
2350 [ + - ]: 13136 : *isdst = dst < 0 ? save != 0 : dst;
2351 : 13136 : return save;
2352 : : }
2353 : :
2354 : : static void
2355 : 8336 : inrule(char **fields, int nfields)
2356 : : {
2357 : : struct rule r;
2358 : :
2359 [ - + ]: 8336 : if (nfields != RULE_FIELDS)
2360 : : {
2361 : 0 : error(_("wrong number of fields on Rule line"));
2362 : 0 : return;
2363 : : }
2364 [ - + ]: 8336 : switch (*fields[RF_NAME])
2365 : : {
2366 : 0 : case '\0':
2367 : : case ' ':
2368 : : case '\f':
2369 : : case '\n':
2370 : : case '\r':
2371 : : case '\t':
2372 : : case '\v':
2373 : : case '+':
2374 : : case '-':
2375 : : case '0':
2376 : : case '1':
2377 : : case '2':
2378 : : case '3':
2379 : : case '4':
2380 : : case '5':
2381 : : case '6':
2382 : : case '7':
2383 : : case '8':
2384 : : case '9':
2385 : 0 : error(_("Invalid rule name \"%s\""), fields[RF_NAME]);
2386 : 0 : return;
2387 : : }
2388 : 8336 : r.r_filenum = filenum;
2389 : 8336 : r.r_linenum = linenum;
2390 : 8336 : r.r_save = getsave(fields[RF_SAVE], &r.r_isdst);
2391 [ - + ]: 8336 : if (!rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR],
2392 : 8336 : fields[RF_COMMAND], fields[RF_MONTH], fields[RF_DAY],
2393 : 8336 : fields[RF_TOD]))
2394 : 0 : return;
2395 : 8336 : r.r_name = xstrdup(fields[RF_NAME]);
2396 : 8336 : r.r_abbrvar = xstrdup(fields[RF_ABBRVAR]);
2397 [ - + ]: 8336 : if (max_abbrvar_len < strlen(r.r_abbrvar))
2398 : 0 : max_abbrvar_len = strlen(r.r_abbrvar);
2399 : 8336 : rules = growalloc(rules, sizeof *rules, nrules, &nrules_alloc);
2400 : 8336 : rules[nrules++] = r;
2401 : : }
2402 : :
2403 : : static bool
2404 : 1364 : inzone(char **fields, int nfields)
2405 : : {
2406 : : ptrdiff_t i;
2407 : :
2408 [ + - - + ]: 1364 : if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
2409 : : {
2410 : 0 : error(_("wrong number of fields on Zone line"));
2411 : 0 : return false;
2412 : : }
2413 [ - + - - ]: 1364 : if (lcltime != NULL && strcmp(fields[ZF_NAME], tzdefault) == 0)
2414 : : {
2415 : 0 : error(_("\"Zone %s\" line and -l option are mutually exclusive"),
2416 : : tzdefault);
2417 : 0 : return false;
2418 : : }
2419 [ - + - - ]: 1364 : if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
2420 : : {
2421 : 0 : error(_("\"Zone %s\" line and -p option are mutually exclusive"),
2422 : : TZDEFRULES);
2423 : 0 : return false;
2424 : : }
2425 [ + + ]: 1422576 : for (i = 0; i < nzones; ++i)
2426 [ + + ]: 1421212 : if (zones[i].z_name != NULL &&
2427 [ - + ]: 231880 : strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
2428 : : {
2429 : 0 : error(_("duplicate zone name %s"
2430 : : " (file \"%s\", line %" PRIdMAX ")"),
2431 : 0 : fields[ZF_NAME],
2432 : 0 : filename(zones[i].z_filenum),
2433 : 0 : zones[i].z_linenum);
2434 : 0 : return false;
2435 : : }
2436 : 1364 : return inzsub(fields, nfields, false);
2437 : : }
2438 : :
2439 : : static bool
2440 : 6468 : inzcont(char **fields, int nfields)
2441 : : {
2442 [ + - - + ]: 6468 : if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
2443 : : {
2444 : 0 : error(_("wrong number of fields on Zone continuation line"));
2445 : 0 : return false;
2446 : : }
2447 : 6468 : return inzsub(fields, nfields, true);
2448 : : }
2449 : :
2450 : : static bool
2451 : 7832 : inzsub(char **fields, int nfields, bool iscont)
2452 : : {
2453 : : char *cp;
2454 : : char *cp1;
2455 : : struct zone z;
2456 : : int format_len;
2457 : : int i_stdoff,
2458 : : i_rule,
2459 : : i_format;
2460 : : int i_untilyear,
2461 : : i_untilmonth;
2462 : : int i_untilday,
2463 : : i_untiltime;
2464 : : bool hasuntil;
2465 : :
2466 [ + + ]: 7832 : if (iscont)
2467 : : {
2468 : 6468 : i_stdoff = ZFC_STDOFF;
2469 : 6468 : i_rule = ZFC_RULE;
2470 : 6468 : i_format = ZFC_FORMAT;
2471 : 6468 : i_untilyear = ZFC_TILYEAR;
2472 : 6468 : i_untilmonth = ZFC_TILMONTH;
2473 : 6468 : i_untilday = ZFC_TILDAY;
2474 : 6468 : i_untiltime = ZFC_TILTIME;
2475 : : }
2476 [ - + ]: 1364 : else if (!namecheck(fields[ZF_NAME]))
2477 : 0 : return false;
2478 : : else
2479 : : {
2480 : 1364 : i_stdoff = ZF_STDOFF;
2481 : 1364 : i_rule = ZF_RULE;
2482 : 1364 : i_format = ZF_FORMAT;
2483 : 1364 : i_untilyear = ZF_TILYEAR;
2484 : 1364 : i_untilmonth = ZF_TILMONTH;
2485 : 1364 : i_untilday = ZF_TILDAY;
2486 : 1364 : i_untiltime = ZF_TILTIME;
2487 : : }
2488 : 7832 : z.z_filenum = filenum;
2489 : 7832 : z.z_linenum = linenum;
2490 : 7832 : z.z_stdoff = gethms(fields[i_stdoff], _("invalid UT offset"));
2491 : 7832 : cp = strchr(fields[i_format], '%');
2492 [ + + ]: 7832 : if (cp)
2493 : : {
2494 [ + + + - : 4860 : if ((*++cp != 's' && *cp != 'z') || strchr(cp, '%')
+ - ]
2495 [ - + ]: 4860 : || strchr(fields[i_format], '/'))
2496 : : {
2497 : 0 : error(_("invalid abbreviation format"));
2498 : 0 : return false;
2499 : : }
2500 : : }
2501 [ + + ]: 7832 : z.z_format_specifier = cp ? *cp : '\0';
2502 : 7832 : format_len = strlen(fields[i_format]);
2503 [ + + ]: 7832 : if (max_format_len < format_len)
2504 : 12 : max_format_len = format_len;
2505 : 7832 : hasuntil = nfields > i_untilyear;
2506 [ + + ]: 7832 : if (hasuntil)
2507 : : {
2508 : 6468 : z.z_untilrule.r_filenum = filenum;
2509 : 6468 : z.z_untilrule.r_linenum = linenum;
2510 [ + + + + : 18960 : if (!rulesub(
+ + - + ]
2511 : : &z.z_untilrule,
2512 : 6468 : fields[i_untilyear],
2513 : : "only",
2514 : : "",
2515 : : (nfields > i_untilmonth) ?
2516 : 4952 : fields[i_untilmonth] : "Jan",
2517 : 3988 : (nfields > i_untilday) ? fields[i_untilday] : "1",
2518 : 2036 : (nfields > i_untiltime) ? fields[i_untiltime] : "0"))
2519 : 0 : return false;
2520 : 6468 : z.z_untiltime = rpytime(&z.z_untilrule,
2521 : : z.z_untilrule.r_loyear);
2522 [ + + + - ]: 6468 : if (iscont && nzones > 0 &&
2523 [ - + ]: 5220 : zones[nzones - 1].z_untiltime >= z.z_untiltime)
2524 : : {
2525 : 0 : error(_("Zone continuation line end time is"
2526 : : " not after end time of previous line"));
2527 : 0 : return false;
2528 : : }
2529 : : }
2530 [ + + ]: 7832 : z.z_name = iscont ? NULL : xstrdup(fields[ZF_NAME]);
2531 : 7832 : z.z_rule = xstrdup(fields[i_rule]);
2532 : 7832 : z.z_format = cp1 = xstrdup(fields[i_format]);
2533 [ + + ]: 7832 : if (z.z_format_specifier == 'z')
2534 : : {
2535 : 3076 : cp1[cp - fields[i_format]] = 's';
2536 [ - + ]: 3076 : if (noise)
2537 : 0 : warning(_("format '%s' not handled by pre-2015 versions of zic"),
2538 : 0 : fields[i_format]);
2539 : : }
2540 : 7832 : zones = growalloc(zones, sizeof *zones, nzones, &nzones_alloc);
2541 : 7832 : zones[nzones++] = z;
2542 : :
2543 : : /*
2544 : : * If there was an UNTIL field on this line, there's more information
2545 : : * about the zone on the next line.
2546 : : */
2547 : 7832 : return hasuntil;
2548 : : }
2549 : :
2550 : : static zic_t
2551 : 0 : getleapdatetime(char **fields, bool expire_line)
2552 : : {
2553 : : const char *cp;
2554 : : const struct lookup *lp;
2555 : : zic_t i,
2556 : : j;
2557 : : zic_t year;
2558 : : int month,
2559 : : day;
2560 : : zic_t dayoff,
2561 : : tod;
2562 : : zic_t t;
2563 : : char xs;
2564 : :
2565 : 0 : dayoff = 0;
2566 : 0 : cp = fields[LP_YEAR];
2567 [ # # ]: 0 : if (sscanf(cp, "%" SCNdZIC "%c", &year, &xs) != 1)
2568 : : {
2569 : : /*
2570 : : * Leapin' Lizards!
2571 : : */
2572 : 0 : error(_("invalid leaping year"));
2573 : 0 : return -1;
2574 : : }
2575 [ # # ]: 0 : if (!expire_line)
2576 : : {
2577 [ # # # # ]: 0 : if (!leapseen || leapmaxyear < year)
2578 : 0 : leapmaxyear = year;
2579 [ # # # # ]: 0 : if (!leapseen || leapminyear > year)
2580 : 0 : leapminyear = year;
2581 : 0 : leapseen = true;
2582 : : }
2583 : 0 : j = EPOCH_YEAR;
2584 [ # # ]: 0 : while (j != year)
2585 : : {
2586 [ # # ]: 0 : if (year > j)
2587 : : {
2588 [ # # # # : 0 : i = len_years[isleap(j)];
# # ]
2589 : 0 : ++j;
2590 : : }
2591 : : else
2592 : : {
2593 : 0 : --j;
2594 [ # # # # : 0 : i = -len_years[isleap(j)];
# # ]
2595 : : }
2596 : 0 : dayoff = oadd(dayoff, i);
2597 : : }
2598 [ # # ]: 0 : if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
2599 : : {
2600 : 0 : error(_("invalid month name"));
2601 : 0 : return -1;
2602 : : }
2603 : 0 : month = lp->l_value;
2604 : 0 : j = TM_JANUARY;
2605 [ # # ]: 0 : while (j != month)
2606 : : {
2607 [ # # # # : 0 : i = len_months[isleap(year)][j];
# # ]
2608 : 0 : dayoff = oadd(dayoff, i);
2609 : 0 : ++j;
2610 : : }
2611 : 0 : cp = fields[LP_DAY];
2612 [ # # ]: 0 : if (sscanf(cp, "%d%c", &day, &xs) != 1 ||
2613 [ # # # # : 0 : day <= 0 || day > len_months[isleap(year)][month])
# # # # #
# ]
2614 : : {
2615 : 0 : error(_("invalid day of month"));
2616 : 0 : return -1;
2617 : : }
2618 : 0 : dayoff = oadd(dayoff, day - 1);
2619 : 0 : t = omul(dayoff, SECSPERDAY);
2620 : 0 : tod = gethms(fields[LP_TIME], _("invalid time of day"));
2621 : 0 : t = tadd(t, tod);
2622 [ # # ]: 0 : if (t < 0)
2623 : 0 : error(_("leap second precedes Epoch"));
2624 : 0 : return t;
2625 : : }
2626 : :
2627 : : static void
2628 : 0 : inleap(char **fields, int nfields)
2629 : : {
2630 [ # # ]: 0 : if (nfields != LEAP_FIELDS)
2631 : 0 : error(_("wrong number of fields on Leap line"));
2632 : : else
2633 : : {
2634 : 0 : zic_t t = getleapdatetime(fields, false);
2635 : :
2636 [ # # ]: 0 : if (0 <= t)
2637 : : {
2638 : 0 : struct lookup const *lp = byword(fields[LP_ROLL], leap_types);
2639 : :
2640 [ # # ]: 0 : if (!lp)
2641 : 0 : error(_("invalid Rolling/Stationary field on Leap line"));
2642 : : else
2643 : : {
2644 : 0 : int correction = 0;
2645 : :
2646 [ # # ]: 0 : if (!fields[LP_CORR][0]) /* infile() turns "-" into "". */
2647 : 0 : correction = -1;
2648 [ # # ]: 0 : else if (strcmp(fields[LP_CORR], "+") == 0)
2649 : 0 : correction = 1;
2650 : : else
2651 : 0 : error(_("invalid CORRECTION field on Leap line"));
2652 [ # # ]: 0 : if (correction)
2653 : 0 : leapadd(t, correction, lp->l_value);
2654 : : }
2655 : : }
2656 : : }
2657 : 0 : }
2658 : :
2659 : : static void
2660 : 0 : inexpires(char **fields, int nfields)
2661 : : {
2662 [ # # ]: 0 : if (nfields != EXPIRES_FIELDS)
2663 : 0 : error(_("wrong number of fields on Expires line"));
2664 [ # # ]: 0 : else if (0 <= leapexpires)
2665 : 0 : error(_("multiple Expires lines"));
2666 : : else
2667 : 0 : leapexpires = getleapdatetime(fields, true);
2668 : 0 : }
2669 : :
2670 : : static void
2671 : 1028 : inlink(char **fields, int nfields)
2672 : : {
2673 : : struct link l;
2674 : :
2675 [ - + ]: 1028 : if (nfields != LINK_FIELDS)
2676 : : {
2677 : 0 : error(_("wrong number of fields on Link line"));
2678 : 0 : return;
2679 : : }
2680 [ - + ]: 1028 : if (*fields[LF_TARGET] == '\0')
2681 : : {
2682 : 0 : error(_("blank TARGET field on Link line"));
2683 : 0 : return;
2684 : : }
2685 [ - + ]: 1028 : if (!namecheck(fields[LF_LINKNAME]))
2686 : 0 : return;
2687 : 1028 : l.l_filenum = filenum;
2688 : 1028 : l.l_linenum = linenum;
2689 : 1028 : l.l_target = xstrdup(fields[LF_TARGET]);
2690 : 1028 : l.l_linkname = xstrdup(fields[LF_LINKNAME]);
2691 : 1028 : links = growalloc(links, sizeof *links, nlinks, &nlinks_alloc);
2692 : 1028 : links[nlinks++] = l;
2693 : : }
2694 : :
2695 : : static bool
2696 : 14804 : rulesub(struct rule *rp, const char *loyearp, const char *hiyearp,
2697 : : const char *typep, const char *monthp, const char *dayp,
2698 : : const char *timep)
2699 : : {
2700 : : const struct lookup *lp;
2701 : : const char *cp;
2702 : : char *dp;
2703 : : char *ep;
2704 : : char xs;
2705 : :
2706 [ - + ]: 14804 : if ((lp = byword(monthp, mon_names)) == NULL)
2707 : : {
2708 : 0 : error(_("invalid month name"));
2709 : 0 : return false;
2710 : : }
2711 : 14804 : rp->r_month = lp->l_value;
2712 : 14804 : rp->r_todisstd = false;
2713 : 14804 : rp->r_todisut = false;
2714 : 14804 : dp = xstrdup(timep);
2715 [ + - ]: 14804 : if (*dp != '\0')
2716 : : {
2717 : 14804 : ep = dp + strlen(dp) - 1;
2718 [ + - + + ]: 14804 : switch (lowerit(*ep))
2719 : : {
2720 : 2740 : case 's': /* Standard */
2721 : 2740 : rp->r_todisstd = true;
2722 : 2740 : rp->r_todisut = false;
2723 : 2740 : *ep = '\0';
2724 : 2740 : break;
2725 : 0 : case 'w': /* Wall */
2726 : 0 : rp->r_todisstd = false;
2727 : 0 : rp->r_todisut = false;
2728 : 0 : *ep = '\0';
2729 : 0 : break;
2730 : 676 : case 'g': /* Greenwich */
2731 : : case 'u': /* Universal */
2732 : : case 'z': /* Zulu */
2733 : 676 : rp->r_todisstd = true;
2734 : 676 : rp->r_todisut = true;
2735 : 676 : *ep = '\0';
2736 : 676 : break;
2737 : : }
2738 : : }
2739 : 14804 : rp->r_tod = gethms(dp, _("invalid time of day"));
2740 : 14804 : free(dp);
2741 : :
2742 : : /*
2743 : : * Year work.
2744 : : */
2745 : 14804 : cp = loyearp;
2746 : 14804 : lp = byword(cp, begin_years);
2747 [ - + ]: 14804 : if (lp)
2748 [ # # ]: 0 : switch (lp->l_value)
2749 : : {
2750 : 0 : case YR_MINIMUM:
2751 : 0 : warning(_("FROM year \"%s\" is obsolete;"
2752 : : " treated as %d"),
2753 : : cp, YEAR_32BIT_MIN - 1);
2754 : 0 : rp->r_loyear = YEAR_32BIT_MIN - 1;
2755 : 0 : break;
2756 : 0 : default:
2757 : 0 : unreachable();
2758 : : }
2759 [ - + ]: 14804 : else if (sscanf(cp, "%" SCNdZIC "%c", &rp->r_loyear, &xs) != 1)
2760 : : {
2761 : 0 : error(_("invalid starting year"));
2762 : 0 : return false;
2763 : : }
2764 : 14804 : cp = hiyearp;
2765 : 14804 : lp = byword(cp, end_years);
2766 : 14804 : rp->r_hiwasnum = lp == NULL;
2767 [ + + ]: 14804 : if (!rp->r_hiwasnum)
2768 [ + + - ]: 12344 : switch (lp->l_value)
2769 : : {
2770 : 184 : case YR_MAXIMUM:
2771 : 184 : rp->r_hiyear = ZIC_MAX;
2772 : 184 : break;
2773 : 12160 : case YR_ONLY:
2774 : 12160 : rp->r_hiyear = rp->r_loyear;
2775 : 12160 : break;
2776 : 0 : default:
2777 : 0 : unreachable();
2778 : : }
2779 [ - + ]: 2460 : else if (sscanf(cp, "%" SCNdZIC "%c", &rp->r_hiyear, &xs) != 1)
2780 : : {
2781 : 0 : error(_("invalid ending year"));
2782 : 0 : return false;
2783 : : }
2784 [ - + ]: 14804 : if (rp->r_loyear > rp->r_hiyear)
2785 : : {
2786 : 0 : error(_("starting year greater than ending year"));
2787 : 0 : return false;
2788 : : }
2789 [ - + ]: 14804 : if (*typep != '\0')
2790 : : {
2791 : 0 : error(_("year type \"%s\" is unsupported; use \"-\" instead"),
2792 : : typep);
2793 : 0 : return false;
2794 : : }
2795 : :
2796 : : /*
2797 : : * Day work. Accept things such as: 1 lastSunday last-Sunday
2798 : : * (undocumented; warn about this) Sun<=20 Sun>=7
2799 : : */
2800 : 14804 : dp = xstrdup(dayp);
2801 [ + + ]: 14804 : if ((lp = byword(dp, lasts)) != NULL)
2802 : : {
2803 : 1368 : rp->r_dycode = DC_DOWLEQ;
2804 : 1368 : rp->r_wday = lp->l_value;
2805 : 1368 : rp->r_dayofmonth = len_months[1][rp->r_month];
2806 : : }
2807 : : else
2808 : : {
2809 : 13436 : ep = strchr(dp, '<');
2810 [ + + ]: 13436 : if (ep)
2811 : 40 : rp->r_dycode = DC_DOWLEQ;
2812 : : else
2813 : : {
2814 : 13396 : ep = strchr(dp, '>');
2815 [ + + ]: 13396 : if (ep)
2816 : 1584 : rp->r_dycode = DC_DOWGEQ;
2817 : : else
2818 : : {
2819 : 11812 : ep = dp;
2820 : 11812 : rp->r_dycode = DC_DOM;
2821 : : }
2822 : : }
2823 [ + + ]: 13436 : if (rp->r_dycode != DC_DOM)
2824 : : {
2825 : 1624 : *ep++ = 0;
2826 [ - + ]: 1624 : if (*ep++ != '=')
2827 : : {
2828 : 0 : error(_("invalid day of month"));
2829 : 0 : free(dp);
2830 : 0 : return false;
2831 : : }
2832 [ - + ]: 1624 : if ((lp = byword(dp, wday_names)) == NULL)
2833 : : {
2834 : 0 : error(_("invalid weekday name"));
2835 : 0 : free(dp);
2836 : 0 : return false;
2837 : : }
2838 : 1624 : rp->r_wday = lp->l_value;
2839 : : }
2840 [ + - ]: 13436 : if (sscanf(ep, "%d%c", &rp->r_dayofmonth, &xs) != 1 ||
2841 [ + - ]: 13436 : rp->r_dayofmonth <= 0 ||
2842 [ - + ]: 13436 : (rp->r_dayofmonth > len_months[1][rp->r_month]))
2843 : : {
2844 : 0 : error(_("invalid day of month"));
2845 : 0 : free(dp);
2846 : 0 : return false;
2847 : : }
2848 : : }
2849 : 14804 : free(dp);
2850 : 14804 : return true;
2851 : : }
2852 : :
2853 : : static void
2854 : 24084 : convert(uint_fast32_t val, char *buf)
2855 : : {
2856 : : int i;
2857 : : int shift;
2858 : 24084 : unsigned char *const b = (unsigned char *) buf;
2859 : :
2860 [ + + ]: 120420 : for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
2861 : 96336 : b[i] = (val >> shift) & 0xff;
2862 : 24084 : }
2863 : :
2864 : : static void
2865 : 67368 : convert64(uint_fast64_t val, char *buf)
2866 : : {
2867 : : int i;
2868 : : int shift;
2869 : 67368 : unsigned char *const b = (unsigned char *) buf;
2870 : :
2871 [ + + ]: 606312 : for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
2872 : 538944 : b[i] = (val >> shift) & 0xff;
2873 : 67368 : }
2874 : :
2875 : : static void
2876 : 7716 : puttzcode(zic_t val, FILE *fp)
2877 : : {
2878 : : char buf[4];
2879 : :
2880 : 7716 : convert(val, buf);
2881 : 7716 : fwrite(buf, sizeof buf, 1, fp);
2882 : 7716 : }
2883 : :
2884 : : static void
2885 : 67368 : puttzcodepass(zic_t val, FILE *fp, int pass)
2886 : : {
2887 [ - + ]: 67368 : if (pass == 1)
2888 : 0 : puttzcode(val, fp);
2889 : : else
2890 : : {
2891 : : char buf[8];
2892 : :
2893 : 67368 : convert64(val, buf);
2894 : 67368 : fwrite(buf, sizeof buf, 1, fp);
2895 : : }
2896 : 67368 : }
2897 : :
2898 : : static int
2899 : 831588 : atcomp(const void *avp, const void *bvp)
2900 : : {
2901 : 831588 : struct attype const *ap = avp,
2902 : 831588 : *bp = bvp;
2903 : 831588 : zic_t a = ap->at,
2904 : 831588 : b = bp->at;
2905 : :
2906 [ + + ]: 831588 : return a < b ? -1 : a > b;
2907 : : }
2908 : :
2909 : : struct timerange
2910 : : {
2911 : : int defaulttype;
2912 : : ptrdiff_t base,
2913 : : count;
2914 : : ptrdiff_t leapbase,
2915 : : leapcount;
2916 : : bool leapexpiry;
2917 : : };
2918 : :
2919 : : static struct timerange
2920 : 2728 : limitrange(struct timerange r, zic_t lo, zic_t hi,
2921 : : zic_t const *ats, unsigned char const *types)
2922 : : {
2923 : : /* Omit ordinary transitions < LO. */
2924 [ + + + + ]: 3468 : while (0 < r.count && ats[r.base] < lo)
2925 : : {
2926 : 740 : r.defaulttype = types[r.base];
2927 : 740 : r.count--;
2928 : 740 : r.base++;
2929 : : }
2930 : :
2931 : : /*
2932 : : * Omit as many initial leap seconds as possible, such that the first leap
2933 : : * second in the truncated list is <= LO, and is a positive leap second if
2934 : : * and only if it has a positive correction. This supports common TZif
2935 : : * readers that assume that the first leap second is positive if and only
2936 : : * if its correction is positive.
2937 : : */
2938 [ - + - - ]: 2728 : while (1 < r.leapcount && leap[r.leapbase + 1].trans <= lo)
2939 : : {
2940 : 0 : r.leapcount--;
2941 : 0 : r.leapbase++;
2942 : : }
2943 : 2728 : while (0 < r.leapbase
2944 [ - + ]: 2728 : && ((leap[r.leapbase - 1].corr < leap[r.leapbase].corr)
2945 [ # # ]: 0 : != (0 < leap[r.leapbase].corr)))
2946 : : {
2947 : 0 : r.leapcount++;
2948 : 0 : r.leapbase--;
2949 : : }
2950 : :
2951 : :
2952 : : /* Omit ordinary and leap second transitions greater than HI + 1. */
2953 [ + + ]: 2728 : if (hi < max_time)
2954 : : {
2955 [ + + + + ]: 3444 : while (0 < r.count && hi + 1 < ats[r.base + r.count - 1])
2956 : 2080 : r.count--;
2957 [ - + - - ]: 1364 : while (0 < r.leapcount && hi + 1 < leap[r.leapbase + r.leapcount - 1].trans)
2958 : 0 : r.leapcount--;
2959 : : }
2960 : :
2961 : : /* Determine whether to append an expiration to the leap second table. */
2962 [ - + - - ]: 2728 : r.leapexpiry = 0 <= leapexpires && leapexpires - 1 <= hi;
2963 : :
2964 : 2728 : return r;
2965 : : }
2966 : :
2967 : : static void
2968 : 1364 : writezone(const char *const name, const char *const string, char version,
2969 : : int defaulttype)
2970 : : {
2971 : : FILE *fp;
2972 : : ptrdiff_t i,
2973 : : j;
2974 : : int pass;
2975 : 1364 : char *tempname = NULL;
2976 : 1364 : char const *outname = name;
2977 : :
2978 : : /*
2979 : : * Allocate the ATS and TYPES arrays via a single malloc, as this is a bit
2980 : : * faster. Do not malloc(0) if !timecnt, as that might return NULL even
2981 : : * on success.
2982 : : */
2983 : 1364 : zic_t *ats = xmalloc(align_to(size_product(timecnt + !timecnt,
2984 : : sizeof *ats + 1),
2985 : : alignof(zic_t)));
2986 : 1364 : void *typesptr = ats + timecnt;
2987 : 1364 : unsigned char *types = typesptr;
2988 : 1364 : struct timerange rangeall = {0}, range32, range64;
2989 : :
2990 : : /*
2991 : : * Sort.
2992 : : */
2993 [ + + ]: 1364 : if (timecnt > 1)
2994 : 1196 : qsort(attypes, timecnt, sizeof *attypes, atcomp);
2995 : :
2996 : : /*
2997 : : * Optimize and skip unwanted transitions.
2998 : : */
2999 : : {
3000 : : ptrdiff_t fromi,
3001 : : toi;
3002 : :
3003 : 1364 : toi = 0;
3004 : 1364 : fromi = 0;
3005 [ + + ]: 70236 : for (; fromi < timecnt; ++fromi)
3006 : : {
3007 [ + + ]: 68872 : if (toi != 0)
3008 : : {
3009 : : /*
3010 : : * Skip the previous transition if it is unwanted because its
3011 : : * local time is not earlier. The UT offset additions can't
3012 : : * overflow because of how the times were calculated.
3013 : : */
3014 [ + + ]: 67624 : unsigned char type_2 =
3015 : 66416 : toi == 1 ? 0 : attypes[toi - 2].type;
3016 : :
3017 : 67988 : if ((attypes[fromi].at
3018 : 67624 : + utoffs[attypes[toi - 1].type])
3019 [ + + ]: 67624 : <= attypes[toi - 1].at + utoffs[type_2])
3020 : : {
3021 [ + + ]: 364 : if (attypes[fromi].type == type_2)
3022 : 4 : toi--;
3023 : : else
3024 : 360 : attypes[toi - 1].type =
3025 : 360 : attypes[fromi].type;
3026 : 364 : continue;
3027 : : }
3028 : : }
3029 : :
3030 : : /*
3031 : : * Use a transition if it is the first one, or if it cannot be
3032 : : * merged for other reasons, or if it transitions to different
3033 : : * timekeeping.
3034 : : */
3035 [ + + ]: 68508 : if (toi == 0
3036 [ + + ]: 67260 : || attypes[fromi].dontmerge
3037 : 66848 : || (utoffs[attypes[toi - 1].type]
3038 [ + + ]: 66848 : != utoffs[attypes[fromi].type])
3039 : 1792 : || (isdsts[attypes[toi - 1].type]
3040 [ + + ]: 1792 : != isdsts[attypes[fromi].type])
3041 : 1400 : || (desigidx[attypes[toi - 1].type]
3042 [ + + ]: 1400 : != desigidx[attypes[fromi].type]))
3043 : 67372 : attypes[toi++] = attypes[fromi];
3044 : : }
3045 : 1364 : timecnt = toi;
3046 : : }
3047 : :
3048 [ - + ]: 1364 : if (noise)
3049 : : {
3050 [ # # ]: 0 : if (1200 < timecnt)
3051 : : {
3052 [ # # ]: 0 : if (TZ_MAX_TIMES < timecnt)
3053 : 0 : warning(_("reference clients mishandle"
3054 : : " more than %d transition times"),
3055 : : TZ_MAX_TIMES);
3056 : : else
3057 : 0 : warning(_("pre-2014 clients may mishandle"
3058 : : " more than 1200 transition times"));
3059 : : }
3060 [ # # ]: 0 : if (TZ_MAX_LEAPS < leapcnt)
3061 : 0 : warning(_("reference clients mishandle more than %d leap seconds"),
3062 : : TZ_MAX_LEAPS);
3063 : : }
3064 : :
3065 : : /*
3066 : : * Transfer.
3067 : : */
3068 [ + + ]: 68732 : for (i = 0; i < timecnt; ++i)
3069 : : {
3070 : 67368 : ats[i] = attypes[i].at;
3071 : 67368 : types[i] = attypes[i].type;
3072 : : }
3073 : :
3074 : : /*
3075 : : * Correct for leap seconds.
3076 : : */
3077 [ + + ]: 68732 : for (i = 0; i < timecnt; ++i)
3078 : : {
3079 : 67368 : j = leapcnt;
3080 [ - + ]: 67368 : while (--j >= 0)
3081 [ # # ]: 0 : if (leap[j].trans - leap[j].corr < ats[i])
3082 : : {
3083 : 0 : ats[i] = tadd(ats[i], leap[j].corr);
3084 : 0 : break;
3085 : : }
3086 : : }
3087 : :
3088 : 1364 : rangeall.defaulttype = defaulttype;
3089 : 1364 : rangeall.count = timecnt;
3090 : 1364 : rangeall.leapcount = leapcnt;
3091 : 1364 : range64 = limitrange(rangeall, lo_time,
3092 : 1364 : max(hi_time,
3093 : : redundant_time - (ZIC_MIN < redundant_time)),
3094 : : ats, types);
3095 : 1364 : range32 = limitrange(range64, ZIC32_MIN, ZIC32_MAX, ats, types);
3096 : :
3097 : : /*
3098 : : * TZif version 4 is needed if a no-op transition is appended to indicate
3099 : : * the expiration of the leap second table, or if the first leap second
3100 : : * transition is not to a +1 or -1 correction.
3101 : : */
3102 [ + + ]: 4092 : for (pass = 1; pass <= 2; pass++)
3103 : : {
3104 [ + + ]: 2728 : struct timerange const *r = pass == 1 ? &range32 : &range64;
3105 : :
3106 [ + + + - ]: 2728 : if (pass == 1 && !want_bloat())
3107 : 1364 : continue;
3108 [ - + ]: 1364 : if (r->leapexpiry)
3109 : : {
3110 [ # # ]: 0 : if (noise)
3111 : 0 : warning(_("%s: pre-2021b clients may mishandle"
3112 : : " leap second expiry"),
3113 : : name);
3114 : 0 : version = '4';
3115 : : }
3116 [ - + ]: 1364 : if (0 < r->leapcount
3117 [ # # # # ]: 0 : && leap[r->leapbase].corr != 1 && leap[r->leapbase].corr != -1)
3118 : : {
3119 [ # # ]: 0 : if (noise)
3120 : 0 : warning(_("%s: pre-2021b clients may mishandle"
3121 : : " leap second table truncation"),
3122 : : name);
3123 : 0 : version = '4';
3124 : : }
3125 [ - + ]: 1364 : if (version == '4')
3126 : 0 : break;
3127 : : }
3128 : :
3129 : 1364 : fp = open_outfile(&outname, &tempname);
3130 : :
3131 [ + + ]: 4092 : for (pass = 1; pass <= 2; ++pass)
3132 : : {
3133 : : ptrdiff_t thistimei,
3134 : : thistimecnt,
3135 : : thistimelim;
3136 : : ptrdiff_t thisleapi,
3137 : : thisleapcnt,
3138 : : thisleaplim;
3139 : : struct tzhead tzh;
3140 : 2728 : int pretranstype = -1,
3141 : : thisdefaulttype;
3142 : : bool locut,
3143 : : hicut,
3144 : : thisleapexpiry;
3145 : : zic_t lo,
3146 : : thismin,
3147 : : thismax;
3148 : : int old0;
3149 : : char omittype[TZ_MAX_TYPES];
3150 : : int typemap[TZ_MAX_TYPES];
3151 : : int thistypecnt,
3152 : : stdcnt,
3153 : : utcnt;
3154 : : char thischars[TZ_MAX_CHARS];
3155 : : int thischarcnt;
3156 : : bool toomanytimes;
3157 : : int indmap[TZ_MAX_CHARS];
3158 : :
3159 [ + + ]: 2728 : if (pass == 1)
3160 : : {
3161 : 1364 : thisdefaulttype = range32.defaulttype;
3162 : 1364 : thistimei = range32.base;
3163 : 1364 : thistimecnt = range32.count;
3164 : 1364 : toomanytimes = thistimecnt >> 31 >> 1 != 0;
3165 : 1364 : thisleapi = range32.leapbase;
3166 : 1364 : thisleapcnt = range32.leapcount;
3167 : 1364 : thisleapexpiry = range32.leapexpiry;
3168 : 1364 : thismin = ZIC32_MIN;
3169 : 1364 : thismax = ZIC32_MAX;
3170 : : }
3171 : : else
3172 : : {
3173 : 1364 : thisdefaulttype = range64.defaulttype;
3174 : 1364 : thistimei = range64.base;
3175 : 1364 : thistimecnt = range64.count;
3176 : 1364 : toomanytimes = thistimecnt >> 31 >> 31 >> 2 != 0;
3177 : 1364 : thisleapi = range64.leapbase;
3178 : 1364 : thisleapcnt = range64.leapcount;
3179 : 1364 : thisleapexpiry = range64.leapexpiry;
3180 : 1364 : thismin = min_time;
3181 : 1364 : thismax = max_time;
3182 : : }
3183 [ - + ]: 2728 : if (toomanytimes)
3184 : 0 : error(_("too many transition times"));
3185 : :
3186 [ - + - - ]: 2728 : locut = thismin < lo_time && lo_time <= thismax;
3187 [ + - - + ]: 2728 : hicut = thismin <= hi_time && hi_time < thismax;
3188 : 2728 : thistimelim = thistimei + thistimecnt;
3189 : 2728 : memset(omittype, true, typecnt);
3190 : :
3191 : : /*
3192 : : * Determine whether to output a transition before the first
3193 : : * transition in range. This is needed when the output is truncated
3194 : : * at the start, and is also useful when catering to buggy 32-bit
3195 : : * clients that do not use time type 0 for timestamps before the first
3196 : : * transition.
3197 : : */
3198 [ + - + + : 2728 : if ((locut || (pass == 1 && thistimei))
+ + ]
3199 [ + + + - ]: 648 : && !(thistimecnt && ats[thistimei] == lo_time))
3200 : : {
3201 : 648 : pretranstype = thisdefaulttype;
3202 : 648 : omittype[pretranstype] = false;
3203 : : }
3204 : :
3205 : : /*
3206 : : * Arguably the default time type in the 32-bit data should be
3207 : : * range32.defaulttype, which is suited for timestamps just before
3208 : : * ZIC32_MIN. However, zic traditionally used the time type of the
3209 : : * indefinite past instead. Internet RFC 8532 says readers should
3210 : : * ignore 32-bit data, so this discrepancy matters only to obsolete
3211 : : * readers where the traditional type might be more appropriate even
3212 : : * if it's "wrong". So, use the historical zic value, unless -r
3213 : : * specifies a low cutoff that excludes some 32-bit timestamps.
3214 : : */
3215 [ + + + - ]: 2728 : if (pass == 1 && lo_time <= thismin)
3216 : 1364 : thisdefaulttype = range64.defaulttype;
3217 : :
3218 [ - + ]: 2728 : if (locut)
3219 : 0 : thisdefaulttype = unspecifiedtype;
3220 : 2728 : omittype[thisdefaulttype] = false;
3221 [ + + ]: 134644 : for (i = thistimei; i < thistimelim; i++)
3222 : 131916 : omittype[types[i]] = false;
3223 [ - + ]: 2728 : if (hicut)
3224 : 0 : omittype[unspecifiedtype] = false;
3225 : :
3226 : : /*
3227 : : * Reorder types to make THISDEFAULTTYPE type 0. Use TYPEMAP to swap
3228 : : * OLD0 and THISDEFAULTTYPE so that THISDEFAULTTYPE appears as type 0
3229 : : * in the output instead of OLD0. TYPEMAP also omits unused types.
3230 : : */
3231 : 2728 : old0 = strlen(omittype);
3232 : :
3233 : : #ifndef LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH
3234 : :
3235 : : /*
3236 : : * For some pre-2011 systems: if the last-to-be-written standard (or
3237 : : * daylight) type has an offset different from the most recently used
3238 : : * offset, append an (unused) copy of the most recently used type (to
3239 : : * help get global "altzone" and "timezone" variables set correctly).
3240 : : */
3241 [ - + ]: 2728 : if (want_bloat())
3242 : : {
3243 : : int mrudst,
3244 : : mrustd,
3245 : : hidst,
3246 : : histd,
3247 : : type;
3248 : :
3249 : 0 : hidst = histd = mrudst = mrustd = -1;
3250 [ # # ]: 0 : if (0 <= pretranstype)
3251 : : {
3252 [ # # ]: 0 : if (isdsts[pretranstype])
3253 : 0 : mrudst = pretranstype;
3254 : : else
3255 : 0 : mrustd = pretranstype;
3256 : : }
3257 [ # # ]: 0 : for (i = thistimei; i < thistimelim; i++)
3258 [ # # ]: 0 : if (isdsts[types[i]])
3259 : 0 : mrudst = types[i];
3260 : : else
3261 : 0 : mrustd = types[i];
3262 [ # # ]: 0 : for (i = old0; i < typecnt; i++)
3263 : : {
3264 [ # # ]: 0 : int h = (i == old0 ? thisdefaulttype
3265 [ # # ]: 0 : : i == thisdefaulttype ? old0 : i);
3266 : :
3267 [ # # ]: 0 : if (!omittype[h])
3268 : : {
3269 [ # # ]: 0 : if (isdsts[h])
3270 : 0 : hidst = i;
3271 : : else
3272 : 0 : histd = i;
3273 : : }
3274 : : }
3275 [ # # # # : 0 : if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
# # ]
3276 [ # # ]: 0 : utoffs[hidst] != utoffs[mrudst])
3277 : : {
3278 : 0 : isdsts[mrudst] = -1;
3279 : 0 : type = addtype(utoffs[mrudst],
3280 : 0 : &chars[desigidx[mrudst]],
3281 : : true,
3282 : 0 : ttisstds[mrudst],
3283 : 0 : ttisuts[mrudst]);
3284 : 0 : isdsts[mrudst] = 1;
3285 : 0 : omittype[type] = false;
3286 : : }
3287 [ # # # # : 0 : if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
# # ]
3288 [ # # ]: 0 : utoffs[histd] != utoffs[mrustd])
3289 : : {
3290 : 0 : isdsts[mrustd] = -1;
3291 : 0 : type = addtype(utoffs[mrustd],
3292 : 0 : &chars[desigidx[mrustd]],
3293 : : false,
3294 : 0 : ttisstds[mrustd],
3295 : 0 : ttisuts[mrustd]);
3296 : 0 : isdsts[mrustd] = 0;
3297 : 0 : omittype[type] = false;
3298 : : }
3299 : : }
3300 : : #endif /* !defined
3301 : : * LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
3302 : 2728 : thistypecnt = 0;
3303 [ + + ]: 15464 : for (i = old0; i < typecnt; i++)
3304 [ + + ]: 12736 : if (!omittype[i])
3305 : 12612 : typemap[i == old0 ? thisdefaulttype
3306 [ - + ]: 12612 : : i == thisdefaulttype ? old0 : i]
3307 [ + + ]: 25224 : = thistypecnt++;
3308 : :
3309 : 2728 : thischarcnt = stdcnt = utcnt = 0;
3310 [ + + ]: 15464 : for (i = old0; i < typecnt; i++)
3311 : : {
3312 [ + + ]: 12736 : if (omittype[i])
3313 : 124 : continue;
3314 [ - + ]: 12612 : if (ttisstds[i])
3315 : 0 : stdcnt = thistypecnt;
3316 [ - + ]: 12612 : if (ttisuts[i])
3317 : 0 : utcnt = thistypecnt;
3318 : 12612 : addabbr(thischars, &thischarcnt, &chars[desigidx[i]]);
3319 : : }
3320 : :
3321 : : /*
3322 : : * Now that all abbrevs have been added to THISCHARS, it is safe to
3323 : : * set INDMAP without worrying about whether the abbrevs might move
3324 : : * later.
3325 : : */
3326 [ + + ]: 701096 : for (i = 0; i < TZ_MAX_CHARS; i++)
3327 : 698368 : indmap[i] = -1;
3328 [ + + ]: 15464 : for (i = old0; i < typecnt; i++)
3329 [ + + + + ]: 12736 : if (!omittype[i] && indmap[desigidx[i]] < 0)
3330 : 11564 : indmap[desigidx[i]] = addabbr(thischars, &thischarcnt,
3331 : 11564 : &chars[desigidx[i]]);
3332 : :
3333 [ + + + - ]: 2728 : if (pass == 1 && !want_bloat())
3334 : : {
3335 : 1364 : hicut = thisleapexpiry = false;
3336 : 1364 : pretranstype = -1;
3337 : 1364 : thistimecnt = thisleapcnt = 0;
3338 : 1364 : thistypecnt = thischarcnt = 1;
3339 : : }
3340 : : #define DO(field) fwrite(tzh.field, sizeof tzh.field, 1, fp)
3341 : 2728 : memset(&tzh, 0, sizeof tzh);
3342 : 2728 : memcpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
3343 : 2728 : tzh.tzh_version[0] = version;
3344 : 2728 : convert(utcnt, tzh.tzh_ttisutcnt);
3345 : 2728 : convert(stdcnt, tzh.tzh_ttisstdcnt);
3346 : 2728 : convert(thisleapcnt + thisleapexpiry, tzh.tzh_leapcnt);
3347 : 2728 : convert((0 <= pretranstype) + thistimecnt + hicut,
3348 : : tzh.tzh_timecnt);
3349 : 2728 : convert(thistypecnt, tzh.tzh_typecnt);
3350 : 2728 : convert(thischarcnt, tzh.tzh_charcnt);
3351 : 2728 : DO(tzh_magic);
3352 : 2728 : DO(tzh_version);
3353 : 2728 : DO(tzh_reserved);
3354 : 2728 : DO(tzh_ttisutcnt);
3355 : 2728 : DO(tzh_ttisstdcnt);
3356 : 2728 : DO(tzh_leapcnt);
3357 : 2728 : DO(tzh_timecnt);
3358 : 2728 : DO(tzh_typecnt);
3359 : 2728 : DO(tzh_charcnt);
3360 : : #undef DO
3361 [ + + + - ]: 2728 : if (pass == 1 && !want_bloat())
3362 : : {
3363 : : /* Output a minimal data block with just one time type. */
3364 : 1364 : puttzcode(0, fp); /* utoff */
3365 : 1364 : putc(0, fp); /* dst */
3366 : 1364 : putc(0, fp); /* index of abbreviation */
3367 : 1364 : putc(0, fp); /* empty-string abbreviation */
3368 : 1364 : continue;
3369 : : }
3370 : :
3371 : : /* PG: print current timezone abbreviations if requested */
3372 [ - + - - ]: 1364 : if (print_abbrevs && pass == 2)
3373 : : {
3374 : : /* Print "type" data for periods ending after print_cutoff */
3375 [ # # ]: 0 : for (i = thistimei; i < thistimelim; ++i)
3376 : : {
3377 [ # # # # ]: 0 : if (i == thistimelim - 1 || ats[i + 1] > print_cutoff)
3378 : : {
3379 : 0 : unsigned char tm = types[i];
3380 : 0 : char *thisabbrev = &thischars[indmap[desigidx[tm]]];
3381 : :
3382 : 0 : fprintf(stdout, "%s\t%" PRIdFAST64 "%s\n",
3383 : : thisabbrev,
3384 : : utoffs[tm],
3385 [ # # ]: 0 : isdsts[tm] ? "\tD" : "");
3386 : : }
3387 : : }
3388 : : /* Print the default type if we have no transitions at all */
3389 [ # # ]: 0 : if (thistimei >= thistimelim)
3390 : : {
3391 : 0 : unsigned char tm = defaulttype;
3392 : 0 : char *thisabbrev = &thischars[indmap[desigidx[tm]]];
3393 : :
3394 : 0 : fprintf(stdout, "%s\t%" PRIdFAST64 "%s\n",
3395 : : thisabbrev,
3396 : : utoffs[tm],
3397 [ # # ]: 0 : isdsts[tm] ? "\tD" : "");
3398 : : }
3399 : : }
3400 : :
3401 [ + - - + : 1364 : if (pass == 2 && noise && 50 < thischarcnt)
- - ]
3402 : 0 : warning(_("%s: pre-2026 reference clients mishandle"
3403 : : " more than 50 bytes of abbreviations"),
3404 : : name);
3405 : :
3406 : : /*
3407 : : * Output a LO_TIME transition if needed; see limitrange. But do not
3408 : : * go below the minimum representable value for this pass.
3409 : : */
3410 [ - + - - ]: 1364 : lo = pass == 1 && lo_time < ZIC32_MIN ? ZIC32_MIN : lo_time;
3411 : :
3412 [ - + ]: 1364 : if (0 <= pretranstype)
3413 : 0 : puttzcodepass(lo, fp, pass);
3414 [ + + ]: 68732 : for (i = thistimei; i < thistimelim; ++i)
3415 : : {
3416 : 67368 : puttzcodepass(ats[i], fp, pass);
3417 : : }
3418 [ - + ]: 1364 : if (hicut)
3419 : 0 : puttzcodepass(hi_time + 1, fp, pass);
3420 [ - + ]: 1364 : if (0 <= pretranstype)
3421 : 0 : putc(typemap[pretranstype], fp);
3422 [ + + ]: 68732 : for (i = thistimei; i < thistimelim; i++)
3423 : 67368 : putc(typemap[types[i]], fp);
3424 [ - + ]: 1364 : if (hicut)
3425 : 0 : putc(typemap[unspecifiedtype], fp);
3426 : :
3427 [ + + ]: 7732 : for (i = old0; i < typecnt; i++)
3428 : : {
3429 [ + + ]: 11372 : int h = (i == old0 ? thisdefaulttype
3430 [ + - ]: 5004 : : i == thisdefaulttype ? old0 : i);
3431 : :
3432 [ + + ]: 6368 : if (!omittype[h])
3433 : : {
3434 : 6352 : puttzcode(utoffs[h], fp);
3435 : 6352 : putc(isdsts[h], fp);
3436 : 6352 : putc(indmap[desigidx[h]], fp);
3437 : : }
3438 : : }
3439 [ + - ]: 1364 : if (thischarcnt != 0)
3440 : 1364 : fwrite(thischars, sizeof thischars[0],
3441 : : thischarcnt, fp);
3442 : 1364 : thisleaplim = thisleapi + thisleapcnt;
3443 [ - + ]: 1364 : for (i = thisleapi; i < thisleaplim; ++i)
3444 : : {
3445 : : zic_t todo;
3446 : :
3447 [ # # ]: 0 : if (leap[i].roll)
3448 : : {
3449 [ # # # # ]: 0 : if (timecnt == 0 || leap[i].trans < ats[0])
3450 : : {
3451 : 0 : j = 0;
3452 [ # # ]: 0 : while (isdsts[j])
3453 [ # # ]: 0 : if (++j >= typecnt)
3454 : : {
3455 : 0 : j = 0;
3456 : 0 : break;
3457 : : }
3458 : : }
3459 : : else
3460 : : {
3461 : 0 : j = 1;
3462 [ # # ]: 0 : while (j < timecnt &&
3463 [ # # ]: 0 : ats[j] <= leap[i].trans)
3464 : 0 : ++j;
3465 : 0 : j = types[j - 1];
3466 : : }
3467 : 0 : todo = tadd(leap[i].trans, -utoffs[j]);
3468 : : }
3469 : : else
3470 : 0 : todo = leap[i].trans;
3471 : 0 : puttzcodepass(todo, fp, pass);
3472 : 0 : puttzcode(leap[i].corr, fp);
3473 : : }
3474 [ - + ]: 1364 : if (thisleapexpiry)
3475 : : {
3476 : : /*
3477 : : * Append a no-op leap correction indicating when the leap second
3478 : : * table expires. Although this does not conform to Internet RFC
3479 : : * 9636, most clients seem to accept this and the plan is to amend
3480 : : * the RFC to allow this in version 4 TZif files.
3481 : : */
3482 : 0 : puttzcodepass(leapexpires, fp, pass);
3483 [ # # ]: 0 : puttzcode(thisleaplim ? leap[thisleaplim - 1].corr : 0, fp);
3484 : : }
3485 [ - + ]: 1364 : if (stdcnt != 0)
3486 [ # # ]: 0 : for (i = old0; i < typecnt; i++)
3487 [ # # ]: 0 : if (!omittype[i])
3488 : 0 : putc(ttisstds[i], fp);
3489 [ - + ]: 1364 : if (utcnt != 0)
3490 [ # # ]: 0 : for (i = old0; i < typecnt; i++)
3491 [ # # ]: 0 : if (!omittype[i])
3492 : 0 : putc(ttisuts[i], fp);
3493 : : }
3494 : 1364 : fprintf(fp, "\n%s\n", string);
3495 : 1364 : close_file(fp, directory, name, tempname);
3496 : 1364 : rename_dest(tempname, name);
3497 : 1364 : free(ats);
3498 : 1364 : }
3499 : :
3500 : : static char const *
3501 : 55096 : abbroffset(char *buf, zic_t offset)
3502 : : {
3503 : 55096 : char sign = '+';
3504 : : int seconds,
3505 : : minutes;
3506 : :
3507 [ + + ]: 55096 : if (offset < 0)
3508 : : {
3509 : 26244 : offset = -offset;
3510 : 26244 : sign = '-';
3511 : : }
3512 : :
3513 : 55096 : seconds = offset % SECSPERMIN;
3514 : 55096 : offset /= SECSPERMIN;
3515 : 55096 : minutes = offset % MINSPERHOUR;
3516 : 55096 : offset /= MINSPERHOUR;
3517 [ - + ]: 55096 : if (100 <= offset)
3518 : : {
3519 : 0 : error(_("%%z UT offset magnitude exceeds 99:59:59"));
3520 : 0 : return "%z";
3521 : : }
3522 : : else
3523 : : {
3524 : 55096 : char *p = buf;
3525 : :
3526 : 55096 : *p++ = sign;
3527 : 55096 : *p++ = '0' + offset / 10;
3528 : 55096 : *p++ = '0' + offset % 10;
3529 [ + + ]: 55096 : if (minutes | seconds)
3530 : : {
3531 : 1620 : *p++ = '0' + minutes / 10;
3532 : 1620 : *p++ = '0' + minutes % 10;
3533 [ - + ]: 1620 : if (seconds)
3534 : : {
3535 : 0 : *p++ = '0' + seconds / 10;
3536 : 0 : *p++ = '0' + seconds % 10;
3537 : : }
3538 : : }
3539 : 55096 : *p = '\0';
3540 : 55096 : return buf;
3541 : : }
3542 : : }
3543 : :
3544 : : static char const disable_percent_s[] = "";
3545 : :
3546 : : static ptrdiff_t
3547 : 137304 : doabbr(char *abbr, struct zone const *zp, char const *letters,
3548 : : bool isdst, zic_t save, bool doquotes)
3549 : 3392 : {
3550 : : char *cp;
3551 : : ptrdiff_t len;
3552 : 137304 : char const *format = zp->z_format;
3553 : 137304 : char const *slashp = strchr(format, '/');
3554 : :
3555 [ + + ]: 137304 : if (slashp == NULL)
3556 : : {
3557 : : char letterbuf[PERCENT_Z_LEN_BOUND + 1];
3558 : :
3559 [ + + ]: 133772 : if (zp->z_format_specifier == 'z')
3560 : 55096 : letters = abbroffset(letterbuf, zp->z_stdoff + save);
3561 [ + + ]: 78676 : else if (!letters)
3562 : 3056 : letters = "%s";
3563 [ - + ]: 75620 : else if (letters == disable_percent_s)
3564 : 0 : return 0;
3565 : 133772 : sprintf(abbr, format, letters);
3566 : : }
3567 [ + + ]: 3532 : else if (isdst)
3568 : 1940 : strcpy(abbr, slashp + 1);
3569 : : else
3570 : : {
3571 : 1592 : memcpy(abbr, format, slashp - format);
3572 : 1592 : abbr[slashp - format] = '\0';
3573 : : }
3574 : 137304 : len = strlen(abbr);
3575 [ + + ]: 137304 : if (!doquotes)
3576 : 135524 : return len;
3577 [ + + ]: 5172 : for (cp = abbr; is_alpha(*cp); cp++)
3578 : 3392 : continue;
3579 [ + - + + ]: 1780 : if (len > 0 && *cp == '\0')
3580 : 1040 : return len;
3581 : 740 : abbr[len + 2] = '\0';
3582 : 740 : abbr[len + 1] = '>';
3583 : 740 : memmove(abbr + 1, abbr, len);
3584 : 740 : abbr[0] = '<';
3585 : 740 : return len + 2;
3586 : : }
3587 : :
3588 : : static void
3589 : 85408 : updateminmax(const zic_t x)
3590 : : {
3591 [ + + ]: 85408 : if (min_year > x)
3592 : 1572 : min_year = x;
3593 [ + + ]: 85408 : if (max_year < x)
3594 : 3616 : max_year = x;
3595 : 85408 : }
3596 : :
3597 : : static int
3598 : 1684 : stringoffset(char *result, zic_t offset)
3599 : : {
3600 : : int hours;
3601 : : int minutes;
3602 : : int seconds;
3603 : 1684 : bool negative = offset < 0;
3604 : 1684 : int len = negative;
3605 : :
3606 [ + + ]: 1684 : if (negative)
3607 : : {
3608 : 728 : offset = -offset;
3609 : 728 : result[0] = '-';
3610 : : }
3611 : 1684 : seconds = offset % SECSPERMIN;
3612 : 1684 : offset /= SECSPERMIN;
3613 : 1684 : minutes = offset % MINSPERHOUR;
3614 : 1684 : offset /= MINSPERHOUR;
3615 [ - + ]: 1684 : if (offset >= HOURSPERDAY * DAYSPERWEEK)
3616 : : {
3617 : 0 : result[0] = '\0';
3618 : 0 : return 0;
3619 : : }
3620 : 1684 : hours = offset;
3621 : 1684 : len += sprintf(result + len, "%d", hours);
3622 [ + + - + ]: 1684 : if (minutes != 0 || seconds != 0)
3623 : : {
3624 : 64 : len += sprintf(result + len, ":%02d", minutes);
3625 [ - + ]: 64 : if (seconds != 0)
3626 : 0 : len += sprintf(result + len, ":%02d", seconds);
3627 : : }
3628 : 1684 : return len;
3629 : : }
3630 : :
3631 : : static int
3632 : 832 : stringrule(char *result, struct rule *const rp, zic_t save, zic_t stdoff)
3633 : : {
3634 : 832 : zic_t tod = rp->r_tod;
3635 : 832 : int compat = 0;
3636 : :
3637 [ - + ]: 832 : if (rp->r_dycode == DC_DOM)
3638 : : {
3639 : : int month,
3640 : : total;
3641 : :
3642 [ # # # # ]: 0 : if (rp->r_dayofmonth == 29 && rp->r_month == TM_FEBRUARY)
3643 : 0 : return -1;
3644 : 0 : total = 0;
3645 [ # # ]: 0 : for (month = 0; month < rp->r_month; ++month)
3646 : 0 : total += len_months[0][month];
3647 : : /* Omit the "J" in Jan and Feb, as that's shorter. */
3648 [ # # ]: 0 : if (rp->r_month <= 1)
3649 : 0 : result += sprintf(result, "%d", total + rp->r_dayofmonth - 1);
3650 : : else
3651 : 0 : result += sprintf(result, "J%d", total + rp->r_dayofmonth);
3652 : : }
3653 : : else
3654 : : {
3655 : : int week;
3656 : 832 : int wday = rp->r_wday;
3657 : : int wdayoff;
3658 : :
3659 [ + + ]: 832 : if (rp->r_dycode == DC_DOWGEQ)
3660 : : {
3661 : 492 : wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
3662 [ + + ]: 492 : if (wdayoff)
3663 : 20 : compat = 2013;
3664 : 492 : wday -= wdayoff;
3665 : 492 : tod += wdayoff * SECSPERDAY;
3666 : 492 : week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
3667 : : }
3668 [ + - ]: 340 : else if (rp->r_dycode == DC_DOWLEQ)
3669 : : {
3670 [ + + ]: 340 : if (rp->r_dayofmonth == len_months[1][rp->r_month])
3671 : 324 : week = 5;
3672 : : else
3673 : : {
3674 : 16 : wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
3675 [ + - ]: 16 : if (wdayoff)
3676 : 16 : compat = 2013;
3677 : 16 : wday -= wdayoff;
3678 : 16 : tod += wdayoff * SECSPERDAY;
3679 : 16 : week = rp->r_dayofmonth / DAYSPERWEEK;
3680 : : }
3681 : : }
3682 : : else
3683 : 0 : return -1; /* "cannot happen" */
3684 [ + + ]: 832 : if (wday < 0)
3685 : 16 : wday += DAYSPERWEEK;
3686 : 832 : result += sprintf(result, "M%d.%d.%d",
3687 : 832 : rp->r_month + 1, week, wday);
3688 : : }
3689 [ + + ]: 832 : if (rp->r_todisut)
3690 : 312 : tod += stdoff;
3691 [ + + + + ]: 832 : if (rp->r_todisstd && !rp->r_isdst)
3692 : 196 : tod += save;
3693 [ + + ]: 832 : if (tod != 2 * SECSPERMIN * MINSPERHOUR)
3694 : : {
3695 : 308 : *result++ = '/';
3696 [ - + ]: 308 : if (!stringoffset(result, tod))
3697 : 0 : return -1;
3698 [ + + ]: 308 : if (tod < 0)
3699 : : {
3700 [ + - ]: 8 : if (compat < 2013)
3701 : 8 : compat = 2013;
3702 : : }
3703 [ + + ]: 300 : else if (SECSPERDAY <= tod)
3704 : : {
3705 [ + + ]: 32 : if (compat < 1994)
3706 : 4 : compat = 1994;
3707 : : }
3708 : : }
3709 : 832 : return compat;
3710 : : }
3711 : :
3712 : : static int
3713 : 10716 : rule_cmp(struct rule const *a, struct rule const *b)
3714 : : {
3715 [ + + ]: 10716 : if (!a)
3716 : 1272 : return -!!b;
3717 [ - + ]: 9444 : if (!b)
3718 : 0 : return 1;
3719 [ + + ]: 9444 : if (a->r_hiyear != b->r_hiyear)
3720 [ + + ]: 8888 : return a->r_hiyear < b->r_hiyear ? -1 : 1;
3721 [ + + ]: 556 : if (a->r_hiyear == ZIC_MAX)
3722 : 416 : return 0;
3723 [ + - ]: 140 : if (a->r_month - b->r_month != 0)
3724 : 140 : return a->r_month - b->r_month;
3725 : 0 : return a->r_dayofmonth - b->r_dayofmonth;
3726 : : }
3727 : :
3728 : : /*
3729 : : * Store into RESULT a proleptic TZ string that represent the future
3730 : : * predictions for the zone ZPFIRST with ZONECOUNT entries. Return a
3731 : : * compatibility indicator (a TZDB release year) if successful, a
3732 : : * negative integer if no such TZ string exists.
3733 : : */
3734 : : static int
3735 : 1364 : stringzone(char *result, struct zone const *zpfirst, ptrdiff_t zonecount)
3736 : : {
3737 : : const struct zone *zp;
3738 : : struct rule *rp;
3739 : : struct rule *stdrp;
3740 : : struct rule *dstrp;
3741 : : ptrdiff_t i;
3742 : 1364 : int compat = 0;
3743 : : int c;
3744 : : int offsetlen;
3745 : : struct rule stdr,
3746 : : dstr;
3747 : : ptrdiff_t len;
3748 : : int dstcmp;
3749 : 1364 : struct rule *lastrp[2] = {NULL, NULL};
3750 : : struct zone zstr[2];
3751 : : struct zone const *stdzp;
3752 : : struct zone const *dstzp;
3753 : :
3754 : 1364 : result[0] = '\0';
3755 : :
3756 : : /*
3757 : : * Internet RFC 9636 section 6.1 says to use an empty TZ string if future
3758 : : * timestamps are truncated.
3759 : : */
3760 [ - + ]: 1364 : if (hi_time < max_time)
3761 : 0 : return -1;
3762 : :
3763 : 1364 : zp = zpfirst + zonecount - 1;
3764 [ + + ]: 11444 : for (i = 0; i < zp->z_nrules; ++i)
3765 : : {
3766 : : struct rule **last;
3767 : : int cmp;
3768 : :
3769 : 10080 : rp = &zp->z_rules[i];
3770 : 10080 : last = &lastrp[rp->r_isdst];
3771 : 10080 : cmp = rule_cmp(*last, rp);
3772 [ + + ]: 10080 : if (cmp < 0)
3773 : 3096 : *last = rp;
3774 [ - + ]: 6984 : else if (cmp == 0)
3775 : 0 : return -1;
3776 : : }
3777 : 1364 : stdrp = lastrp[false];
3778 : 1364 : dstrp = lastrp[true];
3779 [ + + - + ]: 1364 : dstcmp = zp->z_nrules ? rule_cmp(dstrp, stdrp) : zp->z_isdst ? 1 : -1;
3780 : 1364 : stdzp = dstzp = zp;
3781 : :
3782 [ + + ]: 1364 : if (dstcmp < 0)
3783 : : {
3784 : : /* Standard time all year. */
3785 : 948 : dstrp = NULL;
3786 : : }
3787 [ - + ]: 416 : else if (0 < dstcmp)
3788 : : {
3789 : : /*
3790 : : * DST all year. Use an abbreviation like "XXX3EDT4,0/0,J365/23" for
3791 : : * EDT (-04) all year.
3792 : : */
3793 [ # # ]: 0 : zic_t save = dstrp ? dstrp->r_save : zp->z_save;
3794 : :
3795 [ # # ]: 0 : if (0 <= save)
3796 : : {
3797 : : /*
3798 : : * Positive DST, the typical case for all-year DST. Fake a
3799 : : * timezone with negative DST.
3800 : : */
3801 : 0 : stdzp = &zstr[0];
3802 : 0 : dstzp = &zstr[1];
3803 : 0 : zstr[0].z_stdoff = zp->z_stdoff + 2 * save;
3804 : 0 : zstr[0].z_format = "XXX"; /* Any 3 letters will do. */
3805 : 0 : zstr[0].z_format_specifier = 0;
3806 : 0 : zstr[1].z_stdoff = zstr[0].z_stdoff;
3807 : 0 : zstr[1].z_format = zp->z_format;
3808 : 0 : zstr[1].z_format_specifier = zp->z_format_specifier;
3809 : : }
3810 : 0 : dstr.r_month = TM_JANUARY;
3811 : 0 : dstr.r_dycode = DC_DOM;
3812 : 0 : dstr.r_dayofmonth = 1;
3813 : 0 : dstr.r_tod = 0;
3814 : 0 : dstr.r_todisstd = dstr.r_todisut = false;
3815 : 0 : dstr.r_isdst = true;
3816 : 0 : dstr.r_save = save < 0 ? save : -save;
3817 [ # # ]: 0 : dstr.r_abbrvar = dstrp ? dstrp->r_abbrvar : NULL;
3818 : 0 : stdr.r_month = TM_DECEMBER;
3819 : 0 : stdr.r_dycode = DC_DOM;
3820 : 0 : stdr.r_dayofmonth = 31;
3821 : 0 : stdr.r_tod = SECSPERDAY + dstr.r_save;
3822 : 0 : stdr.r_todisstd = stdr.r_todisut = false;
3823 : 0 : stdr.r_isdst = false;
3824 : 0 : stdr.r_save = 0;
3825 [ # # # # ]: 0 : stdr.r_abbrvar = save < 0 && stdrp ? stdrp->r_abbrvar : NULL;
3826 : 0 : dstrp = &dstr;
3827 : 0 : stdrp = &stdr;
3828 : : }
3829 [ + + ]: 1364 : len = doabbr(result, stdzp, stdrp ? stdrp->r_abbrvar : NULL,
3830 : : false, 0, true);
3831 : 1364 : offsetlen = stringoffset(result + len, -stdzp->z_stdoff);
3832 [ - + ]: 1364 : if (!offsetlen)
3833 : : {
3834 : 0 : result[0] = '\0';
3835 : 0 : return -1;
3836 : : }
3837 : 1364 : len += offsetlen;
3838 [ + + ]: 1364 : if (dstrp == NULL)
3839 : 948 : return compat;
3840 : 832 : len += doabbr(result + len, dstzp, dstrp->r_abbrvar,
3841 : 416 : dstrp->r_isdst, dstrp->r_save, true);
3842 [ + + ]: 416 : if (dstrp->r_save != SECSPERMIN * MINSPERHOUR)
3843 : : {
3844 : 12 : offsetlen = stringoffset(result + len,
3845 : 12 : -(dstzp->z_stdoff + dstrp->r_save));
3846 [ - + ]: 12 : if (!offsetlen)
3847 : : {
3848 : 0 : result[0] = '\0';
3849 : 0 : return -1;
3850 : : }
3851 : 12 : len += offsetlen;
3852 : : }
3853 : 416 : result[len++] = ',';
3854 : 416 : c = stringrule(result + len, dstrp, dstrp->r_save, stdzp->z_stdoff);
3855 [ - + ]: 416 : if (c < 0)
3856 : : {
3857 : 0 : result[0] = '\0';
3858 : 0 : return -1;
3859 : : }
3860 [ + + ]: 416 : if (compat < c)
3861 : 28 : compat = c;
3862 : 416 : len += strlen(result + len);
3863 : 416 : result[len++] = ',';
3864 : 416 : c = stringrule(result + len, stdrp, dstrp->r_save, stdzp->z_stdoff);
3865 [ - + ]: 416 : if (c < 0)
3866 : : {
3867 : 0 : result[0] = '\0';
3868 : 0 : return -1;
3869 : : }
3870 [ + + ]: 416 : if (compat < c)
3871 : 4 : compat = c;
3872 : 416 : return compat;
3873 : : }
3874 : :
3875 : : static void
3876 : 1364 : outzone(const struct zone *zpfirst, ptrdiff_t zonecount)
3877 : : {
3878 : : ptrdiff_t i,
3879 : : j;
3880 : : zic_t starttime,
3881 : : untiltime;
3882 : : bool startttisstd;
3883 : : bool startttisut;
3884 : : char *startbuf;
3885 : : char *ab;
3886 : : char *envvar;
3887 : : int max_abbr_len;
3888 : : int max_envvar_len;
3889 : : int compat;
3890 : : bool do_extend;
3891 : : char version;
3892 : 1364 : zic_t nonTZlimtime = ZIC_MIN;
3893 : 1364 : int nonTZlimtype = -1;
3894 : : zic_t max_year0;
3895 : 1364 : int defaulttype = -1;
3896 : 1364 : int max_stringoffset_len = sizeof "-167:59:59" - 1;
3897 : 1364 : int max_comma_stringrule_len = (sizeof ",M12.5.6/" - 1
3898 : 1364 : + max_stringoffset_len);
3899 : :
3900 : 1364 : check_for_signal();
3901 : :
3902 : : /* This cannot overflow; see FORMAT_LEN_GROWTH_BOUND. */
3903 : 1364 : max_abbr_len = 2 + max_format_len + max_abbrvar_len;
3904 : 1364 : max_envvar_len = 2 * (max_abbr_len + max_stringoffset_len
3905 : 1364 : + max_comma_stringrule_len);
3906 : :
3907 : 1364 : startbuf = xmalloc(max_abbr_len + 1);
3908 : 1364 : ab = xmalloc(max_abbr_len + 1);
3909 : 1364 : envvar = xmalloc(max_envvar_len + 1);
3910 : 1364 : INITIALIZE(untiltime);
3911 : 1364 : INITIALIZE(starttime);
3912 : :
3913 : : /*
3914 : : * Now. . .finally. . .generate some useful data!
3915 : : */
3916 : 1364 : timecnt = 0;
3917 : 1364 : typecnt = 0;
3918 : 1364 : charcnt = 0;
3919 : :
3920 : : /*
3921 : : * Thanks to Earl Chew for noting the need to unconditionally initialize
3922 : : * startttisstd.
3923 : : */
3924 : 1364 : startttisstd = false;
3925 : 1364 : startttisut = false;
3926 : 1364 : min_year = max_year = EPOCH_YEAR;
3927 [ - + ]: 1364 : if (leapseen)
3928 : : {
3929 : 0 : updateminmax(leapminyear);
3930 : 0 : updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
3931 : : }
3932 [ + + ]: 9196 : for (i = 0; i < zonecount; ++i)
3933 : : {
3934 : 7832 : struct zone const *zp = &zpfirst[i];
3935 : :
3936 [ + + ]: 7832 : if (i < zonecount - 1)
3937 : 6468 : updateminmax(zp->z_untilrule.r_loyear);
3938 [ + + ]: 69108 : for (j = 0; j < zp->z_nrules; ++j)
3939 : : {
3940 : 61276 : struct rule *rp = &zp->z_rules[j];
3941 : :
3942 : 61276 : updateminmax(rp->r_loyear);
3943 [ + + ]: 61276 : if (rp->r_hiwasnum)
3944 : 17664 : updateminmax(rp->r_hiyear);
3945 : : }
3946 : : }
3947 : :
3948 : : /*
3949 : : * Generate lots of data if a rule can't cover all future times.
3950 : : */
3951 : 1364 : compat = stringzone(envvar, zpfirst, zonecount);
3952 [ + + ]: 1364 : version = compat < 2013 ? '2' : '3';
3953 : 1364 : do_extend = compat < 0;
3954 [ - + ]: 1364 : if (noise)
3955 : : {
3956 [ # # ]: 0 : if (!*envvar)
3957 : 0 : warning("%s %s",
3958 : : _("no proleptic TZ string for zone"),
3959 : 0 : zpfirst->z_name);
3960 [ # # ]: 0 : else if (compat != 0)
3961 : : {
3962 : : /*
3963 : : * Circa-COMPAT clients, and earlier clients, might not work for
3964 : : * this zone when given dates before 1970 or after 2038.
3965 : : */
3966 : 0 : warning(_("%s: pre-%d clients may mishandle"
3967 : : " distant timestamps"),
3968 : 0 : zpfirst->z_name, compat);
3969 : : }
3970 : : }
3971 [ - + ]: 1364 : if (do_extend)
3972 : : {
3973 [ # # ]: 0 : if (min_year >= ZIC_MIN + years_of_observations)
3974 : 0 : min_year -= years_of_observations;
3975 : : else
3976 : 0 : min_year = ZIC_MIN;
3977 [ # # ]: 0 : if (max_year <= ZIC_MAX - years_of_observations)
3978 : 0 : max_year += years_of_observations;
3979 : : else
3980 : 0 : max_year = ZIC_MAX;
3981 : : }
3982 : 1364 : max_year = max(max_year, (redundant_time / (SECSPERDAY * DAYSPERNYEAR)
3983 : : + EPOCH_YEAR + 1));
3984 : 1364 : max_year0 = max_year;
3985 [ - + ]: 1364 : if (want_bloat())
3986 : : {
3987 : : /*
3988 : : * For the benefit of older systems, generate data from 1900 through
3989 : : * 2038.
3990 : : */
3991 [ # # ]: 0 : if (min_year > YEAR_32BIT_MIN - 1)
3992 : 0 : min_year = YEAR_32BIT_MIN - 1;
3993 [ # # ]: 0 : if (max_year < YEAR_32BIT_MAX)
3994 : 0 : max_year = YEAR_32BIT_MAX;
3995 : : }
3996 : :
3997 [ + - - + ]: 1364 : if (min_time < lo_time || hi_time < max_time)
3998 : 0 : unspecifiedtype = addtype(0, "-00", false, false, false);
3999 : :
4000 [ + + ]: 9196 : for (i = 0; i < zonecount; ++i)
4001 : : {
4002 : : /*
4003 : : * A guess that may well be corrected later.
4004 : : */
4005 : 7832 : zic_t save = 0;
4006 : 7832 : struct zone const *zp = &zpfirst[i];
4007 [ + + + - ]: 7832 : bool usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
4008 : 7832 : bool useuntil = i < (zonecount - 1);
4009 : 7832 : zic_t stdoff = zp->z_stdoff;
4010 : 7832 : zic_t startoff = stdoff;
4011 : :
4012 [ + + - + ]: 7832 : if (useuntil && zp->z_untiltime <= min_time)
4013 : 0 : continue;
4014 : 7832 : eat(zp->z_filenum, zp->z_linenum);
4015 : 7832 : *startbuf = '\0';
4016 [ + + ]: 7832 : if (zp->z_nrules == 0)
4017 : : {
4018 : : int type;
4019 : :
4020 : 4800 : save = zp->z_save;
4021 : 4800 : doabbr(startbuf, zp, NULL, zp->z_isdst, save, false);
4022 : 4800 : type = addtype(oadd(zp->z_stdoff, save),
4023 : 4800 : startbuf, zp->z_isdst, startttisstd,
4024 : : startttisut);
4025 [ + + ]: 4800 : if (usestart)
4026 : : {
4027 : 3436 : addtt(starttime, type);
4028 [ + - ]: 3436 : if (nonTZlimtime < starttime)
4029 : : {
4030 : 3436 : nonTZlimtime = starttime;
4031 : 3436 : nonTZlimtype = type;
4032 : : }
4033 : 3436 : usestart = false;
4034 : : }
4035 : : else
4036 : 1364 : defaulttype = type;
4037 : : }
4038 : : else
4039 : : {
4040 : : zic_t year;
4041 : :
4042 [ + + ]: 273956 : for (year = min_year; year <= max_year; ++year)
4043 : : {
4044 [ + + + + ]: 273160 : if (useuntil && year > zp->z_untilrule.r_hiyear)
4045 : 2236 : break;
4046 : :
4047 : : /*
4048 : : * Mark which rules to do in the current year. For those to
4049 : : * do, calculate rpytime(rp, year); The former TYPE field was
4050 : : * also considered here.
4051 : : */
4052 [ + + ]: 5836076 : for (j = 0; j < zp->z_nrules; ++j)
4053 : : {
4054 : 5565152 : zic_t one = 1;
4055 : 5565152 : zic_t y2038_boundary = one << 31;
4056 : 5565152 : struct rule *rp = &zp->z_rules[j];
4057 : :
4058 : 5565152 : eats(zp->z_filenum, zp->z_linenum,
4059 : : rp->r_filenum, rp->r_linenum);
4060 [ + + ]: 7274148 : rp->r_todo = year >= rp->r_loyear &&
4061 [ + + ]: 1708996 : year <= rp->r_hiyear;
4062 [ + + ]: 5565152 : if (rp->r_todo)
4063 : : {
4064 : 132212 : rp->r_temp = rpytime(rp, year);
4065 : : rp->r_todo
4066 : 264424 : = (rp->r_temp < y2038_boundary
4067 [ + + + - ]: 132212 : || year <= max_year0);
4068 : : }
4069 : : }
4070 : : for (;;)
4071 : 129936 : {
4072 : : ptrdiff_t k;
4073 : : zic_t jtime,
4074 : : ktime;
4075 : : zic_t offset;
4076 : : struct rule *rp;
4077 : : int type;
4078 : :
4079 : 400860 : INITIALIZE(ktime);
4080 [ + + ]: 400860 : if (useuntil)
4081 : : {
4082 : : /*
4083 : : * Turn untiltime into UT assuming the current stdoff
4084 : : * and save values.
4085 : : */
4086 : 289068 : untiltime = zp->z_untiltime;
4087 [ + + ]: 289068 : if (!zp->z_untilrule.r_todisut)
4088 : 282504 : untiltime = tadd(untiltime,
4089 : : -stdoff);
4090 [ + + ]: 289068 : if (!zp->z_untilrule.r_todisstd)
4091 : 212728 : untiltime = tadd(untiltime,
4092 : : -save);
4093 : : }
4094 : :
4095 : : /*
4096 : : * Find the rule (of those to do, if any) that takes
4097 : : * effect earliest in the year.
4098 : : */
4099 : 400860 : k = -1;
4100 [ + + ]: 9185524 : for (j = 0; j < zp->z_nrules; ++j)
4101 : : {
4102 : 8784664 : struct rule *r = &zp->z_rules[j];
4103 : :
4104 [ + + ]: 8784664 : if (!r->r_todo)
4105 : 8583804 : continue;
4106 : 200860 : eats(zp->z_filenum, zp->z_linenum,
4107 : : r->r_filenum, r->r_linenum);
4108 [ + + ]: 200860 : offset = r->r_todisut ? 0 : stdoff;
4109 [ + + ]: 200860 : if (!r->r_todisstd)
4110 : 134380 : offset = oadd(offset, save);
4111 : 200860 : jtime = r->r_temp;
4112 : 200860 : jtime = tadd(jtime, -offset);
4113 [ + + + + ]: 200860 : if (k < 0 || jtime < ktime)
4114 : : {
4115 : 164544 : k = j;
4116 : 164544 : ktime = jtime;
4117 : : }
4118 [ - + ]: 36316 : else if (jtime == ktime)
4119 : : {
4120 : 0 : char const *dup_rules_msg =
4121 : : _("two rules for same instant");
4122 : :
4123 : 0 : eats(zp->z_filenum, zp->z_linenum,
4124 : : r->r_filenum, r->r_linenum);
4125 : 0 : warning("%s", dup_rules_msg);
4126 : 0 : r = &zp->z_rules[k];
4127 : 0 : eats(zp->z_filenum, zp->z_linenum,
4128 : : r->r_filenum, r->r_linenum);
4129 : 0 : error("%s", dup_rules_msg);
4130 : : }
4131 : : }
4132 [ + + ]: 400860 : if (k < 0)
4133 : 269464 : break; /* go on to next year */
4134 : 131396 : rp = &zp->z_rules[k];
4135 : 131396 : rp->r_todo = false;
4136 [ + + + + ]: 131396 : if (useuntil && ktime >= untiltime)
4137 : : {
4138 [ - + ]: 1460 : if (!*startbuf
4139 [ # # ]: 0 : && (oadd(zp->z_stdoff, rp->r_save)
4140 : : == startoff))
4141 : 0 : doabbr(startbuf, zp, rp->r_abbrvar,
4142 : 0 : rp->r_isdst, rp->r_save,
4143 : : false);
4144 : 1460 : break;
4145 : : }
4146 : 129936 : save = rp->r_save;
4147 [ + + + + ]: 129936 : if (usestart && ktime == starttime)
4148 : 232 : usestart = false;
4149 [ + + ]: 129936 : if (usestart)
4150 : : {
4151 [ + + ]: 125348 : if (ktime < starttime)
4152 : : {
4153 : 66356 : startoff = oadd(zp->z_stdoff,
4154 : : save);
4155 : 66356 : doabbr(startbuf, zp,
4156 : : rp->r_abbrvar,
4157 : 66356 : rp->r_isdst,
4158 : : rp->r_save,
4159 : : false);
4160 : 66356 : continue;
4161 : : }
4162 [ + + ]: 58992 : if (*startbuf == '\0'
4163 [ + + ]: 1576 : && startoff == oadd(zp->z_stdoff,
4164 : : save))
4165 : : {
4166 : 788 : doabbr(startbuf,
4167 : : zp,
4168 : : rp->r_abbrvar,
4169 : 788 : rp->r_isdst,
4170 : : rp->r_save,
4171 : : false);
4172 : : }
4173 : : }
4174 : 63580 : eats(zp->z_filenum, zp->z_linenum,
4175 : : rp->r_filenum, rp->r_linenum);
4176 : 63580 : doabbr(ab, zp, rp->r_abbrvar,
4177 : 63580 : rp->r_isdst, rp->r_save, false);
4178 : 63580 : offset = oadd(zp->z_stdoff, rp->r_save);
4179 : 63580 : type = addtype(offset, ab, rp->r_isdst,
4180 : 63580 : rp->r_todisstd, rp->r_todisut);
4181 [ - + - - ]: 63580 : if (defaulttype < 0 && !rp->r_isdst)
4182 : 0 : defaulttype = type;
4183 : 63580 : addtt(ktime, type);
4184 [ + - ]: 63580 : if (nonTZlimtime < ktime
4185 [ + + + + ]: 63580 : && (useuntil || rp->r_hiyear != ZIC_MAX))
4186 : : {
4187 : 60708 : nonTZlimtime = ktime;
4188 : 60708 : nonTZlimtype = type;
4189 : : }
4190 : : }
4191 : : }
4192 : : }
4193 [ + + ]: 7832 : if (usestart)
4194 : : {
4195 : 2800 : bool isdst = startoff != zp->z_stdoff;
4196 : :
4197 [ - + - - ]: 2800 : if (*startbuf == '\0' && zp->z_format)
4198 : 0 : doabbr(startbuf, zp, disable_percent_s,
4199 : : isdst, save, false);
4200 : 2800 : eat(zp->z_filenum, zp->z_linenum);
4201 [ - + ]: 2800 : if (*startbuf == '\0')
4202 : 0 : error(_("cannot determine time zone abbreviation"
4203 : : " to use just after until time"));
4204 : : else
4205 : : {
4206 : 2800 : int type = addtype(startoff, startbuf, isdst,
4207 : : startttisstd, startttisut);
4208 : :
4209 [ - + - - ]: 2800 : if (defaulttype < 0 && !isdst)
4210 : 0 : defaulttype = type;
4211 : 2800 : addtt(starttime, type);
4212 : : }
4213 : : }
4214 : :
4215 : : /*
4216 : : * Now we may get to set starttime for the next zone line.
4217 : : */
4218 [ + + ]: 7832 : if (useuntil)
4219 : : {
4220 : 6468 : startttisstd = zp->z_untilrule.r_todisstd;
4221 : 6468 : startttisut = zp->z_untilrule.r_todisut;
4222 : 6468 : starttime = zp->z_untiltime;
4223 [ + + ]: 6468 : if (!startttisstd)
4224 : 5392 : starttime = tadd(starttime, -save);
4225 [ + + ]: 6468 : if (!startttisut)
4226 : 6172 : starttime = tadd(starttime, -stdoff);
4227 : : }
4228 : : }
4229 [ - + ]: 1364 : if (defaulttype < 0)
4230 : 0 : defaulttype = 0;
4231 [ + - + - ]: 1364 : if (!do_extend && !want_bloat())
4232 : : {
4233 : : /* Keep trailing transitions that are no greater than this. */
4234 : : zic_t keep_at_max;
4235 : :
4236 : : /* The earliest transition into a time governed by the TZ string. */
4237 : 1364 : zic_t TZstarttime = ZIC_MAX;
4238 : :
4239 [ + + ]: 71180 : for (i = 0; i < timecnt; i++)
4240 : : {
4241 : 69816 : zic_t at = attypes[i].at;
4242 : :
4243 [ + + + + ]: 69816 : if (nonTZlimtime < at && at < TZstarttime)
4244 : 492 : TZstarttime = at;
4245 : : }
4246 [ + + ]: 1364 : if (TZstarttime == ZIC_MAX)
4247 : 948 : TZstarttime = nonTZlimtime;
4248 : :
4249 : : /*
4250 : : * Omit trailing transitions deducible from the TZ string, and not
4251 : : * needed for -r or -R.
4252 : : */
4253 : 1364 : keep_at_max = max(TZstarttime, redundant_time);
4254 [ + + ]: 71180 : for (i = j = 0; i < timecnt; i++)
4255 [ + + ]: 69816 : if (attypes[i].at <= keep_at_max)
4256 : : {
4257 : 68872 : attypes[j].at = attypes[i].at;
4258 : 137744 : attypes[j].dontmerge = (attypes[i].at == TZstarttime
4259 [ + + + + ]: 69776 : && (nonTZlimtype != attypes[i].type
4260 [ + + ]: 904 : || strchr(envvar, ',')));
4261 : 68872 : attypes[j].type = attypes[i].type;
4262 : 68872 : j++;
4263 : : }
4264 : 1364 : timecnt = j;
4265 : : }
4266 [ - + ]: 1364 : if (do_extend)
4267 : : {
4268 : : /*
4269 : : * If we're extending the explicitly listed observations for 400 years
4270 : : * because we can't fill the proleptic TZ field, check whether we
4271 : : * actually ended up explicitly listing observations through that
4272 : : * period. If there aren't any near the end of the 400-year period,
4273 : : * add a redundant one at the end of the final year, to make it clear
4274 : : * that we are claiming to have definite knowledge of the lack of
4275 : : * transitions up to that point.
4276 : : */
4277 : : struct rule xr;
4278 : : struct attype *lastat;
4279 : :
4280 : 0 : xr.r_month = TM_JANUARY;
4281 : 0 : xr.r_dycode = DC_DOM;
4282 : 0 : xr.r_dayofmonth = 1;
4283 : 0 : xr.r_tod = 0;
4284 [ # # ]: 0 : for (lastat = attypes, i = 1; i < timecnt; i++)
4285 [ # # ]: 0 : if (attypes[i].at > lastat->at)
4286 : 0 : lastat = &attypes[i];
4287 [ # # # # ]: 0 : if (!lastat || lastat->at < rpytime(&xr, max_year - 1))
4288 : : {
4289 [ # # ]: 0 : addtt(rpytime(&xr, max_year + 1),
4290 : 0 : lastat ? lastat->type : defaulttype);
4291 : 0 : attypes[timecnt - 1].dontmerge = true;
4292 : : }
4293 : : }
4294 : 1364 : writezone(zpfirst->z_name, envvar, version, defaulttype);
4295 : 1364 : free(startbuf);
4296 : 1364 : free(ab);
4297 : 1364 : free(envvar);
4298 : 1364 : }
4299 : :
4300 : : static void
4301 : 69816 : addtt(zic_t starttime, int type)
4302 : : {
4303 : 69816 : attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
4304 : 69816 : attypes[timecnt].at = starttime;
4305 : 69816 : attypes[timecnt].dontmerge = false;
4306 : 69816 : attypes[timecnt].type = type;
4307 : 69816 : ++timecnt;
4308 : 69816 : }
4309 : :
4310 : : static int
4311 : 71180 : addtype(zic_t utoff, char const *abbr, bool isdst, bool ttisstd, bool ttisut)
4312 : : {
4313 : : int i,
4314 : : j;
4315 : : int charcnt0;
4316 : :
4317 : : /* RFC 9636 section 3.2 specifies this range for utoff. */
4318 [ + - - + ]: 71180 : if (!(-TWO_31_MINUS_1 <= utoff && utoff <= TWO_31_MINUS_1))
4319 : : {
4320 : 0 : error(_("UT offset out of range"));
4321 : 0 : exit(EXIT_FAILURE);
4322 : : }
4323 [ + - ]: 71180 : if (!want_bloat())
4324 : 71180 : ttisstd = ttisut = false;
4325 : :
4326 : 71180 : checkabbr(abbr);
4327 : :
4328 : 71180 : charcnt0 = charcnt;
4329 : 71180 : j = addabbr(chars, &charcnt, abbr);
4330 [ + + ]: 71180 : if (charcnt0 < charcnt)
4331 : : {
4332 : : /*
4333 : : * If an abbreviation was inserted, increment indexes no earlier than
4334 : : * the insert by the size of the insertion, so that they continue to
4335 : : * point to the same contents.
4336 : : */
4337 [ + + ]: 18132 : for (i = 0; i < typecnt; i++)
4338 [ + + ]: 12320 : if (j <= desigidx[i])
4339 : 4 : desigidx[i] += charcnt - charcnt0;
4340 : : }
4341 : : else
4342 : : {
4343 : : /* If there's already an entry, return its index. */
4344 [ + + ]: 242872 : for (i = 0; i < typecnt; i++)
4345 [ + + + + : 242316 : if (utoff == utoffs[i] && isdst == isdsts[i] && j == desigidx[i]
+ + ]
4346 [ + - + - ]: 64812 : && ttisstd == ttisstds[i] && ttisut == ttisuts[i])
4347 : 64812 : return i;
4348 : : }
4349 : :
4350 : : /*
4351 : : * There isn't one; add a new one, unless there are already too many.
4352 : : */
4353 [ - + ]: 6368 : if (typecnt >= TZ_MAX_TYPES)
4354 : : {
4355 : 0 : error(_("too many local time types"));
4356 : 0 : exit(EXIT_FAILURE);
4357 : : }
4358 : 6368 : i = typecnt++;
4359 : 6368 : utoffs[i] = utoff;
4360 : 6368 : isdsts[i] = isdst;
4361 : 6368 : ttisstds[i] = ttisstd;
4362 : 6368 : ttisuts[i] = ttisut;
4363 : 6368 : desigidx[i] = j;
4364 : 6368 : return i;
4365 : : }
4366 : :
4367 : : static void
4368 : 0 : leapadd(zic_t t, int correction, int rolling)
4369 : : {
4370 : : ptrdiff_t i;
4371 : :
4372 [ # # # # : 0 : if (rolling && (lo_time != min_time || hi_time != max_time))
# # ]
4373 : : {
4374 : 0 : error(_("Rolling leap seconds not supported with -r"));
4375 : 0 : exit(EXIT_FAILURE);
4376 : : }
4377 : 0 : leap = growalloc(leap, sizeof *leap, leapcnt, &leap_alloc);
4378 [ # # ]: 0 : for (i = 0; i < leapcnt; ++i)
4379 [ # # ]: 0 : if (t <= leap[i].trans)
4380 : 0 : break;
4381 : 0 : memmove(&leap[i + 1], &leap[i], (leapcnt - i) * sizeof *leap);
4382 : 0 : leap[i].trans = t;
4383 : 0 : leap[i].corr = correction;
4384 : 0 : leap[i].roll = rolling;
4385 : 0 : ++leapcnt;
4386 : 0 : }
4387 : :
4388 : : static void
4389 : 0 : adjleap(void)
4390 : : {
4391 : : ptrdiff_t i;
4392 : 0 : zic_t last = 0;
4393 : 0 : zic_t prevtrans = 0;
4394 : :
4395 : : /*
4396 : : * propagate leap seconds forward
4397 : : */
4398 [ # # ]: 0 : for (i = 0; i < leapcnt; ++i)
4399 : : {
4400 [ # # ]: 0 : if (leap[i].trans - prevtrans < 28 * SECSPERDAY)
4401 : : {
4402 : 0 : error(_("Leap seconds too close together"));
4403 : 0 : exit(EXIT_FAILURE);
4404 : : }
4405 : 0 : prevtrans = leap[i].trans;
4406 : 0 : leap[i].trans = tadd(prevtrans, last);
4407 : 0 : last = leap[i].corr += last;
4408 : : }
4409 : :
4410 [ # # ]: 0 : if (0 <= leapexpires)
4411 : : {
4412 : 0 : leapexpires = oadd(leapexpires, last);
4413 [ # # # # ]: 0 : if (!(leapcnt == 0 || (leap[leapcnt - 1].trans < leapexpires)))
4414 : : {
4415 : 0 : error(_("last Leap time does not precede Expires time"));
4416 : 0 : exit(EXIT_FAILURE);
4417 : : }
4418 : : }
4419 : 0 : }
4420 : :
4421 : : /* Is A a space character in the C locale? */
4422 : : static bool
4423 : 559872 : is_space(char a)
4424 : : {
4425 [ + + ]: 559872 : switch (a)
4426 : : {
4427 : 334040 : default:
4428 : 334040 : return false;
4429 : 225832 : case ' ':
4430 : : case '\f':
4431 : : case '\n':
4432 : : case '\r':
4433 : : case '\t':
4434 : : case '\v':
4435 : 225832 : return true;
4436 : : }
4437 : : }
4438 : :
4439 : : /* Is A an alphabetic character in the C locale? */
4440 : : static bool
4441 : 303280 : is_alpha(char a)
4442 : : {
4443 [ + + ]: 303280 : switch (a)
4444 : : {
4445 : 151180 : default:
4446 : 151180 : return false;
4447 : 152100 : case 'A':
4448 : : case 'B':
4449 : : case 'C':
4450 : : case 'D':
4451 : : case 'E':
4452 : : case 'F':
4453 : : case 'G':
4454 : : case 'H':
4455 : : case 'I':
4456 : : case 'J':
4457 : : case 'K':
4458 : : case 'L':
4459 : : case 'M':
4460 : : case 'N':
4461 : : case 'O':
4462 : : case 'P':
4463 : : case 'Q':
4464 : : case 'R':
4465 : : case 'S':
4466 : : case 'T':
4467 : : case 'U':
4468 : : case 'V':
4469 : : case 'W':
4470 : : case 'X':
4471 : : case 'Y':
4472 : : case 'Z':
4473 : : case 'a':
4474 : : case 'b':
4475 : : case 'c':
4476 : : case 'd':
4477 : : case 'e':
4478 : : case 'f':
4479 : : case 'g':
4480 : : case 'h':
4481 : : case 'i':
4482 : : case 'j':
4483 : : case 'k':
4484 : : case 'l':
4485 : : case 'm':
4486 : : case 'n':
4487 : : case 'o':
4488 : : case 'p':
4489 : : case 'q':
4490 : : case 'r':
4491 : : case 's':
4492 : : case 't':
4493 : : case 'u':
4494 : : case 'v':
4495 : : case 'w':
4496 : : case 'x':
4497 : : case 'y':
4498 : : case 'z':
4499 : 152100 : return true;
4500 : : }
4501 : : }
4502 : :
4503 : : /*
4504 : : * If A is an uppercase character in the C locale, return its lowercase
4505 : : * counterpart. Otherwise, return A.
4506 : : */
4507 : : static char
4508 : 1720532 : lowerit(char a)
4509 : : {
4510 [ + + - - : 1720532 : switch (a)
+ - + - -
- + - + +
+ + - - +
+ + - - +
- - + ]
4511 : : {
4512 : 813956 : default:
4513 : 813956 : return a;
4514 : 116660 : case 'A':
4515 : 116660 : return 'a';
4516 : 0 : case 'B':
4517 : 0 : return 'b';
4518 : 0 : case 'C':
4519 : 0 : return 'c';
4520 : 37920 : case 'D':
4521 : 37920 : return 'd';
4522 : 0 : case 'E':
4523 : 0 : return 'e';
4524 : 48176 : case 'F':
4525 : 48176 : return 'f';
4526 : 0 : case 'G':
4527 : 0 : return 'g';
4528 : 0 : case 'H':
4529 : 0 : return 'h';
4530 : 0 : case 'I':
4531 : 0 : return 'i';
4532 : 159092 : case 'J':
4533 : 159092 : return 'j';
4534 : 0 : case 'K':
4535 : 0 : return 'k';
4536 : 27624 : case 'L':
4537 : 27624 : return 'l';
4538 : 118532 : case 'M':
4539 : 118532 : return 'm';
4540 : 43680 : case 'N':
4541 : 43680 : return 'n';
4542 : 92160 : case 'O':
4543 : 92160 : return 'o';
4544 : 0 : case 'P':
4545 : 0 : return 'p';
4546 : 0 : case 'Q':
4547 : 0 : return 'q';
4548 : 71472 : case 'R':
4549 : 71472 : return 'r';
4550 : 142992 : case 'S':
4551 : 142992 : return 's';
4552 : 12644 : case 'T':
4553 : 12644 : return 't';
4554 : 0 : case 'U':
4555 : 0 : return 'u';
4556 : 0 : case 'V':
4557 : 0 : return 'v';
4558 : 5984 : case 'W':
4559 : 5984 : return 'w';
4560 : 0 : case 'X':
4561 : 0 : return 'x';
4562 : 0 : case 'Y':
4563 : 0 : return 'y';
4564 : 29640 : case 'Z':
4565 : 29640 : return 'z';
4566 : : }
4567 : : }
4568 : :
4569 : : /* case-insensitive equality */
4570 : : ATTRIBUTE_PURE_114833
4571 : : static bool
4572 : 361708 : ciequal(const char *ap, const char *bp)
4573 : : {
4574 [ + + ]: 458424 : while (lowerit(*ap) == lowerit(*bp++))
4575 [ + + ]: 104260 : if (*ap++ == '\0')
4576 : 7544 : return true;
4577 : 354164 : return false;
4578 : : }
4579 : :
4580 : : ATTRIBUTE_PURE_114833
4581 : : static bool
4582 : 0 : itsabbr(const char *abbr, const char *word)
4583 : : {
4584 [ # # ]: 0 : if (lowerit(*abbr) != lowerit(*word))
4585 : 0 : return false;
4586 : 0 : ++word;
4587 [ # # ]: 0 : while (*++abbr != '\0')
4588 : : do
4589 : : {
4590 [ # # ]: 0 : if (*word == '\0')
4591 : 0 : return false;
4592 [ # # ]: 0 : } while (lowerit(*word++) != lowerit(*abbr));
4593 : 0 : return true;
4594 : : }
4595 : :
4596 : : /* Return true if ABBR is an initial prefix of WORD, ignoring ASCII case. */
4597 : :
4598 : : ATTRIBUTE_PURE_114833
4599 : : static bool
4600 : 358196 : ciprefix(char const *abbr, char const *word)
4601 : : {
4602 : : do
4603 [ + + ]: 429132 : if (!*abbr)
4604 : 34692 : return true;
4605 [ + + ]: 394440 : while (lowerit(*abbr++) == lowerit(*word++));
4606 : :
4607 : 323504 : return false;
4608 : : }
4609 : :
4610 : : static const struct lookup *
4611 : 71568 : byword(const char *word, const struct lookup *table)
4612 : : {
4613 : : const struct lookup *foundlp;
4614 : : const struct lookup *lp;
4615 : :
4616 [ + - - + ]: 71568 : if (word == NULL || table == NULL)
4617 : 0 : return NULL;
4618 : :
4619 : : /*
4620 : : * If TABLE is LASTS and the word starts with "last" followed by a
4621 : : * non-'-', skip the "last" and look in WDAY_NAMES instead. Warn about any
4622 : : * usage of the undocumented prefix "last-".
4623 : : */
4624 [ + + + + : 71568 : if (table == lasts && ciprefix("last", word) && word[4])
+ - ]
4625 : : {
4626 [ - + ]: 1368 : if (word[4] == '-')
4627 : 0 : warning(_("\"%s\" is undocumented; use \"last%s\" instead"),
4628 : : word, word + 5);
4629 : : else
4630 : : {
4631 : 1368 : word += 4;
4632 : 1368 : table = wday_names;
4633 : : }
4634 : : }
4635 : :
4636 : : /*
4637 : : * Look for exact match.
4638 : : */
4639 [ + + ]: 425732 : for (lp = table; lp->l_word != NULL; ++lp)
4640 [ + + ]: 361708 : if (ciequal(word, lp->l_word))
4641 : 7544 : return lp;
4642 : :
4643 : : /*
4644 : : * Look for inexact match.
4645 : : */
4646 : 64024 : foundlp = NULL;
4647 [ + + ]: 407416 : for (lp = table; lp->l_word != NULL; ++lp)
4648 [ + + ]: 343392 : if (ciprefix(word, lp->l_word))
4649 : : {
4650 [ + - ]: 33324 : if (foundlp == NULL)
4651 : 33324 : foundlp = lp;
4652 : : else
4653 : 0 : return NULL; /* multiple inexact matches */
4654 : : }
4655 : :
4656 [ + + - + ]: 64024 : if (foundlp && noise)
4657 : : {
4658 : : /* Warn about any backward-compatibility issue with pre-2017c zic. */
4659 : 0 : bool pre_2017c_match = false;
4660 : :
4661 [ # # ]: 0 : for (lp = table; lp->l_word; lp++)
4662 [ # # ]: 0 : if (itsabbr(word, lp->l_word))
4663 : : {
4664 [ # # ]: 0 : if (pre_2017c_match)
4665 : : {
4666 : 0 : warning(_("\"%s\" is ambiguous in pre-2017c zic"), word);
4667 : 0 : break;
4668 : : }
4669 : 0 : pre_2017c_match = true;
4670 : : }
4671 : : }
4672 : :
4673 : 64024 : return foundlp;
4674 : : }
4675 : :
4676 : : static int
4677 : 17208 : getfields(char *cp, char **array, int arrayelts)
4678 : : {
4679 : : char *dp;
4680 : : int nsubs;
4681 : :
4682 : 17208 : nsubs = 0;
4683 : : for (;;)
4684 : 130112 : {
4685 : : char *dstart;
4686 : :
4687 [ - + ]: 147320 : while (is_space(*cp))
4688 : 0 : ++cp;
4689 [ + + + + ]: 147320 : if (*cp == '\0' || *cp == '#')
4690 : : break;
4691 : 130112 : dstart = dp = cp;
4692 : : do
4693 : : {
4694 [ + - ]: 299636 : if ((*dp = *cp++) != '"')
4695 : 299636 : ++dp;
4696 : : else
4697 [ # # ]: 0 : while ((*dp = *cp++) != '"')
4698 [ # # ]: 0 : if (*dp != '\0')
4699 : 0 : ++dp;
4700 : : else
4701 : : {
4702 : 0 : error(_("Odd number of quotation marks"));
4703 : 0 : exit(EXIT_FAILURE);
4704 : : }
4705 [ + + + - : 299636 : } while (*cp && *cp != '#' && !is_space(*cp));
+ + ]
4706 [ + + ]: 130112 : if (is_space(*cp))
4707 : 112916 : ++cp;
4708 : 130112 : *dp = '\0';
4709 [ - + ]: 130112 : if (nsubs == arrayelts)
4710 : : {
4711 : 0 : error(_("Too many input fields"));
4712 : 0 : exit(EXIT_FAILURE);
4713 : : }
4714 [ + + + + ]: 130112 : array[nsubs++] = dstart + (*dstart == '-' && dp == dstart + 1);
4715 : : }
4716 : 17208 : return nsubs;
4717 : : }
4718 : :
4719 : : ATTRIBUTE_NORETURN static void
4720 : 0 : time_overflow(void)
4721 : : {
4722 : 0 : error(_("time overflow"));
4723 : 0 : exit(EXIT_FAILURE);
4724 : : }
4725 : :
4726 : : /* Return T1 + T2, but diagnose any overflow and exit. */
4727 : : ATTRIBUTE_PURE_114833_HACK
4728 : : static zic_t
4729 : 23034480 : oadd(zic_t t1, zic_t t2)
4730 : : {
4731 : : #ifdef ckd_add
4732 : : zic_t sum;
4733 : :
4734 [ + - ]: 23034480 : if (!ckd_add(&sum, t1, t2))
4735 : 23034480 : return sum;
4736 : : #else
4737 : : if (t1 < 0 ? ZIC_MIN - t1 <= t2 : t2 <= ZIC_MAX - t1)
4738 : : return t1 + t2;
4739 : : #endif
4740 : 0 : time_overflow();
4741 : : }
4742 : :
4743 : : /*
4744 : : * Return T1 + T2, but diagnose any overflow and exit.
4745 : : * This is like oadd, except the result must fit in min_time..max_time range,
4746 : : * which on oddball machines can be a smaller range than ZIC_MIN..ZIC_MAX.
4747 : : */
4748 : : ATTRIBUTE_PURE_114833_HACK
4749 : : static zic_t
4750 : 846336 : tadd(zic_t t1, zic_t t2)
4751 : : {
4752 : 846336 : zic_t sum = oadd(t1, t2);
4753 : :
4754 [ + - + - ]: 846336 : if (min_time <= sum && sum <= max_time)
4755 : 846336 : return sum;
4756 : 0 : time_overflow();
4757 : : }
4758 : :
4759 : : /* Return T1 * T2, but diagnose any overflow and exit. */
4760 : : ATTRIBUTE_PURE_114833_HACK
4761 : : static zic_t
4762 : 169900 : omul(zic_t t1, zic_t t2)
4763 : : {
4764 : : #ifdef ckd_mul
4765 : : zic_t product;
4766 : :
4767 [ + - ]: 169900 : if (!ckd_mul(&product, t1, t2))
4768 : 169900 : return product;
4769 : : #else
4770 : : if (t2 < 0
4771 : : ? ZIC_MAX / t2 <= t1 && (t2 == -1 || t1 <= ZIC_MIN / t2)
4772 : : : t2 == 0 || (ZIC_MIN / t2 <= t1 && t1 <= ZIC_MAX / t2))
4773 : : return t1 * t2;
4774 : : #endif
4775 : 0 : time_overflow();
4776 : : }
4777 : :
4778 : : /*
4779 : : * Given a rule, and a year, compute the date (in seconds since January 1,
4780 : : * 1970, 00:00 LOCAL time) in that year that the rule refers to.
4781 : : * Do not count leap seconds. On error, diagnose and exit.
4782 : : */
4783 : :
4784 : : static zic_t
4785 : 138680 : rpytime(const struct rule *rp, zic_t wantedy)
4786 : : {
4787 : : int m,
4788 : : i;
4789 : : zic_t dayoff; /* with a nod to Margaret O. */
4790 : : zic_t t,
4791 : : y;
4792 : : int yrem;
4793 : :
4794 : 138680 : m = TM_JANUARY;
4795 : 138680 : y = EPOCH_YEAR;
4796 : :
4797 : : /*
4798 : : * dayoff = floor((wantedy - y) / YEARSPERREPEAT) * DAYSPERREPEAT, sans
4799 : : * overflow.
4800 : : */
4801 : 138680 : yrem = wantedy % YEARSPERREPEAT - y % YEARSPERREPEAT;
4802 : 138680 : dayoff = ((wantedy / YEARSPERREPEAT - y / YEARSPERREPEAT
4803 : 138680 : + yrem / YEARSPERREPEAT - (yrem % YEARSPERREPEAT < 0))
4804 : : * DAYSPERREPEAT);
4805 : : /* wantedy = y + ((wantedy - y) mod YEARSPERREPEAT), sans overflow. */
4806 : 138680 : wantedy = y + (yrem + 2 * YEARSPERREPEAT) % YEARSPERREPEAT;
4807 : :
4808 [ + + ]: 20846696 : while (wantedy != y)
4809 : : {
4810 [ + + + + : 20708016 : i = len_years[isleap(y)];
+ + ]
4811 : 20708016 : dayoff = oadd(dayoff, i);
4812 : 20708016 : y++;
4813 : : }
4814 [ + + ]: 918300 : while (m != rp->r_month)
4815 : : {
4816 [ + + + + : 779620 : i = len_months[isleap(y)][m];
+ + ]
4817 : 779620 : dayoff = oadd(dayoff, i);
4818 : 779620 : ++m;
4819 : : }
4820 : 138680 : i = rp->r_dayofmonth;
4821 [ + + + + : 138680 : if (m == TM_FEBRUARY && i == 29 && !isleap(y))
+ + + + -
+ ]
4822 : : {
4823 [ + - ]: 304 : if (rp->r_dycode == DC_DOWLEQ)
4824 : 304 : --i;
4825 : : else
4826 : : {
4827 : 0 : error(_("use of 2/29 in non leap-year"));
4828 : 0 : exit(EXIT_FAILURE);
4829 : : }
4830 : : }
4831 : 138680 : --i;
4832 : 138680 : dayoff = oadd(dayoff, i);
4833 [ + + + + ]: 138680 : if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
4834 : : {
4835 : : /*
4836 : : * Don't trust mod of negative numbers.
4837 : : */
4838 : 89532 : zic_t wday = ((EPOCH_WDAY + dayoff % DAYSPERWEEK + DAYSPERWEEK)
4839 : : % DAYSPERWEEK);
4840 : :
4841 [ + + ]: 349448 : while (wday != rp->r_wday)
4842 [ + + ]: 259916 : if (rp->r_dycode == DC_DOWGEQ)
4843 : : {
4844 : 80620 : dayoff = oadd(dayoff, 1);
4845 [ + + ]: 80620 : if (++wday >= DAYSPERWEEK)
4846 : 21528 : wday = 0;
4847 : 80620 : ++i;
4848 : : }
4849 : : else
4850 : : {
4851 : 179296 : dayoff = oadd(dayoff, -1);
4852 [ + + ]: 179296 : if (--wday < 0)
4853 : 1572 : wday = DAYSPERWEEK - 1;
4854 : 179296 : --i;
4855 : : }
4856 [ + + + + : 89532 : if (i < 0 || i >= len_months[isleap(y)][m])
+ + + - +
+ ]
4857 : : {
4858 [ - + ]: 124 : if (noise)
4859 : 0 : warning(_("rule goes past start/end of month; \
4860 : : will not work with pre-2004 versions of zic"));
4861 : : }
4862 : : }
4863 : 138680 : t = omul(dayoff, SECSPERDAY);
4864 : 138680 : return tadd(t, rp->r_tod);
4865 : : }
4866 : :
4867 : : static void
4868 : 71180 : checkabbr(char const *string)
4869 : : {
4870 [ + - ]: 71180 : if (strcmp(string, GRANDPARENTED) != 0)
4871 : : {
4872 : : const char *cp;
4873 : : const char *mp;
4874 : :
4875 : 71180 : cp = string;
4876 : 71180 : mp = NULL;
4877 [ + + ]: 447508 : while (is_alpha(*cp) || is_digit(*cp)
4878 [ + + + + : 394476 : || *cp == '-' || *cp == '+')
+ + ]
4879 : 226928 : ++cp;
4880 [ - + - - ]: 71180 : if (noise && cp - string < 3)
4881 : 0 : mp = _("time zone abbreviation has fewer than 3 characters");
4882 [ - + ]: 71180 : if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
4883 : 0 : mp = _("time zone abbreviation has too many characters");
4884 [ - + ]: 71180 : if (*cp != '\0')
4885 : 0 : mp = _("time zone abbreviation differs from POSIX standard");
4886 [ - + ]: 71180 : if (mp != NULL)
4887 : 0 : warning("%s (%s)", mp, string);
4888 : : }
4889 : 71180 : }
4890 : :
4891 : : /*
4892 : : * Put into CHS, which currently contains *PNCHS bytes containing
4893 : : * NUL-terminated abbreviations none of which are suffixes of another,
4894 : : * the abbreviation ABBR including its trailing NUL.
4895 : : * If ABBR does not already appear in CHS,
4896 : : * possibly as a suffix of an existing abbreviation,
4897 : : * add ABBR to CHS, remove from CHS any abbreviation
4898 : : * that is a suffix of ABBR, and increment *PNCHS accordingly.
4899 : : * Return the index of ABBR after any modifications to CHS are made.
4900 : : *
4901 : : * If all abbreviations have already been added, this function
4902 : : * lets the caller look up the index of an existing abbreviation.
4903 : : */
4904 : : static int
4905 : 95356 : addabbr(char chs[TZ_MAX_CHARS], int *pnchs, char const *abbr)
4906 : : {
4907 : 95356 : int nchs = *pnchs;
4908 : 95356 : int alen = strlen(abbr),
4909 : 95356 : nchs_incr = alen + 1;
4910 : : int i;
4911 : :
4912 [ + + ]: 317516 : for (i = 0; i < nchs;)
4913 : : {
4914 : 300160 : int clen = strlen(&chs[i]);
4915 : :
4916 [ + + ]: 300160 : if (alen <= clen)
4917 : : {
4918 : : /*
4919 : : * If ABBR is a suffix of an abbreviation in CHS, return the index
4920 : : * of ABBR in CHS.
4921 : : */
4922 : 266592 : int isuff = i + (clen - alen);
4923 : :
4924 [ + + ]: 266592 : if (memcmp(&chs[isuff], abbr, alen) == 0)
4925 : 77988 : return isuff;
4926 : : }
4927 [ + + ]: 33568 : else if (memcmp(&chs[i], &abbr[alen - clen], clen) == 0)
4928 : : {
4929 : : /*
4930 : : * An abbreviation in CHS is a substring of ABBR. Replace it with
4931 : : * ABBR, instead of the more-common actions of appending ABBR or
4932 : : * doing nothing.
4933 : : */
4934 : 12 : nchs_incr = alen - clen;
4935 : 12 : break;
4936 : : }
4937 : 222160 : i += clen + 1;
4938 : : }
4939 [ - + ]: 17368 : if (TZ_MAX_CHARS < nchs + nchs_incr)
4940 : : {
4941 : 0 : error(_("too many, or too long, time zone abbreviations"));
4942 : 0 : exit(EXIT_FAILURE);
4943 : : }
4944 : 17368 : memmove(&chs[i + nchs_incr], &chs[i], nchs - i);
4945 : 17368 : memcpy(&chs[i], abbr, nchs_incr);
4946 : 17368 : *pnchs = nchs + nchs_incr;
4947 : 17368 : return i;
4948 : : }
4949 : :
4950 : : /*
4951 : : * Ensure that the directories of ARGNAME exist, by making any missing
4952 : : * ones. If ANCESTORS, do this only for ARGNAME's ancestors; otherwise,
4953 : : * do it for ARGNAME too. Exit with failure if there is trouble.
4954 : : * Do not consider an existing file to be trouble.
4955 : : */
4956 : : static void
4957 : 42 : mkdirs(char const *argname, bool ancestors)
4958 : : {
4959 : : /*
4960 : : * If -D was specified, do not create directories. If a file operation's
4961 : : * parent directory is missing, the operation will fail and be diagnosed.
4962 : : */
4963 [ + - ]: 42 : if (!skip_mkdir)
4964 : : {
4965 : :
4966 : 42 : char *name = xstrdup(argname);
4967 : 42 : char *cp = name;
4968 : :
4969 : : /*
4970 : : * On MS-Windows systems, do not worry about drive letters or
4971 : : * backslashes, as this should suffice in practice. Time zone names
4972 : : * do not use drive letters and backslashes. If the -d option of zic
4973 : : * does not name an already-existing directory, it can use slashes to
4974 : : * separate the already-existing ancestor prefix from the
4975 : : * to-be-created subdirectories.
4976 : : */
4977 : :
4978 : : /* Do not mkdir a root directory, as it must exist. */
4979 [ + + ]: 44 : while (*cp == '/')
4980 : 2 : cp++;
4981 : :
4982 [ + + + + : 152 : while (cp && ((cp = strchr(cp, '/')) || !ancestors))
+ + ]
4983 : : {
4984 [ + + ]: 68 : if (cp)
4985 : 66 : *cp = '\0';
4986 : :
4987 : : /*
4988 : : * Try to create it. It's OK if creation fails because the
4989 : : * directory already exists, perhaps because some other process
4990 : : * just created it. For simplicity do not check first whether it
4991 : : * already exists, as that is checked anyway if the mkdir fails.
4992 : : */
4993 [ + + ]: 68 : if (mkdir(name, MKDIR_PERMS) < 0)
4994 : : {
4995 : : /*
4996 : : * Do not report an error if err == EEXIST, because some other
4997 : : * process might have made the directory in the meantime.
4998 : : * Likewise for ENOSYS, because Solaris 10 mkdir fails with
4999 : : * ENOSYS if the directory is an automounted mount point.
5000 : : * Likewise for EACCES, since mkdir can fail with EACCES
5001 : : * merely because the parent directory is unwritable. Likewise
5002 : : * for most other error numbers.
5003 : : */
5004 : 26 : int err = errno;
5005 : :
5006 [ + - + - ]: 26 : if (err == ELOOP || err == ENAMETOOLONG
5007 [ + - - + ]: 26 : || err == ENOENT || err == ENOTDIR)
5008 : : {
5009 : 0 : error(_("%s: Cannot create directory %s: %s"),
5010 : : progname, name, strerror(err));
5011 : 0 : exit(EXIT_FAILURE);
5012 : : }
5013 : : }
5014 [ + + ]: 68 : if (cp)
5015 : 66 : *cp++ = '/';
5016 : : }
5017 : 42 : free(name);
5018 : : }
5019 : 42 : }
|