2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-15 15:51:44 +00:00
xNBA/src/core/vsprintf.c

214 lines
4.7 KiB
C
Raw Normal View History

2005-03-08 18:53:11 +00:00
#include <stdarg.h>
2006-04-19 01:56:10 +00:00
#include <gpxe/if_ether.h> /* for ETH_ALEN */
#include "limits.h" /* for CHAR_BIT */
#include "console.h"
#include "errno.h"
#include "vsprintf.h"
2005-03-08 18:53:11 +00:00
#define LONGLONG_SHIFT ((int)((sizeof(unsigned long long)*CHAR_BIT) - 4))
#define LONG_SHIFT ((int)((sizeof(unsigned long)*CHAR_BIT) - 4))
#define INT_SHIFT ((int)((sizeof(unsigned int)*CHAR_BIT) - 4))
#define SHRT_SHIFT ((int)((sizeof(unsigned short)*CHAR_BIT) - 4))
#define CHAR_SHIFT ((int)((sizeof(unsigned char)*CHAR_BIT) - 4))
2005-03-08 18:53:11 +00:00
2005-05-19 18:41:54 +00:00
/** @file */
2005-03-08 18:53:11 +00:00
2005-05-19 14:51:37 +00:00
/**
* Write a formatted string to a buffer.
*
* @v buf Buffer into which to write the string, or NULL
* @v fmt Format string
* @v args Arguments corresponding to the format string
* @ret len Length of string written to buffer (if buf != NULL)
* @ret 0 (if buf == NULL)
* @err None -
2005-05-19 14:51:37 +00:00
*
* If #buf==NULL, then the string will be written to the console
2005-05-19 14:51:37 +00:00
* directly using putchar().
*
*/
2005-03-08 18:53:11 +00:00
static int vsprintf(char *buf, const char *fmt, va_list args)
{
const char *p;
char *s;
2005-03-08 18:53:11 +00:00
s = buf;
for ( ; *fmt != '\0'; ++fmt) {
if (*fmt != '%') {
buf ? *s++ = *fmt : putchar(*fmt);
continue;
}
/* skip width specs */
fmt++;
while (*fmt >= '0' && *fmt <= '9')
fmt++;
if (*fmt == '.')
fmt++;
while (*fmt >= '0' && *fmt <= '9')
fmt++;
if (*fmt == 's') {
2006-03-16 17:40:55 +00:00
for(p = va_arg(args, char *); *p != '\0'; p++)
2005-03-08 18:53:11 +00:00
buf ? *s++ = *p : putchar(*p);
} else if (*fmt == 'm') {
for(p = strerror(errno); *p != '\0'; p++)
buf ? *s++ = *p : putchar(*p);
} else { /* Length of item is bounded */
2005-03-08 18:53:11 +00:00
char tmp[40], *q = tmp;
int alt = 0;
int shift = INT_SHIFT;
if (*fmt == '#') {
alt = 1;
fmt++;
}
if (*fmt == 'l') {
shift = LONG_SHIFT;
fmt++;
}
if (*fmt == 'l') {
shift = LONGLONG_SHIFT;
fmt++;
}
if (*fmt == 'h') {
2005-03-08 18:53:11 +00:00
shift = SHRT_SHIFT;
fmt++;
if (*fmt == 'h') {
shift = CHAR_SHIFT;
fmt++;
}
}
2006-03-16 17:40:55 +00:00
2005-03-08 18:53:11 +00:00
/*
* Before each format q points to tmp buffer
* After each format q points past end of item
*/
if ((*fmt | 0x20) == 'x') {
/* With x86 gcc, sizeof(long) == sizeof(int) */
unsigned long h;
int ncase;
if (shift > LONG_SHIFT) {
h = va_arg(args, unsigned long long);
} else if (shift > INT_SHIFT) {
2005-03-08 18:53:11 +00:00
h = va_arg(args, unsigned long);
} else {
h = va_arg(args, unsigned int);
}
ncase = (*fmt & 0x20);
if (alt) {
*q++ = '0';
*q++ = 'X' | ncase;
}
for ( ; shift >= 0; shift -= 4)
*q++ = "0123456789ABCDEF"[(h >> shift) & 0xF] | ncase;
}
else if (*fmt == 'd') {
char *r, *t;
2005-03-08 18:53:11 +00:00
long i;
if (shift > LONG_SHIFT) {
i = va_arg(args, long long);
} else if (shift > INT_SHIFT) {
2005-03-08 18:53:11 +00:00
i = va_arg(args, long);
} else {
i = va_arg(args, int);
}
if (i < 0) {
*q++ = '-';
i = -i;
}
t = q; /* save beginning of digits */
2005-03-08 18:53:11 +00:00
do {
*q++ = '0' + (i % 10);
i /= 10;
} while (i);
/* reverse digits, stop in middle */
r = q; /* don't alter q */
while (--r > t) {
2005-03-08 18:53:11 +00:00
i = *r;
*r = *t;
*t++ = i;
2005-03-08 18:53:11 +00:00
}
}
else if (*fmt == '@') {
unsigned char *r;
union {
uint32_t l;
unsigned char c[4];
} u;
u.l = va_arg(args, uint32_t);
for (r = &u.c[0]; r < &u.c[4]; ++r)
q += sprintf(q, "%d.", *r);
--q;
}
else if (*fmt == '!') {
const char *r;
2005-03-08 18:53:11 +00:00
p = va_arg(args, char *);
for (r = p + ETH_ALEN; p < r; ++p)
q += sprintf(q, "%hhX:", *p);
--q;
}
else if (*fmt == 'c')
*q++ = va_arg(args, int);
else
*q++ = *fmt;
/* now output the saved string */
for (p = tmp; p < q; ++p)
buf ? *s++ = *p : putchar(*p);
}
}
if (buf)
*s = '\0';
return (s - buf);
}
2005-05-19 14:51:37 +00:00
/**
* Write a formatted string to a buffer.
*
* @v buf Buffer into which to write the string, or NULL
* @v fmt Format string
* @v ... Arguments corresponding to the format string
* @ret len Length of string written to buffer (if buf != NULL)
* @ret 0 (if buf == NULL)
* @err None -
2005-05-19 14:51:37 +00:00
*
* If #buf==NULL, then the string will be written to the console
2005-05-19 14:51:37 +00:00
* directly using putchar().
*
*/
2005-03-08 18:53:11 +00:00
int sprintf(char *buf, const char *fmt, ...)
{
va_list args;
int i;
va_start(args, fmt);
i=vsprintf(buf, fmt, args);
va_end(args);
return i;
}
#warning "Remove this buffer-overflow-in-waiting at some point"
int snprintf ( char *buf, size_t size, const char *fmt, ... ) {
va_list args;
int i;
va_start ( args, fmt );
i = vsprintf ( buf, fmt, args );
va_end ( args );
return i;
}
2005-05-19 14:51:37 +00:00
/**
* Write a formatted string to the console.
*
* @v fmt Format string
* @v ... Arguments corresponding to the format string
* @ret None -
* @err None -
2005-05-19 14:51:37 +00:00
*
*/
2006-03-16 17:40:55 +00:00
int printf(const char *fmt, ...)
2005-03-08 18:53:11 +00:00
{
va_list args;
int i;
va_start(args, fmt);
i=vsprintf(0, fmt, args);
va_end(args);
2006-03-16 17:40:55 +00:00
return i;
2005-03-08 18:53:11 +00:00
}