Add 64 bit int support to scanf()

PR:		2080
Submitted by:	David Dawes <dawes@rf900.physics.usyd.edu.au>
This commit is contained in:
Jordan K. Hubbard 1997-07-01 17:46:39 +00:00
parent 2ee7aa1179
commit 5e17038f01
2 changed files with 32 additions and 14 deletions

View file

@ -171,6 +171,15 @@ and the next pointer is a pointer to
(This type is not implemented; the
.Cm L
flag is currently ignored.)
.It Cm q
Indicates either that the conversion will be one of
.Cm dioux
or
.Cm n
and the next pointer is a pointer to a
.Em long long int
(rather than
.Em int ) ,
.El
.Pp
In addition to these flags,

View file

@ -39,7 +39,7 @@
static char sccsid[] = "@(#)vfscanf.c 8.1 (Berkeley) 6/4/93";
#endif
static const char rcsid[] =
"$Id: vfscanf.c,v 1.9 1997/04/04 18:28:38 ache Exp $";
"$Id: vfscanf.c,v 1.10 1997/04/04 19:07:02 ache Exp $";
#endif /* LIBC_SCCS and not lint */
#include <stdio.h>
@ -69,6 +69,7 @@ static const char rcsid[] =
#define SUPPRESS 0x08 /* suppress assignment */
#define POINTER 0x10 /* weird %p pointer (`fake hex') */
#define NOSKIP 0x20 /* do not skip blanks */
#define QUAD 0x400
/*
* The following are used in numeric conversions only:
@ -90,7 +91,7 @@ static const char rcsid[] =
#define CT_CHAR 0 /* %c conversion */
#define CT_CCL 1 /* %[...] conversion */
#define CT_STRING 2 /* %s conversion */
#define CT_INT 3 /* integer, i.e., strtol or strtoul */
#define CT_INT 3 /* integer, i.e., strtoq or strtouq */
#define CT_FLOAT 4 /* floating, i.e., strtod */
#define u_char unsigned char
@ -117,8 +118,8 @@ __svfscanf(fp, fmt0, ap)
int nassigned; /* number of fields assigned */
int nconversions; /* number of conversions */
int nread; /* number of characters consumed from fp */
int base; /* base argument to strtol/strtoul */
u_long (*ccfn)(); /* conversion function (strtol/strtoul) */
int base; /* base argument to strtoq/strtouq */
u_quad_t(*ccfn)(); /* conversion function (strtoq/strtouq) */
char ccltab[256]; /* character class table for %[...] */
char buf[BUF]; /* buffer for numeric conversions */
@ -171,6 +172,9 @@ literal:
case 'l':
flags |= LONG;
goto again;
case 'q':
flags |= QUAD;
goto again;
case 'L':
flags |= LONGDBL;
goto again;
@ -195,13 +199,13 @@ literal:
/* FALLTHROUGH */
case 'd':
c = CT_INT;
ccfn = (u_long (*)())strtol;
ccfn = (u_quad_t (*)())strtoq;
base = 10;
break;
case 'i':
c = CT_INT;
ccfn = (u_long (*)())strtol;
ccfn = (u_quad_t (*)())strtoq;
base = 0;
break;
@ -210,13 +214,13 @@ literal:
/* FALLTHROUGH */
case 'o':
c = CT_INT;
ccfn = strtoul;
ccfn = strtouq;
base = 8;
break;
case 'u':
c = CT_INT;
ccfn = strtoul;
ccfn = strtouq;
base = 10;
break;
@ -226,7 +230,7 @@ literal:
case 'x':
flags |= PFXOK; /* enable 0x prefixing */
c = CT_INT;
ccfn = strtoul;
ccfn = strtouq;
base = 16;
break;
@ -258,7 +262,7 @@ literal:
case 'p': /* pointer format is like hex */
flags |= POINTER | PFXOK;
c = CT_INT;
ccfn = strtoul;
ccfn = strtouq;
base = 16;
break;
@ -270,6 +274,8 @@ literal:
*va_arg(ap, short *) = nread;
else if (flags & LONG)
*va_arg(ap, long *) = nread;
else if (flags & QUAD)
*va_arg(ap, quad_t *) = nread;
else
*va_arg(ap, int *) = nread;
continue;
@ -284,7 +290,7 @@ literal:
if (isupper(c))
flags |= LONG;
c = CT_INT;
ccfn = (u_long (*)())strtol;
ccfn = (u_quad_t (*)())strtoq;
base = 10;
break;
}
@ -429,7 +435,7 @@ literal:
continue;
case CT_INT:
/* scan an integer as if by strtol/strtoul */
/* scan an integer as if by strtoq/strtouq */
#ifdef hardway
if (width == 0 || width > sizeof(buf) - 1)
width = sizeof(buf) - 1;
@ -547,16 +553,19 @@ literal:
(void) ungetc(c, fp);
}
if ((flags & SUPPRESS) == 0) {
u_long res;
u_quad_t res;
*p = 0;
res = (*ccfn)(buf, (char **)NULL, base);
if (flags & POINTER)
*va_arg(ap, void **) = (void *)res;
*va_arg(ap, void **) =
(void *)(u_long)res;
else if (flags & SHORT)
*va_arg(ap, short *) = res;
else if (flags & LONG)
*va_arg(ap, long *) = res;
else if (flags & QUAD)
*va_arg(ap, quad_t *) = res;
else
*va_arg(ap, int *) = res;
nassigned++;