summary refs log tree commit diff
path: root/chat.h
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-07-30 14:37:46 -0400
committerJune McEnroe <june@causal.agency>2020-07-30 14:37:46 -0400
commit4c1b1fc6a32061e86dedd6f34ed352cfda380feb (patch)
treee2710681e8d7101dea097e1a13fac6c91f6dc8ad /chat.h
parentRemove dependency on libcrypto for compat (diff)
downloadcatgirl-4c1b1fc6a32061e86dedd6f34ed352cfda380feb.tar.gz
catgirl-4c1b1fc6a32061e86dedd6f34ed352cfda380feb.zip
Replace catf with something that tracks len
Also the old catf would be broken with -DNDEBUG oops!
Diffstat (limited to '')
-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 {