summaryrefslogtreecommitdiffstats
path: root/contrib/restricted/emscripten/system/lib/libc/wasi-helpers.c
blob: 0eaba304e980eb4438fd9dcddc13c3d30855bbd0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/*
 * Copyright 2019 The Emscripten Authors.  All rights reserved.
 * Emscripten is available under two separate licenses, the MIT license and the
 * University of Illinois/NCSA Open Source License.  Both these licenses can be
 * found in the LICENSE file.
 */

#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <wasi/api.h>
#include <wasi/wasi-helpers.h>

int __wasi_syscall_ret(__wasi_errno_t code) {
  if (code == __WASI_ERRNO_SUCCESS) return 0;
  // We depend on the fact that wasi codes are identical to our errno codes.
  errno = code;
  return -1;
}

int  __wasi_fd_is_valid(__wasi_fd_t fd) {
  __wasi_fdstat_t statbuf;
  int err = __wasi_fd_fdstat_get(fd, &statbuf);
  if (err != __WASI_ERRNO_SUCCESS) {
    errno = err;
    return 0;
  }
  return 1;
}

#define NSEC_PER_SEC (1000 * 1000 * 1000)

struct timespec __wasi_timestamp_to_timespec(__wasi_timestamp_t timestamp) {
  return (struct timespec){.tv_sec = timestamp / NSEC_PER_SEC,
                           .tv_nsec = timestamp % NSEC_PER_SEC};
}