]> pd.if.org Git - pdclib/blob - platform/example/functions/_PDCLIB/rename.c
Joined 32 and 64 bit configurations, made platform linking unnecessary.
[pdclib] / platform / example / 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 #ifndef REGTEST
100     FILE * file;
101     remove( testfile1 );
102     remove( testfile2 );
103     /* check that neither file exists */
104     TESTCASE( fopen( testfile1, "r" ) == NULL );
105     TESTCASE( fopen( testfile2, "r" ) == NULL );
106     /* rename file 1 to file 2 - expected to fail */
107     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
108     /* create file 1 */
109     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
110     TESTCASE( fputc( 'x', file ) == 'x' );
111     TESTCASE( fclose( file ) == 0 );
112     /* check that file 1 exists */
113     TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
114     TESTCASE( fclose( file ) == 0 );
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( ( file = fopen( testfile2, "r" ) ) != NULL );
120     TESTCASE( fclose( file ) == 0 );
121     /* create another file 1 */
122     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
123     TESTCASE( fputc( 'x', file ) == 'x' );
124     TESTCASE( fclose( file ) == 0 );
125     /* check that file 1 exists */
126     TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
127     TESTCASE( fclose( file ) == 0 );
128     /* rename file 1 to file 2 - expected to fail, see comment in
129        _PDCLIB_rename() itself.
130     */
131     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
132     /* remove both files */
133     remove( testfile1 );
134     remove( testfile2 );
135 #endif
136     return TEST_RESULTS;
137 }
138
139 #endif