Update WCsubst.c for Unicode 5.1.0, and add a README.Unicode
[ghc-base.git] / cbits / dirUtils.c
index a224004..8e87db3 100644 (file)
 /* 
- * (c) The GRASP/AQUA Project, Glasgow University, 1994-
+ * (c) The University of Glasgow 2002
  *
  * Directory Runtime Support
  */
-#include "dirUtils.h"
 
-#if defined(mingw32_TARGET_OS)
-#include <windows.h>
-#endif
+/* needed only for solaris2_HOST_OS */
+#include "ghcconfig.h"
 
-#ifdef HAVE_STDLIB_H
-# include <stdlib.h>
-#endif
-#ifdef HAVE_STDDEF_H
-# include <stddef.h>
-#endif
-#ifdef HAVE_ERRNO_H
-# include <errno.h>
+// The following is required on Solaris to force the POSIX versions of
+// the various _r functions instead of the Solaris versions.
+#ifdef solaris2_HOST_OS
+#define _POSIX_PTHREAD_SEMANTICS
 #endif
 
-HsInt
-prel_mkdir(HsAddr pathName, HsInt mode)
-{
-#if defined(mingw32_TARGET_OS)
-  return mkdir(pathName);
-#else
-  return mkdir(pathName,mode);
+#include "HsBase.h"
+
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
+#include <windows.h>
 #endif
-}
 
-HsInt
-prel_lstat(HsAddr fname, HsAddr st)
+
+/*
+ * read an entry from the directory stream; opt for the
+ * re-entrant friendly way of doing this, if available.
+ */
+int
+__hscore_readdir( DIR *dirPtr, struct dirent **pDirEnt )
 {
-#ifdef HAVE_LSTAT
-  return lstat((const char*)fname, (struct stat*)st);
+#if HAVE_READDIR_R
+  struct dirent* p;
+  int res;
+  static unsigned int nm_max = (unsigned int)-1;
+  
+  if (pDirEnt == NULL) {
+    return -1;
+  }
+  if (nm_max == (unsigned int)-1) {
+#ifdef NAME_MAX
+    nm_max = NAME_MAX + 1;
 #else
-  return stat((const char*)fname, (struct stat*)st);
+    nm_max = pathconf(".", _PC_NAME_MAX);
+    if (nm_max == -1) { nm_max = 255; }
+    nm_max++;
 #endif
-}
-
-HsInt prel_s_ISDIR(mode_t m) {return S_ISDIR(m);}
-HsInt prel_s_ISREG(mode_t m) {return S_ISREG(m);}
-
-HsInt prel_sz_stat()  { return sizeof(struct stat); }
-HsInt prel_path_max() { return PATH_MAX; }
-mode_t prel_R_OK() { return R_OK; }
-mode_t prel_W_OK() { return W_OK; }
-mode_t prel_X_OK() { return X_OK; }
-
-mode_t prel_S_IRUSR() { return S_IRUSR; }
-mode_t prel_S_IWUSR() { return S_IWUSR; }
-mode_t prel_S_IXUSR() { return S_IXUSR; }
+  }
+  p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
+  if (p == NULL) return -1;
+  res = readdir_r(dirPtr, p, pDirEnt);
+  if (res != 0) {
+      *pDirEnt = NULL;
+      free(p);
+  }
+  else if (*pDirEnt == NULL) {
+    // end of stream
+    free(p);
+  }
+  return res;
+#else
 
-time_t prel_st_mtime(struct stat* st) { return st->st_mtime; }
-mode_t prel_st_mode(struct stat* st) { return st->st_mode; }
+  if (pDirEnt == NULL) {
+    return -1;
+  }
 
-HsAddr prel_d_name(struct dirent* d)
-{ 
-#ifndef mingw32_TARGET_OS
-  return (HsAddr)(&d->d_name);
-#else
-  return (HsAddr)(d->d_name);
+  *pDirEnt = readdir(dirPtr);
+  if (*pDirEnt == NULL) {
+    return -1;
+  } else {
+    return 0;
+  }  
 #endif
 }
 
-HsInt prel_end_of_dir()
+/*
+ * 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
+ *
+ */
+int
+__hscore_renameFile( char *src, char *dest)
 {
-#ifndef mingw32_TARGET_OS
-  return 0;
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
+    static int forNT = -1;
+    
+    /* ToDo: propagate error codes back */
+    if (MoveFileA(src, dest)) {
+       return 0;
+    } else {
+       ;
+    }
+    
+    /* Failed...it could be because the target already existed. */
+    if ( !GetFileAttributes(dest) ) {
+       /* No, it's not there - just fail. */
+       maperrno();
+       return (-1);
+    }
+
+    if (forNT == -1) {
+       OSVERSIONINFO ovi;
+       ovi.dwOSVersionInfoSize = sizeof(ovi);
+       if ( !GetVersionEx(&ovi) ) {
+           maperrno();
+           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 {
+           maperrno();
+           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 {
+           maperrno();
+           return (-1);
+       }
+    } else {
+       maperrno();
+       return (-1);
+    }
 #else
-  return ENOENT;
-#endif  
+    return rename(src,dest);
+#endif
 }