diff options
author | Thomas Voss <thomas.voss@humanwave.nl> | 2025-09-11 11:01:57 +0200 |
---|---|---|
committer | Thomas Voss <thomas.voss@humanwave.nl> | 2025-09-11 11:01:57 +0200 |
commit | 1de2789d95a8bfe5ef5acf095cea1875441728f8 (patch) | |
tree | f115ac1f4606a3d236be56668d81ecd30fbe0fa8 | |
parent | 397787df2bab42ca921425ddeba4ae9243277898 (diff) |
Make buffer size dynamic
-rw-r--r-- | main.c | 16 |
1 files changed, 12 insertions, 4 deletions
@@ -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); } } |