// Copyright (c) 2015-2016 Nuxi, https://nuxi.nl/
//
// SPDX-License-Identifier: BSD-2-Clause

#include <wasi/api.h>
#include <errno.h>
#include <unistd.h>

ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {
  if (offset < 0) {
    errno = EINVAL;
    return -1;
  }
  __wasi_ciovec_t iov = {.buf = buf, .buf_len = nbyte};
  size_t bytes_written;
  __wasi_errno_t error =
      __wasi_fd_pwrite(fildes, &iov, 1, offset, &bytes_written);
  if (error != 0) {
    __wasi_fdstat_t fds;
#ifdef __wasilibc_unmodified_upstream
    if (error == ENOTCAPABLE && __wasi_fd_stat_get(fildes, &fds) == 0) {
#else
    if (error == ENOTCAPABLE && __wasi_fd_fdstat_get(fildes, &fds) == 0) {
#endif
      // Determine why we got ENOTCAPABLE.
#ifdef __wasilibc_unmodified_upstream // generated constant names
      if ((fds.fs_rights_base & __WASI_RIGHT_FD_WRITE) == 0)
#else
      if ((fds.fs_rights_base & __WASI_RIGHTS_FD_WRITE) == 0)
#endif
        error = EBADF;
      else
        error = ESPIPE;
    }
    errno = error;
    return -1;
  }
  return bytes_written;
}
