LCOV - differential code coverage report
Current view: top level - src/timezone - zic.c (source / functions) Coverage Total Hit UNC LBC UIC UBC GBC GIC GNC CBC EUB ECB DUB DCB
Current: 77aeca80249c9e640c811e80633a2e334a9320de vs 38afc3dcb25c45b744d4025029ce0a6c90b7059f Lines: 59.0 % 2149 1267 380 1 501 5 501 761 1 248 353
Current Date: 2026-07-25 19:08:27 +0900 Functions: 74.0 % 100 74 21 5 59 15 2 18
Baseline: lcov-20260725-baseline Branches: 49.3 % 1614 796 344 1 5 468 1 292 503 2
Baseline Date: 2026-07-25 19:09:19 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 56.9 % 881 501 380 501
(30,360] days: 15.4 % 13 2 11 2
(360..) days: 60.9 % 1255 764 1 490 5 759 1
Function coverage date bins:
(7,30] days: 72.3 % 47 34 13 34
(30,360] days: 0.0 % 3 0 1 2
(360..) days: 80.0 % 50 40 7 3 25 15
Branch coverage date bins:
(7,30] days: 45.9 % 636 292 344 292
(30,360] days: 10.0 % 20 2 18 2
(360..) days: 52.3 % 960 502 1 5 450 1 501 2

 Age         Owner                    Branch data    TLA  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
   24 tgl@sss.pgh.pa.us         212                 :GNC       37350 : is_digit(char c)
                                213                 :                : {
                                214   [ +  +  +  - ]:          37350 :     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
 3771 tgl@sss.pgh.pa.us         498                 :UBC           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
   24 tgl@sss.pgh.pa.us         505                 :UNC           0 : size_overflow(void)
                                506                 :                : {
                                507                 :              0 :     memory_exhausted(_("size overflow"));
                                508                 :                : }
                                509                 :                : 
                                510                 :                : ATTRIBUTE_PURE_114833_HACK
                                511                 :                : static ptrdiff_t
   24 tgl@sss.pgh.pa.us         512                 :GNC         682 : size_sum(size_t a, size_t b)
                                513                 :                : {
                                514                 :                : #ifdef ckd_add
                                515                 :                :     ptrdiff_t   sum;
                                516                 :                : 
                                517         [ +  - ]:            682 :     if (!ckd_add(&sum, a, b) && sum <= INDEX_MAX)
                                518                 :            682 :         return sum;
                                519                 :                : #else
                                520                 :                :     if (a <= INDEX_MAX && b <= INDEX_MAX - a)
                                521                 :                :         return a + b;
                                522                 :                : #endif
   24 tgl@sss.pgh.pa.us         523                 :UNC           0 :     size_overflow();
                                524                 :                : }
                                525                 :                : 
                                526                 :                : ATTRIBUTE_PURE_114833_HACK
                                527                 :                : static ptrdiff_t
   24 tgl@sss.pgh.pa.us         528                 :GNC         341 : size_product(ptrdiff_t nitems, ptrdiff_t itemsize)
                                529                 :                : {
                                530                 :                : #ifdef ckd_mul
                                531                 :                :     ptrdiff_t   product;
                                532                 :                : 
                                533         [ +  - ]:            341 :     if (!ckd_mul(&product, nitems, itemsize) && product <= INDEX_MAX)
                                534                 :            341 :         return product;
                                535                 :                : #else
                                536                 :                :     ptrdiff_t   nitems_max = INDEX_MAX / itemsize;
                                537                 :                : 
                                538                 :                :     if (nitems <= nitems_max)
                                539                 :                :         return nitems * itemsize;
                                540                 :                : #endif
   24 tgl@sss.pgh.pa.us         541                 :UNC           0 :     size_overflow();
                                542                 :                : }
                                543                 :                : 
                                544                 :                : ATTRIBUTE_PURE_114833_HACK
                                545                 :                : static ptrdiff_t
   24 tgl@sss.pgh.pa.us         546                 :GNC         341 : align_to(ptrdiff_t size, ptrdiff_t alignment)
                                547                 :                : {
                                548                 :            341 :     ptrdiff_t   lo_bits = alignment - 1,
                                549                 :            341 :                 sum = size_sum(size, lo_bits);
                                550                 :                : 
                                551                 :            341 :     return sum & ~lo_bits;
                                552                 :                : }
                                553                 :                : 
                                554                 :                : static void *
 3771 tgl@sss.pgh.pa.us         555                 :CBC       18129 : memcheck(void *ptr)
                                556                 :                : {
 8100 bruce@momjian.us          557         [ -  + ]:          18129 :     if (ptr == NULL)
 3771 tgl@sss.pgh.pa.us         558                 :UBC           0 :         memory_exhausted(strerror(errno));
 3771 tgl@sss.pgh.pa.us         559                 :CBC       18129 :     return ptr;
                                560                 :                : }
                                561                 :                : 
                                562                 :                : static void *
   24 tgl@sss.pgh.pa.us         563                 :GNC        1705 : xmalloc(size_t size)
                                564                 :                : {
 3771 tgl@sss.pgh.pa.us         565                 :CBC        1705 :     return memcheck(malloc(size));
                                566                 :                : }
                                567                 :                : 
                                568                 :                : static void *
   24 tgl@sss.pgh.pa.us         569                 :GNC          62 : xrealloc(void *ptr, size_t size)
                                570                 :                : {
 3771 tgl@sss.pgh.pa.us         571                 :CBC          62 :     return memcheck(realloc(ptr, size));
                                572                 :                : }
                                573                 :                : 
                                574                 :                : static char *
   24 tgl@sss.pgh.pa.us         575                 :GNC       16362 : xstrdup(char const *str)
                                576                 :                : {
 3771 tgl@sss.pgh.pa.us         577                 :CBC       16362 :     return memcheck(strdup(str));
                                578                 :                : }
                                579                 :                : 
                                580                 :                : static ptrdiff_t
   24 tgl@sss.pgh.pa.us         581                 :GNC          62 : grow_nitems_alloc(ptrdiff_t *nitems_alloc, ptrdiff_t itemsize)
                                582                 :                : {
                                583                 :             62 :     ptrdiff_t   addend = (*nitems_alloc >> 1) + 1;
                                584                 :                : #if defined ckd_add && defined ckd_mul
                                585                 :                :     ptrdiff_t   product;
                                586                 :                : 
                                587         [ +  - ]:             62 :     if (!ckd_add(nitems_alloc, *nitems_alloc, addend)
                                588         [ +  - ]:             62 :         && !ckd_mul(&product, *nitems_alloc, itemsize) && product <= INDEX_MAX)
                                589                 :             62 :         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
   24 tgl@sss.pgh.pa.us         597                 :UNC           0 :     memory_exhausted(_("integer overflow"));
                                598                 :                : }
                                599                 :                : 
                                600                 :                : static void *
   24 tgl@sss.pgh.pa.us         601                 :GNC       21753 : growalloc(void *ptr, ptrdiff_t itemsize, ptrdiff_t nitems,
                                602                 :                :           ptrdiff_t *nitems_alloc)
                                603                 :                : {
                                604                 :          21753 :     return (nitems < *nitems_alloc
                                605                 :                :             ? ptr
                                606         [ +  + ]:          21753 :             : 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                 :              1 : filename(int i)
                                625                 :                : {
                                626         [ -  + ]:              1 :     if (i == COMMAND_LINE_FILENUM)
   24 tgl@sss.pgh.pa.us         627                 :UNC           0 :         return _("command line");
                                628                 :                :     else
                                629                 :                :     {
   24 tgl@sss.pgh.pa.us         630         [ +  - ]:GNC           1 :         char const *fname = i == LEAPSEC_FILENUM ? leapsec : main_argv[i];
                                631                 :                : 
                                632         [ +  - ]:              1 :         return strcmp(fname, "-") == 0 ? _("standard input") : fname;
                                633                 :                :     }
                                634                 :                : }
                                635                 :                : 
                                636                 :                : static void
                                637                 :        1465816 : eats(int fnum, lineno_t num, int rfnum, lineno_t rnum)
                                638                 :                : {
                                639                 :        1465816 :     filenum = fnum;
 8121 bruce@momjian.us          640                 :CBC     1465816 :     linenum = num;
   24 tgl@sss.pgh.pa.us         641                 :GNC     1465816 :     rfilenum = rfnum;
 8121 bruce@momjian.us          642                 :CBC     1465816 :     rlinenum = rnum;
                                643                 :        1465816 : }
                                644                 :                : 
                                645                 :                : static void
   24 tgl@sss.pgh.pa.us         646                 :GNC        8418 : eat(int fnum, lineno_t num)
                                647                 :                : {
                                648                 :           8418 :     eats(fnum, num, 0, -1);
 8121 bruce@momjian.us          649                 :CBC        8418 : }
                                650                 :                : 
                                651                 :                : static void
  246 peter@eisentraut.org      652                 :UBC           0 : verror(const char *const string, va_list args)
                                653                 :                : {
   24 tgl@sss.pgh.pa.us         654                 :UNC           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);
 3771 tgl@sss.pgh.pa.us         663                 :UBC           0 :     vfprintf(stderr, string, args);
   24 tgl@sss.pgh.pa.us         664         [ #  # ]:UNC           0 :     if (rfilenum)
  246 peter@eisentraut.org      665                 :UBC           0 :         fprintf(stderr, _(" (rule from \"%s\", line %" PRIdMAX ")"),
                                666                 :                :                 filename(rfilenum), rlinenum);
 3771 tgl@sss.pgh.pa.us         667                 :              0 :     fprintf(stderr, "\n");
 8121 bruce@momjian.us          668                 :              0 : }
                                669                 :                : 
                                670                 :                : static void
   73 tgl@sss.pgh.pa.us         671                 :              0 : error(const char *const string, ...)
                                672                 :                : {
                                673                 :                :     va_list     args;
                                674                 :                : 
 3771                           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
   73                           682                 :              0 : warning(const char *const string, ...)
                                683                 :                : {
                                684                 :                :     va_list     args;
                                685                 :                : 
 3771                           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
   24 tgl@sss.pgh.pa.us         698                 :UNC           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
   24 tgl@sss.pgh.pa.us         748                 :GNC         341 : chmetadata(FILE *stream)
                                749                 :                : {
                                750                 :                : #ifndef WIN32
                                751   [ +  -  -  + ]:            341 :     if (output_owner != no_uid || output_group != no_gid)
                                752                 :                :     {
   24 tgl@sss.pgh.pa.us         753                 :UNC           0 :         int         r = fchown(fileno(stream), output_owner, output_group);
                                754                 :                : 
                                755         [ #  # ]:              0 :         if (r < 0)
                                756                 :              0 :             return r;
                                757                 :                :     }
   24 tgl@sss.pgh.pa.us         758         [ -  + ]:GNC         341 :     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                 :            342 : close_file(FILE *stream, char const *dir, char const *name,
                                773                 :                :            char const *tempname)
                                774                 :                : {
 3771 tgl@sss.pgh.pa.us         775                 :CBC         342 :     char const *e = (ferror(stream) ? _("I/O error")
   24 tgl@sss.pgh.pa.us         776   [ +  -  +  + ]:GNC         684 :                      : ((tempname
                                777   [ +  -  +  - ]:            341 :                          && (fflush(stream) < 0 || chmetadata(stream) < 0))
                                778         [ -  + ]:            342 :                         || fclose(stream) < 0)
   24 tgl@sss.pgh.pa.us         779                 :UNC           0 :                      ? strerror(errno) : NULL);
                                780                 :                : 
 3771 tgl@sss.pgh.pa.us         781         [ -  + ]:CBC         342 :     if (e)
                                782                 :                :     {
   24 tgl@sss.pgh.pa.us         783   [ #  #  #  # ]:UNC           0 :         if (name && *name == '/')
                                784                 :              0 :             dir = NULL;
 3566 tgl@sss.pgh.pa.us         785   [ #  #  #  #  :UBC           0 :         fprintf(stderr, "%s: %s%s%s%s%s\n", progname,
                                        #  #  #  # ]
                                786                 :                :                 dir ? dir : "", dir ? "/" : "",
                                787                 :                :                 name ? name : "", name ? ": " : "",
                                788                 :                :                 e);
   24 tgl@sss.pgh.pa.us         789         [ #  # ]:UNC           0 :         if (tempname)
                                790                 :              0 :             remove(tempname);
 3771 tgl@sss.pgh.pa.us         791                 :UBC           0 :         exit(EXIT_FAILURE);
                                792                 :                :     }
 8121 bruce@momjian.us          793                 :CBC         342 : }
                                794                 :                : 
                                795                 :                : ATTRIBUTE_NORETURN static void
   24 tgl@sss.pgh.pa.us         796                 :UNC           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
 5980 tgl@sss.pgh.pa.us         803                 :UBC           0 : usage(FILE *stream, int status)
                                804                 :                : {
 3771                           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)
   24 tgl@sss.pgh.pa.us         815                 :UNC           0 :         close_file(stream, NULL, NULL, NULL);
 5980 tgl@sss.pgh.pa.us         816                 :UBC           0 :     exit(status);
                                817                 :                : }
                                818                 :                : 
                                819                 :                : static void
   24 tgl@sss.pgh.pa.us         820                 :UNC           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
   24 tgl@sss.pgh.pa.us         870                 :GNC           1 : use_safe_temp_permissions(void)
                                871                 :                : {
                                872   [ +  -  -  + ]:              1 :     if (output_owner != no_uid || output_group != no_gid)
                                873                 :                :     {
                                874                 :                : 
                                875                 :                :         /* The mode when done with the file.  */
                                876                 :                :         mode_t      omode;
                                877                 :                : 
   24 tgl@sss.pgh.pa.us         878         [ #  # ]:UNC           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                 :                :     }
   24 tgl@sss.pgh.pa.us         907                 :GNC           1 : }
                                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
 3321 tgl@sss.pgh.pa.us         915                 :CBC           1 : change_directory(char const *dir)
                                916                 :                : {
 3566                           917         [ +  - ]:              1 :     if (chdir(dir) != 0)
                                918                 :                :     {
                                919                 :              1 :         int         chdir_errno = errno;
                                920                 :                : 
                                921         [ +  - ]:              1 :         if (chdir_errno == ENOENT)
                                922                 :                :         {
                                923                 :              1 :             mkdirs(dir, false);
                                924         [ -  + ]:              1 :             chdir_errno = chdir(dir) == 0 ? 0 : errno;
                                925                 :                :         }
                                926         [ -  + ]:              1 :         if (chdir_errno != 0)
                                927                 :                :         {
 3566 tgl@sss.pgh.pa.us         928                 :UBC           0 :             fprintf(stderr, _("%s: Can't chdir to %s: %s\n"),
                                929                 :                :                     progname, dir, strerror(chdir_errno));
                                930                 :              0 :             exit(EXIT_FAILURE);
                                931                 :                :         }
                                932                 :                :     }
 3566 tgl@sss.pgh.pa.us         933                 :CBC           1 : }
                                934                 :                : 
                                935                 :                : /* Compare the two links A and B, for a stable sort by link name.  */
                                936                 :                : static int
   24 tgl@sss.pgh.pa.us         937                 :GNC        2102 : qsort_linkcmp(void const *a, void const *b)
                                938                 :                : {
                                939                 :           2102 :     struct link const *l = a;
                                940                 :           2102 :     struct link const *m = b;
                                941                 :           2102 :     int         cmp = strcmp(l->l_linkname, m->l_linkname);
                                942                 :                : 
                                943         [ +  - ]:           2102 :     if (cmp)
                                944                 :           2102 :         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                 :                :      */
   24 tgl@sss.pgh.pa.us         951                 :UNC           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
   24 tgl@sss.pgh.pa.us         959                 :GNC        1777 : bsearch_linkcmp(void const *key, void const *b)
                                960                 :                : {
                                961                 :           1777 :     struct link const *m = b;
                                962                 :                : 
                                963                 :           1777 :     return strcmp(key, m->l_linkname);
                                964                 :                : }
                                965                 :                : 
                                966                 :                : /* Make the links specified by the Link lines.  */
                                967                 :                : static void
                                968                 :              1 : make_links(void)
                                969                 :                : {
                                970                 :                :     ptrdiff_t   i,
                                971                 :                :                 j,
                                972                 :                :                 nalinks,
                                973                 :                :                 pass_size;
                                974                 :                : 
                                975         [ +  - ]:              1 :     if (1 < nlinks)
                                976                 :              1 :         qsort(links, nlinks, sizeof *links, qsort_linkcmp);
                                977                 :                : 
                                978                 :                :     /* Ignore each link superseded by a later link with the same name.  */
                                979                 :              1 :     j = 0;
                                980         [ +  + ]:            258 :     for (i = 0; i < nlinks; i++)
                                981                 :                :     {
                                982                 :            257 :         while (i + 1 < nlinks
                                983   [ +  +  -  + ]:            257 :                && strcmp(links[i].l_linkname, links[i + 1].l_linkname) == 0)
   24 tgl@sss.pgh.pa.us         984                 :UNC           0 :             i++;
   24 tgl@sss.pgh.pa.us         985                 :GNC         257 :         links[j++] = links[i];
                                986                 :                :     }
                                987                 :              1 :     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                 :              1 :     j = nalinks = nlinks;
                               1015                 :                : 
                               1016         [ +  + ]:            258 :     for (i = 0; i < nalinks; i++)
                               1017                 :                :     {
                               1018                 :                :         struct link *l;
                               1019                 :                : 
                               1020                 :            257 :         eat(links[i].l_filenum, links[i].l_linenum);
                               1021                 :                : 
                               1022                 :                :         /* If this pass examined all its links, start the next pass.  */
                               1023         [ -  + ]:            257 :         if (i == j)
                               1024                 :                :         {
   24 tgl@sss.pgh.pa.us        1025         [ #  # ]:UNC           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                 :                :          */
   24 tgl@sss.pgh.pa.us        1039         [ -  + ]:GNC         257 :         if (strcmp(links[i].l_target, links[i].l_linkname) == 0)
                               1040                 :                :         {
   24 tgl@sss.pgh.pa.us        1041                 :UNC           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.  */
   24 tgl@sss.pgh.pa.us        1046                 :GNC         257 :         l = bsearch(links[i].l_target, &links[i + 1], j - (i + 1),
                               1047                 :                :                     sizeof *links, bsearch_linkcmp);
                               1048         [ +  - ]:            257 :         if (!l)
                               1049                 :            257 :             l = bsearch(links[i].l_target, &links[j], nalinks - j,
                               1050                 :                :                         sizeof *links, bsearch_linkcmp);
                               1051         [ +  - ]:            257 :         if (!l)
                               1052                 :            257 :             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                 :                :              */
   24 tgl@sss.pgh.pa.us        1059                 :UNC           0 :             links = growalloc(links, sizeof *links, nalinks, &nlinks_alloc);
                               1060                 :              0 :             links[nalinks++] = links[i];
                               1061                 :                :         }
                               1062                 :                : 
   24 tgl@sss.pgh.pa.us        1063   [ -  +  -  - ]:GNC         257 :         if (noise && i < nlinks)
                               1064                 :                :         {
   24 tgl@sss.pgh.pa.us        1065         [ #  # ]:UNC           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                 :                :         }
   24 tgl@sss.pgh.pa.us        1073                 :GNC         257 :         check_for_signal();
                               1074                 :                :     }
                               1075                 :              1 : }
                               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
   24 tgl@sss.pgh.pa.us        1086                 :UNC           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
   24 tgl@sss.pgh.pa.us        1096                 :GNC           1 : 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                 :                :     size_t      i;
                               1109                 :                : 
                               1110         [ +  + ]:              5 :     for (i = 0; i < sizeof signals / sizeof signals[0]; i++)
                               1111                 :                :     {
                               1112                 :                : #ifdef SA_SIGINFO
                               1113                 :                :         struct sigaction act0,
                               1114                 :                :                     act;
                               1115                 :                : 
                               1116                 :              4 :         act.sa_handler = signal_handler;
                               1117                 :              4 :         sigemptyset(&act.sa_mask);
                               1118                 :              4 :         act.sa_flags = 0;
                               1119         [ +  - ]:              4 :         if (sigaction(signals[i], &act, &act0) == 0
                               1120   [ +  -  -  + ]:              4 :             && !(act0.sa_flags & SA_SIGINFO) && act0.sa_handler == SIG_IGN)
                               1121                 :                :         {
   24 tgl@sss.pgh.pa.us        1122                 :UNC           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                 :                :     }
   24 tgl@sss.pgh.pa.us        1133                 :GNC           1 : }
                               1134                 :                : 
                               1135                 :                : /* If a signal has arrived, terminate zic with appropriate status.  */
                               1136                 :                : static void
                               1137                 :         109159 : check_for_signal(void)
                               1138                 :                : {
                               1139                 :         109159 :     int         sig = got_signal;
                               1140                 :                : 
                               1141         [ -  + ]:         109159 :     if (sig)
                               1142                 :                :     {
   24 tgl@sss.pgh.pa.us        1143                 :UNC           0 :         signal(sig, SIG_DFL);
                               1144                 :              0 :         raise(sig);
                               1145                 :              0 :         abort();                /* A bug in 'raise'.  */
                               1146                 :                :     }
   24 tgl@sss.pgh.pa.us        1147                 :GNC      109159 : }
                               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
 2647 tgl@sss.pgh.pa.us        1178                 :UBC           0 : timerange_option(char *timerange)
                               1179                 :                : {
  246 peter@eisentraut.org     1180                 :              0 :     intmax_t    lo = min_time,
 2647 tgl@sss.pgh.pa.us        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);
  246 peter@eisentraut.org     1189   [ #  #  #  #  :              0 :         if (lo_end == timerange + 1 || (lo == INTMAX_MAX && errno == ERANGE))
                                              #  # ]
 2647 tgl@sss.pgh.pa.us        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);
  246 peter@eisentraut.org     1197   [ #  #  #  # ]:              0 :         if (hi_end == lo_end + 2 || hi == INTMAX_MIN)
 2647 tgl@sss.pgh.pa.us        1198                 :              0 :             return false;
  246 peter@eisentraut.org     1199   [ #  #  #  # ]:              0 :         hi -= !(hi == INTMAX_MAX && errno == ERANGE);
                               1200                 :                :     }
 2647 tgl@sss.pgh.pa.us        1201   [ #  #  #  #  :              0 :     if (*hi_end || hi < lo || max_time < lo || hi < min_time)
                                        #  #  #  # ]
                               1202                 :              0 :         return false;
   24 tgl@sss.pgh.pa.us        1203                 :UNC           0 :     lo_time = max(lo, min_time);
                               1204                 :              0 :     hi_time = min(hi, max_time);
 2647 tgl@sss.pgh.pa.us        1205                 :UBC           0 :     return true;
                               1206                 :                : }
                               1207                 :                : 
                               1208                 :                : /* Generate redundant time stamps up to OPT.  Return true if successful.  */
                               1209                 :                : static bool
   24 tgl@sss.pgh.pa.us        1210                 :UNC           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
 2565 tgl@sss.pgh.pa.us        1243                 :CBC       20182 : want_bloat(void)
                               1244                 :                : {
                               1245                 :          20182 :     return 0 <= bloat;
                               1246                 :                : }
                               1247                 :                : 
                               1248                 :                : #ifndef ZIC_BLOAT_DEFAULT
                               1249                 :                : #define ZIC_BLOAT_DEFAULT "slim"
                               1250                 :                : #endif
                               1251                 :                : 
                               1252                 :                : int
 3228                          1253                 :              1 : main(int argc, char **argv)
 8121 bruce@momjian.us         1254                 :           1617 : {
                               1255                 :                :     int         c,
                               1256                 :                :                 k;
                               1257                 :                :     ptrdiff_t   i,
                               1258                 :                :                 j;
 2647 tgl@sss.pgh.pa.us        1259                 :              1 :     bool        timerange_given = false;
                               1260                 :                : 
   24 tgl@sss.pgh.pa.us        1261                 :GNC           1 :     main_argv = argv;
                               1262         [ +  - ]:              1 :     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                 :                :     }
 3551 tgl@sss.pgh.pa.us        1269         [ +  + ]:CBC           4 :     for (k = 1; k < argc; k++)
                               1270         [ -  + ]:              3 :         if (strcmp(argv[k], "--version") == 0)
                               1271                 :                :         {
 3771 tgl@sss.pgh.pa.us        1272                 :UBC           0 :             printf("zic %s\n", PG_VERSION);
   24 tgl@sss.pgh.pa.us        1273                 :UNC           0 :             close_file(stdout, NULL, NULL, NULL);
 3771 tgl@sss.pgh.pa.us        1274                 :UBC           0 :             return EXIT_SUCCESS;
                               1275                 :                :         }
 3551 tgl@sss.pgh.pa.us        1276         [ -  + ]:CBC           3 :         else if (strcmp(argv[k], "--help") == 0)
                               1277                 :                :         {
 5980 tgl@sss.pgh.pa.us        1278                 :UBC           0 :             usage(stdout, EXIT_SUCCESS);
                               1279                 :                :         }
   24 tgl@sss.pgh.pa.us        1280         [ +  + ]:GNC           2 :     while ((c = getopt(argc, argv, "b:d:Dg:l:L:m:p:Pr:R:st:u:vy:")) != -1)
 8100 bruce@momjian.us         1281   [ -  -  +  -  :CBC           1 :         switch (c)
                                     -  -  -  -  -  
                                     -  -  -  -  -  
                                           -  -  - ]
                               1282                 :                :         {
 8121 bruce@momjian.us         1283                 :UBC           0 :             default:
 5980 tgl@sss.pgh.pa.us        1284                 :              0 :                 usage(stderr, EXIT_FAILURE);
 2565                          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;
 8121 bruce@momjian.us         1301                 :CBC           1 :             case 'd':
   24 tgl@sss.pgh.pa.us        1302         [ -  + ]:GNC           1 :                 if (directory)
   24 tgl@sss.pgh.pa.us        1303                 :UNC           0 :                     duplicate_options("-d");
   24 tgl@sss.pgh.pa.us        1304                 :GNC           1 :                 directory = strdup(optarg);
                               1305                 :              1 :                 break;
   24 tgl@sss.pgh.pa.us        1306                 :UNC           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);
 8121 bruce@momjian.us         1316                 :LBC         (1) :                 break;
 8121 bruce@momjian.us         1317                 :UBC           0 :             case 'l':
   24 tgl@sss.pgh.pa.us        1318         [ #  # ]:UNC           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);
 8121 bruce@momjian.us         1326                 :UBC           0 :                 break;
                               1327                 :              0 :             case 'p':
   24 tgl@sss.pgh.pa.us        1328         [ #  # ]:UNC           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);
 8121 bruce@momjian.us         1334                 :UBC           0 :                 break;
 3004 tgl@sss.pgh.pa.us        1335                 :              0 :             case 't':
   24 tgl@sss.pgh.pa.us        1336         [ #  # ]:UNC           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                 :                :                 }
 3004 tgl@sss.pgh.pa.us        1350                 :UBC           0 :                 break;
 8121 bruce@momjian.us         1351                 :              0 :             case 'y':
 2108 tgl@sss.pgh.pa.us        1352                 :              0 :                 warning(_("-y ignored"));
 8121 bruce@momjian.us         1353                 :              0 :                 break;
                               1354                 :              0 :             case 'L':
   24 tgl@sss.pgh.pa.us        1355         [ #  # ]:UNC           0 :                 if (leapsec)
                               1356                 :              0 :                     duplicate_options("-L");
                               1357                 :              0 :                 leapsec = strdup(optarg);
 8121 bruce@momjian.us         1358                 :UBC           0 :                 break;
                               1359                 :              0 :             case 'v':
 3771 tgl@sss.pgh.pa.us        1360                 :              0 :                 noise = true;
 8121 bruce@momjian.us         1361                 :              0 :                 break;
 4872 tgl@sss.pgh.pa.us        1362                 :              0 :             case 'P':
 3771                          1363                 :              0 :                 print_abbrevs = true;
 4872                          1364                 :              0 :                 print_cutoff = time(NULL);
                               1365                 :              0 :                 break;
 2647                          1366                 :              0 :             case 'r':
                               1367         [ #  # ]:              0 :                 if (timerange_given)
   24 tgl@sss.pgh.pa.us        1368                 :UNC           0 :                     duplicate_options("-r");
 2647 tgl@sss.pgh.pa.us        1369         [ #  # ]:UBC           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;
   24 tgl@sss.pgh.pa.us        1378                 :UNC           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;
 8121 bruce@momjian.us         1386                 :UBC           0 :             case 's':
 3771 tgl@sss.pgh.pa.us        1387                 :              0 :                 warning(_("-s ignored"));
 8121 bruce@momjian.us         1388                 :              0 :                 break;
                               1389                 :                :         }
 8121 bruce@momjian.us         1390   [ +  -  -  + ]:CBC           1 :     if (optind == argc - 1 && strcmp(argv[optind], "=") == 0)
 5978 bruce@momjian.us         1391                 :UBC           0 :         usage(stderr, EXIT_FAILURE);    /* usage message by request */
   24 tgl@sss.pgh.pa.us        1392         [ -  + ]:GNC           1 :     if (hi_time + (hi_time < ZIC_MAX) < redundant_time)
                               1393                 :                :     {
   24 tgl@sss.pgh.pa.us        1394                 :UNC           0 :         fprintf(stderr, _("%s: -R time exceeds -r cutoff\n"), progname);
                               1395                 :              0 :         return EXIT_FAILURE;
                               1396                 :                :     }
   24 tgl@sss.pgh.pa.us        1397         [ -  + ]:GNC           1 :     if (redundant_time < lo_time)
   24 tgl@sss.pgh.pa.us        1398                 :UNC           0 :         redundant_time = lo_time;
 2565 tgl@sss.pgh.pa.us        1399         [ +  - ]:CBC           1 :     if (bloat == 0)
                               1400                 :                :     {
                               1401                 :                :         static char const bloat_default[] = ZIC_BLOAT_DEFAULT;
                               1402                 :                : 
 2102                          1403         [ +  - ]:              1 :         if (strcmp(bloat_default, "slim") == 0)
                               1404                 :              1 :             bloat = -1;
 2102 tgl@sss.pgh.pa.us        1405         [ #  # ]:UBC           0 :         else if (strcmp(bloat_default, "fat") == 0)
                               1406                 :              0 :             bloat = 1;
                               1407                 :                :         else
                               1408                 :              0 :             abort();            /* Configuration error.  */
                               1409                 :                :     }
 8121 bruce@momjian.us         1410         [ -  + ]:CBC           1 :     if (directory == NULL)
 8100 tgl@sss.pgh.pa.us        1411                 :UBC           0 :         directory = "data";
 3004 tgl@sss.pgh.pa.us        1412         [ +  - ]:CBC           1 :     if (tzdefault == NULL)
                               1413                 :              1 :         tzdefault = TZDEFAULT;
                               1414                 :                : 
 8100 bruce@momjian.us         1415   [ +  -  -  + ]:              1 :     if (optind < argc && leapsec != NULL)
                               1416                 :                :     {
   24 tgl@sss.pgh.pa.us        1417                 :UNC           0 :         infile(LEAPSEC_FILENUM, leapsec);
 8121 bruce@momjian.us         1418                 :UBC           0 :         adjleap();
                               1419                 :                :     }
                               1420                 :                : 
 3551 tgl@sss.pgh.pa.us        1421         [ +  + ]:CBC           2 :     for (k = optind; k < argc; k++)
   24 tgl@sss.pgh.pa.us        1422                 :GNC           1 :         infile(k, argv[k]);
 8121 bruce@momjian.us         1423         [ -  + ]:CBC           1 :     if (errors)
 3771 tgl@sss.pgh.pa.us        1424                 :UBC           0 :         return EXIT_FAILURE;
 8121 bruce@momjian.us         1425                 :CBC           1 :     associate();
   24 tgl@sss.pgh.pa.us        1426                 :GNC           1 :     use_safe_temp_permissions();
 3566 tgl@sss.pgh.pa.us        1427                 :CBC           1 :     change_directory(directory);
   24 tgl@sss.pgh.pa.us        1428                 :GNC           1 :     directory_ends_in_slash = directory[strlen(directory) - 1] == '/';
                               1429                 :              1 :     catch_signals();
 8100 bruce@momjian.us         1430         [ +  + ]:CBC         342 :     for (i = 0; i < nzones; i = j)
                               1431                 :                :     {
                               1432                 :                :         /*
                               1433                 :                :          * Find the next non-continuation zone entry.
                               1434                 :                :          */
 8121                          1435   [ +  +  +  + ]:           1958 :         for (j = i + 1; j < nzones && zones[j].z_name == NULL; ++j)
                               1436                 :           1617 :             continue;
                               1437                 :            341 :         outzone(&zones[i], j - i);
   24 tgl@sss.pgh.pa.us        1438                 :GNC         341 :         check_for_signal();
                               1439                 :                :     }
                               1440                 :              1 :     make_links();
 8100 bruce@momjian.us         1441         [ -  + ]:CBC           1 :     if (lcltime != NULL)
                               1442                 :                :     {
   24 tgl@sss.pgh.pa.us        1443                 :UNC           0 :         eat(COMMAND_LINE_FILENUM, 1);
 3004 tgl@sss.pgh.pa.us        1444                 :UBC           0 :         dolink(lcltime, tzdefault, true);
                               1445                 :                :     }
 8100 bruce@momjian.us         1446         [ -  + ]:CBC           1 :     if (psxrules != NULL)
                               1447                 :                :     {
   24 tgl@sss.pgh.pa.us        1448                 :UNC           0 :         eat(COMMAND_LINE_FILENUM, 1);
 3566 tgl@sss.pgh.pa.us        1449                 :UBC           0 :         dolink(psxrules, TZDEFRULES, true);
                               1450                 :                :     }
 3771 tgl@sss.pgh.pa.us        1451   [ -  +  -  -  :CBC           1 :     if (warnings && (ferror(stderr) || fclose(stderr) != 0))
                                              -  - ]
 3771 tgl@sss.pgh.pa.us        1452                 :UBC           0 :         return EXIT_FAILURE;
 3771 tgl@sss.pgh.pa.us        1453                 :CBC           1 :     return errors ? EXIT_FAILURE : EXIT_SUCCESS;
                               1454                 :                : }
                               1455                 :                : 
                               1456                 :                : static bool
 3321                          1457                 :           1177 : componentcheck(char const *name, char const *component,
                               1458                 :                :                char const *component_end)
                               1459                 :                : {
                               1460                 :                :     enum
                               1461                 :                :     {
                               1462                 :                :     component_len_max = 14};
 3551                          1463                 :           1177 :     ptrdiff_t   component_len = component_end - component;
                               1464                 :                : 
 3771                          1465         [ -  + ]:           1177 :     if (component_len == 0)
                               1466                 :                :     {
 3771 tgl@sss.pgh.pa.us        1467         [ #  # ]:UBC           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                 :                :     }
 3771 tgl@sss.pgh.pa.us        1478   [ +  -  +  + ]:CBC        1177 :     if (0 < component_len && component_len <= 2
                               1479   [ -  +  -  - ]:             14 :         && component[0] == '.' && component_end[-1] == '.')
                               1480                 :                :     {
 3551 tgl@sss.pgh.pa.us        1481                 :UBC           0 :         int         len = component_len;
                               1482                 :                : 
   24 tgl@sss.pgh.pa.us        1483                 :UNC           0 :         error(_("file name '%s' contains '%.*s' component"),
                               1484                 :                :               name, len, component);
                               1485                 :              0 :         return false;
                               1486                 :                :     }
   24 tgl@sss.pgh.pa.us        1487         [ -  + ]:GNC        1177 :     if (noise)
                               1488                 :                :     {
   24 tgl@sss.pgh.pa.us        1489   [ #  #  #  # ]:UNC           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                 :                :     }
   24 tgl@sss.pgh.pa.us        1497                 :GNC        1177 :     return true;
                               1498                 :                : }
                               1499                 :                : 
                               1500                 :                : static bool
                               1501                 :            598 : 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                 :            598 :     char const *component = name;
                               1519                 :                : 
                               1520         [ +  + ]:           9102 :     for (cp = name; *cp; cp++)
                               1521                 :                :     {
                               1522                 :           8504 :         unsigned char c = *cp;
                               1523                 :                : 
                               1524   [ -  +  -  - ]:           8504 :         if (noise && !strchr(benign, c))
                               1525                 :                :         {
   24 tgl@sss.pgh.pa.us        1526         [ #  # ]:UNC           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                 :                :         }
   24 tgl@sss.pgh.pa.us        1531         [ +  + ]:GNC        8504 :         if (c == '/')
                               1532                 :                :         {
                               1533         [ -  + ]:            579 :             if (!componentcheck(name, component, cp))
   24 tgl@sss.pgh.pa.us        1534                 :UNC           0 :                 return false;
   24 tgl@sss.pgh.pa.us        1535                 :GNC         579 :             component = cp + 1;
                               1536                 :                :         }
                               1537                 :                :     }
                               1538                 :            598 :     return componentcheck(name, component, cp);
                               1539                 :                : }
                               1540                 :                : 
                               1541                 :                : /* Return a random uint_fast64_t.  */
                               1542                 :                : static uint_fast64_t
                               1543                 :            341 : 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         [ +  + ]:            341 :         if (!initialized)
                               1575                 :                :         {
                               1576                 :              1 :             srand(time(NULL));
                               1577                 :              1 :             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                 :            341 :         uint_fast64_t rand_max = RAND_MAX,
                               1588         [ +  - ]:            341 :                     nrand = rand_max < UINT_FAST64_MAX ? rand_max + 1 : 0,
                               1589                 :            341 :                     rmod = INT_MAX < UINT_FAST64_MAX ? 0 : UINT_FAST64_MAX / nrand + 1,
                               1590                 :            341 :                     r = 0,
                               1591                 :            341 :                     rmax = 0;
                               1592                 :                : 
                               1593                 :            682 :         for (;; check_for_signal())
                               1594                 :            682 :         {
                               1595                 :           1023 :             uint_fast64_t rmax1 = rmax;
                               1596                 :                : 
                               1597         [ -  + ]:           1023 :             if (rmod)
                               1598                 :                :             {
                               1599                 :                :                 /*
                               1600                 :                :                  * Avoid signed integer overflow on theoretical platforms
                               1601                 :                :                  * where uint_fast64_t promotes to int.
                               1602                 :                :                  */
   24 tgl@sss.pgh.pa.us        1603                 :UNC           0 :                 rmax1 %= rmod;
                               1604                 :              0 :                 r %= rmod;
                               1605                 :                :             }
   24 tgl@sss.pgh.pa.us        1606                 :GNC        1023 :             rmax1 = nrand * rmax1 + rand_max;
                               1607                 :           1023 :             r = nrand * r + rand();
                               1608         [ +  - ]:           1023 :             rmax = rmax < rmax1 ? rmax1 : UINT_FAST64_MAX;
                               1609         [ +  + ]:           1023 :             if (UINT_FAST64_MAX <= rmax)
                               1610                 :            341 :                 break;
                               1611                 :                :         }
                               1612                 :                : 
                               1613                 :            341 :         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                 :            341 : random_dirent(char const **name, char **namealloc)
                               1626                 :                : {
                               1627                 :            341 :     char const *src = *name;
                               1628                 :            341 :     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                 :            341 :     int         suffixlen = 6;
                               1638                 :            341 :     char const *lastslash = strrchr(src, '/');
                               1639         [ +  + ]:            341 :     ptrdiff_t   dirlen = lastslash ? lastslash + 1 - src : 0;
                               1640                 :                :     int         i;
                               1641                 :                :     uint_fast64_t r;
                               1642                 :            341 :     uint_fast64_t base = alphabetlen;
                               1643                 :                : 
                               1644                 :                :     /* BASE**6 */
                               1645                 :            341 :     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                 :            341 :     uint_fast64_t unfair_min = -((UINTMAX_MAX % base__6 + 1) % base__6);
                               1655                 :                : 
                               1656         [ +  - ]:            341 :     if (!dst)
                               1657                 :                :     {
                               1658                 :            341 :         char       *cp = dst = xmalloc(size_sum(dirlen, prefixlen + suffixlen + 1));
                               1659                 :                : 
                               1660                 :            341 :         memcpy(cp, src, dirlen);
                               1661                 :            341 :         cp += dirlen;
                               1662                 :            341 :         memcpy(cp, prefix, prefixlen);
                               1663                 :            341 :         cp += prefixlen;
                               1664                 :            341 :         cp[suffixlen] = '\0';
                               1665                 :            341 :         *name = *namealloc = dst;
                               1666                 :                :     }
                               1667                 :                : 
   24 tgl@sss.pgh.pa.us        1668                 :UNC           0 :     for (;; check_for_signal())
                               1669                 :                :     {
   24 tgl@sss.pgh.pa.us        1670                 :GNC         341 :         r = get_rand_u64();
                               1671         [ +  - ]:            341 :         if (r < unfair_min)
                               1672                 :            341 :             break;
                               1673                 :                :     }
                               1674                 :                : 
                               1675         [ +  + ]:           2387 :     for (i = 0; i < suffixlen; i++)
                               1676                 :                :     {
                               1677                 :           2046 :         dst[dirlen + prefixlen + i] = alphabet[r % alphabetlen];
                               1678                 :           2046 :         r /= alphabetlen;
                               1679                 :                :     }
 3771 tgl@sss.pgh.pa.us        1680                 :GIC         341 : }
                               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 *
   24 tgl@sss.pgh.pa.us        1688                 :UNC           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 *
   24 tgl@sss.pgh.pa.us        1707                 :GNC         341 : open_outfile(char const **outname, char **tempname)
                               1708                 :                : {
                               1709                 :            341 :     bool        dirs_made = false;
                               1710                 :                : 
                               1711         [ +  - ]:            341 :     if (!*tempname)
                               1712                 :            341 :         random_dirent(outname, tempname);
                               1713                 :                : 
                               1714                 :             14 :     for (;; check_for_signal())
 8100 bruce@momjian.us         1715                 :GIC          14 :     {
   24 tgl@sss.pgh.pa.us        1716                 :GNC         355 :         int         oflags = O_WRONLY | O_BINARY | O_CREAT | O_EXCL;
                               1717                 :            355 :         int         fd = open(*outname, oflags, creat_perms);
                               1718                 :                :         int         err;
                               1719                 :                : 
                               1720         [ +  + ]:            355 :         if (fd < 0)
                               1721                 :             14 :             err = errno;
                               1722                 :                :         else
                               1723                 :                :         {
                               1724                 :            341 :             FILE       *fp = fdopen(fd, "wb");
                               1725                 :                : 
                               1726         [ +  - ]:            341 :             if (fp)
                               1727                 :            341 :                 return fp;
   24 tgl@sss.pgh.pa.us        1728                 :UNC           0 :             err = errno;
                               1729                 :              0 :             close(fd);
                               1730                 :                :         }
   24 tgl@sss.pgh.pa.us        1731   [ +  -  +  - ]:GNC          14 :         if (err == ENOENT && !dirs_made)
                               1732                 :                :         {
                               1733                 :             14 :             mkdirs(*outname, true);
                               1734                 :             14 :             dirs_made = true;
                               1735                 :                :         }
   24 tgl@sss.pgh.pa.us        1736         [ #  # ]:UNC           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
   24 tgl@sss.pgh.pa.us        1754                 :GNC         598 : rename_dest(char *tempname, char const *name)
                               1755                 :                : {
                               1756         [ +  + ]:            598 :     if (tempname)
                               1757                 :                :     {
                               1758         [ -  + ]:            341 :         if (rename(tempname, name) != 0)
                               1759                 :                :         {
   24 tgl@sss.pgh.pa.us        1760                 :UNC           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                 :                :         }
   24 tgl@sss.pgh.pa.us        1768                 :GNC         341 :         free(tempname);
                               1769                 :                :     }
                               1770                 :            598 : }
                               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 *
 2108 tgl@sss.pgh.pa.us        1781                 :UBC           0 : relname(char const *target, char const *linkname)
                               1782                 :                : {
                               1783                 :                :     size_t      i,
                               1784                 :                :                 taillen,
   24 tgl@sss.pgh.pa.us        1785                 :UNC           0 :                 dir_len = 0,
                               1786                 :              0 :                 dotdots = 0;
                               1787                 :                :     ptrdiff_t   dotdotetcsize,
                               1788                 :              0 :                 linksize = INDEX_MAX;
 2108 tgl@sss.pgh.pa.us        1789                 :UBC           0 :     char const *f = target;
 3565                          1790                 :              0 :     char       *result = NULL;
                               1791                 :                : 
 2108                          1792         [ #  # ]:              0 :     if (*linkname == '/')
                               1793                 :                :     {
                               1794                 :                :         /* Make F absolute too.  */
 3565                          1795                 :              0 :         size_t      len = strlen(directory);
   24 tgl@sss.pgh.pa.us        1796   [ #  #  #  # ]:UNC           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                 :                :     }
 2108 tgl@sss.pgh.pa.us        1810   [ #  #  #  # ]:UBC           0 :     for (i = 0; f[i] && f[i] == linkname[i]; i++)
 3565                          1811         [ #  # ]:              0 :         if (f[i] == '/')
                               1812                 :              0 :             dir_len = i + 1;
 2108                          1813         [ #  # ]:              0 :     for (; linkname[i]; i++)
                               1814   [ #  #  #  # ]:              0 :         dotdots += linkname[i] == '/' && linkname[i - 1] != '/';
 3551                          1815                 :              0 :     taillen = strlen(f + dir_len);
   24 tgl@sss.pgh.pa.us        1816                 :UNC           0 :     dotdotetcsize = size_sum(size_product(dotdots, 3), taillen + 1);
 3565 tgl@sss.pgh.pa.us        1817         [ #  # ]:UBC           0 :     if (dotdotetcsize <= linksize)
                               1818                 :                :     {
                               1819                 :                :         char       *cp;
                               1820                 :                : 
                               1821         [ #  # ]:              0 :         if (!result)
   24 tgl@sss.pgh.pa.us        1822                 :UNC           0 :             result = xmalloc(dotdotetcsize);
                               1823                 :              0 :         cp = result;
 3565 tgl@sss.pgh.pa.us        1824         [ #  # ]:UBC           0 :         for (i = 0; i < dotdots; i++)
                               1825                 :                :         {
   24 tgl@sss.pgh.pa.us        1826                 :UNC           0 :             memcpy(cp, "../", 3);
                               1827                 :              0 :             cp += 3;
                               1828                 :                :         }
                               1829                 :              0 :         memmove(cp, f + dir_len, taillen + 1);
                               1830                 :                :     }
 3565 tgl@sss.pgh.pa.us        1831                 :UBC           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
   24 tgl@sss.pgh.pa.us        1842                 :UNC           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
 2108 tgl@sss.pgh.pa.us        1851                 :CBC         257 : dolink(char const *target, char const *linkname, bool staysymlink)
                               1852                 :                : {
                               1853                 :            257 :     bool        linkdirs_made = false;
                               1854                 :                :     int         link_errno;
   24 tgl@sss.pgh.pa.us        1855                 :GNC         257 :     char       *tempname = NULL;
                               1856                 :            257 :     char const *outname = linkname;
                               1857                 :            257 :     int         targetissym = -2,
                               1858                 :            257 :                 linknameissym = -2;
                               1859                 :                : 
                               1860         [ -  + ]:            257 :     if (strcmp(target, "-") == 0)
                               1861                 :                :     {
   24 tgl@sss.pgh.pa.us        1862   [ #  #  #  #  :UNC           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                 :                : 
   24 tgl@sss.pgh.pa.us        1875                 :GNC           6 :     for (;; check_for_signal())
                               1876                 :                :     {
                               1877         [ +  + ]:            263 :         if (linkat(AT_FDCWD, target, AT_FDCWD, outname, AT_SYMLINK_FOLLOW)
                               1878                 :                :             == 0)
                               1879                 :                :         {
                               1880                 :            257 :             link_errno = 0;
                               1881                 :            257 :             break;
                               1882                 :                :         }
                               1883                 :              6 :         link_errno = errno;
                               1884                 :                :         /* Linux 2.6.16 and 2.6.17 mishandle AT_SYMLINK_FOLLOW.  */
                               1885         [ -  + ]:              6 :         if (link_errno == EINVAL)
   24 tgl@sss.pgh.pa.us        1886                 :UNC           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                 :                :          */
   24 tgl@sss.pgh.pa.us        1894         [ -  + ]:GNC           6 :         if (link_errno == ENOTSUP
   24 tgl@sss.pgh.pa.us        1895         [ #  # ]:UNC           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                 :                :         }
   24 tgl@sss.pgh.pa.us        1905   [ +  -  +  - ]:GNC           6 :         if (link_errno == EXDEV || link_errno == ENOTSUP)
                               1906                 :                :             break;
                               1907                 :                : 
                               1908         [ -  + ]:              6 :         if (link_errno == EEXIST)
                               1909                 :                :         {
   24 tgl@sss.pgh.pa.us        1910                 :UNC           0 :             staysymlink &= !tempname;
                               1911                 :              0 :             random_dirent(&outname, &tempname);
                               1912   [ #  #  #  # ]:              0 :             if (staysymlink && itssymlink(linkname, &linknameissym))
                               1913                 :              0 :                 break;
                               1914                 :                :         }
   24 tgl@sss.pgh.pa.us        1915   [ +  -  +  - ]:GNC           6 :         else if (link_errno == ENOENT && !linkdirs_made)
                               1916                 :                :         {
                               1917                 :              6 :             mkdirs(linkname, true);
                               1918                 :              6 :             linkdirs_made = true;
                               1919                 :                :         }
                               1920                 :                :         else
                               1921                 :                :         {
   24 tgl@sss.pgh.pa.us        1922                 :UNC           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                 :                :     }
 3566 tgl@sss.pgh.pa.us        1929         [ -  + ]:CBC         257 :     if (link_errno != 0)
                               1930                 :                :     {
                               1931                 :                : #ifdef HAVE_SYMLINK
 2108 tgl@sss.pgh.pa.us        1932                 :UBC           0 :         bool        absolute = *target == '/';
                               1933         [ #  # ]:              0 :         char       *linkalloc = absolute ? NULL : relname(target, linkname);
                               1934         [ #  # ]:              0 :         char const *contents = absolute ? target : linkalloc;
   24 tgl@sss.pgh.pa.us        1935                 :UNC           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                 :                :         }
 3565 tgl@sss.pgh.pa.us        1958                 :UBC           0 :         free(linkalloc);
 3566                          1959         [ #  # ]:              0 :         if (symlink_errno == 0)
                               1960                 :                :         {
   24 tgl@sss.pgh.pa.us        1961   [ #  #  #  # ]:UNC           0 :             if (link_errno != ENOTSUP && link_errno != EEXIST)
 3566 tgl@sss.pgh.pa.us        1962                 :UBC           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                 :                : 
 2108                          1972                 :              0 :             fp = fopen(target, "rb");
 3566                          1973         [ #  # ]:              0 :             if (!fp)
                               1974                 :                :             {
                               1975                 :              0 :                 char const *e = strerror(errno);
                               1976                 :                : 
   24 tgl@sss.pgh.pa.us        1977                 :UNC           0 :                 fprintf(stderr, _("%s: Can't read %s%s%s: %s\n"),
                               1978                 :                :                         progname, diagdir(target), diagslash(target), target, e);
 3566 tgl@sss.pgh.pa.us        1979                 :UBC           0 :                 exit(EXIT_FAILURE);
                               1980                 :                :             }
   24 tgl@sss.pgh.pa.us        1981                 :UNC           0 :             tp = open_outfile(&outname, &tempname);
                               1982         [ #  # ]:              0 :             for (; (c = getc(fp)) != EOF; check_for_signal())
 3566 tgl@sss.pgh.pa.us        1983                 :UBC           0 :                 putc(c, tp);
   24 tgl@sss.pgh.pa.us        1984                 :UNC           0 :             close_file(tp, directory, linkname, tempname);
                               1985                 :              0 :             close_file(fp, directory, target, NULL);
 3566 tgl@sss.pgh.pa.us        1986         [ #  # ]:UBC           0 :             if (link_errno != ENOTSUP)
                               1987                 :              0 :                 warning(_("copy used because hard link failed: %s"),
                               1988                 :                :                         strerror(link_errno));
                               1989                 :                : #ifdef HAVE_SYMLINK
   24 tgl@sss.pgh.pa.us        1990         [ #  # ]:UNC           0 :             else if (symlink_errno < 0)
                               1991                 :              0 :                 warning(_("copy used because symbolic link not obvious"));
 3566 tgl@sss.pgh.pa.us        1992         [ #  # ]:UBC           0 :             else if (symlink_errno != ENOTSUP)
                               1993                 :              0 :                 warning(_("copy used because symbolic link failed: %s"),
                               1994                 :                :                         strerror(symlink_errno));
                               1995                 :                : #endif
                               1996                 :                :         }
                               1997                 :                :     }
   24 tgl@sss.pgh.pa.us        1998                 :GNC         257 :     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
   24 tgl@sss.pgh.pa.us        2007                 :UNC           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
 8100 bruce@momjian.us         2031                 :CBC       12324 : rcomp(const void *cp1, const void *cp2)
                               2032                 :                : {
   24 tgl@sss.pgh.pa.us        2033                 :GNC       12324 :     struct rule const *r1 = cp1,
                               2034                 :          12324 :                *r2 = cp2;
                               2035                 :                : 
                               2036                 :          12324 :     return strcmp(r1->r_name, r2->r_name);
                               2037                 :                : }
                               2038                 :                : 
                               2039                 :                : static void
 8100 bruce@momjian.us         2040                 :CBC           1 : associate(void)
                               2041                 :                : {
                               2042                 :                :     struct zone *zp;
                               2043                 :                :     struct rule *rp;
                               2044                 :                :     ptrdiff_t   i,
                               2045                 :                :                 j,
                               2046                 :                :                 base,
                               2047                 :                :                 out;
                               2048                 :                : 
   24 tgl@sss.pgh.pa.us        2049         [ +  - ]:GNC           1 :     if (1 < nrules)
                               2050                 :                :     {
 3771 tgl@sss.pgh.pa.us        2051                 :CBC           1 :         qsort(rules, nrules, sizeof *rules, rcomp);
 8100 bruce@momjian.us         2052         [ +  + ]:           2084 :         for (i = 0; i < nrules - 1; ++i)
                               2053                 :                :         {
 8121                          2054                 :           2212 :             if (strcmp(rules[i].r_name,
 8100                          2055         [ +  + ]:           2083 :                        rules[i + 1].r_name) != 0)
                               2056                 :            129 :                 continue;
   24 tgl@sss.pgh.pa.us        2057         [ +  - ]:GNC        1954 :             if (rules[i].r_filenum == rules[i + 1].r_filenum)
 8100 bruce@momjian.us         2058                 :CBC        1954 :                 continue;
   24 tgl@sss.pgh.pa.us        2059                 :UNC           0 :             eat(rules[i].r_filenum, rules[i].r_linenum);
 8121 bruce@momjian.us         2060                 :UBC           0 :             warning(_("same rule name in multiple files"));
   24 tgl@sss.pgh.pa.us        2061                 :UNC           0 :             eat(rules[i + 1].r_filenum, rules[i + 1].r_linenum);
 8121 bruce@momjian.us         2062                 :UBC           0 :             warning(_("same rule name in multiple files"));
 8100                          2063         [ #  # ]:              0 :             for (j = i + 2; j < nrules; ++j)
                               2064                 :                :             {
 8121                          2065                 :              0 :                 if (strcmp(rules[i].r_name,
 8100                          2066         [ #  # ]:              0 :                            rules[j].r_name) != 0)
                               2067                 :              0 :                     break;
   24 tgl@sss.pgh.pa.us        2068         [ #  # ]:UNC           0 :                 if (rules[i].r_filenum == rules[j].r_filenum)
 8100 bruce@momjian.us         2069                 :UBC           0 :                     continue;
   24 tgl@sss.pgh.pa.us        2070                 :UNC           0 :                 if (rules[i + 1].r_filenum
                               2071         [ #  # ]:              0 :                     == rules[j].r_filenum)
 8100 bruce@momjian.us         2072                 :UBC           0 :                     continue;
 8121                          2073                 :              0 :                 break;
                               2074                 :                :             }
                               2075                 :              0 :             i = j - 1;
                               2076                 :                :         }
                               2077                 :                :     }
 8100 bruce@momjian.us         2078         [ +  + ]:CBC        1959 :     for (i = 0; i < nzones; ++i)
                               2079                 :                :     {
 8121                          2080                 :           1958 :         zp = &zones[i];
                               2081                 :           1958 :         zp->z_rules = NULL;
                               2082                 :           1958 :         zp->z_nrules = 0;
                               2083                 :                :     }
 8100                          2084         [ +  + ]:            131 :     for (base = 0; base < nrules; base = out)
                               2085                 :                :     {
 8121                          2086                 :            130 :         rp = &rules[base];
                               2087         [ +  + ]:           2084 :         for (out = base + 1; out < nrules; ++out)
                               2088         [ +  + ]:           2083 :             if (strcmp(rp->r_name, rules[out].r_name) != 0)
                               2089                 :            129 :                 break;
 8100                          2090         [ +  + ]:         254670 :         for (i = 0; i < nzones; ++i)
                               2091                 :                :         {
 8121                          2092                 :         254540 :             zp = &zones[i];
                               2093         [ +  + ]:         254540 :             if (strcmp(zp->z_rule, rp->r_name) != 0)
                               2094                 :         253782 :                 continue;
                               2095                 :            758 :             zp->z_rules = rp;
                               2096                 :            758 :             zp->z_nrules = out - base;
                               2097                 :                :         }
                               2098                 :                :     }
 8100                          2099         [ +  + ]:           1959 :     for (i = 0; i < nzones; ++i)
                               2100                 :                :     {
 8121                          2101                 :           1958 :         zp = &zones[i];
 8100                          2102         [ +  + ]:           1958 :         if (zp->z_nrules == 0)
                               2103                 :                :         {
                               2104                 :                :             /*
                               2105                 :                :              * Maybe we have a local standard time offset.
                               2106                 :                :              */
   24 tgl@sss.pgh.pa.us        2107                 :GNC        1200 :             eat(zp->z_filenum, zp->z_linenum);
 2565 tgl@sss.pgh.pa.us        2108                 :CBC        1200 :             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                 :                :              */
 3771                          2114         [ -  + ]:           1200 :             if (zp->z_format_specifier == 's')
 3771 tgl@sss.pgh.pa.us        2115                 :UBC           0 :                 error("%s", _("%s in ruleless zone"));
                               2116                 :                :         }
                               2117                 :                :     }
 8121 bruce@momjian.us         2118         [ -  + ]:CBC           1 :     if (errors)
 6734 tgl@sss.pgh.pa.us        2119                 :UBC           0 :         exit(EXIT_FAILURE);
 8121 bruce@momjian.us         2120                 :CBC           1 : }
                               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
   24 tgl@sss.pgh.pa.us        2129                 :GNC        4303 : inputline(FILE *fp, char *buf, ptrdiff_t bufsize)
                               2130                 :                : {
                               2131                 :           4303 :     ptrdiff_t   linelen = 0,
                               2132                 :                :                 ch;
                               2133                 :                : 
                               2134         [ +  + ]:         107519 :     for (; (ch = getc(fp)) != '\n'; check_for_signal())
                               2135                 :                :     {
                               2136         [ +  + ]:         103217 :         if (ch < 0)
                               2137                 :                :         {
                               2138         [ -  + ]:              1 :             if (ferror(fp))
                               2139                 :                :             {
   24 tgl@sss.pgh.pa.us        2140                 :UNC           0 :                 error(_("input error"));
                               2141                 :              0 :                 exit(EXIT_FAILURE);
                               2142                 :                :             }
   24 tgl@sss.pgh.pa.us        2143         [ +  - ]:GNC           1 :             if (linelen == 0)
                               2144                 :              1 :                 return false;
   24 tgl@sss.pgh.pa.us        2145                 :UNC           0 :             error(_("unterminated line"));
                               2146                 :              0 :             exit(EXIT_FAILURE);
                               2147                 :                :         }
   24 tgl@sss.pgh.pa.us        2148         [ -  + ]:GNC      103216 :         if (!ch)
                               2149                 :                :         {
   24 tgl@sss.pgh.pa.us        2150                 :UNC           0 :             error(_("NUL input byte"));
                               2151                 :              0 :             exit(EXIT_FAILURE);
                               2152                 :                :         }
   24 tgl@sss.pgh.pa.us        2153                 :GNC      103216 :         buf[linelen++] = ch;
                               2154         [ -  + ]:         103216 :         if (linelen == bufsize)
                               2155                 :                :         {
   24 tgl@sss.pgh.pa.us        2156                 :UNC           0 :             error(_("line too long"));
                               2157                 :              0 :             exit(EXIT_FAILURE);
                               2158                 :                :         }
                               2159                 :                :     }
   24 tgl@sss.pgh.pa.us        2160                 :GNC        4302 :     buf[linelen] = '\0';
                               2161                 :           4302 :     return true;
                               2162                 :                : }
                               2163                 :                : 
                               2164                 :                : static void
                               2165                 :              1 : infile(int fnum, char const *name)
                               2166                 :                : {
                               2167                 :                :     FILE       *fp;
                               2168                 :                :     const struct lookup *lp;
                               2169                 :                :     bool        wantcont;
                               2170                 :                :     lineno_t    num;
                               2171                 :                : 
 8100 bruce@momjian.us         2172         [ -  + ]:CBC           1 :     if (strcmp(name, "-") == 0)
                               2173                 :                :     {
 8121 bruce@momjian.us         2174                 :UBC           0 :         fp = stdin;
                               2175                 :                :     }
 8100 bruce@momjian.us         2176         [ -  + ]:CBC           1 :     else if ((fp = fopen(name, "r")) == NULL)
                               2177                 :                :     {
 8121 bruce@momjian.us         2178                 :UBC           0 :         const char *e = strerror(errno);
                               2179                 :                : 
 3771 tgl@sss.pgh.pa.us        2180                 :              0 :         fprintf(stderr, _("%s: Cannot open %s: %s\n"),
                               2181                 :                :                 progname, name, e);
 6734                          2182                 :              0 :         exit(EXIT_FAILURE);
                               2183                 :                :     }
 3771 tgl@sss.pgh.pa.us        2184                 :CBC           1 :     wantcont = false;
 8100 bruce@momjian.us         2185                 :              1 :     for (num = 1;; ++num)
 8100 bruce@momjian.us         2186                 :GIC        4302 :     {
                               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                 :                : 
   24 tgl@sss.pgh.pa.us        2195                 :GNC        4303 :         eat(fnum, num);
                               2196         [ +  + ]:           4303 :         if (!inputline(fp, buf, sizeof buf))
                               2197                 :              1 :             break;
                               2198                 :           4302 :         nfields = getfields(buf, fields,
                               2199                 :                :                             sizeof fields / sizeof *fields);
 8100 bruce@momjian.us         2200         [ +  + ]:CBC        4302 :         if (nfields == 0)
                               2201                 :                :         {
                               2202                 :                :             /* nothing to do */
                               2203                 :                :         }
                               2204         [ +  + ]:           4299 :         else if (wantcont)
                               2205                 :                :         {
 8121                          2206                 :           1617 :             wantcont = inzcont(fields, nfields);
                               2207                 :                :         }
                               2208                 :                :         else
                               2209                 :                :         {
 3228 tgl@sss.pgh.pa.us        2210                 :           2682 :             struct lookup const *line_codes
   24 tgl@sss.pgh.pa.us        2211         [ -  + ]:GNC        2682 :             = fnum < 0 ? leap_line_codes : zi_line_codes;
                               2212                 :                : 
 8121 bruce@momjian.us         2213                 :CBC        2682 :             lp = byword(fields[0], line_codes);
                               2214         [ -  + ]:           2682 :             if (lp == NULL)
 8121 bruce@momjian.us         2215                 :UBC           0 :                 error(_("input line of unknown type"));
                               2216                 :                :             else
 3551 tgl@sss.pgh.pa.us        2217   [ +  +  +  -  :CBC        2682 :                 switch (lp->l_value)
                                              -  - ]
                               2218                 :                :                 {
 8100 bruce@momjian.us         2219                 :           2084 :                     case LC_RULE:
                               2220                 :           2084 :                         inrule(fields, nfields);
 3771 tgl@sss.pgh.pa.us        2221                 :           2084 :                         wantcont = false;
 8100 bruce@momjian.us         2222                 :           2084 :                         break;
                               2223                 :            341 :                     case LC_ZONE:
                               2224                 :            341 :                         wantcont = inzone(fields, nfields);
                               2225                 :            341 :                         break;
                               2226                 :            257 :                     case LC_LINK:
                               2227                 :            257 :                         inlink(fields, nfields);
 3771 tgl@sss.pgh.pa.us        2228                 :            257 :                         wantcont = false;
 8100 bruce@momjian.us         2229                 :            257 :                         break;
 8100 bruce@momjian.us         2230                 :UBC           0 :                     case LC_LEAP:
 3228 tgl@sss.pgh.pa.us        2231                 :              0 :                         inleap(fields, nfields);
 3771                          2232                 :              0 :                         wantcont = false;
 8100 bruce@momjian.us         2233                 :              0 :                         break;
 2229 tgl@sss.pgh.pa.us        2234                 :              0 :                     case LC_EXPIRES:
                               2235                 :              0 :                         inexpires(fields, nfields);
                               2236                 :              0 :                         wantcont = false;
                               2237                 :              0 :                         break;
   24 tgl@sss.pgh.pa.us        2238                 :UNC           0 :                     default:
                               2239                 :              0 :                         unreachable();
                               2240                 :                :                 }
                               2241                 :                :         }
   24 tgl@sss.pgh.pa.us        2242                 :GNC        4302 :         check_for_signal();
                               2243                 :                :     }
                               2244                 :              1 :     close_file(fp, NULL, filename(fnum), NULL);
 8121 bruce@momjian.us         2245         [ -  + ]:CBC           1 :     if (wantcont)
 8121 bruce@momjian.us         2246                 :UBC           0 :         error(_("expected continuation line not found"));
 8121 bruce@momjian.us         2247                 :CBC           1 : }
                               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
 2836 tgl@sss.pgh.pa.us        2258                 :           8943 : gethms(char const *string, char const *errstring)
                               2259                 :                : {
                               2260                 :                :     zic_t       hh;
                               2261                 :                :     int         sign,
 3004                          2262                 :           8943 :                 mm = 0,
                               2263                 :           8943 :                 ss = 0;
                               2264                 :                :     char        hhx,
                               2265                 :                :                 mmx,
                               2266                 :                :                 ssx,
                               2267                 :           8943 :                 xr = '0',
                               2268                 :                :                 xs;
                               2269                 :           8943 :     int         tenths = 0;
                               2270                 :           8943 :     bool        ok = true;
                               2271                 :                : 
 8121 bruce@momjian.us         2272   [ +  -  +  + ]:           8943 :     if (string == NULL || *string == '\0')
                               2273                 :           1138 :         return 0;
 2836 tgl@sss.pgh.pa.us        2274         [ +  + ]:           7805 :     if (*string == '-')
                               2275                 :                :     {
 8121 bruce@momjian.us         2276                 :            987 :         sign = -1;
                               2277                 :            987 :         ++string;
                               2278                 :                :     }
                               2279                 :                :     else
 8100                          2280                 :           6818 :         sign = 1;
 3004 tgl@sss.pgh.pa.us        2281   [ -  -  -  +  :           7805 :     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                 :                :     {
 3004 tgl@sss.pgh.pa.us        2285                 :UBC           0 :         default:
                               2286                 :              0 :             ok = false;
                               2287                 :              0 :             break;
                               2288                 :              0 :         case 8:
   24 tgl@sss.pgh.pa.us        2289                 :UNC           0 :             ok = is_digit(xr);
                               2290                 :                :             ATTRIBUTE_FALLTHROUGH;
 3004 tgl@sss.pgh.pa.us        2291                 :UBC           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:
 3004 tgl@sss.pgh.pa.us        2298                 :CBC         397 :             ok &= mmx == ':';
                               2299                 :                :             ATTRIBUTE_FALLTHROUGH;
                               2300                 :            605 :         case 3:
                               2301                 :            605 :             ok &= hhx == ':';
                               2302                 :                :             ATTRIBUTE_FALLTHROUGH;
                               2303                 :           7805 :         case 1:
                               2304                 :           7805 :             break;
                               2305                 :                :     }
                               2306         [ -  + ]:           7805 :     if (!ok)
                               2307                 :                :     {
 3771 tgl@sss.pgh.pa.us        2308                 :UBC           0 :         error("%s", errstring);
 8100 bruce@momjian.us         2309                 :              0 :         return 0;
                               2310                 :                :     }
 6734 tgl@sss.pgh.pa.us        2311         [ +  - ]:CBC        7805 :     if (hh < 0 ||
                               2312   [ +  -  +  - ]:           7805 :         mm < 0 || mm >= MINSPERHOUR ||
                               2313   [ +  -  -  + ]:           7805 :         ss < 0 || ss > SECSPERMIN)
                               2314                 :                :     {
 3771 tgl@sss.pgh.pa.us        2315                 :UBC           0 :         error("%s", errstring);
 8100 bruce@momjian.us         2316                 :              0 :         return 0;
                               2317                 :                :     }
 3004 tgl@sss.pgh.pa.us        2318                 :CBC        7805 :     ss += 5 + ((ss ^ 1) & (xr == '0')) <= tenths;    /* Round to even.  */
 6734                          2319   [ -  +  -  - ]:           7805 :     if (noise && (hh > HOURSPERDAY ||
 6734 tgl@sss.pgh.pa.us        2320   [ #  #  #  #  :UBC           0 :                   (hh == HOURSPERDAY && (mm != 0 || ss != 0))))
                                              #  # ]
                               2321                 :              0 :         warning(_("values over 24 hours not handled by pre-2007 versions of zic"));
   24 tgl@sss.pgh.pa.us        2322                 :GNC        7805 :     return oadd(omul(hh, sign * SECSPERHOUR),
 3771 tgl@sss.pgh.pa.us        2323                 :CBC        7805 :                 sign * (mm * SECSPERMIN + ss));
                               2324                 :                : }
                               2325                 :                : 
                               2326                 :                : static zic_t
 2565                          2327                 :           3284 : getsave(char *field, bool *isdst)
                               2328                 :                : {
 3004                          2329                 :           3284 :     int         dst = -1;
                               2330                 :                :     zic_t       save;
   24 tgl@sss.pgh.pa.us        2331                 :GNC        3284 :     ptrdiff_t   fieldlen = strlen(field);
                               2332                 :                : 
 3004 tgl@sss.pgh.pa.us        2333         [ +  + ]:CBC        3284 :     if (fieldlen != 0)
                               2334                 :                :     {
                               2335                 :           2146 :         char       *ep = field + fieldlen - 1;
                               2336                 :                : 
                               2337      [ -  -  + ]:           2146 :         switch (*ep)
                               2338                 :                :         {
 3004 tgl@sss.pgh.pa.us        2339                 :UBC           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                 :                :     }
 2565 tgl@sss.pgh.pa.us        2349                 :CBC        3284 :     save = gethms(field, _("invalid saved time"));
                               2350         [ +  - ]:           3284 :     *isdst = dst < 0 ? save != 0 : dst;
                               2351                 :           3284 :     return save;
                               2352                 :                : }
                               2353                 :                : 
                               2354                 :                : static void
 7705 neilc@samurai.com        2355                 :           2084 : inrule(char **fields, int nfields)
                               2356                 :                : {
                               2357                 :                :     struct rule r;
                               2358                 :                : 
 8100 bruce@momjian.us         2359         [ -  + ]:           2084 :     if (nfields != RULE_FIELDS)
                               2360                 :                :     {
 8121 bruce@momjian.us         2361                 :UBC           0 :         error(_("wrong number of fields on Rule line"));
                               2362                 :              0 :         return;
                               2363                 :                :     }
 2836 tgl@sss.pgh.pa.us        2364         [ -  + ]:CBC        2084 :     switch (*fields[RF_NAME])
                               2365                 :                :     {
 2836 tgl@sss.pgh.pa.us        2366                 :UBC           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                 :                :     }
   24 tgl@sss.pgh.pa.us        2388                 :GNC        2084 :     r.r_filenum = filenum;
 8121 bruce@momjian.us         2389                 :CBC        2084 :     r.r_linenum = linenum;
 2565 tgl@sss.pgh.pa.us        2390                 :           2084 :     r.r_save = getsave(fields[RF_SAVE], &r.r_isdst);
   24 tgl@sss.pgh.pa.us        2391         [ -  + ]:GNC        2084 :     if (!rulesub(&r, fields[RF_LOYEAR], fields[RF_HIYEAR],
                               2392                 :           2084 :                  fields[RF_COMMAND], fields[RF_MONTH], fields[RF_DAY],
                               2393                 :           2084 :                  fields[RF_TOD]))
   24 tgl@sss.pgh.pa.us        2394                 :UNC           0 :         return;
   24 tgl@sss.pgh.pa.us        2395                 :GNC        2084 :     r.r_name = xstrdup(fields[RF_NAME]);
                               2396                 :           2084 :     r.r_abbrvar = xstrdup(fields[RF_ABBRVAR]);
 6734 tgl@sss.pgh.pa.us        2397         [ -  + ]:CBC        2084 :     if (max_abbrvar_len < strlen(r.r_abbrvar))
 6734 tgl@sss.pgh.pa.us        2398                 :UBC           0 :         max_abbrvar_len = strlen(r.r_abbrvar);
 3771 tgl@sss.pgh.pa.us        2399                 :CBC        2084 :     rules = growalloc(rules, sizeof *rules, nrules, &nrules_alloc);
 8121 bruce@momjian.us         2400                 :           2084 :     rules[nrules++] = r;
                               2401                 :                : }
                               2402                 :                : 
                               2403                 :                : static bool
 7705 neilc@samurai.com        2404                 :            341 : inzone(char **fields, int nfields)
                               2405                 :                : {
                               2406                 :                :     ptrdiff_t   i;
                               2407                 :                : 
 8100 bruce@momjian.us         2408   [ +  -  -  + ]:            341 :     if (nfields < ZONE_MINFIELDS || nfields > ZONE_MAXFIELDS)
                               2409                 :                :     {
 8121 bruce@momjian.us         2410                 :UBC           0 :         error(_("wrong number of fields on Zone line"));
 3771 tgl@sss.pgh.pa.us        2411                 :              0 :         return false;
                               2412                 :                :     }
 3004 tgl@sss.pgh.pa.us        2413   [ -  +  -  - ]:CBC         341 :     if (lcltime != NULL && strcmp(fields[ZF_NAME], tzdefault) == 0)
                               2414                 :                :     {
   24 tgl@sss.pgh.pa.us        2415                 :UNC           0 :         error(_("\"Zone %s\" line and -l option are mutually exclusive"),
                               2416                 :                :               tzdefault);
 3771 tgl@sss.pgh.pa.us        2417                 :UBC           0 :         return false;
                               2418                 :                :     }
 8100 bruce@momjian.us         2419   [ -  +  -  - ]:CBC         341 :     if (strcmp(fields[ZF_NAME], TZDEFRULES) == 0 && psxrules != NULL)
                               2420                 :                :     {
   24 tgl@sss.pgh.pa.us        2421                 :UNC           0 :         error(_("\"Zone %s\" line and -p option are mutually exclusive"),
                               2422                 :                :               TZDEFRULES);
 3771 tgl@sss.pgh.pa.us        2423                 :UBC           0 :         return false;
                               2424                 :                :     }
 8121 bruce@momjian.us         2425         [ +  + ]:CBC      355644 :     for (i = 0; i < nzones; ++i)
                               2426         [ +  + ]:         355303 :         if (zones[i].z_name != NULL &&
 8100                          2427         [ -  + ]:          57970 :             strcmp(zones[i].z_name, fields[ZF_NAME]) == 0)
                               2428                 :                :         {
 3551 tgl@sss.pgh.pa.us        2429                 :UBC           0 :             error(_("duplicate zone name %s"
                               2430                 :                :                     " (file \"%s\", line %" PRIdMAX ")"),
 3771                          2431                 :              0 :                   fields[ZF_NAME],
   24 tgl@sss.pgh.pa.us        2432                 :UNC           0 :                   filename(zones[i].z_filenum),
 3771 tgl@sss.pgh.pa.us        2433                 :UBC           0 :                   zones[i].z_linenum);
                               2434                 :              0 :             return false;
                               2435                 :                :         }
 3771 tgl@sss.pgh.pa.us        2436                 :CBC         341 :     return inzsub(fields, nfields, false);
                               2437                 :                : }
                               2438                 :                : 
                               2439                 :                : static bool
 7705 neilc@samurai.com        2440                 :           1617 : inzcont(char **fields, int nfields)
                               2441                 :                : {
 8100 bruce@momjian.us         2442   [ +  -  -  + ]:           1617 :     if (nfields < ZONEC_MINFIELDS || nfields > ZONEC_MAXFIELDS)
                               2443                 :                :     {
 8121 bruce@momjian.us         2444                 :UBC           0 :         error(_("wrong number of fields on Zone continuation line"));
 3771 tgl@sss.pgh.pa.us        2445                 :              0 :         return false;
                               2446                 :                :     }
 3771 tgl@sss.pgh.pa.us        2447                 :CBC        1617 :     return inzsub(fields, nfields, true);
                               2448                 :                : }
                               2449                 :                : 
                               2450                 :                : static bool
                               2451                 :           1958 : 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                 :                : 
 8100 bruce@momjian.us         2466         [ +  + ]:           1958 :     if (iscont)
                               2467                 :                :     {
 2565 tgl@sss.pgh.pa.us        2468                 :           1617 :         i_stdoff = ZFC_STDOFF;
 8121 bruce@momjian.us         2469                 :           1617 :         i_rule = ZFC_RULE;
                               2470                 :           1617 :         i_format = ZFC_FORMAT;
                               2471                 :           1617 :         i_untilyear = ZFC_TILYEAR;
                               2472                 :           1617 :         i_untilmonth = ZFC_TILMONTH;
                               2473                 :           1617 :         i_untilday = ZFC_TILDAY;
                               2474                 :           1617 :         i_untiltime = ZFC_TILTIME;
                               2475                 :                :     }
 3771 tgl@sss.pgh.pa.us        2476         [ -  + ]:            341 :     else if (!namecheck(fields[ZF_NAME]))
 3771 tgl@sss.pgh.pa.us        2477                 :UBC           0 :         return false;
                               2478                 :                :     else
                               2479                 :                :     {
 2565 tgl@sss.pgh.pa.us        2480                 :CBC         341 :         i_stdoff = ZF_STDOFF;
 8121 bruce@momjian.us         2481                 :            341 :         i_rule = ZF_RULE;
                               2482                 :            341 :         i_format = ZF_FORMAT;
                               2483                 :            341 :         i_untilyear = ZF_TILYEAR;
                               2484                 :            341 :         i_untilmonth = ZF_TILMONTH;
                               2485                 :            341 :         i_untilday = ZF_TILDAY;
                               2486                 :            341 :         i_untiltime = ZF_TILTIME;
                               2487                 :                :     }
   24 tgl@sss.pgh.pa.us        2488                 :GNC        1958 :     z.z_filenum = filenum;
 8121 bruce@momjian.us         2489                 :CBC        1958 :     z.z_linenum = linenum;
 2565 tgl@sss.pgh.pa.us        2490                 :           1958 :     z.z_stdoff = gethms(fields[i_stdoff], _("invalid UT offset"));
   24 tgl@sss.pgh.pa.us        2491                 :GNC        1958 :     cp = strchr(fields[i_format], '%');
                               2492         [ +  + ]:           1958 :     if (cp)
                               2493                 :                :     {
 3771 tgl@sss.pgh.pa.us        2494   [ +  +  +  -  :CBC        1215 :         if ((*++cp != 's' && *cp != 'z') || strchr(cp, '%')
                                              +  - ]
                               2495         [ -  + ]:           1215 :             || strchr(fields[i_format], '/'))
                               2496                 :                :         {
 8121 bruce@momjian.us         2497                 :UBC           0 :             error(_("invalid abbreviation format"));
 3771 tgl@sss.pgh.pa.us        2498                 :              0 :             return false;
                               2499                 :                :         }
                               2500                 :                :     }
 3771 tgl@sss.pgh.pa.us        2501         [ +  + ]:CBC        1958 :     z.z_format_specifier = cp ? *cp : '\0';
   24 tgl@sss.pgh.pa.us        2502                 :GNC        1958 :     format_len = strlen(fields[i_format]);
                               2503         [ +  + ]:           1958 :     if (max_format_len < format_len)
                               2504                 :              3 :         max_format_len = format_len;
 8121 bruce@momjian.us         2505                 :CBC        1958 :     hasuntil = nfields > i_untilyear;
 8100                          2506         [ +  + ]:           1958 :     if (hasuntil)
                               2507                 :                :     {
   24 tgl@sss.pgh.pa.us        2508                 :GNC        1617 :         z.z_untilrule.r_filenum = filenum;
 8121 bruce@momjian.us         2509                 :CBC        1617 :         z.z_untilrule.r_linenum = linenum;
   24 tgl@sss.pgh.pa.us        2510   [ +  +  +  +  :GNC        4740 :         if (!rulesub(
                                        +  +  -  + ]
                               2511                 :                :                      &z.z_untilrule,
                               2512                 :           1617 :                      fields[i_untilyear],
                               2513                 :                :                      "only",
                               2514                 :                :                      "",
                               2515                 :                :                      (nfields > i_untilmonth) ?
                               2516                 :           1238 :                      fields[i_untilmonth] : "Jan",
                               2517                 :            997 :                      (nfields > i_untilday) ? fields[i_untilday] : "1",
                               2518                 :            509 :                      (nfields > i_untiltime) ? fields[i_untiltime] : "0"))
   24 tgl@sss.pgh.pa.us        2519                 :UNC           0 :             return false;
 8121 bruce@momjian.us         2520                 :CBC        1617 :         z.z_untiltime = rpytime(&z.z_untilrule,
                               2521                 :                :                                 z.z_untilrule.r_loyear);
                               2522   [ +  +  +  - ]:           1617 :         if (iscont && nzones > 0 &&
 8100                          2523         [ -  + ]:           1305 :             zones[nzones - 1].z_untiltime >= z.z_untiltime)
                               2524                 :                :         {
   24 tgl@sss.pgh.pa.us        2525                 :UNC           0 :             error(_("Zone continuation line end time is"
                               2526                 :                :                     " not after end time of previous line"));
 3771 tgl@sss.pgh.pa.us        2527                 :UBC           0 :             return false;
                               2528                 :                :         }
                               2529                 :                :     }
   24 tgl@sss.pgh.pa.us        2530         [ +  + ]:GNC        1958 :     z.z_name = iscont ? NULL : xstrdup(fields[ZF_NAME]);
                               2531                 :           1958 :     z.z_rule = xstrdup(fields[i_rule]);
                               2532                 :           1958 :     z.z_format = cp1 = xstrdup(fields[i_format]);
                               2533         [ +  + ]:           1958 :     if (z.z_format_specifier == 'z')
                               2534                 :                :     {
                               2535                 :            769 :         cp1[cp - fields[i_format]] = 's';
                               2536         [ -  + ]:            769 :         if (noise)
   24 tgl@sss.pgh.pa.us        2537                 :UNC           0 :             warning(_("format '%s' not handled by pre-2015 versions of zic"),
                               2538                 :              0 :                     fields[i_format]);
                               2539                 :                :     }
 3771 tgl@sss.pgh.pa.us        2540                 :CBC        1958 :     zones = growalloc(zones, sizeof *zones, nzones, &nzones_alloc);
 8121 bruce@momjian.us         2541                 :           1958 :     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                 :           1958 :     return hasuntil;
                               2548                 :                : }
                               2549                 :                : 
                               2550                 :                : static zic_t
   24 tgl@sss.pgh.pa.us        2551                 :UNC           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                 :                : 
 8121 bruce@momjian.us         2565                 :UBC           0 :     dayoff = 0;
                               2566                 :              0 :     cp = fields[LP_YEAR];
  246 peter@eisentraut.org     2567         [ #  # ]:              0 :     if (sscanf(cp, "%" SCNdZIC "%c", &year, &xs) != 1)
                               2568                 :                :     {
                               2569                 :                :         /*
                               2570                 :                :          * Leapin' Lizards!
                               2571                 :                :          */
 8100 bruce@momjian.us         2572                 :              0 :         error(_("invalid leaping year"));
 2229 tgl@sss.pgh.pa.us        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                 :                :     }
 8121 bruce@momjian.us         2583                 :              0 :     j = EPOCH_YEAR;
 8100                          2584         [ #  # ]:              0 :     while (j != year)
                               2585                 :                :     {
                               2586         [ #  # ]:              0 :         if (year > j)
                               2587                 :                :         {
 8121                          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                 :                :         }
 3771 tgl@sss.pgh.pa.us        2596                 :              0 :         dayoff = oadd(dayoff, i);
                               2597                 :                :     }
 8100 bruce@momjian.us         2598         [ #  # ]:              0 :     if ((lp = byword(fields[LP_MONTH], mon_names)) == NULL)
                               2599                 :                :     {
 8121                          2600                 :              0 :         error(_("invalid month name"));
 2229 tgl@sss.pgh.pa.us        2601                 :              0 :         return -1;
                               2602                 :                :     }
 8121 bruce@momjian.us         2603                 :              0 :     month = lp->l_value;
                               2604                 :              0 :     j = TM_JANUARY;
 8100                          2605         [ #  # ]:              0 :     while (j != month)
                               2606                 :                :     {
 8121                          2607   [ #  #  #  #  :              0 :         i = len_months[isleap(year)][j];
                                              #  # ]
 3771 tgl@sss.pgh.pa.us        2608                 :              0 :         dayoff = oadd(dayoff, i);
 8121 bruce@momjian.us         2609                 :              0 :         ++j;
                               2610                 :                :     }
                               2611                 :              0 :     cp = fields[LP_DAY];
 3771 tgl@sss.pgh.pa.us        2612         [ #  # ]:              0 :     if (sscanf(cp, "%d%c", &day, &xs) != 1 ||
 8100 bruce@momjian.us         2613   [ #  #  #  #  :              0 :         day <= 0 || day > len_months[isleap(year)][month])
                                     #  #  #  #  #  
                                                 # ]
                               2614                 :                :     {
                               2615                 :              0 :         error(_("invalid day of month"));
 2229 tgl@sss.pgh.pa.us        2616                 :              0 :         return -1;
                               2617                 :                :     }
 3771                          2618                 :              0 :     dayoff = oadd(dayoff, day - 1);
   24 tgl@sss.pgh.pa.us        2619                 :UNC           0 :     t = omul(dayoff, SECSPERDAY);
 2836 tgl@sss.pgh.pa.us        2620                 :UBC           0 :     tod = gethms(fields[LP_TIME], _("invalid time of day"));
 2229                          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                 :                :     {
   24 tgl@sss.pgh.pa.us        2634                 :UNC           0 :         zic_t       t = getleapdatetime(fields, false);
                               2635                 :                : 
 2229 tgl@sss.pgh.pa.us        2636         [ #  # ]:UBC           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                 :                :     }
 8121 bruce@momjian.us         2657                 :              0 : }
                               2658                 :                : 
                               2659                 :                : static void
 2229 tgl@sss.pgh.pa.us        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
   24 tgl@sss.pgh.pa.us        2667                 :UNC           0 :         leapexpires = getleapdatetime(fields, true);
 2229 tgl@sss.pgh.pa.us        2668                 :UBC           0 : }
                               2669                 :                : 
                               2670                 :                : static void
 7705 neilc@samurai.com        2671                 :CBC         257 : inlink(char **fields, int nfields)
                               2672                 :                : {
                               2673                 :                :     struct link l;
                               2674                 :                : 
 8100 bruce@momjian.us         2675         [ -  + ]:            257 :     if (nfields != LINK_FIELDS)
                               2676                 :                :     {
 8121 bruce@momjian.us         2677                 :UBC           0 :         error(_("wrong number of fields on Link line"));
                               2678                 :              0 :         return;
                               2679                 :                :     }
 2108 tgl@sss.pgh.pa.us        2680         [ -  + ]:CBC         257 :     if (*fields[LF_TARGET] == '\0')
                               2681                 :                :     {
 2108 tgl@sss.pgh.pa.us        2682                 :UBC           0 :         error(_("blank TARGET field on Link line"));
 8121 bruce@momjian.us         2683                 :              0 :         return;
                               2684                 :                :     }
 2108 tgl@sss.pgh.pa.us        2685         [ -  + ]:CBC         257 :     if (!namecheck(fields[LF_LINKNAME]))
 8121 bruce@momjian.us         2686                 :UBC           0 :         return;
   24 tgl@sss.pgh.pa.us        2687                 :GNC         257 :     l.l_filenum = filenum;
 8121 bruce@momjian.us         2688                 :CBC         257 :     l.l_linenum = linenum;
   24 tgl@sss.pgh.pa.us        2689                 :GNC         257 :     l.l_target = xstrdup(fields[LF_TARGET]);
                               2690                 :            257 :     l.l_linkname = xstrdup(fields[LF_LINKNAME]);
 3771 tgl@sss.pgh.pa.us        2691                 :CBC         257 :     links = growalloc(links, sizeof *links, nlinks, &nlinks_alloc);
 8121 bruce@momjian.us         2692                 :            257 :     links[nlinks++] = l;
                               2693                 :                : }
                               2694                 :                : 
                               2695                 :                : static bool
 3321 tgl@sss.pgh.pa.us        2696                 :           3701 : 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                 :                : 
 8100 bruce@momjian.us         2706         [ -  + ]:           3701 :     if ((lp = byword(monthp, mon_names)) == NULL)
                               2707                 :                :     {
 8121 bruce@momjian.us         2708                 :UBC           0 :         error(_("invalid month name"));
   24 tgl@sss.pgh.pa.us        2709                 :UNC           0 :         return false;
                               2710                 :                :     }
 8121 bruce@momjian.us         2711                 :CBC        3701 :     rp->r_month = lp->l_value;
 3771 tgl@sss.pgh.pa.us        2712                 :           3701 :     rp->r_todisstd = false;
 2565                          2713                 :           3701 :     rp->r_todisut = false;
   24 tgl@sss.pgh.pa.us        2714                 :GNC        3701 :     dp = xstrdup(timep);
 8100 bruce@momjian.us         2715         [ +  - ]:CBC        3701 :     if (*dp != '\0')
                               2716                 :                :     {
 8121                          2717                 :           3701 :         ep = dp + strlen(dp) - 1;
 8100                          2718   [ +  -  +  + ]:           3701 :         switch (lowerit(*ep))
                               2719                 :                :         {
                               2720                 :            685 :             case 's':           /* Standard */
 3771 tgl@sss.pgh.pa.us        2721                 :            685 :                 rp->r_todisstd = true;
 2565                          2722                 :            685 :                 rp->r_todisut = false;
 8121 bruce@momjian.us         2723                 :            685 :                 *ep = '\0';
                               2724                 :            685 :                 break;
 8100 bruce@momjian.us         2725                 :UBC           0 :             case 'w':           /* Wall */
 3771 tgl@sss.pgh.pa.us        2726                 :              0 :                 rp->r_todisstd = false;
 2565                          2727                 :              0 :                 rp->r_todisut = false;
 8121 bruce@momjian.us         2728                 :              0 :                 *ep = '\0';
                               2729                 :              0 :                 break;
 8100 bruce@momjian.us         2730                 :CBC         169 :             case 'g':           /* Greenwich */
                               2731                 :                :             case 'u':           /* Universal */
                               2732                 :                :             case 'z':           /* Zulu */
 3771 tgl@sss.pgh.pa.us        2733                 :            169 :                 rp->r_todisstd = true;
 2565                          2734                 :            169 :                 rp->r_todisut = true;
 8121 bruce@momjian.us         2735                 :            169 :                 *ep = '\0';
                               2736                 :            169 :                 break;
                               2737                 :                :         }
                               2738                 :                :     }
 2836 tgl@sss.pgh.pa.us        2739                 :           3701 :     rp->r_tod = gethms(dp, _("invalid time of day"));
 3771                          2740                 :           3701 :     free(dp);
                               2741                 :                : 
                               2742                 :                :     /*
                               2743                 :                :      * Year work.
                               2744                 :                :      */
 8121 bruce@momjian.us         2745                 :           3701 :     cp = loyearp;
                               2746                 :           3701 :     lp = byword(cp, begin_years);
   24 tgl@sss.pgh.pa.us        2747         [ -  + ]:GNC        3701 :     if (lp)
 3551 tgl@sss.pgh.pa.us        2748      [ #  #  # ]:UBC           0 :         switch (lp->l_value)
                               2749                 :                :         {
 8100 bruce@momjian.us         2750                 :              0 :             case YR_MINIMUM:
   24 tgl@sss.pgh.pa.us        2751                 :UNC           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;
 8100 bruce@momjian.us         2755                 :UBC           0 :                 break;
   24 tgl@sss.pgh.pa.us        2756                 :UNC           0 :             default:
                               2757                 :              0 :                 unreachable();
                               2758                 :                :         }
  246 peter@eisentraut.org     2759         [ -  + ]:CBC        3701 :     else if (sscanf(cp, "%" SCNdZIC "%c", &rp->r_loyear, &xs) != 1)
                               2760                 :                :     {
 8121 bruce@momjian.us         2761                 :UBC           0 :         error(_("invalid starting year"));
   24 tgl@sss.pgh.pa.us        2762                 :UNC           0 :         return false;
                               2763                 :                :     }
 8121 bruce@momjian.us         2764                 :CBC        3701 :     cp = hiyearp;
 6734 tgl@sss.pgh.pa.us        2765                 :           3701 :     lp = byword(cp, end_years);
                               2766                 :           3701 :     rp->r_hiwasnum = lp == NULL;
                               2767         [ +  + ]:           3701 :     if (!rp->r_hiwasnum)
 3551                          2768   [ +  +  -  - ]:           3086 :         switch (lp->l_value)
                               2769                 :                :         {
 8100 bruce@momjian.us         2770                 :             46 :             case YR_MAXIMUM:
 3771 tgl@sss.pgh.pa.us        2771                 :             46 :                 rp->r_hiyear = ZIC_MAX;
 8100 bruce@momjian.us         2772                 :             46 :                 break;
                               2773                 :           3040 :             case YR_ONLY:
                               2774                 :           3040 :                 rp->r_hiyear = rp->r_loyear;
                               2775                 :           3040 :                 break;
   24 tgl@sss.pgh.pa.us        2776                 :UNC           0 :             default:
                               2777                 :              0 :                 unreachable();
                               2778                 :                :         }
  246 peter@eisentraut.org     2779         [ -  + ]:CBC         615 :     else if (sscanf(cp, "%" SCNdZIC "%c", &rp->r_hiyear, &xs) != 1)
                               2780                 :                :     {
 8121 bruce@momjian.us         2781                 :UBC           0 :         error(_("invalid ending year"));
   24 tgl@sss.pgh.pa.us        2782                 :UNC           0 :         return false;
                               2783                 :                :     }
 8100 bruce@momjian.us         2784         [ -  + ]:CBC        3701 :     if (rp->r_loyear > rp->r_hiyear)
                               2785                 :                :     {
 8121 bruce@momjian.us         2786                 :UBC           0 :         error(_("starting year greater than ending year"));
   24 tgl@sss.pgh.pa.us        2787                 :UNC           0 :         return false;
                               2788                 :                :     }
 2108 tgl@sss.pgh.pa.us        2789         [ -  + ]:CBC        3701 :     if (*typep != '\0')
                               2790                 :                :     {
 2108 tgl@sss.pgh.pa.us        2791                 :UBC           0 :         error(_("year type \"%s\" is unsupported; use \"-\" instead"),
                               2792                 :                :               typep);
   24 tgl@sss.pgh.pa.us        2793                 :UNC           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                 :                :      */
   24 tgl@sss.pgh.pa.us        2800                 :GNC        3701 :     dp = xstrdup(dayp);
 8100 bruce@momjian.us         2801         [ +  + ]:CBC        3701 :     if ((lp = byword(dp, lasts)) != NULL)
                               2802                 :                :     {
 8121                          2803                 :            342 :         rp->r_dycode = DC_DOWLEQ;
                               2804                 :            342 :         rp->r_wday = lp->l_value;
                               2805                 :            342 :         rp->r_dayofmonth = len_months[1][rp->r_month];
                               2806                 :                :     }
                               2807                 :                :     else
                               2808                 :                :     {
   24 tgl@sss.pgh.pa.us        2809                 :GNC        3359 :         ep = strchr(dp, '<');
                               2810         [ +  + ]:           3359 :         if (ep)
 8121 bruce@momjian.us         2811                 :CBC          10 :             rp->r_dycode = DC_DOWLEQ;
                               2812                 :                :         else
                               2813                 :                :         {
   24 tgl@sss.pgh.pa.us        2814                 :GNC        3349 :             ep = strchr(dp, '>');
                               2815         [ +  + ]:           3349 :             if (ep)
                               2816                 :            396 :                 rp->r_dycode = DC_DOWGEQ;
                               2817                 :                :             else
                               2818                 :                :             {
                               2819                 :           2953 :                 ep = dp;
                               2820                 :           2953 :                 rp->r_dycode = DC_DOM;
                               2821                 :                :             }
                               2822                 :                :         }
 8100 bruce@momjian.us         2823         [ +  + ]:CBC        3359 :         if (rp->r_dycode != DC_DOM)
                               2824                 :                :         {
 8121                          2825                 :            406 :             *ep++ = 0;
 8100                          2826         [ -  + ]:            406 :             if (*ep++ != '=')
                               2827                 :                :             {
 8121 bruce@momjian.us         2828                 :UBC           0 :                 error(_("invalid day of month"));
 3771 tgl@sss.pgh.pa.us        2829                 :              0 :                 free(dp);
   24 tgl@sss.pgh.pa.us        2830                 :UNC           0 :                 return false;
                               2831                 :                :             }
 8100 bruce@momjian.us         2832         [ -  + ]:CBC         406 :             if ((lp = byword(dp, wday_names)) == NULL)
                               2833                 :                :             {
 8121 bruce@momjian.us         2834                 :UBC           0 :                 error(_("invalid weekday name"));
 3771 tgl@sss.pgh.pa.us        2835                 :              0 :                 free(dp);
   24 tgl@sss.pgh.pa.us        2836                 :UNC           0 :                 return false;
                               2837                 :                :             }
 8121 bruce@momjian.us         2838                 :CBC         406 :             rp->r_wday = lp->l_value;
                               2839                 :                :         }
 3771 tgl@sss.pgh.pa.us        2840         [ +  - ]:           3359 :         if (sscanf(ep, "%d%c", &rp->r_dayofmonth, &xs) != 1 ||
 8121 bruce@momjian.us         2841         [ +  - ]:           3359 :             rp->r_dayofmonth <= 0 ||
 8100                          2842         [ -  + ]:           3359 :             (rp->r_dayofmonth > len_months[1][rp->r_month]))
                               2843                 :                :         {
 8100 bruce@momjian.us         2844                 :UBC           0 :             error(_("invalid day of month"));
 3771 tgl@sss.pgh.pa.us        2845                 :              0 :             free(dp);
   24 tgl@sss.pgh.pa.us        2846                 :UNC           0 :             return false;
                               2847                 :                :         }
                               2848                 :                :     }
 3771 tgl@sss.pgh.pa.us        2849                 :CBC        3701 :     free(dp);
   24 tgl@sss.pgh.pa.us        2850                 :GNC        3701 :     return true;
                               2851                 :                : }
                               2852                 :                : 
                               2853                 :                : static void
                               2854                 :           6021 : convert(uint_fast32_t val, char *buf)
                               2855                 :                : {
                               2856                 :                :     int         i;
                               2857                 :                :     int         shift;
 3771 tgl@sss.pgh.pa.us        2858                 :CBC        6021 :     unsigned char *const b = (unsigned char *) buf;
                               2859                 :                : 
 8121 bruce@momjian.us         2860         [ +  + ]:          30105 :     for (i = 0, shift = 24; i < 4; ++i, shift -= 8)
   24 tgl@sss.pgh.pa.us        2861                 :GNC       24084 :         b[i] = (val >> shift) & 0xff;
 8121 bruce@momjian.us         2862                 :CBC        6021 : }
                               2863                 :                : 
                               2864                 :                : static void
   24 tgl@sss.pgh.pa.us        2865                 :GNC       16842 : convert64(uint_fast64_t val, char *buf)
                               2866                 :                : {
                               2867                 :                :     int         i;
                               2868                 :                :     int         shift;
 3771 tgl@sss.pgh.pa.us        2869                 :CBC       16842 :     unsigned char *const b = (unsigned char *) buf;
                               2870                 :                : 
 6734                          2871         [ +  + ]:         151578 :     for (i = 0, shift = 56; i < 8; ++i, shift -= 8)
   24 tgl@sss.pgh.pa.us        2872                 :GNC      134736 :         b[i] = (val >> shift) & 0xff;
 6734 tgl@sss.pgh.pa.us        2873                 :CBC       16842 : }
                               2874                 :                : 
                               2875                 :                : static void
   24 tgl@sss.pgh.pa.us        2876                 :GNC        1929 : puttzcode(zic_t val, FILE *fp)
                               2877                 :                : {
                               2878                 :                :     char        buf[4];
                               2879                 :                : 
 8121 bruce@momjian.us         2880                 :CBC        1929 :     convert(val, buf);
 3771 tgl@sss.pgh.pa.us        2881                 :           1929 :     fwrite(buf, sizeof buf, 1, fp);
 8121 bruce@momjian.us         2882                 :           1929 : }
                               2883                 :                : 
                               2884                 :                : static void
 2647 tgl@sss.pgh.pa.us        2885                 :          16842 : puttzcodepass(zic_t val, FILE *fp, int pass)
                               2886                 :                : {
                               2887         [ -  + ]:          16842 :     if (pass == 1)
 2647 tgl@sss.pgh.pa.us        2888                 :UBC           0 :         puttzcode(val, fp);
                               2889                 :                :     else
                               2890                 :                :     {
                               2891                 :                :         char        buf[8];
                               2892                 :                : 
 2647 tgl@sss.pgh.pa.us        2893                 :CBC       16842 :         convert64(val, buf);
                               2894                 :          16842 :         fwrite(buf, sizeof buf, 1, fp);
                               2895                 :                :     }
 6734                          2896                 :          16842 : }
                               2897                 :                : 
                               2898                 :                : static int
 8100 bruce@momjian.us         2899                 :         207897 : atcomp(const void *avp, const void *bvp)
                               2900                 :                : {
   24 tgl@sss.pgh.pa.us        2901                 :GNC      207897 :     struct attype const *ap = avp,
                               2902                 :         207897 :                *bp = bvp;
                               2903                 :         207897 :     zic_t       a = ap->at,
                               2904                 :         207897 :                 b = bp->at;
                               2905                 :                : 
                               2906         [ +  + ]:         207897 :     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
 2647 tgl@sss.pgh.pa.us        2920                 :CBC         682 : 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   [ +  +  +  + ]:            867 :     while (0 < r.count && ats[r.base] < lo)
                               2925                 :                :     {
                               2926                 :            185 :         r.defaulttype = types[r.base];
                               2927                 :            185 :         r.count--;
                               2928                 :            185 :         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                 :                :      */
   24 tgl@sss.pgh.pa.us        2938   [ -  +  -  - ]:GNC         682 :     while (1 < r.leapcount && leap[r.leapbase + 1].trans <= lo)
                               2939                 :                :     {
 2647 tgl@sss.pgh.pa.us        2940                 :UBC           0 :         r.leapcount--;
                               2941                 :              0 :         r.leapbase++;
                               2942                 :                :     }
   24 tgl@sss.pgh.pa.us        2943                 :GNC         682 :     while (0 < r.leapbase
                               2944         [ -  + ]:            682 :            && ((leap[r.leapbase - 1].corr < leap[r.leapbase].corr)
   24 tgl@sss.pgh.pa.us        2945         [ #  # ]:UNC           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.  */
   24 tgl@sss.pgh.pa.us        2953         [ +  + ]:GNC         682 :     if (hi < max_time)
                               2954                 :                :     {
 2647 tgl@sss.pgh.pa.us        2955   [ +  +  +  + ]:CBC         861 :         while (0 < r.count && hi + 1 < ats[r.base + r.count - 1])
                               2956                 :            520 :             r.count--;
   24 tgl@sss.pgh.pa.us        2957   [ -  +  -  - ]:GNC         341 :         while (0 < r.leapcount && hi + 1 < leap[r.leapbase + r.leapcount - 1].trans)
 2647 tgl@sss.pgh.pa.us        2958                 :UBC           0 :             r.leapcount--;
                               2959                 :                :     }
                               2960                 :                : 
                               2961                 :                :     /* Determine whether to append an expiration to the leap second table.  */
   24 tgl@sss.pgh.pa.us        2962   [ -  +  -  - ]:GNC         682 :     r.leapexpiry = 0 <= leapexpires && leapexpires - 1 <= hi;
                               2963                 :                : 
 2647 tgl@sss.pgh.pa.us        2964                 :CBC         682 :     return r;
                               2965                 :                : }
                               2966                 :                : 
                               2967                 :                : static void
 2836                          2968                 :            341 : 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;
   24 tgl@sss.pgh.pa.us        2975                 :GNC         341 :     char       *tempname = NULL;
                               2976                 :            341 :     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                 :            341 :     zic_t      *ats = xmalloc(align_to(size_product(timecnt + !timecnt,
                               2984                 :                :                                                     sizeof *ats + 1),
                               2985                 :                :                                        alignof(zic_t)));
                               2986                 :            341 :     void       *typesptr = ats + timecnt;
 3771 tgl@sss.pgh.pa.us        2987                 :CBC         341 :     unsigned char *types = typesptr;
   24 tgl@sss.pgh.pa.us        2988                 :GNC         341 :     struct timerange rangeall = {0}, range32, range64;
                               2989                 :                : 
                               2990                 :                :     /*
                               2991                 :                :      * Sort.
                               2992                 :                :      */
 8121 bruce@momjian.us         2993         [ +  + ]:CBC         341 :     if (timecnt > 1)
 3771 tgl@sss.pgh.pa.us        2994                 :            299 :         qsort(attypes, timecnt, sizeof *attypes, atcomp);
                               2995                 :                : 
                               2996                 :                :     /*
                               2997                 :                :      * Optimize and skip unwanted transitions.
                               2998                 :                :      */
                               2999                 :                :     {
                               3000                 :                :         ptrdiff_t   fromi,
                               3001                 :                :                     toi;
                               3002                 :                : 
 8121 bruce@momjian.us         3003                 :            341 :         toi = 0;
                               3004                 :            341 :         fromi = 0;
 8100                          3005         [ +  + ]:          17559 :         for (; fromi < timecnt; ++fromi)
                               3006                 :                :         {
   24 tgl@sss.pgh.pa.us        3007         [ +  + ]:GNC       17218 :             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         [ +  + ]:          16906 :                 unsigned char type_2 =
                               3015                 :          16604 :                     toi == 1 ? 0 : attypes[toi - 2].type;
                               3016                 :                : 
                               3017                 :          16997 :                 if ((attypes[fromi].at
                               3018                 :          16906 :                      + utoffs[attypes[toi - 1].type])
                               3019         [ +  + ]:          16906 :                     <= attypes[toi - 1].at + utoffs[type_2])
                               3020                 :                :                 {
                               3021         [ +  + ]:             91 :                     if (attypes[fromi].type == type_2)
                               3022                 :              1 :                         toi--;
                               3023                 :                :                     else
                               3024                 :             90 :                         attypes[toi - 1].type =
                               3025                 :             90 :                             attypes[fromi].type;
                               3026                 :             91 :                     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                 :                :              */
 3566 tgl@sss.pgh.pa.us        3035         [ +  + ]:CBC       17127 :             if (toi == 0
                               3036         [ +  + ]:          16815 :                 || attypes[fromi].dontmerge
 2565                          3037                 :          16712 :                 || (utoffs[attypes[toi - 1].type]
                               3038         [ +  + ]:          16712 :                     != utoffs[attypes[fromi].type])
                               3039                 :            448 :                 || (isdsts[attypes[toi - 1].type]
                               3040         [ +  + ]:            448 :                     != isdsts[attypes[fromi].type])
                               3041                 :            350 :                 || (desigidx[attypes[toi - 1].type]
                               3042         [ +  + ]:            350 :                     != desigidx[attypes[fromi].type]))
 8100 bruce@momjian.us         3043                 :          16843 :                 attypes[toi++] = attypes[fromi];
                               3044                 :                :         }
 8121                          3045                 :            341 :         timecnt = toi;
                               3046                 :                :     }
                               3047                 :                : 
   24 tgl@sss.pgh.pa.us        3048         [ -  + ]:GNC         341 :     if (noise)
                               3049                 :                :     {
   24 tgl@sss.pgh.pa.us        3050         [ #  # ]:UNC           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                 :                :      */
 8100 bruce@momjian.us         3068         [ +  + ]:CBC       17183 :     for (i = 0; i < timecnt; ++i)
                               3069                 :                :     {
 8121                          3070                 :          16842 :         ats[i] = attypes[i].at;
                               3071                 :          16842 :         types[i] = attypes[i].type;
                               3072                 :                :     }
                               3073                 :                : 
                               3074                 :                :     /*
                               3075                 :                :      * Correct for leap seconds.
                               3076                 :                :      */
 6253                          3077         [ +  + ]:          17183 :     for (i = 0; i < timecnt; ++i)
                               3078                 :                :     {
 6734 tgl@sss.pgh.pa.us        3079                 :          16842 :         j = leapcnt;
                               3080         [ -  + ]:          16842 :         while (--j >= 0)
   24 tgl@sss.pgh.pa.us        3081         [ #  # ]:UNC           0 :             if (leap[j].trans - leap[j].corr < ats[i])
                               3082                 :                :             {
                               3083                 :              0 :                 ats[i] = tadd(ats[i], leap[j].corr);
 6734 tgl@sss.pgh.pa.us        3084                 :UBC           0 :                 break;
                               3085                 :                :             }
                               3086                 :                :     }
                               3087                 :                : 
 2647 tgl@sss.pgh.pa.us        3088                 :CBC         341 :     rangeall.defaulttype = defaulttype;
                               3089                 :            341 :     rangeall.count = timecnt;
                               3090                 :            341 :     rangeall.leapcount = leapcnt;
   24 tgl@sss.pgh.pa.us        3091                 :GNC         341 :     range64 = limitrange(rangeall, lo_time,
                               3092                 :            341 :                          max(hi_time,
                               3093                 :                :                              redundant_time - (ZIC_MIN < redundant_time)),
                               3094                 :                :                          ats, types);
                               3095                 :            341 :     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         [ +  + ]:           1023 :     for (pass = 1; pass <= 2; pass++)
                               3103                 :                :     {
                               3104         [ +  + ]:            682 :         struct timerange const *r = pass == 1 ? &range32 : &range64;
                               3105                 :                : 
                               3106   [ +  +  +  - ]:            682 :         if (pass == 1 && !want_bloat())
                               3107                 :            341 :             continue;
                               3108         [ -  + ]:            341 :         if (r->leapexpiry)
                               3109                 :                :         {
   24 tgl@sss.pgh.pa.us        3110         [ #  # ]:UNC           0 :             if (noise)
                               3111                 :              0 :                 warning(_("%s: pre-2021b clients may mishandle"
                               3112                 :                :                           " leap second expiry"),
                               3113                 :                :                         name);
                               3114                 :              0 :             version = '4';
                               3115                 :                :         }
   24 tgl@sss.pgh.pa.us        3116         [ -  + ]:GNC         341 :         if (0 < r->leapcount
   24 tgl@sss.pgh.pa.us        3117   [ #  #  #  # ]:UNC           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                 :                :         }
   24 tgl@sss.pgh.pa.us        3125         [ -  + ]:GNC         341 :         if (version == '4')
   24 tgl@sss.pgh.pa.us        3126                 :UNC           0 :             break;
                               3127                 :                :     }
                               3128                 :                : 
   24 tgl@sss.pgh.pa.us        3129                 :GNC         341 :     fp = open_outfile(&outname, &tempname);
                               3130                 :                : 
 6253 bruce@momjian.us         3131         [ +  + ]:CBC        1023 :     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;
   24 tgl@sss.pgh.pa.us        3140                 :GNC         682 :         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                 :                : 
 6253 bruce@momjian.us         3159         [ +  + ]:CBC         682 :         if (pass == 1)
                               3160                 :                :         {
   24 tgl@sss.pgh.pa.us        3161                 :GNC         341 :             thisdefaulttype = range32.defaulttype;
 2647 tgl@sss.pgh.pa.us        3162                 :CBC         341 :             thistimei = range32.base;
                               3163                 :            341 :             thistimecnt = range32.count;
 3551                          3164                 :            341 :             toomanytimes = thistimecnt >> 31 >> 1 != 0;
 2647                          3165                 :            341 :             thisleapi = range32.leapbase;
                               3166                 :            341 :             thisleapcnt = range32.leapcount;
   24 tgl@sss.pgh.pa.us        3167                 :GNC         341 :             thisleapexpiry = range32.leapexpiry;
                               3168                 :            341 :             thismin = ZIC32_MIN;
                               3169                 :            341 :             thismax = ZIC32_MAX;
                               3170                 :                :         }
                               3171                 :                :         else
                               3172                 :                :         {
 2647 tgl@sss.pgh.pa.us        3173                 :CBC         341 :             thisdefaulttype = range64.defaulttype;
                               3174                 :            341 :             thistimei = range64.base;
                               3175                 :            341 :             thistimecnt = range64.count;
 3551                          3176                 :            341 :             toomanytimes = thistimecnt >> 31 >> 31 >> 2 != 0;
 2647                          3177                 :            341 :             thisleapi = range64.leapbase;
                               3178                 :            341 :             thisleapcnt = range64.leapcount;
   24 tgl@sss.pgh.pa.us        3179                 :GNC         341 :             thisleapexpiry = range64.leapexpiry;
                               3180                 :            341 :             thismin = min_time;
                               3181                 :            341 :             thismax = max_time;
                               3182                 :                :         }
 3551 tgl@sss.pgh.pa.us        3183         [ -  + ]:CBC         682 :         if (toomanytimes)
 3551 tgl@sss.pgh.pa.us        3184                 :UBC           0 :             error(_("too many transition times"));
                               3185                 :                : 
   24 tgl@sss.pgh.pa.us        3186   [ -  +  -  - ]:GNC         682 :         locut = thismin < lo_time && lo_time <= thismax;
                               3187   [ +  -  -  + ]:            682 :         hicut = thismin <= hi_time && hi_time < thismax;
                               3188                 :            682 :         thistimelim = thistimei + thistimecnt;
                               3189                 :            682 :         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   [ +  -  +  +  :            682 :         if ((locut || (pass == 1 && thistimei))
                                              +  + ]
                               3199   [ +  +  +  - ]:            162 :             && !(thistimecnt && ats[thistimei] == lo_time))
                               3200                 :                :         {
                               3201                 :            162 :             pretranstype = thisdefaulttype;
                               3202                 :            162 :             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   [ +  +  +  - ]:            682 :         if (pass == 1 && lo_time <= thismin)
                               3216                 :            341 :             thisdefaulttype = range64.defaulttype;
                               3217                 :                : 
                               3218         [ -  + ]:            682 :         if (locut)
   24 tgl@sss.pgh.pa.us        3219                 :UNC           0 :             thisdefaulttype = unspecifiedtype;
 2647 tgl@sss.pgh.pa.us        3220                 :CBC         682 :         omittype[thisdefaulttype] = false;
 2836                          3221         [ +  + ]:          33661 :         for (i = thistimei; i < thistimelim; i++)
                               3222                 :          32979 :             omittype[types[i]] = false;
   24 tgl@sss.pgh.pa.us        3223         [ -  + ]:GNC         682 :         if (hicut)
   24 tgl@sss.pgh.pa.us        3224                 :UNC           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                 :                :          */
 2836 tgl@sss.pgh.pa.us        3231                 :CBC         682 :         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                 :                :          */
 2565                          3241         [ -  + ]:            682 :         if (want_bloat())
                               3242                 :                :         {
                               3243                 :                :             int         mrudst,
                               3244                 :                :                         mrustd,
                               3245                 :                :                         hidst,
                               3246                 :                :                         histd,
                               3247                 :                :                         type;
                               3248                 :                : 
 3771 tgl@sss.pgh.pa.us        3249                 :UBC           0 :             hidst = histd = mrudst = mrustd = -1;
   24 tgl@sss.pgh.pa.us        3250         [ #  # ]:UNC           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++)
 3771 tgl@sss.pgh.pa.us        3258         [ #  # ]:UBC           0 :                 if (isdsts[types[i]])
                               3259                 :              0 :                     mrudst = types[i];
                               3260                 :                :                 else
                               3261                 :              0 :                     mrustd = types[i];
 2836                          3262         [ #  # ]:              0 :             for (i = old0; i < typecnt; i++)
                               3263                 :                :             {
 2565                          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])
 3771                          3270                 :              0 :                         hidst = i;
                               3271                 :                :                     else
                               3272                 :              0 :                         histd = i;
                               3273                 :                :                 }
                               3274                 :                :             }
                               3275   [ #  #  #  #  :              0 :             if (hidst >= 0 && mrudst >= 0 && hidst != mrudst &&
                                              #  # ]
 2565                          3276         [ #  # ]:              0 :                 utoffs[hidst] != utoffs[mrudst])
                               3277                 :                :             {
 3771                          3278                 :              0 :                 isdsts[mrudst] = -1;
 2565                          3279                 :              0 :                 type = addtype(utoffs[mrudst],
                               3280                 :              0 :                                &chars[desigidx[mrudst]],
                               3281                 :                :                                true,
 3771                          3282                 :              0 :                                ttisstds[mrudst],
 2565                          3283                 :              0 :                                ttisuts[mrudst]);
 3771                          3284                 :              0 :                 isdsts[mrudst] = 1;
 2836                          3285                 :              0 :                 omittype[type] = false;
                               3286                 :                :             }
 3771                          3287   [ #  #  #  #  :              0 :             if (histd >= 0 && mrustd >= 0 && histd != mrustd &&
                                              #  # ]
 2565                          3288         [ #  # ]:              0 :                 utoffs[histd] != utoffs[mrustd])
                               3289                 :                :             {
 3771                          3290                 :              0 :                 isdsts[mrustd] = -1;
 2565                          3291                 :              0 :                 type = addtype(utoffs[mrustd],
                               3292                 :              0 :                                &chars[desigidx[mrustd]],
                               3293                 :                :                                false,
 3771                          3294                 :              0 :                                ttisstds[mrustd],
 2565                          3295                 :              0 :                                ttisuts[mrustd]);
 3771                          3296                 :              0 :                 isdsts[mrustd] = 0;
 2836                          3297                 :              0 :                 omittype[type] = false;
                               3298                 :                :             }
                               3299                 :                :         }
                               3300                 :                : #endif                          /* !defined
                               3301                 :                :                                  * LEAVE_SOME_PRE_2011_SYSTEMS_IN_THE_LURCH */
 6734 tgl@sss.pgh.pa.us        3302                 :CBC         682 :         thistypecnt = 0;
 2836                          3303         [ +  + ]:           3866 :         for (i = old0; i < typecnt; i++)
                               3304         [ +  + ]:           3184 :             if (!omittype[i])
 2647                          3305                 :           3153 :                 typemap[i == old0 ? thisdefaulttype
                               3306         [ -  + ]:           3153 :                         : i == thisdefaulttype ? old0 : i]
 2836                          3307         [ +  + ]:           6306 :                     = thistypecnt++;
                               3308                 :                : 
 2565                          3309                 :            682 :         thischarcnt = stdcnt = utcnt = 0;
 2836                          3310         [ +  + ]:           3866 :         for (i = old0; i < typecnt; i++)
                               3311                 :                :         {
                               3312         [ +  + ]:           3184 :             if (omittype[i])
 6734                          3313                 :             31 :                 continue;
 2565                          3314         [ -  + ]:           3153 :             if (ttisstds[i])
 2565 tgl@sss.pgh.pa.us        3315                 :UBC           0 :                 stdcnt = thistypecnt;
 2565 tgl@sss.pgh.pa.us        3316         [ -  + ]:CBC        3153 :             if (ttisuts[i])
 2565 tgl@sss.pgh.pa.us        3317                 :UBC           0 :                 utcnt = thistypecnt;
   24 tgl@sss.pgh.pa.us        3318                 :GNC        3153 :             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         [ +  + ]:         175274 :         for (i = 0; i < TZ_MAX_CHARS; i++)
                               3327                 :         174592 :             indmap[i] = -1;
                               3328         [ +  + ]:           3866 :         for (i = old0; i < typecnt; i++)
                               3329   [ +  +  +  + ]:           3184 :             if (!omittype[i] && indmap[desigidx[i]] < 0)
                               3330                 :           2891 :                 indmap[desigidx[i]] = addabbr(thischars, &thischarcnt,
                               3331                 :           2891 :                                               &chars[desigidx[i]]);
                               3332                 :                : 
 2565 tgl@sss.pgh.pa.us        3333   [ +  +  +  - ]:CBC         682 :         if (pass == 1 && !want_bloat())
                               3334                 :                :         {
   24 tgl@sss.pgh.pa.us        3335                 :GNC         341 :             hicut = thisleapexpiry = false;
                               3336                 :            341 :             pretranstype = -1;
                               3337                 :            341 :             thistimecnt = thisleapcnt = 0;
 2565 tgl@sss.pgh.pa.us        3338                 :CBC         341 :             thistypecnt = thischarcnt = 1;
                               3339                 :                :         }
                               3340                 :                : #define DO(field)   fwrite(tzh.field, sizeof tzh.field, 1, fp)
   24 tgl@sss.pgh.pa.us        3341                 :GNC         682 :         memset(&tzh, 0, sizeof tzh);
 3004 tgl@sss.pgh.pa.us        3342                 :CBC         682 :         memcpy(tzh.tzh_magic, TZ_MAGIC, sizeof tzh.tzh_magic);
 3771                          3343                 :            682 :         tzh.tzh_version[0] = version;
 2565                          3344                 :            682 :         convert(utcnt, tzh.tzh_ttisutcnt);
                               3345                 :            682 :         convert(stdcnt, tzh.tzh_ttisstdcnt);
   24 tgl@sss.pgh.pa.us        3346                 :GNC         682 :         convert(thisleapcnt + thisleapexpiry, tzh.tzh_leapcnt);
                               3347                 :            682 :         convert((0 <= pretranstype) + thistimecnt + hicut,
                               3348                 :                :                 tzh.tzh_timecnt);
 3771 tgl@sss.pgh.pa.us        3349                 :CBC         682 :         convert(thistypecnt, tzh.tzh_typecnt);
                               3350                 :            682 :         convert(thischarcnt, tzh.tzh_charcnt);
 6734                          3351                 :            682 :         DO(tzh_magic);
                               3352                 :            682 :         DO(tzh_version);
                               3353                 :            682 :         DO(tzh_reserved);
 2565                          3354                 :            682 :         DO(tzh_ttisutcnt);
 6734                          3355                 :            682 :         DO(tzh_ttisstdcnt);
                               3356                 :            682 :         DO(tzh_leapcnt);
                               3357                 :            682 :         DO(tzh_timecnt);
                               3358                 :            682 :         DO(tzh_typecnt);
                               3359                 :            682 :         DO(tzh_charcnt);
                               3360                 :                : #undef DO
 2565                          3361   [ +  +  +  - ]:            682 :         if (pass == 1 && !want_bloat())
                               3362                 :                :         {
                               3363                 :                :             /* Output a minimal data block with just one time type.  */
                               3364                 :            341 :             puttzcode(0, fp);   /* utoff */
                               3365                 :            341 :             putc(0, fp);        /* dst */
                               3366                 :            341 :             putc(0, fp);        /* index of abbreviation */
                               3367                 :            341 :             putc(0, fp);        /* empty-string abbreviation */
                               3368                 :            341 :             continue;
                               3369                 :                :         }
                               3370                 :                : 
                               3371                 :                :         /* PG: print current timezone abbreviations if requested */
 2836                          3372   [ -  +  -  - ]:            341 :         if (print_abbrevs && pass == 2)
                               3373                 :                :         {
                               3374                 :                :             /* Print "type" data for periods ending after print_cutoff */
 2836 tgl@sss.pgh.pa.us        3375         [ #  # ]:UBC           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];
 2565                          3380                 :              0 :                     char       *thisabbrev = &thischars[indmap[desigidx[tm]]];
                               3381                 :                : 
  246 peter@eisentraut.org     3382                 :              0 :                     fprintf(stdout, "%s\t%" PRIdFAST64 "%s\n",
                               3383                 :                :                             thisabbrev,
                               3384                 :                :                             utoffs[tm],
 2556 tgl@sss.pgh.pa.us        3385         [ #  # ]:              0 :                             isdsts[tm] ? "\tD" : "");
                               3386                 :                :                 }
                               3387                 :                :             }
                               3388                 :                :             /* Print the default type if we have no transitions at all */
 2836                          3389         [ #  # ]:              0 :             if (thistimei >= thistimelim)
                               3390                 :                :             {
                               3391                 :              0 :                 unsigned char tm = defaulttype;
 2565                          3392                 :              0 :                 char       *thisabbrev = &thischars[indmap[desigidx[tm]]];
                               3393                 :                : 
  246 peter@eisentraut.org     3394                 :              0 :                 fprintf(stdout, "%s\t%" PRIdFAST64 "%s\n",
                               3395                 :                :                         thisabbrev,
                               3396                 :                :                         utoffs[tm],
 2556 tgl@sss.pgh.pa.us        3397         [ #  # ]:              0 :                         isdsts[tm] ? "\tD" : "");
                               3398                 :                :             }
                               3399                 :                :         }
                               3400                 :                : 
   24 tgl@sss.pgh.pa.us        3401   [ +  -  -  +  :GNC         341 :         if (pass == 2 && noise && 50 < thischarcnt)
                                              -  - ]
   24 tgl@sss.pgh.pa.us        3402                 :UNC           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                 :                :          */
   24 tgl@sss.pgh.pa.us        3410   [ -  +  -  - ]:GNC         341 :         lo = pass == 1 && lo_time < ZIC32_MIN ? ZIC32_MIN : lo_time;
                               3411                 :                : 
                               3412         [ -  + ]:            341 :         if (0 <= pretranstype)
 2647 tgl@sss.pgh.pa.us        3413                 :UBC           0 :             puttzcodepass(lo, fp, pass);
 6253 bruce@momjian.us         3414         [ +  + ]:CBC       17183 :         for (i = thistimei; i < thistimelim; ++i)
                               3415                 :                :         {
   24 tgl@sss.pgh.pa.us        3416                 :GNC       16842 :             puttzcodepass(ats[i], fp, pass);
                               3417                 :                :         }
 2647 tgl@sss.pgh.pa.us        3418         [ -  + ]:CBC         341 :         if (hicut)
 2647 tgl@sss.pgh.pa.us        3419                 :UBC           0 :             puttzcodepass(hi_time + 1, fp, pass);
   24 tgl@sss.pgh.pa.us        3420         [ -  + ]:GNC         341 :         if (0 <= pretranstype)
   24 tgl@sss.pgh.pa.us        3421                 :UNC           0 :             putc(typemap[pretranstype], fp);
   24 tgl@sss.pgh.pa.us        3422         [ +  + ]:GNC       17183 :         for (i = thistimei; i < thistimelim; i++)
                               3423                 :          16842 :             putc(typemap[types[i]], fp);
 2647 tgl@sss.pgh.pa.us        3424         [ -  + ]:CBC         341 :         if (hicut)
   24 tgl@sss.pgh.pa.us        3425                 :UNC           0 :             putc(typemap[unspecifiedtype], fp);
                               3426                 :                : 
 2836 tgl@sss.pgh.pa.us        3427         [ +  + ]:CBC        1933 :         for (i = old0; i < typecnt; i++)
                               3428                 :                :         {
 2565                          3429         [ +  + ]:           2843 :             int         h = (i == old0 ? thisdefaulttype
                               3430         [ +  - ]:           1251 :                              : i == thisdefaulttype ? old0 : i);
                               3431                 :                : 
                               3432         [ +  + ]:           1592 :             if (!omittype[h])
                               3433                 :                :             {
                               3434                 :           1588 :                 puttzcode(utoffs[h], fp);
                               3435                 :           1588 :                 putc(isdsts[h], fp);
                               3436                 :           1588 :                 putc(indmap[desigidx[h]], fp);
                               3437                 :                :             }
                               3438                 :                :         }
 6734                          3439         [ +  - ]:            341 :         if (thischarcnt != 0)
 3771                          3440                 :            341 :             fwrite(thischars, sizeof thischars[0],
                               3441                 :                :                    thischarcnt, fp);
   24 tgl@sss.pgh.pa.us        3442                 :GNC         341 :         thisleaplim = thisleapi + thisleapcnt;
 6253 bruce@momjian.us         3443         [ -  + ]:CBC         341 :         for (i = thisleapi; i < thisleaplim; ++i)
                               3444                 :                :         {
                               3445                 :                :             zic_t       todo;
                               3446                 :                : 
   24 tgl@sss.pgh.pa.us        3447         [ #  # ]:UNC           0 :             if (leap[i].roll)
                               3448                 :                :             {
                               3449   [ #  #  #  # ]:              0 :                 if (timecnt == 0 || leap[i].trans < ats[0])
                               3450                 :                :                 {
 6734 tgl@sss.pgh.pa.us        3451                 :UBC           0 :                     j = 0;
                               3452         [ #  # ]:              0 :                     while (isdsts[j])
 6253 bruce@momjian.us         3453         [ #  # ]:              0 :                         if (++j >= typecnt)
                               3454                 :                :                         {
 6734 tgl@sss.pgh.pa.us        3455                 :              0 :                             j = 0;
                               3456                 :              0 :                             break;
                               3457                 :                :                         }
                               3458                 :                :                 }
                               3459                 :                :                 else
                               3460                 :                :                 {
                               3461                 :              0 :                     j = 1;
                               3462         [ #  # ]:              0 :                     while (j < timecnt &&
   24 tgl@sss.pgh.pa.us        3463         [ #  # ]:UNC           0 :                            ats[j] <= leap[i].trans)
 6253 bruce@momjian.us         3464                 :UBC           0 :                         ++j;
 6734 tgl@sss.pgh.pa.us        3465                 :              0 :                     j = types[j - 1];
                               3466                 :                :                 }
   24 tgl@sss.pgh.pa.us        3467                 :UNC           0 :                 todo = tadd(leap[i].trans, -utoffs[j]);
                               3468                 :                :             }
                               3469                 :                :             else
                               3470                 :              0 :                 todo = leap[i].trans;
 2647 tgl@sss.pgh.pa.us        3471                 :UBC           0 :             puttzcodepass(todo, fp, pass);
   24 tgl@sss.pgh.pa.us        3472                 :UNC           0 :             puttzcode(leap[i].corr, fp);
                               3473                 :                :         }
   24 tgl@sss.pgh.pa.us        3474         [ -  + ]:GNC         341 :         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                 :                :              */
   24 tgl@sss.pgh.pa.us        3482                 :UNC           0 :             puttzcodepass(leapexpires, fp, pass);
                               3483         [ #  # ]:              0 :             puttzcode(thisleaplim ? leap[thisleaplim - 1].corr : 0, fp);
                               3484                 :                :         }
 2565 tgl@sss.pgh.pa.us        3485         [ -  + ]:CBC         341 :         if (stdcnt != 0)
 2565 tgl@sss.pgh.pa.us        3486         [ #  # ]:UBC           0 :             for (i = old0; i < typecnt; i++)
                               3487         [ #  # ]:              0 :                 if (!omittype[i])
                               3488                 :              0 :                     putc(ttisstds[i], fp);
 2565 tgl@sss.pgh.pa.us        3489         [ -  + ]:CBC         341 :         if (utcnt != 0)
 2565 tgl@sss.pgh.pa.us        3490         [ #  # ]:UBC           0 :             for (i = old0; i < typecnt; i++)
                               3491         [ #  # ]:              0 :                 if (!omittype[i])
                               3492                 :              0 :                     putc(ttisuts[i], fp);
                               3493                 :                :     }
 3771 tgl@sss.pgh.pa.us        3494                 :CBC         341 :     fprintf(fp, "\n%s\n", string);
   24 tgl@sss.pgh.pa.us        3495                 :GNC         341 :     close_file(fp, directory, name, tempname);
                               3496                 :            341 :     rename_dest(tempname, name);
 3771 tgl@sss.pgh.pa.us        3497                 :CBC         341 :     free(ats);
                               3498                 :            341 : }
                               3499                 :                : 
                               3500                 :                : static char const *
                               3501                 :          13774 : abbroffset(char *buf, zic_t offset)
                               3502                 :                : {
                               3503                 :          13774 :     char        sign = '+';
                               3504                 :                :     int         seconds,
                               3505                 :                :                 minutes;
                               3506                 :                : 
                               3507         [ +  + ]:          13774 :     if (offset < 0)
                               3508                 :                :     {
                               3509                 :           6561 :         offset = -offset;
                               3510                 :           6561 :         sign = '-';
                               3511                 :                :     }
                               3512                 :                : 
                               3513                 :          13774 :     seconds = offset % SECSPERMIN;
                               3514                 :          13774 :     offset /= SECSPERMIN;
                               3515                 :          13774 :     minutes = offset % MINSPERHOUR;
                               3516                 :          13774 :     offset /= MINSPERHOUR;
                               3517         [ -  + ]:          13774 :     if (100 <= offset)
                               3518                 :                :     {
 3004 tgl@sss.pgh.pa.us        3519                 :UBC           0 :         error(_("%%z UT offset magnitude exceeds 99:59:59"));
 3771                          3520                 :              0 :         return "%z";
                               3521                 :                :     }
                               3522                 :                :     else
                               3523                 :                :     {
 3771 tgl@sss.pgh.pa.us        3524                 :CBC       13774 :         char       *p = buf;
                               3525                 :                : 
                               3526                 :          13774 :         *p++ = sign;
                               3527                 :          13774 :         *p++ = '0' + offset / 10;
                               3528                 :          13774 :         *p++ = '0' + offset % 10;
                               3529         [ +  + ]:          13774 :         if (minutes | seconds)
                               3530                 :                :         {
                               3531                 :            405 :             *p++ = '0' + minutes / 10;
                               3532                 :            405 :             *p++ = '0' + minutes % 10;
                               3533         [ -  + ]:            405 :             if (seconds)
                               3534                 :                :             {
 3771 tgl@sss.pgh.pa.us        3535                 :UBC           0 :                 *p++ = '0' + seconds / 10;
                               3536                 :              0 :                 *p++ = '0' + seconds % 10;
                               3537                 :                :             }
                               3538                 :                :         }
 3771 tgl@sss.pgh.pa.us        3539                 :CBC       13774 :         *p = '\0';
                               3540                 :          13774 :         return buf;
                               3541                 :                :     }
                               3542                 :                : }
                               3543                 :                : 
                               3544                 :                : static char const disable_percent_s[] = "";
                               3545                 :                : 
                               3546                 :                : static ptrdiff_t
 3321                          3547                 :          34326 : doabbr(char *abbr, struct zone const *zp, char const *letters,
                               3548                 :                :        bool isdst, zic_t save, bool doquotes)
 6734                          3549                 :            848 : {
                               3550                 :                :     char       *cp;
                               3551                 :                :     ptrdiff_t   len;
 3771                          3552                 :          34326 :     char const *format = zp->z_format;
   24 tgl@sss.pgh.pa.us        3553                 :GNC       34326 :     char const *slashp = strchr(format, '/');
                               3554                 :                : 
 6734 tgl@sss.pgh.pa.us        3555         [ +  + ]:CBC       34326 :     if (slashp == NULL)
                               3556                 :                :     {
                               3557                 :                :         char        letterbuf[PERCENT_Z_LEN_BOUND + 1];
                               3558                 :                : 
 3771                          3559         [ +  + ]:          33443 :         if (zp->z_format_specifier == 'z')
 2565                          3560                 :          13774 :             letters = abbroffset(letterbuf, zp->z_stdoff + save);
 3771                          3561         [ +  + ]:          19669 :         else if (!letters)
                               3562                 :            764 :             letters = "%s";
   24 tgl@sss.pgh.pa.us        3563         [ -  + ]:GNC       18905 :         else if (letters == disable_percent_s)
   24 tgl@sss.pgh.pa.us        3564                 :UNC           0 :             return 0;
 3771 tgl@sss.pgh.pa.us        3565                 :CBC       33443 :         sprintf(abbr, format, letters);
                               3566                 :                :     }
 3004                          3567         [ +  + ]:            883 :     else if (isdst)
 3771                          3568                 :            485 :         strcpy(abbr, slashp + 1);
                               3569                 :                :     else
                               3570                 :                :     {
                               3571                 :            398 :         memcpy(abbr, format, slashp - format);
 6734                          3572                 :            398 :         abbr[slashp - format] = '\0';
                               3573                 :                :     }
                               3574                 :          34326 :     len = strlen(abbr);
 3771                          3575         [ +  + ]:          34326 :     if (!doquotes)
                               3576                 :          33881 :         return len;
                               3577         [ +  + ]:           1293 :     for (cp = abbr; is_alpha(*cp); cp++)
                               3578                 :            848 :         continue;
 6734                          3579   [ +  -  +  + ]:            445 :     if (len > 0 && *cp == '\0')
 3771                          3580                 :            260 :         return len;
 6734                          3581                 :            185 :     abbr[len + 2] = '\0';
                               3582                 :            185 :     abbr[len + 1] = '>';
 3771                          3583                 :            185 :     memmove(abbr + 1, abbr, len);
 6734                          3584                 :            185 :     abbr[0] = '<';
 3771                          3585                 :            185 :     return len + 2;
                               3586                 :                : }
                               3587                 :                : 
                               3588                 :                : static void
                               3589                 :          21352 : updateminmax(const zic_t x)
                               3590                 :                : {
 6734                          3591         [ +  + ]:          21352 :     if (min_year > x)
                               3592                 :            393 :         min_year = x;
                               3593         [ +  + ]:          21352 :     if (max_year < x)
                               3594                 :            904 :         max_year = x;
                               3595                 :          21352 : }
                               3596                 :                : 
                               3597                 :                : static int
 3771                          3598                 :            421 : stringoffset(char *result, zic_t offset)
                               3599                 :                : {
                               3600                 :                :     int         hours;
                               3601                 :                :     int         minutes;
                               3602                 :                :     int         seconds;
                               3603                 :            421 :     bool        negative = offset < 0;
                               3604                 :            421 :     int         len = negative;
                               3605                 :                : 
                               3606         [ +  + ]:            421 :     if (negative)
                               3607                 :                :     {
 6734                          3608                 :            182 :         offset = -offset;
 3771                          3609                 :            182 :         result[0] = '-';
                               3610                 :                :     }
 6734                          3611                 :            421 :     seconds = offset % SECSPERMIN;
                               3612                 :            421 :     offset /= SECSPERMIN;
                               3613                 :            421 :     minutes = offset % MINSPERHOUR;
                               3614                 :            421 :     offset /= MINSPERHOUR;
   24 tgl@sss.pgh.pa.us        3615         [ -  + ]:GNC         421 :     if (offset >= HOURSPERDAY * DAYSPERWEEK)
                               3616                 :                :     {
 6734 tgl@sss.pgh.pa.us        3617                 :UBC           0 :         result[0] = '\0';
 3771                          3618                 :              0 :         return 0;
                               3619                 :                :     }
   24 tgl@sss.pgh.pa.us        3620                 :GNC         421 :     hours = offset;
 3771 tgl@sss.pgh.pa.us        3621                 :CBC         421 :     len += sprintf(result + len, "%d", hours);
 6253 bruce@momjian.us         3622   [ +  +  -  + ]:            421 :     if (minutes != 0 || seconds != 0)
                               3623                 :                :     {
 3771 tgl@sss.pgh.pa.us        3624                 :             16 :         len += sprintf(result + len, ":%02d", minutes);
 6734                          3625         [ -  + ]:             16 :         if (seconds != 0)
 3771 tgl@sss.pgh.pa.us        3626                 :UBC           0 :             len += sprintf(result + len, ":%02d", seconds);
                               3627                 :                :     }
 3771 tgl@sss.pgh.pa.us        3628                 :CBC         421 :     return len;
                               3629                 :                : }
                               3630                 :                : 
                               3631                 :                : static int
 2565                          3632                 :            208 : stringrule(char *result, struct rule *const rp, zic_t save, zic_t stdoff)
                               3633                 :                : {
 3771                          3634                 :            208 :     zic_t       tod = rp->r_tod;
                               3635                 :            208 :     int         compat = 0;
                               3636                 :                : 
 6253 bruce@momjian.us         3637         [ -  + ]:            208 :     if (rp->r_dycode == DC_DOM)
                               3638                 :                :     {
                               3639                 :                :         int         month,
                               3640                 :                :                     total;
                               3641                 :                : 
 6734 tgl@sss.pgh.pa.us        3642   [ #  #  #  # ]:UBC           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.  */
 3771                          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;
 3771 tgl@sss.pgh.pa.us        3656                 :CBC         208 :         int         wday = rp->r_wday;
                               3657                 :                :         int         wdayoff;
                               3658                 :                : 
 6734                          3659         [ +  + ]:            208 :         if (rp->r_dycode == DC_DOWGEQ)
                               3660                 :                :         {
 3771                          3661                 :            123 :             wdayoff = (rp->r_dayofmonth - 1) % DAYSPERWEEK;
                               3662         [ +  + ]:            123 :             if (wdayoff)
                               3663                 :              5 :                 compat = 2013;
                               3664                 :            123 :             wday -= wdayoff;
                               3665                 :            123 :             tod += wdayoff * SECSPERDAY;
                               3666                 :            123 :             week = 1 + (rp->r_dayofmonth - 1) / DAYSPERWEEK;
                               3667                 :                :         }
 6734                          3668         [ +  - ]:             85 :         else if (rp->r_dycode == DC_DOWLEQ)
                               3669                 :                :         {
                               3670         [ +  + ]:             85 :             if (rp->r_dayofmonth == len_months[1][rp->r_month])
                               3671                 :             81 :                 week = 5;
                               3672                 :                :             else
                               3673                 :                :             {
 3771                          3674                 :              4 :                 wdayoff = rp->r_dayofmonth % DAYSPERWEEK;
                               3675         [ +  - ]:              4 :                 if (wdayoff)
                               3676                 :              4 :                     compat = 2013;
                               3677                 :              4 :                 wday -= wdayoff;
                               3678                 :              4 :                 tod += wdayoff * SECSPERDAY;
                               3679                 :              4 :                 week = rp->r_dayofmonth / DAYSPERWEEK;
                               3680                 :                :             }
                               3681                 :                :         }
                               3682                 :                :         else
 6253 bruce@momjian.us         3683                 :UBC           0 :             return -1;          /* "cannot happen" */
 3771 tgl@sss.pgh.pa.us        3684         [ +  + ]:CBC         208 :         if (wday < 0)
                               3685                 :              4 :             wday += DAYSPERWEEK;
                               3686                 :            208 :         result += sprintf(result, "M%d.%d.%d",
                               3687                 :            208 :                           rp->r_month + 1, week, wday);
                               3688                 :                :     }
 2565                          3689         [ +  + ]:            208 :     if (rp->r_todisut)
                               3690                 :             78 :         tod += stdoff;
 3004                          3691   [ +  +  +  + ]:            208 :     if (rp->r_todisstd && !rp->r_isdst)
 2565                          3692                 :             49 :         tod += save;
 6734                          3693         [ +  + ]:            208 :     if (tod != 2 * SECSPERMIN * MINSPERHOUR)
                               3694                 :                :     {
 3771                          3695                 :             77 :         *result++ = '/';
                               3696         [ -  + ]:             77 :         if (!stringoffset(result, tod))
 6734 tgl@sss.pgh.pa.us        3697                 :UBC           0 :             return -1;
 3771 tgl@sss.pgh.pa.us        3698         [ +  + ]:CBC          77 :         if (tod < 0)
                               3699                 :                :         {
                               3700         [ +  - ]:              2 :             if (compat < 2013)
                               3701                 :              2 :                 compat = 2013;
                               3702                 :                :         }
                               3703         [ +  + ]:             75 :         else if (SECSPERDAY <= tod)
                               3704                 :                :         {
                               3705         [ +  + ]:              8 :             if (compat < 1994)
                               3706                 :              1 :                 compat = 1994;
                               3707                 :                :         }
                               3708                 :                :     }
                               3709                 :            208 :     return compat;
                               3710                 :                : }
                               3711                 :                : 
                               3712                 :                : static int
 3321                          3713                 :           2679 : rule_cmp(struct rule const *a, struct rule const *b)
                               3714                 :                : {
 3771                          3715         [ +  + ]:           2679 :     if (!a)
                               3716                 :            318 :         return -!!b;
                               3717         [ -  + ]:           2361 :     if (!b)
 3771 tgl@sss.pgh.pa.us        3718                 :UBC           0 :         return 1;
 3771 tgl@sss.pgh.pa.us        3719         [ +  + ]:CBC        2361 :     if (a->r_hiyear != b->r_hiyear)
                               3720         [ +  + ]:           2222 :         return a->r_hiyear < b->r_hiyear ? -1 : 1;
   24 tgl@sss.pgh.pa.us        3721         [ +  + ]:GNC         139 :     if (a->r_hiyear == ZIC_MAX)
                               3722                 :            104 :         return 0;
 3771 tgl@sss.pgh.pa.us        3723         [ +  - ]:CBC          35 :     if (a->r_month - b->r_month != 0)
                               3724                 :             35 :         return a->r_month - b->r_month;
 3771 tgl@sss.pgh.pa.us        3725                 :UBC           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
 3321 tgl@sss.pgh.pa.us        3735                 :CBC         341 : 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;
 3771                          3742                 :            341 :     int         compat = 0;
                               3743                 :                :     int         c;
                               3744                 :                :     int         offsetlen;
                               3745                 :                :     struct rule stdr,
                               3746                 :                :                 dstr;
                               3747                 :                :     ptrdiff_t   len;
                               3748                 :                :     int         dstcmp;
   24 tgl@sss.pgh.pa.us        3749                 :GNC         341 :     struct rule *lastrp[2] = {NULL, NULL};
                               3750                 :                :     struct zone zstr[2];
                               3751                 :                :     struct zone const *stdzp;
                               3752                 :                :     struct zone const *dstzp;
                               3753                 :                : 
 6734 tgl@sss.pgh.pa.us        3754                 :CBC         341 :     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                 :                :      */
 2647                          3760         [ -  + ]:            341 :     if (hi_time < max_time)
 2647 tgl@sss.pgh.pa.us        3761                 :UBC           0 :         return -1;
                               3762                 :                : 
 6734 tgl@sss.pgh.pa.us        3763                 :CBC         341 :     zp = zpfirst + zonecount - 1;
                               3764         [ +  + ]:           2861 :     for (i = 0; i < zp->z_nrules; ++i)
                               3765                 :                :     {
                               3766                 :                :         struct rule **last;
                               3767                 :                :         int         cmp;
                               3768                 :                : 
                               3769                 :           2520 :         rp = &zp->z_rules[i];
   24 tgl@sss.pgh.pa.us        3770                 :GNC        2520 :         last = &lastrp[rp->r_isdst];
                               3771                 :           2520 :         cmp = rule_cmp(*last, rp);
                               3772         [ +  + ]:           2520 :         if (cmp < 0)
                               3773                 :            774 :             *last = rp;
                               3774         [ -  + ]:           1746 :         else if (cmp == 0)
   24 tgl@sss.pgh.pa.us        3775                 :UNC           0 :             return -1;
                               3776                 :                :     }
   24 tgl@sss.pgh.pa.us        3777                 :GNC         341 :     stdrp = lastrp[false];
                               3778                 :            341 :     dstrp = lastrp[true];
                               3779   [ +  +  -  + ]:            341 :     dstcmp = zp->z_nrules ? rule_cmp(dstrp, stdrp) : zp->z_isdst ? 1 : -1;
                               3780                 :            341 :     stdzp = dstzp = zp;
                               3781                 :                : 
                               3782         [ +  + ]:            341 :     if (dstcmp < 0)
                               3783                 :                :     {
                               3784                 :                :         /* Standard time all year.  */
                               3785                 :            237 :         dstrp = NULL;
                               3786                 :                :     }
                               3787         [ -  + ]:            104 :     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                 :                :          */
   24 tgl@sss.pgh.pa.us        3793         [ #  # ]:UNC           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                 :                :     }
   24 tgl@sss.pgh.pa.us        3829         [ +  + ]:GNC         341 :     len = doabbr(result, stdzp, stdrp ? stdrp->r_abbrvar : NULL,
                               3830                 :                :                  false, 0, true);
                               3831                 :            341 :     offsetlen = stringoffset(result + len, -stdzp->z_stdoff);
 3771 tgl@sss.pgh.pa.us        3832         [ -  + ]:CBC         341 :     if (!offsetlen)
                               3833                 :                :     {
 6734 tgl@sss.pgh.pa.us        3834                 :UBC           0 :         result[0] = '\0';
 3771                          3835                 :              0 :         return -1;
                               3836                 :                :     }
 3771 tgl@sss.pgh.pa.us        3837                 :CBC         341 :     len += offsetlen;
 6734                          3838         [ +  + ]:            341 :     if (dstrp == NULL)
 3771                          3839                 :            237 :         return compat;
   24 tgl@sss.pgh.pa.us        3840                 :GNC         208 :     len += doabbr(result + len, dstzp, dstrp->r_abbrvar,
 2565 tgl@sss.pgh.pa.us        3841                 :CBC         104 :                   dstrp->r_isdst, dstrp->r_save, true);
                               3842         [ +  + ]:            104 :     if (dstrp->r_save != SECSPERMIN * MINSPERHOUR)
                               3843                 :                :     {
 3771                          3844                 :              3 :         offsetlen = stringoffset(result + len,
   24 tgl@sss.pgh.pa.us        3845                 :GNC           3 :                                  -(dstzp->z_stdoff + dstrp->r_save));
 3771 tgl@sss.pgh.pa.us        3846         [ -  + ]:CBC           3 :         if (!offsetlen)
                               3847                 :                :         {
 6253 bruce@momjian.us         3848                 :UBC           0 :             result[0] = '\0';
 3771 tgl@sss.pgh.pa.us        3849                 :              0 :             return -1;
                               3850                 :                :         }
 3771 tgl@sss.pgh.pa.us        3851                 :CBC           3 :         len += offsetlen;
                               3852                 :                :     }
                               3853                 :            104 :     result[len++] = ',';
   24 tgl@sss.pgh.pa.us        3854                 :GNC         104 :     c = stringrule(result + len, dstrp, dstrp->r_save, stdzp->z_stdoff);
 3771 tgl@sss.pgh.pa.us        3855         [ -  + ]:CBC         104 :     if (c < 0)
                               3856                 :                :     {
 6734 tgl@sss.pgh.pa.us        3857                 :UBC           0 :         result[0] = '\0';
 3771                          3858                 :              0 :         return -1;
                               3859                 :                :     }
 3771 tgl@sss.pgh.pa.us        3860         [ +  + ]:CBC         104 :     if (compat < c)
                               3861                 :              7 :         compat = c;
                               3862                 :            104 :     len += strlen(result + len);
                               3863                 :            104 :     result[len++] = ',';
   24 tgl@sss.pgh.pa.us        3864                 :GNC         104 :     c = stringrule(result + len, stdrp, dstrp->r_save, stdzp->z_stdoff);
 3771 tgl@sss.pgh.pa.us        3865         [ -  + ]:CBC         104 :     if (c < 0)
                               3866                 :                :     {
 6734 tgl@sss.pgh.pa.us        3867                 :UBC           0 :         result[0] = '\0';
 3771                          3868                 :              0 :         return -1;
                               3869                 :                :     }
 3771 tgl@sss.pgh.pa.us        3870         [ +  + ]:CBC         104 :     if (compat < c)
                               3871                 :              1 :         compat = c;
                               3872                 :            104 :     return compat;
                               3873                 :                : }
                               3874                 :                : 
                               3875                 :                : static void
 3321                          3876                 :            341 : 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;
   24 tgl@sss.pgh.pa.us        3892                 :GNC         341 :     zic_t       nonTZlimtime = ZIC_MIN;
                               3893                 :            341 :     int         nonTZlimtype = -1;
                               3894                 :                :     zic_t       max_year0;
 2836 tgl@sss.pgh.pa.us        3895                 :CBC         341 :     int         defaulttype = -1;
   24 tgl@sss.pgh.pa.us        3896                 :GNC         341 :     int         max_stringoffset_len = sizeof "-167:59:59" - 1;
                               3897                 :            341 :     int         max_comma_stringrule_len = (sizeof ",M12.5.6/" - 1
                               3898                 :            341 :                                             + max_stringoffset_len);
                               3899                 :                : 
                               3900                 :            341 :     check_for_signal();
                               3901                 :                : 
                               3902                 :                :     /* This cannot overflow; see FORMAT_LEN_GROWTH_BOUND.  */
 6734 tgl@sss.pgh.pa.us        3903                 :CBC         341 :     max_abbr_len = 2 + max_format_len + max_abbrvar_len;
   24 tgl@sss.pgh.pa.us        3904                 :GNC         341 :     max_envvar_len = 2 * (max_abbr_len + max_stringoffset_len
                               3905                 :            341 :                           + max_comma_stringrule_len);
                               3906                 :                : 
                               3907                 :            341 :     startbuf = xmalloc(max_abbr_len + 1);
                               3908                 :            341 :     ab = xmalloc(max_abbr_len + 1);
                               3909                 :            341 :     envvar = xmalloc(max_envvar_len + 1);
 3771 tgl@sss.pgh.pa.us        3910                 :CBC         341 :     INITIALIZE(untiltime);
                               3911                 :            341 :     INITIALIZE(starttime);
                               3912                 :                : 
                               3913                 :                :     /*
                               3914                 :                :      * Now. . .finally. . .generate some useful data!
                               3915                 :                :      */
 8121 bruce@momjian.us         3916                 :            341 :     timecnt = 0;
                               3917                 :            341 :     typecnt = 0;
                               3918                 :            341 :     charcnt = 0;
                               3919                 :                : 
                               3920                 :                :     /*
                               3921                 :                :      * Thanks to Earl Chew for noting the need to unconditionally initialize
                               3922                 :                :      * startttisstd.
                               3923                 :                :      */
 3771 tgl@sss.pgh.pa.us        3924                 :            341 :     startttisstd = false;
 2565                          3925                 :            341 :     startttisut = false;
 6734                          3926                 :            341 :     min_year = max_year = EPOCH_YEAR;
                               3927         [ -  + ]:            341 :     if (leapseen)
                               3928                 :                :     {
 6734 tgl@sss.pgh.pa.us        3929                 :UBC           0 :         updateminmax(leapminyear);
 3771                          3930                 :              0 :         updateminmax(leapmaxyear + (leapmaxyear < ZIC_MAX));
                               3931                 :                :     }
 6734 tgl@sss.pgh.pa.us        3932         [ +  + ]:CBC        2299 :     for (i = 0; i < zonecount; ++i)
                               3933                 :                :     {
   24 tgl@sss.pgh.pa.us        3934                 :GNC        1958 :         struct zone const *zp = &zpfirst[i];
                               3935                 :                : 
 6734 tgl@sss.pgh.pa.us        3936         [ +  + ]:CBC        1958 :         if (i < zonecount - 1)
                               3937                 :           1617 :             updateminmax(zp->z_untilrule.r_loyear);
                               3938         [ +  + ]:          17277 :         for (j = 0; j < zp->z_nrules; ++j)
                               3939                 :                :         {
   24 tgl@sss.pgh.pa.us        3940                 :GNC       15319 :             struct rule *rp = &zp->z_rules[j];
                               3941                 :                : 
                               3942                 :          15319 :             updateminmax(rp->r_loyear);
 6734 tgl@sss.pgh.pa.us        3943         [ +  + ]:CBC       15319 :             if (rp->r_hiwasnum)
                               3944                 :           4416 :                 updateminmax(rp->r_hiyear);
                               3945                 :                :         }
                               3946                 :                :     }
                               3947                 :                : 
                               3948                 :                :     /*
                               3949                 :                :      * Generate lots of data if a rule can't cover all future times.
                               3950                 :                :      */
 3771                          3951                 :            341 :     compat = stringzone(envvar, zpfirst, zonecount);
   24 tgl@sss.pgh.pa.us        3952         [ +  + ]:GNC         341 :     version = compat < 2013 ? '2' : '3';
 2565 tgl@sss.pgh.pa.us        3953                 :CBC         341 :     do_extend = compat < 0;
 3771                          3954         [ -  + ]:            341 :     if (noise)
                               3955                 :                :     {
 3771 tgl@sss.pgh.pa.us        3956         [ #  # ]:UBC           0 :         if (!*envvar)
                               3957                 :              0 :             warning("%s %s",
                               3958                 :                :                     _("no proleptic TZ string for zone"),
                               3959                 :              0 :                     zpfirst->z_name);
 2565                          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                 :                :              */
 3771                          3966                 :              0 :             warning(_("%s: pre-%d clients may mishandle"
                               3967                 :                :                       " distant timestamps"),
                               3968                 :              0 :                     zpfirst->z_name, compat);
                               3969                 :                :         }
                               3970                 :                :     }
 3771 tgl@sss.pgh.pa.us        3971         [ -  + ]:CBC         341 :     if (do_extend)
                               3972                 :                :     {
 3771 tgl@sss.pgh.pa.us        3973         [ #  # ]:UBC           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                 :                :     }
   24 tgl@sss.pgh.pa.us        3982                 :GNC         341 :     max_year = max(max_year, (redundant_time / (SECSPERDAY * DAYSPERNYEAR)
                               3983                 :                :                               + EPOCH_YEAR + 1));
 3373 tgl@sss.pgh.pa.us        3984                 :CBC         341 :     max_year0 = max_year;
 2565                          3985         [ -  + ]:            341 :     if (want_bloat())
                               3986                 :                :     {
                               3987                 :                :         /*
                               3988                 :                :          * For the benefit of older systems, generate data from 1900 through
                               3989                 :                :          * 2038.
                               3990                 :                :          */
   24 tgl@sss.pgh.pa.us        3991         [ #  # ]:UNC           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                 :                : 
   24 tgl@sss.pgh.pa.us        3997   [ +  -  -  + ]:GNC         341 :     if (min_time < lo_time || hi_time < max_time)
   24 tgl@sss.pgh.pa.us        3998                 :UNC           0 :         unspecifiedtype = addtype(0, "-00", false, false, false);
                               3999                 :                : 
 8100 bruce@momjian.us         4000         [ +  + ]:CBC        2299 :     for (i = 0; i < zonecount; ++i)
                               4001                 :                :     {
                               4002                 :                :         /*
                               4003                 :                :          * A guess that may well be corrected later.
                               4004                 :                :          */
   24 tgl@sss.pgh.pa.us        4005                 :GNC        1958 :         zic_t       save = 0;
                               4006                 :           1958 :         struct zone const *zp = &zpfirst[i];
                               4007   [ +  +  +  - ]:           1958 :         bool        usestart = i > 0 && (zp - 1)->z_untiltime > min_time;
                               4008                 :           1958 :         bool        useuntil = i < (zonecount - 1);
                               4009                 :           1958 :         zic_t       stdoff = zp->z_stdoff;
                               4010                 :           1958 :         zic_t       startoff = stdoff;
                               4011                 :                : 
 2836 tgl@sss.pgh.pa.us        4012   [ +  +  -  + ]:CBC        1958 :         if (useuntil && zp->z_untiltime <= min_time)
 8121 bruce@momjian.us         4013                 :UBC           0 :             continue;
   24 tgl@sss.pgh.pa.us        4014                 :GNC        1958 :         eat(zp->z_filenum, zp->z_linenum);
 8121 bruce@momjian.us         4015                 :CBC        1958 :         *startbuf = '\0';
 8100                          4016         [ +  + ]:           1958 :         if (zp->z_nrules == 0)
                               4017                 :                :         {
                               4018                 :                :             int         type;
                               4019                 :                : 
 2565 tgl@sss.pgh.pa.us        4020                 :           1200 :             save = zp->z_save;
                               4021                 :           1200 :             doabbr(startbuf, zp, NULL, zp->z_isdst, save, false);
                               4022                 :           1200 :             type = addtype(oadd(zp->z_stdoff, save),
 3004                          4023                 :           1200 :                            startbuf, zp->z_isdst, startttisstd,
                               4024                 :                :                            startttisut);
 8100 bruce@momjian.us         4025         [ +  + ]:           1200 :             if (usestart)
                               4026                 :                :             {
 8121                          4027                 :            859 :                 addtt(starttime, type);
   24 tgl@sss.pgh.pa.us        4028         [ +  - ]:GNC         859 :                 if (nonTZlimtime < starttime)
                               4029                 :                :                 {
                               4030                 :            859 :                     nonTZlimtime = starttime;
                               4031                 :            859 :                     nonTZlimtype = type;
                               4032                 :                :                 }
 3771 tgl@sss.pgh.pa.us        4033                 :CBC         859 :                 usestart = false;
                               4034                 :                :             }
                               4035                 :                :             else
 2836                          4036                 :            341 :                 defaulttype = type;
                               4037                 :                :         }
                               4038                 :                :         else
                               4039                 :                :         {
                               4040                 :                :             zic_t       year;
                               4041                 :                : 
 8100 bruce@momjian.us         4042         [ +  + ]:          68489 :             for (year = min_year; year <= max_year; ++year)
                               4043                 :                :             {
                               4044   [ +  +  +  + ]:          68290 :                 if (useuntil && year > zp->z_untilrule.r_hiyear)
                               4045                 :            559 :                     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         [ +  + ]:        1459019 :                 for (j = 0; j < zp->z_nrules; ++j)
                               4053                 :                :                 {
   24 tgl@sss.pgh.pa.us        4054                 :GNC     1391288 :                     zic_t       one = 1;
                               4055                 :        1391288 :                     zic_t       y2038_boundary = one << 31;
                               4056                 :        1391288 :                     struct rule *rp = &zp->z_rules[j];
                               4057                 :                : 
                               4058                 :        1391288 :                     eats(zp->z_filenum, zp->z_linenum,
                               4059                 :                :                          rp->r_filenum, rp->r_linenum);
 8100 bruce@momjian.us         4060         [ +  + ]:CBC     1818537 :                     rp->r_todo = year >= rp->r_loyear &&
 2108 tgl@sss.pgh.pa.us        4061         [ +  + ]:         427249 :                         year <= rp->r_hiyear;
 8100 bruce@momjian.us         4062         [ +  + ]:        1391288 :                     if (rp->r_todo)
                               4063                 :                :                     {
                               4064                 :          33053 :                         rp->r_temp = rpytime(rp, year);
                               4065                 :                :                         rp->r_todo
 3373 tgl@sss.pgh.pa.us        4066                 :          66106 :                             = (rp->r_temp < y2038_boundary
                               4067   [ +  +  +  - ]:          33053 :                                || year <= max_year0);
                               4068                 :                :                     }
                               4069                 :                :                 }
                               4070                 :                :                 for (;;)
 8100 bruce@momjian.us         4071                 :          32484 :                 {
                               4072                 :                :                     ptrdiff_t   k;
                               4073                 :                :                     zic_t       jtime,
                               4074                 :                :                                 ktime;
                               4075                 :                :                     zic_t       offset;
                               4076                 :                :                     struct rule *rp;
                               4077                 :                :                     int         type;
                               4078                 :                : 
 3228 tgl@sss.pgh.pa.us        4079                 :         100215 :                     INITIALIZE(ktime);
 8100 bruce@momjian.us         4080         [ +  + ]:         100215 :                     if (useuntil)
                               4081                 :                :                     {
                               4082                 :                :                         /*
                               4083                 :                :                          * Turn untiltime into UT assuming the current stdoff
                               4084                 :                :                          * and save values.
                               4085                 :                :                          */
                               4086                 :          72267 :                         untiltime = zp->z_untiltime;
 2565 tgl@sss.pgh.pa.us        4087         [ +  + ]:          72267 :                         if (!zp->z_untilrule.r_todisut)
 8100 bruce@momjian.us         4088                 :          70626 :                             untiltime = tadd(untiltime,
                               4089                 :                :                                              -stdoff);
                               4090         [ +  + ]:          72267 :                         if (!zp->z_untilrule.r_todisstd)
                               4091                 :          53182 :                             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                 :         100215 :                     k = -1;
                               4100         [ +  + ]:        2296381 :                     for (j = 0; j < zp->z_nrules; ++j)
                               4101                 :                :                     {
   24 tgl@sss.pgh.pa.us        4102                 :GNC     2196166 :                         struct rule *r = &zp->z_rules[j];
                               4103                 :                : 
                               4104         [ +  + ]:        2196166 :                         if (!r->r_todo)
 8100 bruce@momjian.us         4105                 :CBC     2145951 :                             continue;
   24 tgl@sss.pgh.pa.us        4106                 :GNC       50215 :                         eats(zp->z_filenum, zp->z_linenum,
                               4107                 :                :                              r->r_filenum, r->r_linenum);
                               4108         [ +  + ]:          50215 :                         offset = r->r_todisut ? 0 : stdoff;
                               4109         [ +  + ]:          50215 :                         if (!r->r_todisstd)
 2565 tgl@sss.pgh.pa.us        4110                 :CBC       33595 :                             offset = oadd(offset, save);
   24 tgl@sss.pgh.pa.us        4111                 :GNC       50215 :                         jtime = r->r_temp;
 8100 bruce@momjian.us         4112                 :CBC       50215 :                         jtime = tadd(jtime, -offset);
                               4113   [ +  +  +  + ]:          50215 :                         if (k < 0 || jtime < ktime)
                               4114                 :                :                         {
                               4115                 :          41136 :                             k = j;
                               4116                 :          41136 :                             ktime = jtime;
                               4117                 :                :                         }
 3771 tgl@sss.pgh.pa.us        4118         [ -  + ]:           9079 :                         else if (jtime == ktime)
                               4119                 :                :                         {
 3771 tgl@sss.pgh.pa.us        4120                 :UBC           0 :                             char const *dup_rules_msg =
                               4121                 :                :                                 _("two rules for same instant");
                               4122                 :                : 
   24 tgl@sss.pgh.pa.us        4123                 :UNC           0 :                             eats(zp->z_filenum, zp->z_linenum,
                               4124                 :                :                                  r->r_filenum, r->r_linenum);
 3771 tgl@sss.pgh.pa.us        4125                 :UBC           0 :                             warning("%s", dup_rules_msg);
   24 tgl@sss.pgh.pa.us        4126                 :UNC           0 :                             r = &zp->z_rules[k];
                               4127                 :              0 :                             eats(zp->z_filenum, zp->z_linenum,
                               4128                 :                :                                  r->r_filenum, r->r_linenum);
 3771 tgl@sss.pgh.pa.us        4129                 :UBC           0 :                             error("%s", dup_rules_msg);
                               4130                 :                :                         }
                               4131                 :                :                     }
 8100 bruce@momjian.us         4132         [ +  + ]:CBC      100215 :                     if (k < 0)
                               4133                 :          67366 :                         break;  /* go on to next year */
                               4134                 :          32849 :                     rp = &zp->z_rules[k];
 3771 tgl@sss.pgh.pa.us        4135                 :          32849 :                     rp->r_todo = false;
 8100 bruce@momjian.us         4136   [ +  +  +  + ]:          32849 :                     if (useuntil && ktime >= untiltime)
                               4137                 :                :                     {
   24 tgl@sss.pgh.pa.us        4138         [ -  + ]:GNC         365 :                         if (!*startbuf
   24 tgl@sss.pgh.pa.us        4139         [ #  # ]:UNC           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);
 8100 bruce@momjian.us         4144                 :CBC         365 :                         break;
                               4145                 :                :                     }
 2565 tgl@sss.pgh.pa.us        4146                 :          32484 :                     save = rp->r_save;
 8100 bruce@momjian.us         4147   [ +  +  +  + ]:          32484 :                     if (usestart && ktime == starttime)
 3771 tgl@sss.pgh.pa.us        4148                 :             58 :                         usestart = false;
 8100 bruce@momjian.us         4149         [ +  + ]:          32484 :                     if (usestart)
                               4150                 :                :                     {
                               4151         [ +  + ]:          31337 :                         if (ktime < starttime)
                               4152                 :                :                         {
 2565 tgl@sss.pgh.pa.us        4153                 :          16589 :                             startoff = oadd(zp->z_stdoff,
                               4154                 :                :                                             save);
 3771                          4155                 :          16589 :                             doabbr(startbuf, zp,
                               4156                 :                :                                    rp->r_abbrvar,
 3004                          4157                 :          16589 :                                    rp->r_isdst,
                               4158                 :                :                                    rp->r_save,
                               4159                 :                :                                    false);
 8100 bruce@momjian.us         4160                 :          16589 :                             continue;
                               4161                 :                :                         }
 2565 tgl@sss.pgh.pa.us        4162         [ +  + ]:          14748 :                         if (*startbuf == '\0'
                               4163         [ +  + ]:            394 :                             && startoff == oadd(zp->z_stdoff,
                               4164                 :                :                                                 save))
                               4165                 :                :                         {
 6734                          4166                 :            197 :                             doabbr(startbuf,
                               4167                 :                :                                    zp,
                               4168                 :                :                                    rp->r_abbrvar,
 3004                          4169                 :            197 :                                    rp->r_isdst,
                               4170                 :                :                                    rp->r_save,
                               4171                 :                :                                    false);
                               4172                 :                :                         }
                               4173                 :                :                     }
   24 tgl@sss.pgh.pa.us        4174                 :GNC       15895 :                     eats(zp->z_filenum, zp->z_linenum,
                               4175                 :                :                          rp->r_filenum, rp->r_linenum);
 3771 tgl@sss.pgh.pa.us        4176                 :CBC       15895 :                     doabbr(ab, zp, rp->r_abbrvar,
 2565                          4177                 :          15895 :                            rp->r_isdst, rp->r_save, false);
                               4178                 :          15895 :                     offset = oadd(zp->z_stdoff, rp->r_save);
 3004                          4179                 :          15895 :                     type = addtype(offset, ab, rp->r_isdst,
 2565                          4180                 :          15895 :                                    rp->r_todisstd, rp->r_todisut);
 2836                          4181   [ -  +  -  - ]:          15895 :                     if (defaulttype < 0 && !rp->r_isdst)
 2836 tgl@sss.pgh.pa.us        4182                 :UBC           0 :                         defaulttype = type;
 8100 bruce@momjian.us         4183                 :CBC       15895 :                     addtt(ktime, type);
   24 tgl@sss.pgh.pa.us        4184         [ +  - ]:GNC       15895 :                     if (nonTZlimtime < ktime
                               4185   [ +  +  +  + ]:          15895 :                         && (useuntil || rp->r_hiyear != ZIC_MAX))
                               4186                 :                :                     {
                               4187                 :          15177 :                         nonTZlimtime = ktime;
                               4188                 :          15177 :                         nonTZlimtype = type;
                               4189                 :                :                     }
                               4190                 :                :                 }
                               4191                 :                :             }
                               4192                 :                :         }
 8100 bruce@momjian.us         4193         [ +  + ]:CBC        1958 :         if (usestart)
                               4194                 :                :         {
   24 tgl@sss.pgh.pa.us        4195                 :GNC         700 :             bool        isdst = startoff != zp->z_stdoff;
                               4196                 :                : 
                               4197   [ -  +  -  - ]:            700 :             if (*startbuf == '\0' && zp->z_format)
   24 tgl@sss.pgh.pa.us        4198                 :UNC           0 :                 doabbr(startbuf, zp, disable_percent_s,
                               4199                 :                :                        isdst, save, false);
   24 tgl@sss.pgh.pa.us        4200                 :GNC         700 :             eat(zp->z_filenum, zp->z_linenum);
 8121 bruce@momjian.us         4201         [ -  + ]:CBC         700 :             if (*startbuf == '\0')
   24 tgl@sss.pgh.pa.us        4202                 :UNC           0 :                 error(_("cannot determine time zone abbreviation"
                               4203                 :                :                         " to use just after until time"));
                               4204                 :                :             else
                               4205                 :                :             {
   24 tgl@sss.pgh.pa.us        4206                 :GNC         700 :                 int         type = addtype(startoff, startbuf, isdst,
                               4207                 :                :                                            startttisstd, startttisut);
                               4208                 :                : 
 2836 tgl@sss.pgh.pa.us        4209   [ -  +  -  - ]:CBC         700 :                 if (defaulttype < 0 && !isdst)
 2836 tgl@sss.pgh.pa.us        4210                 :UBC           0 :                     defaulttype = type;
 2836 tgl@sss.pgh.pa.us        4211                 :CBC         700 :                 addtt(starttime, type);
                               4212                 :                :             }
                               4213                 :                :         }
                               4214                 :                : 
                               4215                 :                :         /*
                               4216                 :                :          * Now we may get to set starttime for the next zone line.
                               4217                 :                :          */
 8100 bruce@momjian.us         4218         [ +  + ]:           1958 :         if (useuntil)
                               4219                 :                :         {
 8121                          4220                 :           1617 :             startttisstd = zp->z_untilrule.r_todisstd;
 2565 tgl@sss.pgh.pa.us        4221                 :           1617 :             startttisut = zp->z_untilrule.r_todisut;
 8121 bruce@momjian.us         4222                 :           1617 :             starttime = zp->z_untiltime;
                               4223         [ +  + ]:           1617 :             if (!startttisstd)
 2565 tgl@sss.pgh.pa.us        4224                 :           1348 :                 starttime = tadd(starttime, -save);
                               4225         [ +  + ]:           1617 :             if (!startttisut)
 8121 bruce@momjian.us         4226                 :           1543 :                 starttime = tadd(starttime, -stdoff);
                               4227                 :                :         }
                               4228                 :                :     }
 2836 tgl@sss.pgh.pa.us        4229         [ -  + ]:            341 :     if (defaulttype < 0)
 2836 tgl@sss.pgh.pa.us        4230                 :UBC           0 :         defaulttype = 0;
   24 tgl@sss.pgh.pa.us        4231   [ +  -  +  - ]:GNC         341 :     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                 :            341 :         zic_t       TZstarttime = ZIC_MAX;
                               4238                 :                : 
                               4239         [ +  + ]:          17795 :         for (i = 0; i < timecnt; i++)
                               4240                 :                :         {
                               4241                 :          17454 :             zic_t       at = attypes[i].at;
                               4242                 :                : 
                               4243   [ +  +  +  + ]:          17454 :             if (nonTZlimtime < at && at < TZstarttime)
                               4244                 :            123 :                 TZstarttime = at;
                               4245                 :                :         }
                               4246         [ +  + ]:            341 :         if (TZstarttime == ZIC_MAX)
                               4247                 :            237 :             TZstarttime = nonTZlimtime;
                               4248                 :                : 
                               4249                 :                :         /*
                               4250                 :                :          * Omit trailing transitions deducible from the TZ string, and not
                               4251                 :                :          * needed for -r or -R.
                               4252                 :                :          */
                               4253                 :            341 :         keep_at_max = max(TZstarttime, redundant_time);
                               4254         [ +  + ]:          17795 :         for (i = j = 0; i < timecnt; i++)
                               4255         [ +  + ]:          17454 :             if (attypes[i].at <= keep_at_max)
                               4256                 :                :             {
                               4257                 :          17218 :                 attypes[j].at = attypes[i].at;
                               4258                 :          34436 :                 attypes[j].dontmerge = (attypes[i].at == TZstarttime
                               4259   [ +  +  +  + ]:          17444 :                                         && (nonTZlimtype != attypes[i].type
                               4260         [ +  + ]:            226 :                                             || strchr(envvar, ',')));
                               4261                 :          17218 :                 attypes[j].type = attypes[i].type;
                               4262                 :          17218 :                 j++;
                               4263                 :                :             }
                               4264                 :            341 :         timecnt = j;
                               4265                 :                :     }
 3771 tgl@sss.pgh.pa.us        4266         [ -  + ]:CBC         341 :     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                 :                : 
 3771 tgl@sss.pgh.pa.us        4280                 :UBC           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;
 2647                          4284         [ #  # ]:              0 :         for (lastat = attypes, i = 1; i < timecnt; i++)
 3771                          4285         [ #  # ]:              0 :             if (attypes[i].at > lastat->at)
                               4286                 :              0 :                 lastat = &attypes[i];
 2647                          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);
 3566                          4291                 :              0 :             attypes[timecnt - 1].dontmerge = true;
                               4292                 :                :         }
                               4293                 :                :     }
 2836 tgl@sss.pgh.pa.us        4294                 :CBC         341 :     writezone(zpfirst->z_name, envvar, version, defaulttype);
 3771                          4295                 :            341 :     free(startbuf);
                               4296                 :            341 :     free(ab);
                               4297                 :            341 :     free(envvar);
 8121 bruce@momjian.us         4298                 :            341 : }
                               4299                 :                : 
                               4300                 :                : static void
 3771 tgl@sss.pgh.pa.us        4301                 :          17454 : addtt(zic_t starttime, int type)
                               4302                 :                : {
                               4303                 :          17454 :     attypes = growalloc(attypes, sizeof *attypes, timecnt, &timecnt_alloc);
 8121 bruce@momjian.us         4304                 :          17454 :     attypes[timecnt].at = starttime;
 3566 tgl@sss.pgh.pa.us        4305                 :          17454 :     attypes[timecnt].dontmerge = false;
 8121 bruce@momjian.us         4306                 :          17454 :     attypes[timecnt].type = type;
                               4307                 :          17454 :     ++timecnt;
                               4308                 :          17454 : }
                               4309                 :                : 
                               4310                 :                : static int
 2565 tgl@sss.pgh.pa.us        4311                 :          17795 : 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.  */
   24 tgl@sss.pgh.pa.us        4318   [ +  -  -  + ]:GNC       17795 :     if (!(-TWO_31_MINUS_1 <= utoff && utoff <= TWO_31_MINUS_1))
                               4319                 :                :     {
 2565 tgl@sss.pgh.pa.us        4320                 :UBC           0 :         error(_("UT offset out of range"));
                               4321                 :              0 :         exit(EXIT_FAILURE);
                               4322                 :                :     }
 2565 tgl@sss.pgh.pa.us        4323         [ +  - ]:CBC       17795 :     if (!want_bloat())
                               4324                 :          17795 :         ttisstd = ttisut = false;
                               4325                 :                : 
   24 tgl@sss.pgh.pa.us        4326                 :GNC       17795 :     checkabbr(abbr);
                               4327                 :                : 
                               4328                 :          17795 :     charcnt0 = charcnt;
                               4329                 :          17795 :     j = addabbr(chars, &charcnt, abbr);
                               4330         [ +  + ]:          17795 :     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         [ +  + ]:           4533 :         for (i = 0; i < typecnt; i++)
                               4338         [ +  + ]:           3080 :             if (j <= desigidx[i])
                               4339                 :              1 :                 desigidx[i] += charcnt - charcnt0;
                               4340                 :                :     }
                               4341                 :                :     else
                               4342                 :                :     {
                               4343                 :                :         /* If there's already an entry, return its index.  */
 2565 tgl@sss.pgh.pa.us        4344         [ +  + ]:CBC       60718 :         for (i = 0; i < typecnt; i++)
                               4345   [ +  +  +  +  :          60579 :             if (utoff == utoffs[i] && isdst == isdsts[i] && j == desigidx[i]
                                              +  + ]
                               4346   [ +  -  +  - ]:          16203 :                 && ttisstd == ttisstds[i] && ttisut == ttisuts[i])
                               4347                 :          16203 :                 return i;
                               4348                 :                :     }
                               4349                 :                : 
                               4350                 :                :     /*
                               4351                 :                :      * There isn't one; add a new one, unless there are already too many.
                               4352                 :                :      */
 8100 bruce@momjian.us         4353         [ -  + ]:           1592 :     if (typecnt >= TZ_MAX_TYPES)
                               4354                 :                :     {
 8121 bruce@momjian.us         4355                 :UBC           0 :         error(_("too many local time types"));
 6734 tgl@sss.pgh.pa.us        4356                 :              0 :         exit(EXIT_FAILURE);
                               4357                 :                :     }
 2565 tgl@sss.pgh.pa.us        4358                 :CBC        1592 :     i = typecnt++;
                               4359                 :           1592 :     utoffs[i] = utoff;
 8121 bruce@momjian.us         4360                 :           1592 :     isdsts[i] = isdst;
                               4361                 :           1592 :     ttisstds[i] = ttisstd;
 2565 tgl@sss.pgh.pa.us        4362                 :           1592 :     ttisuts[i] = ttisut;
                               4363                 :           1592 :     desigidx[i] = j;
 8121 bruce@momjian.us         4364                 :           1592 :     return i;
                               4365                 :                : }
                               4366                 :                : 
                               4367                 :                : static void
 2229 tgl@sss.pgh.pa.us        4368                 :UBC           0 : leapadd(zic_t t, int correction, int rolling)
                               4369                 :                : {
                               4370                 :                :     ptrdiff_t   i;
                               4371                 :                : 
   24 tgl@sss.pgh.pa.us        4372   [ #  #  #  #  :UNC           0 :     if (rolling && (lo_time != min_time || hi_time != max_time))
                                              #  # ]
                               4373                 :                :     {
                               4374                 :              0 :         error(_("Rolling leap seconds not supported with -r"));
 6734 tgl@sss.pgh.pa.us        4375                 :UBC           0 :         exit(EXIT_FAILURE);
                               4376                 :                :     }
   24 tgl@sss.pgh.pa.us        4377                 :UNC           0 :     leap = growalloc(leap, sizeof *leap, leapcnt, &leap_alloc);
 8121 bruce@momjian.us         4378         [ #  # ]:UBC           0 :     for (i = 0; i < leapcnt; ++i)
   24 tgl@sss.pgh.pa.us        4379         [ #  # ]:UNC           0 :         if (t <= leap[i].trans)
 8121 bruce@momjian.us         4380                 :UBC           0 :             break;
   24 tgl@sss.pgh.pa.us        4381                 :UNC           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;
 2229 tgl@sss.pgh.pa.us        4385                 :UBC           0 :     ++leapcnt;
 8121 bruce@momjian.us         4386                 :              0 : }
                               4387                 :                : 
                               4388                 :                : static void
 8100                          4389                 :              0 : adjleap(void)
                               4390                 :                : {
                               4391                 :                :     ptrdiff_t   i;
 3771 tgl@sss.pgh.pa.us        4392                 :              0 :     zic_t       last = 0;
 3228                          4393                 :              0 :     zic_t       prevtrans = 0;
                               4394                 :                : 
                               4395                 :                :     /*
                               4396                 :                :      * propagate leap seconds forward
                               4397                 :                :      */
 8100 bruce@momjian.us         4398         [ #  # ]:              0 :     for (i = 0; i < leapcnt; ++i)
                               4399                 :                :     {
   24 tgl@sss.pgh.pa.us        4400         [ #  # ]:UNC           0 :         if (leap[i].trans - prevtrans < 28 * SECSPERDAY)
                               4401                 :                :         {
 3228 tgl@sss.pgh.pa.us        4402                 :UBC           0 :             error(_("Leap seconds too close together"));
                               4403                 :              0 :             exit(EXIT_FAILURE);
                               4404                 :                :         }
   24 tgl@sss.pgh.pa.us        4405                 :UNC           0 :         prevtrans = leap[i].trans;
                               4406                 :              0 :         leap[i].trans = tadd(prevtrans, last);
                               4407                 :              0 :         last = leap[i].corr += last;
                               4408                 :                :     }
                               4409                 :                : 
 2229 tgl@sss.pgh.pa.us        4410         [ #  # ]:UBC           0 :     if (0 <= leapexpires)
                               4411                 :                :     {
                               4412                 :              0 :         leapexpires = oadd(leapexpires, last);
   24 tgl@sss.pgh.pa.us        4413   [ #  #  #  # ]:UNC           0 :         if (!(leapcnt == 0 || (leap[leapcnt - 1].trans < leapexpires)))
                               4414                 :                :         {
 2229 tgl@sss.pgh.pa.us        4415                 :UBC           0 :             error(_("last Leap time does not precede Expires time"));
                               4416                 :              0 :             exit(EXIT_FAILURE);
                               4417                 :                :         }
                               4418                 :                :     }
 8121 bruce@momjian.us         4419                 :              0 : }
                               4420                 :                : 
                               4421                 :                : /* Is A a space character in the C locale?  */
                               4422                 :                : static bool
 3771 tgl@sss.pgh.pa.us        4423                 :CBC      139968 : is_space(char a)
                               4424                 :                : {
                               4425         [ +  + ]:         139968 :     switch (a)
                               4426                 :                :     {
                               4427                 :          83510 :         default:
                               4428                 :          83510 :             return false;
                               4429                 :          56458 :         case ' ':
                               4430                 :                :         case '\f':
                               4431                 :                :         case '\n':
                               4432                 :                :         case '\r':
                               4433                 :                :         case '\t':
                               4434                 :                :         case '\v':
                               4435                 :          56458 :             return true;
                               4436                 :                :     }
                               4437                 :                : }
                               4438                 :                : 
                               4439                 :                : /* Is A an alphabetic character in the C locale?  */
                               4440                 :                : static bool
                               4441                 :          75820 : is_alpha(char a)
                               4442                 :                : {
                               4443         [ +  + ]:          75820 :     switch (a)
                               4444                 :                :     {
                               4445                 :          37795 :         default:
                               4446                 :          37795 :             return false;
                               4447                 :          38025 :         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                 :          38025 :             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                 :         430133 : lowerit(char a)
                               4509                 :                : {
                               4510   [ +  +  -  -  :         430133 :     switch (a)
                                     +  -  +  -  -  
                                     -  +  -  +  +  
                                     +  +  -  -  +  
                                     +  +  -  -  +  
                                           -  -  + ]
                               4511                 :                :     {
                               4512                 :         203489 :         default:
                               4513                 :         203489 :             return a;
                               4514                 :          29165 :         case 'A':
                               4515                 :          29165 :             return 'a';
 3771 tgl@sss.pgh.pa.us        4516                 :UBC           0 :         case 'B':
                               4517                 :              0 :             return 'b';
                               4518                 :              0 :         case 'C':
                               4519                 :              0 :             return 'c';
 3771 tgl@sss.pgh.pa.us        4520                 :CBC        9480 :         case 'D':
                               4521                 :           9480 :             return 'd';
 3771 tgl@sss.pgh.pa.us        4522                 :UBC           0 :         case 'E':
                               4523                 :              0 :             return 'e';
 3771 tgl@sss.pgh.pa.us        4524                 :CBC       12044 :         case 'F':
                               4525                 :          12044 :             return 'f';
 3771 tgl@sss.pgh.pa.us        4526                 :UBC           0 :         case 'G':
                               4527                 :              0 :             return 'g';
                               4528                 :              0 :         case 'H':
                               4529                 :              0 :             return 'h';
                               4530                 :              0 :         case 'I':
                               4531                 :              0 :             return 'i';
 3771 tgl@sss.pgh.pa.us        4532                 :CBC       39773 :         case 'J':
                               4533                 :          39773 :             return 'j';
 3771 tgl@sss.pgh.pa.us        4534                 :UBC           0 :         case 'K':
                               4535                 :              0 :             return 'k';
 3771 tgl@sss.pgh.pa.us        4536                 :CBC        6906 :         case 'L':
                               4537                 :           6906 :             return 'l';
                               4538                 :          29633 :         case 'M':
                               4539                 :          29633 :             return 'm';
                               4540                 :          10920 :         case 'N':
                               4541                 :          10920 :             return 'n';
                               4542                 :          23040 :         case 'O':
                               4543                 :          23040 :             return 'o';
 3771 tgl@sss.pgh.pa.us        4544                 :UBC           0 :         case 'P':
                               4545                 :              0 :             return 'p';
                               4546                 :              0 :         case 'Q':
                               4547                 :              0 :             return 'q';
 3771 tgl@sss.pgh.pa.us        4548                 :CBC       17868 :         case 'R':
                               4549                 :          17868 :             return 'r';
                               4550                 :          35748 :         case 'S':
                               4551                 :          35748 :             return 's';
                               4552                 :           3161 :         case 'T':
                               4553                 :           3161 :             return 't';
 3771 tgl@sss.pgh.pa.us        4554                 :UBC           0 :         case 'U':
                               4555                 :              0 :             return 'u';
                               4556                 :              0 :         case 'V':
                               4557                 :              0 :             return 'v';
 3771 tgl@sss.pgh.pa.us        4558                 :CBC        1496 :         case 'W':
                               4559                 :           1496 :             return 'w';
 3771 tgl@sss.pgh.pa.us        4560                 :UBC           0 :         case 'X':
                               4561                 :              0 :             return 'x';
                               4562                 :              0 :         case 'Y':
                               4563                 :              0 :             return 'y';
 3771 tgl@sss.pgh.pa.us        4564                 :CBC        7410 :         case 'Z':
                               4565                 :           7410 :             return 'z';
                               4566                 :                :     }
                               4567                 :                : }
                               4568                 :                : 
                               4569                 :                : /* case-insensitive equality */
                               4570                 :                : ATTRIBUTE_PURE_114833
                               4571                 :                : static bool
 7705 neilc@samurai.com        4572                 :          90427 : ciequal(const char *ap, const char *bp)
                               4573                 :                : {
 8121 bruce@momjian.us         4574         [ +  + ]:         114606 :     while (lowerit(*ap) == lowerit(*bp++))
                               4575         [ +  + ]:          26065 :         if (*ap++ == '\0')
 3771 tgl@sss.pgh.pa.us        4576                 :           1886 :             return true;
                               4577                 :          88541 :     return false;
                               4578                 :                : }
                               4579                 :                : 
                               4580                 :                : ATTRIBUTE_PURE_114833
                               4581                 :                : static bool
 7705 neilc@samurai.com        4582                 :UBC           0 : itsabbr(const char *abbr, const char *word)
                               4583                 :                : {
 8121 bruce@momjian.us         4584         [ #  # ]:              0 :     if (lowerit(*abbr) != lowerit(*word))
 3771 tgl@sss.pgh.pa.us        4585                 :              0 :         return false;
 8121 bruce@momjian.us         4586                 :              0 :     ++word;
                               4587         [ #  # ]:              0 :     while (*++abbr != '\0')
                               4588                 :                :         do
                               4589                 :                :         {
                               4590         [ #  # ]:              0 :             if (*word == '\0')
 3771 tgl@sss.pgh.pa.us        4591                 :              0 :                 return false;
 8121 bruce@momjian.us         4592         [ #  # ]:              0 :         } while (lowerit(*word++) != lowerit(*abbr));
 3771 tgl@sss.pgh.pa.us        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
 3228 tgl@sss.pgh.pa.us        4600                 :CBC       89549 : ciprefix(char const *abbr, char const *word)
                               4601                 :                : {
                               4602                 :                :     do
                               4603         [ +  + ]:         107283 :         if (!*abbr)
                               4604                 :           8673 :             return true;
                               4605         [ +  + ]:          98610 :     while (lowerit(*abbr++) == lowerit(*word++));
                               4606                 :                : 
                               4607                 :          80876 :     return false;
                               4608                 :                : }
                               4609                 :                : 
                               4610                 :                : static const struct lookup *
 3321                          4611                 :          17892 : byword(const char *word, const struct lookup *table)
                               4612                 :                : {
                               4613                 :                :     const struct lookup *foundlp;
                               4614                 :                :     const struct lookup *lp;
                               4615                 :                : 
 8121 bruce@momjian.us         4616   [ +  -  -  + ]:          17892 :     if (word == NULL || table == NULL)
 8121 bruce@momjian.us         4617                 :UBC           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                 :                :      */
 3228 tgl@sss.pgh.pa.us        4624   [ +  +  +  +  :CBC       17892 :     if (table == lasts && ciprefix("last", word) && word[4])
                                              +  - ]
                               4625                 :                :     {
                               4626         [ -  + ]:            342 :         if (word[4] == '-')
 3228 tgl@sss.pgh.pa.us        4627                 :UBC           0 :             warning(_("\"%s\" is undocumented; use \"last%s\" instead"),
                               4628                 :                :                     word, word + 5);
                               4629                 :                :         else
                               4630                 :                :         {
 3228 tgl@sss.pgh.pa.us        4631                 :CBC         342 :             word += 4;
                               4632                 :            342 :             table = wday_names;
                               4633                 :                :         }
                               4634                 :                :     }
                               4635                 :                : 
                               4636                 :                :     /*
                               4637                 :                :      * Look for exact match.
                               4638                 :                :      */
 8121 bruce@momjian.us         4639         [ +  + ]:         106433 :     for (lp = table; lp->l_word != NULL; ++lp)
                               4640         [ +  + ]:          90427 :         if (ciequal(word, lp->l_word))
                               4641                 :           1886 :             return lp;
                               4642                 :                : 
                               4643                 :                :     /*
                               4644                 :                :      * Look for inexact match.
                               4645                 :                :      */
                               4646                 :          16006 :     foundlp = NULL;
                               4647         [ +  + ]:         101854 :     for (lp = table; lp->l_word != NULL; ++lp)
 3228 tgl@sss.pgh.pa.us        4648         [ +  + ]:          85848 :         if (ciprefix(word, lp->l_word))
                               4649                 :                :         {
 8121 bruce@momjian.us         4650         [ +  - ]:           8331 :             if (foundlp == NULL)
                               4651                 :           8331 :                 foundlp = lp;
                               4652                 :                :             else
 8100 bruce@momjian.us         4653                 :UBC           0 :                 return NULL;    /* multiple inexact matches */
                               4654                 :                :         }
                               4655                 :                : 
 2565 tgl@sss.pgh.pa.us        4656   [ +  +  -  + ]:CBC       16006 :     if (foundlp && noise)
                               4657                 :                :     {
                               4658                 :                :         /* Warn about any backward-compatibility issue with pre-2017c zic.  */
 3228 tgl@sss.pgh.pa.us        4659                 :UBC           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                 :                : 
 8121 bruce@momjian.us         4673                 :CBC       16006 :     return foundlp;
                               4674                 :                : }
                               4675                 :                : 
                               4676                 :                : static int
   24 tgl@sss.pgh.pa.us        4677                 :GNC        4302 : getfields(char *cp, char **array, int arrayelts)
                               4678                 :                : {
                               4679                 :                :     char       *dp;
                               4680                 :                :     int         nsubs;
                               4681                 :                : 
 8121 bruce@momjian.us         4682                 :CBC        4302 :     nsubs = 0;
                               4683                 :                :     for (;;)
 8100 bruce@momjian.us         4684                 :GIC       32528 :     {
                               4685                 :                :         char       *dstart;
                               4686                 :                : 
 3771 tgl@sss.pgh.pa.us        4687         [ -  + ]:CBC       36830 :         while (is_space(*cp))
 8121 bruce@momjian.us         4688                 :UBC           0 :             ++cp;
 8121 bruce@momjian.us         4689   [ +  +  +  + ]:CBC       36830 :         if (*cp == '\0' || *cp == '#')
                               4690                 :                :             break;
   24 tgl@sss.pgh.pa.us        4691                 :GNC       32528 :         dstart = dp = cp;
                               4692                 :                :         do
                               4693                 :                :         {
 8121 bruce@momjian.us         4694         [ +  - ]:CBC       74909 :             if ((*dp = *cp++) != '"')
                               4695                 :          74909 :                 ++dp;
                               4696                 :                :             else
 8100 bruce@momjian.us         4697         [ #  # ]:UBC           0 :                 while ((*dp = *cp++) != '"')
                               4698         [ #  # ]:              0 :                     if (*dp != '\0')
                               4699                 :              0 :                         ++dp;
                               4700                 :                :                     else
                               4701                 :                :                     {
                               4702                 :              0 :                         error(_("Odd number of quotation marks"));
 3551 tgl@sss.pgh.pa.us        4703                 :              0 :                         exit(EXIT_FAILURE);
                               4704                 :                :                     }
 3771 tgl@sss.pgh.pa.us        4705   [ +  +  +  -  :CBC       74909 :         } while (*cp && *cp != '#' && !is_space(*cp));
                                              +  + ]
                               4706         [ +  + ]:          32528 :         if (is_space(*cp))
 8121 bruce@momjian.us         4707                 :          28229 :             ++cp;
                               4708                 :          32528 :         *dp = '\0';
   24 tgl@sss.pgh.pa.us        4709         [ -  + ]:GNC       32528 :         if (nsubs == arrayelts)
                               4710                 :                :         {
   24 tgl@sss.pgh.pa.us        4711                 :UNC           0 :             error(_("Too many input fields"));
                               4712                 :              0 :             exit(EXIT_FAILURE);
                               4713                 :                :         }
   24 tgl@sss.pgh.pa.us        4714   [ +  +  +  + ]:GNC       32528 :         array[nsubs++] = dstart + (*dstart == '-' && dp == dstart + 1);
                               4715                 :                :     }
                               4716                 :           4302 :     return nsubs;
                               4717                 :                : }
                               4718                 :                : 
                               4719                 :                : ATTRIBUTE_NORETURN static void
 3771 tgl@sss.pgh.pa.us        4720                 :UBC           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
 3771 tgl@sss.pgh.pa.us        4729                 :CBC     5758620 : oadd(zic_t t1, zic_t t2)
                               4730                 :                : {
                               4731                 :                : #ifdef ckd_add
                               4732                 :                :     zic_t       sum;
                               4733                 :                : 
   24 tgl@sss.pgh.pa.us        4734         [ +  - ]:GNC     5758620 :     if (!ckd_add(&sum, t1, t2))
                               4735                 :        5758620 :         return sum;
                               4736                 :                : #else
                               4737                 :                :     if (t1 < 0 ? ZIC_MIN - t1 <= t2 : t2 <= ZIC_MAX - t1)
                               4738                 :                :         return t1 + t2;
                               4739                 :                : #endif
   24 tgl@sss.pgh.pa.us        4740                 :UNC           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
 3771 tgl@sss.pgh.pa.us        4750                 :CBC      211584 : tadd(zic_t t1, zic_t t2)
                               4751                 :                : {
   24 tgl@sss.pgh.pa.us        4752                 :GNC      211584 :     zic_t       sum = oadd(t1, t2);
                               4753                 :                : 
                               4754   [ +  -  +  - ]:         211584 :     if (min_time <= sum && sum <= max_time)
                               4755                 :         211584 :         return sum;
   24 tgl@sss.pgh.pa.us        4756                 :UNC           0 :     time_overflow();
                               4757                 :                : }
                               4758                 :                : 
                               4759                 :                : /* Return T1 * T2, but diagnose any overflow and exit.  */
                               4760                 :                : ATTRIBUTE_PURE_114833_HACK
                               4761                 :                : static zic_t
   24 tgl@sss.pgh.pa.us        4762                 :GNC       42475 : omul(zic_t t1, zic_t t2)
                               4763                 :                : {
                               4764                 :                : #ifdef ckd_mul
                               4765                 :                :     zic_t       product;
                               4766                 :                : 
                               4767         [ +  - ]:          42475 :     if (!ckd_mul(&product, t1, t2))
                               4768                 :          42475 :         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
   24 tgl@sss.pgh.pa.us        4775                 :UNC           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
 3321 tgl@sss.pgh.pa.us        4785                 :CBC       34670 : 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                 :                : 
 8121 bruce@momjian.us         4794                 :          34670 :     m = TM_JANUARY;
                               4795                 :          34670 :     y = EPOCH_YEAR;
                               4796                 :                : 
                               4797                 :                :     /*
                               4798                 :                :      * dayoff = floor((wantedy - y) / YEARSPERREPEAT) * DAYSPERREPEAT, sans
                               4799                 :                :      * overflow.
                               4800                 :                :      */
   24 tgl@sss.pgh.pa.us        4801                 :GNC       34670 :     yrem = wantedy % YEARSPERREPEAT - y % YEARSPERREPEAT;
                               4802                 :          34670 :     dayoff = ((wantedy / YEARSPERREPEAT - y / YEARSPERREPEAT
                               4803                 :          34670 :                + yrem / YEARSPERREPEAT - (yrem % YEARSPERREPEAT < 0))
                               4804                 :                :               * DAYSPERREPEAT);
                               4805                 :                :     /* wantedy = y + ((wantedy - y) mod YEARSPERREPEAT), sans overflow.  */
                               4806                 :          34670 :     wantedy = y + (yrem + 2 * YEARSPERREPEAT) % YEARSPERREPEAT;
                               4807                 :                : 
 8100 bruce@momjian.us         4808         [ +  + ]:CBC     5211674 :     while (wantedy != y)
                               4809                 :                :     {
   24 tgl@sss.pgh.pa.us        4810   [ +  +  +  +  :GNC     5177004 :         i = len_years[isleap(y)];
                                              +  + ]
 3771 tgl@sss.pgh.pa.us        4811                 :CBC     5177004 :         dayoff = oadd(dayoff, i);
   24 tgl@sss.pgh.pa.us        4812                 :GNC     5177004 :         y++;
                               4813                 :                :     }
 8100 bruce@momjian.us         4814         [ +  + ]:CBC      229575 :     while (m != rp->r_month)
                               4815                 :                :     {
 8121                          4816   [ +  +  +  +  :         194905 :         i = len_months[isleap(y)][m];
                                              +  + ]
 3771 tgl@sss.pgh.pa.us        4817                 :         194905 :         dayoff = oadd(dayoff, i);
 8121 bruce@momjian.us         4818                 :         194905 :         ++m;
                               4819                 :                :     }
                               4820                 :          34670 :     i = rp->r_dayofmonth;
 8100                          4821   [ +  +  +  +  :          34670 :     if (m == TM_FEBRUARY && i == 29 && !isleap(y))
                                     +  +  +  +  -  
                                                 + ]
                               4822                 :                :     {
 8121                          4823         [ +  - ]:             76 :         if (rp->r_dycode == DC_DOWLEQ)
                               4824                 :             76 :             --i;
                               4825                 :                :         else
                               4826                 :                :         {
 8121 bruce@momjian.us         4827                 :UBC           0 :             error(_("use of 2/29 in non leap-year"));
 6734 tgl@sss.pgh.pa.us        4828                 :              0 :             exit(EXIT_FAILURE);
                               4829                 :                :         }
                               4830                 :                :     }
 8121 bruce@momjian.us         4831                 :CBC       34670 :     --i;
 3771 tgl@sss.pgh.pa.us        4832                 :          34670 :     dayoff = oadd(dayoff, i);
 8100 bruce@momjian.us         4833   [ +  +  +  + ]:          34670 :     if (rp->r_dycode == DC_DOWGEQ || rp->r_dycode == DC_DOWLEQ)
                               4834                 :                :     {
                               4835                 :                :         /*
                               4836                 :                :          * Don't trust mod of negative numbers.
                               4837                 :                :          */
   24 tgl@sss.pgh.pa.us        4838                 :GNC       22383 :         zic_t       wday = ((EPOCH_WDAY + dayoff % DAYSPERWEEK + DAYSPERWEEK)
                               4839                 :                :                             % DAYSPERWEEK);
                               4840                 :                : 
 3771 tgl@sss.pgh.pa.us        4841         [ +  + ]:CBC       87362 :         while (wday != rp->r_wday)
 8100 bruce@momjian.us         4842         [ +  + ]:          64979 :             if (rp->r_dycode == DC_DOWGEQ)
                               4843                 :                :             {
 3771 tgl@sss.pgh.pa.us        4844                 :          20155 :                 dayoff = oadd(dayoff, 1);
   24 tgl@sss.pgh.pa.us        4845         [ +  + ]:GNC       20155 :                 if (++wday >= DAYSPERWEEK)
 8121 bruce@momjian.us         4846                 :CBC        5382 :                     wday = 0;
                               4847                 :          20155 :                 ++i;
                               4848                 :                :             }
                               4849                 :                :             else
                               4850                 :                :             {
 3771 tgl@sss.pgh.pa.us        4851                 :          44824 :                 dayoff = oadd(dayoff, -1);
 8121 bruce@momjian.us         4852         [ +  + ]:          44824 :                 if (--wday < 0)
   24 tgl@sss.pgh.pa.us        4853                 :GNC         393 :                     wday = DAYSPERWEEK - 1;
 8121 bruce@momjian.us         4854                 :CBC       44824 :                 --i;
                               4855                 :                :             }
 8100                          4856   [ +  +  +  +  :          22383 :         if (i < 0 || i >= len_months[isleap(y)][m])
                                     +  +  +  -  +  
                                                 + ]
                               4857                 :                :         {
 6734 tgl@sss.pgh.pa.us        4858         [ -  + ]:             31 :             if (noise)
 3771 tgl@sss.pgh.pa.us        4859                 :UBC           0 :                 warning(_("rule goes past start/end of month; \
                               4860                 :                : will not work with pre-2004 versions of zic"));
                               4861                 :                :         }
                               4862                 :                :     }
   24 tgl@sss.pgh.pa.us        4863                 :GNC       34670 :     t = omul(dayoff, SECSPERDAY);
 8121 bruce@momjian.us         4864                 :CBC       34670 :     return tadd(t, rp->r_tod);
                               4865                 :                : }
                               4866                 :                : 
                               4867                 :                : static void
   24 tgl@sss.pgh.pa.us        4868                 :GNC       17795 : checkabbr(char const *string)
                               4869                 :                : {
 6734 tgl@sss.pgh.pa.us        4870         [ +  - ]:CBC       17795 :     if (strcmp(string, GRANDPARENTED) != 0)
                               4871                 :                :     {
                               4872                 :                :         const char *cp;
                               4873                 :                :         const char *mp;
                               4874                 :                : 
                               4875                 :          17795 :         cp = string;
 3771                          4876                 :          17795 :         mp = NULL;
   24 tgl@sss.pgh.pa.us        4877         [ +  + ]:GNC      111877 :         while (is_alpha(*cp) || is_digit(*cp)
 3773 tgl@sss.pgh.pa.us        4878   [ +  +  +  +  :CBC       98619 :                || *cp == '-' || *cp == '+')
                                              +  + ]
 6734                          4879                 :          56732 :             ++cp;
 3773                          4880   [ -  +  -  - ]:          17795 :         if (noise && cp - string < 3)
 3771 tgl@sss.pgh.pa.us        4881                 :UBC           0 :             mp = _("time zone abbreviation has fewer than 3 characters");
 6734 tgl@sss.pgh.pa.us        4882         [ -  + ]:CBC       17795 :         if (cp - string > ZIC_MAX_ABBR_LEN_WO_WARN)
 3771 tgl@sss.pgh.pa.us        4883                 :UBC           0 :             mp = _("time zone abbreviation has too many characters");
 6734 tgl@sss.pgh.pa.us        4884         [ -  + ]:CBC       17795 :         if (*cp != '\0')
 3771 tgl@sss.pgh.pa.us        4885                 :UBC           0 :             mp = _("time zone abbreviation differs from POSIX standard");
 3771 tgl@sss.pgh.pa.us        4886         [ -  + ]:CBC       17795 :         if (mp != NULL)
 3771 tgl@sss.pgh.pa.us        4887                 :UBC           0 :             warning("%s (%s)", mp, string);
                               4888                 :                :     }
   24 tgl@sss.pgh.pa.us        4889                 :GNC       17795 : }
                               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                 :          23839 : addabbr(char chs[TZ_MAX_CHARS], int *pnchs, char const *abbr)
                               4906                 :                : {
                               4907                 :          23839 :     int         nchs = *pnchs;
                               4908                 :          23839 :     int         alen = strlen(abbr),
                               4909                 :          23839 :                 nchs_incr = alen + 1;
                               4910                 :                :     int         i;
                               4911                 :                : 
                               4912         [ +  + ]:          79379 :     for (i = 0; i < nchs;)
                               4913                 :                :     {
                               4914                 :          75040 :         int         clen = strlen(&chs[i]);
                               4915                 :                : 
                               4916         [ +  + ]:          75040 :         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                 :          66648 :             int         isuff = i + (clen - alen);
                               4923                 :                : 
                               4924         [ +  + ]:          66648 :             if (memcmp(&chs[isuff], abbr, alen) == 0)
                               4925                 :          19497 :                 return isuff;
                               4926                 :                :         }
                               4927         [ +  + ]:           8392 :         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                 :              3 :             nchs_incr = alen - clen;
                               4935                 :              3 :             break;
                               4936                 :                :         }
                               4937                 :          55540 :         i += clen + 1;
                               4938                 :                :     }
                               4939         [ -  + ]:           4342 :     if (TZ_MAX_CHARS < nchs + nchs_incr)
                               4940                 :                :     {
 8121 bruce@momjian.us         4941                 :UBC           0 :         error(_("too many, or too long, time zone abbreviations"));
 6734 tgl@sss.pgh.pa.us        4942                 :              0 :         exit(EXIT_FAILURE);
                               4943                 :                :     }
   24 tgl@sss.pgh.pa.us        4944                 :GNC        4342 :     memmove(&chs[i + nchs_incr], &chs[i], nchs - i);
                               4945                 :           4342 :     memcpy(&chs[i], abbr, nchs_incr);
                               4946                 :           4342 :     *pnchs = nchs + nchs_incr;
                               4947                 :           4342 :     return i;
 8121 bruce@momjian.us         4948                 :ECB      (1453) : }
                               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
 3321 tgl@sss.pgh.pa.us        4957                 :CBC          21 : 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                 :                :      */
   24 tgl@sss.pgh.pa.us        4963         [ +  - ]:GNC          21 :     if (!skip_mkdir)
                               4964                 :                :     {
                               4965                 :                : 
                               4966                 :             21 :         char       *name = xstrdup(argname);
                               4967                 :             21 :         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         [ -  + ]:             21 :         while (*cp == '/')
   24 tgl@sss.pgh.pa.us        4980                 :UNC           0 :             cp++;
                               4981                 :                : 
   24 tgl@sss.pgh.pa.us        4982   [ +  +  +  +  :GNC          69 :         while (cp && ((cp = strchr(cp, '/')) || !ancestors))
                                              +  + ]
                               4983                 :                :         {
                               4984         [ +  + ]:             27 :             if (cp)
                               4985                 :             26 :                 *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         [ +  + ]:             27 :             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                 :              6 :                 int         err = errno;
                               5005                 :                : 
                               5006   [ +  -  +  - ]:              6 :                 if (err == ELOOP || err == ENAMETOOLONG
                               5007   [ +  -  -  + ]:              6 :                     || err == ENOENT || err == ENOTDIR)
                               5008                 :                :                 {
   24 tgl@sss.pgh.pa.us        5009                 :UNC           0 :                     error(_("%s: Cannot create directory %s: %s"),
                               5010                 :                :                           progname, name, strerror(err));
                               5011                 :              0 :                     exit(EXIT_FAILURE);
                               5012                 :                :                 }
                               5013                 :                :             }
   24 tgl@sss.pgh.pa.us        5014         [ +  + ]:GNC          27 :             if (cp)
                               5015                 :             26 :                 *cp++ = '/';
                               5016                 :                :         }
                               5017                 :             21 :         free(name);
                               5018                 :                :     }
 8121 bruce@momjian.us         5019                 :GIC          21 : }
        

Generated by: LCOV version 2.0-1