Skip to contentSkip to conversation

Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.

DISCUSSION palani saravanan 2 participants 2 messages Jun 17, 2008

lore
Share & tools
lore
#0 lore raw

Hi,

[Forgive me if this is not correct alias for this question.
(Behavior of lseek() on a fd opened with 'RDONLY' flag, when seeking goes beyond file size.)]

In linux, I see that it just goes beyond the file size and returns the resulting offset. For example, 'rc = lseek(fd, 4L, SEEK_END);' on a file which has 5 byte contents, it returns rc as 9.
I expect that it would return size of the file, i.e.) 5.

I am not finding any posix documentation for this either.

May I know the rational behind this behavior?
Does the file pointer internally really points to the new location.?

Thanks,
Saravanan

Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/

#1 lore raw
Quoted 4 lines · palani saravanan

In linux, I see that it just goes beyond the file size and returns the resulting offset. For example, 'rc = lseek(fd, 4L, SEEK_END);' on a file which has 5 byte contents, it returns rc as 9.

Sounds right. It's documented that way, too:

"The lseek() function allows the file offset to be set beyond the
end of the file (but this
does not change the size of the file)."
-- man 2 lseek

Quoted 2 lines

I expect that it would return size of the file, i.e.) 5.

Only if you pass (0,SEEK_END).

Quoted 2 lines

Does the file pointer internally really points to the new location.?

So the documentation promises.