summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/vsnprintf.c
diff options
context:
space:
mode:
authorKacper <kacper@mail.openlinux.dev>2025-12-07 20:10:31 +0100
committerKacper <kacper@mail.openlinux.dev>2025-12-07 20:10:31 +0100
commitfc00c656c96528112d05cf0edf8631bd5eaea446 (patch)
treea6e0e6c588191a8bd1c64afc3b7a258e3e66c236 /lib/libc/stdio/vsnprintf.c
Add build system scaffolding and libc headers
Diffstat (limited to 'lib/libc/stdio/vsnprintf.c')
-rw-r--r--lib/libc/stdio/vsnprintf.c45
1 files changed, 45 insertions, 0 deletions
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 <io.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <stdatomic.h>
+
+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;
+}