mirror of
https://gitlab.com/qemu-project/openbios.git
synced 2024-02-13 08:34:06 +08:00
Update vsprintf.c from Linux to get 64-bit output
git-svn-id: svn://coreboot.org/openbios/openbios-devel@148 f158a5a8-5612-0410-a976-696ce0be7e32
This commit is contained in:
299
libc/vsprintf.c
299
libc/vsprintf.c
@@ -5,7 +5,7 @@
|
|||||||
/*
|
/*
|
||||||
* linux/lib/vsprintf.c
|
* linux/lib/vsprintf.c
|
||||||
*
|
*
|
||||||
* Copyright (C) 1991, 1992, Linus Torvalds
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
|
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
|
||||||
@@ -13,18 +13,20 @@
|
|||||||
* Wirzenius wrote this portably, Torvalds fucked it up :-)
|
* Wirzenius wrote this portably, Torvalds fucked it up :-)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
|
||||||
|
* - changed to provide snprintf and vsnprintf functions
|
||||||
|
*/
|
||||||
|
|
||||||
#include "openbios/config.h"
|
#include "openbios/config.h"
|
||||||
#include "libc/string.h"
|
#include "libc/string.h"
|
||||||
#include "libc/vsprintf.h"
|
#include "libc/vsprintf.h"
|
||||||
|
|
||||||
/* we use this so that we can do without the ctype library */
|
|
||||||
#define is_digit(c) ((c) >= '0' && (c) <= '9')
|
|
||||||
|
|
||||||
static int skip_atoi(const char **s)
|
static int skip_atoi(const char **s)
|
||||||
{
|
{
|
||||||
int i=0;
|
int i=0;
|
||||||
|
|
||||||
while (is_digit(**s))
|
while (isdigit(**s))
|
||||||
i = i*10 + *((*s)++) - '0';
|
i = i*10 + *((*s)++) - '0';
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
@@ -39,21 +41,25 @@ static int skip_atoi(const char **s)
|
|||||||
|
|
||||||
#define do_div(n,base) ({ \
|
#define do_div(n,base) ({ \
|
||||||
int __res; \
|
int __res; \
|
||||||
__res = ((unsigned long) n) % (unsigned) base; \
|
__res = ((unsigned long long) n) % (unsigned) base; \
|
||||||
n = ((unsigned long) n) / (unsigned) base; \
|
n = ((unsigned long long) n) / (unsigned) base; \
|
||||||
__res; })
|
__res; })
|
||||||
|
|
||||||
static int mstrlen( const char *str );
|
static int mstrlen( const char *str );
|
||||||
|
|
||||||
static char * number(char * str, long num, int base, int size, int precision
|
#ifndef PAGE_SIZE
|
||||||
,int type)
|
#define PAGE_SIZE 4096
|
||||||
|
#endif
|
||||||
|
|
||||||
|
static char * number(char * buf, char * end, long long num, int base, int size, int precision, int type)
|
||||||
{
|
{
|
||||||
char c,sign,tmp[66];
|
char c,sign,tmp[66];
|
||||||
const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
|
const char *digits;
|
||||||
|
static const char small_digits[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
|
static const char large_digits[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
if (type & LARGE)
|
digits = (type & LARGE) ? large_digits : small_digits;
|
||||||
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
||||||
if (type & LEFT)
|
if (type & LEFT)
|
||||||
type &= ~ZEROPAD;
|
type &= ~ZEROPAD;
|
||||||
if (base < 2 || base > 36)
|
if (base < 2 || base > 36)
|
||||||
@@ -82,52 +88,78 @@ static char * number(char * str, long num, int base, int size, int precision
|
|||||||
i = 0;
|
i = 0;
|
||||||
if (num == 0)
|
if (num == 0)
|
||||||
tmp[i++]='0';
|
tmp[i++]='0';
|
||||||
else while (num != 0) {
|
else while (num != 0)
|
||||||
#if 0
|
|
||||||
tmp[i++] = digits[do_div(num,base)];
|
tmp[i++] = digits[do_div(num,base)];
|
||||||
#endif
|
|
||||||
int __res;
|
|
||||||
__res = ((unsigned long) num) % (unsigned) base;
|
|
||||||
num = ((unsigned long) num) / (unsigned) base;
|
|
||||||
tmp[i++] = digits[__res];
|
|
||||||
}
|
|
||||||
if (i > precision)
|
if (i > precision)
|
||||||
precision = i;
|
precision = i;
|
||||||
size -= precision;
|
size -= precision;
|
||||||
if (!(type&(ZEROPAD+LEFT)))
|
if (!(type&(ZEROPAD+LEFT))) {
|
||||||
while(size-->0)
|
while(size-->0) {
|
||||||
*str++ = ' ';
|
if (buf <= end)
|
||||||
if (sign)
|
*buf = ' ';
|
||||||
*str++ = sign;
|
++buf;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (sign) {
|
||||||
|
if (buf <= end)
|
||||||
|
*buf = sign;
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
if (type & SPECIAL) {
|
if (type & SPECIAL) {
|
||||||
if (base==8)
|
if (base==8) {
|
||||||
*str++ = '0';
|
if (buf <= end)
|
||||||
else if (base==16) {
|
*buf = '0';
|
||||||
*str++ = '0';
|
++buf;
|
||||||
*str++ = digits[33];
|
} else if (base==16) {
|
||||||
|
if (buf <= end)
|
||||||
|
*buf = '0';
|
||||||
|
++buf;
|
||||||
|
if (buf <= end)
|
||||||
|
*buf = digits[33];
|
||||||
|
++buf;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!(type & LEFT))
|
if (!(type & LEFT)) {
|
||||||
while (size-- > 0)
|
while (size-- > 0) {
|
||||||
*str++ = c;
|
if (buf <= end)
|
||||||
while (i < precision--)
|
*buf = c;
|
||||||
*str++ = '0';
|
++buf;
|
||||||
while (i-- > 0)
|
}
|
||||||
*str++ = tmp[i];
|
}
|
||||||
while (size-- > 0)
|
while (i < precision--) {
|
||||||
*str++ = ' ';
|
if (buf <= end)
|
||||||
return str;
|
*buf = '0';
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
while (i-- > 0) {
|
||||||
|
if (buf <= end)
|
||||||
|
*buf = tmp[i];
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
while (size-- > 0) {
|
||||||
|
if (buf <= end)
|
||||||
|
*buf = ' ';
|
||||||
|
++buf;
|
||||||
|
}
|
||||||
|
return buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Forward decl. to keep compiler happy. */
|
/**
|
||||||
//static int vsprintf(char *buf, const char *fmt, va_list args);
|
* vsnprintf - Format a string and place it in a buffer
|
||||||
|
* @buf: The buffer to place the result into
|
||||||
int vsprintf(char *buf, const char *fmt, va_list args)
|
* @size: The size of the buffer, including the trailing null space
|
||||||
|
* @fmt: The format string to use
|
||||||
|
* @args: Arguments for the format string
|
||||||
|
*
|
||||||
|
* Call this function if you are already dealing with a va_list.
|
||||||
|
* You probably want snprintf instead.
|
||||||
|
*/
|
||||||
|
int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
unsigned long num;
|
unsigned long long num;
|
||||||
int i, base;
|
int i, base;
|
||||||
char * str;
|
char *str, *end, c;
|
||||||
const char *s;
|
const char *s;
|
||||||
|
|
||||||
int flags; /* flags to number() */
|
int flags; /* flags to number() */
|
||||||
@@ -136,10 +168,22 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
int precision; /* min. # of digits for integers; max
|
int precision; /* min. # of digits for integers; max
|
||||||
number of chars for from string */
|
number of chars for from string */
|
||||||
int qualifier; /* 'h', 'l', or 'L' for integer fields */
|
int qualifier; /* 'h', 'l', or 'L' for integer fields */
|
||||||
|
/* 'z' support added 23/7/1999 S.H. */
|
||||||
|
/* 'z' changed to 'Z' --davidm 1/25/99 */
|
||||||
|
|
||||||
for (str=buf ; *fmt ; ++fmt) {
|
str = buf;
|
||||||
|
end = buf + size - 1;
|
||||||
|
|
||||||
|
if (end < buf - 1) {
|
||||||
|
end = ((void *) -1);
|
||||||
|
size = end - buf + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (; *fmt ; ++fmt) {
|
||||||
if (*fmt != '%') {
|
if (*fmt != '%') {
|
||||||
*str++ = *fmt;
|
if (str <= end)
|
||||||
|
*str = *fmt;
|
||||||
|
++str;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -157,7 +201,7 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
|
|
||||||
/* get field width */
|
/* get field width */
|
||||||
field_width = -1;
|
field_width = -1;
|
||||||
if (is_digit(*fmt))
|
if (isdigit(*fmt))
|
||||||
field_width = skip_atoi(&fmt);
|
field_width = skip_atoi(&fmt);
|
||||||
else if (*fmt == '*') {
|
else if (*fmt == '*') {
|
||||||
++fmt;
|
++fmt;
|
||||||
@@ -173,7 +217,7 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
precision = -1;
|
precision = -1;
|
||||||
if (*fmt == '.') {
|
if (*fmt == '.') {
|
||||||
++fmt;
|
++fmt;
|
||||||
if (is_digit(*fmt))
|
if (isdigit(*fmt))
|
||||||
precision = skip_atoi(&fmt);
|
precision = skip_atoi(&fmt);
|
||||||
else if (*fmt == '*') {
|
else if (*fmt == '*') {
|
||||||
++fmt;
|
++fmt;
|
||||||
@@ -186,9 +230,14 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
|
|
||||||
/* get the conversion qualifier */
|
/* get the conversion qualifier */
|
||||||
qualifier = -1;
|
qualifier = -1;
|
||||||
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L') {
|
if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
|
||||||
|
*fmt =='Z' || *fmt == 'z') {
|
||||||
qualifier = *fmt;
|
qualifier = *fmt;
|
||||||
++fmt;
|
++fmt;
|
||||||
|
if (qualifier == 'l' && *fmt == 'l') {
|
||||||
|
qualifier = 'L';
|
||||||
|
++fmt;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* default base */
|
/* default base */
|
||||||
@@ -196,17 +245,27 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
|
|
||||||
switch (*fmt) {
|
switch (*fmt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
if (!(flags & LEFT))
|
if (!(flags & LEFT)) {
|
||||||
while (--field_width > 0)
|
while (--field_width > 0) {
|
||||||
*str++ = ' ';
|
if (str <= end)
|
||||||
*str++ = (unsigned char) va_arg(args, int);
|
*str = ' ';
|
||||||
while (--field_width > 0)
|
++str;
|
||||||
*str++ = ' ';
|
}
|
||||||
|
}
|
||||||
|
c = (unsigned char) va_arg(args, int);
|
||||||
|
if (str <= end)
|
||||||
|
*str = c;
|
||||||
|
++str;
|
||||||
|
while (--field_width > 0) {
|
||||||
|
if (str <= end)
|
||||||
|
*str = ' ';
|
||||||
|
++str;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 's':
|
case 's':
|
||||||
s = va_arg(args, char *);
|
s = va_arg(args, char *);
|
||||||
if (!s)
|
if ((unsigned long)s < PAGE_SIZE)
|
||||||
s = "<NULL>";
|
s = "<NULL>";
|
||||||
|
|
||||||
#if 0
|
#if 0
|
||||||
@@ -216,14 +275,23 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
if( precision > len )
|
if( precision > len )
|
||||||
len = precision;
|
len = precision;
|
||||||
#endif
|
#endif
|
||||||
|
if (!(flags & LEFT)) {
|
||||||
if (!(flags & LEFT))
|
while (len < field_width--) {
|
||||||
while (len < field_width--)
|
if (str <= end)
|
||||||
*str++ = ' ';
|
*str = ' ';
|
||||||
for (i = 0; i < len; ++i)
|
++str;
|
||||||
*str++ = *s++;
|
}
|
||||||
while (len < field_width--)
|
}
|
||||||
*str++ = ' ';
|
for (i = 0; i < len; ++i) {
|
||||||
|
if (str <= end)
|
||||||
|
*str = *s;
|
||||||
|
++str; ++s;
|
||||||
|
}
|
||||||
|
while (len < field_width--) {
|
||||||
|
if (str <= end)
|
||||||
|
*str = ' ';
|
||||||
|
++str;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
case 'p':
|
case 'p':
|
||||||
@@ -231,22 +299,33 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
field_width = 2*sizeof(void *);
|
field_width = 2*sizeof(void *);
|
||||||
flags |= ZEROPAD;
|
flags |= ZEROPAD;
|
||||||
}
|
}
|
||||||
str = number(str,
|
str = number(str, end,
|
||||||
(unsigned long) va_arg(args, void *), 16,
|
(unsigned long) va_arg(args, void *),
|
||||||
field_width, precision, flags);
|
16, field_width, precision, flags);
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
case 'n':
|
case 'n':
|
||||||
|
/* FIXME:
|
||||||
|
* What does C99 say about the overflow case here? */
|
||||||
if (qualifier == 'l') {
|
if (qualifier == 'l') {
|
||||||
long * ip = va_arg(args, long *);
|
long * ip = va_arg(args, long *);
|
||||||
*ip = (str - buf);
|
*ip = (str - buf);
|
||||||
|
} else if (qualifier == 'Z' || qualifier == 'z') {
|
||||||
|
size_t * ip = va_arg(args, size_t *);
|
||||||
|
*ip = (str - buf);
|
||||||
} else {
|
} else {
|
||||||
int * ip = va_arg(args, int *);
|
int * ip = va_arg(args, int *);
|
||||||
*ip = (str - buf);
|
*ip = (str - buf);
|
||||||
}
|
}
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
case '%':
|
||||||
|
if (str <= end)
|
||||||
|
*str = '%';
|
||||||
|
++str;
|
||||||
|
continue;
|
||||||
|
|
||||||
/* integer number formats - set up the flags and "break" */
|
/* integer number formats - set up the flags and "break" */
|
||||||
case 'o':
|
case 'o':
|
||||||
base = 8;
|
base = 8;
|
||||||
@@ -265,30 +344,88 @@ int vsprintf(char *buf, const char *fmt, va_list args)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
if (*fmt != '%')
|
if (str <= end)
|
||||||
*str++ = '%';
|
*str = '%';
|
||||||
if (*fmt)
|
++str;
|
||||||
*str++ = *fmt;
|
if (*fmt) {
|
||||||
else
|
if (str <= end)
|
||||||
|
*str = *fmt;
|
||||||
|
++str;
|
||||||
|
} else {
|
||||||
--fmt;
|
--fmt;
|
||||||
|
}
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (qualifier == 'l')
|
if (qualifier == 'L')
|
||||||
|
num = va_arg(args, long long);
|
||||||
|
else if (qualifier == 'l') {
|
||||||
num = va_arg(args, unsigned long);
|
num = va_arg(args, unsigned long);
|
||||||
else if (qualifier == 'h') {
|
if (flags & SIGN)
|
||||||
|
num = (signed long) num;
|
||||||
|
} else if (qualifier == 'Z' || qualifier == 'z') {
|
||||||
|
num = va_arg(args, size_t);
|
||||||
|
} else if (qualifier == 'h') {
|
||||||
num = (unsigned short) va_arg(args, int);
|
num = (unsigned short) va_arg(args, int);
|
||||||
if (flags & SIGN)
|
if (flags & SIGN)
|
||||||
num = (short) num;
|
num = (signed short) num;
|
||||||
} else if (flags & SIGN)
|
} else {
|
||||||
num = va_arg(args, int);
|
|
||||||
else
|
|
||||||
num = va_arg(args, unsigned int);
|
num = va_arg(args, unsigned int);
|
||||||
str = number(str, num, base, field_width, precision, flags);
|
if (flags & SIGN)
|
||||||
|
num = (signed int) num;
|
||||||
}
|
}
|
||||||
|
str = number(str, end, num, base,
|
||||||
|
field_width, precision, flags);
|
||||||
|
}
|
||||||
|
if (str <= end)
|
||||||
*str = '\0';
|
*str = '\0';
|
||||||
|
else if (size > 0)
|
||||||
|
/* don't write out a null byte if the buf size is zero */
|
||||||
|
*end = '\0';
|
||||||
|
/* the trailing null byte doesn't count towards the total
|
||||||
|
* ++str;
|
||||||
|
*/
|
||||||
return str-buf;
|
return str-buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* snprintf - Format a string and place it in a buffer
|
||||||
|
* @buf: The buffer to place the result into
|
||||||
|
* @size: The size of the buffer, including the trailing null space
|
||||||
|
* @fmt: The format string to use
|
||||||
|
* @...: Arguments for the format string
|
||||||
|
*/
|
||||||
|
int snprintf(char * buf, size_t size, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
i=vsnprintf(buf,size,fmt,args);
|
||||||
|
va_end(args);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* vsprintf - Format a string and place it in a buffer
|
||||||
|
* @buf: The buffer to place the result into
|
||||||
|
* @fmt: The format string to use
|
||||||
|
* @args: Arguments for the format string
|
||||||
|
*
|
||||||
|
* Call this function if you are already dealing with a va_list.
|
||||||
|
* You probably want sprintf instead.
|
||||||
|
*/
|
||||||
|
int vsprintf(char *buf, const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
return vsnprintf(buf, (~0U)>>1, fmt, args);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sprintf - Format a string and place it in a buffer
|
||||||
|
* @buf: The buffer to place the result into
|
||||||
|
* @fmt: The format string to use
|
||||||
|
* @...: Arguments for the format string
|
||||||
|
*/
|
||||||
int sprintf(char * buf, const char *fmt, ...)
|
int sprintf(char * buf, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
|
|||||||
Reference in New Issue
Block a user