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.
12 #include <_PDCLIB_glue.h>
14 #include </usr/include/errno.h>
16 extern int unlink( const char * pathname );
17 extern int link( const char * old, const char * new );
19 int _PDCLIB_rename( const char * old, const char * new )
21 /* Note that the behaviour if new file exists is implementation-defined.
22 There is nothing wrong with either overwriting it or failing the
23 operation, but you might want to document whichever you chose.
24 This example fails if new file exists.
26 if ( link( old, new ) == 0 )
28 if ( unlink( old ) == EOF )
43 _PDCLIB_errno = _PDCLIB_EIO;
46 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
74 _PDCLIB_errno = _PDCLIB_EIO;
77 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
87 #include <_PDCLIB_test.h>
93 char filename1[] = "touch testfile1";
94 char filename2[] = "testfile2";
95 remove( filename1 + 6 );
97 /* check that neither file exists */
98 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
99 TESTCASE( fopen( filename2, "r" ) == NULL );
100 /* rename file 1 to file 2 - expected to fail */
101 TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 );
104 /* check that file 1 exists */
105 TESTCASE( fopen( filename1 + 6, "r" ) != NULL );
106 /* rename file 1 to file 2 */
107 TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 );
108 /* check that file 2 exists, file 1 does not */
109 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
110 TESTCASE( fopen( filename2, "r" ) != NULL );
111 /* create another file 1 */
113 /* check that file 1 exists */
114 TESTCASE( fopen( filename1 + 6, "r" ) != NULL );
115 /* rename file 1 to file 2 - expected to fail, see comment in
116 _PDCLIB_rename() itself.
118 TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 );
119 /* remove both files */
120 remove( filename1 + 6 );
122 /* check that they're gone */
123 TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
124 TESTCASE( fopen( filename2, "r" ) == NULL );