aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/execvp.cc
diff options
context:
space:
mode:
Diffstat (limited to 'nihil.posix/execvp.cc')
-rw-r--r--nihil.posix/execvp.cc50
1 files changed, 50 insertions, 0 deletions
diff --git a/nihil.posix/execvp.cc b/nihil.posix/execvp.cc
new file mode 100644
index 0000000..5eac315
--- /dev/null
+++ b/nihil.posix/execvp.cc
@@ -0,0 +1,50 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+module;
+
+#include <coroutine>
+#include <expected>
+#include <filesystem>
+#include <format>
+#include <string>
+#include <utility>
+
+#include <err.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include "nihil.hh"
+
+module nihil.posix;
+
+import nihil.error;
+import nihil.monad;
+
+namespace nihil {
+#ifdef NIHIL_HAVE_FEXECVE
+
+auto execvp(std::string_view file, argv &&argv) -> std::expected<fexecv, error>
+{
+ auto execfd = open_in_path(file);
+ if (!execfd)
+ return std::unexpected(error(
+ std::format("executable not found in path: {}", file)));
+ return fexecv(std::move(*execfd), std::move(argv));
+}
+
+#else // !NIHIL_HAVE_FEXECVE
+
+auto execvp(std::string_view file, nihil::argv &&argv) -> std::expected<nihil::execv, nihil::error>
+{
+ auto filename = nihil::find_in_path(file);
+ if (!filename)
+ return std::unexpected(nihil::error(
+ std::format("executable not found in path: {}", file)));
+ return execv(std::move(*filename), std::move(argv));
+}
+
+#endif // NIHIL_HAVE_FEXECVE
+
+} // namespace nihil