diff options
Diffstat (limited to 'common/stringop.c')
-rw-r--r-- | common/stringop.c | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/common/stringop.c b/common/stringop.c index 7fb3fe12..16d04917 100644 --- a/common/stringop.c +++ b/common/stringop.c | |||
@@ -1,5 +1,5 @@ | |||
1 | #define _POSIX_C_SOURCE 200809L | ||
2 | #include <ctype.h> | 1 | #include <ctype.h> |
2 | #include <stdarg.h> | ||
3 | #include <stdbool.h> | 3 | #include <stdbool.h> |
4 | #include <stdio.h> | 4 | #include <stdio.h> |
5 | #include <stdlib.h> | 5 | #include <stdlib.h> |
@@ -328,3 +328,35 @@ bool expand_path(char **path) { | |||
328 | wordfree(&p); | 328 | wordfree(&p); |
329 | return true; | 329 | return true; |
330 | } | 330 | } |
331 | |||
332 | char *vformat_str(const char *fmt, va_list args) { | ||
333 | char *str = NULL; | ||
334 | va_list args_copy; | ||
335 | va_copy(args_copy, args); | ||
336 | |||
337 | int len = vsnprintf(NULL, 0, fmt, args); | ||
338 | if (len < 0) { | ||
339 | sway_log_errno(SWAY_ERROR, "vsnprintf(\"%s\") failed", fmt); | ||
340 | goto out; | ||
341 | } | ||
342 | |||
343 | str = malloc(len + 1); | ||
344 | if (str == NULL) { | ||
345 | sway_log_errno(SWAY_ERROR, "malloc() failed"); | ||
346 | goto out; | ||
347 | } | ||
348 | |||
349 | vsnprintf(str, len + 1, fmt, args_copy); | ||
350 | |||
351 | out: | ||
352 | va_end(args_copy); | ||
353 | return str; | ||
354 | } | ||
355 | |||
356 | char *format_str(const char *fmt, ...) { | ||
357 | va_list args; | ||
358 | va_start(args, fmt); | ||
359 | char *str = vformat_str(fmt, args); | ||
360 | va_end(args); | ||
361 | return str; | ||
362 | } | ||