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

Function cbm_detect_self_path

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

9128 * OS-reported identity (safe ownership evidence) from the ~/.local/bin fallback
9129 * used only as an install copy source. */
9130static bool cbm_detect_self_path(char *buf, size_t buf_sz, const char *home) {
9131 buf[0] = '\0';
9132 bool exact = false;
9133#ifdef _WIN32
9134 /* GetModuleFileNameA renders the module path through the ANSI code page,
9135 * which mangles non-ASCII install paths (café_日本語 -> caf?_???). Resolve
9136 * wide and convert to UTF-8 so the returned path survives verbatim. */
9137 char *module_path = cbm_module_path_utf8();
9138 size_t length = module_path ? strlen(module_path) : 0U;
9139 exact = module_path && length > 0U && length < buf_sz;
9140 if (exact) {
9141 memcpy(buf, module_path, length + 1U);
9142 cbm_normalize_path_sep(buf);
9143 } else {
9144 buf[0] = '\0';
9145 }
9146 free(module_path);
9147#elif defined(__APPLE__)
9148 uint32_t sp_sz = (uint32_t)buf_sz;
9149 exact = _NSGetExecutablePath(buf, &sp_sz) == 0;
9150 if (!exact) {
9151 buf[0] = '\0';
9152 }
9153#else
9154 ssize_t sp_len = readlink("/proc/self/exe", buf, buf_sz - SKIP_ONE);
9155 exact = sp_len > 0 && (size_t)sp_len < buf_sz - SKIP_ONE;
9156 if (exact) {
9157 buf[sp_len] = '\0';
9158 } else {
9159 buf[0] = '\0';
9160 }
9161#endif
9162 if (!buf[0]) {
9163#ifdef _WIN32
9164 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp.exe", home);
9165#else
9166 snprintf(buf, buf_sz, "%s/.local/bin/codebase-memory-mcp", home);
9167#endif
9168 }
9169 return exact;
9170}
9171
9172/* Build the agent.install.plan.v1 receipt (#388): a machine-readable list of
9173 * 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