1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 11:37:34 +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:
Tim Schumacher 2022-08-20 18:31:03 +02:00 committed by Linus Groh
parent 39a3775f48
commit 5f99934dce
13 changed files with 74 additions and 95 deletions

View file

@ -232,9 +232,9 @@ int Shell::builtin_type(int argc, char const** argv)
}
// check if its an executable in PATH
auto fullpath = Core::find_executable_in_path(command);
if (!fullpath.is_empty()) {
printf("%s is %s\n", command.characters(), escape_token(fullpath).characters());
auto fullpath = Core::File::resolve_executable_from_environment(command);
if (fullpath.has_value()) {
printf("%s is %s\n", command.characters(), escape_token(fullpath.release_value()).characters());
continue;
}
something_not_found = true;
@ -1075,12 +1075,12 @@ int Shell::builtin_kill(int argc, char const** argv)
{
// Simply translate the arguments and pass them to `kill'
Vector<String> replaced_values;
auto kill_path = find_in_path("kill"sv);
if (kill_path.is_empty()) {
auto kill_path = Core::File::resolve_executable_from_environment("kill"sv);
if (!kill_path.has_value()) {
warnln("kill: `kill' not found in PATH");
return 126;
}
replaced_values.append(kill_path);
replaced_values.append(kill_path.release_value());
for (auto i = 1; i < argc; ++i) {
if (auto job_id = resolve_job_spec({ argv[i], strlen(argv[1]) }); job_id.has_value()) {
auto job = find_job(job_id.value());

View file

@ -1338,27 +1338,6 @@ String Shell::unescape_token(StringView token)
return builder.build();
}
String Shell::find_in_path(StringView program_name)
{
String path = getenv("PATH");
if (!path.is_empty()) {
auto directories = path.split(':');
for (auto const& directory : directories) {
Core::DirIterator programs(directory.characters(), Core::DirIterator::SkipDots);
while (programs.has_next()) {
auto program = programs.next_path();
auto program_path = String::formatted("{}/{}", directory, program);
if (access(program_path.characters(), X_OK) != 0)
continue;
if (program == program_name)
return program_path;
}
}
}
return {};
}
void Shell::cache_path()
{
if (!m_is_interactive)
@ -1387,6 +1366,7 @@ void Shell::cache_path()
cached_path.append({ RunnablePath::Kind::Alias, name });
}
// TODO: Can we make this rely on Core::File::resolve_executable_from_environment()?
String path = getenv("PATH");
if (!path.is_empty()) {
auto directories = path.split(':');

View file

@ -157,8 +157,6 @@ public:
String resolve_path(String) const;
String resolve_alias(StringView) const;
static String find_in_path(StringView program_name);
static bool has_history_event(StringView);
RefPtr<AST::Value> get_argument(size_t) const;