The standard mode is simple: Send a read request. after entering a row, the Terminal Driver returns immediately. The following conditions will result in read return:
Instance: getpass Function
Getpass function: Read the password you typed on the terminal. This function is called by UNIX login (1) and crypt (1) programs. In order to read the password, this function must be disabled for ECHO, but the terminal can still work in standard mode, because after you type the password, you must press Enter, this constitutes a complete line.
Typical unix implementation of the getpass function in the program list 18-8
# Include <signal. h>
# Include <stdio. h>
# Include <termios. h>
# Define MAX_PASS_LEN 8/* max # chars for user to enter */
Char *
Getpass (const char * prompt)
{
Static char buf [MAX_PASS_LEN + 1];/* null byte at end */
Char * ptr;
Sigset_t sig, osig;
Struct termios ts, ots;
FILE * fp;
Int c;
If (fp = fopen (ctermid (NULL), "r +") = NULL)
Return (NULL );
Setbuf (fp, NULL );
Sigemptyset (& sig );
Sigaddset (& sig, SIGINT);/* block SIGINT */
Sigaddset (& sig, SIGTSTP);/* block SIGTSTP */
Sigprocmask (SIG_BLOCK, & sig, & osig);/* and save mask */
Tcgetattr (fileno (fp), & ts);/* save tty state */
Ots = ts;/* sturcture copy */
Tcsetattr (fileno (fp), TCSAFLUSH, & ts );
Fputs (prompt, fp );
Ptr = buf;
While (c = getc (fp ))! = EOF & c! = '\ N ')
If (ptr <& buf [MAX_PASS_LEN])
* Ptr ++ = c;
* Ptr = 0;/* null terminate */
Putc ('\ n', fp);/* we echo a newline */
Tcsetattr (fileno (fp), TCSAFLUSH, & ots);/* restore TTY state */
Sigprocmask (SIG_SETMASK, & osig, NULL);/* restore mask */
Fclose (fp);/* done with/dev/tty */
Return (buf );
}
Program list 18-9 call the getpass Function
# Include "apue. h"
Char * getpass (const char *);
Int
Main (void)
{
Char * ptr;
If (ptr = getpass ("Enter password:") = NULL)
Err_sys ("getpass error ");
Printf ("password: % s \ n", ptr );
While (* ptr! = 0)
* Ptr ++ = 0;/* zero it out when we're done with it */
Exit (0 );
}
Disable echo running effect:
You can't help but stop the echo running effect: