aboutsummaryrefslogtreecommitdiff
path: root/utils.h
diff options
context:
space:
mode:
authorKarel Kočí <karel.koci@nic.cz>2018-08-27 16:46:12 +0200
committerKarel Kočí <karel.koci@nic.cz>2018-08-27 16:54:40 +0200
commit5a7d5fa43536484508aad9d9553f64a33212311b (patch)
treee4ca1be7213a1460df47b63a2fbd9229d61d86d4 /utils.h
downloaduroot-5a7d5fa43536484508aad9d9553f64a33212311b.tar.gz
uroot-5a7d5fa43536484508aad9d9553f64a33212311b.tar.bz2
uroot-5a7d5fa43536484508aad9d9553f64a33212311b.zip
Initial commit
Diffstat (limited to 'utils.h')
-rw-r--r--utils.h17
1 files changed, 17 insertions, 0 deletions
diff --git a/utils.h b/utils.h
new file mode 100644
index 0000000..9e5984d
--- /dev/null
+++ b/utils.h
@@ -0,0 +1,17 @@
+#ifndef _UTILS_H_
+#define _UTILS_H_
+
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <stdarg.h>
+#include <alloca.h>
+
+// Compute the size needed (including \0) to format given message
+size_t printf_len(const char *msg, ...) __attribute__((format(printf, 1, 2)));
+// Like sprintf, but returs the string. Expects there's enough space.
+char *printf_into(char *dst, const char *msg, ...) __attribute__((format(printf, 2, 3)));
+// Like printf, but allocates the data on the stack with alloca and returns. It
+// uses the arguments multiple times, so beware of side effects.
+#define aprintf(...) printf_into(alloca(printf_len(__VA_ARGS__)), __VA_ARGS__)
+
+#endif /* _UTILS_H_ */