summaryrefslogtreecommitdiffstats
path: root/src/system.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2022-10-23 13:41:27 -0400
committerDavid Robillard <d@drobilla.net>2022-10-23 14:57:45 -0400
commitd8b960be46007f9c09356e526d3c2dcff4b186a5 (patch)
treecd40db8d5634e74f8795922b7ab19fc8c6507648 /src/system.c
parentc886d489576cd0bc33d7d22d81981c794067946f (diff)
downloadzix-d8b960be46007f9c09356e526d3c2dcff4b186a5.tar.gz
zix-d8b960be46007f9c09356e526d3c2dcff4b186a5.tar.bz2
zix-d8b960be46007f9c09356e526d3c2dcff4b186a5.zip
Add filesystem API
Diffstat (limited to 'src/system.c')
-rw-r--r--src/system.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/system.c b/src/system.c
new file mode 100644
index 0000000..eab568b
--- /dev/null
+++ b/src/system.c
@@ -0,0 +1,41 @@
+// Copyright 2007-2022 David Robillard <d@drobilla.net>
+// SPDX-License-Identifier: ISC
+
+#include "system.h"
+
+#include "errno_status.h"
+
+#include "zix/status.h"
+
+#ifdef _WIN32
+# include <io.h>
+#else
+# include <unistd.h>
+#endif
+
+#include <errno.h>
+#include <fcntl.h>
+
+int
+zix_system_open_fd(const char* const path, const int flags, const mode_t mode)
+{
+#ifdef O_CLOEXEC
+ return open(path, flags | O_CLOEXEC, mode); // NOLINT(hicpp-signed-bitwise)
+#else
+ return open(path, flags, mode);
+#endif
+}
+
+ZixStatus
+zix_system_close_fds(const int fd1, const int fd2)
+{
+ // Careful: we need to always close both files, but catch errno at any point
+
+ const ZixStatus st0 = zix_errno_status(errno);
+ const int r1 = fd1 >= 0 ? close(fd1) : 0;
+ const ZixStatus st1 = r1 ? ZIX_STATUS_SUCCESS : zix_errno_status(errno);
+ const int r2 = fd2 >= 0 ? close(fd2) : 0;
+ const ZixStatus st2 = r2 ? ZIX_STATUS_SUCCESS : zix_errno_status(errno);
+
+ return st0 ? st0 : st1 ? st1 : st2;
+}