reversed back to 2.2.5

enabled printf_fp


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@11655 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2005-03-10 19:41:37 +00:00
parent 55a93a6ff0
commit 1e5a39d91d
7 changed files with 3147 additions and 1345 deletions
@@ -1,9 +1,11 @@
SubDir OBOS_TOP src kernel libroot posix glibc stdio-common ; SubDir OBOS_TOP src kernel libroot posix glibc stdio-common ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include arch $(OBOS_ARCH) ; SubDirCcFlags -D_GNU_SOURCE -DUSE_IN_LIBIO ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc stdio-common ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc libio ; SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc libio ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include arch $(OBOS_ARCH) ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc include ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc stdio-common ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc locale ; SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc locale ;
SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc ; SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc ;
@@ -11,7 +13,7 @@ SubDirHdrs $(OBOS_TOP) src kernel libroot posix glibc ;
KernelMergeObject posix_gnu_stdio.o : #_common.o : KernelMergeObject posix_gnu_stdio.o : #_common.o :
<$(SOURCE_GRIST)>_itoa.c <$(SOURCE_GRIST)>_itoa.c
# <$(SOURCE_GRIST)>_itowa.c <$(SOURCE_GRIST)>_itowa.c
<$(SOURCE_GRIST)>asprintf.c <$(SOURCE_GRIST)>asprintf.c
<$(SOURCE_GRIST)>dprintf.c <$(SOURCE_GRIST)>dprintf.c
<$(SOURCE_GRIST)>fprintf.c <$(SOURCE_GRIST)>fprintf.c
@@ -20,11 +22,12 @@ KernelMergeObject posix_gnu_stdio.o : #_common.o :
<$(SOURCE_GRIST)>getw.c <$(SOURCE_GRIST)>getw.c
<$(SOURCE_GRIST)>itoa-digits.c <$(SOURCE_GRIST)>itoa-digits.c
<$(SOURCE_GRIST)>itoa-udigits.c <$(SOURCE_GRIST)>itoa-udigits.c
# <$(SOURCE_GRIST)>itowa-digits.c <$(SOURCE_GRIST)>itowa-digits.c
<$(SOURCE_GRIST)>perror.c <$(SOURCE_GRIST)>perror.c
<$(SOURCE_GRIST)>printf-parse.c <$(SOURCE_GRIST)>printf-prs.c
<$(SOURCE_GRIST)>printf.c <$(SOURCE_GRIST)>printf.c
<$(SOURCE_GRIST)>printf_fp.c <$(SOURCE_GRIST)>printf_fp.c
<$(SOURCE_GRIST)>printf_fphex.c
# <$(SOURCE_GRIST)>printf_size.c # <$(SOURCE_GRIST)>printf_size.c
<$(SOURCE_GRIST)>putw.c <$(SOURCE_GRIST)>putw.c
<$(SOURCE_GRIST)>reg-printf.c <$(SOURCE_GRIST)>reg-printf.c
@@ -1,5 +1,5 @@
/* Internal header for parsing printf format strings. /* Internal header for parsing printf format strings.
Copyright (C) 1995-1999, 2000, 2002 Free Software Foundation, Inc. Copyright (C) 1995-1999, 2000 Free Software Foundation, Inc.
This file is part of th GNU C Library. This file is part of th GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@@ -87,27 +87,28 @@ read_int (const UCHAR_T * *pstr)
} }
/* Find the next spec in FORMAT, or the end of the string. Returns /* Find the next spec in FORMAT, or the end of the string. Returns
a pointer into FORMAT, to a '%' or a '\0'. */ a pointer into FORMAT, to a '%' or a '\0'. */
static inline const UCHAR_T * static inline const UCHAR_T *
//#ifdef COMPILE_WPRINTF #ifdef COMPILE_WPRINTF
find_spec (const UCHAR_T *format) find_spec (const UCHAR_T *format)
//#else #else
//find_spec (const UCHAR_T *format, mbstate_t *ps) find_spec (const UCHAR_T *format, mbstate_t *ps)
//#endif #endif
{ {
#ifdef COMPILE_WPRINTF #ifdef COMPILE_WPRINTF
return (const UCHAR_T *) __wcschrnul ((const CHAR_T *) format, L'%'); return (const UCHAR_T *) __wcschrnul ((const CHAR_T *) format, L'%');
#else #else
while (*format != L_('\0') && *format != L_('%')) while (*format != L_('\0') && *format != L_('%'))
{ {
// int len; int len;
/* Remove any hints of a wrong encoding. */ /* Remove any hints of a wrong encoding. */
// ps->__count = 0; ps->__count = 0;
// if (! isascii (*format) && (len = __mbrlen (format, MB_CUR_MAX, ps)) > 0) if (! isascii (*format) && (len = __mbrlen (format, MB_CUR_MAX, ps)) > 0)
// format += len; format += len;
// else else
++format; ++format;
} }
return format; return format;
@@ -116,8 +117,8 @@ find_spec (const UCHAR_T *format)
/* These are defined in reg-printf.c. */ /* These are defined in reg-printf.c. */
extern printf_arginfo_function **__printf_arginfo_table attribute_hidden; extern printf_arginfo_function *__printf_arginfo_table[];
extern printf_function **__printf_function_table attribute_hidden; extern printf_function **__printf_function_table;
/* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC /* FORMAT must point to a '%' at the beginning of a spec. Fills in *SPEC
@@ -126,13 +127,13 @@ extern printf_function **__printf_function_table attribute_hidden;
the number of args consumed by this spec; *MAX_REF_ARG is updated so it the number of args consumed by this spec; *MAX_REF_ARG is updated so it
remains the highest argument index used. */ remains the highest argument index used. */
static inline size_t static inline size_t
//#ifdef COMPILE_WPRINTF #ifdef COMPILE_WPRINTF
parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
size_t *max_ref_arg) size_t *max_ref_arg)
//#else #else
//parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec, parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
// size_t *max_ref_arg, mbstate_t *ps) size_t *max_ref_arg, mbstate_t *ps)
//#endif #endif
{ {
unsigned int n; unsigned int n;
size_t nargs = 0; size_t nargs = 0;
@@ -353,7 +354,7 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
/* Get the format specification. */ /* Get the format specification. */
spec->info.spec = (wchar_t) *format++; spec->info.spec = (wchar_t) *format++;
if (__builtin_expect (__printf_function_table != NULL, 0) if (__printf_function_table != NULL
&& spec->info.spec <= UCHAR_MAX && spec->info.spec <= UCHAR_MAX
&& __printf_arginfo_table[spec->info.spec] != NULL) && __printf_arginfo_table[spec->info.spec] != NULL)
/* We don't try to get the types for all arguments if the format /* We don't try to get the types for all arguments if the format
@@ -442,11 +443,11 @@ parse_one_spec (const UCHAR_T *format, size_t posn, struct printf_spec *spec,
{ {
/* Find the next format spec. */ /* Find the next format spec. */
spec->end_of_fmt = format; spec->end_of_fmt = format;
//#ifdef COMPILE_WPRINTF #ifdef COMPILE_WPRINTF
spec->next_fmt = find_spec (format); spec->next_fmt = find_spec (format);
//#else #else
// spec->next_fmt = find_spec (format, ps); spec->next_fmt = find_spec (format, ps);
//#endif #endif
} }
return nargs; return nargs;
@@ -0,0 +1,120 @@
/* Copyright (C) 1991,1992,1995,1996,1999,2000 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 Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <stdio.h>
#include <printf.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <sys/param.h>
#ifndef COMPILE_WPRINTF
# define CHAR_T char
# define UCHAR_T unsigned char
# define INT_T int
# define L_(Str) Str
# define ISDIGIT(Ch) isdigit (Ch)
# define ISASCII(Ch) isascii (Ch)
# define MBRLEN(Cp, L, St) __mbrlen (Cp, L, St)
# ifdef USE_IN_LIBIO
# define PUT(F, S, N) _IO_sputn (F, S, N)
# define PAD(Padchar) \
if (width > 0) \
done += _IO_padn (s, Padchar, width)
# else
# define PUTC(C, F) putc (C, F)
ssize_t __printf_pad __P ((FILE *, char pad, size_t n));
# define PAD(Padchar) \
if (width > 0) \
{ if (__printf_pad (s, Padchar, width) == -1) \
return -1; else done += width; }
# endif
#else
# define vfprintf vfwprintf
# define CHAR_T wchar_t
# define UCHAR_T uwchar_t
# define INT_T wint_t
# define L_(Str) L##Str
# define ISDIGIT(Ch) iswdigit (Ch)
# ifdef USE_IN_LIBIO
# define PUT(F, S, N) _IO_sputn (F, S, N)
# define PAD(Padchar) \
if (width > 0) \
done += _IO_wpadn (s, Padchar, width)
# else
# define PUTC(C, F) wputc (C, F)
ssize_t __wprintf_pad __P ((FILE *, wchar_t pad, size_t n));
# define PAD(Padchar) \
if (width > 0) \
{ if (__wprintf_pad (s, Padchar, width) == -1) \
return -1; else done += width; }
# endif
#endif
#include "printf-parse.h"
size_t
parse_printf_format (fmt, n, argtypes)
const char *fmt;
size_t n;
int *argtypes;
{
size_t nargs; /* Number of arguments. */
size_t max_ref_arg; /* Highest index used in a positional arg. */
struct printf_spec spec;
mbstate_t mbstate;
nargs = 0;
max_ref_arg = 0;
/* Search for format specifications. */
for (fmt = find_spec (fmt, &mbstate); *fmt != '\0'; fmt = spec.next_fmt)
{
/* Parse this spec. */
nargs += parse_one_spec (fmt, nargs, &spec, &max_ref_arg, &mbstate);
/* If the width is determined by an argument this is an int. */
if (spec.width_arg != -1 && (size_t) spec.width_arg < n)
argtypes[spec.width_arg] = PA_INT;
/* If the precision is determined by an argument this is an int. */
if (spec.prec_arg != -1 && (size_t) spec.prec_arg < n)
argtypes[spec.prec_arg] = PA_INT;
if ((size_t) spec.data_arg < n)
switch (spec.ndata_args)
{
case 0: /* No arguments. */
break;
case 1: /* One argument; we already have the type. */
argtypes[spec.data_arg] = spec.data_arg_type;
break;
default:
/* We have more than one argument for this format spec. We must
call the arginfo function again to determine all the types. */
(void) (*__printf_arginfo_table[spec.info.spec])
(&spec.info, n - spec.data_arg, &argtypes[spec.data_arg]);
break;
}
}
return MAX (nargs, max_ref_arg);
}
@@ -1,5 +1,5 @@
/* Floating point output for `printf'. /* Floating point output for `printf'.
Copyright (C) 1995-1999, 2000, 2001, 2002 Free Software Foundation, Inc. Copyright (C) 1995-1999, 2000, 2001 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
Written by Ulrich Drepper <[email protected]>, 1995. Written by Ulrich Drepper <[email protected]>, 1995.
@@ -21,10 +21,11 @@
/* The gmp headers need some configuration frobs. */ /* The gmp headers need some configuration frobs. */
#define HAVE_ALLOCA 1 #define HAVE_ALLOCA 1
#include <stdio_private.h> #ifdef USE_IN_LIBIO
// ToDo: implement this function for real # include <libioP.h>
#include <printf.h> #else
#if 0 # include <stdio.h>
#endif
#include <alloca.h> #include <alloca.h>
#include <ctype.h> #include <ctype.h>
#include <float.h> #include <float.h>
@@ -36,6 +37,7 @@
#include <locale/localeinfo.h> #include <locale/localeinfo.h>
#include <limits.h> #include <limits.h>
#include <math.h> #include <math.h>
#include <printf.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h> #include <stdlib.h>
@@ -50,7 +52,7 @@
the GNU I/O library. */ the GNU I/O library. */
#ifdef USE_IN_LIBIO #ifdef USE_IN_LIBIO
# define PUT(f, s, n) _IO_sputn (f, s, n) # define PUT(f, s, n) _IO_sputn (f, s, n)
# define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : INTUSE(_IO_padn) (f, c, n)) # define PAD(f, c, n) (wide ? _IO_wpadn (f, c, n) : _IO_padn (f, c, n))
/* We use this file GNU C library and GNU I/O library. So make /* We use this file GNU C library and GNU I/O library. So make
names equal. */ names equal. */
# undef putc # undef putc
@@ -118,8 +120,7 @@ ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
#define MPN_GE(u,v) \ #define MPN_GE(u,v) \
(u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0)) (u##size > v##size || (u##size == v##size && __mpn_cmp (u, v, u##size) >= 0))
extern int __isinfl_internal (long double) attribute_hidden; extern int __isinfl (long double), __isnanl (long double);
extern int __isnanl_internal (long double) attribute_hidden;
extern mp_size_t __mpn_extract_double (mp_ptr res_ptr, mp_size_t size, extern mp_size_t __mpn_extract_double (mp_ptr res_ptr, mp_size_t size,
int *expt, int *is_neg, int *expt, int *is_neg,
@@ -210,7 +211,8 @@ __printf_fp (FILE *fp,
else if (scalesize == 0) else if (scalesize == 0)
{ {
hi = frac[fracsize - 1]; hi = frac[fracsize - 1];
frac[fracsize - 1] = __mpn_mul_1 (frac, frac, fracsize - 1, 10); cy = __mpn_mul_1 (frac, frac, fracsize - 1, 10);
frac[fracsize - 1] = cy;
} }
else else
{ {
@@ -234,11 +236,9 @@ __printf_fp (FILE *fp,
} }
} }
{ cy = __mpn_mul_1 (frac, frac, fracsize, 10);
mp_limb_t _cy = __mpn_mul_1 (frac, frac, fracsize, 10); if (cy != 0)
if (_cy != 0) frac[fracsize++] = cy;
frac[fracsize++] = _cy;
}
} }
return L'0' + hi; return L'0' + hi;
@@ -304,7 +304,7 @@ __printf_fp (FILE *fp,
multibyte representation for the thousands separator, multibyte representation for the thousands separator,
we must ensure the wide character thousands separator we must ensure the wide character thousands separator
is available, even if it is fake. */ is available, even if it is fake. */
thousands_sepwc = (wchar_t)0xfffffffe; thousands_sepwc = 0xfffffffe;
} }
} }
else else
@@ -363,7 +363,6 @@ __printf_fp (FILE *fp,
/* Check for special values: not a number or infinity. */ /* Check for special values: not a number or infinity. */
if (__isnan (fpnum.dbl)) if (__isnan (fpnum.dbl))
{ {
is_neg = 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "NAN"; special = "NAN";
@@ -374,10 +373,10 @@ __printf_fp (FILE *fp,
special = "nan"; special = "nan";
wspecial = L"nan"; wspecial = L"nan";
} }
is_neg = 0;
} }
else if (__isinf (fpnum.dbl)) else if (__isinf (fpnum.dbl))
{ {
is_neg = fpnum.dbl < 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "INF"; special = "INF";
@@ -388,6 +387,7 @@ __printf_fp (FILE *fp,
special = "inf"; special = "inf";
wspecial = L"inf"; wspecial = L"inf";
} }
is_neg = fpnum.dbl < 0;
} }
else else
{ {
@@ -493,9 +493,6 @@ __printf_fp (FILE *fp,
&__tens[powers->arrayoff], &__tens[powers->arrayoff],
tmpsize * sizeof (mp_limb_t)); tmpsize * sizeof (mp_limb_t));
MPN_ZERO (tmp, _FPIO_CONST_SHIFT); MPN_ZERO (tmp, _FPIO_CONST_SHIFT);
/* Adjust exponent, as scaleexpo will be this much
bigger too. */
exponent += _FPIO_CONST_SHIFT * BITS_PER_MP_LIMB;
} }
else else
#endif #endif
@@ -809,12 +806,10 @@ __printf_fp (FILE *fp,
dig_max = INT_MAX; /* Unlimited. */ dig_max = INT_MAX; /* Unlimited. */
significant = 1; /* Does not matter here. */ significant = 1; /* Does not matter here. */
} }
else if (_tolower (info->spec) == 'f') else if (info->spec == 'f')
{ {
type = 'f'; type = 'f';
fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec; fracdig_min = fracdig_max = info->prec < 0 ? 6 : info->prec;
dig_max = INT_MAX; /* Unlimited. */
significant = 1; /* Does not matter here. */
if (expsign == 0) if (expsign == 0)
{ {
intdig_max = exponent + 1; intdig_max = exponent + 1;
@@ -826,6 +821,8 @@ __printf_fp (FILE *fp,
intdig_max = 1; intdig_max = 1;
chars_needed = 1 + 1 + fracdig_max; chars_needed = 1 + 1 + fracdig_max;
} }
dig_max = INT_MAX; /* Unlimited. */
significant = 1; /* Does not matter here. */
} }
else else
{ {
@@ -869,7 +866,7 @@ __printf_fp (FILE *fp,
it is possible that we need two more characters in front of all the it is possible that we need two more characters in front of all the
other output. If the amount of memory we have to allocate is too other output. If the amount of memory we have to allocate is too
large use `malloc' instead of `alloca'. */ large use `malloc' instead of `alloca'. */
buffer_malloced = ! __libc_use_alloca (chars_needed * 2 * sizeof (wchar_t)); buffer_malloced = chars_needed > 5000;
if (buffer_malloced) if (buffer_malloced)
{ {
wbuffer = (wchar_t *) malloc ((2 + chars_needed) * sizeof (wchar_t)); wbuffer = (wchar_t *) malloc ((2 + chars_needed) * sizeof (wchar_t));
@@ -911,7 +908,7 @@ __printf_fp (FILE *fp,
{ {
++fracdig_no; ++fracdig_no;
*wcp = hack_digit (); *wcp = hack_digit ();
if (*wcp++ != L'0') if (*wcp != L'0')
significant = 1; significant = 1;
else if (significant == 0) else if (significant == 0)
{ {
@@ -919,6 +916,7 @@ __printf_fp (FILE *fp,
if (fracdig_min > 0) if (fracdig_min > 0)
++fracdig_min; ++fracdig_min;
} }
++wcp;
} }
/* Do rounding. */ /* Do rounding. */
@@ -1144,7 +1142,6 @@ __printf_fp (FILE *fp,
} }
return done; return done;
} }
libc_hidden_def (__printf_fp)
/* Return the number of extra grouping characters that will be inserted /* Return the number of extra grouping characters that will be inserted
into a number with INTDIG_MAX integer digits. */ into a number with INTDIG_MAX integer digits. */
@@ -1230,10 +1227,3 @@ group_number (wchar_t *buf, wchar_t *bufend, unsigned int intdig_no,
return bufend + ngroups; return bufend + ngroups;
} }
#endif
int
__printf_fp(FILE *fp, const struct printf_info *info, const void * const *args)
{
return fputs("NaN-FIXME", fp);
}
@@ -0,0 +1,493 @@
/* Print floating point number in hexadecimal notation according to ISO C99.
Copyright (C) 1997, 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Contributed by Ulrich Drepper <[email protected]>, 1997.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 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
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
#include <ctype.h>
#include <ieee754.h>
#include <math.h>
#include <printf.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <wchar.h>
#include "_itoa.h"
#include "_itowa.h"
#include <locale/localeinfo.h>
/* #define NDEBUG 1*/ /* Undefine this for debugging assertions. */
#include <assert.h>
/* This defines make it possible to use the same code for GNU C library and
the GNU I/O library. */
#ifdef USE_IN_LIBIO
# include <libioP.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))
/* We use this file GNU C library and GNU I/O library. So make
names equal. */
# undef putc
# define putc(c, f) (wide \
? (int)_IO_putwc_unlocked (c, f) : _IO_putc_unlocked (c, f))
# define size_t _IO_size_t
# define FILE _IO_FILE
#else /* ! USE_IN_LIBIO */
# define PUT(f, s, n) fwrite (s, 1, n, f)
# define PAD(f, c, n) __printf_pad (f, c, n)
ssize_t __printf_pad __P ((FILE *, char pad, int n)); /* In vfprintf.c. */
#endif /* USE_IN_LIBIO */
/* Macros for doing the actual output. */
#define outchar(ch) \
do \
{ \
register const int outc = (ch); \
if (putc (outc, fp) == EOF) \
return -1; \
++done; \
} while (0)
#define PRINT(ptr, wptr, len) \
do \
{ \
register size_t outlen = (len); \
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) \
return -1; \
done += len; \
} \
while (0)
#ifndef MIN
# define MIN(a,b) ((a)<(b)?(a):(b))
#endif
int
__printf_fphex (FILE *fp,
const struct printf_info *info,
const void *const *args)
{
/* The floating-point value to output. */
union
{
union ieee754_double dbl;
union ieee854_long_double ldbl;
}
fpnum;
/* Locale-dependent representation of decimal point. */
const char *decimal;
wchar_t decimalwc;
/* "NaN" or "Inf" for the special cases. */
const char *special = NULL;
const wchar_t *wspecial = NULL;
/* Buffer for the generated number string for the mantissa. The
maximal size for the mantissa is 128 bits. */
char numbuf[32];
char *numstr;
char *numend;
wchar_t wnumbuf[32];
wchar_t *wnumstr;
wchar_t *wnumend;
int negative;
/* The maximal exponent of two in decimal notation has 5 digits. */
char expbuf[5];
char *expstr;
wchar_t wexpbuf[5];
wchar_t *wexpstr;
int expnegative;
int exponent;
/* Non-zero is mantissa is zero. */
int zero_mantissa;
/* The leading digit before the decimal point. */
char leading;
/* Precision. */
int precision = info->prec;
/* Width. */
int width = info->width;
/* Number of characters written. */
int done = 0;
/* Nonzero if this is output on a wide character stream. */
int wide = info->wide;
/* Figure out the decimal point character. */
if (info->extra == 0)
{
decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
decimalwc = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
}
else
{
decimal = _NL_CURRENT (LC_MONETARY, MON_DECIMAL_POINT);
decimalwc = _NL_CURRENT_WORD (LC_MONETARY,
_NL_MONETARY_DECIMAL_POINT_WC);
}
/* The decimal point character must never be zero. */
assert (*decimal != '\0' && decimalwc != L'\0');
/* Fetch the argument value. */
#ifndef __NO_LONG_DOUBLE_MATH
if (info->is_long_double && sizeof (long double) > sizeof (double))
{
fpnum.ldbl.d = *(const long double *) args[0];
/* Check for special values: not a number or infinity. */
if (__isnanl (fpnum.ldbl.d))
{
if (isupper (info->spec))
{
special = "NAN";
wspecial = L"NAN";
}
else
{
special = "nan";
wspecial = L"nan";
}
negative = 0;
}
else
{
if (__isinfl (fpnum.ldbl.d))
{
if (isupper (info->spec))
{
special = "INF";
wspecial = L"INF";
}
else
{
special = "inf";
wspecial = L"inf";
}
}
negative = signbit (fpnum.ldbl.d);
}
}
else
#endif /* no long double */
{
fpnum.dbl.d = *(const double *) args[0];
/* Check for special values: not a number or infinity. */
if (__isnan (fpnum.dbl.d))
{
if (isupper (info->spec))
{
special = "NAN";
wspecial = L"NAN";
}
else
{
special = "nan";
wspecial = L"nan";
}
negative = 0;
}
else
{
if (__isinf (fpnum.dbl.d))
{
if (isupper (info->spec))
{
special = "INF";
wspecial = L"INF";
}
else
{
special = "inf";
wspecial = L"inf";
}
}
negative = signbit (fpnum.dbl.d);
}
}
if (special)
{
int width = info->width;
if (negative || info->showsign || info->space)
--width;
width -= 3;
if (!info->left && width > 0)
PADN (' ', width);
if (negative)
outchar ('-');
else if (info->showsign)
outchar ('+');
else if (info->space)
outchar (' ');
PRINT (special, wspecial, 3);
if (info->left && width > 0)
PADN (' ', width);
return done;
}
if (info->is_long_double == 0 || sizeof (double) == sizeof (long double))
{
/* We have 52 bits of mantissa plus one implicit digit. Since
52 bits are representable without rest using hexadecimal
digits we use only the implicit digits for the number before
the decimal point. */
unsigned long long int num;
num = (((unsigned long long int) fpnum.dbl.ieee.mantissa0) << 32
| fpnum.dbl.ieee.mantissa1);
zero_mantissa = num == 0;
if (sizeof (unsigned long int) > 6)
{
wnumstr = _itowa_word (num, wnumbuf + (sizeof wnumbuf) / sizeof (wchar_t), 16,
info->spec == 'A');
numstr = _itoa_word (num, numbuf + sizeof numbuf, 16,
info->spec == 'A');
}
else
{
wnumstr = _itowa (num, wnumbuf + sizeof wnumbuf / sizeof (wchar_t), 16,
info->spec == 'A');
numstr = _itoa (num, numbuf + sizeof numbuf, 16,
info->spec == 'A');
}
/* Fill with zeroes. */
while (wnumstr > wnumbuf + (sizeof wnumbuf - 52) / sizeof (wchar_t))
{
*--wnumstr = L'0';
*--numstr = '0';
}
leading = fpnum.dbl.ieee.exponent == 0 ? '0' : '1';
exponent = fpnum.dbl.ieee.exponent;
if (exponent == 0)
{
if (zero_mantissa)
expnegative = 0;
else
{
/* This is a denormalized number. */
expnegative = 1;
exponent = IEEE754_DOUBLE_BIAS - 1;
}
}
else if (exponent >= IEEE754_DOUBLE_BIAS)
{
expnegative = 0;
exponent -= IEEE754_DOUBLE_BIAS;
}
else
{
expnegative = 1;
exponent = -(exponent - IEEE754_DOUBLE_BIAS);
}
}
#ifdef PRINT_FPHEX_LONG_DOUBLE
else
PRINT_FPHEX_LONG_DOUBLE;
#endif
/* Look for trailing zeroes. */
if (! zero_mantissa)
{
wnumend = wnumbuf + sizeof wnumbuf;
numend = numbuf + sizeof numbuf;
while (wnumend[-1] == L'0')
{
--wnumend;
--numend;
}
if (precision == -1)
precision = numend - numstr;
else if (precision < numend - numstr
&& (numstr[precision] > '8'
|| (('A' < '0' || 'a' < '0')
&& numstr[precision] < '0')
|| (numstr[precision] == '8'
&& (precision + 1 < numend - numstr
/* Round to even. */
|| (precision > 0
&& ((numstr[precision - 1] & 1)
^ (isdigit (numstr[precision - 1]) == 0)))
|| (precision == 0
&& ((leading & 1)
^ (isdigit (leading) == 0)))))))
{
/* Round up. */
int cnt = precision;
while (--cnt >= 0)
{
char ch = numstr[cnt];
/* We assume that the digits and the letters are ordered
like in ASCII. This is true for the rest of GNU, too. */
if (ch == '9')
{
wnumstr[cnt] = (wchar_t) info->spec;
numstr[cnt] = info->spec; /* This is tricky,
think about it! */
break;
}
else if (tolower (ch) < 'f')
{
++numstr[cnt];
++wnumstr[cnt];
break;
}
else
{
numstr[cnt] = '0';
wnumstr[cnt] = L'0';
}
}
if (cnt < 0)
{
/* The mantissa so far was fff...f Now increment the
leading digit. Here it is again possible that we
get an overflow. */
if (leading == '9')
leading = info->spec;
else if (tolower (leading) < 'f')
++leading;
else
{
leading = 1;
if (expnegative)
{
exponent += 4;
if (exponent >= 0)
expnegative = 0;
}
else
exponent += 4;
}
}
}
}
else
{
if (precision == -1)
precision = 0;
numend = numstr;
wnumend = wnumstr;
}
/* Now we can compute the exponent string. */
expstr = _itoa_word (exponent, expbuf + sizeof expbuf, 10, 0);
wexpstr = _itowa_word (exponent,
wexpbuf + sizeof wexpbuf / sizeof (wchar_t), 10, 0);
/* Now we have all information to compute the size. */
width -= ((negative || info->showsign || info->space)
/* Sign. */
+ 2 + 1 + 0 + precision + 1 + 1
/* 0x h . hhh P ExpoSign. */
+ ((expbuf + sizeof expbuf) - expstr));
/* Exponent. */
/* Count the decimal point. */
if (precision > 0 || info->alt)
width -= wide ? 1 : strlen (decimal);
/* A special case when the mantissa or the precision is zero and the `#'
is not given. In this case we must not print the decimal point. */
if (precision == 0 && !info->alt)
++width; /* This nihilates the +1 for the decimal-point
character in the following equation. */
if (!info->left && width > 0)
PADN (' ', width);
if (negative)
outchar ('-');
else if (info->showsign)
outchar ('+');
else if (info->space)
outchar (' ');
outchar ('0');
if ('X' - 'A' == 'x' - 'a')
outchar (info->spec + ('x' - 'a'));
else
outchar (info->spec == 'A' ? 'X' : 'x');
outchar (leading);
if (precision > 0 || info->alt)
{
const wchar_t *wtmp = &decimalwc;
PRINT (decimal, wtmp, wide ? 1 : strlen (decimal));
}
if (precision > 0)
{
ssize_t tofill = precision - (numend - numstr);
PRINT (numstr, wnumstr, MIN (numend - numstr, precision));
if (tofill > 0)
PADN ('0', tofill);
}
if (info->left && info->pad == '0' && width > 0)
PADN ('0', width);
if ('P' - 'A' == 'p' - 'a')
outchar (info->spec + ('p' - 'a'));
else
outchar (info->spec == 'A' ? 'P' : 'p');
outchar (expnegative ? '-' : '+');
PRINT (expstr, wexpstr, (expbuf + sizeof expbuf) - expstr);
if (info->left && info->pad != '0' && width > 0)
PADN (info->pad, width);
return done;
}
File diff suppressed because it is too large Load Diff
@@ -1,4 +1,4 @@
/* Copyright (C) 1991-2002, 2003 Free Software Foundation, Inc. /* Copyright (C) 1991-1999, 2000, 2001 Free Software Foundation, Inc.
This file is part of the GNU C Library. This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or The GNU C Library is free software; you can redistribute it and/or
@@ -14,9 +14,7 @@
You should have received a copy of the GNU Lesser General Public You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. 02111-1307 USA. */
*/
#include <assert.h> #include <assert.h>
#include <errno.h> #include <errno.h>
@@ -27,7 +25,10 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <wchar.h>
#include <wctype.h>
#include <bits/libc-lock.h> #include <bits/libc-lock.h>
#include <locale/localeinfo.h>
#ifdef __GNUC__ #ifdef __GNUC__
# define HAVE_LONGLONG # define HAVE_LONGLONG
@@ -74,14 +75,12 @@
# ifdef COMPILE_WSCANF # ifdef COMPILE_WSCANF
# define ungetc(c, s) ((void) (c == WEOF \ # define ungetc(c, s) ((void) (c == WEOF \
|| (--read_in, \ || (--read_in, \
INTUSE(_IO_sputbackwc) (s, c)))) _IO_sputbackwc (s, c))))
# define ungetc_not_eof(c, s) ((void) (--read_in, \ # define ungetc_not_eof(c, s) ((void) (--read_in, \
INTUSE(_IO_sputbackwc) (s, c))) _IO_sputbackwc (s, c)))
# define inchar() (c == WEOF ? ((errno = inchar_errno), WEOF) \ # define inchar() (c == WEOF ? WEOF \
: ((c = _IO_getwc_unlocked (s)), \ : ((c = _IO_getwc_unlocked (s)), \
(void) (c != WEOF \ (void) (c != WEOF && ++read_in), c))
? ++read_in \
: (size_t) (inchar_errno = errno)), c))
# define MEMCPY(d, s, n) __wmemcpy (d, s, n) # define MEMCPY(d, s, n) __wmemcpy (d, s, n)
# define ISSPACE(Ch) iswspace (Ch) # define ISSPACE(Ch) iswspace (Ch)
@@ -106,14 +105,12 @@
# else # else
# define ungetc(c, s) ((void) ((int) c == EOF \ # define ungetc(c, s) ((void) ((int) c == EOF \
|| (--read_in, \ || (--read_in, \
INTUSE(_IO_sputbackc) (s, (unsigned char) c)))) _IO_sputbackc (s, (unsigned char) c))))
# define ungetc_not_eof(c, s) ((void) (--read_in, \ # define ungetc_not_eof(c, s) ((void) (--read_in, \
INTUSE(_IO_sputbackc) (s, (unsigned char) c))) _IO_sputbackc (s, (unsigned char) c)))
# define inchar() (c == EOF ? ((errno = inchar_errno), EOF) \ # define inchar() (c == EOF ? EOF \
: ((c = _IO_getc_unlocked (s)), \ : ((c = _IO_getc_unlocked (s)), \
(void) (c != EOF \ (void) (c != EOF && ++read_in), c))
? ++read_in \
: (size_t) (inchar_errno = errno)), c))
# define MEMCPY(d, s, n) memcpy (d, s, n) # define MEMCPY(d, s, n) memcpy (d, s, n)
# define ISSPACE(Ch) isspace (Ch) # define ISSPACE(Ch) isspace (Ch)
# define ISDIGIT(Ch) isdigit (Ch) # define ISDIGIT(Ch) isdigit (Ch)
@@ -275,8 +272,6 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
register int width; /* Maximum field width. */ register int width; /* Maximum field width. */
register int flags; /* Modifiers for current format element. */ register int flags; /* Modifiers for current format element. */
/* Errno of last failed inchar call. */
int inchar_errno = 0;
/* Status for reading F-P nums. */ /* Status for reading F-P nums. */
char got_dot, got_e, negative; char got_dot, got_e, negative;
/* If a [...] is a [^...]. */ /* If a [...] is a [^...]. */
@@ -299,7 +294,8 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
#else #else
const char *thousands; const char *thousands;
#endif #endif
/* State for the conversions. */
mbstate_t state;
/* Integral holding variables. */ /* Integral holding variables. */
union union
{ {
@@ -310,6 +306,7 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
} num; } num;
/* Character-buffer pointer. */ /* Character-buffer pointer. */
char *str = NULL; char *str = NULL;
wchar_t *wstr = NULL;
char **strptr = NULL; char **strptr = NULL;
ssize_t strsize = 0; ssize_t strsize = 0;
/* We must not react on white spaces immediately because they can /* We must not react on white spaces immediately because they can
@@ -354,13 +351,13 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
#ifdef COMPILE_WSCANF #ifdef COMPILE_WSCANF
decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC); decimal = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_DECIMAL_POINT_WC);
#else #else
decimal = "."; //_NL_CURRENT (LC_NUMERIC, DECIMAL_POINT); decimal = _NL_CURRENT (LC_NUMERIC, DECIMAL_POINT);
#endif #endif
/* Figure out the thousands separator character. */ /* Figure out the thousands separator character. */
#ifdef COMPILE_WSCANF #ifdef COMPILE_WSCANF
thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC); thousands = _NL_CURRENT_WORD (LC_NUMERIC, _NL_NUMERIC_THOUSANDS_SEP_WC);
#else #else
thousands = ","; //_NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP); thousands = _NL_CURRENT (LC_NUMERIC, THOUSANDS_SEP);
if (*thousands == '\0') if (*thousands == '\0')
thousands = NULL; thousands = NULL;
#endif #endif
@@ -369,6 +366,11 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
LOCK_STREAM (s); LOCK_STREAM (s);
#ifndef COMPILE_WSCANF
/* From now on we use `state' to convert the format string. */
memset (&state, '\0', sizeof (state));
#endif
/* Run through the format string. */ /* Run through the format string. */
while (*f != '\0') while (*f != '\0')
{ {
@@ -406,6 +408,30 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
# endif # endif
#endif #endif
#ifndef COMPILE_WSCANF
if (!isascii ((unsigned char) *f))
{
/* Non-ASCII, may be a multibyte. */
int len = __mbrlen (f, strlen (f), &state);
if (len > 0)
{
do
{
c = inchar ();
if (c == EOF)
input_error ();
else if (c != (unsigned char) *f++)
{
ungetc_not_eof (c, s);
conv_error ();
}
}
while (--len > 0);
continue;
}
}
#endif
fc = *f++; fc = *f++;
if (fc != '%') if (fc != '%')
{ {
@@ -667,6 +693,26 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
if (width == -1) if (width == -1)
width = 1; width = 1;
#ifdef COMPILE_WSCANF
/* We have to convert the wide character(s) into multibyte
characters and store the result. */
memset (&state, '\0', sizeof (state));
do
{
size_t n;
n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
if (n == (size_t) -1)
/* No valid wide character. */
input_error ();
/* Increment the output pointer. Even if we don't
write anything. */
str += n;
}
while (--width > 0 && inchar () != EOF);
#else
if (!(flags & SUPPRESS)) if (!(flags & SUPPRESS))
{ {
do do
@@ -675,13 +721,85 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
} }
else else
while (--width > 0 && inchar () != EOF); while (--width > 0 && inchar () != EOF);
#endif
if (!(flags & SUPPRESS)) if (!(flags & SUPPRESS))
++done; ++done;
break; break;
} }
// ToDo: check if we ever get here! /* FALLTHROUGH */
case L_('C'):
if (!(flags & SUPPRESS))
{
wstr = ARG (wchar_t *);
if (wstr == NULL)
conv_error ();
}
c = inchar ();
if (c == EOF)
input_error ();
#ifdef COMPILE_WSCANF
/* Just store the incoming wide characters. */
if (!(flags & SUPPRESS))
{
do
*wstr++ = c;
while (--width > 0 && inchar () != EOF);
}
else
while (--width > 0 && inchar () != EOF);
#else
{
/* We have to convert the multibyte input sequence to wide
characters. */
char buf[1];
mbstate_t cstate;
memset (&cstate, '\0', sizeof (cstate));
do
{
/* This is what we present the mbrtowc function first. */
buf[0] = c;
while (1)
{
size_t n;
n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
buf, 1, &cstate);
if (n == (size_t) -2)
{
/* Possibly correct character, just not enough
input. */
if (inchar () == EOF)
encode_error ();
buf[0] = c;
continue;
}
if (n != 1)
encode_error ();
/* We have a match. */
break;
}
/* Advance the result pointer. */
++wstr;
}
while (--width > 0 && inchar () != EOF);
}
#endif
if (!(flags & SUPPRESS))
++done;
break; break;
case L_('s'): /* Read a string. */ case L_('s'): /* Read a string. */
@@ -712,6 +830,10 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
if (c == EOF) if (c == EOF)
input_error (); input_error ();
#ifdef COMPILE_WSCANF
memset (&state, '\0', sizeof (state));
#endif
do do
{ {
if (ISSPACE (c)) if (ISSPACE (c))
@@ -720,6 +842,61 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
break; break;
} }
#ifdef COMPILE_WSCANF
/* This is quite complicated. We have to convert the
wide characters into multibyte characters and then
store them. */
{
size_t n;
if (!(flags & SUPPRESS) && (flags & MALLOC)
&& str + MB_CUR_MAX >= *strptr + strsize)
{
/* We have to enlarge the buffer if the `a' flag
was given. */
size_t strleng = str - *strptr;
char *newstr;
newstr = (char *) realloc (*strptr, strsize * 2);
if (newstr == NULL)
{
/* Can't allocate that much. Last-ditch
effort. */
newstr = (char *) realloc (*strptr,
strleng + MB_CUR_MAX);
if (newstr == NULL)
{
/* We lose. Oh well. Terminate the
string and stop converting,
so at least we don't skip any input. */
((char *) (*strptr))[strleng] = '\0';
++done;
conv_error ();
}
else
{
*strptr = newstr;
str = newstr + strleng;
strsize = strleng + MB_CUR_MAX;
}
}
else
{
*strptr = newstr;
str = newstr + strleng;
strsize *= 2;
}
}
n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c,
&state);
if (n == (size_t) -1)
encode_error ();
assert (n <= MB_CUR_MAX);
str += n;
}
#else
/* This is easy. */ /* This is easy. */
if (!(flags & SUPPRESS)) if (!(flags & SUPPRESS))
{ {
@@ -758,11 +935,44 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
} }
} }
} }
#endif
} }
while ((width <= 0 || --width > 0) && inchar () != EOF); while ((width <= 0 || --width > 0) && inchar () != EOF);
if (!(flags & SUPPRESS)) if (!(flags & SUPPRESS))
{ {
#ifdef COMPILE_WSCANF
/* We have to emit the code to get into the initial
state. */
char buf[MB_LEN_MAX];
size_t n = __wcrtomb (buf, L'\0', &state);
if (n > 0 && (flags & MALLOC)
&& str + n >= *strptr + strsize)
{
/* Enlarge the buffer. */
size_t strleng = str - *strptr;
char *newstr;
newstr = (char *) realloc (*strptr, strleng + n + 1);
if (newstr == NULL)
{
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((char *) (*strptr))[strleng] = '\0';
++done;
conv_error ();
}
else
{
*strptr = newstr;
str = newstr + strleng;
strsize = strleng + n + 1;
}
}
str = __mempcpy (str, buf, n);
#endif
*str++ = '\0'; *str++ = '\0';
if ((flags & MALLOC) && str - *strptr != strsize) if ((flags & MALLOC) && str - *strptr != strsize)
@@ -777,7 +987,165 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
break; break;
} }
/* FALLTHROUGH */ /* FALLTHROUGH */
// ToDo: check if we ever got here!
case L_('S'):
{
#ifndef COMPILE_WSCANF
mbstate_t cstate;
#endif
/* Wide character string. */
STRING_ARG (wstr, wchar_t);
c = inchar ();
if (c == EOF)
input_error ();
#ifndef COMPILE_WSCANF
memset (&cstate, '\0', sizeof (cstate));
#endif
do
{
if (ISSPACE (c))
{
ungetc_not_eof (c, s);
break;
}
#ifdef COMPILE_WSCANF
/* This is easy. */
if (!(flags & SUPPRESS))
{
*wstr++ = c;
if ((flags & MALLOC)
&& wstr == (wchar_t *) *strptr + strsize)
{
/* Enlarge the buffer. */
wstr = (wchar_t *) realloc (*strptr,
(2 * strsize)
* sizeof (wchar_t));
if (wstr == NULL)
{
/* Can't allocate that much. Last-ditch
effort. */
wstr = (wchar_t *) realloc (*strptr,
(strsize + 1)
* sizeof (wchar_t));
if (wstr == NULL)
{
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
++done;
conv_error ();
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
++strsize;
}
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
strsize *= 2;
}
}
}
#else
{
char buf[1];
buf[0] = c;
while (1)
{
size_t n;
n = __mbrtowc (!(flags & SUPPRESS) ? wstr : NULL,
buf, 1, &cstate);
if (n == (size_t) -2)
{
/* Possibly correct character, just not enough
input. */
if (inchar () == EOF)
encode_error ();
buf[0] = c;
continue;
}
if (n != 1)
encode_error ();
/* We have a match. */
++wstr;
break;
}
if (!(flags & SUPPRESS) && (flags & MALLOC)
&& wstr == (wchar_t *) *strptr + strsize)
{
/* Enlarge the buffer. */
wstr = (wchar_t *) realloc (*strptr,
(2 * strsize
* sizeof (wchar_t)));
if (wstr == NULL)
{
/* Can't allocate that much. Last-ditch effort. */
wstr = (wchar_t *) realloc (*strptr,
((strsize + 1)
* sizeof (wchar_t)));
if (wstr == NULL)
{
/* We lose. Oh well. Terminate the
string and stop converting, so at
least we don't skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
++done;
conv_error ();
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
++strsize;
}
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
strsize *= 2;
}
}
}
#endif
}
while ((width <= 0 || --width > 0) && inchar () != EOF);
if (!(flags & SUPPRESS))
{
*wstr++ = L'\0';
if ((flags & MALLOC) && wstr - (wchar_t *) *strptr != strsize)
{
wchar_t *cp = (wchar_t *) realloc (*strptr,
((wstr
- (wchar_t *) *strptr)
* sizeof(wchar_t)));
if (cp != NULL)
*strptr = (char *) cp;
}
++done;
}
}
break; break;
case L_('x'): /* Hexadecimal integer. */ case L_('x'): /* Hexadecimal integer. */
@@ -846,7 +1214,6 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
if (base == 0) if (base == 0)
base = 10; base = 10;
#if 0
if (base == 10 && (flags & I18N) != 0) if (base == 10 && (flags & I18N) != 0)
{ {
int from_level; int from_level;
@@ -1060,7 +1427,6 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
} }
} }
else else
#endif
/* Read the number into workspace. */ /* Read the number into workspace. */
while (c != EOF && width != 0) while (c != EOF && width != 0)
{ {
@@ -1500,19 +1866,19 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
ADDW (L_('\0')); ADDW (L_('\0'));
if (flags & LONGDBL) if (flags & LONGDBL)
{ {
long double d = 42.0; //__strtold_internal (wp, &tw, flags & GROUP); long double d = __strtold_internal (wp, &tw, flags & GROUP);
if (!(flags & SUPPRESS) && tw != wp) if (!(flags & SUPPRESS) && tw != wp)
*ARG (long double *) = negative ? -d : d; *ARG (long double *) = negative ? -d : d;
} }
else if (flags & LONG) else if (flags & LONG)
{ {
double d = 42.0; //__strtod_internal (wp, &tw, flags & GROUP); double d = __strtod_internal (wp, &tw, flags & GROUP);
if (!(flags & SUPPRESS) && tw != wp) if (!(flags & SUPPRESS) && tw != wp)
*ARG (double *) = negative ? -d : d; *ARG (double *) = negative ? -d : d;
} }
else else
{ {
float d = 42.0f; //__strtof_internal (wp, &tw, flags & GROUP); float d = __strtof_internal (wp, &tw, flags & GROUP);
if (!(flags & SUPPRESS) && tw != wp) if (!(flags & SUPPRESS) && tw != wp)
*ARG (float *) = negative ? -d : d; *ARG (float *) = negative ? -d : d;
} }
@@ -1525,6 +1891,9 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
break; break;
case L_('['): /* Character class. */ case L_('['): /* Character class. */
if (flags & LONG)
STRING_ARG (wstr, wchar_t);
else
STRING_ARG (str, char); STRING_ARG (str, char);
if (*f == L_('^')) if (*f == L_('^'))
@@ -1541,6 +1910,23 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
a very high value to make the algorithm easier. */ a very high value to make the algorithm easier. */
width = INT_MAX; width = INT_MAX;
#ifdef COMPILE_WSCANF
/* Find the beginning and the end of the scanlist. We are not
creating a lookup table since it would have to be too large.
Instead we search each time through the string. This is not
a constant lookup time but who uses this feature deserves to
be punished. */
tw = (wchar_t *) f; /* Marks the beginning. */
if (*f == L']')
++f;
while ((fc = *f++) != L'\0' && fc != L']');
if (fc == L'\0')
conv_error ();
wp = (wchar_t *) f - 1;
#else
/* Fill WP with byte flags indexed by character. /* Fill WP with byte flags indexed by character.
We will use this flag map for matching input characters. */ We will use this flag map for matching input characters. */
if (wpmax < UCHAR_MAX + 1) if (wpmax < UCHAR_MAX + 1)
@@ -1575,19 +1961,335 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
if (fc == '\0') if (fc == '\0')
conv_error(); conv_error();
#endif
#if 0
if (flags & LONG) if (flags & LONG)
{ {
size_t now = read_in;
#ifdef COMPILE_WSCANF
if (inchar () == WEOF)
input_error ();
do
{
wchar_t *runp;
/* Test whether it's in the scanlist. */
runp = tw;
while (runp < wp)
{
if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
&& runp != tw
&& (unsigned int) runp[-1] <= (unsigned int) runp[1])
{
/* Match against all characters in between the
first and last character of the sequence. */
wchar_t wc;
for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
if (wc == c)
break;
if (wc <= runp[1] && !not_in)
break;
if (wc <= runp[1] && not_in)
{
/* The current character is not in the
scanset. */
ungetwc (c, s);
goto out;
}
runp += 2;
} }
else else
{
if (*runp == c && !not_in)
break;
if (*runp == c && not_in)
{
ungetwc (c, s);
goto out;
}
++runp;
}
}
if (runp == wp && !not_in)
{
ungetwc (c, s);
goto out;
}
if (!(flags & SUPPRESS))
{
*wstr++ = c;
if ((flags & MALLOC)
&& wstr == (wchar_t *) *strptr + strsize)
{
/* Enlarge the buffer. */
wstr = (wchar_t *) realloc (*strptr,
(2 * strsize)
* sizeof (wchar_t));
if (wstr == NULL)
{
/* Can't allocate that much. Last-ditch
effort. */
wstr = (wchar_t *)
realloc (*strptr, (strsize + 1)
* sizeof (wchar_t));
if (wstr == NULL)
{
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
++done;
conv_error ();
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
++strsize;
}
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
strsize *= 2;
}
}
}
}
while (--width > 0 && inchar () != WEOF);
out:
#else
char buf[MB_LEN_MAX];
size_t cnt = 0;
mbstate_t cstate;
if (inchar () == EOF)
input_error ();
memset (&cstate, '\0', sizeof (cstate));
do
{
if (wp[c] == not_in)
{
ungetc_not_eof (c, s);
break;
}
/* This is easy. */
if (!(flags & SUPPRESS))
{
size_t n;
/* Convert it into a wide character. */
buf[0] = c;
n = __mbrtowc (wstr, buf, 1, &cstate);
if (n == (size_t) -2)
{
/* Possibly correct character, just not enough
input. */
++cnt;
assert (cnt < MB_CUR_MAX);
continue;
}
++wstr;
if ((flags & MALLOC)
&& wstr == (wchar_t *) *strptr + strsize)
{
/* Enlarge the buffer. */
wstr = (wchar_t *) realloc (*strptr,
(2 * strsize
* sizeof (wchar_t)));
if (wstr == NULL)
{
/* Can't allocate that much. Last-ditch
effort. */
wstr = (wchar_t *)
realloc (*strptr, ((strsize + 1)
* sizeof (wchar_t)));
if (wstr == NULL)
{
/* We lose. Oh well. Terminate the
string and stop converting,
so at least we don't skip any input. */
((wchar_t *) (*strptr))[strsize - 1] = L'\0';
++done;
conv_error ();
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
++strsize;
}
}
else
{
*strptr = (char *) wstr;
wstr += strsize;
strsize *= 2;
}
}
}
if (--width <= 0)
break;
}
while (inchar () != EOF);
if (cnt != 0)
/* We stopped in the middle of recognizing another
character. That's a problem. */
encode_error ();
#endif #endif
if (now == read_in)
/* We haven't succesfully read any character. */
conv_error ();
if (!(flags & SUPPRESS))
{
*wstr++ = L'\0';
if ((flags & MALLOC)
&& wstr - (wchar_t *) *strptr != strsize)
{
wchar_t *cp = (wchar_t *)
realloc (*strptr, ((wstr - (wchar_t *) *strptr)
* sizeof(wchar_t)));
if (cp != NULL)
*strptr = (char *) cp;
}
++done;
}
}
else
{ {
size_t now = read_in; size_t now = read_in;
if (inchar () == EOF) if (inchar () == EOF)
input_error (); input_error ();
#ifdef COMPILE_WSCANF
memset (&state, '\0', sizeof (state));
do
{
wchar_t *runp;
size_t n;
/* Test whether it's in the scanlist. */
runp = tw;
while (runp < wp)
{
if (runp[0] == L'-' && runp[1] != '\0' && runp + 1 != wp
&& runp != tw
&& (unsigned int) runp[-1] <= (unsigned int) runp[1])
{
/* Match against all characters in between the
first and last character of the sequence. */
wchar_t wc;
for (wc = runp[-1] + 1; wc <= runp[1]; ++wc)
if (wc == c)
break;
if (wc <= runp[1] && !not_in)
break;
if (wc <= runp[1] && not_in)
{
/* The current character is not in the
scanset. */
ungetwc (c, s);
goto out2;
}
runp += 2;
}
else
{
if (*runp == c && !not_in)
break;
if (*runp == c && not_in)
{
ungetwc (c, s);
goto out2;
}
++runp;
}
}
if (runp == wp && !not_in)
{
ungetwc (c, s);
goto out2;
}
if (!(flags & SUPPRESS))
{
if ((flags & MALLOC)
&& str + MB_CUR_MAX >= *strptr + strsize)
{
/* Enlarge the buffer. */
size_t strleng = str - *strptr;
char *newstr;
newstr = (char *) realloc (*strptr, 2 * strsize);
if (newstr == NULL)
{
/* Can't allocate that much. Last-ditch
effort. */
newstr = (char *) realloc (*strptr,
strleng + MB_CUR_MAX);
if (newstr == NULL)
{
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((char *) (*strptr))[strleng] = '\0';
++done;
conv_error ();
}
else
{
*strptr = newstr;
str = newstr + strleng;
strsize = strleng + MB_CUR_MAX;
}
}
else
{
*strptr = newstr;
str = newstr + strleng;
strsize *= 2;
}
}
}
n = __wcrtomb (!(flags & SUPPRESS) ? str : NULL, c, &state);
if (n == (size_t) -1)
encode_error ();
assert (n <= MB_CUR_MAX);
str += n;
}
while (--width > 0 && inchar () != WEOF);
out2:
#else
do do
{ {
if (wp[c] == not_in) if (wp[c] == not_in)
@@ -1636,6 +2338,7 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
} }
} }
while (--width > 0 && inchar () != EOF); while (--width > 0 && inchar () != EOF);
#endif
if (now == read_in) if (now == read_in)
/* We haven't succesfully read any character. */ /* We haven't succesfully read any character. */
@@ -1643,6 +2346,38 @@ __vfscanf (FILE *s, const char *format, va_list argptr)
if (!(flags & SUPPRESS)) if (!(flags & SUPPRESS))
{ {
#ifdef COMPILE_WSCANF
/* We have to emit the code to get into the initial
state. */
char buf[MB_LEN_MAX];
size_t n = __wcrtomb (buf, L'\0', &state);
if (n > 0 && (flags & MALLOC)
&& str + n >= *strptr + strsize)
{
/* Enlarge the buffer. */
size_t strleng = str - *strptr;
char *newstr;
newstr = (char *) realloc (*strptr, strleng + n + 1);
if (newstr == NULL)
{
/* We lose. Oh well. Terminate the string
and stop converting, so at least we don't
skip any input. */
((char *) (*strptr))[strleng] = '\0';
++done;
conv_error ();
}
else
{
*strptr = newstr;
str = newstr + strleng;
strsize = strleng + n + 1;
}
}
str = __mempcpy (str, buf, n);
#endif
*str++ = '\0'; *str++ = '\0';
if ((flags & MALLOC) && str - *strptr != strsize) if ((flags & MALLOC) && str - *strptr != strsize)
@@ -1700,9 +2435,8 @@ __vfwscanf (FILE *s, const wchar_t *format, va_list argptr)
int int
__vfscanf (FILE *s, const char *format, va_list argptr) __vfscanf (FILE *s, const char *format, va_list argptr)
{ {
return INTUSE(_IO_vfscanf) (s, format, argptr, NULL); return _IO_vfscanf (s, format, argptr, NULL);
} }
libc_hidden_def (__vfscanf)
# endif # endif
#endif #endif
@@ -1710,5 +2444,4 @@ libc_hidden_def (__vfscanf)
weak_alias (__vfwscanf, vfwscanf) weak_alias (__vfwscanf, vfwscanf)
#else #else
weak_alias (__vfscanf, vfscanf) weak_alias (__vfscanf, vfscanf)
INTDEF(_IO_vfscanf)
#endif #endif