12

Running a program expecting input from terminal I can ”close” stdin by Ctrl+D. Is there any way to reopen stdin after that?

9
  • You could fopen("/dev/tty", "r") Jul 5, 2018 at 15:30
  • 1
    It is not 'reopening stdin', but I believe does what you actually want. Jul 5, 2018 at 15:31
  • 1
    I find a simple clearerr(stdin) usually suffices in this case. Jul 5, 2018 at 15:33
  • 1
    Or freopen("/dev/tty", "r", stdin) Jul 5, 2018 at 15:34
  • 1
    @PSkocik: Edited nitpick: Make that char buf[32]; for (;;) printf("read=%zd\n", read(0, buf, sizeof buf)); instead. (I'm just so used to writing STDIN_FILENO for example code, I didn't even notice I changed that part. My point was to use %zd for ssize_t.) Jul 5, 2018 at 16:55

1 Answer 1

21

In 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 causes feof() to return true, fgets() to return NULL, fgetc() to return EOF, 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.

9
  • 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.
    – Gem Taylor
    Jul 5, 2018 at 16:31
  • @GemTaylor: If you do an fseek(stream, bytes, SEEK_SET) or fseek(stream, -bytes, SEEK_CUR) or fseek(stream, -bytes, SEEK_END), seeking backwards in the file, it also clears the end-of-stream flag; no need to call clearerr() 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? :-)
    – Gem Taylor
    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. If stream refers to a file that was not truncated in the mean time, then fseek(stream, 0, SEEK_END) and fseek(stream, 0, SEEK_CUR) both succeed, clearing the internal end-of-file flag.) Jul 5, 2018 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.