From aca589d972c5c12706d57322e89f9dec2b2a3a00 Mon Sep 17 00:00:00 2001 From: owen Date: Sun, 9 Jan 2005 14:27:48 +0000 Subject: [PATCH] New function ltime() returns a struct tm of the current local time. Fixes crashes when time() returns (time_t)-1 on Windows by using the Win32 GetLocalTime() function. (The Unix implementation still just uses time() and localtime().) git-svn-id: svn://svn.tartarus.org/sgt/putty@5086 cda61777-01e9-0310-a592-d414129be87e --- Recipe | 11 ++++++----- cmdgen.c | 10 ++++------ logging.c | 4 +--- misc.h | 3 +++ time.c | 10 ++++++++++ unix/gtkdlg.c | 7 +++---- windows/windlg.c | 7 +++---- windows/winpgen.c | 10 ++++------ windows/wintime.c | 20 ++++++++++++++++++++ 9 files changed, 54 insertions(+), 28 deletions(-) create mode 100644 time.c create mode 100644 windows/wintime.c diff --git a/Recipe b/Recipe index c1cdef99..497ce704 100644 --- a/Recipe +++ b/Recipe @@ -206,8 +206,8 @@ SFTP = sftp int64 logging # Miscellaneous objects appearing in all the network utilities (not # Pageant or PuTTYgen). MISC = timing misc version settings tree234 proxy -WINMISC = MISC winstore winnet cmdline windefs winmisc pproxy -UXMISC = MISC uxstore uxsel uxnet cmdline uxmisc uxproxy +WINMISC = MISC winstore winnet cmdline windefs winmisc pproxy wintime +UXMISC = MISC uxstore uxsel uxnet cmdline uxmisc uxproxy time MACMISC = MISC macstore macnet mtcpnet otnet macmisc macabout pproxy # Character set library, for use in pterm. @@ -245,10 +245,10 @@ pageant : [G] winpgnt sshrsa sshpubk sshdes sshbn sshmd5 version tree234 puttygen : [G] winpgen sshrsag sshdssg sshprime sshdes sshbn sshmd5 version + sshrand winnoise sshsha winstore misc winctrls sshrsa sshdss winmisc + sshpubk sshaes sshsh512 import winutils puttygen.res tree234 - + notiming LIBS + + notiming LIBS wintime pterm : [X] UXTERM uxmisc misc ldisc settings uxpty uxsel BE_NONE uxstore - + uxsignal CHARSET cmdline uxpterm version + + uxsignal CHARSET cmdline uxpterm version time putty : [X] UXTERM uxmisc misc ldisc settings uxpty uxsel BE_ALL uxstore + uxsignal CHARSET uxputty NONSSH UXSSH UXMISC ux_x11 puttytel : [X] UXTERM uxmisc misc ldisc settings uxpty uxsel BE_NOSSH @@ -258,7 +258,8 @@ plink : [U] uxplink uxcons NONSSH UXSSH BE_ALL logging UXMISC uxsignal ux_x11 puttygen : [U] cmdgen sshrsag sshdssg sshprime sshdes sshbn sshmd5 version + sshrand uxnoise sshsha misc sshrsa sshdss uxcons uxstore uxmisc - + sshpubk sshaes sshsh512 import puttygen.res tree234 uxgen notiming + + sshpubk sshaes sshsh512 import puttygen.res time tree234 uxgen + + notiming pscp : [U] pscp uxsftp uxcons UXSSH BE_SSH SFTP wildcard UXMISC psftp : [U] psftp uxsftp uxcons UXSSH BE_SSH SFTP UXMISC diff --git a/cmdgen.c b/cmdgen.c index 42772aa3..5042aa4e 100644 --- a/cmdgen.c +++ b/cmdgen.c @@ -602,19 +602,17 @@ int main(int argc, char **argv) if (keytype != NOKEYGEN) { char *entropy; char default_comment[80]; - time_t t; - struct tm *tm; + struct tm tm; struct progress prog; prog.phase = -1; prog.current = -1; - time(&t); - tm = localtime(&t); + tm = ltime(); if (keytype == DSA) - strftime(default_comment, 30, "dsa-key-%Y%m%d", tm); + strftime(default_comment, 30, "dsa-key-%Y%m%d", &tm); else - strftime(default_comment, 30, "rsa-key-%Y%m%d", tm); + strftime(default_comment, 30, "rsa-key-%Y%m%d", &tm); random_ref(); entropy = get_random_data(bits / 8); diff --git a/logging.c b/logging.c index 808e429f..f1c42cde 100644 --- a/logging.c +++ b/logging.c @@ -169,7 +169,6 @@ void logfopen(void *handle) { struct LogContext *ctx = (struct LogContext *)handle; char buf[256]; - time_t t; struct tm tm; char writemod[4]; @@ -181,8 +180,7 @@ void logfopen(void *handle) return; sprintf(writemod, "wb"); /* default to rewrite */ - time(&t); - tm = *localtime(&t); + tm = ltime(); /* substitute special codes in file name */ xlatlognam(&ctx->currlogfilename, ctx->cfg.logfilename,ctx->cfg.host, &tm); diff --git a/misc.h b/misc.h index 77394469..fe5056c6 100644 --- a/misc.h +++ b/misc.h @@ -5,6 +5,7 @@ #include /* for FILE * */ #include /* for va_list */ +#include /* for struct_tm */ #ifndef FALSE #define FALSE 0 @@ -41,6 +42,8 @@ void bufchain_prefix(bufchain *ch, void **data, int *len); void bufchain_consume(bufchain *ch, int len); void bufchain_fetch(bufchain *ch, void *data, int len); +struct tm ltime(void); + /* * Debugging functions. * diff --git a/time.c b/time.c new file mode 100644 index 00000000..82488e6c --- /dev/null +++ b/time.c @@ -0,0 +1,10 @@ +#include +#include + +struct tm ltime(void) +{ + time_t t; + time(&t); + assert (t != ((time_t)-1)); + return *localtime(&t); +} diff --git a/unix/gtkdlg.c b/unix/gtkdlg.c index 0f3666fa..c774c8bc 100644 --- a/unix/gtkdlg.c +++ b/unix/gtkdlg.c @@ -2728,16 +2728,15 @@ void logevent_dlg(void *estuff, const char *string) struct eventlog_stuff *es = (struct eventlog_stuff *)estuff; char timebuf[40]; - time_t t; + struct tm tm; if (es->nevents >= es->negsize) { es->negsize += 64; es->events = sresize(es->events, es->negsize, char *); } - time(&t); - strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", - localtime(&t)); + tm=ltime(); + strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm); es->events[es->nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char); strcpy(es->events[es->nevents], timebuf); diff --git a/windows/windlg.c b/windows/windlg.c index 9edf110b..3bad7c32 100644 --- a/windows/windlg.c +++ b/windows/windlg.c @@ -661,7 +661,7 @@ int do_reconfig(HWND hwnd, int protcfginfo) void logevent(void *frontend, const char *string) { char timebuf[40]; - time_t t; + struct tm tm; log_eventlog(logctx, string); @@ -670,9 +670,8 @@ void logevent(void *frontend, const char *string) events = sresize(events, negsize, char *); } - time(&t); - strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", - localtime(&t)); + tm=ltime(); + strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S\t", &tm); events[nevents] = snewn(strlen(timebuf) + strlen(string) + 1, char); strcpy(events[nevents], timebuf); diff --git a/windows/winpgen.c b/windows/winpgen.c index e80dbfec..7772f55a 100644 --- a/windows/winpgen.c +++ b/windows/winpgen.c @@ -1279,14 +1279,12 @@ static int CALLBACK MainDlgProc(HWND hwnd, UINT msg, */ *state->commentptr = snewn(30, char); { - time_t t; - struct tm *tm; - time(&t); - tm = localtime(&t); + struct tm tm; + tm = ltime(); if (state->is_dsa) - strftime(*state->commentptr, 30, "dsa-key-%Y%m%d", tm); + strftime(*state->commentptr, 30, "dsa-key-%Y%m%d", &tm); else - strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", tm); + strftime(*state->commentptr, 30, "rsa-key-%Y%m%d", &tm); } /* diff --git a/windows/wintime.c b/windows/wintime.c new file mode 100644 index 00000000..eea0cf62 --- /dev/null +++ b/windows/wintime.c @@ -0,0 +1,20 @@ +#include "putty.h" +#include + +struct tm ltime(void) +{ + SYSTEMTIME st; + struct tm tm; + + GetLocalTime(&st); + tm.tm_sec=st.wSecond; + tm.tm_min=st.wMinute; + tm.tm_hour=st.wHour; + tm.tm_mday=st.wDay; + tm.tm_mon=st.wMonth-1; + tm.tm_year=(st.wYear>=1900?st.wYear-1900:0); + tm.tm_wday=st.wDayOfWeek; + tm.tm_yday=-1; /* GetLocalTime doesn't tell us */ + tm.tm_isdst=0; /* GetLocalTime doesn't tell us */ + return tm; +} -- 2.11.0