From 8c1ab65c0f548d20b7f177bdb736daaf603340e1 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 15:54:38 +0200 Subject: Adding upstream version 0.0~git20221206.8b7148f. Signed-off-by: Daniel Baumann --- libc-bottom-half/cloudlibc/src/libc/unistd/pread.c | 31 ++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 libc-bottom-half/cloudlibc/src/libc/unistd/pread.c (limited to 'libc-bottom-half/cloudlibc/src/libc/unistd/pread.c') diff --git a/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c new file mode 100644 index 0000000..c9944bc --- /dev/null +++ b/libc-bottom-half/cloudlibc/src/libc/unistd/pread.c @@ -0,0 +1,31 @@ +// Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/ +// +// SPDX-License-Identifier: BSD-2-Clause + +#include +#include +#include + +ssize_t pread(int fildes, void *buf, size_t nbyte, off_t offset) { + if (offset < 0) { + errno = EINVAL; + return -1; + } + __wasi_iovec_t iov = {.buf = buf, .buf_len = nbyte}; + size_t bytes_read; + __wasi_errno_t error = + __wasi_fd_pread(fildes, &iov, 1, offset, &bytes_read); + if (error != 0) { + __wasi_fdstat_t fds; + if (error == ENOTCAPABLE && __wasi_fd_fdstat_get(fildes, &fds) == 0) { + // Determine why we got ENOTCAPABLE. + if ((fds.fs_rights_base & __WASI_RIGHTS_FD_READ) == 0) + error = EBADF; + else + error = ESPIPE; + } + errno = error; + return -1; + } + return bytes_read; +} -- cgit v1.2.3