3 /* _PDCLIB_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.
11 #include <_PDCLIB_glue.h>
13 int _PDCLIB_rename( const char * old, const char * new )
15 /* Note that the behaviour if new file exists is implementation-defined.
16 There is nothing wrong with either overwriting it or failing the
17 operation, but you might want to document whichever you chose.
18 This example fails if new file exists.
20 if ( link( old, new ) == 0 )
33 /* TODO: Work around the following undef */
35 #include <_PDCLIB_test.h>
41 char filename1[] = "touch testfile1";
42 char filename2[] = "testfile2";
43 /* check that neither file exists */
44 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
45 TESTCASE( fopen( filename2, "r" ) == NULL );
46 /* rename file 1 to file 2 - expected to fail */
47 TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 );
50 /* check that file 1 exists */
51 TESTCASE( fopen( filename1 + 6, "r" ) != NULL );
52 /* rename file 1 to file 2 */
53 TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 );
54 /* check that file 2 exists, file 1 does not */
55 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
56 TESTCASE( fopen( filename2, "r" ) != NULL );
57 /* create another file 1 */
59 /* check that file 1 exists */
60 TESTCASE( fopen( filename1 + 6, "r" ) != NULL );
61 /* rename file 1 to file 2 - expected to fail, see comment in
62 _PDCLIB_rename() itself.
64 TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 );
65 /* remove both files */
66 remove( filename1 + 6 );
68 /* check that they're gone */
69 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
70 TESTCASE( fopen( filename2, "r" ) == NULL );