aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/printf.h29
-rw-r--r--include/printf_buffer.h36
-rw-r--r--libio/tst-vtables-common.c9
-rw-r--r--libio/vsnprintf.c131
-rw-r--r--stdio-common/printf_buffer_flush.c23
-rw-r--r--stdio-common/printf_fp.c736
-rw-r--r--stdio-common/printf_fphex.c260
-rw-r--r--stdio-common/vfprintf-internal.c664
-rw-r--r--stdio-common/vfprintf-process-arg.c172
-rw-r--r--stdlib/strfmon_l.c196
-rw-r--r--stdlib/strfrom-skeleton.c38
-rw-r--r--sysdeps/ia64/fpu/printf_fphex.c8
-rw-r--r--sysdeps/ieee754/ldbl-128/printf_fphex_macros.h36
-rw-r--r--sysdeps/ieee754/ldbl-128ibm/printf_fphex.c36
-rw-r--r--sysdeps/ieee754/ldbl-96/printf_fphex.c22
-rw-r--r--sysdeps/x86_64/fpu/printf_fphex.c21
16 files changed, 943 insertions, 1474 deletions
diff --git a/include/printf.h b/include/printf.h
index 5127a45f9b..2c998059d4 100644
--- a/include/printf.h
+++ b/include/printf.h
@@ -65,18 +65,31 @@ int __translated_number_width (locale_t loc,
const char *first, const char *last)
attribute_hidden;
-extern int __printf_fphex (FILE *, const struct printf_info *,
- const void *const *) attribute_hidden;
+
+struct __printf_buffer;
+void __printf_buffer (struct __printf_buffer *buf, const char *format,
+ va_list ap, unsigned int mode_flags);
+struct __wprintf_buffer;
+void __wprintf_buffer (struct __wprintf_buffer *buf, const wchar_t *format,
+ va_list ap, unsigned int mode_flags);
+
extern int __printf_fp (FILE *, const struct printf_info *,
const void *const *);
libc_hidden_proto (__printf_fp)
-extern int __printf_fp_l (FILE *, locale_t, const struct printf_info *,
- const void *const *);
-libc_hidden_proto (__printf_fp_l)
-extern unsigned int __guess_grouping (unsigned int intdig_max,
- const char *grouping)
- attribute_hidden;
+void __printf_fphex_l_buffer (struct __printf_buffer *, locale_t,
+ const struct printf_info *,
+ const void *const *) attribute_hidden;
+void __printf_fp_l_buffer (struct __printf_buffer *, locale_t,
+ const struct printf_info *,
+ const void *const *) attribute_hidden;
+struct __wprintf_buffer;
+void __wprintf_fphex_l_buffer (struct __wprintf_buffer *, locale_t,
+ const struct printf_info *,
+ const void *const *) attribute_hidden;
+void __wprintf_fp_l_buffer (struct __wprintf_buffer *, locale_t,
+ const struct printf_info *,
+ const void *const *) attribute_hidden;
# endif /* !_ISOMAC */
#endif
diff --git a/include/printf_buffer.h b/include/printf_buffer.h
index ad08a72b71..e89f984aca 100644
--- a/include/printf_buffer.h
+++ b/include/printf_buffer.h
@@ -44,7 +44,12 @@
enum __printf_buffer_mode
{
__printf_buffer_mode_failed,
+ __printf_buffer_mode_snprintf,
__printf_buffer_mode_to_file,
+ __printf_buffer_mode_strfmon,
+ __printf_buffer_mode_fp, /* For __printf_fp_l_buffer. */
+ __printf_buffer_mode_fp_to_wide, /* For __wprintf_fp_l_buffer. */
+ __printf_buffer_mode_fphex_to_wide, /* For __wprintf_fphex_l_buffer. */
};
/* Buffer for fast character writing with overflow handling.
@@ -266,13 +271,44 @@ bool __wprintf_buffer_flush (struct __wprintf_buffer *buf) attribute_hidden;
#define Xprintf_buffer_puts Xprintf (buffer_puts)
#define Xprintf_buffer_write Xprintf (buffer_write)
+/* Commonly used buffers. */
+
+struct __printf_buffer_snprintf
+{
+ struct __printf_buffer base;
+ char discard[128]; /* Used in counting mode. */
+};
+
+/* Sets up [BUFFER, BUFFER + LENGTH) as the write target. If LENGTH
+ is positive, also writes a NUL byte to *BUFFER. */
+void __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *,
+ char *buffer, size_t length)
+ attribute_hidden;
+
+/* Add the null terminator after everything has been written. The
+ return value is the one expected by printf (see __printf_buffer_done). */
+int __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *)
+ attribute_hidden;
+
/* Flush function implementations follow. They are called from
__printf_buffer_flush. Generic code should not call these flush
functions directly. Some modes have inline implementations. */
+void __printf_buffer_flush_snprintf (struct __printf_buffer_snprintf *)
+ attribute_hidden;
struct __printf_buffer_to_file;
void __printf_buffer_flush_to_file (struct __printf_buffer_to_file *)
attribute_hidden;
+struct __printf_buffer_fp;
+void __printf_buffer_flush_fp (struct __printf_buffer_fp *)
+ attribute_hidden;
+struct __printf_buffer_fp_to_wide;
+void __printf_buffer_flush_fp_to_wide (struct __printf_buffer_fp_to_wide *)
+ attribute_hidden;
+struct __printf_buffer_fphex_to_wide;
+void __printf_buffer_flush_fphex_to_wide (struct
+ __printf_buffer_fphex_to_wide *)
+ attribute_hidden;
struct __wprintf_buffer_to_file;
void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *)
diff --git a/libio/tst-vtables-common.c b/libio/tst-vtables-common.c
index d18df23e55..a310e516f2 100644
--- a/libio/tst-vtables-common.c
+++ b/libio/tst-vtables-common.c
@@ -409,11 +409,14 @@ void _IO_init (FILE *fp, int flags);
static void
with_compatibility_fprintf (void *closure)
{
+ /* A temporary staging buffer is used in the current fprintf
+ implementation, which is why there is just one call to
+ xsputn. */
TEST_COMPARE (fprintf_ptr (shared->fp, "A%sCD", "B"), 4);
- TEST_COMPARE (shared->calls, 3);
- TEST_COMPARE (shared->calls_xsputn, 3);
+ TEST_COMPARE (shared->calls, 1);
+ TEST_COMPARE (shared->calls_xsputn, 1);
TEST_COMPARE_BLOB (shared->buffer, shared->buffer_length,
- "CD", 2);
+ "ABCD", 4);
}
static void
diff --git a/libio/vsnprintf.c b/libio/vsnprintf.c
index 8dae66761d..7a9667f966 100644
--- a/libio/vsnprintf.c
+++ b/libio/vsnprintf.c
@@ -25,97 +25,76 @@
in files containing the exception. */
#include "libioP.h"
-#include "strfile.h"
-static int _IO_strn_overflow (FILE *fp, int c) __THROW;
+#include <array_length.h>
+#include <printf.h>
+#include <printf_buffer.h>
-static int
-_IO_strn_overflow (FILE *fp, int c)
+void
+__printf_buffer_flush_snprintf (struct __printf_buffer_snprintf *buf)
{
- /* When we come to here this means the user supplied buffer is
- filled. But since we must return the number of characters which
- would have been written in total we must provide a buffer for
- further use. We can do this by writing on and on in the overflow
- buffer in the _IO_strnfile structure. */
- _IO_strnfile *snf = (_IO_strnfile *) fp;
-
- if (fp->_IO_buf_base != snf->overflow_buf)
+ /* Record the bytes written so far, before switching buffers. */
+ buf->base.written += buf->base.write_ptr - buf->base.write_base;
+
+ if (buf->base.write_base != buf->discard)
{
- /* Terminate the string. We know that there is room for at
- least one more character since we initialized the stream with
- a size to make this possible. */
- *fp->_IO_write_ptr = '\0';
-
- _IO_setb (fp, snf->overflow_buf,
- snf->overflow_buf + sizeof (snf->overflow_buf), 0);
-
- fp->_IO_write_base = snf->overflow_buf;
- fp->_IO_read_base = snf->overflow_buf;
- fp->_IO_read_ptr = snf->overflow_buf;
- fp->_IO_read_end = snf->overflow_buf + sizeof (snf->overflow_buf);
- }
+ /* We just finished writing the caller-supplied buffer. Force
+ NUL termination if the string length is not zero. */
+ if (buf->base.write_base != buf->base.write_end)
+ buf->base.write_end[-1] = '\0';
- fp->_IO_write_ptr = snf->overflow_buf;
- fp->_IO_write_end = snf->overflow_buf;
- /* Since we are not really interested in storing the characters
- which do not fit in the buffer we simply ignore it. */
- return c;
-}
+ /* Switch to the discard buffer. */
+ buf->base.write_base = buf->discard;
+ buf->base.write_ptr = buf->discard;
+ buf->base.write_end = array_end (buf->discard);
+ }
+ buf->base.write_base = buf->discard;
+ buf->base.write_ptr = buf->discard;
+}
-const struct _IO_jump_t _IO_strn_jumps libio_vtable attribute_hidden =
+void
+__printf_buffer_snprintf_init (struct __printf_buffer_snprintf *buf,
+ char *buffer, size_t length)
{
- JUMP_INIT_DUMMY,
- JUMP_INIT(finish, _IO_str_finish),
- JUMP_INIT(overflow, _IO_strn_overflow),
- JUMP_INIT(underflow, _IO_str_underflow),
- JUMP_INIT(uflow, _IO_default_uflow),
- JUMP_INIT(pbackfail, _IO_str_pbackfail),
- JUMP_INIT(xsputn, _IO_default_xsputn),
- JUMP_INIT(xsgetn, _IO_default_xsgetn),
- JUMP_INIT(seekoff, _IO_str_seekoff),
- JUMP_INIT(seekpos, _IO_default_seekpos),
- JUMP_INIT(setbuf, _IO_default_setbuf),
- JUMP_INIT(sync, _IO_default_sync),
- JUMP_INIT(doallocate, _IO_default_doallocate),
- JUMP_INIT(read, _IO_default_read),
- JUMP_INIT(write, _IO_default_write),
- JUMP_INIT(seek, _IO_default_seek),
- JUMP_INIT(close, _IO_default_close),
- JUMP_INIT(stat, _IO_default_stat),
- JUMP_INIT(showmanyc, _IO_default_showmanyc),
- JUMP_INIT(imbue, _IO_default_imbue)
-};
+ __printf_buffer_init (&buf->base, buffer, length,
+ __printf_buffer_mode_snprintf);
+ if (length > 0)
+ /* Historic behavior for trivially overlapping buffers (checked by
+ the test suite). */
+ *buffer = '\0';
+}
+int
+__printf_buffer_snprintf_done (struct __printf_buffer_snprintf *buf)
+{
+ /* NB: Do not check for buf->base.fail here. Write the null
+ terminator even in case of errors. */
+
+ if (buf->base.write_ptr < buf->base.write_end)
+ *buf->base.write_ptr = '\0';
+ else if (buf->base.write_ptr > buf->base.write_base)
+ /* If write_ptr == write_base, nothing has been written. No null
+ termination is needed because of the early truncation in
+ __printf_buffer_snprintf_init (the historic behavior).
+
+ We might also be at the start of the discard buffer, but in
+ this case __printf_buffer_flush_snprintf has already written
+ the NUL terminator. */
+ buf->base.write_ptr[-1] = '\0';
+
+ return __printf_buffer_done (&buf->base);
+}
int
__vsnprintf_internal (char *string, size_t maxlen, const char *format,
va_list args, unsigned int mode_flags)
{
- _IO_strnfile sf;
- int ret;
-#ifdef _IO_MTSAFE_IO
- sf.f._sbf._f._lock = NULL;
-#endif
-
- /* We need to handle the special case where MAXLEN is 0. Use the
- overflow buffer right from the start. */
- if (maxlen == 0)
- {
- string = sf.overflow_buf;
- maxlen = sizeof (sf.overflow_buf);
- }
-
- _IO_no_init (&sf.f._sbf._f, _IO_USER_LOCK, -1, NULL, NULL);
- _IO_JUMPS (&sf.f._sbf) = &_IO_strn_jumps;
- string[0] = '\0';
- _IO_str_init_static_internal (&sf.f, string, maxlen - 1, string);
- ret = __vfprintf_internal (&sf.f._sbf._f, format, args, mode_flags);
-
- if (sf.f._sbf._f._IO_buf_base != sf.overflow_buf)
- *sf.f._sbf._f._IO_write_ptr = '\0';
- return ret;
+ struct __printf_buffer_snprintf buf;
+ __printf_buffer_snprintf_init (&buf, string, maxlen);
+ __printf_buffer (&buf.base, format, args, mode_flags);
+ return __printf_buffer_snprintf_done (&buf);
}
int
diff --git a/stdio-common/printf_buffer_flush.c b/stdio-common/printf_buffer_flush.c
index 9b25c0fde5..bfd1f9d733 100644
--- a/stdio-common/printf_buffer_flush.c
+++ b/stdio-common/printf_buffer_flush.c
@@ -16,6 +16,7 @@
License along with the GNU C Library; if not, see
<https://www.gnu.org/licenses/>. */
+#include <errno.h>
#include <printf_buffer.h>
#include "printf_buffer-char.h"
@@ -24,7 +25,11 @@
/* The __printf_buffer_flush_* functions are defined together with
functions that are pulled in by strong references. */
#ifndef SHARED
+# pragma weak __printf_buffer_flush_snprintf
# pragma weak __printf_buffer_flush_to_file
+# pragma weak __printf_buffer_flush_fp
+# pragma weak __printf_buffer_flush_fp_to_wide
+# pragma weak __printf_buffer_flush_fphex_to_wide
#endif /* !SHARED */
static void
@@ -34,9 +39,27 @@ __printf_buffer_do_flush (struct __printf_buffer *buf)
{
case __printf_buffer_mode_failed:
return;
+ case __printf_buffer_mode_snprintf:
+ __printf_buffer_flush_snprintf ((struct __printf_buffer_snprintf *) buf);
+ return;
case __printf_buffer_mode_to_file:
__printf_buffer_flush_to_file ((struct __printf_buffer_to_file *) buf);
return;
+ case __printf_buffer_mode_strfmon:
+ __set_errno (E2BIG);
+ __printf_buffer_mark_failed (buf);
+ return;
+ case __printf_buffer_mode_fp:
+ __printf_buffer_flush_fp ((struct __printf_buffer_fp *) buf);
+ return;
+ case __printf_buffer_mode_fp_to_wide:
+ __printf_buffer_flush_fp_to_wide
+ ((struct __printf_buffer_fp_to_wide *) buf);
+ return;
+ case __printf_buffer_mode_fphex_to_wide:
+ __printf_buffer_flush_fphex_to_wide
+ ((struct __printf_buffer_fphex_to_wide *) buf);
+ return;
}
__builtin_trap ();
}
diff --git a/stdio-common/printf_fp.c b/stdio-common/printf_fp.c
index 3a5560fc16..5c9c30aaee 100644
--- a/stdio-common/printf_fp.c
+++ b/stdio-common/printf_fp.c
@@ -41,90 +41,12 @@
#include <wchar.h>
#include <stdbool.h>
#include <rounding-mode.h>
+#include <printf_buffer.h>
+#include <printf_buffer_to_file.h>
+#include <grouping_iterator.h>
-#ifdef COMPILE_WPRINTF
-# define CHAR_T wchar_t
-#else
-# define CHAR_T char
-#endif
-
-#include "_i18n_number.h"
-
-#ifndef NDEBUG
-# define NDEBUG /* Undefine this for debugging assertions. */
-#endif
#include <assert.h>
-#define PUT(f, s, n) _IO_sputn (f, s, n)
-#define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
-#undef putc
-#define putc(c, f) (wide \
- ? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
-
-
-/* Macros for doing the actual output. */
-
-#define outchar(ch) \
- do \
- { \
- const int outc = (ch); \
- if (putc (outc, fp) == EOF) \
- { \
- if (buffer_malloced) \
- { \
- free (buffer); \
- free (wbuffer); \
- } \
- return -1; \
- } \
- ++done; \
- } while (0)
-
-#define PRINT(ptr, wptr, len) \
- do \
- { \
- size_t outlen = (len); \
- if (len > 20) \
- { \
- if (PUT (fp, wide ? (const char *) wptr : ptr, outlen) != outlen) \
- { \
- if (buffer_malloced) \
- { \
- free (buffer); \
- free (wbuffer); \
- } \
- return -1; \
- } \
- ptr += outlen; \
- done += outlen; \
- } \
- else \
- { \
- if (wide) \
- while (outlen-- > 0) \
- outchar (*wptr++); \
- else \
- while (outlen-- > 0) \
- outchar (*ptr++); \
- } \
- } while (0)
-
-#define PADN(ch, len) \
- do \
- { \
- if (PAD (fp, ch, len) != len) \
- { \
- if (buffer_malloced) \
- { \
- free (buffer); \
- free (wbuffer); \
- } \
- return -1; \
- } \
- done += len; \
- } \
- while (0)
-
/* We use the GNU MP library to handle large numbers.
An MP variable occupies a varying number of entries in its array. We keep
@@ -145,10 +67,6 @@ extern mp_size_t __mpn_extract_long_double (mp_ptr res_ptr, mp_size_t size,
long double value);
-static wchar_t *group_number (wchar_t *buf, wchar_t *bufend,
- unsigned int intdig_no, const char *grouping,
- wchar_t thousands_sep, int ngroups);
-
struct hack_digit_param
{
/* Sign of the exponent. */
@@ -165,7 +83,7 @@ struct hack_digit_param
MPN_VAR(tmp);
};
-static wchar_t
+static char
hack_digit (struct hack_digit_param *p)
{
mp_limb_t hi;
@@ -197,7 +115,7 @@ hack_digit (struct hack_digit_param *p)
/* We're not prepared for an mpn variable with zero
limbs. */
p->fracsize = 1;
- return L'0' + hi;
+ return '0' + hi;
}
}
@@ -206,13 +124,22 @@ hack_digit (struct hack_digit_param *p)
p->frac[p->fracsize++] = _cy;
}
- return L'0' + hi;
+ return '0' + hi;
}
-int
-__printf_fp_l (FILE *fp, locale_t loc,
- const struct printf_info *info,
- const void *const *args)
+/* Version that performs grouping (if INFO->group && THOUSANDS_SEP != 0),
+ but not i18n digit translation.
+
+ The output buffer is always multibyte (not wide) at this stage.
+ Wide conversion and i18n digit translation happen later, with a
+ temporary buffer. To prepare for that, THOUSANDS_SEP_LENGTH is the
+ final length of the thousands separator. */
+static void
+__printf_fp_buffer_1 (struct __printf_buffer *buf, locale_t loc,
+ char thousands_sep, char decimal,
+ unsigned int thousands_sep_length,
+ const struct printf_info *info,
+ const void *const *args)
{
/* The floating-point value to output. */
union
@@ -225,18 +152,11 @@ __printf_fp_l (FILE *fp, locale_t loc,
}
fpnum;
- /* Locale-dependent representation of decimal point. */
- const char *decimal;
- wchar_t decimalwc;
-
- /* Locale-dependent thousands separator and grouping specification. */
- const char *thousands_sep = NULL;
- wchar_t thousands_sepwc = 0;
- const char *grouping;
-
/* "NaN" or "Inf" for the special cases. */
const char *special = NULL;
- const wchar_t *wspecial = NULL;
+
+ /* Used to determine grouping rules. */
+ int lc_category = info->extra ? LC_MONETARY : LC_NUMERIC;
/* When _Float128 is enabled in the library and ABI-distinct from long
double, we need mp_limbs enough for any of them. */
@@ -256,90 +176,16 @@ __printf_fp_l (FILE *fp, locale_t loc,
/* Sign of float number. */
int is_neg = 0;
- /* Counter for number of written characters. */
- int done = 0;
-
/* General helper (carry limb). */
mp_limb_t cy;
- /* Nonzero if this is output on a wide character stream. */
- int wide = info->wide;
-
/* Buffer in which we produce the output. */
- wchar_t *wbuffer = NULL;
- char *buffer = NULL;
+ char *wbuffer = NULL;
/* Flag whether wbuffer and buffer are malloc'ed or not. */
int buffer_malloced = 0;
p.expsign = 0;
- /* Figure out the decimal point character. */
- if (info->extra == 0)
- {
- decimal = _nl_lookup (loc, LC_NUMERIC, DECIMAL_POINT);
- decimalwc = _nl_lookup_word
- (loc, LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
- }
- else
- {
- decimal = _nl_lookup (loc, LC_MONETARY, MON_DECIMAL_POINT);
- if (*decimal == '\0')
- decimal = _nl_lookup (loc, LC_NUMERIC, DECIMAL_POINT);
- decimalwc = _nl_lookup_word (loc, LC_MONETARY,
- _NL_MONETARY_DECIMAL_POINT_WC);
- if (decimalwc == L'\0')
- decimalwc = _nl_lookup_word (loc, LC_NUMERIC,
- _NL_NUMERIC_DECIMAL_POINT_WC);
- }
- /* The decimal point character must not be zero. */
- assert (*decimal != '\0');
- assert (decimalwc != L'\0');
-
- if (info->group)
- {
- if (info->extra == 0)
- grouping = _nl_lookup (loc, LC_NUMERIC, GROUPING);
- else
- grouping = _nl_lookup (loc, LC_MONETARY, MON_GROUPING);
-
- if (*grouping <= 0 || *grouping == CHAR_MAX)
- grouping = NULL;
- else
- {
- /* Figure out the thousands separator character. */
- if (wide)
- {
- if (info->extra == 0)
- thousands_sepwc = _nl_lookup_word
- (loc, LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
- else
- thousands_sepwc =
- _nl_lookup_word (loc, LC_MONETARY,
- _NL_MONETARY_THOUSANDS_SEP_WC);
- }
- else
- {
- if (info->extra == 0)
- thousands_sep = _nl_lookup (loc, LC_NUMERIC, THOUSANDS_SEP);
- else
- thousands_sep = _nl_lookup
- (loc, LC_MONETARY, MON_THOUSANDS_SEP);
- }
-
- if ((wide && thousands_sepwc == L'\0')
- || (! wide && *thousands_sep == '\0'))
- grouping = NULL;