X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=functions%2Fstdio%2Frename.c;h=a3583e67571bd8b72b20f992aed6ebcc73ed1d55;hb=b1fc26afebd4d557ff89a44bc21767a8704c3809;hp=4a1cc850e44c1520b323cd50dbf83d27b318ccc9;hpb=d02f38605b53cdff5460cc6b9e1b2a80c3a2ba4c;p=pdclib diff --git a/functions/stdio/rename.c b/functions/stdio/rename.c index 4a1cc85..a3583e6 100644 --- a/functions/stdio/rename.c +++ b/functions/stdio/rename.c @@ -1,7 +1,3 @@ -/* $Id$ */ - -/* Release $Name$ */ - /* rename( const char *, const char * ) This file is part of the Public Domain C Library (PDCLib). @@ -13,9 +9,22 @@ #ifndef REGTEST #include <_PDCLIB_glue.h> +#include + +extern struct _PDCLIB_file_t * _PDCLIB_filelist; + int rename( const char * old, const char * new ) { - /* TODO: Search open file list, flush and close file */ + struct _PDCLIB_file_t * current = _PDCLIB_filelist; + while ( current != NULL ) + { + if ( ( current->filename != NULL ) && ( strcmp( current->filename, old ) == 0 ) ) + { + /* File of that name currently open. Do not rename. */ + return EOF; + } + current = current->next; + } return _PDCLIB_rename( old, new ); } @@ -24,10 +33,51 @@ int rename( const char * old, const char * new ) #ifdef TEST #include <_PDCLIB_test.h> +#include + int main( void ) { - TESTCASE( NO_TESTDRIVER ); + FILE * file; + remove( testfile1 ); + remove( testfile2 ); + /* make sure that neither file exists */ + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( fopen( testfile2, "r" ) == NULL ); + /* rename file 1 to file 2 - expected to fail */ + TESTCASE( rename( testfile1, testfile2 ) == -1 ); + /* create file 1 */ + TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL ); + TESTCASE( fputs( "x", file ) != EOF ); + TESTCASE( fclose( file ) == 0 ); + /* check that file 1 exists */ + TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); + /* rename file 1 to file 2 */ + TESTCASE( rename( testfile1, testfile2 ) == 0 ); + /* check that file 2 exists, file 1 does not */ + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); + /* create another file 1 */ + TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL ); + TESTCASE( fputs( "x", file ) != EOF ); + TESTCASE( fclose( file ) == 0 ); + /* check that file 1 exists */ + TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); + /* rename file 1 to file 2 - expected to fail, see comment in + _PDCLIB_rename() itself. + */ + /* NOREG as glibc overwrites existing destination file. */ + TESTCASE_NOREG( rename( testfile1, testfile2 ) == -1 ); + /* remove both files */ + TESTCASE( remove( testfile1 ) == 0 ); + TESTCASE( remove( testfile2 ) == 0 ); + /* check that they're gone */ + TESTCASE( fopen( testfile1, "r" ) == NULL ); + TESTCASE( fopen( testfile2, "r" ) == NULL ); return TEST_RESULTS; } #endif +