From 1f9494979268b62ed4b4588fb7d9e790c0e12100 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Mon, 26 Oct 2026 22:32:52 +0100 Subject: replace all calls to sprintf with a format helper that advances a cursor. Also convert the meta-formatter (i.e. results printer) to this scheme --- ot_fmt.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 ot_fmt.c (limited to 'ot_fmt.c') diff --git a/ot_fmt.c b/ot_fmt.c new file mode 100644 index 0000000..9cab25f --- /dev/null +++ b/ot_fmt.c @@ -0,0 +1,42 @@ +/* This software was written by Dirk Engling + It is considered beerware. Prost. Skol. Cheers or whatever. + + $id$ */ + +/* System */ +#include +#include +#include + +/* Opentracker */ +#include "ot_fmt.h" + +void fmt_append(char **cursor, const char *end, const char *format, ...) { + va_list arguments; + size_t room; + int written; + + if (*cursor >= end) { + *cursor = (char *)end; + return; + } + + room = (size_t)(end - *cursor); + va_start(arguments, format); + written = vsnprintf(*cursor, room, format, arguments); + va_end(arguments); + + if (written < 0) + return; + *cursor = (size_t)written >= room ? (char *)end : *cursor + written; +} + +void fmt_bytes(char **cursor, const char *end, const void *bytes, size_t length) { + if (*cursor >= end || (size_t)(end - *cursor) < length) { + *cursor = (char *)end; + return; + } + + memcpy(*cursor, bytes, length); + *cursor += length; +} -- cgit v1.3