Sammi De Guzman
Sammi De Guzman

Reputation: 567

How to differentiate between pipe and interactive stdin

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?

Upvotes: 1

Views: 265

Answers (1)

Kerrek SB
Kerrek SB

Reputation: 477040

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.

Upvotes: 4

Related Questions