3 /* rename( const char *, const char * )
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
12 #include <_PDCLIB_glue.h>
16 extern struct _PDCLIB_file_t * _PDCLIB_filelist;
18 int rename( const char * old, const char * new )
20 struct _PDCLIB_file_t * current = _PDCLIB_filelist;
21 while ( current != NULL )
23 if ( ( current->filename != NULL ) && ( strcmp( current->filename, old ) == 0 ) )
25 /* File of that name currently open. Do not rename. */
28 current = current->next;
30 return _PDCLIB_rename( old, new );
36 #include <_PDCLIB_test.h>
42 char filename1[] = "touch testfile1";
43 char filename2[] = "testfile2";
45 remove( filename1 + 6 );
47 /* make sure that neither file exists */
48 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
49 TESTCASE( fopen( filename2, "r" ) == NULL );
50 /* rename file 1 to file 2 - expected to fail */
51 TESTCASE( rename( filename1 + 6, filename2 ) == -1 );
54 /* check that file 1 exists */
55 TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL );
56 TESTCASE( fclose( file ) == 0 );
57 /* rename file 1 to file 2 */
58 TESTCASE( rename( filename1 + 6, filename2 ) == 0 );
59 /* check that file 2 exists, file 1 does not */
60 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
61 TESTCASE( ( file = fopen( filename2, "r" ) ) != NULL );
62 TESTCASE( fclose( file ) == 0 );
63 /* create another file 1 */
65 /* check that file 1 exists */
66 TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL );
67 TESTCASE( fclose( file ) == 0 );
68 /* rename file 1 to file 2 - expected to fail, see comment in
69 _PDCLIB_rename() itself.
72 TESTCASE( rename( filename1 + 6, filename2 ) == -1 );
74 /* glibc rename() overwrites existing destination file. */
75 TESTCASE( rename( filename1 + 6, filename2 ) == 0 );
77 /* remove both files */
78 remove( filename1 + 6 );
80 /* check that they're gone */
81 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
82 TESTCASE( fopen( filename2, "r" ) == NULL );