Standard mode of terminal I/O

Source: Internet
Author: User

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:

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.