2026-01-06 09:48:38 +08:00
|
|
|
#include "common/path.h"
|
|
|
|
|
#include "common/io.h"
|
|
|
|
|
#include "common/stringUtils.h"
|
2024-08-20 16:37:54 +08:00
|
|
|
|
2025-04-07 16:38:13 +08:00
|
|
|
#if !_WIN32
|
2024-08-20 16:37:54 +08:00
|
|
|
const char* ffFindExecutableInPath(const char* name, FFstrbuf* result)
|
|
|
|
|
{
|
|
|
|
|
char* path = getenv("PATH");
|
|
|
|
|
if(!path)
|
|
|
|
|
return "$PATH not set";
|
|
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
const bool appendExe = !ffStrEndsWithIgnCase(name, ".exe");
|
|
|
|
|
#endif
|
|
|
|
|
|
2024-08-23 10:17:06 +08:00
|
|
|
for (char* token = path; *token; path = token + 1)
|
2024-08-20 16:37:54 +08:00
|
|
|
{
|
2024-08-23 10:17:06 +08:00
|
|
|
token = strchr(path,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
';'
|
|
|
|
|
#else
|
|
|
|
|
':'
|
|
|
|
|
#endif
|
|
|
|
|
);
|
|
|
|
|
if (!token) token = path + strlen(path);
|
|
|
|
|
|
2024-08-20 16:37:54 +08:00
|
|
|
ffStrbufSetNS(result, (uint32_t)(token - path), path);
|
|
|
|
|
ffStrbufEnsureEndsWithC(result,
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
'\\'
|
|
|
|
|
#else
|
|
|
|
|
'/'
|
|
|
|
|
#endif
|
|
|
|
|
);
|
|
|
|
|
ffStrbufAppendS(result, name);
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
if (appendExe) ffStrbufAppendS(result, ".exe");
|
|
|
|
|
if (!ffPathExists(result->chars, FF_PATHTYPE_FILE))
|
|
|
|
|
continue;
|
|
|
|
|
#else
|
2025-04-01 16:27:11 +08:00
|
|
|
if (access(result->chars, X_OK) != 0)
|
2024-08-20 16:37:54 +08:00
|
|
|
continue;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
ffStrbufClear(result);
|
|
|
|
|
return "Executable not found";
|
|
|
|
|
}
|
2025-04-07 16:38:13 +08:00
|
|
|
#else
|
|
|
|
|
#include <windows.h>
|
|
|
|
|
|
|
|
|
|
const char* ffFindExecutableInPath(const char* name, FFstrbuf* result)
|
|
|
|
|
{
|
|
|
|
|
char buffer[MAX_PATH + 1];
|
|
|
|
|
DWORD length = SearchPathA(NULL, name, ".exe", sizeof(buffer), buffer, NULL);
|
|
|
|
|
if (length == 0)
|
|
|
|
|
{
|
|
|
|
|
ffStrbufClear(result);
|
|
|
|
|
return "Executable not found";
|
|
|
|
|
}
|
|
|
|
|
ffStrbufSetS(result, buffer);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#endif
|