fgetpos/ftello without system call
Carlos O'Donell
carlos@redhat.com
Fri Sep 1 12:59:00 GMT 2017
On 09/01/2017 04:17 AM, Florian Weimer wrote:
> Is it possible to obtain the current read offset of a stream without
> triggering an lseek system call?
Hard to say without looking at a very specific use case.
The answer to that is actually incredibly complicated, and requires a
table (which may now be out of date), and a bunch of rules from POSIX:
https://sourceware.org/glibc/wiki/File%20offsets%20in%20a%20stdio%20stream%20and%20ftell
> I know that nothing manipulates the underlying file descriptor directly.
That's good, because otherwise you *would* need a seek to allow the active
handle to change between the fd and the FILE* as required by POSIX.
> The actual goal here is to read the same line multiple times using
> fgets, enlarging the buffer if necessary.
You should be able to do that, and it shouldn't trigger a seek.
> It seems that libio maintains an _offset member in the underlying FILE
> object, but never actually uses it for descriptor-based files (mapped
> files are a different story).
It should.
--
Cheers,
Carlos.
More information about the Libc-help
mailing list