1 /* rename( const char *, const char * )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
10 #include <_PDCLIB_glue.h>
14 extern _PDCLIB_file_t * _PDCLIB_filelist;
16 int rename( const char * old, const char * new )
18 FILE * current = _PDCLIB_filelist;
19 while ( current != NULL )
21 if ( ( current->filename != NULL ) && ( strcmp( current->filename, old ) == 0 ) )
23 /* File of that name currently open. Do not rename. */
26 current = current->next;
28 return _PDCLIB_rename( old, new );
34 #include <_PDCLIB_test.h>
43 /* make sure that neither file exists */
44 TESTCASE( fopen( testfile1, "r" ) == NULL );
45 TESTCASE( fopen( testfile2, "r" ) == NULL );
46 /* rename file 1 to file 2 - expected to fail */
47 TESTCASE( rename( testfile1, testfile2 ) != 0 );
49 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
50 TESTCASE( fputs( "x", file ) != EOF );
51 TESTCASE( fclose( file ) == 0 );
52 /* check that file 1 exists */
53 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
54 TESTCASE( fclose( file ) == 0 );
55 /* rename file 1 to file 2 */
56 TESTCASE( rename( testfile1, testfile2 ) == 0 );
57 /* check that file 2 exists, file 1 does not */
58 TESTCASE( fopen( testfile1, "r" ) == NULL );
59 TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
60 TESTCASE( fclose( file ) == 0 );
61 /* create another file 1 */
62 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
63 TESTCASE( fputs( "x", file ) != EOF );
64 TESTCASE( fclose( file ) == 0 );
65 /* check that file 1 exists */
66 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
67 TESTCASE( fclose( file ) == 0 );
68 /* rename file 1 to file 2 - expected to fail, see comment in
69 _PDCLIB_rename() itself.
71 /* NOREG as glibc overwrites existing destination file. */
72 TESTCASE_NOREG( rename( testfile1, testfile2 ) != 0 );
73 /* remove both files */
74 TESTCASE( remove( testfile1 ) == 0 );
75 TESTCASE( remove( testfile2 ) == 0 );
76 /* check that they're gone */
77 TESTCASE( fopen( testfile1, "r" ) == NULL );
78 TESTCASE( fopen( testfile2, "r" ) == NULL );