From d3f6951dbe40eb04c44e406819ed4d07031bff9b Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 7 Dec 2020 03:24:43 +0500 Subject: [PATCH] Add example for --- .gitignore | 1 + Makefile.am | 5 +++++ examples/printf.c | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 examples/printf.c diff --git a/.gitignore b/.gitignore index 29763c8..8c48307 100644 --- a/.gitignore +++ b/.gitignore @@ -33,6 +33,7 @@ /tests/test*.trs /examples/cmdline +/examples/printf /tests/multiboot2_print1 /tests/multiboot2_print2 /tests/test_cmdline diff --git a/Makefile.am b/Makefile.am index 8f07ae2..e2d8794 100644 --- a/Makefile.am +++ b/Makefile.am @@ -10,6 +10,7 @@ AM_CFLAGS = \ lib_LIBRARIES = libkernaux.a TESTS = \ + examples/printf \ tests/test_printf \ tests/test_stdlib @@ -57,6 +58,10 @@ examples_cmdline_SOURCES = \ $(libkernaux_a_SOURCES) \ examples/cmdline.c +examples_printf_SOURCES = \ + $(libkernaux_a_SOURCES) \ + examples/printf.c + tests_multiboot2_print1_SOURCES = \ $(libkernaux_a_SOURCES) \ tests/multiboot2_print1.c diff --git a/examples/printf.c b/examples/printf.c new file mode 100644 index 0000000..7f16b3f --- /dev/null +++ b/examples/printf.c @@ -0,0 +1,26 @@ +#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; +} + +int main() +{ + memset(buffer, '\0', sizeof(buffer)); + buffer_index = 0; + kernaux_printf(my_putchar, "Hello, World!"); + assert(strcmp(buffer, "Hello, World!") == 0); + + return 0; +}