53cf84cb4063a7ff6ef97662314a7dd70e19e0de
[fw/pdclib] / platform / example / functions / _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             switch ( errno )
31             {
32                 case EACCES:
33                 case EFAULT:
34                 case EIO:
35                 case EISDIR:
36                 case ELOOP:
37                 case ENAMETOOLONG:
38                 case ENOENT:
39                 case ENOMEM:
40                 case ENOTDIR:
41                 case EPERM:
42                 case EROFS:
43                     _PDCLIB_errno = _PDCLIB_EIO;
44                     break;
45                 default:
46                     _PDCLIB_errno = _PDCLIB_EUNKNOWN;
47                     break;
48             }
49             return -1;
50         }
51         else
52         {
53             return 0;
54         }
55     }
56     else
57     {
58         switch ( errno )
59         {
60             case EACCES:
61             case EEXIST:
62             case EFAULT:
63             case EIO:
64             case ELOOP:
65             case EMLINK:
66             case ENAMETOOLONG:
67             case ENOENT:
68             case ENOMEM:
69             case ENOSPC:
70             case ENOTDIR:
71             case EPERM:
72             case EROFS:
73             case EXDEV:
74                 _PDCLIB_errno = _PDCLIB_EIO;
75                 break;
76             default:
77                 _PDCLIB_errno = _PDCLIB_EUNKNOWN;
78                 break;
79         }
80         return EOF;
81     }
82 }
83
84 #endif
85
86 #ifdef TEST
87 #include <_PDCLIB_test.h>
88
89 #include <stdlib.h>
90
91 int main( void )
92 {
93     char filename1[] = "touch testfile1";
94     char filename2[] = "testfile2";
95     FILE * file;
96     remove( filename1 + 6 );
97     remove( filename2 );
98     /* check that neither file exists */
99     TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
100     TESTCASE( fopen( filename2, "r" ) == NULL );
101     /* rename file 1 to file 2 - expected to fail */
102     TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 );
103     /* create file 1 */
104     system( filename1 );
105     /* check that file 1 exists */
106     TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL );
107     TESTCASE( fclose( file ) == 0 );
108     /* rename file 1 to file 2 */
109     TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 );
110     /* check that file 2 exists, file 1 does not */
111     TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
112     TESTCASE( ( file = fopen( filename2, "r" ) ) != NULL );
113     TESTCASE( fclose( file ) == 0 );
114     /* create another file 1 */
115     system( filename1 );
116     /* check that file 1 exists */
117     TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL );
118     TESTCASE( fclose( file ) == 0 );
119     /* rename file 1 to file 2 - expected to fail, see comment in
120        _PDCLIB_rename() itself.
121     */
122     TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == -1 );
123     /* remove both files */
124     remove( filename1 + 6 );
125     remove( filename2 );
126     /* check that they're gone */
127     TESTCASE( fopen( filename1 + 6, "r" ) == NULL );
128     TESTCASE( fopen( filename2, "r" ) == NULL );
129     return TEST_RESULTS;
130 }
131
132 #endif