I would like to stuff an ‘A’ character back into stdin using ungetc on receipt of SIGUSR1. Imagine that I have a good reason for doing this.
When calling foo(), the blocking read in stdin is not interrupted by the ungetc call on receipt of the signal. While I didn’t expect this to work as is, I wonder if there is a way to achieve this – does anyone have suggestions?
void handler (int sig)
{
ungetc ('A', stdin);
}
void foo ()
{
signal (SIGUSR1, handler);
while ((key = fgetc (stdin)) != EOF)
{
...
}
}
Rather than try to get
ungetc()to unblock a blockingfgetc()call via a signal, perhaps you could try not havingfgetc()block to begin with and wait for activity on stdin usingselect().By default, the line discipline for a terminal device may work in canonical mode. In this mode, the terminal driver doesn’t present the buffer to userspace until the newline is seen (Enter key is pressed).
To accomplish what you want, you can set the terminal into raw (non-canonical) mode by using
tcsetattr()to manipulate thetermiosstructure. This should case the blocking call tofgetc()to immediately return the character inserted withungetc().NOTE: This code omits error checking for simplicity.
Clearing the
ECHOandICANONflags respectively disables echoing of characters as they are typed and causes read requests to be satisfied directly from the input queue. Setting the values ofVTIMEandVMINto zero in thec_ccarray causes the read request (fgetc()) to return immediately rather than block; effectively polling stdin. This causeskeyto get set toEOFso another method for terminating the loop is necessary. Unnecessary polling of stdin is reduced by waiting for activity on stdin usingselect().Executing the program, sending a
SIGUSR1signal, and typingt e s t results in the following output1:
1) tested on Linux