about summary refs log tree commit diff
path: root/src/libstd/sys
diff options
context:
space:
mode:
authorAlex Crichton <alex@alexcrichton.com>2019-04-01 05:29:17 -0700
committerAlex Crichton <alex@alexcrichton.com>2019-04-01 13:15:31 -0700
commit32a76844c47924a1d230f647db3bafb53c3e20ea (patch)
treeac128fd819487aa63299fa1d3293426c5441b882 /src/libstd/sys
parent60f6cbd0028c61bcca181318f48cdf0c6be61231 (diff)
downloadrust-32a76844c47924a1d230f647db3bafb53c3e20ea.tar.gz
rust-32a76844c47924a1d230f647db3bafb53c3e20ea.zip
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.
Diffstat (limited to 'src/libstd/sys')
-rw-r--r--src/libstd/sys/wasi/os.rs17
1 files changed, 15 insertions, 2 deletions
diff --git a/src/libstd/sys/wasi/os.rs b/src/libstd/sys/wasi/os.rs
index 6d4d6aae61b..822ea02a11b 100644
--- a/src/libstd/sys/wasi/os.rs
+++ b/src/libstd/sys/wasi/os.rs
@@ -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> {