How do I tell if STDIN is a PIPE?

CSSE LAN Test Account lan_csse at netrix.nac.dec.com
Thu Jun 6 05:07:50 AEST 1991


In article <653 at eskimo.celestial.com> nanook at eskimo.celestial.com (Robert Dinse) writes:
>In article <1991May30.101153.27842 at thunder.mcrcim.mcgill.edu>, mouse at thunder.mcrcim.mcgill.edu (der Mouse) writes:
>> 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?
>> 
>> Difficult.  Under some systems, a "pipe" is not a distinguishable
>> thing; in particular, BSD implements pipes as connected pairs of
>> sockets, so a pipe appears identical to any other socket connection (of
>> the same address family, of course).
>     Try:
	...
>	if(s.st_mode & S_IFIFO)
>		printf("Stdin is a pipe.\n");
>	else
>		printf("Stdin is not a pipe.\n");

On this machine (Ultrix 4.1) and on the next one  over  (Ultrix  3.1),
this code says "Stdin is not a pipe.\n".  On investigation, the status
struct was *entirely* filled with null bytes. It's quite reproducible.
Some  time back, I discovered that a method that works here (and on on
most of the systems I've tried to port the test to) is:

	switch (s.st_mode & S_IFMT) {
		case S_IFSOCK:
			...;
			break;
		case S_IFIFO:
		case 0:
			...;
			break;
		default:
			fprintf(stderr,"+++ File %d: Unexpected mode 0%o.",f,s.st_mode);
	}

Of course, I expect this to break on the next system I try.  Ain't  it
just wunnerful, having such a nice basis for portable code?



More information about the Comp.unix.programmer mailing list