From e295e8f98f9c59aae6b1b511bad9979715a0fe7d Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Wed, 9 Jun 2021 14:31:15 -0400 Subject: Add seprintf Based on seprint(2) from Plan 9. I'm not sure if my return value exactly matches Plan 9's in the case of truncation. seprint(2) is described only as returning a pointer to the terminating '\0', but if it does so even in the case of truncation, it is awkward for the caller to detect. This implementation returns end in the truncation case, so that (ptr == end) indicates truncation. --- bounce.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/bounce.h b/bounce.h index a2265c8..2010d45 100644 --- a/bounce.h +++ b/bounce.h @@ -26,6 +26,7 @@ */ #include +#include #include #include #include @@ -49,6 +50,18 @@ typedef unsigned char byte; +static inline char *seprintf(char *ptr, char *end, const char *fmt, ...) + __attribute__((format(printf, 3, 4))); +static inline char *seprintf(char *ptr, char *end, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + int n = vsnprintf(ptr, end - ptr, fmt, ap); + va_end(ap); + if (n < 0) return NULL; + if (n > end - ptr) return end; + return ptr + n; +} + enum { MessageCap = 8191 + 512 }; enum { ParamCap = 15 }; -- cgit 1.4.1