[comp.unix.wizards] is it a socket or is it not?

generous@dgis.daitc.mil (Curtis Generous) (07/30/89)

What's a good method of determining whether a file descriptor
is a socket or not?  The fstat(2) call does not work well on sockets
(and it even says so in the man page :-)

This is what I would like to do but doesn't work:

    if (fstat(fildes, &statb) == 0) {
	if ((statb.st_mode &= S_IFMT) == S_IFSOCK)
	    sockflag++;
    }

Is there a better/cleaner way?  Do you go through the list of possibles
and work by elimination (S_IFREG, S_IFDIR, ...)?  Can I assume that fstat(2)
will _always_ return a zeroed buffer as mentioned in the BUGS of the man page?

The OS is More/BSD 4.3 on a VAX 780, but I think this spans many BSD derivatives

--curtis
-- 
Curtis C. Generous
DTIC Special Projects Office (DTIC-SPO)
ARPA: generous@daitc.mil
UUCP: {uunet,vrdxhq,lll-tis}!daitc!generous

SANDEEP@S63.Prime.COM (08/01/89)

generous@dgis.UUCP writes :
>What's a good method of determining whether a file descriptor
>is a socket or not?  The fstat(2) call does not work well on sockets

If you want the name of the socket also, try

if (getsockname(s,&..,&..) < 0) {
   if (errno == ENOTSOCK) {
      /* Is not a socket */
    }
}


--------------------------------------------------------------------------
In_Real_Life : Sandeep Srivastava           Prime Computer, Natick, MA.
Phone : 508-879-2960 x3711         Internet:sandeep@s63.prime.com

Above opinions are not those of Prime Computer.

rbj@dsys.ncsl.nist.gov (Root Boy Jim) (08/01/89)

? What's a good method of determining whether a file descriptor
? is a socket or not?  The fstat(2) call does not work well on sockets
? (and it even says so in the man page :-)

Do a getsockopt on it. If you get back ENOTSOCK, it ain't.

? Curtis C. Generous
? DTIC Special Projects Office (DTIC-SPO)
? ARPA: generous@daitc.mil
? UUCP: {uunet,vrdxhq,lll-tis}!daitc!generous

	Root Boy Jim
	Have GNU, Will Travel.