From 1de2789d95a8bfe5ef5acf095cea1875441728f8 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Thu, 11 Sep 2025 11:01:57 +0200 Subject: Make buffer size dynamic --- main.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'main.c') diff --git a/main.c b/main.c index b7c7b0d..28066d9 100644 --- a/main.c +++ b/main.c @@ -107,11 +107,19 @@ main(int argc, char **argv) if (putenv(argv[i]) == -1) warn(_("failed to set the timezone")); } else { - char buf[256]; /* TODO: Make dynamic */ + static char *buf; + static size_t bufsz = 1024; + if (buf == nullptr && (buf = malloc(bufsz)) == nullptr) + err(EXIT_FAILURE, "malloc"); + struct tm *tm = localtime(&now.tv_sec); - size_t n = strftime(buf, sizeof(buf), argv[i], tm); - if (n == 0) - warnx(_("buffer too small")); + size_t n = strftime(buf, bufsz, argv[i], tm); + while (n == 0) { + bufsz *= 2; + if ((buf = realloc(buf, bufsz)) == nullptr) + err(EXIT_FAILURE, "malloc"); + n = strftime(buf, bufsz, argv[i], tm); + } fputs(buf, stdout); } } -- cgit v1.2.3