| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- // (‑●‑●)> released under the WTFPL v2 license, by Gregory Pakosz (@gpakosz)
- // in case you want to #include "whereami.c" in a larger compilation unit
- #if !defined(WHEREAMI_H)
- #include "whereami.h"
- #endif
- #ifdef __cplusplus
- extern "C" {
- #endif
- #if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
- #include <stdlib.h>
- #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
- #if defined(_WIN32)
- #define WIN32_LEAN_AND_MEAN
- #if defined(_MSC_VER)
- #pragma warning(push, 3)
- #endif
- #include <windows.h>
- #include <intrin.h>
- #if defined(_MSC_VER)
- #pragma warning(pop)
- #endif
- #ifndef WAI_NOINLINE
- #ifdef _MSC_VER
- #define WAI_NOINLINE __declspec(noinline)
- #endif
- #endif
- 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;
- for (;;)
- {
- DWORD size;
- int 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
- {
- size_ *= 2;
- path = (wchar_t*)WAI_REALLOC(path, sizeof(wchar_t) * size_);
- size = GetModuleFileNameW(NULL, path, size_);
- } while (size == size_);
- }
- else
- path = buffer1;
- _wfullpath(buffer2, path, MAX_PATH);
- length_ = WideCharToMultiByte(CP_UTF8, 0, buffer2, -1, out, capacity, NULL, NULL);
- if (length_ == 0)
- length_ = WideCharToMultiByte(CP_UTF8, 0, buffer2, -1, 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_;
- break;
- }
- if (path != buffer1)
- WAI_FREE(path);
- return length;
- }
- 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)_ReturnAddress(), &module))
- #if defined(_MSC_VER)
- #pragma warning(pop)
- #endif
- {
- length = WAI_PREFIX(getModulePath_)(module, out, capacity, dirname_length);
- }
- return length;
- }
- #elif defined(__linux__)
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <limits.h>
- #ifndef __STDC_FORMAT_MACROS
- #define __STDC_FORMAT_MACROS
- #endif
- #include <inttypes.h>
- char *realpath(const char * pathname, char * resolved_path);
- #ifndef PATH_MAX
- #define PATH_MAX 4096
- #endif
- #if !defined(WAI_PROC_SELF_EXE)
- #define WAI_PROC_SELF_EXE "/proc/self/exe"
- #endif
- #ifndef WAI_NOINLINE
- #ifdef __GNUC__
- #define WAI_NOINLINE __attribute__((noinline))
- #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;
- for (;;)
- {
- 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;
- }
- }
- }
- }
- break;
- }
- return length;
- }
- #if !defined(WAI_PROC_SELF_MAPS_RETRY)
- #define WAI_PROC_SELF_MAPS_RETRY 5
- #endif
- #if !defined(WAI_PROC_SELF_MAPS)
- #define WAI_PROC_SELF_MAPS "/proc/self/maps"
- #endif
- WAI_NOINLINE
- WAI_FUNCSPEC
- int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
- {
- int length = -1;
- FILE* maps = NULL;
- int i;
- for (i = 0; i < WAI_PROC_SELF_MAPS_RETRY; ++i)
- {
- 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 = (uint64_t)(uintptr_t) __builtin_extract_return_addr(__builtin_return_address(0));
- if (low <= addr && addr <= high)
- {
- char* resolved;
- resolved = realpath(path, 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;
- }
- }
- }
- fclose(maps);
- if (length != -1)
- break;
- }
- return length;
- }
- #elif defined(__APPLE__)
- #define _DARWIN_BETTER_REALPATH
- #include <mach-o/dyld.h>
- #include <limits.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dlfcn.h>
- #ifndef WAI_NOINLINE
- #ifdef __GNUC__
- #define WAI_NOINLINE __attribute__((noinline))
- #endif
- #endif
- 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;
- for (;;)
- {
- 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;
- }
- }
- }
- }
- break;
- }
- if (path != buffer1)
- WAI_FREE(path);
- return length;
- }
- 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(__builtin_extract_return_addr(__builtin_return_address(0)), &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 <limits.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <dlfcn.h>
- #ifndef WAI_NOINLINE
- #ifdef __GNUC__
- #define WAI_NOINLINE __attribute__((noinline))
- #endif
- #endif
- #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;
- for (;;)
- {
- 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;
- }
- }
- }
- }
- break;
- }
- fclose(self_exe);
- return length;
- }
- 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(__builtin_extract_return_addr(__builtin_return_address(0)), &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;
- }
- #endif
- #ifdef __cplusplus
- }
- #endif
|