From 04f49a681919d19f62875b96fe8bd5dd0542c763 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 27 Nov 2024 21:33:10 +0200 Subject: [PATCH] LibC: Implement {,v}dprintf --- userspace/libraries/LibC/stdio.cpp | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/userspace/libraries/LibC/stdio.cpp b/userspace/libraries/LibC/stdio.cpp index 90f05a5655..fc7021c181 100644 --- a/userspace/libraries/LibC/stdio.cpp +++ b/userspace/libraries/LibC/stdio.cpp @@ -102,6 +102,15 @@ char* ctermid(char* buffer) return target; } +int dprintf(int fildes, const char* __restrict format, ...) +{ + va_list arguments; + va_start(arguments, format); + int ret = vdprintf(fildes, format, arguments); + va_end(arguments); + return ret; +} + int fclose(FILE* file) { ScopeLock _(file); @@ -811,6 +820,41 @@ int ungetc(int c, FILE* stream) return ungetc_unlocked(c, stream); } +int vdprintf(int fildes, const char* __restrict format, va_list arguments) +{ + struct print_info + { + int fildes; + size_t offset; + char buffer[512]; + }; + + print_info info { + .fildes = fildes, + .offset = 0, + .buffer = {}, + }; + + const int ret = printf_impl(format, arguments, + [](int c, void* _info) -> int + { + auto* info = static_cast(_info); + info->buffer[info->offset++] = c; + if (info->offset >= sizeof(info->buffer)) + { + write(info->fildes, info->buffer, info->offset); + info->offset = 0; + } + return 0; + }, static_cast(&info) + ); + + if (info.offset) + write(info.fildes, info.buffer, info.offset); + + return ret; +} + int vfprintf(FILE* file, const char* format, va_list arguments) { ScopeLock _(file);