diff options
author | June McEnroe <june@causal.agency> | 2020-07-30 14:37:46 -0400 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2020-07-30 14:37:46 -0400 |
commit | 4c1b1fc6a32061e86dedd6f34ed352cfda380feb (patch) | |
tree | e2710681e8d7101dea097e1a13fac6c91f6dc8ad /chat.h | |
parent | Remove dependency on libcrypto for compat (diff) | |
download | catgirl-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.h | 15 |
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 { |