]> pd.if.org Git - pdclib/blob - platform/example_cygwin/functions/_PDCLIB/rename.c
Comment cleanups.
[pdclib] / platform / example_cygwin / functions / _PDCLIB / rename.c
1 /* _PDCLIB_rename( const char *, const char * )
2
3    This file is part of the Public Domain C Library (PDCLib).
4    Permission is granted to use, modify, and / or redistribute at will.
5 */
6
7 #include <stdio.h>
8
9 #ifndef REGTEST
10 #include <_PDCLIB_glue.h>
11
12 #include </usr/include/errno.h>
13
14 extern int unlink( const char * pathname );
15 extern int link( const char * old, const char * new );
16
17 int _PDCLIB_rename( const char * old, const char * new )
18 {
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.
23     */
24     if ( link( old, new ) == 0 )
25     {
26         if ( unlink( old ) == EOF )
27         {
28             switch ( errno )
29             {
30                 /* See the comments on implementation-defined errno values in
31                    <_PDCLIB_config.h>.
32                 */
33                 case EACCES:
34                 case EFAULT:
35                 case EIO:
36                 case EISDIR:
37                 case ELOOP:
38                 case ENAMETOOLONG:
39                 case ENOENT:
40                 case ENOMEM:
41                 case ENOTDIR:
42                 case EPERM:
43                 case EROFS:
44                     _PDCLIB_errno = _PDCLIB_ERROR;
45                     break;
46                 default:
47                     /* This should be something like EUNKNOWN. */
48                     _PDCLIB_errno = _PDCLIB_ERROR;
49                     break;
50             }
51             return -1;
52         }
53         else
54         {
55             return 0;
56         }
57     }
58     else
59     {
60         switch ( errno )
61         {
62             /* See the comments on implementation-defined errno values in
63                <_PDCLIB_config.h>.
64             */
65             case EACCES:
66             case EEXIST:
67             case EFAULT:
68             case EIO:
69             case ELOOP:
70             case EMLINK:
71             case ENAMETOOLONG:
72             case ENOENT:
73             case ENOMEM:
74             case ENOSPC:
75             case ENOTDIR:
76             case EPERM:
77             case EROFS:
78             case EXDEV:
79                 _PDCLIB_errno = _PDCLIB_ERROR;
80                 break;
81             default:
82                 /* This should be something like EUNKNOWN. */
83                 _PDCLIB_errno = _PDCLIB_ERROR;
84                 break;
85         }
86         return EOF;
87     }
88 }
89
90 #endif
91
92 #ifdef TEST
93 #include <_PDCLIB_test.h>
94
95 #include <stdlib.h>
96
97 int main( void )
98 {
99     FILE * file;
100     remove( testfile1 );
101     remove( testfile2 );
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 );
107     /* create file 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.
127     */
128     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
129     /* remove both files */
130     remove( testfile1 );
131     remove( testfile2 );
132     return TEST_RESULTS;
133 }
134
135 #endif