aboutsummaryrefslogtreecommitdiff
path: root/steprn.c
blob: 2ca0a2796d2f44b887334f72ead054602f8816c6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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;
}