Assume 'long double' exists.
[gnulib.git] / lib / vasnprintf.c
index b13dbd5..06bf034 100644 (file)
@@ -41,6 +41,9 @@
 #include <errno.h>     /* errno */
 #include <limits.h>    /* CHAR_BIT */
 #include <float.h>     /* DBL_MAX_EXP, LDBL_MAX_EXP */
+#if HAVE_NL_LANGINFO
+# include <langinfo.h>
+#endif
 #if WIDE_CHAR_VERSION
 # include "wprintf-parse.h"
 #else
 #include "xsize.h"
 
 #if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
+# include "float+.h"
 # include "isnan.h"
-# include "isnanl.h"
-# if HAVE_LONG_DOUBLE
-#  include "printf-frexp.h"
-#  include "printf-frexpl.h"
-# endif
+# include "printf-frexp.h"
+# include "isnanl-nolibm.h"
+# include "printf-frexpl.h"
+# include "fpucw.h"
+#endif
+
+/* Some systems, like OSF/1 4.0 and Woe32, don't have EOVERFLOW.  */
+#ifndef EOVERFLOW
+# define EOVERFLOW E2BIG
 #endif
 
 #if HAVE_WCHAR_T
@@ -117,6 +125,33 @@ local_wcslen (const wchar_t *s)
 /* Here we need to call the native sprintf, not rpl_sprintf.  */
 #undef sprintf
 
+#if NEED_PRINTF_DIRECTIVE_A && !defined IN_LIBINTL
+/* Determine the decimal-point character according to the current locale.  */
+# ifndef decimal_point_char_defined
+#  define decimal_point_char_defined 1
+static char
+decimal_point_char ()
+{
+  const char *point;
+  /* Determine it in a multithread-safe way.  We know nl_langinfo is
+     multithread-safe on glibc systems, but is not required to be multithread-
+     safe by POSIX.  sprintf(), however, is multithread-safe.  localeconv()
+     is rarely multithread-safe.  */
+#  if HAVE_NL_LANGINFO && __GLIBC__
+  point = nl_langinfo (RADIXCHAR);
+#  elif 1
+  char pointbuf[5];
+  sprintf (pointbuf, "%#.0f", 1.0);
+  point = &pointbuf[1];
+#  else
+  point = localeconv () -> decimal_point;
+#  endif
+  /* The decimal point is always a single byte: either '.' or ','.  */
+  return (point[0] != '\0' ? point[0] : '.');
+}
+# endif
+#endif
+
 CHAR_T *
 VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list args)
 {
@@ -349,7 +384,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                  }
 
                /* Allocate a temporary buffer of sufficient size.  */
-# if HAVE_LONG_DOUBLE
                if (type == TYPE_LONGDOUBLE)
                  tmp_length =
                    (unsigned int) ((LDBL_DIG + 1)
@@ -357,7 +391,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                                   )
                    + 1; /* turn floor into ceil */
                else
-# endif
                  tmp_length =
                    (unsigned int) ((DBL_DIG + 1)
                                    * 0.831 /* decimal -> hexadecimal */
@@ -390,7 +423,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 
                pad_ptr = NULL;
                p = tmp;
-# if HAVE_LONG_DOUBLE
                if (type == TYPE_LONGDOUBLE)
                  {
                    long double arg = a.arg[dp->arg_index].a.a_longdouble;
@@ -409,6 +441,9 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    else
                      {
                        int sign = 0;
+                       DECL_LONG_DOUBLE_ROUNDING
+
+                       BEGIN_LONG_DOUBLE_ROUNDING ();
 
                        if (arg < 0.0L)
                          {
@@ -419,10 +454,8 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          {
                            /* Distinguish 0.0L and -0.0L.  */
                            static long double plus_zero = 0.0L;
-                           long double arg_mem;
-                           memset (&arg_mem, 0, sizeof (long double));
-                           arg_mem = arg;
-                           if (memcmp (&plus_zero, &arg_mem, sizeof (long double)) != 0)
+                           long double arg_mem = arg;
+                           if (memcmp (&plus_zero, &arg_mem, SIZEOF_LDBL) != 0)
                              {
                                sign = -1;
                                arg = -arg;
@@ -499,11 +532,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                              if ((flags & FLAG_ALT)
                                  || mantissa > 0.0L || precision > 0)
                                {
-                                 const char *point =
-                                   localeconv () -> decimal_point;
-                                 /* The decimal point is always a single byte:
-                                    either '.' or ','.  */
-                                 *p++ = (point[0] != '\0' ? point[0] : '.');
+                                 *p++ = decimal_point_char ();
                                  /* This loop terminates because we assume
                                     that FLT_RADIX is a power of 2.  */
                                  while (mantissa > 0.0L)
@@ -526,22 +555,23 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                                }
                              }
                              *p++ = dp->conversion - 'A' + 'P';
-#  if WIDE_CHAR_VERSION
+# if WIDE_CHAR_VERSION
                              {
                                static const wchar_t decimal_format[] =
                                  { '%', '+', 'd', '\0' };
                                SNPRINTF (p, 6 + 1, decimal_format, exponent);
                              }
-#  else
+# else
                              sprintf (p, "%+d", exponent);
-#  endif
+# endif
                              while (*p != '\0')
                                p++;
                          }
+
+                       END_LONG_DOUBLE_ROUNDING ();
                      }
                  }
                else
-# endif
                  {
                    double arg = a.arg[dp->arg_index].a.a_double;
 
@@ -569,10 +599,8 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          {
                            /* Distinguish 0.0 and -0.0.  */
                            static double plus_zero = 0.0;
-                           double arg_mem;
-                           memset (&arg_mem, 0, sizeof (double));
-                           arg_mem = arg;
-                           if (memcmp (&plus_zero, &arg_mem, sizeof (double)) != 0)
+                           double arg_mem = arg;
+                           if (memcmp (&plus_zero, &arg_mem, SIZEOF_DBL) != 0)
                              {
                                sign = -1;
                                arg = -arg;
@@ -649,11 +677,7 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                              if ((flags & FLAG_ALT)
                                  || mantissa > 0.0 || precision > 0)
                                {
-                                 const char *point =
-                                   localeconv () -> decimal_point;
-                                 /* The decimal point is always a single byte:
-                                    either '.' or ','.  */
-                                 *p++ = (point[0] != '\0' ? point[0] : '.');
+                                 *p++ = decimal_point_char ();
                                  /* This loop terminates because we assume
                                     that FLT_RADIX is a power of 2.  */
                                  while (mantissa > 0.0)
@@ -902,7 +926,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      break;
 
                    case 'f': case 'F':
-# if HAVE_LONG_DOUBLE
                      if (type == TYPE_LONGDOUBLE)
                        tmp_length =
                          (unsigned int) (LDBL_MAX_EXP
@@ -912,7 +935,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          + 1 /* turn floor into ceil */
                          + 10; /* sign, decimal point etc. */
                      else
-# endif
                        tmp_length =
                          (unsigned int) (DBL_MAX_EXP
                                          * 0.30103 /* binary -> decimal */
@@ -930,7 +952,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                      break;
 
                    case 'a': case 'A':
-# if HAVE_LONG_DOUBLE
                      if (type == TYPE_LONGDOUBLE)
                        tmp_length =
                          (unsigned int) (LDBL_DIG
@@ -938,7 +959,6 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                                         )
                          + 1; /* turn floor into ceil */
                      else
-# endif
                        tmp_length =
                          (unsigned int) (DBL_DIG
                                          * 0.831 /* decimal -> hexadecimal */
@@ -1057,11 +1077,9 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
 #endif
                    *p++ = 'l';
                    break;
-#if HAVE_LONG_DOUBLE
                  case TYPE_LONGDOUBLE:
                    *p++ = 'L';
                    break;
-#endif
                  default:
                    break;
                  }
@@ -1107,6 +1125,9 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                    retcount = 0;
 
 #if USE_SNPRINTF
+                   /* SNPRINTF can fail if maxlen > INT_MAX.  */
+                   if (maxlen > INT_MAX)
+                     goto overflow;
 # define SNPRINTF_BUF(arg) \
                    switch (prefix_count)                                   \
                      {                                                     \
@@ -1215,14 +1236,12 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
                          SNPRINTF_BUF (arg);
                        }
                        break;
-#if HAVE_LONG_DOUBLE
                      case TYPE_LONGDOUBLE:
                        {
                          long double arg = a.arg[dp->arg_index].a.a_longdouble;
                          SNPRINTF_BUF (arg);
                        }
                        break;
-#endif
                      case TYPE_CHAR:
                        {
                          int arg = a.arg[dp->arg_index].a.a_char;
@@ -1382,6 +1401,15 @@ VASNPRINTF (CHAR_T *resultbuf, size_t *lengthp, const CHAR_T *format, va_list ar
        not have this limitation.  */
     return result;
 
+  overflow:
+    if (!(result == resultbuf || result == NULL))
+      free (result);
+    if (buf_malloced != NULL)
+      free (buf_malloced);
+    CLEANUP ();
+    errno = EOVERFLOW;
+    return NULL;
+
   out_of_memory:
     if (!(result == resultbuf || result == NULL))
       free (result);