mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Tobias Waldekranz <tobias@waldekranz.com>
To: barebox@lists.infradead.org
Subject: [PATCH v2 2/7] vsprintf: Add rasprintf(): the reallocing string printf family
Date: Tue,  9 Sep 2025 15:18:35 +0200	[thread overview]
Message-ID: <20250909131843.2260573-3-tobias@waldekranz.com> (raw)
In-Reply-To: <20250909131843.2260573-1-tobias@waldekranz.com>

Generates the formatted version of the input and appends it to an
existing dynamically allocated string, growing it as necessary using
realloc().

Signed-off-by: Tobias Waldekranz <tobias@waldekranz.com>
---
 include/stdio.h  | 10 ++++++++++
 include/xfuncs.h |  4 ++++
 lib/vsprintf.c   | 35 +++++++++++++++++++++++++++++++++++
 lib/xfuncs.c     | 24 ++++++++++++++++++++++++
 4 files changed, 73 insertions(+)

diff --git a/include/stdio.h b/include/stdio.h
index cecb97ea1a..f3e430b3b3 100644
--- a/include/stdio.h
+++ b/include/stdio.h
@@ -21,6 +21,8 @@ int vscnprintf(char *buf, size_t size, const char *fmt, va_list args);
 int asprintf(char **strp, const char *fmt, ...) __printf(2, 3);
 char *bvasprintf(const char *fmt, va_list ap);
 int vasprintf(char **strp, const char *fmt, va_list ap);
+int vrasprintf(char **strp, const char *fmt, va_list ap);
+int rasprintf(char **strp, const char *fmt, ...) __printf(2, 3);
 #else
 static inline __printf(2, 3) int asprintf(char **strp, const char *fmt, ...)
 {
@@ -34,6 +36,14 @@ static inline int vasprintf(char **strp, const char *fmt, va_list ap)
 {
 	return -1;
 }
+static inline int vrasprintf(char **strp, const char *fmt, va_list ap)
+{
+	return -1;
+}
+static inline __printf(2, 3) int rasprintf(char **strp, const char *fmt, ...)
+{
+	return -1;
+}
 #endif
 
 #define basprintf xasprintf
diff --git a/include/xfuncs.h b/include/xfuncs.h
index eb4050f489..2cc2048bd5 100644
--- a/include/xfuncs.h
+++ b/include/xfuncs.h
@@ -19,6 +19,8 @@ void* xmemalign(size_t alignment, size_t bytes) __xalloc_size(2);
 void* xmemdup(const void *orig, size_t size) __returns_nonnull;
 char *xasprintf(const char *fmt, ...) __printf(1, 2) __returns_nonnull;
 char *xvasprintf(const char *fmt, va_list ap) __returns_nonnull;
+char *xvrasprintf(char *str, const char *fmt, va_list ap) __returns_nonnull;
+char *xrasprintf(char *str, const char *fmt, ...) __printf(2, 3) __returns_nonnull;
 
 wchar_t *xstrdup_wchar(const wchar_t *src);
 wchar_t *xstrdup_char_to_wchar(const char *src);
@@ -35,6 +37,8 @@ static inline void* xmemalign(size_t alignment, size_t bytes) { BUG(); }
 static inline void* xmemdup(const void *orig, size_t size) { BUG(); }
 static inline __printf(1, 2) char *xasprintf(const char *fmt, ...) { BUG(); }
 static inline char *xvasprintf(const char *fmt, va_list ap) { BUG(); }
+static inline char *xvrasprintf(char *str, const char *fmt, va_list ap) { BUG(); }
+static inline __printf(2, 3) char *xrasprintf(char *str, const char *fmt, ...) { BUG(); }
 
 static inline wchar_t *xstrdup_wchar(const wchar_t *src) { BUG(); }
 static inline wchar_t *xstrdup_char_to_wchar(const char *src) { BUG(); }
diff --git a/lib/vsprintf.c b/lib/vsprintf.c
index 409a8f02de..c7295c1067 100644
--- a/lib/vsprintf.c
+++ b/lib/vsprintf.c
@@ -1005,3 +1005,38 @@ int asprintf(char **strp, const char *fmt, ...)
 	return len;
 }
 EXPORT_SYMBOL(asprintf);
+
+int vrasprintf(char **strp, const char *fmt, va_list ap)
+{
+	int fmtlen, inlen = *strp ? strlen(*strp) : 0;
+	va_list aq;
+	char *p;
+
+	va_copy(aq, ap);
+	fmtlen = vsnprintf(NULL, 0, fmt, aq);
+	va_end(aq);
+
+	p = realloc(*strp, inlen + fmtlen + 1);
+	if (!p)
+		return -1;
+
+	vsnprintf(&p[inlen], fmtlen + 1, fmt, ap);
+
+	*strp = p;
+
+	return inlen + fmtlen;
+}
+EXPORT_SYMBOL(vrasprintf);
+
+int rasprintf(char **strp, const char *fmt, ...)
+{
+	va_list ap;
+	int len;
+
+	va_start(ap, fmt);
+	len = vrasprintf(strp, fmt, ap);
+	va_end(ap);
+
+	return len;
+}
+EXPORT_SYMBOL(rasprintf);
diff --git a/lib/xfuncs.c b/lib/xfuncs.c
index d4beecddf5..4c69451305 100644
--- a/lib/xfuncs.c
+++ b/lib/xfuncs.c
@@ -144,6 +144,30 @@ char *xasprintf(const char *fmt, ...)
 }
 EXPORT_SYMBOL(xasprintf);
 
+char *xvrasprintf(char *str, const char *fmt, va_list ap)
+{
+	int len;
+
+	len = vrasprintf(&str, fmt, ap);
+	if (len < 0)
+		enomem_panic(0);
+
+	return str;
+}
+EXPORT_SYMBOL(xvrasprintf);
+
+char *xrasprintf(char *str, const char *fmt, ...)
+{
+	va_list ap;
+
+	va_start(ap, fmt);
+	str = xvrasprintf(str, fmt, ap);
+	va_end(ap);
+
+	return str;
+}
+EXPORT_SYMBOL(xrasprintf);
+
 wchar_t *xstrdup_wchar(const wchar_t *s)
 {
 	wchar_t *p;
-- 
2.43.0




  parent reply	other threads:[~2025-09-09 16:02 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-09-09 13:18 [PATCH v2 0/7] dm: Initial work on a device mapper Tobias Waldekranz
2025-09-09 13:18 ` [PATCH v2 1/7] string: add strtok/strtokv Tobias Waldekranz
2025-09-09 13:18 ` Tobias Waldekranz [this message]
2025-09-09 13:18 ` [PATCH v2 3/7] dm: Add initial device mapper infrastructure Tobias Waldekranz
2025-09-09 13:18 ` [PATCH v2 4/7] dm: linear: Add linear target Tobias Waldekranz
2025-09-09 13:18 ` [PATCH v2 5/7] MIPS: qemu-malta_defconfig: Use largest possible relocation table Tobias Waldekranz
2025-09-09 13:18 ` [PATCH v2 6/7] test: self: dm: Add test of linear target Tobias Waldekranz
2025-09-09 13:18 ` [PATCH v2 7/7] commands: dmsetup: Basic command set for dm device management Tobias Waldekranz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250909131843.2260573-3-tobias@waldekranz.com \
    --to=tobias@waldekranz.com \
    --cc=barebox@lists.infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox