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

Function cbm_detect_self_path

src/cli/cli.c:9453–9500  ·  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

9451 * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback
9452 * used only as an install copy source. */
9453static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
9454 buf[0] = '\0';
9455 bool exact = false;
9456#ifdef _WIN32
9457 /* GetModuleFileNameA renders the module path through the ANSI code page,
9458 * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve
9459 * wide and convert to UTF-8 so the returned path survives verbatim. */
9460 char *module_path = cbm_module_path_utf8();
9461 size_t length = module_path ? strlen(module_path) : 0U;
9462 exact = module_path && length > 0U && length < buf_sz;
9463 if (exact) {
9464 memcpy(buf, module_path, length + 1U);
9465 cbm_normalize_path_sep(buf);
9466 } else {
9467 buf[0] = '\0';
9468 }
9469 free(module_path);
9470#elif defined(__APPLE__)
9471 uint32_t sp_sz = (uint32_t)buf_sz;
9472 exact = _NSGetExecutablePath(buf, &sp_sz) == 0;
9473 if (!exact) {
9474 buf[0] = '\0';
9475 }
9476#elif defined(__FreeBSD__)
9477 int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
9478 size_t cb = buf_sz;
9479 exact = sysctl(mib, 4, buf, &cb, NULL, 0) == 0 && cb > 0;
9480 if (!exact) {
9481 buf[0] = '\0';
9482 }
9483#else
9484 ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE);
9485 exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE;
9486 if (exact) {
9487 buf[sp_len] = '\0';
9488 } else {
9489 buf[0] = '\0';
9490 }
9491#endif
9492 if (!buf[0]) {
9493#ifdef _WIN32
9494 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home);
9495#else
9496 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home);
9497#endif
9498 }
9499 return exact;
9500}
9501
9502/* Is the running binary owned by an external package manager rather than by us?
9503 *

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