LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Detect pipe from running C program (https://www.linuxquestions.org/questions/programming-9/detect-pipe-from-running-c-program-699676/)

jhwilliams 01-24-2009 07:51 PM

Detect pipe from running C program
 
I'm looking for a way to detect whether or not a program has been called from pipe, e.g.

Code:

whatever | my_program
versus simply just being exectuated directly:

Code:

my_program
Why? In the first case, I want to run the program non-interactively, and in the latter case I want to print out user-friendly messages. I've been thinking along the lines of some check I haven't yet found, like:
Code:

if( stream_buffer_is_not_empty() )
    print_interactive_messages();

Any ideas?

Thanks to you!

Jameson

SciYro 01-24-2009 10:34 PM

Code:

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

int main (argc, argv)
  int argc;
  char** argv;
{
    if (isatty (STDIN_FILENO))
        printf ("Not a pipe.\n");
    else printf ("Its a pipe.\n");

    return 0;
}

The isatty function, as defined in unistd.h seems to work.


All times are GMT -5. The time now is 08:46 PM.