From b58cb9f72b1bc17da7ee98c78024a904c0730022 Mon Sep 17 00:00:00 2001 From: Eshan Kelkar Date: Tue, 13 Aug 2024 11:52:21 +0530 Subject: [PATCH] misc.c, priv.h: Add support for localtime_r() on Windows Windows supports localtime_s() instead of POSIX's localtime_r() and the function prototype of localtime_s() is different as compared to localtime_r(). This commit introduces ssh_localtime() (having same prototype as localtime_r()) for Windows which acts as a wrapper for localtime_s(), and defines localtime_r as a macro which expands to ssh_localtime for Windows. As a result, libssh can now use localtime_r() on Windows in the same manner as localtime_r() can be used on POSIX systems. Signed-off-by: Eshan Kelkar Reviewed-by: Jakub Jelen --- include/libssh/priv.h | 4 ++++ src/misc.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/include/libssh/priv.h b/include/libssh/priv.h index 35fd8506..e164154b 100644 --- a/include/libssh/priv.h +++ b/include/libssh/priv.h @@ -34,6 +34,7 @@ #include #include #include +#include #if !defined(HAVE_STRTOULL) # if defined(HAVE___STRTOULL) @@ -164,6 +165,9 @@ int ssh_gettimeofday(struct timeval *__p, void *__t); #define gettimeofday ssh_gettimeofday +struct tm *ssh_localtime(const time_t *timer, struct tm *result); +# define localtime_r ssh_localtime + #define _XCLOSESOCKET closesocket # ifdef HAVE_IO_H diff --git a/src/misc.c b/src/misc.c index 7fe243f5..349f407a 100644 --- a/src/misc.c +++ b/src/misc.c @@ -178,6 +178,35 @@ int ssh_gettimeofday(struct timeval *__p, void *__t) return (0); } +/** + * @internal + * + * @brief Convert time in seconds since the Epoch to broken-down local time + * + * This is a helper used to provide localtime_r() like function interface + * on Windows. + * + * @param timer Pointer to a location storing the time_t which + * represents the time in seconds since the Epoch. + * + * @param result Pointer to a location where the broken-down time + * (expressed as local time) should be stored. + * + * @returns A pointer to the structure pointed to by the parameter + * result on success, NULL on error with the errno + * set to indicate the error. + */ +struct tm *ssh_localtime(const time_t *timer, struct tm *result) +{ + errno_t rc; + rc = localtime_s(result, timer); + if (rc != 0) { + return NULL; + } + + return result; +} + char *ssh_get_local_username(void) { DWORD size = 0;