473,322 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Using select() and read();

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

Nov 14 '05 #1
10 2771
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

Nov 14 '05 #2
Martin Holm Pedersen <mh****@kom.auc.dk> wrote:

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

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


--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com
Nov 14 '05 #3
Floyd L. Davidson wrote:
Martin Holm Pedersen <mh****@kom.auc.dk> wrote:

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... :-)


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


--
Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
Ukpeagvik (Barrow, Alaska) fl***@barrow.com


Nov 14 '05 #4
Martin Holm Pedersen <mh****@kom.auc.dk> wrote in
news:40**********************@news.sunsite.dk:
/*
* Get terminal modes, and saves them in the OldTerm struct.
*/
(void)ioctl(fd, TCGETA, &OldTerm);


You'll want to initalize NewTerm also... :-)


(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?

--
- Mark ->
--
Nov 14 '05 #5
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.

--
a signature
Nov 14 '05 #6
Mark A. Odell wrote:
Martin Holm Pedersen <mh****@kom.auc.dk> wrote in
news:40**********************@news.sunsite.dk:
/*
* Get terminal modes, and saves them in the OldTerm struct.
*/
(void)ioctl(fd, TCGETA, &OldTerm);

You'll want to initalize NewTerm also... :-)


(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?


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
Nov 14 '05 #7
In <40**********************@news.sunsite.dk> Martin Holm Pedersen <mh****@kom.auc.dk> writes:
Mark A. Odell wrote:
Martin Holm Pedersen <mh****@kom.auc.dk> wrote in
news:40**********************@news.sunsite.dk:
> /*
> * Get terminal modes, and saves them in the OldTerm struct.
> */
> (void)ioctl(fd, TCGETA, &OldTerm);

You'll want to initalize NewTerm also... :-)

(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?


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
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #8
On Thu, 13 May 2004 04:09:41 -0800, fl***@barrow.com (Floyd L.
Davidson) wrote:
Martin Holm Pedersen <mh****@kom.auc.dk> wrote:

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

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #9
On Thu, 13 May 2004 15:38:00 +0200, Martin Holm Pedersen
<mh****@kom.auc.dk> wrote:
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..


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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #10
Martin Holm Pedersen <mh****@kom.auc.dk> writes:
[...]
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Nashat Wanly | last post by:
HOW TO: Call a Parameterized Stored Procedure by Using ADO.NET and Visual C# .NET View products that this article applies to. This article was previously published under Q310070 For a Microsoft...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
1
by: Daveyk0 | last post by:
Hello there, I have a front end database that I have recently made very many changes to to allow off-line use. I keep copies of the databases on my hard drive and link to them rather than the...
5
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
4
by: Együd Csaba | last post by:
Hi All, I'd like to "compress" the following two filter expressions into one - assuming that it makes sense regarding query execution performance. .... where (adate LIKE "2004.01.10 __:30" or...
6
by: ransoma22 | last post by:
I developing an application that receive SMS from a connected GSM handphone, e.g Siemens M55, Nokia 6230,etc through the data cable. The application(VB.NET) will receive the SMS automatically,...
1
by: Screenbert | last post by:
After finding nothing anywhere in google I am posting this so everyone can benefit by it. The formating is not pretty since I copied it from my word document, but you should benefit by it. ...
0
by: screenbert | last post by:
Managing DHCP Servers using C# They said it was impossible. It couldn't be done. But you can in fact manage DHCP servers using C#. This includes creating and deleting Scopes, SuperScopes,...
2
by: Nikhil | last post by:
I am using the MySQLdb python module. I have a table named 'testing' with few columns, under the 'test' database, what is hosted on a remote mysql server. I want to run the following query to...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.