]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/_PDCLIB_rename.c
_PDCLIB_* prefixing of filenames.
[pdclib] / platform / example / functions / _PDCLIB / _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
11 #include "_PDCLIB_glue.h"
12
13 #include </usr/include/errno.h>
14
15 extern int unlink( const char * pathname );
16 extern int link( const char * old, const char * new );
17
18 int _PDCLIB_rename( const char * old, const char * new )
19 {
20     /* Note that the behaviour if new file exists is implementation-defined.
21        There is nothing wrong with either overwriting it or failing the
22        operation, but you might want to document whichever you chose.
23        This example fails if new file exists.
24     */
25     if ( link( old, new ) == 0 )
26     {
27         if ( unlink( old ) == EOF )
28         {
29             switch ( errno )
30             {
31                 /* See the comments on implementation-defined errno values in
32                    <_PDCLIB_config.h>.
33                 */
34                 case EACCES:
35                 case EFAULT:
36                 case EIO:
37                 case EISDIR:
38                 case ELOOP:
39                 case ENAMETOOLONG:
40                 case ENOENT:
41                 case ENOMEM:
42                 case ENOTDIR:
43                 case EPERM:
44                 case EROFS:
45                     _PDCLIB_errno = _PDCLIB_ERROR;
46                     break;
47                 default:
48                     /* This should be something like EUNKNOWN. */
49                     _PDCLIB_errno = _PDCLIB_ERROR;
50                     break;
51             }
52             return -1;
53         }
54         else
55         {
56             return 0;
57         }
58     }
59     else
60     {
61         switch ( errno )
62         {
63             /* See the comments on implementation-defined errno values in
64                <_PDCLIB_config.h>.
65             */
66             case EACCES:
67             case EEXIST:
68             case EFAULT:
69             case EIO:
70             case ELOOP:
71             case EMLINK:
72             case ENAMETOOLONG:
73             case ENOENT:
74             case ENOMEM:
75             case ENOSPC:
76             case ENOTDIR:
77             case EPERM:
78             case EROFS:
79             case EXDEV:
80                 _PDCLIB_errno = _PDCLIB_ERROR;
81                 break;
82             default:
83                 /* This should be something like EUNKNOWN. */
84                 _PDCLIB_errno = _PDCLIB_ERROR;
85                 break;
86         }
87         return EOF;
88     }
89 }
90
91 #endif
92
93 #ifdef TEST
94 #include "_PDCLIB_test.h"
95
96 #include <stdlib.h>
97
98 int main( void )
99 {
100 #ifndef REGTEST
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( ( file = fopen( testfile1, "r" ) ) != NULL );
115     TESTCASE( fclose( file ) == 0 );
116     /* rename file 1 to file 2 */
117     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
118     /* check that file 2 exists, file 1 does not */
119     TESTCASE( fopen( testfile1, "r" ) == NULL );
120     TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
121     TESTCASE( fclose( file ) == 0 );
122     /* create another file 1 */
123     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
124     TESTCASE( fputc( 'x', file ) == 'x' );
125     TESTCASE( fclose( file ) == 0 );
126     /* check that file 1 exists */
127     TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
128     TESTCASE( fclose( file ) == 0 );
129     /* rename file 1 to file 2 - expected to fail, see comment in
130        _PDCLIB_rename() itself.
131     */
132     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
133     /* remove both files */
134     remove( testfile1 );
135     remove( testfile2 );
136 #endif
137     return TEST_RESULTS;
138 }
139
140 #endif