diff options
author | Panu Matilainen <pmatilai@redhat.com> | 2019-05-17 08:50:10 +0300 |
---|---|---|
committer | Panu Matilainen <pmatilai@redhat.com> | 2020-03-26 11:57:58 +0200 |
commit | 91756cd35626bc050e79ebad2db2b04dfb887358 (patch) | |
tree | 405092715d63e5bd52b8592779c2681318bcc8ae | |
parent | 2a516fb983e260328db31cf73088976b7cf84eed (diff) | |
download | rpm-91756cd35626bc050e79ebad2db2b04dfb887358.tar.gz |
Add a local vasprintf() clone rvasprintf() to match rasprintf()
Every now and then you need the v-version instead, and now rasprintf()
implementation comes practically free on top.
(cherry picked from commit d9dbd6ece3ba94d41c1b720565a26e50ac6757e8)
-rw-r--r-- | rpmio/rpmstring.c | 32 | ||||
-rw-r--r-- | rpmio/rpmstring.h | 6 |
2 files changed, 26 insertions, 12 deletions
diff --git a/rpmio/rpmstring.c b/rpmio/rpmstring.c index 0022b6075..ba13856cf 100644 --- a/rpmio/rpmstring.c +++ b/rpmio/rpmstring.c @@ -52,34 +52,42 @@ int rstrncasecmp(const char *s1, const char *s2, size_t n) return (int)(c1 - c2); } -/* - * Simple and stupid asprintf() clone. - * FIXME: write to work with non-C99 vsnprintf or check for one in configure. - */ -int rasprintf(char **strp, const char *fmt, ...) +int rvasprintf(char **strp, const char *fmt, va_list ap) { int n; - va_list ap; char * p = NULL; + va_list aq; if (strp == NULL) return -1; - va_start(ap, fmt); - n = vsnprintf(NULL, 0, fmt, ap); - va_end(ap); + va_copy(aq, ap); + n = vsnprintf(NULL, 0, fmt, aq); + va_end(aq); if (n >= -1) { size_t nb = n + 1; p = xmalloc(nb); - va_start(ap, fmt); - n = vsnprintf(p, nb, fmt, ap); - va_end(ap); + va_copy(aq, ap); + n = vsnprintf(p, nb, fmt, aq); + va_end(aq); } *strp = p; return n; } +int rasprintf(char **strp, const char *fmt, ...) +{ + int n; + va_list ap; + + va_start(ap, fmt); + n = rvasprintf(strp, fmt, ap); + va_end(ap); + + return n; +} + /* * Concatenate two strings with dynamically (re)allocated * memory what prevents static buffer overflows by design. diff --git a/rpmio/rpmstring.h b/rpmio/rpmstring.h index 16ce5ba66..093890975 100644 --- a/rpmio/rpmstring.h +++ b/rpmio/rpmstring.h @@ -8,6 +8,7 @@ #include <stddef.h> #include <string.h> +#include <stdarg.h> #include <rpm/rpmutil.h> @@ -145,6 +146,11 @@ int rstrncasecmp(const char *s1, const char * s2, size_t n) ; int rasprintf(char **strp, const char *fmt, ...) RPM_GNUC_PRINTF(2, 3); /** \ingroup rpmstring + * vasprintf() clone + */ +int rvasprintf(char **strp, const char *fmt, va_list ap); + +/** \ingroup rpmstring * Concatenate two strings with dynamically (re)allocated memory. * @param dest pointer to destination string * @param src source string |