2019-10-22 23:36:05 +00:00
|
|
|
// Args:
|
|
|
|
// mapdir: .:wasitests/test_fs/hamlet
|
|
|
|
|
|
|
|
use std::fs;
|
|
|
|
#[cfg(target_os = "wasi")]
|
|
|
|
use std::os::wasi::prelude::AsRawFd;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
#[cfg(target_os = "wasi")]
|
|
|
|
#[link(wasm_import_module = "wasi_unstable")]
|
|
|
|
extern "C" {
|
|
|
|
fn fd_close(fd: u32) -> u16;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
#[cfg(not(target_os = "wasi"))]
|
|
|
|
let mut base = PathBuf::from("wasitests/test_fs/hamlet");
|
|
|
|
#[cfg(target_os = "wasi")]
|
|
|
|
let mut base = PathBuf::from(".");
|
|
|
|
|
|
|
|
base.push("act3/scene3.txt");
|
|
|
|
let file = fs::File::open(&base).expect("could not open file");
|
|
|
|
|
|
|
|
#[cfg(target_os = "wasi")]
|
|
|
|
{
|
2019-10-23 00:29:19 +00:00
|
|
|
let file_fd = file.as_raw_fd();
|
2019-10-22 23:36:05 +00:00
|
|
|
let stdout_fd = std::io::stdout().as_raw_fd();
|
|
|
|
let stderr_fd = std::io::stderr().as_raw_fd();
|
|
|
|
let stdin_fd = std::io::stdin().as_raw_fd();
|
|
|
|
|
2019-10-23 00:29:19 +00:00
|
|
|
let result = unsafe { fd_close(file_fd) };
|
|
|
|
if result == 0 {
|
|
|
|
println!("Successfully closed file!")
|
|
|
|
} else {
|
|
|
|
println!("Could not close file");
|
|
|
|
}
|
|
|
|
|
2019-10-22 23:36:05 +00:00
|
|
|
let result = unsafe { fd_close(stderr_fd) };
|
|
|
|
if result == 0 {
|
|
|
|
println!("Successfully closed stderr!")
|
|
|
|
} else {
|
|
|
|
println!("Could not close stderr");
|
|
|
|
}
|
|
|
|
let result = unsafe { fd_close(stdin_fd) };
|
|
|
|
if result == 0 {
|
|
|
|
println!("Successfully closed stdin!")
|
|
|
|
} else {
|
|
|
|
println!("Could not close stdin");
|
|
|
|
}
|
|
|
|
let result = unsafe { fd_close(stdout_fd) };
|
|
|
|
if result == 0 {
|
|
|
|
println!("Successfully closed stdout!")
|
|
|
|
} else {
|
|
|
|
println!("Could not close stdout");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[cfg(not(target_os = "wasi"))]
|
|
|
|
{
|
2019-10-23 00:29:19 +00:00
|
|
|
println!("Successfully closed file!");
|
2019-10-22 23:36:05 +00:00
|
|
|
println!("Successfully closed stderr!");
|
|
|
|
println!("Successfully closed stdin!");
|
|
|
|
}
|
|
|
|
}
|