-- -----------------------------------------------------------------------------
--- $Id: Directory.hsc,v 1.5 2001/02/13 15:23:33 rrt Exp $
+-- $Id: Directory.hsc,v 1.14 2001/08/10 13:48:06 simonmar Exp $
--
-- (c) The University of Glasgow, 1994-2000
--
import Time ( ClockTime(..) )
+import PrelPosix
import PrelStorable
import PrelCString
import PrelMarshalAlloc
import PrelCTypesISO
import PrelCTypes
-import PrelPosixTypes
import PrelCError
import PrelPtr
import PrelIOBase
import PrelBase
+#include "config.h"
#include <sys/stat.h>
#include <dirent.h>
#include <limits.h>
createDirectory :: FilePath -> IO ()
createDirectory path = do
- withUnsafeCString path $ \s -> do
+ withCString path $ \s -> do
throwErrnoIfMinus1Retry_ "createDirectory" $
#if defined(mingw32_TARGET_OS)
mkdir s
removeDirectory :: FilePath -> IO ()
removeDirectory path = do
- withUnsafeCString path $ \s ->
+ withCString path $ \s ->
throwErrnoIfMinus1Retry_ "removeDirectory" (rmdir s)
{-
removeFile :: FilePath -> IO ()
removeFile path = do
- withUnsafeCString path $ \s ->
+ withCString path $ \s ->
throwErrnoIfMinus1Retry_ "removeFile" (unlink s)
{-
("not a directory") (Just opath))
else do
- withUnsafeCString opath $ \s1 ->
- withUnsafeCString npath $ \s2 ->
+ withCString opath $ \s1 ->
+ withCString npath $ \s2 ->
throwErrnoIfMinus1Retry_ "renameDirectory" (rename s1 s2)
{-
"is a directory" (Just opath))
else do
- withUnsafeCString opath $ \s1 ->
- withUnsafeCString npath $ \s2 ->
+ withCString opath $ \s1 ->
+ withCString npath $ \s2 ->
throwErrnoIfMinus1Retry_ "renameFile" (rename s1 s2)
{-
getDirectoryContents :: FilePath -> IO [FilePath]
getDirectoryContents path = do
- p <- withUnsafeCString path $ \s ->
+ p <- withCString path $ \s ->
throwErrnoIfNullRetry "getDirectoryContents" (opendir s)
loop p
where
resetErrno
p <- readdir dir
if (p /= nullPtr)
- then do entry <- peekCString ((#ptr struct dirent,d_name) p)
+ then do
+#ifndef mingw32_TARGET_OS
+ entry <- peekCString ((#ptr struct dirent,d_name) p)
+#else
+ entryp <- (#peek struct dirent,d_name) p
+ entry <- peekCString entryp -- on mingwin it's a char *, not a char []
+#endif
entries <- loop dir
return (entry:entries)
else do errno <- getErrno
if (errno == eINTR) then loop dir else do
throwErrnoIfMinus1_ "getDirectoryContents" $ closedir dir
+#ifndef mingw32_TARGET_OS
if (errno == eOK)
+#else
+ if (errno == eNOENT) -- mingwin (20001111) cunningly sets errno to ENOENT when it runs out of files
+#endif
then return []
else throwErrno "getDirectoryContents"
setCurrentDirectory :: FilePath -> IO ()
setCurrentDirectory path = do
- withUnsafeCString path $ \s ->
+ withCString path $ \s ->
throwErrnoIfMinus1Retry_ "setCurrentDirectory" (chdir s)
-- ToDo: add path to error
getPermissions :: FilePath -> IO Permissions
getPermissions name = do
- withUnsafeCString name $ \s -> do
+ withCString name $ \s -> do
read <- access s (#const R_OK)
write <- access s (#const W_OK)
exec <- access s (#const X_OK)
mode = read `unionCMode` (write `unionCMode` exec)
- withUnsafeCString name $ \s ->
+ withCString name $ \s ->
throwErrnoIfMinus1_ "setPermissions" $ chmod s mode
withFileStatus :: FilePath -> (Ptr CStat -> IO a) -> IO a
withFileStatus name f = do
allocaBytes (#const sizeof(struct stat)) $ \p ->
- withUnsafeCString name $ \s -> do
+ withCString name $ \s -> do
throwErrnoIfMinus1Retry_ "withFileStatus" (stat s p)
f p
unionCMode :: CMode -> CMode -> CMode
unionCMode = (+)
-type UCString = UnsafeCString
-
#if defined(mingw32_TARGET_OS)
-foreign import ccall unsafe mkdir :: UCString -> IO CInt
+foreign import ccall unsafe mkdir :: CString -> IO CInt
#else
-foreign import ccall unsafe mkdir :: UCString -> CInt -> IO CInt
+foreign import ccall unsafe mkdir :: CString -> CInt -> IO CInt
#endif
-foreign import ccall unsafe chmod :: UCString -> CMode -> IO CInt
-foreign import ccall unsafe access :: UCString -> CMode -> IO CInt
-foreign import ccall unsafe rmdir :: UCString -> IO CInt
-foreign import ccall unsafe chdir :: UCString -> IO CInt
+foreign import ccall unsafe chmod :: CString -> CMode -> IO CInt
+foreign import ccall unsafe access :: CString -> CMode -> IO CInt
+foreign import ccall unsafe rmdir :: CString -> IO CInt
+foreign import ccall unsafe chdir :: CString -> IO CInt
foreign import ccall unsafe getcwd :: Ptr CChar -> CInt -> IO (Ptr CChar)
-foreign import ccall unsafe unlink :: UCString -> IO CInt
-foreign import ccall unsafe rename :: UCString -> UCString -> IO CInt
+foreign import ccall unsafe unlink :: CString -> IO CInt
+foreign import ccall unsafe rename :: CString -> CString -> IO CInt
-foreign import ccall unsafe opendir :: UCString -> IO (Ptr CDir)
+foreign import ccall unsafe opendir :: CString -> IO (Ptr CDir)
foreign import ccall unsafe readdir :: Ptr CDir -> IO (Ptr CDirent)
foreign import ccall unsafe closedir :: Ptr CDir -> IO CInt
-foreign import ccall unsafe stat :: UCString -> Ptr CStat -> IO CInt
+foreign import ccall unsafe stat :: CString -> Ptr CStat -> IO CInt
type CDirent = ()
-type CStat = ()