{-# LANGUAGE Trustworthy #-}
{-# LANGUAGE CPP, NoImplicitPrelude, CApiFFI #-}
module System.IO (
IO,
fixIO,
FilePath,
Handle,
stdin, stdout, stderr,
withFile,
openFile,
IOMode(ReadMode,WriteMode,AppendMode,ReadWriteMode),
hClose,
readFile,
readFile',
writeFile,
appendFile,
hFileSize,
hSetFileSize,
hIsEOF,
isEOF,
BufferMode(NoBuffering,LineBuffering,BlockBuffering),
hSetBuffering,
hGetBuffering,
hFlush,
hGetPosn,
hSetPosn,
HandlePosn,
hSeek,
SeekMode(AbsoluteSeek,RelativeSeek,SeekFromEnd),
hTell,
hIsOpen, hIsClosed,
hIsReadable, hIsWritable,
hIsSeekable,
hIsTerminalDevice,
hSetEcho,
hGetEcho,
hShow,
hWaitForInput,
hReady,
hGetChar,
hGetLine,
hLookAhead,
hGetContents,
hGetContents',
hPutChar,
hPutStr,
hPutStrLn,
hPrint,
interact,
putChar,
putStr,
putStrLn,
print,
getChar,
getLine,
getContents,
getContents',
readIO,
readLn,
withBinaryFile,
openBinaryFile,
hSetBinaryMode,
hPutBuf,
hGetBuf,
hGetBufSome,
hPutBufNonBlocking,
hGetBufNonBlocking,
openTempFile,
openBinaryTempFile,
openTempFileWithDefaultPermissions,
openBinaryTempFileWithDefaultPermissions,
hSetEncoding,
hGetEncoding,
TextEncoding,
latin1,
utf8, utf8_bom,
utf16, utf16le, utf16be,
utf32, utf32le, utf32be,
localeEncoding,
char8,
mkTextEncoding,
hSetNewlineMode,
Newline(..), nativeNewline,
NewlineMode(..),
noNewlineTranslation, universalNewlineMode, nativeNewlineMode,
) where
import Control.Exception.Base
import Data.Bits
import Data.Maybe
import Foreign.C.Error
#if defined(mingw32_HOST_OS)
import Foreign.C.String
import Foreign.Ptr
import Foreign.Marshal.Alloc
import Foreign.Marshal.Utils (with)
import Foreign.Storable
import GHC.IO.SubSystem
import GHC.IO.Windows.Handle (openFileAsTemp)
import GHC.IO.Handle.Windows (mkHandleFromHANDLE)
import GHC.IO.Device as IODevice
import GHC.Real (fromIntegral)
#endif
import Foreign.C.Types
import System.Posix.Internals
import System.Posix.Types
import GHC.Base
import GHC.List
#if !defined(mingw32_HOST_OS)
import GHC.IORef
#endif
import GHC.Num
import GHC.IO hiding ( bracket, onException )
import GHC.IO.IOMode
import qualified GHC.IO.FD as FD
import GHC.IO.Handle
import qualified GHC.IO.Handle.FD as POSIX
import GHC.IO.Handle.Text ( hGetBufSome, hPutStrLn )
import GHC.IO.Exception ( userError )
import GHC.IO.Encoding
import Text.Read
import GHC.IO.StdHandles
import GHC.Show
import GHC.MVar
putChar :: Char -> IO ()
putChar :: Char -> IO ()
putChar Char
c = Handle -> Char -> IO ()
hPutChar Handle
stdout Char
c
putStr :: String -> IO ()
putStr :: String -> IO ()
putStr String
s = Handle -> String -> IO ()
hPutStr Handle
stdout String
s
putStrLn :: String -> IO ()
putStrLn :: String -> IO ()
putStrLn String
s = Handle -> String -> IO ()
hPutStrLn Handle
stdout String
s
print :: Show a => a -> IO ()
print :: forall a. Show a => a -> IO ()
print a
x = String -> IO ()
putStrLn (a -> String
forall a. Show a => a -> String
show a
x)
getChar :: IO Char
getChar :: IO Char
getChar = Handle -> IO Char
hGetChar Handle
stdin
getLine :: IO String
getLine :: IO String
getLine = Handle -> IO String
hGetLine Handle
stdin
getContents :: IO String
getContents :: IO String
getContents = Handle -> IO String
hGetContents Handle
stdin
getContents' :: IO String
getContents' :: IO String
getContents' = Handle -> IO String
hGetContents' Handle
stdin
interact :: (String -> String) -> IO ()
interact :: (String -> String) -> IO ()
interact String -> String
f = do String
s <- IO String
getContents
String -> IO ()
putStr (String -> String
f String
s)
readFile :: FilePath -> IO String
readFile :: String -> IO String
readFile String
name = String -> IOMode -> IO Handle
openFile String
name IOMode
ReadMode IO Handle -> (Handle -> IO String) -> IO String
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Handle -> IO String
hGetContents
readFile' :: FilePath -> IO String
readFile' :: String -> IO String
readFile' String
name = String -> IOMode -> (Handle -> IO String) -> IO String
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
name IOMode
ReadMode Handle -> IO String
hGetContents'
writeFile :: FilePath -> String -> IO ()
writeFile :: String -> String -> IO ()
writeFile String
f String
txt = String -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
f IOMode
WriteMode (\ Handle
hdl -> Handle -> String -> IO ()
hPutStr Handle
hdl String
txt)
appendFile :: FilePath -> String -> IO ()
appendFile :: String -> String -> IO ()
appendFile String
f String
txt = String -> IOMode -> (Handle -> IO ()) -> IO ()
forall r. String -> IOMode -> (Handle -> IO r) -> IO r
withFile String
f IOMode
AppendMode (\ Handle
hdl -> Handle -> String -> IO ()
hPutStr Handle
hdl String
txt)
readLn :: Read a => IO a
readLn :: forall a. Read a => IO a
readLn = IO String
getLine IO String -> (String -> IO a) -> IO a
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> IO a
forall a. Read a => String -> IO a
readIO
readIO :: Read a => String -> IO a
readIO :: forall a. Read a => String -> IO a
readIO String
s = case (do { (a
x,String
t) <- ReadS a
forall a. Read a => ReadS a
reads String
s ;
(String
"",String
"") <- ReadS String
lex String
t ;
a -> [a]
forall a. a -> [a]
forall (m :: * -> *) a. Monad m => a -> m a
return a
x }) of
[a
x] -> a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
x
[] -> IOError -> IO a
forall a. IOError -> IO a
ioError (String -> IOError
userError String
"Prelude.readIO: no parse")
[a]
_ -> IOError -> IO a
forall a. IOError -> IO a
ioError (String -> IOError
userError String
"Prelude.readIO: ambiguous parse")
localeEncoding :: TextEncoding
localeEncoding :: TextEncoding
localeEncoding = TextEncoding
initLocaleEncoding
hReady :: Handle -> IO Bool
hReady :: Handle -> IO Bool
hReady Handle
h = Handle -> Int -> IO Bool
hWaitForInput Handle
h Int
0
hPrint :: Show a => Handle -> a -> IO ()
hPrint :: forall a. Show a => Handle -> a -> IO ()
hPrint Handle
hdl = Handle -> String -> IO ()
hPutStrLn Handle
hdl (String -> IO ()) -> (a -> String) -> a -> IO ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> String
forall a. Show a => a -> String
show
fixIO :: (a -> IO a) -> IO a
fixIO :: forall a. (a -> IO a) -> IO a
fixIO a -> IO a
k = do
MVar a
m <- IO (MVar a)
forall a. IO (MVar a)
newEmptyMVar
a
ans <- IO a -> IO a
forall a. IO a -> IO a
unsafeDupableInterleaveIO
(MVar a -> IO a
forall a. MVar a -> IO a
readMVar MVar a
m IO a -> (BlockedIndefinitelyOnMVar -> IO a) -> IO a
forall e a. Exception e => IO a -> (e -> IO a) -> IO a
`catch` \BlockedIndefinitelyOnMVar
BlockedIndefinitelyOnMVar ->
FixIOException -> IO a
forall e a. Exception e => e -> IO a
throwIO FixIOException
FixIOException)
a
result <- a -> IO a
k a
ans
MVar a -> a -> IO ()
forall a. MVar a -> a -> IO ()
putMVar MVar a
m a
result
a -> IO a
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return a
result
openTempFile :: FilePath
-> String
-> IO (FilePath, Handle)
openTempFile :: String -> String -> IO (String, Handle)
openTempFile String
tmp_dir String
template
= String -> String -> String -> Bool -> CMode -> IO (String, Handle)
openTempFile' String
"openTempFile" String
tmp_dir String
template Bool
False CMode
0o600
openBinaryTempFile :: FilePath -> String -> IO (FilePath, Handle)
openBinaryTempFile :: String -> String -> IO (String, Handle)
openBinaryTempFile String
tmp_dir String
template
= String -> String -> String -> Bool -> CMode -> IO (String, Handle)
openTempFile' String
"openBinaryTempFile" String
tmp_dir String
template Bool
True CMode
0o600
openTempFileWithDefaultPermissions :: FilePath -> String
-> IO (FilePath, Handle)
openTempFileWithDefaultPermissions :: String -> String -> IO (String, Handle)
openTempFileWithDefaultPermissions String
tmp_dir String
template
= String -> String -> String -> Bool -> CMode -> IO (String, Handle)
openTempFile' String
"openTempFileWithDefaultPermissions" String
tmp_dir String
template Bool
False CMode
0o666
openBinaryTempFileWithDefaultPermissions :: FilePath -> String
-> IO (FilePath, Handle)
openBinaryTempFileWithDefaultPermissions :: String -> String -> IO (String, Handle)
openBinaryTempFileWithDefaultPermissions String
tmp_dir String
template
= String -> String -> String -> Bool -> CMode -> IO (String, Handle)
openTempFile' String
"openBinaryTempFileWithDefaultPermissions" String
tmp_dir String
template Bool
True CMode
0o666
openTempFile' :: String -> FilePath -> String -> Bool -> CMode
-> IO (FilePath, Handle)
openTempFile' :: String -> String -> String -> Bool -> CMode -> IO (String, Handle)
openTempFile' String
loc String
tmp_dir String
template Bool
binary CMode
mode
| String -> Bool
pathSeparator String
template
= String -> IO (String, Handle)
forall a. String -> IO a
failIO (String -> IO (String, Handle)) -> String -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ String
"openTempFile': Template string must not contain path separator characters: "String -> String -> String
forall a. [a] -> [a] -> [a]
++String
template
| Bool
otherwise = IO (String, Handle)
findTempName
where
(String
prefix, String
suffix) =
case (Char -> Bool) -> String -> (String, String)
forall a. (a -> Bool) -> [a] -> ([a], [a])
break (Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'.') (String -> (String, String)) -> String -> (String, String)
forall a b. (a -> b) -> a -> b
$ String -> String
forall a. [a] -> [a]
reverse String
template of
(String
rev_suffix, String
"") -> (String -> String
forall a. [a] -> [a]
reverse String
rev_suffix, String
"")
(String
rev_suffix, Char
'.':String
rest) -> (String -> String
forall a. [a] -> [a]
reverse String
rest, Char
'.'Char -> String -> String
forall a. a -> [a] -> [a]
:String -> String
forall a. [a] -> [a]
reverse String
rev_suffix)
(String, String)
_ -> String -> (String, String)
forall a. String -> a
errorWithoutStackTrace String
"bug in System.IO.openTempFile"
#if defined(mingw32_HOST_OS)
findTempName :: IO (String, Handle)
findTempName = IO (String, Handle)
findTempNamePosix IO (String, Handle) -> IO (String, Handle) -> IO (String, Handle)
forall a. a -> a -> a
<!> IO (String, Handle)
findTempNameWinIO
findTempNameWinIO :: IO (String, Handle)
findTempNameWinIO = do
let label :: String
label = if String -> Bool
forall a. [a] -> Bool
null String
prefix then String
"ghc" else String
prefix
String
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withCWString String
tmp_dir ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_tmp_dir ->
String
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withCWString String
label ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_template ->
String
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withCWString String
suffix ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_suffix ->
Ptr CWchar
-> (Ptr (Ptr CWchar) -> IO (String, Handle)) -> IO (String, Handle)
forall a b. Storable a => a -> (Ptr a -> IO b) -> IO b
with Ptr CWchar
forall a. Ptr a
nullPtr ((Ptr (Ptr CWchar) -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr (Ptr CWchar) -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr (Ptr CWchar)
c_ptr -> do
Bool
res <- Ptr CWchar
-> Ptr CWchar -> Ptr CWchar -> Ptr (Ptr CWchar) -> IO Bool
c_createUUIDTempFileErrNo Ptr CWchar
c_tmp_dir Ptr CWchar
c_template Ptr CWchar
c_suffix Ptr (Ptr CWchar)
c_ptr
if Bool -> Bool
not Bool
res
then do Errno
errno <- IO Errno
getErrno
IOError -> IO (String, Handle)
forall a. IOError -> IO a
ioError (String -> Errno -> Maybe Handle -> Maybe String -> IOError
errnoToIOError String
loc Errno
errno Maybe Handle
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just String
tmp_dir))
else do Ptr CWchar
c_p <- Ptr (Ptr CWchar) -> IO (Ptr CWchar)
forall a. Storable a => Ptr a -> IO a
peek Ptr (Ptr CWchar)
c_ptr
String
filename <- Ptr CWchar -> IO String
peekCWString Ptr CWchar
c_p
Ptr CWchar -> IO ()
forall a. Ptr a -> IO ()
free Ptr CWchar
c_p
let flags :: CInt
flags = CMode -> CInt
forall a b. (Integral a, Num b) => a -> b
fromIntegral CMode
mode CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.&. CInt
o_EXCL
String -> Bool -> IO (String, Handle)
handleResultsWinIO String
filename (CInt
flags CInt -> CInt -> Bool
forall a. Eq a => a -> a -> Bool
== CInt
o_EXCL)
findTempNamePosix :: IO (String, Handle)
findTempNamePosix = do
let label :: String
label = if String -> Bool
forall a. [a] -> Bool
null String
prefix then String
"ghc" else String
prefix
String
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withCWString String
tmp_dir ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_tmp_dir ->
String
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withCWString String
label ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_template ->
String
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withCWString String
suffix ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_suffix ->
Int -> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. Int -> (Ptr a -> IO b) -> IO b
allocaBytes (CWchar -> Int
forall a. Storable a => a -> Int
sizeOf (CWchar
forall a. HasCallStack => a
undefined :: CWchar) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
260) ((Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle))
-> (Ptr CWchar -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \Ptr CWchar
c_str -> do
Bool
res <- Ptr CWchar
-> Ptr CWchar -> Ptr CWchar -> CUInt -> Ptr CWchar -> IO Bool
c_getTempFileNameErrorNo Ptr CWchar
c_tmp_dir Ptr CWchar
c_template Ptr CWchar
c_suffix CUInt
0
Ptr CWchar
c_str
if Bool -> Bool
not Bool
res
then do Errno
errno <- IO Errno
getErrno
IOError -> IO (String, Handle)
forall a. IOError -> IO a
ioError (String -> Errno -> Maybe Handle -> Maybe String -> IOError
errnoToIOError String
loc Errno
errno Maybe Handle
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just String
tmp_dir))
else do String
filename <- Ptr CWchar -> IO String
peekCWString Ptr CWchar
c_str
String -> IO (String, Handle)
handleResultsPosix String
filename
handleResultsPosix :: String -> IO (String, Handle)
handleResultsPosix String
filename = do
let oflags1 :: CInt
oflags1 = CInt
rw_flags CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.|. CInt
o_EXCL
binary_flags :: CInt
binary_flags
| Bool
binary = CInt
o_BINARY
| Bool
otherwise = CInt
0
oflags :: CInt
oflags = CInt
oflags1 CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.|. CInt
binary_flags
CInt
fd <- String -> (Ptr CWchar -> IO CInt) -> IO CInt
forall a. String -> (Ptr CWchar -> IO a) -> IO a
withFilePath String
filename ((Ptr CWchar -> IO CInt) -> IO CInt)
-> (Ptr CWchar -> IO CInt) -> IO CInt
forall a b. (a -> b) -> a -> b
$ \ Ptr CWchar
f -> Ptr CWchar -> CInt -> CMode -> IO CInt
c_open Ptr CWchar
f CInt
oflags CMode
mode
case CInt
fd CInt -> CInt -> Bool
forall a. Ord a => a -> a -> Bool
< CInt
0 of
Bool
True -> do Errno
errno <- IO Errno
getErrno
IOError -> IO (String, Handle)
forall a. IOError -> IO a
ioError (String -> Errno -> Maybe Handle -> Maybe String -> IOError
errnoToIOError String
loc Errno
errno Maybe Handle
forall a. Maybe a
Nothing (String -> Maybe String
forall a. a -> Maybe a
Just String
tmp_dir))
Bool
False ->
do (FD
fD,IODeviceType
fd_type) <- CInt
-> IOMode
-> Maybe (IODeviceType, CDev, CIno)
-> Bool
-> Bool
-> IO (FD, IODeviceType)
FD.mkFD CInt
fd IOMode
ReadWriteMode Maybe (IODeviceType, CDev, CIno)
forall a. Maybe a
Nothing
Bool
False
Bool
True
TextEncoding
enc <- IO TextEncoding
getLocaleEncoding
Handle
h <- FD
-> IODeviceType
-> String
-> IOMode
-> Bool
-> Maybe TextEncoding
-> IO Handle
POSIX.mkHandleFromFD FD
fD IODeviceType
fd_type String
filename IOMode
ReadWriteMode
Bool
False (TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just TextEncoding
enc)
(String, Handle) -> IO (String, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (String
filename, Handle
h)
handleResultsWinIO :: String -> Bool -> IO (String, Handle)
handleResultsWinIO String
filename Bool
excl = do
(Io NativeHandle
hwnd, IODeviceType
hwnd_type) <- String -> Bool -> Bool -> IO (Io NativeHandle, IODeviceType)
openFileAsTemp String
filename Bool
True Bool
excl
Maybe TextEncoding
mb_codec <- if Bool
binary then Maybe TextEncoding -> IO (Maybe TextEncoding)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe TextEncoding
forall a. Maybe a
Nothing else (TextEncoding -> Maybe TextEncoding)
-> IO TextEncoding -> IO (Maybe TextEncoding)
forall a b. (a -> b) -> IO a -> IO b
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap TextEncoding -> Maybe TextEncoding
forall a. a -> Maybe a
Just IO TextEncoding
getLocaleEncoding
Handle
h <- Io NativeHandle
-> IODeviceType
-> String
-> IOMode
-> Maybe TextEncoding
-> IO Handle
forall dev.
(RawIO dev, IODevice dev, BufferedIO dev, Typeable dev) =>
dev
-> IODeviceType
-> String
-> IOMode
-> Maybe TextEncoding
-> IO Handle
mkHandleFromHANDLE Io NativeHandle
hwnd IODeviceType
hwnd_type String
filename IOMode
ReadWriteMode Maybe TextEncoding
mb_codec
IO Handle -> IO () -> IO Handle
forall a b. IO a -> IO b -> IO a
`onException` Io NativeHandle -> IO ()
forall a. IODevice a => a -> IO ()
IODevice.close Io NativeHandle
hwnd
(String, Handle) -> IO (String, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (String
filename, Handle
h)
foreign import ccall "getTempFileNameErrorNo" c_getTempFileNameErrorNo
:: CWString -> CWString -> CWString -> CUInt -> Ptr CWchar -> IO Bool
foreign import ccall "__createUUIDTempFileErrNo" c_createUUIDTempFileErrNo
:: CWString -> CWString -> CWString -> Ptr CWString -> IO Bool
pathSeparator :: String -> Bool
pathSeparator :: String -> Bool
pathSeparator String
template = (Char -> Bool) -> String -> Bool
forall a. (a -> Bool) -> [a] -> Bool
any (\Char
x-> Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'/' Bool -> Bool -> Bool
|| Char
x Char -> Char -> Bool
forall a. Eq a => a -> a -> Bool
== Char
'\\') String
template
output_flags :: CInt
output_flags = CInt
std_flags
#else /* else mingw32_HOST_OS */
findTempName = do
rs <- rand_string
let filename = prefix ++ rs ++ suffix
filepath = tmp_dir `combine` filename
r <- openNewFile filepath binary mode
case r of
FileExists -> findTempName
OpenNewError errno -> ioError (errnoToIOError loc errno Nothing (Just tmp_dir))
NewFileCreated fd -> do
(fD,fd_type) <- FD.mkFD fd ReadWriteMode Nothing
False
True
enc <- getLocaleEncoding
h <- POSIX.mkHandleFromFD fD fd_type filepath ReadWriteMode False (Just enc)
return (filepath, h)
where
combine a b
| null b = a
| null a = b
| pathSeparator [last a] = a ++ b
| otherwise = a ++ [pathSeparatorChar] ++ b
tempCounter :: IORef Int
tempCounter = unsafePerformIO $ newIORef 0
{-# NOINLINE tempCounter #-}
rand_string :: IO String
rand_string = do
r1 <- c_getpid
(r2, _) <- atomicModifyIORef'_ tempCounter (+1)
return $ show r1 ++ "-" ++ show r2
data OpenNewFileResult
= NewFileCreated CInt
| FileExists
| OpenNewError Errno
openNewFile :: FilePath -> Bool -> CMode -> IO OpenNewFileResult
openNewFile filepath binary mode = do
let oflags1 = rw_flags .|. o_EXCL
binary_flags
| binary = o_BINARY
| otherwise = 0
oflags = oflags1 .|. binary_flags
fd <- withFilePath filepath $ \ f ->
c_open f oflags mode
if fd < 0
then do
errno <- getErrno
case errno of
_ | errno == eEXIST -> return FileExists
_ -> return (OpenNewError errno)
else return (NewFileCreated fd)
pathSeparatorChar :: Char
pathSeparatorChar = '/'
pathSeparator :: String -> Bool
pathSeparator template = pathSeparatorChar `elem` template
output_flags = std_flags .|. o_CREAT
#endif /* mingw32_HOST_OS */
std_flags, output_flags, rw_flags :: CInt
std_flags :: CInt
std_flags = CInt
o_NONBLOCK CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.|. CInt
o_NOCTTY
rw_flags :: CInt
rw_flags = CInt
output_flags CInt -> CInt -> CInt
forall a. Bits a => a -> a -> a
.|. CInt
o_RDWR