From 4c1b1fc6a32061e86dedd6f34ed352cfda380feb Mon Sep 17 00:00:00 2001 From: C. McEnroe Date: Thu, 30 Jul 2020 14:37:46 -0400 Subject: Replace catf with something that tracks len Also the old catf would be broken with -DNDEBUG oops! --- chat.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) (limited to 'chat.h') 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 { -- cgit 1.4.1-2-gfad0