Using constants for testfile names
[fw/pdclib] / platform / example / 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( ( file = fopen( testfile1, "r" ) ) != NULL );
107     TESTCASE( fclose( file ) == 0 );
108     /* rename file 1 to file 2 */
109     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
110     /* check that file 2 exists, file 1 does not */
111     TESTCASE( fopen( testfile1, "r" ) == NULL );
112     TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
113     TESTCASE( fclose( file ) == 0 );
114     /* create another file 1 */
115     TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
116     TESTCASE( fputc( 'x', file ) == 'x' );
117     TESTCASE( fclose( file ) == 0 );
118     /* check that file 1 exists */
119     TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
120     TESTCASE( fclose( file ) == 0 );
121     /* rename file 1 to file 2 - expected to fail, see comment in
122        _PDCLIB_rename() itself.
123     */
124     TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
125     /* remove both files */
126     remove( testfile1 );
127     remove( testfile2 );
128     return TEST_RESULTS;
129 }
130
131 #endif