终端驱动程序:几个简单例子
2012-01-05 14:23
113 查看
例1. 显示回显位的状态
例2.改变回显位状态,若命令行参数以‘y' 开始,终端回显位被开启,其他字符则关闭
例3.显示大量驱动程序属性
PS:以上例子来自《Unix/Linux编程实践教程》
#include <stdio.h> #include <termios.h> #include <stdlib.h> int main(void) { struct termios tty_info; int result; result = tcgetattr(0,&tty_info); /*0 represent stardard input*/ if(result == -1){ perror("tcgetattr"); exit(1); } if(info.c_lflag & ECHO) /*c_lflag is local mode flags*/ printf("echo is working, cause its bit is 1\n"); else printf("echo is working, cause its bit is 0\n"); return 0; }
例2.改变回显位状态,若命令行参数以‘y' 开始,终端回显位被开启,其他字符则关闭
#include <stdio.h> #include <termios.h> #include <stdlib.h> int main(int argc, char *argv[]) { struct termios tty_info; if(argc == 1) exit(0); if(tcgetattr(0,&tty_info) == -1){ perror("tcgetattr"); exit(1); } if(argv[1][0] == 'y'){ tty_info.c_lflag |= ECHO; } else{ tty_info.c_lflag &= ~ECHO; } if(tcsetattr(0,TCSANOW,&tty_info) == 1){ perror("tcsetattr"); exit(2); } return 0; }
例3.显示大量驱动程序属性
#include <stdio.h> #include <stdlib.h> #include <termios.h> struct flag_info { int fl_value; char *fl_name; }; struct flag_info input_flags[] = { IGNBRK, "Ignore break condition", BRKINT, "Signal interrupt on break", IGNPAR, "Ignore chars with parity errors", PARMRK, "Mark parity errors", INPCK, "Enable input parity check", ISTRIP, "Strip character", INLCR, "Map NL to CR on input", IGNCR, "Ignore CR", ICRNL, "Map CR to NL on input", IXON, "Enable start/stop output control", /* _IXANY, "enable start/stop output control",*/ IXOFF, "Enable start/stop input control", 0, NULL }; struct flag_info local_flags[] = { ISIG, "Enable signals", ICANON, "Cannonical input(erase and kill)", /* _XCASE, "Cannonical upper/lower appearance", */ ECHO, "Enable echo", ECHOE, "ECHO ERASE as BS-SPACE-BS", ECHOK, "ECHO KILL by starting new line", 0, NULL }; void showbaud (int thespeed); void show_some_flags (struct termios *tty); void show_flagset (int thevalue,struct flag_info thebitnames[]); int main (void) { struct termios tty_info; /*this structure hold tty information*/ if(tcgetattr(0,&tty_info) == -1){ /*get information from stdin*/ perror("cannot get parameters from stdin"); exit(1); } /*show information*/ showbaud(cfgetospeed(&tty_info)); /*get and show the baud rate*/ printf("The erase character is ascii %d, Ctrl - %c\n", tty_info.c_cc[VERASE],tty_info.c_cc[VERASE]-1+'A'); printf("The line kill character is ascii %d, Ctrl - %c\n", tty_info.c_cc[VKILL],tty_info.c_cc[VKILL]-1+'A'); show_some_flags(&tty_info); /*show misc.flag*/ return 0; } /*prints the speed in english*/ void showbaud (int thespeed) { printf("the baud rate is "); switch(thespeed){ case B300: printf("300\n"); break; case B600: printf("600\n"); break; case B1200: printf("1200\n"); break; case B1800: printf("1800\n"); break; case B2400: printf("2400\n"); break; case B4800: printf("4800\n"); break; case B9600: printf("9600\n"); break; default: printf("Fast\n"); break; } } /*show the values of two of the flag sets: c_iflag and c_lflag*/ void show_some_flags (struct termios *tty) { show_flagset(tty->c_iflag,input_flags); printf("\n"); show_flagset(tty->c_lflag,local_flags); } /*check each bit pattern and display descriptive title*/ void show_flagset (int thevalue,struct flag_info thebitnames[]) { int i; for(i=0;thebitnames[i].fl_value;i++){ printf("%s is ",thebitnames[i].fl_name); if(thevalue & thebitnames[i].fl_value) printf("ON\n"); else printf("OFF\n"); } }
PS:以上例子来自《Unix/Linux编程实践教程》
相关文章推荐
- linux module的几个命令的简单例子
- boost库asio详解8——几个TCP的简单例子
- 【Boost】boost::algorithm的几个简单例子
- MapReduce几个简单的例子
- Markdown几个简单的例子
- 几个简单php例子
- 简单几个例子,教你用单例模式
- SQL存储过程的几个简单例子
- Jedis几个简单测试例子
- flume-ng安装,应用,几个简单测试例子!
- 几个简单的正则小例子
- 简单几个例子,教你用单例模式
- 几个简单java基础的例子
- sql存储过程几个简单例子
- Jedis几个简单测试例子
- 几个简单的博弈例子
- 海量数据处理实例--几个使用bit-map的简单例子
- php中几个简单的例子
- sql存储过程几个简单例子
- ASP.NET学习篇(3)—几个简单的ASP.ENT的例子