How do I tell if STDIN is a PIPE?

Doug Gwyn gwyn at smoke.brl.mil
Mon May 27 13:26:37 AEST 1991


In article <1991May26.172328.713 at arizona.edu> jjr at ace.ece.arizona.edu (Jeffrey J. Rodriguez) writes:
>How do I tell whether stdin is coming from a pipe?
>There must be some system call I can use from a C program.

The simplest method is to attempt an lseek(0,0L,1) and see if it
reports failure:  If you can't seek to relative location 0, the
file is undoubtedly not seekable in general.  (If you CAN seek to
relative location 0, doing so will not mess anything up.)

>If stdin is coming from a pipe, then what is the best way to do a seek?

There isn't one.

>Please let me know if there's a better way to handle this.

Why do you think you need to seek?  Perhaps what you need to do is
copy the standard input into a temporary (regular) file and switch
to that instead of trying to use stdin directly.



More information about the Comp.unix.questions mailing list