How can we distinguish that the entrance to our program is directed or is it just user input using the isatty function?

I asked a similar question about stack overflow at this link:

Why can we redirect the input of the less command, but we cannot work less without any arguments?

which leads me to this:
How can we distinguish whether the entrance to our program is directed or is it just user input? can someone give me a small example of how i can use the function isatty?

+4
source share
1 answer

An example isatty:

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

int main()
{
  if( isatty(STDIN_FILENO) )
    puts("Connected to a terminal");
  else
    puts("Not connected to a terminal");

  return 0;
}

Using:

$ gcc isatty.c 
$ ./a.out 
Connected to a terminal
$ echo hello | ./a.out 
Not connected to a terminal

Not much easier!

+3
source

Source: https://habr.com/ru/post/1530140/


All Articles