| 6635 | /* ── PATH management ──────────────────────────────────────────── */ |
| 6636 | |
| 6637 | int cbm_ensure_path(const char *bin_dir, const char *rc_file, bool dry_run) { |
| 6638 | if (!bin_dir || !rc_file) { |
| 6639 | return CLI_ERR; |
| 6640 | } |
| 6641 | |
| 6642 | /* fish uses a different syntax than POSIX shells: `export PATH="...:$PATH"` |
| 6643 | * is a syntax error in fish and breaks config.fish (#319). When the target |
| 6644 | * is a fish config, emit the fish-native `fish_add_path` (idempotent, |
| 6645 | * prepends only if absent) instead. */ |
| 6646 | size_t rc_len = strlen(rc_file); |
| 6647 | bool is_fish = rc_len >= CBM_SZ_5 && strcmp(rc_file + rc_len - CBM_SZ_5, ".fish") == 0; |
| 6648 | |
| 6649 | char line[CLI_BUF_1K]; |
| 6650 | if (is_fish) { |
| 6651 | snprintf(line, sizeof(line), "fish_add_path %s", bin_dir); |
| 6652 | } else { |
| 6653 | snprintf(line, sizeof(line), "export PATH=\"%s:$PATH\"", bin_dir); |
| 6654 | } |
| 6655 | |
| 6656 | /* Check if already present in rc file */ |
| 6657 | FILE *f = fopen(rc_file, "r"); |
| 6658 | if (f) { |
| 6659 | char buf[CLI_BUF_2K]; |
| 6660 | while (fgets(buf, sizeof(buf), f)) { |
| 6661 | if (strstr(buf, line)) { |
| 6662 | (void)fclose(f); |
| 6663 | return CLI_TRUE; /* already present */ |
| 6664 | } |
| 6665 | } |
| 6666 | (void)fclose(f); |
| 6667 | } |
| 6668 | |
| 6669 | if (dry_run) { |
| 6670 | return 0; |
| 6671 | } |
| 6672 | |
| 6673 | f = fopen(rc_file, "a"); |
| 6674 | if (!f) { |
| 6675 | return CLI_ERR; |
| 6676 | } |
| 6677 | |
| 6678 | (void)fprintf(f, "\n# Added by codebase-memory-mcp install\n%s\n", line); |
| 6679 | (void)fclose(f); |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
| 6683 | #ifdef _WIN32 |
| 6684 | static wchar_t *cli_windows_utf8_to_wide(const char *value) { |
no test coverage detected