From 4017d8237e56a531f880ada608db2a67f46c8096 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 3 Jan 2017 18:59:12 -0500 Subject: [PATCH] Add System::executable_path() --- src/core/System.cc | 24 +++++++++++++++++++++++- src/core/System.h | 5 ++++- src/jes.cc | 2 +- test/src/main.cc | 2 +- 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/core/System.cc b/src/core/System.cc index 14a7924..a999694 100644 --- a/src/core/System.cc +++ b/src/core/System.cc @@ -3,13 +3,15 @@ #include #include #include +#include unsigned long System::page_size; unsigned int System::page_size_log; unsigned long System::page_base_mask; unsigned long System::page_offset_mask; +static std::string Executable_Path; -bool System::init() +bool System::init(char * argv[]) { long sc_page_size = sysconf(_SC_PAGESIZE); if (sc_page_size < 0) @@ -41,6 +43,21 @@ bool System::init() return false; } +#if defined(PLATFORM_LINUX) + char link_buffer[1000]; + ssize_t len = readlink("/proc/self/exe", link_buffer, sizeof(link_buffer)); + if ((len > 0) && (len < (ssize_t)sizeof(link_buffer))) + { + Executable_Path = std::string(link_buffer, len); + } + else + { + return false; + } +#elif defined(PLATFORM_WINDOWS) + Executable_Path = argv[0]; +#endif + return true; } @@ -59,3 +76,8 @@ void System::free_pages(void * addr, int num) { (void)munmap(addr, num << System::page_size_log); } + +std::string System::executable_path() +{ + return Executable_Path; +} diff --git a/src/core/System.h b/src/core/System.h index 764bac9..49586d8 100644 --- a/src/core/System.h +++ b/src/core/System.h @@ -1,12 +1,15 @@ #ifndef SYSTEM_H #define SYSTEM_H +#include + class System { public: - static bool init(); + static bool init(char * argv[]); static void * alloc_pages(int num); static void free_pages(void * addr, int num); + static std::string executable_path(); static unsigned long page_size; static unsigned int page_size_log; diff --git a/src/jes.cc b/src/jes.cc index e3d0899..56b460a 100644 --- a/src/jes.cc +++ b/src/jes.cc @@ -6,7 +6,7 @@ int main(int argc, char * argv[]) { - if (!System::init()) + if (!System::init(argv)) { return 1; } diff --git a/test/src/main.cc b/test/src/main.cc index dc6d270..ecb91e3 100644 --- a/test/src/main.cc +++ b/test/src/main.cc @@ -6,7 +6,7 @@ int main(int argc, char * argv[]) { ::testing::InitGoogleTest(&argc, argv); - System::init(); + System::init(argv); mkdir("test/tmp", 0777); return RUN_ALL_TESTS(); }