summaryrefslogtreecommitdiff
path: root/misc
diff options
context:
space:
mode:
authorWerner Almesberger <werner@almesberger.net>2016-08-30 09:49:49 (GMT)
committerWerner Almesberger <werner@almesberger.net>2016-08-30 09:49:49 (GMT)
commit9a0fe02922549d804ba0e4a9c8535ce25f7930d9 (patch)
tree44caf72d27bd070597a7a846fbb9f44f40a92568 /misc
parentfe2d12a590aa637881c7f3fc53d3cca9e246c195 (diff)
downloadeeshow-9a0fe02922549d804ba0e4a9c8535ce25f7930d9.zip
eeshow-9a0fe02922549d804ba0e4a9c8535ce25f7930d9.tar.gz
eeshow-9a0fe02922549d804ba0e4a9c8535ce25f7930d9.tar.bz2
misc/util.h (alloc_printf): error-checking vasprintf wrapper; use it
Diffstat (limited to 'misc')
-rw-r--r--misc/util.c34
-rw-r--r--misc/util.h4
2 files changed, 38 insertions, 0 deletions
diff --git a/misc/util.c b/misc/util.c
new file mode 100644
index 0000000..0ca5e6b
--- /dev/null
+++ b/misc/util.c
@@ -0,0 +1,34 @@
+/*
+ * misc/util.c - Common utility functions
+ *
+ * Written 2016 by Werner Almesberger
+ * Copyright 2016 Werner Almesberger
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#define _GNU_SOURCE /* for vasprintf */
+#include <stdarg.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#include "misc/util.h"
+
+
+int alloc_printf(char **s, const char *fmt, ...)
+{
+ va_list ap;
+ int res;
+
+ va_start(ap, fmt);
+ res = vasprintf(s, fmt, ap);
+ va_end(ap);
+ if (res == -1) {
+ perror("vasprintf");
+ exit(1);
+ }
+ return res;
+}
diff --git a/misc/util.h b/misc/util.h
index b71a8ea..5c195f1 100644
--- a/misc/util.h
+++ b/misc/util.h
@@ -70,4 +70,8 @@ static inline bool strbegins(const char *s, const char *prefix)
return !strncmp(s, prefix, strlen(prefix));
}
+
+int alloc_printf(char **s, const char *fmt, ...)
+ __attribute__((format(printf, 2, 3)));
+
#endif /* !MISC_UTIL_H */