* [ECOS] process_mode question
@ 2008-06-27 8:49 Slide
2008-08-18 14:22 ` Jonathan Larmour
0 siblings, 1 reply; 2+ messages in thread
From: Slide @ 2008-06-27 8:49 UTC (permalink / raw)
To: ecos-discuss
In packages/language/c/libc/stdio/current/src/common/fopen.cxx fopen
is defined as the following:
externC FILE *
fopen( const char *filename, const char *mode ) __THROW
{
cyg_stdio_handle_t dev = 0;
Cyg_ErrNo err;
Cyg_StdioStream::OpenMode open_mode = Cyg_StdioStream::CYG_STREAM_READ;
cyg_bool binary, append;
// process_mode returns true on error
if (process_mode( mode, &open_mode, &binary, &append )) {
errno = EINVAL;
return NULL;
} // if
err = cyg_stdio_open( filename, open_mode, binary, append, &dev );
// if not found
if (err != ENOERR) {
errno = ENOENT;
return NULL;
} // if
return fopen_inner( dev, open_mode, binary, append );
} // fopen()
You can see that open_mode is passed into process_mode initialized to
CYG_STREAM_READ.
If I call fopen("myfile.txt", "a") process_mode will have left
open_mode as CYG_STREAM_READ and set the append parameter to true.
Then in cyg_stdio_open, it does the following:
inline Cyg_ErrNo cyg_stdio_open( const char *filename,
const Cyg_StdioStream::OpenMode rw,
const cyg_bool binary,
const cyg_bool append,
cyg_stdio_handle_t *dev)
{
mode_t mode = 0;
int fd;
switch( rw )
{
case Cyg_StdioStream::CYG_STREAM_WRITE:
mode = O_WRONLY|O_CREAT|O_TRUNC;
break;
case Cyg_StdioStream::CYG_STREAM_READ:
mode = O_RDONLY;
break;
case Cyg_StdioStream::CYG_STREAM_READWRITE_NOCREATE:
mode = O_RDWR;
break;
case Cyg_StdioStream::CYG_STREAM_READWRITE_CREATE:
mode = O_RDWR|O_CREAT|O_TRUNC;
break;
}
if( append )
{
mode |= O_APPEND;
mode &= ~O_TRUNC;
}
fd = open( filename, mode );
if( fd < 0 )
return errno;
*dev = fd;
return ENOERR;
}
by the time it gets to the call to open(), the mode is now (O_RDONLY |
O_APPEND), if its appending, shouldn't it at least start out as write?
Thanks,
slide
--
slide-o-blog
http://slide-o-blog.blogspot.com/
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [ECOS] process_mode question
2008-06-27 8:49 [ECOS] process_mode question Slide
@ 2008-08-18 14:22 ` Jonathan Larmour
0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Larmour @ 2008-08-18 14:22 UTC (permalink / raw)
To: Slide; +Cc: ecos-discuss
Slide wrote:
> In packages/language/c/libc/stdio/current/src/common/fopen.cxx fopen
> is defined as the following:
>
> externC FILE *
> fopen( const char *filename, const char *mode ) __THROW
> {
> cyg_stdio_handle_t dev = 0;
> Cyg_ErrNo err;
> Cyg_StdioStream::OpenMode open_mode = Cyg_StdioStream::CYG_STREAM_READ;
> cyg_bool binary, append;
>
> // process_mode returns true on error
> if (process_mode( mode, &open_mode, &binary, &append )) {
[snip]
> You can see that open_mode is passed into process_mode initialized to
> CYG_STREAM_READ.
>
> If I call fopen("myfile.txt", "a") process_mode will have left
> open_mode as CYG_STREAM_READ and set the append parameter to true.
Actually it doesn't quite work like that:
The relevant bit is:
switch (mode[0]) {
case 'r':
*rw = Cyg_StdioStream::CYG_STREAM_READ;
break;
case 'a':
*append = true;
case 'w':
*rw = Cyg_StdioStream::CYG_STREAM_WRITE;
break;
default:
return true;
} // switch
Note that there is no 'break;' in the 'a' case, so it drops through.
But to make this clearer for readers I'll add a /* DROPTHROUGH */ comment.
Jifl
--
eCosCentric Limited http://www.eCosCentric.com/ The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK. Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["Si fractum non sit, noli id reficere"]------ Opinions==mine
--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2008-08-18 14:14 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-06-27 8:49 [ECOS] process_mode question Slide
2008-08-18 14:22 ` Jonathan Larmour
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).