博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Understanding Unix/Linux Programming-用户程序play_again4.c
阅读量:4676 次
发布时间:2019-06-09

本文共 2358 字,大约阅读时间需要 7 分钟。

1 /* play_again4.c  2  * When no is input , restore the tty settings  3  */  4   5  #include 
6 #include
7 #include
8 #include
9 #include
10 #include
11 12 #define ASK "Do you want to play again?" 13 #define TRIES 3 14 #define SLEEPTIME 2 15 #define BEEP putchar('\a'); 16 17 void ctrl_c_handler(int); 18 int get_response(char * , int ); 19 int get_ok_char(void); 20 void set_nodelay_mode(void); 21 void set_cr_noecho_mode(void); 22 void tty_mode(int); 23 24 int main() 25 { 26 int response ; 27 tty_mode(0); 28 set_cr_noecho_mode(); 29 set_nodelay_mode(); 30 31 signal(SIGINT , ctrl_c_handler) ; // Set signal responding function 32 signal(SIGQUIT , SIG_IGN ); 33 34 response = get_response(ASK , TRIES ); 35 36 tty_mode(1); 37 return response ; 38 } 39 40 void ctrl_c_handler(int signum ) 41 { 42 tty_mode(1) ; // Restore the tty settings 43 exit(1) ; 44 } 45 46 int get_response(char * qiz , int tries ) 47 { 48 int input ; 49 int maxtries = tries ; 50 printf("%s(y/n)" , qiz); 51 fflush(stdout); 52 while(1) 53 { 54 BEEP ; 55 sleep(SLEEPTIME); 56 input = tolower(get_ok_char()) ; 57 if(input == 'y') 58 { 59 printf("\n"); 60 return 0 ; 61 } 62 if(input == 'n') 63 { 64 printf("\n"); 65 return 1 ; 66 } 67 if(maxtries -- <= 0 ) 68 { 69 printf("\n"); 70 return 2 ; 71 } 72 BEEP ; 73 } 74 } 75 76 int get_ok_char(void) 77 { 78 int c ; 79 while( (c = getchar() ) != EOF && strchr("yYnN" , c ) == NULL ) 80 ; 81 return c ; 82 } 83 84 void set_cr_noecho_mode(void) 85 { 86 struct termios ttystate ; 87 tcgetattr(0 , &ttystate); 88 ttystate.c_lflag &= ~ICANON ; // No Buffering 89 ttystate.c_lflag &= ~ECHO ; 90 ttystate.c_cc[VMIN] = 1 ; //Get one char one time 91 tcsetattr( 0 , TCSANOW , &ttystate); 92 } 93 94 void set_nodelay_mode(void) 95 { 96 int termflags ; 97 termflags = fcntl(0 , F_GETFL); 98 termflags |= O_NDELAY ; 99 fcntl(0 , F_SETFL , termflags) ;100 }101 102 void tty_mode(int mode)103 {104 static struct termios original_mode ;// 设置静态结构体变量105 if(mode == 0 )106 {107 tcgetattr( 0 , & original_mode);// 存储原有设置108 }109 else110 {111 //还原原有设置112 if( tcsetattr(0 , TCSANOW , & original_mode) == -1 )113 {114 perror("Restore tty settings failed!\n");115 }116 }117 }

 

转载于:https://www.cnblogs.com/NJdonghao/p/5309724.html

你可能感兴趣的文章
Azure Web连接到Azure MySql Db
查看>>
《麻辣江湖》即将上线!
查看>>
Mybatis中mapper.xml文件判断语句中的单双引号问题
查看>>
frameset和frame
查看>>
饥饿的小易(规律,同余大数)
查看>>
ats透明代理
查看>>
PHP 小代码
查看>>
2016/03/16 codes
查看>>
2018年7月21日工作总结
查看>>
Linux shell 命令判断执行语法 ; , && , ||
查看>>
vim代码格式化插件clang-format
查看>>
What does the dot after dollar sign mean in jQuery when declaring variables?
查看>>
windows registry
查看>>
jquery 动画总结(主要指效果函数)
查看>>
【BZOJ4155】[Ipsc2015]Humble Captains
查看>>
【事件】阻止事件的冒泡
查看>>
mac os 安装 geckodriver
查看>>
【数据分析 R语言实战】学习笔记 第十一章 对应分析
查看>>
谁的青春不迷茫
查看>>
socket知识总结
查看>>