aboutsummaryrefslogtreecommitdiffstats
path: root/src/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/misc.c')
-rw-r--r--src/misc.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/misc.c b/src/misc.c
index 612ec82..e0aba02 100644
--- a/src/misc.c
+++ b/src/misc.c
@@ -18,6 +18,8 @@
*/
#include "misc.h"
+#include <stdarg.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
@@ -68,6 +70,31 @@ char *strdup_until(const char *s, const char *endchars)
}
}
+char *sprintf_new(const char *format, ...)
+{
+ va_list ap;
+ size_t buffer_size = strlen(format) + 32;
+ char *buffer = malloc(buffer_size);
+ if (buffer == NULL) {
+ return NULL;
+ }
+
+ while (1) {
+ va_start(ap, format);
+ size_t len = (size_t)vsnprintf(buffer, buffer_size, format, ap);
+ va_end(ap);
+ if (len < buffer_size) {
+ return buffer;
+ }
+ free(buffer);
+ buffer_size *= 2;
+ buffer = malloc(buffer_size);
+ if (buffer == NULL) {
+ return NULL;
+ }
+ }
+}
+
const char *my_basename(const char *path)
{
const char *p;