fstat(2) is ok for filesystem based file-descriptors: files, directories, named pipes, unix domain sockets, etc
isatty(3) "returns 1 if desc is an open descriptor connected to a terminal and 0 else."
This library function is usually implemented with fstat(2).
For sockets, you may use a combination of getsockname(2) and getsockopt(2)
With getsockname(2) you get the address type (AF_INET) in the sockaddr buffer,
and with getsockopt(..., SOL_SOCKET, SO_TYPE, ...) you get the socket type...
According to socket(7):
Quote:
SO_TYPE - Gets the socket type as an integer (like SOCK_STREAM). Can only be read with getsockopt.
|
So, with AF_INET / SOCK_STREAM you can be 99% sure it's a TCP/IP socket
The same accounts for SOCK_DGRAM (UDP/IP)
Note that functions that expect sockets and are passed a valid (opened) non-socket file descriptor will return errno == ENOTSOCK (Socket operation on non-socket),
Also, attempting to lseek(2) a pipe, socket or fifo will return ESPIPE. Check the manpage...
I would try a harmless lseek(fd, 0, SEEK_CUR) and check errno
If the context or meanings are sometimes not clear, then check other Unix'es manpages at
http://www.freebsd.org/cgi/man.cgi