Question about lseek/fseek

Artem Belevich art at freebsd.org
Sat Jul 2 18:04:21 UTC 2011


On Sat, Jul 2, 2011 at 6:57 AM, Martin Möller
<moeller.akt at googlemail.com> wrote:
> Hi Hackers,
>
> why does these function allow seeking beyond the EOF of a file in
> O_RDONLY/²rb² mode ?

Perhaps because the standard explicitly says that it's allowed:
http://pubs.opengroup.org/onlinepubs/009695399/functions/lseek.html

> How does these function then signal the EOF correctly ?

It does not. It only sets offset for a given file descriptor. It's
read's job to signal EOF.

> Seeking beyond the EOF makes sense for me in write-mode but not in read-only
> mode !

Given that lseek does not change file size (I.e. it does not ever
modify the file), there's no reason to prohibit seek beyond end of
file even in read-only mode.

Now, if you would try to read after seeking beyond EOF, then the
read() should complain that there's no data.

--Artem

>
> With Regards,
> Martin
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
>


More information about the freebsd-hackers mailing list