Signed-off-by: Luc Van Oostenryck <luc.vanoostenryck@xxxxxxxxx> --- utils.c | 30 ++++++++++++++++++++++++++++++ utils.h | 18 ++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/utils.c b/utils.c index 4945e1ca1..094df3f9b 100644 --- a/utils.c +++ b/utils.c @@ -4,6 +4,8 @@ #include "utils.h" #include "allocate.h" #include <string.h> +#include <stdarg.h> +#include <stdio.h> void *xmemdup(const void *src, size_t len) @@ -15,3 +17,31 @@ char *xstrdup(const char *src) { return xmemdup(src, strlen(src) + 1); } + +char *xvasprintf(const char *fmt, va_list ap) +{ + va_list ap2; + char *str; + int n; + + va_copy(ap2, ap); + n = vsnprintf(NULL, 0, fmt, ap2) + 1; + va_end(ap2); + + str = __alloc_bytes(n); + vsnprintf(str, n, fmt, ap); + + return str; +} + +char *xasprintf(const char *fmt, ...) +{ + va_list ap; + char *str; + + va_start(ap, fmt); + str = xvasprintf(fmt, ap); + va_end(ap); + + return str; +} diff --git a/utils.h b/utils.h index 38749be29..7bd14f467 100644 --- a/utils.h +++ b/utils.h @@ -6,6 +6,7 @@ // ----------------------- #include <stddef.h> +#include <stdarg.h> /// // duplicate a memory buffer in a newly allocated buffer. @@ -22,4 +23,21 @@ void *xmemdup(const void *src, size_t len); // :func:`__alloc_bytes()`. char *xstrdup(const char *src); +/// +// printf to allocated string +// @fmt: the format followed by its arguments. +// @return: the allocated & formatted string. +// This function is similar to asprintf() but the resulting string +// is allocated with __alloc_bytes(). +char *xasprintf(const char *fmt, ...); + +/// +// vprintf to allocated string +// @fmt: the format +// @ap: the variadic arguments +// @return: the allocated & formatted string. +// This function is similar to asprintf() but the resulting string +// is allocated with __alloc_bytes(). +char *xvasprintf(const char *fmt, va_list ap); + #endif -- 2.17.1 -- To unsubscribe from this list: send the line "unsubscribe linux-sparse" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html