Skip to content

Commit 770f653

Browse files
youknowoneJohnTitor
authored andcommitted
Add wasi select, FD_SET, FD_ZERO, FD_ISSET
1 parent 7d7151c commit 770f653

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/wasi.rs

+35
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,11 @@ s! {
178178
pub st_ctim: timespec,
179179
__reserved: [c_longlong; 3],
180180
}
181+
182+
pub struct fd_set {
183+
__nfds: usize,
184+
__fds: [c_int; FD_SETSIZE],
185+
}
181186
}
182187

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

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

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

0 commit comments

Comments
 (0)