{-# LINE 1 "System/Posix/Temp.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LANGUAGE Safe #-}
module System.Posix.Temp (
mkstemp, mkstemps, mkdtemp
) where
import Foreign.C
import System.IO
{-# LINE 29 "System/Posix/Temp.hsc" #-}
import System.Posix.IO
import System.Posix.Types
import System.Posix.Internals (withFilePath, peekFilePath)
{-# LINE 37 "System/Posix/Temp.hsc" #-}
{-# LINE 45 "System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemp"
c_mkstemp :: CString -> IO CInt
mkstemp :: String -> IO (FilePath, Handle)
mkstemp :: String -> IO (String, Handle)
mkstemp String
template' = do
let template :: String
template = String
template' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"XXXXXX"
String -> (CString -> IO (String, Handle)) -> IO (String, Handle)
forall a. String -> (CString -> IO a) -> IO a
withFilePath String
template ((CString -> IO (String, Handle)) -> IO (String, Handle))
-> (CString -> IO (String, Handle)) -> IO (String, Handle)
forall a b. (a -> b) -> a -> b
$ \ CString
ptr -> do
CInt
fd <- String -> IO CInt -> IO CInt
forall a. (Eq a, Num a) => String -> IO a -> IO a
throwErrnoIfMinus1 String
"mkstemp" (CString -> IO CInt
c_mkstemp CString
ptr)
String
name <- CString -> IO String
peekFilePath CString
ptr
Handle
h <- Fd -> IO Handle
fdToHandle (CInt -> Fd
Fd CInt
fd)
(String, Handle) -> IO (String, Handle)
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return (String
name, Handle
h)
{-# LINE 66 "System/Posix/Temp.hsc" #-}
{-# LINE 68 "System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 71 "System/Posix/Temp.hsc" #-}
mkstemps :: String -> String -> IO (FilePath, Handle)
mkstemps :: String -> String -> IO (String, Handle)
{-# LINE 83 "System/Posix/Temp.hsc" #-}
mkstemps prefix suffix = do
let template = prefix ++ "XXXXXX" ++ suffix
lenOfsuf = (fromIntegral $ length suffix) :: CInt
withFilePath template $ \ ptr -> do
fd <- throwErrnoIfMinus1 "mkstemps" (c_mkstemps ptr lenOfsuf)
name <- peekFilePath ptr
h <- fdToHandle (Fd fd)
return (name, h)
{-# LINE 95 "System/Posix/Temp.hsc" #-}
{-# LINE 97 "System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 100 "System/Posix/Temp.hsc" #-}
mkdtemp :: String -> IO FilePath
mkdtemp :: String -> IO String
mkdtemp String
template' = do
let template :: String
template = String
template' String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"XXXXXX"
{-# LINE 112 "System/Posix/Temp.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 121 "System/Posix/Temp.hsc" #-}
{-# LINE 136 "System/Posix/Temp.hsc" #-}