# HG changeset patch # User asl # Date 1080559477 0 # Node ID 0ef97dc5b090f2d54c747f6bd8625fe6c9b50bc8 # Parent 956d016d5e0e0755af4ca2a4682f93419920f0fb * src/common/fopen.cxx: * src/common/stream.cxx: * include/io.inl: * include/stream.hxx: Split CYGSTREAM_READWRITE into CYGSTREAM_READWRITE_CREATE and CYGSTREAM_READWRITE_NOCREATE. This fixes fopen() with mode 'w+' which previously did not allow file creation. diff --git a/packages/language/c/libc/stdio/current/ChangeLog b/packages/language/c/libc/stdio/current/ChangeLog --- a/packages/language/c/libc/stdio/current/ChangeLog +++ b/packages/language/c/libc/stdio/current/ChangeLog @@ -1,3 +1,13 @@ +2004-03-29 Kelvin Lawson + + * src/common/fopen.cxx: + * src/common/stream.cxx: + * include/io.inl: + * include/stream.hxx: + Split CYGSTREAM_READWRITE into CYGSTREAM_READWRITE_CREATE and + CYGSTREAM_READWRITE_NOCREATE. This fixes fopen() with mode 'w+' + which previously did not allow file creation. + 2004-03-15 Jonathan Larmour * src/common/fileops.cxx (tmpnam): Only close if open() succeded. diff --git a/packages/language/c/libc/stdio/current/include/io.inl b/packages/language/c/libc/stdio/current/include/io.inl --- a/packages/language/c/libc/stdio/current/include/io.inl +++ b/packages/language/c/libc/stdio/current/include/io.inl @@ -84,9 +84,12 @@ inline Cyg_ErrNo cyg_stdio_open( const c case Cyg_StdioStream::CYG_STREAM_READ: mode = O_RDONLY; break; - case Cyg_StdioStream::CYG_STREAM_READWRITE: + 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 ) diff --git a/packages/language/c/libc/stdio/current/include/stream.hxx b/packages/language/c/libc/stdio/current/include/stream.hxx --- a/packages/language/c/libc/stdio/current/include/stream.hxx +++ b/packages/language/c/libc/stdio/current/include/stream.hxx @@ -157,7 +157,8 @@ public: typedef enum { CYG_STREAM_READ, CYG_STREAM_WRITE, - CYG_STREAM_READWRITE + CYG_STREAM_READWRITE_NOCREATE, + CYG_STREAM_READWRITE_CREATE } OpenMode; // CONSTRUCTORS @@ -168,8 +169,8 @@ public: // dev is a valid Cyg_Device_Table_t, although it does not have to // be a member of the device table object itself // - // open_mode is one of CYG_STREAM_READ, CYG_STREAM_WRITE or - // CYG_STREAM_READWRITE + // open_mode is one of CYG_STREAM_READ, CYG_STREAM_WRITE, + // CYG_STREAM_READWRITE_NOCREATE or CYG_STREAM_READWRITE_CREATE // // append is true if the file position should be set at EOF on opening // diff --git a/packages/language/c/libc/stdio/current/include/stream.inl b/packages/language/c/libc/stdio/current/include/stream.inl --- a/packages/language/c/libc/stdio/current/include/stream.inl +++ b/packages/language/c/libc/stdio/current/include/stream.inl @@ -446,27 +446,32 @@ Cyg_StdioStream::set_position( fpos_t po } //endif (whence != SEEK_END) Cyg_ErrNo err; - off_t newpos=pos; - err = cyg_stdio_lseek( my_device, &newpos, whence ); + // Flush output if any present. + err = flush_output_unlocked(); if( err == ENOERR ) { - // Clean out the buffer. Flush output if any present, - // and clear any input out of input buffer and any ungot - // chars from unread buffer. + off_t newpos=pos; + + // Clear any input out of input buffer and any ungot chars + // from unread buffer. + io_buf.drain_buffer(); - err = flush_output_unlocked(); - io_buf.drain_buffer(); #ifdef CYGFUN_LIBC_STDIO_ungetc flags.unread_char_buf_in_use = false; #endif // Clear EOF indicator. flags.at_eof = false; - - // update stream pos - position = newpos; + + // Seek the file to the correct place + err = cyg_stdio_lseek( my_device, &newpos, whence ); + + if ( err == ENOERR) { + // update stream pos + position = newpos; + } } unlock_me(); diff --git a/packages/language/c/libc/stdio/current/src/common/fopen.cxx b/packages/language/c/libc/stdio/current/src/common/fopen.cxx --- a/packages/language/c/libc/stdio/current/src/common/fopen.cxx +++ b/packages/language/c/libc/stdio/current/src/common/fopen.cxx @@ -112,7 +112,10 @@ process_mode( const char *mode, Cyg_Stdi *binary = true; break; case '+': - *rw = Cyg_StdioStream::CYG_STREAM_READWRITE; + if (mode[0] == 'r') + *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_NOCREATE; + else + *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_CREATE; break; default: return false; @@ -123,7 +126,10 @@ process_mode( const char *mode, Cyg_Stdi *binary = true; break; case '+': - *rw = Cyg_StdioStream::CYG_STREAM_READWRITE; + if (mode[0] == 'r') + *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_NOCREATE; + else + *rw = Cyg_StdioStream::CYG_STREAM_READWRITE_CREATE; break; default: return false; diff --git a/packages/language/c/libc/stdio/current/src/common/stream.cxx b/packages/language/c/libc/stdio/current/src/common/stream.cxx --- a/packages/language/c/libc/stdio/current/src/common/stream.cxx +++ b/packages/language/c/libc/stdio/current/src/common/stream.cxx @@ -107,7 +107,8 @@ void Cyg_StdioStream::initialize(cyg_std case CYG_STREAM_WRITE: flags.opened_for_write = true; break; - case CYG_STREAM_READWRITE: + case CYG_STREAM_READWRITE_NOCREATE: + case CYG_STREAM_READWRITE_CREATE: flags.opened_for_read = true; flags.opened_for_write = true; break;