ladybird/Userland/Libraries/LibCore/Process.cpp
sin-ack a3eeeaa6a1 Userland: Remove erroneous String -> char* -> StringView conversions
These were accidental (or leftover) uses of String::characters() to
construct StringViews through its StringView(char const*) constructor.
Since this constructor is due to be removed, this will no longer work.
Plus this prevents strlen from being run on these strings unnecessarily.
2022-07-12 23:11:35 +02:00

84 lines
1.9 KiB
C++

/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibCore/Process.h>
#include <LibCore/System.h>
#include <errno.h>
#include <spawn.h>
#ifdef __serenity__
# include <serenity.h>
#endif
extern char** environ;
namespace Core {
struct ArgvList {
String m_path;
Vector<char const*, 10> 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<char const*> get()
{
if (m_argv.is_empty() || m_argv.last() != nullptr)
m_argv.append(nullptr);
return m_argv;
}
ErrorOr<pid_t> spawn()
{
auto pid = TRY(System::posix_spawn(m_path.view(), nullptr, nullptr, const_cast<char**>(get().data()), environ));
#ifdef __serenity__
TRY(System::disown(pid));
#endif
return pid;
}
};
ErrorOr<pid_t> Process::spawn(StringView path, Span<String const> arguments)
{
ArgvList argv { path, arguments.size() };
for (auto const& arg : arguments)
argv.append(arg.characters());
return argv.spawn();
}
ErrorOr<pid_t> Process::spawn(StringView path, Span<StringView const> arguments)
{
Vector<String> 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<pid_t> Process::spawn(StringView path, Span<char const* const> arguments)
{
ArgvList argv { path, arguments.size() };
for (auto arg : arguments)
argv.append(arg);
return argv.spawn();
}
}