Skip to content

Commit 625fac3

Browse files
committed
core: Rewrite last_os_error in Rust for windows as well.
1 parent 9877d98 commit 625fac3

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/libcore/os.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ pub fn errno() -> uint {
808808
}
809809

810810
unsafe {
811-
GetLastError() as uint;
811+
GetLastError() as uint
812812
}
813813
}
814814

@@ -852,15 +852,45 @@ pub fn last_os_error() -> ~str {
852852
if err < 0 {
853853
die!(~"strerror_r failure");
854854
}
855-
855+
856856
str::raw::from_c_str(&buf[0])
857857
}
858858
}
859859
860860
#[cfg(windows)]
861861
fn strerror() -> ~str {
862+
use libc::types::os::arch::extra::DWORD;
863+
use libc::types::os::arch::extra::LPSTR;
864+
use libc::types::os::arch::extra::LPVOID;
865+
866+
#[link_name = "kernel32"]
867+
#[abi = "stdcall"]
868+
extern {
869+
unsafe fn FormatMessageA(flags: DWORD, lpSrc: LPVOID,
870+
msgId: DWORD, langId: DWORD,
871+
buf: LPSTR, nsize: DWORD,
872+
args: *c_void) -> DWORD;
873+
}
874+
875+
const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
876+
const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
877+
878+
let mut buf = [0 as c_char, ..TMPBUF_SZ];
879+
880+
// This value is calculated from the macro
881+
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
882+
let langId = 0x0800 as DWORD;
883+
let err = errno() as DWORD;
862884
unsafe {
863-
rustrt::last_os_error()
885+
let res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
886+
FORMAT_MESSAGE_IGNORE_INSERTS,
887+
ptr::mut_null(), err, langId,
888+
&mut buf[0], TMPBUF_SZ as DWORD, ptr::null());
889+
if res == 0 {
890+
die!(fmt!("[%?] FormatMessage failure", errno()));
891+
}
892+
893+
str::raw::from_c_str(&buf[0])
864894
}
865895
}
866896

0 commit comments

Comments
 (0)