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
|
|
|
// //
|
2020-02-06 14:23:26 +01:00
|
|
|
// Copyright © 2018-2020 The OS/K Team //
|
2019-02-16 23:36:33 +01:00
|
|
|
// //
|
|
|
|
// 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-05-13 23:22:27 +02:00
|
|
|
#include <lib/buf.h>
|
2019-05-07 22:32:17 +02:00
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
//
|
2019-05-07 22:32:17 +02:00
|
|
|
// XXX
|
|
|
|
// For now vsnprintf won't support n > 4096
|
|
|
|
// vsnprintf() is implemented using the libbuf, which
|
|
|
|
// does not support dynamically sized buffers (yet)
|
|
|
|
// (because we need realloc() for that, and we don't have it yet)
|
|
|
|
// So for now we have to create a buffer of size n which is allocated
|
|
|
|
// hard, with no paging etc.
|
|
|
|
// Once libbuf is supports dynamic buffers, the only changes necessary
|
|
|
|
// will be to change the value below to and add a line to vsnprintf()
|
2019-03-08 09:00:55 +01:00
|
|
|
//
|
2020-02-11 11:16:24 +01:00
|
|
|
#define VSNPRINTF_MAX 4096
|
2019-01-21 15:00:04 +01:00
|
|
|
|
|
|
|
//
|
|
|
|
// Format str according to fmt using ellipsed arguments
|
|
|
|
//
|
2019-03-08 09:00:55 +01:00
|
|
|
size_t sprintf(char *str, const char *fmt, ...)
|
2019-01-21 15:00:04 +01:00
|
|
|
{
|
2020-02-11 11:16:24 +01:00
|
|
|
size_t ret;
|
2019-01-21 15:00:04 +01:00
|
|
|
va_list ap;
|
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
assert(!"sprintf() is deprecated, used snprintf() for more safety");
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
va_start(ap, fmt);
|
2019-05-07 22:32:17 +02:00
|
|
|
ret = vsnprintf(str, VSNPRINTF_MAX, fmt, ap);
|
2019-01-21 15:00:04 +01:00
|
|
|
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
|
|
|
{
|
2019-05-07 22:32:17 +02:00
|
|
|
return vsnprintf(str, VSNPRINTF_MAX, fmt, ap);
|
2019-01-21 15:00:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// (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
|
|
|
{
|
2020-02-11 11:16:24 +01:00
|
|
|
size_t ret;
|
2019-01-21 15:00:04 +01:00
|
|
|
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-05-07 22:32:17 +02:00
|
|
|
size_t vsnprintf(char *str, size_t n, const char *fmt, va_list ap)
|
|
|
|
{
|
2020-02-11 11:16:24 +01:00
|
|
|
error_t rc;
|
|
|
|
size_t ret, sz;
|
2019-05-07 23:16:56 +02:00
|
|
|
Buffer_t *buf = NULL;
|
2019-05-07 22:32:17 +02:00
|
|
|
|
|
|
|
assert(str && fmt);
|
|
|
|
|
|
|
|
if (n == 0) return 0;
|
|
|
|
if (n > VSNPRINTF_MAX) {
|
|
|
|
assert(!"Not yet...");
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2019-05-12 18:01:29 +02:00
|
|
|
rc = BOpenPureBuf(&buf, BS_WRONLY, n-1);
|
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
// n-1 to leave place for the '\0'
|
2019-05-12 18:01:29 +02:00
|
|
|
if (rc != EOK) {
|
2019-05-07 22:32:17 +02:00
|
|
|
goto fail;
|
|
|
|
}
|
2019-05-12 18:01:29 +02:00
|
|
|
|
2020-02-11 11:16:24 +01:00
|
|
|
sz = vbprintf(buf, fmt, ap);
|
2019-05-12 18:01:29 +02:00
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
// We don't mind EOFs, just just return how much was successfully written
|
2020-02-11 11:16:24 +01:00
|
|
|
if (sz == 0 && !(buf->flags & BF_EOF))
|
2019-05-12 18:01:29 +02:00
|
|
|
goto fail;
|
2019-05-07 22:32:17 +02:00
|
|
|
|
|
|
|
ret = (size_t)buf->wp - (size_t)buf->buf;
|
|
|
|
|
2020-02-11 11:16:24 +01:00
|
|
|
if (ret > 0)
|
2019-05-07 22:32:17 +02:00
|
|
|
memmove(str, (char *)buf->buf, ret);
|
|
|
|
|
|
|
|
str[ret++] = 0;
|
|
|
|
assert(ret <= n);
|
|
|
|
|
|
|
|
BCloseBuf(buf);
|
|
|
|
return ret;
|
2020-02-11 11:16:24 +01:00
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
fail:
|
2019-05-12 18:01:29 +02:00
|
|
|
KeStartPanic("vsnprintf() failure\nRC: %d\nbuf->flags & BF_EOF: %d\n",
|
|
|
|
rc, buf->flags & BF_EOF);
|
2019-05-07 22:32:17 +02:00
|
|
|
*str = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Old code
|
|
|
|
//
|
|
|
|
#if 0
|
|
|
|
|
2019-03-08 09:00:55 +01:00
|
|
|
// Size of the buffer is for convertions
|
2019-03-12 17:41:14 +01:00
|
|
|
#define CONVBUF 64
|
2019-03-08 09:00:55 +01:00
|
|
|
|
|
|
|
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 };
|
|
|
|
|
2019-03-12 17:41:14 +01:00
|
|
|
char c, *s;
|
|
|
|
int d;
|
|
|
|
uint u;
|
|
|
|
|
|
|
|
assert(str && fmt);
|
|
|
|
|
|
|
|
if (n == 0) return 0;
|
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-03-11 19:20:11 +01:00
|
|
|
ret++;
|
2019-01-21 15:00:04 +01:00
|
|
|
break;
|
|
|
|
}
|
2019-03-08 09:00:55 +01:00
|
|
|
|
|
|
|
// We fallthrough here from the "while (1)"
|
2019-03-12 17:41:14 +01:00
|
|
|
lflag = hflag = hhflag = altflag = 0;
|
2019-03-08 09:00:55 +01:00
|
|
|
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
|
|
|
|
2019-05-07 22:32:17 +02:00
|
|
|
#endif
|
|
|
|
|