Add System::executable_path()

This commit is contained in:
Josh Holtrop 2017-01-03 18:59:12 -05:00
parent 7a9ee92eaa
commit 4017d8237e
4 changed files with 29 additions and 4 deletions

View File

@ -3,13 +3,15 @@
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
#include <stdint.h> #include <stdint.h>
#include <fcntl.h>
unsigned long System::page_size; unsigned long System::page_size;
unsigned int System::page_size_log; unsigned int System::page_size_log;
unsigned long System::page_base_mask; unsigned long System::page_base_mask;
unsigned long System::page_offset_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); long sc_page_size = sysconf(_SC_PAGESIZE);
if (sc_page_size < 0) if (sc_page_size < 0)
@ -41,6 +43,21 @@ bool System::init()
return false; 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; return true;
} }
@ -59,3 +76,8 @@ void System::free_pages(void * addr, int num)
{ {
(void)munmap(addr, num << System::page_size_log); (void)munmap(addr, num << System::page_size_log);
} }
std::string System::executable_path()
{
return Executable_Path;
}

View File

@ -1,12 +1,15 @@
#ifndef SYSTEM_H #ifndef SYSTEM_H
#define SYSTEM_H #define SYSTEM_H
#include <string>
class System class System
{ {
public: public:
static bool init(); static bool init(char * argv[]);
static void * alloc_pages(int num); static void * alloc_pages(int num);
static void free_pages(void * addr, int num); static void free_pages(void * addr, int num);
static std::string executable_path();
static unsigned long page_size; static unsigned long page_size;
static unsigned int page_size_log; static unsigned int page_size_log;

View File

@ -6,7 +6,7 @@
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
if (!System::init()) if (!System::init(argv))
{ {
return 1; return 1;
} }

View File

@ -6,7 +6,7 @@
int main(int argc, char * argv[]) int main(int argc, char * argv[])
{ {
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
System::init(); System::init(argv);
mkdir("test/tmp", 0777); mkdir("test/tmp", 0777);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
} }