#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);
}
阅读(1082) | 评论(0) | 转发(0) |