Search for an executable named `name` in the PATH environment variable. * Returns the full path in `out` (max out_sz) if found, else empty string. */
| 887 | /* Search for an executable named `name` in the PATH environment variable. |
| 888 | * Returns the full path in `out` (max out_sz) if found, else empty string. */ |
| 889 | static bool find_in_path(const char *name, char *out, size_t out_sz) { |
| 890 | char path_copy[CLI_BUF_4K]; |
| 891 | if (!cbm_safe_getenv("PATH", path_copy, sizeof(path_copy), NULL)) { |
| 892 | return false; |
| 893 | } |
| 894 | char *saveptr; |
| 895 | char *dir = strtok_r(path_copy, PATH_DELIM, &saveptr); |
| 896 | while (dir) { |
| 897 | snprintf(out, out_sz, "%s/%s", dir, name); |
| 898 | if (is_executable(out)) { |
| 899 | return true; |
| 900 | } |
| 901 | #ifdef _WIN32 |
| 902 | /* On Windows executables carry an extension (PATHEXT). A CLI like |
| 903 | * opencode is often installed as a .cmd / .ps1 / .exe shim (e.g. via |
| 904 | * mise or npm), so the bare-name probe above misses it (#221). Try the |
| 905 | * common executable extensions before moving to the next PATH entry. */ |
| 906 | static const char *const win_exts[] = {".exe", ".cmd", ".bat", ".ps1", NULL}; |
| 907 | for (int i = 0; win_exts[i]; i++) { |
| 908 | snprintf(out, out_sz, "%s/%s%s", dir, name, win_exts[i]); |
| 909 | if (is_executable(out)) { |
| 910 | return true; |
| 911 | } |
| 912 | } |
| 913 | #endif |
| 914 | dir = strtok_r(NULL, PATH_DELIM, &saveptr); |
| 915 | } |
| 916 | return false; |
| 917 | } |
| 918 | |
| 919 | const char *cbm_find_cli(const char *name, const char *home_dir) { |
| 920 | static char buf[CLI_BUF_512]; |
no test coverage detected