2

I have stumbled on this C code. It uses freopen to reopen stdin in binary mode:

if (!isatty(STDIN_FILENO))
    freopen(NULL, "rb", stdin);

I don't understand where the isatty test comes from. Does it mean that it does not make sense to reopen a terminal ?

What would be wrong with omitting the isatty test, leaving

freopen(NULL, "rb", stdin);
epsicot
  • 121
  • 3
  • Using the 'b' does no damage on Unix, but doesn't change things either. – Jonathan Leffler Jan 28 '13 at 15:00
  • This code is not needed on Unix where there is no difference between text and binary mode. Knowing if this code really works on Win32 is the subject of another question :) [link](http://stackoverflow.com/q/7052896/119337) – epsicot Jan 28 '13 at 15:10
  • If you're concerned about Windows, be very worried. If you're not concerned about Windows, then you could unconditionally do the `freopen()` and it would make no difference but do no damage. The whole test only makes sense on a system where there's a difference between binary and text files — aka Windows — and the linked question indicates that there's a Windows-specific (non-POSIX, at any rate) function `setmode()` to do what's required where the `freopen()` shown leads to 'undefined behaviour' that should be caught be some sort of exception handler in the run-time. – Jonathan Leffler Jan 28 '13 at 15:15
  • 1
    About changing the binary/text mode, there is a discussion at http://old.nabble.com/Gnulib%27s-freopen-replacement-and-MinGW-td33799977.html – epsicot Jan 31 '13 at 21:02

1 Answers1

0

The isatty() function is POSIX standard.

I guess it can make sense to do this, if you want to make sure you treat a file being read (through shell input redirection, I presume) in binary mode.

unwind
  • 391,730
  • 64
  • 469
  • 606