Skip to content

Commit

Permalink
Switch from winapi to windows-sys
Browse files Browse the repository at this point in the history
  • Loading branch information
messense authored and conradkleinespel committed Nov 10, 2023
1 parent 1af6830 commit 449a8f3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ rust-version = "1.60"
libc = "0.2"

[target.'cfg(windows)'.dependencies]
winapi = { version = "0.3", features = ["std", "winnt", "fileapi", "processenv", "winbase", "handleapi", "consoleapi", "minwindef", "wincon"] }
windows-sys = { version = "0.42.0", features = ["Win32_Foundation", "Win32_System_Console", "Win32_Storage_FileSystem", "Win32_Security", "Win32_System_SystemServices"] }

[dependencies]
rtoolbox = { path = "../rtoolbox", version = "0.0" }
27 changes: 14 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,18 @@ mod unix {

#[cfg(target_family = "windows")]
mod windows {
use std::io::{self, BufReader};
use std::io::BufRead;
use std::io::{self, BufReader};
use std::os::windows::io::FromRawHandle;
use winapi::shared::minwindef::LPDWORD;
use winapi::um::consoleapi::{GetConsoleMode, SetConsoleMode};
use winapi::um::fileapi::{CreateFileA, OPEN_EXISTING};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::wincon::{ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT};
use winapi::um::winnt::{
FILE_SHARE_READ, FILE_SHARE_WRITE, GENERIC_READ, GENERIC_WRITE, HANDLE,
use windows_sys::core::PCSTR;
use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
use windows_sys::Win32::Storage::FileSystem::{
CreateFileA, FILE_SHARE_READ, FILE_SHARE_WRITE, OPEN_EXISTING,
};
use windows_sys::Win32::System::Console::{
GetConsoleMode, SetConsoleMode, CONSOLE_MODE, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT,
};
use windows_sys::Win32::System::SystemServices::{GENERIC_READ, GENERIC_WRITE};

struct HiddenInput {
mode: u32,
Expand All @@ -162,7 +163,7 @@ mod windows {
let mut mode = 0;

// Get the old mode so we can reset back to it when we are done
if unsafe { GetConsoleMode(handle, &mut mode as LPDWORD) } == 0 {
if unsafe { GetConsoleMode(handle, &mut mode as *mut CONSOLE_MODE) } == 0 {
return Err(std::io::Error::last_os_error());
}

Expand All @@ -189,21 +190,21 @@ mod windows {
pub fn read_password() -> std::io::Result<String> {
let handle = unsafe {
CreateFileA(
b"CONIN$\x00".as_ptr() as *const i8,
b"CONIN$\x00".as_ptr() as PCSTR,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
std::ptr::null_mut(),
std::ptr::null(),
OPEN_EXISTING,
0,
std::ptr::null_mut(),
INVALID_HANDLE_VALUE,
)
};

if handle == INVALID_HANDLE_VALUE {
return Err(std::io::Error::last_os_error());
}

let mut stream = BufReader::new(unsafe { std::fs::File::from_raw_handle(handle) });
let mut stream = BufReader::new(unsafe { std::fs::File::from_raw_handle(handle as _) });
read_password_from_handle_with_hidden_input(&mut stream, handle)
}

Expand Down
7 changes: 2 additions & 5 deletions tests/no-terminal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,17 @@ use std::io::Cursor;

use rpassword::read_password_from_bufread;

#[cfg(unix)]
#[cfg(unix)]
fn close_stdin() {
unsafe {
libc::close(libc::STDIN_FILENO);
}
}

#[cfg(windows)]
#[cfg(windows)]
fn close_stdin() {
use winapi::um::handleapi::CloseHandle;
use winapi::um::processenv::GetStdHandle;
use winapi::um::winbase::STD_INPUT_HANDLE;
use windows_sys::Win32::Foundation::CloseHandle;
use windows_sys::Win32::System::Console::{GetStdHandle, STD_INPUT_HANDLE};

unsafe {
CloseHandle(GetStdHandle(STD_INPUT_HANDLE));
Expand Down

0 comments on commit 449a8f3

Please sign in to comment.