LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-24-2009, 07:51 PM   #1
jhwilliams
Senior Member
 
Registered: Apr 2007
Location: Portland, OR
Distribution: Debian, Android, LFS
Posts: 1,168

Rep: Reputation: 207Reputation: 207Reputation: 207
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
 
Old 01-24-2009, 10:34 PM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
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.
 
  


Reply

Tags
interactive, pipe, stream


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mimics of unix pipe program psandeepnair1985 Programming 1 03-20-2007 07:42 AM
Can't get user input when using a pipe to my C++ program ta0kira Programming 18 04-22-2006 12:53 PM
pipe for adding UI on console program okeyla Linux - Newbie 1 09-20-2005 08:36 PM
Program received signal SIGPIPE, Broken pipe. grupoapunte Programming 1 06-03-2005 05:49 AM
How to detect broken pipe in C (Linux)? alanwolfen Programming 4 02-23-2005 01:22 AM


All times are GMT -5. The time now is 06:38 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration