Ted,

On Saturday 23 February 2013 12:22 PM, Ted Unangst wrote:
On Sat, Feb 23, 2013 at 12:07, Sachidananda wrote:
Hi,

When a file is open(2)ed with O_APPEND flag, write(2) seeks to the end
of file before writing. We are missing this in the man page.

If this is a valid point to note, I will send a patch to the man page.
That's documented in the open man page.  The flag is passed to
open and not write, so that's the correct place.

What I mean was to document write(2)'s behaviour when file is opened with O_APPEND mode.

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.

Correct me if I'm wrong here:

#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>

int
main()
{
        int fd;
        char *file = "/tmp/foo.txt";

        if ((fd = open(file, O_RDWR|O_APPEND)) == -1)
                return -1;
        lseek(fd, 5, SEEK_SET);
        write(fd, "Hello world\n", 13);
        return 0;
}

Reply via email to