terminal.c

来自「linux下基于网络编程的TCP/IP端控制开发源程序」· C语言 代码 · 共 57 行

C
57
字号
#include<termios.h>#include<stdio.h>#include<sys/signal.h>struct termios old_save;void error_quit(char *err_string){    fprintf(stderr,"%s\n",err_string);   exit(1);} void change_mode(void) {  struct termios new_term;  if(!isatty(0))   error_quit("standard input is not a terminal");    if(tcgetattr(0,&new_term)==-1)   error_quit("get the attributes of the terminal");   old_save=new_term;   new_term.c_lflag&=~ISIG;   if(tcsetattr(0,TCSANOW,&new_term)==-1)      error_quit("set new terminal attributes"); }void restore_mode(void) {   if( tcsetattr(0,TCSANOW,&old_save)==-1)            error_quit("restore terminalattributes"); }void handle_ctrl_c(int signo)  {      printf("the process receive SIGINT signle number=%d\n",signo);      printf("now the process will change its terminal attributes.\n");      change_mode();   }void handle_term_signal(int signo)  {      printf("signle number=%d\n",signo);      printf("the process will restore its terminal attributes and exit\n");      restore_mode();     exit(0);   }main(int argc,char **argv)  {    signal(SIGINT,handle_ctrl_c);    signal(SIGTERM,handle_term_signal);    printf("please press the buttons ctrl+c\n");    for(;;);  }

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?