1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00
v/vlib/os/password_nix.c.v
Thomas Mangin 580d9cedc7
termios: new termios module (#17792)
* termio: new termio module

move the tcgetattr and tcsetattr functions in a new termio module.
The code needed refactoring as different OS have different fields
size, position and number for the C.termios structure, which
could not be correctly expressed consitently otherwise.

It has the positive side effect to reduce the number of unsafe calls.
New testing code was also added for the readline module as it is
relying of the feature.

* apply 2023 copyright to the new files too
2023-03-30 08:58:52 +03:00

30 lines
781 B
V

module os
import term.termios
// input_password prompts the user for a password-like secret. It disables
// the terminal echo during user input and resets it back to normal when done.
pub fn input_password(prompt string) !string {
if is_atty(1) <= 0 || getenv('TERM') == 'dumb' {
return error('Could not obtain password discretely.')
}
mut old_state := termios.Termios{}
if termios.tcgetattr(0, mut old_state) != 0 {
return last_error()
}
defer {
termios.tcsetattr(0, C.TCSANOW, mut old_state)
println('')
}
mut new_state := old_state
new_state.c_lflag &= termios.invert(C.ECHO) // Disable echoing of characters
termios.tcsetattr(0, C.TCSANOW, mut new_state)
password := input_opt(prompt) or { return error('Failed to read password') }
return password
}