lsnes rr2-β24
[lsnes.git] / src / library / running-executable.cpp
blob789e3966cd31b58a708139a6c5d58f77f511ec0d
1 #include "running-executable.hpp"
2 #include "directory.hpp"
3 #include <stdexcept>
5 namespace
7 #if __unix__
8 #include <unistd.h>
9 std::string readlink_generic(std::string path)
11 char buf[8192] = {0};
12 if(readlink(path.c_str(), buf, sizeof(buf)) > 0)
13 return buf;
14 throw std::runtime_error("Can't determine executable path");
16 #endif
18 #if __linux__
19 #include <unistd.h>
20 std::string _running_executable()
22 return readlink_generic("/proc/self/exe");
24 #elif __APPLE__
25 #include <mach-o/dyld.h>
26 std::string _running_executable()
28 char buf[8192];
29 uint32_t size = 8192;
30 _NSGetExecutablePath(buf, &size);
31 return buf;
33 #elif __WIN32__ || __WIN64__
34 #include "windows.h"
35 std::string _running_executable()
37 char buf[8192];
38 GetModuleFileName(NULL, buf, 8191);
39 return buf;
41 #elif __OpenBSD__
42 std::string _running_executable()
44 //TODO: This isn't doable?
45 throw std::runtime_error("running_executable() unsupported");
47 #elif __FreeBSD__
48 #include <sys/param.h>
49 #include <sys/sysctl.h>
50 std::string _running_executable()
52 char buf[8192];
53 int args[] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
54 size_t len = 8192;
55 if(sysctl(args, 4, buf, &len, NULL, 0) < 0)
56 throw std::runtime_error("Can't determine executable path");
57 return buf;
59 #elif __NetBSD__
60 std::string _running_executable()
62 return readlink_generic("/proc/curproc/exe");
64 #elif __sun__
65 #include <stdlib.h>
66 std::string _running_executable()
68 return getexecname();
70 #else
71 //Unsupported
72 std::string _running_executable()
74 throw std::runtime_error("running_executable() unsupported");
76 #endif
79 std::string running_executable()
81 return directory::absolute_path(_running_executable());