Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace winapi with windows-sys #80

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
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
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
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