Advanced Programming in UNIX Environment Episode 119

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值