One of the questions you linked to actually has your answer. But first, a couple of references. Note that getline
is internally using other functions (probably read
).
From the man page of read
:
If a read() is interrupted by a signal before it reads any data, it shall return -1 with errno set to [EINTR].
...
... there is an additional function, select(), whose purpose is to pause until specified activity (data to read, space to write, and so on) is detected on specified file descriptors. It is common in applications written for those systems for select() to be used before read() in situations (such as keyboard input) where interruption of I/O due to a signal is desired.
From the man page of select
:
The pselect() function shall examine the file descriptor sets ... to see whether some of their descriptors are ready for reading, are ready for writing, or have an exceptional condition pending, respectively.
So you can use select
to know when it's safe to start reading. However, that still doesn't prevent read
from getting interrupted later. (I just said this for your information).
To actually clear the error flag of your stream, as this answer to one of your linked questions states (although in C++), you need to use the clearerr
function to clear any error flags on the stream. Note that the man page (as well as the C standard) doesn't say specifically that it also clears "interrupted" flag, but that is just because that is beyond the scope of C and is in the realm of POSIX.
As a side note, I'd like to say, please don't use memset
to clear a struct. C has a very nice way of doing it:
struct sigaction act = {0};
Or if you want to be pedantic, since the first element of struct sigaction
could be another struct or an array, you can assign to one of its mandated fields:
struct sigaction act = { .sa_handler = NULL };