]> pd.if.org Git - pdclib/blob - platform/posix/functions/_PDCLIB/_PDCLIB_rename.c
cc5a17392d1f9d8ef99822a759f6a8d1acedfd1e
[pdclib] / platform / posix / functions / _PDCLIB / _PDCLIB_rename.c
1 /* $Id$ */
2
3 /* _PDCLIB_rename( const char *, const char * )
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
9 #include <stdio.h>
10
11 #ifndef REGTEST
12 #include <_PDCLIB_glue.h>
13
14 #include </usr/include/errno.h>
15
16 extern int unlink( const char * pathname );
17 extern int link( const char * old, const char * new );
18
19 int _PDCLIB_rename( const char * old, const char * new )
20 {
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.
25     */
26     if ( link( old, new ) == 0 )
27     {
28         if ( unlink( old ) == EOF )
29         {
30             return -1;
31         }
32         else
33         {
34             return 0;
35         }
36     }
37     else
38     {
39         return EOF;
40     }
41 }
42
43 #endif
44
45 #ifdef TEST
46 #include <_PDCLIB_test.h>
47
48 #include <stdlib.h>
49
50 int main( void )
51 {
52 #ifndef REGTEST
53     FILE * file;
54     remove( testfile1 );
55     remove( testfile2 );
56     /* check that neither file exists */
57     TESTCASE( fopen( testfile1, "r" ) == NULL );
58     TESTCASE( fopen( testfile2, "r" ) == NULL );
59     /* rename file 1 to file 2 - expected to fail */
60     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
61     /* create file 1 */
62     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
63     TESTCASE( fputc( 'x', file ) == 'x' );
64     TESTCASE( fclose( file ) == 0 );
65     /* check that file 1 exists */
66     TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
67     TESTCASE( fclose( file ) == 0 );
68     /* rename file 1 to file 2 */
69     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
70     /* check that file 2 exists, file 1 does not */
71     TESTCASE( fopen( testfile1, "r" ) == NULL );
72     TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
73     TESTCASE( fclose( file ) == 0 );
74     /* create another file 1 */
75     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
76     TESTCASE( fputc( 'x', file ) == 'x' );
77     TESTCASE( fclose( file ) == 0 );
78     /* check that file 1 exists */
79     TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
80     TESTCASE( fclose( file ) == 0 );
81     /* rename file 1 to file 2 - expected to fail, see comment in
82        _PDCLIB_rename() itself.
83     */
84     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
85     /* remove both files */
86     remove( testfile1 );
87     remove( testfile2 );
88 #endif
89     return TEST_RESULTS;
90 }
91
92 #endif