aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
Diffstat (limited to 'common')
-rw-r--r--common/stringop.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/common/stringop.c b/common/stringop.c
index 7fb3fe12..c503143a 100644
--- a/common/stringop.c
+++ b/common/stringop.c
@@ -1,5 +1,6 @@
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
+#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@@ -328,3 +329,35 @@ bool expand_path(char **path) {
wordfree(&p);
return true;
}
+
+char *vformat_str(const char *fmt, va_list args) {
+ char *str = NULL;
+ va_list args_copy;
+ va_copy(args_copy, args);
+
+ int len = vsnprintf(NULL, 0, fmt, args);
+ if (len < 0) {
+ sway_log_errno(SWAY_ERROR, "vsnprintf(\"%s\") failed", fmt);
+ goto out;
+ }
+
+ str = malloc(len + 1);
+ if (str == NULL) {
+ sway_log_errno(SWAY_ERROR, "malloc() failed");
+ goto out;
+ }
+
+ vsnprintf(str, len + 1, fmt, args_copy);
+
+out:
+ va_end(args_copy);
+ return str;
+}
+
+char *format_str(const char *fmt, ...) {
+ va_list args;
+ va_start(args, fmt);
+ char *str = vformat_str(fmt, args);
+ va_end(args);
+ return str;
+}