From de6e1a02d43a3836f422913b9a85ebe093b38d62 Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 7 Dec 2020 05:27:42 +0500 Subject: [PATCH] Add example for "kernaux_printf_va" --- .gitignore | 1 + Makefile.am | 5 +++++ examples/printf_va.c | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 examples/printf_va.c diff --git a/.gitignore b/.gitignore index 8c48307..e8dfedd 100644 --- a/.gitignore +++ b/.gitignore @@ -34,6 +34,7 @@ /examples/cmdline /examples/printf +/examples/printf_va /tests/multiboot2_print1 /tests/multiboot2_print2 /tests/test_cmdline diff --git a/Makefile.am b/Makefile.am index e2d8794..9827e06 100644 --- a/Makefile.am +++ b/Makefile.am @@ -11,6 +11,7 @@ lib_LIBRARIES = libkernaux.a TESTS = \ examples/printf \ + examples/printf_va \ tests/test_printf \ tests/test_stdlib @@ -62,6 +63,10 @@ examples_printf_SOURCES = \ $(libkernaux_a_SOURCES) \ examples/printf.c +examples_printf_va_SOURCES = \ + $(libkernaux_a_SOURCES) \ + examples/printf_va.c + tests_multiboot2_print1_SOURCES = \ $(libkernaux_a_SOURCES) \ tests/multiboot2_print1.c diff --git a/examples/printf_va.c b/examples/printf_va.c new file mode 100644 index 0000000..72fad69 --- /dev/null +++ b/examples/printf_va.c @@ -0,0 +1,37 @@ +#include + +#include +#include +#include +#include + +#define BUFFER_SIZE 1024 + +static char buffer[BUFFER_SIZE]; +static unsigned int buffer_index; + +static void my_putchar(const char chr) +{ + if (buffer_index >= BUFFER_SIZE) abort(); + buffer[buffer_index++] = chr; +} + +static void my_printf(const char *const format, ...) +{ + va_list va; + va_start(va, format); + kernaux_printf_va(my_putchar, format, va); + va_end(va); +} + +int main() +{ + memset(buffer, '\0', sizeof(buffer)); + buffer_index = 0; + my_printf("Hello, %s! Session ID: %u.", "Alex", 123); + assert(strcmp(buffer, "Hello, Alex! Session ID: 123.") == 0); + + printf("OK!\n"); + + return 0; +}