|
发表于 2004-5-17 13:51:09
|
显示全部楼层
使终端处于非规范模式,即可实现这样的实时响应。
针对tcgetattr函数返回的termios结构,就可以改变终端特性。
给出一个例子。
- #include <termios.h>
- #include <sys/types.h>
- #define SMALLSZ 10
-
- main(){
- struct termios tdes;
- int ttyfd;
- ssize_t nread;
- char smallbuf[SMALLSZ+1];
-
- tcgetattr(ttyfd,&tdes);
-
- tdes.c_lflag &= ~ICANON; /*关闭canonical即规范模式*/
- tdes.c_cc[VMIN]=0; /*MIN给出了在来自终端的read调用返回之前,终端驱动程序所必须接 受的最小字符数。若为0,则在收到第一个字符时read调用立即返回*/
- tdes.c_cc[VTIME]=50; /*输入超时值,在read调用时立即启动*/
- tcsetattr(0,TCSAFLUSH,&tdes);
-
- nread=read(0,smallbuf,SMALLSZ);
- if(smallbuf[0]=='y') printf(" -- Yeah! You typed yes.\n");
- }
复制代码
运行结果:
debian:/tmp# ./a.out
y -- Yeah! You type yes. |
|