diff --git a/util/include/util/filesystem.h b/util/include/util/filesystem.h index daced7d5d..7454b16c5 100644 --- a/util/include/util/filesystem.h +++ b/util/include/util/filesystem.h @@ -16,6 +16,12 @@ namespace util */ std::string binaryPathToInstallDir(const char* path); +/** + * @brief Find the directory where a CodeCompass binary is being run from. + * @return The absolute path of the directory where the binary is located. + */ +std::string findCurrentExecutableDir(); + } // namespace util } // namespace cc diff --git a/util/src/filesystem.cpp b/util/src/filesystem.cpp index 62bef5156..d18c16b8d 100644 --- a/util/src/filesystem.cpp +++ b/util/src/filesystem.cpp @@ -1,4 +1,6 @@ #include +#include +#include #include @@ -50,5 +52,17 @@ std::string binaryPathToInstallDir(const char* path) throw std::runtime_error(std::string("Could not find ") + path + std::string(".")); } +std::string findCurrentExecutableDir() +{ + char exePath[PATH_MAX]; + ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath)); + + if (len == -1 || len == sizeof(exePath)) + len = 0; + + exePath[len] = '\0'; + return fs::path(exePath).parent_path().string(); +} + } // namespace util } // namespace cc