Skip to content

Commit

Permalink
Add wasi select, FD_SET, FD_ZERO, FD_ISSET
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowone authored and JohnTitor committed Jun 18, 2024
1 parent 42446e9 commit 55e51a5
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ s! {
pub st_ctim: timespec,
__reserved: [c_longlong; 3],
}

pub struct fd_set {
__nfds: usize,
__fds: [c_int; FD_SETSIZE],
}
}

// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
Expand Down Expand Up @@ -438,6 +443,28 @@ pub const NOEXPR: ::nl_item = 0x50001;
pub const YESSTR: ::nl_item = 0x50002;
pub const NOSTR: ::nl_item = 0x50003;

f! {
pub fn FD_ISSET(fd: ::c_int, set: *const fd_set) -> bool {
let set = &*set;
let n = set.__nfds;
return set.__fds[..n].iter().any(|p| *p == fd)
}

pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () {
let set = &mut *set;
let n = set.__nfds;
if !set.__fds[..n].iter().any(|p| *p == fd) {
set.__nfds = n + 1;
set.__fds[n] = fd;
}
}

pub fn FD_ZERO(set: *mut fd_set) -> () {
(*set).__nfds = 0;
return
}
}

#[cfg_attr(
feature = "rustc-dep-of-std",
link(
Expand Down Expand Up @@ -733,6 +760,14 @@ extern "C" {
pub fn nl_langinfo(item: ::nl_item) -> *mut ::c_char;
pub fn nl_langinfo_l(item: ::nl_item, loc: ::locale_t) -> *mut ::c_char;

pub fn select(
nfds: c_int,
readfds: *mut fd_set,
writefds: *mut fd_set,
errorfds: *mut fd_set,
timeout: *const timeval,
) -> c_int;

pub fn __wasilibc_register_preopened_fd(fd: c_int, path: *const c_char) -> c_int;
pub fn __wasilibc_fd_renumber(fd: c_int, newfd: c_int) -> c_int;
pub fn __wasilibc_unlinkat(fd: c_int, path: *const c_char) -> c_int;
Expand Down

0 comments on commit 55e51a5

Please sign in to comment.