From a1b373e49a4b05ae059c494e54405732f152807d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 7 Dec 2020 03:30:15 +0500 Subject: [PATCH] Temporary implementation of %s formatter --- examples/printf.c | 4 ++-- src/printf.c | 16 +++++++++++++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/examples/printf.c b/examples/printf.c index 7f16b3f..54a4f08 100644 --- a/examples/printf.c +++ b/examples/printf.c @@ -19,8 +19,8 @@ int main() { memset(buffer, '\0', sizeof(buffer)); buffer_index = 0; - kernaux_printf(my_putchar, "Hello, World!"); - assert(strcmp(buffer, "Hello, World!") == 0); + kernaux_printf(my_putchar, "Hello, %s!", "Alex"); + assert(strcmp(buffer, "Hello, Alex!") == 0); return 0; } diff --git a/src/printf.c b/src/printf.c index 22c7014..278b49a 100644 --- a/src/printf.c +++ b/src/printf.c @@ -19,7 +19,21 @@ void kernaux_printf_va( for (const char *current_ptr = format; *current_ptr; ++current_ptr) { const char current = *current_ptr; - putchar(current); + if (current != '%') { + putchar(current); + continue; + } + + if (*(++current_ptr) == 's') { + const char *const arg = va_arg(va, const char*); + + for (const char *arg_ptr = arg; *arg_ptr; ++arg_ptr) { + putchar(*arg_ptr); + } + } + else { + break; + } } putchar('\0');