summaryrefslogtreecommitdiff
path: root/util.c
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2018-05-19 19:33:04 +0200
committerAaron Marcher <me@drkhsh.at>2018-05-19 19:44:02 +0200
commit9a07ac6f6d76e6a97429f5295df3b9c8e10b6b30 (patch)
treecf243529272e96e3d65b44afecd08cbb9f4e3005 /util.c
parentfc9345c4844c880f9e5b3d53c6514b2477687ee8 (diff)
Implement esnprintf() and make formatted calls more efficient
Within the components, snprintf() was unchecked and had inefficient calls in some places. We implement esnprintf() that does all the dirty laundry for us and use it exclusively now.
Diffstat (limited to 'util.c')
-rw-r--r--util.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/util.c b/util.c
index a7576b4..923d4df 100644
--- a/util.c
+++ b/util.c
@@ -48,6 +48,27 @@ die(const char *fmt, ...)
exit(1);
}
+int
+esnprintf(char *str, size_t size, const char *fmt, ...)
+{
+ va_list ap;
+ int ret;
+
+ va_start(ap, fmt);
+ ret = vsnprintf(str, size, fmt, ap);
+ va_end(ap);
+
+ if (ret < 0) {
+ warn("snprintf:");
+ return -1;
+ } else if ((size_t)ret >= size) {
+ warn("snprintf: Output truncated");
+ return -1;
+ }
+
+ return ret;
+}
+
const char *
bprintf(const char *fmt, ...)
{