/* FIXME: Last file not removed from list. */ int fclose( struct _PDCLIB_file_t * stream ) { struct _PDCLIB_file_t * current = _PDCLIB_filelist; struct _PDCLIB_file_t * previous = NULL; /* Checking that the FILE handle is actually one we had opened before. */ while ( current != NULL ) { if ( stream == current ) { if ( stream->status & _PDCLIB_WROTELAST ) fflush( stream ); if ( stream->status & _PDCLIB_LIBBUFFER ) free( stream->buffer ); _PDCLIB_close( stream->handle ); if ( previous != NULL ) { previous->next = stream->next; } else { _PDCLIB_filelist = stream->next; } free( stream ); return 0; } previous = current; current = current->next; } return -1; }
int main( void ) { /* This testdriver assumes POSIX, i.e. _PDCLIB_fd_t being int and being incremented by one on each successful open. */ int fh; char buffer[ 10 ]; remove( testfile ); /* Trying to read non-existent file. */ TESTCASE( _PDCLIB_open( testfile, _PDCLIB_FREAD ) == _PDCLIB_NOHANDLE ); /* Writing to file, trying to read from it. */ TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 4 ) == -1 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Reading from file, trying to write to it. */ TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == -1 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Appending to file, trying to read from it. */ TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "app", 3 ) == 3 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == -1 ); TESTCASE( write( fh, "end", 3 ) == 3 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Reading and writing from file ("r+"). */ TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FREAD | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "testappend", 10 ) == 0 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( write( fh, "wedo", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "wedoappend", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Writing and reading from file ("w+"). */ TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FWRITE | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "test", 4 ) == 4 ); TESTCASE( lseek( fh, 1, SEEK_SET ) == 1 ); TESTCASE( read( fh, buffer, 2 ) == 2 ); TESTCASE( memcmp( buffer, "es", 2 ) == 0 ); TESTCASE( write( fh, "sie", 3 ) == 3 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 6 ) == 6 ); TESTCASE( memcmp( buffer, "tessie", 6 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Appending and reading from file ("a+"). */ TESTCASE( ( fh = _PDCLIB_open( testfile, _PDCLIB_FAPPEND | _PDCLIB_FRW ) ) != _PDCLIB_NOHANDLE ); TESTCASE( write( fh, "baby", 4 ) == 4 ); TESTCASE( lseek( fh, 0, SEEK_SET ) == 0 ); TESTCASE( read( fh, buffer, 10 ) == 10 ); TESTCASE( memcmp( buffer, "tessiebaby", 10 ) == 0 ); TESTCASE( _PDCLIB_close( fh ) == 0 ); /* Cleaning up. */ TESTCASE( remove( testfile ) == 0 ); return TEST_RESULTS; }
int fclose( struct _PDCLIB_file_t * stream ) { struct _PDCLIB_file_t * current = _PDCLIB_filelist; struct _PDCLIB_file_t * previous = NULL; /* Checking that the FILE handle is actually one we had opened before. */ while ( current != NULL ) { if ( stream == current ) { /* Flush buffer */ if ( stream->status & _PDCLIB_FWRITE ) { if ( _PDCLIB_flushbuffer( stream ) == EOF ) { /* Flush failed, errno already set */ return EOF; } } /* Close handle */ _PDCLIB_close( stream->handle ); /* Remove stream from list */ if ( previous != NULL ) { previous->next = stream->next; } else { _PDCLIB_filelist = stream->next; } /* Delete tmpfile() */ if ( stream->status & _PDCLIB_DELONCLOSE ) { remove( stream->filename ); } /* Free stream */ if ( ! ( stream->status & _PDCLIB_STATIC ) ) { free( stream ); } return 0; } previous = current; current = current->next; } _PDCLIB_errno = _PDCLIB_EIO; return -1; }