aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/execv.ccm
blob: ef9d259ad5d7a53e2a51db7aaf60db852d0bc308 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// This source code is released into the public domain.
module;

#include <expected>
#include <filesystem>
#include <string>

#include <unistd.h>

export module nihil.posix:execv;

import nihil.error;
import :argv;
import :executor;

namespace nihil {

// execv: use a filename and an argument vector to call ::execv().
export struct execv final
{
	using tag = exec_tag;

	execv(std::filesystem::path path, argv &&args) noexcept
		: m_path(std::move(path))
		, m_args(std::move(args))
	{
	}

	~execv() = default;

	// Movable
	execv(execv &&) noexcept = default;
	auto operator=(execv &&) noexcept -> execv & = default;

	// Not copyable (because m_args isn't copyable).
	execv(execv const &) = delete;
	auto operator=(this execv &, execv const &) -> execv & = delete;

	// Perform the execv().  This only returns on failure.
	[[nodiscard]] auto exec(this execv &self) -> std::expected<void, error>
	{
		::execv(self.m_path.string().c_str(), self.m_args.data());
		return std::unexpected(error("execve failed", error(std::errc(errno))));
	}

private:
	std::filesystem::path m_path;
	argv m_args;
};

} // namespace nihil