Understanding Unix/Linux Programming-用户程序play_again4.c

  1 /* play_again4.c
  2  * When no is input , restore the tty settings
  3  */
  4 
  5  #include <stdio.h>
  6  #include <termios.h>
  7  #include <fcntl.h>
  8  #include <string.h>
  9  #include <signal.h>
 10  #include <stdlib.h>
 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     else
110     {
111         //还原原有设置
112         if( tcsetattr(0 , TCSANOW , & original_mode) == -1 )
113         {
114             perror("Restore tty settings failed!\n");
115         }
116     }
117 }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值