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:
parent
a5654e98cb
commit
de6e1a02d4
3 changed files with 43 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -34,6 +34,7 @@
|
|||
|
||||
/examples/cmdline
|
||||
/examples/printf
|
||||
/examples/printf_va
|
||||
/tests/multiboot2_print1
|
||||
/tests/multiboot2_print2
|
||||
/tests/test_cmdline
|
||||
|
|
|
@ -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
37
examples/printf_va.c
Normal 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;
|
||||
}
|
Loading…
Add table
Reference in a new issue