mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:57:45 +00:00
Userland: Consolidate most PATH resolving into a single implementation
We previously had at least three different implementations for resolving executables in the PATH, all of which had slightly different characteristics. Merge those into a single implementation to keep the behaviour consistent, and maybe to make that implementation more configurable in the future.
This commit is contained in:
parent
39a3775f48
commit
5f99934dce
13 changed files with 74 additions and 95 deletions
|
@ -557,4 +557,35 @@ ErrorOr<void, File::RemoveError> File::remove(String const& path, RecursionMode
|
|||
return {};
|
||||
}
|
||||
|
||||
Optional<String> File::resolve_executable_from_environment(StringView filename)
|
||||
{
|
||||
if (filename.is_empty())
|
||||
return {};
|
||||
|
||||
// Paths that aren't just a file name generally count as already resolved.
|
||||
if (filename.contains('/')) {
|
||||
if (access(String { filename }.characters(), X_OK) != 0)
|
||||
return {};
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
auto const* path_str = getenv("PATH");
|
||||
StringView path { path_str, strlen(path_str) };
|
||||
|
||||
if (path.is_empty())
|
||||
path = DEFAULT_PATH_SV;
|
||||
|
||||
auto directories = path.split_view(':');
|
||||
|
||||
for (auto directory : directories) {
|
||||
auto file = String::formatted("{}/{}", directory, filename);
|
||||
|
||||
if (access(file.characters(), X_OK) == 0)
|
||||
return file;
|
||||
}
|
||||
|
||||
return {};
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue