Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/headers/ti/sprintf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The OS comes with an implementation of ANSI C89 `sprintf`, which can reduce the
boot_sprintf
------------

The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`. The minimum field width :code:`*`, precision :code:`.*`, alongside :code:`-+#0` and the space flag are also supported.
The following type specifiers are supported :code:`%s %c %d %i %u %o %x %X %p %n`. The minimum field width :code:`*`, precision :code:`.*`, alongside :code:`-+#0` and the space flag are also supported. However, the precision :code:`.*` field is ignored for integers.

All length modifiers :code:`hh h l ll j z t L` and floating point specifiers :code:`%f %g %e %a` are **not** supported.

Expand Down
2 changes: 1 addition & 1 deletion src/libc/boot_vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ int boot_vsnprintf(char *__restrict buffer, size_t count, const char *__restrict
int boot_snprintf(char *__restrict buffer, size_t count, const char *__restrict format, ...) {
va_list args;
va_start(args, format);
const int ret = vsnprintf(buffer, count, format, args);
const int ret = boot_vsnprintf(buffer, count, format, args);
va_end(args);
return ret;
}
Expand Down
50 changes: 26 additions & 24 deletions src/libc/nanoprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -982,15 +982,7 @@ int npf_vpprintf(npf_putc pc, void *pc_ctx, char const *format, va_list args) {
#undef NPF_EXTRACT
#undef NPF_WRITEBACK

int _printf_c(char const *format, ...) {
va_list va;
va_start(va, format);
int const rv = vprintf(format, va);
va_end(va);
return rv;
}

int _vsnprintf_c(char *buffer, size_t bufsz, char const *format, va_list vlist) {
int _vsnprintf_c(char *__restrict buffer, size_t bufsz, char const *__restrict format, va_list vlist) {
npf_bufputc_ctx_t bufputc_ctx;
bufputc_ctx.dst = buffer;
bufputc_ctx.len = bufsz;
Expand All @@ -1009,36 +1001,31 @@ int _vsnprintf_c(char *buffer, size_t bufsz, char const *format, va_list vlist)
return n;
}

int _snprintf_c(char *buffer, size_t bufsz, const char *format, ...) {
int _snprintf_c(char *__restrict buffer, size_t bufsz, const char *__restrict format, ...) {
va_list va;
va_start(va, format);
int const rv = vsnprintf(buffer, bufsz, format, va);
int const rv = _vsnprintf_c(buffer, bufsz, format, va);
va_end(va);
return rv;
}

int _vsprintf_c(char *buffer, const char *format, va_list vlist)
int _vsprintf_c(char *__restrict buffer, const char *__restrict format, va_list vlist)
{
return vsnprintf(buffer, (size_t)INT_MAX, format, vlist);
}

int _vprintf_c(const char *format, va_list vlist)
{
return npf_vpprintf(npf_putc_std, NULL, format, vlist);
return _vsnprintf_c(buffer, (size_t)INT_MAX, format, vlist);
}

int _sprintf_c(char *buffer, const char *format, ...)
int _sprintf_c(char *__restrict buffer, const char *__restrict format, ...)
{
va_list va;
va_start(va, format);
const int ret = vsnprintf(buffer, (size_t)INT_MAX, format, va);
const int ret = _vsnprintf_c(buffer, (size_t)INT_MAX, format, va);
va_end(va);
return ret;
}

int _vasprintf_c(char **__restrict p_str, const char *__restrict format, va_list vlist) {
*p_str = NULL;
int str_len = vsnprintf(NULL, 0, format, vlist);
int str_len = _vsnprintf_c(NULL, 0, format, vlist);
if (str_len <= 0) {
return str_len;
}
Expand All @@ -1048,7 +1035,7 @@ int _vasprintf_c(char **__restrict p_str, const char *__restrict format, va_list
// malloc failure
return -1;
}
int ret = vsnprintf(buf, buf_len, format, vlist);
int ret = _vsnprintf_c(buf, buf_len, format, vlist);
if (ret <= 0) {
free(buf);
return ret;
Expand All @@ -1060,11 +1047,12 @@ int _vasprintf_c(char **__restrict p_str, const char *__restrict format, va_list
int _asprintf_c(char **__restrict p_str, const char *__restrict format, ...) {
va_list va;
va_start(va, format);
const int ret = vasprintf(p_str, format, va);
const int ret = _vasprintf_c(p_str, format, va);
va_end(va);
return ret;
}

__attribute__((__always_inline__))
int _vfprintf_c(FILE* __restrict stream, const char* __restrict format, va_list vlist)
{
return npf_vpprintf(npf_fputc_std, (void*)stream, format, vlist);
Expand All @@ -1074,11 +1062,25 @@ int _fprintf_c(FILE* __restrict stream, const char* __restrict format, ...)
{
va_list va;
va_start(va, format);
const int ret = vfprintf(stream, format, va);
const int ret = _vfprintf_c(stream, format, va);
va_end(va);
return ret;
}

__attribute__((__always_inline__))
int _vprintf_c(const char *__restrict format, va_list vlist)
{
return npf_vpprintf(npf_putc_std, NULL, format, vlist);
}

int _printf_c(char const *__restrict format, ...) {
va_list va;
va_start(va, format);
int const rv = _vprintf_c(format, va);
va_end(va);
return rv;
}

#if NANOPRINTF_HAVE_GCC_WARNING_PRAGMAS
#pragma GCC diagnostic pop
#endif
Expand Down
27 changes: 24 additions & 3 deletions test/standalone/asprintf_fprintf/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ static char const * const test_3 =
"sprintf%%%% %%is unsafe";
static const int pos_3 = 20;

static const int pos_4 = 209;
static const int pos_4 = 212;

static char* buf = NULL;
static FILE* file = NULL;
Expand Down Expand Up @@ -153,7 +153,7 @@ int boot_sprintf_tests(void) {
"%%%.*s\n"
"Characters:\t%c %%\n"
"Integers:\n"
"%%*Decimal:\t%i %d %.6i %i %.0i %+i %i\n"
"%%*Decimal:\t%i %d %.6i %i %3u %+i %i\n"
"*%%Hexadecimal:\t%x %x %X %#x\n"
"%%*.*%%Octal:\t%o %#o %#o\n"
"Width trick: %*d \n",
Expand Down Expand Up @@ -256,7 +256,7 @@ int nano_tests(void) {
"%%%.*s\n"
"Characters:\t%c %%\n"
"Integers:\n"
"%%*Decimal:\t%i %d %.6i %i %.0i %+i %i\n"
"%%*Decimal:\t%i %d %.6i %i %3u %+i %i\n"
"*%%Hexadecimal:\t%x %x %X %#x\n"
"%%*.*%%Octal:\t%o %#o %#o\n"
"Width trick: %*d \n",
Expand Down Expand Up @@ -467,6 +467,27 @@ int run_tests(void) {
return 0;
}

#if 0
static void write_letter(char c) {
if (isgraph(c)) {
fputc(c, stdout);
return;
}
fputc('\\', stdout);
switch (c) {
case '\0': fputc('0', stdout); return;
case ' ': fputc('s', stdout); return;
case '\n': fputc('n', stdout); return;
case '\t': fputc('t', stdout); return;
case '\v': fputc('v', stdout); return;
case '\r': fputc('r', stdout); return;
case '\f': fputc('f', stdout); return;
case '\b': fputc('b', stdout); return;
default: printf("x%02X", (unsigned int)c); return;
}
}
#endif

int main(void)
{
os_ClrHome();
Expand Down
Loading