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

Function cbm_detect_self_path

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

9184 * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback
9185 * used only as an install copy source. */
9186static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
9187 buf[0] = '\0';
9188 bool exact = false;
9189#ifdef _WIN32
9190 /* GetModuleFileNameA renders the module path through the ANSI code page,
9191 * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve
9192 * wide and convert to UTF-8 so the returned path survives verbatim. */
9193 char *module_path = cbm_module_path_utf8();
9194 size_t length = module_path ? strlen(module_path) : 0U;
9195 exact = module_path && length > 0U && length < buf_sz;
9196 if (exact) {
9197 memcpy(buf, module_path, length + 1U);
9198 cbm_normalize_path_sep(buf);
9199 } else {
9200 buf[0] = '\0';
9201 }
9202 free(module_path);
9203#elif defined(__APPLE__)
9204 uint32_t sp_sz = (uint32_t)buf_sz;
9205 exact = _NSGetExecutablePath(buf, &sp_sz) == 0;
9206 if (!exact) {
9207 buf[0] = '\0';
9208 }
9209#else
9210 ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE);
9211 exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE;
9212 if (exact) {
9213 buf[sp_len] = '\0';
9214 } else {
9215 buf[0] = '\0';
9216 }
9217#endif
9218 if (!buf[0]) {
9219#ifdef _WIN32
9220 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home);
9221#else
9222 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home);
9223#endif
9224 }
9225 return exact;
9226}
9227
9228/* Build the agent.install.plan.v1 receipt (#388): a machine-readable list of
9229 * the config / instruction / skill / agent / hook files `install` WOULD write, produced by

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