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. */
| 9616 | * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback |
| 9617 | * used only as an install copy source. */ |
| 9618 | static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) { |
| 9619 | buf[0] = '\0'; |
| 9620 | bool exact = false; |
| 9621 | #ifdef _WIN32 |
| 9622 | /* GetModuleFileNameA renders the module path through the ANSI code page, |
| 9623 | * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve |
| 9624 | * wide and convert to UTF-8 so the returned path survives verbatim. */ |
| 9625 | char *module_path = cbm_module_path_utf8(); |
| 9626 | size_t length = module_path ? strlen(module_path) : 0U; |
| 9627 | exact = module_path && length > 0U && length < buf_sz; |
| 9628 | if (exact) { |
| 9629 | memcpy(buf, module_path, length + 1U); |
| 9630 | cbm_normalize_path_sep(buf); |
| 9631 | } else { |
| 9632 | buf[0] = '\0'; |
| 9633 | } |
| 9634 | free(module_path); |
| 9635 | #elif defined(__APPLE__) |
| 9636 | uint32_t sp_sz = (uint32_t)buf_sz; |
| 9637 | exact = _NSGetExecutablePath(buf, &sp_sz) == 0; |
| 9638 | if (!exact) { |
| 9639 | buf[0] = '\0'; |
| 9640 | } |
| 9641 | #elif defined(__FreeBSD__) |
| 9642 | int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; |
| 9643 | size_t cb = buf_sz; |
| 9644 | exact = sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0; |
| 9645 | if (!exact) { |
| 9646 | buf[0] = '\0'; |
| 9647 | } |
| 9648 | #else |
| 9649 | ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE); |
| 9650 | exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE; |
| 9651 | if (exact) { |
| 9652 | buf[sp_len] = '\0'; |
| 9653 | } else { |
| 9654 | buf[0] = '\0'; |
| 9655 | } |
| 9656 | #endif |
| 9657 | if (!buf[0]) { |
| 9658 | #ifdef _WIN32 |
| 9659 | snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home); |
| 9660 | #else |
| 9661 | snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home); |
| 9662 | #endif |
| 9663 | } |
| 9664 | return exact; |
| 9665 | } |
| 9666 | |
| 9667 | /* Is the running binary owned by an external package manager rather than by us? |
| 9668 | * |
no test coverage detected