- if ( filename == NULL ) return NULL;
- if ( stream->status & _PDCLIB_FWRITE ) fflush( stream );
- if ( stream->status & _PDCLIB_LIBBUFFER ) free( stream->buffer );
- _PDCLIB_close( stream->handle );
- clearerr( stream );
- if ( ( mode == NULL ) || ( filename[0] == '\0' ) ) return NULL;
- if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 ) return NULL;
- stream->handle = _PDCLIB_open( filename, stream->status );
- if ( ( stream->buffer = malloc( BUFSIZ ) ) == NULL ) return NULL;
- stream->bufsize = BUFSIZ;
+ if ( stream->status & _PDCLIB_FWRITE )
+ {
+ _PDCLIB_flushbuffer( stream );
+ }
+ if ( ( filename == NULL ) && ( stream->filename == NULL ) )
+ {
+ /* TODO: Special handling for mode changes on std-streams */
+ _PDCLIB_funlockfile( stream );
+ return NULL;
+ }
+ stream->ops->close(stream->handle);
+
+ /* TODO: It is not nice to do this on a stream we just closed.
+ It does not matter with the current implementation of clearerr(),
+ but it might start to matter if someone replaced that implementation.
+ */
+ _PDCLIB_clearerr_unlocked( stream );
+ /* The new filename might not fit the old buffer */
+ if ( filename == NULL )
+ {
+ /* Use previous filename */
+ filename = stream->filename;
+ }
+ else if ( ( stream->filename != NULL ) && ( strlen( stream->filename ) >= strlen( filename ) ) )
+ {
+ /* Copy new filename into existing buffer */
+ strcpy( stream->filename, filename );
+ }
+ else
+ {
+ /* Allocate new buffer */
+ if ( ( stream->filename = (char *)malloc( strlen( filename ) ) ) == NULL )
+ {
+ _PDCLIB_funlockfile( stream );
+ return NULL;
+ }
+ strcpy( stream->filename, filename );
+ }
+ if ( ( mode == NULL ) || ( filename[0] == '\0' ) )
+ {
+ _PDCLIB_funlockfile( stream );
+ return NULL;
+ }
+ if ( ( stream->status = _PDCLIB_filemode( mode ) ) == 0 )
+ {
+ _PDCLIB_funlockfile( stream );
+ return NULL;
+ }
+ /* Re-add the flags we saved above */
+ stream->status |= status;