diff --git a/src/stdio/Cargo.toml b/src/stdio/Cargo.toml index dacf967fe2db40868de921bb7ed1dd01277b65da..570e409bd012b742ed12e710775ae776bf39da35 100644 --- a/src/stdio/Cargo.toml +++ b/src/stdio/Cargo.toml @@ -11,3 +11,4 @@ cbindgen = { path = "../../cbindgen" } platform = { path = "../platform" } va_list = { path = "../../va_list", features = ["no_std"] } string = { path = "../string" } +errno = { path = "../errno"} diff --git a/src/stdio/src/lib.rs b/src/stdio/src/lib.rs index 66128a1cd1f95ed7b562e5e185308db6768afafc..1f166000216c8899d1f4dfa3c7787b5d90fcc557 100644 --- a/src/stdio/src/lib.rs +++ b/src/stdio/src/lib.rs @@ -5,12 +5,15 @@ extern crate platform; extern crate va_list as vl; extern crate string; +extern crate errno; use core::str; use core::fmt::Write; use platform::types::*; use platform::c_str; +use platform::errno; +use errno::STR_ERROR; use vl::VaList as va_list; mod printf; @@ -207,12 +210,21 @@ pub extern "C" fn pclose(stream: *mut FILE) -> c_int { #[no_mangle] pub unsafe extern "C" fn perror(s: *const c_char) { - let strerror = string::strerror(platform::errno); + let mut buf: [u8; 256] = [0; 256]; + + let mut sw = platform::StringWriter(buf.as_mut_ptr(), buf.len()); + + if errno >= 0 && errno < STR_ERROR.len() as c_int { + sw.write_str(STR_ERROR[errno as usize]); + } else { + sw.write_fmt(format_args!("Unknown error {}", errno)); + } + let mut w = platform::FileWriter(2); w.write_fmt(format_args!( "{}: {}\n", str::from_utf8_unchecked(c_str(s)), - str::from_utf8_unchecked(c_str(strerror)) + str::from_utf8_unchecked(c_str(buf.as_mut_ptr() as *mut c_char)) )); }