Skip to content

Commit 00ee174

Browse files
committed
stdlib: win: stdio: Don't use console APIs on UWP
1 parent afc92f2 commit 00ee174

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

src/libstd/sys/windows/c.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ pub type LPBOOL = *mut BOOL;
4242
pub type LPBYTE = *mut BYTE;
4343
pub type LPBY_HANDLE_FILE_INFORMATION = *mut BY_HANDLE_FILE_INFORMATION;
4444
pub type LPCSTR = *const CHAR;
45+
#[cfg(not(target_os = "uwp"))]
4546
pub type LPCVOID = *const c_void;
4647
pub type LPCWSTR = *const WCHAR;
4748
pub type LPDWORD = *mut DWORD;
@@ -970,6 +971,7 @@ pub enum EXCEPTION_DISPOSITION {
970971
ExceptionCollidedUnwind
971972
}
972973

974+
#[cfg(not(target_os = "uwp"))]
973975
#[repr(C)]
974976
#[derive(Copy, Clone)]
975977
pub struct CONSOLE_READCONSOLE_CONTROL {
@@ -978,6 +980,7 @@ pub struct CONSOLE_READCONSOLE_CONTROL {
978980
pub dwCtrlWakeupMask: ULONG,
979981
pub dwControlKeyState: ULONG,
980982
}
983+
#[cfg(not(target_os = "uwp"))]
981984
pub type PCONSOLE_READCONSOLE_CONTROL = *mut CONSOLE_READCONSOLE_CONTROL;
982985

983986
#[repr(C)]
@@ -1039,18 +1042,21 @@ extern "system" {
10391042
pub fn LeaveCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
10401043
pub fn DeleteCriticalSection(CriticalSection: *mut CRITICAL_SECTION);
10411044

1045+
#[cfg(not(target_os = "uwp"))]
10421046
pub fn ReadConsoleW(hConsoleInput: HANDLE,
10431047
lpBuffer: LPVOID,
10441048
nNumberOfCharsToRead: DWORD,
10451049
lpNumberOfCharsRead: LPDWORD,
10461050
pInputControl: PCONSOLE_READCONSOLE_CONTROL) -> BOOL;
10471051

1052+
#[cfg(not(target_os = "uwp"))]
10481053
pub fn WriteConsoleW(hConsoleOutput: HANDLE,
10491054
lpBuffer: LPCVOID,
10501055
nNumberOfCharsToWrite: DWORD,
10511056
lpNumberOfCharsWritten: LPDWORD,
10521057
lpReserved: LPVOID) -> BOOL;
10531058

1059+
#[cfg(not(target_os = "uwp"))]
10541060
pub fn GetConsoleMode(hConsoleHandle: HANDLE,
10551061
lpMode: LPDWORD) -> BOOL;
10561062
pub fn RemoveDirectoryW(lpPathName: LPCWSTR) -> BOOL;

src/libstd/sys/windows/stdio.rs

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#![unstable(issue = "0", feature = "windows_stdio")]
22

33
use crate::char::decode_utf16;
4-
use crate::cmp;
54
use crate::io;
6-
use crate::ptr;
7-
use crate::str;
5+
#[cfg(not(target_os = "uwp"))]
6+
use crate::{ptr, cmp, str};
87
use crate::sys::c;
8+
#[cfg(not(target_os = "uwp"))]
99
use crate::sys::cvt;
1010
use crate::sys::handle::Handle;
1111

1212
// Don't cache handles but get them fresh for every read/write. This allows us to track changes to
1313
// the value over time (such as if a process calls `SetStdHandle` while it's running). See #40490.
1414
pub struct Stdin {
15+
#[allow(dead_code)]
1516
surrogate: u16,
1617
}
1718
pub struct Stdout;
@@ -42,6 +43,7 @@ pub fn get_handle(handle_id: c::DWORD) -> io::Result<c::HANDLE> {
4243
}
4344
}
4445

46+
#[cfg(not(target_os = "uwp"))]
4547
fn is_console(handle: c::HANDLE) -> bool {
4648
// `GetConsoleMode` will return false (0) if this is a pipe (we don't care about the reported
4749
// mode). This will only detect Windows Console, not other terminals connected to a pipe like
@@ -50,6 +52,16 @@ fn is_console(handle: c::HANDLE) -> bool {
5052
unsafe { c::GetConsoleMode(handle, &mut mode) != 0 }
5153
}
5254

55+
#[cfg(target_os = "uwp")]
56+
fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result<usize> {
57+
let handle = get_handle(handle_id)?;
58+
let handle = Handle::new(handle);
59+
let ret = handle.write(data);
60+
handle.into_raw(); // Don't close the handle
61+
return ret;
62+
}
63+
64+
#[cfg(not(target_os = "uwp"))]
5365
fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result<usize> {
5466
let handle = get_handle(handle_id)?;
5567
if !is_console(handle) {
@@ -113,6 +125,7 @@ fn write(handle_id: c::DWORD, data: &[u8]) -> io::Result<usize> {
113125
}
114126
}
115127

128+
#[cfg(not(target_os = "uwp"))]
116129
fn write_u16s(handle: c::HANDLE, data: &[u16]) -> io::Result<usize> {
117130
let mut written = 0;
118131
cvt(unsafe {
@@ -132,6 +145,7 @@ impl Stdin {
132145
}
133146

134147
impl io::Read for Stdin {
148+
#[cfg(not(target_os = "uwp"))]
135149
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
136150
let handle = get_handle(c::STD_INPUT_HANDLE)?;
137151
if !is_console(handle) {
@@ -158,12 +172,22 @@ impl io::Read for Stdin {
158172

159173
utf16_to_utf8(&utf16_buf[..read], buf)
160174
}
175+
176+
#[cfg(target_os = "uwp")]
177+
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
178+
let handle = get_handle(c::STD_INPUT_HANDLE)?;
179+
let handle = Handle::new(handle);
180+
let ret = handle.read(buf);
181+
handle.into_raw(); // Don't close the handle
182+
ret
183+
}
161184
}
162185

163186

164187
// We assume that if the last `u16` is an unpaired surrogate they got sliced apart by our
165188
// buffer size, and keep it around for the next read hoping to put them together.
166189
// This is a best effort, and may not work if we are not the only reader on Stdin.
190+
#[cfg(not(target_os = "uwp"))]
167191
fn read_u16s_fixup_surrogates(handle: c::HANDLE,
168192
buf: &mut [u16],
169193
mut amount: usize,
@@ -194,6 +218,7 @@ fn read_u16s_fixup_surrogates(handle: c::HANDLE,
194218
Ok(amount)
195219
}
196220

221+
#[cfg(not(target_os = "uwp"))]
197222
fn read_u16s(handle: c::HANDLE, buf: &mut [u16]) -> io::Result<usize> {
198223
// Configure the `pInputControl` parameter to not only return on `\r\n` but also Ctrl-Z, the
199224
// traditional DOS method to indicate end of character stream / user input (SUB).

0 commit comments

Comments
 (0)