]> pd.if.org Git - pdclib/blob - platform/example_cygwin/functions/_PDCLIB/rename.c
Addressed ticket #40 (non-standard errno values).
[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                 /* See the comments on implementation-defined errno values in
33                    <_PDCLIB_config.h>.
34                 */
35                 case EACCES:
36                 case EFAULT:
37                 case EIO:
38                 case EISDIR:
39                 case ELOOP:
40                 case ENAMETOOLONG:
41                 case ENOENT:
42                 case ENOMEM:
43                 case ENOTDIR:
44                 case EPERM:
45                 case EROFS:
46                     _PDCLIB_errno = _PDCLIB_ERROR;
47                     break;
48                 default:
49                     /* This should be something like EUNKNOWN. */
50                     _PDCLIB_errno = _PDCLIB_ERROR;
51                     break;
52             }
53             return -1;
54         }
55         else
56         {
57             return 0;
58         }
59     }
60     else
61     {
62         switch ( errno )
63         {
64             /* See the comments on implementation-defined errno values in
65                <_PDCLIB_config.h>.
66             */
67             case EACCES:
68             case EEXIST:
69             case EFAULT:
70             case EIO:
71             case ELOOP:
72             case EMLINK:
73             case ENAMETOOLONG:
74             case ENOENT:
75             case ENOMEM:
76             case ENOSPC:
77             case ENOTDIR:
78             case EPERM:
79             case EROFS:
80             case EXDEV:
81                 _PDCLIB_errno = _PDCLIB_ERROR;
82                 break;
83             default:
84                 /* This should be something like EUNKNOWN. */
85                 _PDCLIB_errno = _PDCLIB_ERROR;
86                 break;
87         }
88         return EOF;
89     }
90 }
91
92 #endif
93
94 #ifdef TEST
95 #include <_PDCLIB_test.h>
96
97 #include <stdlib.h>
98
99 int main( void )
100 {
101     FILE * file;
102     remove( testfile1 );
103     remove( testfile2 );
104     /* check that neither file exists */
105     TESTCASE( fopen( testfile1, "r" ) == NULL );
106     TESTCASE( fopen( testfile2, "r" ) == NULL );
107     /* rename file 1 to file 2 - expected to fail */
108     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
109     /* create file 1 */
110     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
111     TESTCASE( fputc( 'x', file ) == 'x' );
112     TESTCASE( fclose( file ) == 0 );
113     /* check that file 1 exists */
114     TESTCASE( fopen( testfile1, "r" ) != NULL );
115     /* rename file 1 to file 2 */
116     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
117     /* check that file 2 exists, file 1 does not */
118     TESTCASE( fopen( testfile1, "r" ) == NULL );
119     TESTCASE( fopen( testfile2, "r" ) != NULL );
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.
129     */
130     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
131     /* remove both files */
132     remove( testfile1 );
133     remove( testfile2 );
134     return TEST_RESULTS;
135 }
136
137 #endif