Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions util/include/util/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions util/src/filesystem.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#include <cstdlib>
#include <climits>
#include <unistd.h>

#include <boost/filesystem.hpp>

Expand Down Expand Up @@ -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