Ted,
On Sunday 24 February 2013 10:10 AM, Ted Unangst wrote:
On Sun, Feb 24, 2013 at 09:12, Sachidananda wrote:
Also, for the record, it's open that seeks to the end. You can open a
file with O_APPEND and seek back to the beginning, and write will not
seek to the end again.
My observation is, if I open(2) the file with O_APPEND it seeks to the
end. And I lseek back to the beginning and write(2) to it, write does
seek back to the end and write the data at the end.
No, I apologize. I'm the one who's wrong. The O_APPEND flag is
sticky, and continues to affect the file even after seeking. This
probably is worth documenting. The current write man page is
misleading, even. Checking the source, pwrite is not affected by the
flag. That'd be worth mentioning too.
Attaching patch for review.
--- /usr/share/man/man2/write.2 Sun Feb 24 17:11:12 2013
+++ write.2 Sun Feb 24 16:59:41 2013
@@ -108,6 +108,13 @@
.Fn write ,
the pointer is incremented by the number of bytes which were written.
.Pp
+If the file is open(2)ed with O_APPEND flag, the file offset is set to
+end of the file before writing.
+However, the behaviour of
+.Fn pwrite
+is not affected by the O_APPEND flag, and write happens at the specified
+.Fa offset .
+.Pp
Objects that are not capable of seeking always write from the current
position.
The value of the pointer associated with such an object is undefined.