Skip to content

Commit

Permalink
Port to NetBSD, where, e.g., 'time_t' is wider than 'long'.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
eggert committed May 28, 2013
1 parent d3b025a commit f0133dd
Show file tree
Hide file tree
Showing 10 changed files with 323 additions and 224 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ LDLIBS=

# Add the following to the end of the "CFLAGS=" line as needed.
# -DHAVE_ADJTIME=0 if `adjtime' does not exist (SVR0?)
# -DHAVE_DOS_FILE_NAMES if file names have drive specifiers etc. (MS-DOS)
# -DHAVE_GETTEXT=1 if `gettext' works (GNU, Linux, Solaris); also see LDLIBS
# -DHAVE_INCOMPATIBLE_CTIME_R=1 if your system's time.h declares
# ctime_r and asctime_r incompatibly with the POSIX standard (Solaris 8).
# -DHAVE_INTTYPES_H=1 if you have a pre-C99 compiler with "inttypes.h"
# -DHAVE_SETTIMEOFDAY=0 if settimeofday does not exist (SVR0?)
# -DHAVE_SETTIMEOFDAY=1 if settimeofday has just 1 arg (SVR4)
# -DHAVE_SETTIMEOFDAY=2 if settimeofday uses 2nd arg (4.3BSD)
Expand Down
6 changes: 2 additions & 4 deletions date.c
Original file line number Diff line number Diff line change
Expand Up @@ -708,8 +708,7 @@ checkfinal(const char * const value,
time_t othert;
struct tm tm;
struct tm othertm;
register int pass;
register long offset;
register int pass, offset;

/*
** See if there's both a USG and a BSD interpretation.
Expand Down Expand Up @@ -807,8 +806,7 @@ iffy(const time_t thist, const time_t thatt,
static int
netsettime(struct timeval ntv)
{
int s, length, port, timed_ack, found, err;
long waittime;
int s, length, port, timed_ack, found, err, waittime;
fd_set ready;
char hostname[MAXHOSTNAMELEN];
struct timeval tout;
Expand Down
21 changes: 10 additions & 11 deletions difftime.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@

#include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */

double
double ATTRIBUTE_CONST
difftime(const time_t time1, const time_t time0)
{
/*
** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
** (assuming that the larger type has more precision).
** This is the common real-world case circa 2004.
*/
if (sizeof (double) > sizeof (time_t))
return (double) time1 - (double) time0;
Expand All @@ -30,8 +29,8 @@ difftime(const time_t time1, const time_t time0)
** if the minuend is greater than or equal to the subtrahend.
*/
if (time1 >= time0)
return time1 - time0;
else return -((double) (time0 - time1));
return time1 - time0;
else return -(double) (time0 - time1);
}
/*
** time_t is integral and signed.
Expand All @@ -42,16 +41,16 @@ difftime(const time_t time1, const time_t time0)
return time1 - time0;
/*
** time1 and time0 have opposite signs.
** Punt if unsigned long is too narrow.
** Punt if uintmax_t is too narrow.
** This suffers from double rounding; attempt to lessen that
** by using long double temporaries.
*/
if (sizeof (unsigned long) < sizeof (time_t))
return (double) time1 - (double) time0;
if (sizeof (uintmax_t) < sizeof (time_t))
return (long double) time1 - (long double) time0;
/*
** Stay calm...decent optimizers will eliminate the complexity below.
*/
if (time1 >= 0 /* && time0 < 0 */)
return (unsigned long) time1 +
(unsigned long) (-(time0 + 1)) + 1;
return -(double) ((unsigned long) time0 +
(unsigned long) (-(time1 + 1)) + 1);
return (uintmax_t) time1 + (uintmax_t) (-1 - time0) + 1;
return -(double) ((uintmax_t) time0 + (uintmax_t) (-1 - time1) + 1);
}
Loading

0 comments on commit f0133dd

Please sign in to comment.