env: Use Core::System::exec()

This commit is contained in:
Sam Atkins 2022-04-02 16:48:43 +01:00 committed by Andreas Kling
parent f0aba519c3
commit f64ff945b2
Notes: sideshowbarker 2024-07-17 12:05:11 +09:00

View File

@ -8,7 +8,6 @@
#include <LibCore/DirIterator.h>
#include <LibCore/System.h>
#include <LibMain/Main.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -18,7 +17,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
bool ignore_env = false;
char const* split_string = nullptr;
Vector<char const*> values;
Vector<String> values;
Core::ArgsParser args_parser;
args_parser.set_stop_on_first_non_option(true);
@ -34,21 +33,17 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
size_t argv_start;
for (argv_start = 0; argv_start < values.size(); ++argv_start) {
if (StringView { values[argv_start] }.contains('=')) {
putenv(const_cast<char*>(values[argv_start]));
if (values[argv_start].contains('=')) {
putenv(const_cast<char*>(values[argv_start].characters()));
} else {
break;
}
}
Vector<String> split_string_storage;
Vector<char const*> new_argv;
Vector<StringView> new_argv;
if (split_string) {
for (auto view : StringView(split_string).split_view(' ')) {
split_string_storage.append(view);
}
for (auto& str : split_string_storage) {
new_argv.append(str.characters());
new_argv.append(view);
}
}
@ -63,12 +58,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 0;
}
new_argv.append(nullptr);
char const* executable = new_argv[0];
char* const* new_argv_ptr = const_cast<char* const*>(&new_argv[0]);
execvp(executable, new_argv_ptr);
perror("execvp");
TRY(Core::System::exec(new_argv[0], new_argv, Core::System::SearchInPath::Yes));
return 1;
}