1 /* _PDCLIB_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"
12 #include </usr/include/errno.h>
14 extern int unlink( const char * pathname );
15 extern int link( const char * old, const char * new );
17 int _PDCLIB_rename( const char * old, const char * new )
19 /* Note that the behaviour if new file exists is implementation-defined.
20 There is nothing wrong with either overwriting it or failing the
21 operation, but you might want to document whichever you chose.
22 This example fails if new file exists.
24 if ( link( old, new ) == 0 )
26 if ( unlink( old ) == EOF )
30 /* See the comments on implementation-defined errno values in
44 _PDCLIB_errno = _PDCLIB_ERROR;
47 /* This should be something like EUNKNOWN. */
48 _PDCLIB_errno = _PDCLIB_ERROR;
62 /* See the comments on implementation-defined errno values in
79 _PDCLIB_errno = _PDCLIB_ERROR;
82 /* This should be something like EUNKNOWN. */
83 _PDCLIB_errno = _PDCLIB_ERROR;
93 #include "_PDCLIB_test.h"
103 /* check that neither file exists */
104 TESTCASE( fopen( testfile1, "r" ) == NULL );
105 TESTCASE( fopen( testfile2, "r" ) == NULL );
106 /* rename file 1 to file 2 - expected to fail */
107 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
109 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
110 TESTCASE( fputc( 'x', file ) == 'x' );
111 TESTCASE( fclose( file ) == 0 );
112 /* check that file 1 exists */
113 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
114 TESTCASE( fclose( file ) == 0 );
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( ( file = fopen( testfile2, "r" ) ) != NULL );
120 TESTCASE( fclose( file ) == 0 );
121 /* create another file 1 */
122 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
123 TESTCASE( fputc( 'x', file ) == 'x' );
124 TESTCASE( fclose( file ) == 0 );
125 /* check that file 1 exists */
126 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
127 TESTCASE( fclose( file ) == 0 );
128 /* rename file 1 to file 2 - expected to fail, see comment in
129 _PDCLIB_rename() itself.
131 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
132 /* remove both files */