Is there any portable way (on POSIX systems) to determine if a file descriptor is seekable? My thought is to use lseek(fd, 0, SEEK_CUR);
and check if the return value is -1, but I'm uncertain if this could give false negatives or false positives. Using fstat
and making assumptions about what types of files are seekable/nonseekable does not sound like a good idea. Any other ideas?
views:
45answers:
2The lseek
method seems reasonable. It certainly can't cause a false negative - if it did, something is seriously wrong with the implementation. Also, according to the POSIX spec, it is supposed to fail if the descriptor is a pipe, FIFO or socket, so theoretically you shouldn't have false positives either. The only remaining question is how well different systems comply with the specs. However, it seems like any other methods, whatever they may be, would definitely be less portable than this.
You can use fstat(), then the S_ISREG macro on the mode field of the stat struct to check whether it's a regular file; a regular file, per definiton, is seekable whereas a "non-regular" (special) file might not be (I don't know if there are special files that are also seekable).
But yeah, checking the return value of lseek() and errno == ESPIPE should also work. In principle, the effect of lseek() on devices which are incapable of seeking is implementation-defined, so beware of nasal daemons.