Skip to content

Commit 4ff9af1

Browse files
authored
Merge pull request #579 from intjftw/executable_path
Finding CodeCompass binary path
2 parents 3d6f857 + c23afb7 commit 4ff9af1

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

util/include/util/filesystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ namespace util
1616
*/
1717
std::string binaryPathToInstallDir(const char* path);
1818

19+
/**
20+
* @brief Find the directory where a CodeCompass binary is being run from.
21+
* @return The absolute path of the directory where the binary is located.
22+
*/
23+
std::string findCurrentExecutableDir();
24+
1925
} // namespace util
2026
} // namespace cc
2127

util/src/filesystem.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
#include <cstdlib>
2+
#include <climits>
3+
#include <unistd.h>
24

35
#include <boost/filesystem.hpp>
46

@@ -50,5 +52,17 @@ std::string binaryPathToInstallDir(const char* path)
5052
throw std::runtime_error(std::string("Could not find ") + path + std::string("."));
5153
}
5254

55+
std::string findCurrentExecutableDir()
56+
{
57+
char exePath[PATH_MAX];
58+
ssize_t len = ::readlink("/proc/self/exe", exePath, sizeof(exePath));
59+
60+
if (len == -1 || len == sizeof(exePath))
61+
len = 0;
62+
63+
exePath[len] = '\0';
64+
return fs::path(exePath).parent_path().string();
65+
}
66+
5367
} // namespace util
5468
} // namespace cc

0 commit comments

Comments
 (0)