[project @ 2003-08-01 15:56:11 by panne]
[ghc-base.git] / cbits / dirUtils.c
index 8d94a45..ad3b406 100644 (file)
@@ -34,7 +34,7 @@ __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
   if (pDirE == NULL) {
     return -1;
   }
-  if (nm_max == -1) {
+  if (nm_max == (unsigned int)-1) {
 #ifdef NAME_MAX
     nm_max = NAME_MAX + 1;
 #else
@@ -65,3 +65,73 @@ __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
   }  
 #endif
 }
+
+/*
+ * Function: __hscore_renameFile()
+ *
+ * Provide Haskell98's semantics for renaming files and directories.
+ * It mirrors that of POSIX.1's behaviour for rename() by overwriting
+ * the target if it exists (the MS CRT implementation of rename() returns
+ * an error
+ *
+ */
+HsInt
+__hscore_renameFile( HsAddr src,
+                    HsAddr dest)
+{
+#if defined(_MSC_VER) || defined(_WIN32)
+    static int forNT = -1;
+    DWORD rc;
+    
+    /* ToDo: propagate error codes back */
+    if (MoveFileA(src, dest)) {
+       return 0;
+    } else {
+       rc = GetLastError();
+    }
+    
+    /* Failed...it could be because the target already existed. */
+    if ( !GetFileAttributes(dest) ) {
+       /* No, it's not there - just fail. */
+       errno = 0;
+       return (-1);
+    }
+
+    if (forNT == -1) {
+       OSVERSIONINFO ovi;
+       ovi.dwOSVersionInfoSize = sizeof(ovi);
+       if ( !GetVersionEx(&ovi) ) {
+           errno = 0; 
+           return (-1);
+       }
+       forNT = ((ovi.dwPlatformId & VER_PLATFORM_WIN32_NT) != 0);
+    }
+    
+    if (forNT) {
+       /* Easy, go for MoveFileEx() */
+       if ( MoveFileExA(src, dest, MOVEFILE_REPLACE_EXISTING) ) {
+           return 0;
+       } else {
+           errno = 0; 
+           return (-1);
+       }
+    }
+
+    /* No MoveFileEx() for Win9x, try deleting the target. */
+    /* Similarly, if the MoveFile*() ops didn't work out under NT */
+    if (DeleteFileA(dest)) {
+       if (MoveFileA(src,dest)) {
+           return 0;
+       } else {
+           errno = 0;
+           return (-1);
+       }
+    } else {
+       errno = 0;
+       return (-1);
+    }
+#else
+    return rename(src,dest);
+#endif
+}
+