Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/unix_term.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub fn terminal_size(out: &Term) -> Option<(u16, u16)> {
}

pub fn read_secure() -> io::Result<String> {
let f_tty;
let mut f_tty;
let fd = unsafe {
if libc::isatty(libc::STDIN_FILENO) == 1 {
f_tty = None;
Expand All @@ -88,14 +88,17 @@ pub fn read_secure() -> io::Result<String> {
c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSAFLUSH, &termios) })?;
let mut rv = String::new();

let read_rv = if let Some(mut f) = f_tty {
let read_rv = if let Some(f) = &mut f_tty {
f.read_line(&mut rv)
} else {
io::stdin().read_line(&mut rv)
};

c_result(|| unsafe { libc::tcsetattr(fd, libc::TCSAFLUSH, &original) })?;

// Ensure the fd is only closed after everything has been restored.
drop(f_tty);

read_rv.map(|_| {
let len = rv.trim_end_matches(&['\r', '\n'][..]).len();
rv.truncate(len);
Expand Down