aboutsummaryrefslogtreecommitdiffstats
path: root/nihil/open_file.ccm
diff options
context:
space:
mode:
Diffstat (limited to 'nihil/open_file.ccm')
-rw-r--r--nihil/open_file.ccm35
1 files changed, 35 insertions, 0 deletions
diff --git a/nihil/open_file.ccm b/nihil/open_file.ccm
new file mode 100644
index 0000000..38fedbd
--- /dev/null
+++ b/nihil/open_file.ccm
@@ -0,0 +1,35 @@
+/*
+ * This source code is released into the public domain.
+ */
+
+module;
+
+#include <expected>
+#include <filesystem>
+#include <system_error>
+
+#include <fcntl.h>
+#include <unistd.h>
+
+export module nihil:open_file;
+
+import :fd;
+
+namespace nihil {
+
+/*
+ * Open the given file and return an fd for it.
+ */
+auto open_file(std::filesystem::path const &filename,
+ int flags,
+ int mode = 0777)
+ -> std::expected<fd, std::error_code>
+{
+ auto fdno = ::open(filename.c_str(), flags, mode);
+ if (fdno != -1)
+ return fd(fdno);
+
+ return std::unexpected(std::make_error_code(std::errc(errno)));
+}
+
+} // namespace nihil