2019-01-21 15:00:04 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
// GNU GPL OS/K //
|
|
|
|
// //
|
2019-02-16 23:36:33 +01:00
|
|
|
// Desc: *s*printf() family //
|
2019-01-21 15:00:04 +01:00
|
|
|
// //
|
2019-02-16 23:36:33 +01:00
|
|
|
// //
|
|
|
|
// Copyright © 2018-2019 The OS/K Team //
|
|
|
|
// //
|
|
|
|
// This file is part of OS/K. //
|
|
|
|
// //
|
|
|
|
// OS/K is free software: you can redistribute it and/or modify //
|
|
|
|
// it under the terms of the GNU General Public License as published by //
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or //
|
|
|
|
// any later version. //
|
|
|
|
// //
|
|
|
|
// OS/K is distributed in the hope that it will be useful, //
|
|
|
|
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
|
|
|
// GNU General Public License for more details. //
|
|
|
|
// //
|
|
|
|
// You should have received a copy of the GNU General Public License //
|
|
|
|
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
|
2019-01-21 15:00:04 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
//
|
|
|
|
// TODO integer modifiers
|
|
|
|
//
|
|
|
|
|
2019-01-21 15:00:04 +01:00
|
|
|
#include <kalbase.h>
|
|
|
|
|
|
|
|
//
|
|
|
|
// Format str according to fmt using ellipsed arguments
|
|
|
|
//
|
2019-03-08 09:00:55 +01:00
|
|
|
// Standard is dumb so we won't follow it:
|
|
|
|
// 1) we return a size_t instead of int
|
|
|
|
// 2) for (v)snprintf we return the number of characters written
|
|
|
|
// instead of the number of characters that would have been written
|
|
|
|
// given large enough n
|
2019-01-21 15:00:04 +01:00
|
|
|
//
|
2019-03-08 09:00:55 +01:00
|
|
|
size_t sprintf(char *str, const char *fmt, ...)
|
2019-01-21 15:00:04 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list ap;
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
va_start(ap, fmt);
|
2019-01-21 15:00:04 +01:00
|
|
|
ret = vsnprintf(str, SIZE_T_MAX, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
size_t vsprintf(char *str, const char *fmt, va_list ap)
|
2019-01-21 15:00:04 +01:00
|
|
|
{
|
|
|
|
return vsnprintf(str, SIZE_T_MAX, fmt, ap);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// (v)sprintf() but with a size limit: no more than n bytes are written in str
|
|
|
|
// Always null-terminate str
|
|
|
|
//
|
2019-03-08 09:00:55 +01:00
|
|
|
size_t snprintf(char *str, size_t n, const char *fmt, ...)
|
2019-01-21 15:00:04 +01:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
va_list ap;
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
va_start(ap, fmt);
|
|
|
|
ret = vsnprintf(str, n, fmt, ap);
|
2019-01-21 15:00:04 +01:00
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
// Size of the buffer is for convertions
|
|
|
|
#define CONVBUF 4096
|
|
|
|
|
|
|
|
size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
|
2019-01-21 15:00:04 +01:00
|
|
|
{
|
2019-03-08 09:00:55 +01:00
|
|
|
size_t ret = 0;
|
|
|
|
bool lflag = 0, hflag = 0, hhflag = 0, altflag = 0;
|
|
|
|
|
|
|
|
int base;
|
|
|
|
char mod;
|
|
|
|
char convbuf[CONVBUF] = { 0 };
|
|
|
|
|
|
|
|
char c;
|
|
|
|
int d;
|
|
|
|
uint u;
|
|
|
|
char *s;
|
2019-01-21 15:00:04 +01:00
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
// For aesthetic reasons...
|
|
|
|
n--;
|
|
|
|
|
|
|
|
// Go through the format string
|
|
|
|
while (*fmt && ret < n) {
|
|
|
|
|
|
|
|
// Regular character
|
2019-01-21 15:00:04 +01:00
|
|
|
if (*fmt != '%') {
|
2019-03-08 09:00:55 +01:00
|
|
|
*str++ = *fmt++;
|
|
|
|
ret++;
|
2019-01-21 15:00:04 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
// Found a '%'
|
|
|
|
while (1) {
|
|
|
|
mod = *++fmt;
|
|
|
|
|
|
|
|
if (mod == '%') {
|
|
|
|
*str++ = '%';
|
|
|
|
ret++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == '#') {
|
|
|
|
altflag = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 'l' || mod == 'z' || mod == 't') {
|
|
|
|
// 'll'/'z'/'t' aliased to 'l'
|
|
|
|
lflag = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 'h') {
|
|
|
|
if (hflag) hhflag = 1;
|
|
|
|
else hflag = 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 'c') {
|
|
|
|
c = (char)va_arg(ap, int);
|
|
|
|
*str++ = c;
|
|
|
|
ret++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 's') {
|
|
|
|
s = va_arg(ap, char *);
|
|
|
|
|
|
|
|
while (*s && ret < n) {
|
|
|
|
*str++ = *s++;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 'i') {
|
|
|
|
mod = 'd';
|
|
|
|
goto numeric;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 'p') {
|
|
|
|
mod = 'x';
|
|
|
|
lflag = 1;
|
|
|
|
altflag = 1;
|
|
|
|
goto numeric;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mod == 'd' || mod == 'u' || mod == 'b' ||
|
|
|
|
mod == 'o' || mod == 'x' || mod == 'X') {
|
|
|
|
|
|
|
|
// Label to avoid some useless tests
|
|
|
|
numeric:
|
|
|
|
|
|
|
|
if (altflag && mod != 'd' && mod != 'u') {
|
|
|
|
// #d / #u is undefined behaviour
|
|
|
|
|
|
|
|
*str++ = '0';
|
|
|
|
if (++ret >= n) break;
|
|
|
|
|
|
|
|
if (mod != 'o') {
|
|
|
|
*str++ = mod;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// "%d" is a special snowflake, deal with it separately
|
|
|
|
if (mod == 'd') {
|
|
|
|
if (lflag) {
|
|
|
|
ltoa(va_arg(ap, long), convbuf, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
d = va_arg(ap, int);
|
|
|
|
if (hhflag) d &= 0xff; // char-ify
|
|
|
|
else if (hflag) d &= 0xffff; // short-ify
|
|
|
|
itoa(d, convbuf, 10);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// All unsigned mods
|
|
|
|
else {
|
|
|
|
base = (mod == 'u' ? 10 : (mod == 'b' ? 2 : (mod == 'o' ? 8 : 16)));
|
|
|
|
|
|
|
|
// Every other mod here is unsigned
|
|
|
|
if (lflag) {
|
|
|
|
ultoa(va_arg(ap, ulong), convbuf, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
u = va_arg(ap, uint);
|
|
|
|
if (hhflag) u &= 0xff; // char-ify
|
|
|
|
else if (hflag) u &= 0xffff; // short-ify
|
|
|
|
utoa(u, convbuf, base);
|
|
|
|
}
|
|
|
|
|
|
|
|
// "abcdef" => "ABCDEF"
|
|
|
|
if (mod == 'X') {
|
|
|
|
// Re-use s as an iterator for convbuf
|
|
|
|
s = convbuf;
|
|
|
|
while (*s) {
|
|
|
|
if (islower(*s))
|
|
|
|
*s = toupper(*s);
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s = convbuf;
|
|
|
|
// Convertions happened, now we write into str
|
|
|
|
while (*s && ret < n) {
|
|
|
|
*str++ = *s++;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're done dealing with this modifier
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unknown/unsupported modifier :|
|
|
|
|
*str++ = mod;
|
2019-01-21 15:00:04 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-03-08 09:00:55 +01:00
|
|
|
|
|
|
|
// We fallthrough here from the "while (1)"
|
|
|
|
fmt++;
|
2019-01-21 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
// Null-terminate no matter what
|
|
|
|
*str = 0;
|
2019-01-21 15:00:04 +01:00
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
return ret + 1;
|
|
|
|
}
|
2019-01-21 15:00:04 +01:00
|
|
|
|