MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / cbm_detect_self_path

Function cbm_detect_self_path

src/cli/cli.c:9391–9438  ·  view source on GitHub ↗

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. */

Source from the content-addressed store, hash-verified

9389 * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback
9390 * used only as an install copy source. */
9391static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
9392 buf[0] = '\0';
9393 bool exact = false;
9394#ifdef _WIN32
9395 /* GetModuleFileNameA renders the module path through the ANSI code page,
9396 * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve
9397 * wide and convert to UTF-8 so the returned path survives verbatim. */
9398 char *module_path = cbm_module_path_utf8();
9399 size_t length = module_path ? strlen(module_path) : 0U;
9400 exact = module_path && length > 0U && length < buf_sz;
9401 if (exact) {
9402 memcpy(buf, module_path, length + 1U);
9403 cbm_normalize_path_sep(buf);
9404 } else {
9405 buf[0] = '\0';
9406 }
9407 free(module_path);
9408#elif defined(__APPLE__)
9409 uint32_t sp_sz = (uint32_t)buf_sz;
9410 exact = _NSGetExecutablePath(buf, &sp_sz) == 0;
9411 if (!exact) {
9412 buf[0] = '\0';
9413 }
9414#elif defined(__FreeBSD__)
9415 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
9416 size_t cb = buf_sz;
9417 exact = sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0;
9418 if (!exact) {
9419 buf[0] = '\0';
9420 }
9421#else
9422 ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE);
9423 exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE;
9424 if (exact) {
9425 buf[sp_len] = '\0';
9426 } else {
9427 buf[0] = '\0';
9428 }
9429#endif
9430 if (!buf[0]) {
9431#ifdef _WIN32
9432 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home);
9433#else
9434 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home);
9435#endif
9436 }
9437 return exact;
9438}
9439
9440/* Is the running binary owned by an external package manager rather than by us?
9441 *

Callers 4

cli_install_activateFunction · 0.85
cbm_cmd_installFunction · 0.85
cbm_cmd_updateFunction · 0.85

Calls 2

cbm_module_path_utf8Function · 0.85
cbm_normalize_path_sepFunction · 0.85

Tested by

no test coverage detected