Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/waterbox/libc/functions/stdio/rename.c
2 views
1
/* 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 <string.h>
13
14
extern _PDCLIB_file_t * _PDCLIB_filelist;
15
16
int rename( const char * old, const char * new )
17
{
18
FILE * current = _PDCLIB_filelist;
19
while ( current != NULL )
20
{
21
if ( ( current->filename != NULL ) && ( strcmp( current->filename, old ) == 0 ) )
22
{
23
/* File of that name currently open. Do not rename. */
24
return EOF;
25
}
26
current = current->next;
27
}
28
return _PDCLIB_rename( old, new );
29
}
30
31
#endif
32
33
#ifdef TEST
34
#include "_PDCLIB_test.h"
35
36
#include <stdlib.h>
37
38
int main( void )
39
{
40
FILE * file;
41
remove( testfile1 );
42
remove( testfile2 );
43
/* make sure that neither file exists */
44
TESTCASE( fopen( testfile1, "r" ) == NULL );
45
TESTCASE( fopen( testfile2, "r" ) == NULL );
46
/* rename file 1 to file 2 - expected to fail */
47
TESTCASE( rename( testfile1, testfile2 ) != 0 );
48
/* create file 1 */
49
TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
50
TESTCASE( fputs( "x", file ) != EOF );
51
TESTCASE( fclose( file ) == 0 );
52
/* check that file 1 exists */
53
TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
54
TESTCASE( fclose( file ) == 0 );
55
/* rename file 1 to file 2 */
56
TESTCASE( rename( testfile1, testfile2 ) == 0 );
57
/* check that file 2 exists, file 1 does not */
58
TESTCASE( fopen( testfile1, "r" ) == NULL );
59
TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
60
TESTCASE( fclose( file ) == 0 );
61
/* create another file 1 */
62
TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
63
TESTCASE( fputs( "x", file ) != EOF );
64
TESTCASE( fclose( file ) == 0 );
65
/* check that file 1 exists */
66
TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
67
TESTCASE( fclose( file ) == 0 );
68
/* rename file 1 to file 2 - expected to fail, see comment in
69
_PDCLIB_rename() itself.
70
*/
71
/* NOREG as glibc overwrites existing destination file. */
72
TESTCASE_NOREG( rename( testfile1, testfile2 ) != 0 );
73
/* remove both files */
74
TESTCASE( remove( testfile1 ) == 0 );
75
TESTCASE( remove( testfile2 ) == 0 );
76
/* check that they're gone */
77
TESTCASE( fopen( testfile1, "r" ) == NULL );
78
TESTCASE( fopen( testfile2, "r" ) == NULL );
79
return TEST_RESULTS;
80
}
81
82
#endif
83
84
85