os-k/kaleid/common/sprintf.c

76 lines
1.8 KiB
C
Raw Normal View History

2018-12-25 19:09:58 +01:00
//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Authors: spectral` //
// NeoX //
// //
// Desc: sprintf()-related functions //
//----------------------------------------------------------------------------//
2019-01-01 13:09:57 +01:00
#include <kaleid.h>
2018-12-25 19:09:58 +01:00
//
2019-01-14 14:31:49 +01:00
// Format str according to fmt using ellipsed arguments
2018-12-25 19:09:58 +01:00
//
int sprintf(char *str, const char *fmt, ...)
{
int ret;
va_list ap;
2019-01-14 14:31:49 +01:00
2018-12-25 19:09:58 +01:00
va_start(ap);
ret = vsnprintf(str, SIZE_T_MAX, fmt, ap);
va_end(ap);
2019-01-14 14:31:49 +01:00
2018-12-25 19:09:58 +01:00
return ret;
}
int vsprintf(char *str, const char *fmt, va_list ap)
{
return vsnprintf(str, SIZE_T_MAX, fmt, ap);
}
//
2019-01-14 14:31:49 +01:00
// (v)sprintf() but with a size limit: no more than n bytes are written in str
2018-12-25 19:09:58 +01:00
//
int snprintf(char *str, size_t n, const char *fmt, ...)
{
int ret;
va_list ap;
2019-01-14 14:31:49 +01:00
2018-12-25 19:09:58 +01:00
va_start(ap);
ret = vsnprintf(str, n, fmt, ap)
va_end(ap);
2019-01-14 14:31:49 +01:00
2018-12-25 19:09:58 +01:00
return ret;
}
int vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
{
int ret = 0;
2019-01-14 14:31:49 +01:00
//
// Go throught the format string
//
while (*fmt) {
2018-12-25 19:09:58 +01:00
if (*fmt != '%') {
2019-01-14 14:31:49 +01:00
//
// Even if we don't have any more room we still increase ret
//
if (ret++ < n) {
*str++ = *fmt++;
}
2018-12-25 19:09:58 +01:00
continue;
}
2019-01-14 14:31:49 +01:00
2018-12-25 19:09:58 +01:00
switch (*fmt) {
case 'd':
default:
break;
}
}
2019-01-14 14:31:49 +01:00
2018-12-25 19:09:58 +01:00
return ret;
}