summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibC/stdio.cpp
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2021-03-28 18:39:32 +0200
committerAndreas Kling <kling@serenityos.org>2021-03-28 18:39:32 +0200
commitfdd01a0a07d98581a2879ae7dbf49f37b41b23e4 (patch)
tree24c9edbfe83570190f1f43fe96f4c772883cae57 /Userland/Libraries/LibC/stdio.cpp
parent5f71bf0cc7de17d0aa2703aabf4e0cad5f000ae8 (diff)
downloadserenity-fdd01a0a07d98581a2879ae7dbf49f37b41b23e4.zip
LibC: Implement asprintf() and vasprintf()
These simply use StringBuilder::appendvf() internally which is not optimal in terms of heap allocations, but simple enough and I don't think they are performance sensitive functions anyway.
Diffstat (limited to 'Userland/Libraries/LibC/stdio.cpp')
-rw-r--r--Userland/Libraries/LibC/stdio.cpp24
1 files changed, 24 insertions, 0 deletions
diff --git a/Userland/Libraries/LibC/stdio.cpp b/Userland/Libraries/LibC/stdio.cpp
index 56e74dd5cb..99b19ca8f8 100644
--- a/Userland/Libraries/LibC/stdio.cpp
+++ b/Userland/Libraries/LibC/stdio.cpp
@@ -29,6 +29,7 @@
#include <AK/PrintfImplementation.h>
#include <AK/ScopedValueRollback.h>
#include <AK/StdLibExtras.h>
+#include <AK/String.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
@@ -880,6 +881,29 @@ int printf(const char* fmt, ...)
return ret;
}
+int vasprintf(char** strp, const char* fmt, va_list ap)
+{
+ StringBuilder builder;
+ builder.appendvf(fmt, ap);
+ VERIFY(builder.length() <= NumericLimits<int>::max());
+ int length = builder.length();
+ *strp = strdup(builder.to_string().characters());
+ return length;
+}
+
+int asprintf(char** strp, const char* fmt, ...)
+{
+ StringBuilder builder;
+ va_list ap;
+ va_start(ap, fmt);
+ builder.appendvf(fmt, ap);
+ va_end(ap);
+ VERIFY(builder.length() <= NumericLimits<int>::max());
+ int length = builder.length();
+ *strp = strdup(builder.to_string().characters());
+ return length;
+}
+
static void buffer_putch(char*& bufptr, char ch)
{
*bufptr++ = ch;