From ffd9176cdfe04694a25ec62f9b6a424e996e9e94 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Wed, 7 Mar 2018 18:45:03 -0800 Subject: [PATCH 1/3] forgot to commit the makefile --- tests/.gitignore | 1 + tests/Makefile | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/.gitignore b/tests/.gitignore index 759d9904ee..dead47ff35 100644 --- a/tests/.gitignore +++ b/tests/.gitignore @@ -6,6 +6,7 @@ /create.out /dup /dup.out +/error /fchdir /fsync /ftruncate diff --git a/tests/Makefile b/tests/Makefile index 41dc224061..8d87af542c 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -5,6 +5,7 @@ BINS=\ chdir \ create \ dup \ + error \ fchdir \ fsync \ ftruncate \ From 3b7149612f329c5f38965970605203dd98199337 Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Wed, 7 Mar 2018 20:57:51 -0800 Subject: [PATCH 2/3] implement perror --- Cargo.lock | 1 + src/stdio/Cargo.toml | 1 + src/stdio/src/lib.rs | 15 ++++++++++++--- tests/error.c | 1 + 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2ea9141fb..514bc4b615 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -445,6 +445,7 @@ version = "0.1.0" dependencies = [ "cbindgen 0.5.0", "platform 0.1.0", + "string 0.1.0", "va_list 0.1.0", ] diff --git a/src/stdio/Cargo.toml b/src/stdio/Cargo.toml index e64c0df1a5..dacf967fe2 100644 --- a/src/stdio/Cargo.toml +++ b/src/stdio/Cargo.toml @@ -10,3 +10,4 @@ cbindgen = { path = "../../cbindgen" } [dependencies] platform = { path = "../platform" } va_list = { path = "../../va_list", features = ["no_std"] } +string = { path = "../string" } diff --git a/src/stdio/src/lib.rs b/src/stdio/src/lib.rs index 05933a409a..696f752df6 100644 --- a/src/stdio/src/lib.rs +++ b/src/stdio/src/lib.rs @@ -4,10 +4,13 @@ extern crate platform; extern crate va_list as vl; +extern crate string; -use core::slice; +use core::str; +use core::fmt::Write; use platform::types::*; +use platform::c_str; use vl::VaList as va_list; mod printf; @@ -201,8 +204,14 @@ pub extern "C" fn pclose(stream: *mut FILE) -> c_int { } #[no_mangle] -pub extern "C" fn perror(s: *const c_char) { - unimplemented!(); +pub unsafe extern "C" fn perror(s: *const c_char) { + let strerror = string::strerror(platform::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)) + )); } #[no_mangle] diff --git a/tests/error.c b/tests/error.c index 67fb25ed39..9ab04c2bd9 100644 --- a/tests/error.c +++ b/tests/error.c @@ -6,4 +6,5 @@ int main(int argc, char** argv) { chdir("nonexistent"); printf("errno: %d = %s\n", errno, strerror(errno)); + perror("perror"); } From 22fb6c5bf0a732db666c73f2cbabec2d2bcadcda Mon Sep 17 00:00:00 2001 From: Paul Sajna Date: Thu, 8 Mar 2018 11:20:13 -0800 Subject: [PATCH 3/3] copy strerror implementation into perror --- src/stdio/Cargo.toml | 1 + src/stdio/src/lib.rs | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/src/stdio/Cargo.toml b/src/stdio/Cargo.toml index dacf967fe2..570e409bd0 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 66128a1cd1..1f16600021 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)) )); }