diff options
Diffstat (limited to 'shared/utils.c')
-rw-r--r-- | shared/utils.c | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/shared/utils.c b/shared/utils.c index 9bf8cd8..99fcaa3 100644 --- a/shared/utils.c +++ b/shared/utils.c @@ -13,6 +13,7 @@ #include <netinet/ip.h> #include <netinet/tcp.h> #include <inttypes.h> +#include <stdarg.h> #include "utils.h" @@ -218,3 +219,36 @@ int strtou16_strict(const char *str, uint16_t *result) *result = val; return 0; } + +char *xsprintf(size_t *rlen, const char *fmt, ...) +{ + va_list ap; + int len; + char *str; + + va_start(ap, fmt); + len = vsnprintf(NULL, 0, fmt, ap); + va_end(ap); + + if (len < 0) + return NULL; + + len++; + str = zmalloc(len); + if (!str) + return NULL; + + va_start(ap, fmt); + len = vsnprintf(str, len, fmt, ap); + va_end(ap); + + if (len < 0) { + xfree(str); + return NULL; + } + + if (rlen) + *rlen = len; + + return str; +} |