| 5748 | /* ── PATH management ──────────────────────────────────────────── */ |
| 5749 | |
| 5750 | int cbm_ensure_path(const char *bin_dir, const char *rc_file, bool dry_run) { |
| 5751 | if (!bin_dir || !rc_file) { |
| 5752 | return CLI_ERR; |
| 5753 | } |
| 5754 | |
| 5755 | /* fish uses a different syntax than POSIX shells: `export PATH="...:$PATH"` |
| 5756 | * is a syntax error in fish and breaks config.fish (#319). When the target |
| 5757 | * is a fish config, emit the fish-native `fish_add_path` (idempotent, |
| 5758 | * prepends only if absent) instead. */ |
| 5759 | size_t rc_len = strlen(rc_file); |
| 5760 | bool is_fish = rc_len >= CBM_SZ_5 && strcmp(rc_file + rc_len - CBM_SZ_5, ".fish") == 0; |
| 5761 | |
| 5762 | char line[CLI_BUF_1K]; |
| 5763 | if (is_fish) { |
| 5764 | snprintf(line, sizeof(line), "fish_add_path %s", bin_dir); |
| 5765 | } else { |
| 5766 | snprintf(line, sizeof(line), "export PATH=\"%s:$PATH\"", bin_dir); |
| 5767 | } |
| 5768 | |
| 5769 | /* Check if already present in rc file */ |
| 5770 | FILE *f = fopen(rc_file, "r"); |
| 5771 | if (f) { |
| 5772 | char buf[CLI_BUF_2K]; |
| 5773 | while (fgets(buf, sizeof(buf), f)) { |
| 5774 | if (strstr(buf, line)) { |
| 5775 | (void)fclose(f); |
| 5776 | return CLI_TRUE; /* already present */ |
| 5777 | } |
| 5778 | } |
| 5779 | (void)fclose(f); |
| 5780 | } |
| 5781 | |
| 5782 | if (dry_run) { |
| 5783 | return 0; |
| 5784 | } |
| 5785 | |
| 5786 | f = fopen(rc_file, "a"); |
| 5787 | if (!f) { |
| 5788 | return CLI_ERR; |
| 5789 | } |
| 5790 | |
| 5791 | (void)fprintf(f, "\n# Added by codebase-memory-mcp install\n%s\n", line); |
| 5792 | (void)fclose(f); |
| 5793 | return 0; |
| 5794 | } |
| 5795 | |
| 5796 | #ifdef _WIN32 |
| 5797 | static wchar_t *cli_windows_utf8_to_wide(const char *value) { |
no outgoing calls
no test coverage detected