fxlibc/src/libc/stdio/vsnprintf.c

40 lines
1.0 KiB
C
Raw Normal View History

2021-05-09 17:34:00 +02:00
#include <stdio.h>
2020-09-17 19:27:01 +02:00
2020-10-14 12:07:29 +02:00
// internal depency
#include "internal/printf.h"
2020-10-14 12:07:29 +02:00
2020-09-17 19:27:01 +02:00
static void disp_char(struct printf_opt *opt, char n)
{
// Check write possibility
if (opt->buffer_cursor < opt->str_size - 1) {
opt->str[opt->buffer_cursor] = n;
opt->buffer_cursor = opt->buffer_cursor + 1;
}
}
static void disp_fflush(struct printf_opt *opt)
{
opt->str[opt->buffer_cursor] = '\0';
}
2020-10-14 15:18:10 +02:00
/*
** The functions vsnprintf() are equivalent to the snprintf() except that they
** are called with a va_list instead of a variable number of arguments. These
** functions do not call the va_end macro. Because they invoke the va_arg macro,
** the value of ap is undefined after the call.
*/
int vsnprintf(char *restrict str, size_t size, const char *restrict format,
va_list ap)
2020-09-17 19:27:01 +02:00
{
extern int printf_common(struct printf_opt *opt,
const char *restrict format);
2020-09-17 19:27:01 +02:00
struct printf_opt opt;
opt.str = str;
opt.str_size = size;
opt.disp_char = &disp_char;
opt.disp_fflush = &disp_fflush;
va_copy(opt.ap, ap);
return (printf_common(&opt, format) + 1);
}