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
(backport <rust-lang#3681>)
(cherry picked from commit 1edaad1)
  • Loading branch information
youknowone authored and tgross35 committed Aug 15, 2024
1 parent 9105f6a commit 191c59f
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 as usize],
}
}

// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
Expand Down Expand Up @@ -442,6 +447,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 @@ -737,6 +764,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 191c59f

Please sign in to comment.