summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorThomas Voss <thomas.voss@humanwave.nl> 2025-09-11 11:01:57 +0200
committerThomas Voss <thomas.voss@humanwave.nl> 2025-09-11 11:01:57 +0200
commit1de2789d95a8bfe5ef5acf095cea1875441728f8 (patch)
treef115ac1f4606a3d236be56668d81ecd30fbe0fa8 /main.c
parent397787df2bab42ca921425ddeba4ae9243277898 (diff)
Make buffer size dynamic
Diffstat (limited to 'main.c')
-rw-r--r--main.c16
1 files changed, 12 insertions, 4 deletions
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);
}
}