New function ltime() returns a struct tm of the current local time.
authorowen <owen@cda61777-01e9-0310-a592-d414129be87e>
Sun, 9 Jan 2005 14:27:48 +0000 (14:27 +0000)
committerowen <owen@cda61777-01e9-0310-a592-d414129be87e>
Sun, 9 Jan 2005 14:27:48 +0000 (14:27 +0000)
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
cmdgen.c
logging.c
misc.h
time.c [new file with mode: 0644]
unix/gtkdlg.c
windows/windlg.c
windows/winpgen.c
windows/wintime.c [new file with mode: 0644]

diff --git a/Recipe b/Recipe
index c1cdef9..497ce70 100644 (file)
--- 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
index 42772aa..5042aa4 100644 (file)
--- 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);
index 808e429..f1c42cd 100644 (file)
--- 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 7739446..fe5056c 100644 (file)
--- a/misc.h
+++ b/misc.h
@@ -5,6 +5,7 @@
 
 #include <stdio.h>                    /* for FILE * */
 #include <stdarg.h>                   /* for va_list */
+#include <time.h>                      /* 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 (file)
index 0000000..82488e6
--- /dev/null
+++ b/time.c
@@ -0,0 +1,10 @@
+#include <time.h>
+#include <assert.h>
+
+struct tm ltime(void)
+{
+    time_t t;
+    time(&t);
+    assert (t != ((time_t)-1));
+    return *localtime(&t);
+}
index 0f3666f..c774c8b 100644 (file)
@@ -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);
index 9edf110..3bad7c3 100644 (file)
@@ -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);
index e80dbfe..7772f55 100644 (file)
@@ -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 (file)
index 0000000..eea0cf6
--- /dev/null
@@ -0,0 +1,20 @@
+#include "putty.h"
+#include <time.h>
+
+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;
+}