]> git.lizzy.rs Git - rust.git/commitdiff
wasi: Implement `error_string` to get readable errors
authorAlex Crichton <alex@alexcrichton.com>
Mon, 1 Apr 2019 12:29:17 +0000 (05:29 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 1 Apr 2019 20:15:31 +0000 (13:15 -0700)
This routes the `error_string` API to `strerror` in libc which should
have more human readable descriptions.

src/libstd/sys/wasi/os.rs

index 6d4d6aae61b9fa385e7ff0547920cde26250b695..822ea02a11b8923d0fd7ada3b9aaceeceee827e2 100644 (file)
@@ -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> {