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>
102 /* check that neither file exists */
103 TESTCASE( fopen( testfile1, "r" ) == NULL );
104 TESTCASE( fopen( testfile2, "r" ) == NULL );
105 /* rename file 1 to file 2 - expected to fail */
106 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
108 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
109 TESTCASE( fputc( 'x', file ) == 'x' );
110 TESTCASE( fclose( file ) == 0 );
111 /* check that file 1 exists */
112 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
113 TESTCASE( fclose( file ) == 0 );
114 /* rename file 1 to file 2 */
115 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
116 /* check that file 2 exists, file 1 does not */
117 TESTCASE( fopen( testfile1, "r" ) == NULL );
118 TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
119 TESTCASE( fclose( file ) == 0 );
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 */