How do I tell if STDIN is a PIPE?

Dave Eisen dkeisen at leland.Stanford.EDU
Tue Jun 4 00:11:44 AEST 1991


In article <653 at eskimo.celestial.com> nanook at eskimo.celestial.com (Robert Dinse) writes: 
> How do I tell whether stdin is coming from a pipe?

>> In article <1991May26.172328.713 at arizona.edu>, jjr at ace.ece.arizona.edu (Jeffrey J. Rodriguez) writes:
>
>     Try:
>	fstat(0, &s);
>	if(s.st_mode & S_IFIFO)
>		printf("Stdin is a pipe.\n");
>	else
>		printf("Stdin is not a pipe.\n");

This doesn't work everywhere, in particular, it doesn't work on the machine I
am posting from. s.st_mode & S_IFREG (which, by the way, is what you should
be testing for) is 0 when stdin is a pipe. This doesn't seem to be consistently
done across Unices.

That is one of the reasons I suggested that the original poster just try his
seek and see what happens --- as far as I know, any UNIX asked to 
seek on a file type that is unseekable will fail with errno set to ESPIPE.



-- 
Dave Eisen                           dkeisen at leland.Stanford.EDU
1101 San Antonio Road, Suite 102     
Mountain View, CA 94043
(415) 967-5644



More information about the Comp.unix.programmer mailing list