diff --git a/Userland/Libraries/LibCore/System.cpp b/Userland/Libraries/LibCore/System.cpp index a277a5dd56..4679f25637 100644 --- a/Userland/Libraries/LibCore/System.cpp +++ b/Userland/Libraries/LibCore/System.cpp @@ -1655,4 +1655,39 @@ ErrorOr posix_fallocate(int fd, off_t offset, off_t length) } #endif +// This constant is copied from LibFileSystem. We cannot use or even include it directly, +// because that would cause a dependency of LibCore on LibFileSystem, effectively rendering +// the distinction between these libraries moot. +static constexpr StringView INTERNAL_DEFAULT_PATH_SV = "/usr/local/sbin:/usr/local/bin:/usr/bin:/bin"sv; + +ErrorOr resolve_executable_from_environment(StringView filename, int flags) +{ + if (filename.is_empty()) + return Error::from_errno(ENOENT); + + // Paths that aren't just a file name generally count as already resolved. + if (filename.contains('/')) { + TRY(Core::System::access(filename, X_OK, flags)); + return TRY(String::from_utf8(filename)); + } + + auto const* path_str = ::getenv("PATH"); + StringView path; + if (path_str) + path = { path_str, strlen(path_str) }; + if (path.is_empty()) + path = INTERNAL_DEFAULT_PATH_SV; + + auto directories = path.split_view(':'); + + for (auto directory : directories) { + auto file = TRY(String::formatted("{}/{}", directory, filename)); + + if (!Core::System::access(file, X_OK, flags).is_error()) + return file; + } + + return Error::from_errno(ENOENT); +} + } diff --git a/Userland/Libraries/LibCore/System.h b/Userland/Libraries/LibCore/System.h index 7dba27fe6f..98d1f7b6d2 100644 --- a/Userland/Libraries/LibCore/System.h +++ b/Userland/Libraries/LibCore/System.h @@ -267,4 +267,6 @@ ErrorOr getaddrinfo(char const* nodename, char const* servnam ErrorOr posix_fallocate(int fd, off_t offset, off_t length); #endif +ErrorOr resolve_executable_from_environment(StringView filename, int flags = 0); + } diff --git a/Userland/Libraries/LibFileSystem/FileSystem.cpp b/Userland/Libraries/LibFileSystem/FileSystem.cpp index 41c10b8f22..8e4e6b8c78 100644 --- a/Userland/Libraries/LibFileSystem/FileSystem.cpp +++ b/Userland/Libraries/LibFileSystem/FileSystem.cpp @@ -361,32 +361,8 @@ ErrorOr link_file(StringView destination_path, StringView source_path) ErrorOr resolve_executable_from_environment(StringView filename, int flags) { - if (filename.is_empty()) - return Error::from_errno(ENOENT); - - // Paths that aren't just a file name generally count as already resolved. - if (filename.contains('/')) { - TRY(Core::System::access(filename, X_OK, flags)); - return TRY(String::from_utf8(filename)); - } - - auto const* path_str = ::getenv("PATH"); - StringView path; - if (path_str) - 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 = TRY(String::formatted("{}/{}", directory, filename)); - - if (!Core::System::access(file, X_OK, flags).is_error()) - return file; - } - - return Error::from_errno(ENOENT); + // FIXME: Callers should Call Core::System::resolve_executable_from_environment instead of FileSystem::resolve_executable_from_environment. + return Core::System::resolve_executable_from_environment(filename, flags); } bool looks_like_shared_library(StringView path)