guy@auspex.auspex.com (Guy Harris) (04/08/90)
>three quick methods: A fourth method, which should work on any "modern" UNIX (defined for the purposes of this discussion as "any UNIX with 'fcntl'" - which includes 4.2BSD and later, System III and later, and any system derived from or providing compatibility with either or both of the above): #include <fcntl.h> #include <errno.h> extern int errno; /* BSD doesn't declare it in <errno.h> */ int isopen4(int fd) { return !(fcntl(fd, F_GETFD, 0) == -1 && errno == EBADF); } which should be faster than the "fstat" one (since it doesn't have to get the attributes of the file), doesn't do any "dup"ing of file descriptors (and thus has no funny side effects), and doesn't depend on something that, according to the manual page, shouldn't work. (If you don't have a C compiler that supports prototypes, make the obvious change to the beginning of "isopen4"....)