Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/system/lib/libc/wasi-helpers.c
6162 views
1
/*
2
* Copyright 2019 The Emscripten Authors. All rights reserved.
3
* Emscripten is available under two separate licenses, the MIT license and the
4
* University of Illinois/NCSA Open Source License. Both these licenses can be
5
* found in the LICENSE file.
6
*/
7
8
#include <errno.h>
9
#include <stdlib.h>
10
#include <time.h>
11
#include <wasi/api.h>
12
#include <wasi/wasi-helpers.h>
13
14
int __wasi_syscall_ret(__wasi_errno_t code) {
15
if (code == __WASI_ERRNO_SUCCESS) return 0;
16
// We depend on the fact that wasi codes are identical to our errno codes.
17
errno = code;
18
return -1;
19
}
20
21
int __wasi_fd_is_valid(__wasi_fd_t fd) {
22
__wasi_fdstat_t statbuf;
23
int err = __wasi_fd_fdstat_get(fd, &statbuf);
24
if (err != __WASI_ERRNO_SUCCESS) {
25
errno = err;
26
return 0;
27
}
28
return 1;
29
}
30
31
#define NSEC_PER_SEC (1000 * 1000 * 1000)
32
33
struct timespec __wasi_timestamp_to_timespec(__wasi_timestamp_t timestamp) {
34
return (struct timespec){.tv_sec = timestamp / NSEC_PER_SEC,
35
.tv_nsec = timestamp % NSEC_PER_SEC};
36
}
37
38