Check output device is a terminal

Revision as of 07:49, 28 March 2013 by rosettacode>Blue Prawn (initial page with C)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

In this task, the job is to check whatever output filehandle exits to the terminal and mention whatever output goes to it or not.

C

Use isatty() on file descriptor to determine if it's a TTY. To get the file descriptor from a FILE* pointer, use fileno:

<lang c>#include <unistd.h> // for isatty()

  1. include <stdio.h> // for fileno()

int main() {

   puts(isatty(fileno(stdout))
         ? "stdout is tty"
         : "stdout is not tty");
   return 0;

}</lang>

Output:
$ ./a.out
stdout is tty

$ ./a.out > tmp
$ cat tmp
stdout is not tty

$ ./a.out | cat
stdout is not tty