-{-# OPTIONS -fno-implicit-prelude #-}
+{-# OPTIONS_GHC -fno-implicit-prelude #-}
-----------------------------------------------------------------------------
-- |
-- #hide
module System.Posix.Internals where
-#include "config.h"
+#include "ghcconfig.h"
+#include "HsBaseConfig.h"
import Control.Monad
import System.Posix.Types
#ifdef __HUGS__
import Hugs.Prelude (IOException(..), IOErrorType(..))
-{-# CBITS PrelIOUtils.c dirUtils.c consUtils.c #-}
+{-# CFILES cbits/PrelIOUtils.c cbits/dirUtils.c cbits/consUtils.c #-}
ioException = ioError
#endif
ioe_unknownfiletype = IOError Nothing UnsupportedOperation "fdType"
"unknown file type" Nothing
--- It isn't clear whether ftruncate is POSIX or not (I've read several
--- manpages and they seem to conflict), so we truncate using open/2.
-fileTruncate :: FilePath -> IO ()
-fileTruncate file = do
- let flags = o_WRONLY .|. o_TRUNC
- withCString file $ \file_cstr -> do
- fd <- fromIntegral `liftM`
- throwErrnoIfMinus1Retry "fileTruncate"
- (c_open file_cstr (fromIntegral flags) 0o666)
- c_close fd
- return ()
-
#if defined(mingw32_TARGET_OS) || defined(__MINGW32__)
closeFd :: Bool -> CInt -> IO CInt
closeFd isStream fd
fdGetMode :: Int -> IO IOMode
fdGetMode fd = do
#if defined(mingw32_TARGET_OS) || defined(__MINGW32__)
+ -- XXX: this code is *BROKEN*, _setmode only deals with O_TEXT/O_BINARY
flags1 <- throwErrnoIfMinus1Retry "fdGetMode"
(c__setmode (fromIntegral fd) (fromIntegral o_WRONLY))
flags <- throwErrnoIfMinus1Retry "fdGetMode"
-- An error when setting O_NONBLOCK isn't fatal: on some systems
-- there are certain file handles on which this will fail (eg. /dev/null
-- on FreeBSD) so we throw away the return code from fcntl_write.
- c_fcntl_write (fromIntegral fd) const_f_setfl (flags .|. o_NONBLOCK)
+ unless (testBit flags (fromIntegral o_NONBLOCK)) $ do
+ c_fcntl_write (fromIntegral fd) const_f_setfl (flags .|. o_NONBLOCK)
+ return ()
#else
-- bogus defns for win32
foreign import ccall unsafe "HsBase.h dup2"
c_dup2 :: CInt -> CInt -> IO CInt
-foreign import ccall unsafe "HsBase.h fstat"
+foreign import ccall unsafe "HsBase.h __hscore_fstat"
c_fstat :: CInt -> Ptr CStat -> IO CInt
foreign import ccall unsafe "HsBase.h getcwd"
foreign import ccall unsafe "HsBase.h isatty"
c_isatty :: CInt -> IO CInt
-foreign import ccall unsafe "HsBase.h lseek"
+foreign import ccall unsafe "HsBase.h __hscore_lseek"
c_lseek :: CInt -> COff -> CInt -> IO COff
foreign import ccall unsafe "HsBase.h __hscore_lstat"
lstat :: CString -> Ptr CStat -> IO CInt
-foreign import ccall unsafe "HsBase.h open"
+foreign import ccall unsafe "HsBase.h __hscore_open"
c_open :: CString -> CInt -> CMode -> IO CInt
foreign import ccall unsafe "HsBase.h opendir"
foreign import ccall unsafe "HsBase.h read"
c_read :: CInt -> Ptr CChar -> CSize -> IO CSsize
-foreign import ccall unsafe "HsBase.h readdir"
- c_readdir :: Ptr CDir -> IO (Ptr CDirent)
-
foreign import ccall unsafe "dirUtils.h __hscore_renameFile"
c_rename :: CString -> CString -> IO CInt
foreign import ccall unsafe "HsBase.h rmdir"
c_rmdir :: CString -> IO CInt
-foreign import ccall unsafe "HsBase.h stat"
+foreign import ccall unsafe "HsBase.h __hscore_stat"
c_stat :: CString -> Ptr CStat -> IO CInt
foreign import ccall unsafe "HsBase.h umask"
foreign import ccall unsafe "HsBase.h write"
c_write :: CInt -> Ptr CChar -> CSize -> IO CSsize
+foreign import ccall unsafe "HsBase.h __hscore_ftruncate"
+ c_ftruncate :: CInt -> COff -> IO CInt
+
foreign import ccall unsafe "HsBase.h unlink"
c_unlink :: CString -> IO CInt
+foreign import ccall unsafe "HsBase.h getpid"
+ c_getpid :: IO CPid
+
#if !defined(mingw32_TARGET_OS) && !defined(__MINGW32__)
foreign import ccall unsafe "HsBase.h fcntl"
c_fcntl_read :: CInt -> CInt -> IO CInt
foreign import ccall unsafe "HsBase.h fork"
c_fork :: IO CPid
-foreign import ccall unsafe "HsBase.h getpid"
- c_getpid :: IO CPid
-
foreign import ccall unsafe "HsBase.h link"
c_link :: CString -> CString -> IO CInt