1 #include2 #include 3 #include 4 #include 5 #include 6 #include 7 #include 8 #include 9 #include 10 #include 11 #include 12 #include 13 #include 14 #include 15 16 int main()17 {18 int keyboard;19 int ret,i;20 char c;21 fd_set readfd;22 struct timeval timeout;23 keyboard = open("/dev/tty",O_RDONLY|O_NONBLOCK);24 assert(keyboard>0);25 while(1){26 timeout.tv_sec = 5;27 timeout.tv_usec = 0;28 FD_ZERO(&readfd);29 FD_SET(keyboard,&readfd);30 ret = select(keyboard+1,&readfd,NULL,NULL,&timeout);31 if(ret == -1){32 perror("select error!\n");33 }34 else if(ret){35 if(FD_ISSET(keyboard,&readfd)){36 i = read(keyboard,&c,1);37 if('\n'==c)38 continue;39 printf("The input is %c\n",c);40 if('q'==c)41 break;42 }43 }44 else if(ret == 0){45 printf("time out !\n");46 }47 }48 return 0;49 }