1 //! Implementation of `errno` functionality for Windows.
2 //!
3 //! Adapted from `src/libstd/sys/windows/os.rs` in the Rust distribution.
4 
5 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
6 // file at the top-level directory of this distribution and at
7 // http://rust-lang.org/COPYRIGHT.
8 //
9 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
10 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
11 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
12 // option. This file may not be copied, modified, or distributed
13 // except according to those terms.
14 
15 use core::char::{self, REPLACEMENT_CHARACTER};
16 use core::ptr;
17 use core::str;
18 use windows_sys::Win32::Foundation::{GetLastError, SetLastError, WIN32_ERROR};
19 use windows_sys::Win32::System::Diagnostics::Debug::{
20     FormatMessageW, FORMAT_MESSAGE_FROM_SYSTEM, FORMAT_MESSAGE_IGNORE_INSERTS,
21 };
22 
23 use crate::Errno;
24 
from_utf16_lossy<'a>(input: &[u16], output: &'a mut [u8]) -> &'a str25 fn from_utf16_lossy<'a>(input: &[u16], output: &'a mut [u8]) -> &'a str {
26     let mut output_len = 0;
27     for c in char::decode_utf16(input.iter().copied().take_while(|&x| x != 0))
28         .map(|x| x.unwrap_or(REPLACEMENT_CHARACTER))
29     {
30         let c_len = c.len_utf8();
31         if c_len > output.len() - output_len {
32             break;
33         }
34         c.encode_utf8(&mut output[output_len..]);
35         output_len += c_len;
36     }
37     unsafe { str::from_utf8_unchecked(&output[..output_len]) }
38 }
39 
with_description<F, T>(err: Errno, callback: F) -> T where F: FnOnce(Result<&str, Errno>) -> T,40 pub fn with_description<F, T>(err: Errno, callback: F) -> T
41 where
42     F: FnOnce(Result<&str, Errno>) -> T,
43 {
44     // This value is calculated from the macro
45     // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
46     let lang_id = 0x0800_u32;
47 
48     let mut buf = [0u16; 2048];
49 
50     unsafe {
51         let res = FormatMessageW(
52             FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
53             ptr::null_mut(),
54             err.0 as u32,
55             lang_id,
56             buf.as_mut_ptr(),
57             buf.len() as u32,
58             ptr::null_mut(),
59         );
60         if res == 0 {
61             // Sometimes FormatMessageW can fail e.g. system doesn't like lang_id
62             let fm_err = errno();
63             return callback(Err(fm_err));
64         }
65 
66         let mut msg = [0u8; 2048];
67         let msg = from_utf16_lossy(&buf[..res as usize], &mut msg[..]);
68         // Trim trailing CRLF inserted by FormatMessageW
69         callback(Ok(msg.trim_end()))
70     }
71 }
72 
73 pub const STRERROR_NAME: &str = "FormatMessageW";
74 
errno() -> Errno75 pub fn errno() -> Errno {
76     unsafe { Errno(GetLastError() as i32) }
77 }
78 
set_errno(Errno(errno): Errno)79 pub fn set_errno(Errno(errno): Errno) {
80     unsafe { SetLastError(errno as WIN32_ERROR) }
81 }
82