# Include <stdio. h>
# Include <termios. h>
# Include <unistd. h>
# Include <errno. h>
# Define echoflags (echo | echoe | echok | echonl)
// The set_disp_mode function is used to control whether the input echo function is enabled.
// If option is 0, the echo is disabled. If option is 1, the echo is enabled.
Int set_disp_mode (int fd, int option)
{
Int err;
Struct termios term;
If (tcgetattr (FD, & term) =-1 ){
Perror ("cannot get the attribution of the terminal ");
Return 1;
}
If (option)
Term. c_lflag | = echoflags;
Else
Term. c_lflag & = ~ Echoflags;
Err = tcsetattr (FD, tcsaflush, & term );
If (ERR =-1 & err = eintr ){
Perror ("cannot set the attribution of the terminal ");
Return 1;
}
Return 0;
}
// The getpasswd function is used to obtain the password entered by the user and store it in the specified character array.
Int getpasswd (char * passwd, int size)
{
Int C;
Int n = 0;
Printf ("Please input password :");
Do {
C = getchar ();
If (C! = '/N' | C! = '/R '){
Passwd [n ++] = C;
}
} While (C! = '/N' & C! = '/R' & n <(size-1 ));
Passwd [N] = '/0 ';
Return N;
}
Int main ()
{
Char passwd [20];
// Close the output echo first, so that the input character information is not displayed when the password is entered.
Set_disp_mode (stdin_fileno, 0 );
// Call the getpasswd function to obtain the password entered by the user
Getpasswd (passwd, sizeof (passwd ));
Printf ("/Nyour passwd is: % s/n", passwd );
Printf ("press any key continue.../N ");
Set_disp_mode (stdin_fileno, 1 );
Getchar ();
Return 0;
}