views:

45

answers:

2

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?

+4  A: 

The 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.

casablanca
A: 

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.

janneb
Block devices, such as hard drives, can be seekable. Maybe even character devices, but I'm not sure.
Thomas
some devices that aren't really seekable will not fail the seek (at leas on Linux, someone tried to make the seek fail but discovered userspace apps that depended on the behaviour...)
Spudd86
True; some character device drivers simply ignore seek() and silently return.
Michael Foukarakis