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( fopen( testfile1, "r" ) != NULL );
113 /* rename file 1 to file 2 */
114 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
115 /* check that file 2 exists, file 1 does not */
116 TESTCASE( fopen( testfile1, "r" ) == NULL );
117 TESTCASE( fopen( testfile2, "r" ) != NULL );
118 /* create another file 1 */
119 TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
120 TESTCASE( fputc( 'x', file ) == 'x' );
121 TESTCASE( fclose( file ) == 0 );
122 /* check that file 1 exists */
123 TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
124 TESTCASE( fclose( file ) == 0 );
125 /* rename file 1 to file 2 - expected to fail, see comment in
126 _PDCLIB_rename() itself.
128 TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
129 /* remove both files */