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. */
| 254 | /* Search for an executable named `name` in the PATH environment variable. |
| 255 | * Returns the full path in `out` (max out_sz) if found, else empty string. */ |
| 256 | static bool find_in_path(const char *name, char *out, size_t out_sz) { |
| 257 | char path_copy[CLI_BUF_4K]; |
| 258 | if (!cbm_safe_getenv("PATH", path_copy, sizeof(path_copy), NULL)) { |
| 259 | return false; |
| 260 | } |
| 261 | char *saveptr; |
| 262 | char *dir = strtok_r(path_copy, PATH_DELIM, &saveptr); |
| 263 | while (dir) { |
| 264 | snprintf(out, out_sz, "%s/%s", dir, name); |
| 265 | if (is_executable(out)) { |
| 266 | return true; |
| 267 | } |
| 268 | #ifdef _WIN32 |
| 269 | /* On Windows executables carry an extension (PATHEXT). A CLI like |
| 270 | * opencode is often installed as a .cmd / .ps1 / .exe shim (e.g. via |
| 271 | * mise or npm), so the bare-name probe above misses it (#221). Try the |
| 272 | * common executable extensions before moving to the next PATH entry. */ |
| 273 | static const char *const win_exts[] = {".exe", ".cmd", ".bat", ".ps1", NULL}; |
| 274 | for (int i = 0; win_exts[i]; i++) { |
| 275 | snprintf(out, out_sz, "%s/%s%s", dir, name, win_exts[i]); |
| 276 | if (is_executable(out)) { |
| 277 | return true; |
| 278 | } |
| 279 | } |
| 280 | #endif |
| 281 | dir = strtok_r(NULL, PATH_DELIM, &saveptr); |
| 282 | } |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | const char *cbm_find_cli(const char *name, const char *home_dir) { |
| 287 | static char buf[CLI_BUF_512]; |
no test coverage detected