zoukankan      html  css  js  c++  java
  • linux 非等待用户输入

    有时候我们需要和终端用户交互,等待用户的输入等,但是有时候用户忘了输入或时突然走开了,这个时候如果如果别人使用它的窗口来操作就会发生安全的漏洞,因此我们要开发一种在等待用户有限的时间内输入操作,否则系统自动退出,本程序正是基于这个思路开发的,当然,该程序有一些不完整的地方,就是 ctrl - c 没有处理好,有时间再完善,现在我把它拿出来共享

    /**
    * nodelay.c
    */
    #include
    <stdio.h>
    #include
    <fcntl.h>
    #include
    <termios.h>
    #include
    <signal.h>
    #define SEC     4
    void beep(int num);
    void set_cr_mode();
    void reset_mode(int how);
    void ctrl_c_handler();
    int get_operator();
    int main(int ac,char *avg[])
    {
    int num=SEC;
    char c;
    signal(SIGINT,ctrl_c_handler);
    reset_mode(
    0);
    set_cr_mode();
    while(1)
    {
            printf(
    "please select 1,2,.,9 menu.\n");

            sleep(
    3);
            
    switch(c=tolower(get_operator()))
            {
                    
    case '1':
                    
    case '2':
                    
    case '3':
                    
    case '4':
                    
    case '5':
                    
    case '6':
                    
    case '7':
                    
    case '8':
                    
    case '9':printf("you select menu %c\n",c);num=SEC;break;
                    
    default:num-=1;beep(num);break;
            }
            
    if(num<=0)
            {
                    printf(
    "now,exit.\n");
                    
    //reset_mode(1);
                    break;
            }
    }
    reset_mode(
    1);
    return 0;
    }
    void beep(int num)
    {
            printf(
    "if you not active,system will exit in %d sec.\n",num);
    }
    int get_operator()
    {
    char c;
            
    while((c=getchar())!=EOF && strchr("123456789",c)==NULL);
    return c;
    }
    void ctrl_c_handler()
    {
    printf(
    "do you want exit.(y/n)\n");
            
    if(tolower(getchar())=='y')
            {
                    reset_mode(
    1);
                    exit(
    1);
            }
    }
    void set_cr_mode()
    {
    int termflag;
    struct termios setting;
    termflag
    =fcntl(0,F_GETFL);
    termflag 
    |= O_NDELAY;   //关键的地方,不等待用户的输入
    fcntl(0,F_SETFL,termflag);
    tcgetattr(
    0,&setting);
    setting.c_lflag 
    &= ~ECHO;
    setting.c_lflag 
    &= ~ICANON;
    setting.c_cc[VMIN]
    =1;
    tcsetattr(
    0,TCSANOW,&setting);
    }
    void reset_mode(int how)
    {
    static int ioflag;
    static struct termios  info;
    static int does;
            
    if(how==0)
            {
                    ioflag
    =fcntl(0,F_GETFL);
                    tcgetattr(
    0,&info);
                    does
    =1;
            }
            
    else
            {
                    fcntl(
    0,F_SETFL,ioflag);
                    tcsetattr(
    0,TCSANOW,&info);
                    printf(
    "execute\n");
            }
    }
  • 相关阅读:
    OpenJudge 2738 浮点数加法
    OpenJudge 2809 计算2的N次方
    OpenJudge / Poj 1003 Hangover
    OpenJudge 2706 麦森数
    模板:大整数除法
    OpenJudge 2737 大整数除法
    模板:大整数减法
    ES Field Collapsing 字段折叠使用详解
    ES aggregation详解
    一个一站式流式处理云平台解决方案
  • 原文地址:https://www.cnblogs.com/ringwang/p/1429892.html
Copyright © 2011-2022 走看看