From 38677f63c6fa63e1477fde20806781384ec193f4 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Tue, 28 Jun 2022 18:09:57 +0200 Subject: [PATCH] pls: Don't rely on `getenv` to find `TERM` This allows us to skip storing the actual backing data in a separate `Vector`, as we know that we are working with `environ`-backed storage here. Also, while the logic is currently very similar to what `getenv` does internally, this allows us to eventually implement custom environment variable filters while remaining linear in run time. --- Userland/Utilities/pls.cpp | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/Userland/Utilities/pls.cpp b/Userland/Utilities/pls.cpp index 7ada606c75..356eff18ff 100644 --- a/Userland/Utilities/pls.cpp +++ b/Userland/Utilities/pls.cpp @@ -45,11 +45,18 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio rpath exec")); - Vector exec_environment_strings; Vector exec_environment; - if (auto* term = getenv("TERM")) { - exec_environment_strings.append(String::formatted("TERM={}", term)); - exec_environment.append(exec_environment_strings.last()); + for (size_t i = 0; environ[i]; ++i) { + StringView env_view { environ[i] }; + auto maybe_needle = env_view.find('='); + + if (!maybe_needle.has_value()) + continue; + + if (env_view.substring_view(0, maybe_needle.value()) != "TERM"sv) + continue; + + exec_environment.append(env_view); } Vector exec_arguments;