From d8b960be46007f9c09356e526d3c2dcff4b186a5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 23 Oct 2022 13:41:27 -0400 Subject: Add filesystem API --- src/system.c | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/system.c (limited to 'src/system.c') 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 +// SPDX-License-Identifier: ISC + +#include "system.h" + +#include "errno_status.h" + +#include "zix/status.h" + +#ifdef _WIN32 +# include +#else +# include +#endif + +#include +#include + +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; +} -- cgit v1.2.1