Using constants for testfile names
[fw/pdclib] / platform / example / functions / _PDCLIB / rename.c
index 6b7ecf19d4a2e8ef91c0d0c2ad65b9a7215cdb1f..f314e4c5186cdba5b8e635f6ee3a915900c95c0e 100644 (file)
@@ -6,10 +6,16 @@
    Permission is granted to use, modify, and / or redistribute at will.
 */
 
+#include <stdio.h>
+
 #ifndef REGTEST
-#include <unistd.h>
 #include <_PDCLIB_glue.h>
 
+#include </usr/include/errno.h>
+
+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,24 +25,106 @@ 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 <stdlib.h>
+
 int main( void )
 {
-    TESTCASE( NO_TESTDRIVER );
+    FILE * file;
+    remove( testfile1 );
+    remove( testfile2 );
+    /* check that neither file exists */
+    TESTCASE( fopen( testfile1, "r" ) == NULL );
+    TESTCASE( fopen( testfile2, "r" ) == NULL );
+    /* rename file 1 to file 2 - expected to fail */
+    TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
+    /* create file 1 */
+    TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
+    TESTCASE( fputc( 'x', file ) == 'x' );
+    TESTCASE( fclose( file ) == 0 );
+    /* check that file 1 exists */
+    TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
+    TESTCASE( fclose( file ) == 0 );
+    /* rename file 1 to file 2 */
+    TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == 0 );
+    /* check that file 2 exists, file 1 does not */
+    TESTCASE( fopen( testfile1, "r" ) == NULL );
+    TESTCASE( ( file = fopen( testfile2, "r" ) ) != NULL );
+    TESTCASE( fclose( file ) == 0 );
+    /* create another file 1 */
+    TESTCASE( ( file = fopen( testfile1, "w" ) ) != NULL );
+    TESTCASE( fputc( 'x', file ) == 'x' );
+    TESTCASE( fclose( file ) == 0 );
+    /* check that file 1 exists */
+    TESTCASE( ( file = fopen( testfile1, "r" ) ) != NULL );
+    TESTCASE( fclose( file ) == 0 );
+    /* rename file 1 to file 2 - expected to fail, see comment in
+       _PDCLIB_rename() itself.
+    */
+    TESTCASE( _PDCLIB_rename( testfile1, testfile2 ) == -1 );
+    /* remove both files */
+    remove( testfile1 );
+    remove( testfile2 );
     return TEST_RESULTS;
 }