]> pd.if.org Git - pdclib/blob - platform/example_cygwin/functions/_PDCLIB/rename.c
e8e7cb179bfe36da140eed3ce3c3336195dea958
[pdclib] / platform / example_cygwin / 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     FILE * file;
94     remove( testfile1 );
95     remove( testfile2 );
96     /* check that neither file exists */
97     TESTCASE( fopen( testfile1, "r" ) == NULL );
98     TESTCASE( fopen( testfile2, "r" ) == NULL );
99     /* rename file 1 to file 2 - expected to fail */
100     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
101     /* create file 1 */
102     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
103     TESTCASE( fputc( 'x', file ) == 'x' );
104     TESTCASE( fclose( file ) == 0 );
105     /* check that file 1 exists */
106     TESTCASE( fopen( testfile1, "r" ) != NULL );
107     /* rename file 1 to file 2 */
108     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
109     /* check that file 2 exists, file 1 does not */
110     TESTCASE( fopen( testfile1, "r" ) == NULL );
111     TESTCASE( fopen( testfile2, "r" ) != NULL );
112     /* create another file 1 */
113     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
114     TESTCASE( fputc( 'x', file ) == 'x' );
115     TESTCASE( fclose( file ) == 0 );
116     /* check that file 1 exists */
117     TESTCASE( ( file = fopen( testfile1, "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( testfile1, testfile2 ) == -1 );
123     /* remove both files */
124     remove( testfile1 );
125     remove( testfile2 );
126     return TEST_RESULTS;
127 }
128
129 #endif