|
Home > Archive > Unix Programming > May 2004 > Re: Using select() and read();
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Re: Using select() and read();
|
|
| Nils Petter Vaskinn 2004-05-13, 6:36 am |
| On Thu, 13 May 2004 11:40:55 +0200, Martin Holm Pedersen wrote:
Hi This is probably not a topic for comp.lang.c but for
comp.unix.programmer. Crossposted and followups set.
> Hey All..
> Im having a bit of a problem with my program that i wrote for linux in c. I
> use select() to monitor if the user has pressed a key and reads the key
> with read(). It works fine om my IBM laptop but once i move the program to
> my dell laptop it seems like it doesn't even recognize the select-function.
> That is, it doesn't use the timeout assigned at all. I don't get any errors
> when i compile on either computer. I run debian/testing on both laptops and
> i use CVS for version-control.. I have tried to compile it on the IBM and
> run it on the dell.. But nothing seems to work..
>
> - Martin
>
> The code is something along the lines of:
>
> #include <sys/types.h>
> #include <sys/stat.h>
> #include <asm/io.h>
> #include <fcntl.h>
> #include <termios.h>
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> #include <unistd.h>
> #include <limits.h>
> #include <time.h>
> #include <termio.h>
> #include <sys/ioctl.h>
>
> struct termio OldTerm;
> struct termio NewTerm;
> struct timeval tv1;
>
> void SetRaw(int);
> void TermRestore(int);
>
> int main(void) {
> int fd=0, res=0, i=0;
> char keystroke = 0;
> fd_set rfds1;
>
> SetRaw(fd);
> FD_ZERO(&rfds1);
> FD_SET(fileno(stdin), &rfds1);
> while(1){
>
> tv1.tv_sec=0;
> tv1.tv_usec=1;
>
> /* PRINT THE MENU */
> res=select(fd+1, &rfds1,NULL,NULL,&tv1);
> if(res){
> read(fd, &keystroke, 1);
>
> if(keystroke=='1'){
>
> }
>
> if(keystroke=='2'){
>
> }
>
> if(keystroke=='3'){
>
> }
>
> if(keystroke=='0'){
> printf("så slutter vi!\n");
> TermRestore(fd);
> exit(1);
> }
> }
> system("clear");
> }
>
> TermRestore(fd);
> return 0;
> }
>
> void SetRaw(int fd)
> {
>
> /*
> * Get terminal modes, and saves them in the OldTerm struct.
> */
> (void)ioctl(fd, TCGETA, &OldTerm);
Check return value
>
>
> /*
> * Set the modes to the way we want them.
> */
> NewTerm.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL);
> NewTerm.c_oflag |= (OPOST|ONLCR|TAB3);
> NewTerm.c_oflag &= ~(OCRNL|ONOCR|ONLRET);
> NewTerm.c_cc[VMIN] = 1;
> NewTerm.c_cc[VTIME] = 0;
> (void)ioctl(fd, TCSETAW, &NewTerm);
Check return value
> printf("fd is %d\n", fd);
> }
>
> void TermRestore(int fd)
> {
> /*
> * Restore saved modes.
> */
> (void)ioctl(fd, TCSETAW, &OldTerm);
Check return value
> }
--
NPV
"the large print giveth, and the small print taketh away"
Tom Waits - Step right up
| |
| Martin Holm Pedersen 2004-05-13, 7:34 am |
| Nils Petter Vaskinn wrote:
> On Thu, 13 May 2004 11:40:55 +0200, Martin Holm Pedersen wrote:
>
> Hi This is probably not a topic for comp.lang.c but for
> comp.unix.programmer. Crossposted and followups set.
>
>
> Check return value
>
>
> Check return value
>
>
> Check return value
>
>
Thanks..
- Martin
|
|
|
|
|