1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-09 15:41:10 +03:00

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 <eshankelkar@galorithm.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
Eshan Kelkar
2024-08-13 11:52:21 +05:30
committed by Jakub Jelen
parent 861590192f
commit b58cb9f72b
2 changed files with 33 additions and 0 deletions

View File

@@ -34,6 +34,7 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <time.h>
#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

View File

@@ -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
* <tt>result</tt> 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;