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