newLISP Fan Club

Forum => newLISP and the O.S. => Topic started by: kosh on August 24, 2014, 09:45:44 AM

Title: mingw patch
Post by: kosh on August 24, 2014, 09:45:44 AM
Hi Lutz.



This patch is solves about 2 problems on MinGW compile.



1. _WIN32_WINNT macros must define before windows.h and other headers



Because of most of mingw headers (stdio.h, signal.h, ...) include _mingw.h and sdkddkver.h.

And define _WIN32_WINNT there if not defined.


diff --git a/newlisp.h b/newlisp.h
index d7537fd..23ce42a 100644
--- a/newlisp.h
+++ b/newlisp.h
@@ -159,6 +159,16 @@ This is for 64bit large file support (LFS),
 #define _FILE_OFFSET_BITS 64
 #endif
 
+#ifdef WINDOWS
+/* NOTE:
+ * Windows XP [0x0501] end of support on 2014/04/08
+ * WIndows Vista [0x0600] support ends on 2017/04/11
+ */
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0600
+#endif
+#endif
+
 #include <signal.h>
 #include <errno.h>
 #include <stdio.h>
@@ -188,9 +198,6 @@ This is for 64bit large file support (LFS),
 #endif
 
 #ifdef WINDOWS
-#ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0501
-#endif
 #include <windef.h>
 #include <winbase.h>
 #else


2. time_t and struct timeval bit size



Windows time_t is now a 64-bit integer by default.

Therefore sizeof(UINT) and sizeof(time_t) are not equal on 32-bit windows.

//http://msdn.microsoft.com/en-us/library/1f4c8f33.aspx



And struct timeval tv.tv_sec is different type long (windows) and time_t (linux).


diff --git a/nl-filesys.c b/nl-filesys.c
index 99a6c84..5655132 100644
--- a/nl-filesys.c
+++ b/nl-filesys.c
@@ -2454,7 +2454,7 @@ struct tm * ltm;
 char * ct;
 char * fmt;
 ssize_t offset;
-time_t tme;
+UINT tme;
 size_t size;
 
 #ifdef SUPPORT_UTF8
@@ -2474,8 +2474,8 @@ if(params == nilCell)
     }
 else
     {
-    params = getInteger(params, (UINT *)&tme);
-    t = tme;
+    params = getInteger(params, &tme);
+    t = (time_t)tme;
     
     if(params != nilCell)
         {
@@ -2524,10 +2524,12 @@ INT64 microSecTime(void)
 {
 struct timeval tv;
 struct tm * ttm;
+time_t sec;
 
 gettimeofday(&tv, NULL);
 
-ttm = localtime((time_t *)&tv.tv_sec);
+sec = tv.tv_sec;
+ttm = localtime(&sec);
 
 return (ttm->tm_hour * 3600000000LL +
        ttm->tm_min * 60000000LL + ttm->tm_sec * 1000000 +
@@ -2650,6 +2652,7 @@ UINT isdst;
 TIME_ZONE_INFORMATION timeZone;
 #endif
 ssize_t offset = 0;
+time_t sec;
 CELL * cell;
 
 gettimeofday(&tv, NULL); /* get secs and microsecs */
@@ -2681,7 +2684,8 @@ gmtoff = ltm->tm_gmtoff/60;
 GetTimeZoneInformation(&timeZone);
 #endif
 
-ttm = gmtime((time_t *)&tv.tv_sec);
+sec = tv.tv_sec;
+ttm = gmtime(&sec);
 
 cell = stuffIntegerList(
     11,
@@ -2731,10 +2735,12 @@ CELL * p_dateList(CELL * params)
 {
 struct tm *ttm;
 ssize_t timeValue;
+time_t timer;
 CELL * cell;
 
 params = getInteger(params, (UINT*)&timeValue);
-ttm = gmtime((time_t *)&timeValue);
+timer = (time_t)timeValue;
+ttm = gmtime(&timer);
 
 cell = stuffIntegerList(
     8,
@@ -2804,12 +2810,11 @@ dateValue = 367 * year - (7 * (year + ((month + 9) / 12)))/4
 dateValue = dateValue * 24 * 3600 + hour * 3600 + min * 60 + sec
             - 413319296; /* correction for 1970-1-1 */
 
-#ifdef NEWLISP64
 if(dateValue & 0x80000000)
     dateValue = dateValue | 0xFFFFFFFF00000000;
 else
-    dateValue = dateValue & 0xffffffff;
-#endif
+    dateValue = dateValue & 0x00000000ffffffff;
 
 return(dateValue);
 }


Regards.
Title: Re: mingw patch
Post by: Lutz on August 25, 2014, 10:02:01 AM
Thankyou very much Kosh, changes here:



http://www.newlisp.org/downloads/development/inprogress/
Title: Re: mingw patch
Post by: kosh on August 27, 2014, 12:40:39 PM
Lutz, thank you for the merge my patch.

However please modified a little.


$ wget http://www.newlisp.org/downloads/development/inprogress/newlisp-10.6.1.tgz
$ tar xf newlisp-10.6.1.tgz
$ cd newlisp-10.6.1
$ make -f makefile_mingw_utf8 CC=gcc STRIP=touch
...
$ newlisp
newLISP v.10.6.1 32-bit on Win32 IPv4/6 UTF-8, options: newlisp -h

> (date-value 1970 1 1)
210453397504   ; NG. should == 0
> (date-list (date-value 1970 1 1))
core dump.


variable 'dateValue' in calcDateValue must run bitwise operation part if on windows (line 2819-2824).

patch file here:


diff --git a/nl-filesys.c b/nl-filesys.c
index 34d75ae..82c8327 100644
--- a/nl-filesys.c
+++ b/nl-filesys.c
@@ -2816,7 +2816,7 @@ dateValue = 367 * year - (7 * (year + ((month + 9) / 12)))/4
 dateValue = dateValue * 24 * 3600 + hour * 3600 + min * 60 + sec
             - 413319296; /* correction for 1970-1-1 */

-#ifdef NEWLISP64
+#if defined(NEWLISP64) || defined(WINDOWS)
 if(dateValue & 0x80000000)
     dateValue = dateValue | 0xFFFFFFFF00000000;
 else
Title: Re: mingw patch
Post by: Lutz on August 27, 2014, 02:06:33 PM
Thanks Kosh. On my older version of MinGW (3.15.2) time_t is still 32 bit and date-value and date-list work as they should. For now I just have put a comment in the code.



Perhaps we should check __MINGW32_VERSION. What version of MINGW are you running?



ps: looks like MinGW-w64 is used for both, 32 and 64 bit Windows. Are you using MinGW-w64? Is there a constant in _mingw.h, we can use? E.g.:
#if defined(NEWLISP64) || defined(__MINGW64_VERSION)
that would then make it work on both versions of MinGW.
Title: Re: mingw patch
Post by: Lutz on August 28, 2014, 08:08:31 AM
This: http://www.newlisp.org/downloads/development/inprogress/



now should work independent of compilers and without compiler warning messages when time_t is 64-bit on 32-bit newLISP
Title: Re: mingw patch
Post by: kosh on August 29, 2014, 01:40:24 AM
Quote from: "Lutz"This: http://www.newlisp.org/downloads/development/inprogress/



now should work independent of compilers and without compiler warning messages when time_t is 64-bit on 32-bit newLISP


It looks good to me :) thanks again.


Quote from: "Lutz"What version of MINGW are you running?


I used MinGW gcc 4.8.1 (latest version). MinGW-w64 installed, but not used this issue.