Posted by J-Dogg on 17:22:00 03-24-2003
Hi. Does anyone know if you can set fgets() so that rather than returning only when a line has been input, it will time-out after a certain length of time?
Thanks in advance.
Posted by PGuard on 06:21:00 03-25-2003
[Code]
1: #include <sys/select.h>
2: #include <stdio.h>
3:
4: int main(int argc,char *argv[])
5: {
6: fd_set stdinput;
7: /*
8: struct timeval
9: {
a: int tv_sec;
b: int tv_usec;
c: };
d: */
e: struct timeval tv;
f:
10: /* Init stdinput */
11: FD_ZERO(&stdinput);
12: /* Add stdin (fileno 0) to stdinput */
13: FD_SET(0,&stdinput);
14:
15: /* Wait 1 second */
16: tv.tv_sec=1;
17: /* And 0 microseconds */
18: tv.tv_usec=0;
19:
1a: /* Wait for input... */
1b: /* Check the man page on select for more info about select. */
1c: if(select(1,&stdinput,NULL,NULL,&tv)!=0) {
1d: /* New input */
1e: /* Warning: The value of tv is change now...*/
1f: } else {
20: /* No input */
21: /* The value of tv should be zero */
22: }
23: return 0;
24: }
25:
26: /* I assume that the shell is line buffered. */
27: /* Is you dont want to read from stdin the just change line: 13 to FD_SET(stream,&stdinput); and line 1c to if(select(stream+1,&stdinput,NULL,NULL,&tv)!=0) {*/
[/Code]