+setCurrentDirectory path =
+#ifdef mingw32_HOST_OS
+ Win32.setCurrentDirectory path
+#else
+ Posix.changeWorkingDirectory path
+#endif
+
+#endif /* __GLASGOW_HASKELL__ */
+
+#ifndef __HUGS__
+{- |The operation 'doesDirectoryExist' returns 'True' if the argument file
+exists and is a directory, and 'False' otherwise.
+-}
+
+doesDirectoryExist :: FilePath -> IO Bool
+doesDirectoryExist name =
+#ifdef mingw32_HOST_OS
+ (withFileStatus "doesDirectoryExist" name $ \st -> isDirectory st)
+#else
+ (do stat <- Posix.getFileStatus name
+ return (Posix.fileMode stat .&. Posix.directoryMode /= 0))
+#endif
+ `catch` ((\ _ -> return False) :: IOException -> IO Bool)
+
+{- |The operation 'doesFileExist' returns 'True'
+if the argument file exists and is not a directory, and 'False' otherwise.
+-}
+
+doesFileExist :: FilePath -> IO Bool
+doesFileExist name =
+#ifdef mingw32_HOST_OS
+ (withFileStatus "doesFileExist" name $ \st -> do b <- isDirectory st; return (not b))
+#else
+ (do stat <- Posix.getFileStatus name
+ return (Posix.fileMode stat .&. Posix.directoryMode == 0))
+#endif
+ `catch` ((\ _ -> return False) :: IOException -> IO Bool)
+
+{- |The 'getModificationTime' operation returns the
+clock time at which the file or directory was last modified.
+
+The operation may fail with:
+
+* 'isPermissionError' if the user is not permitted to access
+ the modification time; or
+
+* 'isDoesNotExistError' if the file or directory does not exist.
+
+-}
+
+getModificationTime :: FilePath -> IO ClockTime
+getModificationTime name = do
+#ifdef mingw32_HOST_OS
+ -- ToDo: use Win32 API
+ withFileStatus "getModificationTime" name $ \ st -> do
+ modificationTime st
+#else
+ stat <- Posix.getFileStatus name
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
+ return (TOD (realToInteger (Posix.modificationTime stat)) 0)
+#endif
+
+
+#endif /* !__HUGS__ */
+
+#ifdef mingw32_HOST_OS
+withFileStatus :: String -> FilePath -> (Ptr CStat -> IO a) -> IO a
+withFileStatus loc name f = do
+ modifyIOError (`ioeSetFileName` name) $
+ allocaBytes sizeof_stat $ \p ->
+ withFilePath (fileNameEndClean name) $ \s -> do
+ throwErrnoIfMinus1Retry_ loc (c_stat s p)
+ f p
+
+withFileOrSymlinkStatus :: String -> FilePath -> (Ptr CStat -> IO a) -> IO a
+withFileOrSymlinkStatus loc name f = do
+ modifyIOError (`ioeSetFileName` name) $
+ allocaBytes sizeof_stat $ \p ->
+ withFilePath name $ \s -> do
+ throwErrnoIfMinus1Retry_ loc (lstat s p)
+ f p
+
+modificationTime :: Ptr CStat -> IO ClockTime
+modificationTime stat = do
+ mtime <- st_mtime stat
+ let realToInteger = round . realToFrac :: Real a => a -> Integer
+ return (TOD (realToInteger (mtime :: CTime)) 0)
+
+isDirectory :: Ptr CStat -> IO Bool
+isDirectory stat = do
+ mode <- st_mode stat
+ return (s_isdir mode)
+
+fileNameEndClean :: String -> String
+fileNameEndClean name = if isDrive name then addTrailingPathSeparator name
+ else dropTrailingPathSeparator name
+
+foreign import ccall unsafe "HsDirectory.h __hscore_R_OK" r_OK :: CInt
+foreign import ccall unsafe "HsDirectory.h __hscore_W_OK" w_OK :: CInt
+foreign import ccall unsafe "HsDirectory.h __hscore_X_OK" x_OK :: CInt
+
+foreign import ccall unsafe "HsDirectory.h __hscore_S_IRUSR" s_IRUSR :: CMode
+foreign import ccall unsafe "HsDirectory.h __hscore_S_IWUSR" s_IWUSR :: CMode
+foreign import ccall unsafe "HsDirectory.h __hscore_S_IXUSR" s_IXUSR :: CMode
+foreign import ccall unsafe "__hscore_S_IFDIR" s_IFDIR :: CMode
+#endif
+
+
+#ifdef __GLASGOW_HASKELL__
+foreign import ccall unsafe "__hscore_long_path_size"
+ long_path_size :: Int
+#else
+long_path_size :: Int
+long_path_size = 2048 -- // guess?
+#endif /* __GLASGOW_HASKELL__ */