1
0
Fork 0
mirror of https://github.com/tailix/libkernaux.git synced 2025-02-24 15:55:41 -05:00

Add example for "kernaux_printf_va"

This commit is contained in:
Alex Kotov 2020-12-07 05:27:42 +05:00
parent a5654e98cb
commit de6e1a02d4
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
3 changed files with 43 additions and 0 deletions

1
.gitignore vendored
View file

@ -34,6 +34,7 @@
/examples/cmdline
/examples/printf
/examples/printf_va
/tests/multiboot2_print1
/tests/multiboot2_print2
/tests/test_cmdline

View file

@ -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

37
examples/printf_va.c Normal file
View file

@ -0,0 +1,37 @@
#include <kernaux/printf.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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;
}