From fa4fec5f81df43539907f27e30124cc17d9876bd Mon Sep 17 00:00:00 2001 From: trilader Date: Thu, 26 Nov 2020 19:41:48 +0100 Subject: [PATCH] Look for configuration file next to the executable not in CWD --- main.cpp | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/main.cpp b/main.cpp index 44cc8f3..ad47398 100644 --- a/main.cpp +++ b/main.cpp @@ -13,6 +13,7 @@ #include #include +#include #include #include @@ -441,16 +442,31 @@ bool save_json(const std::string &filename, const json &data) { } } +static std::string get_module_path() { + std::string str; + char *exe = realpath("/proc/self/exe", nullptr); + if(exe) { + str = std::string(exe); + free(exe); + exe = nullptr; + } else { + perror("realpath"); + } + exe = dirname((char*)str.c_str()); + + return std::string(exe); +} int main() { + const std::string module_path = get_module_path(); user_home = std::string(std::getenv("HOME")); std::string database_filename = user_home + "/.local/share/yttui.db"; - std::vector config_locations{user_home + "/.config/", "./"}; curl_global_init(CURL_GLOBAL_ALL); tp_init(); + const std::vector config_locations{user_home + "/.config", module_path}; std::string config_file; nlohmann::json config; for(const std::string &location: config_locations) {