#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;
return 0;
}
Call the getpass function
Noncanonical Mode
Noncanonical mode is specified by turning off the ICANON flag in the c_lflag field of the termios structure. In noncanonical mode, the input data is not assembled into lines. The following special characters (Section 18.3) are not processed: ERASE, KILL, EOF, NL, EOL, EOL2, CR, REPRINT, STATUS, and WERASE.
Case A: MIN > 0, TIME > 0
TIME specifies an interbyte timer that is started only when the first byte is received. If MIN bytes are received before the timer expires, read returns MIN bytes. If the timer expires before MIN bytes are received, read returns the bytes received. (At least one byte is returned if the timer expires, because the timer is not started until the first byte is received.) In this case, the caller blocks until the first byte is received. If data is already available when read is called, it is as if the data had been received immediately after the read.
Case B: MIN > 0, TIME == 0
The read does not return until MIN bytes have been received. This can cause a read to block indefinitely.
Case C: MIN == 0, TIME > 0
TIME specifies a read timer that is started when read is called. (Compare this to case A, in which a nonzero TIME represented an interbyte timer that was not started until the first byte was received.) The read returns when a single byte is received or when the timer expires. If the timer expires, read returns 0.
Case D: MIN == 0, TIME == 0
If some data is available, read returns up to the number of bytes requested. If no data is available, read returns 0 immediately.
#include "apue.h"
#include <termios.h>
#include <errno.h>
static struct termios save_termios;
static int ttysavefd=-1;
static enum
{
RESET, RAW, CBREAK
} ttystate=RESET;
int tty_cbreak(int fd)
{
int err;
struct termios buf;
if(ttystate!=RESET)
{
errno=EINVAL;
return -1;
}
if(tcgetattr(fd,&buf)<0)
return -1;
save_termios=buf;
buf.c_lflag&=~(ECHO|ICANON);
buf.c_cc[VMIN]=1;
buf.c_cc[VTIME]=0;
if(tcsetattr(fd,TCSAFLUSH,&buf)<0)
return -1;
if(tcgetattr(fd,&buf)<0)
{
err=errno;
tcsetattr(fd, TCSAFLUSH,&save_termios);
errno=err;
return -1;
}
if((buf.c_lflag&(ECHO|ICANON))||buf.c_cc[VMIN]!=1||
buf.c_cc[VTIME]!=0)
{
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno=EINVAL;
return -1;
}
ttystate=CBREAK;
ttysavefd=fd;
return 0;
}
int tty_raw(int fd)
{
int err;
struct termios buf;
if(ttystate!=RESET)
{
errno=EINVAL;
return -1;
}
if(tcgetattr(fd,&buf)<0)
return -1;
save_termios=buf;
buf.c_lflag &=~(ECHO|ICANON|IEXTEN+ISIG);
buf.c_iflag &=~(BRKINT|ICRNL|INPCK|ISTRIP|IXON);
buf.c_cflag&=~(CSDIZE|PARENB);
buf.c_cflag|=CS8;
buf.c_oflag &=~(OPOST);
buf.c_cc[VMIN]=1;
buf.c_cc[VTIME]=0;
if(tcsetattr(fd, TCSAFLUSH, &buf)<0)
return -1;
if(tcgetattr(fd, &buf)<0)
{
err=errno;
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno=err;
return -1;
}
if((buf.c_lflag&(ECHO|ICANON|IEXTEN|ISGI))||
(buf.c_iflag&(BRKINT|ICRNL|INPCK|ISTRIP|IXON))||
(buf.c_cflag&(CSIZE|PARENB|CS8))!=CS8||
(buf.c_oflag&OPOST)||buf.c_cc[VMIN]!=1||
buf.c_cc[VTIME]!=0)
{
tcsetattr(fd, TCSAFLUSH, &save_termios);
errno=EINVAL;
return -1;
}
ttystate=RAW;
ttysavefd=fd;
return 0;
}
int tty_reset(int fd)
{
if(ttystate==RESET)
return 0;
if(tcsetattr(fd, TCSAFLUSH, &save_termios)<0)
return -1;
ttystate=__REGISTER_PREFIX__;
return 0;
}
void tty_atexit(void)
{
if(ttysavefd>=0)
tty_reset(ttysavefd);
}
struct termios *tty_termios(void)
{
return &save_termios;
}
Set terminal mode to cbreak or raw