1
0
mirror of https://frontier.innolan.net/rainlance/amiga-tz.git synced 2025-11-24 05:16:24 +00:00

Port to NetBSD, where, e.g., 'time_t' is wider than 'long'.

I audited the code and fixed as many width-asssumptions as I could
find, including several places where the code assumed that 'time_t'
was no wider than 'long'; this assumption is not true on 32-bit
NetBSD platforms.  This caught every problem that is already fixed
in the NetBSD zic.c, and caught quite a few more.
* Makefile: Add comments re HAVE_DOS_FILE_NAMES and HAVE_INTTYPES_H.
* date.c (checkfinal, netsettime): Don't use 'long' where 'int' will do.
* difftime.c (difftime): Mark with ATTRIBUTE_CONST.
Use uintmax_t, not unsigned long, for the widest unsigned integer type.
Use long double, not double, if time_t is wider than uintmax_t;
this can in theory help on nonstandard platforms, such as GCC
with 64-bit uintmax_t and 128-bit __int128_t.
* localtime.c (struct ttinfo.tt_gmtoff, struct rule.r_time)
(detzcode, getsecs, getoffset, gmtsub, localsub, increment_overflow32)
(normalize_overflow32, time1, time2, timesub, transtime, tzparse)
(time2sub, timeoff, gtime):
* tzfile.h (SECSPERDAY):
* zdump.c (SECSPERDAY):
* zic.c (convert, puttzcode):
Use int_fast32_t, not long, when all we care is that values up to
2**31 can be stored.  This doesn't fix any bugs, but it allows
more opportunity for compiler optimization.
(struct lsinfo.ls_corr, timesub, leapcorr):
Use int_fast64_t, not long, when values up to 2**63 can be stored.
(timesub): Make it clearer when we are truncating 0.5 to 0.
(increment_overflow32): Rename from long_increment_overflow.
All uses changed.
(normalize_overflow32): Rename from long_normalize_overflow.
All uses changed.
* private.h (HAVE_INTTYPES_H, ATTRIBUTE_CONST): New macros.
Include <inttypes.h> if HAVE_INTTYPES_H.
(INT_FAST64_MIN, INT_FAST64_MAX, SCNdFAST64, int_fast32_t, PRIdMAX)
(uintmax_t, PRIuMAX, _Noreturn):
Define to reasonable values if it's an older compiler.
* scheck.c (scheck): Add support for arbitrary formats, such as
those that SCNdFAST64 can expand to, at the price of no longer
supporting weird conversion specs like "%[%]".
* strftime.c (_fmt): Use intmax_t and uintmax_t to format time_t,
not long and unsigned long.
* zdump.c (int_fast32_t, intmax_t, PRIdMAX, SCNdMAX):
Define for pre-C99 compilers, like private.h does.
(delta, yeartot, main): Use intmax_t, not long.
(hunt): Use time_t, not long, since the diff must be nonnegative.
(tformat): Allow for time_t wider than long.
* zic.c (ZIC_MIN, ZIC_MAX, SCNdZIC): New macros.
(OFFSET_STRLEN_MAXIMUM, RULE_STRLEN_MAXIMUM): Remove.
(struct rule): Make r_loyear, r_hiyear, r_tod, r_stdoff, z_gmtoff,
z_stdoff zic_t, not long.
(addtype, gethms, oadd, rpytime, tadd, gmtoffs, corr, inleap)
(stringoffset, stringrule, outzone, addtype, adjleap, rpytime)
(LDAYSPERWEEK):
Use zic_t, not long.
(leapminyear, leapmaxyear, min_year, max_year, rulesub, updateminmax)
(outzone, rpytime):
Use zic_t, not int.
(usage): Now _Noreturn.
(main): Use S_IWGRP, not 'unix', to determine whether to call umask.
(writezone): Omit unnecessary cast.
(mkdirs): Use HAVE_DOS_FILE_NAMES, not 'unix', to determine
whether to parse DOS file anmes.
(eitol): Remove; no longer needed.
This commit is contained in:
Paul Eggert
2013-05-27 21:26:18 -07:00
parent d3b025adb2
commit f0133dd86a
10 changed files with 323 additions and 224 deletions

88
zdump.c
View File

@ -30,6 +30,47 @@
#define isascii(x) 1
#endif /* !defined isascii */
/*
** Substitutes for pre-C99 compilers.
** Much of this section of code is stolen from private.h.
*/
#ifndef HAVE_STDINT_H
# define HAVE_STDINT_H \
(199901 <= __STDC_VERSION__ || 2 < (__GLIBC__ + (0 < __GLIBC_MINOR__)))
#endif
#if HAVE_STDINT_H
# include "stdint.h"
#endif
#ifndef HAVE_INTTYPES_H
# define HAVE_INTTYPES_H HAVE_STDINT_H
#endif
#if HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#ifndef INT_FAST32_MAX
# if INT_MAX >> 31 == 0
typedef long int_fast32_t;
# else
typedef int int_fast32_t;
# endif
#endif
#ifndef INTMAX_MAX
# if defined LLONG_MAX || defined __LONG_LONG_MAX__
typedef long long intmax_t;
# define PRIdMAX "lld"
# else
typedef long intmax_t;
# define PRIdMAX "ld"
# endif
#endif
#ifndef SCNdMAX
# define SCNdMAX PRIdMAX
#endif
#ifndef ZDUMP_LO_YEAR
#define ZDUMP_LO_YEAR (-500)
#endif /* !defined ZDUMP_LO_YEAR */
@ -97,7 +138,7 @@
#define isleap_sum(a, b) isleap((a) % 400 + (b) % 400)
#endif /* !defined isleap_sum */
#define SECSPERDAY ((long) SECSPERHOUR * HOURSPERDAY)
#define SECSPERDAY ((int_fast32_t) SECSPERHOUR * HOURSPERDAY)
#define SECSPERNYEAR (SECSPERDAY * DAYSPERNYEAR)
#define SECSPERLYEAR (SECSPERNYEAR + SECSPERDAY)
@ -179,13 +220,13 @@ static int warned;
static char * abbr(struct tm * tmp);
static void abbrok(const char * abbrp, const char * zone);
static long delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
static intmax_t delta(struct tm * newp, struct tm * oldp) ATTRIBUTE_PURE;
static void dumptime(const struct tm * tmp);
static time_t hunt(char * name, time_t lot, time_t hit);
static void checkabsolutes(void);
static void show(char * zone, time_t t, int v);
static const char * tformat(void);
static time_t yeartot(long y) ATTRIBUTE_PURE;
static time_t yeartot(intmax_t y) ATTRIBUTE_PURE;
#ifndef TYPECHECK
#define my_localtime localtime
@ -324,15 +365,15 @@ main(int argc, char *argv[])
arg_processing_done:;
if (vflag | Vflag) {
long lo;
long hi;
char dummy;
register long cutloyear = ZDUMP_LO_YEAR;
register long cuthiyear = ZDUMP_HI_YEAR;
intmax_t lo;
intmax_t hi;
char dummy;
register intmax_t cutloyear = ZDUMP_LO_YEAR;
register intmax_t cuthiyear = ZDUMP_HI_YEAR;
if (cutarg != NULL) {
if (sscanf(cutarg, "%ld%c", &hi, &dummy) == 1) {
if (sscanf(cutarg, "%"SCNdMAX"%c", &hi, &dummy) == 1) {
cuthiyear = hi;
} else if (sscanf(cutarg, "%ld,%ld%c",
} else if (sscanf(cutarg, "%"SCNdMAX",%"SCNdMAX"%c",
&lo, &hi, &dummy) == 2) {
cutloyear = lo;
cuthiyear = hi;
@ -348,13 +389,13 @@ main(int argc, char *argv[])
cuthitime = yeartot(cuthiyear);
}
if (cuttimes != NULL) {
if (sscanf(cuttimes, "%ld%c", &hi, &dummy) == 1) {
if (sscanf(cuttimes, "%"SCNdMAX"%c", &hi, &dummy) == 1) {
if (hi < cuthitime) {
if (hi < absolute_min_time)
hi = absolute_min_time;
cuthitime = hi;
}
} else if (sscanf(cuttimes, "%ld,%ld%c",
} else if (sscanf(cuttimes, "%"SCNdMAX",%"SCNdMAX"%c",
&lo, &hi, &dummy) == 2) {
if (cutlotime < lo) {
if (absolute_max_time < lo)
@ -475,11 +516,11 @@ _("%s: use of -v on system with floating time_t other than float or double\n"),
}
static time_t
yeartot(const long y)
yeartot(const intmax_t y)
{
register long myy;
register long seconds;
register time_t t;
register intmax_t myy;
register int_fast32_t seconds;
register time_t t;
myy = EPOCH_YEAR;
t = 0;
@ -509,7 +550,6 @@ static time_t
hunt(char *name, time_t lot, time_t hit)
{
time_t t;
long diff;
struct tm lotm;
register struct tm * lotmp;
struct tm tm;
@ -522,7 +562,7 @@ hunt(char *name, time_t lot, time_t hit)
(void) strncpy(loab, abbr(&lotm), (sizeof loab) - 1);
}
for ( ; ; ) {
diff = (long) (hit - lot);
time_t diff = hit - lot;
if (diff < 2)
break;
t = lot;
@ -552,11 +592,11 @@ hunt(char *name, time_t lot, time_t hit)
** Thanks to Paul Eggert for logic used in delta.
*/
static long
static intmax_t
delta(struct tm * newp, struct tm *oldp)
{
register long result;
register int tmy;
register intmax_t result;
register int tmy;
if (newp->tm_year < oldp->tm_year)
return -delta(oldp, newp);
@ -632,12 +672,18 @@ tformat(void)
return "%g";
}
if (0 > (time_t) -1) { /* signed */
if (sizeof (time_t) == sizeof (intmax_t))
return "%"PRIdMAX;
if (sizeof (time_t) > sizeof (long))
return "%lld";
if (sizeof (time_t) > sizeof (int))
return "%ld";
return "%d";
}
#ifdef PRIuMAX
if (sizeof (time_t) == sizeof (uintmax_t))
return "%"PRIuMAX;
#endif
if (sizeof (time_t) > sizeof (unsigned long))
return "%llu";
if (sizeof (time_t) > sizeof (unsigned int))