Skip to content

Commit

Permalink
Merge pull request #3681 from youknowone/wasi-select
Browse files Browse the repository at this point in the history
Add wasi select, FD_SET, FD_ZERO, FD_ISSET
  • Loading branch information
tgross35 authored Aug 15, 2024
2 parents bcd5546 + e9da8a0 commit 50b4720
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
5 changes: 5 additions & 0 deletions libc-test/semver/wasi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
fd_set
FD_SET
FD_ZERO
FD_ISSET
select
35 changes: 35 additions & 0 deletions src/wasi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,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 @@ -446,6 +451,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 @@ -741,6 +768,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 50b4720

Please sign in to comment.