473,378 Members | 1,555 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Detecting a PIPE in a program

Stu

I have the following code in a "C" program.

test_prog.c
main()
{
while ( fgets ( buffer, sizeof(buffer), stdin) != NULL)
{
.....
.....
}
}
I want to run the program like this:

ls | test_prog

Is there anyway I can detect in my program if I am being feed
("PIPPED") data.

If I just run my program like this:

test_prog

it sits and waits. I want to put the smarts in it to see if it is
actually getting data feed to it. If not, than I can take action.

Ant sample "C" code tidbets would be appreciated.
Thanks in advance for all that answer this post!!

Nov 14 '05 #1
9 1974
In article <11**********************@g14g2000cwa.googlegroups .com>,
Stu <be********@hotmail.com> wrote:
Is there anyway I can detect in my program if I am being feed
("PIPPED") data.


I think you mean PIPED :-) And probably what you really want to know
is whether the input is a terminal; a file should most likely be
treated the same way as a pipe.

Anyway, there's no way to do this in standard C. Your operating
system may provide a way to do it. For example, in unix you can
determine whether stdin is a terminal by using isatty(0).

-- Richard
Nov 14 '05 #2
Stu wrote:

I have the following code in a "C" program.

test_prog.c
main()
{
while ( fgets ( buffer, sizeof(buffer), stdin) != NULL)
{
....
....
}
}

I want to run the program like this:

ls | test_prog

Is there anyway I can detect in my program if I am being feed
("PIPPED") data.

If I just run my program like this:

test_prog

it sits and waits. I want to put the smarts in it to see if it is
actually getting data feed to it. If not, than I can take action.


Here is a non-portable routine I use for the purpose. It gets
called with something like "if (akeyboard(stdin)) helpandquit();"
It also works with TC2.01 from the Borland museum.

/* ------------------- */

/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);
# endif
#endif
return ((fp != NULL) && isatty(fileno(fp)));
} /* akeyboard */

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #3
CBFalconer <cb********@yahoo.com> wrote:
/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);


Only "very likely" to be non-portable?

Richard
Nov 14 '05 #4
CBFalconer <cb********@yahoo.com> writes:
[...]
Here is a non-portable routine I use for the purpose. It gets
called with something like "if (akeyboard(stdin)) helpandquit();"
It also works with TC2.01 from the Borland museum.

/* ------------------- */

/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);
# endif
#endif
return ((fp != NULL) && isatty(fileno(fp)));
} /* akeyboard */


Why do you declare the fileno() and isatty() functions rather than
#include'ing whatever header declares them?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #5
Richard Bos wrote:
CBFalconer <cb********@yahoo.com> wrote:
/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);


Only "very likely" to be non-portable?


Yup, in the sense that it has a finite chance of failing on your
conforming system. Would you rather I claimed it to be portable,
or didn't even mention it?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #6
Keith Thompson wrote:
CBFalconer <cb********@yahoo.com> writes:
[...]
Here is a non-portable routine I use for the purpose. It gets
called with something like "if (akeyboard(stdin)) helpandquit();"
It also works with TC2.01 from the Borland museum.

/* ------------------- */

/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);
# endif
#endif
return ((fp != NULL) && isatty(fileno(fp)));
} /* akeyboard */


Why do you declare the fileno() and isatty() functions rather than
#include'ing whatever header declares them?


Because they live in headers in which they are disabled by -ansi
-pedantic. I know about this location, and I want the warnings
elsewhere in the code. I even commented that.

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
Nov 14 '05 #7

Stu wrote:
Is there anyway I can detect in my program if I am being feed
("PIPPED") data.


Maybe.. depending on platform. In the *nix world, you could
try something like this:

.. #include <stdio.h>
.. #include <unistd.h>
.. #include <sys/stat.h>

.. int main()
.. {
.. struct stat st;
..
.. if ( fstat(fileno(stdin),&st) < 0) { perror("fstat"); exit(0);
}
.. if (S_ISFIFO(st.st_mode)) { printf("Pipe\n"); }
.. else if (S_ISCHR(st.st_mode)) { printf("Character device\n");
}
.. else if (S_ISREG(st.st_mode)) { printf("Regular file\n"); }
..}

But usually this kind of detection is not among the best of ideas...
for determining input type rather than setting aside a command line
option or separate binary to give the user control of input method.

(You can't be too sure of the underlying input, actually... and by
making a decision based on input device.. you risk sacrificing
generality)

Nov 14 '05 #8
On Thu, 03 Feb 2005 01:54:13 +0000, CBFalconer wrote:
Richard Bos wrote:
CBFalconer <cb********@yahoo.com> wrote:
/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);


Only "very likely" to be non-portable?


Yup, in the sense that it has a finite chance of failing on your
conforming system. Would you rather I claimed it to be portable,
or didn't even mention it?


I think Richard would rather you stated that it is non-portable without
qualification.

Lawrence

Nov 14 '05 #9
CBFalconer <cb********@yahoo.com> wrote:
Richard Bos wrote:
CBFalconer <cb********@yahoo.com> wrote:
/* This is very likely to be non-portable */
/* DOES NOT check fp open for reading */
/* NULL fp is considered a keyboard here! */
static int akeyboard(FILE *fp)
{
#ifndef __TURBOC__
# ifdef __STDC__
/* This dirty operation allows gcc -ansi -pedantic */
extern int fileno(FILE *fp);
extern int isatty(int fn);


Only "very likely" to be non-portable?


Yup, in the sense that it has a finite chance of failing on your
conforming system. Would you rather I claimed it to be portable,
or didn't even mention it?


In the context of c.l.c, it not very likely, but with certainty
non-portable. In fact, IIRC the solution you give isn't even guarantee
to work correctly under exotic circumstances (over a serial line springs
to mind), but I could be wrong; that's the danger of non-portable code
in c.l.c! (Of cource, the OP isn't likely to be in such unusual
circumstances, but still...)

Richard
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ruben | last post by:
I am reading a large text file a chunk at a time using the readlines(buffer_size) statement. I get an IOERROR ERRNO 32 Broken Pipe command when I "pipe" the output to MYSQL database using the...
2
by: Brian | last post by:
On a Slackware 9.1 box, I'm trying to detect if mpg123 is currently running/playing a song so that when the song is done, it'll play the next in the list. The problem is that popen'ing ps doesn't...
2
by: Hoegje | last post by:
I am writing a C++ program, which should create a sub- process to start a telnet session to another server. Then it should login to that server (on the telnet login) and execute one or more...
6
by: Carson | last post by:
Hi, How to write c code such that it supports pipe in? i.e., echo "ABCD" | a.out (how to write a c-code which generates the binaries a.out that can take the pipe in input for further...
7
by: Greg | last post by:
I am trying to implement the UNIX pipe command using C but with the "->" operator. Everything works fine with 1 pipe, but when I try to use 2 or more, it hangs up when reading the pipe_in...
1
by: David Reed | last post by:
Is there any way to have one program run another arbitrary program with input from stdin and display the output as if you had run it in a shell (i.e., you'd see some of the output followed by the...
0
by: olaf.dietsche | last post by:
Hi, The system is Windows XP and DB2 v8.1.7. I'm trying to load a text file via named pipe into a table. I have two programs: the first program creates the named pipe, waits for a client...
2
by: Igna | last post by:
Hello. I have to write a pipe to joint a GUI in perl and a simulation program in c. I have read all the docs found in perl.com and now I am trying to make a test with this simple program if it is...
11
by: 7stud | last post by:
Hi, Can someone explain what a broken pipe is? The following produces a broken pipe error: ---------- import subprocess as sub p = sub.Popen(, stdin=sub.PIPE, stdout=sub.PIPE)
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.