summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Kernel/kprintf.cpp22
-rw-r--r--Userland/Libraries/LibC/stdio.cpp31
2 files changed, 21 insertions, 32 deletions
diff --git a/Kernel/kprintf.cpp b/Kernel/kprintf.cpp
index 669a95245b..ae88d36af2 100644
--- a/Kernel/kprintf.cpp
+++ b/Kernel/kprintf.cpp
@@ -123,26 +123,24 @@ int sprintf(char* buffer, const char* fmt, ...)
return ret;
}
-static size_t __vsnprintf_space_remaining;
-ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
-{
- if (__vsnprintf_space_remaining) {
- *bufptr++ = ch;
- --__vsnprintf_space_remaining;
- }
-}
-
int snprintf(char* buffer, size_t size, const char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
+ size_t space_remaining = 0;
if (size) {
- __vsnprintf_space_remaining = size - 1;
+ space_remaining = size - 1;
} else {
- __vsnprintf_space_remaining = 0;
+ space_remaining = 0;
}
+ auto sized_buffer_putch = [&](char*& bufptr, char ch) {
+ if (space_remaining) {
+ *bufptr++ = ch;
+ --space_remaining;
+ }
+ };
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
- if (__vsnprintf_space_remaining) {
+ if (space_remaining) {
buffer[ret] = '\0';
} else if (size > 0) {
buffer[size - 1] = '\0';
diff --git a/Userland/Libraries/LibC/stdio.cpp b/Userland/Libraries/LibC/stdio.cpp
index 4fbd70e8aa..bc9091cd27 100644
--- a/Userland/Libraries/LibC/stdio.cpp
+++ b/Userland/Libraries/LibC/stdio.cpp
@@ -870,17 +870,10 @@ ALWAYS_INLINE void stdout_putch(char*&, char ch)
putchar(ch);
}
-static FILE* __current_stream = nullptr;
-ALWAYS_INLINE static void stream_putch(char*&, char ch)
-{
- fputc(ch, __current_stream);
-}
-
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vfprintf.html
int vfprintf(FILE* stream, const char* fmt, va_list ap)
{
- __current_stream = stream;
- return printf_internal(stream_putch, nullptr, fmt, ap);
+ return printf_internal([stream](auto, char ch) { fputc(ch, stream); }, nullptr, fmt, ap);
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html
@@ -957,25 +950,23 @@ int sprintf(char* buffer, const char* fmt, ...)
return ret;
}
-static size_t __vsnprintf_space_remaining;
-ALWAYS_INLINE void sized_buffer_putch(char*& bufptr, char ch)
-{
- if (__vsnprintf_space_remaining) {
- *bufptr++ = ch;
- --__vsnprintf_space_remaining;
- }
-}
-
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/vsnprintf.html
int vsnprintf(char* buffer, size_t size, const char* fmt, va_list ap)
{
+ size_t space_remaining = 0;
if (size) {
- __vsnprintf_space_remaining = size - 1;
+ space_remaining = size - 1;
} else {
- __vsnprintf_space_remaining = 0;
+ space_remaining = 0;
}
+ auto sized_buffer_putch = [&](char*& bufptr, char ch) {
+ if (space_remaining) {
+ *bufptr++ = ch;
+ --space_remaining;
+ }
+ };
int ret = printf_internal(sized_buffer_putch, buffer, fmt, ap);
- if (__vsnprintf_space_remaining) {
+ if (space_remaining) {
buffer[ret] = '\0';
} else if (size > 0) {
buffer[size - 1] = '\0';