/* * Copyright (c) 2021, Andreas Kling * Copyright (c) 2022, MacDue * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #ifdef AK_OS_SERENITY # include #endif extern char** environ; namespace Core { struct ArgvList { String m_path; Vector m_argv; ArgvList(String path, size_t size) : m_path { path } { m_argv.ensure_capacity(size + 2); m_argv.append(m_path.characters()); } void append(char const* arg) { m_argv.append(arg); } Span get() { if (m_argv.is_empty() || m_argv.last() != nullptr) m_argv.append(nullptr); return m_argv; } ErrorOr spawn() { auto pid = TRY(System::posix_spawn(m_path.view(), nullptr, nullptr, const_cast(get().data()), environ)); #ifdef AK_OS_SERENITY TRY(System::disown(pid)); #endif return pid; } }; ErrorOr Process::spawn(StringView path, Span arguments) { ArgvList argv { path, arguments.size() }; for (auto const& arg : arguments) argv.append(arg.characters()); return argv.spawn(); } ErrorOr Process::spawn(StringView path, Span arguments) { Vector backing_strings; backing_strings.ensure_capacity(arguments.size()); ArgvList argv { path, arguments.size() }; for (auto const& arg : arguments) { backing_strings.append(arg); argv.append(backing_strings.last().characters()); } return argv.spawn(); } ErrorOr Process::spawn(StringView path, Span arguments) { ArgvList argv { path, arguments.size() }; for (auto arg : arguments) argv.append(arg); return argv.spawn(); } }