Skip to content

Commit

Permalink
wasi: Implement error_string to get readable errors
Browse files Browse the repository at this point in the history
This routes the `error_string` API to `strerror` in libc which should
have more human readable descriptions.
  • Loading branch information
alexcrichton committed Apr 1, 2019
1 parent 60f6cbd commit 32a7684
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/libstd/sys/wasi/os.rs
Expand Up @@ -27,8 +27,21 @@ pub fn errno() -> i32 {
unsafe { errno as i32 }
}

pub fn error_string(_errno: i32) -> String {
"operation failed".to_string()
pub fn error_string(errno: i32) -> String {
extern {
fn strerror_r(errnum: libc::c_int, buf: *mut libc::c_char,
buflen: libc::size_t) -> libc::c_int;
}

let mut buf = [0 as libc::c_char; 1024];

let p = buf.as_mut_ptr();
unsafe {
if strerror_r(errno as libc::c_int, p, buf.len()) < 0 {
panic!("strerror_r failure");
}
str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap().to_owned()
}
}

pub fn getcwd() -> io::Result<PathBuf> {
Expand Down

0 comments on commit 32a7684

Please sign in to comment.