The activation flow (self-path detection, simple-copy activate) is the same * on every platform: Windows ships ONE binary, exactly like Linux and macOS. */ Detect the running binary's path at runtime. The return value distinguishes * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback * used only as an install copy source. */
| 10839 | * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback |
| 10840 | * used only as an install copy source. */ |
| 10841 | static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) { |
| 10842 | buf[0] = '\0'; |
| 10843 | bool exact = false; |
| 10844 | #ifdef _WIN32 |
| 10845 | /* GetModuleFileNameA renders the module path through the ANSI code page, |
| 10846 | * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve |
| 10847 | * wide and convert to UTF-8 so the returned path survives verbatim. */ |
| 10848 | char *module_path = cbm_module_path_utf8(); |
| 10849 | size_t length = module_path ? strlen(module_path) : 0U; |
| 10850 | exact = module_path && length > 0U && length < buf_sz; |
| 10851 | if (exact) { |
| 10852 | memcpy(buf, module_path, length + 1U); |
| 10853 | cbm_normalize_path_sep(buf); |
| 10854 | } else { |
| 10855 | buf[0] = '\0'; |
| 10856 | } |
| 10857 | free(module_path); |
| 10858 | #elif defined(__APPLE__) |
| 10859 | uint32_t sp_sz = (uint32_t)buf_sz; |
| 10860 | exact = _NSGetExecutablePath(buf, &sp_sz) == 0; |
| 10861 | if (!exact) { |
| 10862 | buf[0] = '\0'; |
| 10863 | } |
| 10864 | #elif defined(__FreeBSD__) || defined(__NetBSD__) |
| 10865 | #if defined(__NetBSD__) |
| 10866 | int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME}; |
| 10867 | #else |
| 10868 | int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; |
| 10869 | #endif |
| 10870 | size_t cb = buf_sz; |
| 10871 | exact = sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0; |
| 10872 | if (!exact) { |
| 10873 | buf[0] = '\0'; |
| 10874 | } |
| 10875 | #else |
| 10876 | ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE); |
| 10877 | exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE; |
| 10878 | if (exact) { |
| 10879 | buf[sp_len] = '\0'; |
| 10880 | } else { |
| 10881 | buf[0] = '\0'; |
| 10882 | } |
| 10883 | #endif |
| 10884 | if (!buf[0]) { |
| 10885 | #ifdef _WIN32 |
| 10886 | snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home); |
| 10887 | #else |
| 10888 | snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home); |
| 10889 | #endif |
| 10890 | } |
| 10891 | return exact; |
| 10892 | } |
| 10893 | |
| 10894 | /* Is the running binary owned by an external package manager rather than by us? |
| 10895 | * |
no test coverage detected