aboutsummaryrefslogtreecommitdiffstats
path: root/nihil.posix/fd.ccm
diff options
context:
space:
mode:
authorLexi Winter <lexi@le-fay.org>2025-07-01 17:07:04 +0100
committerLexi Winter <lexi@le-fay.org>2025-07-01 17:07:04 +0100
commit2e2d1bd3b6c7776b77c33b94f30ead89367a71e6 (patch)
tree54d37ffadf8e677938d9b7a28e4e9b71be1e75c1 /nihil.posix/fd.ccm
parent36427c0966faa7aecd586b397ed9b845f18172f5 (diff)
downloadnihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.gz
nihil-2e2d1bd3b6c7776b77c33b94f30ead89367a71e6.tar.bz2
add nihil.std
Diffstat (limited to 'nihil.posix/fd.ccm')
-rw-r--r--nihil.posix/fd.ccm31
1 files changed, 15 insertions, 16 deletions
diff --git a/nihil.posix/fd.ccm b/nihil.posix/fd.ccm
index 7faf2f1..8210b6d 100644
--- a/nihil.posix/fd.ccm
+++ b/nihil.posix/fd.ccm
@@ -1,24 +1,23 @@
// This source code is released into the public domain.
module;
-#include <coroutine>
-#include <expected>
-#include <ranges>
-#include <span>
-#include <stdexcept>
-#include <system_error>
-
#include <fcntl.h>
#include <unistd.h>
export module nihil.posix:fd;
+import nihil.std;
import nihil.flagset;
import nihil.error;
import nihil.monad;
namespace nihil {
+// Useful constants
+export inline int constexpr stdin_fileno = STDIN_FILENO;
+export inline int constexpr stdout_fileno = STDOUT_FILENO;
+export inline int constexpr stderr_fileno = STDERR_FILENO;
+
// F_{GET,SET}FL flags
struct fd_flags_tag
{
@@ -96,7 +95,7 @@ export struct fd final
if (ret == 0)
return {};
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
// Return the stored fd.
@@ -124,7 +123,7 @@ export struct fd final
if (ret >= 0)
return ret;
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
// Read data from the fd to the provided buffer. Returns a
@@ -136,7 +135,7 @@ export struct fd final
if (ret >= 0)
return buffer.subspan(0, ret);
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
private:
@@ -152,7 +151,7 @@ export [[nodiscard]] auto dup(fd const &self) -> std::expected<fd, error>
if (newfd != -1)
return fd(newfd);
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
// Create a copy of this fd by calling dup2(). Note that because this results
@@ -170,7 +169,7 @@ export [[nodiscard]] auto dup(fd const &self, int newfd) -> std::expected<fd, er
if (ret != -1)
return fd(newfd);
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
// Create a copy of this fd by calling dup().
@@ -180,7 +179,7 @@ export [[nodiscard]] auto raw_dup(fd const &self) -> std::expected<int, error>
if (newfd != -1)
return newfd;
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
// Create a copy of this fd by calling dup2().
@@ -190,7 +189,7 @@ export [[nodiscard]] auto raw_dup(fd const &self, int newfd) -> std::expected<in
if (ret != -1)
return newfd;
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
}
// Call fcntl() on this fd. Prefer one of the type-safe wrappers to this, if available.
@@ -199,7 +198,7 @@ export [[nodiscard]] auto fcntl(fd const &fd, int op, int arg = 0)
{
auto const ret = ::fcntl(fd.get(), op, arg);
if (ret == -1)
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
return ret;
}
@@ -277,7 +276,7 @@ export [[nodiscard]] auto pipe() -> std::expected<std::pair<fd, fd>, error>
auto fds = std::array<int, 2>{};
if (auto const ret = ::pipe(fds.data()); ret != 0)
- return std::unexpected(error(std::errc(errno)));
+ return std::unexpected(error(sys_error()));
return {{fd(fds[0]), fd(fds[1])}};
}