|
From: Lasse Kärkkäi. <tr...@us...> - 2009-11-29 08:21:50
|
Module: performous
Branch: dance
Commit: abd07672ce3718a01f68cdd59744e1c92b792a26
Author: Lasse Karkkainen <tro...@tr...>
Date: Thu Nov 19 03:45:09 2009 +0200
Make execname return fs::path + other fixes to it
---
game/fs.cc | 2 +-
libs/plugin++/include/plugin++/execname.hpp | 4 +-
libs/plugin++/src/execname.cc | 38 +++++++++++++++-----------
3 files changed, 25 insertions(+), 19 deletions(-)
diff --git a/game/fs.cc b/game/fs.cc
index 0bd60b0..852529a 100644
--- a/game/fs.cc
+++ b/game/fs.cc
@@ -89,7 +89,7 @@ std::string getPath(fs::path const& filename) {
}
#endif
// Adding relative path from executable
- dirs.push_back(fs::path(plugin::execname()).parent_path().parent_path() / shareDir);
+ dirs.push_back(plugin::execname().parent_path().parent_path() / shareDir);
#ifndef _WIN32
// Adding XDG_DATA_DIRS
{
diff --git a/libs/plugin++/include/plugin++/execname.hpp b/libs/plugin++/include/plugin++/execname.hpp
index 03b7fe0..4c53315 100644
--- a/libs/plugin++/include/plugin++/execname.hpp
+++ b/libs/plugin++/include/plugin++/execname.hpp
@@ -1,9 +1,9 @@
#pragma once
-#include <string>
+#include <boost/filesystem.hpp>
namespace plugin {
/// Get the current executable name with path. Returns empty path if the name
/// cannot be found. May return absolute or relative paths.
- std::string execname();
+ boost::filesystem::path execname();
}
diff --git a/libs/plugin++/src/execname.cc b/libs/plugin++/src/execname.cc
index d647f26..6844d74 100644
--- a/libs/plugin++/src/execname.cc
+++ b/libs/plugin++/src/execname.cc
@@ -1,52 +1,58 @@
#include <plugin++/execname.hpp>
+namespace fs = boost::filesystem;
+
#if defined(_WIN32)
#include <windows.h>
-std::string plugin::execname() {
+fs::path plugin::execname() {
char buf[1024];
DWORD ret = GetModuleFileName(NULL, buf, sizeof(buf));
if (ret == 0 || ret == sizeof(buf)) return std::string();
return buf;
}
-#elif defined(__APPLE__) // Not tested
+#elif defined(__APPLE__)
#include <mach-o/dyld.h>
-std::string plugin::execname() {
+fs::path plugin::execname() {
char buf[1024];
uint32_t size = sizeof(buf);
int ret = _NSGetExecutablePath(buf, &size);
- if (ret != 0) return std::string();
+ if (ret != 0) return fs::path();
return buf;
}
#elif defined(sun) || defined(__sun)
#include <stdlib.h>
-std::string plugin::execname() {
- return getplugin::execname();
+fs::path plugin::execname() {
+ return getexecname();
}
#elif defined(__FreeBSD__)
#include <sys/sysctl.h>
-std::string plugin::execname() {
+fs::path plugin::execname() {
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);
+ size_t maxchars = sizeof(buf) - 1;
+ size_t size = maxchars;
sysctl(mib, 4, buf, &size, NULL, 0);
- if (size == 0 || size == sizeof(buf)) return std::string();
- return std::string(buf, size);
+ if (size == 0 || size >= maxchars) return fs::path();
+ buf[size] = '\0';
+ return buf;
}
#elif defined(__linux__)
#include <unistd.h>
-std::string plugin::execname() {
+fs::path plugin::execname() {
char buf[1024];
- ssize_t ret = readlink("/proc/self/exe", buf, sizeof(buf));
- if (ret == 0 || ret == sizeof(buf)) return std::string();
- return std::string(buf, ret);
+ ssize_t maxchars = sizeof(buf) - 1;
+ ssize_t size = readlink("/proc/self/exe", buf, sizeof(buf));
+ if (size <= 0 || size >= maxchars) return fs::path();
+ buf[size] = '\0';
+ return buf;
}
#else
-std::string plugin::execname() {
- return std::string();
+fs::path plugin::execname() {
+ return fs::path();
}
#endif
|