Running a program expecting input from terminal I can ”close” stdin by Ctrl+D. Is there any way to reopen stdin after that?
1 Answer
In linux and on POSIXy systems in general, the standard input descriptor is not closed when you press Ctrl+D in the terminal; it just causes the pseudoterminal layer to become readable, with read()
returning 0. This is how POSIXy systems indicate end of input.
It does not mean the file descriptor (or even the stream handle provided on top of it by the C library) gets closed. As Steve Summit mentioned in a comment, you only need to clear the end-of-input status of the stream using clearerr()
, to be able to read further data; this tells the C library that you noticed the status change, but want to try further reading anyway.
A similar situation can occur when a process is writing to a file, and another reads it. When the reader gets to the end of the file, a read()
returns 0, which the C library understands as end-of-input; it sets an internal flag, so that unless you call clearerr()
, feof()
will return true for that stream. Now, if the writer writes more data, and the reader does a clearerr()
, the reader can read the newly written additional data.
This is perfectly normal, and expected behaviour.
In summary:
End of input is indicated by a
read()
operation returning 0, but the file descriptor status does not change, and can be used as normal.Ctrl+D on a terminal causes only that to happen; the file descriptors open to the terminal are not affected in any other way, and it is up to the foreground process reading the terminal input to decide what it does. It is allowed to simply go on reading more data.
Most programs do exit when that happens, but that is a convention, not a technical requirement at all.
The C library detects
read()
returning 0, and sets its internal "end of input seen" flag for that stream. That causesfeof()
to return true,fgets()
to return NULL,fgetc()
to returnEOF
, and so on, for that stream.Calling
clearerr()
on the stream handle clears the flag, so that the next read attempt will actually try to read further data from the descriptor.This is described in the very first sentence in the Description section of the
man 3 clearerr
man page.
-
I don't think an example program is necessary. If you think it would be useful for multiple readers, I can whip one or two up. Jul 5, 2018 at 16:15
-
Another example is random-access. If you read past the EOF, then clearerr and seek inside, then you can start to read again. Jul 5, 2018 at 16:31
-
@GemTaylor: If you do an
fseek(stream, bytes, SEEK_SET)
orfseek(stream, -bytes, SEEK_CUR)
orfseek(stream, -bytes, SEEK_END)
, seeking backwards in the file, it also clears the end-of-stream flag; no need to callclearerr()
too. Jul 5, 2018 at 16:45 -
Even better :-) Still it demonstrates the principle that EOF does not mean the istream/FILE*/handle is defunct. Does it clear for seek relative 0? :-) Jul 5, 2018 at 17:11
-
@GemTaylor: Yes.
fseek()
clears the internal end-of-file flag, unless it fails. (You cannot seek pipes and sockets. Ifstream
refers to a file that was not truncated in the mean time, thenfseek(stream, 0, SEEK_END)
andfseek(stream, 0, SEEK_CUR)
both succeed, clearing the internal end-of-file flag.) Jul 5, 2018 at 17:18
clearerr(stdin)
usually suffices in this case.freopen("/dev/tty", "r", stdin)
char buf[32]; for (;;) printf("read=%zd\n", read(0, buf, sizeof buf));
instead. (I'm just so used to writingSTDIN_FILENO
for example code, I didn't even notice I changed that part. My point was to use%zd
forssize_t
.)