[ale] re: timeout for read
Eric Z. Ayers
eric at compgen.com
Wed Oct 30 07:29:44 EST 1996
Geoffrey Myers writes:
> }Is there any shell command in bash that will allow me to grab keystrokes other
> }than read. I would like to do a read command but give a time limit of
> }60 seconds. Ne1 know how. Or does ne1 have a C program that will take
> }the time length on the command line and return the input on STDOUT?
>
> I would think it would be pretty straight forward in C to fork and exec
> a process that is going to expect the input and the parent could have the
> timer. If the timer expires, the parent could signal the child to go away.
> If the timer doesn't expire, the child takes the input and signals the
> parent that it has input. You could get real fancy with pipes between the
> two processes for communication and passing info. Really, choose your
> favorite IPC...
>
> }
Let's not go overboard here.
This little program mayb be all you need to get the job done.
I couldn't find a bash command to call alarm(), but I know you can catch
signals in a bash shell. A more sophisticated version might reset the
alarm when a keystroke is pressed.
When the program exits, you can test $? in your bash program to see
if something was really typed, or if the alarm went off.
-------------------
#include <signal.h>
#include <stdio.h>
void alarm_cb (int signum)
{
/*fprintf (stderr, "ALARM\n");*/
exit(1);
}
int main (int argc, char * argv)
{
char inbuf[1024];
signal (SIGALRM, alarm_cb);
alarm(30);
fgets(inbuf, sizeof(inbuf), stdin);
puts(inbuf);
return 0;
}
-----------------
More information about the Ale
mailing list