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

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

To: austin-group-l@xxxxxxxxxxxxx
Subject: return value of lseek(fd, 0, SEEK_CUR) for O_APPEND files
From: Martin Sebor <msebor@xxxxxxxxx>
Date: Tue, 12 May 2009 17:17:40 -0600
Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:content-type :content-transfer-encoding; bh=gy3+aizM0YE+D2sdhdDr4jEBHBDnfPoluaaOdrsYJAg=; b=T88O7V9KYuWhCGcBRt9Zjf0zBl84KDLQyrpEwE0dNov+0WONLCx2Y9y8lD+U20ygiP hs1JfaZ8ShoggXWNZOD93TXvJDRwyPDyY77F1j/di7gzqb0VBw0IgNW6ooSeVUEyDknY r9yeiEKG2bqMhb/pPWur6jxq59HVghyFzFj7E=
Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject :content-type:content-transfer-encoding; b=ZSJouguEJH7FjZ9dypHzEnOrYSHajBpW6YiaE2ZkeKg0OZ4sYrrl15vrumoyTMs3sL xO2mfkVClRtqsJwi3X/x7T8wI3gXoq+Dwsq3CLhbBJufA+SDHHjMmx2GXRVI/YR2wnSS ej1O4TWOvpH1dST3sP1FIZm3DIGu23r4KUhqc=
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?")

Thanks
Martin

PS Here's a test program along with its output on the two systems:

$ 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));
    PRINT (lseek (fd, 0, SEEK_CUR));
}

lseek (fileno (fp), 0, SEEK_CUR) = 341
ftell (fp) = 341
lseek (fd, 0, SEEK_CUR) = 0

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