Chinaunix首页 | 论坛 | 博客
  • 博客访问: 966349
  • 博文数量: 184
  • 博客积分: 10030
  • 博客等级: 上将
  • 技术积分: 1532
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-27 18:32
文章分类

全部博文(184)

文章存档

2009年(1)

2008年(63)

2007年(39)

2006年(79)

2005年(2)

我的朋友

分类: C/C++

2008-05-15 11:37:42

#include
#include
#define QUESTION    "Do you want play again?"

int get_response(char *);
void set_crmode(void);

int
main(void)
{
    int response;
    tty_mode(0);
    set_crmode();
    do {
        response = get_response(QUESTION);
        if(response) {
            printf("\nYour answer is y.\n");
        }
        else {
            printf("\nYou answer is n.\n");
        }
    } while(response);
    tty_mode(1);
    return 0;
}

int
get_response(char *question)
{
    int input;
    printf("%s(y/n)?",question);
    while(1) {
        switch(getchar()) {
            case 'y':
            case 'Y': return 1;
            case 'n':
            case 'N': return 0;
        }
    }
}



// how == 0 => save current mode, how == 1 => restore mode
int
tty_mode(int how) {
    static struct termios original_mode;
    if(how == 0)
        tcgetattr(0,&original_mode);
    else
        return tcsetattr(0,TCSANOW,&original_mode);
}

void
set_crmode(void)
{
    struct termios ttystate;
    tcgetattr(0,&ttystate);
    ttystate.c_lflag &= ~ICANON;
    ttystate.c_lflag &= ~ECHO;
    ttystate.c_cc[VMIN] = 1;
    tcsetattr(0,TCSANOW,&ttystate);
}
阅读(1054) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~