aboutsummaryrefslogtreecommitdiff
path: root/stdio-common
diff options
context:
space:
mode:
Diffstat (limited to 'stdio-common')
-rw-r--r--stdio-common/Makefile3
-rw-r--r--stdio-common/_itoa.c4
-rw-r--r--stdio-common/_itowa.c346
-rw-r--r--stdio-common/_itowa.h63
-rw-r--r--stdio-common/itoa-digits.c7
-rw-r--r--stdio-common/itowa-digits.c27
-rw-r--r--stdio-common/printf-parse.h71
-rw-r--r--stdio-common/printf-prs.c4
-rw-r--r--stdio-common/printf.h3
-rw-r--r--stdio-common/printf_fp.c13
-rw-r--r--stdio-common/printf_size.c3
-rw-r--r--stdio-common/vfprintf.c585
-rw-r--r--stdio-common/vfscanf.c1313
-rw-r--r--stdio-common/vfwprintf.c3
-rw-r--r--stdio-common/vfwscanf.c2
15 files changed, 1874 insertions, 573 deletions
diff --git a/stdio-common/Makefile b/stdio-common/Makefile
index 9fb0c5d15d..74900189a8 100644
--- a/stdio-common/Makefile
+++ b/stdio-common/Makefile
@@ -25,11 +25,12 @@ headers := printf.h
routines := \
ctermid cuserid \
- _itoa itoa-digits \
+ _itoa _itowa itoa-digits itowa-digits \
vfprintf vprintf printf_fp reg-printf printf-prs printf_fphex \
printf_size fprintf printf snprintf sprintf asprintf dprintf \
vfscanf \
fscanf scanf sscanf \
+ vfwprintf vfwscanf \
perror psignal \
tmpfile tmpfile64 tmpnam tmpnam_r tempnam tempname \
getline getw putw \
diff --git a/stdio-common/_itoa.c b/stdio-common/_itoa.c
index 3a7cd78003..2eca838229 100644
--- a/stdio-common/_itoa.c
+++ b/stdio-common/_itoa.c
@@ -78,7 +78,7 @@ struct base_table_t
/* Local variables. */
-static const struct base_table_t base_table[] =
+const struct base_table_t _itoa_base_table[] =
{
#if BITS_PER_MP_LIMB == 64
/* 2 */ {SEL1(0ul) 1, 1},
@@ -171,7 +171,7 @@ _itoa (value, buflim, base, upper_case)
{
const char *digits = upper_case ? _itoa_upper_digits : _itoa_lower_digits;
char *bp = buflim;
- const struct base_table_t *brec = &base_table[base - 2];
+ const struct base_table_t *brec = &_itoa_base_table[base - 2];
switch (base)
{
diff --git a/stdio-common/_itowa.c b/stdio-common/_itowa.c
new file mode 100644
index 0000000000..430415b96b
--- /dev/null
+++ b/stdio-common/_itowa.c
@@ -0,0 +1,346 @@
+/* Internal function for converting integers to ASCII.
+ Copyright (C) 1994, 1995, 1996, 1999 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+ Contributed by Torbjorn Granlund <tege@matematik.su.se>
+ and Ulrich Drepper <drepper@gnu.org>.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <gmp-mparam.h>
+#include <stdlib/gmp.h>
+#include <stdlib/gmp-impl.h>
+#include <stdlib/longlong.h>
+
+#include "_itowa.h"
+
+
+/* Canonize environment. For some architectures not all values might
+ be defined in the GMP header files. */
+#ifndef UMUL_TIME
+# define UMUL_TIME 1
+#endif
+#ifndef UDIV_TIME
+# define UDIV_TIME 3
+#endif
+
+/* Control memory layout. */
+#ifdef PACK
+# undef PACK
+# define PACK __attribute__ ((packed))
+#else
+# define PACK
+#endif
+
+
+/* Declare local types. */
+struct base_table_t
+{
+#if (UDIV_TIME > 2 * UMUL_TIME)
+ mp_limb_t base_multiplier;
+#endif
+ char flag;
+ char post_shift;
+#if BITS_PER_MP_LIMB == 32
+ struct
+ {
+ char normalization_steps;
+ char ndigits;
+ mp_limb_t base PACK;
+#if UDIV_TIME > 2 * UMUL_TIME
+ mp_limb_t base_ninv PACK;
+#endif
+ } big;
+#endif
+};
+
+/* To reduce the memory needed we include some fields of the tables
+ only conditionally. */
+#if UDIV_TIME > 2 * UMUL_TIME
+# define SEL1(X) X,
+# define SEL2(X) ,X
+#else
+# define SEL1(X)
+# define SEL2(X)
+#endif
+
+/* Factor table for the different bases. */
+extern const struct base_table_t _itoa_base_table[];
+
+/* Lower-case digits. */
+extern const wchar_t _itowa_lower_digits[];
+/* Upper-case digits. */
+extern const wchar_t _itowa_upper_digits[];
+
+
+wchar_t *
+_itowa (value, buflim, base, upper_case)
+ unsigned long long int value;
+ wchar_t *buflim;
+ unsigned int base;
+ int upper_case;
+{
+ const wchar_t *digits = (upper_case
+ ? _itowa_upper_digits : _itowa_lower_digits);
+ wchar_t *bp = buflim;
+ const struct base_table_t *brec = &_itoa_base_table[base - 2];
+
+ switch (base)
+ {
+#define RUN_2N(BITS) \
+ do \
+ { \
+ /* `unsigned long long int' always has 64 bits. */ \
+ mp_limb_t work_hi = value >> (64 - BITS_PER_MP_LIMB); \
+ \
+ if (BITS_PER_MP_LIMB == 32) \
+ { \
+ if (work_hi != 0) \
+ { \
+ mp_limb_t work_lo; \
+ int cnt; \
+ \
+ work_lo = value & 0xfffffffful; \
+ for (cnt = BITS_PER_MP_LIMB / BITS; cnt > 0; --cnt) \
+ { \
+ *--bp = digits[work_lo & ((1ul << BITS) - 1)]; \
+ work_lo >>= BITS; \
+ } \
+ if (BITS_PER_MP_LIMB % BITS != 0) \
+ { \
+ work_lo \
+ |= ((work_hi \
+ & ((1 << (BITS - BITS_PER_MP_LIMB%BITS)) \
+ - 1)) \
+ << BITS_PER_MP_LIMB % BITS); \
+ work_hi >>= BITS - BITS_PER_MP_LIMB % BITS; \
+ if (work_hi == 0) \
+ work_hi = work_lo; \
+ else \
+ *--bp = digits[work_lo]; \
+ } \
+ } \
+ else \
+ work_hi = value & 0xfffffffful; \
+ } \
+ do \
+ { \
+ *--bp = digits[work_hi & ((1 << BITS) - 1)]; \
+ work_hi >>= BITS; \
+ } \
+ while (work_hi != 0); \
+ } \
+ while (0)
+ case 8:
+ RUN_2N (3);
+ break;
+
+ case 16:
+ RUN_2N (4);
+ break;
+
+ default:
+ {
+#if BITS_PER_MP_LIMB == 64
+ mp_limb_t base_multiplier = brec->base_multiplier;
+ if (brec->flag)
+ while (value != 0)
+ {
+ mp_limb_t quo, rem, x, dummy;
+
+ umul_ppmm (x, dummy, value, base_multiplier);
+ quo = (x + ((value - x) >> 1)) >> (brec->post_shift - 1);
+ rem = value - quo * base;
+ *--bp = digits[rem];
+ value = quo;
+ }
+ else
+ while (value != 0)
+ {
+ mp_limb_t quo, rem, x, dummy;
+
+ umul_ppmm (x, dummy, value, base_multiplier);
+ quo = x >> brec->post_shift;
+ rem = value - quo * base;
+ *--bp = digits[rem];
+ value = quo;
+ }
+#endif
+#if BITS_PER_MP_LIMB == 32
+ mp_limb_t t[3];
+ int n;
+
+ /* First convert x0 to 1-3 words in base s->big.base.
+ Optimize for frequent cases of 32 bit numbers. */
+ if ((mp_limb_t) (value >> 32) >= 1)
+ {
+#if UDIV_TIME > 2 * UMUL_TIME || UDIV_NEEDS_NORMALIZATION
+ int big_normalization_steps = brec->big.normalization_steps;
+ mp_limb_t big_base_norm
+ = brec->big.base << big_normalization_steps;
+#endif
+ if ((mp_limb_t) (value >> 32) >= brec->big.base)
+ {
+ mp_limb_t x1hi, x1lo, r;
+ /* If you want to optimize this, take advantage of
+ that the quotient in the first udiv_qrnnd will
+ always be very small. It might be faster just to
+ subtract in a tight loop. */
+
+#if UDIV_TIME > 2 * UMUL_TIME
+ mp_limb_t x, xh, xl;
+
+ if (big_normalization_steps == 0)
+ xh = 0;
+ else
+ xh = (mp_limb_t) (value >> (64 - big_normalization_steps));
+ xl = (mp_limb_t) (value >> (32 - big_normalization_steps));
+ udiv_qrnnd_preinv (x1hi, r, xh, xl, big_base_norm,
+ brec->big.base_ninv);
+
+ xl = ((mp_limb_t) value) << big_normalization_steps;
+ udiv_qrnnd_preinv (x1lo, x, r, xl, big_base_norm,
+ brec->big.base_ninv);
+ t[2] = x >> big_normalization_steps;
+
+ if (big_normalization_steps == 0)
+ xh = x1hi;
+ else
+ xh = ((x1hi << big_normalization_steps)
+ | (x1lo >> (32 - big_normalization_steps)));
+ xl = x1lo << big_normalization_steps;
+ udiv_qrnnd_preinv (t[0], x, xh, xl, big_base_norm,
+ brec->big.base_ninv);
+ t[1] = x >> big_normalization_steps;
+#elif UDIV_NEEDS_NORMALIZATION
+ mp_limb_t x, xh, xl;
+
+ if (big_normalization_steps == 0)
+ xh = 0;
+ else
+ xh = (mp_limb_t) (value >> 64 - big_normalization_steps);
+ xl = (mp_limb_t) (value >> 32 - big_normalization_steps);
+ udiv_qrnnd (x1hi, r, xh, xl, big_base_norm);
+
+ xl = ((mp_limb_t) value) << big_normalization_steps;
+ udiv_qrnnd (x1lo, x, r, xl, big_base_norm);
+ t[2] = x >> big_normalization_steps;
+
+ if (big_normalization_steps == 0)
+ xh = x1hi;
+ else
+ xh = ((x1hi << big_normalization_steps)
+ | (x1lo >> 32 - big_normalization_steps));
+ xl = x1lo << big_normalization_steps;
+ udiv_qrnnd (t[0], x, xh, xl, big_base_norm);
+ t[1] = x >> big_normalization_steps;
+#else
+ udiv_qrnnd (x1hi, r, 0, (mp_limb_t) (value >> 32),
+ brec->big.base);
+ udiv_qrnnd (x1lo, t[2], r, (mp_limb_t) value, brec->big.base);
+ udiv_qrnnd (t[0], t[1], x1hi, x1lo, brec->big.base);
+#endif
+ n = 3;
+ }
+ else
+ {
+#if (UDIV_TIME > 2 * UMUL_TIME)
+ mp_limb_t x;
+
+ value <<= brec->big.normalization_steps;
+ udiv_qrnnd_preinv (t[0], x, (mp_limb_t) (value >> 32),
+ (mp_limb_t) value, big_base_norm,
+ brec->big.base_ninv);
+ t[1] = x >> brec->big.normalization_steps;
+#elif UDIV_NEEDS_NORMALIZATION
+ mp_limb_t x;
+
+ value <<= big_normalization_steps;
+ udiv_qrnnd (t[0], x, (mp_limb_t) (value >> 32),
+ (mp_limb_t) value, big_base_norm);
+ t[1] = x >> big_normalization_steps;
+#else
+ udiv_qrnnd (t[0], t[1], (mp_limb_t) (value >> 32),
+ (mp_limb_t) value, brec->big.base);
+#endif
+ n = 2;
+ }
+ }
+ else
+ {
+ t[0] = value;
+ n = 1;
+ }
+
+ /* Convert the 1-3 words in t[], word by word, to ASCII. */
+ do
+ {
+ mp_limb_t ti = t[--n];
+ int ndig_for_this_limb = 0;
+
+#if UDIV_TIME > 2 * UMUL_TIME
+ mp_limb_t base_multiplier = brec->base_multiplier;
+ if (brec->flag)
+ while (ti != 0)
+ {
+ mp_limb_t quo, rem, x, dummy;
+
+ umul_ppmm (x, dummy, ti, base_multiplier);
+ quo = (x + ((ti - x) >> 1)) >> (brec->post_shift - 1);
+ rem = ti - quo * base;
+ *--bp = digits[rem];
+ ti = quo;
+ ++ndig_for_this_limb;
+ }
+ else
+ while (ti != 0)
+ {
+ mp_limb_t quo, rem, x, dummy;
+
+ umul_ppmm (x, dummy, ti, base_multiplier);
+ quo = x >> brec->post_shift;
+ rem = ti - quo * base;
+ *--bp = digits[rem];
+ ti = quo;
+ ++ndig_for_this_limb;
+ }
+#else
+ while (ti != 0)
+ {
+ mp_limb_t quo, rem;
+
+ quo = ti / base;
+ rem = ti % base;
+ *--bp = digits[rem];
+ ti = quo;
+ ++ndig_for_this_limb;
+ }
+#endif
+ /* If this wasn't the most significant word, pad with zeros. */
+ if (n != 0)
+ while (ndig_for_this_limb < brec->big.ndigits)
+ {
+ *--bp = '0';
+ ++ndig_for_this_limb;
+ }
+ }
+ while (n != 0);
+#endif
+ }
+ break;
+ }
+
+ return bp;
+}
diff --git a/stdio-common/_itowa.h b/stdio-common/_itowa.h
new file mode 100644
index 0000000000..e219f298ee
--- /dev/null
+++ b/stdio-common/_itowa.h
@@ -0,0 +1,63 @@
+/* Internal function for converting integers to ASCII.
+ Copyright (C) 1994, 95, 96, 97, 98, 99 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#ifndef _ITOWA_H
+#define _ITOWA_H 1
+#include <features.h>
+#include <wchar.h>
+
+/* Convert VALUE into ASCII in base BASE (2..36).
+ Write backwards starting the character just before BUFLIM.
+ Return the address of the first (left-to-right) character in the number.
+ Use upper case letters iff UPPER_CASE is nonzero. */
+
+extern wchar_t *_itowa __P ((unsigned long long int value, wchar_t *buflim,
+ unsigned int base, int upper_case));
+
+static inline wchar_t *
+__attribute__ ((unused))
+_itowa_word (unsigned long value, wchar_t *buflim,
+ unsigned int base, int upper_case)
+{
+ extern const wchar_t _itowa_upper_digits[], _itowa_lower_digits[];
+ const wchar_t *digits = (upper_case
+ ? _itowa_upper_digits : _itowa_lower_digits);
+ wchar_t *bp = buflim;
+
+ switch (base)
+ {
+#define SPECIAL(Base) \
+ case Base: \
+ do \
+ *--bp = digits[value % Base]; \
+ while ((value /= Base) != 0); \
+ break
+
+ SPECIAL (10);
+ SPECIAL (16);
+ SPECIAL (8);
+ default:
+ do
+ *--bp = digits[value % base];
+ while ((value /= base) != 0);
+ }
+ return bp;
+}
+
+#endif /* itowa.h */
diff --git a/stdio-common/itoa-digits.c b/stdio-common/itoa-digits.c
index b475bbca42..34699dbcc8 100644
--- a/stdio-common/itoa-digits.c
+++ b/stdio-common/itoa-digits.c
@@ -1,5 +1,5 @@
/* Digits.
- Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
+ Copyright (C) 1994, 1995, 1996, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
@@ -18,9 +18,8 @@
Boston, MA 02111-1307, USA. */
/* Lower-case digits. */
-const char _itoa_lower_digits[]
+const char _itoa_lower_digits[36]
= "0123456789abcdefghijklmnopqrstuvwxyz";
/* Upper-case digits. */
-const char _itoa_upper_digits[]
+const char _itoa_upper_digits[36]
= "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
-
diff --git a/stdio-common/itowa-digits.c b/stdio-common/itowa-digits.c
new file mode 100644
index 0000000000..60a85789e3
--- /dev/null
+++ b/stdio-common/itowa-digits.c
@@ -0,0 +1,27 @@
+/* Digits.
+ Copyright (C) 1994, 1995, 1996, 1999 Free Software Foundation, Inc.
+ This file is part of the GNU C Library.
+
+ The GNU C Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The GNU C Library is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the GNU C Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA. */
+
+#include <wchar.h>
+
+/* Lower-case digits. */
+const wchar_t _itowa_lower_digits[36]
+ = L"0123456789abcdefghijklmnopqrstuvwxyz";
+/* Upper-case digits. */
+const wchar_t _itowa_upper_digits[36]
+ = L"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
diff --git a/stdio-common/printf-parse.h b/stdio-common/printf-parse.h
index a915f03f18..d62f3a835e 100644
--- a/stdio-common/printf-parse.h
+++ b/stdio-common/printf-parse.h
@@ -35,7 +35,7 @@ struct printf_spec
/* Pointers into the format string for the end of this format
spec and the next (or to the end of the string if no more). */
- const char *end_of_fmt, *next_fmt;
+ const UCHAR_T *end_of_fmt, *next_fmt;
/* Position of arguments for precision and width, or -1 if `info' has
the constant value. */
@@ -90,21 +90,29 @@ read_int (const UCHAR_T * *pstr)
/* Find the next spec in FORMAT, or the end of the string. Returns
a pointer into FORMAT, to a '%' or a '\0'. */
-static inline const char *
-find_spec (const char *format, mbstate_t *ps)
+static inline const UCHAR_T *
+#ifdef COMPILE_WPRINTF
+find_spec (const UCHAR_T *format)
+#else
+find_spec (const UCHAR_T *format, mbstate_t *ps)
+#endif
{
- while (*format != '\0' && *format != '%')
+#ifdef COMPILE_WPRINTF
+ return (const UCHAR_T *) __wcschrnul ((const CHAR_T *) format, L'%');
+#else
+ while (*format != L_('\0') && *format != L_('%'))
{
int len;
/* Remove any hints of a wrong encoding. */
ps->count = 0;
- if (isascii (*format) || (len = mbrlen (format, MB_CUR_MAX, ps)) <= 0)
- ++format;
- else
+ if (! ISASCII (*format) && (len = MBRLEN (format, MB_CUR_MAX, ps)) > 0)
format += len;
+ else
+ ++format;
}
return format;
+#endif
}
@@ -119,8 +127,13 @@ extern printf_function **__printf_function_table;
the number of args consumed by this spec; *MAX_REF_ARG is updated so it
remains the highest argument index used. */
static inline size_t
+#ifdef COMPILE_WPRINTF
+parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
+ size_t *max_ref_arg)
+#else
parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
size_t *max_ref_arg, mbstate_t *ps)
+#endif
{
unsigned int n;
size_t nargs = 0;
@@ -342,12 +355,12 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
switch (spec->info.spec)
{
- case L'i':
- case L'd':
- case L'u':
- case L'o':
- case L'X':
- case L'x':
+ case L_('i'):
+ case L_('d'):
+ case L_('u'):
+ case L_('o'):
+ case L_('X'):
+ case L_('x'):
#if LONG_MAX != LONG_LONG_MAX
if (spec->info.is_long_double)
spec->data_arg_type = PA_INT|PA_FLAG_LONG_LONG;
@@ -362,38 +375,38 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
else
spec->data_arg_type = PA_INT;
break;
- case L'e':
- case L'E':
- case L'f':
- case L'g':
- case L'G':
- case L'a':
- case L'A':
+ case L_('e'):
+ case L_('E'):
+ case L_('f'):
+ case L_('g'):
+ case L_('G'):
+ case L_('a'):
+ case L_('A'):
if (spec->info.is_long_double)
spec->data_arg_type = PA_DOUBLE|PA_FLAG_LONG_DOUBLE;
else
spec->data_arg_type = PA_DOUBLE;
break;
- case L'c':
+ case L_('c'):
spec->data_arg_type = PA_CHAR;
break;
- case L'C':
+ case L_('C'):
spec->data_arg_type = PA_WCHAR;
break;
- case L's':
+ case L_('s'):
spec->data_arg_type = PA_STRING;
break;
- case L'S':
+ case L_('S'):
spec->data_arg_type = PA_WSTRING;
break;
- case L'p':
+ case L_('p'):
spec->data_arg_type = PA_POINTER;
break;
- case L'n':
+ case L_('n'):
spec->data_arg_type = PA_INT|PA_FLAG_PTR;
break;
- case L'm':
+ case L_('m'):
default:
/* An unknown spec will consume no args. */
spec->ndata_args = 0;
@@ -416,7 +429,11 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
{
/* Find the next format spec. */
spec->end_of_fmt = format;
+#ifdef COMPILE_WPRINTF