add System.Posix.Types to default nhc98 build
[haskell-directory.git] / cbits / dirUtils.c
index 86b3657..d6da255 100644 (file)
@@ -4,6 +4,7 @@
  * Directory Runtime Support
  */
 
+/* needed only for solaris2_HOST_OS */
 #include "ghcconfig.h"
 
 // The following is required on Solaris to force the POSIX versions of
@@ -14,7 +15,7 @@
 
 #include "HsBase.h"
 
-#if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
 #include <windows.h>
 
 static
@@ -56,16 +57,15 @@ toErrno(DWORD rc)
  * read an entry from the directory stream; opt for the
  * re-entrant friendly way of doing this, if available.
  */
-HsInt
-__hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
+int
+__hscore_readdir( DIR *dirPtr, struct dirent **pDirEnt )
 {
-  struct dirent **pDirE = (struct dirent**)pDirEnt;
 #if HAVE_READDIR_R
   struct dirent* p;
   int res;
   static unsigned int nm_max = (unsigned int)-1;
   
-  if (pDirE == NULL) {
+  if (pDirEnt == NULL) {
     return -1;
   }
   if (nm_max == (unsigned int)-1) {
@@ -79,24 +79,24 @@ __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
   }
   p = (struct dirent*)malloc(sizeof(struct dirent) + nm_max);
   if (p == NULL) return -1;
-  res = readdir_r((DIR*)dirPtr, p, pDirE);
+  res = readdir_r(dirPtr, p, pDirEnt);
   if (res != 0) {
-      *pDirE = NULL;
+      *pDirEnt = NULL;
       free(p);
   }
-  else if (*pDirE == NULL) {
+  else if (*pDirEnt == NULL) {
     // end of stream
     free(p);
   }
   return res;
 #else
 
-  if (pDirE == NULL) {
+  if (pDirEnt == NULL) {
     return -1;
   }
 
-  *pDirE = readdir((DIR*)dirPtr);
-  if (*pDirE == NULL) {
+  *pDirEnt = readdir(dirPtr);
+  if (*pDirEnt == NULL) {
     return -1;
   } else {
     return 0;
@@ -113,11 +113,10 @@ __hscore_readdir( HsAddr dirPtr, HsAddr pDirEnt )
  * an error
  *
  */
-HsInt
-__hscore_renameFile( HsAddr src,
-                    HsAddr dest)
+int
+__hscore_renameFile( char *src, char *dest)
 {
-#if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
     static int forNT = -1;
     
     /* ToDo: propagate error codes back */
@@ -179,7 +178,7 @@ __hscore_renameFile( HsAddr src,
  * that have shell32's lacking that particular API.
  *
  */
-#if defined(mingw32_HOST_OS) || defined(__MINGW32__) || defined(_MSC_VER)
+#if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
 int
 __hscore_getFolderPath(HWND hwndOwner,
@@ -191,26 +190,28 @@ __hscore_getFolderPath(HWND hwndOwner,
     static int loaded_dll = 0;
     static HMODULE hMod = (HMODULE)NULL;
     static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
+    /* The DLLs to try loading entry point from */
+    char* dlls[] = { "shell32.dll", "shfolder.dll" };
     
     if (loaded_dll < 0) {
        return (-1);
     } else if (loaded_dll == 0) {
-       hMod = LoadLibrary("shell32.dll");
-       if (hMod == NULL) {
-           loaded_dll = (-1);
-           return (-1);
-       } else {
-           funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA");
-           if (!funcPtr) {
-               loaded_dll = (-1);
-               return (-1);
-           } else {
+       int i;
+       for(i=0;i < sizeof(dlls); i++) {
+           hMod = LoadLibrary(dlls[i]);
+           if ( hMod != NULL &&
+                (funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA")) ) {
                loaded_dll = 1;
+               break;
            }
        }
+       if (loaded_dll == 0) {
+           loaded_dll = (-1);
+           return (-1);
+       }
     }
     /* OK, if we got this far the function has been bound */
     return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
-    /* ToDo: unload the DLL? */
+    /* ToDo: unload the DLL on shutdown? */
 }
 #endif