Skip to content

Commit 32a7684

Browse files
committed
wasi: Implement error_string to get readable errors
This routes the `error_string` API to `strerror` in libc which should have more human readable descriptions.
1 parent 60f6cbd commit 32a7684

File tree

1 file changed

+15
-2
lines changed

1 file changed

+15
-2
lines changed

src/libstd/sys/wasi/os.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,21 @@ pub fn errno() -> i32 {
2727
unsafe { errno as i32 }
2828
}
2929

30-
pub fn error_string(_errno: i32) -> String {
31-
"operation failed".to_string()
30+
pub fn error_string(errno: i32) -> String {
31+
extern {
32+
fn strerror_r(errnum: libc::c_int, buf: *mut libc::c_char,
33+
buflen: libc::size_t) -> libc::c_int;
34+
}
35+
36+
let mut buf = [0 as libc::c_char; 1024];
37+
38+
let p = buf.as_mut_ptr();
39+
unsafe {
40+
if strerror_r(errno as libc::c_int, p, buf.len()) < 0 {
41+
panic!("strerror_r failure");
42+
}
43+
str::from_utf8(CStr::from_ptr(p).to_bytes()).unwrap().to_owned()
44+
}
3245
}
3346

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

0 commit comments

Comments
 (0)