~ chicken-core (chicken-5) 700500202d1f948bed363ad2dfc599ec5e642587


commit 700500202d1f948bed363ad2dfc599ec5e642587
Author:     Peter Bex <peter.bex@xs4all.nl>
AuthorDate: Mon Nov 18 22:06:13 2013 +0100
Commit:     Moritz Heidkamp <moritz@twoticketsplease.de>
CommitDate: Sun Dec 1 14:52:49 2013 +0100

    Remove HAVE_GCVT check and definition. Use snprintf on all platforms.
    
    gcvt() is deprecated in POSIX, and it produces problematic output in
    at least cygwin.  For example, -0.0 is printed as "0.0" and more fun
    things.  This fix adds a regression test for the negative zero case.
    
    Replace sprintf usage with snprintf just in case someone decides to
    use insane values for flonum_print_precision.
    
    Signed-off-by: Moritz Heidkamp <moritz@twoticketsplease.de>

diff --git a/Makefile.aix b/Makefile.aix
index d4534994..8bde9468 100644
--- a/Makefile.aix
+++ b/Makefile.aix
@@ -97,7 +97,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA_H 1" >>$@
 	echo "#define HAVE_GRP_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_SYSEXITS_H 1" >>$@
 	echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@
 ifdef GCHOOKS
diff --git a/Makefile.cross-linux-mingw b/Makefile.cross-linux-mingw
index c18dd185..e1778cae 100644
--- a/Makefile.cross-linux-mingw
+++ b/Makefile.cross-linux-mingw
@@ -116,7 +116,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA_H 1" >>$@
 	echo "#define HAVE_DIRECT_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_LOADLIBRARY 1" >>$@
 	echo "#define HAVE_GETPROCADDRESS 1" >>$@
 	echo "#define HAVE_WINSOCK2_H 1" >>$@
diff --git a/Makefile.cygwin b/Makefile.cygwin
index b4122efb..1518a16e 100644
--- a/Makefile.cygwin
+++ b/Makefile.cygwin
@@ -113,7 +113,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA_H 1" >>$@
 	echo "#define HAVE_GRP_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_SYSEXITS_H 1" >>$@
 	echo "#define HAVE_DLFCN_H 1" >>$@
 	echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@
diff --git a/Makefile.haiku b/Makefile.haiku
index c6e33abd..63da7c00 100644
--- a/Makefile.haiku
+++ b/Makefile.haiku
@@ -91,7 +91,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA_H 1" >>$@
 	echo "#define HAVE_GRP_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_MEMMOVE 1" >>$@
 	echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@
 	echo "#define SIGIO 0" >>$@
diff --git a/Makefile.hurd b/Makefile.hurd
index 74c8c007..1a641585 100644
--- a/Makefile.hurd
+++ b/Makefile.hurd
@@ -92,7 +92,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA_H 1" >>$@
 	echo "#define HAVE_GRP_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_SYSEXITS_H 1" >>$@
 	echo "#define HAVE_MEMMOVE 1" >>$@
 	echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@
diff --git a/Makefile.linux b/Makefile.linux
index 11d3b9fa..7f5898db 100644
--- a/Makefile.linux
+++ b/Makefile.linux
@@ -92,7 +92,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA_H 1" >>$@
 	echo "#define HAVE_GRP_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_SYSEXITS_H 1" >>$@
 	echo "#define HAVE_MEMMOVE 1" >>$@
 	echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@
diff --git a/Makefile.mingw b/Makefile.mingw
index e34f604c..48b801df 100644
--- a/Makefile.mingw
+++ b/Makefile.mingw
@@ -110,7 +110,6 @@ chicken-config.h: chicken-defaults.h
 	echo #define STDC_HEADERS 1 >>$@
 	echo #define HAVE_DIRECT_H 1 >>$@
 	echo #define HAVE_ERRNO_H 1 >>$@
-	echo #define HAVE_GCVT 1 >>$@
 	echo #define HAVE_LOADLIBRARY 1 >>$@
 	echo #define HAVE_GETPROCADDRESS 1 >>$@
 	echo #define HAVE_WINSOCK2_H 1 >>$@
diff --git a/Makefile.mingw-msys b/Makefile.mingw-msys
index d2ac34b4..74607aa2 100644
--- a/Makefile.mingw-msys
+++ b/Makefile.mingw-msys
@@ -109,7 +109,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define STDC_HEADERS 1" >>$@
 	echo "#define HAVE_DIRECT_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_LOADLIBRARY 1" >>$@
 	echo "#define HAVE_GETPROCADDRESS 1" >>$@
 	echo "#define HAVE_WINSOCK2_H 1" >>$@
diff --git a/Makefile.solaris b/Makefile.solaris
index 99f98263..a7c8894c 100644
--- a/Makefile.solaris
+++ b/Makefile.solaris
@@ -119,7 +119,6 @@ chicken-config.h: chicken-defaults.h
 	echo "#define HAVE_ALLOCA 1" >>$@
 	echo "#define HAVE_GRP_H 1" >>$@
 	echo "#define HAVE_ERRNO_H 1" >>$@
-	echo "#define HAVE_GCVT 1" >>$@
 	echo "#define HAVE_SYSEXITS_H 1" >>$@
 	echo "#define C_STACK_GROWS_DOWNWARD 1" >>$@
 ifdef GCHOOKS
diff --git a/chicken.h b/chicken.h
index c018f13c..bee2e37f 100644
--- a/chicken.h
+++ b/chicken.h
@@ -942,7 +942,6 @@ DECL_C_PROC_p0 (128,  1,0,0,0,0,0,0,0)
 # define C_fopen                    fopen
 # define C_fclose                   fclose
 # define C_strpbrk                  strpbrk
-# define C_gcvt                     gcvt
 # define C_sprintf                  sprintf
 # define C_snprintf                 snprintf
 # define C_printf                   printf
diff --git a/runtime.c b/runtime.c
index 555c76bb..93463c22 100644
--- a/runtime.c
+++ b/runtime.c
@@ -4130,11 +4130,7 @@ C_regparm C_word C_fcall C_display_flonum(C_word port, C_word n)
 {
   C_FILEPTR fp = C_port_file(port);
 
-#ifdef HAVE_GCVT
-  C_fprintf(fp, C_text("%s"), C_gcvt(C_flonum_magnitude(n), flonum_print_precision, buffer));
-#else
   C_fprintf(fp, C_text("%.*g"), flonum_print_precision, C_flonum_magnitude(n));
-#endif
   return C_SCHEME_UNDEFINED;
 }
 
@@ -7800,11 +7796,9 @@ void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num,
       goto fini;
     }
 
-#ifdef HAVE_GCVT
-    p = C_gcvt(f, flonum_print_precision, buffer); /* p unused, but we want to avoid stupid warnings */
-#else
-    C_sprintf(buffer, C_text("%.*g"), flonum_print_precision, f);
-#endif
+    C_snprintf(buffer, STRING_BUFFER_SIZE, C_text("%.*g"),
+	       flonum_print_precision, f);
+    buffer[STRING_BUFFER_SIZE-1] = '\0';
 
     if((p = C_strpbrk(buffer, C_text(".eE"))) == NULL) {
       if(*buffer == 'i' || *buffer == 'n') { /* inf or nan */
@@ -7813,11 +7807,6 @@ void C_ccall C_number_to_string(C_word c, C_word closure, C_word k, C_word num,
       }
       else if(buffer[ 1 ] != 'i') C_strcat(buffer, C_text(".0")); /* negative infinity? */
     }
-#ifdef __MINGW32__
-    /* On mingw32, gcvt(3) does not add a trailing zero */
-    else if(buffer[ C_strlen(buffer) - 1 ] == '.')
-      C_strcat(buffer, C_text("0"));
-#endif
 
     p = buffer;
   }
diff --git a/tests/numbers-string-conversion-tests.scm b/tests/numbers-string-conversion-tests.scm
index 33731172..3c19c24d 100644
--- a/tests/numbers-string-conversion-tests.scm
+++ b/tests/numbers-string-conversion-tests.scm
@@ -17,6 +17,7 @@
 (define the-nan (fp/ 0.0 0.0))
 (define pos-inf (fp/ 1.0 0.0))
 (define neg-inf (fp/ -1.0 0.0))
+(define neg-zero (/ -1.0 +inf.0))
 
 (define (nan? x) (and (number? x) (not (= x x))))
 
@@ -171,7 +172,7 @@
  ("1#e2" 1000.0 1500.0 "1000.0" "1500.0" "1000." "1500." "1.0e3" "15.0e2")
  ("1e2#" #f)
 
- "NaN, Inf"
+ "NaN, Inf, negative zero"
  ("+nan.0" the-nan "+NaN.0")
  ("+NAN.0" the-nan "+nan.0" "+NaN.0")
  ("+nan.1" #f)
@@ -193,6 +194,7 @@
  ("#i+nan.0" the-nan "+nan.0" "+NaN.0")
  ("#i+inf.0" pos-inf "+inf.0" "+Inf.0")
  ("#i-inf.0" neg-inf "-inf.0" "-Inf.0")
+ ("-0.0" neg-zero "-.0" "-0.")
  ;; These used to be accepted but are invalid
  ("+nan" #f)
  ("+inf" #f)
Trap