76 lines
1.8 KiB
C
76 lines
1.8 KiB
C
|
//----------------------------------------------------------------------------//
|
||
|
// GNU GPL OS/K //
|
||
|
// //
|
||
|
// Authors: spectral` //
|
||
|
// NeoX //
|
||
|
// //
|
||
|
// Desc: sprintf()-related functions //
|
||
|
//----------------------------------------------------------------------------//
|
||
|
|
||
|
#include "common/string.h"
|
||
|
|
||
|
//
|
||
|
// Format str according to fmt using ellipsed arguments
|
||
|
//
|
||
|
int sprintf(char *str, const char *fmt, ...)
|
||
|
{
|
||
|
int ret;
|
||
|
va_list ap;
|
||
|
|
||
|
va_start(ap);
|
||
|
ret = vsnprintf(str, SIZE_T_MAX, fmt, ap);
|
||
|
va_end(ap);
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// Format str according to fmt, using the va_list ap
|
||
|
//
|
||
|
int vsprintf(char *str, const char *fmt, va_list ap)
|
||
|
{
|
||
|
return vsnprintf(str, SIZE_T_MAX, fmt, ap);
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// sprintf() but with a size limit: no more than n bytes are written in str
|
||
|
// XXX null termination behavior?
|
||
|
//
|
||
|
int snprintf(char *str, size_t n, const char *fmt, ...)
|
||
|
{
|
||
|
int ret;
|
||
|
va_list ap;
|
||
|
|
||
|
va_start(ap);
|
||
|
ret = vsnprintf(str, n, fmt, ap)
|
||
|
va_end(ap);
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// snprintf() but arguments
|
||
|
//
|
||
|
int vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
|
||
|
{
|
||
|
int ret = 0;
|
||
|
|
||
|
while (*fmt && ret < n) {
|
||
|
if (*fmt != '%') {
|
||
|
*str++ = *fmt++;
|
||
|
ret++;
|
||
|
continue;
|
||
|
}
|
||
|
|
||
|
switch (*fmt) {
|
||
|
case 'd':
|
||
|
default:
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return ret;
|
||
|
}
|
||
|
|
||
|
|