From 2adce1795211fd0a42c3b4e03ab35a90bb01bccf Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sun, 26 Nov 2023 14:05:29 -0500 Subject: Stop relying on correctly set working directory --- CMakeLists.txt | 3 + src/eye_indicator.cpp | 10 +- src/game_data.cpp | 12 +- src/global.cpp | 24 ++ src/global.h | 11 + src/logger.cpp | 4 +- src/tracker_config.cpp | 9 +- src/tracker_config.h | 5 + src/tracker_panel.cpp | 4 +- vendor/whereami/LICENSE.MIT | 18 ++ vendor/whereami/whereami.c | 704 ++++++++++++++++++++++++++++++++++++++++++++ vendor/whereami/whereami.h | 67 +++++ 12 files changed, 857 insertions(+), 14 deletions(-) create mode 100644 src/global.cpp create mode 100644 src/global.h create mode 100644 vendor/whereami/LICENSE.MIT create mode 100644 vendor/whereami/whereami.c create mode 100644 vendor/whereami/whereami.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7750364..8e08b68 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,7 @@ include_directories( vendor/wswrap/include ${yaml-cpp_INCLUDE_DIRS} ${OpenSSL_INCLUDE_DIRS} + vendor/whereami ) find_path(SYSTEM_INCLUDE_DIR zlib.h) @@ -41,6 +42,8 @@ add_executable(lingo_ap_tracker "src/logger.cpp" "src/achievements_pane.cpp" "src/settings_dialog.cpp" + "src/global.cpp" + "vendor/whereami/whereami.c" ) set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD 20) set_property(TARGET lingo_ap_tracker PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/src/eye_indicator.cpp b/src/eye_indicator.cpp index 03e234e..61ad780 100644 --- a/src/eye_indicator.cpp +++ b/src/eye_indicator.cpp @@ -1,5 +1,7 @@ #include "eye_indicator.h" +#include "global.h" + EyeIndicator::EyeIndicator(wxWindow* parent) : wxWindow(parent, wxID_ANY) { SetMinSize({32, 32}); @@ -17,14 +19,14 @@ void EyeIndicator::SetChecked(bool checked) { } const wxImage& EyeIndicator::GetUncheckedImage() { - static wxImage* unchecked_image = - new wxImage("assets/unchecked.png", wxBITMAP_TYPE_PNG); + static wxImage* unchecked_image = new wxImage( + GetAbsolutePath("assets/unchecked.png").c_str(), wxBITMAP_TYPE_PNG); return *unchecked_image; } const wxImage& EyeIndicator::GetCheckedImage() { - static wxImage* checked_image = - new wxImage("assets/checked.png", wxBITMAP_TYPE_PNG); + static wxImage* checked_image = new wxImage( + GetAbsolutePath("assets/checked.png").c_str(), wxBITMAP_TYPE_PNG); return *checked_image; } diff --git a/src/game_data.cpp b/src/game_data.cpp index 31e23ec..5204262 100644 --- a/src/game_data.cpp +++ b/src/game_data.cpp @@ -6,6 +6,7 @@ #include #include +#include "global.h" #include "logger.h" namespace { @@ -61,10 +62,13 @@ struct GameData { std::set malconfigured_areas_; GameData() { - YAML::Node lingo_config = YAML::LoadFile("assets/LL1.yaml"); - YAML::Node areas_config = YAML::LoadFile("assets/areas.yaml"); - YAML::Node pilgrimage_config = YAML::LoadFile("assets/pilgrimage.yaml"); - YAML::Node ids_config = YAML::LoadFile("assets/ids.yaml"); + YAML::Node lingo_config = + YAML::LoadFile(GetAbsolutePath("assets/LL1.yaml")); + YAML::Node areas_config = + YAML::LoadFile(GetAbsolutePath("assets/areas.yaml")); + YAML::Node pilgrimage_config = + YAML::LoadFile(GetAbsolutePath("assets/pilgrimage.yaml")); + YAML::Node ids_config = YAML::LoadFile(GetAbsolutePath("assets/ids.yaml")); auto init_color_id = [this, &ids_config](const std::string &color_name) { if (ids_config["special_items"] && diff --git a/src/global.cpp b/src/global.cpp new file mode 100644 index 0000000..3ee4f58 --- /dev/null +++ b/src/global.cpp @@ -0,0 +1,24 @@ +#include "global.h" + +#include + +#include +#include +#include + +const std::filesystem::path& GetExecutableDirectory() { + static const std::filesystem::path* executable_directory = []() { + int length = wai_getExecutablePath(NULL, 0, NULL); + std::string buf(length, 0); + wai_getExecutablePath(buf.data(), length, NULL); + + std::filesystem::path exec_path(buf); + return new std::filesystem::path(exec_path.parent_path()); + }(); + + return *executable_directory; +} + +std::string GetAbsolutePath(std::string_view path) { + return (GetExecutableDirectory() / path).string(); +} diff --git a/src/global.h b/src/global.h new file mode 100644 index 0000000..2eb7884 --- /dev/null +++ b/src/global.h @@ -0,0 +1,11 @@ +#ifndef GLOBAL_H_44945DBA +#define GLOBAL_H_44945DBA + +#include +#include + +const std::filesystem::path& GetExecutableDirectory(); + +std::string GetAbsolutePath(std::string_view path); + +#endif /* end of include guard: GLOBAL_H_44945DBA */ diff --git a/src/logger.cpp b/src/logger.cpp index ccb721a..4b722c8 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -4,11 +4,13 @@ #include #include +#include "global.h" + namespace { class Logger { public: - Logger() : logfile_("debug.log") {} + Logger() : logfile_(GetAbsolutePath("debug.log")) {} void LogLine(const std::string& text) { std::lock_guard guard(file_mutex_); diff --git a/src/tracker_config.cpp b/src/tracker_config.cpp index 11e1cec..b0f4ac4 100644 --- a/src/tracker_config.cpp +++ b/src/tracker_config.cpp @@ -4,11 +4,11 @@ #include -constexpr const char* CONFIG_FILE_NAME = "config.yaml"; +#include "global.h" void TrackerConfig::Load() { try { - YAML::Node file = YAML::LoadFile(CONFIG_FILE_NAME); + YAML::Node file = YAML::LoadFile(filename_); ap_server = file["ap_server"].as(); ap_player = file["ap_player"].as(); @@ -32,11 +32,12 @@ void TrackerConfig::Save() { output["hybrid_areas"] = hybrid_areas; output["show_hunt_panels"] = show_hunt_panels; - std::ofstream filewriter(CONFIG_FILE_NAME); + std::ofstream filewriter(filename_); filewriter << output; } TrackerConfig& GetTrackerConfig() { - static TrackerConfig* instance = new TrackerConfig(); + static TrackerConfig* instance = + new TrackerConfig(GetAbsolutePath("config.yaml")); return *instance; } diff --git a/src/tracker_config.h b/src/tracker_config.h index 0a29d2c..b95194e 100644 --- a/src/tracker_config.h +++ b/src/tracker_config.h @@ -5,6 +5,8 @@ class TrackerConfig { public: + explicit TrackerConfig(const std::string& filename) : filename_(filename) {} + void Load(); void Save(); @@ -16,6 +18,9 @@ class TrackerConfig { bool should_check_for_updates = false; bool hybrid_areas = false; bool show_hunt_panels = false; + + private: + std::string filename_; }; TrackerConfig& GetTrackerConfig(); diff --git a/src/tracker_panel.cpp b/src/tracker_panel.cpp index 5e035af..81cb595 100644 --- a/src/tracker_panel.cpp +++ b/src/tracker_panel.cpp @@ -3,6 +3,7 @@ #include "ap_state.h" #include "area_popup.h" #include "game_data.h" +#include "global.h" #include "tracker_config.h" #include "tracker_state.h" @@ -11,7 +12,8 @@ constexpr int AREA_BORDER_SIZE = 5; constexpr int AREA_EFFECTIVE_SIZE = AREA_ACTUAL_SIZE + AREA_BORDER_SIZE * 2; TrackerPanel::TrackerPanel(wxWindow *parent) : wxPanel(parent, wxID_ANY) { - map_image_ = wxImage("assets/lingo_map.png", wxBITMAP_TYPE_PNG); + map_image_ = wxImage(GetAbsolutePath("assets/lingo_map.png").c_str(), + wxBITMAP_TYPE_PNG); if (!map_image_.IsOk()) { return; } diff --git a/vendor/whereami/LICENSE.MIT b/vendor/whereami/LICENSE.MIT new file mode 100644 index 0000000..fd8ed6b --- /dev/null +++ b/vendor/whereami/LICENSE.MIT @@ -0,0 +1,18 @@ +Copyright Gregory Pakosz + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/vendor/whereami/whereami.c b/vendor/whereami/whereami.c new file mode 100644 index 0000000..f238e1b --- /dev/null +++ b/vendor/whereami/whereami.c @@ -0,0 +1,704 @@ +// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses +// without any warranty. +// by Gregory Pakosz (@gpakosz) +// https://github.com/gpakosz/whereami + +// in case you want to #include "whereami.c" in a larger compilation unit +#if !defined(WHEREAMI_H) +#include +#endif + +#ifdef __cplusplus +extern "C" { +#endif + +#if defined(__linux__) || defined(__CYGWIN__) +#undef _DEFAULT_SOURCE +#define _DEFAULT_SOURCE +#elif defined(__APPLE__) +#undef _DARWIN_C_SOURCE +#define _DARWIN_C_SOURCE +#define _DARWIN_BETTER_REALPATH +#endif + +#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC) +#include +#endif + +#if !defined(WAI_MALLOC) +#define WAI_MALLOC(size) malloc(size) +#endif + +#if !defined(WAI_FREE) +#define WAI_FREE(p) free(p) +#endif + +#if !defined(WAI_REALLOC) +#define WAI_REALLOC(p, size) realloc(p, size) +#endif + +#ifndef WAI_NOINLINE +#if defined(_MSC_VER) +#define WAI_NOINLINE __declspec(noinline) +#elif defined(__GNUC__) +#define WAI_NOINLINE __attribute__((noinline)) +#else +#error unsupported compiler +#endif +#endif + +#if defined(_MSC_VER) +#define WAI_RETURN_ADDRESS() _ReturnAddress() +#elif defined(__GNUC__) +#define WAI_RETURN_ADDRESS() \ + __builtin_extract_return_addr(__builtin_return_address(0)) +#else +#error unsupported compiler +#endif + +#if defined(_WIN32) + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#if defined(_MSC_VER) +#pragma warning(push, 3) +#endif +#include +#include +#if defined(_MSC_VER) +#pragma warning(pop) +#endif +#include + +static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, + int* dirname_length) { + wchar_t buffer1[MAX_PATH]; + wchar_t buffer2[MAX_PATH]; + wchar_t* path = NULL; + int length = -1; + bool ok; + + for (ok = false; !ok; ok = true) { + DWORD size; + int length_, length__; + + size = GetModuleFileNameW(module, buffer1, + sizeof(buffer1) / sizeof(buffer1[0])); + + if (size == 0) + break; + else if (size == (DWORD)(sizeof(buffer1) / sizeof(buffer1[0]))) { + DWORD size_ = size; + do { + wchar_t* path_; + + path_ = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_ * 2); + if (!path_) break; + size_ *= 2; + path = path_; + size = GetModuleFileNameW(module, path, size_); + } while (size == size_); + + if (size == size_) break; + } else + path = buffer1; + + if (!_wfullpath(buffer2, path, MAX_PATH)) break; + length_ = (int)wcslen(buffer2); + length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, out, capacity, + NULL, NULL); + + if (length__ == 0) + length__ = WideCharToMultiByte(CP_UTF8, 0, buffer2, length_, NULL, 0, + NULL, NULL); + if (length__ == 0) break; + + if (length__ <= capacity && dirname_length) { + int i; + + for (i = length__ - 1; i >= 0; --i) { + if (out[i] == '\\') { + *dirname_length = i; + break; + } + } + } + + length = length__; + } + + if (path != buffer1) WAI_FREE(path); + + return ok ? length : -1; +} + +WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getExecutablePath)( + char* out, int capacity, int* dirname_length) { + return WAI_PREFIX(getModulePath_)(NULL, out, capacity, dirname_length); +} + +WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, + int* dirname_length) { + HMODULE module; + int length = -1; + +#if defined(_MSC_VER) +#pragma warning(push) +#pragma warning(disable : 4054) +#endif + if (GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | + GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, + (LPCTSTR)WAI_RETURN_ADDRESS(), &module)) +#if defined(_MSC_VER) +#pragma warning(pop) +#endif + { + length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length); + } + + return length; +} + +#elif defined(__linux__) || defined(__CYGWIN__) || defined(__sun) || \ + defined(WAI_USE_PROC_SELF_EXE) + +#include +#include +#include +#if defined(__linux__) +#include +#else +#include +#endif +#ifndef __STDC_FORMAT_MACROS +#define __STDC_FORMAT_MACROS +#endif +#include +#include + +#if !defined(WAI_PROC_SELF_EXE) +#if defined(__sun) +#define WAI_PROC_SELF_EXE "/proc/self/path/a.out" +#else +#define WAI_PROC_SELF_EXE "/proc/self/exe" +#endif +#endif + +WAI_FUNCSPEC +int WAI_PREFIX(getExecutablePath)(char* out, int capacity, + int* dirname_length) { + char buffer[PATH_MAX]; + char* resolved = NULL; + int length = -1; + bool ok; + + for (ok = false; !ok; ok = true) { + resolved = realpath(WAI_PROC_SELF_EXE, buffer); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + return ok ? length : -1; +} + +#if !defined(WAI_PROC_SELF_MAPS_RETRY) +#define WAI_PROC_SELF_MAPS_RETRY 5 +#endif + +#if !defined(WAI_PROC_SELF_MAPS) +#if defined(__sun) +#define WAI_PROC_SELF_MAPS "/proc/self/map" +#else +#define WAI_PROC_SELF_MAPS "/proc/self/maps" +#endif +#endif + +#if defined(__ANDROID__) || defined(ANDROID) +#include +#include +#include +#endif +#include + +WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, + int* dirname_length) { + int length = -1; + FILE* maps = NULL; + + for (int r = 0; r < WAI_PROC_SELF_MAPS_RETRY; ++r) { + maps = fopen(WAI_PROC_SELF_MAPS, "r"); + if (!maps) break; + + for (;;) { + char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX]; + uint64_t low, high; + char perms[5]; + uint64_t offset; + uint32_t major, minor; + char path[PATH_MAX]; + uint32_t inode; + + if (!fgets(buffer, sizeof(buffer), maps)) break; + + if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", + &low, &high, perms, &offset, &major, &minor, &inode, + path) == 8) { + uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS(); + if (low <= addr && addr <= high) { + char* resolved; + + resolved = realpath(path, buffer); + if (!resolved) break; + + length = (int)strlen(resolved); +#if defined(__ANDROID__) || defined(ANDROID) + if (length > 4 && buffer[length - 1] == 'k' && + buffer[length - 2] == 'p' && buffer[length - 3] == 'a' && + buffer[length - 4] == '.') { + int fd = open(path, O_RDONLY); + if (fd == -1) { + length = -1; // retry + break; + } + + char* begin = (char*)mmap(0, offset, PROT_READ, MAP_SHARED, fd, 0); + if (begin == MAP_FAILED) { + close(fd); + length = -1; // retry + break; + } + + char* p = begin + offset - 30; // minimum size of local file header + while (p >= begin) // scan backwards + { + if (*((uint32_t*)p) == + 0x04034b50UL) // local file header signature found + { + uint16_t length_ = *((uint16_t*)(p + 26)); + + if (length + 2 + length_ < (int)sizeof(buffer)) { + memcpy(&buffer[length], "!/", 2); + memcpy(&buffer[length + 2], p + 30, length_); + length += 2 + length_; + } + + break; + } + + --p; + } + + munmap(begin, offset); + close(fd); + } +#endif + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + + break; + } + } + } + + fclose(maps); + maps = NULL; + + if (length != -1) break; + } + + return length; +} + +#elif defined(__APPLE__) + +#include +#include +#include +#include +#include +#include + +WAI_FUNCSPEC +int WAI_PREFIX(getExecutablePath)(char* out, int capacity, + int* dirname_length) { + char buffer1[PATH_MAX]; + char buffer2[PATH_MAX]; + char* path = buffer1; + char* resolved = NULL; + int length = -1; + bool ok; + + for (ok = false; !ok; ok = true) { + uint32_t size = (uint32_t)sizeof(buffer1); + if (_NSGetExecutablePath(path, &size) == -1) { + path = (char*)WAI_MALLOC(size); + if (!_NSGetExecutablePath(path, &size)) break; + } + + resolved = realpath(path, buffer2); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + if (path != buffer1) WAI_FREE(path); + + return ok ? length : -1; +} + +WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, + int* dirname_length) { + char buffer[PATH_MAX]; + char* resolved = NULL; + int length = -1; + + for (;;) { + Dl_info info; + + if (dladdr(WAI_RETURN_ADDRESS(), &info)) { + resolved = realpath(info.dli_fname, buffer); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + break; + } + + return length; +} + +#elif defined(__QNXNTO__) + +#include +#include +#include +#include +#include +#include + +#if !defined(WAI_PROC_SELF_EXE) +#define WAI_PROC_SELF_EXE "/proc/self/exefile" +#endif + +WAI_FUNCSPEC +int WAI_PREFIX(getExecutablePath)(char* out, int capacity, + int* dirname_length) { + char buffer1[PATH_MAX]; + char buffer2[PATH_MAX]; + char* resolved = NULL; + FILE* self_exe = NULL; + int length = -1; + bool ok; + + for (ok = false; !ok; ok = true) { + self_exe = fopen(WAI_PROC_SELF_EXE, "r"); + if (!self_exe) break; + + if (!fgets(buffer1, sizeof(buffer1), self_exe)) break; + + resolved = realpath(buffer1, buffer2); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + fclose(self_exe); + + return ok ? length : -1; +} + +WAI_FUNCSPEC +int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length) { + char buffer[PATH_MAX]; + char* resolved = NULL; + int length = -1; + + for (;;) { + Dl_info info; + + if (dladdr(WAI_RETURN_ADDRESS(), &info)) { + resolved = realpath(info.dli_fname, buffer); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + break; + } + + return length; +} + +#elif defined(__DragonFly__) || defined(__FreeBSD__) || \ + defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__) + +#include +#include +#include +#include +#include +#include +#include + +#if defined(__OpenBSD__) + +#include + +WAI_FUNCSPEC +int WAI_PREFIX(getExecutablePath)(char* out, int capacity, + int* dirname_length) { + char buffer1[4096]; + char buffer2[PATH_MAX]; + char buffer3[PATH_MAX]; + char** argv = (char**)buffer1; + char* resolved = NULL; + int length = -1; + bool ok; + + for (ok = false; !ok; ok = true) { + int mib[4] = {CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV}; + size_t size; + + if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0) break; + + if (size > sizeof(buffer1)) { + argv = (char**)WAI_MALLOC(size); + if (!argv) break; + } + + if (sysctl(mib, 4, argv, &size, NULL, 0) != 0) break; + + if (strchr(argv[0], '/')) { + resolved = realpath(argv[0], buffer2); + if (!resolved) break; + } else { + const char* PATH = getenv("PATH"); + if (!PATH) break; + + size_t argv0_length = strlen(argv[0]); + + const char* begin = PATH; + while (1) { + const char* separator = strchr(begin, ':'); + const char* end = separator ? separator : begin + strlen(begin); + + if (end - begin > 0) { + if (*(end - 1) == '/') --end; + + if (((end - begin) + 1 + argv0_length + 1) <= sizeof(buffer2)) { + memcpy(buffer2, begin, end - begin); + buffer2[end - begin] = '/'; + memcpy(buffer2 + (end - begin) + 1, argv[0], argv0_length + 1); + + resolved = realpath(buffer2, buffer3); + if (resolved) break; + } + } + + if (!separator) break; + + begin = ++separator; + } + + if (!resolved) break; + } + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + if (argv != (char**)buffer1) WAI_FREE(argv); + + return ok ? length : -1; +} + +#else + +WAI_FUNCSPEC +int WAI_PREFIX(getExecutablePath)(char* out, int capacity, + int* dirname_length) { + char buffer1[PATH_MAX]; + char buffer2[PATH_MAX]; + char* path = buffer1; + char* resolved = NULL; + int length = -1; + bool ok; + + for (ok = false; !ok; ok = true) { +#if defined(__NetBSD__) + int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; +#else + int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; +#endif + size_t size = sizeof(buffer1); + + if (sysctl(mib, 4, path, &size, NULL, 0) != 0) break; + + resolved = realpath(path, buffer2); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + return ok ? length : -1; +} + +#endif + +WAI_NOINLINE WAI_FUNCSPEC int WAI_PREFIX(getModulePath)(char* out, int capacity, + int* dirname_length) { + char buffer[PATH_MAX]; + char* resolved = NULL; + int length = -1; + + for (;;) { + Dl_info info; + + if (dladdr(WAI_RETURN_ADDRESS(), &info)) { + resolved = realpath(info.dli_fname, buffer); + if (!resolved) break; + + length = (int)strlen(resolved); + if (length <= capacity) { + memcpy(out, resolved, length); + + if (dirname_length) { + int i; + + for (i = length - 1; i >= 0; --i) { + if (out[i] == '/') { + *dirname_length = i; + break; + } + } + } + } + } + + break; + } + + return length; +} + +#else + +#error unsupported platform + +#endif + +#ifdef __cplusplus +} +#endif diff --git a/vendor/whereami/whereami.h b/vendor/whereami/whereami.h new file mode 100644 index 0000000..8988f1d --- /dev/null +++ b/vendor/whereami/whereami.h @@ -0,0 +1,67 @@ +// (‑●‑●)> dual licensed under the WTFPL v2 and MIT licenses +// without any warranty. +// by Gregory Pakosz (@gpakosz) +// https://github.com/gpakosz/whereami + +#ifndef WHEREAMI_H +#define WHEREAMI_H + +#ifdef __cplusplus +extern "C" { +#endif + +#ifndef WAI_FUNCSPEC +#define WAI_FUNCSPEC +#endif +#ifndef WAI_PREFIX +#define WAI_PREFIX(function) wai_##function +#endif + +/** + * Returns the path to the current executable. + * + * Usage: + * - first call `int length = wai_getExecutablePath(NULL, 0, NULL);` to + * retrieve the length of the path + * - allocate the destination buffer with `path = (char*)malloc(length + 1);` + * - call `wai_getExecutablePath(path, length, NULL)` again to retrieve the + * path + * - add a terminal NUL character with `path[length] = '\0';` + * + * @param out destination buffer, optional + * @param capacity destination buffer capacity + * @param dirname_length optional recipient for the length of the dirname part + * of the path. + * + * @return the length of the executable path on success (without a terminal NUL + * character), otherwise `-1` + */ +WAI_FUNCSPEC +int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length); + +/** + * Returns the path to the current module + * + * Usage: + * - first call `int length = wai_getModulePath(NULL, 0, NULL);` to retrieve + * the length of the path + * - allocate the destination buffer with `path = (char*)malloc(length + 1);` + * - call `wai_getModulePath(path, length, NULL)` again to retrieve the path + * - add a terminal NUL character with `path[length] = '\0';` + * + * @param out destination buffer, optional + * @param capacity destination buffer capacity + * @param dirname_length optional recipient for the length of the dirname part + * of the path. + * + * @return the length of the module path on success (without a terminal NUL + * character), otherwise `-1` + */ +WAI_FUNCSPEC +int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length); + +#ifdef __cplusplus +} +#endif + +#endif // #ifndef WHEREAMI_H -- cgit 1.4.1