[comp.unix.questions] distinguishing from pipes and files

fuss@rocksanne.UUCP (William Fuss) (11/21/87)

Seeing how there has been a lot of discussion about pipes....
I would like to know the following:

Can a process can recognize the "source" of its input/output ???
for example, given the scenerio below:

	(1)
		foo < input > file2 &
		bar < file2 > file3 &
		foobar < file3 > output &

	(2)
		cat input | foo | bar | foobar > output

Can "bar" figure out that its standard input is coming from:

	a file in example (1),
		vs
	a pipe in example (2),

or does the shell take care of things such that they look the same?


Any ideas?  Any clues?
or am I digging a hole that cannot be dug?

Any *useful* advice/comments/... appreciated.
Please respond via email.

Thanks,

billfuss	...!rochester!fuss.wbst@xerox.COM
----------------------------------------------------
Smile the world may never know!


	. .
	\_/

tj@mks.UUCP (T. J. Thompson) (11/23/87)

In article <454@rocksanne.UUCP>, fuss@rocksanne.UUCP (William Fuss) writes:
> Can a process can recognize the "source" of its input/output ???
> for example, given the scenerio below:
> (1)
>     foo < input > file2 && bar < file2 > file3 && foobar < file3 > output
> (2)
>     cat input | foo | bar | foobar > output
> 
> Can "bar" figure out that its standard input is coming from:
> 
> 	a file in example (1),
> 		vs
> 	a pipe in example (2),

	lseek(0, 0L, 1)

will return -1 with errno set to ESPIPE if standard input is a pipe,
and succeed if standard input is a regular file.
I am sure many people will be ready to point this out.
What i wonder is why you want to know the difference.
I strongly suggest that any program which behaves differently
when its standard input is redirected from a file rather than piped
is mis-designed. The only counter-example that springs to mind is
a file pager that allows backward scrolling, so copies its input
to a temporary file if coming from a pipe (which behaviour is reasonable,
being invisible to the user).
-- 
     ll  // // ,'/~~\'   T. J. Thompson              uunet!watmath!mks!tj
    /ll/// //l' `\\\     Mortice Kern Systems Inc.         (519) 884-2251
   / l //_// ll\___/     35 King St. N., Waterloo, Ont., Can. N2J 2W9
O_/                                Do not void where prohibited

dhesi@bsu-cs.UUCP (Rahul Dhesi) (11/24/87)

In article <320@mks.UUCP> tj@mks.UUCP (T. J. Thompson) writes:
>I strongly suggest that any program which behaves differently
>when its standard input is redirected from a file rather than piped
>is mis-designed. The only counter-example that springs to mind is
>a file pager that allows backward scrolling, so copies its input
>to a temporary file if coming from a pipe (which behaviour is reasonable,
>being invisible to the user).

Actually, any program that accepts a sequential stream of data but
which may need to backtrack will be more efficient if it can recognize
the difference.  For then, if its standard input is a file, it need not
save the data in another temporary file.

I seem to recall reading something in the "smail" documentation about
this--gain efficiency by saving standard input to a temporary file only
if it's not already coming from a file.  Programs like "diff" and
"sort" could probably benefit from the same technique (if they don't
already).

But you're right in the sense that there should be no need to
distinguish between an input pipe and an input file except for
efficiency, for one can always save the input to a temporary file.  But
then again, if one were willing to always do that, UNIX wouldn't need
pipes to begin with!  (Cf. use of "|" character on MSDOS command line.)
-- 
Rahul Dhesi         UUCP:  <backbones>!{iuvax,pur-ee,uunet}!bsu-cs!dhesi