{-# LINE 1 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LANGUAGE CApiFFI #-}
{-# LINE 3 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LANGUAGE Safe #-}
{-# LINE 7 "libraries/unix/System/Posix/Temp.hsc" #-}
module System.Posix.Temp (
mkstemp, mkstemps, mkdtemp
) where
import Foreign.C
import System.IO
{-# LINE 33 "libraries/unix/System/Posix/Temp.hsc" #-}
import System.Posix.IO
import System.Posix.Types
import System.Posix.Internals (withFilePath, peekFilePath)
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 (m :: * -> *) a. Monad m => a -> m a
return (String
name, Handle
h)
{-# LINE 57 "libraries/unix/System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkstemps"
c_mkstemps :: CString -> CInt -> IO CInt
{-# LINE 60 "libraries/unix/System/Posix/Temp.hsc" #-}
mkstemps :: String -> String -> IO (FilePath, Handle)
mkstemps :: String -> String -> IO (String, Handle)
{-# LINE 72 "libraries/unix/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 83 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LINE 85 "libraries/unix/System/Posix/Temp.hsc" #-}
foreign import capi unsafe "HsUnix.h mkdtemp"
c_mkdtemp :: CString -> IO CString
{-# LINE 88 "libraries/unix/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 100 "libraries/unix/System/Posix/Temp.hsc" #-}
withFilePath template $ \ ptr -> do
_ <- throwErrnoIfNull "mkdtemp" (c_mkdtemp ptr)
name <- peekFilePath ptr
return name
{-# LINE 109 "libraries/unix/System/Posix/Temp.hsc" #-}
{-# LINE 124 "libraries/unix/System/Posix/Temp.hsc" #-}