From 96a628da0db3ba733a0cb7facab9e6078f2aa856 Mon Sep 17 00:00:00 2001 From: Sigmanificient Date: Sat, 20 Apr 2024 00:38:17 +0200 Subject: [PATCH] Use shared macro for print_version --- commons.mk | 2 +- src/echo/echo.c | 5 +---- src/ls/ls_main.c | 11 +++++------ src/rmdir/rmdir.c | 5 +---- src/version_info.h | 13 +++++++++++++ 5 files changed, 21 insertions(+), 15 deletions(-) create mode 100644 src/version_info.h diff --git a/commons.mk b/commons.mk index 1193c31..d9a1baf 100644 --- a/commons.mk +++ b/commons.mk @@ -4,7 +4,7 @@ CFLAGS := @$/compile_flags.txt CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -Wp,-U_FORTIFY_SOURCE -CFLAGS += -iquote src +CFLAGS += -iquote $/src LDFLAGS := -fwhole-program -flto LDFLAGS += -Wl,--gc-sections diff --git a/src/echo/echo.c b/src/echo/echo.c index e6bc26d..8038e12 100644 --- a/src/echo/echo.c +++ b/src/echo/echo.c @@ -7,10 +7,7 @@ #define VERSION "1.0.0" #define AUTHOR "Hoorad Farrokh (proh14)" -#define print_version() \ - do { \ - printf("%s\nversion: %s\nby: %s\n", NAME, VERSION, AUTHOR); \ - } while (0) +#include "version_info.h" int isoctal(int c) { return (c >= '0' && c <= '7'); } diff --git a/src/ls/ls_main.c b/src/ls/ls_main.c index 50b8ceb..d50813a 100644 --- a/src/ls/ls_main.c +++ b/src/ls/ls_main.c @@ -8,10 +8,7 @@ #define VERSION "1.0.0" #define AUTHOR "Yohann Boniface (Sigmanificient)" -#define print_version() \ - do { \ - printf("%s\nversion: %s\nby: %s\n", NAME, VERSION, AUTHOR); \ - } while (0) +#include "version_info.h" static const char FLAGLIST[] = "alRdrt"; static char DEFAULT_LOCATION[] = "."; @@ -62,8 +59,10 @@ int main(int argc, char **argv) { int err = 0; for (int i = 0; argv[i] != NULL; i++) - if (!strcmp(argv[i], "--version")) - return printf(VERSION), EXIT_SUCCESS; + if (!strcmp(argv[i], "--version")) { + print_version(); + return EXIT_SUCCESS; + } flags = compose_flaglist(argc, argv); db.entries = malloc(db.size * sizeof(*db.entries)); if (db.entries == NULL) diff --git a/src/rmdir/rmdir.c b/src/rmdir/rmdir.c index 216ed4f..fcb715f 100644 --- a/src/rmdir/rmdir.c +++ b/src/rmdir/rmdir.c @@ -10,10 +10,7 @@ #define VERSION "1.0.0" #define AUTHOR "tim-tm" -#define print_version() \ - do { \ - printf("%s\nversion: %s\nby: %s\n", NAME, VERSION, AUTHOR); \ - } while (0) +#include "version_info.h" bool ignore_fail = false; bool parents = false; diff --git a/src/version_info.h b/src/version_info.h new file mode 100644 index 0000000..9a911f9 --- /dev/null +++ b/src/version_info.h @@ -0,0 +1,13 @@ +#ifndef VERSION_INFO_H +#define VERSION_INFO_H + +#if !defined(NAME) || !defined(VERSION) || !defined(AUTHOR) +#error "missing NAME, VERSION or AUTHOR definition" +#endif + +#define print_version() \ + do { \ + printf("%s\nversion: %s\nby: %s\n", NAME, VERSION, AUTHOR); \ + } while (0) + +#endif