From fc00c656c96528112d05cf0edf8631bd5eaea446 Mon Sep 17 00:00:00 2001 From: Kacper Date: Sun, 7 Dec 2025 20:10:31 +0100 Subject: Add build system scaffolding and libc headers --- lib/libc/stdio/vsnprintf.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 lib/libc/stdio/vsnprintf.c (limited to 'lib/libc/stdio/vsnprintf.c') diff --git a/lib/libc/stdio/vsnprintf.c b/lib/libc/stdio/vsnprintf.c new file mode 100644 index 00000000..b7a59064 --- /dev/null +++ b/lib/libc/stdio/vsnprintf.c @@ -0,0 +1,45 @@ +#include +#include +#include +#include +#include + +int vsnprintf(char *restrict s, size_t n, const char *restrict format, + va_list ap) +{ + int r; + FILE f; + + if (n == 0) { + return 0; + } + + if (!s) { + return -1; + } + + memset(&f, 0, sizeof(f)); + f.fd = -1; + f.flags = O_WRONLY; + f.type = _IONBF; + atomic_flag_clear(&f.lock); + f.buf = s; + f.buf_size = n; + f.buf_len = 0; + f.buf_pos = 0; + f.eof = 0; + f.unget_cnt = 0; + f.offset = 0; + f.next = NULL; + + r = vfprintf(&f, format, ap); + + if (f.buf_len < n) { + s[f.buf_len] = '\0'; + } else if (n > 0) { + s[n - 1] = '\0'; + r = n - 1; + } + + return r; +} -- cgit v1.2.3