|
From: Tapio V. <aa...@us...> - 2009-11-08 08:31:05
|
Module: performous
Branch: dance
Commit: 3be2d201398b4e6baba09495c66c17bfa07ab84c
Author: Lasse Karkkainen <tro...@tr...>
Date: Tue Nov 3 04:13:27 2009 +0200
Add a function for finding where the executable is
---
game/selfpath.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
game/selfpath.hh | 7 +++++++
2 files changed, 59 insertions(+), 0 deletions(-)
diff --git a/game/selfpath.cc b/game/selfpath.cc
new file mode 100644
index 0000000..69cfca3
--- /dev/null
+++ b/game/selfpath.cc
@@ -0,0 +1,52 @@
+#include "selfpath.hh"
+
+#if defined(_WIN32)
+#include <windows.h>
+boost::filesystem::path selfpath() {
+ char buf[1024];
+ DWORD ret = GetModuleFileName(NULL, buf, sizeof(buf));
+ if (ret == 0 || ret == sizeof(buf)) return boost::filesystem::path();
+ return buf;
+}
+#elif defined(__APPLE__) // Not tested
+#include <mach-o/dyld.h>
+boost::filesystem::path selfpath() {
+ char buf[1024];
+ uint32_t size = sizeof(buf);
+ int ret = _NSGetExecutablePath(buf, &size);
+ if (ret != 0) return boost::filesystem::path();
+ return buf;
+}
+#elif defined(sun) || defined(__sun)
+#include <stdlib.h>
+boost::filesystem::path selfpath() {
+ return getexecname();
+}
+#elif defined(__FreeBSD__)
+#include <sys/sysctl.h>
+boost::filesystem::path selfpath() {
+ int mib[4];
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_PROC;
+ mib[2] = KERN_PROC_PATHNAME;
+ mib[3] = -1;
+ char buf[1024];
+ size_t size = sizeof(buf);
+ sysctl(mib, 4, buf, &size, NULL, 0);
+ if (size == 0 || size == sizeof(buf)) return boost::filesystem::path();
+ return std::string(buf, size);
+}
+#elif defined(__linux__)
+#include <unistd.h>
+boost::filesystem::path selfpath() {
+ char buf[1024];
+ ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf));
+ if (ret == 0 || ret == sizeof(buf)) return boost::filesystem::path();
+ return std::string(buf, ret);
+}
+#else
+boost::filesystem::path selfpath() {
+ return boost::filesystem::path();
+}
+#endif
+
diff --git a/game/selfpath.hh b/game/selfpath.hh
new file mode 100644
index 0000000..5acbd12
--- /dev/null
+++ b/game/selfpath.hh
@@ -0,0 +1,7 @@
+#pragma once
+#include <boost/filesystem.hpp>
+
+/// Get the current executable name with path. Returns empty path if the name
+/// cannot be found. May return absolute or relative paths.
+boost::filesystem::path selfpath();
+
|