summary refs log tree commit diff
path: root/chat.h
diff options
context:
space:
mode:
authorC. McEnroe2020-07-30 14:37:46 -0400
committerC. McEnroe2020-07-30 14:37:46 -0400
commit4c1b1fc6a32061e86dedd6f34ed352cfda380feb (patch)
treee2710681e8d7101dea097e1a13fac6c91f6dc8ad /chat.h
parent09e6b147943ffa6d08039aa56d76aefddac03e27 (diff)
Replace catf with something that tracks len
Also the old catf would be broken with -DNDEBUG oops!
Diffstat (limited to 'chat.h')
-rw-r--r--chat.h15
1 files changed, 11 insertions, 4 deletions
diff --git a/chat.h b/chat.h
index fd8f81a..023baf6 100644
--- a/chat.h
+++ b/chat.h
@@ -43,13 +43,20 @@
 typedef unsigned uint;
 typedef unsigned char byte;
 
-static inline void __attribute__((format(printf, 3, 4)))
-catf(char *buf, size_t cap, const char *format, ...) {
-	size_t len = strnlen(buf, cap);
+struct Cat {
+	char *buf;
+	size_t cap;
+	size_t len;
+};
+static inline void __attribute__((format(printf, 2, 3)))
+catf(struct Cat *cat, const char *format, ...) {
 	va_list ap;
 	va_start(ap, format);
-	assert(0 <= vsnprintf(&buf[len], cap - len, format, ap));
+	int len = vsnprintf(&cat->buf[cat->len], cat->cap - cat->len, format, ap);
+	assert(len >= 0);
 	va_end(ap);
+	cat->len += len;
+	if (cat->len >= cat->cap) cat->len = cat->cap - 1;
 }
 
 enum Color {