Using select() and read();

  • Thread starter Martin Holm Pedersen
  • Start date
M

Martin Holm Pedersen

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);


/*
* 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);
printf("fd is %d\n", fd);
}

void TermRestore(int fd)
{
/*
* Restore saved modes.
*/
(void)ioctl(fd, TCSETAW, &OldTerm);
}
 
N

Nils Petter Vaskinn

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
 
F

Floyd L. Davidson

Martin Holm Pedersen said:
struct termio OldTerm;
struct termio NewTerm;
....

void SetRaw(int fd)
{

/*
* Get terminal modes, and saves them in the OldTerm struct.
*/
(void)ioctl(fd, TCGETA, &OldTerm);

You'll want to initalize NewTerm also... :)
/*
* 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);

It's an accident if it does anything at this point!
 
M

Martin Holm Pedersen

Floyd said:
You'll want to initalize NewTerm also... :)

(void)ioctl(fd, TCGETA, &OldTerm);
is for getting the existing settings and saving them in OldTerm?

Do i have to initialize NewTerm more than:
struct termio NewTerm; ??
/*
* 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);

It's an accident if it does anything at this point!
printf("fd is %d\n", fd);
}

void TermRestore(int fd)
{
/*
* Restore saved modes.
*/
(void)ioctl(fd, TCSETAW, &OldTerm);
}
 
M

Mark A. Odell

(void)ioctl(fd, TCGETA, &OldTerm);
is for getting the existing settings and saving them in OldTerm?

What's this have to do with the C language? Can't you ask this non-ISO C
stuff in comp.unix.programmer?
 
T

those who know me have no need of my name

in comp.lang.c i read:
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().

these things are off-topic here. try comp.unix.programmer.
 
M

Martin Holm Pedersen

Mark said:
What's this have to do with the C language? Can't you ask this non-ISO C
stuff in comp.unix.programmer?

What it has to do with the C-language? It's written in C. I got it
crosspostet. But then i got a reply here.. Ignoring it would be rude..

- Martin
 
D

Dan Pop

In said:
What it has to do with the C-language? It's written in C.

But it uses features beyond the scope of the C language, so it's unfit
for this newsgroup.
I got it crosspostet.

Very bad idea, as it belongs to only one newsgroup and that newsgroup is
not comp.lang.c.
But then i got a reply here.. Ignoring it would be rude..

Anything wrong with replying by email?

Dan
 
A

Alan Balmer

Please don't answer off-topic questions here. refer the OP to the
appropriate newsgroup.
 
A

Alan Balmer

What it has to do with the C-language? It's written in C. I got it
crosspostet. But then i got a reply here.. Ignoring it would be rude..

The *first* reply you got told you it was off-topic and suggested
alternates. Continuing the topic after that was rude to everyone here.
We would much prefer that you be rude to any idiot who answers your
off-topic question here.
 
K

Keith Thompson

Martin Holm Pedersen said:
What it has to do with the C-language? It's written in C. I got it
crosspostet. But then i got a reply here.. Ignoring it would be rude..

Most of us who hang out here in comp.lang.c are not experts on the
system-specific things you're asking about. Because of that, we're
not competent to judge whether any answers you may get here are valid.
That's why we try to discourage answers to off-topic questions; we've
seen it lead to bad advice. If you post in comp.unix.programmer, any
answers you get will be checked by experts.

It's understandable to assume that, because your program is written in
C, it's topical in comp.lang.c, but in fact it isn't, because it uses
features that aren't defined by the C standard. (The features that
are defined in the C standard are more than enough to keep us busy
here.) The best way we can help you is to tell you where to go for
help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,129
Latest member
FastBurnketo
Top