about summary refs log tree commit diff
path: root/steprn.c
diff options
context:
space:
mode:
Diffstat (limited to 'steprn.c')
-rw-r--r--steprn.c23
1 files changed, 23 insertions, 0 deletions
diff --git a/steprn.c b/steprn.c
new file mode 100644
index 0000000..2ca0a27
--- /dev/null
+++ b/steprn.c
@@ -0,0 +1,23 @@
+#include <stdarg.h>
+#include <stdio.h>
+
+char *
+steprn(char *dst, char *end, const char *fmt, ...)
+{
+	if (dst >= end)
+		return end;
+
+	va_list ap;
+	va_start(ap, fmt);
+	int r = vsnprintf(dst, end - dst, fmt, ap);
+	va_end(ap);
+
+	if (r < 0) {
+		/* snprintf only fails for silly reasons:
+		   truncate what was written, behave as noop.  */
+		*dst = 0;
+		return dst;
+	}
+
+	return r > end - dst ? end : dst + r;
+}