/* * Copyright (c) 2020, the SerenityOS developers. * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include int main(int argc, char** argv) { if (pledge("stdio rpath exec", nullptr) < 0) { perror("pledge"); return 1; } bool ignore_env = false; const char* split_string = nullptr; Vector values; Core::ArgsParser args_parser; args_parser.add_option(ignore_env, "Start with an empty environment", "ignore-environment", 'i'); args_parser.add_option(split_string, "Process and split S into separate arguments; used to pass multiple arguments on shebang lines", "split-string", 'S', "S"); args_parser.add_positional_argument(values, "Environment and commands", "env/command", Core::ArgsParser::Required::No); args_parser.parse(argc, argv); if (ignore_env) clearenv(); size_t argv_start; for (argv_start = 0; argv_start < values.size(); ++argv_start) { if (StringView { values[argv_start] }.contains('=')) { putenv(const_cast(values[argv_start])); } else { break; } } Vector split_string_storage; Vector 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()); } } for (size_t i = argv_start; i < values.size(); ++i) { new_argv.append(values[i]); } if (new_argv.size() == 0) { for (auto entry = environ; *entry != nullptr; ++entry) printf("%s\n", *entry); return 0; } new_argv.append(nullptr); const char* executable = new_argv[0]; char* const* new_argv_ptr = const_cast(&new_argv[0]); execvp(executable, new_argv_ptr); perror("execvp"); return 1; }