public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
* fopen() behavior: File positioning in append mode
@ 2017-02-03 10:59 Stefan Heinzmann
  2017-02-03 15:11 ` Craig Howland
  0 siblings, 1 reply; 2+ messages in thread
From: Stefan Heinzmann @ 2017-02-03 10:59 UTC (permalink / raw)
  To: 'newlib@sourceware.org'

Hi,

I am unsure what the behavior of fopen() should be when the "a+" mode is 
given. The source for this is in file newlib/libc/stdio/fopen.c

Mode a+ translates to open() flags O_RDWR|O_CREAT|O_APPEND according to 
the POSIX standard. This would set the file position to the beginning of 
the file. The file position would be relevant only to read operations, 
since the O_APPEND flag causes writes to happen at the file end in any case.

However, fopen() seems to set the file position to the end in this case, 
by calling fseek() explicitly. The consequence is that read operations 
also happen at the file end. I don't understand why this is done. 
Wouldn't it be more useful to omit the call to fseek(), or to restrict 
it to the write-only case?

The C standard doesn't seem to be clear here. Does it actually specify 
what the result of ftell() should be immediately after calling fopen()? 
I seem to be unable to find conclusive text.

Thanks
Stefan

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: fopen() behavior: File positioning in append mode
  2017-02-03 10:59 fopen() behavior: File positioning in append mode Stefan Heinzmann
@ 2017-02-03 15:11 ` Craig Howland
  0 siblings, 0 replies; 2+ messages in thread
From: Craig Howland @ 2017-02-03 15:11 UTC (permalink / raw)
  To: newlib

On 02/03/2017 05:59 AM, Stefan Heinzmann wrote:
> Hi,
>
> I am unsure what the behavior of fopen() should be when the "a+" mode is 
> given. The source for this is in file newlib/libc/stdio/fopen.c
>
> Mode a+ translates to open() flags O_RDWR|O_CREAT|O_APPEND according to the 
> POSIX standard. This would set the file position to the beginning of the file. 
> The file position would be relevant only to read operations, since the 
> O_APPEND flag causes writes to happen at the file end in any case.
>
> However, fopen() seems to set the file position to the end in this case, by 
> calling fseek() explicitly. The consequence is that read operations also 
> happen at the file end. I don't understand why this is done. Wouldn't it be 
> more useful to omit the call to fseek(), or to restrict it to the write-only 
> case?
>
> The C standard doesn't seem to be clear here. Does it actually specify what 
> the result of ftell() should be immediately after calling fopen()? I seem to 
> be unable to find conclusive text.
>
> Thanks
> Stefan
      See C99 section 7.19.3, where it defines that, if a file does support 
positioning requests, the initial position is 0 unless the file is opened in 
append mode.  If the file is opened in append mode, then the initial position is 
implementation defined.  So there is conclusive text, but it does allow a choice.
      I suggest that the newlib implementation choice makes sense. The "a" mode 
opens for writing only at the end of the file.  Using a+ adds the ability to 
read, also.  Since it is based on "a", starting at the end makes sense.  The 
user could also choose r+--based on r--in which case a reasonable assumption 
(and the required behavior) is that it would start at 0 for read.  This line of 
reasoning (which I thought of before checking the code) is borne out by the 
actual implementation--the fseek happens if "a" was used.
                 Craig

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-02-03 15:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-02-03 10:59 fopen() behavior: File positioning in append mode Stefan Heinzmann
2017-02-03 15:11 ` Craig Howland

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).