-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
152 additions
and
146 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
#include <bee/error.h> | ||
#include <bee/sys/path.h> | ||
#include <unistd.h> | ||
|
||
#if defined(__FreeBSD__) | ||
# include <sys/param.h> | ||
# include <sys/sysctl.h> | ||
#elif defined(__OpenBSD__) | ||
# include <sys/sysctl.h> | ||
#endif | ||
|
||
namespace bee::sys { | ||
#if defined(__FreeBSD__) | ||
path_expected exe_path() noexcept { | ||
int name[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; | ||
char exe[1024]; | ||
size_t length = 1024; | ||
int error = sysctl(name, 4, exe, &length, NULL, 0); | ||
if (error < 0 || length <= 1) { | ||
return unexpected<std::string>(error::sys_errmsg("exe_path")); | ||
} | ||
return fs::path(exe, exe + length - 1); | ||
} | ||
#elif defined(__OpenBSD__) | ||
path_expected exe_path() noexcept { | ||
int name[] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV }; | ||
size_t argc; | ||
if (sysctl(name, 4, NULL, &argc, NULL, 0) < 0) { | ||
return unexpected<std::string>(error::sys_errmsg("exe_path")); | ||
} | ||
const char** argv = (const char**)malloc(argc); | ||
if (!argv) { | ||
return unexpected<std::string>(error::sys_errmsg("exe_path")); | ||
} | ||
if (sysctl(name, 4, argv, &argc, NULL, 0) < 0) { | ||
return unexpected<std::string>(error::sys_errmsg("exe_path")); | ||
} | ||
return fs::path(argv[0]); | ||
} | ||
#else | ||
path_expected exe_path() noexcept { | ||
std::error_code ec; | ||
auto res = fs::read_symlink("/proc/self/exe", ec); | ||
if (ec) { | ||
return unexpected<std::string>(error::sys_errmsg("exe_path")); | ||
} | ||
return res; | ||
} | ||
#endif | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
#include <bee/error.h> | ||
#include <bee/sys/path.h> | ||
#include <unistd.h> | ||
|
||
namespace bee::sys { | ||
path_expected exe_path() noexcept { | ||
std::error_code ec; | ||
auto res = fs::read_symlink("/proc/self/exe", ec); | ||
if (ec) { | ||
return unexpected<std::string>(error::sys_errmsg("exe_path")); | ||
} | ||
return res; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#include <bee/sys/path.h> | ||
#include <bee/utility/dynarray.h> | ||
#include <mach-o/dyld.h> | ||
|
||
namespace bee::sys { | ||
path_expected exe_path() noexcept { | ||
uint32_t path_len = 0; | ||
_NSGetExecutablePath(0, &path_len); | ||
if (path_len <= 1) { | ||
return unexpected<std::string>("_NSGetExecutablePath failed."); | ||
} | ||
dynarray<char> buf(path_len); | ||
int rv = _NSGetExecutablePath(buf.data(), &path_len); | ||
if (rv != 0) { | ||
return unexpected<std::string>("_NSGetExecutablePath failed."); | ||
} | ||
return fs::path(buf.data(), buf.data() + path_len - 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include <bee/sys/path.h> | ||
#include <dlfcn.h> | ||
|
||
namespace bee::sys { | ||
static path_expected dll_path(void* module_handle) noexcept { | ||
::Dl_info dl_info; | ||
dl_info.dli_fname = 0; | ||
const int ret = ::dladdr(module_handle, &dl_info); | ||
if (0 != ret && dl_info.dli_fname != NULL) { | ||
std::error_code ec; | ||
auto path = fs::absolute(dl_info.dli_fname, ec); | ||
if (ec) { | ||
return unexpected<std::string>(ec.message()); | ||
} | ||
return path.lexically_normal(); | ||
} | ||
return unexpected<std::string>("::dladdr failed."); | ||
} | ||
|
||
path_expected dll_path() noexcept { | ||
return dll_path((void*)&exe_path); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#include <Windows.h> | ||
#include <bee/error.h> | ||
#include <bee/sys/path.h> | ||
#include <bee/utility/dynarray.h> | ||
#include <shlobj.h> | ||
|
||
// http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx | ||
extern "C" IMAGE_DOS_HEADER __ImageBase; | ||
|
||
namespace bee::sys { | ||
static path_expected dll_path(HMODULE module_handle) noexcept { | ||
wchar_t buffer[MAX_PATH]; | ||
DWORD path_len = ::GetModuleFileNameW(module_handle, buffer, _countof(buffer)); | ||
if (path_len == 0) { | ||
return unexpected<std::string>(error::sys_errmsg("GetModuleFileNameW")); | ||
} | ||
if (path_len < _countof(buffer)) { | ||
return fs::path(buffer, buffer + path_len); | ||
} | ||
for (DWORD buf_len = 0x200; buf_len <= 0x10000; buf_len <<= 1) { | ||
dynarray<wchar_t> buf(buf_len); | ||
path_len = ::GetModuleFileNameW(module_handle, buf.data(), buf_len); | ||
if (path_len == 0) { | ||
return unexpected<std::string>(error::sys_errmsg("GetModuleFileNameW")); | ||
} | ||
if (path_len < _countof(buffer)) { | ||
return fs::path(buf.data(), buf.data() + path_len); | ||
} | ||
} | ||
return unexpected<std::string>("::GetModuleFileNameW return too long."); | ||
} | ||
|
||
path_expected exe_path() noexcept { | ||
return dll_path(NULL); | ||
} | ||
|
||
path_expected dll_path() noexcept { | ||
return dll_path(reinterpret_cast<HMODULE>(&__ImageBase)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters