Skip to content

Commit 191c59f

Browse files
youknowonetgross35
authored andcommitted
Add wasi select, FD_SET, FD_ZERO, FD_ISSET
(backport <#3681>) (cherry picked from commit 1edaad1)
1 parent 9105f6a commit 191c59f

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/wasi.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,11 @@ s! {
175175
pub st_ctim: timespec,
176176
__reserved: [c_longlong; 3],
177177
}
178+
179+
pub struct fd_set {
180+
__nfds: usize,
181+
__fds: [c_int; FD_SETSIZE as usize],
182+
}
178183
}
179184

180185
// Declare dirent outside of s! so that it doesn't implement Copy, Eq, Hash,
@@ -442,6 +447,28 @@ pub const NOEXPR: ::nl_item = 0x50001;
442447
pub const YESSTR: ::nl_item = 0x50002;
443448
pub const NOSTR: ::nl_item = 0x50003;
444449

450+
f! {
451+
pub fn FD_ISSET(fd: ::c_int, set: *const fd_set) -> bool {
452+
let set = &*set;
453+
let n = set.__nfds;
454+
return set.__fds[..n].iter().any(|p| *p == fd)
455+
}
456+
457+
pub fn FD_SET(fd: ::c_int, set: *mut fd_set) -> () {
458+
let set = &mut *set;
459+
let n = set.__nfds;
460+
if !set.__fds[..n].iter().any(|p| *p == fd) {
461+
set.__nfds = n + 1;
462+
set.__fds[n] = fd;
463+
}
464+
}
465+
466+
pub fn FD_ZERO(set: *mut fd_set) -> () {
467+
(*set).__nfds = 0;
468+
return
469+
}
470+
}
471+
445472
#[cfg_attr(
446473
feature = "rustc-dep-of-std",
447474
link(
@@ -737,6 +764,14 @@ extern "C" {
737764
pub fn nl_langinfo(item: ::nl_item) -> *mut ::c_char;
738765
pub fn nl_langinfo_l(item: ::nl_item, loc: ::locale_t) -> *mut ::c_char;
739766

767+
pub fn select(
768+
nfds: c_int,
769+
readfds: *mut fd_set,
770+
writefds: *mut fd_set,
771+
errorfds: *mut fd_set,
772+
timeout: *const timeval,
773+
) -> c_int;
774+
740775
pub fn __wasilibc_register_preopened_fd(fd: c_int, path: *const c_char) -> c_int;
741776
pub fn __wasilibc_fd_renumber(fd: c_int, newfd: c_int) -> c_int;
742777
pub fn __wasilibc_unlinkat(fd: c_int, path: *const c_char) -> c_int;

0 commit comments

Comments
 (0)