LibCore: Add System::exec_command method

This method was taken from the pls utility and its purpose is to execute
a given command with all the required requirements such as providing a
suitable exec environment.
This commit is contained in:
Liav A 2022-11-04 11:50:40 +02:00 committed by Andrew Kaster
parent 942e262e86
commit 0d1af1ad63
2 changed files with 28 additions and 0 deletions

View file

@ -979,6 +979,29 @@ ErrorOr<void> adjtime(const struct timeval* delta, struct timeval* old_delta)
}
#endif
#ifdef AK_OS_SERENITY
ErrorOr<void> exec_command(Vector<StringView>& command, bool preserve_env)
{
Vector<StringView> exec_environment;
for (size_t i = 0; environ[i]; ++i) {
StringView env_view { environ[i], strlen(environ[i]) };
auto maybe_needle = env_view.find('=');
if (!maybe_needle.has_value())
continue;
// FIXME: Allow a custom selection of variables once ArgsParser supports options with optional parameters.
if (!preserve_env && env_view.substring_view(0, maybe_needle.value()) != "TERM"sv)
continue;
exec_environment.append(env_view);
}
TRY(Core::System::exec(command.at(0), command, Core::System::SearchInPath::Yes, exec_environment));
return {};
}
#endif
ErrorOr<void> exec(StringView filename, Span<StringView> arguments, SearchInPath search_in_path, Optional<Span<StringView>> environment)
{
#ifdef AK_OS_SERENITY

View file

@ -165,6 +165,11 @@ enum class SearchInPath {
No,
Yes,
};
#ifdef AK_OS_SERENITY
ErrorOr<void> exec_command(Vector<StringView>& command, bool preserve_env);
#endif
ErrorOr<void> exec(StringView filename, Span<StringView> arguments, SearchInPath, Optional<Span<StringView>> environment = {});
ErrorOr<int> socket(int domain, int type, int protocol);