X-Git-Url: https://pd.if.org/git/?a=blobdiff_plain;f=platform%2Fexample%2Ffunctions%2F_PDCLIB%2Frename.c;h=53cf84cb4063a7ff6ef97662314a7dd70e19e0de;hb=b189506b1b895940933bdfc5d6d6ae5d0ba65721;hp=ab1a75d6a51ebd6edf6d4a91e730b6fff033975e;hpb=42bbbcf0bf71d93b6dff9d86e96a2791da1383be;p=pdclib diff --git a/platform/example/functions/_PDCLIB/rename.c b/platform/example/functions/_PDCLIB/rename.c index ab1a75d..53cf84c 100644 --- a/platform/example/functions/_PDCLIB/rename.c +++ b/platform/example/functions/_PDCLIB/rename.c @@ -6,10 +6,16 @@ Permission is granted to use, modify, and / or redistribute at will. */ +#include + #ifndef REGTEST -#include #include <_PDCLIB_glue.h> +#include + +extern int unlink( const char * pathname ); +extern int link( const char * old, const char * new ); + int _PDCLIB_rename( const char * old, const char * new ) { /* Note that the behaviour if new file exists is implementation-defined. @@ -19,29 +25,76 @@ int _PDCLIB_rename( const char * old, const char * new ) */ if ( link( old, new ) == 0 ) { - return unlink( old ); + if ( unlink( old ) == EOF ) + { + switch ( errno ) + { + case EACCES: + case EFAULT: + case EIO: + case EISDIR: + case ELOOP: + case ENAMETOOLONG: + case ENOENT: + case ENOMEM: + case ENOTDIR: + case EPERM: + case EROFS: + _PDCLIB_errno = _PDCLIB_EIO; + break; + default: + _PDCLIB_errno = _PDCLIB_EUNKNOWN; + break; + } + return -1; + } + else + { + return 0; + } } else { - return -1; + switch ( errno ) + { + case EACCES: + case EEXIST: + case EFAULT: + case EIO: + case ELOOP: + case EMLINK: + case ENAMETOOLONG: + case ENOENT: + case ENOMEM: + case ENOSPC: + case ENOTDIR: + case EPERM: + case EROFS: + case EXDEV: + _PDCLIB_errno = _PDCLIB_EIO; + break; + default: + _PDCLIB_errno = _PDCLIB_EUNKNOWN; + break; + } + return EOF; } } #endif #ifdef TEST -/* TODO: Work around the following undef */ -#undef SEEK_SET #include <_PDCLIB_test.h> #include int main( void ) { - char filename1[ L_tmpnam + 6 ] = "touch "; - char filename2[ L_tmpnam ]; - tmpnam( filename1 + 6 ); - tmpnam( filename2); + char filename1[] = "touch testfile1"; + char filename2[] = "testfile2"; + FILE * file; + remove( filename1 + 6 ); + remove( filename2 ); /* check that neither file exists */ TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); TESTCASE( fopen( filename2, "r" ) == NULL ); @@ -50,16 +103,19 @@ int main( void ) /* create file 1 */ system( filename1 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 */ TESTCASE( _PDCLIB_rename( filename1 + 6, filename2 ) == 0 ); /* check that file 2 exists, file 1 does not */ TESTCASE( fopen( filename1 + 6, "r" ) == NULL ); - TESTCASE( fopen( filename2, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename2, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* create another file 1 */ system( filename1 ); /* check that file 1 exists */ - TESTCASE( fopen( filename1 + 6, "r" ) != NULL ); + TESTCASE( ( file = fopen( filename1 + 6, "r" ) ) != NULL ); + TESTCASE( fclose( file ) == 0 ); /* rename file 1 to file 2 - expected to fail, see comment in _PDCLIB_rename() itself. */