From 6481bb22b5e6d60909d09cf6179412c4f34b9b3c Mon Sep 17 00:00:00 2001 From: Ron Yorston Date: Wed, 14 Aug 2024 12:52:56 +0100 Subject: win32: use 64-bit time on 32-bit platforms To avoid problems with dates in 2038 and beyond use 64-bit time values on 32-bit platforms. - Mostly this just requires a few preprocessor macros to choose the appropriate functions, structs and typedefs. - We have our own implementations of nanosleep(), clock_gettime() and clock_settime(). Omit the Windows include file that declares them. - Apply the hack for struct timeval in the 'ts' applet on 32-bit. Adds 1624 bytes on 32-bit, none on 64-bit. (GitHub issue #446) --- include/libbb.h | 6 ++++++ include/mingw.h | 14 ++++++++++++++ miscutils/ts.c | 4 ++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/libbb.h b/include/libbb.h index e5000831f..17fb6fae6 100644 --- a/include/libbb.h +++ b/include/libbb.h @@ -10,6 +10,12 @@ #ifndef LIBBB_H #define LIBBB_H 1 +#if ENABLE_PLATFORM_MINGW32 +/* We have our own nanosleep(), clock_gettime() and clock_settime(). */ +/* Skip the Windows include file that declares them. */ +# define WIN_PTHREADS_TIME_H +#endif + #include "platform.h" #include diff --git a/include/mingw.h b/include/mingw.h index 7a07de619..104ca3b2e 100644 --- a/include/mingw.h +++ b/include/mingw.h @@ -2,6 +2,17 @@ #define NOIMPL(name,...) static inline int name(__VA_ARGS__) { errno = ENOSYS; return -1; } #define IMPL(name,ret,retval,...) static inline ret name(__VA_ARGS__) { return retval; } +/* Use 64-bit time on 32-bit platforms. */ +#if !defined(_WIN64) +# define time_t __time64_t +# define ctime(t) _ctime64(t) +# define localtime(t) _localtime64(t) +# define time(t) _time64(t) +# define gmtime(t) _gmtime64(t) +# define mktime(t) _mktime64(t) +# define timespec _timespec64 +#endif + /* * sys/types.h */ @@ -297,6 +308,9 @@ struct timespec { }; #endif +typedef int clockid_t; +#define CLOCK_REALTIME 0 + time_t timegm(struct tm *tm); int nanosleep(const struct timespec *req, struct timespec *rem); diff --git a/miscutils/ts.c b/miscutils/ts.c index f5e727eb5..fb669b858 100644 --- a/miscutils/ts.c +++ b/miscutils/ts.c @@ -25,7 +25,7 @@ int ts_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; int ts_main(int argc UNUSED_PARAM, char **argv) { struct timeval base; -#if ENABLE_PLATFORM_MINGW32 && !defined(_USE_32BIT_TIME_T) +#if ENABLE_PLATFORM_MINGW32 time_t t; #endif unsigned opt; @@ -76,7 +76,7 @@ int ts_main(int argc UNUSED_PARAM, char **argv) if (opt & 1) /* -i */ base = ts1; } -#if ENABLE_PLATFORM_MINGW32 && !defined(_USE_32BIT_TIME_T) +#if ENABLE_PLATFORM_MINGW32 t = ts.tv_sec; localtime_r(&t, &tm_time); #else -- cgit v1.2.3-55-g6feb