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 )
32 /* See the comments on implementation-defined errno values in
46 _PDCLIB_errno = _PDCLIB_ERROR;
49 /* This should be something like EUNKNOWN. */
50 _PDCLIB_errno = _PDCLIB_ERROR;
64 /* See the comments on implementation-defined errno values in
81 _PDCLIB_errno = _PDCLIB_ERROR;
84 /* This should be something like EUNKNOWN. */
85 _PDCLIB_errno = _PDCLIB_ERROR;
95 #include <_PDCLIB_test.h>
104 /* check that neither file exists */
105 TESTCASE( fopen( testfile1, "r" ) == NULL );
106 TESTCASE( fopen( testfile2, "r" ) == NULL );
107 /* rename file 1 to file 2 - expected to fail */
108 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
110 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
111 TESTCASE( fputc( 'x', file ) == 'x' );
112 TESTCASE( fclose( file ) == 0 );
113 /* check that file 1 exists */
114 TESTCASE( fopen( testfile1, "r" ) != NULL );
115 /* rename file 1 to file 2 */
116 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
117 /* check that file 2 exists, file 1 does not */
118 TESTCASE( fopen( testfile1, "r" ) == NULL );
119 TESTCASE( fopen( testfile2, "r" ) != NULL );
120 /* create another file 1 */
121 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
122 TESTCASE( fputc( 'x', file ) == 'x' );
123 TESTCASE( fclose( file ) == 0 );
124 /* check that file 1 exists */
125 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
126 TESTCASE( fclose( file ) == 0 );
127 /* rename file 1 to file 2 - expected to fail, see comment in
128 _PDCLIB_rename() itself.
130 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
131 /* remove both files */