Check for localtime_r before using it; switch back to localtime if it doesn't exist.
diff --git a/CMakeLists.txt b/CMakeLists.txt index 99f4701..a3e43ba 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt
@@ -149,6 +149,7 @@ CHECK_FUNCTION_EXISTS(fseeko HAVE_FSEEKO) CHECK_FUNCTION_EXISTS(ftello HAVE_FTELLO) CHECK_FUNCTION_EXISTS(getprogname HAVE_GETPROGNAME) +CHECK_FUNCTION_EXISTS(localtime_r HAVE_LOCALTIME_R) CHECK_FUNCTION_EXISTS(open HAVE_OPEN) CHECK_FUNCTION_EXISTS(setmode HAVE_SETMODE) CHECK_FUNCTION_EXISTS(snprintf HAVE_SNPRINTF)
diff --git a/cmake-config.h.in b/cmake-config.h.in index 8dde4bd..fd2d451 100644 --- a/cmake-config.h.in +++ b/cmake-config.h.in
@@ -30,6 +30,7 @@ #cmakedefine HAVE_GETPROGNAME #cmakedefine HAVE_GNUTLS #cmakedefine HAVE_LIBBZ2 +#cmakedefine HAVE_LOCALTIME_R #cmakedefine HAVE_MBEDTLS #cmakedefine HAVE_MKSTEMP #cmakedefine HAVE_NULLABLE
diff --git a/lib/zip_dirent.c b/lib/zip_dirent.c index 80b5b15..1a120c3 100644 --- a/lib/zip_dirent.c +++ b/lib/zip_dirent.c
@@ -1066,21 +1066,26 @@ void _zip_u2d_time(time_t intime, zip_uint16_t *dtime, zip_uint16_t *ddate) { - struct tm tm; + struct tm *tpm; - localtime_r(&intime, &tm); - if (localtime_r(&intime, &tm) == NULL) { +#ifdef HAVE_LOCALTIME_R + struct tm tm; + tpm = localtime_r(&intime, &tm); +#else + tpm = localtime(&intime); +#endif + if (tpm == NULL) { /* if localtime() fails, return an arbitrary date (1980-01-01 00:00:00) */ *ddate = (1 << 5) + 1; *dtime = 0; return; } - if (tm.tm_year < 80) { - tm.tm_year = 80; + if (tpm->tm_year < 80) { + tpm->tm_year = 80; } - *ddate = (zip_uint16_t)(((tm.tm_year + 1900 - 1980) << 9) + ((tm.tm_mon + 1) << 5) + tm.tm_mday); - *dtime = (zip_uint16_t)(((tm.tm_hour) << 11) + ((tm.tm_min) << 5) + ((tm.tm_sec) >> 1)); + *ddate = (zip_uint16_t)(((tpm->tm_year + 1900 - 1980) << 9) + ((tpm->tm_mon + 1) << 5) + tpm->tm_mday); + *dtime = (zip_uint16_t)(((tpm->tm_hour) << 11) + ((tpm->tm_min) << 5) + ((tpm->tm_sec) >> 1)); return; }
diff --git a/src/ziptool.c b/src/ziptool.c index 2fdcf70..e8cedd8 100644 --- a/src/ziptool.c +++ b/src/ziptool.c
@@ -37,6 +37,7 @@ #include <stdlib.h> #include <string.h> #include <sys/stat.h> +#include <time.h> #ifdef HAVE_UNISTD_H #include <unistd.h> #endif @@ -561,11 +562,17 @@ if (sb.valid & ZIP_STAT_COMP_SIZE) printf("compressed size: '%" PRIu64 "'\n", sb.comp_size); if (sb.valid & ZIP_STAT_MTIME) { - struct tm tpm; - if (localtime_r(&sb.mtime, &tpm) == NULL) { + struct tm *tpm; +#ifdef HAVE_LOCALTIME_R + struct tm tm; + tpm = localtime_r(&sb.mtime, &tm); +#else + tpm = localtime(&sb.mtime); +#endif + if (tpm == NULL) { printf("mtime: <not valid>\n"); } else { - strftime(buf, sizeof(buf), "%a %b %d %Y %H:%M:%S", &tpm); + strftime(buf, sizeof(buf), "%a %b %d %Y %H:%M:%S", tpm); printf("mtime: '%s'\n", buf); } }