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");
            }
    }
  • 相关阅读:
    OpenCV 2.48配置
    win进入当前文件夹,启动当前文件夹的程序
    C++程序运行效率的10个简单方法
    银行国际清算业务平台架构
    股票证券交易系统架构分析与设计
    负载均衡|六种负载均衡算法
    Intelli IDEA快捷键(配合IdeaVim)(转)
    [易学易懂系列|golang语言|零基础|快速入门|(三)]
    [易学易懂系列|golang语言|零基础|快速入门|(二)]
    [易学易懂系列|golang语言|零基础|快速入门|(一)]
  • 原文地址:https://www.cnblogs.com/ringwang/p/1429892.html
Copyright © 2011-2022 走看看