fxlibc/src/libc/stdio/vdprintf.c

46 lines
1.2 KiB
C

#include <fxlibc/stdio.h>
#include <fxlibc/unistd.h>
// internal depency
#include "internal/printf.h"
// FIXME:
// if the writte syscall do not return the same
// number of bytes that requested, stop the function !
static void disp_fflush(struct printf_opt *opt)
{
if (opt->buffer_cursor != 0) {
opt->counter += write(opt->fd, opt->buffer, opt->buffer_cursor);
opt->buffer_cursor = 0;
}
}
static void disp_char(struct printf_opt *opt, char n)
{
// Check if we should force flush the internal buffer
if (opt->buffer_cursor >= PRINTF_INTERNAL_BUFFER_SIZE)
disp_fflush(opt);
// Save char
opt->buffer[opt->buffer_cursor++] = n;
}
/*
** The functions vdprintf() are equivalent to the dprintf() 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 vdprintf(int fd, const char *restrict format, va_list ap)
{
extern int printf_common(struct printf_opt *opt,
const char *restrict format);
struct printf_opt opt;
opt.fd = fd;
opt.disp_char = &disp_char;
opt.disp_fflush = &disp_fflush;
va_copy(opt.ap, ap);
return (printf_common(&opt, format));
}