Email List: Xaustin-group-lX
[All Lists]

Re: return value of lseek(fd, 0, SEEK_CUR) for O_APPEND files

To: Martin Sebor <msebor@xxxxxxxxx>
Subject: Re: return value of lseek(fd, 0, SEEK_CUR) for O_APPEND files
From: Don Cragun <dcragun@xxxxxxxxx>
Date: Tue, 12 May 2009 18:02:58 -0700
Cc: austin-group-l@xxxxxxxxxxxxx
References: <4A0A0394.9070003@xxxxxx>
Hi Martin,
Please find comments in-line below...

 - Don

Martin Sebor wrote:
On the two implementations I've tried lseek(fd, 0, SEEK_CUR) returns
0 for files opened with the O_APPEND bit set. AFAICS, this violates
the requirements that:

  1) open(2): O_APPEND If set, the file offset shall be set to
     the end of the file prior to each write.

  2) lseek(2): "Upon successful completion, the resulting offset,
     as measured in bytes from the beginning of the file, shall be
     returned."

Am I missing something or are these implementations buggy? (E.g.,
does the "prior to each write" part in (1) above mean prior to
the OS writing to the actual file rather than "prior to the
application making the write() call?")
It means that when you call a function that writes to the file, the
kernel sets the file offset for that handle to the current end of the
file before output starts.

Thanks
Martin

PS Here's a test program along with its output on the two systems:
I only see output from one system???

$ cat t.c && gcc t.c && ./a.out
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main ()
{
    FILE *fp;
    int fd;

    fp = fopen (__FILE__, "a");
    fd = open (__FILE__, O_WRONLY | O_APPEND);

#define PRINT(A)   printf ("%s = %li\n", #A, (A))

    PRINT (lseek (fileno (fp), 0, SEEK_CUR));
    PRINT (ftell (fp));
The 1st line above sets the file pointer on the file description
underlying the STDIO stream to the STDIO stream's current offset; the
second line reports the STDIO stream's current file position indicator.
Since the output for these two lines match, nothing seems to be wrong.
The description of Standard I/O Streams (XSH7, subclause 2.5, P490,
L16782-16786) says:
       "If a file can support positioning requests (such as a
	disk file, as opposed to a terminal), then a ‘‘file
	position indicator’’ associated with the stream is
	positioned at the start (byte number 0) of the file,
	unless the file is opened with append mode, in which
	case it is implementation-defined whether the file
	position indicator is initially positioned at the
	beginning or end of the file."

    PRINT (lseek (fd, 0, SEEK_CUR));
The 3rd paragraph of the description of open() (XSH7, P1379,
L48174-48175) says:
       "The file offset used to mark the current position
	within the file shall be set to the beginning of
	the file."
Since the output below shows that the file descriptor offset for the
file descriptor that is not associated with the STDIO stream is
initially set to zero, nothing seems to be wrong here either.

}

lseek (fileno (fp), 0, SEEK_CUR) = 341
ftell (fp) = 341
lseek (fd, 0, SEEK_CUR) = 0
Assuming the source file size is 341 bytes, the above output is one
correct result.  The other correct result would be for all three
reported values to be 0.

<Prev in Thread] Current Thread [Next in Thread>