Skip to content

eBPF-based security platform

util: add vstrsprintf()

Daniel Borkmannf6030a4b2c37
diff --git a/include/shirudo/string.h b/include/shirudo/string.h
index 283b7143..39667b15 100644
--- a/include/shirudo/string.h
+++ b/include/shirudo/string.h
@@ -9,6 +9,7 @@
#define SHIRUDO_STRING_H
#include <inttypes.h>
+#include <stdarg.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
@@ -41,10 +42,13 @@ ssize_t strscpy(char *dst, const char *src, size_t size);
/*
* snprintf() into the size bytes at buf: the length written, or -E2BIG,
- * with buf left empty, when the output did not fit.
+ * with buf left empty, when the output did not fit. vstrsprintf() takes
+ * the arguments as a va_list.
*/
int strsprintf(char *buf, size_t size, const char *fmt, ...)
__attribute__((format(printf, 3, 4), warn_unused_result));
+int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
+ __attribute__((format(printf, 3, 0), warn_unused_result));
/*
* What the functions above replace, refused once the headers included
diff --git a/util/string.c b/util/string.c
index a56c15fc..3abb8c0e 100644
--- a/util/string.c
+++ b/util/string.c
@@ -108,17 +108,24 @@ ssize_t strscpy(char *dst, const char *src, size_t size)
return (ssize_t)len;
}
-int strsprintf(char *buf, size_t size, const char *fmt, ...)
+int vstrsprintf(char *buf, size_t size, const char *fmt, va_list ap)
{
- va_list ap;
- int n;
+ int n = vsnprintf(buf, size, fmt, ap);
- va_start(ap, fmt);
- n = vsnprintf(buf, size, fmt, ap);
- va_end(ap);
if (n >= 0 && (size_t)n < size)
return n;
if (size)
buf[0] = '\0';
return -E2BIG;
}
+
+int strsprintf(char *buf, size_t size, const char *fmt, ...)
+{
+ va_list ap;
+ int n;
+
+ va_start(ap, fmt);
+ n = vstrsprintf(buf, size, fmt, ap);
+ va_end(ap);
+ return n;
+}