2004-04-21 J"orn Rennecke <joern.rennecke@superh.com>

* libc/stdio/vfscanf.c (NNZDIGITS): New define.
        (__svfscanf_r): In integer conversions, leave out leading zeroes
        which are not part of a base prefix.
        Keep track of width truncation to fit into buf, not counting left-out
        zeroes against width till the truncation has been compensated for.
This commit is contained in:
Jeff Johnston 2004-04-22 21:58:15 +00:00
parent b05d102cc1
commit 04e8fca153
2 changed files with 46 additions and 17 deletions

View File

@ -1,3 +1,11 @@
2004-04-21 J"orn Rennecke <joern.rennecke@superh.com>
* libc/stdio/vfscanf.c (NNZDIGITS): New define.
(__svfscanf_r): In integer conversions, leave out leading zeroes
which are not part of a base prefix.
Keep track of width truncation to fit into buf, not counting left-out
zeroes against width till the truncation has been compensated for.
2004-04-20 Corinna Vinschen <corinna@vinschen.de> 2004-04-20 Corinna Vinschen <corinna@vinschen.de>
* libc/include/sys/unistd.h (ttyname_r): Add missing comma. * libc/include/sys/unistd.h (ttyname_r): Add missing comma.

View File

@ -179,6 +179,7 @@ extern _LONG_DOUBLE _strtold _PARAMS((char *s, char **sptr));
#define PFXOK 0x200 /* 0x prefix is (still) legal */ #define PFXOK 0x200 /* 0x prefix is (still) legal */
#define NZDIGITS 0x400 /* no zero digits detected */ #define NZDIGITS 0x400 /* no zero digits detected */
#define NNZDIGITS 0x800 /* no non-zero digits detected */
/* /*
* Conversion types. * Conversion types.
@ -759,17 +760,20 @@ __svfscanf_r (rptr, fp, fmt0, ap)
continue; continue;
case CT_INT: case CT_INT:
{
/* scan an integer as if by strtol/strtoul */ /* scan an integer as if by strtol/strtoul */
unsigned width_left = 0;
#ifdef hardway #ifdef hardway
if (width == 0 || width > sizeof (buf) - 1) if (width == 0 || width > sizeof (buf) - 1)
width = sizeof (buf) - 1;
#else #else
/* size_t is unsigned, hence this optimisation */ /* size_t is unsigned, hence this optimisation */
if (--width > sizeof (buf) - 2) if (width - 1 > sizeof (buf) - 2)
width = sizeof (buf) - 2;
width++;
#endif #endif
flags |= SIGNOK | NDIGITS | NZDIGITS; {
width_left = width - (sizeof (buf) - 1);
width = sizeof (buf) - 1;
}
flags |= SIGNOK | NDIGITS | NZDIGITS | NNZDIGITS;
for (p = buf; width; width--) for (p = buf; width; width--)
{ {
c = *fp->_p; c = *fp->_p;
@ -789,16 +793,25 @@ __svfscanf_r (rptr, fp, fmt0, ap)
* will turn it off if we have scanned any nonzero digits). * will turn it off if we have scanned any nonzero digits).
*/ */
case '0': case '0':
if (! (flags & NNZDIGITS))
goto ok;
if (base == 0) if (base == 0)
{ {
base = 8; base = 8;
flags |= PFXOK; flags |= PFXOK;
} }
if (flags & NZDIGITS) if (flags & NZDIGITS)
flags &= ~(SIGNOK | NZDIGITS | NDIGITS); {
else flags &= ~(SIGNOK | NZDIGITS | NDIGITS);
flags &= ~(SIGNOK | PFXOK | NDIGITS); goto ok;
goto ok; }
flags &= ~(SIGNOK | PFXOK | NDIGITS);
if (width_left)
{
width_left--;
width++;
}
goto skip;
/* 1 through 7 always legal */ /* 1 through 7 always legal */
case '1': case '1':
@ -809,7 +822,7 @@ __svfscanf_r (rptr, fp, fmt0, ap)
case '6': case '6':
case '7': case '7':
base = basefix[base]; base = basefix[base];
flags &= ~(SIGNOK | PFXOK | NDIGITS); flags &= ~(SIGNOK | PFXOK | NDIGITS | NNZDIGITS);
goto ok; goto ok;
/* digits 8 and 9 ok iff decimal or hex */ /* digits 8 and 9 ok iff decimal or hex */
@ -818,7 +831,7 @@ __svfscanf_r (rptr, fp, fmt0, ap)
base = basefix[base]; base = basefix[base];
if (base <= 8) if (base <= 8)
break; /* not legal here */ break; /* not legal here */
flags &= ~(SIGNOK | PFXOK | NDIGITS); flags &= ~(SIGNOK | PFXOK | NDIGITS | NNZDIGITS);
goto ok; goto ok;
/* letters ok iff hex */ /* letters ok iff hex */
@ -837,7 +850,7 @@ __svfscanf_r (rptr, fp, fmt0, ap)
/* no need to fix base here */ /* no need to fix base here */
if (base <= 10) if (base <= 10)
break; /* not legal here */ break; /* not legal here */
flags &= ~(SIGNOK | PFXOK | NDIGITS); flags &= ~(SIGNOK | PFXOK | NDIGITS | NNZDIGITS);
goto ok; goto ok;
/* sign ok only as first character */ /* sign ok only as first character */
@ -872,6 +885,7 @@ __svfscanf_r (rptr, fp, fmt0, ap)
* c is legal: store it and look at the next. * c is legal: store it and look at the next.
*/ */
*p++ = c; *p++ = c;
skip:
if (--fp->_r > 0) if (--fp->_r > 0)
fp->_p++; fp->_p++;
else else
@ -939,7 +953,7 @@ __svfscanf_r (rptr, fp, fmt0, ap)
} }
nread += p - buf; nread += p - buf;
break; break;
}
#ifdef FLOATING_POINT #ifdef FLOATING_POINT
case CT_FLOAT: case CT_FLOAT:
{ {
@ -951,15 +965,17 @@ __svfscanf_r (rptr, fp, fmt0, ap)
long leading_zeroes = 0; long leading_zeroes = 0;
long zeroes, exp_adjust; long zeroes, exp_adjust;
char *exp_start = NULL; char *exp_start = NULL;
unsigned width_left = 0;
#ifdef hardway #ifdef hardway
if (width == 0 || width > sizeof (buf) - 1) if (width == 0 || width > sizeof (buf) - 1)
width = sizeof (buf) - 1;
#else #else
/* size_t is unsigned, hence this optimisation */ /* size_t is unsigned, hence this optimisation */
if (--width > sizeof (buf) - 2) if (width - 1 > sizeof (buf) - 2)
width = sizeof (buf) - 2;
width++;
#endif #endif
{
width_left = width - (sizeof (buf) - 1);
width = sizeof (buf) - 1;
}
flags |= SIGNOK | NDIGITS | DPTOK | EXPOK; flags |= SIGNOK | NDIGITS | DPTOK | EXPOK;
zeroes = 0; zeroes = 0;
exp_adjust = 0; exp_adjust = 0;
@ -978,6 +994,11 @@ __svfscanf_r (rptr, fp, fmt0, ap)
{ {
flags &= ~SIGNOK; flags &= ~SIGNOK;
zeroes++; zeroes++;
if (width_left)
{
width_left--;
width++;
}
goto fskip; goto fskip;
} }
/* Fall through. */ /* Fall through. */