From e42004de94fc602bb1d5ab85763aa6ff35d6e57c Mon Sep 17 00:00:00 2001 From: Chewing_Bever Date: Thu, 16 Nov 2023 12:21:13 +0100 Subject: [PATCH] feat(landerctl): properly find config file --- config.mk | 2 +- landerctl/config.mk | 2 +- landerctl/src/main.c | 17 +++++++++++++++-- lsm/config.mk | 2 +- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/config.mk b/config.mk index 9b22a21..24e2149 100644 --- a/config.mk +++ b/config.mk @@ -16,7 +16,7 @@ LIB_DIRS = ./lsm/build # object file is also recompiled if only a header is changed. # -MP: generate a dummy target for every header file (according to the docs it # prevents some errors when removing header files) -CFLAGS = -MMD -MP -g +CFLAGS ?= -MMD -MP -g # When compiling release builds, these flags are better # CLAGS = -O3 diff --git a/landerctl/config.mk b/landerctl/config.mk index 611faaf..a9ec814 100644 --- a/landerctl/config.mk +++ b/landerctl/config.mk @@ -15,7 +15,7 @@ LIB_DIRS = # object file is also recompiled if only a header is changed. # -MP: generate a dummy target for every header file (according to the docs it # prevents some errors when removing header files) -CFLAGS = -MMD -MP -g +CFLAGS ?= -MMD -MP -g # When compiling release builds, these flags are better # CLAGS = -O3 diff --git a/landerctl/src/main.c b/landerctl/src/main.c index 7805635..f88008a 100644 --- a/landerctl/src/main.c +++ b/landerctl/src/main.c @@ -9,14 +9,27 @@ #include "landerctl.h" -const char *default_cfg_path = ".landerrc"; +const char *cfg_file_name = ".landerrc"; const char *usage = "%s [-SPFsv] arg [key]\n"; int main(int argc, char **argv) { landerctl_ctx ctx = {0}; char *err_msg = NULL; - switch (landerctl_cfg_parse(&ctx.cfg, default_cfg_path)) { + landerctl_cfg_err parse_res; + + const char *home_dir = getenv("HOME"); + + if (home_dir == NULL) { + parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_file_name); + } else { + char cfg_path[strlen(home_dir) + strlen(cfg_file_name) + 2]; + sprintf(cfg_path, "%s/%s", home_dir, cfg_file_name); + + parse_res = landerctl_cfg_parse(&ctx.cfg, cfg_path); + } + + switch (parse_res) { case landerctl_cfg_err_ok: break; case landerctl_cfg_err_not_found: diff --git a/lsm/config.mk b/lsm/config.mk index 4a7502c..5e75e26 100644 --- a/lsm/config.mk +++ b/lsm/config.mk @@ -12,4 +12,4 @@ INC_DIRS = $(PUB_INC_DIR) src/_include # object file is also recompiled if only a header is changed. # -MP: generate a dummy target for every header file (according to the docs it # prevents some errors when removing header files) -CFLAGS = -MMD -MP -g +CFLAGS ?= -MMD -MP -g