1

So far, I have that if the filename argument (fname) is left empty, the program reads stdin automatically.

if (!strcmp(fname, ""))
    fin = stdin;

But I need to know whether that stdin was piped in, or interactive, because I could possibly get something like:

rsm: reading from (stdin)
^Z
rsm:(stdin):1: not an attribute: `√┘2ç∩'

if interactive input was used. Is there some sort of library function I could use?

Sammi De Guzman
  • 567
  • 9
  • 20

1 Answers1

4

On Posix systems, you can use isatty:

#include <stdio.h>
#include <unistd.h>

if (isatty(STDIN_FILENO))
{
    // interactive stdin
}

On Windows, you can use the corresponding function _isatty.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • POSIX should include MinGW right? How would I do this on Windows? – Sammi De Guzman Sep 29 '13 at 20:49
  • @SammiFernandez: Posix is an operating system interface specification. It doesn't include a compiler, let alone MingW. And MingW is just a compiler bundle, it doesn't implement Posix. There are Posix implementations for Windows (Interix, Cygwin), but you should probably find the direct Windows API function that solves this simple problem. – Kerrek SB Sep 29 '13 at 20:59
  • @SammiFernandez: Try [`_isatty`](http://msdn.microsoft.com/en-us/library/f4s0ddew.aspx) on Windows. – Kerrek SB Sep 29 '13 at 21:01
  • I meant MSYS, which would be my primary development platform. And I don't think POSIX includes that either. :/ Thanks. – Sammi De Guzman Sep 29 '13 at 22:21