Hi, i get a valid "fd" object from a caller. How can i find out what is the fd type - whether it is referring to a File, Socket, Device, etc? Depending on the refered type, i need to execute specific functions, say if the fd is associated with a File, then i need to do a read. If it is associated with a socket, then i need to get the socket properties.
+4
A:
Well, in theory at least, you still do a read for a socket, and for a device, and for a pipe, and.... :-P
If you want more data from the socket, such as the socket addresses, you can just call the functions for doing that. It will simply fail for non-sockets, and it's up to you to decide how you want to deal with that.
If you really must know, do an fstat
on your file descriptor, then look at its mode (st_mode
):
mode_t type;
struct stat fdstat;
/* ... */
if (fstat(fd, &fdstat) == -1)
/* error out */
type = fdstat.st_mode & S_IFMT;
switch (type) {
case S_IFSOCK:
/* socket */
case S_IFIFO:
/* FIFO */
/* other cases */
}
Chris Jester-Young
2009-05-10 15:17:36