about summary refs log tree commit diff
path: root/steprn.c
diff options
context:
space:
mode:
authorLeah Neukirchen <leah@vuxu.org>2021-11-05 18:52:11 +0100
committerLeah Neukirchen <leah@vuxu.org>2021-11-05 18:52:46 +0100
commit75ea20675d012e692996bc6beae9149b0deb08b0 (patch)
treeab1569403e1c5f8e76f5f93f969a3335275c54a5 /steprn.c
downloadlibste-75ea20675d012e692996bc6beae9149b0deb08b0.tar.gz
libste-75ea20675d012e692996bc6beae9149b0deb08b0.tar.xz
libste-75ea20675d012e692996bc6beae9149b0deb08b0.zip
initial commit
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;
+}