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

Function cbm_ensure_path

src/cli/cli.c:2299–2343  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2297/* ── PATH management ──────────────────────────────────────────── */
2298
2299int cbm_ensure_path(const char *bin_dir, const char *rc_file, bool dry_run) {
2300 if (!bin_dir || !rc_file) {
2301 return CLI_ERR;
2302 }
2303
2304 /* fish uses a different syntax than POSIX shells: `export PATH="...:$PATH"`
2305 * is a syntax error in fish and breaks config.fish (#319). When the target
2306 * is a fish config, emit the fish-native `fish_add_path` (idempotent,
2307 * prepends only if absent) instead. */
2308 size_t rc_len = strlen(rc_file);
2309 bool is_fish = rc_len >= CBM_SZ_5 && strcmp(rc_file + rc_len - CBM_SZ_5, ".fish") == 0;
2310
2311 char line[CLI_BUF_1K];
2312 if (is_fish) {
2313 snprintf(line, sizeof(line), "fish_add_path %s", bin_dir);
2314 } else {
2315 snprintf(line, sizeof(line), "export PATH=\"%s:$PATH\"", bin_dir);
2316 }
2317
2318 /* Check if already present in rc file */
2319 FILE *f = fopen(rc_file, "r");
2320 if (f) {
2321 char buf[CLI_BUF_2K];
2322 while (fgets(buf, sizeof(buf), f)) {
2323 if (strstr(buf, line)) {
2324 (void)fclose(f);
2325 return CLI_TRUE; /* already present */
2326 }
2327 }
2328 (void)fclose(f);
2329 }
2330
2331 if (dry_run) {
2332 return 0;
2333 }
2334
2335 f = fopen(rc_file, "a");
2336 if (!f) {
2337 return CLI_ERR;
2338 }
2339
2340 (void)fprintf(f, "\n# Added by codebase-memory-mcp install\n%s\n", line);
2341 (void)fclose(f);
2342 return 0;
2343}
2344
2345/* ── Tar.gz extraction ────────────────────────────────────────── */
2346

Callers 2

cbm_cmd_installFunction · 0.85
test_cli.cFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected