Xref: utzoo comp.unix.questions:31954 comp.unix.programmer:1996 Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!stanford.edu!decwrl!pa.dec.com!hollie.rdg.dec.com!decuk.uvo.dec.com!nntpd.lkg.dec.com!netrix.nac.dec.com!lan_csse From: lan_csse@netrix.nac.dec.com (CSSE LAN Test Account) Newsgroups: comp.unix.questions,comp.unix.programmer Subject: Re: How do I tell if STDIN is a PIPE? Message-ID: <23190@shlump.lkg.dec.com> Date: 5 Jun 91 19:07:50 GMT References: <1991May26.172328.713@arizona.edu> <1991May30.101153.27842@thunder.mcrcim.mcgill.edu> <653@eskimo.celestial.com> Sender: news@shlump.lkg.dec.com Followup-To: comp.unix.questions Distribution: world,local Organization: Digital Equipment Lines: 37 In article <653@eskimo.celestial.com> nanook@eskimo.celestial.com (Robert Dinse) writes: >In article <1991May30.101153.27842@thunder.mcrcim.mcgill.edu>, mouse@thunder.mcrcim.mcgill.edu (der Mouse) writes: >> In article <1991May26.172328.713@arizona.edu>, jjr@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?