Implemented snprintf and similar functions. It took a lot of thinking,
but the modifications were relatively simple and straightforward.
This commit is contained in:
parent
c3e9b2ab24
commit
53a416d376
2 changed files with 52 additions and 13 deletions
53
lib/printf.c
53
lib/printf.c
|
@ -4,6 +4,7 @@
|
||||||
* (c) 1991, 1992 Lars Wirzenius & Linus Torvalds
|
* (c) 1991, 1992 Lars Wirzenius & Linus Torvalds
|
||||||
*
|
*
|
||||||
* Hacked up for BIRD by Martin Mares <mj@ucw.cz>
|
* Hacked up for BIRD by Martin Mares <mj@ucw.cz>
|
||||||
|
* Buffer size limitation implemented by Martin Mares.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "nest/bird.h"
|
#include "nest/bird.h"
|
||||||
|
@ -38,13 +39,15 @@ __res = ((unsigned long) n) % (unsigned) base; \
|
||||||
n = ((unsigned long) n) / (unsigned) base; \
|
n = ((unsigned long) n) / (unsigned) base; \
|
||||||
__res; })
|
__res; })
|
||||||
|
|
||||||
static char * number(char * str, long num, int base, int size, int precision
|
static char * number(char * str, long num, int base, int size, int precision,
|
||||||
,int type)
|
int type, int remains)
|
||||||
{
|
{
|
||||||
char c,sign,tmp[66];
|
char c,sign,tmp[66];
|
||||||
const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
|
const char *digits="0123456789abcdefghijklmnopqrstuvwxyz";
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
if (size >= 0 && (remains -= size) < 0)
|
||||||
|
return NULL;
|
||||||
if (type & LARGE)
|
if (type & LARGE)
|
||||||
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||||
if (type & LEFT)
|
if (type & LEFT)
|
||||||
|
@ -80,6 +83,8 @@ static char * number(char * str, long num, int base, int size, int precision
|
||||||
if (i > precision)
|
if (i > precision)
|
||||||
precision = i;
|
precision = i;
|
||||||
size -= precision;
|
size -= precision;
|
||||||
|
if (size < 0 && -size > remains)
|
||||||
|
return NULL;
|
||||||
if (!(type&(ZEROPAD+LEFT)))
|
if (!(type&(ZEROPAD+LEFT)))
|
||||||
while(size-->0)
|
while(size-->0)
|
||||||
*str++ = ' ';
|
*str++ = ' ';
|
||||||
|
@ -105,12 +110,12 @@ static char * number(char * str, long num, int base, int size, int precision
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
int bvsprintf(char *buf, const char *fmt, va_list args)
|
int bvsnprintf(char *buf, int size, const char *fmt, va_list args)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
unsigned long num;
|
unsigned long num;
|
||||||
int i, base;
|
int i, base;
|
||||||
char * str;
|
char *str, *start;
|
||||||
const char *s;
|
const char *s;
|
||||||
|
|
||||||
int flags; /* flags to number() */
|
int flags; /* flags to number() */
|
||||||
|
@ -120,8 +125,10 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
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 */
|
||||||
|
|
||||||
for (str=buf ; *fmt ; ++fmt) {
|
for (start=str=buf ; *fmt ; ++fmt, size-=(str-start), start=str) {
|
||||||
if (*fmt != '%') {
|
if (*fmt != '%') {
|
||||||
|
if (!size)
|
||||||
|
return -1;
|
||||||
*str++ = *fmt;
|
*str++ = *fmt;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -177,6 +184,8 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
/* default base */
|
/* default base */
|
||||||
base = 10;
|
base = 10;
|
||||||
|
|
||||||
|
if (field_width > size)
|
||||||
|
return -1;
|
||||||
switch (*fmt) {
|
switch (*fmt) {
|
||||||
case 'c':
|
case 'c':
|
||||||
if (!(flags & LEFT))
|
if (!(flags & LEFT))
|
||||||
|
@ -199,6 +208,8 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
len = strlen(s);
|
len = strlen(s);
|
||||||
if (precision >= 0 && len > precision)
|
if (precision >= 0 && len > precision)
|
||||||
len = precision;
|
len = precision;
|
||||||
|
if (len > size)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (!(flags & LEFT))
|
if (!(flags & LEFT))
|
||||||
while (len < field_width--)
|
while (len < field_width--)
|
||||||
|
@ -216,7 +227,9 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
}
|
}
|
||||||
str = number(str,
|
str = number(str,
|
||||||
(unsigned long) va_arg(args, void *), 16,
|
(unsigned long) va_arg(args, void *), 16,
|
||||||
field_width, precision, flags);
|
field_width, precision, flags, size);
|
||||||
|
if (!str)
|
||||||
|
return -1;
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
|
||||||
|
@ -232,6 +245,8 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
|
|
||||||
/* IP address */
|
/* IP address */
|
||||||
case 'I':
|
case 'I':
|
||||||
|
if (size < STD_ADDRESS_P_LENGTH)
|
||||||
|
return -1;
|
||||||
if (flags & SPECIAL)
|
if (flags & SPECIAL)
|
||||||
str = ip_ntox(va_arg(args, ip_addr), str);
|
str = ip_ntox(va_arg(args, ip_addr), str);
|
||||||
else {
|
else {
|
||||||
|
@ -261,6 +276,8 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
if (size < 2)
|
||||||
|
return -1;
|
||||||
if (*fmt != '%')
|
if (*fmt != '%')
|
||||||
*str++ = '%';
|
*str++ = '%';
|
||||||
if (*fmt)
|
if (*fmt)
|
||||||
|
@ -280,19 +297,39 @@ int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
num = va_arg(args, int);
|
num = va_arg(args, int);
|
||||||
else
|
else
|
||||||
num = va_arg(args, unsigned int);
|
num = va_arg(args, unsigned int);
|
||||||
str = number(str, num, base, field_width, precision, flags);
|
str = number(str, num, base, field_width, precision, flags, size);
|
||||||
|
if (!str)
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
if (!size)
|
||||||
|
return -1;
|
||||||
*str = '\0';
|
*str = '\0';
|
||||||
return str-buf;
|
return str-buf;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int bvsprintf(char *buf, const char *fmt, va_list args)
|
||||||
|
{
|
||||||
|
return bvsnprintf(buf, 1000000000, fmt, args);
|
||||||
|
}
|
||||||
|
|
||||||
int bsprintf(char * buf, const char *fmt, ...)
|
int bsprintf(char * buf, const char *fmt, ...)
|
||||||
{
|
{
|
||||||
va_list args;
|
va_list args;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
va_start(args, fmt);
|
va_start(args, fmt);
|
||||||
i=bvsprintf(buf,fmt,args);
|
i=bvsnprintf(buf, 1000000000, fmt, args);
|
||||||
|
va_end(args);
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
|
||||||
|
int bsnprintf(char * buf, int size, const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list args;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
va_start(args, fmt);
|
||||||
|
i=bvsnprintf(buf, size, fmt, args);
|
||||||
va_end(args);
|
va_end(args);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,5 +13,7 @@
|
||||||
|
|
||||||
int bsprintf(char *str, const char *fmt, ...);
|
int bsprintf(char *str, const char *fmt, ...);
|
||||||
int bvsprintf(char *str, const char *fmt, va_list args);
|
int bvsprintf(char *str, const char *fmt, va_list args);
|
||||||
|
int bsnprintf(char *str, int size, const char *fmt, ...);
|
||||||
|
int bvsnprintf(char *str, int size, const char *fmt, va_list args);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in a new issue