+setCurrentDirectory path =
+#ifdef mingw32_HOST_OS
+ Win32.setCurrentDirectory path
+#else
+ Posix.changeWorkingDirectory path
+#endif
+
+#endif /* __GLASGOW_HASKELL__ */
+
+#ifdef __GLASGOW_HASKELL__
+{- |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.isDirectory stat))
+#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 (not (Posix.isDirectory stat)))
+#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 mod_time :: Posix.EpochTime
+ mod_time = Posix.modificationTime stat
+ dbl_time :: Double
+ dbl_time = realToFrac mod_time
+ return (TOD (round dbl_time) 0)
+#endif
+ -- For info
+ -- round :: (RealFrac a, Integral b => a -> b
+ -- realToFrac :: (Real a, Fractional b) => a -> b
+
+#endif /* __GLASGOW_HASKELL__ */
+
+#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 dbl_time :: Double
+ dbl_time = realToFrac (mtime :: CTime)
+ return (TOD (round dbl_time) 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_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__ */