mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 08:37:45 +00:00
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:
parent
942e262e86
commit
0d1af1ad63
2 changed files with 28 additions and 0 deletions
|
@ -979,6 +979,29 @@ ErrorOr<void> adjtime(const struct timeval* delta, struct timeval* old_delta)
|
||||||
}
|
}
|
||||||
#endif
|
#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)
|
ErrorOr<void> exec(StringView filename, Span<StringView> arguments, SearchInPath search_in_path, Optional<Span<StringView>> environment)
|
||||||
{
|
{
|
||||||
#ifdef AK_OS_SERENITY
|
#ifdef AK_OS_SERENITY
|
||||||
|
|
|
@ -165,6 +165,11 @@ enum class SearchInPath {
|
||||||
No,
|
No,
|
||||||
Yes,
|
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<void> exec(StringView filename, Span<StringView> arguments, SearchInPath, Optional<Span<StringView>> environment = {});
|
||||||
|
|
||||||
ErrorOr<int> socket(int domain, int type, int protocol);
|
ErrorOr<int> socket(int domain, int type, int protocol);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue