From ac35ca7947fd83ba09efa06e0a723a33f8fa4d19 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Thu, 30 Apr 2026 09:43:55 -0400 Subject: Move center_text function into utils.c/utils.h * Remove 'v' prefix * Print version the same way in the indexer --- src/cli/stasis/stasis_main.c | 34 ---------------------------- src/cli/stasis_indexer/stasis_indexer_main.c | 12 +++++++++- src/lib/core/include/utils.h | 1 + src/lib/core/utils.c | 33 +++++++++++++++++++++++++++ 4 files changed, 45 insertions(+), 35 deletions(-) (limited to 'src') diff --git a/src/cli/stasis/stasis_main.c b/src/cli/stasis/stasis_main.c index 58bbfa2..d152278 100644 --- a/src/cli/stasis/stasis_main.c +++ b/src/cli/stasis/stasis_main.c @@ -493,40 +493,6 @@ static void transfer_artifacts(struct Delivery *ctx) { } } -static char *center_text(const char *s, const size_t maxwidth) { - if (maxwidth < 2) { - SYSERROR("%s", "maximum width must be greater than 0"); - return NULL; - } - - if (maxwidth % 2 != 0) { - SYSERROR("maximum width (%zu) must be even", maxwidth); - return NULL; - } - - const size_t s_len = strlen(s); - if (s_len + 1 > maxwidth) { - SYSERROR("length of input string (%zu) exceeds maximum width (%zu)", s_len, maxwidth); - return NULL; - } - - char *result = calloc(maxwidth + 1, sizeof(*result)); - if (!result) { - SYSERROR("%s", "unable to allocate bytes for centered text string"); - return NULL; - } - const size_t middle = (maxwidth / 2) - s_len / 2; - size_t i = 0; - for (; i < middle; i++) { - result[i] = ' '; - } - result[i++] = 'v'; - strncpy(&result[i], s, maxwidth - middle - 1); - result[maxwidth] = '\0'; - - return result; -} - int main(int argc, char *argv[]) { struct Delivery ctx; struct Process proc = { diff --git a/src/cli/stasis_indexer/stasis_indexer_main.c b/src/cli/stasis_indexer/stasis_indexer_main.c index f42e686..0ef2a5f 100644 --- a/src/cli/stasis_indexer/stasis_indexer_main.c +++ b/src/cli/stasis_indexer/stasis_indexer_main.c @@ -303,7 +303,17 @@ int main(const int argc, char *argv[]) { struct Delivery ctx = {0}; - printf(BANNER, VERSION, AUTHOR); + char *version = center_text(VERSION, strlen(STASIS_BANNER_HEADER)); + if (!version) { + SYSERROR("%s", "version too long?"); + version = strdup(VERSION); + if (!version) { + SYSERROR("%s", "unable to allocate uncentered fallback version string"); + exit(1); + } + } + printf(BANNER, version, AUTHOR); + guard_free(version); indexer_init_dirs(&ctx, workdir); diff --git a/src/lib/core/include/utils.h b/src/lib/core/include/utils.h index 22835ee..3bd0785 100644 --- a/src/lib/core/include/utils.h +++ b/src/lib/core/include/utils.h @@ -490,4 +490,5 @@ int get_random_bytes(char *result, size_t maxlen); */ int non_format_len(const char *s); +char *center_text(const char *s, size_t maxwidth); #endif //STASIS_UTILS_H diff --git a/src/lib/core/utils.c b/src/lib/core/utils.c index 4f06d14..f18a2e5 100644 --- a/src/lib/core/utils.c +++ b/src/lib/core/utils.c @@ -1239,3 +1239,36 @@ int non_format_len(const char *s) { return len; } +char *center_text(const char *s, const size_t maxwidth) { + if (maxwidth < 2) { + SYSERROR("%s", "maximum width must be greater than 0"); + return NULL; + } + + if (maxwidth % 2 != 0) { + SYSERROR("maximum width (%zu) must be even", maxwidth); + return NULL; + } + + const size_t s_len = strlen(s); + if (s_len + 1 > maxwidth) { + SYSERROR("length of input string (%zu) exceeds maximum width (%zu)", s_len, maxwidth); + return NULL; + } + + char *result = calloc(maxwidth + 1, sizeof(*result)); + if (!result) { + SYSERROR("%s", "unable to allocate bytes for centered text string"); + return NULL; + } + const size_t middle = (maxwidth / 2) - s_len / 2; + size_t i = 0; + for (; i < middle; i++) { + result[i] = ' '; + } + strncpy(&result[i], s, maxwidth - middle - 1); + result[maxwidth] = '\0'; + + return result; +} + -- cgit