|
[code:1]
#include <stdio.h>
#include <termio.h>
char getch();
void restore();
struct termio ttysave;
int main(int argc, char* argv[])
{
char ch;
do{
ch=getch();
printf("get '%c'\n", ch);
}while(ch!=' ');
exit(0);
}
char getch()
{
static char ch;
static int total, flag=1;
struct termio tty;
if(flag){
flag = 0;
if(ioctl(0, TCGETA, &tty)==-1){
printf("error 1\n");
exit(1);
}
ttysave = tty;
tty.c_lflag &= ~(ICANON | ECHO| ISIG);
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 0;
if(ioctl(0, TCSETAF, &tty)==-1){
restore();
printf("error 2\n");
exit(2);
}
}
switch(total=read(0, &ch, 1)){
case -1:
restore();
printf("read error\n");
exit(3);
break;
case 0:
restore();
printf("EOF error\n");
exit(4);
break;
default:
;
}
restore();
return(ch);
}
void restore()
{
if(ioctl(0, TCSETAF, &ttysave)==-1){
printf("restore error\n");
exit(5);
}
return;
}
[/code:1]
/**********************************************
* gcc编译成功 gcc test.c -o test
* 为什么运行之后没有任何输出就自动结束了,想不通
* 请大虾指教
**********************************************/ |
|