Use the tcgetattr and tcsetattr functions to solve the password echo problem.

Source: Internet
Author: User

Use the tcgetattr and tcsetattr Functions

There is another way to solve the Password Input echo problem without using the curses library. The program p6.4.c achieves the same purpose by using the tcgetattr function and the tcsetattr function. The Code is as follows:

    

# 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;
}

Use GCC to compile the p6.4.c code and obtain an executable program named p6.4. Run the program and get the following output:

    

[program@localhost charter6]$ gcc -o p6.4 p6.4.c
[program@localhost charter6]$ ./p6.4
Please Input password:
Your passwd is:afdfasf

Press any key continue ...

[program@localhost charter6]$

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.