How do I tell if STDIN is a PIPE?

Robert Dinse nanook at eskimo.celestial.com
Sun Jun 2 15:17:47 AEST 1991


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:

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

main()
{
	struct stat s;

	if(fstat(0, &s))
	{
		fprintf(stderr, "Can't stat stdin.\n");
		exit(-1);
	}
	if(s.st_mode & S_IFIFO)
		printf("Stdin is a pipe.\n");
	else
		printf("Stdin is not a pipe.\n");
	exit(0);
}

     This works on my machine but I don't have sockets so that may cloud
the issue, but maybe this will be helpful.



More information about the Comp.unix.questions mailing list