valou3433 wrote:
I don't understand why the offset/mode is present at kernel level. That requires to add a system call to seek(), which is slow and not needed.
To the contrary, it's
faster that way.
Upon opening the file,
you have all the information, courtesy of the arguments to open(). You know the mode, you know the initial offset. (Beginning of the file, or end of it for O_APPEND in which case you need to seek() anyway.) All it costs you to keep track of it are the couple of bytes required to store the information.
Updating the offset merely requires addition / subtraction and storing the result, which is definitely faster than querying the file structure about it.
The standard library -- mine at least, and the one showcased by Plaugher's book on its implementation -- do hold their own version of offset / mode... you might notice that there is no complete direct mapping of standard options to POSIX options and vice versa. Plus, the library
has to handle its own kind of buffering. (Implementing ungetc() is a
real PITA if you designed your buffer handling without having that particular function in mind from the beginning...)